Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / ypserv / makedbm / makedbm.c
blob3a28e2d2c52fda1fd84369a37b453a1b4ee16d4a
1 /* $NetBSD: makedbm.c,v 1.23 2009/04/19 06:06:40 lukem Exp $ */
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: makedbm.c,v 1.23 2009/04/19 06:06:40 lukem Exp $");
32 #endif
34 #include <sys/param.h>
35 #include <sys/stat.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
46 #include <rpc/rpc.h>
47 #include <rpc/xdr.h>
49 #include "protos.h"
50 #include "ypdb.h"
51 #include "ypdef.h"
53 int main(int, char *[]);
54 void usage(void);
55 int add_record(DBM *, const char *, const char *, int);
56 char *file_date(char *);
57 void list_database(char *);
58 void create_database(char *, char *, char *, char *, char *, char *,
59 int, int, int);
61 int
62 main(int argc, char *argv[])
64 int aflag, uflag, bflag, lflag, sflag;
65 char *yp_input_file, *yp_output_file;
66 char *yp_master_name, *yp_domain_name;
67 char *infile, *outfile;
68 int ch;
70 yp_input_file = yp_output_file = NULL;
71 yp_master_name = yp_domain_name = NULL;
72 aflag = uflag = bflag = lflag = sflag = 0;
73 infile = outfile = NULL;
75 while ((ch = getopt(argc, argv, "blsui:o:m:d:")) != -1) {
76 switch (ch) {
77 case 'b':
78 bflag = aflag = 1;
79 break;
81 case 'l':
82 lflag = aflag = 1;
83 break;
85 case 's':
86 sflag = aflag = 1;
87 break;
89 case 'i':
90 yp_input_file = optarg;
91 aflag = 1;
92 break;
94 case 'o':
95 yp_output_file = optarg;
96 aflag = 1;
97 break;
99 case 'm':
100 yp_master_name = optarg;
101 aflag = 1;
102 break;
104 case 'd':
105 yp_domain_name = optarg;
106 aflag = 1;
107 break;
109 case 'u':
110 uflag = 1;
111 break;
113 default:
114 usage();
117 argc -= optind; argv += optind;
119 if ((uflag != 0) && (aflag != 0))
120 usage();
121 else {
122 if (uflag != 0) {
123 if (argc == 1)
124 infile = argv[0];
125 else
126 usage();
127 } else {
128 if (argc == 2) {
129 infile = argv[0];
130 outfile = argv[1];
131 } else
132 usage();
136 if (uflag != 0)
137 list_database(infile);
138 else
139 create_database(infile, outfile,
140 yp_input_file, yp_output_file, yp_master_name,
141 yp_domain_name, bflag, lflag, sflag);
143 exit(0);
147 add_record(DBM *db, const char *str1, const char *str2, int check)
149 datum key, val;
150 int status;
152 key.dptr = __UNCONST(str1);
153 key.dsize = strlen(str1);
155 if (check) {
156 val = ypdb_fetch(db, key);
158 if (val.dptr != NULL)
159 return 0; /* already there */
161 val.dptr = __UNCONST(str2);
162 val.dsize = strlen(str2);
163 status = ypdb_store(db, key, val, YPDB_INSERT);
165 if (status != 0) {
166 warnx("can't store `%s %s'", str1, str2);
167 return -1;
169 return 0;
172 char *
173 file_date(char *filename)
175 struct stat finfo;
176 static char datestr[11];
178 memset(datestr, 0, sizeof(datestr));
180 if (strcmp(filename, "-") == 0)
181 snprintf(datestr, sizeof(datestr), "%010d",
182 (int)time(NULL));
183 else {
184 if (stat(filename, &finfo) != 0)
185 err(1, "can't stat %s", filename);
186 snprintf(datestr, sizeof(datestr), "%010d",
187 (int)finfo.st_mtime);
190 return datestr;
193 void
194 list_database(char *database)
196 DBM *db;
197 datum key, val;
199 db = ypdb_open(database);
200 if (db == NULL)
201 err(1, "can't open database `%s'", database);
203 key = ypdb_firstkey(db);
205 while (key.dptr != NULL) {
206 val = ypdb_fetch(db, key);
207 /* workround trailing \0 in aliases.db */
208 if (key.dptr[key.dsize - 1] == '\0')
209 key.dsize--;
210 printf("%*.*s", key.dsize, key.dsize, key.dptr);
211 if (val.dsize > 0) {
212 if (val.dptr[val.dsize - 1] == '\0')
213 val.dsize--;
214 printf(" %*.*s", val.dsize, val.dsize, val.dptr);
216 printf("\n");
217 key = ypdb_nextkey(db);
220 ypdb_close(db);
223 void
224 create_database(char *infile, char *database, char *yp_input_file,
225 char *yp_output_file, char *yp_master_name,
226 char *yp_domain_name, int bflag, int lflag, int sflag)
228 FILE *data_file;
229 char myname[MAXHOSTNAMELEN];
230 size_t line_no = 0;
231 size_t len;
232 char *p, *k, *v, *slash;
233 DBM *new_db;
234 static const char template[] = "ypdbXXXXXX";
235 char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
236 char empty_str[] = "";
238 memset(db_mapname, 0, sizeof(db_mapname));
239 memset(db_outfile, 0, sizeof(db_outfile));
241 if (strcmp(infile, "-") == 0)
242 data_file = stdin;
243 else {
244 data_file = fopen(infile, "r");
245 if (data_file == NULL)
246 err(1, "can't open `%s'", infile);
249 if (strlen(database) + strlen(YPDB_SUFFIX) > (sizeof(db_outfile) - 1))
250 errx(1, "file name `%s' too long", database);
252 snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
254 slash = strrchr(database, '/');
255 if (slash != NULL)
256 slash[1] = '\0'; /* truncate to dir */
257 else
258 *database = '\0'; /* eliminate */
260 /* NOTE: database is now directory where map goes ! */
262 if (strlen(database) + strlen(template) + strlen(YPDB_SUFFIX) >
263 (sizeof(db_mapname) - 1))
264 errx(1, "directory name `%s' too long", database);
266 snprintf(db_mapname, sizeof(db_mapname), "%s%s",
267 database, template);
269 new_db = ypdb_mktemp(db_mapname);
270 if (new_db == NULL)
271 err(1, "can't create temp database `%s'", db_mapname);
273 for (;
274 (p = fparseln(data_file, &len, &line_no, NULL, FPARSELN_UNESCALL));
275 free(p)) {
276 k = p; /* set start of key */
277 while (*k && isspace((unsigned char)*k)) /* skip leading whitespace */
278 k++;
280 if (! *k)
281 continue;
283 v = k;
284 while (*v && !isspace((unsigned char)*v)) { /* find leading whitespace */
285 /* convert key to lower case if forcing. */
286 if (lflag && isupper((unsigned char)*v))
287 *v = tolower((unsigned char)*v);
288 v++;
290 while (*v && isspace((unsigned char)*v)) /* replace space with <NUL> */
291 *v++ = '\0';
293 if (add_record(new_db, k, v, TRUE)) { /* save record */
294 bad_record:
295 ypdb_close(new_db);
296 unlink(db_mapname);
297 errx(1, "error adding record for line %lu",
298 (unsigned long)line_no);
302 if (strcmp(infile, "-") != 0)
303 (void) fclose(data_file);
305 if (add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE))
306 goto bad_record;
308 if (yp_input_file) {
309 if (add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE))
310 goto bad_record;
313 if (yp_output_file) {
314 if (add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE))
315 goto bad_record;
318 if (yp_master_name) {
319 if (add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE))
320 goto bad_record;
321 } else {
322 localhostname(myname, sizeof(myname) - 1);
323 if (add_record(new_db, YP_MASTER_KEY, myname, FALSE))
324 goto bad_record;
327 if (yp_domain_name) {
328 if (add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE))
329 goto bad_record;
332 if (bflag) {
333 if (add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE))
334 goto bad_record;
337 if (sflag) {
338 if (add_record(new_db, YP_SECURE_KEY, empty_str, FALSE))
339 goto bad_record;
342 ypdb_close(new_db);
343 if (rename(db_mapname, db_outfile) < 0) {
344 unlink(db_mapname);
345 err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
349 void
350 usage(void)
353 fprintf(stderr, "usage: %s -u file\n", getprogname());
354 fprintf(stderr, " %s [-lbs] %s\n", getprogname(),
355 "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
356 fprintf(stderr, " %s infile outfile\n",
357 "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
358 exit(1);