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. 31 * 32 * $FreeBSD: src/libexec/ypxfr/ypxfr_misc.c,v 1.9.2.2 2002/02/15 00:46:54 des Exp $ 33 * $DragonFly: src/libexec/ypxfr/ypxfr_misc.c,v 1.2 2003/06/17 04:27:08 dillon Exp $ 34 */ 35 36 #include <stdio.h> 37 #include <string.h> 38 #include <stdlib.h> 39 #include <unistd.h> 40 #include <sys/param.h> 41 #include <rpc/rpc.h> 42 #include <rpcsvc/yp.h> 43 struct dom_binding {}; 44 #include <rpcsvc/ypclnt.h> 45 #include "ypxfr_extern.h" 46 47 char * 48 ypxfrerr_string(ypxfrstat code) 49 { 50 switch (code) { 51 case YPXFR_SUCC: 52 return ("Map successfully transferred"); 53 break; 54 case YPXFR_AGE: 55 return ("Master's version not newer"); 56 break; 57 case YPXFR_NOMAP: 58 return ("No such map in server's domain"); 59 break; 60 case YPXFR_NODOM: 61 return ("Domain not supported by server"); 62 break; 63 case YPXFR_RSRC: 64 return ("Local resource allocation failure"); 65 break; 66 case YPXFR_RPC: 67 return ("RPC failure talking to server"); 68 break; 69 case YPXFR_MADDR: 70 return ("Could not get master server address"); 71 break; 72 case YPXFR_YPERR: 73 return ("NIS server/map database error"); 74 break; 75 case YPXFR_BADARGS: 76 return ("Request arguments bad"); 77 break; 78 case YPXFR_DBM: 79 return ("Local database operation failed"); 80 break; 81 case YPXFR_FILE: 82 return ("Local file I/O operation failed"); 83 break; 84 case YPXFR_SKEW: 85 return ("Map version skew during transfer"); 86 break; 87 case YPXFR_CLEAR: 88 return ("Couldn't send \"clear\" request to local ypserv"); 89 break; 90 case YPXFR_FORCE: 91 return ("No local order number in map -- use -f flag"); 92 break; 93 case YPXFR_XFRERR: 94 return ("General ypxfr error"); 95 break; 96 case YPXFR_REFUSED: 97 return ("Transfer request refused by ypserv"); 98 break; 99 default: 100 return ("Unknown error code"); 101 break; 102 } 103 } 104 105 /* 106 * These are wrappers for the usual yp_master() and yp_order() functions. 107 * They can use either local yplib functions (the real yp_master() and 108 * yp_order()) or do direct RPCs to a specified server. The latter is 109 * necessary if ypxfr is run on a machine that isn't configured as an 110 * NIS client (this can happen very easily: a given machine need not be 111 * an NIS client in order to be an NIS server). 112 */ 113 114 /* 115 * Careful: yp_master() returns a pointer to a dynamically allocated 116 * buffer. Calling ypproc_master_2() ourselves also returns a pointer 117 * to dynamically allocated memory, though this time it's memory 118 * allocated by the XDR routines. We have to rememver to free() or 119 * xdr_free() the memory as required to avoid leaking memory. 120 */ 121 char * 122 ypxfr_get_master(char *domain, char *map, char *source, const int yplib) 123 { 124 static char mastername[MAXPATHLEN + 2]; 125 126 bzero((char *)&mastername, sizeof(mastername)); 127 128 if (yplib) { 129 int res; 130 char *master; 131 if ((res = yp_master(domain, map, &master))) { 132 switch (res) { 133 case YPERR_DOMAIN: 134 yp_errno = YPXFR_NODOM; 135 break; 136 case YPERR_MAP: 137 yp_errno = YPXFR_NOMAP; 138 break; 139 case YPERR_YPERR: 140 default: 141 yp_errno = YPXFR_YPERR; 142 break; 143 } 144 return(NULL); 145 } else { 146 snprintf(mastername, sizeof(mastername), "%s", master); 147 free(master); 148 return((char *)&mastername); 149 } 150 } else { 151 CLIENT *clnt; 152 ypresp_master *resp; 153 ypreq_nokey req; 154 155 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) { 156 yp_error("%s",clnt_spcreateerror("failed to \ 157 create udp handle to ypserv")); 158 yp_errno = YPXFR_RPC; 159 return(NULL); 160 } 161 162 req.map = map; 163 req.domain = domain; 164 if ((resp = ypproc_master_2(&req, clnt)) == NULL) { 165 yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \ 166 failed")); 167 clnt_destroy(clnt); 168 yp_errno = YPXFR_RPC; 169 return(NULL); 170 } 171 clnt_destroy(clnt); 172 if (resp->stat != YP_TRUE) { 173 switch (resp->stat) { 174 case YP_NODOM: 175 yp_errno = YPXFR_NODOM; 176 break; 177 case YP_NOMAP: 178 yp_errno = YPXFR_NOMAP; 179 break; 180 case YP_YPERR: 181 default: 182 yp_errno = YPXFR_YPERR; 183 break; 184 } 185 return(NULL); 186 } 187 snprintf(mastername, sizeof(mastername), "%s", resp->peer); 188 /* xdr_free(xdr_ypresp_master, (char *)&resp); */ 189 return((char *)&mastername); 190 } 191 } 192 193 unsigned long 194 ypxfr_get_order(char *domain, char *map, char *source, const int yplib) 195 { 196 if (yplib) { 197 unsigned long order; 198 int res; 199 if ((res = yp_order(domain, map, (int *)&order))) { 200 switch (res) { 201 case YPERR_DOMAIN: 202 yp_errno = YPXFR_NODOM; 203 break; 204 case YPERR_MAP: 205 yp_errno = YPXFR_NOMAP; 206 break; 207 case YPERR_YPERR: 208 default: 209 yp_errno = YPXFR_YPERR; 210 break; 211 } 212 return(0); 213 } else 214 return(order); 215 } else { 216 CLIENT *clnt; 217 ypresp_order *resp; 218 ypreq_nokey req; 219 220 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) { 221 yp_error("%s",clnt_spcreateerror("couldn't create \ 222 udp handle to ypserv")); 223 yp_errno = YPXFR_RPC; 224 return(0); 225 } 226 req.map = map; 227 req.domain = domain; 228 if ((resp = ypproc_order_2(&req, clnt)) == NULL) { 229 yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \ 230 failed")); 231 clnt_destroy(clnt); 232 yp_errno = YPXFR_RPC; 233 return(0); 234 } 235 clnt_destroy(clnt); 236 if (resp->stat != YP_TRUE) { 237 switch (resp->stat) { 238 case YP_NODOM: 239 yp_errno = YPXFR_NODOM; 240 break; 241 case YP_NOMAP: 242 yp_errno = YPXFR_NOMAP; 243 break; 244 case YP_YPERR: 245 default: 246 yp_errno = YPXFR_YPERR; 247 break; 248 } 249 return(0); 250 } 251 return(resp->ordernum); 252 } 253 } 254 255 int 256 ypxfr_match(char *server, char *domain, char *map, char *key, 257 unsigned long keylen) 258 { 259 ypreq_key ypkey; 260 ypresp_val *ypval; 261 CLIENT *clnt; 262 static char buf[YPMAXRECORD + 2]; 263 264 bzero((char *)buf, sizeof(buf)); 265 266 if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) { 267 yp_error("failed to create UDP handle: %s", 268 clnt_spcreateerror(server)); 269 return(0); 270 } 271 272 ypkey.domain = domain; 273 ypkey.map = map; 274 ypkey.key.keydat_len = keylen; 275 ypkey.key.keydat_val = key; 276 277 if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) { 278 clnt_destroy(clnt); 279 yp_error("%s: %s", server, 280 clnt_sperror(clnt,"YPPROC_MATCH failed")); 281 return(0); 282 } 283 284 clnt_destroy(clnt); 285 286 if (ypval->stat != YP_TRUE) { 287 xdr_free((xdrproc_t)xdr_ypresp_val, ypval); 288 return(0); 289 } 290 291 xdr_free((xdrproc_t)xdr_ypresp_val, ypval); 292 293 return(1); 294 } 295