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