2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 static const char rcsid[] =
47 #include <rpcsvc/yp.h>
48 #include <sys/param.h>
49 #include <sys/types.h>
51 #include "yp_extern.h"
52 #include "ypxfr_extern.h"
54 char *yp_dir = ""; /* No particular default needed. */
61 fprintf(stderr, "%s\n%s\n%s\n%s\n",
64 " yp_mkdb [-c] [-b] [-s] [-f] [-i inputfile] [-o outputfile]",
65 " [-d domainname ] [-m mastername] inputfile dbname");
69 #define PERM_SECURE (S_IRUSR|S_IWUSR)
72 open_db(char *path, int flags)
74 extern HASHINFO openinfo;
76 return(dbopen(path, flags, PERM_SECURE, DB_HASH, &openinfo));
85 dbp = open_db(map, O_RDONLY);
88 err(1, "open_db(%s) failed", map);
91 while (yp_next_record(dbp, &key, &data, 1, 1) == YP_TRUE)
92 printf("%.*s %.*s\n", key.size,key.data,data.size,data.data);
94 (void)(dbp->close)(dbp);
99 main(int argc, char *argv[])
104 int filter_plusminus = 0;
108 char *infilename = NULL;
109 char *outfilename = NULL;
110 char *mastername = NULL;
116 char *keybuf, *datbuf;
118 char hname[MAXHOSTNAMELEN + 2];
120 while ((ch = getopt(argc, argv, "uhcbsfd:i:o:m:")) != -1) {
144 outfilename = optarg;
171 if (infile == NULL || map == NULL) {
177 if (mastername == NULL) {
178 if (gethostname((char *)&hname, sizeof(hname)) == -1)
179 err(1, "gethostname() failed");
180 mastername = (char *)&hname;
184 * Note that while we can read from stdin, we can't
185 * write to stdout; the db library doesn't let you
186 * write to a file stream like that.
189 if (!strcmp(infile, "-")) {
192 if ((ifp = fopen(infile, "r")) == NULL)
193 err(1, "failed to open %s", infile);
196 if ((dbp = open_db(map, O_RDWR|O_EXLOCK|O_EXCL|O_CREAT)) == NULL)
197 err(1, "open_db(%s) failed", map);
200 key.data = "YP_INTERDOMAIN";
201 key.size = sizeof("YP_INTERDOMAIN") - 1;
204 yp_put_record(dbp, &key, &data, 0);
208 key.data = "YP_SECURE";
209 key.size = sizeof("YP_SECURE") - 1;
212 yp_put_record(dbp, &key, &data, 0);
215 key.data = "YP_MASTER_NAME";
216 key.size = sizeof("YP_MASTER_NAME") - 1;
217 data.data = mastername;
218 data.size = strlen(mastername);
219 yp_put_record(dbp, &key, &data, 0);
221 key.data = "YP_LAST_MODIFIED";
222 key.size = sizeof("YP_LAST_MODIFIED") - 1;
223 snprintf(buf, sizeof(buf), "%lu", time(NULL));
224 data.data = (char *)&buf;
225 data.size = strlen(buf);
226 yp_put_record(dbp, &key, &data, 0);
229 key.data = "YP_INPUT_FILE";
230 key.size = sizeof("YP_INPUT_FILE") - 1;
231 data.data = infilename;
232 data.size = strlen(infilename);
233 yp_put_record(dbp, &key, &data, 0);
237 key.data = "YP_OUTPUT_FILE";
238 key.size = sizeof("YP_OUTPUT_FILE") - 1;
239 data.data = outfilename;
240 data.size = strlen(outfilename);
241 yp_put_record(dbp, &key, &data, 0);
245 key.data = "YP_DOMAIN_NAME";
246 key.size = sizeof("YP_DOMAIN_NAME") - 1;
248 data.size = strlen(domain);
249 yp_put_record(dbp, &key, &data, 0);
252 while (fgets((char *)&buf, sizeof(buf), ifp)) {
257 if ((sep = strchr(buf, '\n')))
260 /* handle backslash line continuations */
261 while (buf[strlen(buf) - 1] == '\\') {
262 fgets((char *)&buf[strlen(buf) - 1],
263 sizeof(buf) - strlen(buf), ifp);
264 if ((sep = strchr(buf, '\n')))
268 /* find the separation between the key and data */
269 if ((sep = strpbrk(buf, " \t")) == NULL) {
270 warnx("bad input -- no white space: %s", buf);
274 /* separate the strings */
275 keybuf = (char *)&buf;
279 /* set datbuf to start at first non-whitespace character */
280 while (*datbuf == ' ' || *datbuf == '\t')
283 /* Check for silliness. */
284 if (filter_plusminus) {
285 if (*keybuf == '+' || *keybuf == '-' ||
286 *datbuf == '+' || *datbuf == '-') {
287 warnx("bad character at "
288 "start of line: %s", buf);
293 if (strlen(keybuf) > YPMAXRECORD) {
294 warnx("key too long: %s", keybuf);
298 if (!strlen(keybuf)) {
299 warnx("no key -- check source file for blank lines");
303 if (strlen(datbuf) > YPMAXRECORD) {
304 warnx("data too long: %s", datbuf);
309 key.size = strlen(keybuf);
311 data.size = strlen(datbuf);
313 if ((rval = yp_put_record(dbp, &key, &data, 0)) != YP_TRUE) {
316 warnx("duplicate key '%s' - skipping", keybuf);
320 err(1,"failed to write new record - exiting");
327 (void)(dbp->close)(dbp);
335 if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
336 xdr_void, (void *)&in,
337 xdr_void, (void *)out)) != RPC_SUCCESS) {
338 warnx("failed to send 'clear' to local ypserv: %s",
339 clnt_sperrno((enum clnt_stat) stat));