1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/* passwd.cc: getpwnam () and friends
Copyright 1996, 1997, 1998, 2001 Cygnus Solutions.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include "cygerrno.h"
#include "security.h"
#include "fhandler.h"
#include "dtable.h"
#include "path.h"
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "cygheap.h"
#include <sys/termios.h>
#include "pwdgrp.h"
/* Read /etc/passwd only once for better performance. This is done
on the first call that needs information from it. */
static struct passwd *passwd_buf; /* passwd contents in memory */
static int curr_lines;
static int max_lines;
static pwdgrp_check passwd_state;
/* Position in the passwd cache */
#ifdef _MT_SAFE
#define pw_pos _reent_winsup ()->_pw_pos
#else
static int pw_pos = 0;
#endif
/* Remove a : teminated string from the buffer, and increment the pointer */
static char *
grab_string (char **p)
{
char *src = *p;
char *res = src;
while (*src && *src != ':' && *src != '\n')
src++;
if (*src == ':')
{
*src = 0;
src++;
}
*p = src;
return res;
}
/* same, for ints */
static int
grab_int (char **p)
{
char *src = *p;
int val = strtol (src, NULL, 10);
while (*src && *src != ':' && *src != '\n')
src++;
if (*src == ':')
src++;
*p = src;
return val;
}
/* Parse /etc/passwd line into passwd structure. */
void
parse_pwd (struct passwd &res, char *buf)
{
/* Allocate enough room for the passwd struct and all the strings
in it in one go */
size_t len = strlen (buf);
char *mybuf = (char *) malloc (len + 1);
(void) memcpy (mybuf, buf, len + 1);
if (mybuf[--len] == '\n')
mybuf[len] = '\0';
res.pw_name = grab_string (&mybuf);
res.pw_passwd = grab_string (&mybuf);
res.pw_uid = grab_int (&mybuf);
res.pw_gid = grab_int (&mybuf);
res.pw_comment = 0;
res.pw_gecos = grab_string (&mybuf);
res.pw_dir = grab_string (&mybuf);
res.pw_shell = grab_string (&mybuf);
}
/* Add one line from /etc/passwd into the password cache */
static void
add_pwd_line (char *line)
{
if (curr_lines >= max_lines)
{
max_lines += 10;
passwd_buf = (struct passwd *) realloc (passwd_buf, max_lines * sizeof (struct passwd));
}
parse_pwd (passwd_buf[curr_lines++], line);
}
/* Read in /etc/passwd and save contents in the password cache.
This sets passwd_state to loaded or emulated so functions in this file can
tell that /etc/passwd has been read in or will be emulated. */
void
read_etc_passwd ()
{
char linebuf[1024];
/* A mutex is ok for speed here - pthreads will use critical sections not mutex's
* for non-shared mutexs in the future. Also, this function will at most be called
* once from each thread, after that the passwd_state test will succeed
*/
static NO_COPY pthread_mutex_t etc_passwd_mutex = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock (&etc_passwd_mutex);
/* if we got blocked by the mutex, then etc_passwd may have been processed */
if (passwd_state != uninitialized)
{
pthread_mutex_unlock(&etc_passwd_mutex);
return;
}
if (passwd_state != initializing)
{
passwd_state = initializing;
if (max_lines) /* When rereading, free allocated memory first. */
{
for (int i = 0; i < curr_lines; ++i)
free (passwd_buf[i].pw_name);
free (passwd_buf);
curr_lines = max_lines = 0;
}
FILE *f = fopen ("/etc/passwd", "rt");
if (f)
{
while (fgets (linebuf, sizeof (linebuf), f) != NULL)
{
if (strlen (linebuf))
add_pwd_line (linebuf);
}
passwd_state.set_last_modified (f);
fclose (f);
passwd_state = loaded;
}
else
{
debug_printf ("Emulating /etc/passwd");
snprintf (linebuf, sizeof (linebuf), "%s::%u:%u::%s:/bin/sh", cygheap->user.name (),
DEFAULT_UID, DEFAULT_GID, getenv ("HOME") ?: "/");
add_pwd_line (linebuf);
passwd_state = emulated;
}
}
pthread_mutex_unlock (&etc_passwd_mutex);
}
/* Cygwin internal */
/* If this ever becomes non-reentrant, update all the getpw*_r functions */
static struct passwd *
search_for (uid_t uid, const char *name)
{
struct passwd *res = 0;
struct passwd *default_pw = 0;
for (int i = 0; i < curr_lines; i++)
{
res = passwd_buf + i;
if (res->pw_uid == DEFAULT_UID)
default_pw = res;
/* on Windows NT user names are case-insensitive */
if (name)
{
if (strcasematch (name, res->pw_name))
return res;
}
else if (uid == res->pw_uid)
return res;
}
/* Return default passwd entry if passwd is emulated or it's a
request for the current user. */
if (passwd_state != loaded
|| (!name && uid == myself->uid)
|| (name && strcasematch (name, cygheap->user.name ())))
return default_pw;
return NULL;
}
extern "C" struct passwd *
getpwuid (uid_t uid)
{
if (passwd_state <= initializing)
read_etc_passwd ();
pthread_testcancel();
return search_for (uid, 0);
}
extern "C" int
getpwuid_r (uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
*result = NULL;
if (!pwd || !buffer)
return ERANGE;
if (passwd_state <= initializing)
read_etc_passwd ();
pthread_testcancel();
struct passwd *temppw = search_for (uid, 0);
if (!temppw)
return 0;
/* check needed buffer size. */
size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_dir) +
strlen (temppw->pw_shell) + strlen (temppw->pw_gecos) +
strlen (temppw->pw_passwd) + 5;
if (needsize > bufsize)
return ERANGE;
/* make a copy of temppw */
*result = pwd;
pwd->pw_uid = temppw->pw_uid;
pwd->pw_gid = temppw->pw_gid;
pwd->pw_name = buffer;
pwd->pw_dir = pwd->pw_name + strlen (temppw->pw_name) + 1;
pwd->pw_shell = pwd->pw_dir + strlen (temppw->pw_dir) + 1;
pwd->pw_gecos = pwd->pw_shell + strlen (temppw->pw_shell) + 1;
pwd->pw_passwd = pwd->pw_gecos + strlen (temppw->pw_gecos) + 1;
strcpy (pwd->pw_name, temppw->pw_name);
strcpy (pwd->pw_dir, temppw->pw_dir);
strcpy (pwd->pw_shell, temppw->pw_shell);
strcpy (pwd->pw_gecos, temppw->pw_gecos);
strcpy (pwd->pw_passwd, temppw->pw_passwd);
return 0;
}
extern "C" struct passwd *
getpwnam (const char *name)
{
if (passwd_state <= initializing)
read_etc_passwd ();
pthread_testcancel();
return search_for (0, name);
}
/* the max size buffer we can expect to
* use is returned via sysconf with _SC_GETPW_R_SIZE_MAX.
* This may need updating! - Rob Collins April 2001.
*/
extern "C" int
getpwnam_r (const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
*result = NULL;
if (!pwd || !buffer || !nam)
return ERANGE;
if (passwd_state <= initializing)
read_etc_passwd ();
pthread_testcancel();
struct passwd *temppw = search_for (0, nam);
if (!temppw)
return 0;
/* check needed buffer size. */
size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_dir) +
strlen (temppw->pw_shell) + strlen (temppw->pw_gecos) +
strlen (temppw->pw_passwd) + 5;
if (needsize > bufsize)
return ERANGE;
/* make a copy of temppw */
*result = pwd;
pwd->pw_uid = temppw->pw_uid;
pwd->pw_gid = temppw->pw_gid;
pwd->pw_name = buffer;
pwd->pw_dir = pwd->pw_name + strlen (temppw->pw_name) + 1;
pwd->pw_shell = pwd->pw_dir + strlen (temppw->pw_dir) + 1;
pwd->pw_gecos = pwd->pw_shell + strlen (temppw->pw_shell) + 1;
pwd->pw_passwd = pwd->pw_gecos + strlen (temppw->pw_gecos) + 1;
strcpy (pwd->pw_name, temppw->pw_name);
strcpy (pwd->pw_dir, temppw->pw_dir);
strcpy (pwd->pw_shell, temppw->pw_shell);
strcpy (pwd->pw_gecos, temppw->pw_gecos);
strcpy (pwd->pw_passwd, temppw->pw_passwd);
return 0;
}
extern "C" struct passwd *
getpwent (void)
{
if (passwd_state <= initializing)
read_etc_passwd ();
if (pw_pos < curr_lines)
return passwd_buf + pw_pos++;
return NULL;
}
extern "C" struct passwd *
getpwduid (uid_t)
{
return NULL;
}
extern "C" void
setpwent (void)
{
pw_pos = 0;
}
extern "C" void
endpwent (void)
{
pw_pos = 0;
}
extern "C" int
setpassent ()
{
return 0;
}
/* Internal function. ONLY USE THIS INTERNALLY, NEVER `getpwent'!!! */
struct passwd *
internal_getpwent (int pos)
{
if (passwd_state <= initializing)
read_etc_passwd ();
if (pos < curr_lines)
return passwd_buf + pos;
return NULL;
}
extern "C" char *
getpass (const char * prompt)
{
#ifdef _MT_SAFE
char *pass=_reent_winsup ()->_pass;
#else
static char pass[_PASSWORD_LEN];
#endif
struct termios ti, newti;
if (passwd_state <= initializing)
read_etc_passwd ();
if (cygheap->fdtab.not_open (0))
{
set_errno (EBADF);
pass[0] = '\0';
}
else
{
fhandler_base *fhstdin = cygheap->fdtab[0];
fhstdin->tcgetattr (&ti);
newti = ti;
newti.c_lflag &= ~ECHO;
fhstdin->tcsetattr (TCSANOW, &newti);
fputs (prompt, stderr);
fgets (pass, _PASSWORD_LEN, stdin);
fprintf (stderr, "\n");
for (int i=0; pass[i]; i++)
if (pass[i] == '\r' || pass[i] == '\n')
pass[i] = '\0';
fhstdin->tcsetattr (TCSANOW, &ti);
}
return pass;
}
|