rsalz@bbn.com (Rich Salz) (02/11/88)
Submitted-by: Michael Saboff <hplabs!hpirs!mike> Posting-number: Volume 13, Issue 34 Archive-name: pwget [ This is a small utility to get information from the password file. Programs (like rn stuff :-) that might other do grep or ypmatch can use this instead. --r$ ] Michael Saboff hplabs!hpda!mike Phone: (408) 447-5976 # This is a shell archive. Remove anything before this line, # then unpack it by saving it in a file and typing "sh file". # This archive contains: # README Makefile pwget.1 pwget.c LANG=""; export LANG echo x - README cat >README <<'@EOF' ---NOTICE--- This is code is available to the public free of charge. Hewlett-Packard Company asks that it be given author recognition. The information in this document is subject to change without notice. THIS MATERIAL IS PROVIDED "AS IS". HEWLETT-PACKARD MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Hewlett-Packard shall not be liable for errors contained herein or for incidental or consequential damages or for any claim of any kind in connection with the furnishing, performance or use of this material. Hewlett-Packard assumes no responsibility for the use, reliability, or support of this software. ---END OF NOTICE--- These utilities provided a common command level access to /etc/group and /etc/passwd much in the same way as getgrent(), getpwent(), et al. This is especially useful if your system is running NFS and Yellow Pages. With Yellow pages, there is no easy command level interface for this information that will give you the same results as the libc routines. These routines use "getopt()" so make sure your system has it. Edit the Makefile for your machine/os and for the owner, group, mode and destination of the binary and the man page. Install with: make install. File list: README - This file. Makefile - Makefile. pwget.1 - Man Page. pwget.c - Command source. @EOF chmod 664 README echo x - Makefile cat >Makefile <<'@EOF' # Configure CFLAGS/LDFLAGS for your machine/OS # for HP-UX and other Sys V systems: CFLAGS= -O -DSYSV # for VAX 4.[23] BSD, SunOS and other BSD based systems: # CFLAGS= -O -DBSD # Need getopt for pwget, include library that has it. # System V based system (like HP-UX), 4.3BSD and SunOS have null LDFLAGS. LDFLAGS= # 4.2BSD requires getopt from someplace. # LDFLAGS= -lgetopt MODE=755 OWNER=bin GROUP=bin DEST=dest MANMODE=444 MANOWNER=bin MANGROUP=bin MANDEST=man all: grget pwget pwget.cat install: installcmd installman installcmd: pwget strip pwget # use install for BSD and cpset for SYSV # install -c -o $(OWNER) -g $(GROUP) -m $(MODE) pwget $(DEST) cpset pwget $(DEST) $(MODE) $(OWNER) $(GROUP) ln $(DEST)/pwget $(DEST)/grget installman: pwget.1 # use install for BSD and cpset for SYSV # install -c -o $(MANOWNER) -g $(MANGROUP) -m $(MANMODE) pwget.1 $(MANDEST) cpset pwget.1 $(MANDEST) $(MANMODE) $(MANOWNER) $(MANGROUP) grget: pwget ln pwget grget pwget: pwget.o cc $(LDFLAGS) -o pwget pwget.o pwget.o: pwget.c cc $(CFLAGS) -c pwget.c pwget.cat: pwget.1 tbl pwget.1 | nroff -man -T > pwget.cat clean: /bin/rm -f pwget.o pwget grget clobber: clean @EOF chmod 664 Makefile echo x - pwget.1 cat >pwget.1 <<'@EOF' .TH PWGET UTIL "" "" "" .ad b .SH NAME pwget, grget \- get password and group information .SH SYNOPSIS .B pwget .RB [ " \-n" " name" .RB | " \-u" " uid " ] .PP .B grget .RB [ " \-n" " name" .RB | " \-g" " gid " ] .br .SH DESCRIPTION .I Pwget\^ and .I grget\^ are used to access the information found in .B /etc/passwd and .BR /etc/group . These routines provide a common access method whether using the Yellow Page network database or not. The output of these routines is sent to standard output. With no options, .I pwget and .I grget output all of the data found using .IR getpwent( LIBC ) and .IR getgrent( LIBC ) respectively. When options are given, only specified entries are searched for. .PP The options for .I pwget are: .RS .TP .8i .BI \-n " name" Output the first entry that matches using .BI getpwnam( name ). .TP .BI \-u " uid" Output the first entry that matches using .BI getpwuid( uid ). .RE .PP The options for .I grget are: .RS .TP .8i .BI \-n " name" Output the first entry that matches using .BI getgrnam( name ). .TP .BI \-g " gid" Output the first entry that matches using .BI getgrgid( gid ). .RE .SH RETURN VALUE These routines return 0 upon success, 1 when a specific search fails and 2 upon error. .SH WARNINGS If the Yellow Page network database is in use and the YP client daemon, .IR ypbind (ADMIN), is not connected to a YP server daemon, .IR ypserv (ADMIN), then these utilities will wait until such a connection is established. These routines can be terminated in this condition by sending a SIGINT signal to the process (see .IR kill (UTIL)). .SH AUTHOR Pwget and grget were developed by Hewlett-Packard Company. .SH FILES .TS l l. /etc/group Local group data file /etc/passwd Local password data file .TE .SH SEE ALSO getgrent(LIBC), getpwent(LIBC), group(FILE), passwd(FILE). @EOF chmod 644 pwget.1 echo x - pwget.c cat >pwget.c <<'@EOF' #include <stdio.h> #include <grp.h> #include <pwd.h> #ifdef SYSV #include <string.h> #else /* not SYSV but BSD */ #include <strings.h> #endif /* SYSV / BSD */ int atoi(), getopt(); char *arg0; #define GRGET 1 #define PWGET 2 int mode; /* Mode of operation, either GRGET or PWGET. */ main(argc, argv) int argc; char **argv; { int printgr(), printpw(); int c; extern char *optarg; extern int optind; struct group *grp; struct passwd *pwd; int anyflag = 0, gflag = 0, nflag = 0, uflag = 0; int gid, uid; char *name, *opts; mode = 0; #ifdef SYSV if ((arg0 = strrchr(argv[0], '/')) == NULL) #else /* not SYSV but BSD */ if ((arg0 = rindex(argv[0], '/')) == NULL) #endif /* SYSV / BSD */ arg0 = argv[0]; else arg0++; /* Start after the '/' */ if (strcmp(arg0, "grget") == 0) mode = GRGET; else if (strcmp(arg0, "pwget") == 0) mode = PWGET; else usage(); switch(mode) { case GRGET: setgrent(); opts = "g:n:"; break; case PWGET: setpwent(); opts = "u:n:"; break; } while ((c = getopt(argc, argv, opts)) != EOF) { switch (c) { case 'g': if (anyflag != 0) usage(); gflag++; anyflag++; gid = atoi(optarg); break; case 'n': if (anyflag != 0) usage(); nflag++; anyflag++; name = optarg; break; case 'u': if (anyflag != 0) usage(); uflag++; anyflag++; uid = atoi(optarg); break; case '?': usage(); break; } } if (argv[optind] != NULL) usage(); if (gflag) { if ((grp = getgrgid(gid)) != NULL) printgr(grp); else exit(1); } else if (nflag) { if (mode == GRGET) { if ((grp = getgrnam(name)) != NULL) printgr(grp); else exit(1); } else if (mode == PWGET) { if ((pwd = getpwnam(name)) != NULL) printpw(pwd); else exit(1); } } else if (uflag) { if ((pwd = getpwuid(uid)) != NULL) printpw(pwd); else exit(1); } else { if (mode == GRGET) { while ((grp = getgrent()) != NULL) printgr(grp); } else if (mode == PWGET) { while ((pwd = getpwent()) != NULL) printpw(pwd); } } switch(mode) { case GRGET: endgrent(); break; case PWGET: endpwent(); break; } exit(0); } usage() { switch(mode) { case GRGET: fprintf(stderr, "usage: %s [ -g gid | -n name ]\n", arg0); break; case PWGET: fprintf(stderr, "usage: %s [ -n name | -u uid ]\n", arg0); break; default: fprintf(stderr, "Call as either grget or pwget\n"); break; } exit(2); } printgr(g) struct group *g; { char **chr; int comma; if (g != NULL) { printf("%s:%s:%d:", g->gr_name, g->gr_passwd, g->gr_gid); /* prints "grp1,grp2,grp3, ... ,grpn" */ for (comma = 0, chr = g->gr_mem; *chr != NULL; chr++) printf("%s%s", ((comma==0)?comma++,"":","), *chr); printf("\n"); } } printpw(p) struct passwd *p; { if (p != NULL) { printf("%s:%s", p->pw_name, p->pw_passwd); #ifdef SYSV if (strcmp(p->pw_age, "") != 0) printf(",%s", p->pw_age); #endif /* SYSV */ printf(":%d:%d:%s:%s:%s\n", p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell); } } @EOF chmod 666 pwget.c exit 0 -- For comp.sources.unix stuff, mail to sources@uunet.uu.net.