1 | /* Copyright (C) 1996-2017 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
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. |
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 |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <nss.h> |
20 | #include <ctype.h> |
21 | #include <errno.h> |
22 | #include <string.h> |
23 | /* The following is an ugly trick to avoid a prototype declaration for |
24 | _nss_nis_endspent. */ |
25 | #define _nss_nis_endspent _nss_nis_endspent_XXX |
26 | #include <shadow.h> |
27 | #undef _nss_nis_endspent |
28 | #include <libc-lock.h> |
29 | #include <rpcsvc/yp.h> |
30 | #include <rpcsvc/ypclnt.h> |
31 | |
32 | #include "nss-nis.h" |
33 | #include <libnsl.h> |
34 | |
35 | /* Get the declaration of the parser function. */ |
36 | #define ENTNAME spent |
37 | #define STRUCTURE spwd |
38 | #define EXTERN_PARSER |
39 | #include <nss/nss_files/files-parse.c> |
40 | |
41 | /* Protect global state against multiple changers */ |
42 | __libc_lock_define_initialized (static, lock) |
43 | |
44 | static bool new_start = true; |
45 | static bool ent_adjunct_used; |
46 | static char *oldkey; |
47 | static int oldkeylen; |
48 | |
49 | enum nss_status |
50 | _nss_nis_setspent (int stayopen) |
51 | { |
52 | __libc_lock_lock (lock); |
53 | |
54 | new_start = true; |
55 | ent_adjunct_used = false; |
56 | free (oldkey); |
57 | oldkey = NULL; |
58 | oldkeylen = 0; |
59 | |
60 | __libc_lock_unlock (lock); |
61 | |
62 | return NSS_STATUS_SUCCESS; |
63 | } |
64 | /* Make _nss_nis_endspent an alias of _nss_nis_setspent. We do this |
65 | even though the prototypes don't match. The argument of setspent |
66 | is not used so this makes no difference. */ |
67 | strong_alias (_nss_nis_setspent, _nss_nis_endspent) |
68 | |
69 | static enum nss_status |
70 | internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen, |
71 | int *errnop) |
72 | { |
73 | char *domain; |
74 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
75 | return NSS_STATUS_UNAVAIL; |
76 | |
77 | /* Get the next entry until we found a correct one. */ |
78 | int parse_res; |
79 | do |
80 | { |
81 | char *result; |
82 | char *outkey; |
83 | int len; |
84 | int keylen; |
85 | int yperr; |
86 | |
87 | if (new_start) |
88 | { |
89 | yperr = yp_first (domain, "shadow.byname" , &outkey, &keylen, &result, |
90 | &len); |
91 | if (__builtin_expect (yperr == YPERR_MAP, 0) |
92 | && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW)) |
93 | { |
94 | free (result); |
95 | yperr = yp_first (domain, "passwd.adjunct.byname" , &outkey, |
96 | &keylen, &result, &len); |
97 | ent_adjunct_used = true; |
98 | } |
99 | } |
100 | else |
101 | yperr = yp_next (domain, (ent_adjunct_used |
102 | ? "passwd.adjunct.byname" : "shadow.byname" ), |
103 | oldkey, oldkeylen, &outkey, &keylen, &result, &len); |
104 | |
105 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
106 | { |
107 | enum nss_status retval = yperr2nss (yperr); |
108 | |
109 | if (retval == NSS_STATUS_TRYAGAIN) |
110 | *errnop = errno; |
111 | return retval; |
112 | } |
113 | |
114 | if (__builtin_expect ((size_t) (len + (ent_adjunct_used ? 3 : 1)) |
115 | > buflen, 0)) |
116 | { |
117 | free (result); |
118 | *errnop = ERANGE; |
119 | return NSS_STATUS_TRYAGAIN; |
120 | } |
121 | |
122 | char *p = strncpy (buffer, result, len); |
123 | if (ent_adjunct_used) |
124 | /* This is an ugly trick. The format of passwd.adjunct.byname almost |
125 | matches the shadow.byname format except that the last two fields |
126 | are missing. Synthesize them by marking them empty. */ |
127 | strcpy (&buffer[len], "::" ); |
128 | else |
129 | buffer[len] = '\0'; |
130 | while (isspace (*p)) |
131 | ++p; |
132 | free (result); |
133 | |
134 | parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen, |
135 | errnop); |
136 | if (__builtin_expect (parse_res == -1, 0)) |
137 | { |
138 | free (outkey); |
139 | *errnop = ERANGE; |
140 | return NSS_STATUS_TRYAGAIN; |
141 | } |
142 | |
143 | free (oldkey); |
144 | oldkey = outkey; |
145 | oldkeylen = keylen; |
146 | new_start = false; |
147 | } |
148 | while (!parse_res); |
149 | |
150 | return NSS_STATUS_SUCCESS; |
151 | } |
152 | |
153 | enum nss_status |
154 | _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen, |
155 | int *errnop) |
156 | { |
157 | int status; |
158 | |
159 | __libc_lock_lock (lock); |
160 | |
161 | status = internal_nis_getspent_r (result, buffer, buflen, errnop); |
162 | |
163 | __libc_lock_unlock (lock); |
164 | |
165 | return status; |
166 | } |
167 | |
168 | enum nss_status |
169 | _nss_nis_getspnam_r (const char *name, struct spwd *sp, |
170 | char *buffer, size_t buflen, int *errnop) |
171 | { |
172 | if (name == NULL) |
173 | { |
174 | *errnop = EINVAL; |
175 | return NSS_STATUS_UNAVAIL; |
176 | } |
177 | const size_t name_len = strlen (name); |
178 | |
179 | char *domain; |
180 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
181 | return NSS_STATUS_UNAVAIL; |
182 | |
183 | bool adjunct_used = false; |
184 | char *result; |
185 | int len; |
186 | int yperr = yp_match (domain, "shadow.byname" , name, name_len, &result, |
187 | &len); |
188 | if (__builtin_expect (yperr == YPERR_MAP, 0) |
189 | && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW)) |
190 | { |
191 | free (result); |
192 | yperr = yp_match (domain, "passwd.adjunct.byname" , name, name_len, |
193 | &result, &len); |
194 | adjunct_used = true; |
195 | } |
196 | |
197 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
198 | { |
199 | enum nss_status retval = yperr2nss (yperr); |
200 | |
201 | if (retval == NSS_STATUS_TRYAGAIN) |
202 | *errnop = errno; |
203 | return retval; |
204 | } |
205 | |
206 | if (__glibc_unlikely ((size_t) (len + (adjunct_used ? 3 : 1)) > buflen)) |
207 | { |
208 | free (result); |
209 | *errnop = ERANGE; |
210 | return NSS_STATUS_TRYAGAIN; |
211 | } |
212 | |
213 | char *p = strncpy (buffer, result, len); |
214 | if (__builtin_expect (adjunct_used, false)) |
215 | /* This is an ugly trick. The format of passwd.adjunct.byname almost |
216 | matches the shadow.byname format except that the last two fields |
217 | are missing. Synthesize them by marking them empty. */ |
218 | strcpy (&buffer[len], "::" ); |
219 | else |
220 | buffer[len] = '\0'; |
221 | while (isspace (*p)) |
222 | ++p; |
223 | free (result); |
224 | |
225 | int parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen, |
226 | errnop); |
227 | if (__glibc_unlikely (parse_res < 1)) |
228 | { |
229 | if (parse_res == -1) |
230 | return NSS_STATUS_TRYAGAIN; |
231 | else |
232 | return NSS_STATUS_NOTFOUND; |
233 | } |
234 | return NSS_STATUS_SUCCESS; |
235 | } |
236 | |