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