]>
sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/passwd.cc
1 /* passwd.cc: getpwnam () and friends
3 Copyright 1996, 1997, 1998 Cygnus Solutions.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
24 /* Read /etc/passwd only once for better performance. This is done
25 on the first call that needs information from it. */
27 static struct passwd
*passwd_buf
= NULL
; /* passwd contents in memory */
28 static int curr_lines
= 0;
29 static int max_lines
= 0;
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. */
40 static pwd_state passwd_state
= uninitialized
;
42 /* Position in the passwd cache */
44 #define pw_pos _reent_winsup()->_pw_pos
46 static int pw_pos
= 0;
49 /* Remove a : teminated string from the buffer, and increment the pointer */
51 grab_string (char **p
)
56 while (*src
&& *src
!= ':' && *src
!= '\n')
73 int val
= strtol (src
, NULL
, 10);
74 while (*src
&& *src
!= ':' && *src
!= '\n')
82 /* Parse /etc/passwd line into passwd structure. */
84 parse_pwd (struct passwd
&res
, char *buf
)
86 /* Allocate enough room for the passwd struct and all the strings
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')
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
);
99 res
.pw_gecos
= grab_string (&mybuf
);
100 res
.pw_dir
= grab_string (&mybuf
);
101 res
.pw_shell
= grab_string (&mybuf
);
104 /* Add one line from /etc/passwd into the password cache */
106 add_pwd_line (char *line
)
108 if (curr_lines
>= max_lines
)
111 passwd_buf
= (struct passwd
*) realloc (passwd_buf
, max_lines
* sizeof (struct passwd
));
113 parse_pwd (passwd_buf
[curr_lines
++], line
);
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. */
122 extern int passwd_sem
;
125 FILE *f
= fopen ("/etc/passwd", "rt");
130 while (fgets (linebuf
, sizeof (linebuf
), f
) != NULL
)
132 if (strlen (linebuf
))
133 add_pwd_line (linebuf
);
137 passwd_state
= loaded
;
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
))
146 strncpy (user_name
, "Administrator", MAX_USER_NAME
);
147 debug_printf ("Failed to get current user name. %E");
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
;
156 /* Cygwin internal */
157 static struct passwd
*
158 search_for (uid_t uid
, const char *name
)
160 struct passwd
*res
= 0;
161 struct passwd
*default_pw
= 0;
163 for (int i
= 0; i
< curr_lines
; i
++)
165 res
= passwd_buf
+ i
;
166 if (res
->pw_uid
== DEFAULT_UID
)
168 /* on Windows NT user names are case-insensitive */
171 if (strcasematch (name
, res
->pw_name
))
174 else if (uid
== res
->pw_uid
)
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
)))
188 extern "C" struct passwd
*
191 if (passwd_state
== uninitialized
)
194 return search_for (uid
, 0);
197 extern "C" struct passwd
*
198 getpwnam (const char *name
)
200 if (passwd_state
== uninitialized
)
203 return search_for (0, name
);
206 extern "C" struct passwd
*
209 if (passwd_state
== uninitialized
)
212 if (pw_pos
< curr_lines
)
213 return passwd_buf
+ pw_pos
++;
218 extern "C" struct passwd
*
221 if (passwd_state
== uninitialized
)
230 if (passwd_state
== uninitialized
)
239 if (passwd_state
== uninitialized
)
248 if (passwd_state
== uninitialized
)
255 getpass (const char * prompt
)
258 char *pass
=_reent_winsup()->_pass
;
260 static char pass
[_PASSWORD_LEN
];
262 struct termios ti
, newti
;
264 if (passwd_state
== uninitialized
)
267 if (fdtab
.not_open (0))
274 fhandler_base
*fhstdin
= fdtab
[0];
275 fhstdin
->tcgetattr (&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')
285 fhstdin
->tcsetattr (TCSANOW
, &ti
);
This page took 0.056725 seconds and 6 git commands to generate.