(root)/
glibc-2.38/
hesiod/
nss_hesiod/
hesiod-pwd.c
       1  /* Copyright (C) 1997-2023 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 Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the 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     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <hesiod.h>
      20  #include <pwd.h>
      21  #include <nss.h>
      22  #include <stdio.h>
      23  #include <stdlib.h>
      24  #include <string.h>
      25  
      26  NSS_DECLARE_MODULE_FUNCTIONS (hesiod)
      27  
      28  /* Get the declaration of the parser function.  */
      29  #define ENTNAME pwent
      30  #define STRUCTURE passwd
      31  #define EXTERN_PARSER
      32  #include <nss/nss_files/files-parse.c>
      33  
      34  enum nss_status
      35  _nss_hesiod_setpwent (int stayopen)
      36  {
      37    return NSS_STATUS_SUCCESS;
      38  }
      39  
      40  enum nss_status
      41  _nss_hesiod_endpwent (void)
      42  {
      43    return NSS_STATUS_SUCCESS;
      44  }
      45  
      46  static enum nss_status
      47  lookup (const char *name, const char *type, struct passwd *pwd,
      48  	char *buffer, size_t buflen, int *errnop)
      49  {
      50    struct parser_data *data = (void *) buffer;
      51    size_t linebuflen;
      52    void *context;
      53    char **list;
      54    int parse_res;
      55    size_t len;
      56    int olderr = errno;
      57  
      58    if (hesiod_init (&context) < 0)
      59      return NSS_STATUS_UNAVAIL;
      60  
      61    list = hesiod_resolve (context, name, type);
      62    if (list == NULL)
      63      {
      64        int err = errno;
      65        hesiod_end (context);
      66        __set_errno (olderr);
      67        return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
      68      }
      69  
      70    linebuflen = buffer + buflen - data->linebuffer;
      71    len = strlen (*list) + 1;
      72    if (linebuflen < len)
      73      {
      74        hesiod_free_list (context, list);
      75        hesiod_end (context);
      76        *errnop = ERANGE;
      77        return NSS_STATUS_TRYAGAIN;
      78      }
      79  
      80    memcpy (data->linebuffer, *list, len);
      81    hesiod_free_list (context, list);
      82    hesiod_end (context);
      83  
      84    parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop);
      85    if (parse_res < 1)
      86      {
      87        __set_errno (olderr);
      88        return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
      89      }
      90  
      91    return NSS_STATUS_SUCCESS;
      92  }
      93  
      94  enum nss_status
      95  _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd,
      96  			char *buffer, size_t buflen, int *errnop)
      97  {
      98    return lookup (name, "passwd", pwd, buffer, buflen, errnop);
      99  }
     100  
     101  enum nss_status
     102  _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd,
     103  			char *buffer, size_t buflen, int *errnop)
     104  {
     105    char uidstr[21];	/* We will probably never have a gid_t with more
     106  			   than 64 bits.  */
     107  
     108    snprintf (uidstr, sizeof uidstr, "%d", uid);
     109  
     110    return lookup (uidstr, "uid", pwd, buffer, buflen, errnop);
     111  }