1 | /* |
---|
2 | * POSIX 1003.1b - 9.2.2 - User Database Access Routines |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.OARcorp.com/rtems/license.html. |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | #if HAVE_CONFIG_H |
---|
12 | #include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <pwd.h> |
---|
18 | #include <errno.h> |
---|
19 | #include <unistd.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <string.h> |
---|
22 | #include <limits.h> |
---|
23 | #include <fcntl.h> |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #include <rtems/libio_.h> |
---|
27 | |
---|
28 | static struct passwd pw_passwd; /* password structure */ |
---|
29 | static FILE *passwd_fp; |
---|
30 | |
---|
31 | /* |
---|
32 | * The size of these buffers is arbitrary and there is no provision |
---|
33 | * to protect any of them from overflowing. The scanf patterns |
---|
34 | * need to be changed to prevent overflowing. In addition, |
---|
35 | * the limits on these needs to be examined. |
---|
36 | */ |
---|
37 | |
---|
38 | static char logname[8]; |
---|
39 | static char password[1024]; |
---|
40 | static char comment[1024]; |
---|
41 | static char gecos[1024]; |
---|
42 | static char dir[1024]; |
---|
43 | static char shell[1024]; |
---|
44 | |
---|
45 | /* |
---|
46 | * Initialize a useable but dummy /etc/passwd |
---|
47 | * |
---|
48 | * NOTE: Ignore all errors. |
---|
49 | * |
---|
50 | */ |
---|
51 | |
---|
52 | static char etc_passwd_initted = 0; |
---|
53 | |
---|
54 | void init_etc_passwd_group(void) |
---|
55 | { |
---|
56 | FILE *fp; |
---|
57 | |
---|
58 | if ( etc_passwd_initted ) |
---|
59 | return; |
---|
60 | etc_passwd_initted = 1; |
---|
61 | |
---|
62 | (void) mkdir( "/etc", 0777); |
---|
63 | |
---|
64 | /* |
---|
65 | * Initialize /etc/passwd |
---|
66 | */ |
---|
67 | |
---|
68 | if ((fp = fopen ("/etc/passwd", "w")) == NULL) |
---|
69 | return; |
---|
70 | |
---|
71 | fprintf( fp, "root:*:0:0:root,,,,:/:/bin/sh\n" |
---|
72 | "rtems:*:1:1:RTEMS Application,,,,:/:/bin/sh\n" |
---|
73 | "tty:!:2:2:tty owner,,,,:/:/bin/false\n" ); |
---|
74 | |
---|
75 | fclose( fp ); |
---|
76 | |
---|
77 | /* |
---|
78 | * Initialize /etc/group |
---|
79 | */ |
---|
80 | |
---|
81 | if ((fp = fopen ("/etc/group", "w")) == NULL) |
---|
82 | return; |
---|
83 | |
---|
84 | fprintf( fp, "root:x:0:root\n" |
---|
85 | "rtems:x:1:rtems\n" |
---|
86 | "tty:x:2:tty\n" ); |
---|
87 | |
---|
88 | fclose( fp ); |
---|
89 | } |
---|
90 | |
---|
91 | int getpwnam_r( |
---|
92 | const char *name, |
---|
93 | struct passwd *pwd, |
---|
94 | char *buffer, |
---|
95 | size_t bufsize, |
---|
96 | struct passwd **result |
---|
97 | ) |
---|
98 | { |
---|
99 | FILE *fp; |
---|
100 | rtems_user_env_t * aux=rtems_current_user_env; /* save */ |
---|
101 | |
---|
102 | init_etc_passwd_group(); |
---|
103 | rtems_current_user_env=&rtems_global_user_env; /* set root */ |
---|
104 | |
---|
105 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) { |
---|
106 | errno = EINVAL; |
---|
107 | rtems_current_user_env=aux; /* restore */ |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|
111 | while (fgets (buffer, bufsize, fp)) { |
---|
112 | sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
113 | logname, password, &pwd->pw_uid, |
---|
114 | &pwd->pw_gid, comment, gecos, |
---|
115 | dir, shell); |
---|
116 | pwd->pw_name = logname; |
---|
117 | pwd->pw_passwd = password; |
---|
118 | pwd->pw_comment = comment; |
---|
119 | pwd->pw_gecos = gecos; |
---|
120 | pwd->pw_dir = dir; |
---|
121 | pwd->pw_shell = shell; |
---|
122 | |
---|
123 | if (!strcmp (logname, name)) { |
---|
124 | fclose (fp); |
---|
125 | *result = pwd; |
---|
126 | rtems_current_user_env=aux; /* restore */ |
---|
127 | return 0; |
---|
128 | } |
---|
129 | } |
---|
130 | fclose (fp); |
---|
131 | errno = EINVAL; |
---|
132 | rtems_current_user_env=aux; /* restore */ |
---|
133 | return -1; |
---|
134 | } |
---|
135 | |
---|
136 | struct passwd *getpwnam( |
---|
137 | const char *name |
---|
138 | ) |
---|
139 | { |
---|
140 | char buf[1024]; |
---|
141 | struct passwd *p; |
---|
142 | |
---|
143 | if ( getpwnam_r( name, &pw_passwd, buf, 1024, &p ) ) |
---|
144 | return NULL; |
---|
145 | |
---|
146 | return p; |
---|
147 | } |
---|
148 | |
---|
149 | int getpwuid_r( |
---|
150 | uid_t uid, |
---|
151 | struct passwd *pwd, |
---|
152 | char *buffer, |
---|
153 | size_t bufsize, |
---|
154 | struct passwd **result |
---|
155 | ) |
---|
156 | { |
---|
157 | FILE *fp; |
---|
158 | rtems_user_env_t * aux=rtems_current_user_env; /* save */ |
---|
159 | |
---|
160 | init_etc_passwd_group(); |
---|
161 | rtems_current_user_env=&rtems_global_user_env; /* set root */ |
---|
162 | |
---|
163 | if ((fp = fopen ("/etc/passwd", "r")) == NULL) { |
---|
164 | errno = EINVAL; |
---|
165 | rtems_current_user_env=aux; /* restore */ |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | while (fgets (buffer, bufsize, fp)) { |
---|
170 | sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
171 | logname, password, &pw_passwd.pw_uid, |
---|
172 | &pw_passwd.pw_gid, comment, gecos, |
---|
173 | dir, shell); |
---|
174 | pwd->pw_name = logname; |
---|
175 | pwd->pw_passwd = password; |
---|
176 | pwd->pw_comment = comment; |
---|
177 | pwd->pw_gecos = gecos; |
---|
178 | pwd->pw_dir = dir; |
---|
179 | pwd->pw_shell = shell; |
---|
180 | |
---|
181 | if (uid == pwd->pw_uid) { |
---|
182 | fclose (fp); |
---|
183 | *result = pwd; |
---|
184 | rtems_current_user_env=aux; /* restore */ |
---|
185 | return 0; |
---|
186 | } |
---|
187 | } |
---|
188 | fclose (fp); |
---|
189 | errno = EINVAL; |
---|
190 | rtems_current_user_env=aux; /* restore */ |
---|
191 | return -1; |
---|
192 | } |
---|
193 | |
---|
194 | struct passwd *getpwuid( |
---|
195 | uid_t uid |
---|
196 | ) |
---|
197 | { |
---|
198 | char buf[1024]; |
---|
199 | struct passwd *p; |
---|
200 | |
---|
201 | if ( getpwuid_r( uid, &pw_passwd, buf, 1024, &p ) ) |
---|
202 | return NULL; |
---|
203 | |
---|
204 | return p; |
---|
205 | } |
---|
206 | |
---|
207 | struct passwd *getpwent() |
---|
208 | { |
---|
209 | char buf[1024]; |
---|
210 | |
---|
211 | if (passwd_fp == NULL) |
---|
212 | return NULL; |
---|
213 | |
---|
214 | if (fgets (buf, sizeof (buf), passwd_fp) == NULL) |
---|
215 | return NULL; |
---|
216 | |
---|
217 | sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%[^:]:%s\n", |
---|
218 | logname, password, &pw_passwd.pw_uid, |
---|
219 | &pw_passwd.pw_gid, comment, gecos, |
---|
220 | dir, shell); |
---|
221 | pw_passwd.pw_name = logname; |
---|
222 | pw_passwd.pw_passwd = password; |
---|
223 | pw_passwd.pw_comment = comment; |
---|
224 | pw_passwd.pw_gecos = gecos; |
---|
225 | pw_passwd.pw_dir = dir; |
---|
226 | pw_passwd.pw_shell = shell; |
---|
227 | |
---|
228 | return &pw_passwd; |
---|
229 | } |
---|
230 | |
---|
231 | void setpwent( void ) |
---|
232 | { |
---|
233 | rtems_user_env_t * aux=rtems_current_user_env; /* save */ |
---|
234 | init_etc_passwd_group(); |
---|
235 | rtems_current_user_env=&rtems_global_user_env; /* set root */ |
---|
236 | |
---|
237 | if (passwd_fp != NULL) |
---|
238 | fclose (passwd_fp); |
---|
239 | |
---|
240 | passwd_fp = fopen ("/etc/passwd", "r"); |
---|
241 | rtems_current_user_env=aux; /* restore */ |
---|
242 | } |
---|
243 | |
---|
244 | void endpwent( void ) |
---|
245 | { |
---|
246 | if (passwd_fp != NULL) |
---|
247 | fclose (passwd_fp); |
---|
248 | } |
---|