]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-spwd.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-spwd.c
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
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
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <nss.h>
21 #include <errno.h>
22 #include <shadow.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
26 #include <rpcsvc/nislib.h>
27
28 #include "nss-nisplus.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result = NULL;
33 static nis_name *names = NULL;
34
35 #define NISENTRYVAL(idx,col,res) \
36 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
37
38 #define NISENTRYLEN(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
40
41 int
42 _nss_nisplus_parse_spent (nis_result *result, struct spwd *sp,
43 char *buffer, size_t buflen)
44 {
45 char *first_unused = buffer;
46 size_t room_left = buflen;
47
48 if (result == NULL)
49 return 0;
50
51 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
52 result->objects.objects_len != 1 ||
53 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
54 strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
55 "passwd_tbl") != 0 ||
56 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 8)
57 return 0;
58
59 if (NISENTRYLEN(0, 0, result) >= room_left)
60 {
61 /* The line is too long for our buffer. */
62 no_more_room:
63 __set_errno (ERANGE);
64 return 0;
65 }
66
67 strncpy (first_unused, NISENTRYVAL (0, 0, result),
68 NISENTRYLEN (0, 0, result));
69 first_unused[NISENTRYLEN(0, 0, result)] = '\0';
70 sp->sp_namp = first_unused;
71 room_left -= (strlen (first_unused) +1);
72 first_unused += strlen (first_unused) +1;
73
74 if (NISENTRYLEN(0, 1, result) >= room_left)
75 goto no_more_room;
76
77 strncpy (first_unused, NISENTRYVAL (0, 1, result),
78 NISENTRYLEN (0, 1, result));
79 first_unused[NISENTRYLEN(0, 1, result)] = '\0';
80 sp->sp_pwdp = first_unused;
81 room_left -= (strlen (first_unused) +1);
82 first_unused += strlen (first_unused) +1;
83
84 sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact =
85 sp->sp_expire = sp->sp_flag = -1;
86
87 if (NISENTRYVAL (0, 7, result) != NULL)
88 {
89 char *line, *cp;
90
91 line = NISENTRYVAL (0, 7, result);
92 cp = strchr (line, ':');
93 if (cp == NULL)
94 return 0;
95 *cp++ = '\0';
96 sp->sp_lstchg = atol (line);
97
98 line = cp;
99 cp = strchr (line, ':');
100 if (cp == NULL)
101 return 0;
102 *cp++ = '\0';
103 sp->sp_min = atol(line);
104
105 line = cp;
106 cp = strchr (line, ':');
107 if (cp == NULL)
108 return 0;
109 *cp++ = '\0';
110 sp->sp_max = atol(line);
111
112 line = cp;
113 cp = strchr (line, ':');
114 if (cp == NULL)
115 return 0;
116 *cp++ = '\0';
117 sp->sp_warn = atol(line);
118
119 line = cp;
120 cp = strchr (line, ':');
121 if (cp == NULL)
122 return 0;
123 *cp++ = '\0';
124 sp->sp_inact = atol(line);
125
126 line = cp;
127 cp = strchr (line, ':');
128 if (cp == NULL)
129 return 0;
130 *cp++ = '\0';
131 sp->sp_expire = atol(line);
132
133 line = cp;
134 if (line == NULL)
135 return 0;
136 sp->sp_flag = atol(line);
137 }
138
139 return 1;
140 }
141
142 enum nss_status
143 _nss_nisplus_setspent (void)
144 {
145 __libc_lock_lock (lock);
146
147 if (result)
148 nis_freeresult (result);
149 result = NULL;
150 if (names)
151 {
152 nis_freenames (names);
153 names = NULL;
154 }
155
156 __libc_lock_unlock (lock);
157
158 return NSS_STATUS_SUCCESS;
159 }
160
161 enum nss_status
162 _nss_nisplus_endspent (void)
163 {
164 __libc_lock_lock (lock);
165
166 if (result)
167 nis_freeresult (result);
168 result = NULL;
169 if (names)
170 {
171 nis_freenames (names);
172 names = NULL;
173 }
174
175 __libc_lock_unlock (lock);
176
177 return NSS_STATUS_SUCCESS;
178 }
179
180 static enum nss_status
181 internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
182 {
183 int parse_res;
184
185 /* Get the next entry until we found a correct one. */
186 do
187 {
188 if (result == NULL)
189 {
190 names = nis_getnames ("passwd.org_dir");
191 if (names == NULL || names[0] == NULL)
192 return NSS_STATUS_UNAVAIL;
193
194 result = nis_first_entry (names[0]);
195 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
196 return niserr2nss (result->status);
197 }
198 else
199 {
200 nis_result *res;
201
202 res = nis_next_entry (names[0], &result->cookie);
203 nis_freeresult (result);
204 result = res;
205 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
206 return niserr2nss (result->status);
207 }
208
209 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen);
210 } while (!parse_res);
211
212 return NSS_STATUS_SUCCESS;
213 }
214
215 enum nss_status
216 _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen)
217 {
218 int status;
219
220 __libc_lock_lock (lock);
221
222 status = internal_nisplus_getspent_r (result, buffer, buflen);
223
224 __libc_lock_unlock (lock);
225
226 return status;
227 }
228
229 enum nss_status
230 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
231 char *buffer, size_t buflen)
232 {
233 int parse_res;
234
235 if (name == NULL || strlen (name) > 8)
236 return NSS_STATUS_NOTFOUND;
237 else
238 {
239 nis_result *result;
240 char buf[strlen (name) + 24];
241
242 sprintf (buf, "[name=%s],passwd.org_dir", name);
243
244 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
245
246 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
247 {
248 enum nss_status status = niserr2nss (result->status);
249
250 nis_freeresult (result);
251 return status;
252 }
253
254 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen);
255
256 nis_freeresult (result);
257
258 if (parse_res)
259 return NSS_STATUS_SUCCESS;
260
261 if (!parse_res && errno == ERANGE)
262 return NSS_STATUS_TRYAGAIN;
263 else
264 return NSS_STATUS_NOTFOUND;
265 }
266 }