#include <stdio.h>
|
#include <stdio.h>
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <pwd.h>
|
#include <pwd.h>
|
#include <errno.h>
|
#include <errno.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <limits.h>
|
#include <limits.h>
|
|
|
static struct passwd pw_passwd; /* password structure */
|
static struct passwd pw_passwd; /* password structure */
|
static FILE *passwd_fp;
|
static FILE *passwd_fp;
|
|
|
static char logname[8];
|
static char logname[8];
|
static char password[1024];
|
static char password[1024];
|
static char gecos[1024];
|
static char gecos[1024];
|
static char dir[1024];
|
static char dir[1024];
|
static char shell[1024];
|
static char shell[1024];
|
|
|
struct passwd *
|
struct passwd *
|
getpwnam (name)
|
getpwnam (name)
|
const char *name;
|
const char *name;
|
{
|
{
|
FILE *fp;
|
FILE *fp;
|
char buf[1024];
|
char buf[1024];
|
|
|
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
|
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
|
{
|
{
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
while (fgets (buf, sizeof (buf), fp))
|
while (fgets (buf, sizeof (buf), fp))
|
{
|
{
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
logname, password, &pw_passwd.pw_uid,
|
logname, password, &pw_passwd.pw_uid,
|
&pw_passwd.pw_gid, gecos,
|
&pw_passwd.pw_gid, gecos,
|
dir, shell);
|
dir, shell);
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_shell = shell;
|
pw_passwd.pw_shell = shell;
|
|
|
if (!strcmp (logname, name))
|
if (!strcmp (logname, name))
|
{
|
{
|
fclose (fp);
|
fclose (fp);
|
return &pw_passwd;
|
return &pw_passwd;
|
}
|
}
|
}
|
}
|
fclose (fp);
|
fclose (fp);
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
struct passwd *
|
struct passwd *
|
getpwuid (uid_t uid)
|
getpwuid (uid_t uid)
|
{
|
{
|
FILE *fp;
|
FILE *fp;
|
char buf[1024];
|
char buf[1024];
|
|
|
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
|
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
|
{
|
{
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
while (fgets (buf, sizeof (buf), fp))
|
while (fgets (buf, sizeof (buf), fp))
|
{
|
{
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
logname, password, &pw_passwd.pw_uid,
|
logname, password, &pw_passwd.pw_uid,
|
&pw_passwd.pw_gid, gecos,
|
&pw_passwd.pw_gid, gecos,
|
dir, shell);
|
dir, shell);
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_shell = shell;
|
pw_passwd.pw_shell = shell;
|
|
|
if (uid == pw_passwd.pw_uid)
|
if (uid == pw_passwd.pw_uid)
|
{
|
{
|
fclose (fp);
|
fclose (fp);
|
return &pw_passwd;
|
return &pw_passwd;
|
}
|
}
|
}
|
}
|
fclose (fp);
|
fclose (fp);
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
struct passwd *
|
struct passwd *
|
getpwent ()
|
getpwent ()
|
{
|
{
|
char buf[1024];
|
char buf[1024];
|
|
|
if (passwd_fp == NULL)
|
if (passwd_fp == NULL)
|
return NULL;
|
return NULL;
|
|
|
if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
|
if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
|
return NULL;
|
return NULL;
|
|
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
|
logname, password, &pw_passwd.pw_uid,
|
logname, password, &pw_passwd.pw_uid,
|
&pw_passwd.pw_gid, gecos,
|
&pw_passwd.pw_gid, gecos,
|
dir, shell);
|
dir, shell);
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_name = logname;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_passwd = password;
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_comment = "";
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_gecos = gecos;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_dir = dir;
|
pw_passwd.pw_shell = shell;
|
pw_passwd.pw_shell = shell;
|
|
|
return &pw_passwd;
|
return &pw_passwd;
|
}
|
}
|
|
|
void
|
void
|
setpwent ()
|
setpwent ()
|
{
|
{
|
if (passwd_fp != NULL)
|
if (passwd_fp != NULL)
|
fclose (passwd_fp);
|
fclose (passwd_fp);
|
|
|
passwd_fp = fopen ("/etc/passwd", "r");
|
passwd_fp = fopen ("/etc/passwd", "r");
|
}
|
}
|
|
|
void
|
void
|
endpwent ()
|
endpwent ()
|
{
|
{
|
if (passwd_fp != NULL)
|
if (passwd_fp != NULL)
|
fclose (passwd_fp);
|
fclose (passwd_fp);
|
}
|
}
|
|
|