[svn-upgrade] Integrating new upstream version, shadow (4.0.8)
This commit is contained in:
261
lib/gshadow.c
261
lib/gshadow.c
@@ -30,47 +30,38 @@
|
||||
#include <config.h>
|
||||
|
||||
/* Newer versions of Linux libc already have shadow support. */
|
||||
#if defined(SHADOWGRP) && !defined(HAVE_SHADOWGRP) /*{*/
|
||||
#if defined(SHADOWGRP) && !defined(HAVE_SHADOWGRP) /*{ */
|
||||
|
||||
#include "rcsid.h"
|
||||
RCSID("$Id: gshadow.c,v 1.6 1998/04/02 21:51:43 marekm Exp $")
|
||||
|
||||
RCSID ("$Id: gshadow.c,v 1.9 2005/04/06 04:26:05 kloczek Exp $")
|
||||
#include <stdio.h>
|
||||
#include "prototypes.h"
|
||||
#include "defines.h"
|
||||
|
||||
#ifdef NDBM
|
||||
#include <ndbm.h>
|
||||
#include <fcntl.h>
|
||||
DBM *sg_dbm;
|
||||
int sg_dbm_mode = -1;
|
||||
static int dbmopened;
|
||||
static int dbmerror;
|
||||
#endif
|
||||
static FILE *shadow;
|
||||
static char sgrbuf[BUFSIZ * 4];
|
||||
static char **members = NULL;
|
||||
static size_t nmembers = 0;
|
||||
static char **admins = NULL;
|
||||
static size_t nadmins = 0;
|
||||
static struct sgrp sgroup;
|
||||
|
||||
#define MAXMEM 1024
|
||||
|
||||
static FILE *shadow;
|
||||
static char sgrbuf[BUFSIZ*4];
|
||||
static char *members[MAXMEM+1];
|
||||
static char *admins[MAXMEM+1];
|
||||
static struct sgrp sgroup;
|
||||
|
||||
extern char *fgetsx();
|
||||
extern int fputsx();
|
||||
extern char *fgetsx ();
|
||||
extern int fputsx ();
|
||||
|
||||
#define FIELDS 4
|
||||
|
||||
#ifdef USE_NIS
|
||||
static int nis_used;
|
||||
static int nis_ignore;
|
||||
static enum { native, start, middle, native2 } nis_state;
|
||||
static int nis_bound;
|
||||
static char *nis_domain;
|
||||
static char *nis_key;
|
||||
static int nis_keylen;
|
||||
static char *nis_val;
|
||||
static int nis_vallen;
|
||||
static int nis_used;
|
||||
static int nis_ignore;
|
||||
static enum { native, start, middle, native2 } nis_state;
|
||||
static int nis_bound;
|
||||
static char *nis_domain;
|
||||
static char *nis_key;
|
||||
static int nis_keylen;
|
||||
static char *nis_val;
|
||||
static int nis_vallen;
|
||||
|
||||
#define IS_NISCHAR(c) ((c)=='+')
|
||||
#endif
|
||||
|
||||
@@ -80,10 +71,9 @@ static int nis_vallen;
|
||||
* __setsgNIS - turn on or off NIS searches
|
||||
*/
|
||||
|
||||
void
|
||||
__setsgNIS(int flag)
|
||||
void __setsgNIS (int flag)
|
||||
{
|
||||
nis_ignore = ! flag;
|
||||
nis_ignore = !flag;
|
||||
|
||||
if (nis_ignore)
|
||||
nis_used = 0;
|
||||
@@ -93,8 +83,7 @@ __setsgNIS(int flag)
|
||||
* bind_nis - bind to NIS server
|
||||
*/
|
||||
|
||||
static int
|
||||
bind_nis(void)
|
||||
static int bind_nis (void)
|
||||
{
|
||||
if (yp_get_default_domain (&nis_domain))
|
||||
return -1;
|
||||
@@ -104,82 +93,53 @@ bind_nis(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
static char **
|
||||
list(char *s, char **l)
|
||||
static char **list (char *s, char **list[], size_t * nlist)
|
||||
{
|
||||
int nmembers = 0;
|
||||
char **ptr = *list;
|
||||
size_t nelem = *nlist, size;
|
||||
|
||||
while (s && *s) {
|
||||
l[nmembers++] = s;
|
||||
if ((s = strchr (s, ',')))
|
||||
*s++ = '\0';
|
||||
while (s != NULL && *s != '\0') {
|
||||
size = (nelem + 1) * sizeof (ptr);
|
||||
if ((ptr = realloc (*list, size)) != NULL) {
|
||||
ptr[nelem++] = s;
|
||||
*list = ptr;
|
||||
*nlist = nelem;
|
||||
if ((s = strchr (s, ',')))
|
||||
*s++ = '\0';
|
||||
}
|
||||
}
|
||||
l[nmembers] = (char *) 0;
|
||||
return l;
|
||||
size = (nelem + 1) * sizeof (ptr);
|
||||
if ((ptr = realloc (*list, size)) != NULL) {
|
||||
ptr[nelem] = '\0';
|
||||
*list = ptr;
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void
|
||||
setsgent(void)
|
||||
void setsgent (void)
|
||||
{
|
||||
#ifdef NDBM
|
||||
int mode;
|
||||
#endif /* NDBM */
|
||||
|
||||
#ifdef USE_NIS
|
||||
nis_state = native;
|
||||
#endif
|
||||
if (shadow)
|
||||
rewind (shadow);
|
||||
else
|
||||
shadow = fopen(SGROUP_FILE, "r");
|
||||
|
||||
/*
|
||||
* Attempt to open the DBM files if they have never been opened
|
||||
* and an error has never been returned.
|
||||
*/
|
||||
|
||||
#ifdef NDBM
|
||||
if (! dbmerror && ! dbmopened) {
|
||||
char dbmfiles[BUFSIZ];
|
||||
|
||||
strcpy (dbmfiles, SGROUP_PAG_FILE);
|
||||
|
||||
if (sg_dbm_mode == -1)
|
||||
mode = O_RDWR;
|
||||
else
|
||||
mode = (sg_dbm_mode == O_RDWR) ? O_RDWR:O_RDONLY;
|
||||
|
||||
if (access(dbmfiles, F_OK) ||
|
||||
(! (sg_dbm = dbm_open(SGROUP_FILE, mode, 0))))
|
||||
dbmerror = 1;
|
||||
else
|
||||
dbmopened = 1;
|
||||
}
|
||||
#endif /* NDBM */
|
||||
shadow = fopen (SGROUP_FILE, "r");
|
||||
}
|
||||
|
||||
void
|
||||
endsgent(void)
|
||||
void endsgent (void)
|
||||
{
|
||||
if (shadow)
|
||||
(void) fclose (shadow);
|
||||
|
||||
shadow = (FILE *) 0;
|
||||
#ifdef NDBM
|
||||
if (dbmopened && sg_dbm) {
|
||||
dbm_close (sg_dbm);
|
||||
dbmopened = 0;
|
||||
sg_dbm = 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
struct sgrp *
|
||||
sgetsgent(const char *string)
|
||||
struct sgrp *sgetsgent (const char *string)
|
||||
{
|
||||
char *fields[FIELDS];
|
||||
char *cp;
|
||||
int i;
|
||||
char *fields[FIELDS];
|
||||
char *cp;
|
||||
int i;
|
||||
|
||||
strncpy (sgrbuf, string, (int) sizeof sgrbuf - 1);
|
||||
sgrbuf[sizeof sgrbuf - 1] = '\0';
|
||||
@@ -192,7 +152,7 @@ sgetsgent(const char *string)
|
||||
* all 4 of them and save the starting addresses in fields[].
|
||||
*/
|
||||
|
||||
for (cp = sgrbuf, i = 0;i < FIELDS && cp;i++) {
|
||||
for (cp = sgrbuf, i = 0; i < FIELDS && cp; i++) {
|
||||
fields[i] = cp;
|
||||
if ((cp = strchr (cp, ':')))
|
||||
*cp++ = '\0';
|
||||
@@ -205,7 +165,7 @@ sgetsgent(const char *string)
|
||||
|
||||
if (cp || i != FIELDS)
|
||||
#ifdef USE_NIS
|
||||
if (! IS_NISCHAR (fields[0][0]))
|
||||
if (!IS_NISCHAR (fields[0][0]))
|
||||
return 0;
|
||||
else
|
||||
nis_used = 1;
|
||||
@@ -215,8 +175,18 @@ sgetsgent(const char *string)
|
||||
|
||||
sgroup.sg_name = fields[0];
|
||||
sgroup.sg_passwd = fields[1];
|
||||
sgroup.sg_adm = list (fields[2], admins);
|
||||
sgroup.sg_mem = list (fields[3], members);
|
||||
if (nadmins) {
|
||||
nadmins = 0;
|
||||
free (admins);
|
||||
admins = NULL;
|
||||
}
|
||||
if (nmembers) {
|
||||
nmembers = 0;
|
||||
free (members);
|
||||
members = NULL;
|
||||
}
|
||||
sgroup.sg_adm = list (fields[2], &admins, &nadmins);
|
||||
sgroup.sg_mem = list (fields[3], &members, &nmembers);
|
||||
|
||||
return &sgroup;
|
||||
}
|
||||
@@ -228,13 +198,12 @@ sgetsgent(const char *string)
|
||||
* converts it to a (struct sgrp). NULL is returned on EOF.
|
||||
*/
|
||||
|
||||
struct sgrp *
|
||||
fgetsgent(FILE *fp)
|
||||
struct sgrp *fgetsgent (FILE * fp)
|
||||
{
|
||||
char buf[sizeof sgrbuf];
|
||||
char *cp;
|
||||
char buf[sizeof sgrbuf];
|
||||
char *cp;
|
||||
|
||||
if (! fp)
|
||||
if (!fp)
|
||||
return (0);
|
||||
|
||||
#ifdef USE_NIS
|
||||
@@ -258,19 +227,18 @@ fgetsgent(FILE *fp)
|
||||
* getsgent - get a single shadow group entry
|
||||
*/
|
||||
|
||||
struct sgrp *
|
||||
getsgent(void)
|
||||
struct sgrp *getsgent (void)
|
||||
{
|
||||
#ifdef USE_NIS
|
||||
int nis_1_group = 0;
|
||||
struct sgrp *val;
|
||||
char buf[BUFSIZ];
|
||||
int nis_1_group = 0;
|
||||
struct sgrp *val;
|
||||
char buf[BUFSIZ];
|
||||
#endif
|
||||
if (! shadow)
|
||||
if (!shadow)
|
||||
setsgent ();
|
||||
|
||||
#ifdef USE_NIS
|
||||
again:
|
||||
again:
|
||||
/*
|
||||
* See if we are reading from the local file.
|
||||
*/
|
||||
@@ -282,7 +250,7 @@ again:
|
||||
* NULL right away if there is none.
|
||||
*/
|
||||
|
||||
if (! (val = fgetsgent (shadow)))
|
||||
if (!(val = fgetsgent (shadow)))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
@@ -328,15 +296,15 @@ again:
|
||||
}
|
||||
if (nis_state == start) {
|
||||
if (yp_first (nis_domain, "gshadow.byname", &nis_key,
|
||||
&nis_keylen, &nis_val, &nis_vallen)) {
|
||||
&nis_keylen, &nis_val, &nis_vallen)) {
|
||||
nis_state = native2;
|
||||
goto again;
|
||||
}
|
||||
nis_state = middle;
|
||||
} else if (nis_state == middle) {
|
||||
if (yp_next (nis_domain, "gshadow.byname", nis_key,
|
||||
nis_keylen, &nis_key, &nis_keylen,
|
||||
&nis_val, &nis_vallen)) {
|
||||
nis_keylen, &nis_key, &nis_keylen,
|
||||
&nis_val, &nis_vallen)) {
|
||||
nis_state = native2;
|
||||
goto again;
|
||||
}
|
||||
@@ -352,61 +320,35 @@ again:
|
||||
* getsgnam - get a shadow group entry by name
|
||||
*/
|
||||
|
||||
struct sgrp *
|
||||
getsgnam(const char *name)
|
||||
struct sgrp *getsgnam (const char *name)
|
||||
{
|
||||
struct sgrp *sgrp;
|
||||
#ifdef NDBM
|
||||
datum key;
|
||||
datum content;
|
||||
#endif
|
||||
struct sgrp *sgrp;
|
||||
|
||||
#ifdef USE_NIS
|
||||
char buf[BUFSIZ];
|
||||
static char save_name[16];
|
||||
int nis_disabled = 0;
|
||||
char buf[BUFSIZ];
|
||||
static char save_name[16];
|
||||
int nis_disabled = 0;
|
||||
#endif
|
||||
|
||||
setsgent ();
|
||||
|
||||
#ifdef NDBM
|
||||
|
||||
/*
|
||||
* If the DBM file are now open, create a key for this group and
|
||||
* try to fetch the entry from the database. A matching record
|
||||
* will be unpacked into a static structure and returned to
|
||||
* the user.
|
||||
*/
|
||||
|
||||
if (dbmopened) {
|
||||
key.dsize = strlen (name);
|
||||
key.dptr = (void *) name;
|
||||
|
||||
content = dbm_fetch (sg_dbm, key);
|
||||
if (content.dptr != 0) {
|
||||
memcpy (sgrbuf, content.dptr, content.dsize);
|
||||
sgroup.sg_mem = members;
|
||||
sgroup.sg_adm = admins;
|
||||
sgr_unpack (sgrbuf, content.dsize, &sgroup);
|
||||
return &sgroup;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_NIS
|
||||
if (nis_used) {
|
||||
again:
|
||||
again:
|
||||
|
||||
/*
|
||||
* Search the gshadow.byname map for this group.
|
||||
*/
|
||||
|
||||
if (! nis_bound)
|
||||
if (!nis_bound)
|
||||
bind_nis ();
|
||||
|
||||
if (nis_bound) {
|
||||
char *cp;
|
||||
char *cp;
|
||||
|
||||
if (yp_match (nis_domain, "gshadow.byname", name,
|
||||
strlen (name), &nis_val, &nis_vallen) == 0) {
|
||||
strlen (name), &nis_val,
|
||||
&nis_vallen) == 0) {
|
||||
if (cp = strchr (nis_val, '\n'))
|
||||
*cp = '\0';
|
||||
|
||||
@@ -448,24 +390,23 @@ again:
|
||||
* opposite of fgetsgent.
|
||||
*/
|
||||
|
||||
int
|
||||
putsgent(const struct sgrp *sgrp, FILE *fp)
|
||||
int putsgent (const struct sgrp *sgrp, FILE * fp)
|
||||
{
|
||||
char *buf, *cp;
|
||||
int i;
|
||||
size_t size;
|
||||
|
||||
if (! fp || ! sgrp)
|
||||
if (!fp || !sgrp)
|
||||
return -1;
|
||||
|
||||
/* calculate the required buffer size */
|
||||
size = strlen(sgrp->sg_name) + strlen(sgrp->sg_passwd) + 10;
|
||||
size = strlen (sgrp->sg_name) + strlen (sgrp->sg_passwd) + 10;
|
||||
for (i = 0; sgrp->sg_adm && sgrp->sg_adm[i]; i++)
|
||||
size += strlen(sgrp->sg_adm[i]) + 1;
|
||||
size += strlen (sgrp->sg_adm[i]) + 1;
|
||||
for (i = 0; sgrp->sg_mem && sgrp->sg_mem[i]; i++)
|
||||
size += strlen(sgrp->sg_mem[i]) + 1;
|
||||
size += strlen (sgrp->sg_mem[i]) + 1;
|
||||
|
||||
buf = malloc(size);
|
||||
buf = malloc (size);
|
||||
if (!buf)
|
||||
return -1;
|
||||
cp = buf;
|
||||
@@ -487,7 +428,7 @@ putsgent(const struct sgrp *sgrp, FILE *fp)
|
||||
* with a ",".
|
||||
*/
|
||||
|
||||
for (i = 0;sgrp->sg_adm[i];i++) {
|
||||
for (i = 0; sgrp->sg_adm[i]; i++) {
|
||||
if (i > 0)
|
||||
*cp++ = ',';
|
||||
|
||||
@@ -500,7 +441,7 @@ putsgent(const struct sgrp *sgrp, FILE *fp)
|
||||
* Now do likewise with the group members.
|
||||
*/
|
||||
|
||||
for (i = 0;sgrp->sg_mem[i];i++) {
|
||||
for (i = 0; sgrp->sg_mem[i]; i++) {
|
||||
if (i > 0)
|
||||
*cp++ = ',';
|
||||
|
||||
@@ -515,14 +456,14 @@ putsgent(const struct sgrp *sgrp, FILE *fp)
|
||||
* continuation conventions.
|
||||
*/
|
||||
|
||||
if (fputsx(buf, fp) == EOF) {
|
||||
free(buf);
|
||||
if (fputsx (buf, fp) == EOF) {
|
||||
free (buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
free (buf);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
extern int errno; /* warning: ANSI C forbids an empty source file */
|
||||
#endif /*} SHADOWGRP */
|
||||
extern int errno; /* warning: ANSI C forbids an empty source file */
|
||||
#endif /*} SHADOWGRP */
|
||||
|
Reference in New Issue
Block a user