ypxfr_misc.c revision 16132
113007Swpaul/*
213007Swpaul * Copyright (c) 1995
313007Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
413007Swpaul *
513007Swpaul * Redistribution and use in source and binary forms, with or without
613007Swpaul * modification, are permitted provided that the following conditions
713007Swpaul * are met:
813007Swpaul * 1. Redistributions of source code must retain the above copyright
913007Swpaul *    notice, this list of conditions and the following disclaimer.
1013007Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1113007Swpaul *    notice, this list of conditions and the following disclaimer in the
1213007Swpaul *    documentation and/or other materials provided with the distribution.
1313007Swpaul * 3. All advertising materials mentioning features or use of this software
1413007Swpaul *    must display the following acknowledgement:
1513007Swpaul *	This product includes software developed by Bill Paul.
1613007Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1713007Swpaul *    may be used to endorse or promote products derived from this software
1813007Swpaul *    without specific prior written permission.
1913007Swpaul *
2013007Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2113007Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2213007Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2313007Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2413007Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2513007Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2613007Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2713007Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2813007Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2913007Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3013007Swpaul * SUCH DAMAGE.
3113007Swpaul *
3216132Swpaul *	$Id: ypxfr_misc.c,v 1.8 1996/06/02 05:12:00 wpaul Exp $
3313007Swpaul */
3413895Swpaul#include <stdlib.h>
3513895Swpaul#include <unistd.h>
3613895Swpaul#include <sys/param.h>
3713007Swpaul#include <rpc/rpc.h>
3813007Swpaul#include <rpcsvc/yp.h>
3913007Swpaulstruct dom_binding {};
4013007Swpaul#include <rpcsvc/ypclnt.h>
4113007Swpaul#include "ypxfr_extern.h"
4213007Swpaul
4313007Swpaul#ifndef lint
4416132Swpaulstatic const char rcsid[] = "$Id: ypxfr_misc.c,v 1.8 1996/06/02 05:12:00 wpaul Exp $";
4513007Swpaul#endif
4613007Swpaul
4713007Swpaulchar *ypxfrerr_string(code)
4813007Swpaul	ypxfrstat code;
4913007Swpaul{
5013007Swpaul	switch(code) {
5113007Swpaul	case YPXFR_SUCC:
5213007Swpaul		return ("Map successfully transfered");
5313007Swpaul		break;
5413007Swpaul	case YPXFR_AGE:
5513007Swpaul		return ("Master's version not newer");
5613007Swpaul		break;
5713007Swpaul	case YPXFR_NOMAP:
5813007Swpaul		return ("No such map in server's domain");
5913007Swpaul		break;
6013007Swpaul	case YPXFR_NODOM:
6113007Swpaul		return ("Domain not supported by server");
6213007Swpaul		break;
6313007Swpaul	case YPXFR_RSRC:
6413007Swpaul		return ("Local resource allocation failure");
6513007Swpaul		break;
6613007Swpaul	case YPXFR_RPC:
6713007Swpaul		return ("RPC failure talking to server");
6813007Swpaul		break;
6913007Swpaul	case YPXFR_MADDR:
7013007Swpaul		return ("Could not get master server address");
7113007Swpaul		break;
7213007Swpaul	case YPXFR_YPERR:
7313007Swpaul		return ("NIS server/map database error");
7413007Swpaul		break;
7513007Swpaul	case YPXFR_BADARGS:
7613007Swpaul		return ("Request arguments bad");
7713007Swpaul		break;
7813007Swpaul	case YPXFR_DBM:
7913007Swpaul		return ("Local database operation failed");
8013007Swpaul		break;
8113007Swpaul	case YPXFR_FILE:
8213007Swpaul		return ("Local file I/O operation failed");
8313007Swpaul		break;
8413007Swpaul	case YPXFR_SKEW:
8513007Swpaul		return ("Map version skew during transfer");
8613007Swpaul		break;
8713007Swpaul	case YPXFR_CLEAR:
8813007Swpaul		return ("Couldn't send \"clear\" request to local ypserv");
8913007Swpaul		break;
9013007Swpaul	case YPXFR_FORCE:
9113007Swpaul		return ("No local order number in map -- use -f flag");
9213007Swpaul		break;
9313007Swpaul	case YPXFR_XFRERR:
9413007Swpaul		return ("General ypxfr error");
9513007Swpaul		break;
9613007Swpaul	case YPXFR_REFUSED:
9713007Swpaul		return ("Transfer request refused by ypserv");
9813007Swpaul		break;
9913007Swpaul	default:
10013007Swpaul		return ("Unknown error code");
10113007Swpaul		break;
10213007Swpaul	}
10313007Swpaul}
10413007Swpaul
10513007Swpaul/*
10613007Swpaul * These are wrappers for the usual yp_master() and yp_order() functions.
10713007Swpaul * They can use either local yplib functions (the real yp_master() and
10813007Swpaul * yp_order()) or do direct RPCs to a specified server. The latter is
10913007Swpaul * necessary if ypxfr is run on a machine that isn't configured as an
11013007Swpaul * NIS client (this can happen very easily: a given machine need not be
11113007Swpaul * an NIS client in order to be an NIS server).
11213007Swpaul */
11313007Swpaul
11413895Swpaul/*
11513895Swpaul * Careful: yp_master() returns a pointer to a dynamically allocated
11613895Swpaul * buffer. Calling ypproc_master_2() ourselves also returns a pointer
11713895Swpaul * to dynamically allocated memory, though this time it's memory
11813895Swpaul * allocated by the XDR routines. We have to rememver to free() or
11913895Swpaul * xdr_free() the memory as required to avoid leaking memory.
12013895Swpaul */
12113007Swpaulchar *ypxfr_get_master(domain,map,source,yplib)
12213007Swpaul	char *domain;
12313007Swpaul	char *map;
12413007Swpaul	char *source;
12513007Swpaul	const int yplib;
12613007Swpaul{
12713895Swpaul	static char mastername[MAXPATHLEN + 2];
12813895Swpaul
12913895Swpaul	bzero((char *)&mastername, sizeof(mastername));
13013895Swpaul
13113007Swpaul	if (yplib) {
13213007Swpaul		int res;
13313007Swpaul		char *master;
13413007Swpaul		if ((res = yp_master(domain, map, &master))) {
13513007Swpaul			switch (res) {
13613007Swpaul			case YPERR_DOMAIN:
13713007Swpaul				yp_errno = YPXFR_NODOM;
13813007Swpaul				break;
13913007Swpaul			case YPERR_MAP:
14013007Swpaul				yp_errno = YPXFR_NOMAP;
14113007Swpaul				break;
14213007Swpaul			case YPERR_YPERR:
14313007Swpaul			default:
14413007Swpaul				yp_errno = YPXFR_YPERR;
14513007Swpaul				break;
14613007Swpaul			}
14713007Swpaul			return(NULL);
14813895Swpaul		} else {
14913895Swpaul			snprintf(mastername, sizeof(mastername), "%s", master);
15013895Swpaul			free(master);
15113895Swpaul			return((char *)&mastername);
15213895Swpaul		}
15313007Swpaul	} else {
15413007Swpaul		CLIENT *clnt;
15513007Swpaul		ypresp_master *resp;
15613007Swpaul		ypreq_nokey req;
15713007Swpaul
15813007Swpaul		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
15913007Swpaul			yp_error("%s",clnt_spcreateerror("failed to \
16013007Swpaulcreate udp handle to ypserv"));
16113007Swpaul			yp_errno = YPXFR_RPC;
16213007Swpaul			return(NULL);
16313007Swpaul		}
16413007Swpaul
16513007Swpaul		req.map = map;
16613007Swpaul		req.domain = domain;
16713007Swpaul		if ((resp = ypproc_master_2(&req, clnt)) == NULL) {
16813007Swpaul			yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \
16913007Swpaulfailed"));
17013007Swpaul			clnt_destroy(clnt);
17113007Swpaul			yp_errno = YPXFR_RPC;
17213007Swpaul			return(NULL);
17313007Swpaul		}
17413007Swpaul		clnt_destroy(clnt);
17513007Swpaul		if (resp->stat != YP_TRUE) {
17613007Swpaul			switch (resp->stat) {
17713007Swpaul			case YP_NODOM:
17813007Swpaul				yp_errno = YPXFR_NODOM;
17913007Swpaul				break;
18013007Swpaul			case YP_NOMAP:
18113007Swpaul				yp_errno = YPXFR_NOMAP;
18213007Swpaul				break;
18313007Swpaul			case YP_YPERR:
18413007Swpaul			default:
18513007Swpaul				yp_errno = YPXFR_YPERR;
18613007Swpaul				break;
18713007Swpaul			}
18813007Swpaul			return(NULL);
18913007Swpaul		}
19013895Swpaul		snprintf(mastername, sizeof(mastername), "%s", resp->peer);
19116132Swpaul/*		xdr_free(xdr_ypresp_master, (char *)&resp); */
19213895Swpaul		return((char *)&mastername);
19313007Swpaul	}
19413007Swpaul}
19513007Swpaul
19613007Swpaulunsigned long ypxfr_get_order(domain, map, source, yplib)
19713007Swpaul	char *domain;
19813007Swpaul	char *map;
19913007Swpaul	char *source;
20013007Swpaul	const int yplib;
20113007Swpaul{
20213007Swpaul	if (yplib) {
20313007Swpaul		unsigned long order;
20413007Swpaul		int res;
20513007Swpaul		if ((res = yp_order(domain, map, (int *)&order))) {
20613007Swpaul			switch (res) {
20713895Swpaul			case YPERR_DOMAIN:
20813007Swpaul				yp_errno = YPXFR_NODOM;
20913007Swpaul				break;
21013895Swpaul			case YPERR_MAP:
21113007Swpaul				yp_errno = YPXFR_NOMAP;
21213007Swpaul				break;
21313895Swpaul			case YPERR_YPERR:
21413007Swpaul			default:
21513007Swpaul				yp_errno = YPXFR_YPERR;
21613007Swpaul				break;
21713007Swpaul			}
21813007Swpaul			return(0);
21913007Swpaul		} else
22013007Swpaul			return(order);
22113007Swpaul	} else {
22213007Swpaul		CLIENT *clnt;
22313007Swpaul		ypresp_order *resp;
22413007Swpaul		ypreq_nokey req;
22513007Swpaul
22613007Swpaul		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
22713007Swpaul			yp_error("%s",clnt_spcreateerror("couldn't create \
22813007Swpauludp handle to ypserv"));
22913007Swpaul			yp_errno = YPXFR_RPC;
23013007Swpaul			return(0);
23113007Swpaul		}
23213007Swpaul		req.map = map;
23313007Swpaul		req.domain = domain;
23413007Swpaul		if ((resp = ypproc_order_2(&req, clnt)) == NULL) {
23513007Swpaul			yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \
23613007Swpaulfailed"));
23713007Swpaul			clnt_destroy(clnt);
23813007Swpaul			yp_errno = YPXFR_RPC;
23913007Swpaul			return(0);
24013007Swpaul		}
24113007Swpaul		clnt_destroy(clnt);
24213007Swpaul		if (resp->stat != YP_TRUE) {
24313007Swpaul			switch (resp->stat) {
24413895Swpaul			case YP_NODOM:
24513007Swpaul				yp_errno = YPXFR_NODOM;
24613007Swpaul				break;
24713895Swpaul			case YP_NOMAP:
24813007Swpaul				yp_errno = YPXFR_NOMAP;
24913007Swpaul				break;
25013895Swpaul			case YP_YPERR:
25113007Swpaul			default:
25213007Swpaul				yp_errno = YPXFR_YPERR;
25313007Swpaul				break;
25413007Swpaul			}
25513007Swpaul			return(0);
25613007Swpaul		}
25713007Swpaul		return(resp->ordernum);
25813007Swpaul	}
25913007Swpaul}
260