]>
git.saurik.com Git - apple/network_cmds.git/blob - ypxfr.tproj/ypxfr.c
ee763f7391155bd48e5a9be100cab5bba2e638e2
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $ */
28 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by Mats O Jansson
42 * 4. The name of the author may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
46 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
49 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static char rcsid
[] = "$OpenBSD: ypxfr.c,v 1.22 1997/07/30 12:07:02 maja Exp $";
62 #include <sys/types.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
78 #include <rpcsvc/yp.h>
79 #include <rpcsvc/ypclnt.h>
81 #include "yplib_host.h"
86 extern char *__progname
; /* from crt0.o */
89 extern bool_t
xdr_ypresp_all_seq();
91 extern int (*ypresp_allfn
)();
92 extern void *ypresp_data
;
95 ypxfr_foreach(status
,keystr
,keylen
,valstr
,vallen
,data
)
96 int status
,keylen
,vallen
;
97 char *keystr
,*valstr
,*data
;
101 if (status
== YP_NOMORE
)
104 keystr
[keylen
] = '\0';
105 valstr
[vallen
] = '\0';
108 key
.dsize
= strlen(keystr
);
111 val
.dsize
= strlen(valstr
);
113 ypdb_store(db
, key
, val
, YPDB_INSERT
);
119 get_local_ordernum(domain
, map
, lordernum
)
122 u_int32_t
*lordernum
;
124 char map_path
[MAXPATHLEN
];
125 char order_key
[] = YP_LAST_KEY
;
126 char order
[MAX_LAST_LEN
+1];
132 /* This routine returns YPPUSH_SUCC or YPPUSH_NODOM */
134 status
= YPPUSH_SUCC
;
136 snprintf(map_path
, sizeof map_path
, "%s/%s", YP_DB_PATH
, domain
);
137 if (!((stat(map_path
, &finfo
) == 0) &&
138 ((finfo
.st_mode
& S_IFMT
) == S_IFDIR
))) {
139 fprintf(stderr
, "%s: domain %s not found locally\n",
141 status
= YPPUSH_NODOM
;
145 snprintf(map_path
, sizeof map_path
, "%s/%s/%s%s",
146 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
147 if(!(stat(map_path
, &finfo
) == 0)) {
148 status
= YPPUSH_NOMAP
;
153 snprintf(map_path
, sizeof map_path
, "%s/%s/%s",
154 YP_DB_PATH
, domain
, map
);
155 db
= ypdb_open(map_path
, O_RDONLY
, 0444);
163 k
.dptr
= (char *)&order_key
;
164 k
.dsize
= YP_LAST_LEN
;
166 v
= ypdb_fetch(db
,k
);
169 if (v
.dptr
== NULL
) {
172 strncpy(order
, v
.dptr
, sizeof order
-1);
173 order
[sizeof order
-1] = '\0';
174 *lordernum
= (u_int32_t
)atol(order
);
178 if((status
== YPPUSH_NOMAP
) || (status
== YPPUSH_DBM
)) {
180 status
= YPPUSH_SUCC
;
188 get_remote_ordernum(client
, domain
, map
, lordernum
, rordernum
)
193 u_int32_t
*rordernum
;
197 status
= yp_order_host(client
, domain
, map
, rordernum
);
200 if(*rordernum
<= lordernum
) {
203 status
= YPPUSH_SUCC
;
211 get_map(client
,domain
,map
,incallback
)
215 struct ypall_callback
*incallback
;
217 (void)yp_all_host(client
, domain
, map
, incallback
);
222 create_db(domain
,map
,temp_map
)
227 return ypdb_open_suf(temp_map
, O_RDWR
, 0444);
231 install_db(domain
,map
,temp_map
)
236 char db_name
[MAXPATHLEN
];
238 snprintf(db_name
, sizeof db_name
, "%s/%s/%s%s",
239 YP_DB_PATH
, domain
, map
, YPDB_SUFFIX
);
240 rename(temp_map
, db_name
);
246 add_order(db
, ordernum
)
252 char keystr
[] = YP_LAST_KEY
;
255 sprintf(datestr
, "%010u", ordernum
);
258 key
.dsize
= strlen(keystr
);
261 val
.dsize
= strlen(datestr
);
263 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
265 status
= YPPUSH_SUCC
;
273 add_master(client
, domain
, map
, db
)
279 char keystr
[] = YP_MASTER_KEY
;
288 status
= yp_master_host(client
, domain
, map
, &master
);
292 key
.dsize
= strlen(keystr
);
295 val
.dsize
= strlen(master
);
297 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
299 status
= YPPUSH_SUCC
;
309 add_interdomain(client
, domain
, map
, db
)
315 char keystr
[] = YP_INTERDOMAIN_KEY
;
321 /* Get INTERDOMAIN */
324 k
.dsize
= strlen(keystr
);
326 status
= yp_match_host(client
, domain
, map
,
327 k
.dptr
, k
.dsize
, &value
, &vallen
);
329 if(status
== 0 && value
) {
334 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
336 status
= YPPUSH_SUCC
;
347 add_secure(client
, domain
, map
, db
)
353 char keystr
[] = YP_SECURE_KEY
;
362 k
.dsize
= strlen(keystr
);
364 status
= yp_match_host(client
, domain
, map
,
365 k
.dptr
, k
.dsize
, &value
, &vallen
);
372 status
= ypdb_store(db
,k
,v
,YPDB_INSERT
);
374 status
= YPPUSH_SUCC
;
393 status
= YPPUSH_SUCC
;
400 r
= clnt_call(client
, YPPROC_CLEAR
,
401 xdr_void
, 0, xdr_void
, 0, tv
);
402 if(r
!= RPC_SUCCESS
) {
403 clnt_perror(client
, "yp_clear: clnt_call");
411 send_reply(client
,status
,tid
)
417 struct ypresp_xfr resp
;
424 resp
.xfrstat
= status
;
428 r
= clnt_call(client
, 1,
429 xdr_ypresp_xfr
, &resp
, xdr_void
, 0, tv
);
430 if(r
!= RPC_SUCCESS
) {
431 clnt_perror(client
, "yppushresp_xdr: clnt_call");
451 char *srcdomain
= NULL
;
457 u_int32_t ordernum
, new_ordernum
;
458 struct ypall_callback callback
;
460 int status
,xfr_status
;
463 status
= YPPUSH_SUCC
;
466 yp_get_default_domain(&domain
);
468 while ((ch
= getopt(argc
, argv
, "cd:fh:s:C:")) != -1)
474 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
485 if (strchr(optarg
, '/')) /* Ha ha, we are not listening */
490 if (optind
+ 3 >= argc
) {
497 prog
= argv
[optind
++];
498 ipadd
= argv
[optind
++];
499 port
= argv
[optind
++];
506 if(optind
+ 1 != argc
) {
513 status
= YPPUSH_BADARGS
;
514 fprintf(stderr
, "usage: %s %s %s\n",
515 "[-cf] [-d domain] [-h host] [-s domain]",
516 "[-C tid prog ipadd port] mapname\n",
523 yplog("ypxfr: Arguments:");
524 yplog("YP clear to local: %s", (cflag
) ? "no" : "yes");
525 yplog(" Force transfer: %s", (fflag
) ? "yes" : "no");
526 yplog(" domain: %s", domain
);
527 yplog(" host: %s", host
);
528 yplog(" source domain: %s", srcdomain
);
529 yplog(" transid: %s", tid
);
530 yplog(" prog: %s", prog
);
531 yplog(" port: %s", port
);
532 yplog(" ipadd: %s", ipadd
);
533 yplog(" map: %s", map
);
538 status
= get_local_ordernum(domain
, map
, &ordernum
);
547 if (srcdomain
== NULL
) {
548 status
= yp_master(domain
,map
,&host
);
550 status
= yp_master(srcdomain
,map
,&host
);
553 status
= YPPUSH_SUCC
;
560 /* XXX this is raceable if portmap has holes! */
563 yplog("Check for reserved port on host: %s", host
);
565 srvport
= getrpcport(host
,YPPROG
,YPVERS
,IPPROTO_TCP
);
566 if (srvport
>= IPPORT_RESERVED
)
567 status
= YPPUSH_REFUSED
;
573 yplog("Connect host: %s", host
);
575 client
= yp_bind_host(host
,YPPROG
,YPVERS
,0,1);
577 status
= get_remote_ordernum(client
, domain
, map
,
578 ordernum
, &new_ordernum
);
582 if (status
== YPPUSH_SUCC
) {
583 char tmpmapname
[MAXPATHLEN
];
586 /* Create temporary db */
587 snprintf(tmpmapname
, sizeof tmpmapname
,
588 "%s/%s/ypdbXXXXXXXXXX", YP_DB_PATH
, domain
);
589 fd
= mkstemp(tmpmapname
);
596 db
= create_db(domain
,map
,tmpmapname
);
603 status
= add_order(db
, new_ordernum
);
608 status
= add_master(client
,domain
,map
,db
);
611 /* Add INTERDOMAIN */
613 status
= add_interdomain(client
,domain
,map
,db
);
618 status
= add_secure(client
,domain
,map
,db
);
622 callback
.foreach
=ypxfr_foreach
;
623 get_map(client
,domain
,map
,&callback
);
633 status
= install_db(domain
,map
,tmpmapname
);
636 status
= YPPUSH_SUCC
;
644 clnt_destroy(client
);
650 client
= yp_bind_local(YPPROG
,YPVERS
);
651 status
= send_clear(client
);
652 clnt_destroy(client
);
657 client
= yp_bind_host(ipadd
,
662 status
= send_reply(client
,xfr_status
,atoi(tid
));
663 clnt_destroy(client
);