Home | History | Annotate | Line # | Download | only in makedbm
makedbm.c revision 1.18
      1 /*	$NetBSD: makedbm.c,v 1.18 2002/07/06 21:33:25 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
      5  * All rights reserved.
      6  *
      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.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Mats O Jansson
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: makedbm.c,v 1.18 2002/07/06 21:33:25 wiz Exp $");
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/stat.h>
     41 
     42 #include <ctype.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <fcntl.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <time.h>
     50 #include <unistd.h>
     51 #include <util.h>
     52 
     53 #include <rpc/rpc.h>
     54 #include <rpc/xdr.h>
     55 
     56 #include "protos.h"
     57 #include "ypdb.h"
     58 #include "ypdef.h"
     59 
     60 int	main(int, char *[]);
     61 void	usage(void);
     62 int	add_record(DBM *, char *, char *, int);
     63 char	*file_date(char *);
     64 void	list_database(char *);
     65 void	create_database(char *, char *, char *, char *, char *, char *,
     66 			int, int, int);
     67 
     68 int
     69 main(int argc, char *argv[])
     70 {
     71 	int aflag, uflag, bflag, lflag, sflag;
     72 	char *yp_input_file, *yp_output_file;
     73 	char *yp_master_name, *yp_domain_name;
     74 	char *infile, *outfile;
     75 	int ch;
     76 
     77 	yp_input_file = yp_output_file = NULL;
     78 	yp_master_name = yp_domain_name = NULL;
     79 	aflag = uflag = bflag = lflag = sflag = 0;
     80 	infile = outfile = NULL;
     81 
     82 	while ((ch = getopt(argc, argv, "blsui:o:m:d:")) != -1) {
     83 		switch (ch) {
     84 		case 'b':
     85 			bflag = aflag = 1;
     86 			break;
     87 
     88 		case 'l':
     89 			lflag = aflag = 1;
     90 			break;
     91 
     92 		case 's':
     93 			sflag = aflag = 1;
     94 			break;
     95 
     96 		case 'i':
     97 			yp_input_file = optarg;
     98 			aflag = 1;
     99 			break;
    100 
    101 		case 'o':
    102 			yp_output_file = optarg;
    103 			aflag = 1;
    104 			break;
    105 
    106 		case 'm':
    107 			yp_master_name = optarg;
    108 			aflag = 1;
    109 			break;
    110 
    111 		case 'd':
    112 			yp_domain_name = optarg;
    113 			aflag = 1;
    114 			break;
    115 
    116 		case 'u':
    117 			uflag = 1;
    118 			break;
    119 
    120 		default:
    121 			usage();
    122 		}
    123 	}
    124 	argc -= optind; argv += optind;
    125 
    126 	if ((uflag != 0) && (aflag != 0))
    127 		usage();
    128 	else {
    129 		if (uflag != 0) {
    130 			if (argc == 1)
    131 				infile = argv[0];
    132 			else
    133 				usage();
    134 		} else {
    135 			if (argc == 2) {
    136 				infile = argv[0];
    137 				outfile = argv[1];
    138 			} else
    139 				usage();
    140 		}
    141 	}
    142 
    143 	if (uflag != 0)
    144 		list_database(infile);
    145 	else
    146 		create_database(infile, outfile,
    147 		    yp_input_file, yp_output_file, yp_master_name,
    148 		    yp_domain_name, bflag, lflag, sflag);
    149 
    150 	exit(0);
    151 }
    152 
    153 int
    154 add_record(DBM *db, char *str1, char *str2, int check)
    155 {
    156 	datum key, val;
    157 	int status;
    158 
    159 	key.dptr = str1;
    160 	key.dsize = strlen(str1);
    161 
    162 	if (check) {
    163 		val = ypdb_fetch(db, key);
    164 
    165 		if (val.dptr != NULL)
    166 			return 0;	/* already there */
    167 	}
    168 	val.dptr = str2;
    169 	val.dsize = strlen(str2);
    170 	status = ypdb_store(db, key, val, YPDB_INSERT);
    171 
    172 	if (status != 0) {
    173 		warnx("can't store `%s %s'", str1, str2);
    174 		return -1;
    175 	}
    176 	return 0;
    177 }
    178 
    179 char *
    180 file_date(char *filename)
    181 {
    182 	struct stat finfo;
    183 	static char datestr[11];
    184 
    185 	memset(datestr, 0, sizeof(datestr));
    186 
    187 	if (strcmp(filename, "-") == 0)
    188 		snprintf(datestr, sizeof(datestr), "%010d",
    189 		    (int)time(NULL));
    190 	else {
    191 		if (stat(filename, &finfo) != 0)
    192 			err(1, "can't stat %s", filename);
    193 		snprintf(datestr, sizeof(datestr), "%010d",
    194 		    (int)finfo.st_mtime);
    195 	}
    196 
    197 	return datestr;
    198 }
    199 
    200 void
    201 list_database(char *database)
    202 {
    203 	DBM *db;
    204 	datum key, val;
    205 
    206 	db = ypdb_open(database, O_RDONLY, 0444);
    207 	if (db == NULL)
    208 		err(1, "can't open database `%s'", database);
    209 
    210 	key = ypdb_firstkey(db);
    211 
    212 	while (key.dptr != NULL) {
    213 		val = ypdb_fetch(db, key);
    214 				/* workround trailing \0 in aliases.db */
    215 		if (key.dptr[key.dsize - 1] == '\0')
    216 			key.dsize--;
    217 		printf("%*.*s", key.dsize, key.dsize, key.dptr);
    218 		if (val.dsize > 0) {
    219 			if (val.dptr[val.dsize - 1] == '\0')
    220 				val.dsize--;
    221 			printf(" %*.*s", val.dsize, val.dsize, val.dptr);
    222 		}
    223 		printf("\n");
    224 		key = ypdb_nextkey(db);
    225 	}
    226 
    227 	ypdb_close(db);
    228 }
    229 
    230 void
    231 create_database(char *infile, char *database, char *yp_input_file,
    232 		char *yp_output_file, char *yp_master_name,
    233 		char *yp_domain_name, int bflag, int lflag, int sflag)
    234 {
    235 	FILE *data_file;
    236 	char myname[MAXHOSTNAMELEN];
    237 	size_t line_no = 0;
    238 	size_t len;
    239 	char *p, *k, *v, *slash;
    240 	DBM *new_db;
    241 	static char mapname[] = "ypdbXXXXXX";
    242 	char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
    243 	char db_tempname[MAXPATHLEN + 1];
    244 	char empty_str[] = "";
    245 
    246 	memset(db_mapname, 0, sizeof(db_mapname));
    247 	memset(db_outfile, 0, sizeof(db_outfile));
    248 	memset(db_tempname, 0, sizeof(db_tempname));
    249 
    250 	if (strcmp(infile, "-") == 0)
    251 		data_file = stdin;
    252 	else {
    253 		data_file = fopen(infile, "r");
    254 		if (data_file == NULL)
    255 			err(1, "can't open `%s'", infile);
    256 	}
    257 
    258 	if (strlen(database) + strlen(YPDB_SUFFIX) > MAXPATHLEN)
    259 		errx(1, "file name `%s' too long", database);
    260 
    261 	snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
    262 
    263 	slash = strrchr(database, '/');
    264 	if (slash != NULL)
    265 		slash[1] = '\0';	/* truncate to dir */
    266 	else
    267 		*database = '\0';	/* eliminate */
    268 
    269 	/* NOTE: database is now directory where map goes ! */
    270 
    271 	if (strlen(database) + strlen(mapname) +
    272 	    strlen(YPDB_SUFFIX) > MAXPATHLEN)
    273 		errx(1, "directory name `%s' too long", database);
    274 
    275 	snprintf(db_tempname, sizeof(db_tempname), "%s%s",
    276 	    database, mapname);
    277 	mktemp(db_tempname);	/* OK */
    278 	snprintf(db_mapname, sizeof(db_mapname), "%s%s",
    279 	    db_tempname, YPDB_SUFFIX);
    280 
    281 	new_db = ypdb_open(db_tempname, O_RDWR | O_CREAT | O_EXCL, 0644);
    282 	if (new_db == NULL)
    283 		err(1, "can't create temp database `%s'", db_tempname);
    284 
    285 	for (;
    286 	    (p = fparseln(data_file, &len, &line_no, NULL, FPARSELN_UNESCALL));
    287 	    free(p)) {
    288 		k = p;				/* set start of key */
    289 		while (*k && isspace(*k))	/* skip leading whitespace */
    290 			k++;
    291 
    292 		if (! *k)
    293 			continue;
    294 
    295 		v = k;
    296 		while (*v && !isspace(*v)) {	/* find leading whitespace */
    297 				/* convert key to lower case if forcing. */
    298 			if (lflag && isupper(*v))
    299 				*v = tolower(*v);
    300 			v++;
    301 		}
    302 		while (*v && isspace(*v))	/* replace space with <NUL> */
    303 			*v++ = '\0';
    304 
    305 		if (add_record(new_db, k, v, TRUE)) {    /* save record */
    306 bad_record:
    307 			ypdb_close(new_db);
    308 			unlink(db_mapname);
    309 			errx(1, "error adding record for line %lu",
    310 			    (unsigned long)line_no);
    311 		}
    312 	}
    313 
    314 	if (strcmp(infile, "-") != 0)
    315 		(void) fclose(data_file);
    316 
    317 	if (add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE))
    318 		goto bad_record;
    319 
    320 	if (yp_input_file) {
    321 		if (add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE))
    322 			goto bad_record;
    323 	}
    324 
    325 	if (yp_output_file) {
    326 		if (add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE))
    327 			goto bad_record;
    328 	}
    329 
    330 	if (yp_master_name) {
    331 		if (add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE))
    332 			goto bad_record;
    333 	} else {
    334 		localhostname(myname, sizeof(myname) - 1);
    335 		if (add_record(new_db, YP_MASTER_KEY, myname, FALSE))
    336 			goto bad_record;
    337 	}
    338 
    339 	if (yp_domain_name) {
    340 		if (add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE))
    341 			goto bad_record;
    342 	}
    343 
    344 	if (bflag) {
    345 		if (add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE))
    346 			goto bad_record;
    347 	}
    348 
    349 	if (sflag) {
    350 		if (add_record(new_db, YP_SECURE_KEY, empty_str, FALSE))
    351 			goto bad_record;
    352 	}
    353 
    354 	ypdb_close(new_db);
    355 	if (rename(db_mapname, db_outfile) < 0) {
    356 		unlink(db_mapname);
    357 		err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
    358 	}
    359 }
    360 
    361 void
    362 usage(void)
    363 {
    364 
    365 	fprintf(stderr, "usage: %s -u file\n", getprogname());
    366 	fprintf(stderr, "       %s [-lbs] %s\n", getprogname(),
    367 	    "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
    368 	fprintf(stderr, "          %s infile outfile\n",
    369 	    "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
    370 	exit(1);
    371 }
    372