summaryrefslogtreecommitdiffstats
path: root/lib/libaccess/nsadb.cpp
blob: 119111e97e1ebe3dcc856afd8fa4c68d3a4820e2 (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 * Description (nsadb.c)
 *
 *	This module contains routines for retrieving information from
 *	a Netscape authentication database.  An authentication database
 *	consists of a user database and a group database.  This module
 *	implements an authentication database based on Netscape user and
 *	group databases defined in nsuser.h and nsgroup.h, which in turn
 *	are based on the Netscape (server) database implementation
 *	defined in nsdb.h.  The interface for managing information in
 *	an authentication database is described separately in nsamgmt.h.
 */

#include <base/systems.h>
#include <netsite.h>
#include <base/file.h>
#include <base/fsmutex.h>
#include <libaccess/nsdbmgmt.h>
#define __PRIVATE_NSADB
#include <libaccess/nsadb.h>
#include <libaccess/nsuser.h>
#include <libaccess/nsgroup.h>

/*
 * Description (NSADB_AuthIF)
 *
 *	This structure defines a generic authentication database
 *	interface for this module.  It does not currently support
 *	user/group id lookup.
 */
AuthIF_t NSADB_AuthIF = {
    0,					/* find user/group by id */
    nsadbFindByName,			/* find user/group by name */
    nsadbIdToName,			/* lookup name for user/group id */
    nsadbOpen,				/* open a named database */
    nsadbClose,				/* close a database */
};

/*
 * Description (nsadbClose)
 *
 *	This function closes an authentication database previously opened
 *	via nsadbOpen().
 *
 * Arguments:
 *
 *	authdb				- handle returned by nsadbOpen()
 *	flags				- unused (must be zero)
 */

NSAPI_PUBLIC void nsadbClose(void * authdb, int flags)
{
    AuthDB_t * adb = (AuthDB_t *)authdb;

    if (adb->adb_userdb != 0) {
	ndbClose(adb->adb_userdb, 0);
    }

    if (adb->adb_groupdb != 0) {
	ndbClose(adb->adb_groupdb, 0);
    }

#if defined(CLIENT_AUTH)
    nsadbCloseCerts(authdb, flags);
#endif

    if (adb->adb_dbname) {
	FREE(adb->adb_dbname);
    }

    FREE(adb);
}

/*
 * Description (nsadbOpen)
 *
 *	This function is used to open an authentication database.
 *	The caller specifies a name for the database, which is actually
 *	the name of a directory containing the files which comprise the
 *	database.  The caller also indicates whether this is a new
 *	database, in which case it is created.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	adbname			- name of this database (directory)
 *	flags			- open flags:
 *				    AIF_CREATE - new database (create)
 *	rptr			- pointer to returned handle
 *
 * Returns:
 *
 *	A handle for accessing the database is always returned via 'rptr'
 *	unless there was a shortage of dynamic memory, in which case a
 *	null handle is returned.  The return value of the function is
 *	0 if it completes successfully.  An error is indicated by a
 *	negative return value (see nsautherr.h).
 */

NSAPI_PUBLIC int nsadbOpen(NSErr_t * errp,
			   char * adbname, int flags, void **rptr)
{
    AuthDB_t * authdb = 0;		/* pointer to database descriptor */
    SYS_DIR dbdir;			/* database directory handle */
    int eid;				/* error id code */
    int rv;				/* result value */

    /* Make sure we have a place to return the database handle */
    if (rptr == 0) goto err_inval;

    /* Allocate the database descriptor */
    authdb = (AuthDB_t *)MALLOC(sizeof(AuthDB_t));
    if (authdb == 0) goto err_nomem;

    /* Return the descriptor pointer as the database handle */
    *rptr = (void *)authdb;

    authdb->adb_dbname = STRDUP(adbname);
    authdb->adb_userdb = 0;
    authdb->adb_groupdb = 0;
#if defined(CLIENT_AUTH)
    authdb->adb_certdb = 0;
    authdb->adb_certlock = 0;
    authdb->adb_certnm = 0;
#endif
    authdb->adb_flags = 0;

    /* See if the database directory exists */
    dbdir = dir_open(adbname);
    if (dbdir == 0) {
	/* No, create it if this is a new database, else error */
	if (flags & AIF_CREATE) {
	    rv = dir_create(adbname);
	    if (rv < 0) goto err_mkdir;
	    authdb->adb_flags |= ADBF_NEW;
	}
	else goto err_dopen;
    }
    else {
	/* Ok, it's there */
	dir_close(dbdir);
    }

    return 0;

  err_inval:
    eid = NSAUERR3000;
    rv = NSAERRINVAL;
    goto err_ret;

  err_nomem:
    /* Error - insufficient dynamic memory */
    eid = NSAUERR3020;
    rv = NSAERRNOMEM;
    goto err_ret;

  err_ret:
    nserrGenerate(errp, rv, eid, NSAuth_Program, 0);
    goto punt;

  err_mkdir:
    eid = NSAUERR3040;
    rv = NSAERRMKDIR;
    goto err_dir;

  err_dopen:
    eid = NSAUERR3060;
    rv = NSAERROPEN;
    goto err_dir;

  err_dir:
    nserrGenerate(errp, rv, eid, NSAuth_Program, 1, adbname);
    goto punt;

  punt:
    /* Fatal error - free database descriptor and return null handle */
    if (authdb) {
	if (authdb->adb_dbname) {
	    FREE(authdb->adb_dbname);
	}
	FREE(authdb);
    }

    if (rptr) *rptr = 0;

    return rv;
}

/*
 * Description (nsadbOpenUsers)
 *
 *	This function is called to open the users subdatabase of an
 *	open authentication database.  The caller specifies flags to
 *	indicate whether read or write access is required.  This
 *	function is normally called only by routines below the
 *	nsadbOpen() API, in response to perform particular operations
 *	on user or group objects.  If the open is successful, the
 *	resulting handle is stored in the AuthDB_t structure.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	authdb			- handle returned by nsadbOpen()
 *	flags			- open flags:
 *					ADBF_UREAD - open for read
 *					ADBF_UWRITE - open for read/write
 * Returns:
 *
 *	The return value is zero if the operation is successfully
 *	completed.  An error is indicated by a negative return value
 *	(see nsautherr.h), and an error frame is generated if an error
 *	frame list was provided.
 */

NSAPI_PUBLIC int nsadbOpenUsers(NSErr_t * errp, void * authdb, int flags)
{
    AuthDB_t * adb = (AuthDB_t *)authdb;
    char * userfn = 0;			/* user database name */
    int dblen;				/* strlen(adb_dbname) */
    int uversion;			/* user database version number */
    int eid;				/* error id code */
    int rv;				/* result value */

    if (adb == 0) goto err_inval;

    /* Is the user database already open? */
    if (adb->adb_userdb != 0) {

	/* Yes, is it open for the desired access? */
	if (adb->adb_flags & flags) {

	    /* Yes, that was easy */
	    return 0;
	}
    }
    else {

	/* We need to open the database */

	/* Allocate space for the user database filename */
	dblen = strlen(adb->adb_dbname);

	userfn = (char *)MALLOC(dblen + strlen(ADBUSERDBNAME) + 2);
	if (userfn == 0) goto err_nomem;

	/* Construct user database name */
	strcpy(userfn, adb->adb_dbname);

	/* Put in a '/' (or '\') if it's not there */
	if (userfn[dblen-1] != FILE_PATHSEP) {
	    userfn[dblen] = FILE_PATHSEP;
	    userfn[dblen+1] = 0;
	    ++dblen;
	}

	strcpy(&userfn[dblen], ADBUSERDBNAME);

	adb->adb_userdb = ndbOpen(errp,
				  userfn, 0, NDB_TYPE_USERDB, &uversion);
	if (adb->adb_userdb == 0) goto err_uopen;

	FREE(userfn);
    }

    /*
     * We don't really reopen the database to get the desired
     * access mode, since that is handled at the nsdb level.
     * But we do update the flags, just for the record.
     */
    adb->adb_flags &= ~(ADBF_UREAD|ADBF_UWRITE);
    if (flags & ADBF_UWRITE) adb->adb_flags |= ADBF_UWRITE;
    else adb->adb_flags |= ADBF_UREAD;

    return 0;

  err_inval:
    eid = NSAUERR3200;
    rv = NSAERRINVAL;
    goto err_ret;

  err_nomem:
    eid = NSAUERR3220;
    rv = NSAERRNOMEM;
    goto err_ret;

  err_ret:
    nserrGenerate(errp, rv, eid, NSAuth_Program, 0);
    goto punt;

  err_uopen:
    eid = NSAUERR3240;
    rv = NSAERROPEN;
    nserrGenerate(errp, rv, eid, NSAuth_Program, 1, userfn);
    goto punt;

  punt:
    return rv;
}

/*
 * Description (nsadbOpenGroups)
 *
 *	This function is called to open the groups subdatabase of an
 *	open authentication database.  The caller specifies flags to
 *	indicate whether read or write access is required.  This
 *	function is normally called only by routines below the
 *	nsadbOpen() API, in response to perform particular operations
 *	on user or group objects.  If the open is successful, the
 *	resulting handle is stored in the AuthDB_t structure.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	authdb			- handle returned by nsadbOpen()
 *	flags			- open flags:
 *					ADBF_GREAD - open for read
 *					ADBF_GWRITE - open for read/write
 * Returns:
 *
 *	The return value is zero if the operation is successfully
 *	completed.  An error is indicated by a negative return value
 *	(see nsautherr.h), and an error frame is generated if an error
 *	frame list was provided.
 */

NSAPI_PUBLIC int nsadbOpenGroups(NSErr_t * errp, void * authdb, int flags)
{
    AuthDB_t * adb = (AuthDB_t *)authdb;
    char * groupfn = 0;			/* group database name */
    int dblen;				/* strlen(adb_dbname) */
    int gversion;			/* group database version number */
    int eid;				/* error id code */
    int rv;				/* result value */

    if (adb == 0) goto err_inval;

    /* Is the group database already open? */
    if (adb->adb_groupdb != 0) {

	/* Yes, is it open for the desired access? */
	if (adb->adb_flags & flags) {

	    /* Yes, that was easy */
	    return 0;
	}
    }
    else {

	/* We need to open the database */

	/* Allocate space for the group database filename */
	dblen = strlen(adb->adb_dbname);

	groupfn = (char *)MALLOC(dblen + strlen(ADBGROUPDBNAME) + 2);
	if (groupfn == 0) goto err_nomem;

	/* Construct group database name */
	strcpy(groupfn, adb->adb_dbname);

	/* Put in a '/' (or '\') if it's not there */
	if (groupfn[dblen-1] != FILE_PATHSEP) {
	    groupfn[dblen] = FILE_PATHSEP;
	    groupfn[dblen+1] = 0;
	    ++dblen;
	}

	strcpy(&groupfn[dblen], ADBGROUPDBNAME);

	adb->adb_groupdb = ndbOpen(errp,
				   groupfn, 0, NDB_TYPE_GROUPDB, &gversion);
	if (adb->adb_groupdb == 0) goto err_gopen;

	FREE(groupfn);
    }

    /*
     * We don't really reopen the database to get the desired
     * access mode, since that is handled at the nsdb level.
     * But we do update the flags, just for the record.
     */
    adb->adb_flags &= ~(ADBF_GREAD|ADBF_GWRITE);
    if (flags & ADBF_GWRITE) adb->adb_flags |= ADBF_GWRITE;
    else adb->adb_flags |= ADBF_GREAD;

    return 0;

  err_inval:
    eid = NSAUERR3300;
    rv = NSAERRINVAL;
    goto err_ret;

  err_nomem:
    eid = NSAUERR3320;
    rv = NSAERRNOMEM;
    goto err_ret;

  err_ret:
    nserrGenerate(errp, rv, eid, NSAuth_Program, 0);
    goto punt;

  err_gopen:
    eid = NSAUERR3340;
    rv = NSAERROPEN;
    nserrGenerate(errp, rv, eid, NSAuth_Program, 1, groupfn);
    goto punt;

  punt:
    return rv;
}

/*
 * Description (nsadbIdToName)
 *
 *	This function looks up a specified user or group id in the
 *	authentication database.  The name associated with the specified
 *	id is returned.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	authdb			- handle returned by nsadbOpen()
 *	id			- user or group id
 *	flags			- AIF_USER or AIF_GROUP (defined in nsauth.h)
 *	rptr			- pointer to returned group or user name
 *
 * Returns:
 *
 *	The return value is zero if no error occurs,
 *	A negative return value indicates an error.
 */

NSAPI_PUBLIC int nsadbIdToName(NSErr_t * errp,
			       void * authdb, USI_t id, int flags, char **rptr)
{
    AuthDB_t * adb = (AuthDB_t *)authdb;
    void * whichdb = 0;
    char * name;
    int rv;

    if (rptr != 0) *rptr = 0;

    /* Decide whether to use user or group database */
    if (flags & AIF_USER) {

	whichdb = adb->adb_userdb;
	if (whichdb == 0) {
	    rv = nsadbOpenUsers(errp, authdb, ADBF_UREAD);
	    if (rv < 0) goto punt;
	    whichdb = adb->adb_userdb;
	}
    }
    else if (flags & AIF_GROUP) {

	whichdb = adb->adb_groupdb;
	if (whichdb == 0) {
	    rv = nsadbOpenGroups(errp, authdb, ADBF_GREAD);
	    if (rv < 0) goto punt;
	    whichdb = adb->adb_groupdb;
	}
    }

    if (whichdb != 0) {

	/* Get the name corresponding to the id */
	rv = ndbIdToName(errp, whichdb, id, 0, &name);
	if (rv < 0) goto punt;

	if ((rptr != 0)) *rptr = name;
	rv = 0;
    }

  punt:
    return rv;
}

/*
 * Description (nsadbFindByName)
 *
 *	This function looks up a specified name in the authentication
 *	database.  Flags specified by the caller indicate whether a
 *	group name, user name, or either should be found.  The caller
 *	may optionally provide for the return of a user or group object
 *	pointer, in which case the information associated with a
 *	matching group or user is used to create a group or user object.
 *
 * Arguments:
 *
 *	errp			- error frame list pointer (may be null)
 *	authdb			- handle returned by nsadbOpen()
 *	name			- name of group or user
 *	flags			- search flags (defined in nsauth.h)
 *	rptr			- pointer to returned group or user
 *				  object pointer (may be null)
 *
 * Returns:
 *
 *	The return value is a non-negative value if no error occurs,
 *	and the value indicates whether the name matched a group or
 *	user:
 *
 *	AIF_NONE		- name did not match a group or user name
 *	AIF_GROUP		- name matched a group name
 *	AIF_USER		- name matched a user name
 *
 *	If the value is AIF_GROUP or AIF_USER, and rptr is non-null,
 *	then a group or user object is created, and a pointer to it is
 *	returned in the location indicated by rptr.
 *
 *	A negative return value indicates an error.
 */

NSAPI_PUBLIC int nsadbFindByName(NSErr_t * errp, void * authdb,
				 char * name, int flags, void **rptr)
{
    AuthDB_t * adb = (AuthDB_t *)authdb;
    ATR_t recptr;
    int reclen;
    int rv;

    if (rptr != 0) *rptr = 0;

    /* Search for group name? */
    if (flags & AIF_GROUP) {

	if (adb->adb_groupdb == 0) {
	    rv = nsadbOpenGroups(errp, authdb, ADBF_GREAD);
	    if (rv < 0) goto punt;
	}

	/* Look up the name in the group database */
	rv = ndbFindName(errp, adb->adb_groupdb, 0, (char *)name,
			 &reclen, (char **)&recptr);
	if (rv == 0) {

	    /* Found it.  Make a group object if requested. */
	    if (rptr != 0) {

		/* Got the group record.  Decode into a group object. */
		*rptr = (void *)groupDecode((NTS_t)name, reclen, recptr);
	    }

	    return AIF_GROUP;
	}
    }

    /* Search for user name? */
    if (flags & AIF_USER) {

	if (adb->adb_userdb == 0) {
	    rv = nsadbOpenUsers(errp, authdb, ADBF_UREAD);
	    if (rv < 0) goto punt;
	}

	/* Look up the name in the user database */
	rv = ndbFindName(errp, adb->adb_userdb, 0, (char *)name,
			 &reclen, (char **)&recptr);
	if (rv == 0) {

	    /* Found it.  Make a user object if requested. */
	    if (rptr != 0) {

		/* Got the user record.  Decode into a user object. */
		*rptr = (void *)userDecode((NTS_t)name, reclen, recptr);
	    }

	    return AIF_USER;
	}
    }

    /* Nothing found */
    nserrDispose(errp);
    return AIF_NONE;

  punt:
    return rv;
}