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

  1. /* Copyright (C) 1991, 1992 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 <errno.h>
  21. #include <limits.h>
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <pwd.h>
  27. #include <sys/types.h>
  28.  
  29. #ifndef NO_SHADOW
  30. #include "shadow.h"
  31. #endif
  32.  
  33. #ifdef YP
  34. extern struct passwd * __nis_parsepwddata(char *, void *);
  35. #endif
  36.  
  37. /* This is the function that all the others are based on.
  38.    The format of the password file is known only here.  */
  39.  
  40. /* Structure containing info kept by each __pwdread caller.  */
  41. typedef struct
  42.   {
  43.     char *buf;
  44.     size_t buflen;
  45.     struct passwd p;
  46.   } pwdread_info;
  47.  
  48.  
  49. /* Return a chunk of memory containing a pre-initialized `pwdread_info'.  */
  50. PTR
  51. DEFUN_VOID(__pwdalloc)
  52. {
  53.   pwdread_info *info = (PTR) malloc (sizeof(pwdread_info));
  54.   if (info == NULL)
  55.     return NULL;
  56.   info->buf = NULL;
  57.   info->buflen = 0;
  58.   return info;
  59. }
  60.  
  61. /* Read a password entry from STREAM, filling in P.  */
  62. struct passwd *
  63. DEFUN(__pwdread, (stream, p), FILE *stream AND PTR CONST p)
  64. {
  65.   register pwdread_info *CONST info = (pwdread_info *) p;
  66.   char *start, *end;
  67.   int is_nis_entry = 0;
  68.   
  69.   /* Idiocy checks.  */
  70.   if (stream == NULL)
  71.     {
  72.       errno = EINVAL;
  73.       return NULL;
  74.     }
  75.  
  76.   do
  77.     if (__getline (&info->buf, &info->buflen, stream) == -1)
  78.       return NULL;
  79.   while (info->buf[0] == '#');
  80.  
  81.   start = info->buf;
  82. #ifdef YP
  83.   if ('+' == *start || '-' == *start)
  84.     {
  85.       is_nis_entry = 1;
  86.       info->p.pw_gecos = info->p.pw_dir = info->p.pw_shell = NULL ;
  87.     }
  88. #endif
  89.   end = strchr (start, ':');
  90.   info->p.pw_name = start;
  91.   if (end == NULL)
  92.     {
  93.       if (!is_nis_entry)
  94.         return NULL;
  95.       end = strchr(info->p.pw_name, '\n');
  96.       if (NULL != end)
  97.         *end = '\0';
  98.       return &info->p;
  99.     }
  100.   *end = '\0';
  101.  
  102.   start = end + 1;
  103.   end = strchr (start, ':');
  104.   if (end == NULL)
  105.     return ( is_nis_entry ? &info->p : NULL );
  106.   *end = '\0';
  107.   info->p.pw_passwd = start;
  108.  
  109. #ifndef NO_SHADOW
  110.   {
  111.     struct spwd *spw = getspnam (info->p.pw_name);
  112.  
  113.     if (spw)
  114.       info->p.pw_passwd = spw->sp_pwdp;
  115.   }
  116. #endif
  117.  
  118.   info->p.pw_uid = (uid_t) strtol (end + 1, &end, 10);
  119.   if (*end != ':')
  120.     return ( is_nis_entry ? &info->p : NULL );
  121.   info->p.pw_gid = (gid_t) strtol (end + 1, &end, 10);
  122.   if (*end != ':')
  123.     return ( is_nis_entry ? &info->p : NULL );
  124.  
  125.   start = end + 1;
  126.   info->p.pw_gecos = start;
  127.   end = strchr (start, ':');
  128.   if (end == NULL)
  129.     {
  130.       if (!is_nis_entry)
  131.         return NULL;
  132.       end = strchr(info->p.pw_gecos, '\n');
  133.       if (NULL != end)
  134.         *end = '\0';
  135.       return &info->p;
  136.     }
  137.   *end = '\0';
  138.  
  139.   start = end + 1;
  140.   info->p.pw_dir = start;
  141.   end = strchr (start, ':');
  142.   if (end == NULL)
  143.     {
  144.       if (!is_nis_entry)
  145.         return NULL;
  146.       end = strchr(info->p.pw_dir, '\n');
  147.       if (NULL != end)
  148.         *end = '\0';
  149.       return &info->p;
  150.     }
  151.   *end = '\0';
  152.  
  153.   start = end + 1;
  154.   info->p.pw_shell = start;
  155.   end = strchr (start, '\n');
  156.   if (end == NULL)
  157.     return ( is_nis_entry ? &info->p : NULL );
  158.   *end = '\0';
  159.  
  160.   return &info->p;
  161. }
  162.  
  163. #ifdef YP
  164. struct passwd *
  165. __nis_parsepwddata (char *line, void *p)
  166. {
  167.   register pwdread_info *const info = (pwdread_info *)p;
  168.   struct passwd *pw = &info->p;
  169.   char *start, *end;
  170.   register int i;
  171.   
  172.   i = strlen(line) + 1;
  173.   if (NULL == info->buf || info->buflen < i)
  174.     {
  175.       start = (NULL == info->buf) ? malloc(i) : realloc(info->buf, i);
  176.       if (NULL == start)
  177.         return NULL;
  178.       info->buf = start;
  179.       info->buflen = i;
  180.     }
  181.   strcpy(info->buf, line);
  182.  
  183.   start = info->buf ;
  184.   end = strchr(start, ':') ;
  185.   if (end == NULL)
  186.       return NULL ;
  187.   *end = '\0' ;
  188.   pw->pw_name = start ;
  189.   start = end + 1 ;
  190.   end = strchr(start, ':') ;
  191.   if (end == NULL)
  192.       return NULL ;
  193.   *end = '\0' ;
  194.   pw->pw_passwd = start ;
  195.   start = end + 1 ;
  196.   end = strchr(start, ':') ;
  197.   if (end == NULL)
  198.       return NULL ;
  199.   *end = '\0' ;
  200.   pw->pw_uid = atoi(start);
  201.   start = end + 1 ;
  202.   end = strchr(start, ':') ;
  203.   if (end == NULL)
  204.       return NULL ;
  205.   *end = '\0' ;
  206.   pw->pw_gid = atoi(start);
  207.   start = end + 1 ;
  208.   end = strchr(start, ':') ;
  209.   if (end == NULL)
  210.       return NULL ;
  211.   *end = '\0' ;
  212.   pw->pw_gecos = start ;
  213.   start = end + 1 ;
  214.   end = strchr(start, ':') ;
  215.   if (end == NULL)
  216.       return NULL ;
  217.   *end = '\0' ;
  218.   pw->pw_dir = start ;
  219.   start = end + 1 ;
  220.   end = strchr(start, '\n') ;
  221.   if (end != NULL)
  222.     *end = '\0' ;
  223.   pw->pw_shell = start ;
  224.   return pw;
  225. }
  226. #endif
  227.