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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* $OpenBSD: yplib_host.c,v 1.15 2006/04/03 05:01:20 deraadt Exp $ */
/*
* Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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[] = "$OpenBSD: yplib_host.c,v 1.15 2006/04/03 05:01:20 deraadt Exp $";
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp.h>
#include <rpcsvc/ypclnt.h>
#include "yplib_host.h"
extern int (*ypresp_allfn)(u_long, char *, int, char *, int, void *);
extern void *ypresp_data;
int _yplib_host_timeout = 10;
CLIENT *
yp_bind_host(char *server, u_long program, u_long version, u_short port,
int usetcp)
{
struct sockaddr_in rsrv_sin;
static CLIENT *client;
struct hostent *h;
struct timeval tv;
int rsrv_sock;
memset(&rsrv_sin, 0, sizeof rsrv_sin);
rsrv_sin.sin_len = sizeof rsrv_sin;
rsrv_sin.sin_family = AF_INET;
rsrv_sock = RPC_ANYSOCK;
if (port != 0)
rsrv_sin.sin_port = htons(port);
if (*server >= '0' && *server <= '9') {
if (inet_aton(server, &rsrv_sin.sin_addr) == 0) {
fprintf(stderr, "inet_aton: invalid address %s.\n",
server);
exit(1);
}
} else {
h = gethostbyname(server);
if (h == NULL) {
fprintf(stderr, "gethostbyname: unknown host %s.\n",
server);
exit(1);
}
rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr;
}
tv.tv_sec = 10;
tv.tv_usec = 0;
if (usetcp)
client = clnttcp_create(&rsrv_sin, program, version,
&rsrv_sock, 0, 0);
else
client = clntudp_create(&rsrv_sin, program, version, tv,
&rsrv_sock);
if (client == NULL) {
fprintf(stderr, "clntudp_create: no contact with host %s.\n",
server);
exit(1);
}
return(client);
}
CLIENT *
yp_bind_local(u_long program, u_long version)
{
struct sockaddr_in rsrv_sin;
static CLIENT *client;
struct timeval tv;
int rsrv_sock;
memset(&rsrv_sin, 0, sizeof rsrv_sin);
rsrv_sin.sin_len = sizeof rsrv_sin;
rsrv_sin.sin_family = AF_INET;
rsrv_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
rsrv_sock = RPC_ANYSOCK;
tv.tv_sec = 10;
tv.tv_usec = 0;
client = clntudp_create(&rsrv_sin, program, version, tv, &rsrv_sock);
if (client == NULL) {
fprintf(stderr,"clntudp_create: no contact with localhost.\n");
exit(1);
}
return(client);
}
int
yp_match_host(CLIENT *client, char *indomain, char *inmap, const char *inkey,
int inkeylen, char **outval, int *outvallen)
{
struct ypresp_val yprv;
struct ypreq_key yprk;
struct timeval tv;
int r;
*outval = NULL;
*outvallen = 0;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprk.domain = indomain;
yprk.map = inmap;
yprk.key.keydat_val = (char *)inkey;
yprk.key.keydat_len = inkeylen;
memset(&yprv, 0, sizeof yprv);
r = clnt_call(client, YPPROC_MATCH,
xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_match_host: clnt_call");
if ( !(r = ypprot_err(yprv.stat)) ) {
*outvallen = yprv.val.valdat_len;
*outval = malloc(*outvallen+1);
memcpy(*outval, yprv.val.valdat_val, *outvallen);
(*outval)[*outvallen] = '\0';
}
xdr_free(xdr_ypresp_val, (char *)&yprv);
return r;
}
int
yp_first_host(CLIENT *client, char *indomain, char *inmap, char **outkey,
int *outkeylen, char **outval, int *outvallen)
{
struct ypresp_key_val yprkv;
struct ypreq_nokey yprnk;
struct timeval tv;
int r;
*outkey = *outval = NULL;
*outkeylen = *outvallen = 0;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprnk.domain = indomain;
yprnk.map = inmap;
memset(&yprkv, 0, sizeof yprkv);
r = clnt_call(client, YPPROC_FIRST,
xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_first_host: clnt_call");
if ( !(r = ypprot_err(yprkv.stat)) ) {
*outkeylen = yprkv.key.keydat_len;
*outkey = malloc(*outkeylen+1);
memcpy(*outkey, yprkv.key.keydat_val, *outkeylen);
(*outkey)[*outkeylen] = '\0';
*outvallen = yprkv.val.valdat_len;
*outval = malloc(*outvallen+1);
memcpy(*outval, yprkv.val.valdat_val, *outvallen);
(*outval)[*outvallen] = '\0';
}
xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
return r;
}
int
yp_next_host(CLIENT *client, char *indomain, char *inmap, char *inkey,
int inkeylen, char **outkey, int *outkeylen, char **outval, int *outvallen)
{
struct ypresp_key_val yprkv;
struct ypreq_key yprk;
struct timeval tv;
int r;
*outkey = *outval = NULL;
*outkeylen = *outvallen = 0;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprk.domain = indomain;
yprk.map = inmap;
yprk.key.keydat_val = inkey;
yprk.key.keydat_len = inkeylen;
memset(&yprkv, 0, sizeof yprkv);
r = clnt_call(client, YPPROC_NEXT,
xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_next_host: clnt_call");
if ( !(r = ypprot_err(yprkv.stat)) ) {
*outkeylen = yprkv.key.keydat_len;
*outkey = malloc(*outkeylen+1);
memcpy(*outkey, yprkv.key.keydat_val, *outkeylen);
(*outkey)[*outkeylen] = '\0';
*outvallen = yprkv.val.valdat_len;
*outval = malloc(*outvallen+1);
memcpy(*outval, yprkv.val.valdat_val, *outvallen);
(*outval)[*outvallen] = '\0';
}
xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
return r;
}
int
yp_all_host(CLIENT *client, char *indomain, char *inmap,
struct ypall_callback *incallback)
{
struct ypreq_nokey yprnk;
struct timeval tv;
u_long status;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprnk.domain = indomain;
yprnk.map = inmap;
ypresp_allfn = incallback->foreach;
ypresp_data = (void *)incallback->data;
(void) clnt_call(client, YPPROC_ALL,
xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
if (status != YP_FALSE)
return ypprot_err(status);
return 0;
}
int
yp_order_host(CLIENT *client, char *indomain, char *inmap, u_int32_t *outorder)
{
struct ypresp_order ypro;
struct ypreq_nokey yprnk;
struct timeval tv;
int r;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprnk.domain = indomain;
yprnk.map = inmap;
memset(&ypro, 0, sizeof ypro);
r = clnt_call(client, YPPROC_ORDER,
xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_order_host: clnt_call");
*outorder = ypro.ordernum;
xdr_free(xdr_ypresp_order, (char *)&ypro);
return ypprot_err(ypro.stat);
}
int
yp_master_host(CLIENT *client, char *indomain, char *inmap, char **outname)
{
struct ypresp_master yprm;
struct ypreq_nokey yprnk;
struct timeval tv;
int r;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
yprnk.domain = indomain;
yprnk.map = inmap;
memset(&yprm, 0, sizeof yprm);
r = clnt_call(client, YPPROC_MASTER,
xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_master: clnt_call");
if (!(r = ypprot_err(yprm.stat)))
*outname = strdup(yprm.peer);
xdr_free(xdr_ypresp_master, (char *)&yprm);
return r;
}
int
yp_maplist_host(CLIENT *client, char *indomain, struct ypmaplist **outmaplist)
{
struct ypresp_maplist ypml;
struct timeval tv;
int r;
tv.tv_sec = _yplib_host_timeout;
tv.tv_usec = 0;
memset(&ypml, 0, sizeof ypml);
r = clnt_call(client, YPPROC_MAPLIST,
xdr_domainname, &indomain, xdr_ypresp_maplist, &ypml, tv);
if (r != RPC_SUCCESS)
clnt_perror(client, "yp_maplist: clnt_call");
*outmaplist = ypml.maps;
/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
return ypprot_err(ypml.stat);
}
|