powerpc: Update ulps
[glibc.git] / hesiod / nss_hesiod / hesiod-pwd.c
blob7a7958bd6094eafcfd8e3f76ea77d2646af346fe
1 /* Copyright (C) 1997-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
26 NSS_DECLARE_MODULE_FUNCTIONS (hesiod)
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>
34 enum nss_status
35 _nss_hesiod_setpwent (int stayopen)
37 return NSS_STATUS_SUCCESS;
40 enum nss_status
41 _nss_hesiod_endpwent (void)
43 return NSS_STATUS_SUCCESS;
46 static enum nss_status
47 lookup (const char *name, const char *type, struct passwd *pwd,
48 char *buffer, size_t buflen, int *errnop)
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;
58 if (hesiod_init (&context) < 0)
59 return NSS_STATUS_UNAVAIL;
61 list = hesiod_resolve (context, name, type);
62 if (list == NULL)
64 int err = errno;
65 hesiod_end (context);
66 __set_errno (olderr);
67 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
70 linebuflen = buffer + buflen - data->linebuffer;
71 len = strlen (*list) + 1;
72 if (linebuflen < len)
74 hesiod_free_list (context, list);
75 hesiod_end (context);
76 *errnop = ERANGE;
77 return NSS_STATUS_TRYAGAIN;
80 memcpy (data->linebuffer, *list, len);
81 hesiod_free_list (context, list);
82 hesiod_end (context);
84 parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop);
85 if (parse_res < 1)
87 __set_errno (olderr);
88 return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
91 return NSS_STATUS_SUCCESS;
94 enum nss_status
95 _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd,
96 char *buffer, size_t buflen, int *errnop)
98 return lookup (name, "passwd", pwd, buffer, buflen, errnop);
101 enum nss_status
102 _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd,
103 char *buffer, size_t buflen, int *errnop)
105 char uidstr[21]; /* We will probably never have a gid_t with more
106 than 64 bits. */
108 snprintf (uidstr, sizeof uidstr, "%d", uid);
110 return lookup (uidstr, "uid", pwd, buffer, buflen, errnop);