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