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