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
|
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* #define DBG_PRINT */
#include <netsite.h>
extern "C" {
#include <secitem.h>
}
#include <base/crit.h>
#include <ldaputil/errors.h>
#include <libaccess/usrcache.h>
#include <libaccess/las.h>
#include <libaccess/authdb.h>
#include "permhash.h"
/* uid is unique within a database. The user cache tables are stored per
* database. The following table maps a database name to the corresponding
* user cache table. The user cache table is another hash table which stores
* the UserCacheObj instances.
*/
static PRHashTable *databaseUserCacheTable = 0;
static time_t acl_usr_cache_lifetime = (time_t)120;
static PRCList *usrobj_list = 0;
static const int num_usrobj = 200;
static CRITICAL usr_hash_crit = NULL; /* Controls user cache hash tables & */
/* usrobj link list */
static pool_handle_t *usrcache_pool = NULL;
static PRHashTable *singleDbTable = 0;
#define USEROBJ_PTR(l) \
((UserCacheObj*) ((char*) (l) - offsetof(UserCacheObj, list)))
static void user_hash_crit_enter (void)
{
/* Caching may be disabled (usr_hash_crit will be NULL) */
if (usr_hash_crit) crit_enter(usr_hash_crit);
}
static void user_hash_crit_exit (void)
{
/* Caching may be disabled (usr_hash_crit will be NULL) */
if (usr_hash_crit) crit_exit(usr_hash_crit);
}
static void user_hash_crit_init (void)
{
usr_hash_crit = crit_init();
}
static PRHashNumber
usr_cache_hash_cert(const void *key)
{
PRHashNumber h;
const unsigned char *s;
unsigned int i = 0;
SECItem *derCert = (SECItem *)key;
unsigned int len = derCert->len;
h = 0;
for (s = (const unsigned char *)derCert->data; i < len; s++, i++)
h = (h >> 28) ^ (h << 4) ^ *s;
return h;
}
static PRHashNumber
usr_cache_hash_fn (const void *key)
{
UserCacheObj *usrObj = (UserCacheObj *)key;
if (usrObj->derCert)
return usr_cache_hash_cert(usrObj->derCert);
else
return PR_HashCaseString(usrObj->uid);
}
static int
usr_cache_compare_certs(const void *v1, const void *v2)
{
const SECItem *c1 = (const SECItem *)v1;
const SECItem *c2 = (const SECItem *)v2;
return (c1->len == c2 ->len && !memcmp(c1->data, c2->data, c1->len));
}
static int
usr_cache_compare_fn(const void *v1, const void *v2)
{
UserCacheObj *usrObj1 = (UserCacheObj *)v1;
UserCacheObj *usrObj2 = (UserCacheObj *)v2;
if (usrObj1->derCert && usrObj2->derCert)
return usr_cache_compare_certs(usrObj1->derCert, usrObj2->derCert);
else if (!usrObj1->derCert && !usrObj2->derCert)
return PR_CompareCaseStrings(usrObj1->uid, usrObj1->uid);
else
return 0;
}
static PRHashTable *alloc_db2uid_table ()
{
return PR_NewHashTable(0,
usr_cache_hash_fn,
usr_cache_compare_fn,
PR_CompareValues,
&ACLPermAllocOps,
usrcache_pool);
}
int acl_usr_cache_set_timeout (const int nsec)
{
acl_usr_cache_lifetime = (time_t)nsec;
return 0;
}
int acl_usr_cache_enabled ()
{
return (acl_usr_cache_lifetime > 0);
}
int acl_usr_cache_init ()
{
UserCacheObj *usrobj;
int i;
if (acl_usr_cache_lifetime <= 0) {
/* Caching is disabled */
DBG_PRINT1("usrcache is disabled");
return 0;
}
usrcache_pool = pool_create();
user_hash_crit_init();
if (acl_num_databases() == 0) {
/* Something wrong -- No databases registered yet! */
return -1;
}
else if (acl_num_databases() == 1) {
/* Optimize for single database */
DBG_PRINT1("Optimizing usrcache for single db");
singleDbTable = alloc_db2uid_table();
}
else {
singleDbTable = 0;
databaseUserCacheTable = PR_NewHashTable(0,
PR_HashCaseString,
PR_CompareCaseStrings,
PR_CompareValues,
&ACLPermAllocOps,
usrcache_pool);
}
/* Allocate first UserCacheObj and initialize the circular link list */
usrobj = (UserCacheObj *)pool_malloc(usrcache_pool, sizeof(UserCacheObj));
if (!usrobj) return -1;
memset((void *)usrobj, 0, sizeof(UserCacheObj));
usrobj_list = &usrobj->list;
PR_INIT_CLIST(usrobj_list);
/* Allocate rest of the UserCacheObj and put them in the link list */
for(i = 0; i < num_usrobj; i++){
usrobj = (UserCacheObj *)pool_malloc(usrcache_pool,
sizeof(UserCacheObj));
if (!usrobj) return -1;
memset((void *)usrobj, 0, sizeof(UserCacheObj));
PR_INSERT_AFTER(&usrobj->list, usrobj_list);
}
return (singleDbTable || databaseUserCacheTable) ? 0 : -1;
}
/* If the user hash table exists in the databaseUserCacheTable then return it.
* Otherwise, create a new hash table, insert it in the databaseUserCacheTable
* and then return it.
*/
static int usr_cache_table_get (const char *dbname, PRHashTable **usrTable)
{
PRHashTable *table;
if (singleDbTable) {
*usrTable = singleDbTable;
return LAS_EVAL_TRUE;
}
user_hash_crit_enter();
table = (PRHashTable *)PR_HashTableLookup(databaseUserCacheTable,
dbname);
if (!table) {
/* create a new table and insert it in the databaseUserCacheTable */
table = alloc_db2uid_table();
if (table) {
PR_HashTableAdd(databaseUserCacheTable,
pool_strdup(usrcache_pool, dbname),
table);
}
}
*usrTable = table;
user_hash_crit_exit();
return table ? LAS_EVAL_TRUE : LAS_EVAL_FAIL;
}
int acl_usr_cache_insert (const char *uid, const char *dbname,
const char *userdn, const char *passwd,
const char *group,
const SECItem *derCert, const time_t time)
{
PRHashTable *usrTable;
UserCacheObj *usrobj;
UserCacheObj key;
int rv;
if (acl_usr_cache_lifetime <= 0) {
/* Caching is disabled */
return LAS_EVAL_TRUE;
}
rv = usr_cache_table_get (dbname, &usrTable);
if (rv != LAS_EVAL_TRUE) return rv;
user_hash_crit_enter();
key.uid = (char *)uid;
key.derCert = (SECItem *)derCert;
usrobj = (UserCacheObj *)PR_HashTableLookup(usrTable, &key);
if (usrobj) {
time_t elapsed = time - usrobj->time;
int expired = (elapsed >= acl_usr_cache_lifetime);
/* Free & reset the old values in usrobj if -- there is an old value
* and if the new value is given then it is different or the usrobj
* has expired */
/* Set the field if the new value is given and the field is not set */
/* If the usrobj has not expired then we only want to update the field
* whose new value is non-NULL and different */
/* Work on the 'uid' field */
if (usrobj->uid &&
(uid ? strcmp(usrobj->uid, uid) : expired))
{
pool_free(usrcache_pool, usrobj->uid);
usrobj->uid = 0;
}
if (uid && !usrobj->uid) {
usrobj->uid = pool_strdup(usrcache_pool, uid);
}
/* Work on the 'userdn' field */
if (usrobj->userdn &&
(userdn ? strcmp(usrobj->userdn, userdn) : expired))
{
pool_free(usrcache_pool, usrobj->userdn);
usrobj->userdn = 0;
}
if (userdn && !usrobj->userdn) {
usrobj->userdn = pool_strdup(usrcache_pool, userdn);
}
/* Work on the 'passwd' field */
if (usrobj->passwd &&
(passwd ? strcmp(usrobj->passwd, passwd) : expired))
{
pool_free(usrcache_pool, usrobj->passwd);
usrobj->passwd = 0;
}
if (passwd && !usrobj->passwd) {
usrobj->passwd = pool_strdup(usrcache_pool, passwd);
}
/* Work on the 'group' field -- not replace a valid group */
if (!expired && usrobj->group &&
(group ? strcmp(usrobj->group, group) : expired))
{
pool_free(usrcache_pool, usrobj->group);
usrobj->group = 0;
}
if (group && !usrobj->group) {
usrobj->group = pool_strdup(usrcache_pool, group);
}
/* Work on the 'derCert' field */
if (usrobj->derCert &&
(derCert ? (derCert->len != usrobj->derCert->len ||
memcmp(usrobj->derCert->data, derCert->data,
derCert->len))
: expired))
{
SECITEM_FreeItem(usrobj->derCert, PR_TRUE);
usrobj->derCert = 0;
}
if (derCert && !usrobj->derCert) {
usrobj->derCert = SECITEM_DupItem((SECItem *)derCert);
}
/* Reset the time only if the usrobj has expired */
if (expired) {
DBG_PRINT1("Replace ");
usrobj->time = time;
}
else {
DBG_PRINT1("Update ");
}
}
else {
/* Get the last usrobj from the link list, erase it and use it */
/* Maybe the last usrobj is not invalid yet but we don't want to grow
* the list of usrobjs. The last obj is the best candidate for being
* not valid. We don't want to compare the time -- just use it.
*/
PRCList *tail = PR_LIST_TAIL(usrobj_list);
usrobj = USEROBJ_PTR(tail);
if (!usrobj) {
rv = LAS_EVAL_FAIL;
goto out;
}
/* If the removed usrobj is in the hashtable, remove it from there */
if (usrobj->hashtable) {
PR_HashTableRemove(usrobj->hashtable, usrobj);
}
/* Free the memory associated with the usrobj */
if (usrobj->userdn) pool_free(usrcache_pool, usrobj->userdn);
if (usrobj->passwd) pool_free(usrcache_pool, usrobj->passwd);
if (usrobj->group) pool_free(usrcache_pool, usrobj->group);
if (usrobj->derCert) SECITEM_FreeItem(usrobj->derCert, PR_TRUE);
if (usrobj->uid) pool_free(usrcache_pool, usrobj->uid);
/* Fill in the usrobj with the current data */
usrobj->uid = pool_strdup(usrcache_pool, uid);
usrobj->userdn = userdn ? pool_strdup(usrcache_pool, userdn) : 0;
usrobj->passwd = passwd ? pool_strdup(usrcache_pool, passwd) : 0;
usrobj->derCert = derCert ? SECITEM_DupItem((SECItem *)derCert) : 0;
usrobj->group = group ? pool_strdup(usrcache_pool, group) : 0;
usrobj->time = time;
/* Add the usrobj to the user hash table */
PR_HashTableAdd(usrTable, usrobj, usrobj);
usrobj->hashtable = usrTable;
DBG_PRINT1("Insert ");
}
/* Move the usrobj to the head of the list */
PR_REMOVE_LINK(&usrobj->list);
PR_INSERT_AFTER(&usrobj->list, usrobj_list);
DBG_PRINT4("acl_usr_cache_insert: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
uid, time);
out:
user_hash_crit_exit();
return rv;
}
static int acl_usr_cache_get_usrobj (const char *uid, const SECItem *derCert,
const char *dbname, const time_t time,
UserCacheObj **usrobj_out)
{
PRHashTable *usrtable;
UserCacheObj *usrobj;
UserCacheObj key;
time_t elapsed;
int rv;
*usrobj_out = 0;
if (acl_usr_cache_lifetime <= 0) {
/* Caching is disabled */
return LAS_EVAL_FALSE;
}
rv = usr_cache_table_get(dbname, &usrtable);
if (!usrtable) return LAS_EVAL_FALSE;
key.uid = (char *)uid;
key.derCert = (SECItem *)derCert;
usrobj = (UserCacheObj *)PR_HashTableLookup(usrtable, &key);
if (!usrobj) return LAS_EVAL_FALSE;
rv = LAS_EVAL_FALSE;
elapsed = time - usrobj->time;
/* If the cache is valid, return the usrobj */
if (elapsed < acl_usr_cache_lifetime) {
rv = LAS_EVAL_TRUE;
*usrobj_out = usrobj;
DBG_PRINT4("usr_cache found: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
usrobj->uid, time);
}
else {
DBG_PRINT4("usr_cache expired: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
usrobj->uid, time);
}
return rv;
}
int acl_usr_cache_passwd_check (const char *uid, const char *dbname,
const char *passwd,
const time_t time, char **dn,
pool_handle_t *pool)
{
UserCacheObj *usrobj;
int rv;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE && usrobj->passwd && passwd &&
!strcmp(usrobj->passwd, passwd))
{
/* extract dn from the usrobj */
*dn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
rv = LAS_EVAL_TRUE;
DBG_PRINT1("Success ");
}
else {
rv = LAS_EVAL_FALSE;
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_passwd_check: uid = \"%s\" at time = %ld\n",
uid, time);
user_hash_crit_exit();
return rv;
}
int acl_usr_cache_group_check (const char *uid, const char *dbname,
const char *group, const time_t time)
{
UserCacheObj *usrobj;
int rv;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE && usrobj->group && group &&
!strcmp(usrobj->group, group))
{
DBG_PRINT1("Success ");
}
else {
rv = LAS_EVAL_FALSE;
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_group_check: uid = \"%s\" group = \"%s\"\n",
uid, group ? group : "<NONE>");
user_hash_crit_exit();
return rv;
}
int acl_usr_cache_group_len_check (const char *uid, const char *dbname,
const char *group, const int len,
const time_t time)
{
UserCacheObj *usrobj;
int rv;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE && usrobj->group && group &&
!strncmp(usrobj->group, group, len))
{
rv = LAS_EVAL_TRUE;
DBG_PRINT1("Success ");
}
else {
rv = LAS_EVAL_FALSE;
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_group_check: uid = \"%s\" group = \"%s\"\n",
uid, group);
user_hash_crit_exit();
return rv;
}
int acl_usr_cache_get_userdn (const char *uid, const char *dbname,
const time_t time, char **userdn,
pool_handle_t *pool)
{
UserCacheObj *usrobj;
int rv;
*userdn = 0;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE) {
*userdn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
DBG_PRINT1("Success ");
}
else {
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_get_userdn: uid = \"%s\" userdn = \"%s\"\n",
uid, *userdn ? *userdn : "<NONE>");
user_hash_crit_exit();
return *userdn ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
}
int acl_usr_cache_userdn_check (const char *uid, const char *dbname,
const char *userdn, const time_t time)
{
UserCacheObj *usrobj;
int rv;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE && usrobj->userdn && userdn &&
!strcmp(usrobj->userdn, userdn))
{
DBG_PRINT1("Success ");
}
else {
rv = LAS_EVAL_FALSE;
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_userdn_check: uid = \"%s\" userdn = \"%s\"\n",
uid, userdn ? userdn : "<NONE>");
user_hash_crit_exit();
return rv;
}
int acl_usr_cache_set_userdn (const char *uid, const char *dbname,
const char *userdn, const time_t time)
{
int rv;
/* acl_usr_cache_insert updates the existing un-expired entry or creates a
* new one */
rv = acl_usr_cache_insert(uid, dbname, userdn, 0, 0, 0, time);
return rv;
}
int acl_usr_cache_get_group (const char *uid, const char *dbname,
const time_t time, char **group,
pool_handle_t *pool)
{
UserCacheObj *usrobj;
int rv;
*group = 0;
user_hash_crit_enter();
rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE) {
*group = usrobj->group ? pool_strdup(pool, usrobj->group) : 0;
DBG_PRINT1("Success ");
}
else {
DBG_PRINT1("Failed ");
}
DBG_PRINT3("acl_usr_cache_get_group: uid = \"%s\" group = \"%s\"\n",
uid, *group ? *group : "<NONE>");
user_hash_crit_exit();
return *group ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
}
int acl_usr_cache_set_group (const char *uid, const char *dbname,
const char *group, const time_t time)
{
int rv;
/* acl_usr_cache_insert updates the existing un-expired entry or creates a
* new one */
rv = acl_usr_cache_insert(uid, dbname, 0, 0, group, 0, time);
return rv;
}
int acl_cert_cache_insert (void *cert_in, const char *dbname,
const char *uid, const char *dn,
const time_t time)
{
CERTCertificate *cert = (CERTCertificate *)cert_in;
SECItem derCert = cert->derCert;
int rv;
rv = acl_usr_cache_insert(uid, dbname, dn, 0, 0, &derCert, time);
return rv;
}
/* Returns LAS_EVAL_TRUE if the user's cache is valid and returns uid */
int acl_cert_cache_get_uid (void *cert_in, const char *dbname,
const time_t time, char **uid, char **dn,
pool_handle_t *pool)
{
CERTCertificate *cert = (CERTCertificate *)cert_in;
SECItem derCert = cert->derCert;
UserCacheObj *usrobj = 0;
int rv;
rv = acl_usr_cache_get_usrobj(0, &derCert, dbname, time, &usrobj);
if (rv == LAS_EVAL_TRUE && usrobj && usrobj->uid) {
*uid = pool_strdup(pool, usrobj->uid);
*dn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
}
else {
*uid = 0;
*dn = 0;
rv = LAS_EVAL_FALSE;
}
return rv;
}
|