File: cc_db.c

package info (click to toggle)
libpam-ccreds 10-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 644 kB
  • sloc: sh: 2,981; ansic: 2,066; makefile: 60
file content (372 lines) | stat: -rw-r--r-- 6,256 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2004 PADL Software Pty Ltd.
 * All rights reserved.
 * Use is subject to license.
 */

/*
 * Database library wrappers
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>

#include <sys/file.h>

#ifdef HAVE_DB_H
#include <db.h>
#endif

#include "cc_private.h"

static int _pam_cc_db_lock(void *_db, int operation);
static int _pam_cc_db_sync(void *_db);

#if DB_VERSION_MAJOR <= 2
#define USE_FLOCK	1
#else
/* XXX how do we enable locking here? */
#define USE_FLOCK	1
#endif

#ifndef DB_NOTFOUND
#define DB_NOTFOUND	1
#endif

/* Open an underlying datastore */
int pam_cc_db_open(const char *filename, unsigned int flags,
		   int mode, void **db_p)
{
	DB *db;
	unsigned int db_flags = 0;

#if DB_VERSION_MAJOR > 2
	int rc;

	if (flags & CC_DB_FLAGS_WRITE) {
		db_flags |= DB_CREATE;
	}
	if (flags & CC_DB_FLAGS_READ) {
		db_flags |= DB_RDONLY;
	}

	rc = db_create(&db, NULL, 0);
	if (rc != 0) {
		errno = rc;
		return PAM_SERVICE_ERR;
	}

#if (DB_VERSION_MAJOR > 3) && (DB_VERSION_MINOR > 0)
	rc = db->open(db, NULL, filename, NULL,
		      DB_BTREE, db_flags, mode);
#else
	rc = db->open(db, filename, NULL,
		      DB_BTREE, db_flags, mode);
#endif

	if (rc != 0) {
		db->close(db, 0);
		errno = rc;
		return PAM_SERVICE_ERR;
	}

#elif DB_VERSION_MAJOR == 2
	int rc;

	if (flags & CC_DB_FLAGS_WRITE) {
		db_flags |= DB_CREATE;
	}
	if (flags & CC_DB_FLAGS_READ) {
		db_flags |= DB_RDONLY;
	}

	rc = db_open(filename, DB_BTREE, db_flags, mode, NULL, NULL, &db);
	if (rc != 0) {
		errno = rc;
		return PAM_SERVICE_ERR;
	}
#else
	if (flags & CC_DB_FLAGS_WRITE) {
		db_flags |= O_CREAT;
	}
	if (flags & CC_DB_FLAGS_READ) {
		db_flags |= O_RDONLY;
	}
	db = dbopen(filename, db_flags, mode, DB_BTREE, NULL);
	if (db == NULL) {
		return PAM_SERVICE_ERR;
	}
#endif /* DB_VERSION_MAJOR > 2 */

	*db_p = (void *)db;

	return PAM_SUCCESS;
}

/* Write to underlying datastore */  
int pam_cc_db_put(void *_db, const char *keyname, size_t keylength,
		  const char *data, size_t size)
{
	DB *db = (DB *)_db;
	DBT key;
	DBT val;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (char *)keyname;
	key.size = keylength;

	memset(&val, 0, sizeof(val));
	val.data = (char *)data;
	val.size = size;

#if USE_FLOCK
	rc = _pam_cc_db_lock(db, LOCK_EX);
	if (rc != PAM_SUCCESS) {
		return rc;
	}
#endif

	rc = db->put(db,
#if DB_VERSION_MAJOR >= 2
		NULL,
#endif
		&key, &val, 0);
	if (rc == 0) {
		rc = _pam_cc_db_sync(db);
	} else {
		fprintf(stderr, "%s\n", strerror(rc));
#if DB_VERSION_MAJOR >= 2
		errno = rc;
#endif
		rc = PAM_SERVICE_ERR;
	}

#if USE_FLOCK
	_pam_cc_db_lock(db, LOCK_UN);
#endif

	return rc;
}

/* Read from underlying datastore */
int pam_cc_db_get(void *_db, const char *keyname, size_t keylength,
		  char *data, size_t *size)
{
	DB *db = (DB *)_db;
	DBT key;
	DBT val;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (char *)keyname;
	key.size = keylength;

	memset(&val, 0, sizeof(val));

#if USE_FLOCK
	rc = _pam_cc_db_lock(db, LOCK_SH);
	if (rc != PAM_SUCCESS) {
		return rc;
	}
#endif

	rc = db->get(db,
#if DB_VERSION_MAJOR >= 2
		NULL,
#endif
		&key, &val, 0);

#if USE_FLOCK
	_pam_cc_db_lock(db, LOCK_UN);
#endif

	if (rc != 0) {
#if DB_VERSION_MAJOR >= 2
		if (rc != DB_NOTFOUND)
			errno = rc;
#endif
		return (rc == DB_NOTFOUND) ? PAM_AUTHINFO_UNAVAIL : PAM_SERVICE_ERR;
	}

	if (val.size > *size) {
		return PAM_BUF_ERR;
	}

	memcpy(data, val.data, val.size);
	*size = val.size;

	return PAM_SUCCESS;
}

int pam_cc_db_delete(void *_db, const char *keyname, size_t keylength)
{
	DB *db = (DB *)_db;
	DBT key;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (char *)keyname;
	key.size = keylength;

#if USE_FLOCK
	rc = _pam_cc_db_lock(db, LOCK_EX);
	if (rc != PAM_SUCCESS) {
		return rc;
	}
#endif

	rc = db->del(db,
#if DB_VERSION_MAJOR >= 2
		NULL,
#endif
		&key, 0);
	if (rc == 0) {
		rc = _pam_cc_db_sync(db);
	} else {
#if DB_VERSION_MAJOR >= 2
		if (rc != DB_NOTFOUND)
			errno = rc;
#endif
		return (rc == DB_NOTFOUND) ? PAM_AUTHINFO_UNAVAIL : PAM_SERVICE_ERR;
	}

#if USE_FLOCK
	_pam_cc_db_lock(db, LOCK_UN);
#endif

	return rc;
}

/* Close underlying datastore */
int pam_cc_db_close(void **db_p)
{
	DB *db;

	if (*db_p != NULL) {
		db = *db_p;

		if (db != NULL) {
#if DB_VERSION_MAJOR >= 2
			db->close(db, 0);
#else
			db->close(db);
#endif /* DB_VERSION_MAJOR > 2 */
		}

		*db_p = NULL;
	}

	return PAM_SUCCESS;
}

static int _pam_cc_db_lock(void *_db, int operation)
{
	DB *db = (DB *)_db;
	int fd;

#if DB_VERSION_MAJOR >= 2
	int rc;

	rc = db->fd(db, &fd);
	if (rc != 0) {
		return PAM_SERVICE_ERR;
	}
#else
	fd = db->fd(db);
#endif /* DB_VERSION_MAJOR >= 2 */
	if (fd < 0) {
		return PAM_SERVICE_ERR;
	}

	if (flock(fd, operation) != 0) {
		return PAM_AUTHTOK_LOCK_BUSY;
	}

	return PAM_SUCCESS;
}

static int _pam_cc_db_sync(void *_db)
{
	DB *db = (DB *)_db;
	int rc;

	rc = db->sync(db, 0);

	return (rc == 0) ? PAM_SUCCESS : PAM_AUTHINFO_UNAVAIL;	
}

int pam_cc_db_seq(void *_db, void **cookie,
		  const char **key_p, size_t *keylength_p,
		  const char **data_p, size_t *datalength_p)
{
	DB *db = (DB *)_db;
	DBT key;
	DBT val;
	int rc;
#if DB_VERSION_MAJOR >= 2
	DBC *cursor = (DBC *)*cookie;
	int first = 0;
#endif

	memset(&key, 0, sizeof(key));
	memset(&val, 0, sizeof(val));

#if DB_VERSION_MAJOR < 2
	rc = db->seq(db, &key, &val, (*cookie == NULL ? R_FIRST : R_NEXT));
	if (*cookie == NULL) {
		*cookie = (void *)1;
	}
#else
	if (cursor == NULL) {
# if DB_VERSION_MAJOR > 2 || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR > 5)
		rc = db->cursor(db, NULL, &cursor, 0);
# else
		rc = db->cursor(db, NULL, &cursor);
# endif
		if (rc != 0) {
			errno = rc;
			return PAM_SERVICE_ERR;
		}

		*cookie = cursor;
		first++;
	}

	rc = cursor->c_get(cursor, &key, &val,
			   first ? DB_FIRST : DB_NEXT);
#endif /* DB_VERSION_MAJOR <= 2 */

	switch (rc) {
	case DB_NOTFOUND:
		rc = PAM_SUCCESS;
		break;
	case 0:
		rc = PAM_INCOMPLETE;
		break;
	default:
		errno = rc;
		rc = PAM_SERVICE_ERR;
		return rc;
		break;
	}

	*key_p = key.data;
	*keylength_p = key.size;

	*data_p = val.data;
	*datalength_p = val.size;

	return rc;
}