yp_b_svc.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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
* 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 2004 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 <stdlib.h>
#include <signal.h>
#include <memory.h>
#include <netconfig.h>
#include <syslog.h>
#include "yp_b.h"
#include <sys/resource.h>
#include <unistd.h>
#include <string.h>
#include <tiuser.h>
#ifdef DEBUG
#define RPC_SVC_FG
#endif
#define _RPCSVC_CLOSEDOWN 120
static int _rpcpmstart; /* Started by a port monitor ? */
static int _rpcsvcdirty; /* Still serving ? */
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 argc;
char **argv;
{
int pfd[2];
void *nc_handle;
int loopback_found = 0, udp_found = 0;
int pipe_closed = 0;
int connmaxrec = RPC_MAXDATASIZE;
if (geteuid() != 0) {
exit(1);
}
argc--;
argv++;
while (argc > 0) {
setok = YPSETLOCAL;
} else {
"usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
exit(1);
}
argc--,
argv++;
}
"ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
}
if (setok == YPSETLOCAL) {
"ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
}
"ypbind -broadcast: allowing broadcast! \
(insecure and transport dependent)\n");
}
"%s: no info on servers - run ypinit -c\n", Argv[0]);
exit(1);
}
} else {
Argv[0]);
exit(1);
}
/*
* 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.
*/
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;
int pmclose;
extern char *getenv();
/*
* Set non-blocking mode and maximum record size for
* connection oriented RPC transports.
*/
msgout("unable to set maximum RPC record size");
}
#ifdef DEBUG
msgout("cannot get transport name");
#endif
#ifdef DEBUG
msgout("cannot get transport info");
#endif
}
msgout("cannot create server handle");
exit(1);
}
"could not negotiate with loopback tranport %s",
}
}
if (nconf)
msgout("unable to register (YPBINDPROG, YPBINDVERS).");
exit(1);
}
ypbindprog_2, 0)) {
"unable to register (YPBINDPROG, YPBINDVERS_2).");
exit(1);
}
/* version 2 and version 1 are the same as far as we care */
ypbindprog_2, 0)) {
"unable to register (YPBINDPROG, YPBINDVERS_1).");
exit(1);
}
if (pmclose) {
(void) alarm(_RPCSVC_CLOSEDOWN);
}
#ifdef 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.
*/
perror("pipe");
exit(1);
}
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).
*/
exit(0);
}
/* close all files except pfd[1] */
(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.
*/
msgout("unable to set maximum RPC record size");
}
/*
* from being hijacked by a bind to a more specific addr.
*/
}
#ifdef INIT_DEFAULT
#endif
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) {
pipe_closed = 1;
}
if (!__rpcbind_is_up()) {
msgout("terminating: rpcbind is not running");
exit(1);
}
msgout("terminating: cannot create rpcbind handle");
exit(1);
}
} else {
}
ypbindprog_2, nconf)) {
"unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
continue;
}
/* For NC_INET, register v1 as well; error is fatal */
nconf);
ypbindprog_2, nconf)) {
"unable to register (YPBINDPROG, YPBINDVERS_1).");
exit(1);
}
}
udp_found++;
} else {
}
}
"could not negotiate with loopback tranport %s",
}
/*
* 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) &&
pipe_closed = 1;
}
}
}
/* Did we manage to register all IPv4 or all IPv6 transports ? */
"unable to register all %s transports, exiting..",
NC_INET);
exit(1);
"unable to register all %s transports, exiting..",
NC_INET6);
exit(1);
}
if (!pipe_closed) {
pipe_closed = 1;
}
if (!loopback_found) {
"could not find loopback transports, exiting..");
exit(1);
}
if (!udp_found) {
"could not find inet-clts (udp) transport, exiting..");
exit(1);
}
svc_run();
exit(1);
/* NOTREACHED */
}
/*
* Callback function for fdwalk() to close all files.
*/
static int
{
return (0);
}
void
{
union {
} argument;
char *result;
char *(*local)();
if (sigcld_event)
_rpcsvcdirty = 1;
case YPBINDPROC_NULL:
local = (char *(*)()) ypbindproc_null_3;
break;
case YPBINDPROC_DOMAIN:
local = (char *(*)()) ypbindproc_domain_3;
break;
case YPBINDPROC_SETDOM:
local = (char *(*)()) ypbindproc_setdom_3;
break;
default:
_rpcsvcdirty = 0;
return;
}
_rpcsvcdirty = 0;
return;
}
else
}
exit(1);
}
_rpcsvcdirty = 0;
}
void
{
union {
} argument;
char *result;
char *(*local)();
if (sigcld_event)
_rpcsvcdirty = 1;
case YPBINDPROC_NULL:
/* XXX - don't need two null procedures */
local = (char *(*)()) ypbindproc_null_3;
break;
case YPBINDPROC_DOMAIN:
local = (char *(*)()) ypbindproc_domain_2;
break;
case YPBINDPROC_SETDOM: /* not supported, fall through to error */
default:
_rpcsvcdirty = 0;
return;
}
_rpcsvcdirty = 0;
return;
}
}
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
{
}
/*
* 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)
{
clean_cache();
sleep(2);
exit(-1);
}
static
void
{
int i;
for (i = 1; i <= SIGTERM; i++) {
if (i == SIGCHLD)
continue;
else if (i == SIGHUP)
else
signal(i, unregister);
}
}
void
char *msg;
{
#ifdef RPC_SVC_FG
if (_rpcpmstart)
else
#else
#endif
}
void
{
if (_rpcsvcdirty == 0) {
int i, openfd;
exit(0);
if (svc_pollfd[i].fd >= 0)
openfd++;
if (openfd <= 1)
exit(0);
}
(void) alarm(_RPCSVC_CLOSEDOWN);
}