2000-02-18 03:38:33 +08:00
|
|
|
/* passwd.cc: getpwnam () and friends
|
|
|
|
|
|
|
|
Copyright 1996, 1997, 1998 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. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-11-15 08:13:09 +08:00
|
|
|
#include "cygheap.h"
|
2000-09-08 00:23:51 +08:00
|
|
|
#include <sys/termios.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* 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 = NULL; /* passwd contents in memory */
|
|
|
|
static int curr_lines = 0;
|
|
|
|
static int max_lines = 0;
|
|
|
|
|
2000-07-26 19:48:38 +08:00
|
|
|
/* Set to loaded when /etc/passwd has been read in by read_etc_passwd ().
|
|
|
|
Set to emulated if passwd is emulated. */
|
|
|
|
/* Functions in this file need to check the value of passwd_state
|
2000-02-18 03:38:33 +08:00
|
|
|
and read in the password file if it isn't set. */
|
2000-07-26 19:48:38 +08:00
|
|
|
enum pwd_state {
|
|
|
|
uninitialized = 0,
|
|
|
|
emulated,
|
|
|
|
loaded
|
|
|
|
};
|
|
|
|
static pwd_state passwd_state = uninitialized;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Position in the passwd cache */
|
|
|
|
#ifdef _MT_SAFE
|
2001-01-28 13:51:15 +08:00
|
|
|
#define pw_pos _reent_winsup ()->_pw_pos
|
2000-02-18 03:38:33 +08:00
|
|
|
#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;
|
2000-07-02 01:30:35 +08:00
|
|
|
int val = strtol (src, NULL, 10);
|
2000-02-18 03:38:33 +08:00
|
|
|
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';
|
|
|
|
|
2001-01-28 13:51:15 +08:00
|
|
|
res.pw_name = strlwr (grab_string (&mybuf));
|
2000-02-18 03:38:33 +08:00
|
|
|
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.
|
2000-07-26 19:48:38 +08:00
|
|
|
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. */
|
2000-06-25 01:37:52 +08:00
|
|
|
void
|
2000-02-18 03:38:33 +08:00
|
|
|
read_etc_passwd ()
|
|
|
|
{
|
|
|
|
extern int passwd_sem;
|
|
|
|
char linebuf[1024];
|
|
|
|
++passwd_sem;
|
2000-06-25 11:48:10 +08:00
|
|
|
FILE *f = fopen ("/etc/passwd", "rt");
|
2000-02-18 03:38:33 +08:00
|
|
|
--passwd_sem;
|
|
|
|
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
while (fgets (linebuf, sizeof (linebuf), f) != NULL)
|
|
|
|
{
|
|
|
|
if (strlen (linebuf))
|
|
|
|
add_pwd_line (linebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
2000-09-03 12:16:35 +08:00
|
|
|
passwd_state = loaded;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
debug_printf ("Emulating /etc/passwd");
|
2001-01-28 13:51:15 +08:00
|
|
|
snprintf (linebuf, sizeof (linebuf), "%s::%u:%u::%s:/bin/sh", cygheap->user.name (),
|
2000-02-18 03:38:33 +08:00
|
|
|
DEFAULT_UID, DEFAULT_GID, getenv ("HOME") ?: "/");
|
2000-09-03 12:16:35 +08:00
|
|
|
add_pwd_line (linebuf);
|
|
|
|
passwd_state = emulated;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
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)
|
2000-09-03 12:16:35 +08:00
|
|
|
default_pw = res;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2000-07-26 19:48:38 +08:00
|
|
|
/* Return default passwd entry if passwd is emulated or it's a
|
|
|
|
request for the current user. */
|
|
|
|
if (passwd_state != loaded
|
2000-09-05 01:52:42 +08:00
|
|
|
|| (!name && uid == myself->uid)
|
2001-01-28 13:51:15 +08:00
|
|
|
|| (name && strcasematch (name, cygheap->user.name ())))
|
2000-07-26 19:48:38 +08:00
|
|
|
return default_pw;
|
|
|
|
|
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" struct passwd *
|
2000-02-18 03:38:33 +08:00
|
|
|
getpwuid (uid_t uid)
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return search_for (uid, 0);
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" struct passwd *
|
2000-02-18 03:38:33 +08:00
|
|
|
getpwnam (const char *name)
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return search_for (0, name);
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" struct passwd *
|
2000-02-18 03:38:33 +08:00
|
|
|
getpwent (void)
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (pw_pos < curr_lines)
|
|
|
|
return passwd_buf + pw_pos++;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" struct passwd *
|
|
|
|
getpwduid (uid_t)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" void
|
2000-02-18 03:38:33 +08:00
|
|
|
setpwent (void)
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
pw_pos = 0;
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" void
|
2000-02-18 03:38:33 +08:00
|
|
|
endpwent (void)
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
pw_pos = 0;
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
setpassent ()
|
|
|
|
{
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-21 13:20:38 +08:00
|
|
|
extern "C" char *
|
2000-02-18 03:38:33 +08:00
|
|
|
getpass (const char * prompt)
|
|
|
|
{
|
|
|
|
#ifdef _MT_SAFE
|
2001-01-28 13:51:15 +08:00
|
|
|
char *pass=_reent_winsup ()->_pass;
|
2000-02-18 03:38:33 +08:00
|
|
|
#else
|
|
|
|
static char pass[_PASSWORD_LEN];
|
|
|
|
#endif
|
|
|
|
struct termios ti, newti;
|
|
|
|
|
2000-07-26 19:48:38 +08:00
|
|
|
if (passwd_state == uninitialized)
|
2001-01-28 13:51:15 +08:00
|
|
|
read_etc_passwd ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-12 12:48:44 +08:00
|
|
|
if (fdtab.not_open (0))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
pass[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-08-12 12:48:44 +08:00
|
|
|
fhandler_base *fhstdin = fdtab[0];
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
|
|
|
}
|