1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
46290107bSBill Paul * Copyright (c) 1995, 1996
56290107bSBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
66290107bSBill Paul *
76290107bSBill Paul * Redistribution and use in source and binary forms, with or without
86290107bSBill Paul * modification, are permitted provided that the following conditions
96290107bSBill Paul * are met:
106290107bSBill Paul * 1. Redistributions of source code must retain the above copyright
116290107bSBill Paul * notice, this list of conditions and the following disclaimer.
126290107bSBill Paul * 2. Redistributions in binary form must reproduce the above copyright
136290107bSBill Paul * notice, this list of conditions and the following disclaimer in the
146290107bSBill Paul * documentation and/or other materials provided with the distribution.
156290107bSBill Paul * 3. All advertising materials mentioning features or use of this software
166290107bSBill Paul * must display the following acknowledgement:
176290107bSBill Paul * This product includes software developed by Bill Paul.
186290107bSBill Paul * 4. Neither the name of the author nor the names of any co-contributors
196290107bSBill Paul * may be used to endorse or promote products derived from this software
206290107bSBill Paul * without specific prior written permission.
216290107bSBill Paul *
226290107bSBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
236290107bSBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
246290107bSBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
256290107bSBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
266290107bSBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
276290107bSBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
286290107bSBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296290107bSBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306290107bSBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
316290107bSBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326290107bSBill Paul * SUCH DAMAGE.
336290107bSBill Paul */
346290107bSBill Paul
35b728350eSDavid E. O'Brien #include <sys/cdefs.h>
36e7f0e9caSPhilippe Charnier #include <err.h>
376290107bSBill Paul #include <fcntl.h>
38e7f0e9caSPhilippe Charnier #include <limits.h>
398a0e4ea1SDimitry Andric #include <stdint.h>
40e7f0e9caSPhilippe Charnier #include <stdio.h>
41e7f0e9caSPhilippe Charnier #include <stdlib.h>
42e7f0e9caSPhilippe Charnier #include <string.h>
43e7f0e9caSPhilippe Charnier #include <time.h>
44e7f0e9caSPhilippe Charnier #include <unistd.h>
45e7f0e9caSPhilippe Charnier #include <rpc/rpc.h>
46e7f0e9caSPhilippe Charnier #include <rpcsvc/yp.h>
476290107bSBill Paul #include <sys/param.h>
486290107bSBill Paul #include <sys/types.h>
496290107bSBill Paul #include <sys/stat.h>
506290107bSBill Paul #include "yp_extern.h"
517de38547SBill Paul #include "ypxfr_extern.h"
526290107bSBill Paul
536290107bSBill Paul char *yp_dir = ""; /* No particular default needed. */
546290107bSBill Paul int debug = 1;
556290107bSBill Paul
56dc584ddbSDag-Erling Smørgrav static void
usage(void)57dc584ddbSDag-Erling Smørgrav usage(void)
586290107bSBill Paul {
59e7f0e9caSPhilippe Charnier fprintf(stderr, "%s\n%s\n%s\n%s\n",
60e7f0e9caSPhilippe Charnier "usage: yp_mkdb -c",
61e7f0e9caSPhilippe Charnier " yp_mkdb -u dbname",
62b22e036eSBill Paul " yp_mkdb [-c] [-b] [-s] [-f] [-i inputfile] [-o outputfile]",
63e7f0e9caSPhilippe Charnier " [-d domainname ] [-m mastername] inputfile dbname");
646290107bSBill Paul exit(1);
656290107bSBill Paul }
666290107bSBill Paul
676290107bSBill Paul #define PERM_SECURE (S_IRUSR|S_IWUSR)
68dc584ddbSDag-Erling Smørgrav static DB *
open_db(char * path,int flags)69dc584ddbSDag-Erling Smørgrav open_db(char *path, int flags)
706290107bSBill Paul {
716290107bSBill Paul extern HASHINFO openinfo;
726290107bSBill Paul
736290107bSBill Paul return(dbopen(path, flags, PERM_SECURE, DB_HASH, &openinfo));
746290107bSBill Paul }
756290107bSBill Paul
76dc584ddbSDag-Erling Smørgrav static void
unwind(char * map)77dc584ddbSDag-Erling Smørgrav unwind(char *map)
786290107bSBill Paul {
796290107bSBill Paul DB *dbp;
806290107bSBill Paul DBT key, data;
816290107bSBill Paul
826290107bSBill Paul dbp = open_db(map, O_RDONLY);
836290107bSBill Paul
846290107bSBill Paul if (dbp == NULL)
856290107bSBill Paul err(1, "open_db(%s) failed", map);
866290107bSBill Paul
876290107bSBill Paul key.data = NULL;
886290107bSBill Paul while (yp_next_record(dbp, &key, &data, 1, 1) == YP_TRUE)
89*dd7660e5SDimitry Andric printf("%.*s %.*s\n", (int)key.size, (char *)key.data,
90*dd7660e5SDimitry Andric (int)data.size, (char *)data.data);
916290107bSBill Paul
926290107bSBill Paul (void)(dbp->close)(dbp);
936290107bSBill Paul }
946290107bSBill Paul
95dc584ddbSDag-Erling Smørgrav int
main(int argc,char * argv[])96dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[])
976290107bSBill Paul {
986290107bSBill Paul int ch;
996290107bSBill Paul int un = 0;
1006290107bSBill Paul int clear = 0;
101b22e036eSBill Paul int filter_plusminus = 0;
1026290107bSBill Paul char *infile = NULL;
1036290107bSBill Paul char *map = NULL;
1046290107bSBill Paul char *domain = NULL;
1056290107bSBill Paul char *infilename = NULL;
1066290107bSBill Paul char *outfilename = NULL;
1076290107bSBill Paul char *mastername = NULL;
10821c2d66cSBill Paul int interdom = 0;
10921c2d66cSBill Paul int secure = 0;
1106290107bSBill Paul DB *dbp;
1116290107bSBill Paul DBT key, data;
1126290107bSBill Paul char buf[10240];
1136290107bSBill Paul char *keybuf, *datbuf;
1146290107bSBill Paul FILE *ifp;
1156290107bSBill Paul char hname[MAXHOSTNAMELEN + 2];
1166290107bSBill Paul
117c6b85633SSheldon Hearn while ((ch = getopt(argc, argv, "uhcbsfd:i:o:m:")) != -1) {
1186290107bSBill Paul switch (ch) {
119b22e036eSBill Paul case 'f':
120b22e036eSBill Paul filter_plusminus++;
121b22e036eSBill Paul break;
1226290107bSBill Paul case 'u':
1236290107bSBill Paul un++;
1246290107bSBill Paul break;
1256290107bSBill Paul case 'c':
1266290107bSBill Paul clear++;
1276290107bSBill Paul break;
12821c2d66cSBill Paul case 'b':
12921c2d66cSBill Paul interdom++;
13021c2d66cSBill Paul break;
13121c2d66cSBill Paul case 's':
13221c2d66cSBill Paul secure++;
13321c2d66cSBill Paul break;
1346290107bSBill Paul case 'd':
1356290107bSBill Paul domain = optarg;
1366290107bSBill Paul break;
1376290107bSBill Paul case 'i':
1386290107bSBill Paul infilename = optarg;
1396290107bSBill Paul break;
1406290107bSBill Paul case 'o':
1416290107bSBill Paul outfilename = optarg;
1426290107bSBill Paul break;
1436290107bSBill Paul case 'm':
1446290107bSBill Paul mastername = optarg;
1456290107bSBill Paul break;
1466290107bSBill Paul case 'h':
1476290107bSBill Paul default:
1486290107bSBill Paul usage();
1496290107bSBill Paul break;
1506290107bSBill Paul }
1516290107bSBill Paul }
1526290107bSBill Paul
1536290107bSBill Paul argc -= optind;
1546290107bSBill Paul argv += optind;
1556290107bSBill Paul
1566290107bSBill Paul if (un) {
1576290107bSBill Paul map = argv[0];
1586290107bSBill Paul if (map == NULL)
1596290107bSBill Paul usage();
1606290107bSBill Paul unwind(map);
1616290107bSBill Paul exit(0);
1626290107bSBill Paul
1636290107bSBill Paul }
1646290107bSBill Paul
1656290107bSBill Paul infile = argv[0];
1666290107bSBill Paul map = argv[1];
1676290107bSBill Paul
1686290107bSBill Paul if (infile == NULL || map == NULL) {
1696290107bSBill Paul if (clear)
1706290107bSBill Paul goto doclear;
1716290107bSBill Paul usage();
1726290107bSBill Paul }
1736290107bSBill Paul
1746290107bSBill Paul if (mastername == NULL) {
1756290107bSBill Paul if (gethostname((char *)&hname, sizeof(hname)) == -1)
1766290107bSBill Paul err(1, "gethostname() failed");
1776290107bSBill Paul mastername = (char *)&hname;
1786290107bSBill Paul }
1796290107bSBill Paul
1806290107bSBill Paul /*
1816290107bSBill Paul * Note that while we can read from stdin, we can't
1826290107bSBill Paul * write to stdout; the db library doesn't let you
1836290107bSBill Paul * write to a file stream like that.
1846290107bSBill Paul */
1856290107bSBill Paul if (!strcmp(infile, "-")) {
1866290107bSBill Paul ifp = stdin;
1876290107bSBill Paul } else {
1886290107bSBill Paul if ((ifp = fopen(infile, "r")) == NULL)
1896290107bSBill Paul err(1, "failed to open %s", infile);
1906290107bSBill Paul }
1916290107bSBill Paul
1926290107bSBill Paul if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL)
1936290107bSBill Paul err(1, "open_db(%s) failed", map);
1946290107bSBill Paul
19521c2d66cSBill Paul if (interdom) {
19621c2d66cSBill Paul key.data = "YP_INTERDOMAIN";
19721c2d66cSBill Paul key.size = sizeof("YP_INTERDOMAIN") - 1;
19821c2d66cSBill Paul data.data = "";
19921c2d66cSBill Paul data.size = 0;
20021c2d66cSBill Paul yp_put_record(dbp, &key, &data, 0);
20121c2d66cSBill Paul }
20221c2d66cSBill Paul
20321c2d66cSBill Paul if (secure) {
20421c2d66cSBill Paul key.data = "YP_SECURE";
20521c2d66cSBill Paul key.size = sizeof("YP_SECURE") - 1;
20621c2d66cSBill Paul data.data = "";
20721c2d66cSBill Paul data.size = 0;
20821c2d66cSBill Paul yp_put_record(dbp, &key, &data, 0);
20921c2d66cSBill Paul }
21021c2d66cSBill Paul
2116290107bSBill Paul key.data = "YP_MASTER_NAME";
2126290107bSBill Paul key.size = sizeof("YP_MASTER_NAME") - 1;
2136290107bSBill Paul data.data = mastername;
2146290107bSBill Paul data.size = strlen(mastername);
2157de38547SBill Paul yp_put_record(dbp, &key, &data, 0);
2166290107bSBill Paul
2176290107bSBill Paul key.data = "YP_LAST_MODIFIED";
2186290107bSBill Paul key.size = sizeof("YP_LAST_MODIFIED") - 1;
2198a0e4ea1SDimitry Andric snprintf(buf, sizeof(buf), "%jd", (intmax_t)time(NULL));
2207de38547SBill Paul data.data = (char *)&buf;
2216290107bSBill Paul data.size = strlen(buf);
2227de38547SBill Paul yp_put_record(dbp, &key, &data, 0);
2236290107bSBill Paul
2246290107bSBill Paul if (infilename) {
2256290107bSBill Paul key.data = "YP_INPUT_FILE";
2266290107bSBill Paul key.size = sizeof("YP_INPUT_FILE") - 1;
2276290107bSBill Paul data.data = infilename;
2286290107bSBill Paul data.size = strlen(infilename);
2297de38547SBill Paul yp_put_record(dbp, &key, &data, 0);
2306290107bSBill Paul }
2316290107bSBill Paul
2326290107bSBill Paul if (outfilename) {
2336290107bSBill Paul key.data = "YP_OUTPUT_FILE";
2346290107bSBill Paul key.size = sizeof("YP_OUTPUT_FILE") - 1;
2356290107bSBill Paul data.data = outfilename;
2366290107bSBill Paul data.size = strlen(outfilename);
2377de38547SBill Paul yp_put_record(dbp, &key, &data, 0);
2386290107bSBill Paul }
2396290107bSBill Paul
2406290107bSBill Paul if (domain) {
2416290107bSBill Paul key.data = "YP_DOMAIN_NAME";
2426290107bSBill Paul key.size = sizeof("YP_DOMAIN_NAME") - 1;
2436290107bSBill Paul data.data = domain;
2446290107bSBill Paul data.size = strlen(domain);
2457de38547SBill Paul yp_put_record(dbp, &key, &data, 0);
2466290107bSBill Paul }
2476290107bSBill Paul
2486290107bSBill Paul while (fgets((char *)&buf, sizeof(buf), ifp)) {
2496290107bSBill Paul char *sep = NULL;
2506290107bSBill Paul int rval;
2516290107bSBill Paul
2526290107bSBill Paul /* NUL terminate */
2536290107bSBill Paul if ((sep = strchr(buf, '\n')))
2546290107bSBill Paul *sep = '\0';
2556290107bSBill Paul
2566290107bSBill Paul /* handle backslash line continuations */
2576290107bSBill Paul while (buf[strlen(buf) - 1] == '\\') {
2586290107bSBill Paul fgets((char *)&buf[strlen(buf) - 1],
2596290107bSBill Paul sizeof(buf) - strlen(buf), ifp);
2606290107bSBill Paul if ((sep = strchr(buf, '\n')))
2616290107bSBill Paul *sep = '\0';
2626290107bSBill Paul }
2636290107bSBill Paul
2646290107bSBill Paul /* find the separation between the key and data */
2656290107bSBill Paul if ((sep = strpbrk(buf, " \t")) == NULL) {
2666290107bSBill Paul warnx("bad input -- no white space: %s", buf);
2676290107bSBill Paul continue;
2686290107bSBill Paul }
2696290107bSBill Paul
2706290107bSBill Paul /* separate the strings */
2716290107bSBill Paul keybuf = (char *)&buf;
2726290107bSBill Paul datbuf = sep + 1;
2736290107bSBill Paul *sep = '\0';
2746290107bSBill Paul
2756290107bSBill Paul /* set datbuf to start at first non-whitespace character */
2766290107bSBill Paul while (*datbuf == ' ' || *datbuf == '\t')
2776290107bSBill Paul datbuf++;
2786290107bSBill Paul
2796290107bSBill Paul /* Check for silliness. */
280b22e036eSBill Paul if (filter_plusminus) {
2816290107bSBill Paul if (*keybuf == '+' || *keybuf == '-' ||
2826290107bSBill Paul *datbuf == '+' || *datbuf == '-') {
283b22e036eSBill Paul warnx("bad character at "
284b22e036eSBill Paul "start of line: %s", buf);
2856290107bSBill Paul continue;
2866290107bSBill Paul }
287b22e036eSBill Paul }
2886290107bSBill Paul
2896290107bSBill Paul if (strlen(keybuf) > YPMAXRECORD) {
2906290107bSBill Paul warnx("key too long: %s", keybuf);
2916290107bSBill Paul continue;
2926290107bSBill Paul }
2936290107bSBill Paul
294157f1019SBill Paul if (!strlen(keybuf)) {
295157f1019SBill Paul warnx("no key -- check source file for blank lines");
296157f1019SBill Paul continue;
297157f1019SBill Paul }
298157f1019SBill Paul
2996290107bSBill Paul if (strlen(datbuf) > YPMAXRECORD) {
3006290107bSBill Paul warnx("data too long: %s", datbuf);
3016290107bSBill Paul continue;
3026290107bSBill Paul }
3036290107bSBill Paul
3046290107bSBill Paul key.data = keybuf;
3056290107bSBill Paul key.size = strlen(keybuf);
3066290107bSBill Paul data.data = datbuf;
3076290107bSBill Paul data.size = strlen(datbuf);
3086290107bSBill Paul
3097de38547SBill Paul if ((rval = yp_put_record(dbp, &key, &data, 0)) != YP_TRUE) {
3106290107bSBill Paul switch (rval) {
3116290107bSBill Paul case YP_FALSE:
3126290107bSBill Paul warnx("duplicate key '%s' - skipping", keybuf);
3136290107bSBill Paul break;
3146290107bSBill Paul case YP_BADDB:
3156290107bSBill Paul default:
3166290107bSBill Paul err(1,"failed to write new record - exiting");
3176290107bSBill Paul break;
3186290107bSBill Paul }
3196290107bSBill Paul }
3206290107bSBill Paul
3216290107bSBill Paul }
3226290107bSBill Paul
3236290107bSBill Paul (void)(dbp->close)(dbp);
3246290107bSBill Paul
3256290107bSBill Paul doclear:
3266290107bSBill Paul if (clear) {
3276290107bSBill Paul char in = 0;
3286290107bSBill Paul char *out = NULL;
3296290107bSBill Paul int stat;
3306290107bSBill Paul if ((stat = callrpc("localhost", YPPROG,YPVERS, YPPROC_CLEAR,
331f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_void, &in,
332f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_void, out)) != RPC_SUCCESS) {
3336290107bSBill Paul warnx("failed to send 'clear' to local ypserv: %s",
3346290107bSBill Paul clnt_sperrno((enum clnt_stat) stat));
3356290107bSBill Paul }
3366290107bSBill Paul }
3376290107bSBill Paul
3386290107bSBill Paul exit(0);
3396290107bSBill Paul }
340