home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume16 / deliver / part02 / context.c next >
C/C++ Source or Header  |  1988-11-14  |  3KB  |  147 lines

  1. /* $Header: context.c,v 1.3 88/09/14 19:41:40 network Exp $
  2.  *
  3.  * User context manager.
  4.  * This module exists for efficiency reasons; I could just call getpwnam()
  5.  * every time I need context info.
  6.  *
  7.  * $Log:    context.c,v $
  8.  * Revision 1.3  88/09/14  19:41:40  network
  9.  * Portability to System V and BSD.
  10.  * General fixup.
  11.  * 
  12.  * Revision 1.2  88/08/30  16:12:28  network
  13.  * Use savestr() instead of strdup().
  14.  * 
  15.  * Revision 1.1  88/06/06  09:38:05  chip
  16.  * Initial revision
  17.  * 
  18.  */
  19.  
  20. #include "deliver.h"
  21. #include <pwd.h>
  22. #include <grp.h>
  23.  
  24. extern  struct passwd   *getpwnam();
  25. extern  struct passwd   *getpwuid();
  26. extern  struct group    *getgrnam();
  27. extern  struct group    *getgrgid();
  28.  
  29. /*
  30.  * Local functions.
  31.  */
  32.  
  33. static  CONTEXT *new_context();
  34.  
  35. /*
  36.  * Local data.
  37.  */
  38.  
  39. static  CONTEXT *ctlist;    /* Chain of CONTEXT structures.        */
  40.  
  41. /*----------------------------------------------------------------------
  42.  * Look up a context by user name.
  43.  */
  44.  
  45. CONTEXT *
  46. name_context(name)
  47. char    *name;
  48. {
  49.     struct passwd *pw;
  50.     CONTEXT *ct;
  51.  
  52.     for (ct = ctlist; ct; ct = ct->next)
  53.     {
  54.         if (strcmp(ct->name, name) == 0)
  55.             return ct;
  56.     }
  57.  
  58.     if ((pw = getpwnam(name)) == NULL)
  59.         return NULL;
  60.  
  61.     return new_context(pw);
  62. }
  63.  
  64. /*----------------------------------------------------------------------
  65.  * Look up a context by user ID.
  66.  */
  67.  
  68. CONTEXT *
  69. uid_context(uid)
  70. int     uid;
  71. {
  72.     struct passwd *pw;
  73.     CONTEXT *ct;
  74.  
  75.     for (ct = ctlist; ct; ct = ct->next)
  76.     {
  77.         if (ct->uid == uid)
  78.             return ct;
  79.     }
  80.  
  81.     if ((pw = getpwuid(uid)) == NULL)
  82.         return NULL;
  83.  
  84.     return new_context(pw);
  85. }
  86.  
  87. /*----------------------------------------------------------------------
  88.  * Local function -- create a new context structure and return
  89.  * its address.
  90.  */
  91.  
  92. static CONTEXT *
  93. new_context(pw)
  94. struct passwd *pw;
  95. {
  96.     CONTEXT *ct;
  97.  
  98.     ct = (CONTEXT *) zalloc(sizeof(CONTEXT));
  99.     ct->uid = pw->pw_uid;
  100.     ct->gid = pw->pw_gid;
  101.     ct->name = copystr(pw->pw_name);
  102.     ct->home = copystr(pw->pw_dir);
  103.  
  104.     ct->next = ctlist;
  105.     ctlist = ct;
  106.  
  107.     return ct;
  108. }
  109.  
  110. /*----------------------------------------------------------------------
  111.  * Report whether is is possible or not to enter the given context.
  112.  */
  113.  
  114. int
  115. ok_context(ct)
  116. CONTEXT *ct;
  117. {
  118.     if (! ct)
  119.         return FALSE;
  120.  
  121.     if (eff_uid == 0
  122.      || ((real_uid == ct->uid) && (real_gid == ct->gid)))
  123.         return TRUE;
  124.     else
  125.         return FALSE;
  126. }
  127.  
  128. /*----------------------------------------------------------------------
  129.  * Look up a group ID by name.
  130.  */
  131.  
  132. #ifdef MAILBOX_GROUP
  133.  
  134. int
  135. group_id(name)
  136. char    *name;
  137. {
  138.     struct group *grp;
  139.  
  140.     if ((grp = getgrnam(name)) == NULL)
  141.         return -1;
  142.  
  143.     return grp->gr_gid;
  144. }
  145.  
  146. #endif /* MAILBOX_GROUP */
  147.