Home | History | Annotate | Line # | Download | only in makedbm
makedbm.c revision 1.22
      1 /*	$NetBSD: makedbm.c,v 1.22 2008/02/29 03:00:47 lukem 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.22 2008/02/29 03:00:47 lukem 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 <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <time.h>
     49 #include <unistd.h>
     50 
     51 #include <rpc/rpc.h>
     52 #include <rpc/xdr.h>
     53 
     54 #include "protos.h"
     55 #include "ypdb.h"
     56 #include "ypdef.h"
     57 
     58 int	main(int, char *[]);
     59 void	usage(void);
     60 int	add_record(DBM *, char *, char *, int);
     61 char	*file_date(char *);
     62 void	list_database(char *);
     63 void	create_database(char *, char *, char *, char *, char *, char *,
     64 			int, int, int);
     65 
     66 int
     67 main(int argc, char *argv[])
     68 {
     69 	int aflag, uflag, bflag, lflag, sflag;
     70 	char *yp_input_file, *yp_output_file;
     71 	char *yp_master_name, *yp_domain_name;
     72 	char *infile, *outfile;
     73 	int ch;
     74 
     75 	yp_input_file = yp_output_file = NULL;
     76 	yp_master_name = yp_domain_name = NULL;
     77 	aflag = uflag = bflag = lflag = sflag = 0;
     78 	infile = outfile = NULL;
     79 
     80 	while ((ch = getopt(argc, argv, "blsui:o:m:d:")) != -1) {
     81 		switch (ch) {
     82 		case 'b':
     83 			bflag = aflag = 1;
     84 			break;
     85 
     86 		case 'l':
     87 			lflag = aflag = 1;
     88 			break;
     89 
     90 		case 's':
     91 			sflag = aflag = 1;
     92 			break;
     93 
     94 		case 'i':
     95 			yp_input_file = optarg;
     96 			aflag = 1;
     97 			break;
     98 
     99 		case 'o':
    100 			yp_output_file = optarg;
    101 			aflag = 1;
    102 			break;
    103 
    104 		case 'm':
    105 			yp_master_name = optarg;
    106 			aflag = 1;
    107 			break;
    108 
    109 		case 'd':
    110 			yp_domain_name = optarg;
    111 			aflag = 1;
    112 			break;
    113 
    114 		case 'u':
    115 			uflag = 1;
    116 			break;
    117 
    118 		default:
    119 			usage();
    120 		}
    121 	}
    122 	argc -= optind; argv += optind;
    123 
    124 	if ((uflag != 0) && (aflag != 0))
    125 		usage();
    126 	else {
    127 		if (uflag != 0) {
    128 			if (argc == 1)
    129 				infile = argv[0];
    130 			else
    131 				usage();
    132 		} else {
    133 			if (argc == 2) {
    134 				infile = argv[0];
    135 				outfile = argv[1];
    136 			} else
    137 				usage();
    138 		}
    139 	}
    140 
    141 	if (uflag != 0)
    142 		list_database(infile);
    143 	else
    144 		create_database(infile, outfile,
    145 		    yp_input_file, yp_output_file, yp_master_name,
    146 		    yp_domain_name, bflag, lflag, sflag);
    147 
    148 	exit(0);
    149 }
    150 
    151 int
    152 add_record(DBM *db, char *str1, char *str2, int check)
    153 {
    154 	datum key, val;
    155 	int status;
    156 
    157 	key.dptr = str1;
    158 	key.dsize = strlen(str1);
    159 
    160 	if (check) {
    161 		val = ypdb_fetch(db, key);
    162 
    163 		if (val.dptr != NULL)
    164 			return 0;	/* already there */
    165 	}
    166 	val.dptr = str2;
    167 	val.dsize = strlen(str2);
    168 	status = ypdb_store(db, key, val, YPDB_INSERT);
    169 
    170 	if (status != 0) {
    171 		warnx("can't store `%s %s'", str1, str2);
    172 		return -1;
    173 	}
    174 	return 0;
    175 }
    176 
    177 char *
    178 file_date(char *filename)
    179 {
    180 	struct stat finfo;
    181 	static char datestr[11];
    182 
    183 	memset(datestr, 0, sizeof(datestr));
    184 
    185 	if (strcmp(filename, "-") == 0)
    186 		snprintf(datestr, sizeof(datestr), "%010d",
    187 		    (int)time(NULL));
    188 	else {
    189 		if (stat(filename, &finfo) != 0)
    190 			err(1, "can't stat %s", filename);
    191 		snprintf(datestr, sizeof(datestr), "%010d",
    192 		    (int)finfo.st_mtime);
    193 	}
    194 
    195 	return datestr;
    196 }
    197 
    198 void
    199 list_database(char *database)
    200 {
    201 	DBM *db;
    202 	datum key, val;
    203 
    204 	db = ypdb_open(database);
    205 	if (db == NULL)
    206 		err(1, "can't open database `%s'", database);
    207 
    208 	key = ypdb_firstkey(db);
    209 
    210 	while (key.dptr != NULL) {
    211 		val = ypdb_fetch(db, key);
    212 				/* workround trailing \0 in aliases.db */
    213 		if (key.dptr[key.dsize - 1] == '\0')
    214 			key.dsize--;
    215 		printf("%*.*s", key.dsize, key.dsize, key.dptr);
    216 		if (val.dsize > 0) {
    217 			if (val.dptr[val.dsize - 1] == '\0')
    218 				val.dsize--;
    219 			printf(" %*.*s", val.dsize, val.dsize, val.dptr);
    220 		}
    221 		printf("\n");
    222 		key = ypdb_nextkey(db);
    223 	}
    224 
    225 	ypdb_close(db);
    226 }
    227 
    228 void
    229 create_database(char *infile, char *database, char *yp_input_file,
    230 		char *yp_output_file, char *yp_master_name,
    231 		char *yp_domain_name, int bflag, int lflag, int sflag)
    232 {
    233 	FILE *data_file;
    234 	char myname[MAXHOSTNAMELEN];
    235 	size_t line_no = 0;
    236 	size_t len;
    237 	char *p, *k, *v, *slash;
    238 	DBM *new_db;
    239 	static const char template[] = "ypdbXXXXXX";
    240 	char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
    241 	char empty_str[] = "";
    242 
    243 	memset(db_mapname, 0, sizeof(db_mapname));
    244 	memset(db_outfile, 0, sizeof(db_outfile));
    245 
    246 	if (strcmp(infile, "-") == 0)
    247 		data_file = stdin;
    248 	else {
    249 		data_file = fopen(infile, "r");
    250 		if (data_file == NULL)
    251 			err(1, "can't open `%s'", infile);
    252 	}
    253 
    254 	if (strlen(database) + strlen(YPDB_SUFFIX) > (sizeof(db_outfile) - 1))
    255 		errx(1, "file name `%s' too long", database);
    256 
    257 	snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
    258 
    259 	slash = strrchr(database, '/');
    260 	if (slash != NULL)
    261 		slash[1] = '\0';	/* truncate to dir */
    262 	else
    263 		*database = '\0';	/* eliminate */
    264 
    265 	/* NOTE: database is now directory where map goes ! */
    266 
    267 	if (strlen(database) + strlen(template) + strlen(YPDB_SUFFIX) >
    268 	    (sizeof(db_mapname) - 1))
    269 		errx(1, "directory name `%s' too long", database);
    270 
    271 	snprintf(db_mapname, sizeof(db_mapname), "%s%s",
    272 	    database, template);
    273 
    274 	new_db = ypdb_mktemp(db_mapname);
    275 	if (new_db == NULL)
    276 		err(1, "can't create temp database `%s'", db_mapname);
    277 
    278 	for (;
    279 	    (p = fparseln(data_file, &len, &line_no, NULL, FPARSELN_UNESCALL));
    280 	    free(p)) {
    281 		k = p;				/* set start of key */
    282 		while (*k && isspace((unsigned char)*k)) /* skip leading whitespace */
    283 			k++;
    284 
    285 		if (! *k)
    286 			continue;
    287 
    288 		v = k;
    289 		while (*v && !isspace((unsigned char)*v)) {	/* find leading whitespace */
    290 				/* convert key to lower case if forcing. */
    291 			if (lflag && isupper((unsigned char)*v))
    292 				*v = tolower((unsigned char)*v);
    293 			v++;
    294 		}
    295 		while (*v && isspace((unsigned char)*v))	/* replace space with <NUL> */
    296 			*v++ = '\0';
    297 
    298 		if (add_record(new_db, k, v, TRUE)) {    /* save record */
    299 bad_record:
    300 			ypdb_close(new_db);
    301 			unlink(db_mapname);
    302 			errx(1, "error adding record for line %lu",
    303 			    (unsigned long)line_no);
    304 		}
    305 	}
    306 
    307 	if (strcmp(infile, "-") != 0)
    308 		(void) fclose(data_file);
    309 
    310 	if (add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE))
    311 		goto bad_record;
    312 
    313 	if (yp_input_file) {
    314 		if (add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE))
    315 			goto bad_record;
    316 	}
    317 
    318 	if (yp_output_file) {
    319 		if (add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE))
    320 			goto bad_record;
    321 	}
    322 
    323 	if (yp_master_name) {
    324 		if (add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE))
    325 			goto bad_record;
    326 	} else {
    327 		localhostname(myname, sizeof(myname) - 1);
    328 		if (add_record(new_db, YP_MASTER_KEY, myname, FALSE))
    329 			goto bad_record;
    330 	}
    331 
    332 	if (yp_domain_name) {
    333 		if (add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE))
    334 			goto bad_record;
    335 	}
    336 
    337 	if (bflag) {
    338 		if (add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE))
    339 			goto bad_record;
    340 	}
    341 
    342 	if (sflag) {
    343 		if (add_record(new_db, YP_SECURE_KEY, empty_str, FALSE))
    344 			goto bad_record;
    345 	}
    346 
    347 	ypdb_close(new_db);
    348 	if (rename(db_mapname, db_outfile) < 0) {
    349 		unlink(db_mapname);
    350 		err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
    351 	}
    352 }
    353 
    354 void
    355 usage(void)
    356 {
    357 
    358 	fprintf(stderr, "usage: %s -u file\n", getprogname());
    359 	fprintf(stderr, "       %s [-lbs] %s\n", getprogname(),
    360 	    "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
    361 	fprintf(stderr, "          %s infile outfile\n",
    362 	    "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
    363 	exit(1);
    364 }
    365