]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1997-2014 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 | ||
2f54c82d UD |
27 | #include "nss_hesiod.h" |
28 | ||
61eb22d3 UD |
29 | /* Get the declaration of the parser function. */ |
30 | #define ENTNAME pwent | |
31 | #define STRUCTURE passwd | |
32 | #define EXTERN_PARSER | |
33 | #include <nss/nss_files/files-parse.c> | |
34 | ||
61eb22d3 | 35 | enum nss_status |
51eecc4a | 36 | _nss_hesiod_setpwent (int stayopen) |
61eb22d3 | 37 | { |
2f54c82d | 38 | return NSS_STATUS_SUCCESS; |
61eb22d3 UD |
39 | } |
40 | ||
41 | enum nss_status | |
42 | _nss_hesiod_endpwent (void) | |
43 | { | |
61eb22d3 UD |
44 | return NSS_STATUS_SUCCESS; |
45 | } | |
46 | ||
47 | static enum nss_status | |
48 | lookup (const char *name, const char *type, struct passwd *pwd, | |
d71b808a | 49 | char *buffer, size_t buflen, int *errnop) |
61eb22d3 | 50 | { |
61eb22d3 UD |
51 | struct parser_data *data = (void *) buffer; |
52 | size_t linebuflen; | |
2f54c82d | 53 | void *context; |
61eb22d3 UD |
54 | char **list; |
55 | int parse_res; | |
d71b808a | 56 | size_t len; |
34816665 | 57 | int olderr = errno; |
61eb22d3 | 58 | |
2f54c82d UD |
59 | context = _nss_hesiod_init (); |
60 | if (context == NULL) | |
61 | return NSS_STATUS_UNAVAIL; | |
61eb22d3 UD |
62 | |
63 | list = hesiod_resolve (context, name, type); | |
64 | if (list == NULL) | |
2f54c82d | 65 | { |
34816665 | 66 | int err = errno; |
2f54c82d | 67 | hesiod_end (context); |
34816665 UD |
68 | __set_errno (olderr); |
69 | return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL; | |
2f54c82d | 70 | } |
61eb22d3 UD |
71 | |
72 | linebuflen = buffer + buflen - data->linebuffer; | |
d71b808a UD |
73 | len = strlen (*list) + 1; |
74 | if (linebuflen < len) | |
61eb22d3 UD |
75 | { |
76 | hesiod_free_list (context, list); | |
2f54c82d | 77 | hesiod_end (context); |
d71b808a | 78 | *errnop = ERANGE; |
61eb22d3 UD |
79 | return NSS_STATUS_TRYAGAIN; |
80 | } | |
81 | ||
d71b808a | 82 | memcpy (data->linebuffer, *list, len); |
61eb22d3 | 83 | hesiod_free_list (context, list); |
2f54c82d | 84 | hesiod_end (context); |
61eb22d3 | 85 | |
d71b808a | 86 | parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop); |
61eb22d3 | 87 | if (parse_res < 1) |
34816665 UD |
88 | { |
89 | __set_errno (olderr); | |
90 | return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND; | |
91 | } | |
61eb22d3 UD |
92 | |
93 | return NSS_STATUS_SUCCESS; | |
94 | } | |
95 | ||
96 | enum nss_status | |
97 | _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd, | |
d71b808a | 98 | char *buffer, size_t buflen, int *errnop) |
61eb22d3 | 99 | { |
2f54c82d | 100 | return lookup (name, "passwd", pwd, buffer, buflen, errnop); |
61eb22d3 UD |
101 | } |
102 | ||
103 | enum nss_status | |
104 | _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd, | |
d71b808a | 105 | char *buffer, size_t buflen, int *errnop) |
61eb22d3 | 106 | { |
61eb22d3 UD |
107 | char uidstr[21]; /* We will probably never have a gid_t with more |
108 | than 64 bits. */ | |
109 | ||
110 | snprintf (uidstr, sizeof uidstr, "%d", uid); | |
111 | ||
2f54c82d | 112 | return lookup (uidstr, "uid", pwd, buffer, buflen, errnop); |
61eb22d3 | 113 | } |