ypbind.c revision 1.1 1 1.1 deraadt #include <sys/param.h>
2 1.1 deraadt #include <sys/types.h>
3 1.1 deraadt #include <sys/ioctl.h>
4 1.1 deraadt #include <sys/signal.h>
5 1.1 deraadt #include <sys/socket.h>
6 1.1 deraadt #include <sys/file.h>
7 1.1 deraadt #include <sys/fcntl.h>
8 1.1 deraadt #include <sys/syslog.h>
9 1.1 deraadt #include <stdio.h>
10 1.1 deraadt #include <errno.h>
11 1.1 deraadt #include <ctype.h>
12 1.1 deraadt #include <dirent.h>
13 1.1 deraadt #include <netdb.h>
14 1.1 deraadt #include <string.h>
15 1.1 deraadt #include <rpc/rpc.h>
16 1.1 deraadt #include <rpc/xdr.h>
17 1.1 deraadt #include <net/if.h>
18 1.1 deraadt #include <arpa/inet.h>
19 1.1 deraadt #include <rpc/pmap_clnt.h>
20 1.1 deraadt #include <rpc/pmap_prot.h>
21 1.1 deraadt #include <rpc/pmap_rmt.h>
22 1.1 deraadt #include <unistd.h>
23 1.1 deraadt #include <rpcsvc/yp_prot.h>
24 1.1 deraadt #include <rpcsvc/ypclnt.h>
25 1.1 deraadt
26 1.1 deraadt #ifndef BINDINGDIR
27 1.1 deraadt #define BINDINGDIR "/var/yp/binding"
28 1.1 deraadt #endif
29 1.1 deraadt
30 1.1 deraadt struct _dom_binding {
31 1.1 deraadt struct _dom_binding *dom_pnext;
32 1.1 deraadt char dom_domain[YPMAXDOMAIN + 1];
33 1.1 deraadt struct sockaddr_in dom_server_addr;
34 1.1 deraadt unsigned short int dom_server_port;
35 1.1 deraadt int dom_socket;
36 1.1 deraadt CLIENT *dom_client;
37 1.1 deraadt long int dom_vers;
38 1.1 deraadt time_t dom_check_t;
39 1.1 deraadt int dom_lockfd;
40 1.1 deraadt int dom_alive;
41 1.1 deraadt };
42 1.1 deraadt
43 1.1 deraadt extern bool_t xdr_domainname(), xdr_ypbind_resp();
44 1.1 deraadt extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
45 1.1 deraadt extern bool_t xdr_ypbind_setdom();
46 1.1 deraadt
47 1.1 deraadt char *domainname;
48 1.1 deraadt
49 1.1 deraadt struct _dom_binding *ypbindlist;
50 1.1 deraadt int check;
51 1.1 deraadt
52 1.1 deraadt #define YPSET_NO 0
53 1.1 deraadt #define YPSET_LOCAL 1
54 1.1 deraadt #define YPSET_ALL 2
55 1.1 deraadt int ypsetmode = YPSET_NO;
56 1.1 deraadt
57 1.1 deraadt int rpcsock;
58 1.1 deraadt struct rmtcallargs rmtca;
59 1.1 deraadt struct rmtcallres rmtcr;
60 1.1 deraadt char rmtcr_outval;
61 1.1 deraadt u_long rmtcr_port;
62 1.1 deraadt
63 1.1 deraadt void *
64 1.1 deraadt ypbindproc_null_2(transp, argp, clnt)
65 1.1 deraadt SVCXPRT *transp;
66 1.1 deraadt void *argp;
67 1.1 deraadt CLIENT *clnt;
68 1.1 deraadt {
69 1.1 deraadt static char res;
70 1.1 deraadt
71 1.1 deraadt bzero((char *)&res, sizeof(res));
72 1.1 deraadt return (void *)&res;
73 1.1 deraadt }
74 1.1 deraadt
75 1.1 deraadt struct ypbind_resp *
76 1.1 deraadt ypbindproc_domain_2(transp, argp, clnt)
77 1.1 deraadt SVCXPRT *transp;
78 1.1 deraadt char *argp;
79 1.1 deraadt CLIENT *clnt;
80 1.1 deraadt {
81 1.1 deraadt static struct ypbind_resp res;
82 1.1 deraadt struct _dom_binding *ypdb;
83 1.1 deraadt char path[MAXPATHLEN];
84 1.1 deraadt
85 1.1 deraadt bzero((char *)&res, sizeof res);
86 1.1 deraadt res.ypbind_status = YPBIND_FAIL_VAL;
87 1.1 deraadt
88 1.1 deraadt for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
89 1.1 deraadt if( strcmp(ypdb->dom_domain, argp) == 0)
90 1.1 deraadt break;
91 1.1 deraadt
92 1.1 deraadt if(ypdb==NULL) {
93 1.1 deraadt ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
94 1.1 deraadt bzero((char *)ypdb, sizeof *ypdb);
95 1.1 deraadt strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
96 1.1 deraadt ypdb->dom_vers = YPVERS;
97 1.1 deraadt ypdb->dom_alive = 0;
98 1.1 deraadt ypdb->dom_lockfd = -1;
99 1.1 deraadt sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
100 1.1 deraadt unlink(path);
101 1.1 deraadt ypdb->dom_pnext = ypbindlist;
102 1.1 deraadt ypbindlist = ypdb;
103 1.1 deraadt check++;
104 1.1 deraadt return NULL;
105 1.1 deraadt }
106 1.1 deraadt
107 1.1 deraadt if(ypdb->dom_alive==0)
108 1.1 deraadt return NULL;
109 1.1 deraadt
110 1.1 deraadt #if 0
111 1.1 deraadt delta = ypdb->dom_check_t - ypdb->dom_ask_t;
112 1.1 deraadt if( !(ypdb->dom_ask_t==0 || delta > 5)) {
113 1.1 deraadt ypdb->dom_ask_t = time(NULL);
114 1.1 deraadt /*
115 1.1 deraadt * Hmm. More than 2 requests in 5 seconds have indicated that my
116 1.1 deraadt * binding is possibly incorrect. Ok, make myself unalive, and
117 1.1 deraadt * find out what the actual state is.
118 1.1 deraadt */
119 1.1 deraadt if(ypdb->dom_lockfd!=-1)
120 1.1 deraadt close(ypdb->dom_lockfd);
121 1.1 deraadt ypdb->dom_lockfd = -1;
122 1.1 deraadt ypdb->dom_alive = 0;
123 1.1 deraadt ypdb->dom_lockfd = -1;
124 1.1 deraadt sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
125 1.1 deraadt unlink(path);
126 1.1 deraadt check++;
127 1.1 deraadt return NULL;
128 1.1 deraadt }
129 1.1 deraadt #endif
130 1.1 deraadt
131 1.1 deraadt answer:
132 1.1 deraadt res.ypbind_status = YPBIND_SUCC_VAL;
133 1.1 deraadt res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
134 1.1 deraadt ypdb->dom_server_addr.sin_addr.s_addr;
135 1.1 deraadt res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
136 1.1 deraadt ypdb->dom_server_port;
137 1.1 deraadt /*printf("domain %s at %s/%d\n", ypdb->dom_domain,
138 1.1 deraadt inet_ntoa(ypdb->dom_server_addr.sin_addr),
139 1.1 deraadt ntohs(ypdb->dom_server_addr.sin_port));*/
140 1.1 deraadt return &res;
141 1.1 deraadt }
142 1.1 deraadt
143 1.1 deraadt bool_t *
144 1.1 deraadt ypbindproc_setdom_2(transp, argp, clnt)
145 1.1 deraadt SVCXPRT *transp;
146 1.1 deraadt struct ypbind_setdom *argp;
147 1.1 deraadt CLIENT *clnt;
148 1.1 deraadt {
149 1.1 deraadt struct sockaddr_in *fromsin, bindsin;
150 1.1 deraadt char res;
151 1.1 deraadt
152 1.1 deraadt bzero((char *)&res, sizeof(res));
153 1.1 deraadt fromsin = svc_getcaller(transp);
154 1.1 deraadt
155 1.1 deraadt switch(ypsetmode) {
156 1.1 deraadt case YPSET_LOCAL:
157 1.1 deraadt if( fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
158 1.1 deraadt return (void *)NULL;
159 1.1 deraadt break;
160 1.1 deraadt case YPSET_ALL:
161 1.1 deraadt break;
162 1.1 deraadt case YPSET_NO:
163 1.1 deraadt default:
164 1.1 deraadt return (void *)NULL;
165 1.1 deraadt }
166 1.1 deraadt
167 1.1 deraadt if(argp->ypsetdom_vers != YPVERS)
168 1.1 deraadt return (void *)&res;
169 1.1 deraadt
170 1.1 deraadt bzero((char *)&bindsin, sizeof bindsin);
171 1.1 deraadt bindsin.sin_family = AF_INET;
172 1.1 deraadt bindsin.sin_addr.s_addr = argp->ypsetdom_addr.s_addr;
173 1.1 deraadt bindsin.sin_port = argp->ypsetdom_port;
174 1.1 deraadt rpc_received(argp->ypsetdom_domain, &bindsin, 1);
175 1.1 deraadt
176 1.1 deraadt res = 1;
177 1.1 deraadt return (void *)&res;
178 1.1 deraadt }
179 1.1 deraadt
180 1.1 deraadt static void
181 1.1 deraadt ypbindprog_2(rqstp, transp)
182 1.1 deraadt struct svc_req *rqstp;
183 1.1 deraadt register SVCXPRT *transp;
184 1.1 deraadt {
185 1.1 deraadt union {
186 1.1 deraadt char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
187 1.1 deraadt struct ypbind_setdom ypbindproc_setdom_2_arg;
188 1.1 deraadt } argument;
189 1.1 deraadt struct authunix_parms *creds;
190 1.1 deraadt char *result;
191 1.1 deraadt bool_t (*xdr_argument)(), (*xdr_result)();
192 1.1 deraadt char *(*local)();
193 1.1 deraadt
194 1.1 deraadt switch (rqstp->rq_proc) {
195 1.1 deraadt case YPBINDPROC_NULL:
196 1.1 deraadt xdr_argument = xdr_void;
197 1.1 deraadt xdr_result = xdr_void;
198 1.1 deraadt local = (char *(*)()) ypbindproc_null_2;
199 1.1 deraadt break;
200 1.1 deraadt
201 1.1 deraadt case YPBINDPROC_DOMAIN:
202 1.1 deraadt xdr_argument = xdr_domainname;
203 1.1 deraadt xdr_result = xdr_ypbind_resp;
204 1.1 deraadt local = (char *(*)()) ypbindproc_domain_2;
205 1.1 deraadt break;
206 1.1 deraadt
207 1.1 deraadt case YPBINDPROC_SETDOM:
208 1.1 deraadt switch(rqstp->rq_cred.oa_flavor) {
209 1.1 deraadt case AUTH_UNIX:
210 1.1 deraadt creds = (struct authunix_parms *)rqstp->rq_clntcred;
211 1.1 deraadt if( creds->aup_uid != 0) {
212 1.1 deraadt svcerr_auth(transp, AUTH_BADCRED);
213 1.1 deraadt return;
214 1.1 deraadt }
215 1.1 deraadt break;
216 1.1 deraadt default:
217 1.1 deraadt svcerr_auth(transp, AUTH_TOOWEAK);
218 1.1 deraadt return;
219 1.1 deraadt }
220 1.1 deraadt
221 1.1 deraadt xdr_argument = xdr_ypbind_setdom;
222 1.1 deraadt xdr_result = xdr_void;
223 1.1 deraadt local = (char *(*)()) ypbindproc_setdom_2;
224 1.1 deraadt break;
225 1.1 deraadt
226 1.1 deraadt default:
227 1.1 deraadt svcerr_noproc(transp);
228 1.1 deraadt return;
229 1.1 deraadt }
230 1.1 deraadt bzero((char *)&argument, sizeof(argument));
231 1.1 deraadt if (!svc_getargs(transp, xdr_argument, &argument)) {
232 1.1 deraadt svcerr_decode(transp);
233 1.1 deraadt return;
234 1.1 deraadt }
235 1.1 deraadt result = (*local)(transp, &argument, rqstp);
236 1.1 deraadt if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
237 1.1 deraadt svcerr_systemerr(transp);
238 1.1 deraadt }
239 1.1 deraadt return;
240 1.1 deraadt }
241 1.1 deraadt
242 1.1 deraadt main(argc, argv)
243 1.1 deraadt char **argv;
244 1.1 deraadt {
245 1.1 deraadt char path[MAXPATHLEN];
246 1.1 deraadt struct timeval tv;
247 1.1 deraadt SVCXPRT *transp;
248 1.1 deraadt fd_set fdsr;
249 1.1 deraadt int width;
250 1.1 deraadt int i;
251 1.1 deraadt
252 1.1 deraadt yp_get_default_domain(&domainname);
253 1.1 deraadt if( domainname[0] == '\0') {
254 1.1 deraadt fprintf(stderr, "domainname not set. Aborting.\n");
255 1.1 deraadt exit(1);
256 1.1 deraadt }
257 1.1 deraadt
258 1.1 deraadt for(i=1; i<argc; i++) {
259 1.1 deraadt if( strcmp("-ypset", argv[i]) == 0)
260 1.1 deraadt ypsetmode = YPSET_ALL;
261 1.1 deraadt else if (strcmp("-ypsetme", argv[i]) == 0)
262 1.1 deraadt ypsetmode = YPSET_LOCAL;
263 1.1 deraadt }
264 1.1 deraadt
265 1.1 deraadt /* blow away everything in BINDINGDIR */
266 1.1 deraadt
267 1.1 deraadt
268 1.1 deraadt
269 1.1 deraadt #ifdef DAEMON
270 1.1 deraadt switch(fork()) {
271 1.1 deraadt case 0:
272 1.1 deraadt break;
273 1.1 deraadt case -1:
274 1.1 deraadt perror("fork");
275 1.1 deraadt exit(1);
276 1.1 deraadt default:
277 1.1 deraadt exit(0);
278 1.1 deraadt }
279 1.1 deraadt setsid();
280 1.1 deraadt #endif
281 1.1 deraadt
282 1.1 deraadt pmap_unset(YPBINDPROG, YPBINDVERS);
283 1.1 deraadt
284 1.1 deraadt transp = svcudp_create(RPC_ANYSOCK);
285 1.1 deraadt if (transp == NULL) {
286 1.1 deraadt fprintf(stderr, "cannot create udp service.");
287 1.1 deraadt exit(1);
288 1.1 deraadt }
289 1.1 deraadt if (!svc_register(transp, YPBINDPROG, YPBINDVERS, ypbindprog_2, IPPROTO_UDP)) {
290 1.1 deraadt fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).");
291 1.1 deraadt exit(1);
292 1.1 deraadt }
293 1.1 deraadt
294 1.1 deraadt transp = svctcp_create(RPC_ANYSOCK, 0, 0);
295 1.1 deraadt if (transp == NULL) {
296 1.1 deraadt fprintf(stderr, "cannot create tcp service.");
297 1.1 deraadt exit(1);
298 1.1 deraadt }
299 1.1 deraadt
300 1.1 deraadt if (!svc_register(transp, YPBINDPROG, YPBINDVERS, ypbindprog_2, IPPROTO_TCP)) {
301 1.1 deraadt fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).");
302 1.1 deraadt exit(1);
303 1.1 deraadt }
304 1.1 deraadt
305 1.1 deraadt if( (rpcsock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
306 1.1 deraadt perror("socket");
307 1.1 deraadt return -1;
308 1.1 deraadt }
309 1.1 deraadt fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
310 1.1 deraadt i = 1;
311 1.1 deraadt setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i));
312 1.1 deraadt rmtca.prog = YPPROG;
313 1.1 deraadt rmtca.vers = YPVERS;
314 1.1 deraadt rmtca.proc = YPPROC_DOMAIN_NONACK;
315 1.1 deraadt rmtca.xdr_args = NULL; /* set at call time */
316 1.1 deraadt rmtca.args_ptr = NULL; /* set at call time */
317 1.1 deraadt rmtcr.port_ptr = &rmtcr_port;
318 1.1 deraadt rmtcr.xdr_results = xdr_bool;
319 1.1 deraadt rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
320 1.1 deraadt
321 1.1 deraadt /* build initial domain binding, make it "unsuccessful" */
322 1.1 deraadt ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
323 1.1 deraadt bzero((char *)ypbindlist, sizeof *ypbindlist);
324 1.1 deraadt strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
325 1.1 deraadt ypbindlist->dom_vers = YPVERS;
326 1.1 deraadt ypbindlist->dom_alive = 0;
327 1.1 deraadt ypbindlist->dom_lockfd = -1;
328 1.1 deraadt sprintf(path, "%s/%s.%d", BINDINGDIR, ypbindlist->dom_domain,
329 1.1 deraadt ypbindlist->dom_vers);
330 1.1 deraadt (void)unlink(path);
331 1.1 deraadt
332 1.1 deraadt width = getdtablesize();
333 1.1 deraadt while(1) {
334 1.1 deraadt fdsr = svc_fdset;
335 1.1 deraadt FD_SET(rpcsock, &fdsr);
336 1.1 deraadt tv.tv_sec = 1;
337 1.1 deraadt tv.tv_usec = 0;
338 1.1 deraadt
339 1.1 deraadt switch(select(width, &fdsr, NULL, NULL, &tv)) {
340 1.1 deraadt case 0:
341 1.1 deraadt checkwork();
342 1.1 deraadt break;
343 1.1 deraadt case -1:
344 1.1 deraadt perror("select\n");
345 1.1 deraadt break;
346 1.1 deraadt default:
347 1.1 deraadt if(FD_ISSET(rpcsock, &fdsr)) {
348 1.1 deraadt FD_CLR(rpcsock, &fdsr);
349 1.1 deraadt handle_replies();
350 1.1 deraadt }
351 1.1 deraadt svc_getreqset(&fdsr);
352 1.1 deraadt if(check)
353 1.1 deraadt checkwork();
354 1.1 deraadt break;
355 1.1 deraadt }
356 1.1 deraadt }
357 1.1 deraadt }
358 1.1 deraadt
359 1.1 deraadt /*
360 1.1 deraadt * change to do something like this:
361 1.1 deraadt *
362 1.1 deraadt * STATE TIME ACTION NEWTIME NEWSTATE
363 1.1 deraadt * no binding t==* broadcast t=2 no binding
364 1.1 deraadt * binding t==60 check server t=10 binding
365 1.1 deraadt * binding t=10 broadcast t=2 no binding
366 1.1 deraadt */
367 1.1 deraadt checkwork()
368 1.1 deraadt {
369 1.1 deraadt struct _dom_binding *ypdb;
370 1.1 deraadt time_t t;
371 1.1 deraadt
372 1.1 deraadt check = 0;
373 1.1 deraadt
374 1.1 deraadt time(&t);
375 1.1 deraadt for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext) {
376 1.1 deraadt if(ypdb->dom_alive==0 || ypdb->dom_check_t < t) {
377 1.1 deraadt broadcast(ypdb->dom_domain);
378 1.1 deraadt time(&t);
379 1.1 deraadt ypdb->dom_check_t = t + 5;
380 1.1 deraadt }
381 1.1 deraadt }
382 1.1 deraadt }
383 1.1 deraadt
384 1.1 deraadt broadcast(dom)
385 1.1 deraadt char *dom;
386 1.1 deraadt {
387 1.1 deraadt struct rpc_msg rpcmsg;
388 1.1 deraadt char buf[1400], inbuf[8192];
389 1.1 deraadt enum clnt_stat st;
390 1.1 deraadt struct timeval tv;
391 1.1 deraadt int outlen, i, sock, len;
392 1.1 deraadt struct sockaddr_in bsin;
393 1.1 deraadt struct ifconf ifc;
394 1.1 deraadt struct ifreq ifreq, *ifr;
395 1.1 deraadt struct in_addr in;
396 1.1 deraadt AUTH *rpcua;
397 1.1 deraadt XDR rpcxdr;
398 1.1 deraadt
399 1.1 deraadt rmtca.xdr_args = xdr_domainname;
400 1.1 deraadt rmtca.args_ptr = dom;
401 1.1 deraadt
402 1.1 deraadt bzero((char *)&bsin, sizeof bsin);
403 1.1 deraadt bsin.sin_family = AF_INET;
404 1.1 deraadt bsin.sin_port = htons(PMAPPORT);
405 1.1 deraadt
406 1.1 deraadt bzero((char *)&rpcxdr, sizeof rpcxdr);
407 1.1 deraadt bzero((char *)&rpcmsg, sizeof rpcmsg);
408 1.1 deraadt
409 1.1 deraadt rpcua = authunix_create_default();
410 1.1 deraadt if( rpcua == (AUTH *)NULL) {
411 1.1 deraadt /*printf("cannot get unix auth\n");*/
412 1.1 deraadt return RPC_SYSTEMERROR;
413 1.1 deraadt }
414 1.1 deraadt rpcmsg.rm_direction = CALL;
415 1.1 deraadt rpcmsg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
416 1.1 deraadt rpcmsg.rm_call.cb_prog = PMAPPROG;
417 1.1 deraadt rpcmsg.rm_call.cb_vers = PMAPVERS;
418 1.1 deraadt rpcmsg.rm_call.cb_proc = PMAPPROC_CALLIT;
419 1.1 deraadt rpcmsg.rm_call.cb_cred = rpcua->ah_cred;
420 1.1 deraadt rpcmsg.rm_call.cb_verf = rpcua->ah_verf;
421 1.1 deraadt
422 1.1 deraadt gettimeofday(&tv, (struct timezone *)0);
423 1.1 deraadt rpcmsg.rm_xid = (int)dom;
424 1.1 deraadt tv.tv_usec = 0;
425 1.1 deraadt xdrmem_create(&rpcxdr, buf, sizeof buf, XDR_ENCODE);
426 1.1 deraadt if( (!xdr_callmsg(&rpcxdr, &rpcmsg)) ) {
427 1.1 deraadt st = RPC_CANTENCODEARGS;
428 1.1 deraadt AUTH_DESTROY(rpcua);
429 1.1 deraadt return st;
430 1.1 deraadt }
431 1.1 deraadt if( (!xdr_rmtcall_args(&rpcxdr, &rmtca)) ) {
432 1.1 deraadt st = RPC_CANTENCODEARGS;
433 1.1 deraadt AUTH_DESTROY(rpcua);
434 1.1 deraadt return st;
435 1.1 deraadt }
436 1.1 deraadt outlen = (int)xdr_getpos(&rpcxdr);
437 1.1 deraadt xdr_destroy(&rpcxdr);
438 1.1 deraadt if(outlen<1) {
439 1.1 deraadt AUTH_DESTROY(rpcua);
440 1.1 deraadt return st;
441 1.1 deraadt }
442 1.1 deraadt AUTH_DESTROY(rpcua);
443 1.1 deraadt
444 1.1 deraadt /* find all networks and send the RPC packet out them all */
445 1.1 deraadt if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
446 1.1 deraadt perror("socket");
447 1.1 deraadt return -1;
448 1.1 deraadt }
449 1.1 deraadt
450 1.1 deraadt ifc.ifc_len = sizeof inbuf;
451 1.1 deraadt ifc.ifc_buf = inbuf;
452 1.1 deraadt if( ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
453 1.1 deraadt close(sock);
454 1.1 deraadt perror("ioctl(SIOCGIFCONF)");
455 1.1 deraadt return -1;
456 1.1 deraadt }
457 1.1 deraadt ifr = ifc.ifc_req;
458 1.1 deraadt ifreq.ifr_name[0] = '\0';
459 1.1 deraadt for(i=0; i<ifc.ifc_len; i+=len, ifr=(struct ifreq *)((caddr_t)ifr+len)) {
460 1.1 deraadt #if defined(BSD) && BSD >= 199103
461 1.1 deraadt len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
462 1.1 deraadt #else
463 1.1 deraadt len = sizeof ifc.ifc_len / sizeof(struct ifreq);
464 1.1 deraadt #endif
465 1.1 deraadt ifreq = *ifr;
466 1.1 deraadt if( ifreq.ifr_addr.sa_family != AF_INET)
467 1.1 deraadt continue;
468 1.1 deraadt if( ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
469 1.1 deraadt perror("ioctl(SIOCGIFFLAGS)");
470 1.1 deraadt continue;
471 1.1 deraadt }
472 1.1 deraadt if( (ifreq.ifr_flags & IFF_UP) == 0)
473 1.1 deraadt continue;
474 1.1 deraadt
475 1.1 deraadt ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
476 1.1 deraadt if( ifreq.ifr_flags==IFF_BROADCAST ) {
477 1.1 deraadt if( ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0 ) {
478 1.1 deraadt perror("ioctl(SIOCGIFBRDADDR)");
479 1.1 deraadt continue;
480 1.1 deraadt }
481 1.1 deraadt } else if( ifreq.ifr_flags==IFF_LOOPBACK ) {
482 1.1 deraadt if( ioctl(sock, SIOCGIFADDR, &ifreq) < 0 ) {
483 1.1 deraadt perror("ioctl(SIOCGIFADDR)");
484 1.1 deraadt continue;
485 1.1 deraadt }
486 1.1 deraadt } else
487 1.1 deraadt continue;
488 1.1 deraadt
489 1.1 deraadt in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
490 1.1 deraadt bsin.sin_addr = in;
491 1.1 deraadt if( sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bsin,
492 1.1 deraadt sizeof bsin) < 0 )
493 1.1 deraadt perror("sendto");
494 1.1 deraadt }
495 1.1 deraadt close(sock);
496 1.1 deraadt return 0;
497 1.1 deraadt }
498 1.1 deraadt
499 1.1 deraadt /*enum clnt_stat*/
500 1.1 deraadt handle_replies()
501 1.1 deraadt {
502 1.1 deraadt char buf[1400];
503 1.1 deraadt int fromlen, inlen;
504 1.1 deraadt struct sockaddr_in raddr;
505 1.1 deraadt struct rpc_msg msg;
506 1.1 deraadt XDR xdr;
507 1.1 deraadt
508 1.1 deraadt recv_again:
509 1.1 deraadt bzero((char *)&xdr, sizeof(xdr));
510 1.1 deraadt bzero((char *)&msg, sizeof(msg));
511 1.1 deraadt msg.acpted_rply.ar_verf = _null_auth;
512 1.1 deraadt msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
513 1.1 deraadt msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
514 1.1 deraadt
515 1.1 deraadt try_again:
516 1.1 deraadt fromlen = sizeof (struct sockaddr);
517 1.1 deraadt inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
518 1.1 deraadt (struct sockaddr *)&raddr, &fromlen);
519 1.1 deraadt if(inlen<0) {
520 1.1 deraadt if(errno==EINTR)
521 1.1 deraadt goto try_again;
522 1.1 deraadt return RPC_CANTRECV;
523 1.1 deraadt }
524 1.1 deraadt if(inlen<sizeof(u_long))
525 1.1 deraadt goto recv_again;
526 1.1 deraadt
527 1.1 deraadt /*
528 1.1 deraadt * see if reply transaction id matches sent id.
529 1.1 deraadt * If so, decode the results.
530 1.1 deraadt */
531 1.1 deraadt xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
532 1.1 deraadt if( xdr_replymsg(&xdr, &msg)) {
533 1.1 deraadt if( (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
534 1.1 deraadt (msg.acpted_rply.ar_stat == SUCCESS)) {
535 1.1 deraadt raddr.sin_port = htons((u_short)rmtcr_port);
536 1.1 deraadt rpc_received(msg.rm_xid, &raddr, 0);
537 1.1 deraadt }
538 1.1 deraadt }
539 1.1 deraadt xdr.x_op = XDR_FREE;
540 1.1 deraadt msg.acpted_rply.ar_results.proc = xdr_void;
541 1.1 deraadt xdr_destroy(&xdr);
542 1.1 deraadt
543 1.1 deraadt return RPC_SUCCESS;
544 1.1 deraadt }
545 1.1 deraadt
546 1.1 deraadt /*
547 1.1 deraadt * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
548 1.1 deraadt */
549 1.1 deraadt rpc_received(dom, raddrp, force)
550 1.1 deraadt char *dom;
551 1.1 deraadt struct sockaddr_in *raddrp;
552 1.1 deraadt int force;
553 1.1 deraadt {
554 1.1 deraadt struct _dom_binding *ypdb;
555 1.1 deraadt char path[MAXPATHLEN];
556 1.1 deraadt int fd;
557 1.1 deraadt
558 1.1 deraadt /*printf("returned from %s about %s\n", inet_ntoa(raddrp->sin_addr), dom);*/
559 1.1 deraadt
560 1.1 deraadt if(dom==NULL)
561 1.1 deraadt return;
562 1.1 deraadt
563 1.1 deraadt for(ypdb=ypbindlist; ypdb; ypdb=ypdb->dom_pnext)
564 1.1 deraadt if( strcmp(ypdb->dom_domain, dom) == 0)
565 1.1 deraadt break;
566 1.1 deraadt
567 1.1 deraadt if(ypdb==NULL) {
568 1.1 deraadt if(force==0)
569 1.1 deraadt return;
570 1.1 deraadt ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
571 1.1 deraadt bzero((char *)ypdb, sizeof *ypdb);
572 1.1 deraadt strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
573 1.1 deraadt ypdb->dom_lockfd = -1;
574 1.1 deraadt ypdb->dom_pnext = ypbindlist;
575 1.1 deraadt ypbindlist = ypdb;
576 1.1 deraadt }
577 1.1 deraadt
578 1.1 deraadt /* soft update, alive, less than 30 seconds old */
579 1.1 deraadt if(ypdb->dom_alive==1 && force==0 && ypdb->dom_check_t<time(NULL)+30)
580 1.1 deraadt return;
581 1.1 deraadt
582 1.1 deraadt bcopy((char *)raddrp, (char *)&ypdb->dom_server_addr,
583 1.1 deraadt sizeof ypdb->dom_server_addr);
584 1.1 deraadt ypdb->dom_check_t = time(NULL) + 60; /* recheck binding in 60 seconds */
585 1.1 deraadt ypdb->dom_vers = YPVERS;
586 1.1 deraadt ypdb->dom_alive = 1;
587 1.1 deraadt
588 1.1 deraadt if(ypdb->dom_lockfd != -1)
589 1.1 deraadt close(ypdb->dom_lockfd);
590 1.1 deraadt
591 1.1 deraadt sprintf(path, "%s/%s.%d", BINDINGDIR,
592 1.1 deraadt ypdb->dom_domain, ypdb->dom_vers);
593 1.1 deraadt #ifdef O_SHLOCK
594 1.1 deraadt if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
595 1.1 deraadt (void)mkdir(BINDINGDIR, 0755);
596 1.1 deraadt if( (fd=open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
597 1.1 deraadt return;
598 1.1 deraadt }
599 1.1 deraadt #else
600 1.1 deraadt if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
601 1.1 deraadt (void)mkdir(BINDINGDIR, 0755);
602 1.1 deraadt if( (fd=open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
603 1.1 deraadt return;
604 1.1 deraadt }
605 1.1 deraadt flock(fd, LOCK_SH);
606 1.1 deraadt
607 1.1 deraadt /*
608 1.1 deraadt * ok, if BINDINGDIR exists, and we can create the binding file,
609 1.1 deraadt * then write to it..
610 1.1 deraadt */
611 1.1 deraadt ypdb->dom_lockfd = fd;
612 1.1 deraadt if( write(ypdb->dom_lockfd, raddrp, sizeof *raddrp) != sizeof *raddrp) {
613 1.1 deraadt perror("write");
614 1.1 deraadt close(ypdb->dom_lockfd);
615 1.1 deraadt ypdb->dom_lockfd = -1;
616 1.1 deraadt return;
617 1.1 deraadt }
618 1.1 deraadt #endif
619 1.1 deraadt }
620