File: cache.c

package info (click to toggle)
nss-updatedb 10-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 696 kB
  • ctags: 66
  • sloc: sh: 5,802; ansic: 732; makefile: 54
file content (423 lines) | stat: -rw-r--r-- 8,624 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
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
/*
 * Copyright (c) 2004-2008 PADL Software Pty Ltd.
 * All rights reserved.
 * Use is subject to license.
 */

#define _GNU_SOURCE /* stpcpy() */

#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 <db.h>

#include "cache.h"
#include "updatedb.h"

/*
 * Make name service data persistent
 */

struct nss_cache {
	char *filename;
	char *tmpfilename;
	DB *db;
	int index;
};

static void nss_cache_error(const DB_ENV *dbenv, const char *errpfx, const char *msg)
{
	fprintf(stderr, "ERROR: nss_cache_error: %s\n", msg);
}

enum nss_status nss_cache_init(const char *filename,
			       nss_cache_t **cache_p)
{
	nss_cache_t *cache;
	int rc = -1, mode = 0644;

	cache = (nss_cache_t *)calloc(1, sizeof(*cache));
	if (cache == NULL) {
		return NSS_STATUS_TRYAGAIN;
	}

	cache->filename = strdup(filename);
	if (cache->filename == NULL) {
		nss_cache_close(&cache);
		return NSS_STATUS_TRYAGAIN;
	}

	/* Create a temporary database file */
	cache->tmpfilename = strdup(DB_DIR "/tmpdbXXXXXX");
	if (cache->tmpfilename == NULL) {
		nss_cache_close(&cache);
		return NSS_STATUS_TRYAGAIN;
	}
	if (mktemp(cache->tmpfilename) == NULL) {
		nss_cache_close(&cache);
		return NSS_STATUS_TRYAGAIN;
	}

#if DB_VERSION_MAJOR >= 4
	rc = db_create(&cache->db, NULL, 0);
	if (rc != 0) {
		nss_cache_close(&cache);
		errno = rc;
		return NSS_STATUS_UNAVAIL;
	}

	cache->db->set_errcall(cache->db, &nss_cache_error);

	rc = cache->db->open(cache->db, NULL,
			     cache->tmpfilename, NULL, 
			     DB_BTREE, DB_CREATE | DB_EXCL, mode);
	if (rc != 0) {
		nss_cache_close(&cache);
		errno = rc;
		return NSS_STATUS_UNAVAIL;
	}

#elif DB_VERSION_MAJOR == 3
	rc = db_create(&cache->db, NULL, 0);
	if (rc != 0) {
		nss_cache_close(&cache);
		errno = rc;
		return NSS_STATUS_UNAVAIL;
	}
	rc = cache->db->open(cache->db, cache->tmpfilename, NULL,
			     DB_BTREE, DB_CREATE | DB_TRUNCATE, mode);
	if (rc != 0) {
		nss_cache_close(&cache);
		errno = rc;
		return NSS_STATUS_UNAVAIL;
	}
#elif DB_VERSION_MAJOR == 2
	rc = db_open(cache->tmpfilename, DB_BTREE, DB_CREATE | DB_TRUNCATE,
		     mode, NULL, NULL, &cache->db);
	if (rc != 0) {
		nss_cache_close(&cache);
		errno = rc;
		return NSS_STATUS_UNAVAIL;
	}
#else
	cache->db = dbopen(cache->tmpfilename, O_CREAT | O_TRUNC,
			   mode, DB_BTREE, NULL);
	if (cache->db == NULL) {
		nss_cache_close(&cache);
		return NSS_STATUS_UNAVAIL;
	}
#endif

	*cache_p = cache;

	return NSS_STATUS_SUCCESS;
}

enum nss_status nss_cache_put(nss_cache_t *cache,
			      const char *key,
			      const char *value)
{
	DBT db_key;
	DBT db_val;
	int rc;

	memset(&db_key, 0, sizeof(db_key));
	db_key.data = (char *)key;
	db_key.size = strlen(key);

	memset(&db_val, 0, sizeof(db_val));
	db_val.data = (char *)value;
	db_val.size = strlen(value) + 1;

	rc = (cache->db->put)(cache->db,
#if DB_VERSION_MAJOR >= 4
			      NULL,
#elif DB_VERSION_MAJOR >= 2
			      NULL,
#endif
			      &db_key,
			      &db_val,
#if DB_VERSION_MAJOR >= 2
			      DB_NOOVERWRITE
#else
			      R_NOOVERWRITE
#endif
			      );

#if DB_VERSION_MAJOR < 2
#define DB_KEYEXIST 1
#endif

	if (rc == DB_KEYEXIST) {
		/*fprintf(stderr, "Ignoring duplicate key %s\n", key);*/
	} else if (rc != 0) {
#if DB_VERSION_MAJOR >= 2
		errno = rc;
#endif
		fprintf(stderr, "Error: puting into DB: %s: key '%s' value '%s'\n", 
			db_strerror(rc), (char *)db_key.data, (char *)db_val.data);
		return NSS_STATUS_UNAVAIL;
	}

	return NSS_STATUS_SUCCESS;
}

static enum nss_status _nss_cache_put_name(nss_cache_t *cache,
					   const char *name,
					   const char *value)
{
	char *s = NULL;
	size_t len;
	char buf[1024], *buf2 = NULL;
	enum nss_status status;

	len = strlen(name);
	if (len >= sizeof(buf)) {
		buf2 = malloc(1 + len + 1);
		if (buf2 == NULL)
			return NSS_STATUS_TRYAGAIN;
		s = buf2;
	} else {
		s = buf;
	}

	s[0] = '.';
	memcpy(&s[1], name, len);
	s[1 + len] = '\0';

	status = nss_cache_put(cache, s, value);

	if (buf2 != NULL)
		free(buf2);

	return status;
}

static enum nss_status _nss_cache_put_id(nss_cache_t *cache,
					 int id,
					 const char *value)
{
	char buf[32];

	snprintf(buf, sizeof(buf), "=%d", id);

	return nss_cache_put(cache, buf, value);
}

static enum nss_status _nss_cache_put_index(nss_cache_t *cache,
					    int index,
					    const char *value)
{
	char buf[32];

	snprintf(buf, sizeof(buf), "0%d", index);

	return nss_cache_put(cache, buf, value);
}

enum nss_status nss_cache_putpwent(nss_cache_t *cache,
				   struct passwd *pw)
{
	/*
	 * We need to put the following keys:
	 *
	 *	=<UID>
	 *	0<SEQUENCE>
	 *	.<USERNAME>
	 */
	char pwbuf[1024];
	enum nss_status status;

	if (pw->pw_name == NULL) {
		return NSS_STATUS_NOTFOUND;
	}

	snprintf(pwbuf, sizeof(pwbuf), "%s:%s:%d:%d:%s:%s:%s",
		 pw->pw_name,
		 (pw->pw_passwd != NULL) ? pw->pw_passwd : "x",
		 pw->pw_uid,
		 pw->pw_gid,
		 (pw->pw_gecos != NULL) ? pw->pw_gecos : "",
		 (pw->pw_dir != NULL) ? pw->pw_dir : "",
		 (pw->pw_shell != NULL) ? pw->pw_shell : "");

	status = _nss_cache_put_name(cache, pw->pw_name, pwbuf);
	if (status != NSS_STATUS_SUCCESS)
		return status;

	status = _nss_cache_put_id(cache, pw->pw_uid, pwbuf);
	if (status != NSS_STATUS_SUCCESS)
		return status;

	status = _nss_cache_put_index(cache, cache->index, pwbuf);
	if (status != NSS_STATUS_SUCCESS)
		return status;

	cache->index++;

	return NSS_STATUS_SUCCESS;
}

enum nss_status nss_cache_putgrent(nss_cache_t *cache,
				   struct group *gr)
{
	/*
	 * We need to put the following keys:
	 *
	 *	=<GID>
	 *	0<SEQUENCE>
	 *	.<GROUPNAME>
	 */
	char grbuf[1024], *grbuf2 = NULL;
	char *grent, *p;
	enum nss_status status;
	char **grmem_p;
	size_t len;
	char gidbuf[32];

	if (gr->gr_name == NULL) {
		return NSS_STATUS_NOTFOUND;
	}

	snprintf(gidbuf, sizeof(gidbuf), "%d", gr->gr_gid);

	len = strlen(gr->gr_name) + 1 /* : */;
	len += ((gr->gr_passwd != NULL) ? strlen(gr->gr_passwd) : 1) + 1 /* : */;
	len += strlen(gidbuf) + 1 /* : */;

	if (gr->gr_mem != NULL) {
		for (grmem_p = gr->gr_mem; *grmem_p != NULL; grmem_p++) {
			if (grmem_p != gr->gr_mem)
				len++; /* , */
			len += strlen(*grmem_p);
		}
	}
	len++; /* \0 terminator */

	if (len > sizeof(grbuf)) {
		grbuf2 = malloc(len);
		if (grbuf2 == NULL)
			return NSS_STATUS_TRYAGAIN;
		grent = grbuf2;
	} else {
		grent = grbuf;
	}

	p = grent;
	p = stpcpy(p, gr->gr_name);
	p = stpcpy(p, ":");
	p = stpcpy(p, (gr->gr_passwd != NULL) ? gr->gr_passwd : "x");
	p = stpcpy(p, ":");
	p = stpcpy(p, gidbuf);
	p = stpcpy(p, ":");

	if (gr->gr_mem != NULL) {
		for (grmem_p = gr->gr_mem; *grmem_p != NULL; grmem_p++) {
			if (grmem_p != gr->gr_mem)
				p = stpcpy(p, ",");
			p = stpcpy(p, *grmem_p);
		}
	}

	status = _nss_cache_put_name(cache, gr->gr_name, grent);
	if (status != NSS_STATUS_SUCCESS) {
		if (grbuf2 != NULL)
			free(grbuf2);
		return status;
	}

	status = _nss_cache_put_id(cache, gr->gr_gid, grent);
	if (status != NSS_STATUS_SUCCESS) {
		if (grbuf2 != NULL)
			free(grbuf2);
		return status;
	}

	status = _nss_cache_put_index(cache, cache->index, grent);
	if (status != NSS_STATUS_SUCCESS) {
		if (grbuf2 != NULL)
			free(grbuf2);
		return status;
	}

	cache->index++;

	if (grbuf2 != NULL)
		free(grbuf2);

	return NSS_STATUS_SUCCESS;
}

enum nss_status nss_cache_commit(nss_cache_t *cache)
{
	int rc;

#if 0
	if (cache->index == 0) {
		fprintf(stderr, "Warning: no information was retrieved from the "
			"name service, so the cache will not be replaced\n");
		return NSS_STATUS_UNAVAIL;
	}
#endif

	rc = (cache->db->sync)(cache->db, 0);
	if (rc != 0) {
#if DB_VERSION_MAJOR > 2
		errno = rc;
#endif
		return NSS_STATUS_UNAVAIL;
	}

	/* Link temporary file to final filename.
	   Use rename and link to avoid cache being unavailable. */
	rc = rename(cache->tmpfilename, cache->filename);
	if (rc != 0) {
		perror("rename");
		return NSS_STATUS_UNAVAIL;
	}
	rc = link(cache->filename, cache->tmpfilename);
	if (rc != 0) {
		perror("link");
		return NSS_STATUS_UNAVAIL;
	}

	return NSS_STATUS_SUCCESS;
}

enum nss_status nss_cache_abort(nss_cache_t *cache)
{
	return NSS_STATUS_UNAVAIL;
}

enum nss_status nss_cache_close(nss_cache_t **cache_p)
{
	nss_cache_t *cache;

	cache = *cache_p;

	if (cache != NULL) {
#if DB_VERSION_MAJOR >= 4
		if (cache->db != NULL)
			cache->db->close(cache->db, 0);
#endif
		if (cache->filename != NULL)
			free(cache->filename);
		if (cache->tmpfilename != NULL) {
			(void)unlink(cache->tmpfilename);
			free(cache->tmpfilename);
		}
		free(cache);
		*cache_p = NULL;
	}

	return NSS_STATUS_SUCCESS;
}