]> git.ipfire.org Git - thirdparty/glibc.git/blame - hesiod/nss_hesiod/hesiod-pwd.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / hesiod / nss_hesiod / hesiod-pwd.c
CommitLineData
04277e02 1/* Copyright (C) 1997-2019 Free Software Foundation, Inc.
61eb22d3
UD
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
61eb22d3
UD
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
41bdb6e2 13 Lesser General Public License for more details.
61eb22d3 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
61eb22d3 18
61eb22d3
UD
19#include <errno.h>
20#include <hesiod.h>
61eb22d3 21#include <pwd.h>
2f54c82d 22#include <nss.h>
61eb22d3
UD
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27/* Get the declaration of the parser function. */
28#define ENTNAME pwent
29#define STRUCTURE passwd
30#define EXTERN_PARSER
31#include <nss/nss_files/files-parse.c>
32
61eb22d3 33enum nss_status
51eecc4a 34_nss_hesiod_setpwent (int stayopen)
61eb22d3 35{
2f54c82d 36 return NSS_STATUS_SUCCESS;
61eb22d3
UD
37}
38
39enum nss_status
40_nss_hesiod_endpwent (void)
41{
61eb22d3
UD
42 return NSS_STATUS_SUCCESS;
43}
44
45static enum nss_status
46lookup (const char *name, const char *type, struct passwd *pwd,
d71b808a 47 char *buffer, size_t buflen, int *errnop)
61eb22d3 48{
61eb22d3
UD
49 struct parser_data *data = (void *) buffer;
50 size_t linebuflen;
2f54c82d 51 void *context;
61eb22d3
UD
52 char **list;
53 int parse_res;
d71b808a 54 size_t len;
34816665 55 int olderr = errno;
61eb22d3 56
5018f16c 57 if (hesiod_init (&context) < 0)
2f54c82d 58 return NSS_STATUS_UNAVAIL;
61eb22d3
UD
59
60 list = hesiod_resolve (context, name, type);
61 if (list == NULL)
2f54c82d 62 {
34816665 63 int err = errno;
2f54c82d 64 hesiod_end (context);
34816665
UD
65 __set_errno (olderr);
66 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
2f54c82d 67 }
61eb22d3
UD
68
69 linebuflen = buffer + buflen - data->linebuffer;
d71b808a
UD
70 len = strlen (*list) + 1;
71 if (linebuflen < len)
61eb22d3
UD
72 {
73 hesiod_free_list (context, list);
2f54c82d 74 hesiod_end (context);
d71b808a 75 *errnop = ERANGE;
61eb22d3
UD
76 return NSS_STATUS_TRYAGAIN;
77 }
78
d71b808a 79 memcpy (data->linebuffer, *list, len);
61eb22d3 80 hesiod_free_list (context, list);
2f54c82d 81 hesiod_end (context);
61eb22d3 82
d71b808a 83 parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop);
61eb22d3 84 if (parse_res < 1)
34816665
UD
85 {
86 __set_errno (olderr);
87 return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
88 }
61eb22d3
UD
89
90 return NSS_STATUS_SUCCESS;
91}
92
93enum nss_status
94_nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd,
d71b808a 95 char *buffer, size_t buflen, int *errnop)
61eb22d3 96{
2f54c82d 97 return lookup (name, "passwd", pwd, buffer, buflen, errnop);
61eb22d3
UD
98}
99
100enum nss_status
101_nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd,
d71b808a 102 char *buffer, size_t buflen, int *errnop)
61eb22d3 103{
61eb22d3
UD
104 char uidstr[21]; /* We will probably never have a gid_t with more
105 than 64 bits. */
106
107 snprintf (uidstr, sizeof uidstr, "%d", uid);
108
2f54c82d 109 return lookup (uidstr, "uid", pwd, buffer, buflen, errnop);
61eb22d3 110}