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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* under license from the Regents of the University of
* California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <signal.h>
#include <rpc/rpc.h>
#include <memory.h>
#include <netconfig.h>
#include <syslog.h>
#include <rpcsvc/yp_prot.h>
#include "yp_b.h"
#include <sys/resource.h>
#include <sys/stropts.h>
#include <unistd.h>
#include <rpc/nettype.h>
#include <string.h>
#include <tiuser.h>
#ifdef DEBUG
#define RPC_SVC_FG
#endif
#define _RPCSVC_CLOSEDOWN 120
#define YPBIND_ERR_ERR 1 /* Internal error */
#define YPBIND_ERR_NOSERV 2 /* No bound server for passed domain */
#define YPBIND_ERR_RESC 3 /* System resource allocation failure */
#define YPBIND_ERR_NODOMAIN 4 /* Domain doesn't exist */
static int _rpcpmstart; /* Started by a port monitor ? */
static int _rpcsvcdirty; /* Still serving ? */
int setok = YPSETNONE; /* who is allowed to ypset */
int broadcast = 0;
int cache_okay = 0; /* if set, then bindings are cached in files */
extern int sigcld_event;
extern void broadcast_proc_exit();
extern int __rpc_negotiate_uid();
extern bool_t __rpcbind_is_up();
extern void ypbind_init_default();
static void set_signal_handlers();
static void clear_bindings();
static void unregister(int);
static int void_close(void *, int);
void closedown();
void ypbindprog_3();
void ypbindprog_2();
void msgout();
extern void cache_transport();
extern void clean_cache();
int
main(argc, argv)
int argc;
char **argv;
{
pid_t pid;
int pfd[2];
char domain[256], servers[300];
char **Argv = argv;
struct netconfig *nconf;
void *nc_handle;
int loopback_found = 0, udp_found = 0;
int pipe_closed = 0;
struct rlimit rl;
int connmaxrec = RPC_MAXDATASIZE;
uint32_t inet_tpts = 0, inet6_tpts = 0;
uint32_t inet_desired_tpts = 0, inet6_desired_tpts = 0;
bool_t exclbind = TRUE;
if (geteuid() != 0) {
(void) fprintf(stderr, "must be root to run %s\n", argv[0]);
exit(1);
}
argc--;
argv++;
while (argc > 0) {
if (strcmp(*argv, "-ypset") == 0) {
setok = YPSETALL;
} else if (strcmp(*argv, "-ypsetme") == 0) {
setok = YPSETLOCAL;
} else if (strcmp(*argv, "-broadcast") == 0) {
broadcast = TRUE;
} else {
fprintf(stderr,
"usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
exit(1);
}
argc--,
argv++;
}
if (setok == YPSETALL) {
fprintf(stderr,
"ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
}
if (setok == YPSETLOCAL) {
fprintf(stderr,
"ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
}
if (broadcast == TRUE) {
fprintf(stderr,
"ypbind -broadcast: allowing broadcast! \
(insecure and transport dependent)\n");
}
if (getdomainname(domain, sizeof (domain)) == 0) {
sprintf(servers, "%s/%s/ypservers", BINDING, domain);
if (!broadcast && access(servers, R_OK) != 0) {
(void) fprintf(stderr,
"%s: no info on servers - run ypinit -c\n", Argv[0]);
exit(1);
}
} else {
(void) fprintf(stderr, "%s: domainname not set - exiting\n",
Argv[0]);
exit(1);
}
getrlimit(RLIMIT_NOFILE, &rl);
rl.rlim_cur = rl.rlim_max;
setrlimit(RLIMIT_NOFILE, &rl);
(void) enable_extended_FILE_stdio(-1, -1);
openlog("ypbind", LOG_PID, LOG_DAEMON);
/*
* If stdin looks like a TLI endpoint, we assume
* that we were started by a port monitor. If
* t_getstate fails with TBADF, this is not a
* TLI endpoint.
*/
_rpcpmstart = (t_getstate(0) != -1 || t_errno != TBADF);
if (!__rpcbind_is_up()) {
msgout("terminating: rpcbind is not running");
exit(1);
}
if (_rpcpmstart) {
/*
* We were invoked by ypbind with the request on stdin.
*
* XXX - This is not the normal way ypbind is used
* and has never been tested.
*/
char *netid;
struct netconfig *nconf = NULL;
SVCXPRT *transp;
int pmclose;
extern char *getenv();
/*
* Set non-blocking mode and maximum record size for
* connection oriented RPC transports.
*/
if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
msgout("unable to set maximum RPC record size");
}
clear_bindings();
if ((netid = getenv("NLSPROVIDER")) == NULL) {
#ifdef DEBUG
msgout("cannot get transport name");
#endif
} else if ((nconf = getnetconfigent(netid)) == NULL) {
#ifdef DEBUG
msgout("cannot get transport info");
#endif
}
pmclose = (t_getstate(0) != T_DATAXFER);
if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
msgout("cannot create server handle");
exit(1);
}
if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
if ((setok != YPSETNONE) &&
__rpc_negotiate_uid(transp->xp_fd)) {
syslog(LOG_ERR,
"could not negotiate with loopback tranport %s",
nconf->nc_netid);
}
}
if (nconf)
freenetconfigent(nconf);
if (!svc_reg(transp, YPBINDPROG, YPBINDVERS, ypbindprog_3, 0)) {
msgout("unable to register (YPBINDPROG, YPBINDVERS).");
exit(1);
}
if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_2,
ypbindprog_2, 0)) {
msgout(
"unable to register (YPBINDPROG, YPBINDVERS_2).");
exit(1);
}
/* version 2 and version 1 are the same as far as we care */
if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_1,
ypbindprog_2, 0)) {
msgout(
"unable to register (YPBINDPROG, YPBINDVERS_1).");
exit(1);
}
set_signal_handlers();
if (pmclose) {
(void) signal(SIGALRM, closedown);
(void) alarm(_RPCSVC_CLOSEDOWN);
}
#ifdef INIT_DEFAULT
ypbind_init_default();
#endif
svc_run();
msgout("svc_run returned");
exit(1);
/* NOTREACHED */
}
#ifndef RPC_SVC_FG
/*
* In normal operation, ypbind forks a child to do all the work
* so that it can run in background. But, if the parent exits
* too soon during system startup, clients will start trying to
* talk to the child ypbind before it is ready. This can cause
* spurious client errors.
*
* To prevent these problems, the parent process creates a pipe,
* which is inherited by the child, and waits for the child to
* close its end. This happens explicitly before the child goes
* into svc_run(), or as a side-effect of exiting.
*/
if (pipe(pfd) == -1) {
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("cannot fork");
exit(1);
}
if (pid) {
/*
* The parent waits for the child to close its end of
* the pipe (to indicate that it is ready to process
* requests). The read blocks until the child does
* a close (the "domain" array is just a handy buffer).
*/
close(pfd[1]);
read(pfd[0], domain, sizeof (domain));
exit(0);
}
/* close all files except pfd[1] */
(void) fdwalk(void_close, &pfd[1]);
(void) open("/dev/null", O_RDONLY);
(void) open("/dev/null", O_WRONLY);
(void) dup(1);
setsid();
#endif
clean_cache(); /* make sure there are no left-over files */
cache_okay = cache_check();
cache_pid();
/*
* Set non-blocking mode and maximum record size for
* connection oriented RPC transports.
*/
if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
msgout("unable to set maximum RPC record size");
}
/*
* Prevent our non-priv udp and tcp ports bound w/wildcard addr
* from being hijacked by a bind to a more specific addr.
*/
if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
msgout("warning: unable to set udp/tcp EXCLBIND");
}
#ifdef INIT_DEFAULT
ypbind_init_default();
#endif
nc_handle = __rpc_setconf("netpath"); /* open netconfig file */
if (nc_handle == NULL) {
syslog(LOG_ERR, "could not read /etc/netconfig, exiting..");
exit(1);
}
/*
* The parent waits for the child to close its end of
* the pipe (to indicate that it is ready to process
* requests). Now the non-diskless client will wait because the
* cache file is valid.
*/
if (cache_okay) {
close(pfd[1]);
pipe_closed = 1;
}
clear_bindings();
while (nconf = __rpc_getconf(nc_handle)) {
SVCXPRT *xprt;
if (!__rpcbind_is_up()) {
msgout("terminating: rpcbind is not running");
exit(1);
}
if ((xprt = svc_tp_create(ypbindprog_3,
YPBINDPROG, YPBINDVERS, nconf)) == NULL) {
msgout("terminating: cannot create rpcbind handle");
exit(1);
}
cache_transport(nconf, xprt, YPBINDVERS);
/* support ypbind V2 and V1, but only on udp/tcp transports */
if (((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
(strcmp(nconf->nc_protofmly, NC_INET6))) &&
((nconf->nc_semantics == NC_TPI_CLTS) ||
(nconf->nc_semantics == NC_TPI_COTS_ORD))) {
if (strcmp(nconf->nc_protofmly, NC_INET)) {
inet_desired_tpts |= 1 >> nconf->nc_semantics;
} else {
inet6_desired_tpts |= 1 >> nconf->nc_semantics;
}
(void) rpcb_unset(YPBINDPROG, YPBINDVERS_2, nconf);
if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_2,
ypbindprog_2, nconf)) {
syslog(LOG_INFO,
"unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
nconf->nc_netid);
continue;
}
cache_transport(nconf, xprt, YPBINDVERS_2);
/* For NC_INET, register v1 as well; error is fatal */
if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
(void) rpcb_unset(YPBINDPROG, YPBINDVERS_1,
nconf);
if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_1,
ypbindprog_2, nconf)) {
syslog(LOG_ERR,
"unable to register (YPBINDPROG, YPBINDVERS_1).");
exit(1);
}
}
cache_transport(nconf, xprt, YPBINDVERS_1);
if (nconf->nc_semantics == NC_TPI_CLTS)
udp_found++;
if (strcmp(nconf->nc_protofmly, NC_INET)) {
inet_tpts |= 1 >> nconf->nc_semantics;
} else {
inet6_tpts |= 1 >> nconf->nc_semantics;
}
}
if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
loopback_found++;
if ((setok != YPSETNONE) &&
__rpc_negotiate_uid(xprt->xp_fd)) {
syslog(LOG_ERR,
"could not negotiate with loopback tranport %s",
nconf->nc_netid);
}
/*
* On a diskless client:
* The parent waits for the child to close its end of
* the pipe (to indicate that it is ready to process
* requests). Now the diskless client will wait
* only if ypbind is registered on the loopback.
*/
if ((!pipe_closed) &&
((nconf->nc_semantics == NC_TPI_COTS) ||
(nconf->nc_semantics == NC_TPI_COTS_ORD))) {
close(pfd[1]);
pipe_closed = 1;
}
}
}
/* Did we manage to register all IPv4 or all IPv6 transports ? */
if (inet_tpts != 0 && inet_tpts != inet_desired_tpts) {
syslog(LOG_ERR,
"unable to register all %s transports, exiting..",
NC_INET);
exit(1);
} else if (inet6_tpts != 0 && inet6_tpts != inet6_desired_tpts) {
syslog(LOG_ERR,
"unable to register all %s transports, exiting..",
NC_INET6);
exit(1);
}
if (!pipe_closed) {
close(pfd[1]);
pipe_closed = 1;
}
__rpc_endconf(nc_handle);
if (!loopback_found) {
syslog(LOG_ERR,
"could not find loopback transports, exiting..");
exit(1);
}
if (!udp_found) {
syslog(LOG_ERR,
"could not find inet-clts (udp) transport, exiting..");
exit(1);
}
set_signal_handlers();
svc_run();
syslog(LOG_ERR, "svc_run returned, exiting..");
exit(1);
/* NOTREACHED */
}
/*
* Callback function for fdwalk() to close all files.
*/
static int
void_close(void *pfdp, int fd)
{
if (fd != *(int *)pfdp)
(void) close(fd);
return (0);
}
void
ypbindprog_3(rqstp, transp)
struct svc_req *rqstp;
register SVCXPRT *transp;
{
union {
ypbind_domain ypbindproc_domain_3_arg;
ypbind_setdom ypbindproc_setdom_3_arg;
} argument;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
if (sigcld_event)
broadcast_proc_exit();
_rpcsvcdirty = 1;
switch (rqstp->rq_proc) {
case YPBINDPROC_NULL:
xdr_argument = xdr_void;
xdr_result = xdr_void;
local = (char *(*)()) ypbindproc_null_3;
break;
case YPBINDPROC_DOMAIN:
xdr_argument = xdr_ypbind_domain;
xdr_result = xdr_ypbind_resp;
local = (char *(*)()) ypbindproc_domain_3;
break;
case YPBINDPROC_SETDOM:
xdr_argument = xdr_ypbind_setdom;
xdr_result = xdr_void;
local = (char *(*)()) ypbindproc_setdom_3;
break;
default:
svcerr_noproc(transp);
_rpcsvcdirty = 0;
return;
}
(void) memset((char *)&argument, 0, sizeof (argument));
if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
svcerr_decode(transp);
_rpcsvcdirty = 0;
return;
}
if (rqstp->rq_proc == YPBINDPROC_SETDOM)
result = (*local)(&argument, rqstp, transp);
else
result = (*local)(&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
svcerr_systemerr(transp);
}
if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
syslog(LOG_ERR, "unable to free arguments");
exit(1);
}
_rpcsvcdirty = 0;
}
void
ypbindprog_2(rqstp, transp)
struct svc_req *rqstp;
register SVCXPRT *transp;
{
union {
domainname_2 ypbindproc_domain_2_arg;
ypbind_setdom_2 ypbindproc_setdom_2_arg;
} argument;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
if (sigcld_event)
broadcast_proc_exit();
_rpcsvcdirty = 1;
switch (rqstp->rq_proc) {
case YPBINDPROC_NULL:
xdr_argument = xdr_void;
xdr_result = xdr_void;
/* XXX - don't need two null procedures */
local = (char *(*)()) ypbindproc_null_3;
break;
case YPBINDPROC_DOMAIN:
xdr_argument = (bool_t (*)())xdr_ypdomain_wrap_string;
xdr_result = xdr_ypbind_resp_2;
local = (char *(*)()) ypbindproc_domain_2;
break;
case YPBINDPROC_SETDOM: /* not supported, fall through to error */
default:
svcerr_noproc(transp);
_rpcsvcdirty = 0;
return;
}
(void) memset((char *)&argument, 0, sizeof (argument));
if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
svcerr_decode(transp);
_rpcsvcdirty = 0;
return;
}
result = (*local)(&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
svcerr_systemerr(transp);
}
if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
syslog(LOG_ERR, "unable to free arguments");
exit(1);
}
_rpcsvcdirty = 0;
}
/*
* We clear out any old bindings that might have been
* left behind. If there is already a ypbind running,
* it will no longer get requests. We are in control
* now. We ignore the error from rpcb_unset() because
* this is just a "best effort". If the rpcb_unset()
* does fail, we will get an error in svc_reg(). By
* using 0 for the last argument we are telling the
* portmapper to remove the bindings for all transports.
*/
static
void
clear_bindings()
{
rpcb_unset(YPBINDPROG, YPBINDVERS, 0);
rpcb_unset(YPBINDPROG, YPBINDVERS_2, 0);
rpcb_unset(YPBINDPROG, YPBINDVERS_1, 0);
}
/*
* This routine is called when we are killed (by most signals).
* It first tries to unregister with the portmapper. Then it
* resets the signal handler to the default so that if we get
* the same signal, we will just go away. We clean up our
* children by doing a hold in SIGTERM and then killing the
* process group (-getpid()) with SIGTERM. Finally, we redeliver
* the signal to ourselves (the handler was reset to the default)
* so that we will do the normal handling (e.g., coredump).
* If we can't kill ourselves, we get drastic and just exit
* after sleeping for a couple of seconds.
*
* This code was taken from the SunOS version of ypbind.
*/
static
void
unregister(int code)
{
clear_bindings();
clean_cache();
signal(code, SIG_DFL); /* to prevent recursive calls to unregister */
fprintf(stderr, "ypbind: goind down on signal %d\n", code);
sighold(SIGCHLD);
sighold(SIGTERM);
kill(-getpid(), SIGTERM); /* kill process group (i.e., children) */
sigrelse(SIGTERM);
kill(getpid(), code); /* throw signal again */
sleep(2);
exit(-1);
}
static
void
set_signal_handlers()
{
int i;
for (i = 1; i <= SIGTERM; i++) {
if (i == SIGCHLD)
continue;
else if (i == SIGHUP)
signal(i, SIG_IGN);
else
signal(i, unregister);
}
}
void
msgout(msg)
char *msg;
{
#ifdef RPC_SVC_FG
if (_rpcpmstart)
syslog(LOG_ERR, msg);
else
(void) fprintf(stderr, "%s\n", msg);
#else
syslog(LOG_ERR, msg);
#endif
}
void
closedown()
{
if (_rpcsvcdirty == 0) {
int i, openfd;
struct t_info tinfo;
if (t_getinfo(0, &tinfo) || (tinfo.servtype == T_CLTS))
exit(0);
for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)
if (svc_pollfd[i].fd >= 0)
openfd++;
if (openfd <= 1)
exit(0);
}
(void) alarm(_RPCSVC_CLOSEDOWN);
}
|