amd64 port: mainly on the pmap headers, identify_cpu and initcpu
[dragonfly/port-amd64.git] / libexec / ypxfr / ypxfr_main.c
blob55849900ca0deb7df556ad8a6a15c0966fcd2d41
1 /*
2 * Copyright (c) 1995
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
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.
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.
32 * $FreeBSD: src/libexec/ypxfr/ypxfr_main.c,v 1.14.2.1 2002/02/15 00:46:54 des Exp $
33 * $DragonFly: src/libexec/ypxfr/ypxfr_main.c,v 1.2 2003/06/17 04:27:08 dillon Exp $
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <rpc/rpc.h>
48 #include <rpc/clnt.h>
49 #include <rpcsvc/yp.h>
50 struct dom_binding {};
51 #include <rpcsvc/ypclnt.h>
52 #include <rpcsvc/ypxfrd.h>
53 #include "ypxfr_extern.h"
55 char *progname = "ypxfr";
56 char *yp_dir = _PATH_YP;
57 int _rpcpmstart = 0;
58 int ypxfr_use_yplib = 0; /* Assume the worst. */
59 int ypxfr_clear = 1;
60 int ypxfr_prognum = 0;
61 struct sockaddr_in ypxfr_callback_addr;
62 struct yppushresp_xfr ypxfr_resp;
63 DB *dbp;
65 static void ypxfr_exit(retval, temp)
66 ypxfrstat retval;
67 char *temp;
69 CLIENT *clnt;
70 int sock = RPC_ANYSOCK;
71 struct timeval timeout;
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));
82 if (ypxfr_prognum) {
83 timeout.tv_sec = 20;
84 timeout.tv_usec = 0;
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);
93 ypxfr_resp.status = retval;
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);
100 clnt_destroy(clnt);
101 } else {
102 yp_error("Exiting: %s", ypxfrerr_string(retval));
105 exit(0);
108 static void usage()
110 if (_rpcpmstart) {
111 ypxfr_exit(YPXFR_BADARGS,NULL);
112 } else {
113 fprintf(stderr, "%s\n%s\n%s\n",
114 "usage: ypxfr [-f] [-c] [-d target domain] [-h source host]",
115 " [-s source domain] [-p path]",
116 " [-C taskid program-number ipaddr port] mapname");
117 exit(1);
121 int ypxfr_foreach(status, key, keylen, val, vallen, data)
122 int status;
123 char *key;
124 int keylen;
125 char *val;
126 int vallen;
127 char *data;
129 DBT dbkey, dbval;
131 if (status != YP_TRUE)
132 return (status);
135 * XXX Do not attempt to write zero-length keys or
136 * data into a Berkeley DB hash database. It causes a
137 * strange failure mode where sequential searches get
138 * caught in an infinite loop.
140 if (keylen) {
141 dbkey.data = key;
142 dbkey.size = keylen;
143 } else {
144 dbkey.data = "";
145 dbkey.size = 1;
147 if (vallen) {
148 dbval.data = val;
149 dbval.size = vallen;
150 } else {
151 dbval.data = "";
152 dbval.size = 1;
155 if (yp_put_record(dbp, &dbkey, &dbval, 0) != YP_TRUE)
156 return(yp_errno);
158 return (0);
162 main(argc,argv)
163 int argc;
164 char *argv[];
166 int ch;
167 int ypxfr_force = 0;
168 char *ypxfr_dest_domain = NULL;
169 char *ypxfr_source_host = NULL;
170 char *ypxfr_source_domain = NULL;
171 char *ypxfr_local_domain = NULL;
172 char *ypxfr_master = NULL;
173 unsigned long ypxfr_order = -1, ypxfr_skew_check = -1;
174 char *ypxfr_mapname = NULL;
175 int ypxfr_args = 0;
176 char ypxfr_temp_map[MAXPATHLEN + 2];
177 char tempmap[MAXPATHLEN + 2];
178 char buf[MAXPATHLEN + 2];
179 DBT key, data;
180 int remoteport;
181 int interdom = 0;
182 int secure = 0;
184 debug = 1;
186 if (!isatty(fileno(stderr))) {
187 openlog("ypxfr", LOG_PID, LOG_DAEMON);
188 _rpcpmstart = 1;
191 if (argc < 2)
192 usage();
194 while ((ch = getopt(argc, argv, "fcd:h:s:p:C:")) != -1) {
195 int my_optind;
196 switch (ch) {
197 case 'f':
198 ypxfr_force++;
199 ypxfr_args++;
200 break;
201 case 'c':
202 ypxfr_clear = 0;
203 ypxfr_args++;
204 break;
205 case 'd':
206 ypxfr_dest_domain = optarg;
207 ypxfr_args += 2;
208 break;
209 case 'h':
210 ypxfr_source_host = optarg;
211 ypxfr_args += 2;
212 break;
213 case 's':
214 ypxfr_source_domain = optarg;
215 ypxfr_args += 2;
216 break;
217 case 'p':
218 yp_dir = optarg;
219 ypxfr_args += 2;
220 break;
221 case 'C':
223 * Whoever decided that the -C flag should take
224 * four arguments is a twit.
226 my_optind = optind - 1;
227 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
228 yp_error("transaction ID not specified");
229 usage();
231 ypxfr_resp.transid = atol(argv[my_optind]);
232 my_optind++;
233 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
234 yp_error("RPC program number not specified");
235 usage();
237 ypxfr_prognum = atol(argv[my_optind]);
238 my_optind++;
239 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
240 yp_error("address not specified");
241 usage();
243 if (!inet_aton(argv[my_optind], &ypxfr_callback_addr.sin_addr)) {
244 yp_error("failed to convert '%s' to IP addr",
245 argv[my_optind]);
246 exit(1);
248 my_optind++;
249 if (argv[my_optind] == NULL || !strlen(argv[my_optind])) {
250 yp_error("port not specified");
251 usage();
253 ypxfr_callback_addr.sin_port = htons((u_short)atoi(argv[my_optind]));
254 ypxfr_args += 5;
255 break;
256 default:
257 usage();
258 break;
262 ypxfr_mapname = argv[ypxfr_args + 1];
264 if (ypxfr_mapname == NULL) {
265 yp_error("no map name specified");
266 usage();
269 /* Always the case. */
270 ypxfr_callback_addr.sin_family = AF_INET;
272 /* Determine if local NIS client facilities are turned on. */
273 if (!yp_get_default_domain(&ypxfr_local_domain) &&
274 _yp_check(&ypxfr_local_domain))
275 ypxfr_use_yplib = 1;
278 * If no destination domain is specified, assume that the
279 * local default domain is to be used and try to obtain it.
280 * Fails if NIS client facilities are turned off.
282 if (ypxfr_dest_domain == NULL) {
283 if (ypxfr_use_yplib) {
284 yp_get_default_domain(&ypxfr_dest_domain);
285 } else {
286 yp_error("no destination domain specified and \
287 the local domain name isn't set");
288 ypxfr_exit(YPXFR_BADARGS,NULL);
293 * If a source domain is not specified, assume it to
294 * be the same as the destination domain.
296 if (ypxfr_source_domain == NULL) {
297 ypxfr_source_domain = ypxfr_dest_domain;
301 * If the source host is not specified, assume it to be the
302 * master for the specified map. If local NIS client facilities
303 * are turned on, we can figure this out using yp_master().
304 * If not, we have to see if a local copy of the map exists
305 * and extract its YP_MASTER_NAME record. If _that_ fails,
306 * we are stuck and must ask the user for more information.
308 if (ypxfr_source_host == NULL) {
309 if (!ypxfr_use_yplib) {
311 * Double whammy: NIS isn't turned on and the user
312 * didn't specify a source host.
314 char *dptr;
315 key.data = "YP_MASTER_NAME";
316 key.size = sizeof("YP_MASTER_NAME") - 1;
318 if (yp_get_record(ypxfr_dest_domain, ypxfr_mapname,
319 &key, &data, 1) != YP_TRUE) {
320 yp_error("no source host specified");
321 ypxfr_exit(YPXFR_BADARGS,NULL);
323 dptr = data.data;
324 dptr[data.size] = '\0';
325 ypxfr_master = ypxfr_source_host = strdup(dptr);
327 } else {
328 if (ypxfr_use_yplib)
329 ypxfr_use_yplib = 0;
332 if (ypxfr_master == NULL) {
333 if ((ypxfr_master = ypxfr_get_master(ypxfr_source_domain,
334 ypxfr_mapname,
335 ypxfr_source_host,
336 ypxfr_use_yplib)) == NULL) {
337 yp_error("failed to find master of %s in domain %s: %s",
338 ypxfr_mapname, ypxfr_source_domain,
339 ypxfrerr_string(yp_errno));
340 ypxfr_exit(YPXFR_MADDR,NULL);
345 * If we got here and ypxfr_source_host is still undefined,
346 * it means we had to resort to using yp_master() to find the
347 * master server for the map. The source host and master should
348 * be identical.
350 if (ypxfr_source_host == NULL)
351 ypxfr_source_host = ypxfr_master;
354 * Don't talk to ypservs on unprivileged ports.
356 remoteport = getrpcport(ypxfr_source_host, YPPROG, YPVERS, IPPROTO_UDP);
357 if (remoteport >= IPPORT_RESERVED) {
358 yp_error("ypserv on %s not running on reserved port",
359 ypxfr_source_host);
360 ypxfr_exit(YPXFR_REFUSED, NULL);
363 if ((ypxfr_order = ypxfr_get_order(ypxfr_source_domain,
364 ypxfr_mapname,
365 ypxfr_master, 0)) == 0) {
366 yp_error("failed to get order number of %s: %s",
367 ypxfr_mapname, yp_errno == YPXFR_SUCC ?
368 "map has order 0" : ypxfrerr_string(yp_errno));
369 ypxfr_exit(YPXFR_YPERR,NULL);
372 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
373 "YP_INTERDOMAIN", sizeof("YP_INTERDOMAIN") - 1))
374 interdom++;
376 if (ypxfr_match(ypxfr_master, ypxfr_source_domain, ypxfr_mapname,
377 "YP_SECURE", sizeof("YP_SECURE") - 1))
378 secure++;
380 key.data = "YP_LAST_MODIFIED";
381 key.size = sizeof("YP_LAST_MODIFIED") - 1;
383 /* The order number is immaterial when the 'force' flag is set. */
385 if (!ypxfr_force) {
386 int ignore = 0;
387 if (yp_get_record(ypxfr_dest_domain,ypxfr_mapname,&key,&data,1) != YP_TRUE) {
388 switch (yp_errno) {
389 case YP_NOKEY:
390 ypxfr_exit(YPXFR_FORCE,NULL);
391 break;
392 case YP_NOMAP:
394 * If the map doesn't exist, we're
395 * creating it. Ignore the error.
397 ignore++;
398 break;
399 case YP_BADDB:
400 default:
401 ypxfr_exit(YPXFR_DBM,NULL);
402 break;
405 if (!ignore && ypxfr_order <= atoi(data.data))
406 ypxfr_exit(YPXFR_AGE, NULL);
410 /* Construct a temporary map file name */
411 snprintf(tempmap, sizeof(tempmap), "%s.%d",ypxfr_mapname, getpid());
412 snprintf(ypxfr_temp_map, sizeof(ypxfr_temp_map), "%s/%s/%s", yp_dir,
413 ypxfr_dest_domain, tempmap);
415 if ((remoteport = getrpcport(ypxfr_source_host, YPXFRD_FREEBSD_PROG,
416 YPXFRD_FREEBSD_VERS, IPPROTO_TCP))) {
418 /* Don't talk to rpc.ypxfrds on unprovileged ports. */
419 if (remoteport >= IPPORT_RESERVED) {
420 yp_error("rpc.ypxfrd on %s not using privileged port",
421 ypxfr_source_host);
422 ypxfr_exit(YPXFR_REFUSED, NULL);
425 /* Try to send using ypxfrd. If it fails, use old method. */
426 if (!ypxfrd_get_map(ypxfr_source_host, ypxfr_mapname,
427 ypxfr_source_domain, ypxfr_temp_map))
428 goto leave;
431 /* Open the temporary map read/write. */
432 if ((dbp = yp_open_db_rw(ypxfr_dest_domain, tempmap, 0)) == NULL) {
433 yp_error("failed to open temporary map file");
434 ypxfr_exit(YPXFR_DBM,NULL);
438 * Fill in the keys we already know, such as the order number,
439 * master name, input file name (we actually make up a bogus
440 * name for that) and output file name.
442 snprintf(buf, sizeof(buf), "%lu", ypxfr_order);
443 data.data = buf;
444 data.size = strlen(buf);
446 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
447 yp_error("failed to write order number to database");
448 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
451 key.data = "YP_MASTER_NAME";
452 key.size = sizeof("YP_MASTER_NAME") - 1;
453 data.data = ypxfr_master;
454 data.size = strlen(ypxfr_master);
456 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
457 yp_error("failed to write master name to database");
458 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
461 key.data = "YP_DOMAIN_NAME";
462 key.size = sizeof("YP_DOMAIN_NAME") - 1;
463 data.data = ypxfr_dest_domain;
464 data.size = strlen(ypxfr_dest_domain);
466 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
467 yp_error("failed to write domain name to database");
468 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
471 snprintf (buf, sizeof(buf), "%s:%s", ypxfr_source_host, ypxfr_mapname);
473 key.data = "YP_INPUT_NAME";
474 key.size = sizeof("YP_INPUT_NAME") - 1;
475 data.data = &buf;
476 data.size = strlen(buf);
478 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
479 yp_error("failed to write input name to database");
480 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
484 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
485 ypxfr_mapname);
487 key.data = "YP_OUTPUT_NAME";
488 key.size = sizeof("YP_OUTPUT_NAME") - 1;
489 data.data = &buf;
490 data.size = strlen(buf);
492 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
493 yp_error("failed to write output name to database");
494 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
497 if (interdom) {
498 key.data = "YP_INTERDOMAIN";
499 key.size = sizeof("YP_INTERDOMAIN") - 1;
500 data.data = "";
501 data.size = 0;
503 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
504 yp_error("failed to add interdomain flag to database");
505 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
509 if (secure) {
510 key.data = "YP_SECURE";
511 key.size = sizeof("YP_SECURE") - 1;
512 data.data = "";
513 data.size = 0;
515 if (yp_put_record(dbp, &key, &data, 0) != YP_TRUE) {
516 yp_error("failed to add secure flag to database");
517 ypxfr_exit(YPXFR_DBM,&ypxfr_temp_map);
521 /* Now suck over the contents of the map from the master. */
523 if (ypxfr_get_map(ypxfr_mapname,ypxfr_source_domain,
524 ypxfr_source_host, ypxfr_foreach)){
525 yp_error("failed to retrieve map from source host");
526 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map);
529 (void)(dbp->close)(dbp);
530 dbp = NULL; /* <- yes, it seems this is necessary. */
532 leave:
534 snprintf(buf, sizeof(buf), "%s/%s/%s", yp_dir, ypxfr_dest_domain,
535 ypxfr_mapname);
537 /* Peek at the order number again and check for skew. */
538 if ((ypxfr_skew_check = ypxfr_get_order(ypxfr_source_domain,
539 ypxfr_mapname,
540 ypxfr_master, 0)) == 0) {
541 yp_error("failed to get order number of %s: %s",
542 ypxfr_mapname, yp_errno == YPXFR_SUCC ?
543 "map has order 0" : ypxfrerr_string(yp_errno));
544 ypxfr_exit(YPXFR_YPERR,&ypxfr_temp_map);
547 if (ypxfr_order != ypxfr_skew_check)
548 ypxfr_exit(YPXFR_SKEW,&ypxfr_temp_map);
551 * Send a YPPROC_CLEAR to the local ypserv.
553 if (ypxfr_clear) {
554 char in = 0;
555 char *out = NULL;
556 int stat;
557 if ((stat = callrpc("localhost",YPPROG,YPVERS,YPPROC_CLEAR,
558 xdr_void, (void *)&in,
559 xdr_void, (void *)out)) != RPC_SUCCESS) {
560 yp_error("failed to send 'clear' to local ypserv: %s",
561 clnt_sperrno((enum clnt_stat) stat));
562 ypxfr_exit(YPXFR_CLEAR, &ypxfr_temp_map);
567 * Put the new map in place immediately. I'm not sure if the
568 * kernel does an unlink() and rename() atomically in the event
569 * that we move a new copy of a map over the top of an existing
570 * one, but there's less chance of a race condition happening
571 * than if we were to do the unlink() ourselves.
573 if (rename(ypxfr_temp_map, buf) == -1) {
574 yp_error("rename(%s,%s) failed: %s", ypxfr_temp_map, buf,
575 strerror(errno));
576 ypxfr_exit(YPXFR_FILE,NULL);
579 ypxfr_exit(YPXFR_SUCC,NULL);
581 return(1);