2 * Copyright (c) 2011 Jiri Zarevucky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup libposix
32 /** @file Password handling.
41 static bool entry_read
= false;
43 /* dummy user account */
44 static const struct passwd dummy_pwd
= {
45 .pw_name
= (char *) "user",
48 .pw_dir
= (char *) "/",
49 .pw_shell
= (char *) "/app/bdsh"
53 * Retrieve next broken-down entry from the user database.
55 * Since HelenOS doesn't have user accounts, this always returns
56 * the same made-up entry.
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.
61 struct passwd
*getpwent(void)
68 return (struct passwd
*) &dummy_pwd
;
72 * Rewind the user list.
80 * Ends enumerating and releases all resources. (Noop)
88 * Find an entry by name.
90 * @param name Name of the entry.
91 * @return Either found entry or NULL if no such entry exists.
93 struct passwd
*getpwnam(const char *name
)
97 if (strcmp(name
, "user") != 0) {
101 return (struct passwd
*) &dummy_pwd
;
105 * Find an entry by name, thread safely.
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.
115 int getpwnam_r(const char *name
, struct passwd
*pwd
,
116 char *buffer
, size_t bufsize
, struct passwd
**result
)
118 assert(name
!= NULL
);
120 assert(buffer
!= NULL
);
121 assert(result
!= NULL
);
123 if (strcmp(name
, "user") != 0) {
128 return getpwuid_r(0, pwd
, buffer
, bufsize
, result
);
132 * Find an entry by UID.
134 * @param uid UID of the entry.
135 * @return Either found entry or NULL if no such entry exists.
137 struct passwd
*getpwuid(uid_t uid
)
143 return (struct passwd
*) &dummy_pwd
;
147 * Find an entry by UID, thread safely.
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.
157 int getpwuid_r(uid_t uid
, struct passwd
*pwd
,
158 char *buffer
, size_t bufsize
, struct passwd
**result
)
161 assert(buffer
!= NULL
);
162 assert(result
!= NULL
);
164 static const char bf
[] = { 'u', 's', 'e', 'r', '\0',
165 '/', '\0', 'b', 'd', 's', 'h', '\0' };
171 if (bufsize
< sizeof(bf
)) {
176 memcpy(buffer
, bf
, sizeof(bf
));
178 pwd
->pw_name
= (char *) bf
;
181 pwd
->pw_dir
= (char *) bf
+ 5;
182 pwd
->pw_shell
= (char *) bf
+ 7;
183 *result
= (struct passwd
*) pwd
;