File: updatedb.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 (262 lines) | stat: -rw-r--r-- 5,888 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
/*
 * Copyright (c) 2004-2008 PADL Software Pty Ltd.
 * All rights reserved.
 * Use is subject to license.
 */

#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 <pwd.h>
#include <grp.h>

#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#else
#error This system does not appear to support the dlfcn interface.
#endif

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

struct nss_backend_handle {
	char *dbname;
	void *dlhandle;
};

typedef struct nss_vtable {
	enum nss_status (*setent)(void);
	enum nss_status (*getent)(void *, char *, size_t, int *);
	enum nss_status (*endent)(void);
} nss_vtable_t;


enum nss_status nss_backend_open(const char *dbname,
				 nss_backend_handle_t **handle_p)
{
	char libpath[PATH_MAX];
	nss_backend_handle_t *handle;

	*handle_p = NULL;

	snprintf(libpath, sizeof(libpath), "libnss_%s.so.2", dbname);

	handle = (nss_backend_handle_t *)calloc(1, sizeof(*handle));
	if (handle == NULL) {
		fprintf(stderr, "Failed to allocate backend handle: %s\n",
			strerror(errno));
		return NSS_STATUS_TRYAGAIN;
	}

	handle->dbname = strdup(dbname);
	if (handle->dbname == NULL) {
		fprintf(stderr, "Failed to allocate backend handle: %s\n",
			strerror(errno));
		nss_backend_close(&handle);
		return NSS_STATUS_TRYAGAIN;
	}

	handle->dlhandle = dlopen(libpath, RTLD_NOW | RTLD_LOCAL);
	if (handle->dlhandle == NULL) {
		fprintf(stderr, "Failed to load nameservice provider %s: %s\n",
			libpath, dlerror());
		nss_backend_close(&handle);
		return NSS_STATUS_UNAVAIL;
	}

	*handle_p = handle;

	return NSS_STATUS_SUCCESS;
}

static enum nss_status _nss_get_vtable(nss_backend_handle_t *handle,
				       unsigned dbname,
				       nss_vtable_t *vtable)
{
	char function[128];
	char *prefix;

	vtable->setent = NULL;
	vtable->getent = NULL;
	vtable->endent = NULL;

	if (dbname == MAP_PASSWD)
		prefix = "pw";
	else if (dbname == MAP_GROUP)
		prefix = "gr";
	else
		assert(0 && "Unknown map type");

	snprintf(function, sizeof(function), "_nss_%s_set%sent",
		 handle->dbname, prefix);
	vtable->setent = dlsym(handle->dlhandle, function);
	if (vtable->setent == NULL) {
		fprintf(stderr, "Failed to find symbol %s: %s\n",
			function, dlerror());
		return NSS_STATUS_UNAVAIL;
	}

	snprintf(function, sizeof(function), "_nss_%s_get%sent_r",
		 handle->dbname, prefix);
	vtable->getent = dlsym(handle->dlhandle, function);
	if (vtable->getent == NULL) {
		fprintf(stderr, "Failed to find symbol %s: %s\n",
			function, dlerror());
		return NSS_STATUS_UNAVAIL;
	}

	snprintf(function, sizeof(function), "_nss_%s_end%sent",
		 handle->dbname, prefix);
	vtable->endent = dlsym(handle->dlhandle, function);
	if (vtable->endent == NULL) {
		fprintf(stderr, "Failed to find symbol %s: %s\n",
			function, dlerror());
		return NSS_STATUS_UNAVAIL;
	}

	return NSS_STATUS_SUCCESS;
}

static enum nss_status _nss_pw_callback(nss_backend_handle_t *handle,
					void *result,
					void *private)
{
	return nss_cache_putpwent((nss_cache_t *)private,
				  (struct passwd *)result);
}

static enum nss_status _nss_gr_callback(nss_backend_handle_t *handle,
					void *result,
					void *private)
{
	return nss_cache_putgrent((nss_cache_t *)private,
				  (struct group *)result);
}

static enum nss_status _nss_enumerate(nss_backend_handle_t *handle,
				      nss_vtable_t *vtable,
				      enum nss_status (*callback)(nss_backend_handle_t *, void *, void *),
				      void *private)
{
	char *buffer;
	size_t buflen = 1024;
	enum nss_status status;
	int max_retry = 10;
	int errnop;
	union {
		struct passwd pw;
		struct group gr;
	} result;

	buffer = malloc(buflen);
	if (buffer == NULL) {
		return NSS_STATUS_TRYAGAIN;
	}

	status = vtable->setent();
	if (status != NSS_STATUS_SUCCESS) {
		return status;
	}

tryagain:
	do {
		status = vtable->getent((void *)&result, buffer, buflen, &errnop);
		if (status != NSS_STATUS_SUCCESS) {
			break;
		}
		status = callback(handle, (void *)&result, private);
	} while (status == NSS_STATUS_SUCCESS);

	if (status == NSS_STATUS_TRYAGAIN) {
		char *tmp;

		buflen *= 2;
		tmp = realloc(buffer, buflen);
		if (tmp == NULL) {
			vtable->endent();
			free(buffer);
			return NSS_STATUS_TRYAGAIN;
		}
		buffer = tmp;
		status = NSS_STATUS_SUCCESS; /* enter the loop again */
		max_retry--;
		if (max_retry >= 0)
			goto tryagain;
	}

	vtable->endent();
	
	free(buffer);

	return (status == NSS_STATUS_NOTFOUND) ? NSS_STATUS_SUCCESS : status;
}

enum nss_status nss_update_db(nss_backend_handle_t *handle,
			      unsigned map,
			      const char *filename)
{
	enum nss_status (*callback)(nss_backend_handle_t *, void *, void *);
	nss_vtable_t vtable;
	nss_cache_t *cache;
	enum nss_status status;

	status = _nss_get_vtable(handle, map, &vtable);
	if (status != NSS_STATUS_SUCCESS) {
		return status;
	}

	if (map == MAP_PASSWD)
		callback = _nss_pw_callback;
	else if (map == MAP_GROUP)
		callback = _nss_gr_callback;
	else
		assert(0 && "Unknown map type");

	status = nss_cache_init(filename, &cache);
	if (status != NSS_STATUS_SUCCESS) {
		fprintf(stderr, "Failed to open backing cache in %s: %s\n",
			filename, strerror(errno));
		return status;
	}

	status = _nss_enumerate(handle, &vtable, callback, cache);
	if (status == NSS_STATUS_SUCCESS) {
		status = nss_cache_commit(cache);
	} else {
		fprintf(stderr, "Failed to enumerate nameservice: %s\n", strerror(errno));
		status = nss_cache_abort(cache);
	}

	nss_cache_close(&cache);

	return status;
}

enum nss_status nss_backend_close(nss_backend_handle_t **handle_p)
{
	nss_backend_handle_t *handle;

	handle = *handle_p;

	if (handle != NULL) {
		if (handle->dlhandle != NULL) {
			dlclose(handle->dlhandle);
		}
		if (handle->dbname != NULL) {
			free(handle->dbname);
		}
		free(handle);
		*handle_p = NULL;
	}

	return NSS_STATUS_SUCCESS;
}