[svn-upgrade] Integrating new upstream version, shadow (20000902)

This commit is contained in:
nekral-guest
2007-10-07 11:44:32 +00:00
parent be1f391d2a
commit d6e9891ad7
41 changed files with 910 additions and 720 deletions

View File

@ -2,7 +2,7 @@
#include <config.h>
#include "rcsid.h"
RCSID("$Id: commonio.c,v 1.15 2000/08/26 18:27:17 marekm Exp $")
RCSID("$Id: commonio.c,v 1.16 2000/09/02 18:40:42 marekm Exp $")
#include "defines.h"
#include <sys/stat.h>
@ -29,9 +29,8 @@ static int name_is_nis(const char *);
static int write_all(const struct commonio_db *);
static struct commonio_entry *find_entry_by_name(struct commonio_db *, const char *);
#ifdef HAVE_LCKPWDF
static int lock_count = 0;
#endif
static int nscd_need_reload = 0;
static int
check_link_count(const char *file)
@ -236,6 +235,7 @@ commonio_lock_nowait(struct commonio_db *db)
snprintf(lock, sizeof lock, "%s.lock", db->filename);
if (do_lock_file(file, lock)) {
db->locked = 1;
lock_count++;
return 1;
}
return 0;
@ -245,31 +245,30 @@ commonio_lock_nowait(struct commonio_db *db)
int
commonio_lock(struct commonio_db *db)
{
int i;
#ifdef HAVE_LCKPWDF
/*
* only if the system libc has a real lckpwdf() - the one from
* lockpw.c calls us and would cause infinite recursion!
*/
if (db->use_lckpwdf) {
/*
* Call lckpwdf() on the first lock.
* If it succeeds, call *_lock() only once
* (no retries, it should always succeed).
*/
if (lock_count == 0) {
if (lckpwdf() == -1)
return 0; /* failure */
}
if (!commonio_lock_nowait(db)) {
ulckpwdf();
/*
* Call lckpwdf() on the first lock.
* If it succeeds, call *_lock() only once
* (no retries, it should always succeed).
*/
if (lock_count == 0) {
if (lckpwdf() == -1)
return 0; /* failure */
}
lock_count++;
return 1; /* success */
}
#endif
if (commonio_lock_nowait(db))
return 1; /* success */
ulckpwdf();
return 0; /* failure */
#else
int i;
/*
* lckpwdf() not used - do it the old way.
*/
@ -290,6 +289,53 @@ commonio_lock(struct commonio_db *db)
return 0;
}
return 0; /* failure */
#endif
}
#ifndef NSCD_PID_FILE
#define NSCD_PID_FILE "/var/run/nscd.pid"
#endif
/*
reload_nscd() is called after updating all of the password files,
to tell the "nscd" caching daemon to clear its cache.
Very loosely based on a shadow-utils patch from Red Hat.
*/
static void
reload_nscd(void)
{
FILE *pidfile;
int pid;
pidfile = fopen(NSCD_PID_FILE, "r");
if (pidfile) {
pid = 0;
fscanf(pidfile, "%d", &pid);
if (pid > 0)
kill(pid, SIGHUP);
fclose(pidfile);
}
}
static void
dec_lock_count(void)
{
if (lock_count > 0) {
lock_count--;
if (lock_count == 0) {
/* Tell nscd when lock count goes to zero,
if any of the files were changed. */
if (nscd_need_reload) {
reload_nscd();
nscd_need_reload = 0;
}
#ifdef HAVE_LCKPWDF
ulckpwdf();
#endif
}
}
}
@ -300,8 +346,11 @@ commonio_unlock(struct commonio_db *db)
if (db->isopen) {
db->readonly = 1;
if (!commonio_close(db))
if (!commonio_close(db)) {
if (db->locked)
dec_lock_count();
return 0;
}
}
if (db->locked) {
/*
@ -311,13 +360,7 @@ commonio_unlock(struct commonio_db *db)
db->locked = 0;
snprintf(lock, sizeof lock, "%s.lock", db->filename);
unlink(lock);
#ifdef HAVE_LCKPWDF
if (db->use_lckpwdf && lock_count > 0) {
lock_count--;
if (lock_count == 0)
ulckpwdf();
}
#endif
dec_lock_count();
return 1;
}
return 0;
@ -353,7 +396,6 @@ name_is_nis(const char *n)
#endif
#if KEEP_NIS_AT_END
/* prototype */
static void add_one_entry_nis(struct commonio_db *, struct commonio_entry *);
static void
@ -377,16 +419,20 @@ add_one_entry_nis(struct commonio_db *db, struct commonio_entry *newp)
}
#endif /* KEEP_NIS_AT_END */
/* Initial buffer size, as well as increment if not sufficient
(for reading very long lines in group files). */
#define BUFLEN 4096
int
commonio_open(struct commonio_db *db, int mode)
{
char buf[8192];
char *cp;
char *buf;
char *cp;
char *line;
struct commonio_entry *p;
void *eptr;
int flags = mode;
int buflen;
mode &= ~O_CREAT;
@ -417,12 +463,25 @@ commonio_open(struct commonio_db *db, int mode)
return 0;
}
while (db->ops->fgets(buf, sizeof buf, db->fp)) {
buflen = BUFLEN;
buf = (char *) malloc(buflen);
if (!buf)
goto cleanup;
while (db->ops->fgets(buf, buflen, db->fp)) {
while (!(cp = strrchr(buf, '\n')) && !feof(db->fp)) {
buflen += BUFLEN;
cp = (char *) realloc(buf, buflen);
if (!cp)
goto cleanup_buf;
buf = cp;
db->ops->fgets(buf + buflen - BUFLEN, BUFLEN, db->fp);
}
if ((cp = strrchr(buf, '\n')))
*cp = '\0';
if (!(line = strdup(buf)))
goto cleanup;
goto cleanup_buf;
if (name_is_nis(line)) {
eptr = NULL;
@ -444,6 +503,7 @@ commonio_open(struct commonio_db *db, int mode)
}
db->isopen = 1;
free(buf);
return 1;
cleanup_entry:
@ -451,6 +511,8 @@ cleanup_entry:
db->ops->free(eptr);
cleanup_line:
free(line);
cleanup_buf:
free(buf);
cleanup:
free_linked_list(db);
fclose(db->fp);
@ -564,6 +626,8 @@ commonio_close(struct commonio_db *db)
if (rename(buf, db->filename))
goto fail;
nscd_need_reload = 1;
success:
free_linked_list(db);
return 1;