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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
45 #include <rpcsvc/yp.h>
46 #include <sys/param.h>
47 #include <sys/types.h>
49 #include "yp_extern.h"
50 #include "ypxfr_extern.h"
52 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)
70 open_db(char *path
, int flags
)
72 extern HASHINFO openinfo
;
74 return(dbopen(path
, flags
, PERM_SECURE
, DB_HASH
, &openinfo
));
83 dbp
= open_db(map
, O_RDONLY
);
86 err(1, "open_db(%s) failed", map
);
89 while (yp_next_record(dbp
, &key
, &data
, 1, 1) == YP_TRUE
)
90 printf("%.*s %.*s\n", key
.size
,key
.data
,data
.size
,data
.data
);
92 (void)(dbp
->close
)(dbp
);
97 main(int argc
, char *argv
[])
102 int filter_plusminus
= 0;
106 char *infilename
= NULL
;
107 char *outfilename
= NULL
;
108 char *mastername
= NULL
;
114 char *keybuf
, *datbuf
;
116 char hname
[MAXHOSTNAMELEN
+ 2];
118 while ((ch
= getopt(argc
, argv
, "uhcbsfd:i:o:m:")) != -1) {
142 outfilename
= optarg
;
169 if (infile
== NULL
|| map
== NULL
) {
175 if (mastername
== NULL
) {
176 if (gethostname((char *)&hname
, sizeof(hname
)) == -1)
177 err(1, "gethostname() failed");
178 mastername
= (char *)&hname
;
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.
187 if (!strcmp(infile
, "-")) {
190 if ((ifp
= fopen(infile
, "r")) == NULL
)
191 err(1, "failed to open %s", infile
);
194 if ((dbp
= open_db(map
, O_RDWR
|O_EXLOCK
|O_EXCL
|O_CREAT
)) == NULL
)
195 err(1, "open_db(%s) failed", map
);
198 key
.data
= "YP_INTERDOMAIN";
199 key
.size
= sizeof("YP_INTERDOMAIN") - 1;
202 yp_put_record(dbp
, &key
, &data
, 0);
206 key
.data
= "YP_SECURE";
207 key
.size
= sizeof("YP_SECURE") - 1;
210 yp_put_record(dbp
, &key
, &data
, 0);
213 key
.data
= "YP_MASTER_NAME";
214 key
.size
= sizeof("YP_MASTER_NAME") - 1;
215 data
.data
= mastername
;
216 data
.size
= strlen(mastername
);
217 yp_put_record(dbp
, &key
, &data
, 0);
219 key
.data
= "YP_LAST_MODIFIED";
220 key
.size
= sizeof("YP_LAST_MODIFIED") - 1;
221 snprintf(buf
, sizeof(buf
), "%lu", time(NULL
));
222 data
.data
= (char *)&buf
;
223 data
.size
= strlen(buf
);
224 yp_put_record(dbp
, &key
, &data
, 0);
227 key
.data
= "YP_INPUT_FILE";
228 key
.size
= sizeof("YP_INPUT_FILE") - 1;
229 data
.data
= infilename
;
230 data
.size
= strlen(infilename
);
231 yp_put_record(dbp
, &key
, &data
, 0);
235 key
.data
= "YP_OUTPUT_FILE";
236 key
.size
= sizeof("YP_OUTPUT_FILE") - 1;
237 data
.data
= outfilename
;
238 data
.size
= strlen(outfilename
);
239 yp_put_record(dbp
, &key
, &data
, 0);
243 key
.data
= "YP_DOMAIN_NAME";
244 key
.size
= sizeof("YP_DOMAIN_NAME") - 1;
246 data
.size
= strlen(domain
);
247 yp_put_record(dbp
, &key
, &data
, 0);
250 while (fgets((char *)&buf
, sizeof(buf
), ifp
)) {
255 if ((sep
= strchr(buf
, '\n')))
258 /* handle backslash line continuations */
259 while (buf
[strlen(buf
) - 1] == '\\') {
260 fgets((char *)&buf
[strlen(buf
) - 1],
261 sizeof(buf
) - strlen(buf
), ifp
);
262 if ((sep
= strchr(buf
, '\n')))
266 /* find the separation between the key and data */
267 if ((sep
= strpbrk(buf
, " \t")) == NULL
) {
268 warnx("bad input -- no white space: %s", buf
);
272 /* separate the strings */
273 keybuf
= (char *)&buf
;
277 /* set datbuf to start at first non-whitespace character */
278 while (*datbuf
== ' ' || *datbuf
== '\t')
281 /* Check for silliness. */
282 if (filter_plusminus
) {
283 if (*keybuf
== '+' || *keybuf
== '-' ||
284 *datbuf
== '+' || *datbuf
== '-') {
285 warnx("bad character at "
286 "start of line: %s", buf
);
291 if (strlen(keybuf
) > YPMAXRECORD
) {
292 warnx("key too long: %s", keybuf
);
296 if (!strlen(keybuf
)) {
297 warnx("no key -- check source file for blank lines");
301 if (strlen(datbuf
) > YPMAXRECORD
) {
302 warnx("data too long: %s", datbuf
);
307 key
.size
= strlen(keybuf
);
309 data
.size
= strlen(datbuf
);
311 if ((rval
= yp_put_record(dbp
, &key
, &data
, 0)) != YP_TRUE
) {
314 warnx("duplicate key '%s' - skipping", keybuf
);
318 err(1,"failed to write new record - exiting");
325 (void)(dbp
->close
)(dbp
);
333 if ((stat
= callrpc("localhost", YPPROG
,YPVERS
, YPPROC_CLEAR
,
334 (xdrproc_t
)xdr_void
, &in
,
335 (xdrproc_t
)xdr_void
, out
)) != RPC_SUCCESS
) {
336 warnx("failed to send 'clear' to local ypserv: %s",
337 clnt_sperrno((enum clnt_stat
) stat
));