]>
Commit | Line | Data |
---|---|---|
800d775e | 1 | /* |
a7ab6ec8 | 2 | * Copyright (c) 2010, Oracle America, Inc. |
cb636bb2 | 3 | * |
a7ab6ec8 UD |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are | |
6 | * met: | |
800d775e | 7 | * |
a7ab6ec8 UD |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above | |
11 | * copyright notice, this list of conditions and the following | |
12 | * disclaimer in the documentation and/or other materials | |
13 | * provided with the distribution. | |
14 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
15 | * contributors may be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
800d775e | 17 | * |
a7ab6ec8 UD |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
800d775e | 30 | * |
800d775e UD |
31 | * svcauth_des.c, server-side des authentication |
32 | * | |
33 | * We insure for the service the following: | |
34 | * (1) The timestamp microseconds do not exceed 1 million. | |
35 | * (2) The timestamp plus the window is less than the current time. | |
36 | * (3) The timestamp is not less than the one previously | |
37 | * seen in the current session. | |
38 | * | |
39 | * It is up to the server to determine if the window size is | |
40 | * too small . | |
41 | * | |
42 | */ | |
43 | ||
773640b4 | 44 | #include <limits.h> |
800d775e | 45 | #include <string.h> |
e054f494 | 46 | #include <stdint.h> |
800d775e UD |
47 | #include <sys/param.h> |
48 | #include <netinet/in.h> | |
f1e4a4a4 | 49 | #include <rpc/rpc.h> |
800d775e UD |
50 | #include <rpc/xdr.h> |
51 | #include <rpc/auth.h> | |
52 | #include <rpc/auth_des.h> | |
53 | #include <rpc/svc_auth.h> | |
54 | #include <rpc/svc.h> | |
55 | #include <rpc/des_crypt.h> | |
56 | ||
57 | #define debug(msg) /*printf("svcauth_des: %s\n", msg) */ | |
58 | ||
f8afba91 | 59 | #define USEC_PER_SEC ((uint32_t) 1000000L) |
800d775e UD |
60 | #define BEFORE(t1, t2) timercmp(t1, t2, <) |
61 | ||
62 | /* | |
63 | * LRU cache of conversation keys and some other useful items. | |
64 | */ | |
65 | #define AUTHDES_CACHESZ 64 | |
66 | struct cache_entry | |
67 | { | |
68 | des_block key; /* conversation key */ | |
69 | char *rname; /* client's name */ | |
70 | u_int window; /* credential lifetime window */ | |
f8afba91 | 71 | struct rpc_timeval laststamp; /* detect replays of creds */ |
800d775e UD |
72 | char *localcred; /* generic local credential */ |
73 | }; | |
f1e4a4a4 | 74 | #ifdef _RPC_THREAD_SAFE_ |
1bc1a2b9 AS |
75 | #define authdes_cache RPC_THREAD_VARIABLE(authdes_cache_s) |
76 | #define authdes_lru RPC_THREAD_VARIABLE(authdes_lru_s) | |
f1e4a4a4 UD |
77 | #else |
78 | static struct cache_entry *authdes_cache; | |
79 | static int *authdes_lru; | |
80 | #endif | |
800d775e | 81 | |
dfd2257a | 82 | static void cache_init (void) internal_function; /* initialize the cache */ |
f8afba91 | 83 | static short cache_spot (des_block *, char *, struct rpc_timeval *) |
dfd2257a | 84 | internal_function; /* find an entry in the cache */ |
f8afba91 | 85 | static void cache_ref (uint32_t sid) internal_function; |
dfd2257a | 86 | /* note that sid was ref'd */ |
800d775e | 87 | |
dfd2257a UD |
88 | static void invalidate (char *cred) internal_function; |
89 | /* invalidate entry in cache */ | |
800d775e UD |
90 | |
91 | /* | |
92 | * cache statistics | |
93 | */ | |
94 | struct | |
95 | { | |
96 | u_long ncachehits; /* times cache hit, and is not replay */ | |
97 | u_long ncachereplays; /* times cache hit, and is replay */ | |
98 | u_long ncachemisses; /* times cache missed */ | |
99 | } | |
100 | svcauthdes_stats; | |
101 | ||
102 | /* | |
103 | * Service side authenticator for AUTH_DES | |
104 | */ | |
105 | enum auth_stat | |
106 | _svcauth_des (register struct svc_req *rqst, register struct rpc_msg *msg) | |
107 | { | |
f8afba91 | 108 | register uint32_t *ixdr; |
800d775e UD |
109 | des_block cryptbuf[2]; |
110 | register struct authdes_cred *cred; | |
111 | struct authdes_verf verf; | |
112 | int status; | |
113 | register struct cache_entry *entry; | |
f8afba91 | 114 | uint32_t sid = 0; |
800d775e UD |
115 | des_block *sessionkey; |
116 | des_block ivec; | |
117 | u_int window; | |
f8afba91 UD |
118 | struct rpc_timeval timestamp; |
119 | uint32_t namelen; | |
800d775e UD |
120 | struct area |
121 | { | |
122 | struct authdes_cred area_cred; | |
123 | char area_netname[MAXNETNAMELEN + 1]; | |
124 | } | |
125 | *area; | |
126 | ||
127 | if (authdes_cache == NULL) | |
128 | cache_init (); | |
f8afba91 UD |
129 | if (authdes_cache == NULL) /* No free memory */ |
130 | return AUTH_FAILED; | |
800d775e UD |
131 | |
132 | area = (struct area *) rqst->rq_clntcred; | |
133 | cred = (struct authdes_cred *) &area->area_cred; | |
134 | ||
135 | /* | |
136 | * Get the credential | |
137 | */ | |
f8afba91 UD |
138 | if (msg->rm_call.cb_cred.oa_length <= 0 || |
139 | msg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) | |
140 | return AUTH_BADCRED; | |
141 | ||
142 | ixdr = (uint32_t *) msg->rm_call.cb_cred.oa_base; | |
800d775e UD |
143 | cred->adc_namekind = IXDR_GET_ENUM (ixdr, enum authdes_namekind); |
144 | switch (cred->adc_namekind) | |
145 | { | |
146 | case ADN_FULLNAME: | |
f8afba91 | 147 | namelen = IXDR_GET_U_INT32 (ixdr); |
800d775e UD |
148 | if (namelen > MAXNETNAMELEN) |
149 | { | |
150 | return AUTH_BADCRED; | |
151 | } | |
152 | cred->adc_fullname.name = area->area_netname; | |
9af652f6 | 153 | memcpy (cred->adc_fullname.name, (char *) ixdr, namelen); |
800d775e UD |
154 | cred->adc_fullname.name[namelen] = 0; |
155 | ixdr += (RNDUP (namelen) / BYTES_PER_XDR_UNIT); | |
f8afba91 UD |
156 | cred->adc_fullname.key.key.high = *ixdr++; |
157 | cred->adc_fullname.key.key.low = *ixdr++; | |
158 | cred->adc_fullname.window = *ixdr++; | |
800d775e UD |
159 | break; |
160 | case ADN_NICKNAME: | |
f8afba91 | 161 | cred->adc_nickname = *ixdr++; |
800d775e UD |
162 | break; |
163 | default: | |
164 | return AUTH_BADCRED; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Get the verifier | |
169 | */ | |
f8afba91 UD |
170 | if (msg->rm_call.cb_verf.oa_length <= 0 || |
171 | msg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) | |
172 | return AUTH_BADCRED; | |
800d775e | 173 | |
f8afba91 UD |
174 | ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base; |
175 | verf.adv_xtimestamp.key.high = *ixdr++; | |
176 | verf.adv_xtimestamp.key.low = *ixdr++; | |
177 | verf.adv_int_u = *ixdr++; | |
800d775e UD |
178 | |
179 | /* | |
180 | * Get the conversation key | |
181 | */ | |
182 | if (cred->adc_namekind == ADN_FULLNAME) | |
183 | { | |
26a60f90 UD |
184 | netobj pkey; |
185 | char pkey_data[1024]; | |
186 | ||
800d775e | 187 | sessionkey = &cred->adc_fullname.key; |
26a60f90 UD |
188 | if (!getpublickey (cred->adc_fullname.name, pkey_data)) |
189 | { | |
190 | debug("getpublickey"); | |
191 | return AUTH_BADCRED; | |
192 | } | |
193 | pkey.n_bytes = pkey_data; | |
194 | pkey.n_len = strlen (pkey_data) + 1; | |
195 | if (key_decryptsession_pk (cred->adc_fullname.name, &pkey, | |
196 | sessionkey) < 0) | |
800d775e UD |
197 | { |
198 | debug ("decryptsessionkey"); | |
199 | return AUTH_BADCRED; /* key not found */ | |
200 | } | |
201 | } | |
202 | else | |
203 | { /* ADN_NICKNAME */ | |
f8afba91 | 204 | if (cred->adc_nickname >= AUTHDES_CACHESZ) |
800d775e UD |
205 | { |
206 | debug ("bad nickname"); | |
207 | return AUTH_BADCRED; /* garbled credential */ | |
208 | } | |
f8afba91 UD |
209 | else |
210 | sid = cred->adc_nickname; | |
211 | ||
212 | /* XXX This could be wrong, but else we have a | |
213 | security problem */ | |
214 | if (authdes_cache[sid].rname == NULL) | |
215 | return AUTH_BADCRED; | |
800d775e UD |
216 | sessionkey = &authdes_cache[sid].key; |
217 | } | |
218 | ||
219 | ||
220 | /* | |
221 | * Decrypt the timestamp | |
222 | */ | |
223 | cryptbuf[0] = verf.adv_xtimestamp; | |
224 | if (cred->adc_namekind == ADN_FULLNAME) | |
225 | { | |
226 | cryptbuf[1].key.high = cred->adc_fullname.window; | |
227 | cryptbuf[1].key.low = verf.adv_winverf; | |
228 | ivec.key.high = ivec.key.low = 0; | |
229 | status = cbc_crypt ((char *) sessionkey, (char *) cryptbuf, | |
230 | 2 * sizeof (des_block), DES_DECRYPT | DES_HW, | |
231 | (char *) &ivec); | |
232 | } | |
233 | else | |
f8afba91 UD |
234 | status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf, |
235 | sizeof (des_block), DES_DECRYPT | DES_HW); | |
236 | ||
800d775e UD |
237 | if (DES_FAILED (status)) |
238 | { | |
239 | debug ("decryption failure"); | |
240 | return AUTH_FAILED; /* system error */ | |
241 | } | |
242 | ||
243 | /* | |
244 | * XDR the decrypted timestamp | |
245 | */ | |
f8afba91 UD |
246 | ixdr = (uint32_t *) cryptbuf; |
247 | timestamp.tv_sec = IXDR_GET_INT32 (ixdr); | |
248 | timestamp.tv_usec = IXDR_GET_INT32 (ixdr); | |
800d775e UD |
249 | |
250 | /* | |
251 | * Check for valid credentials and verifiers. | |
252 | * They could be invalid because the key was flushed | |
253 | * out of the cache, and so a new session should begin. | |
254 | * Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case. | |
255 | */ | |
256 | { | |
257 | struct timeval current; | |
258 | int nick; | |
259 | u_int winverf; | |
260 | ||
261 | if (cred->adc_namekind == ADN_FULLNAME) | |
262 | { | |
f8afba91 UD |
263 | short tmp_spot; |
264 | ||
265 | window = IXDR_GET_U_INT32 (ixdr); | |
266 | winverf = IXDR_GET_U_INT32 (ixdr); | |
800d775e UD |
267 | if (winverf != window - 1) |
268 | { | |
269 | debug ("window verifier mismatch"); | |
270 | return AUTH_BADCRED; /* garbled credential */ | |
271 | } | |
f8afba91 UD |
272 | tmp_spot = cache_spot (sessionkey, cred->adc_fullname.name, |
273 | ×tamp); | |
274 | if (tmp_spot < 0 || tmp_spot > AUTHDES_CACHESZ) | |
800d775e UD |
275 | { |
276 | debug ("replayed credential"); | |
277 | return AUTH_REJECTEDCRED; /* replay */ | |
278 | } | |
f8afba91 | 279 | sid = tmp_spot; |
800d775e UD |
280 | nick = 0; |
281 | } | |
282 | else | |
283 | { /* ADN_NICKNAME */ | |
284 | window = authdes_cache[sid].window; | |
285 | nick = 1; | |
286 | } | |
287 | ||
f8afba91 | 288 | if (timestamp.tv_usec >= USEC_PER_SEC) |
800d775e UD |
289 | { |
290 | debug ("invalid usecs"); | |
291 | /* cached out (bad key), or garbled verifier */ | |
292 | return nick ? AUTH_REJECTEDVERF : AUTH_BADVERF; | |
293 | } | |
f8afba91 | 294 | if (nick && BEFORE (×tamp, &authdes_cache[sid].laststamp)) |
800d775e UD |
295 | { |
296 | debug ("timestamp before last seen"); | |
f8afba91 | 297 | return AUTH_REJECTEDVERF; /* replay */ |
800d775e | 298 | } |
50304ef0 | 299 | __gettimeofday (¤t, (struct timezone *) NULL); |
800d775e UD |
300 | current.tv_sec -= window; /* allow for expiration */ |
301 | if (!BEFORE (¤t, ×tamp)) | |
302 | { | |
303 | debug ("timestamp expired"); | |
304 | /* replay, or garbled credential */ | |
305 | return nick ? AUTH_REJECTEDVERF : AUTH_BADCRED; | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | * Set up the reply verifier | |
311 | */ | |
f8afba91 | 312 | verf.adv_nickname = sid; |
800d775e UD |
313 | |
314 | /* | |
315 | * xdr the timestamp before encrypting | |
316 | */ | |
9cfe5381 | 317 | ixdr = (uint32_t *) cryptbuf; |
f8afba91 UD |
318 | IXDR_PUT_INT32 (ixdr, timestamp.tv_sec - 1); |
319 | IXDR_PUT_INT32 (ixdr, timestamp.tv_usec); | |
800d775e UD |
320 | |
321 | /* | |
322 | * encrypt the timestamp | |
323 | */ | |
324 | status = ecb_crypt ((char *) sessionkey, (char *) cryptbuf, | |
325 | sizeof (des_block), DES_ENCRYPT | DES_HW); | |
326 | if (DES_FAILED (status)) | |
327 | { | |
328 | debug ("encryption failure"); | |
329 | return AUTH_FAILED; /* system error */ | |
330 | } | |
331 | verf.adv_xtimestamp = cryptbuf[0]; | |
332 | ||
333 | /* | |
334 | * Serialize the reply verifier, and update rqst | |
335 | */ | |
f8afba91 UD |
336 | ixdr = (uint32_t *) msg->rm_call.cb_verf.oa_base; |
337 | *ixdr++ = verf.adv_xtimestamp.key.high; | |
338 | *ixdr++ = verf.adv_xtimestamp.key.low; | |
339 | *ixdr++ = verf.adv_int_u; | |
800d775e UD |
340 | |
341 | rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES; | |
342 | rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base; | |
343 | rqst->rq_xprt->xp_verf.oa_length = | |
344 | (char *) ixdr - msg->rm_call.cb_verf.oa_base; | |
345 | ||
346 | /* | |
347 | * We succeeded, commit the data to the cache now and | |
348 | * finish cooking the credential. | |
349 | */ | |
350 | entry = &authdes_cache[sid]; | |
351 | entry->laststamp = timestamp; | |
352 | cache_ref (sid); | |
353 | if (cred->adc_namekind == ADN_FULLNAME) | |
354 | { | |
86187531 UD |
355 | size_t full_len; |
356 | ||
800d775e | 357 | cred->adc_fullname.window = window; |
f8afba91 | 358 | cred->adc_nickname = sid; /* save nickname */ |
800d775e | 359 | if (entry->rname != NULL) |
f8afba91 | 360 | mem_free (entry->rname, strlen (entry->rname) + 1); |
86187531 UD |
361 | full_len = strlen (cred->adc_fullname.name) + 1; |
362 | entry->rname = mem_alloc ((u_int) full_len); | |
800d775e | 363 | if (entry->rname != NULL) |
f8afba91 | 364 | memcpy (entry->rname, cred->adc_fullname.name, full_len); |
800d775e UD |
365 | else |
366 | { | |
367 | debug ("out of memory"); | |
f8afba91 | 368 | return AUTH_FAILED; /* out of memory is bad */ |
800d775e UD |
369 | } |
370 | entry->key = *sessionkey; | |
371 | entry->window = window; | |
372 | invalidate (entry->localcred); /* mark any cached cred invalid */ | |
373 | } | |
374 | else | |
375 | { /* ADN_NICKNAME */ | |
376 | /* | |
377 | * nicknames are cooked into fullnames | |
378 | */ | |
379 | cred->adc_namekind = ADN_FULLNAME; | |
380 | cred->adc_fullname.name = entry->rname; | |
381 | cred->adc_fullname.key = entry->key; | |
382 | cred->adc_fullname.window = entry->window; | |
383 | } | |
384 | return AUTH_OK; /* we made it! */ | |
385 | } | |
386 | ||
387 | ||
388 | /* | |
389 | * Initialize the cache | |
390 | */ | |
391 | static void | |
dfd2257a | 392 | internal_function |
800d775e UD |
393 | cache_init (void) |
394 | { | |
395 | register int i; | |
396 | ||
397 | authdes_cache = (struct cache_entry *) | |
cdb9c321 | 398 | calloc (sizeof (struct cache_entry) * AUTHDES_CACHESZ, 1); |
f8afba91 UD |
399 | if (authdes_cache == NULL) |
400 | return; | |
800d775e | 401 | |
f8afba91 | 402 | authdes_lru = (int *) mem_alloc (sizeof (int) * AUTHDES_CACHESZ); |
800d775e UD |
403 | /* |
404 | * Initialize the lru list | |
405 | */ | |
f8afba91 UD |
406 | for (i = 0; i < AUTHDES_CACHESZ; ++i) |
407 | authdes_lru[i] = i; | |
800d775e UD |
408 | } |
409 | ||
410 | ||
411 | /* | |
412 | * Find the lru victim | |
413 | */ | |
414 | static short | |
415 | cache_victim (void) | |
416 | { | |
f8afba91 | 417 | return authdes_lru[AUTHDES_CACHESZ - 1]; |
800d775e UD |
418 | } |
419 | ||
420 | /* | |
421 | * Note that sid was referenced | |
422 | */ | |
423 | static void | |
dfd2257a | 424 | internal_function |
f8afba91 | 425 | cache_ref (register uint32_t sid) |
800d775e UD |
426 | { |
427 | register int i; | |
f8afba91 UD |
428 | register int curr; |
429 | register int prev; | |
800d775e UD |
430 | |
431 | prev = authdes_lru[0]; | |
432 | authdes_lru[0] = sid; | |
f8afba91 | 433 | for (i = 1; prev != sid; ++i) |
800d775e UD |
434 | { |
435 | curr = authdes_lru[i]; | |
436 | authdes_lru[i] = prev; | |
437 | prev = curr; | |
438 | } | |
439 | } | |
440 | ||
800d775e UD |
441 | /* |
442 | * Find a spot in the cache for a credential containing | |
443 | * the items given. Return -1 if a replay is detected, otherwise | |
444 | * return the spot in the cache. | |
445 | */ | |
446 | static short | |
dfd2257a | 447 | internal_function |
f8afba91 UD |
448 | cache_spot (register des_block *key, char *name, |
449 | struct rpc_timeval *timestamp) | |
800d775e UD |
450 | { |
451 | register struct cache_entry *cp; | |
452 | register int i; | |
f8afba91 | 453 | register uint32_t hi; |
800d775e UD |
454 | |
455 | hi = key->key.high; | |
f8afba91 | 456 | for (cp = authdes_cache, i = 0; i < AUTHDES_CACHESZ; ++i, ++cp) |
800d775e UD |
457 | { |
458 | if (cp->key.key.high == hi && | |
459 | cp->key.key.low == key->key.low && | |
460 | cp->rname != NULL && | |
50304ef0 | 461 | memcmp (cp->rname, name, strlen (name) + 1) == 0) |
800d775e UD |
462 | { |
463 | if (BEFORE (timestamp, &cp->laststamp)) | |
464 | { | |
f8afba91 | 465 | ++svcauthdes_stats.ncachereplays; |
800d775e UD |
466 | return -1; /* replay */ |
467 | } | |
f8afba91 | 468 | ++svcauthdes_stats.ncachehits; |
800d775e UD |
469 | return i; /* refresh */ |
470 | } | |
471 | } | |
f8afba91 | 472 | ++svcauthdes_stats.ncachemisses; |
800d775e UD |
473 | return cache_victim (); /* new credential */ |
474 | } | |
475 | ||
800d775e UD |
476 | /* |
477 | * Local credential handling stuff. | |
478 | * NOTE: bsd unix dependent. | |
479 | * Other operating systems should put something else here. | |
480 | */ | |
481 | #define UNKNOWN -2 /* grouplen, if cached cred is unknown user */ | |
482 | #define INVALID -1 /* grouplen, if cache entry is invalid */ | |
483 | ||
484 | struct bsdcred | |
485 | { | |
486 | uid_t uid; /* cached uid */ | |
487 | gid_t gid; /* cached gid */ | |
773640b4 UD |
488 | int grouplen; /* length of cached groups */ |
489 | int grouplen_max; /* length of allocated cached groups */ | |
490 | gid_t groups[0]; /* cached groups */ | |
800d775e UD |
491 | }; |
492 | ||
493 | /* | |
494 | * Map a des credential into a unix cred. | |
495 | * We cache the credential here so the application does | |
496 | * not have to make an rpc call every time to interpret | |
497 | * the credential. | |
498 | */ | |
499 | int | |
500 | authdes_getucred (const struct authdes_cred *adc, uid_t * uid, gid_t * gid, | |
501 | short *grouplen, gid_t * groups) | |
502 | { | |
503 | unsigned sid; | |
504 | register int i; | |
505 | uid_t i_uid; | |
506 | gid_t i_gid; | |
507 | int i_grouplen; | |
508 | struct bsdcred *cred; | |
509 | ||
510 | sid = adc->adc_nickname; | |
511 | if (sid >= AUTHDES_CACHESZ) | |
512 | { | |
513 | debug ("invalid nickname"); | |
514 | return 0; | |
515 | } | |
516 | cred = (struct bsdcred *) authdes_cache[sid].localcred; | |
773640b4 | 517 | if (cred == NULL || cred->grouplen == INVALID) |
800d775e UD |
518 | { |
519 | /* | |
520 | * not in cache: lookup | |
521 | */ | |
522 | if (!netname2user (adc->adc_fullname.name, &i_uid, &i_gid, | |
523 | &i_grouplen, groups)) | |
524 | { | |
525 | debug ("unknown netname"); | |
773640b4 UD |
526 | if (cred != NULL) |
527 | cred->grouplen = UNKNOWN; /* mark as lookup up, but not found */ | |
800d775e UD |
528 | return 0; |
529 | } | |
773640b4 UD |
530 | |
531 | if (cred != NULL && cred->grouplen_max < i_grouplen) | |
532 | { | |
533 | /* We already have an allocated data structure. But it is | |
534 | too small. */ | |
535 | free (cred); | |
536 | authdes_cache[sid].localcred = NULL; | |
537 | cred = NULL; | |
538 | } | |
539 | ||
540 | if (cred == NULL) | |
541 | { | |
542 | /* We should allocate room for at least NGROUPS groups. */ | |
543 | int ngroups_max = MAX (i_grouplen, NGROUPS); | |
544 | ||
545 | cred = (struct bsdcred *) mem_alloc (sizeof (struct bsdcred) | |
546 | + ngroups_max * sizeof (gid_t)); | |
547 | if (cred == NULL) | |
548 | return 0; | |
549 | ||
550 | authdes_cache[sid].localcred = (char *) cred; | |
551 | cred->grouplen = INVALID; | |
552 | cred->grouplen_max = ngroups_max; | |
553 | } | |
554 | ||
800d775e UD |
555 | debug ("missed ucred cache"); |
556 | *uid = cred->uid = i_uid; | |
557 | *gid = cred->gid = i_gid; | |
773640b4 | 558 | cred->grouplen = i_grouplen; |
f8afba91 | 559 | for (i = i_grouplen - 1; i >= 0; --i) |
773640b4 UD |
560 | cred->groups[i] = groups[i]; |
561 | /* Make sure no too large values are reported. */ | |
562 | *grouplen = MIN (SHRT_MAX, i_grouplen); | |
800d775e UD |
563 | return 1; |
564 | } | |
565 | else if (cred->grouplen == UNKNOWN) | |
566 | { | |
567 | /* | |
568 | * Already lookup up, but no match found | |
569 | */ | |
570 | return 0; | |
571 | } | |
572 | ||
573 | /* | |
574 | * cached credentials | |
575 | */ | |
576 | *uid = cred->uid; | |
577 | *gid = cred->gid; | |
773640b4 UD |
578 | |
579 | /* Another stupidity in the interface: *grouplen is of type short. | |
580 | So we might have to cut the information passed up short. */ | |
581 | int grouplen_copy = MIN (SHRT_MAX, cred->grouplen); | |
582 | *grouplen = grouplen_copy; | |
583 | for (i = grouplen_copy - 1; i >= 0; --i) | |
584 | groups[i] = cred->groups[i]; | |
800d775e UD |
585 | return 1; |
586 | } | |
021db4be | 587 | libc_hidden_nolink_sunrpc (authdes_getucred, GLIBC_2_1) |
800d775e UD |
588 | |
589 | static void | |
dfd2257a | 590 | internal_function |
800d775e UD |
591 | invalidate (char *cred) |
592 | { | |
593 | if (cred == NULL) | |
f8afba91 | 594 | return; |
800d775e UD |
595 | ((struct bsdcred *) cred)->grouplen = INVALID; |
596 | } |