summaryrefslogtreecommitdiffstats
path: root/libexec/ypxfr/ypxfr_misc.c
blob: ede4f56e632dabca640635bb21d05626956d115a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (c) 1995
 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static const char rcsid[] =
  "$FreeBSD$";
#endif /* not lint */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <rpcsvc/yp.h>
struct dom_binding {};
#include <rpcsvc/ypclnt.h>
#include "ypxfr_extern.h"

char *
ypxfrerr_string(ypxfrstat code)
{
	switch (code) {
	case YPXFR_SUCC:
		return ("Map successfully transferred");
		break;
	case YPXFR_AGE:
		return ("Master's version not newer");
		break;
	case YPXFR_NOMAP:
		return ("No such map in server's domain");
		break;
	case YPXFR_NODOM:
		return ("Domain not supported by server");
		break;
	case YPXFR_RSRC:
		return ("Local resource allocation failure");
		break;
	case YPXFR_RPC:
		return ("RPC failure talking to server");
		break;
	case YPXFR_MADDR:
		return ("Could not get master server address");
		break;
	case YPXFR_YPERR:
		return ("NIS server/map database error");
		break;
	case YPXFR_BADARGS:
		return ("Request arguments bad");
		break;
	case YPXFR_DBM:
		return ("Local database operation failed");
		break;
	case YPXFR_FILE:
		return ("Local file I/O operation failed");
		break;
	case YPXFR_SKEW:
		return ("Map version skew during transfer");
		break;
	case YPXFR_CLEAR:
		return ("Couldn't send \"clear\" request to local ypserv");
		break;
	case YPXFR_FORCE:
		return ("No local order number in map -- use -f flag");
		break;
	case YPXFR_XFRERR:
		return ("General ypxfr error");
		break;
	case YPXFR_REFUSED:
		return ("Transfer request refused by ypserv");
		break;
	default:
		return ("Unknown error code");
		break;
	}
}

/*
 * These are wrappers for the usual yp_master() and yp_order() functions.
 * They can use either local yplib functions (the real yp_master() and
 * yp_order()) or do direct RPCs to a specified server. The latter is
 * necessary if ypxfr is run on a machine that isn't configured as an
 * NIS client (this can happen very easily: a given machine need not be
 * an NIS client in order to be an NIS server).
 */

/*
 * Careful: yp_master() returns a pointer to a dynamically allocated
 * buffer. Calling ypproc_master_2() ourselves also returns a pointer
 * to dynamically allocated memory, though this time it's memory
 * allocated by the XDR routines. We have to rememver to free() or
 * xdr_free() the memory as required to avoid leaking memory.
 */
char *
ypxfr_get_master(char *domain, char *map, char *source, const int yplib)
{
	static char mastername[MAXPATHLEN + 2];

	bzero((char *)&mastername, sizeof(mastername));

	if (yplib) {
		int res;
		char *master;
		if ((res = yp_master(domain, map, &master))) {
			switch (res) {
			case YPERR_DOMAIN:
				yp_errno = YPXFR_NODOM;
				break;
			case YPERR_MAP:
				yp_errno = YPXFR_NOMAP;
				break;
			case YPERR_YPERR:
			default:
				yp_errno = YPXFR_YPERR;
				break;
			}
			return(NULL);
		} else {
			snprintf(mastername, sizeof(mastername), "%s", master);
			free(master);
			return((char *)&mastername);
		}
	} else {
		CLIENT *clnt;
		ypresp_master *resp;
		ypreq_nokey req;

		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
			yp_error("%s",clnt_spcreateerror("failed to \
create udp handle to ypserv"));
			yp_errno = YPXFR_RPC;
			return(NULL);
		}

		req.map = map;
		req.domain = domain;
		if ((resp = ypproc_master_2(&req, clnt)) == NULL) {
			yp_error("%s",clnt_sperror(clnt,"YPPROC_MASTER \
failed"));
			clnt_destroy(clnt);
			yp_errno = YPXFR_RPC;
			return(NULL);
		}
		clnt_destroy(clnt);
		if (resp->stat != YP_TRUE) {
			switch (resp->stat) {
			case YP_NODOM:
				yp_errno = YPXFR_NODOM;
				break;
			case YP_NOMAP:
				yp_errno = YPXFR_NOMAP;
				break;
			case YP_YPERR:
			default:
				yp_errno = YPXFR_YPERR;
				break;
			}
			return(NULL);
		}
		snprintf(mastername, sizeof(mastername), "%s", resp->peer);
/*		xdr_free(xdr_ypresp_master, (char *)&resp); */
		return((char *)&mastername);
	}
}

unsigned long
ypxfr_get_order(char *domain, char *map, char *source, const int yplib)
{
	if (yplib) {
		unsigned long order;
		int res;
		if ((res = yp_order(domain, map, (int *)&order))) {
			switch (res) {
			case YPERR_DOMAIN:
				yp_errno = YPXFR_NODOM;
				break;
			case YPERR_MAP:
				yp_errno = YPXFR_NOMAP;
				break;
			case YPERR_YPERR:
			default:
				yp_errno = YPXFR_YPERR;
				break;
			}
			return(0);
		} else
			return(order);
	} else {
		CLIENT *clnt;
		ypresp_order *resp;
		ypreq_nokey req;

		if ((clnt = clnt_create(source,YPPROG,YPVERS,"udp")) == NULL) {
			yp_error("%s",clnt_spcreateerror("couldn't create \
udp handle to ypserv"));
			yp_errno = YPXFR_RPC;
			return(0);
		}
		req.map = map;
		req.domain = domain;
		if ((resp = ypproc_order_2(&req, clnt)) == NULL) {
			yp_error("%s", clnt_sperror(clnt, "YPPROC_ORDER \
failed"));
			clnt_destroy(clnt);
			yp_errno = YPXFR_RPC;
			return(0);
		}
		clnt_destroy(clnt);
		if (resp->stat != YP_TRUE) {
			switch (resp->stat) {
			case YP_NODOM:
				yp_errno = YPXFR_NODOM;
				break;
			case YP_NOMAP:
				yp_errno = YPXFR_NOMAP;
				break;
			case YP_YPERR:
			default:
				yp_errno = YPXFR_YPERR;
				break;
			}
			return(0);
		}
		return(resp->ordernum);
	}
}

int
ypxfr_match(char *server, char *domain, char *map, char *key,
    unsigned long keylen)
{
	ypreq_key ypkey;
	ypresp_val *ypval;
	CLIENT *clnt;
	static char buf[YPMAXRECORD + 2];

	bzero((char *)buf, sizeof(buf));

	if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) {
		yp_error("failed to create UDP handle: %s",
					clnt_spcreateerror(server));
		return(0);
	}

	ypkey.domain = domain;
	ypkey.map = map;
	ypkey.key.keydat_len = keylen;
	ypkey.key.keydat_val = key;

	if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) {
		clnt_destroy(clnt);
		yp_error("%s: %s", server,
				clnt_sperror(clnt,"YPPROC_MATCH failed"));
		return(0);
	}

	clnt_destroy(clnt);

	if (ypval->stat != YP_TRUE) {
		xdr_free(xdr_ypresp_val, (char *)ypval);
		return(0);
	}

	xdr_free(xdr_ypresp_val, (char *)ypval);

	return(1);
}
OpenPOWER on IntegriCloud