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