home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / id.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  3.2 KB  |  162 lines

  1. /*
  2.  * Copyright 1991, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. /*
  13.  * id - print current process user identification information
  14.  *
  15.  *    Print the current process identifiers.  This includes the
  16.  *    UID, GID, effective-UID and effective-GID.  Optionally print
  17.  *    the concurrent group set if the current system supports it.
  18.  */
  19.  
  20. #include <sys/types.h>
  21. #include <stdio.h>
  22. #include <grp.h>
  23. #include "pwd.h"
  24.  
  25. #ifndef    lint
  26. static    char    sccsid[] = "@(#)id.c    3.5    20:36:50    3/7/92";
  27. #endif
  28.  
  29. usage ()
  30. {
  31. #if NGROUPS > 0
  32.     fprintf (stderr, "usage: id [ -a ]\n");
  33. #else
  34.     fprintf (stderr, "usage: id\n");
  35. #endif
  36.     exit (1);
  37. }
  38.  
  39. /*ARGSUSED*/
  40. main (argc, argv)
  41. int    argc;
  42. char    **argv;
  43. {
  44.     int    id;
  45. #if NGROUPS > 0
  46. #if NGROUPS > 100
  47.     int    *groups;
  48. #else
  49.     int    groups[NGROUPS];
  50. #endif
  51.     int    ngroups;
  52.     int    aflg = 0;
  53. #endif
  54.     struct    passwd    *pw,
  55.             *getpwuid();
  56.     struct    group    *gr,
  57.             *getgrgid();
  58.  
  59. #if NGROUPS > 0
  60.     /*
  61.      * See if the -a flag has been given to print out the
  62.      * concurrent group set.
  63.      */
  64.  
  65.     if (argc > 1) {
  66.         if (argc > 2 || strcmp (argv[1], "-a"))
  67.             usage ();
  68.         else
  69.             aflg = 1;
  70.     }
  71. #else
  72.     if (argc > 1)
  73.         usage ();
  74. #endif
  75.  
  76.     /*
  77.      * Print out the real user ID and group ID.  If the user or
  78.      * group does not exist, just give the numerical value.
  79.      */
  80.  
  81.     if (pw = getpwuid (id = getuid ()))
  82.         printf ("uid=%d(%s)", id, pw->pw_name);
  83.     else
  84.         printf ("uid=%d", id);
  85.  
  86.     if (gr = getgrgid (id = getgid ()))
  87.         printf (" gid=%d(%s)", id, gr->gr_name);
  88.     else
  89.         printf (" gid=%d", id);
  90.  
  91.     /*
  92.      * Print out the effective user ID and group ID if they are
  93.      * different from the real values.
  94.      */
  95.  
  96.     if (getuid () != geteuid ()) {
  97.         if (pw = getpwuid (id = geteuid ()))
  98.             printf (" euid=%d(%s)", id, pw->pw_name);
  99.         else
  100.             printf (" euid=%d", id);
  101.     }
  102.     if (getgid () != getegid ()) {
  103.         if (gr = getgrgid (id = getegid ()))
  104.             printf (" egid=%d(%s)", id, gr->gr_name);
  105.         else
  106.             printf (" egid=%d", id);
  107.     }
  108. #if NGROUPS > 0
  109.  
  110.     /*
  111.      * Print out the concurrent group set if the user has requested
  112.      * it.  The group numbers will be printed followed by their
  113.      * names.
  114.      */
  115.  
  116.     if (aflg && (ngroups = getgroups (0, 0)) != -1) {
  117.         int    i;
  118.  
  119. #if NGROUPS > 100
  120.         /*
  121.          * The size of the group set is determined so an array
  122.          * large enough to hold it can be allocated.
  123.          */
  124.  
  125.         if (groups = (int *) malloc (ngroups * sizeof *groups)) {
  126.             putchar ('\n');
  127.             perror ("out of memory");
  128.             exit (1);
  129.         }
  130. #endif
  131.         /*
  132.          * Start off the group message.  It will be of the format
  133.          *
  134.          *    groups=###(aaa),###(aaa),###(aaa)
  135.          *
  136.          * where "###" is a numerical value and "aaa" is the
  137.          * corresponding name for each respective numerical value.
  138.          */
  139.  
  140.         getgroups (ngroups, groups);
  141.         printf (" groups=");
  142.         for (i = 0;i < ngroups;i++) {
  143.             if (i)
  144.                 putchar (',');
  145.  
  146.             if (gr = getgrgid (groups[i]))
  147.                 printf ("%d(%s)", groups[i], gr->gr_name);
  148.             else
  149.                 printf ("%d", groups[i]);
  150.         }
  151.     }
  152. #endif
  153.  
  154.     /*
  155.      * Finish off the line.
  156.      */
  157.  
  158.     putchar ('\n');
  159.     exit (0);
  160.     /*NOTREACHED*/
  161. }
  162.