2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
46 #include <rpcsvc/yp.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
50 #include "yp_extern.h"
51 #include "ypxfr_extern.h"
53 char *yp_dir = ""; /* No particular default needed. */
59 fprintf(stderr, "%s\n%s\n%s\n%s\n",
62 " yp_mkdb [-c] [-b] [-s] [-f] [-i inputfile] [-o outputfile]",
63 " [-d domainname ] [-m mastername] inputfile dbname");
67 #define PERM_SECURE (S_IRUSR|S_IWUSR)
69 open_db(char *path, int flags)
71 extern HASHINFO openinfo;
73 return(dbopen(path, flags, PERM_SECURE, DB_HASH, &openinfo));
82 dbp = open_db(map, O_RDONLY);
85 err(1, "open_db(%s) failed", map);
88 while (yp_next_record(dbp, &key, &data, 1, 1) == YP_TRUE)
89 printf("%.*s %.*s\n", (int)key.size, (char *)key.data,
90 (int)data.size, (char *)data.data);
92 (void)(dbp->close)(dbp);
96 main(int argc, char *argv[])
101 int filter_plusminus = 0;
105 char *infilename = NULL;
106 char *outfilename = NULL;
107 char *mastername = NULL;
113 char *keybuf, *datbuf;
115 char hname[MAXHOSTNAMELEN + 2];
117 while ((ch = getopt(argc, argv, "uhcbsfd:i:o:m:")) != -1) {
141 outfilename = optarg;
168 if (infile == NULL || map == NULL) {
174 if (mastername == NULL) {
175 if (gethostname((char *)&hname, sizeof(hname)) == -1)
176 err(1, "gethostname() failed");
177 mastername = (char *)&hname;
181 * Note that while we can read from stdin, we can't
182 * write to stdout; the db library doesn't let you
183 * write to a file stream like that.
185 if (!strcmp(infile, "-")) {
188 if ((ifp = fopen(infile, "r")) == NULL)
189 err(1, "failed to open %s", infile);
192 if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL)
193 err(1, "open_db(%s) failed", map);
196 key.data = "YP_INTERDOMAIN";
197 key.size = sizeof("YP_INTERDOMAIN") - 1;
200 yp_put_record(dbp, &key, &data, 0);
204 key.data = "YP_SECURE";
205 key.size = sizeof("YP_SECURE") - 1;
208 yp_put_record(dbp, &key, &data, 0);
211 key.data = "YP_MASTER_NAME";
212 key.size = sizeof("YP_MASTER_NAME") - 1;
213 data.data = mastername;
214 data.size = strlen(mastername);
215 yp_put_record(dbp, &key, &data, 0);
217 key.data = "YP_LAST_MODIFIED";
218 key.size = sizeof("YP_LAST_MODIFIED") - 1;
219 snprintf(buf, sizeof(buf), "%jd", (intmax_t)time(NULL));
220 data.data = (char *)&buf;
221 data.size = strlen(buf);
222 yp_put_record(dbp, &key, &data, 0);
225 key.data = "YP_INPUT_FILE";
226 key.size = sizeof("YP_INPUT_FILE") - 1;
227 data.data = infilename;
228 data.size = strlen(infilename);
229 yp_put_record(dbp, &key, &data, 0);
233 key.data = "YP_OUTPUT_FILE";
234 key.size = sizeof("YP_OUTPUT_FILE") - 1;
235 data.data = outfilename;
236 data.size = strlen(outfilename);
237 yp_put_record(dbp, &key, &data, 0);
241 key.data = "YP_DOMAIN_NAME";
242 key.size = sizeof("YP_DOMAIN_NAME") - 1;
244 data.size = strlen(domain);
245 yp_put_record(dbp, &key, &data, 0);
248 while (fgets((char *)&buf, sizeof(buf), ifp)) {
253 if ((sep = strchr(buf, '\n')))
256 /* handle backslash line continuations */
257 while (buf[strlen(buf) - 1] == '\\') {
258 fgets((char *)&buf[strlen(buf) - 1],
259 sizeof(buf) - strlen(buf), ifp);
260 if ((sep = strchr(buf, '\n')))
264 /* find the separation between the key and data */
265 if ((sep = strpbrk(buf, " \t")) == NULL) {
266 warnx("bad input -- no white space: %s", buf);
270 /* separate the strings */
271 keybuf = (char *)&buf;
275 /* set datbuf to start at first non-whitespace character */
276 while (*datbuf == ' ' || *datbuf == '\t')
279 /* Check for silliness. */
280 if (filter_plusminus) {
281 if (*keybuf == '+' || *keybuf == '-' ||
282 *datbuf == '+' || *datbuf == '-') {
283 warnx("bad character at "
284 "start of line: %s", buf);
289 if (strlen(keybuf) > YPMAXRECORD) {
290 warnx("key too long: %s", keybuf);
294 if (!strlen(keybuf)) {
295 warnx("no key -- check source file for blank lines");
299 if (strlen(datbuf) > YPMAXRECORD) {
300 warnx("data too long: %s", datbuf);
305 key.size = strlen(keybuf);
307 data.size = strlen(datbuf);
309 if ((rval = yp_put_record(dbp, &key, &data, 0)) != YP_TRUE) {
312 warnx("duplicate key '%s' - skipping", keybuf);
316 err(1,"failed to write new record - exiting");
323 (void)(dbp->close)(dbp);
330 if ((stat = callrpc("localhost", YPPROG,YPVERS, YPPROC_CLEAR,
331 (xdrproc_t)xdr_void, &in,
332 (xdrproc_t)xdr_void, out)) != RPC_SUCCESS) {
333 warnx("failed to send 'clear' to local ypserv: %s",
334 clnt_sperrno((enum clnt_stat) stat));