1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016 by Delphix. All rights reserved.
* Copyright 2017 Joyent Inc
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* 4.3 BSD under license from the Regents of the University of
* California.
*/
/*
* svcauth_des.c, server-side des authentication
*
* We insure for the service the following:
* (1) The timestamp microseconds do not exceed 1 million.
* (2) The timestamp plus the window is less than the current time.
* (3) The timestamp is not less than the one previously
* seen in the current session.
*
* It is up to the server to determine if the window size is
* too small.
*
*/
#include "mt.h"
#include "rpc_mt.h"
#include <assert.h>
#include <rpc/des_crypt.h>
#include <rpc/rpc.h>
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/debug.h>
#include <syslog.h>
extern int key_decryptsession_pk(const char *, netobj *, des_block *);
#define USEC_PER_SEC ((ulong_t)1000000L)
#define BEFORE(t1, t2) timercmp(t1, t2, < /* EMPTY */)
/*
* LRU cache of conversation keys and some other useful items.
*/
#define DEF_AUTHDES_CACHESZ 128
int authdes_cachesz = DEF_AUTHDES_CACHESZ;
struct cache_entry {
des_block key; /* conversation key */
char *rname; /* client's name */
uint_t window; /* credential lifetime window */
struct timeval laststamp; /* detect replays of creds */
char *localcred; /* generic local credential */
int index; /* where are we in array? */
struct cache_entry *prev; /* prev entry on LRU list */
struct cache_entry *next; /* next entry on LRU list */
};
static const char __getucredstr[] = "authdes_getucred:";
static struct cache_entry *_rpc_authdes_cache; /* [authdes_cachesz] */
static struct cache_entry *cache_head; /* cache (in LRU order) */
static struct cache_entry *cache_tail; /* cache (in LRU order) */
/*
* A rwlock_t would seem to make more sense, but it turns out we always
* muck with the cache entries, so would always need a write lock (in
* which case, we might as well use a mutex).
*/
extern mutex_t authdes_lock;
static int cache_init(void); /* initialize the cache */
/* find an entry in the cache */
static int cache_spot(des_block *, char *, struct timeval *);
static void cache_ref(uint32_t); /* note that sid was ref'd */
static void invalidate(char *); /* invalidate entry in cache */
static void __msgout(int, const char *, const char *);
static void __msgout2(const char *, const char *);
/*
* cache statistics
*/
struct {
ulong_t ncachehits; /* times cache hit, and is not replay */
ulong_t ncachereplays; /* times cache hit, and is replay */
ulong_t ncachemisses; /* times cache missed */
} svcauthdes_stats;
/*
* NOTE: this has to fit inside RQCRED_SIZE bytes. If you update this struct,
* double-check it still fits.
*/
struct authdes_area {
struct authdes_cred area_cred;
char area_netname[MAXNETNAMELEN+1];
};
CTASSERT(sizeof (struct authdes_area) <= RQCRED_SIZE);
/*
* Service side authenticator for AUTH_DES
*/
enum auth_stat
__svcauth_des(struct svc_req *rqst, struct rpc_msg *msg)
{
int32_t *ixdr;
des_block cryptbuf[2];
struct authdes_cred *cred;
struct authdes_verf verf;
int status;
struct cache_entry *entry;
uint32_t sid;
int cache_spot_id;
des_block *sessionkey, init_sessionkey;
des_block ivec;
uint_t window;
struct authdes_area *area;
struct timeval timestamp;
uint32_t namelen;
int fullname_rcvd = 0;
int from_cache = 0;
(void) mutex_lock(&authdes_lock);
if (_rpc_authdes_cache == NULL) {
int ret = cache_init();
if (ret == -1) {
(void) mutex_unlock(&authdes_lock);
return (AUTH_FAILED);
}
}
(void) mutex_unlock(&authdes_lock);
/* LINTED pointer cast */
area = (struct authdes_area *)rqst->rq_clntcred;
cred = (struct authdes_cred *)&area->area_cred;
if ((uint_t)msg->rm_call.cb_cred.oa_length == 0)
return (AUTH_BADCRED);
/*
* Get the credential
*/
/* LINTED pointer cast */
ixdr = (int32_t *)msg->rm_call.cb_cred.oa_base;
cred->adc_namekind = IXDR_GET_ENUM(ixdr, enum authdes_namekind);
switch (cred->adc_namekind) {
case ADN_FULLNAME:
namelen = IXDR_GET_U_INT32(ixdr);
if (namelen > MAXNETNAMELEN)
return (AUTH_BADCRED);
cred->adc_fullname.name = area->area_netname;
(void) memcpy(cred->adc_fullname.name, ixdr, (uint_t)namelen);
cred->adc_fullname.name[namelen] = 0;
ixdr += (RNDUP(namelen) / BYTES_PER_XDR_UNIT);
cred->adc_fullname.key.key.high = (uint32_t)*ixdr++;
cred->adc_fullname.key.key.low = (uint32_t)*ixdr++;
cred->adc_fullname.window = (uint32_t)*ixdr++;
fullname_rcvd++;
break;
case ADN_NICKNAME:
cred->adc_nickname = (uint32_t)*ixdr++;
break;
default:
return (AUTH_BADCRED);
}
if ((uint_t)msg->rm_call.cb_verf.oa_length == 0)
return (AUTH_BADVERF);
/*
* Get the verifier
*/
/* LINTED pointer cast */
ixdr = (int32_t *)msg->rm_call.cb_verf.oa_base;
verf.adv_xtimestamp.key.high = (uint32_t)*ixdr++;
verf.adv_xtimestamp.key.low = (uint32_t)*ixdr++;
verf.adv_int_u = (uint32_t)*ixdr++;
(void) mutex_lock(&authdes_lock);
/*
* Get the conversation key
*/
if (fullname_rcvd) { /* ADN_FULLNAME */
netobj pkey;
char pkey_data[1024];
again:
init_sessionkey = cred->adc_fullname.key;
sessionkey = &init_sessionkey;
if (!__getpublickey_cached(cred->adc_fullname.name,
pkey_data, &from_cache)) {
/*
* if the user has no public key, treat them as the
* unauthenticated identity - nobody. If this
* works, it means the client didn't find the
* user's keys and used nobody's secret key
* as a backup.
*/
if (!__getpublickey_cached("nobody",
pkey_data, &from_cache)) {
__msgout(LOG_INFO,
"_svcauth_des: no public key for nobody or ",
cred->adc_fullname.name);
(void) mutex_unlock(&authdes_lock);
return (AUTH_BADCRED); /* no key */
}
/*
* found a public key for nobody. change
* the fullname id to nobody, so the caller
* thinks the client specified nobody
* as the user identity.
*/
(void) strcpy(cred->adc_fullname.name, "nobody");
}
pkey.n_bytes = pkey_data;
pkey.n_len = strlen(pkey_data) + 1;
if (key_decryptsession_pk(cred->adc_fullname.name, &pkey,
sessionkey) < 0) {
if (from_cache) {
__getpublickey_flush(cred->adc_fullname.name);
goto again;
}
__msgout(LOG_INFO,
"_svcauth_des: key_decryptsessionkey failed for",
cred->adc_fullname.name);
(void) mutex_unlock(&authdes_lock);
return (AUTH_BADCRED); /* key not found */
}
} else { /* ADN_NICKNAME */
sid = cred->adc_nickname;
if (sid >= authdes_cachesz) {
__msgout(LOG_INFO, "_svcauth_des:", "bad nickname");
(void) mutex_unlock(&authdes_lock);
return (AUTH_BADCRED); /* garbled credential */
}
/* actually check that the entry is not null */
entry = &_rpc_authdes_cache[sid];
if (entry->rname == NULL) {
(void) mutex_unlock(&authdes_lock);
return (AUTH_BADCRED); /* cached out */
}
sessionkey = &_rpc_authdes_cache[sid].key;
}
/*
* Decrypt the timestamp
*/
cryptbuf[0] = verf.adv_xtimestamp;
if (fullname_rcvd) { /* ADN_FULLNAME */
cryptbuf[1].key.high = cred->adc_fullname.window;
cryptbuf[1].key.low = verf.adv_winverf;
ivec.key.high = ivec.key.low = 0;
status = cbc_crypt((char *)sessionkey, (char *)cryptbuf,
2 * (int)sizeof (des_block), DES_DECRYPT | DES_HW,
(char *)&ivec);
} else {
status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
(int)sizeof (des_block), DES_DECRYPT | DES_HW);
}
if (DES_FAILED(status)) {
if (fullname_rcvd && from_cache) {
__getpublickey_flush(cred->adc_fullname.name);
goto again;
}
__msgout(LOG_ERR, "_svcauth_des: DES decryption failure for",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
(void) mutex_unlock(&authdes_lock);
return (AUTH_FAILED); /* system error */
}
/*
* XDR the decrypted timestamp
*/
ixdr = (int32_t *)cryptbuf;
timestamp.tv_sec = IXDR_GET_INT32(ixdr);
timestamp.tv_usec = IXDR_GET_INT32(ixdr);
/*
* Check for valid credentials and verifiers.
* They could be invalid because the key was flushed
* out of the cache, and so a new session should begin.
* Be sure and send AUTH_REJECTED{CRED, VERF} if this is the case.
*/
{
struct timeval current;
int nick;
int winverf;
if (fullname_rcvd) {
window = IXDR_GET_U_INT32(ixdr);
winverf = IXDR_GET_U_INT32(ixdr);
if (winverf != window - 1) {
if (from_cache) {
__getpublickey_flush(
cred->adc_fullname.name);
goto again;
}
__msgout(LOG_INFO,
"_svcauth_des: corrupted window from",
cred->adc_fullname.name);
(void) mutex_unlock(&authdes_lock);
/* garbled credential or invalid secret key */
return (AUTH_BADCRED);
}
cache_spot_id = cache_spot(sessionkey,
cred->adc_fullname.name,
×tamp);
if (cache_spot_id < 0) {
__msgout(LOG_INFO,
"_svcauth_des: replayed credential from",
cred->adc_fullname.name);
(void) mutex_unlock(&authdes_lock);
return (AUTH_REJECTEDCRED); /* replay */
} else sid = cache_spot_id;
nick = 0;
} else { /* ADN_NICKNAME */
window = _rpc_authdes_cache[sid].window;
nick = 1;
}
if ((ulong_t)timestamp.tv_usec >= USEC_PER_SEC) {
if (fullname_rcvd && from_cache) {
__getpublickey_flush(cred->adc_fullname.name);
goto again;
}
__msgout(LOG_INFO,
"_svcauth_des: invalid timestamp received from",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
/* cached out (bad key), or garbled verifier */
(void) mutex_unlock(&authdes_lock);
return (nick ? AUTH_REJECTEDVERF : AUTH_BADVERF);
}
if (nick && BEFORE(×tamp,
&_rpc_authdes_cache[sid].laststamp)) {
if (fullname_rcvd && from_cache) {
__getpublickey_flush(cred->adc_fullname.name);
goto again;
}
__msgout(LOG_INFO,
"_svcauth_des: timestamp is earlier than the one previously seen from",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
(void) mutex_unlock(&authdes_lock);
return (AUTH_REJECTEDVERF); /* replay */
}
(void) gettimeofday(¤t, NULL);
current.tv_sec -= window; /* allow for expiration */
if (!BEFORE(¤t, ×tamp)) {
if (fullname_rcvd && from_cache) {
__getpublickey_flush(cred->adc_fullname.name);
goto again;
}
__msgout(LOG_INFO,
"_svcauth_des: timestamp expired for",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
/* replay, or garbled credential */
(void) mutex_unlock(&authdes_lock);
return (nick ? AUTH_REJECTEDVERF : AUTH_BADCRED);
}
}
/*
* Set up the reply verifier
*/
verf.adv_nickname = sid;
/*
* xdr the timestamp before encrypting
*/
ixdr = (int32_t *)cryptbuf;
IXDR_PUT_INT32(ixdr, timestamp.tv_sec - 1);
IXDR_PUT_INT32(ixdr, timestamp.tv_usec);
/*
* encrypt the timestamp
*/
status = ecb_crypt((char *)sessionkey, (char *)cryptbuf,
(int)sizeof (des_block), DES_ENCRYPT | DES_HW);
if (DES_FAILED(status)) {
__msgout(LOG_ERR, "_svcauth_des: DES encryption failure for",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
(void) mutex_unlock(&authdes_lock);
return (AUTH_FAILED); /* system error */
}
verf.adv_xtimestamp = cryptbuf[0];
/*
* Serialize the reply verifier, and update rqst
*/
/* LINTED pointer cast */
ixdr = (int32_t *)msg->rm_call.cb_verf.oa_base;
*ixdr++ = (int32_t)verf.adv_xtimestamp.key.high;
*ixdr++ = (int32_t)verf.adv_xtimestamp.key.low;
*ixdr++ = (int32_t)verf.adv_int_u;
rqst->rq_xprt->xp_verf.oa_flavor = AUTH_DES;
rqst->rq_xprt->xp_verf.oa_base = msg->rm_call.cb_verf.oa_base;
rqst->rq_xprt->xp_verf.oa_length =
(char *)ixdr - msg->rm_call.cb_verf.oa_base;
if (rqst->rq_xprt->xp_verf.oa_length > MAX_AUTH_BYTES) {
__msgout(LOG_ERR,
"_svcauth_des: Authenticator length error",
fullname_rcvd ? cred->adc_fullname.name :
_rpc_authdes_cache[sid].rname);
(void) mutex_unlock(&authdes_lock);
return (AUTH_REJECTEDVERF);
}
/*
* We succeeded, commit the data to the cache now and
* finish cooking the credential.
*/
entry = &_rpc_authdes_cache[sid];
entry->laststamp = timestamp;
cache_ref(sid);
if (cred->adc_namekind == ADN_FULLNAME) {
cred->adc_fullname.window = window;
cred->adc_nickname = sid; /* save nickname */
if (entry->rname != NULL)
free(entry->rname);
entry->rname = malloc(strlen(cred->adc_fullname.name) + 1);
if (entry->rname != NULL) {
(void) strcpy(entry->rname, cred->adc_fullname.name);
} else {
__msgout(LOG_CRIT, "_svcauth_des:", "out of memory");
(void) mutex_unlock(&authdes_lock);
return (AUTH_FAILED);
}
entry->key = *sessionkey;
entry->window = window;
/* mark any cached cred invalid */
invalidate(entry->localcred);
} else { /* ADN_NICKNAME */
/*
* nicknames are cooked into fullnames
*/
cred->adc_namekind = ADN_FULLNAME;
cred->adc_fullname.name = entry->rname;
cred->adc_fullname.key = entry->key;
cred->adc_fullname.window = entry->window;
}
(void) mutex_unlock(&authdes_lock);
return (AUTH_OK); /* we made it! */
}
/*
* Initialize the cache
*/
static int
cache_init(void)
{
int i;
/* LOCK HELD ON ENTRY: authdes_lock */
assert(MUTEX_HELD(&authdes_lock));
_rpc_authdes_cache =
malloc(sizeof (struct cache_entry) * authdes_cachesz);
if (_rpc_authdes_cache == NULL) {
__msgout(LOG_CRIT, "cache_init:", "out of memory");
return (-1);
}
(void) memset(_rpc_authdes_cache, 0,
sizeof (struct cache_entry) * authdes_cachesz);
/*
* Initialize the lru chain (linked-list)
*/
for (i = 1; i < (authdes_cachesz - 1); i++) {
_rpc_authdes_cache[i].index = i;
_rpc_authdes_cache[i].next = &_rpc_authdes_cache[i + 1];
_rpc_authdes_cache[i].prev = &_rpc_authdes_cache[i - 1];
}
cache_head = &_rpc_authdes_cache[0];
cache_tail = &_rpc_authdes_cache[authdes_cachesz - 1];
/*
* These elements of the chain need special attention...
*/
cache_head->index = 0;
cache_tail->index = authdes_cachesz - 1;
cache_head->next = &_rpc_authdes_cache[1];
cache_head->prev = cache_tail;
cache_tail->next = cache_head;
cache_tail->prev = &_rpc_authdes_cache[authdes_cachesz - 2];
return (0);
}
/*
* Find the lru victim
*/
static uint32_t
cache_victim(void)
{
/* LOCK HELD ON ENTRY: authdes_lock */
assert(MUTEX_HELD(&authdes_lock));
return (cache_head->index); /* list in lru order */
}
/*
* Note that sid was referenced
*/
static void
cache_ref(uint32_t sid)
{
struct cache_entry *curr = &_rpc_authdes_cache[sid];
/* LOCK HELD ON ENTRY: authdes_lock */
assert(MUTEX_HELD(&authdes_lock));
/*
* move referenced item from its place on the LRU chain
* to the tail of the chain while checking for special
* conditions (mainly for performance).
*/
if (cache_tail == curr) { /* no work to do */
/*EMPTY*/;
} else if (cache_head == curr) {
cache_head = cache_head->next;
cache_tail = curr;
} else {
(curr->next)->prev = curr->prev; /* fix thy neighbor */
(curr->prev)->next = curr->next;
curr->next = cache_head; /* fix thy self... */
curr->prev = cache_tail;
cache_head->prev = curr; /* fix the head */
cache_tail->next = curr; /* fix the tail */
cache_tail = curr; /* move the tail */
}
}
/*
* Find a spot in the cache for a credential containing
* the items given. Return -1 if a replay is detected, otherwise
* return the spot in the cache.
*/
static int
cache_spot(des_block *key, char *name, struct timeval *timestamp)
{
struct cache_entry *cp;
int i;
uint32_t hi;
/* LOCK HELD ON ENTRY: authdes_lock */
assert(MUTEX_HELD(&authdes_lock));
hi = key->key.high;
for (cp = _rpc_authdes_cache, i = 0; i < authdes_cachesz; i++, cp++) {
if (cp->key.key.high == hi &&
cp->key.key.low == key->key.low &&
cp->rname != NULL &&
memcmp(cp->rname, name, strlen(name) + 1) == 0) {
if (BEFORE(timestamp, &cp->laststamp)) {
svcauthdes_stats.ncachereplays++;
return (-1); /* replay */
}
svcauthdes_stats.ncachehits++;
return (i);
/* refresh */
}
}
svcauthdes_stats.ncachemisses++;
return (cache_victim());
}
/*
* Local credential handling stuff.
* NOTE: bsd unix dependent.
* Other operating systems should put something else here.
*/
#define UNKNOWN -2 /* grouplen, if cached cred is unknown user */
#define INVALID -1 /* grouplen, if cache entry is invalid */
struct bsdcred {
uid_t uid; /* cached uid */
gid_t gid; /* cached gid */
short grouplen; /* length of cached groups */
gid_t groups[1]; /* cached groups allocate _SC_NGROUPS_MAX */
};
static void
invalidate(char *cred)
{
if (cred == NULL)
return;
/* LINTED pointer cast */
((struct bsdcred *)cred)->grouplen = INVALID;
}
/*
* Map a des credential into a unix cred.
* We cache the credential here so the application does
* not have to make an rpc call every time to interpret
* the credential.
*/
int
authdes_getucred(const struct authdes_cred *adc, uid_t *uid, gid_t *gid,
short *grouplen, gid_t *groups)
{
uint32_t sid;
int i;
uid_t i_uid;
gid_t i_gid;
int i_grouplen;
struct bsdcred *cred;
sid = adc->adc_nickname;
if (sid >= authdes_cachesz) {
__msgout2(__getucredstr, "invalid nickname");
return (0);
}
(void) mutex_lock(&authdes_lock);
/* LINTED pointer cast */
cred = (struct bsdcred *)_rpc_authdes_cache[sid].localcred;
if (cred == NULL) {
static size_t bsdcred_sz;
if (bsdcred_sz == 0) {
bsdcred_sz = sizeof (struct bsdcred) +
(sysconf(_SC_NGROUPS_MAX) - 1) * sizeof (gid_t);
}
cred = malloc(bsdcred_sz);
if (cred == NULL) {
__msgout2(__getucredstr, "out of memory");
(void) mutex_unlock(&authdes_lock);
return (0);
}
_rpc_authdes_cache[sid].localcred = (char *)cred;
cred->grouplen = INVALID;
}
if (cred->grouplen == INVALID) {
/*
* not in cache: lookup
*/
if (!netname2user(adc->adc_fullname.name, (uid_t *)&i_uid,
(gid_t *)&i_gid, &i_grouplen, (gid_t *)groups)) {
__msgout2(__getucredstr, "unknown netname");
/* mark as lookup up, but not found */
cred->grouplen = UNKNOWN;
(void) mutex_unlock(&authdes_lock);
return (0);
}
__msgout2(__getucredstr, "missed ucred cache");
*uid = cred->uid = i_uid;
*gid = cred->gid = i_gid;
*grouplen = cred->grouplen = i_grouplen;
for (i = i_grouplen - 1; i >= 0; i--) {
cred->groups[i] = groups[i];
}
(void) mutex_unlock(&authdes_lock);
return (1);
}
if (cred->grouplen == UNKNOWN) {
/*
* Already lookup up, but no match found
*/
(void) mutex_unlock(&authdes_lock);
return (0);
}
/*
* cached credentials
*/
*uid = cred->uid;
*gid = cred->gid;
*grouplen = cred->grouplen;
for (i = cred->grouplen - 1; i >= 0; i--) {
groups[i] = cred->groups[i];
}
(void) mutex_unlock(&authdes_lock);
return (1);
}
static void
__msgout(int level, const char *str, const char *strarg)
{
(void) syslog(level, "%s %s", str, strarg);
}
static void
__msgout2(const char *str, const char *str2)
{
(void) syslog(LOG_DEBUG, "%s %s", str, str2);
}
|