This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / portmap / portmap.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * 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 the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)portmap.c 5.4 (Berkeley) 4/19/91";
42#endif /* not lint */
43
44/*
45@(#)portmap.c 2.3 88/08/11 4.0 RPCSRC
46static char sccsid[] = "@(#)portmap.c 1.32 87/08/06 Copyr 1984 Sun Micro";
47*/
48
49/*
50 * portmap.c, Implements the program,version to port number mapping for
51 * rpc.
52 */
53
54/*
55 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
56 * unrestricted use provided that this legend is included on all tape
57 * media and as a part of the software program in whole or part. Users
58 * may copy or modify Sun RPC without charge, but are not authorized
59 * to license or distribute it to anyone else except as part of a product or
60 * program developed by the user.
61 *
62 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
63 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
65 *
66 * Sun RPC is provided with no support and without any obligation on the
67 * part of Sun Microsystems, Inc. to assist in its use, correction,
68 * modification or enhancement.
69 *
70 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
71 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
72 * OR ANY PART THEREOF.
73 *
74 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
75 * or profits or other special, indirect and consequential damages, even if
76 * Sun has been advised of the possibility of such damages.
77 *
78 * Sun Microsystems, Inc.
79 * 2550 Garcia Avenue
80 * Mountain View, California 94043
81 */
82
83#include <rpc/rpc.h>
84#include <rpc/pmap_prot.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <syslog.h>
89#include <unistd.h>
90#include <netdb.h>
91#include <sys/socket.h>
92#include <sys/ioctl.h>
93#include <sys/wait.h>
94#include <sys/signal.h>
95#include <sys/resource.h>
96
97void reg_service();
98void reap();
99static void callit();
100struct pmaplist *pmaplist;
101int debugging = 0;
102extern int errno;
103
104main(argc, argv)
105 int argc;
106 char **argv;
107{
108 SVCXPRT *xprt;
109 int sock, c;
110 struct sockaddr_in addr;
111 int len = sizeof(struct sockaddr_in);
112 register struct pmaplist *pml;
113
114 while ((c = getopt(argc, argv, "d")) != EOF) {
115 switch (c) {
116
117 case 'd':
118 debugging = 1;
119 break;
120
121 default:
122 (void) fprintf(stderr, "usage: %s [-d]\n", argv[0]);
123 exit(1);
124 }
125 }
126
127 if (!debugging && daemon(0, 0)) {
128 (void) fprintf(stderr, "portmap: fork: %s", strerror(errno));
129 exit(1);
130 }
131
132 openlog("portmap", debugging ? LOG_PID | LOG_PERROR : LOG_PID,
133 LOG_DAEMON);
134
135 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
136 syslog(LOG_ERR, "cannot create udp socket: %m");
137 exit(1);
138 }
139
140 addr.sin_addr.s_addr = 0;
141 addr.sin_family = AF_INET;
142 addr.sin_port = htons(PMAPPORT);
143 if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
144 syslog(LOG_ERR, "cannot bind udp: %m");
145 exit(1);
146 }
147
148 if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
149 syslog(LOG_ERR, "couldn't do udp_create");
150 exit(1);
151 }
152 /* make an entry for ourself */
153 pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
154 pml->pml_next = 0;
155 pml->pml_map.pm_prog = PMAPPROG;
156 pml->pml_map.pm_vers = PMAPVERS;
157 pml->pml_map.pm_prot = IPPROTO_UDP;
158 pml->pml_map.pm_port = PMAPPORT;
159 pmaplist = pml;
160
161 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
162 syslog(LOG_ERR, "cannot create tcp socket: %m");
163 exit(1);
164 }
165 if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
166 syslog(LOG_ERR, "cannot bind udp: %m");
167 exit(1);
168 }
169 if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
170 == (SVCXPRT *)NULL) {
171 syslog(LOG_ERR, "couldn't do tcp_create");
172 exit(1);
173 }
174 /* make an entry for ourself */
175 pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
176 pml->pml_map.pm_prog = PMAPPROG;
177 pml->pml_map.pm_vers = PMAPVERS;
178 pml->pml_map.pm_prot = IPPROTO_TCP;
179 pml->pml_map.pm_port = PMAPPORT;
180 pml->pml_next = pmaplist;
181 pmaplist = pml;
182
183 (void)svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE);
184
185 (void)signal(SIGCHLD, reap);
186 svc_run();
187 syslog(LOG_ERR, "run_svc returned unexpectedly");
188 abort();
189}
190
191#ifndef lint
192/* need to override perror calls in rpc library */
193void
194perror(what)
195 const char *what;
196{
197
198 syslog(LOG_ERR, "%s: %m", what);
199}
200#endif
201
202static struct pmaplist *
203find_service(prog, vers, prot)
204 u_long prog, vers, prot;
205{
206 register struct pmaplist *hit = NULL;
207 register struct pmaplist *pml;
208
209 for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
210 if ((pml->pml_map.pm_prog != prog) ||
211 (pml->pml_map.pm_prot != prot))
212 continue;
213 hit = pml;
214 if (pml->pml_map.pm_vers == vers)
215 break;
216 }
217 return (hit);
218}
219
220/*
221 * 1 OK, 0 not
222 */
223void
224reg_service(rqstp, xprt)
225 struct svc_req *rqstp;
226 SVCXPRT *xprt;
227{
228 struct pmap reg;
229 struct pmaplist *pml, *prevpml, *fnd;
230 int ans, port;
231 caddr_t t;
232
233 if (debugging)
234 (void) fprintf(stderr, "server: about do a switch\n");
235 switch (rqstp->rq_proc) {
236
237 case PMAPPROC_NULL:
238 /*
239 * Null proc call
240 */
241 if (!svc_sendreply(xprt, xdr_void, (caddr_t)0) && debugging) {
242 abort();
243 }
244 break;
245
246 case PMAPPROC_SET:
247 /*
248 * Set a program,version to port mapping
249 */
250 if (!svc_getargs(xprt, xdr_pmap, &reg))
251 svcerr_decode(xprt);
252 else {
253 /*
254 * check to see if already used
255 * find_service returns a hit even if
256 * the versions don't match, so check for it
257 */
258 fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
259 if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
260 if (fnd->pml_map.pm_port == reg.pm_port) {
261 ans = 1;
262 goto done;
263 }
264 else {
265 ans = 0;
266 goto done;
267 }
268 } else {
269 /*
270 * add to END of list
271 */
272 pml = (struct pmaplist *)
273 malloc((u_int)sizeof(struct pmaplist));
274 pml->pml_map = reg;
275 pml->pml_next = 0;
276 if (pmaplist == 0) {
277 pmaplist = pml;
278 } else {
279 for (fnd= pmaplist; fnd->pml_next != 0;
280 fnd = fnd->pml_next);
281 fnd->pml_next = pml;
282 }
283 ans = 1;
284 }
285 done:
286 if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
287 debugging) {
288 (void) fprintf(stderr, "svc_sendreply\n");
289 abort();
290 }
291 }
292 break;
293
294 case PMAPPROC_UNSET:
295 /*
296 * Remove a program,version to port mapping.
297 */
298 if (!svc_getargs(xprt, xdr_pmap, &reg))
299 svcerr_decode(xprt);
300 else {
301 ans = 0;
302 for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
303 if ((pml->pml_map.pm_prog != reg.pm_prog) ||
304 (pml->pml_map.pm_vers != reg.pm_vers)) {
305 /* both pml & prevpml move forwards */
306 prevpml = pml;
307 pml = pml->pml_next;
308 continue;
309 }
310 /* found it; pml moves forward, prevpml stays */
311 ans = 1;
312 t = (caddr_t)pml;
313 pml = pml->pml_next;
314 if (prevpml == NULL)
315 pmaplist = pml;
316 else
317 prevpml->pml_next = pml;
318 free(t);
319 }
320 if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
321 debugging) {
322 (void) fprintf(stderr, "svc_sendreply\n");
323 abort();
324 }
325 }
326 break;
327
328 case PMAPPROC_GETPORT:
329 /*
330 * Lookup the mapping for a program,version and return its port
331 */
332 if (!svc_getargs(xprt, xdr_pmap, &reg))
333 svcerr_decode(xprt);
334 else {
335 fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
336 if (fnd)
337 port = fnd->pml_map.pm_port;
338 else
339 port = 0;
340 if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&port)) &&
341 debugging) {
342 (void) fprintf(stderr, "svc_sendreply\n");
343 abort();
344 }
345 }
346 break;
347
348 case PMAPPROC_DUMP:
349 /*
350 * Return the current set of mapped program,version
351 */
352 if (!svc_getargs(xprt, xdr_void, NULL))
353 svcerr_decode(xprt);
354 else {
355 if ((!svc_sendreply(xprt, xdr_pmaplist,
356 (caddr_t)&pmaplist)) && debugging) {
357 (void) fprintf(stderr, "svc_sendreply\n");
358 abort();
359 }
360 }
361 break;
362
363 case PMAPPROC_CALLIT:
364 /*
365 * Calls a procedure on the local machine. If the requested
366 * procedure is not registered this procedure does not return
367 * error information!!
368 * This procedure is only supported on rpc/udp and calls via
369 * rpc/udp. It passes null authentication parameters.
370 */
371 callit(rqstp, xprt);
372 break;
373
374 default:
375 svcerr_noproc(xprt);
376 break;
377 }
378}
379
380
381/*
382 * Stuff for the rmtcall service
383 */
384#define ARGSIZE 9000
385
386struct encap_parms {
387 u_long arglen;
388 char *args;
389};
390
391static bool_t
392xdr_encap_parms(xdrs, epp)
393 XDR *xdrs;
394 struct encap_parms *epp;
395{
396
397 return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
398}
399
400struct rmtcallargs {
401 u_long rmt_prog;
402 u_long rmt_vers;
403 u_long rmt_port;
404 u_long rmt_proc;
405 struct encap_parms rmt_args;
406};
407
408static bool_t
409xdr_rmtcall_args(xdrs, cap)
410 register XDR *xdrs;
411 register struct rmtcallargs *cap;
412{
413
414 /* does not get a port number */
415 if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
416 xdr_u_long(xdrs, &(cap->rmt_vers)) &&
417 xdr_u_long(xdrs, &(cap->rmt_proc))) {
418 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
419 }
420 return (FALSE);
421}
422
423static bool_t
424xdr_rmtcall_result(xdrs, cap)
425 register XDR *xdrs;
426 register struct rmtcallargs *cap;
427{
428 if (xdr_u_long(xdrs, &(cap->rmt_port)))
429 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
430 return (FALSE);
431}
432
433/*
434 * only worries about the struct encap_parms part of struct rmtcallargs.
435 * The arglen must already be set!!
436 */
437static bool_t
438xdr_opaque_parms(xdrs, cap)
439 XDR *xdrs;
440 struct rmtcallargs *cap;
441{
442
443 return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
444}
445
446/*
447 * This routine finds and sets the length of incoming opaque paraters
448 * and then calls xdr_opaque_parms.
449 */
450static bool_t
451xdr_len_opaque_parms(xdrs, cap)
452 register XDR *xdrs;
453 struct rmtcallargs *cap;
454{
455 register u_int beginpos, lowpos, highpos, currpos, pos;
456
457 beginpos = lowpos = pos = xdr_getpos(xdrs);
458 highpos = lowpos + ARGSIZE;
459 while ((int)(highpos - lowpos) >= 0) {
460 currpos = (lowpos + highpos) / 2;
461 if (xdr_setpos(xdrs, currpos)) {
462 pos = currpos;
463 lowpos = currpos + 1;
464 } else {
465 highpos = currpos - 1;
466 }
467 }
468 xdr_setpos(xdrs, beginpos);
469 cap->rmt_args.arglen = pos - beginpos;
470 return (xdr_opaque_parms(xdrs, cap));
471}
472
473/*
474 * Call a remote procedure service
475 * This procedure is very quiet when things go wrong.
476 * The proc is written to support broadcast rpc. In the broadcast case,
477 * a machine should shut-up instead of complain, less the requestor be
478 * overrun with complaints at the expense of not hearing a valid reply ...
479 *
480 * This now forks so that the program & process that it calls can call
481 * back to the portmapper.
482 */
483static void
484callit(rqstp, xprt)
485 struct svc_req *rqstp;
486 SVCXPRT *xprt;
487{
488 struct rmtcallargs a;
489 struct pmaplist *pml;
490 u_short port;
491 struct sockaddr_in me;
492 int pid, so = -1;
493 CLIENT *client;
494 struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
495 struct timeval timeout;
496 char buf[ARGSIZE];
497
498 timeout.tv_sec = 5;
499 timeout.tv_usec = 0;
500 a.rmt_args.args = buf;
501 if (!svc_getargs(xprt, xdr_rmtcall_args, &a))
502 return;
503 if ((pml = find_service(a.rmt_prog, a.rmt_vers,
504 (u_long)IPPROTO_UDP)) == NULL)
505 return;
506 /*
507 * fork a child to do the work. Parent immediately returns.
508 * Child exits upon completion.
509 */
510 if ((pid = fork()) != 0) {
511 if (pid < 0)
512 syslog(LOG_ERR, "CALLIT (prog %lu): fork: %m",
513 a.rmt_prog);
514 return;
515 }
516 port = pml->pml_map.pm_port;
517 get_myaddress(&me);
518 me.sin_port = htons(port);
519 client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
520 if (client != (CLIENT *)NULL) {
521 if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
522 client->cl_auth = authunix_create(au->aup_machname,
523 au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
524 }
525 a.rmt_port = (u_long)port;
526 if (clnt_call(client, a.rmt_proc, xdr_opaque_parms, &a,
527 xdr_len_opaque_parms, &a, timeout) == RPC_SUCCESS) {
528 svc_sendreply(xprt, xdr_rmtcall_result, (caddr_t)&a);
529 }
530 AUTH_DESTROY(client->cl_auth);
531 clnt_destroy(client);
532 }
533 (void)close(so);
534 exit(0);
535}
536
537void
538reap()
539{
540 while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
541}