home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume13 / pwget < prev    next >
Text File  |  1988-01-31  |  9KB  |  445 lines

  1. Subject:  v13i034:  Programs to retrieve /etc/passwd info
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Michael Saboff <hplabs!hpirs!mike>
  7. Posting-number: Volume 13, Issue 34
  8. Archive-name: pwget
  9.  
  10. [  This is a small utility to get information from the password file.
  11.    Programs (like rn stuff :-) that might other do grep or ypmatch
  12.    can use this instead.  --r$ ]
  13.  
  14. Michael Saboff
  15. hplabs!hpda!mike
  16. Phone: (408) 447-5976
  17.  
  18. # This is a shell archive.  Remove anything before this line,
  19. # then unpack it by saving it in a file and typing "sh file".
  20. # This archive contains:
  21. #    README        Makefile    pwget.1        pwget.c        
  22.  
  23. LANG=""; export LANG
  24.  
  25. echo x - README
  26. cat >README <<'@EOF'
  27. ---NOTICE---
  28. This is code is available to the public free of charge.
  29.  
  30. Hewlett-Packard Company asks that it be given author recognition.
  31.  
  32. The information in this document is subject to change without notice.
  33.  
  34. THIS MATERIAL IS PROVIDED "AS IS".  HEWLETT-PACKARD MAKES NO WARRANTY OF
  35. ANY KIND WITH REGARD TO THIS  MATERIAL,  INCLUDING,  BUT NOT LIMITED TO,
  36. THE IMPLIED WARRANTIES OF  MERCHANTABILITY  AND FITNESS FOR A PARTICULAR
  37. PURPOSE.
  38.  
  39. Hewlett-Packard  shall not be liable for errors  contained herein or for
  40. incidental  or  consequential  damages  or for any  claim of any kind in
  41. connection  with the  furnishing,  performance or use of this  material.
  42. Hewlett-Packard  assumes no responsibility  for the use, reliability, or
  43. support of this software.
  44. ---END OF NOTICE---
  45.  
  46. These utilities provided a common command level access to /etc/group and
  47. /etc/passwd much in the same way as getgrent(), getpwent(), et al.
  48.  
  49. This is especially useful if your system is running NFS and Yellow Pages.
  50.  
  51. With Yellow pages, there is no easy command level interface for this
  52. information that will give you the same results as the libc routines.
  53.  
  54. These routines use "getopt()" so make sure your system has it.
  55.  
  56. Edit the Makefile for your machine/os and for the owner, group, mode
  57. and destination of the binary and the man page.
  58.  
  59. Install with: make install.
  60.  
  61. File list:
  62. README        -    This file.
  63. Makefile    -    Makefile.
  64. pwget.1        -    Man Page.
  65. pwget.c        -    Command source.
  66.  
  67. @EOF
  68.  
  69. chmod 664 README
  70.  
  71. echo x - Makefile
  72. cat >Makefile <<'@EOF'
  73. # Configure CFLAGS/LDFLAGS for your machine/OS
  74. # for HP-UX and other Sys V systems:
  75. CFLAGS= -O -DSYSV
  76. # for VAX 4.[23] BSD, SunOS and other BSD based systems:
  77. # CFLAGS= -O -DBSD
  78.  
  79. # Need getopt for pwget, include library that has it.
  80. # System V based system (like HP-UX), 4.3BSD and SunOS have null LDFLAGS.
  81. LDFLAGS=
  82. # 4.2BSD requires getopt from someplace.
  83. # LDFLAGS= -lgetopt
  84.  
  85. MODE=755
  86. OWNER=bin
  87. GROUP=bin
  88. DEST=dest
  89. MANMODE=444
  90. MANOWNER=bin
  91. MANGROUP=bin
  92. MANDEST=man
  93.  
  94. all: grget pwget pwget.cat
  95.  
  96. install: installcmd installman
  97.  
  98. installcmd: pwget
  99.     strip pwget
  100. # use install for BSD and cpset for SYSV
  101. #    install -c -o $(OWNER) -g $(GROUP) -m $(MODE) pwget $(DEST)
  102.     cpset pwget $(DEST) $(MODE) $(OWNER) $(GROUP)
  103.     ln $(DEST)/pwget $(DEST)/grget
  104.  
  105. installman: pwget.1
  106. # use install for BSD and cpset for SYSV
  107. #    install -c -o $(MANOWNER) -g $(MANGROUP) -m $(MANMODE) pwget.1 $(MANDEST)
  108.     cpset pwget.1 $(MANDEST) $(MANMODE) $(MANOWNER) $(MANGROUP)
  109.  
  110. grget: pwget
  111.     ln pwget grget
  112.  
  113. pwget: pwget.o
  114.     cc $(LDFLAGS) -o pwget pwget.o
  115.  
  116. pwget.o: pwget.c
  117.     cc $(CFLAGS) -c pwget.c
  118.  
  119. pwget.cat: pwget.1
  120.     tbl pwget.1 | nroff -man -T > pwget.cat
  121.  
  122. clean:
  123.     /bin/rm -f pwget.o pwget grget
  124.  
  125. clobber: clean
  126. @EOF
  127.  
  128. chmod 664 Makefile
  129.  
  130. echo x - pwget.1
  131. cat >pwget.1 <<'@EOF'
  132. .TH PWGET UTIL "" "" ""
  133. .ad b
  134. .SH NAME
  135. pwget, grget \- get password and group information
  136. .SH SYNOPSIS
  137. .B pwget
  138. .RB [ " \-n" " name"
  139. .RB | " \-u" " uid " ]
  140. .PP
  141. .B grget
  142. .RB [ " \-n" " name"
  143. .RB | " \-g" " gid " ]
  144. .br
  145. .SH DESCRIPTION
  146. .I Pwget\^
  147. and
  148. .I grget\^
  149. are used to access the information found in
  150. .B /etc/passwd
  151. and
  152. .BR /etc/group .
  153. These routines provide a common access method
  154. whether using the Yellow Page network database or not.
  155. The output of these routines is sent to standard output.
  156. With no options,
  157. .I pwget
  158. and
  159. .I grget
  160. output all of the data found using
  161. .IR getpwent( LIBC )
  162. and
  163. .IR getgrent( LIBC )
  164. respectively.
  165. When options are given, only specified entries are searched for.
  166. .PP
  167. The options for
  168. .I pwget
  169. are:
  170. .RS
  171. .TP .8i
  172. .BI \-n " name"
  173. Output the first entry that matches using
  174. .BI getpwnam( name ).
  175. .TP
  176. .BI \-u " uid"
  177. Output the first entry that matches using
  178. .BI getpwuid( uid ).
  179. .RE
  180. .PP
  181. The options for
  182. .I grget
  183. are:
  184. .RS
  185. .TP .8i
  186. .BI \-n " name"
  187. Output the first entry that matches using
  188. .BI getgrnam( name ).
  189. .TP
  190. .BI \-g " gid"
  191. Output the first entry that matches using
  192. .BI getgrgid( gid ).
  193. .RE
  194. .SH RETURN VALUE
  195. These routines return 0 upon success, 1 when
  196. a specific search fails and 2 upon error.
  197. .SH WARNINGS
  198. If the Yellow Page network database is in use and the
  199. YP client daemon,
  200. .IR ypbind (ADMIN),
  201. is not connected to a YP server daemon,
  202. .IR ypserv (ADMIN),
  203. then these utilities will wait until such a connection is
  204. established.  These routines can be terminated in this
  205. condition by sending a SIGINT signal to the process (see
  206. .IR kill (UTIL)).
  207. .SH AUTHOR
  208. Pwget and grget were developed by Hewlett-Packard Company.
  209. .SH FILES
  210. .TS
  211. l l.
  212. /etc/group    Local group data file
  213. /etc/passwd    Local password data file
  214. .TE
  215. .SH SEE ALSO
  216. getgrent(LIBC), getpwent(LIBC), group(FILE), passwd(FILE).
  217. @EOF
  218.  
  219. chmod 644 pwget.1
  220.  
  221. echo x - pwget.c
  222. cat >pwget.c <<'@EOF'
  223. #include <stdio.h>
  224. #include <grp.h>
  225. #include <pwd.h>
  226. #ifdef SYSV
  227. #include <string.h>
  228. #else /* not SYSV but BSD */
  229. #include <strings.h>
  230. #endif /* SYSV / BSD */
  231.  
  232. int atoi(), getopt();
  233. char *arg0;
  234.  
  235. #define GRGET    1
  236. #define PWGET    2
  237.  
  238. int mode;            /* Mode of operation, either GRGET or PWGET. */
  239.  
  240. main(argc, argv)
  241. int argc;
  242. char **argv;
  243. {
  244.     int printgr(), printpw();
  245.     int c;
  246.     extern char *optarg;
  247.     extern int optind;
  248.     struct group *grp;
  249.     struct passwd *pwd;
  250.     int anyflag = 0,
  251.     gflag = 0,
  252.     nflag = 0,
  253.     uflag = 0;
  254.     int gid, uid;
  255.     char *name, *opts;
  256.  
  257.     mode = 0;
  258.  
  259. #ifdef SYSV
  260.     if ((arg0 = strrchr(argv[0], '/')) == NULL)
  261. #else /* not SYSV but BSD */
  262.     if ((arg0 = rindex(argv[0], '/')) == NULL)
  263. #endif /* SYSV / BSD */
  264.     arg0 = argv[0];
  265.     else
  266.     arg0++;            /* Start after the '/' */
  267.  
  268.     if (strcmp(arg0, "grget") == 0)
  269.     mode = GRGET;
  270.     else if (strcmp(arg0, "pwget") == 0)
  271.     mode = PWGET;
  272.     else
  273.     usage();
  274.  
  275.     switch(mode)
  276.     {
  277. case GRGET:
  278.     setgrent();
  279.     opts = "g:n:";
  280.     break;
  281. case PWGET:
  282.     setpwent();
  283.     opts = "u:n:";
  284.     break;
  285.     }
  286.  
  287.     while ((c = getopt(argc, argv, opts)) != EOF)
  288.     {
  289.     switch (c)
  290.     {
  291.     case 'g':
  292.         if (anyflag != 0)
  293.         usage();
  294.  
  295.         gflag++;
  296.         anyflag++;
  297.         gid = atoi(optarg);
  298.         break;
  299.     case 'n':
  300.         if (anyflag != 0)
  301.         usage();
  302.  
  303.         nflag++;
  304.         anyflag++;
  305.         name = optarg;
  306.         break;
  307.     case 'u':
  308.         if (anyflag != 0)
  309.         usage();
  310.  
  311.         uflag++;
  312.         anyflag++;
  313.         uid = atoi(optarg);
  314.         break;
  315.     case '?':
  316.         usage();
  317.         break;
  318.     }
  319.     }
  320.  
  321.     if (argv[optind] != NULL)
  322.     usage();
  323.  
  324.     if (gflag)
  325.     {
  326.     if ((grp = getgrgid(gid)) != NULL)
  327.         printgr(grp);
  328.     else
  329.         exit(1);
  330.     }
  331.     else if (nflag)
  332.     {
  333.     if (mode == GRGET)
  334.     {
  335.         if ((grp = getgrnam(name)) != NULL)
  336.         printgr(grp);
  337.         else
  338.         exit(1);
  339.     }
  340.     else if (mode == PWGET)
  341.     {
  342.         if ((pwd = getpwnam(name)) != NULL)
  343.         printpw(pwd);
  344.         else
  345.         exit(1);
  346.     }
  347.     }
  348.     else if (uflag)
  349.     {
  350.     if ((pwd = getpwuid(uid)) != NULL)
  351.         printpw(pwd);
  352.     else
  353.         exit(1);
  354.     }
  355.     else
  356.     {
  357.     if (mode == GRGET)
  358.     {
  359.         while ((grp = getgrent()) != NULL)
  360.         printgr(grp);
  361.     }
  362.     else if (mode == PWGET)
  363.     {
  364.         while ((pwd = getpwent()) != NULL)
  365.         printpw(pwd);
  366.     }
  367.     }
  368.  
  369.     switch(mode)
  370.     {
  371. case GRGET:
  372.     endgrent();
  373.     break;
  374. case PWGET:
  375.     endpwent();
  376.     break;
  377.     }
  378.  
  379.     exit(0);
  380. }
  381.  
  382.  
  383. usage()
  384. {
  385.     switch(mode)
  386.     {
  387. case GRGET:
  388.     fprintf(stderr, "usage: %s [ -g gid | -n name ]\n", arg0);
  389.     break;
  390. case PWGET:
  391.     fprintf(stderr, "usage: %s [ -n name | -u uid ]\n", arg0);
  392.     break;
  393. default:
  394.     fprintf(stderr, "Call as either grget or pwget\n");
  395.     break;
  396.     }
  397.  
  398.     exit(2);
  399. }
  400.  
  401.  
  402. printgr(g)
  403. struct group *g;
  404. {
  405.     char **chr;
  406.     int comma;
  407.  
  408.     if (g != NULL)
  409.     {
  410.     printf("%s:%s:%d:", g->gr_name, g->gr_passwd, g->gr_gid);
  411.  
  412.     /* prints "grp1,grp2,grp3, ... ,grpn" */
  413.     for (comma = 0, chr = g->gr_mem; *chr != NULL; chr++)
  414.         printf("%s%s", ((comma==0)?comma++,"":","), *chr);
  415.  
  416.     printf("\n");
  417.     }
  418. }
  419.  
  420.  
  421. printpw(p)
  422. struct passwd *p;
  423. {
  424.     if (p != NULL)
  425.     {
  426.     printf("%s:%s", p->pw_name, p->pw_passwd);
  427.  
  428. #ifdef SYSV
  429.     if (strcmp(p->pw_age, "") != 0)
  430.         printf(",%s", p->pw_age);
  431. #endif /* SYSV */
  432.  
  433.     printf(":%d:%d:%s:%s:%s\n", p->pw_uid, p->pw_gid,
  434.         p->pw_gecos, p->pw_dir, p->pw_shell);
  435.     }
  436. }
  437.  
  438. @EOF
  439.  
  440. chmod 666 pwget.c
  441.  
  442. exit 0
  443.  
  444.  
  445.