]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/passwd.cc
* winsup.h: Eliminate inclusion of most of the cygwin .h files. Use .h files
[newlib-cygwin.git] / winsup / cygwin / passwd.cc
1 /* passwd.cc: getpwnam () and friends
2
3 Copyright 1996, 1997, 1998 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <stdlib.h>
13 #include <pwd.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include "cygerrno.h"
17 #include "fhandler.h"
18 #include "dtable.h"
19 #include "thread.h"
20 #include "sync.h"
21 #include "sigproc.h"
22 #include "pinfo.h"
23
24 /* Read /etc/passwd only once for better performance. This is done
25 on the first call that needs information from it. */
26
27 static struct passwd *passwd_buf = NULL; /* passwd contents in memory */
28 static int curr_lines = 0;
29 static int max_lines = 0;
30
31 /* Set to loaded when /etc/passwd has been read in by read_etc_passwd ().
32 Set to emulated if passwd is emulated. */
33 /* Functions in this file need to check the value of passwd_state
34 and read in the password file if it isn't set. */
35 enum pwd_state {
36 uninitialized = 0,
37 emulated,
38 loaded
39 };
40 static pwd_state passwd_state = uninitialized;
41
42 /* Position in the passwd cache */
43 #ifdef _MT_SAFE
44 #define pw_pos _reent_winsup()->_pw_pos
45 #else
46 static int pw_pos = 0;
47 #endif
48
49 /* Remove a : teminated string from the buffer, and increment the pointer */
50 static char *
51 grab_string (char **p)
52 {
53 char *src = *p;
54 char *res = src;
55
56 while (*src && *src != ':' && *src != '\n')
57 src++;
58
59 if (*src == ':')
60 {
61 *src = 0;
62 src++;
63 }
64 *p = src;
65 return res;
66 }
67
68 /* same, for ints */
69 static int
70 grab_int (char **p)
71 {
72 char *src = *p;
73 int val = strtol (src, NULL, 10);
74 while (*src && *src != ':' && *src != '\n')
75 src++;
76 if (*src == ':')
77 src++;
78 *p = src;
79 return val;
80 }
81
82 /* Parse /etc/passwd line into passwd structure. */
83 void
84 parse_pwd (struct passwd &res, char *buf)
85 {
86 /* Allocate enough room for the passwd struct and all the strings
87 in it in one go */
88 size_t len = strlen (buf);
89 char *mybuf = (char *) malloc (len + 1);
90 (void) memcpy (mybuf, buf, len + 1);
91 if (mybuf[--len] == '\n')
92 mybuf[len] = '\0';
93
94 res.pw_name = strlwr(grab_string (&mybuf));
95 res.pw_passwd = grab_string (&mybuf);
96 res.pw_uid = grab_int (&mybuf);
97 res.pw_gid = grab_int (&mybuf);
98 res.pw_comment = 0;
99 res.pw_gecos = grab_string (&mybuf);
100 res.pw_dir = grab_string (&mybuf);
101 res.pw_shell = grab_string (&mybuf);
102 }
103
104 /* Add one line from /etc/passwd into the password cache */
105 static void
106 add_pwd_line (char *line)
107 {
108 if (curr_lines >= max_lines)
109 {
110 max_lines += 10;
111 passwd_buf = (struct passwd *) realloc (passwd_buf, max_lines * sizeof (struct passwd));
112 }
113 parse_pwd (passwd_buf[curr_lines++], line);
114 }
115
116 /* Read in /etc/passwd and save contents in the password cache.
117 This sets passwd_state to loaded or emulated so functions in this file can
118 tell that /etc/passwd has been read in or will be emulated. */
119 void
120 read_etc_passwd ()
121 {
122 extern int passwd_sem;
123 char linebuf[1024];
124 ++passwd_sem;
125 FILE *f = fopen ("/etc/passwd", "rt");
126 --passwd_sem;
127
128 if (f)
129 {
130 while (fgets (linebuf, sizeof (linebuf), f) != NULL)
131 {
132 if (strlen (linebuf))
133 add_pwd_line (linebuf);
134 }
135
136 fclose (f);
137 passwd_state = loaded;
138 }
139 else
140 {
141 debug_printf ("Emulating /etc/passwd");
142 char user_name [ MAX_USER_NAME ];
143 DWORD user_name_len = MAX_USER_NAME;
144 if (! GetUserNameA (user_name, &user_name_len))
145 {
146 strncpy (user_name, "Administrator", MAX_USER_NAME);
147 debug_printf ("Failed to get current user name. %E");
148 }
149 snprintf (linebuf, sizeof (linebuf), "%s::%u:%u::%s:/bin/sh", user_name,
150 DEFAULT_UID, DEFAULT_GID, getenv ("HOME") ?: "/");
151 add_pwd_line (linebuf);
152 passwd_state = emulated;
153 }
154 }
155
156 /* Cygwin internal */
157 static struct passwd *
158 search_for (uid_t uid, const char *name)
159 {
160 struct passwd *res = 0;
161 struct passwd *default_pw = 0;
162
163 for (int i = 0; i < curr_lines; i++)
164 {
165 res = passwd_buf + i;
166 if (res->pw_uid == DEFAULT_UID)
167 default_pw = res;
168 /* on Windows NT user names are case-insensitive */
169 if (name)
170 {
171 if (strcasematch (name, res->pw_name))
172 return res;
173 }
174 else if (uid == res->pw_uid)
175 return res;
176 }
177
178 /* Return default passwd entry if passwd is emulated or it's a
179 request for the current user. */
180 if (passwd_state != loaded
181 || (! name && uid == myself->uid)
182 || ( name && strcasematch(name, myself->username)))
183 return default_pw;
184
185 return NULL;
186 }
187
188 extern "C" struct passwd *
189 getpwuid (uid_t uid)
190 {
191 if (passwd_state == uninitialized)
192 read_etc_passwd();
193
194 return search_for (uid, 0);
195 }
196
197 extern "C" struct passwd *
198 getpwnam (const char *name)
199 {
200 if (passwd_state == uninitialized)
201 read_etc_passwd();
202
203 return search_for (0, name);
204 }
205
206 extern "C" struct passwd *
207 getpwent (void)
208 {
209 if (passwd_state == uninitialized)
210 read_etc_passwd();
211
212 if (pw_pos < curr_lines)
213 return passwd_buf + pw_pos++;
214
215 return NULL;
216 }
217
218 extern "C" struct passwd *
219 getpwduid (uid_t)
220 {
221 if (passwd_state == uninitialized)
222 read_etc_passwd();
223
224 return NULL;
225 }
226
227 extern "C" void
228 setpwent (void)
229 {
230 if (passwd_state == uninitialized)
231 read_etc_passwd();
232
233 pw_pos = 0;
234 }
235
236 extern "C" void
237 endpwent (void)
238 {
239 if (passwd_state == uninitialized)
240 read_etc_passwd();
241
242 pw_pos = 0;
243 }
244
245 extern "C" int
246 setpassent ()
247 {
248 if (passwd_state == uninitialized)
249 read_etc_passwd();
250
251 return 0;
252 }
253
254 extern "C" char *
255 getpass (const char * prompt)
256 {
257 #ifdef _MT_SAFE
258 char *pass=_reent_winsup()->_pass;
259 #else
260 static char pass[_PASSWORD_LEN];
261 #endif
262 struct termios ti, newti;
263
264 if (passwd_state == uninitialized)
265 read_etc_passwd();
266
267 if (fdtab.not_open (0))
268 {
269 set_errno (EBADF);
270 pass[0] = '\0';
271 }
272 else
273 {
274 fhandler_base *fhstdin = fdtab[0];
275 fhstdin->tcgetattr (&ti);
276 newti = ti;
277 newti.c_lflag &= ~ECHO;
278 fhstdin->tcsetattr (TCSANOW, &newti);
279 fputs (prompt, stderr);
280 fgets (pass, _PASSWORD_LEN, stdin);
281 fprintf (stderr, "\n");
282 for (int i=0; pass[i]; i++)
283 if (pass[i] == '\r' || pass[i] == '\n')
284 pass[i] = '\0';
285 fhstdin->tcsetattr (TCSANOW, &ti);
286 }
287 return pass;
288 }
This page took 0.049862 seconds and 6 git commands to generate.