home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / util / id / id.c next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  3.8 KB  |  174 lines

  1. RCS_ID_C="$Id: id.c,v 2.1 1994/02/15 15:25:46 ppessi Exp $";
  2. /*
  3.  * id.c -- print out the identity of user
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1994 AmiTCP/IP Group, <AmiTCP-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *
  10.  * Created      : Tue Jan 11 22:33:06 1994 ppessi
  11.  * Last modified: Tue Feb 15 14:25:22 1994 ppessi
  12.  */
  13.  
  14. #include "id_rev.h"
  15.  
  16. static const char version[] = VERSTAG " "
  17. "Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  18.   "Helsinki University of Technology, Finland.\n";
  19.  
  20. #include <pwd.h>
  21. #include <grp.h>
  22. #include <unistd.h>
  23.  
  24. #include <proto/usergroup.h>
  25. #include <proto/dos.h>
  26.  
  27. #include <clib/exec_protos.h>
  28. extern struct ExecBase* SysBase;
  29. #include <pragmas/exec_sysbase_pragmas.h>
  30.  
  31. #include <dos/dos.h>
  32.  
  33. #include <string.h>
  34. #include <stdlib.h>
  35.  
  36. #define USERGROUPVERSION 2    /* minimum version to use */
  37.  
  38. #ifndef MAXLINELENGTH 
  39. #define MAXLINELENGTH 1024
  40. #endif
  41.  
  42. LONG id(void)
  43. {
  44.   LONG retval = 128;
  45.   struct DosLibrary *DOSBase;
  46.   struct ExecBase *SysBase;
  47.   struct Library *UserGroupBase;
  48.  
  49.   SysBase = *(struct ExecBase**)4;
  50.   DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L);
  51.   UserGroupBase = OpenLibrary(USERGROUPNAME, USERGROUPVERSION);
  52.   
  53.   if (DOSBase && UserGroupBase) {
  54.     uid_t ruid = getuid(), euid = geteuid();
  55.     gid_t rgid = getgid(), egid = getegid(), groups[NGROUPS];
  56.     
  57.     const char *template = "-u,-g,-n,-r,USER";
  58.     struct {
  59.       LONG   a_uid;
  60.       LONG   a_gid;
  61.       LONG   a_name;
  62.       LONG   a_real;
  63.       STRPTR a_user;
  64.     } args[1] = { 0 };
  65.     struct RDArgs *rdargs = NULL;
  66.  
  67.     retval = RETURN_OK;
  68.  
  69.     rdargs = ReadArgs((UBYTE *)template, (LONG *)args, NULL);
  70.  
  71.     if (rdargs == NULL) {
  72.       PrintFault(IoErr(), "id");
  73.       retval = RETURN_ERROR;
  74.     } else if (args->a_uid) {
  75.       struct passwd *pw;
  76.       /* Print user id */
  77.       uid_t uid = euid;
  78.  
  79.       /* Use real id? */
  80.       if (args->a_real)
  81.       uid = euid;
  82.  
  83.       if (args->a_name && (pw = getpwuid(uid))) {
  84.     Printf("%s\n", pw->pw_name);
  85.       } else {
  86.     Printf("%ld\n", uid);
  87.       }
  88.     } else if (args->a_gid) {
  89.       struct group *gr;
  90.       /* Print primary group id */
  91.       gid_t gid = egid;
  92.  
  93.       /* Use real id? */
  94.       if (args->a_real)
  95.     gid = egid;
  96.  
  97.       if (args->a_name && (gr = getgrgid(gid))) {
  98.     Printf("%s\n", gr->gr_name);
  99.       } else {
  100.     Printf("%ld\n", gid);
  101.       }
  102.     } else {
  103.       /* Print all identification information */
  104.       char *sep = " groups=";
  105.       short i, ngroups;
  106.  
  107.       if (euid == ruid) {
  108.     struct passwd *pw;
  109.  
  110.     Printf("uid=%ld", euid);
  111.     if (pw = getpwuid(ruid))
  112.       Printf("(%s)", pw->pw_name);
  113.       } else {
  114.     struct passwd *pw;
  115.  
  116.     Printf("ruid=%ld", ruid);
  117.     if (pw = getpwuid(ruid))
  118.       Printf("(%s)", pw->pw_name);
  119.     Printf(" euid=%ld", euid);
  120.     if (pw = getpwuid(euid))
  121.       Printf("(%s)", pw->pw_name);
  122.       }
  123.  
  124.       if (egid == rgid) {
  125.     struct group *gr = getgrgid(egid);
  126.  
  127.     Printf(" gid=%ld", egid);
  128.     if (gr)
  129.       Printf("(%s)", gr->gr_name);
  130.       } else {
  131.     struct group *gr;
  132.  
  133.     Printf(" rgid=%ld", rgid);
  134.     if (gr = getgrgid(rgid))
  135.       Printf("(%s)", gr->gr_name);
  136.     Printf(" egid=%ld", egid);
  137.     if (gr = getgrgid(egid))
  138.       Printf("(%s)", gr->gr_name);
  139.       }
  140.  
  141.       ngroups = getgroups(NGROUPS, groups);
  142.       if (ngroups != -1) {
  143.     for (i = 0; i < ngroups; i++) {
  144.       if ((rgid = groups[i]) != egid) {
  145.         struct group *gr;
  146.  
  147.         Printf("%s%ld", sep, rgid);
  148.         if (gr = getgrgid(rgid))
  149.           Printf("(%s)", gr->gr_name);
  150.         sep = ",";
  151.       }
  152.     }
  153.     PutStr("\n");
  154.       } else {
  155.     Printf("%s: %s\n", "getgroups", ug_StrError(ug_GetErr()));
  156.       }
  157.     }
  158.     if (rdargs) {
  159.       FreeArgs(rdargs);
  160.     }
  161.   }
  162.  
  163.   if (UserGroupBase) {
  164.     CloseLibrary(UserGroupBase);
  165.   } else {
  166.     Printf("id: cannot open %s\n", USERGROUPNAME);
  167.   }
  168.  
  169.   if (DOSBase)
  170.     CloseLibrary((struct Library *)DOSBase);
  171.  
  172.   return retval;
  173. }
  174.