yp*(8): Fix building with -fno-common.
[dragonfly.git] / libexec / ypxfr / ypxfr_main.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1995
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.
1de703da
MD
31 *
32 * $FreeBSD: src/libexec/ypxfr/ypxfr_main.c,v 1.14.2.1 2002/02/15 00:46:54 des Exp $
984263bc
MD
33 */
34
984263bc
MD
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <syslog.h>
40#include <unistd.h>
41#include <sys/types.h>
42#include <sys/param.h>
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46#include <rpc/rpc.h>
47#include <rpc/clnt.h>
48#include <rpcsvc/yp.h>
49struct dom_binding {};
50#include <rpcsvc/ypclnt.h>
51#include <rpcsvc/ypxfrd.h>
52#include "ypxfr_extern.h"
53
04277bb2
SW
54int debug = 1;
55
b843d307
SW
56const char *progname = "ypxfr";
57const char *yp_dir = _PATH_YP;
984263bc 58int _rpcpmstart = 0;
b90fe988 59static int ypxfr_use_yplib = 0; /* Assume the worst. */
60static int ypxfr_clear = 1;
61static int ypxfr_prognum = 0;
62static struct sockaddr_in ypxfr_callback_addr;
63static struct yppushresp_xfr ypxfr_resp;
64static DB *dbp;
984263bc 65
b843d307
SW
66static void
67ypxfr_exit(ypxfrstat retval, char *temp)
984263bc
MD
68{
69 CLIENT *clnt;
70 int sock = RPC_ANYSOCK;
71 struct timeval timeout;
72
73 /* Clean up no matter what happened previously. */
74 if (temp != NULL) {
75 if (dbp != NULL)
76 (void)(dbp->close)(dbp);
77 if (unlink(temp) == -1) {
78 yp_error("failed to unlink %s",strerror(errno));
79 }
80 }
81
82 if (ypxfr_prognum) {
83 timeout.tv_sec = 20;
84 timeout.tv_usec = 0;
85
86 if ((clnt = clntudp_create(&ypxfr_callback_addr, ypxfr_prognum,
87 1, timeout, &sock)) == NULL) {
88 yp_error("%s", clnt_spcreateerror("failed to "
89 "establish callback handle"));
90 exit(1);
91 }
92
93 ypxfr_resp.status = retval;
94
95 if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
96 yp_error("%s", clnt_sperror(clnt, "callback failed"));
97 clnt_destroy(clnt);
98 exit(1);
99 }
100 clnt_destroy(clnt);
101 } else {
102 yp_error("Exiting: %s", ypxfrerr_string(retval));
103 }
104
105 exit(0);
106}
107
b843d307
SW
108static void
109usage(void)
984263bc
MD
110{
111 if (_rpcpmstart) {
112 ypxfr_exit(YPXFR_BADARGS,NULL);
113 } else {
114 fprintf(stderr, "%s\n%s\n%s\n",
115 "usage: ypxfr [-f] [-c] [-d target domain] [-h source host]",
116 " [-s source domain] [-p path]",
117 " [-C taskid program-number ipaddr port] mapname");
118 exit(1);
119 }
120}
121
b843d307
SW
122static int
123ypxfr_foreach(int status, char *key, int keylen, char *val, int vallen,
124 char *data __unused)
984263bc
MD
125{
126 DBT dbkey, dbval;
127
128 if (status != YP_TRUE)
129 return (status);
130
131 /*
132 * XXX Do not attempt to write zero-length keys or
133 * data into a Berkeley DB hash database. It causes a
134 * strange failure mode where sequential searches get
135 * caught in an infinite loop.
136 */
137 if (keylen) {
138 dbkey.data = key;
139 dbkey.size = keylen;
140 } else {
141 dbkey.data = "";
142 dbkey.size = 1;
143 }
144 if (vallen) {
145 dbval.data = val;
146 dbval.size = vallen;
147 } else {
148 dbval.data = "";
149 dbval.size = 1;
150 }
151
152 if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE)
153 return(yp_errno);
154
155 return (0);
156}
157
158int
b843d307 159main(int argc, char *argv[])
984263bc
MD
160{
161 int ch;
162 int ypxfr_force = 0;
163 char *ypxfr_dest_domain = NULL;
164 char *ypxfr_source_host = NULL;
165 char *ypxfr_source_domain = NULL;
166 char *ypxfr_local_domain = NULL;
167 char *ypxfr_master = NULL;
168 unsigned long ypxfr_order = -1, ypxfr_skew_check = -1;
169 char *ypxfr_mapname = NULL;
170 int ypxfr_args = 0;
b90fe988 171 char ypxfr_temp_map[MAXPATHLEN + 2 + 3];
984263bc
MD
172 char tempmap[MAXPATHLEN + 2];
173 char buf[MAXPATHLEN + 2];
174 DBT key, data;
175 int remoteport;
176 int interdom = 0;
177 int secure = 0;
178
179 debug = 1;
180
181 if (!isatty(fileno(stderr))) {
182 openlog("ypxfr", LOG_PID, LOG_DAEMON);
183 _rpcpmstart = 1;
184 }
185
186 if (argc < 2)
187 usage();
188
189 while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != -1) {
190 int my_optind;
191 switch (ch) {
192 case 'f':
193 ypxfr_force++;
194 ypxfr_args++;
195 break;
196 case 'c':
197 ypxfr_clear = 0;
198 ypxfr_args++;
199 break;
200 case 'd':
201 ypxfr_dest_domain = optarg;
202 ypxfr_args += 2;
203 break;
204 case 'h':
205 ypxfr_source_host = optarg;
206 ypxfr_args += 2;
207 break;
208 case 's':
209 ypxfr_source_domain = optarg;
210 ypxfr_args += 2;
211 break;
212 case 'p':
213 yp_dir = optarg;
214 ypxfr_args += 2;
215 break;
216 case 'C':
217 /*
218 * Whoever decided that the -C flag should take
219 * four arguments is a twit.
220 */
221 my_optind = optind - 1;
222 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
223 yp_error("transaction ID not specified");
224 usage();
225 }
226 ypxfr_resp.transid = atol(argv[my_optind]);
227 my_optind++;
228 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
229 yp_error("RPC program number not specified");
230 usage();
231 }
232 ypxfr_prognum = atol(argv[my_optind]);
233 my_optind++;
234 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
235 yp_error("address not specified");
236 usage();
237 }
238 if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) {
239 yp_error("failed to convert '%s' to IP addr",
240 argv[my_optind]);
241 exit(1);
242 }
243 my_optind++;
244 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
245 yp_error("port not specified");
246 usage();
247 }
248 ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind]));
249 ypxfr_args += 5;
250 break;
251 default:
252 usage();
253 break;
254 }
255 }
256
257 ypxfr_mapname = argv[ypxfr_args + 1];
258
259 if (ypxfr_mapname == NULL) {
260 yp_error("no map name specified");
261 usage();
262 }
263
264 /* Always the case. */
265 ypxfr_callback_addr.sin_family = AF_INET;
266
267 /* Determine if local NIS client facilities are turned on. */
268 if (!yp_get_default_domain(&ypxfr_local_domain) &&
269 _yp_check(&ypxfr_local_domain))
270 ypxfr_use_yplib = 1;
271
272 /*
273 * If no destination domain is specified, assume that the
274 * local default domain is to be used and try to obtain it.
275 * Fails if NIS client facilities are turned off.
276 */
277 if (ypxfr_dest_domain == NULL) {
278 if (ypxfr_use_yplib) {
279 yp_get_default_domain(&ypxfr_dest_domain);
280 } else {
281 yp_error("no destination domain specified and \
282the local domain name isn't set");
283 ypxfr_exit(YPXFR_BADARGS,NULL);
284 }
285 }
286
287 /*
288 * If a source domain is not specified, assume it to
289 * be the same as the destination domain.
290 */
291 if (ypxfr_source_domain == NULL) {
292 ypxfr_source_domain = ypxfr_dest_domain;
293 }
294
295 /*
296 * If the source host is not specified, assume it to be the
297 * master for the specified map. If local NIS client facilities
298 * are turned on, we can figure this out using yp_master().
299 * If not, we have to see if a local copy of the map exists
300 * and extract its YP_MASTER_NAME record. If _that_ fails,
301 * we are stuck and must ask the user for more information.
302 */
303 if (ypxfr_source_host == NULL) {
304 if (!ypxfr_use_yplib) {
305 /*
306 * Double whammy: NIS isn't turned on and the user
307 * didn't specify a source host.
308 */
309 char *dptr;
310 key.data = "YP_MASTER_NAME";
311 key.size = sizeof("YP_MASTER_NAME") - 1;
312
313 if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname,
314 &key, &data, 1) != YP_TRUE) {
315 yp_error("no source host specified");
316 ypxfr_exit(YPXFR_BADARGS,NULL);
317 }
318 dptr = data.data;
319 dptr[data.size] = '\0';
320 ypxfr_master = ypxfr_source_host = strdup(dptr);
321 }
322 } else {
323 if (ypxfr_use_yplib)
324 ypxfr_use_yplib = 0;
325 }
326
327 if (ypxfr_master == NULL) {
328 if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain,
329 ypxfr_mapname,
330 ypxfr_source_host,
331 ypxfr_use_yplib)) == NULL) {
332 yp_error("failed to find master of %s in domain %s: %s",
333 ypxfr_mapname, ypxfr_source_domain,
334 ypxfrerr_string(yp_errno));
335 ypxfr_exit(YPXFR_MADDR,NULL);
336 }
337 }
338
339 /*
340 * If we got here and ypxfr_source_host is still undefined,
341 * it means we had to resort to using yp_master() to find the
342 * master server for the map. The source host and master should
343 * be identical.
344 */
345 if (ypxfr_source_host == NULL)
346 ypxfr_source_host = ypxfr_master;
347
348 /*
349 * Don't talk to ypservs on unprivileged ports.
350 */
351 remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP);
352 if (remoteport >= IPPORT_RESERVED) {
353 yp_error("ypserv on %s not running on reserved port",
354 ypxfr_source_host);
355 ypxfr_exit(YPXFR_REFUSED, NULL);
356 }
357
358 if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain,
359 ypxfr_mapname,
360 ypxfr_master, 0)) == 0) {
361 yp_error("failed to get order number of %s: %s",
d0b3a17c 362 ypxfr_mapname, yp_errno == YP_TRUE ?
984263bc
MD
363 "map has order 0" : ypxfrerr_string(yp_errno));
364 ypxfr_exit(YPXFR_YPERR,NULL);
365 }
366
367 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
368 "YP_INTERDOMAIN", sizeof("YP_INTERDOMAIN") - 1))
369 interdom++;
370
371 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
372 "YP_SECURE", sizeof("YP_SECURE") - 1))
373 secure++;
374
375 key.data = "YP_LAST_MODIFIED";
376 key.size = sizeof("YP_LAST_MODIFIED") - 1;
377
378 /* The order number is immaterial when the 'force' flag is set. */
379
380 if (!ypxfr_force) {
381 int ignore = 0;
382 if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) {
383 switch (yp_errno) {
384 case YP_NOKEY:
385 ypxfr_exit(YPXFR_FORCE,NULL);
386 break;
387 case YP_NOMAP:
388 /*
389 * If the map doesn't exist, we're
390 * creating it. Ignore the error.
391 */
392 ignore++;
393 break;
394 case YP_BADDB:
395 default:
396 ypxfr_exit(YPXFR_DBM,NULL);
397 break;
398 }
399 }
b843d307 400 if (!ignore && ypxfr_order <= (unsigned)atoi(data.data))
984263bc
MD
401 ypxfr_exit(YPXFR_AGE, NULL);
402
403 }
404
405 /* Construct a temporary map file name */
406 snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid());
407 snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir,
408 ypxfr_dest_domain, tempmap);
409
410 if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG,
411 YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) {
412
413 /* Don't talk to rpc.ypxfrds on unprovileged ports. */
414 if (remoteport >= IPPORT_RESERVED) {
415 yp_error("rpc.ypxfrd on %s not using privileged port",
416 ypxfr_source_host);
417 ypxfr_exit(YPXFR_REFUSED, NULL);
418 }
419
420 /* Try to send using ypxfrd. If it fails, use old method. */
421 if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname,
422 ypxfr_source_domain, ypxfr_temp_map))
423 goto leave;
424 }
425
426 /* Open the temporary map read/write. */
427 if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) {
428 yp_error("failed to open temporary map file");
429 ypxfr_exit(YPXFR_DBM,NULL);
430 }
431
432 /*
433 * Fill in the keys we already know, such as the order number,
434 * master name, input file name (we actually make up a bogus
435 * name for that) and output file name.
436 */
437 snprintf(buf, sizeof(buf), "%lu", ypxfr_order);
438 data.data = buf;
439 data.size = strlen(buf);
440
441 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
442 yp_error("failed to write order number to database");
b843d307 443 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
444 }
445
446 key.data = "YP_MASTER_NAME";
447 key.size = sizeof("YP_MASTER_NAME") - 1;
448 data.data = ypxfr_master;
449 data.size = strlen(ypxfr_master);
450
451 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
452 yp_error("failed to write master name to database");
b843d307 453 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
454 }
455
456 key.data = "YP_DOMAIN_NAME";
457 key.size = sizeof("YP_DOMAIN_NAME") - 1;
458 data.data = ypxfr_dest_domain;
459 data.size = strlen(ypxfr_dest_domain);
460
461 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
462 yp_error("failed to write domain name to database");
b843d307 463 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
464 }
465
466 snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname);
467
468 key.data = "YP_INPUT_NAME";
469 key.size = sizeof("YP_INPUT_NAME") - 1;
470 data.data = &buf;
471 data.size = strlen(buf);
472
473 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
474 yp_error("failed to write input name to database");
b843d307 475 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
476
477 }
478
479 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
480 ypxfr_mapname);
481
482 key.data = "YP_OUTPUT_NAME";
483 key.size = sizeof("YP_OUTPUT_NAME") - 1;
484 data.data = &buf;
485 data.size = strlen(buf);
486
487 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
488 yp_error("failed to write output name to database");
b843d307 489 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
490 }
491
492 if (interdom) {
493 key.data = "YP_INTERDOMAIN";
494 key.size = sizeof("YP_INTERDOMAIN") - 1;
495 data.data = "";
496 data.size = 0;
497
498 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
499 yp_error("failed to add interdomain flag to database");
b843d307 500 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
501 }
502 }
503
504 if (secure) {
505 key.data = "YP_SECURE";
506 key.size = sizeof("YP_SECURE") - 1;
507 data.data = "";
508 data.size = 0;
509
510 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
511 yp_error("failed to add secure flag to database");
b843d307 512 ypxfr_exit(YPXFR_DBM, ypxfr_temp_map);
984263bc
MD
513 }
514 }
515
516 /* Now suck over the contents of the map from the master. */
517
518 if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain,
519 ypxfr_source_host, ypxfr_foreach)){
520 yp_error("failed to retrieve map from source host");
b843d307 521 ypxfr_exit(YPXFR_YPERR, ypxfr_temp_map);
984263bc
MD
522 }
523
524 (void)(dbp->close)(dbp);
525 dbp = NULL; /* <- yes, it seems this is necessary. */
526
527leave:
528
529 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
530 ypxfr_mapname);
531
532 /* Peek at the order number again and check for skew. */
533 if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain,
534 ypxfr_mapname,
535 ypxfr_master, 0)) == 0) {
536 yp_error("failed to get order number of %s: %s",
d0b3a17c 537 ypxfr_mapname, yp_errno == YP_TRUE ?
984263bc 538 "map has order 0" : ypxfrerr_string(yp_errno));
b843d307 539 ypxfr_exit(YPXFR_YPERR, ypxfr_temp_map);
984263bc
MD
540 }
541
542 if (ypxfr_order != ypxfr_skew_check)
b843d307 543 ypxfr_exit(YPXFR_SKEW, ypxfr_temp_map);
984263bc
MD
544
545 /*
546 * Send a YPPROC_CLEAR to the local ypserv.
547 */
548 if (ypxfr_clear) {
549 char in = 0;
550 char *out = NULL;
551 int stat;
552 if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
b843d307
SW
553 (xdrproc_t)xdr_void, (void *)&in,
554 (xdrproc_t)xdr_void, (void *)out)) != RPC_SUCCESS) {
984263bc
MD
555 yp_error("failed to send 'clear' to local ypserv: %s",
556 clnt_sperrno((enum clnt_stat) stat));
b843d307 557 ypxfr_exit(YPXFR_CLEAR, ypxfr_temp_map);
984263bc
MD
558 }
559 }
560
561 /*
562 * Put the new map in place immediately. I'm not sure if the
563 * kernel does an unlink() and rename() atomically in the event
564 * that we move a new copy of a map over the top of an existing
565 * one, but there's less chance of a race condition happening
566 * than if we were to do the unlink() ourselves.
567 */
568 if (rename(ypxfr_temp_map, buf) == -1) {
569 yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf,
570 strerror(errno));
571 ypxfr_exit(YPXFR_FILE,NULL);
572 }
573
574 ypxfr_exit(YPXFR_SUCC,NULL);
575
576 return(1);
577}