source: mainline/uspace/lib/posix/src/pwd.c@ 1be7bee

Last change on this file since 1be7bee was 9b8be79, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

libposix: Change header organization and remove passthrough headers

Posix headers now function like an overlay. The system include directories
are searched after posix directories. The headers don't need to be patched
for export now. libposix files now include headers using bracket notation
instead of quoted notation.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file Password handling.
33 */
34
35#include <stdbool.h>
36#include <pwd.h>
37#include <string.h>
38#include <errno.h>
39#include <assert.h>
40
41static bool entry_read = false;
42
43/* dummy user account */
44static const struct passwd dummy_pwd = {
45 .pw_name = (char *) "user",
46 .pw_uid = 0,
47 .pw_gid = 0,
48 .pw_dir = (char *) "/",
49 .pw_shell = (char *) "/app/bdsh"
50};
51
52/**
53 * Retrieve next broken-down entry from the user database.
54 *
55 * Since HelenOS doesn't have user accounts, this always returns
56 * the same made-up entry.
57 *
58 * @return Next user database entry or NULL if not possible. Since HelenOS
59 * doesn't have user accounts, this always returns the same made-up entry.
60 */
61struct passwd *getpwent(void)
62{
63 if (entry_read) {
64 return NULL;
65 }
66
67 entry_read = true;
68 return (struct passwd *) &dummy_pwd;
69}
70
71/**
72 * Rewind the user list.
73 */
74void setpwent(void)
75{
76 entry_read = false;
77}
78
79/**
80 * Ends enumerating and releases all resources. (Noop)
81 */
82void endpwent(void)
83{
84 /* noop */
85}
86
87/**
88 * Find an entry by name.
89 *
90 * @param name Name of the entry.
91 * @return Either found entry or NULL if no such entry exists.
92 */
93struct passwd *getpwnam(const char *name)
94{
95 assert(name != NULL);
96
97 if (strcmp(name, "user") != 0) {
98 return NULL;
99 }
100
101 return (struct passwd *) &dummy_pwd;
102}
103
104/**
105 * Find an entry by name, thread safely.
106 *
107 * @param name Name of the entry.
108 * @param pwd Original structure.
109 * @param buffer Buffer for the strings referenced from the result structure.
110 * @param bufsize Length of the buffer.
111 * @param result Where to store updated structure.
112 * @return Zero on success (either found or not found, but without an error),
113 * non-zero error number if error occured.
114 */
115int getpwnam_r(const char *name, struct passwd *pwd,
116 char *buffer, size_t bufsize, struct passwd **result)
117{
118 assert(name != NULL);
119 assert(pwd != NULL);
120 assert(buffer != NULL);
121 assert(result != NULL);
122
123 if (strcmp(name, "user") != 0) {
124 *result = NULL;
125 return 0;
126 }
127
128 return getpwuid_r(0, pwd, buffer, bufsize, result);
129}
130
131/**
132 * Find an entry by UID.
133 *
134 * @param uid UID of the entry.
135 * @return Either found entry or NULL if no such entry exists.
136 */
137struct passwd *getpwuid(uid_t uid)
138{
139 if (uid != 0) {
140 return NULL;
141 }
142
143 return (struct passwd *) &dummy_pwd;
144}
145
146/**
147 * Find an entry by UID, thread safely.
148 *
149 * @param uid UID of the entry.
150 * @param pwd Original structure.
151 * @param buffer Buffer for the strings referenced from the result structure.
152 * @param bufsize Length of the buffer.
153 * @param result Where to store updated structure.
154 * @return Zero on success (either found or not found, but without an error),
155 * non-zero error number if error occured.
156 */
157int getpwuid_r(uid_t uid, struct passwd *pwd,
158 char *buffer, size_t bufsize, struct passwd **result)
159{
160 assert(pwd != NULL);
161 assert(buffer != NULL);
162 assert(result != NULL);
163
164 static const char bf[] = { 'u', 's', 'e', 'r', '\0',
165 '/', '\0', 'b', 'd', 's', 'h', '\0' };
166
167 if (uid != 0) {
168 *result = NULL;
169 return 0;
170 }
171 if (bufsize < sizeof(bf)) {
172 *result = NULL;
173 return ERANGE;
174 }
175
176 memcpy(buffer, bf, sizeof(bf));
177
178 pwd->pw_name = (char *) bf;
179 pwd->pw_uid = 0;
180 pwd->pw_gid = 0;
181 pwd->pw_dir = (char *) bf + 5;
182 pwd->pw_shell = (char *) bf + 7;
183 *result = (struct passwd *) pwd;
184
185 return 0;
186}
187
188/** @}
189 */
Note: See TracBrowser for help on using the repository browser.