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 / getpwnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  4.0 KB  |  145 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <pwd.h>
  24.  
  25. #ifdef YP
  26. #include <rpc/rpc.h>
  27. #include <rpcsvc/yp_prot.h>
  28. #include <rpcsvc/ypclnt.h>
  29. extern void setnetgrent(const char *);
  30. extern void endnetgrent(void);
  31. extern int getnetgrent(char **, char **, char **);
  32. extern int innetgr(const char *, const char *, const char *, const char *);
  33. extern int __yp_check(char **);
  34. extern struct passwd * __nis_parsepwddata(char *, void *);
  35. extern struct passwd * __nis_getpwnam(const char *, void *);
  36. #endif
  37.  
  38.  
  39. /* Search for an entry with a matching name.  */
  40. struct passwd *
  41. DEFUN(getpwnam, (name), register CONST char *name)
  42. {
  43. #ifdef YP
  44.   static void *info_nis = NULL;
  45. #endif
  46.   static PTR info = NULL;
  47.   register FILE *stream;
  48.   register struct passwd *p;
  49.  
  50.   if (info == NULL)
  51.     {
  52.       info = __pwdalloc();
  53.       if (info == NULL)
  54.     return(NULL);
  55.     }
  56.  
  57.   stream = __pwdopen();
  58.   if (stream == NULL)
  59.     return(NULL);
  60.   while ((p = __pwdread(stream, info)) != NULL)
  61.     {
  62.       if (!strcmp(p->pw_name, name))
  63.         break;
  64. #ifdef YP
  65.           /* Handle -@netgroup entries */
  66.       if ('-' == p->pw_name[0]
  67.           && '@' == p->pw_name[1]
  68.           && '\0' != p->pw_name[2]
  69.           && 1 == innetgr(&p->pw_name[2], NULL, name, NULL))
  70.         return NULL ;
  71.       
  72.           /* Handle +@netgroup entries */
  73.       if ('+' == p->pw_name[0]
  74.           && '@' == p->pw_name[1]
  75.           && '\0' != p->pw_name[2])
  76.         {
  77.           if (1 == innetgr(&p->pw_name[2], NULL, name, NULL))
  78.             {
  79.               p = __nis_getpwnam(name, info) ;
  80.               break;
  81.             }
  82.           continue;
  83.         }
  84.         
  85.           /*
  86.            * Handle +user/-user entries.
  87.            * Bug: overwriting of pw_passwd field
  88.            * not supported (yet). --Swen
  89.            */
  90.       if (('-' == p->pw_name[0]) && (0 == strcmp(p->pw_name + 1, name)))
  91.         return NULL;
  92.       if (('+' == p->pw_name[0]) && (0 == strcmp(p->pw_name + 1, name)))
  93.         {
  94.           struct passwd *pwd = p;
  95.           if (NULL == info_nis)
  96.             {
  97.               info_nis = __pwdalloc();
  98.               if (NULL == info_nis)
  99.                 return(NULL);
  100.             }
  101.           p = __nis_getpwnam(name, info_nis);
  102.           if (NULL != pwd->pw_gecos && '\0' != *pwd->pw_gecos)
  103.             p->pw_gecos = pwd->pw_gecos;
  104.           if (NULL != pwd->pw_dir && '\0' != *pwd->pw_dir)
  105.             p->pw_dir   = pwd->pw_dir  ;
  106.           if (NULL != pwd->pw_shell && '\0' != *pwd->pw_shell)
  107.             p->pw_shell = pwd->pw_shell;
  108.           break;
  109.         }
  110.       if (0 == strcmp(p->pw_name, "+"))
  111.         {
  112.           p = __nis_getpwnam(name, info);
  113.           break;
  114.         }
  115. #endif;
  116.     }
  117.   (void) fclose(stream);
  118.   return(p);
  119. }
  120.  
  121. #ifdef YP
  122. struct passwd *
  123. __nis_getpwnam(const char *name, void *info)
  124. {
  125.   static char *nisdomain = NULL;
  126.   char *outkey;
  127.   int status, outkeylen ;
  128.   struct passwd *pwptr = NULL;
  129.   
  130.   if (1 == __yp_check(NULL))
  131.     {
  132.       if (NULL == nisdomain)
  133.         yp_get_default_domain(&nisdomain);
  134.       status = yp_match(nisdomain, "passwd.byname",
  135.                         name, strlen(name),
  136.                         &outkey, &outkeylen) ;
  137.       if (0 != status)
  138.         return NULL;
  139.       pwptr = __nis_parsepwddata(outkey, info) ;
  140.       free (outkey);
  141.     }
  142.   return (pwptr);
  143. }
  144. #endif
  145.