home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / apps / posix / source / PAX / NAMES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  7.0 KB  |  318 lines

  1. /* $Source: /u/mark/src/pax/RCS/names.c,v $
  2.  *
  3.  * $Revision: 1.2 $
  4.  *
  5.  * names.c - Look up user and/or group names. 
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These functions support UID and GID name lookup.  The results are
  10.  *    cached to improve performance.
  11.  *
  12.  * AUTHOR
  13.  *
  14.  *    Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  15.  *
  16.  * Sponsored by The USENIX Association for public distribution. 
  17.  *
  18.  * Copyright (c) 1989 Mark H. Colburn.
  19.  * All rights reserved.
  20.  *
  21.  * Redistribution and use in source and binary forms are permitted
  22.  * provided that the above copyright notice is duplicated in all such 
  23.  * forms and that any documentation, advertising materials, and other 
  24.  * materials related to such distribution and use acknowledge that the 
  25.  * software was developed * by Mark H. Colburn and sponsored by The 
  26.  * USENIX Association. 
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  29.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  30.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31.  *
  32.  * $Log:    names.c,v $
  33.  * Revision 1.2  89/02/12  10:05:05  mark
  34.  * 1.2 release fixes
  35.  * 
  36.  * Revision 1.1  88/12/23  18:02:19  mark
  37.  * Initial revision
  38.  * 
  39.  */
  40.  
  41. #ifndef lint
  42. static char *ident = "$Id: names.c,v 1.2 89/02/12 10:05:05 mark Exp $";
  43. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  44. #endif /* ! lint */
  45.  
  46.  
  47. /* Headers */
  48.  
  49. #include "pax.h"
  50.  
  51.  
  52. /* Defines */
  53.  
  54. #define myuid    ( (int)my_uid < 0? (my_uid = getuid()): my_uid )  /* Xn */
  55. #define    mygid    ( (int)my_gid < 0? (my_gid = getgid()): my_gid )  /* Xn */
  56.  
  57.  
  58. /* Internal Identifiers */
  59.  
  60. #ifdef _POSIX_SOURCE  /* Xn */
  61. static uid_t    saveuid = (uid_t)-993;  /* Xn */
  62. static char     saveuname[TUNMLEN];  /* Xn */
  63. static uid_t    my_uid = (uid_t)-993;  /* Xn */
  64.   /* Xn */
  65. static gid_t    savegid = (gid_t)-993;  /* Xn */
  66. static char     savegname[TGNMLEN];  /* Xn */
  67. static gid_t    my_gid = (gid_t)-993;  /* Xn */
  68. #else  /* Xn */
  69. static int      saveuid = -993;
  70. static char     saveuname[TUNMLEN];
  71. static int      my_uid = -993;
  72.  
  73. static int      savegid = -993;
  74. static char     savegname[TGNMLEN];
  75. static int      my_gid = -993;
  76. #endif  /* Xn */
  77.  
  78.  
  79. /* finduname - find a user or group name from a uid or gid
  80.  *
  81.  * DESCRIPTION
  82.  *
  83.  *     Look up a user name from a uid/gid, maintaining a cache. 
  84.  *
  85.  * PARAMETERS
  86.  *
  87.  *    char    uname[]        - name (to be returned to user)
  88.  *    int    uuid        - id of name to find
  89.  *
  90.  *
  91.  * RETURNS
  92.  *
  93.  *    Returns a name which is associated with the user id given.  If there
  94.  *    is not name which corresponds to the user-id given, then a pointer
  95.  *    to a string of zero length is returned.
  96.  *    
  97.  * FIXME
  98.  *
  99.  *     1. for now it's a one-entry cache. 
  100.  *    2. The "-993" is to reduce the chance of a hit on the first lookup. 
  101.  */
  102.  
  103. #ifdef __STDC__
  104.  
  105. #ifdef _POSIX_SOURCE  /* Xn */
  106. char *finduname(uid_t uuid)  /* Xn */
  107. #else  /* Xn */
  108. char *finduname(int uuid)
  109. #endif  /* Xn */
  110.  
  111. #else
  112.     
  113. char *finduname(uuid)
  114. int             uuid;
  115.  
  116. #endif
  117. {
  118.     struct passwd  *pw;
  119.  
  120. #ifdef DF_TRACE_DEBUG
  121. printf("DF_TRACE_DEBUG: char *finduname() in names.c\n");
  122. #endif
  123.     if (uuid != saveuid) {
  124.     saveuid = uuid;
  125.     saveuname[0] = '\0';
  126.     pw = getpwuid(uuid);
  127.     if (pw) {
  128.         strncpy(saveuname, pw->pw_name, TUNMLEN);
  129.     }
  130.     }
  131.     return(saveuname);
  132. }
  133.  
  134.  
  135. /* finduid - get the uid for a given user name
  136.  *
  137.  * DESCRIPTION
  138.  *
  139.  *    This does just the opposit of finduname.  Given a user name it
  140.  *    finds the corresponding UID for that name.
  141.  *
  142.  * PARAMETERS
  143.  *
  144.  *    char    uname[]        - username to find a UID for
  145.  *
  146.  * RETURNS
  147.  *
  148.  *    The UID which corresponds to the uname given, if any.  If no UID
  149.  *    could be found, then the UID which corrsponds the user running the
  150.  *    program is returned.
  151.  *
  152.  */
  153.  
  154. #ifdef __STDC__
  155.  
  156. #ifdef _POSIX_SOURCE  /* Xn */
  157. uid_t finduid(char *uname)  /* Xn */
  158. #else  /* Xn */
  159. int finduid(char *uname)
  160. #endif  /* Xn */
  161.  
  162. #else
  163.     
  164. int finduid(uname)
  165. char            *uname;
  166.  
  167. #endif
  168. {
  169. #ifndef WIN_NT
  170.     struct passwd  *pw;
  171.     extern struct passwd *getpwnam();
  172. #endif
  173.  
  174. #ifdef DF_TRACE_DEBUG
  175. printf("DF_TRACE_DEBUG: int finduid() in names.c\n");
  176. #endif
  177. #if 0 && WIN_NT
  178.     printf("names.c/finduid - myuid: %lu\n", (unsigned long) myuid);
  179. #endif
  180. #if 1 && WIN_NT
  181.     if (uname == NULL)
  182.     {
  183.     ;
  184.     }
  185.     return (myuid);
  186. #else
  187.     if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  188.     ||0 != strncmp(uname, saveuname, TUNMLEN)) {
  189.     strncpy(saveuname, uname, TUNMLEN);
  190.     pw = getpwnam(uname);
  191.     if (pw) {
  192.         saveuid = pw->pw_uid;
  193.     } else {
  194.         saveuid = myuid;
  195.     }
  196.     }
  197.     return (saveuid);
  198. #endif
  199. }
  200.  
  201.  
  202. /* findgname - look up a group name from a gid
  203.  *
  204.  * DESCRIPTION
  205.  *
  206.  *     Look up a group name from a gid, maintaining a cache.
  207.  *    
  208.  *
  209.  * PARAMETERS
  210.  *
  211.  *    int    ggid        - goupid of group to find
  212.  *
  213.  * RETURNS
  214.  *
  215.  *    A string which is associated with the group ID given.  If no name
  216.  *    can be found, a string of zero length is returned.
  217.  */
  218.  
  219. #ifdef __STDC__
  220.  
  221. #ifdef _POSIX_SOURCE  /* Xn */
  222. char *findgname(gid_t ggid)  /* Xn */
  223. #else  /* Xn */
  224. char *findgname(int ggid)
  225. #endif  /* Xn */
  226.  
  227. #else
  228.     
  229. char *findgname(ggid)
  230. int             ggid;
  231.  
  232. #endif
  233. {
  234.     struct group   *gr;
  235.  
  236. #ifdef DF_TRACE_DEBUG
  237. printf("DF_TRACE_DEBUG: char *findgname() in names.c\n");
  238. #endif
  239.     if (ggid != savegid) {
  240.     savegid = ggid;
  241.     savegname[0] = '\0';
  242. #ifndef _POSIX_SOURCE  /* Xn */
  243.     setgrent();
  244. #endif  /* Xn */
  245.     gr = getgrgid(ggid);
  246.     if (gr) {
  247.         strncpy(savegname, gr->gr_name, TGNMLEN);
  248.     }
  249.     }
  250.     return(savegname);
  251. }
  252.  
  253.  
  254.  
  255. /* findgid - get the gid for a given group name
  256.  *
  257.  * DESCRIPTION
  258.  *
  259.  *    This does just the opposit of finduname.  Given a group name it
  260.  *    finds the corresponding GID for that name.
  261.  *
  262.  * PARAMETERS
  263.  *
  264.  *    char    uname[]        - groupname to find a GID for
  265.  *
  266.  * RETURNS
  267.  *
  268.  *    The GID which corresponds to the uname given, if any.  If no GID
  269.  *    could be found, then the GID which corrsponds the group running the
  270.  *    program is returned.
  271.  *
  272.  */
  273.  
  274. #ifdef __STDC__
  275.  
  276. #ifdef _POSIX_SOURCE
  277. gid_t findgid(char *gname)
  278. #else
  279. int findgid(char *gname)
  280. #endif
  281.  
  282. #else
  283.     
  284. int findgid(gname)
  285. char           *gname;
  286.  
  287. #endif
  288. {
  289.     struct group   *gr;
  290.  
  291.     /* Quick test w/o proc call */
  292. #ifdef DF_TRACE_DEBUG
  293. printf("DF_TRACE_DEBUG: int findgid() in names.c\n");
  294. #endif
  295. #if 0
  296.     (void) fprintf(stderr, "names.c/findgid - gname: \"%s\"; savegname: \"%s\"\n", gname, savegname);
  297.     (void) fflush(stderr);
  298. #endif /* Xn 1993-06-22 */
  299.     if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) {
  300.     strncpy(savegname, gname, TUNMLEN);
  301. #if 0
  302.     (void) fprintf(stderr, "names.c/findgid - before getgrnam; gname: \"%s\"\n", gname);
  303.     (void) fflush(stderr);
  304. #endif /* Xn 1993-06-22 */
  305.     gr = getgrnam(gname);
  306. #if 0
  307.     (void) fprintf(stderr, "names.c/findgid - after getgrnam; gr: %p\n", gr);
  308.     (void) fflush(stderr);
  309. #endif /* Xn 1993-06-22 */
  310.     if (gr) {
  311.         savegid = gr->gr_gid;
  312.     } else {
  313.         savegid = mygid;
  314.     }
  315.     }
  316.     return (savegid);
  317. }
  318.