]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/passwd.cc
Throughout, update copyrights to reflect dates which correspond to main-branch
[newlib-cygwin.git] / winsup / cygwin / passwd.cc
1 /* passwd.cc: getpwnam () and friends
2
3 Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 2009, 2010, 2011, 2012 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "winsup.h"
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include "cygerrno.h"
16 #include "security.h"
17 #include "path.h"
18 #include "fhandler.h"
19 #include "dtable.h"
20 #include "pinfo.h"
21 #include "cygheap.h"
22 #include "pwdgrp.h"
23 #include "shared_info.h"
24
25 /* Read /etc/passwd only once for better performance. This is done
26 on the first call that needs information from it. */
27
28 passwd *passwd_buf;
29 static pwdgrp pr (passwd_buf);
30
31 /* Parse /etc/passwd line into passwd structure. */
32 bool
33 pwdgrp::parse_passwd ()
34 {
35 passwd &res = (*passwd_buf)[curr_lines];
36 res.pw_name = next_str (':');
37 res.pw_passwd = next_str (':');
38 if (!next_num (res.pw_uid))
39 return false;
40 if (!next_num (res.pw_gid))
41 return false;
42 res.pw_comment = NULL;
43 res.pw_gecos = next_str (':');
44 res.pw_dir = next_str (':');
45 res.pw_shell = next_str (':');
46 return true;
47 }
48
49 /* Read in /etc/passwd and save contents in the password cache.
50 This sets pr to loaded or emulated so functions in this file can
51 tell that /etc/passwd has been read in or will be emulated. */
52 void
53 pwdgrp::read_passwd ()
54 {
55 load (L"\\etc\\passwd");
56
57 char strbuf[128] = "";
58 bool searchentry = true;
59 struct passwd *pw;
60 /* must be static */
61 static char NO_COPY pretty_ls[] = "????????:*:-1:-1:::";
62
63 add_line (pretty_ls);
64 cygsid tu = cygheap->user.sid ();
65 tu.string (strbuf);
66 if (!user_shared->cb || myself->uid == ILLEGAL_UID)
67 searchentry = !internal_getpwsid (tu);
68 if (searchentry
69 && (!(pw = internal_getpwnam (cygheap->user.name ()))
70 || !user_shared->cb
71 || (myself->uid != ILLEGAL_UID
72 && myself->uid != (__uid32_t) pw->pw_uid
73 && !internal_getpwuid (myself->uid))))
74 {
75 static char linebuf[1024]; // must be static and
76 // should not be NO_COPY
77 snprintf (linebuf, sizeof (linebuf), "%s:*:%lu:%lu:,%s:%s:/bin/sh",
78 cygheap->user.name (),
79 (!user_shared->cb || myself->uid == ILLEGAL_UID)
80 ? UNKNOWN_UID : myself->uid,
81 !user_shared->cb ? UNKNOWN_GID : myself->gid,
82 strbuf, getenv ("HOME") ?: "");
83 debug_printf ("Completing /etc/passwd: %s", linebuf);
84 add_line (linebuf);
85 }
86 }
87
88 struct passwd *
89 internal_getpwsid (cygpsid &sid)
90 {
91 struct passwd *pw;
92 char *ptr1, *ptr2, *endptr;
93 char sid_string[128] = {0,','};
94
95 pr.refresh (false);
96
97 if (sid.string (sid_string + 2))
98 {
99 endptr = strchr (sid_string + 2, 0) - 1;
100 for (int i = 0; i < pr.curr_lines; i++)
101 {
102 pw = passwd_buf + i;
103 if (pw->pw_dir > pw->pw_gecos + 8)
104 for (ptr1 = endptr, ptr2 = pw->pw_dir - 2;
105 *ptr1 == *ptr2; ptr2--)
106 if (!*--ptr1)
107 return pw;
108 }
109 }
110 return NULL;
111 }
112
113 struct passwd *
114 internal_getpwuid (__uid32_t uid, bool check)
115 {
116 pr.refresh (check);
117
118 for (int i = 0; i < pr.curr_lines; i++)
119 if (uid == (__uid32_t) passwd_buf[i].pw_uid)
120 return passwd_buf + i;
121 return NULL;
122 }
123
124 struct passwd *
125 internal_getpwnam (const char *name, bool check)
126 {
127 pr.refresh (check);
128
129 for (int i = 0; i < pr.curr_lines; i++)
130 /* on Windows NT user names are case-insensitive */
131 if (strcasematch (name, passwd_buf[i].pw_name))
132 return passwd_buf + i;
133 return NULL;
134 }
135
136
137 extern "C" struct passwd *
138 getpwuid32 (__uid32_t uid)
139 {
140 struct passwd *temppw = internal_getpwuid (uid, true);
141 pthread_testcancel ();
142 return temppw;
143 }
144
145 extern "C" struct passwd *
146 getpwuid (__uid16_t uid)
147 {
148 return getpwuid32 (uid16touid32 (uid));
149 }
150
151 extern "C" int
152 getpwuid_r32 (__uid32_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
153 {
154 *result = NULL;
155
156 if (!pwd || !buffer)
157 return ERANGE;
158
159 struct passwd *temppw = internal_getpwuid (uid, true);
160 pthread_testcancel ();
161 if (!temppw)
162 return 0;
163
164 /* check needed buffer size. */
165 size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_passwd)
166 + strlen (temppw->pw_gecos) + strlen (temppw->pw_dir)
167 + strlen (temppw->pw_shell) + 5;
168 if (needsize > bufsize)
169 return ERANGE;
170
171 /* make a copy of temppw */
172 *result = pwd;
173 pwd->pw_uid = temppw->pw_uid;
174 pwd->pw_gid = temppw->pw_gid;
175 buffer = stpcpy (pwd->pw_name = buffer, temppw->pw_name);
176 buffer = stpcpy (pwd->pw_passwd = buffer + 1, temppw->pw_passwd);
177 buffer = stpcpy (pwd->pw_gecos = buffer + 1, temppw->pw_gecos);
178 buffer = stpcpy (pwd->pw_dir = buffer + 1, temppw->pw_dir);
179 stpcpy (pwd->pw_shell = buffer + 1, temppw->pw_shell);
180 pwd->pw_comment = NULL;
181 return 0;
182 }
183
184 extern "C" int
185 getpwuid_r (__uid16_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
186 {
187 return getpwuid_r32 (uid16touid32 (uid), pwd, buffer, bufsize, result);
188 }
189
190 extern "C" struct passwd *
191 getpwnam (const char *name)
192 {
193 struct passwd *temppw = internal_getpwnam (name, true);
194 pthread_testcancel ();
195 return temppw;
196 }
197
198
199 /* the max size buffer we can expect to
200 * use is returned via sysconf with _SC_GETPW_R_SIZE_MAX.
201 * This may need updating! - Rob Collins April 2001.
202 */
203 extern "C" int
204 getpwnam_r (const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
205 {
206 *result = NULL;
207
208 if (!pwd || !buffer || !nam)
209 return ERANGE;
210
211 struct passwd *temppw = internal_getpwnam (nam, true);
212 pthread_testcancel ();
213
214 if (!temppw)
215 return 0;
216
217 /* check needed buffer size. */
218 size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_passwd)
219 + strlen (temppw->pw_gecos) + strlen (temppw->pw_dir)
220 + strlen (temppw->pw_shell) + 5;
221 if (needsize > bufsize)
222 return ERANGE;
223
224 /* make a copy of temppw */
225 *result = pwd;
226 pwd->pw_uid = temppw->pw_uid;
227 pwd->pw_gid = temppw->pw_gid;
228 buffer = stpcpy (pwd->pw_name = buffer, temppw->pw_name);
229 buffer = stpcpy (pwd->pw_passwd = buffer + 1, temppw->pw_passwd);
230 buffer = stpcpy (pwd->pw_gecos = buffer + 1, temppw->pw_gecos);
231 buffer = stpcpy (pwd->pw_dir = buffer + 1, temppw->pw_dir);
232 stpcpy (pwd->pw_shell = buffer + 1, temppw->pw_shell);
233 pwd->pw_comment = NULL;
234 return 0;
235 }
236
237 extern "C" struct passwd *
238 getpwent (void)
239 {
240 if (_my_tls.locals.pw_pos == 0)
241 pr.refresh (true);
242 if (_my_tls.locals.pw_pos < pr.curr_lines)
243 return passwd_buf + _my_tls.locals.pw_pos++;
244
245 return NULL;
246 }
247
248 extern "C" struct passwd *
249 getpwduid (__uid16_t)
250 {
251 return NULL;
252 }
253
254 extern "C" void
255 setpwent (void)
256 {
257 _my_tls.locals.pw_pos = 0;
258 }
259
260 extern "C" void
261 endpwent (void)
262 {
263 _my_tls.locals.pw_pos = 0;
264 }
265
266 extern "C" int
267 setpassent ()
268 {
269 return 0;
270 }
271
272 static void
273 _getpass_close_fd (void *arg)
274 {
275 if (arg)
276 fclose ((FILE *) arg);
277 }
278
279 extern "C" char *
280 getpass (const char * prompt)
281 {
282 char *pass = _my_tls.locals.pass;
283 struct termios ti, newti;
284 bool tc_set = false;
285
286 /* Try to use controlling tty in the first place. Use stdin and stderr
287 only as fallback. */
288 FILE *in = stdin, *err = stderr;
289 FILE *tty = fopen ("/dev/tty", "w+b");
290 pthread_cleanup_push (_getpass_close_fd, tty);
291 if (tty)
292 {
293 /* Set close-on-exec for obvious reasons. */
294 fcntl (fileno (tty), F_SETFD, fcntl (fileno (tty), F_GETFD) | FD_CLOEXEC);
295 in = err = tty;
296 }
297
298 /* Make sure to notice if stdin is closed. */
299 if (fileno (in) >= 0)
300 {
301 flockfile (in);
302 /* Change tty attributes if possible. */
303 if (!tcgetattr (fileno (in), &ti))
304 {
305 newti = ti;
306 newti.c_lflag &= ~(ECHO | ISIG); /* No echo, no signal handling. */
307 if (!tcsetattr (fileno (in), TCSANOW, &newti))
308 tc_set = true;
309 }
310 fputs (prompt, err);
311 fflush (err);
312 fgets (pass, _PASSWORD_LEN, in);
313 fprintf (err, "\n");
314 if (tc_set)
315 tcsetattr (fileno (in), TCSANOW, &ti);
316 funlockfile (in);
317 char *crlf = strpbrk (pass, "\r\n");
318 if (crlf)
319 *crlf = '\0';
320 }
321 pthread_cleanup_pop (1);
322 return pass;
323 }
This page took 0.050247 seconds and 5 git commands to generate.