2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:17:01 +05:30
|
|
|
#ident "$Id: groupio.c,v 1.13 2005/08/31 17:24:56 kloczek Exp $"
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "commonio.h"
|
|
|
|
#include "groupio.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
extern int putgrent (const struct group *, FILE *);
|
|
|
|
extern struct group *sgetgrent (const char *);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct group *__gr_dup (const struct group *grent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int i;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(gr = (struct group *) malloc (sizeof *gr)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
|
|
|
*gr = *grent;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(gr->gr_name = strdup (grent->gr_name)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
2007-10-07 17:16:07 +05:30
|
|
|
if (!(gr->gr_passwd = strdup (grent->gr_passwd)))
|
2007-10-07 17:14:02 +05:30
|
|
|
return NULL;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
for (i = 0; grent->gr_mem[i]; i++);
|
|
|
|
gr->gr_mem = (char **) malloc ((i + 1) * sizeof (char *));
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!gr->gr_mem)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; grent->gr_mem[i]; i++) {
|
2007-10-07 17:16:07 +05:30
|
|
|
gr->gr_mem[i] = strdup (grent->gr_mem[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (!gr->gr_mem[i])
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gr->gr_mem[i] = NULL;
|
|
|
|
return gr;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void *group_dup (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct group *gr = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
return __gr_dup (gr);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void group_free (void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct group *gr = ent;
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
free (gr->gr_name);
|
|
|
|
free (gr->gr_passwd);
|
|
|
|
while (*(gr->gr_mem)) {
|
|
|
|
free (*(gr->gr_mem));
|
2007-10-07 17:14:02 +05:30
|
|
|
gr->gr_mem++;
|
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
free (gr);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static const char *group_getname (const void *ent)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct group *gr = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
return gr->gr_name;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static void *group_parse (const char *line)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return (void *) sgetgrent (line);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
static int group_put (const void *ent, FILE * file)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const struct group *gr = ent;
|
2007-10-07 17:16:07 +05:30
|
|
|
|
|
|
|
return (putgrent (gr, file) == -1) ? -1 : 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static struct commonio_ops group_ops = {
|
|
|
|
group_dup,
|
|
|
|
group_free,
|
|
|
|
group_getname,
|
|
|
|
group_parse,
|
|
|
|
group_put,
|
|
|
|
fgetsx,
|
|
|
|
fputsx
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct commonio_db group_db = {
|
2007-10-07 17:16:07 +05:30
|
|
|
GROUP_FILE, /* filename */
|
|
|
|
&group_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 gr_name (const char *filename)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_setname (&group_db, filename);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_lock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_lock (&group_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_open (int mode)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_open (&group_db, mode);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
const struct group *gr_locate (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_locate (&group_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_update (const struct group *gr)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_update (&group_db, (const void *) gr);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_remove (const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_remove (&group_db, name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_rewind (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_rewind (&group_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
const struct group *gr_next (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_next (&group_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_close (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_close (&group_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_unlock (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_unlock (&group_db);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
void __gr_set_changed (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
group_db.changed = 1;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct commonio_entry *__gr_get_head (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
return group_db.head;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
struct commonio_db *__gr_get_db (void)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
return &group_db;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
void __gr_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 (&group_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
|
|
|
static int gr_cmp (const void *p1, const void *p2)
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
|
|
|
gid_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 group *) (*(struct commonio_entry **) p1)->eptr)->gr_gid;
|
|
|
|
u2 = ((struct group *) (*(struct commonio_entry **) p2)->eptr)->gr_gid;
|
2007-10-07 17:14:51 +05:30
|
|
|
|
|
|
|
if (u1 < u2)
|
|
|
|
return -1;
|
|
|
|
else if (u1 > u2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:16:25 +05:30
|
|
|
/* Sort entries by GID */
|
2007-10-07 17:16:07 +05:30
|
|
|
int gr_sort ()
|
2007-10-07 17:14:51 +05:30
|
|
|
{
|
2007-10-07 17:16:07 +05:30
|
|
|
return commonio_sort (&group_db, gr_cmp);
|
2007-10-07 17:14:51 +05:30
|
|
|
}
|