summaryrefslogtreecommitdiffstats
path: root/src/generic.c
blob: f23792d214a65e3bd4cf480bf7c2f2ce1518f4ca (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
/*
 * Copyright (C) 2002 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ident "$Id: generic.c,v 1.1 2002/11/18 19:53:21 nalin Exp $"

#include "../config.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <limits.h>
#include <nss.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parsers.h"
#include <pwd.h>

#define CHUNK_SIZE LINE_MAX
#define FALSE 0
#define TRUE (!FALSE)

static const char *skip_names[] = {
	"*~",
	"*.rpmsave",
	"*.rpmorig",
	"*.rpmnew",
};

static int
skip_file_by_name(const char *filename)
{
	int i;
	for (i = 0; i < sizeof(skip_names) / sizeof(skip_names[0]); i++) {
		if (fnmatch(skip_names[i], filename, 0) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static char *
read_line(FILE *fp)
{
	char *buffer;
	size_t buflen, length;

	buflen = CHUNK_SIZE;
	length = 0;
	buffer = malloc(CHUNK_SIZE);
	if (buffer == NULL) {
		return NULL;
	}
	memset(buffer, 0, buflen);

	while (fgets(buffer + length, buflen - length, fp) != NULL) {
		length = strlen(buffer);
		if (length > 0) {
			if (buffer[length - 1] == '\n') {
				break;
			} else {
				buflen += CHUNK_SIZE;
				buffer = realloc(buffer, buflen);
				if (buffer == NULL) {
					return NULL;
				}
				memset(buffer + length, 0, buflen - length);
			}
		}
	}

	if (strlen(buffer) == 0) {
		free(buffer);
		return NULL;
	}

	if ((length > 0) && (buffer[length - 1] == '\n')) {
		buffer[length - 1] = '\0';
	}

	return buffer;
}

static enum nss_status
getgen(struct STRUCTURE *result,
#ifdef GET_EXTRA_CRITERIA
       GET_EXTRA_CRITERIA,
#endif
       char *buffer, size_t buflen, int *errnop,
       int (*compare)(const void *compare_data, struct STRUCTURE *structure),
       const void *compare_data)
{
	DIR *dir = NULL;
	FILE *fp = NULL;
	struct STRUCTURE structure;
	struct dirent *ent = NULL;
	struct stat st;
	char path[PATH_MAX], *line;

	/* Start reading the directory. */
	dir = opendir(SYSCONFDIR "/" FILENAME ".d");
	if (dir == NULL) {
		*errnop = errno;
		return NSS_STATUS_NOTFOUND;
	}

	/* Iterate over each file in the directory. */
	while ((ent = readdir(dir)) != NULL) {
		/* If the file has a certain name, skip it. */
		if (skip_file_by_name(ent->d_name)) {
			continue;
		}

		/* Figure out the full name of the file. */
		snprintf(path, sizeof(path), SYSCONFDIR "/" FILENAME ".d/%s",
			 ent->d_name);

		/* If we can't open it, skip it. */
		fp = fopen(path, "r");
		if (fp == NULL) {
			continue;
		}

		/* If we can't stat() it, or it's not a regular file or
		 * a symlink, skip it. */
		if ((fstat(fileno(fp), &st) != 0) ||
		    (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))) {
			fclose(fp);
			continue;
		}

		/* Read the next line. */
		while ((line = read_line(fp)) != NULL) {
			/* If we had trouble parsing it, continue. */
			if (parser(line, &structure,
				   (struct parser_data*) buffer, buflen,
				   errnop) == 0) {
				free(line);
				continue;
			}
			/* If it matches, close the file and the directory,
			 * and return the answer. */
			if ((compare(compare_data, &structure) == 0) &&
#ifdef CHECK_EXTRA_CRITERIA
			    CHECK_EXTRA_CRITERIA(&structure) &&
#endif
			    TRUE) {
				*result = structure;
				free(line);
				fclose(fp);
				closedir(dir);
				return NSS_STATUS_SUCCESS;
			}

			/* Free this line. */
			free(line);
		}

		/* Close this file. */
		fclose(fp);
	}

	/* Close this directory. */
	closedir(dir);

	/* Tell the caller that we didn't find anything. */
	return NSS_STATUS_NOTFOUND;
}
#if defined(getnam) && defined(getnam_field)
static int
compare_name(const void *compare_data, struct STRUCTURE *structure)
{
#ifdef getnam_fields
	int i;
	if (structure->getnam_fields != NULL) {
		for (i = 0; structure->getnam_fields[i] != NULL; i++) {
			if (strcmp(structure->getnam_fields[i],
				   (const char *)compare_data) == 0) {
				return 0;
			}
		}
	}
#endif
	return strcmp(structure->getnam_field, (const char *)compare_data);
}
enum nss_status
getnam(const char *name,
#ifdef GET_EXTRA_CRITERIA
       GET_EXTRA_CRITERIA,
#endif
       struct STRUCTURE *result, char *buffer, size_t buflen, int *errnop )
{
	return getgen(result,
#ifdef EXTRA_CRITERIA_NAMES
		      EXTRA_CRITERIA_NAMES,
#endif
		      buffer, buflen, errnop,
		      compare_name, (const void*) name);
}
#endif
#if defined(getnum) && defined(getnum_type) && defined(getnum_field)
static int
compare_number(const void *compare_data, struct STRUCTURE *structure)
{
	return (structure->getnum_field != (getnum_type) compare_data);
}
enum nss_status
getnum(getnum_type number,
#ifdef GET_EXTRA_CRITERIA
       GET_EXTRA_CRITERIA,
#endif
       struct STRUCTURE *result, char *buffer, size_t buflen, int *errnop )
{
	return getgen(result,
#ifdef EXTRA_CRITERIA_NAMES
		      EXTRA_CRITERIA_NAMES,
#endif
		      buffer, buflen, errnop,
		      compare_number, (const void*) number);
}
#endif
#if defined(setent) && defined(getent) && defined(endent)

static pthread_mutex_t enumeration_lock = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() pthread_mutex_lock(&enumeration_lock)
#define UNLOCK() pthread_mutex_unlock(&enumeration_lock)
static DIR *dir = NULL;
static FILE *fp = NULL;

enum nss_status
endent(void)
{
	/* Close the directory and the current file. */
	LOCK();
	if (fp != NULL) {
		fclose(fp);
		fp = NULL;
	}
	if (dir != NULL) {
		closedir(dir);
		dir = NULL;
	}
	UNLOCK();
	return NSS_STATUS_UNAVAIL;
}
enum nss_status
setent(int stayopen)
{
	/* Close and reopen the directory. */
	endent();
	LOCK();
	dir = opendir(SYSCONFDIR "/" FILENAME ".d");
	UNLOCK();
	return NSS_STATUS_UNAVAIL;
}
enum nss_status
getent(struct STRUCTURE *result, char *buffer, size_t buflen, int *errnop)
{
	char path[PATH_MAX], *line;
	struct dirent *ent;
	struct stat st;
	struct STRUCTURE structure;

	LOCK();

	/* If we don't have a current directory, try to reset. */
	if (dir == NULL) {
		setent(TRUE);
		/* If we couldn't open the directory, then we'd better just
		 * give up now. */
		if (dir == NULL) {
			UNLOCK();
			return NSS_STATUS_NOTFOUND;
		}
	}

	do {
		/* If we don't have a current file, try to open the next file
		 * in the directory. */
		if ((fp == NULL) || feof(fp)) {
			if (fp != NULL) {
				fclose(fp);
				fp = NULL;
			}

			do {
				/* Read the next entry in the directory. */
				ent = readdir(dir);
				if (ent == NULL) {
					closedir(dir);
					dir = NULL;
					continue;
				}

				/* If the file has a certain name, skip it. */
				if (skip_file_by_name(ent->d_name)) {
					continue;
				}

				/* Formulate the full path name and try to
				 * open it. */
				snprintf(path, sizeof(path),
					 SYSCONFDIR "/" FILENAME ".d/%s",
					 ent->d_name);
				fp = fopen(path, "r");

				/* If we failed to open the file, move on. */
				if (fp == NULL) {
					continue;
				}

				/* If we can't stat() the file, move on. */
				if (fstat(fileno(fp), &st) != 0) {
					fclose(fp);
					fp = NULL;
					continue;
				}

				/* If the file isn't normal or a symlink,
				 * move on. */
				if (!S_ISREG(st.st_mode) &&
				    !S_ISLNK(st.st_mode)) {
					fclose(fp);
					fp = NULL;
					continue;
				}
			} while ((dir != NULL) && (fp == NULL));
		}

		/* If we're out of data, return NOTFOUND. */
		if ((dir == NULL) || (fp == NULL)) {
			UNLOCK();
			return NSS_STATUS_NOTFOUND;
		}

		/* Read a line from the file. */
		line = read_line(fp);
		if (line == NULL) {
			fclose(fp);
			fp = NULL;
			continue;
		}

		/* Try to parse the line. */
		if (parser(line, &structure,
			   (struct parser_data*) buffer, buflen,
			   errnop) != 0) {
			free(line);
			*result = structure;
			UNLOCK();
			return NSS_STATUS_SUCCESS;
		}

		/* Try the next entry. */
		free(line);
	} while (1);

	/* We never really get here, but oh well. */
	UNLOCK();
	return NSS_STATUS_UNAVAIL;
}
#endif

#ifdef IMPLEMENT_MAIN
int
main(int argc, char **argv)
{
	struct passwd pwd;
	char buffer[LINE_MAX];
	int error;
	if (getnum(500, &pwd, buffer, sizeof(buffer),
		   &error) == NSS_STATUS_SUCCESS) {
		printf("%s:%ld\n", pwd.pw_name, (long) pwd.pw_uid);
	}
	while (getent(&pwd, buffer, sizeof(buffer),
		      &error) == NSS_STATUS_SUCCESS) {
		printf("%s:%ld\n", pwd.pw_name, (long) pwd.pw_uid);
	}
	return 0;
}
#endif