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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "updatedb.h"

/* Return a sensible exit code so success == 0 */
static int nss_err2exitcode(enum nss_status status)
{
	int serr;

	switch (status) {
	case NSS_STATUS_SUCCESS:
		serr = 0;
		break;
	case NSS_STATUS_NOTFOUND:
		serr = 1;
		break;
	case NSS_STATUS_UNAVAIL:
		serr = 2;
		break;
	case NSS_STATUS_TRYAGAIN:
		serr = 3;
		break;
	default:
		serr = 4;
		break;
	}

	return serr;
}

static void usage(void)
{
	fprintf(stderr, "Usage: nss_updatedb [nameservice] [passwd|group]\n");
	exit(nss_err2exitcode(NSS_STATUS_UNAVAIL));
}

static const char *nss_err2string(enum nss_status status)
{
	const char *serr;

	switch (status) {
	case NSS_STATUS_SUCCESS:
		serr = "success";
		break;
	case NSS_STATUS_TRYAGAIN:
		serr = "out of memory";
		break;
	case NSS_STATUS_UNAVAIL:
		serr = "nameservice unavailable";
		break;
	case NSS_STATUS_NOTFOUND:
		serr = "not found";
		break;
	default:
		serr = "unknown error";
		break;
	}

	return serr;
}

int main(int argc, char *argv[])
{
	nss_backend_handle_t *handle;
	unsigned maps = 0;
	char *dbname;
	enum nss_status status;

	if (argc < 2 || argc > 3) {
		usage();
	}

	dbname = argv[1];
	if (strcmp(dbname, "db") == 0) {
		fprintf(stderr, "Cannot run nss_updatedb against nss_db.\n");
		exit(nss_err2exitcode(NSS_STATUS_UNAVAIL));
	}

	if (argc == 3) {
		char *mapname;

		mapname = argv[2];

		if (strcmp(mapname, "passwd") == 0)
			maps = MAP_PASSWD;
		else if (strcmp(mapname, "group") == 0)
			maps = MAP_GROUP;
		else
			usage();
	} else {
		maps = MAP_ALL;
	}

	status = nss_backend_open(dbname, &handle);
	if (status != NSS_STATUS_SUCCESS) {
		exit(nss_err2exitcode(status));
	}

	if (maps & MAP_PASSWD) {
		printf("passwd... ");
		status = nss_update_db(handle, MAP_PASSWD, DB_PASSWD);
		if (status != NSS_STATUS_SUCCESS) {
			printf("%s.\n", nss_err2string(status));
			exit(nss_err2exitcode(status));
		}
		printf("done.\n");
	}

	if (maps & MAP_GROUP) {
		printf("group... ");
		status = nss_update_db(handle, MAP_GROUP, DB_GROUP);
		if (status != NSS_STATUS_SUCCESS) {
			printf("%s.\n", nss_err2string(status));
			exit(nss_err2exitcode(status));
		}
		printf("done.\n");
	}

	status = nss_backend_close(&handle);
	if (status != NSS_STATUS_SUCCESS) {
		printf("%s.\n", nss_err2string(status));
		exit(nss_err2exitcode(status));
	}
	return 0;
}