17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
59acbbeann * Common Development and Distribution License (the "License").
69acbbeann * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
229ff75adSurya Prakki * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bdstevel@tonic-gate */
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
267c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
307c478bdstevel@tonic-gate * under license from the Regents of the University of California.
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/*
347c478bdstevel@tonic-gate * This module provides the user level support for the NFSv4
357c478bdstevel@tonic-gate * callback program.  It is modeled after nfsd.  When a nfsv4
367c478bdstevel@tonic-gate * mount occurs, the mount command forks and the child runs
377c478bdstevel@tonic-gate * start_nfs4_callback.  If this is the first mount, then the
387c478bdstevel@tonic-gate * process will hang around listening for incoming connection
397c478bdstevel@tonic-gate * requests from the nfsv4 server.
407c478bdstevel@tonic-gate *
417c478bdstevel@tonic-gate * For connection-less protocols, the krpc is started immediately.
427c478bdstevel@tonic-gate * For connection oriented protocols, the kernel module is informed
437c478bdstevel@tonic-gate * of netid and universal address that it can give this
447c478bdstevel@tonic-gate * information to the server during setclientid.
457c478bdstevel@tonic-gate */
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate#include <sys/param.h>
487c478bdstevel@tonic-gate#include <sys/types.h>
497c478bdstevel@tonic-gate#include <syslog.h>
507c478bdstevel@tonic-gate#include <tiuser.h>
517c478bdstevel@tonic-gate#include <rpc/rpc.h>
527c478bdstevel@tonic-gate#include <errno.h>
537c478bdstevel@tonic-gate#include <thread.h>
547c478bdstevel@tonic-gate#include <sys/resource.h>
557c478bdstevel@tonic-gate#include <sys/file.h>
567c478bdstevel@tonic-gate#include <nfs/nfs.h>
577c478bdstevel@tonic-gate#include <nfs/nfssys.h>
587c478bdstevel@tonic-gate#include <stdio.h>
597c478bdstevel@tonic-gate#include <stdlib.h>
607c478bdstevel@tonic-gate#include <netconfig.h>
617c478bdstevel@tonic-gate#include <netdir.h>
627c478bdstevel@tonic-gate#include <string.h>
637c478bdstevel@tonic-gate#include <unistd.h>
647c478bdstevel@tonic-gate#include <stropts.h>
657c478bdstevel@tonic-gate#include <sys/tihdr.h>
667c478bdstevel@tonic-gate#include <netinet/tcp.h>
677c478bdstevel@tonic-gate#include "nfs_tbind.h"
687c478bdstevel@tonic-gate#include "thrpool.h"
697c478bdstevel@tonic-gate#include <rpcsvc/nfs4_prot.h>
707c478bdstevel@tonic-gate#include <netdb.h>
717c478bdstevel@tonic-gate#include <signal.h>
727c478bdstevel@tonic-gate#include <strings.h>
737c478bdstevel@tonic-gate#include <priv_utils.h>
747c478bdstevel@tonic-gate#include <rpcsvc/daemon_utils.h>
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gatestatic	int	nfs4svc(int, struct netbuf *, struct netconfig *, int,
777c478bdstevel@tonic-gate		struct netbuf *);
787c478bdstevel@tonic-gateextern	int	_nfssys(int, void *);
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gatestatic	char	*MyName;
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate/*
837c478bdstevel@tonic-gate * The following are all globals used by routines in nfs_tbind.c.
847c478bdstevel@tonic-gate */
857c478bdstevel@tonic-gatesize_t	end_listen_fds;		/* used by conn_close_oldest() */
867c478bdstevel@tonic-gatesize_t	num_fds = 0;		/* used by multiple routines */
877c478bdstevel@tonic-gateint	listen_backlog = 32;	/* used by bind_to_{provider,proto}() */
887c478bdstevel@tonic-gateint	num_servers;		/* used by cots_listen_event() */
897c478bdstevel@tonic-gateint	(*Mysvc)(int, struct netbuf, struct netconfig *) = NULL;
907c478bdstevel@tonic-gate				/* used by cots_listen_event() */
917c478bdstevel@tonic-gateint	max_conns_allowed = -1;	/* used by cots_listen_event() */
927c478bdstevel@tonic-gate
931160694jwahligint
941160694jwahligmain(int argc, char *argv[])
957c478bdstevel@tonic-gate{
967c478bdstevel@tonic-gate	int pid;
977c478bdstevel@tonic-gate	int i;
987c478bdstevel@tonic-gate	struct protob *protobp;
997c478bdstevel@tonic-gate	struct flock f;
1007c478bdstevel@tonic-gate	pid_t pi;
1017c478bdstevel@tonic-gate	struct svcpool_args cb_svcpool;
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gate	MyName = "nfs4cbd";
1047c478bdstevel@tonic-gate	Mysvc4 = nfs4svc;
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate#ifndef	DEBUG
1077c478bdstevel@tonic-gate	/*
1087c478bdstevel@tonic-gate	 * Close existing file descriptors, open "/dev/null" as
1097c478bdstevel@tonic-gate	 * standard input, output, and error, and detach from
1107c478bdstevel@tonic-gate	 * controlling terminal.
1117c478bdstevel@tonic-gate	 */
1127c478bdstevel@tonic-gate	closefrom(0);
1137c478bdstevel@tonic-gate	(void) open("/dev/null", O_RDONLY);
1147c478bdstevel@tonic-gate	(void) open("/dev/null", O_WRONLY);
1157c478bdstevel@tonic-gate	(void) dup(1);
1167c478bdstevel@tonic-gate	(void) setsid();
1177c478bdstevel@tonic-gate#endif
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate	/*
1207c478bdstevel@tonic-gate	 * create a child to continue our work
1217c478bdstevel@tonic-gate	 * Parent's exit will tell mount command we're ready to go
1227c478bdstevel@tonic-gate	 */
1237c478bdstevel@tonic-gate	if ((pi = fork()) > 0) {
1247c478bdstevel@tonic-gate		exit(0);
1257c478bdstevel@tonic-gate	}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	if (pi == -1) {
1287c478bdstevel@tonic-gate		(void) syslog(LOG_ERR,
1297c478bdstevel@tonic-gate			"Could not start NFS4_CALLBACK service");
1307c478bdstevel@tonic-gate		exit(1);
1317c478bdstevel@tonic-gate	}
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	(void) _create_daemon_lock(NFS4CBD, DAEMON_UID, DAEMON_GID);
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate	svcsetprio();
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
1387c478bdstevel@tonic-gate	    DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, (char *)NULL) == -1) {
1397c478bdstevel@tonic-gate		(void) fprintf(stderr, "%s must be run with sufficient"
1407c478bdstevel@tonic-gate			" privileges\n", argv[0]);
1417c478bdstevel@tonic-gate		exit(1);
1427c478bdstevel@tonic-gate	}
1437c478bdstevel@tonic-gate	/* Basic privileges we don't need, remove from E/P. */
1447c478bdstevel@tonic-gate	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_FILE_LINK_ANY,
1457c478bdstevel@tonic-gate	    PRIV_PROC_SESSION, PRIV_PROC_INFO, (char *)NULL);
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate	/*
1487c478bdstevel@tonic-gate	 * establish our lock on the lock file and write our pid to it.
1497c478bdstevel@tonic-gate	 * exit if some other process holds the lock, or if there's any
1507c478bdstevel@tonic-gate	 * error in writing/locking the file.
1517c478bdstevel@tonic-gate	 */
1527c478bdstevel@tonic-gate	pid = _enter_daemon_lock(NFS4CBD);
1537c478bdstevel@tonic-gate	switch (pid) {
1547c478bdstevel@tonic-gate	case 0:
1557c478bdstevel@tonic-gate		break;
1567c478bdstevel@tonic-gate	case -1:
1577c478bdstevel@tonic-gate		syslog(LOG_ERR, "error locking for %s: %s", NFS4CBD,
1587c478bdstevel@tonic-gate		    strerror(errno));
1597c478bdstevel@tonic-gate		exit(2);
1607c478bdstevel@tonic-gate	default:
1617c478bdstevel@tonic-gate		/* daemon was already running */
1627c478bdstevel@tonic-gate		exit(0);
1637c478bdstevel@tonic-gate	}
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	cb_svcpool.id = NFS_CB_SVCPOOL_ID;
1687c478bdstevel@tonic-gate	cb_svcpool.maxthreads = 0;
1697c478bdstevel@tonic-gate	cb_svcpool.redline = 0;
1707c478bdstevel@tonic-gate	cb_svcpool.qsize = 0;
1717c478bdstevel@tonic-gate	cb_svcpool.timeout = 0;
1727c478bdstevel@tonic-gate	cb_svcpool.stksize = 0;
1737c478bdstevel@tonic-gate	cb_svcpool.max_same_xprt = 0;
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate	/* create a SVC_POOL for the nfsv4 callback deamon */
1767c478bdstevel@tonic-gate	if (_nfssys(SVCPOOL_CREATE, &cb_svcpool)) {
1777c478bdstevel@tonic-gate		(void) syslog(LOG_ERR, "can't setup NFS_CB SVCPOOL: Exiting");
1787c478bdstevel@tonic-gate		exit(1);
1797c478bdstevel@tonic-gate	}
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gate	/*
1827c478bdstevel@tonic-gate	 * Set up blocked thread to do LWP creation on behalf of the kernel.
1837c478bdstevel@tonic-gate	 */
1847c478bdstevel@tonic-gate	if (svcwait(NFS_CB_SVCPOOL_ID)) {
1857c478bdstevel@tonic-gate		(void) syslog(LOG_ERR,
1867c478bdstevel@tonic-gate		    "Can't set up NFS_CB LWP creator: Exiting");
1877c478bdstevel@tonic-gate		exit(1);
1887c478bdstevel@tonic-gate	}
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	/*
1927c478bdstevel@tonic-gate	 * Build a protocol block list for registration.
1937c478bdstevel@tonic-gate	 */
1947c478bdstevel@tonic-gate	protobp = (struct protob *)malloc(sizeof (struct protob));
1957c478bdstevel@tonic-gate	protobp->serv = "NFS4_CALLBACK";
1967c478bdstevel@tonic-gate	protobp->versmin = NFS_CB;
1977c478bdstevel@tonic-gate	protobp->versmax = NFS_CB;
1987c478bdstevel@tonic-gate	protobp->program = NFS4_CALLBACK;
1997c478bdstevel@tonic-gate	protobp->next = NULL;
2007c478bdstevel@tonic-gate
2019ff75adSurya Prakki	if (do_all(protobp, NULL) == -1) {
2027c478bdstevel@tonic-gate		exit(1);
2037c478bdstevel@tonic-gate	}
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate	free(protobp);
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate	if (num_fds == 0) {
2087c478bdstevel@tonic-gate		(void) syslog(LOG_ERR,
2097c478bdstevel@tonic-gate		"Could not start NFS4_CALLBACK service for any protocol");
2107c478bdstevel@tonic-gate		exit(1);
2117c478bdstevel@tonic-gate	}
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate	end_listen_fds = num_fds;
2147c478bdstevel@tonic-gate	/*
2157c478bdstevel@tonic-gate	 * Poll for non-data control events on the transport descriptors.
2167c478bdstevel@tonic-gate	 */
2177c478bdstevel@tonic-gate	poll_for_action();
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate	/*
2207c478bdstevel@tonic-gate	 * If we get here, something failed in poll_for_action().
2217c478bdstevel@tonic-gate	 */
2227c478bdstevel@tonic-gate	return (1);
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gatechar *
2267c478bdstevel@tonic-gateget_uaddr(int fd, struct netconfig *nconf, struct netbuf *nb)
2277c478bdstevel@tonic-gate{
2287c478bdstevel@tonic-gate	struct nfs_svc_args nsa;
2297c478bdstevel@tonic-gate	char *ua, *ua2, *mua = NULL;
2307c478bdstevel@tonic-gate	char me[MAXHOSTNAMELEN];
2317c478bdstevel@tonic-gate	struct nd_addrlist *nas;
2327c478bdstevel@tonic-gate	struct nd_hostserv hs;
2337c478bdstevel@tonic-gate	struct nd_mergearg ma;
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	ua = taddr2uaddr(nconf, nb);
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate	if (ua == NULL) {
2387c478bdstevel@tonic-gate#ifdef	DEBUG
2397c478bdstevel@tonic-gate		fprintf(stderr, "taddr2uaddr failed for netid %s\n",
2407c478bdstevel@tonic-gate			nconf->nc_netid);
2417c478bdstevel@tonic-gate#endif
2427c478bdstevel@tonic-gate		return (NULL);
2437c478bdstevel@tonic-gate	}
2447c478bdstevel@tonic-gate
2457c478bdstevel@tonic-gate	gethostname(me, MAXHOSTNAMELEN);
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate	hs.h_host = me;
2487c478bdstevel@tonic-gate	hs.h_serv = "nfs";
2497c478bdstevel@tonic-gate	if (netdir_getbyname(nconf, &hs, &nas)) {
2507c478bdstevel@tonic-gate#ifdef DEBUG
2517c478bdstevel@tonic-gate		netdir_perror("netdir_getbyname");
2527c478bdstevel@tonic-gate#endif
2537c478bdstevel@tonic-gate		return (NULL);
2547c478bdstevel@tonic-gate	}
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate	ua2 = taddr2uaddr(nconf, nas->n_addrs);
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate	if (ua2 == NULL) {
2597c478bdstevel@tonic-gate#ifdef	DEBUG
2607c478bdstevel@tonic-gate		fprintf(stderr, "taddr2uaddr failed for netid %s.\n",
2617c478bdstevel@tonic-gate			nconf->nc_netid);
2627c478bdstevel@tonic-gate#endif
2637c478bdstevel@tonic-gate		return (NULL);
2647c478bdstevel@tonic-gate	}
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	ma.s_uaddr = ua;
2677c478bdstevel@tonic-gate	ma.c_uaddr = ua2;
2687c478bdstevel@tonic-gate	ma.m_uaddr = NULL;
2697c478bdstevel@tonic-gate
2707c478bdstevel@tonic-gate	if (netdir_options(nconf, ND_MERGEADDR, 0, (char *)&ma)) {
2717c478bdstevel@tonic-gate#ifdef DEBUG
2727c478bdstevel@tonic-gate		netdir_perror("netdir_options");
2737c478bdstevel@tonic-gate#endif
2747c478bdstevel@tonic-gate		return (NULL);
2757c478bdstevel@tonic-gate	}
2767c478bdstevel@tonic-gate
2777c478bdstevel@tonic-gate	mua = ma.m_uaddr;
2787c478bdstevel@tonic-gate	return (mua);
2797c478bdstevel@tonic-gate}
2807c478bdstevel@tonic-gate
2817c478bdstevel@tonic-gate/*
2827c478bdstevel@tonic-gate * Establish NFS4 callback service thread.
2837c478bdstevel@tonic-gate */
2847c478bdstevel@tonic-gatestatic int
2857c478bdstevel@tonic-gatenfs4svc(int fd, struct netbuf *addrmask, struct netconfig *nconf,
2867c478bdstevel@tonic-gate	int cmd, struct netbuf *addr)
2877c478bdstevel@tonic-gate{
2887c478bdstevel@tonic-gate	struct nfs4_svc_args nsa;
2897c478bdstevel@tonic-gate	char *ua;
2907c478bdstevel@tonic-gate	int error;
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate	ua = get_uaddr(fd, nconf, addr);
2937c478bdstevel@tonic-gate
2947c478bdstevel@tonic-gate	if (ua == NULL) {
2957c478bdstevel@tonic-gate		syslog(LOG_NOTICE, "nfsv4 cannot determine local hostname "
2967c478bdstevel@tonic-gate			"binding for transport %s - delegations will not be "
2977c478bdstevel@tonic-gate			"available on this transport\n", nconf->nc_netid);
2987c478bdstevel@tonic-gate		return (0);
2997c478bdstevel@tonic-gate	}
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate#ifdef	DEBUG
3027c478bdstevel@tonic-gate	if (cmd & NFS4_KRPC_START)
3037c478bdstevel@tonic-gate		fprintf(stderr, "nfs4cbd: starting callback rpc on %s %s\n",
3047c478bdstevel@tonic-gate			nconf->nc_netid, ua);
3057c478bdstevel@tonic-gate	else
3067c478bdstevel@tonic-gate		fprintf(stderr, "nfs4cbd: listening on %s %s\n",
3077c478bdstevel@tonic-gate			nconf->nc_netid, ua);
3087c478bdstevel@tonic-gate#endif
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	nsa.fd = fd;
3117c478bdstevel@tonic-gate	nsa.cmd = cmd;
3127c478bdstevel@tonic-gate	nsa.netid = nconf->nc_netid;
3137c478bdstevel@tonic-gate	if (addrmask)
3147c478bdstevel@tonic-gate		nsa.addrmask = *addrmask;
3157c478bdstevel@tonic-gate	else
3167c478bdstevel@tonic-gate		bzero(&nsa.addrmask, sizeof (struct netbuf));
3177c478bdstevel@tonic-gate	nsa.addr = ua;
3187c478bdstevel@tonic-gate	nsa.protofmly = nconf->nc_protofmly;
3197c478bdstevel@tonic-gate	nsa.proto = nconf->nc_proto;
3207c478bdstevel@tonic-gate	if ((error = _nfssys(NFS4_SVC, &nsa)) != 0)
3217c478bdstevel@tonic-gate		syslog(LOG_ERR, "nfssys NFS4_SVC failed\n");
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gate	return (error);
3247c478bdstevel@tonic-gate}
325