2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "rcsid.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
RCSID ("$Id: pwio.c,v 1.14 2005/03/31 05:14:49 kloczek Exp $")
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "commonio.h"
|
|
|
|
#include "pwio.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
extern struct passwd *sgetpwent (const char *);
|
|
|
|
extern int putpwent (const struct passwd *, FILE *);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct passwd *__pw_dup (const struct passwd *pwent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw = (struct passwd *) malloc (sizeof *pw)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
|
|
|
*pw = *pwent;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw->pw_name = strdup (pwent->pw_name)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw->pw_passwd = strdup (pwent->pw_passwd)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw->pw_gecos = strdup (pwent->pw_gecos)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw->pw_dir = strdup (pwent->pw_dir)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(pw->pw_shell = strdup (pwent->pw_shell)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void *passwd_dup (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
return __pw_dup (pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void passwd_free (void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct passwd *pw = ent;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
free (pw->pw_name);
|
|
|
|
free (pw->pw_passwd);
|
|
|
|
free (pw->pw_gecos);
|
|
|
|
free (pw->pw_dir);
|
|
|
|
free (pw->pw_shell);
|
|
|
|
free (pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static const char *passwd_getname (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
return pw->pw_name;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void *passwd_parse (const char *line)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return (void *) sgetpwent (line);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static int passwd_put (const void *ent, FILE * file)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct passwd *pw = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
return (putpwent (pw, file) == -1) ? -1 : 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static struct commonio_ops passwd_ops = {
|
|
|
|
passwd_dup,
|
|
|
|
passwd_free,
|
|
|
|
passwd_getname,
|
|
|
|
passwd_parse,
|
|
|
|
passwd_put,
|
|
|
|
fgets,
|
|
|
|
fputs
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct commonio_db passwd_db = {
|
2007-10-07 17:16:07 +05:30
|
|
|
PASSWD_FILE, /* filename */
|
|
|
|
&passwd_ops, /* ops */
|
|
|
|
NULL, /* fp */
|
|
|
|
NULL, /* head */
|
|
|
|
NULL, /* tail */
|
|
|
|
NULL, /* cursor */
|
|
|
|
0, /* changed */
|
|
|
|
0, /* isopen */
|
|
|
|
0, /* locked */
|
|
|
|
0 /* readonly */
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_name (const char *filename)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_setname (&passwd_db, filename);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_lock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_lock (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_open (int mode)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_open (&passwd_db, mode);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
const struct passwd *pw_locate (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_locate (&passwd_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_update (const struct passwd *pw)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_update (&passwd_db, (const void *) pw);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_remove (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_remove (&passwd_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_rewind (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_rewind (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
const struct passwd *pw_next (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_next (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_close (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_close (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_unlock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_unlock (&passwd_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct commonio_entry *__pw_get_head (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
return passwd_db.head;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
void __pw_del_entry (const struct commonio_entry *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
commonio_del_entry (&passwd_db, ent);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:51 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct commonio_db *__pw_get_db (void)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
return &passwd_db;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static int pw_cmp (const void *p1, const void *p2)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
uid_t u1, u2;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if ((*(struct commonio_entry **) p1)->eptr == NULL)
|
2007-10-07 17:14:51 +05:30
|
|
|
return 1;
|
2007-10-07 17:16:07 +05:30
|
|
|
if ((*(struct commonio_entry **) p2)->eptr == NULL)
|
2007-10-07 17:14:51 +05:30
|
|
|
return -1;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
u1 = ((struct passwd *) (*(struct commonio_entry **) p1)->eptr)->pw_uid;
|
|
|
|
u2 = ((struct passwd *) (*(struct commonio_entry **) p2)->eptr)->pw_uid;
|
2007-10-07 17:14:51 +05:30
|
|
|
|
|
|
|
if (u1 < u2)
|
|
|
|
return -1;
|
|
|
|
else if (u1 > u2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort entries by uid */
|
2007-10-07 17:16:07 +05:30
|
|
|
int pw_sort ()
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_sort (&passwd_db, pw_cmp);
|
2007-10-07 17:14:51 +05:30
|
|
|
}
|