ypxfr_misc.c revision 15671
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 * $Id: ypxfr_misc.c,v 1.2 1996/02/04 05:18:44 wpaul Exp $ 33 */ 34#include <stdlib.h> 35#include <unistd.h> 36#include <sys/param.h> 37#include <rpc/rpc.h> 38#include <rpcsvc/yp.h> 39struct dom_binding {}; 40#include <rpcsvc/ypclnt.h> 41#include "ypxfr_extern.h" 42 43#ifndef lint 44static const char rcsid[] = "$Id: ypxfr_misc.c,v 1.2 1996/02/04 05:18:44 wpaul Exp $"; 45#endif 46 47char *ypxfrerr_string(code) 48 ypxfrstat code; 49{ 50 switch(code) { 51 case YPXFR_SUCC: 52 return ("Map successfully transfered"); 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 */ 121char *ypxfr_get_master(domain,map,source,yplib) 122 char *domain; 123 char *map; 124 char *source; 125 const int yplib; 126{ 127 static char mastername[MAXPATHLEN + 2]; 128 129 bzero((char *)&mastername, sizeof(mastername)); 130 131 if (yplib) { 132 int res; 133 char *master; 134 if ((res = yp_master(domain, map, &master))) { 135 switch (res) { 136 case YPERR_DOMAIN: 137 yp_errno = YPXFR_NODOM; 138 break; 139 case YPERR_MAP: 140 yp_errno = YPXFR_NOMAP; 141 break; 142 case YPERR_YPERR: 143 default: 144 yp_errno = YPXFR_YPERR; 145 break; 146 } 147 return(NULL); 148 } else { 149 snprintf(mastername, sizeof(mastername), "%s", master); 150 free(master); 151 return((char *)&mastername); 152 } 153 } else { 154 CLIENT *clnt; 155 ypresp_master *resp; 156 ypreq_nokey req; 157 158 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) { 159 yp_error("%s",clnt_spcreateerror("failed to \ 160create udp handle to ypserv")); 161 yp_errno = YPXFR_RPC; 162 return(NULL); 163 } 164 165 req.map = map; 166 req.domain = domain; 167 if ((resp = ypproc_master_2(&req, clnt)) == NULL) { 168 yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \ 169failed")); 170 clnt_destroy(clnt); 171 yp_errno = YPXFR_RPC; 172 return(NULL); 173 } 174 clnt_destroy(clnt); 175 if (resp->stat != YP_TRUE) { 176 switch (resp->stat) { 177 case YP_NODOM: 178 yp_errno = YPXFR_NODOM; 179 break; 180 case YP_NOMAP: 181 yp_errno = YPXFR_NOMAP; 182 break; 183 case YP_YPERR: 184 default: 185 yp_errno = YPXFR_YPERR; 186 break; 187 } 188 return(NULL); 189 } 190 snprintf(mastername, sizeof(mastername), "%s", resp->peer); 191/* xdr_free(xdr_ypresp_master, (char *)&resp); */ 192 return((char *)&mastername); 193 } 194} 195 196unsigned long ypxfr_get_order(domain, map, source, yplib) 197 char *domain; 198 char *map; 199 char *source; 200 const int yplib; 201{ 202 if (yplib) { 203 unsigned long order; 204 int res; 205 if ((res = yp_order(domain, map, (int *)&order))) { 206 switch (res) { 207 case YPERR_DOMAIN: 208 yp_errno = YPXFR_NODOM; 209 break; 210 case YPERR_MAP: 211 yp_errno = YPXFR_NOMAP; 212 break; 213 case YPERR_YPERR: 214 default: 215 yp_errno = YPXFR_YPERR; 216 break; 217 } 218 return(0); 219 } else 220 return(order); 221 } else { 222 CLIENT *clnt; 223 ypresp_order *resp; 224 ypreq_nokey req; 225 226 if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) { 227 yp_error("%s",clnt_spcreateerror("couldn't create \ 228udp handle to ypserv")); 229 yp_errno = YPXFR_RPC; 230 return(0); 231 } 232 req.map = map; 233 req.domain = domain; 234 if ((resp = ypproc_order_2(&req, clnt)) == NULL) { 235 yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \ 236failed")); 237 clnt_destroy(clnt); 238 yp_errno = YPXFR_RPC; 239 return(0); 240 } 241 clnt_destroy(clnt); 242 if (resp->stat != YP_TRUE) { 243 switch (resp->stat) { 244 case YP_NODOM: 245 yp_errno = YPXFR_NODOM; 246 break; 247 case YP_NOMAP: 248 yp_errno = YPXFR_NOMAP; 249 break; 250 case YP_YPERR: 251 default: 252 yp_errno = YPXFR_YPERR; 253 break; 254 } 255 return(0); 256 } 257 return(resp->ordernum); 258 } 259} 260