Home | History | Annotate | Line # | Download | only in makedbm
makedbm.c revision 1.4
      1 /*	$NetBSD: makedbm.c,v 1.4 1997/09/08 03:18:19 mikel 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/param.h>
     35 #include <sys/stat.h>
     36 
     37 #include <ctype.h>
     38 #include <err.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <stdio.h>
     42 #include <strings.h>
     43 #include <unistd.h>
     44 
     45 #include <rpc/rpc.h>
     46 #include <rpc/xdr.h>
     47 
     48 #include "protos.h"
     49 #include "ypdb.h"
     50 #include "ypdef.h"
     51 
     52 extern	char *__progname;		/* from crt0.o */
     53 extern	char *optarg;
     54 extern	int optind;
     55 
     56 int	main __P((int, char *[]));
     57 void	usage __P((void));
     58 void	add_record __P((DBM *, char *, char *, int));
     59 char	*file_date __P((char *));
     60 void	list_database __P((char *));
     61 void	create_database __P((char *, char *, char *, char *,
     62 	    char *, char *, int, int, int));
     63 
     64 int
     65 main(argc, argv)
     66 	int argc;
     67 	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 void
    152 add_record(db, str1, str2, check)
    153 	DBM *db;
    154 	char *str1, *str2;
    155 	int check;
    156 {
    157 	datum key, val;
    158 	int status;
    159 
    160 	key.dptr = str1;
    161 	key.dsize = strlen(str1);
    162 
    163 	if (check) {
    164 		val = ypdb_fetch(db, key);
    165 
    166 		if (val.dptr != NULL)
    167 			return;	/* already there */
    168 	}
    169 	val.dptr = str2;
    170 	val.dsize = strlen(str2);
    171 	status = ypdb_store(db, key, val, YPDB_INSERT);
    172 
    173 	if (status != 0)
    174 		errx(1, "can't store `%s %s'", str1, str2);
    175 }
    176 
    177 char *
    178 file_date(filename)
    179 	char *filename;
    180 {
    181 	struct stat finfo;
    182 	static char datestr[11];
    183 
    184 	memset(datestr, 0, sizeof(datestr));
    185 
    186 	if (strcmp(filename, "-") == 0)
    187 		snprintf(datestr, sizeof(datestr), "%010d",
    188 		    (int)time(NULL));
    189 	else {
    190 		if (stat(filename, &finfo) != 0)
    191 			err(1, "can't stat %s", filename);
    192 		snprintf(datestr, sizeof(datestr), "%010d",
    193 		    (int)finfo.st_mtime);
    194 	}
    195 
    196 	return datestr;
    197 }
    198 
    199 void
    200 list_database(database)
    201 	char *database;
    202 {
    203 	DBM *db;
    204 	datum key, val;
    205 
    206 	db = ypdb_open(database, O_RDONLY, 0444);
    207 	if (db == NULL)
    208 		errx(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 		printf("%*.*s %*.*s\n",
    215 		    key.dsize, key.dsize, key.dptr,
    216 		    val.dsize, val.dsize, val.dptr);
    217 		key = ypdb_nextkey(db);
    218 	}
    219 
    220 	ypdb_close(db);
    221 }
    222 
    223 void
    224 create_database(infile, database, yp_input_file, yp_output_file,
    225     yp_master_name, yp_domain_name, bflag, lflag, sflag)
    226 	char *infile, *database, *yp_input_file, *yp_output_file;
    227 	char *yp_master_name, *yp_domain_name;
    228 	int bflag, lflag, sflag;
    229 {
    230 	FILE *data_file;
    231 	char data_line[4096];	/* XXX: DB bsize = 4096 in ypdb.c */
    232 	char myname[MAXHOSTNAMELEN];
    233 	int line_no = 0;
    234 	int len;
    235 	char *p, *k, *v, *slash;
    236 	DBM *new_db;
    237 	static char mapname[] = "ypdbXXXXXX";
    238 	char db_mapname[MAXPATHLEN + 1], db_outfile[MAXPATHLEN + 1];
    239 	char db_tempname[MAXPATHLEN + 1];
    240 	char empty_str[] = "";
    241 
    242 	memset(db_mapname, 0, sizeof(db_mapname));
    243 	memset(db_outfile, 0, sizeof(db_outfile));
    244 	memset(db_tempname, 0, sizeof(db_tempname));
    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) > MAXPATHLEN)
    255 		errx(1, "%s: file name too long\n", 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';	/* elminate */
    264 
    265 	/* NOTE: database is now directory where map goes ! */
    266 
    267 	if (strlen(database) + strlen(mapname) +
    268 	    strlen(YPDB_SUFFIX) > MAXPATHLEN)
    269 		errx(1, "%s: directory name too long", database);
    270 
    271 	snprintf(db_tempname, sizeof(db_tempname), "%s%s",
    272 	    database, mapname);
    273 	mktemp(db_tempname);
    274 	snprintf(db_mapname, sizeof(db_mapname), "%s%s",
    275 	    db_tempname, YPDB_SUFFIX);
    276 
    277 	new_db = ypdb_open(db_tempname, O_RDWR | O_CREAT | O_EXCL, 0644);
    278 	if (new_db == NULL)
    279 		errx(1, "can't create temp database `%s'", db_tempname);
    280 
    281 	while (read_line(data_file, data_line, sizeof(data_line))) {
    282 		line_no++;
    283 		len = strlen(data_line);
    284 
    285 		/* Check if we have the whole line */
    286 
    287 		if (data_line[len - 1] != '\n')
    288 			warnx("%s: line %d too long", infile, line_no);
    289 		else
    290 			data_line[len - 1] = '\0';
    291 
    292 		p = (char *)&data_line;
    293 
    294 		k = p;		/* save start of key */
    295 		while (!isspace(*p)) {	/* find first "space" */
    296 			/*
    297 			 * Convert to lower case if forcing.
    298 			 */
    299 			if (lflag && isupper(*p))
    300 				*p = tolower(*p);
    301 			p++;
    302 		}
    303 		while (isspace(*p))	/* replace space with <NUL> */
    304 			*p++ = '\0';
    305 
    306 		v = p;			/* save start of value */
    307 
    308 		while (*p != '\0')	/* find end of string */
    309 			p++;
    310 
    311 		add_record(new_db, k, v, TRUE);	/* save record */
    312 	}
    313 
    314 	if (strcmp(infile, "-") != 0)
    315 		(void) fclose(data_file);
    316 
    317 	add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE);
    318 
    319 	if (yp_input_file)
    320 		add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE);
    321 
    322 	if (yp_output_file)
    323 		add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE);
    324 
    325 	if (yp_master_name)
    326 		add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE);
    327 	else {
    328 		localhostname(myname, sizeof(myname) - 1);
    329 		add_record(new_db, YP_MASTER_KEY, myname, FALSE);
    330 	}
    331 
    332 	if (yp_domain_name)
    333 		add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE);
    334 
    335 	if (bflag)
    336 		add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE);
    337 
    338 	if (sflag)
    339 		add_record(new_db, YP_SECURE_KEY, empty_str, FALSE);
    340 
    341 	ypdb_close(new_db);
    342 	if (rename(db_mapname, db_outfile) < 0)
    343 		err(1, "rename `%s' -> `%s'", db_mapname, db_outfile);
    344 }
    345 
    346 void
    347 usage()
    348 {
    349 
    350 	fprintf(stderr, "usage: %s -u file\n", __progname);
    351 	fprintf(stderr, "       %s [-lbs] %s\n", __progname,
    352 	    "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
    353 	fprintf(stderr, "          %s infile outfile\n",
    354 	    "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");
    355 	exit(1);
    356 }
    357