]>
git.saurik.com Git - apple/network_cmds.git/blob - ypxfr.tproj/ypxfr.c
4dad0041b78b22740e4b6755d6405a8124d4a82d
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $ */
26 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Mats O Jansson
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 static char rcsid
[] = "$OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $";
60 #include <sys/types.h>
62 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
76 #include <rpcsvc/yp.h>
77 #include <rpcsvc/ypclnt.h>
79 #include "yplib_host.h"
84 extern char *__progname
; /* from crt0.o */
87 extern bool_t
xdr_ypresp_all_seq();
89 extern int (*ypresp_allfn
)();
90 extern void *ypresp_data
;
93 ypxfr_foreach(status
,keystr
,keylen
,valstr
,vallen
,data
)
94 int status
,keylen
,vallen
;
95 char *keystr
,*valstr
,*data
;
99 if (status
== YP_NOMORE
)
102 keystr
[keylen
] = '\0';
103 valstr
[vallen
] = '\0';
106 key
.dsize
= strlen(keystr
);
109 val
.dsize
= strlen(valstr
);
111 ypdb_store(db
, key
, val
, YPDB_INSERT
);
117 get_local_ordernum(domain
, map
, lordernum
)
120 u_int32_t
*lordernum
;
122 char map_path
[MAXPATHLEN
];
123 char order_key
[] = YP_LAST_KEY
;
124 char order
[MAX_LAST_LEN
+1];
130 /* This routine returns YPPUSH_SUCC or YPPUSH_NODOM */
132 status
= YPPUSH_SUCC
;
134 snprintf(map_path
, sizeof map_path
, "%s/%s", YP_DB_PATH
, domain
);
135 if (!((stat(map_path
, &finfo
) == 0) &&
136 ((finfo
.st_mode
& S_IFMT
) == S_IFDIR
))) {
137 fprintf(stderr
, "%s: domain %s not found locally\n",
139 status
= YPPUSH_NODOM
;
143 snprintf(map_path
, sizeof map_path
, "%s/%s/%s%s",
144 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
145 if(!(stat(map_path
, &finfo
) == 0)) {
146 status
= YPPUSH_NOMAP
;
151 snprintf(map_path
, sizeof map_path
, "%s/%s/%s",
152 YP_DB_PATH
, domain
, map
);
153 db
= ypdb_open(map_path
, O_RDONLY
, 0444);
161 k
.dptr
= (char *)&order_key
;
162 k
.dsize
= YP_LAST_LEN
;
164 v
= ypdb_fetch(db
,k
);
167 if (v
.dptr
== NULL
) {
170 strncpy(order
, v
.dptr
, sizeof order
-1);
171 order
[sizeof order
-1] = '\0';
172 *lordernum
= (u_int32_t
)atol(order
);
176 if((status
== YPPUSH_NOMAP
) || (status
== YPPUSH_DBM
)) {
178 status
= YPPUSH_SUCC
;
186 get_remote_ordernum(client
, domain
, map
, lordernum
, rordernum
)
191 u_int32_t
*rordernum
;
195 status
= yp_order_host(client
, domain
, map
, rordernum
);
198 if(*rordernum
<= lordernum
) {
201 status
= YPPUSH_SUCC
;
209 get_map(client
,domain
,map
,incallback
)
213 struct ypall_callback
*incallback
;
215 (void)yp_all_host(client
, domain
, map
, incallback
);
220 create_db(domain
,map
,temp_map
)
225 return ypdb_open_suf(temp_map
, O_RDWR
, 0444);
229 install_db(domain
,map
,temp_map
)
234 char db_name
[MAXPATHLEN
];
236 snprintf(db_name
, sizeof db_name
, "%s/%s/%s%s",
237 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
238 rename(temp_map
, db_name
);
244 add_order(db
, ordernum
)
250 char keystr
[] = YP_LAST_KEY
;
253 sprintf(datestr
, "%010u", ordernum
);
256 key
.dsize
= strlen(keystr
);
259 val
.dsize
= strlen(datestr
);
261 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
263 status
= YPPUSH_SUCC
;
271 add_master(client
, domain
, map
, db
)
277 char keystr
[] = YP_MASTER_KEY
;
286 status
= yp_master_host(client
, domain
, map
, &master
);
290 key
.dsize
= strlen(keystr
);
293 val
.dsize
= strlen(master
);
295 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
297 status
= YPPUSH_SUCC
;
307 add_interdomain(client
, domain
, map
, db
)
313 char keystr
[] = YP_INTERDOMAIN_KEY
;
319 /* Get INTERDOMAIN */
322 k
.dsize
= strlen(keystr
);
324 status
= yp_match_host(client
, domain
, map
,
325 k
.dptr
, k
.dsize
, &value
, &vallen
);
327 if(status
== 0 && value
) {
332 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
334 status
= YPPUSH_SUCC
;
345 add_secure(client
, domain
, map
, db
)
351 char keystr
[] = YP_SECURE_KEY
;
360 k
.dsize
= strlen(keystr
);
362 status
= yp_match_host(client
, domain
, map
,
363 k
.dptr
, k
.dsize
, &value
, &vallen
);
370 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
372 status
= YPPUSH_SUCC
;
391 status
= YPPUSH_SUCC
;
398 r
= clnt_call(client
, YPPROC_CLEAR
,
399 xdr_void
, 0, xdr_void
, 0, tv
);
400 if(r
!= RPC_SUCCESS
) {
401 clnt_perror(client
, "yp_clear: clnt_call");
409 send_reply(client
,status
,tid
)
415 struct ypresp_xfr resp
;
422 resp
.xfrstat
= status
;
426 r
= clnt_call(client
, 1,
427 xdr_ypresp_xfr
, &resp
, xdr_void
, 0, tv
);
428 if(r
!= RPC_SUCCESS
) {
429 clnt_perror(client
, "yppushresp_xdr: clnt_call");
449 char *srcdomain
= NULL
;
455 u_int32_t ordernum
, new_ordernum
;
456 struct ypall_callback callback
;
458 int status
,xfr_status
;
461 status
= YPPUSH_SUCC
;
464 yp_get_default_domain(&domain
);
466 while ((ch
= getopt(argc
, argv
, "cd:fh:s:C:")) != -1)
472 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
483 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
488 if (optind
+ 3 >= argc
) {
495 prog
= argv
[optind
++];
496 ipadd
= argv
[optind
++];
497 port
= argv
[optind
++];
504 if(optind
+ 1 != argc
) {
511 status
= YPPUSH_BADARGS
;
512 fprintf(stderr
, "usage: %s %s %s\n",
513 "[-cf] [-d domain] [-h host] [-s domain]",
514 "[-C tid prog ipadd port] mapname\n",
521 yplog("ypxfr: Arguments:");
522 yplog("YP clear to local: %s", (cflag
) ? "no" : "yes");
523 yplog(" Force transfer: %s", (fflag
) ? "yes" : "no");
524 yplog(" domain: %s", domain
);
525 yplog(" host: %s", host
);
526 yplog(" source domain: %s", srcdomain
);
527 yplog(" transid: %s", tid
);
528 yplog(" prog: %s", prog
);
529 yplog(" port: %s", port
);
530 yplog(" ipadd: %s", ipadd
);
531 yplog(" map: %s", map
);
536 status
= get_local_ordernum(domain
, map
, &ordernum
);
545 if (srcdomain
== NULL
) {
546 status
= yp_master(domain
,map
,&host
);
548 status
= yp_master(srcdomain
,map
,&host
);
551 status
= YPPUSH_SUCC
;
558 /* XXX this is raceable if portmap has holes! */
561 yplog("Check for reserved port on host: %s", host
);
563 srvport
= getrpcport(host
,YPPROG
,YPVERS
,IPPROTO_TCP
);
564 if (srvport
>= IPPORT_RESERVED
)
565 status
= YPPUSH_REFUSED
;
571 yplog("Connect host: %s", host
);
573 client
= yp_bind_host(host
,YPPROG
,YPVERS
,0,1);
575 status
= get_remote_ordernum(client
, domain
, map
,
576 ordernum
, &new_ordernum
);
580 if (status
== YPPUSH_SUCC
) {
581 char tmpmapname
[MAXPATHLEN
];
584 /* Create temporary db */
585 snprintf(tmpmapname
, sizeof tmpmapname
,
586 "%s/%s/ypdbXXXXXXXXXX", YP_DB_PATH
, domain
);
587 fd
= mkstemp(tmpmapname
);
594 db
= create_db(domain
,map
,tmpmapname
);
601 status
= add_order(db
, new_ordernum
);
606 status
= add_master(client
,domain
,map
,db
);
609 /* Add INTERDOMAIN */
611 status
= add_interdomain(client
,domain
,map
,db
);
616 status
= add_secure(client
,domain
,map
,db
);
620 callback
.foreach
=ypxfr_foreach
;
621 get_map(client
,domain
,map
,&callback
);
631 status
= install_db(domain
,map
,tmpmapname
);
634 status
= YPPUSH_SUCC
;
642 clnt_destroy(client
);
648 client
= yp_bind_local(YPPROG
,YPVERS
);
649 status
= send_clear(client
);
650 clnt_destroy(client
);
655 client
= yp_bind_host(ipadd
,
660 status
= send_reply(client
,xfr_status
,atoi(tid
));
661 clnt_destroy(client
);