]>
git.saurik.com Git - apple/network_cmds.git/blob - ypxfr.tproj/ypxfr.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $ */
27 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Mats O Jansson
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char rcsid
[] = "$OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $";
61 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
77 #include <rpcsvc/yp.h>
78 #include <rpcsvc/ypclnt.h>
80 #include "yplib_host.h"
85 extern char *__progname
; /* from crt0.o */
88 extern bool_t
xdr_ypresp_all_seq();
90 extern int (*ypresp_allfn
)();
91 extern void *ypresp_data
;
94 ypxfr_foreach(status
,keystr
,keylen
,valstr
,vallen
,data
)
95 int status
,keylen
,vallen
;
96 char *keystr
,*valstr
,*data
;
100 if (status
== YP_NOMORE
)
103 keystr
[keylen
] = '\0';
104 valstr
[vallen
] = '\0';
107 key
.dsize
= strlen(keystr
);
110 val
.dsize
= strlen(valstr
);
112 ypdb_store(db
, key
, val
, YPDB_INSERT
);
118 get_local_ordernum(domain
, map
, lordernum
)
121 u_int32_t
*lordernum
;
123 char map_path
[MAXPATHLEN
];
124 char order_key
[] = YP_LAST_KEY
;
125 char order
[MAX_LAST_LEN
+1];
131 /* This routine returns YPPUSH_SUCC or YPPUSH_NODOM */
133 status
= YPPUSH_SUCC
;
135 snprintf(map_path
, sizeof map_path
, "%s/%s", YP_DB_PATH
, domain
);
136 if (!((stat(map_path
, &finfo
) == 0) &&
137 ((finfo
.st_mode
& S_IFMT
) == S_IFDIR
))) {
138 fprintf(stderr
, "%s: domain %s not found locally\n",
140 status
= YPPUSH_NODOM
;
144 snprintf(map_path
, sizeof map_path
, "%s/%s/%s%s",
145 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
146 if(!(stat(map_path
, &finfo
) == 0)) {
147 status
= YPPUSH_NOMAP
;
152 snprintf(map_path
, sizeof map_path
, "%s/%s/%s",
153 YP_DB_PATH
, domain
, map
);
154 db
= ypdb_open(map_path
, O_RDONLY
, 0444);
162 k
.dptr
= (char *)&order_key
;
163 k
.dsize
= YP_LAST_LEN
;
165 v
= ypdb_fetch(db
,k
);
168 if (v
.dptr
== NULL
) {
171 strncpy(order
, v
.dptr
, sizeof order
-1);
172 order
[sizeof order
-1] = '\0';
173 *lordernum
= (u_int32_t
)atol(order
);
177 if((status
== YPPUSH_NOMAP
) || (status
== YPPUSH_DBM
)) {
179 status
= YPPUSH_SUCC
;
187 get_remote_ordernum(client
, domain
, map
, lordernum
, rordernum
)
192 u_int32_t
*rordernum
;
196 status
= yp_order_host(client
, domain
, map
, rordernum
);
199 if(*rordernum
<= lordernum
) {
202 status
= YPPUSH_SUCC
;
210 get_map(client
,domain
,map
,incallback
)
214 struct ypall_callback
*incallback
;
216 (void)yp_all_host(client
, domain
, map
, incallback
);
221 create_db(domain
,map
,temp_map
)
226 return ypdb_open_suf(temp_map
, O_RDWR
, 0444);
230 install_db(domain
,map
,temp_map
)
235 char db_name
[MAXPATHLEN
];
237 snprintf(db_name
, sizeof db_name
, "%s/%s/%s%s",
238 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
239 rename(temp_map
, db_name
);
245 add_order(db
, ordernum
)
251 char keystr
[] = YP_LAST_KEY
;
254 sprintf(datestr
, "%010u", ordernum
);
257 key
.dsize
= strlen(keystr
);
260 val
.dsize
= strlen(datestr
);
262 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
264 status
= YPPUSH_SUCC
;
272 add_master(client
, domain
, map
, db
)
278 char keystr
[] = YP_MASTER_KEY
;
287 status
= yp_master_host(client
, domain
, map
, &master
);
291 key
.dsize
= strlen(keystr
);
294 val
.dsize
= strlen(master
);
296 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
298 status
= YPPUSH_SUCC
;
308 add_interdomain(client
, domain
, map
, db
)
314 char keystr
[] = YP_INTERDOMAIN_KEY
;
320 /* Get INTERDOMAIN */
323 k
.dsize
= strlen(keystr
);
325 status
= yp_match_host(client
, domain
, map
,
326 k
.dptr
, k
.dsize
, &value
, &vallen
);
328 if(status
== 0 && value
) {
333 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
335 status
= YPPUSH_SUCC
;
346 add_secure(client
, domain
, map
, db
)
352 char keystr
[] = YP_SECURE_KEY
;
361 k
.dsize
= strlen(keystr
);
363 status
= yp_match_host(client
, domain
, map
,
364 k
.dptr
, k
.dsize
, &value
, &vallen
);
371 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
373 status
= YPPUSH_SUCC
;
392 status
= YPPUSH_SUCC
;
399 r
= clnt_call(client
, YPPROC_CLEAR
,
400 xdr_void
, 0, xdr_void
, 0, tv
);
401 if(r
!= RPC_SUCCESS
) {
402 clnt_perror(client
, "yp_clear: clnt_call");
410 send_reply(client
,status
,tid
)
416 struct ypresp_xfr resp
;
423 resp
.xfrstat
= status
;
427 r
= clnt_call(client
, 1,
428 xdr_ypresp_xfr
, &resp
, xdr_void
, 0, tv
);
429 if(r
!= RPC_SUCCESS
) {
430 clnt_perror(client
, "yppushresp_xdr: clnt_call");
450 char *srcdomain
= NULL
;
456 u_int32_t ordernum
, new_ordernum
;
457 struct ypall_callback callback
;
459 int status
,xfr_status
;
462 status
= YPPUSH_SUCC
;
465 yp_get_default_domain(&domain
);
467 while ((ch
= getopt(argc
, argv
, "cd:fh:s:C:")) != -1)
473 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
484 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
489 if (optind
+ 3 >= argc
) {
496 prog
= argv
[optind
++];
497 ipadd
= argv
[optind
++];
498 port
= argv
[optind
++];
505 if(optind
+ 1 != argc
) {
512 status
= YPPUSH_BADARGS
;
513 fprintf(stderr
, "usage: %s %s %s\n",
514 "[-cf] [-d domain] [-h host] [-s domain]",
515 "[-C tid prog ipadd port] mapname\n",
522 yplog("ypxfr: Arguments:");
523 yplog("YP clear to local: %s", (cflag
) ? "no" : "yes");
524 yplog(" Force transfer: %s", (fflag
) ? "yes" : "no");
525 yplog(" domain: %s", domain
);
526 yplog(" host: %s", host
);
527 yplog(" source domain: %s", srcdomain
);
528 yplog(" transid: %s", tid
);
529 yplog(" prog: %s", prog
);
530 yplog(" port: %s", port
);
531 yplog(" ipadd: %s", ipadd
);
532 yplog(" map: %s", map
);
537 status
= get_local_ordernum(domain
, map
, &ordernum
);
546 if (srcdomain
== NULL
) {
547 status
= yp_master(domain
,map
,&host
);
549 status
= yp_master(srcdomain
,map
,&host
);
552 status
= YPPUSH_SUCC
;
559 /* XXX this is raceable if portmap has holes! */
562 yplog("Check for reserved port on host: %s", host
);
564 srvport
= getrpcport(host
,YPPROG
,YPVERS
,IPPROTO_TCP
);
565 if (srvport
>= IPPORT_RESERVED
)
566 status
= YPPUSH_REFUSED
;
572 yplog("Connect host: %s", host
);
574 client
= yp_bind_host(host
,YPPROG
,YPVERS
,0,1);
576 status
= get_remote_ordernum(client
, domain
, map
,
577 ordernum
, &new_ordernum
);
581 if (status
== YPPUSH_SUCC
) {
582 char tmpmapname
[MAXPATHLEN
];
585 /* Create temporary db */
586 snprintf(tmpmapname
, sizeof tmpmapname
,
587 "%s/%s/ypdbXXXXXXXXXX", YP_DB_PATH
, domain
);
588 fd
= mkstemp(tmpmapname
);
595 db
= create_db(domain
,map
,tmpmapname
);
602 status
= add_order(db
, new_ordernum
);
607 status
= add_master(client
,domain
,map
,db
);
610 /* Add INTERDOMAIN */
612 status
= add_interdomain(client
,domain
,map
,db
);
617 status
= add_secure(client
,domain
,map
,db
);
621 callback
.foreach
=ypxfr_foreach
;
622 get_map(client
,domain
,map
,&callback
);
632 status
= install_db(domain
,map
,tmpmapname
);
635 status
= YPPUSH_SUCC
;
643 clnt_destroy(client
);
649 client
= yp_bind_local(YPPROG
,YPVERS
);
650 status
= send_clear(client
);
651 clnt_destroy(client
);
656 client
= yp_bind_host(ipadd
,
661 status
= send_reply(client
,xfr_status
,atoi(tid
));
662 clnt_destroy(client
);