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 | #define LIBPOSIX_INTERNAL
|
---|
36 |
|
---|
37 | #include "pwd.h"
|
---|
38 | #include "string.h"
|
---|
39 | #include "errno.h"
|
---|
40 |
|
---|
41 | // TODO: documentation
|
---|
42 |
|
---|
43 | static bool entry_read = false;
|
---|
44 |
|
---|
45 | /* dummy user account */
|
---|
46 | static const struct posix_passwd dummy_pwd = {
|
---|
47 | .pw_name = (char *) "user",
|
---|
48 | .pw_uid = 1,
|
---|
49 | .pw_gid = 1,
|
---|
50 | .pw_dir = (char *) "/",
|
---|
51 | .pw_shell = (char *) "/app/bdsh"
|
---|
52 | };
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Since HelenOS doesn't have user accounts, this always returns
|
---|
56 | * the same made-up entry.
|
---|
57 | *
|
---|
58 | * @return
|
---|
59 | */
|
---|
60 | struct posix_passwd *posix_getpwent(void)
|
---|
61 | {
|
---|
62 | if (entry_read) {
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 |
|
---|
66 | entry_read = true;
|
---|
67 | return (struct posix_passwd *) &dummy_pwd;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * "Rewind the user list".
|
---|
72 | */
|
---|
73 | void posix_setpwent(void)
|
---|
74 | {
|
---|
75 | entry_read = false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Ends enumerating and releases all resources. (Noop)
|
---|
80 | */
|
---|
81 | void posix_endpwent(void)
|
---|
82 | {
|
---|
83 | /* noop */
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Find an entry by name.
|
---|
88 | *
|
---|
89 | * @param name Name of the entry.
|
---|
90 | * @return
|
---|
91 | */
|
---|
92 | struct posix_passwd *posix_getpwnam(const char *name)
|
---|
93 | {
|
---|
94 | assert(name != NULL);
|
---|
95 |
|
---|
96 | if (posix_strcmp(name, "user") != 0) {
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return (struct posix_passwd *) &dummy_pwd;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Find an entry by name, thread safely.
|
---|
105 | *
|
---|
106 | * @param name Name of the entry.
|
---|
107 | * @param pwd
|
---|
108 | * @param buffer
|
---|
109 | * @param bufsize
|
---|
110 | * @param result
|
---|
111 | * @return
|
---|
112 | */
|
---|
113 | int posix_getpwnam_r(const char *name, struct posix_passwd *pwd,
|
---|
114 | char *buffer, size_t bufsize, struct posix_passwd **result)
|
---|
115 | {
|
---|
116 | assert(name != NULL);
|
---|
117 |
|
---|
118 | if (posix_strcmp(name, "user") != 0) {
|
---|
119 | *result = NULL;
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return posix_getpwuid_r(1, pwd, buffer, bufsize, result);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Find an entry by UID.
|
---|
128 | *
|
---|
129 | * @param uid UID of the entry.
|
---|
130 | * @return
|
---|
131 | */
|
---|
132 | struct posix_passwd *posix_getpwuid(posix_uid_t uid)
|
---|
133 | {
|
---|
134 | if (uid != 1) {
|
---|
135 | return NULL;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return (struct posix_passwd *) &dummy_pwd;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Find an entry by UID, thread safely.
|
---|
143 | *
|
---|
144 | * @param uid UID of the entry.
|
---|
145 | * @param pwd
|
---|
146 | * @param buffer
|
---|
147 | * @param bufsize
|
---|
148 | * @param result
|
---|
149 | * @return
|
---|
150 | */
|
---|
151 | int posix_getpwuid_r(posix_uid_t uid, struct posix_passwd *pwd,
|
---|
152 | char *buffer, size_t bufsize, struct posix_passwd **result)
|
---|
153 | {
|
---|
154 | assert(pwd != NULL);
|
---|
155 | assert(buffer != NULL);
|
---|
156 | assert(result != NULL);
|
---|
157 |
|
---|
158 | static const char bf[] = { 'u', 's', 'e', 'r', '\0',
|
---|
159 | '/', '\0', 'b', 'd', 's', 'h', '\0' };
|
---|
160 |
|
---|
161 | if (uid != 1) {
|
---|
162 | *result = NULL;
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 | if (bufsize < sizeof(bf)) {
|
---|
166 | *result = NULL;
|
---|
167 | return ERANGE;
|
---|
168 | }
|
---|
169 |
|
---|
170 | memcpy(buffer, bf, sizeof(bf));
|
---|
171 |
|
---|
172 | pwd->pw_name = (char *) bf;
|
---|
173 | pwd->pw_uid = 1;
|
---|
174 | pwd->pw_gid = 1;
|
---|
175 | pwd->pw_dir = (char *) bf + 5;
|
---|
176 | pwd->pw_shell = (char *) bf + 7;
|
---|
177 | *result = (struct posix_passwd *) pwd;
|
---|
178 |
|
---|
179 | return 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** @}
|
---|
183 | */
|
---|