]>
Commit | Line | Data |
---|---|---|
e61abf83 UD |
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 <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 | ||
26dee9c4 | 41 | int |
e61abf83 UD |
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 | char *line, *cp; | |
48 | ||
49 | if (result == NULL) | |
26dee9c4 | 50 | return 0; |
e61abf83 UD |
51 | |
52 | if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || | |
53 | result->objects.objects_len != 1 || | |
54 | result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || | |
55 | strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, | |
56 | "passwd_tbl") != 0 || | |
57 | result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 8) | |
26dee9c4 | 58 | return 0; |
e61abf83 UD |
59 | |
60 | if (NISENTRYLEN(0, 0, result) >= room_left) | |
61 | { | |
62 | /* The line is too long for our buffer. */ | |
63 | no_more_room: | |
64 | __set_errno (ERANGE); | |
26dee9c4 | 65 | return 0; |
e61abf83 UD |
66 | } |
67 | ||
68 | strncpy (first_unused, NISENTRYVAL (0, 0, result), | |
69 | NISENTRYLEN (0, 0, result)); | |
70 | first_unused[NISENTRYLEN(0, 0, result)] = '\0'; | |
71 | sp->sp_namp = first_unused; | |
72 | room_left -= (strlen (first_unused) +1); | |
73 | first_unused += strlen (first_unused) +1; | |
74 | ||
75 | if (NISENTRYLEN(0, 1, result) >= room_left) | |
76 | goto no_more_room; | |
77 | ||
78 | strncpy (first_unused, NISENTRYVAL (0, 1, result), | |
79 | NISENTRYLEN (0, 1, result)); | |
80 | first_unused[NISENTRYLEN(0, 1, result)] = '\0'; | |
81 | sp->sp_pwdp = first_unused; | |
82 | room_left -= (strlen (first_unused) +1); | |
83 | first_unused += strlen (first_unused) +1; | |
84 | ||
85 | sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact = | |
86 | sp->sp_expire = sp->sp_flag = -1; | |
87 | ||
88 | line = NISENTRYVAL (0, 7, result); | |
89 | cp = strchr (line, ':'); | |
90 | if (cp == NULL) | |
91 | return 0; | |
92 | *cp++ = '\0'; | |
1f205a47 | 93 | sp->sp_lstchg = atol (line); |
e61abf83 UD |
94 | |
95 | line = cp; | |
96 | cp = strchr (line, ':'); | |
97 | if (cp == NULL) | |
98 | return 0; | |
99 | *cp++ = '\0'; | |
1f205a47 | 100 | sp->sp_min = atol(line); |
e61abf83 UD |
101 | |
102 | line = cp; | |
103 | cp = strchr (line, ':'); | |
104 | if (cp == NULL) | |
105 | return 0; | |
106 | *cp++ = '\0'; | |
1f205a47 | 107 | sp->sp_max = atol(line); |
e61abf83 UD |
108 | |
109 | line = cp; | |
110 | cp = strchr (line, ':'); | |
111 | if (cp == NULL) | |
112 | return 0; | |
113 | *cp++ = '\0'; | |
1f205a47 | 114 | sp->sp_warn = atol(line); |
e61abf83 UD |
115 | |
116 | line = cp; | |
117 | cp = strchr (line, ':'); | |
118 | if (cp == NULL) | |
119 | return 0; | |
120 | *cp++ = '\0'; | |
1f205a47 | 121 | sp->sp_inact = atol(line); |
e61abf83 UD |
122 | |
123 | line = cp; | |
124 | cp = strchr (line, ':'); | |
125 | if (cp == NULL) | |
126 | return 0; | |
127 | *cp++ = '\0'; | |
1f205a47 | 128 | sp->sp_expire = atol(line); |
e61abf83 UD |
129 | |
130 | line = cp; | |
131 | if (line == NULL) | |
132 | return 0; | |
1f205a47 | 133 | sp->sp_flag = atol(line); |
e61abf83 UD |
134 | |
135 | return 1; | |
136 | } | |
137 | ||
138 | enum nss_status | |
139 | _nss_nisplus_setspent (void) | |
140 | { | |
141 | __libc_lock_lock (lock); | |
142 | ||
143 | if (result) | |
144 | nis_freeresult (result); | |
145 | result = NULL; | |
146 | if (names) | |
147 | { | |
148 | nis_freenames (names); | |
149 | names = NULL; | |
150 | } | |
151 | ||
152 | __libc_lock_unlock (lock); | |
153 | ||
154 | return NSS_STATUS_SUCCESS; | |
155 | } | |
156 | ||
157 | enum nss_status | |
158 | _nss_nisplus_endspent (void) | |
159 | { | |
160 | __libc_lock_lock (lock); | |
161 | ||
162 | if (result) | |
163 | nis_freeresult (result); | |
164 | result = NULL; | |
165 | if (names) | |
166 | { | |
167 | nis_freenames (names); | |
168 | names = NULL; | |
169 | } | |
170 | ||
171 | __libc_lock_unlock (lock); | |
172 | ||
173 | return NSS_STATUS_SUCCESS; | |
174 | } | |
175 | ||
176 | static enum nss_status | |
177 | internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen) | |
178 | { | |
179 | int parse_res; | |
180 | ||
181 | /* Get the next entry until we found a correct one. */ | |
182 | do | |
183 | { | |
184 | if (result == NULL) | |
185 | { | |
186 | names = nis_getnames ("passwd.org_dir"); | |
187 | if (names == NULL || names[0] == NULL) | |
188 | return NSS_STATUS_UNAVAIL; | |
189 | ||
190 | result = nis_first_entry (names[0]); | |
191 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
192 | return niserr2nss (result->status); | |
193 | } | |
194 | else | |
195 | { | |
196 | nis_result *res; | |
197 | ||
198 | res = nis_next_entry (names[0], &result->cookie); | |
199 | nis_freeresult (result); | |
200 | result = res; | |
201 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
202 | return niserr2nss (result->status); | |
203 | } | |
204 | ||
205 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); | |
206 | } while (!parse_res); | |
207 | ||
208 | return NSS_STATUS_SUCCESS; | |
209 | } | |
210 | ||
211 | enum nss_status | |
212 | _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen) | |
213 | { | |
214 | int status; | |
215 | ||
216 | __libc_lock_lock (lock); | |
217 | ||
218 | status = internal_nisplus_getspent_r (result, buffer, buflen); | |
219 | ||
220 | __libc_lock_unlock (lock); | |
221 | ||
222 | return status; | |
223 | } | |
224 | ||
225 | enum nss_status | |
226 | _nss_nisplus_getspnam_r (const char *name, struct spwd *sp, | |
227 | char *buffer, size_t buflen) | |
228 | { | |
229 | int parse_res; | |
230 | ||
231 | if (name == NULL || strlen (name) > 8) | |
232 | return NSS_STATUS_NOTFOUND; | |
233 | else | |
234 | { | |
235 | nis_result *result; | |
236 | char buf[strlen (name) + 24]; | |
237 | ||
238 | sprintf (buf, "[name=%s],passwd.org_dir", name); | |
239 | ||
240 | result = nis_list (buf, EXPAND_NAME, NULL, NULL); | |
241 | ||
242 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
243 | { | |
244 | enum nss_status status = niserr2nss (result->status); | |
245 | ||
246 | nis_freeresult (result); | |
247 | return status; | |
248 | } | |
249 | ||
250 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); | |
251 | ||
252 | nis_freeresult (result); | |
253 | ||
254 | if (parse_res) | |
255 | return NSS_STATUS_SUCCESS; | |
256 | ||
257 | if (!parse_res && errno == ERANGE) | |
258 | return NSS_STATUS_TRYAGAIN; | |
259 | else | |
260 | return NSS_STATUS_NOTFOUND; | |
261 | } | |
262 | } |