1 /* 2 * Copyright (c) 1995, 1996 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <err.h> 37 #include <fcntl.h> 38 #include <limits.h> 39 #include <stdint.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <time.h> 44 #include <unistd.h> 45 #include <rpc/rpc.h> 46 #include <rpcsvc/yp.h> 47 #include <sys/param.h> 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include "yp_extern.h" 51 #include "ypxfr_extern.h" 52 53 char *yp_dir = ""; /* No particular default needed. */ 54 int debug = 1; 55 56 static void 57 usage(void) 58 { 59 fprintf(stderr, "%s\n%s\n%s\n%s\n", 60 "usage: yp_mkdb -c", 61 " yp_mkdb -u dbname", 62 " yp_mkdb [-c] [-b] [-s] [-f] [-i inputfile] [-o outputfile]", 63 " [-d domainname ] [-m mastername] inputfile dbname"); 64 exit(1); 65 } 66 67 #define PERM_SECURE (S_IRUSR|S_IWUSR) 68 static DB * 69 open_db(char *path, int flags) 70 { 71 extern HASHINFO openinfo; 72 73 return(dbopen(path, flags, PERM_SECURE, DB_HASH, &openinfo)); 74 } 75 76 static void 77 unwind(char *map) 78 { 79 DB *dbp; 80 DBT key, data; 81 82 dbp = open_db(map, O_RDONLY); 83 84 if (dbp == NULL) 85 err(1, "open_db(%s) failed", map); 86 87 key.data = NULL; 88 while (yp_next_record(dbp, &key, &data, 1, 1) == YP_TRUE) 89 printf("%.*s %.*s\n", (int)key.size, key.data, (int)data.size, 90 data.data); 91 92 (void)(dbp->close)(dbp); 93 return; 94 } 95 96 int 97 main(int argc, char *argv[]) 98 { 99 int ch; 100 int un = 0; 101 int clear = 0; 102 int filter_plusminus = 0; 103 char *infile = NULL; 104 char *map = NULL; 105 char *domain = NULL; 106 char *infilename = NULL; 107 char *outfilename = NULL; 108 char *mastername = NULL; 109 int interdom = 0; 110 int secure = 0; 111 DB *dbp; 112 DBT key, data; 113 char buf[10240]; 114 char *keybuf, *datbuf; 115 FILE *ifp; 116 char hname[MAXHOSTNAMELEN + 2]; 117 118 while ((ch = getopt(argc, argv, "uhcbsfd:i:o:m:")) != -1) { 119 switch (ch) { 120 case 'f': 121 filter_plusminus++; 122 break; 123 case 'u': 124 un++; 125 break; 126 case 'c': 127 clear++; 128 break; 129 case 'b': 130 interdom++; 131 break; 132 case 's': 133 secure++; 134 break; 135 case 'd': 136 domain = optarg; 137 break; 138 case 'i': 139 infilename = optarg; 140 break; 141 case 'o': 142 outfilename = optarg; 143 break; 144 case 'm': 145 mastername = optarg; 146 break; 147 case 'h': 148 default: 149 usage(); 150 break; 151 } 152 } 153 154 argc -= optind; 155 argv += optind; 156 157 if (un) { 158 map = argv[0]; 159 if (map == NULL) 160 usage(); 161 unwind(map); 162 exit(0); 163 164 } 165 166 infile = argv[0]; 167 map = argv[1]; 168 169 if (infile == NULL || map == NULL) { 170 if (clear) 171 goto doclear; 172 usage(); 173 } 174 175 if (mastername == NULL) { 176 if (gethostname((char *)&hname, sizeof(hname)) == -1) 177 err(1, "gethostname() failed"); 178 mastername = (char *)&hname; 179 } 180 181 /* 182 * Note that while we can read from stdin, we can't 183 * write to stdout; the db library doesn't let you 184 * write to a file stream like that. 185 */ 186 if (!strcmp(infile, "-")) { 187 ifp = stdin; 188 } else { 189 if ((ifp = fopen(infile, "r")) == NULL) 190 err(1, "failed to open %s", infile); 191 } 192 193 if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL) 194 err(1, "open_db(%s) failed", map); 195 196 if (interdom) { 197 key.data = "YP_INTERDOMAIN"; 198 key.size = sizeof("YP_INTERDOMAIN") - 1; 199 data.data = ""; 200 data.size = 0; 201 yp_put_record(dbp, &key, &data, 0); 202 } 203 204 if (secure) { 205 key.data = "YP_SECURE"; 206 key.size = sizeof("YP_SECURE") - 1; 207 data.data = ""; 208 data.size = 0; 209 yp_put_record(dbp, &key, &data, 0); 210 } 211 212 key.data = "YP_MASTER_NAME"; 213 key.size = sizeof("YP_MASTER_NAME") - 1; 214 data.data = mastername; 215 data.size = strlen(mastername); 216 yp_put_record(dbp, &key, &data, 0); 217 218 key.data = "YP_LAST_MODIFIED"; 219 key.size = sizeof("YP_LAST_MODIFIED") - 1; 220 snprintf(buf, sizeof(buf), "%jd", (intmax_t)time(NULL)); 221 data.data = (char *)&buf; 222 data.size = strlen(buf); 223 yp_put_record(dbp, &key, &data, 0); 224 225 if (infilename) { 226 key.data = "YP_INPUT_FILE"; 227 key.size = sizeof("YP_INPUT_FILE") - 1; 228 data.data = infilename; 229 data.size = strlen(infilename); 230 yp_put_record(dbp, &key, &data, 0); 231 } 232 233 if (outfilename) { 234 key.data = "YP_OUTPUT_FILE"; 235 key.size = sizeof("YP_OUTPUT_FILE") - 1; 236 data.data = outfilename; 237 data.size = strlen(outfilename); 238 yp_put_record(dbp, &key, &data, 0); 239 } 240 241 if (domain) { 242 key.data = "YP_DOMAIN_NAME"; 243 key.size = sizeof("YP_DOMAIN_NAME") - 1; 244 data.data = domain; 245 data.size = strlen(domain); 246 yp_put_record(dbp, &key, &data, 0); 247 } 248 249 while (fgets((char *)&buf, sizeof(buf), ifp)) { 250 char *sep = NULL; 251 int rval; 252 253 /* NUL terminate */ 254 if ((sep = strchr(buf, '\n'))) 255 *sep = '\0'; 256 257 /* handle backslash line continuations */ 258 while (buf[strlen(buf) - 1] == '\\') { 259 fgets((char *)&buf[strlen(buf) - 1], 260 sizeof(buf) - strlen(buf), ifp); 261 if ((sep = strchr(buf, '\n'))) 262 *sep = '\0'; 263 } 264 265 /* find the separation between the key and data */ 266 if ((sep = strpbrk(buf, " \t")) == NULL) { 267 warnx("bad input -- no white space: %s", buf); 268 continue; 269 } 270 271 /* separate the strings */ 272 keybuf = (char *)&buf; 273 datbuf = sep + 1; 274 *sep = '\0'; 275 276 /* set datbuf to start at first non-whitespace character */ 277 while (*datbuf == ' ' || *datbuf == '\t') 278 datbuf++; 279 280 /* Check for silliness. */ 281 if (filter_plusminus) { 282 if (*keybuf == '+' || *keybuf == '-' || 283 *datbuf == '+' || *datbuf == '-') { 284 warnx("bad character at " 285 "start of line: %s", buf); 286 continue; 287 } 288 } 289 290 if (strlen(keybuf) > YPMAXRECORD) { 291 warnx("key too long: %s", keybuf); 292 continue; 293 } 294 295 if (!strlen(keybuf)) { 296 warnx("no key -- check source file for blank lines"); 297 continue; 298 } 299 300 if (strlen(datbuf) > YPMAXRECORD) { 301 warnx("data too long: %s", datbuf); 302 continue; 303 } 304 305 key.data = keybuf; 306 key.size = strlen(keybuf); 307 data.data = datbuf; 308 data.size = strlen(datbuf); 309 310 if ((rval = yp_put_record(dbp, &key, &data, 0)) != YP_TRUE) { 311 switch (rval) { 312 case YP_FALSE: 313 warnx("duplicate key '%s' - skipping", keybuf); 314 break; 315 case YP_BADDB: 316 default: 317 err(1,"failed to write new record - exiting"); 318 break; 319 } 320 } 321 322 } 323 324 (void)(dbp->close)(dbp); 325 326 doclear: 327 if (clear) { 328 char in = 0; 329 char *out = NULL; 330 int stat; 331 if ((stat = callrpc("localhost", YPPROG,YPVERS, YPPROC_CLEAR, 332 (xdrproc_t)xdr_void, &in, 333 (xdrproc_t)xdr_void, out)) != RPC_SUCCESS) { 334 warnx("failed to send 'clear' to local ypserv: %s", 335 clnt_sperrno((enum clnt_stat) stat)); 336 } 337 } 338 339 exit(0); 340 } 341