]>
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
17 /* Read /etc/passwd only once for better performance. This is done
18 on the first call that needs information from it. */
20 static struct passwd
*passwd_buf
= NULL
; /* passwd contents in memory */
21 static int curr_lines
= 0;
22 static int max_lines
= 0;
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. */
33 static pwd_state passwd_state
= uninitialized
;
35 /* Position in the passwd cache */
37 #define pw_pos _reent_winsup()->_pw_pos
39 static int pw_pos
= 0;
42 /* Remove a : teminated string from the buffer, and increment the pointer */
44 grab_string (char **p
)
49 while (*src
&& *src
!= ':' && *src
!= '\n')
66 int val
= strtol (src
, NULL
, 10);
67 while (*src
&& *src
!= ':' && *src
!= '\n')
75 /* Parse /etc/passwd line into passwd structure. */
77 parse_pwd (struct passwd
&res
, char *buf
)
79 /* Allocate enough room for the passwd struct and all the strings
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')
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
);
92 res
.pw_gecos
= grab_string (&mybuf
);
93 res
.pw_dir
= grab_string (&mybuf
);
94 res
.pw_shell
= grab_string (&mybuf
);
97 /* Add one line from /etc/passwd into the password cache */
99 add_pwd_line (char *line
)
101 if (curr_lines
>= max_lines
)
104 passwd_buf
= (struct passwd
*) realloc (passwd_buf
, max_lines
* sizeof (struct passwd
));
106 parse_pwd (passwd_buf
[curr_lines
++], line
);
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. */
115 extern int passwd_sem
;
118 FILE *f
= fopen ("/etc/passwd", "rt");
123 while (fgets (linebuf
, sizeof (linebuf
), f
) != NULL
)
125 if (strlen (linebuf
))
126 add_pwd_line (linebuf
);
130 passwd_state
= loaded
;
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
))
139 strncpy (user_name
, "Administrator", MAX_USER_NAME
);
140 debug_printf ("Failed to get current user name. %E");
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
;
149 /* Cygwin internal */
150 static struct passwd
*
151 search_for (uid_t uid
, const char *name
)
153 struct passwd
*res
= 0;
154 struct passwd
*default_pw
= 0;
156 for (int i
= 0; i
< curr_lines
; i
++)
158 res
= passwd_buf
+ i
;
159 if (res
->pw_uid
== DEFAULT_UID
)
161 /* on Windows NT user names are case-insensitive */
164 if (strcasematch (name
, res
->pw_name
))
167 else if (uid
== res
->pw_uid
)
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
)))
181 extern "C" struct passwd
*
184 if (passwd_state
== uninitialized
)
187 return search_for (uid
, 0);
190 extern "C" struct passwd
*
191 getpwnam (const char *name
)
193 if (passwd_state
== uninitialized
)
196 return search_for (0, name
);
199 extern "C" struct passwd
*
202 if (passwd_state
== uninitialized
)
205 if (pw_pos
< curr_lines
)
206 return passwd_buf
+ pw_pos
++;
211 extern "C" struct passwd
*
214 if (passwd_state
== uninitialized
)
223 if (passwd_state
== uninitialized
)
232 if (passwd_state
== uninitialized
)
241 if (passwd_state
== uninitialized
)
248 getpass (const char * prompt
)
251 char *pass
=_reent_winsup()->_pass
;
253 static char pass
[_PASSWORD_LEN
];
255 struct termios ti
, newti
;
257 if (passwd_state
== uninitialized
)
260 if (dtable
.not_open (0))
267 fhandler_base
*fhstdin
= dtable
[0];
268 fhstdin
->tcgetattr (&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')
278 fhstdin
->tcsetattr (TCSANOW
, &ti
);
This page took 0.049442 seconds and 6 git commands to generate.