[comp.sources.misc] v14i009: libpw, password/group file library

root@lsrhs.UUCP (Mr System) (07/16/90)

Posting-number: Volume 14, Issue 9
Submitted-by: root@lsrhs.UUCP (Mr System)
Archive-name: libpw/part01

This is a library package that helps to sift around in the password file.
I have tested it thoroughly and have found no bugs on our 11/780 Ultrix 3.1.
Please distribute this to everyone.. I'm sure that someone can use it.
To follow, an interactive user editor should arrive to you within the
next few days.	- Jim

O /		O /		O /		O /		O /
 x		 x		 x		 x		 x
o \		o \		o \		o \		o \

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile README defs.h isuidused.c lockgrfile.c
#   lockpwfile.c putgrent.c sharfile sortgrp.c sortuid.c sortuser.c
#   unlockgrfile.c unlockpwfile.c
# Wrapped by pulsar@lsrhs on Fri Jul 13 15:31:00 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(1383 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
CC	      = gcc
CFLAGS        = -ansi -O -W
DEST	      = .
XEXTHDRS	      = /usr/include/grp.h \
X		/usr/include/pwd.h \
X		/usr/include/stdio.h \
X		/usr/include/sys/types.h
HDRS	      = defs.h
LDFLAGS	      = -ansi -O -W
LIBS	      = 
LINKER	      = gcc
MAKEFILE      = Makefile
OBJS	      = isuidused.o \
X		lockgrfile.o \
X		lockpwfile.o \
X		putgrent.o \
X		sortgrp.o \
X		sortuid.o \
X		sortuser.o \
X		unlockgrfile.o \
X		unlockpwfile.o
PRINT	      = prnt
PROGRAM	      = libpw.a
SRCS	      = isuidused.c \
X		lockgrfile.c \
X		lockpwfile.c \
X		putgrent.c \
X		sortgrp.c \
X		sortuid.c \
X		sortuser.c \
X		unlockgrfile.c \
X		unlockpwfile.c
X
all:		$(PROGRAM)
X
X$(PROGRAM):     $(OBJS)
X		@echo -n "Creating library..."
X		ar cr $(PROGRAM) $(OBJS)
X		ranlib $(PROGRAM)
X		@echo "done"
X
clean:;		@rm -f $(OBJS)
X
depend:;	@mkmf -f $(MAKEFILE) PROGRAM=$(PROGRAM) DEST=$(DEST)
X
X###
isuidused.o: /usr/include/stdio.h /usr/include/pwd.h \
X	/usr/include/sys/types.h
lockgrfile.o: /usr/include/stdio.h defs.h
lockpwfile.o: /usr/include/stdio.h defs.h
putgrent.o: /usr/include/stdio.h /usr/include/grp.h \
X	/usr/include/sys/types.h
sortgrp.o: /usr/include/stdio.h /usr/include/grp.h /usr/include/sys/types.h
sortuid.o: /usr/include/stdio.h /usr/include/pwd.h /usr/include/sys/types.h
sortuser.o: /usr/include/stdio.h /usr/include/pwd.h \
X	/usr/include/sys/types.h
unlockgrfile.o: defs.h
unlockpwfile.o: defs.h
END_OF_FILE
if test 1383 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(1559 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
X	This is a little something I put together once I began to write
password and group file editors.. If you haven't received one through the
news yet, one will be coming shortly.  This is a library package, which
I put in /usr/local/lib/libpw.a.  You use it by compiling with the
X-lpw option.  I, personally, enjoy using gcc and the -ansi flag to ensure
that this program was ansi compatible, and it is.  If you don't have gcc,
you either should get it or change the Makefile.  I don't suggest touching
defs.h, it was originally there for debugging purposes.  Please change the
Makefile to suit your system before you make.
X
C subroutine	Input	Returns		Purpose / Usefulness
X------------------------------------------------------------------------------
isuidused(x)	int	TRUE, FALSE	Checks to see if uid x is in use.
lockpwfile()	nothing	nothing		Waits for a lockfile and creates.
lockgrfile()	nothing	nothing		Same as above for group file.
unlockpwfile()	nothing	nothing		Removes the lock.
unlockgrfile()	nothing	nothing		Removes the lock for the group file.
sortgrp(grp)	char	int		Returns GID of group name 'grp'.
sortuser(unm)	char	TRUE, FALSE	Checks to see if username 'unm' in use.
sortuid(d)	int	int		Returns the next available UID after d.
putgrent(gr)	struct	nothing		A complement to getgrent().
X
Since nobody ever bothered to write putgrent, I did..
Sortuid(d) is a most useful tool.. You can organize the passwd file with
it, instead of tacking on new users to the end.
X
Replies, Flames, Comments, Bugs, Gross Errors to:
X
X	xait.xerox.com!lsrhs!pulsar
X
END_OF_FILE
if test 1559 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
chmod +x 'README'
# end of 'README'
fi
if test -f 'defs.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'defs.h'\"
else
echo shar: Extracting \"'defs.h'\" \(86 characters\)
sed "s/^X//" >'defs.h' <<'END_OF_FILE'
X/*
X * defs - lockfile defines
X */
X
X#define GTMP	"/etc/gtmp"
X#define PTMP	"/etc/ptmp"
X
END_OF_FILE
if test 86 -ne `wc -c <'defs.h'`; then
    echo shar: \"'defs.h'\" unpacked with wrong size!
fi
chmod +x 'defs.h'
# end of 'defs.h'
fi
if test -f 'isuidused.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'isuidused.c'\"
else
echo shar: Extracting \"'isuidused.c'\" \(332 characters\)
sed "s/^X//" >'isuidused.c' <<'END_OF_FILE'
X/*
X * isuidused(x) - if uid x is used, return 1 else 0
X */
X
X#include <stdio.h>
X#include <pwd.h>
X
struct passwd	*tmp;
X
isuidused(x)
int	x;
X{
X	lockpwfile();
X	setpwent();
X
X	while ((tmp = getpwent()) != NULL) {
X		if (tmp->pw_uid == x) {
X			unlockpwfile();
X			endpwent();
X			return(1);
X		}
X	}
X	unlockpwfile();
X	endpwent();
X	return(0);
X}
END_OF_FILE
if test 332 -ne `wc -c <'isuidused.c'`; then
    echo shar: \"'isuidused.c'\" unpacked with wrong size!
fi
chmod +x 'isuidused.c'
# end of 'isuidused.c'
fi
if test -f 'lockgrfile.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'lockgrfile.c'\"
else
echo shar: Extracting \"'lockgrfile.c'\" \(288 characters\)
sed "s/^X//" >'lockgrfile.c' <<'END_OF_FILE'
X/*
X * lockgrfile - check for GTMP, create it when available to
X */
X
X#include <stdio.h>
X#include "defs.h"
X
lockgrfile()
X{
X	FILE	*grp;
X
X	while ((grp = fopen(GTMP, "r")) != NULL) {
X		fclose(grp);
X/*		printf("Group file is locked.  Please wait...\n"); */
X		sleep(5);
X	}
X
X	creat(GTMP, 022);
X}
END_OF_FILE
if test 288 -ne `wc -c <'lockgrfile.c'`; then
    echo shar: \"'lockgrfile.c'\" unpacked with wrong size!
fi
chmod +x 'lockgrfile.c'
# end of 'lockgrfile.c'
fi
if test -f 'lockpwfile.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'lockpwfile.c'\"
else
echo shar: Extracting \"'lockpwfile.c'\" \(291 characters\)
sed "s/^X//" >'lockpwfile.c' <<'END_OF_FILE'
X/*
X * lockpwfile - check for lockfile PTMP, when free, create it.
X */
X
X#include <stdio.h>
X#include "defs.h"
X
lockpwfile()
X{
X	FILE	*pwd;
X
X	while ((pwd = fopen(PTMP, "r")) != NULL) {
X		fclose(pwd);
X/*		printf("Password file locked.  Please wait...\n"); */
X		sleep(5);
X	}
X
X	creat(PTMP, 022);
X}
END_OF_FILE
if test 291 -ne `wc -c <'lockpwfile.c'`; then
    echo shar: \"'lockpwfile.c'\" unpacked with wrong size!
fi
chmod +x 'lockpwfile.c'
# end of 'lockpwfile.c'
fi
if test -f 'putgrent.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'putgrent.c'\"
else
echo shar: Extracting \"'putgrent.c'\" \(358 characters\)
sed "s/^X//" >'putgrent.c' <<'END_OF_FILE'
X/*
X * putgrent(fp, ent) - since there is no putgrent I'm making one.
X */
X
X#include <stdio.h>
X#include <grp.h>
X
putgrent(fp, grp)
XFILE	*fp;
struct group	*grp;
X{
X	int	i;
X
X	fprintf(fp, "%s:*:%d:%s", grp->gr_name, grp->gr_gid, grp->gr_mem[0]);
X	for (i = 1; grp->gr_mem[i][0] != NULL; i++)
X		fprintf(fp, ",%s", grp->gr_mem[i]);
X	fprintf(fp, "\n");
X	fflush(fp);
X}
END_OF_FILE
if test 358 -ne `wc -c <'putgrent.c'`; then
    echo shar: \"'putgrent.c'\" unpacked with wrong size!
fi
chmod +x 'putgrent.c'
# end of 'putgrent.c'
fi
if test -f 'sharfile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sharfile'\"
else
echo shar: Extracting \"'sharfile'\" \(0 characters\)
sed "s/^X//" >'sharfile' <<'END_OF_FILE'
END_OF_FILE
if test 0 -ne `wc -c <'sharfile'`; then
    echo shar: \"'sharfile'\" unpacked with wrong size!
fi
# end of 'sharfile'
fi
if test -f 'sortgrp.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sortgrp.c'\"
else
echo shar: Extracting \"'sortgrp.c'\" \(410 characters\)
sed "s/^X//" >'sortgrp.c' <<'END_OF_FILE'
X/*
X * sortgrp(str) - returns the appropriate gid for group name str
X */
X
X#include <stdio.h>
X#include <grp.h>
X
struct group	*gr;
X
sortgrp(str)
char	str[20];
X{
X	lockgrfile();
X	setgrent();
X	
X	while ((gr = getgrent()) != NULL) {
X		if (strcmp(gr->gr_name, str) == 0) { /* Compare gname */
X			endgrent();
X			unlockgrfile();
X			return(gr->gr_gid);	/* return gid */
X		}
X	}
X	unlockgrfile();
X	endgrent();
X	return(-1);
X}
END_OF_FILE
if test 410 -ne `wc -c <'sortgrp.c'`; then
    echo shar: \"'sortgrp.c'\" unpacked with wrong size!
fi
chmod +x 'sortgrp.c'
# end of 'sortgrp.c'
fi
if test -f 'sortuid.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sortuid.c'\"
else
echo shar: Extracting \"'sortuid.c'\" \(445 characters\)
sed "s/^X//" >'sortuid.c' <<'END_OF_FILE'
X/*
X * sortuid(d) - check for next available uid after d
X */
X
X#include <stdio.h>
X#include <pwd.h>
X
struct passwd	*tmp;
X
int sortuid(d)
int	d;
X{
X	int	i = d-1;
X
X	lockpwfile();
X	setpwent();
X
X	for (;;) {
X		if ((tmp = getpwent()) == NULL) {
X			unlockpwfile();
X			endpwent();
X			return 65534;
X		}
X		if (tmp->pw_uid < d)
X			continue;
X		if (tmp->pw_uid == i+1) {
X			i++;
X			continue;
X		} else {
X			unlockpwfile();
X			endpwent();
X			return(++i);
X		}
X	}
X}
END_OF_FILE
if test 445 -ne `wc -c <'sortuid.c'`; then
    echo shar: \"'sortuid.c'\" unpacked with wrong size!
fi
chmod +x 'sortuid.c'
# end of 'sortuid.c'
fi
if test -f 'sortuser.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'sortuser.c'\"
else
echo shar: Extracting \"'sortuser.c'\" \(377 characters\)
sed "s/^X//" >'sortuser.c' <<'END_OF_FILE'
X/*
X * sortuser(uname) - look for uname in /etc/passwd.  TRUE=1, FALSE=0
X */
X
X#include <stdio.h>
X#include <pwd.h>
X
struct passwd *tmp;
X
sortuser(uname)
char	uname[10];
X{
X	lockpwfile();
X	setpwent();
X
X	while ((tmp = getpwent()) != NULL) {
X		if (strcmp(tmp->pw_name, uname) == 0) {
X			endpwent();
X			unlockpwfile();
X			return(1);
X		}
X	}
X	unlockpwfile();
X	endpwent();
X	return(0);
X}
END_OF_FILE
if test 377 -ne `wc -c <'sortuser.c'`; then
    echo shar: \"'sortuser.c'\" unpacked with wrong size!
fi
chmod +x 'sortuser.c'
# end of 'sortuser.c'
fi
if test -f 'unlockgrfile.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'unlockgrfile.c'\"
else
echo shar: Extracting \"'unlockgrfile.c'\" \(113 characters\)
sed "s/^X//" >'unlockgrfile.c' <<'END_OF_FILE'
X/*
X * unlockgrfile - unlink GTMP to free up group file
X */
X
X#include "defs.h"
X
unlockgrfile()
X{
X	unlink(GTMP);
X}
END_OF_FILE
if test 113 -ne `wc -c <'unlockgrfile.c'`; then
    echo shar: \"'unlockgrfile.c'\" unpacked with wrong size!
fi
chmod +x 'unlockgrfile.c'
# end of 'unlockgrfile.c'
fi
if test -f 'unlockpwfile.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'unlockpwfile.c'\"
else
echo shar: Extracting \"'unlockpwfile.c'\" \(110 characters\)
sed "s/^X//" >'unlockpwfile.c' <<'END_OF_FILE'
X/*
X * unlockpwfile - unlink the lockfile for passwd
X */
X
X#include "defs.h"
X
unlockpwfile()
X{
X	unlink(PTMP);
X}
END_OF_FILE
if test 110 -ne `wc -c <'unlockpwfile.c'`; then
    echo shar: \"'unlockpwfile.c'\" unpacked with wrong size!
fi
chmod +x 'unlockpwfile.c'
# end of 'unlockpwfile.c'
fi
echo shar: End of shell archive.
exit 0