]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-spwd.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nis / nss_nis / nis-spwd.c
CommitLineData
04277e02 1/* Copyright (C) 1996-2019 Free Software Foundation, Inc.
6259ec0d
UD
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
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.
6259ec0d
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.
6259ec0d 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
6259ec0d
UD
18
19#include <nss.h>
20#include <ctype.h>
21#include <errno.h>
22#include <string.h>
b677d674
UD
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
6259ec0d 26#include <shadow.h>
b677d674 27#undef _nss_nis_endspent
ec999b8e 28#include <libc-lock.h>
6259ec0d
UD
29#include <rpcsvc/yp.h>
30#include <rpcsvc/ypclnt.h>
31
32#include "nss-nis.h"
71170aa0 33#include <libnsl.h>
6259ec0d 34
7e3be507
UD
35/* Get the declaration of the parser function. */
36#define ENTNAME spent
37#define STRUCTURE spwd
38#define EXTERN_PARSER
cf29ffbe 39#include <nss/nss_files/files-parse.c>
7e3be507 40
6259ec0d
UD
41/* Protect global state against multiple changers */
42__libc_lock_define_initialized (static, lock)
43
71170aa0
UD
44static bool new_start = true;
45static bool ent_adjunct_used;
fc9f33e3
UD
46static char *oldkey;
47static int oldkeylen;
6259ec0d
UD
48
49enum nss_status
51eecc4a 50_nss_nis_setspent (int stayopen)
6259ec0d
UD
51{
52 __libc_lock_lock (lock);
53
71170aa0
UD
54 new_start = true;
55 ent_adjunct_used = false;
b677d674
UD
56 free (oldkey);
57 oldkey = NULL;
58 oldkeylen = 0;
6259ec0d
UD
59
60 __libc_lock_unlock (lock);
61
62 return NSS_STATUS_SUCCESS;
63}
b677d674
UD
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. */
67strong_alias (_nss_nis_setspent, _nss_nis_endspent)
6259ec0d
UD
68
69static enum nss_status
d71b808a
UD
70internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
71 int *errnop)
6259ec0d 72{
ab9a9ff8 73 char *domain;
a1ffb40e 74 if (__glibc_unlikely (yp_get_default_domain (&domain)))
6259ec0d
UD
75 return NSS_STATUS_UNAVAIL;
76
77 /* Get the next entry until we found a correct one. */
ab9a9ff8 78 int parse_res;
6259ec0d
UD
79 do
80 {
ab9a9ff8
UD
81 char *result;
82 char *outkey;
83 int len;
84 int keylen;
85 int yperr;
6259ec0d
UD
86
87 if (new_start)
71170aa0
UD
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 }
6259ec0d 100 else
71170aa0
UD
101 yperr = yp_next (domain, (ent_adjunct_used
102 ? "passwd.adjunct.byname" : "shadow.byname"),
103 oldkey, oldkeylen, &outkey, &keylen, &result, &len);
6259ec0d 104
a1ffb40e 105 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
71170aa0 106 {
ab9a9ff8
UD
107 enum nss_status retval = yperr2nss (yperr);
108
34816665 109 if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 110 *errnop = errno;
71170aa0
UD
111 return retval;
112 }
6259ec0d 113
71170aa0
UD
114 if (__builtin_expect ((size_t) (len + (ent_adjunct_used ? 3 : 1))
115 > buflen, 0))
116 {
117 free (result);
d71b808a 118 *errnop = ERANGE;
71170aa0
UD
119 return NSS_STATUS_TRYAGAIN;
120 }
6259ec0d 121
ab9a9ff8 122 char *p = strncpy (buffer, result, len);
71170aa0
UD
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';
6259ec0d 130 while (isspace (*p))
71170aa0 131 ++p;
6259ec0d
UD
132 free (result);
133
ab9a9ff8
UD
134 parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
135 errnop);
136 if (__builtin_expect (parse_res == -1, 0))
60c96635
UD
137 {
138 free (outkey);
d71b808a 139 *errnop = ERANGE;
60c96635
UD
140 return NSS_STATUS_TRYAGAIN;
141 }
d71b808a 142
6259ec0d
UD
143 free (oldkey);
144 oldkey = outkey;
145 oldkeylen = keylen;
71170aa0 146 new_start = false;
6259ec0d
UD
147 }
148 while (!parse_res);
149
150 return NSS_STATUS_SUCCESS;
151}
152
153enum nss_status
d71b808a
UD
154_nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
155 int *errnop)
6259ec0d
UD
156{
157 int status;
158
159 __libc_lock_lock (lock);
160
d71b808a 161 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
6259ec0d
UD
162
163 __libc_lock_unlock (lock);
164
165 return status;
166}
167
168enum nss_status
169_nss_nis_getspnam_r (const char *name, struct spwd *sp,
d71b808a 170 char *buffer, size_t buflen, int *errnop)
6259ec0d 171{
6259ec0d
UD
172 if (name == NULL)
173 {
ac9f45cf 174 *errnop = EINVAL;
6259ec0d
UD
175 return NSS_STATUS_UNAVAIL;
176 }
71170aa0 177 const size_t name_len = strlen (name);
6259ec0d 178
ab9a9ff8 179 char *domain;
a1ffb40e 180 if (__glibc_unlikely (yp_get_default_domain (&domain)))
6259ec0d
UD
181 return NSS_STATUS_UNAVAIL;
182
71170aa0 183 bool adjunct_used = false;
ab9a9ff8
UD
184 char *result;
185 int len;
71170aa0 186 int yperr = yp_match (domain, "shadow.byname", name, name_len, &result,
ab9a9ff8 187 &len);
71170aa0
UD
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 }
6259ec0d 196
a1ffb40e 197 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
6259ec0d 198 {
ab9a9ff8
UD
199 enum nss_status retval = yperr2nss (yperr);
200
34816665 201 if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 202 *errnop = errno;
6259ec0d
UD
203 return retval;
204 }
205
a1ffb40e 206 if (__glibc_unlikely ((size_t) (len + (adjunct_used ? 3 : 1)) > buflen))
6259ec0d
UD
207 {
208 free (result);
d71b808a 209 *errnop = ERANGE;
6259ec0d
UD
210 return NSS_STATUS_TRYAGAIN;
211 }
212
ab9a9ff8 213 char *p = strncpy (buffer, result, len);
71170aa0
UD
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';
6259ec0d
UD
221 while (isspace (*p))
222 ++p;
223 free (result);
224
ab9a9ff8
UD
225 int parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
226 errnop);
a1ffb40e 227 if (__glibc_unlikely (parse_res < 1))
ac9f45cf
UD
228 {
229 if (parse_res == -1)
230 return NSS_STATUS_TRYAGAIN;
231 else
34816665 232 return NSS_STATUS_NOTFOUND;
ac9f45cf
UD
233 }
234 return NSS_STATUS_SUCCESS;
6259ec0d 235}