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