]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nisplus/nisplus-spwd.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-spwd.c
CommitLineData
04277e02 1/* Copyright (C) 1997-2019 Free Software Foundation, Inc.
e61abf83
UD
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 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.
e61abf83
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.
e61abf83 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/>. */
e61abf83
UD
18
19#include <nss.h>
20#include <errno.h>
21#include <shadow.h>
22#include <string.h>
ec999b8e 23#include <libc-lock.h>
e61abf83 24#include <rpcsvc/nis.h>
e61abf83
UD
25
26#include "nss-nisplus.h"
2d7da676 27#include "nisplus-parser.h"
e61abf83
UD
28
29__libc_lock_define_initialized (static, lock)
30
fc9f33e3 31static nis_result *result;
e61abf83 32
48b29391
UD
33/* Defined in nisplus-pwd.c. */
34extern nis_name pwd_tablename_val attribute_hidden;
35extern size_t pwd_tablename_len attribute_hidden;
36extern enum nss_status _nss_pwd_create_tablename (int *errnop);
2d7da676 37
e61abf83
UD
38
39enum nss_status
51eecc4a 40_nss_nisplus_setspent (int stayopen)
e61abf83 41{
2d7da676 42 enum nss_status status = NSS_STATUS_SUCCESS;
d71b808a 43 int err;
2d7da676 44
e61abf83
UD
45 __libc_lock_lock (lock);
46
48b29391
UD
47 if (result != NULL)
48 {
49 nis_freeresult (result);
50 result = NULL;
51 }
2d7da676 52
48b29391
UD
53 if (pwd_tablename_val == NULL)
54 status = _nss_pwd_create_tablename (&err);
e61abf83
UD
55
56 __libc_lock_unlock (lock);
57
1a544854 58 return status;
e61abf83
UD
59}
60
61enum nss_status
62_nss_nisplus_endspent (void)
63{
64 __libc_lock_lock (lock);
65
48b29391
UD
66 if (result != NULL)
67 {
68 nis_freeresult (result);
69 result = NULL;
70 }
e61abf83
UD
71
72 __libc_lock_unlock (lock);
73
74 return NSS_STATUS_SUCCESS;
75}
76
77static enum nss_status
d71b808a
UD
78internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
79 int *errnop)
e61abf83
UD
80{
81 int parse_res;
82
83 /* Get the next entry until we found a correct one. */
84 do
85 {
60c96635
UD
86 nis_result *saved_res;
87
e61abf83
UD
88 if (result == NULL)
89 {
60c96635
UD
90 saved_res = NULL;
91
1a544854 92 if (pwd_tablename_val == NULL)
d71b808a 93 {
48b29391 94 enum nss_status status = _nss_pwd_create_tablename (errnop);
d71b808a
UD
95
96 if (status != NSS_STATUS_SUCCESS)
97 return status;
98 }
e61abf83 99
48b29391 100 result = nis_first_entry (pwd_tablename_val);
487609e3
UD
101 if (result == NULL)
102 {
103 *errnop = errno;
104 return NSS_STATUS_TRYAGAIN;
105 }
e61abf83
UD
106 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
107 return niserr2nss (result->status);
108 }
109 else
110 {
60c96635 111 saved_res = result;
48b29391 112 result = nis_next_entry (pwd_tablename_val, &result->cookie);
487609e3
UD
113 if (result == NULL)
114 {
115 *errnop = errno;
116 return NSS_STATUS_TRYAGAIN;
117 }
e61abf83 118 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
60c96635
UD
119 {
120 nis_freeresult (saved_res);
121 return niserr2nss (result->status);
122 }
e61abf83
UD
123 }
124
d71b808a
UD
125 parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
126 buflen, errnop);
a1ffb40e 127 if (__glibc_unlikely (parse_res == -1))
60c96635
UD
128 {
129 nis_freeresult (result);
130 result = saved_res;
d71b808a 131 *errnop = ERANGE;
60c96635
UD
132 return NSS_STATUS_TRYAGAIN;
133 }
48b29391
UD
134
135 if (saved_res != NULL)
136 nis_freeresult (saved_res);
137 }
138 while (!parse_res);
e61abf83
UD
139
140 return NSS_STATUS_SUCCESS;
141}
142
143enum nss_status
d71b808a
UD
144_nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen,
145 int *errnop)
e61abf83
UD
146{
147 int status;
148
149 __libc_lock_lock (lock);
150
d71b808a 151 status = internal_nisplus_getspent_r (result, buffer, buflen, errnop);
e61abf83
UD
152
153 __libc_lock_unlock (lock);
154
155 return status;
156}
157
158enum nss_status
159_nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
d71b808a 160 char *buffer, size_t buflen, int *errnop)
e61abf83
UD
161{
162 int parse_res;
163
48b29391 164 if (pwd_tablename_val == NULL)
d71b808a 165 {
48b29391
UD
166 enum nss_status status = _nss_pwd_create_tablename (errnop);
167
d71b808a
UD
168 if (status != NSS_STATUS_SUCCESS)
169 return status;
170 }
2d7da676 171
ac9f45cf
UD
172 if (name == NULL)
173 {
174 *errnop = EINVAL;
175 return NSS_STATUS_NOTFOUND;
176 }
0ecb606c 177
48b29391
UD
178 nis_result *result;
179 char buf[strlen (name) + 9 + pwd_tablename_len];
180 int olderr = errno;
e61abf83 181
48b29391 182 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
0ecb606c 183
697d37b1 184 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
a334319f 185
48b29391
UD
186 if (result == NULL)
187 {
188 *errnop = ENOMEM;
189 return NSS_STATUS_TRYAGAIN;
190 }
a334319f 191
a1ffb40e 192 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
48b29391
UD
193 {
194 enum nss_status status = niserr2nss (result->status);
195
196 __set_errno (olderr);
0ecb606c 197
a334319f 198 nis_freeresult (result);
48b29391
UD
199 return status;
200 }
201
202 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, errnop);
203 nis_freeresult (result);
a334319f 204
a1ffb40e 205 if (__glibc_unlikely (parse_res < 1))
48b29391
UD
206 {
207 if (parse_res == -1)
a334319f 208 {
48b29391
UD
209 *errnop = ERANGE;
210 return NSS_STATUS_TRYAGAIN;
211 }
212 else
213 {
214 __set_errno (olderr);
215 return NSS_STATUS_NOTFOUND;
a334319f 216 }
a334319f 217 }
48b29391
UD
218
219 return NSS_STATUS_SUCCESS;
e61abf83 220}