home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / pwd / getpwuid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  5.2 KB  |  196 lines

  1. /*  LAST EDIT: Wed Mar 31 06:07:51 1993 by Swen Thⁿmmler (swen)  */
  2. /* Copyright (C) 1991 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4.  
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with the GNU C Library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.  */
  19.  
  20. #include <ansidecl.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <pwd.h>
  24. #include <sys/types.h>
  25.  
  26. #ifdef YP
  27. #include <rpcsvc/yp_prot.h>
  28. #include <rpcsvc/ypclnt.h>
  29. extern int __yp_check(char **) ;
  30. extern struct passwd * __nis_parsepwddata(char *, void *);
  31. extern void setnetgrent(const char *);
  32. extern void endnetgrent(void);
  33. extern int getnetgrent(char **, char **, char **);
  34. extern int innetgr(const char *, const char *, const char *, const char *);
  35. extern struct passwd * __nis_getpwnam(const char *, void *);
  36.  
  37. static struct passwd * __nis_getpwuid(uid_t, void *);
  38. static struct passwd * __netgroup_getpwuid(const char *, uid_t, void *);
  39. #endif
  40.  
  41. /* Search for an entry with a matching uid.  */
  42. struct passwd *
  43. DEFUN(getpwuid, (uid), register uid_t uid)
  44. {
  45. #ifdef YP
  46.   static void *info_nis = NULL;
  47. #endif
  48.   static PTR info = NULL;
  49.   register FILE *stream;
  50.   register struct passwd *p;
  51.  
  52.   if (info == NULL)
  53.     {
  54.       info = __pwdalloc();
  55.       if (info == NULL)
  56.     return(NULL);
  57.     }
  58.  
  59.   stream = __pwdopen();
  60.   if (stream == NULL)
  61.     return(NULL);
  62.  
  63.   while ((p = __pwdread(stream, info)) != NULL)
  64.     {
  65.       if (p->pw_uid == uid)
  66.         break;
  67. #ifdef YP
  68.           /* Handle -@netgroup entries */
  69.       if ('-' == p->pw_name[0]
  70.           && '@' == p->pw_name[1]
  71.           && '\0' != p->pw_name[2])
  72.         {
  73.           p = __netgroup_getpwuid(&p->pw_name[2], uid, info);
  74.           if (NULL != p)
  75.             return NULL;
  76.           break;
  77.         }
  78.  
  79.           /* Handle +@netgroup entries */
  80.       if ('+' == p->pw_name[0]
  81.           && '@' == p->pw_name[1]
  82.           && '\0' != p->pw_name[2])
  83.         {
  84.           p = __netgroup_getpwuid(&p->pw_name[2], uid, info);
  85.           if (NULL != p)
  86.             break;
  87.           continue;
  88.         }
  89.       
  90.           /*
  91.            * Handle +user/-user entries.
  92.            * Bug: gets full entry from NIS, overwriting of some fields
  93.            *      not supported (yet). --Swen
  94.            */
  95.       if ('-' == p->pw_name[0] && '\0' != p->pw_name[1])
  96.         {
  97.           p = __nis_getpwnam(p->pw_name + 1, info);
  98.           if (p->pw_uid == uid)
  99.             return NULL;
  100.           continue;
  101.         }
  102.       if ('+' == p->pw_name[0] && '\0' != p->pw_name[1])
  103.         {
  104.           struct passwd *pwd = p;
  105.           if (NULL == info_nis)
  106.             {
  107.               info_nis = __pwdalloc();
  108.               if (NULL == info_nis)
  109.                 return(NULL);
  110.             }
  111.           p = __nis_getpwnam(p->pw_name + 1, info_nis);
  112.           if (p->pw_uid == uid)
  113.             {
  114.               if (NULL != pwd->pw_gecos && '\0' != *pwd->pw_gecos)
  115.                 p->pw_gecos = pwd->pw_gecos;
  116.               if (NULL != pwd->pw_dir && '\0' != *pwd->pw_dir)
  117.                 p->pw_dir   = pwd->pw_dir  ;
  118.               if (NULL != pwd->pw_shell && '\0' != *pwd->pw_shell)
  119.                 p->pw_shell = pwd->pw_shell;
  120.               break;
  121.             }
  122.           continue;
  123.         }
  124.       if (0 == strcmp(p->pw_name, "+"))
  125.         {
  126.           p = __nis_getpwuid(uid, info);
  127.           break;
  128.         }
  129. #endif
  130.     }
  131.   (void) fclose(stream);
  132.   return(p);
  133. }
  134.  
  135. #ifdef YP
  136. static struct passwd *
  137. __nis_getpwuid(uid_t uid, void *info)
  138. {
  139.   static char *nisdomain = NULL;
  140.   char *outkey;
  141.   int status, outkeylen ;
  142.   struct passwd *pwptr = NULL;
  143.   
  144.   if (1 == __yp_check(NULL))
  145.     {
  146.       char uidbuf[20] ;
  147.       if (NULL == nisdomain)
  148.         yp_get_default_domain(&nisdomain);
  149.       sprintf(uidbuf, "%d", uid) ;
  150.       status = yp_match(nisdomain, "passwd.byuid",
  151.                         uidbuf, strlen(uidbuf),
  152.                         &outkey, &outkeylen) ;
  153.       if (0 != status)
  154.         return NULL;
  155.       pwptr = __nis_parsepwddata(outkey, info) ;
  156.       free (outkey);
  157.       return (pwptr);
  158.     }
  159.   return NULL;
  160. }
  161.  
  162. static struct passwd *
  163. __netgroup_getpwuid(const char *netgr, uid_t uid, void *info)
  164. {
  165.   int status;
  166.   static char *nisdomain = NULL;
  167.   char *host, *user, *domain;
  168.   struct passwd *p = NULL;
  169.     
  170.   if (NULL == nisdomain)
  171.     yp_get_default_domain(&nisdomain);
  172.   
  173.   setnetgrent(netgr);
  174.   while(1)
  175.     {
  176.       while (1 == (status = getnetgrent(&host, &user, &domain))
  177.              && NULL == user
  178.              && NULL != domain
  179.              && 0 != strcmp(domain, nisdomain))
  180.         ;
  181.       
  182.       if (0 == status || NULL == user)
  183.         {
  184.           p = NULL;
  185.           break;
  186.         }
  187.       
  188.       p = __nis_getpwnam(user, info);
  189.       if (p->pw_uid == uid)
  190.         break;
  191.       }
  192.   endnetgrent();
  193.   return p;
  194. }
  195. #endif
  196.