10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include "mhd_local.h" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <grp.h> 310Sstevel@tonic-gate #include <pwd.h> 320Sstevel@tonic-gate #include <signal.h> 33*1914Scasper #include <stdio_ext.h> 340Sstevel@tonic-gate #include <syslog.h> 350Sstevel@tonic-gate #include <netdir.h> 360Sstevel@tonic-gate #include <netdb.h> 370Sstevel@tonic-gate #include <sys/resource.h> 380Sstevel@tonic-gate #include <sys/priocntl.h> 390Sstevel@tonic-gate #include <sys/rtpriocntl.h> 400Sstevel@tonic-gate #include <sys/utsname.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate extern void nc_perror(const char *msg); 430Sstevel@tonic-gate 440Sstevel@tonic-gate /* daemon name */ 450Sstevel@tonic-gate static char *myname = "rpc.metamhd"; 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * reset and exit daemon 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate void 510Sstevel@tonic-gate mhd_exit( 520Sstevel@tonic-gate int eval 530Sstevel@tonic-gate ) 540Sstevel@tonic-gate { 550Sstevel@tonic-gate /* log exit */ 560Sstevel@tonic-gate mhd_eprintf("exiting with %d\n", eval); 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* exit with value */ 590Sstevel@tonic-gate exit(eval); 600Sstevel@tonic-gate } 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * signal catchers 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate static void 660Sstevel@tonic-gate mhd_catcher( 670Sstevel@tonic-gate int sig 680Sstevel@tonic-gate ) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate char buf[128]; 710Sstevel@tonic-gate char *msg; 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* log signal */ 740Sstevel@tonic-gate if ((msg = strsignal(sig)) == NULL) { 750Sstevel@tonic-gate (void) sprintf(buf, "unknown signal %d", sig); 760Sstevel@tonic-gate msg = buf; 770Sstevel@tonic-gate } 780Sstevel@tonic-gate mhd_eprintf("%s\n", msg); 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* let default handler do it's thing */ 810Sstevel@tonic-gate (void) sigset(sig, SIG_DFL); 820Sstevel@tonic-gate if (kill(getpid(), sig) != 0) { 830Sstevel@tonic-gate mhd_perror("kill(getpid())"); 840Sstevel@tonic-gate mhd_exit(-sig); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * initialize daemon 900Sstevel@tonic-gate */ 910Sstevel@tonic-gate static int 920Sstevel@tonic-gate mhd_setup( 930Sstevel@tonic-gate mhd_error_t *mhep 940Sstevel@tonic-gate ) 950Sstevel@tonic-gate { 960Sstevel@tonic-gate struct rlimit rlimit; 970Sstevel@tonic-gate pcinfo_t pcinfo; 980Sstevel@tonic-gate pcparms_t pcparms; 990Sstevel@tonic-gate rtparms_t *rtparmsp = (rtparms_t *)pcparms.pc_clparms; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* catch common signals */ 1020Sstevel@tonic-gate if ((sigset(SIGHUP, mhd_catcher) == SIG_ERR) || 1030Sstevel@tonic-gate (sigset(SIGINT, mhd_catcher) == SIG_ERR) || 1040Sstevel@tonic-gate (sigset(SIGABRT, mhd_catcher) == SIG_ERR) || 1050Sstevel@tonic-gate (sigset(SIGBUS, mhd_catcher) == SIG_ERR) || 1060Sstevel@tonic-gate (sigset(SIGSEGV, mhd_catcher) == SIG_ERR) || 1070Sstevel@tonic-gate (sigset(SIGPIPE, mhd_catcher) == SIG_ERR) || 1080Sstevel@tonic-gate (sigset(SIGTERM, mhd_catcher) == SIG_ERR)) { 1090Sstevel@tonic-gate return (mhd_error(mhep, errno, "sigset")); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* ignore SIGHUP (used in mhd_cv_timedwait) */ 1130Sstevel@tonic-gate if (sigset(SIGALRM, SIG_IGN) == SIG_ERR) { 1140Sstevel@tonic-gate return (mhd_error(mhep, errno, "sigset")); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* increase number of file descriptors */ 1180Sstevel@tonic-gate (void) memset(&rlimit, 0, sizeof (rlimit)); 1190Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlimit) != 0) 1200Sstevel@tonic-gate return (mhd_error(mhep, errno, "getrlimit(RLIMIT_NOFILE)")); 1210Sstevel@tonic-gate rlimit.rlim_cur = rlimit.rlim_max = 1024; 1220Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlimit) != 0) 1230Sstevel@tonic-gate return (mhd_error(mhep, errno, "setrlimit(RLIMIT_NOFILE)")); 124*1914Scasper (void) enable_extended_FILE_stdio(-1, -1); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* set default RT priority */ 1270Sstevel@tonic-gate (void) memset(&pcinfo, 0, sizeof (pcinfo)); 1280Sstevel@tonic-gate (void) strncpy(pcinfo.pc_clname, "RT", sizeof (pcinfo.pc_clname)); 1290Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) < 0) 1300Sstevel@tonic-gate return (mhd_error(mhep, errno, "priocntl(PC_GETCID): \"RT\"")); 1310Sstevel@tonic-gate (void) memset(&pcparms, 0, sizeof (pcparms)); 1320Sstevel@tonic-gate pcparms.pc_cid = pcinfo.pc_cid; 1330Sstevel@tonic-gate rtparmsp->rt_pri = RT_NOCHANGE; 1340Sstevel@tonic-gate rtparmsp->rt_tqsecs = (ulong_t)RT_NOCHANGE; 1350Sstevel@tonic-gate rtparmsp->rt_tqnsecs = RT_NOCHANGE; 1360Sstevel@tonic-gate if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) != 0) 1370Sstevel@tonic-gate return (mhd_error(mhep, errno, "priocntl(PC_SETPARMS)")); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* return success */ 1400Sstevel@tonic-gate return (0); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate /* 1440Sstevel@tonic-gate * (re)initalize daemon 1450Sstevel@tonic-gate */ 1460Sstevel@tonic-gate static int 1470Sstevel@tonic-gate mhd_init_daemon( 1480Sstevel@tonic-gate mhd_error_t *mhep 1490Sstevel@tonic-gate ) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate static int already = 0; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* setup */ 1540Sstevel@tonic-gate if (! already) { 1550Sstevel@tonic-gate if (mhd_setup(mhep) != 0) 1560Sstevel@tonic-gate return (-1); 1570Sstevel@tonic-gate openlog(myname, LOG_CONS, LOG_DAEMON); 1580Sstevel@tonic-gate already = 1; 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* return success */ 1620Sstevel@tonic-gate return (0); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * get my nodename 1670Sstevel@tonic-gate */ 1680Sstevel@tonic-gate static char * 1690Sstevel@tonic-gate mynodename() 1700Sstevel@tonic-gate { 1710Sstevel@tonic-gate static struct utsname myuname; 1720Sstevel@tonic-gate static int done = 0; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate if (! done) { 1750Sstevel@tonic-gate if (uname(&myuname) == -1) { 1760Sstevel@tonic-gate mhd_perror("uname"); 1770Sstevel@tonic-gate assert(0); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate done = 1; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate return (myuname.nodename); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate /* 1850Sstevel@tonic-gate * check for trusted host and user 1860Sstevel@tonic-gate */ 1870Sstevel@tonic-gate static int 1880Sstevel@tonic-gate check_host( 1890Sstevel@tonic-gate struct svc_req *rqstp /* RPC stuff */ 1900Sstevel@tonic-gate ) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate struct authsys_parms *sys_credp; 1930Sstevel@tonic-gate SVCXPRT *transp = rqstp->rq_xprt; 1940Sstevel@tonic-gate struct netconfig *nconfp = NULL; 1950Sstevel@tonic-gate struct nd_hostservlist *hservlistp = NULL; 1960Sstevel@tonic-gate int i; 1970Sstevel@tonic-gate int rval = -1; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* check for root */ 2000Sstevel@tonic-gate /*LINTED*/ 2010Sstevel@tonic-gate sys_credp = (struct authsys_parms *)rqstp->rq_clntcred; 2020Sstevel@tonic-gate assert(sys_credp != NULL); 2030Sstevel@tonic-gate if (sys_credp->aup_uid != 0) 2040Sstevel@tonic-gate goto out; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* get hostnames */ 2070Sstevel@tonic-gate if (transp->xp_netid == NULL) { 2080Sstevel@tonic-gate mhd_eprintf("transp->xp_netid == NULL\n"); 2090Sstevel@tonic-gate goto out; 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate if ((nconfp = getnetconfigent(transp->xp_netid)) == NULL) { 2120Sstevel@tonic-gate #ifdef DEBUG 2130Sstevel@tonic-gate nc_perror("getnetconfigent(transp->xp_netid)"); 2140Sstevel@tonic-gate #endif 2150Sstevel@tonic-gate goto out; 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate if ((__netdir_getbyaddr_nosrv(nconfp, &hservlistp, &transp->xp_rtaddr) 2180Sstevel@tonic-gate != 0) || (hservlistp == NULL)) { 2190Sstevel@tonic-gate #ifdef DEBUG 2200Sstevel@tonic-gate netdir_perror("netdir_getbyaddr(transp->xp_rtaddr)"); 2210Sstevel@tonic-gate #endif 2220Sstevel@tonic-gate goto out; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* check hostnames */ 2260Sstevel@tonic-gate for (i = 0; (i < hservlistp->h_cnt); ++i) { 2270Sstevel@tonic-gate struct nd_hostserv *hservp = &hservlistp->h_hostservs[i]; 2280Sstevel@tonic-gate char *hostname = hservp->h_host; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate /* localhost is OK */ 2310Sstevel@tonic-gate if (strcmp(hostname, mynodename()) == 0) { 2320Sstevel@tonic-gate rval = 0; 2330Sstevel@tonic-gate goto out; 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* check for remote root access */ 2370Sstevel@tonic-gate if (ruserok(hostname, 1, "root", "root") == 0) { 2380Sstevel@tonic-gate rval = 0; 2390Sstevel@tonic-gate goto out; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* cleanup, return success */ 2440Sstevel@tonic-gate out: 2450Sstevel@tonic-gate if (hservlistp != NULL) 2460Sstevel@tonic-gate netdir_free(hservlistp, ND_HOSTSERVLIST); 2470Sstevel@tonic-gate if (nconfp != NULL) 2480Sstevel@tonic-gate Free(nconfp); 2490Sstevel@tonic-gate return (rval); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * check for user in local group 14 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate static int 2560Sstevel@tonic-gate check_gid14( 2570Sstevel@tonic-gate uid_t uid 2580Sstevel@tonic-gate ) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate struct passwd *pwp; 2610Sstevel@tonic-gate struct group *grp; 2620Sstevel@tonic-gate char **namep; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* get user info, check default GID */ 2650Sstevel@tonic-gate if ((pwp = getpwuid(uid)) == NULL) 2660Sstevel@tonic-gate return (-1); 2670Sstevel@tonic-gate if (pwp->pw_gid == METAMHD_GID) 2680Sstevel@tonic-gate return (0); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* check in group */ 2710Sstevel@tonic-gate if ((grp = getgrgid(METAMHD_GID)) == NULL) 2720Sstevel@tonic-gate return (-1); 2730Sstevel@tonic-gate for (namep = grp->gr_mem; ((*namep != NULL) && (**namep != '\0')); 2740Sstevel@tonic-gate ++namep) { 2750Sstevel@tonic-gate if (strcmp(*namep, pwp->pw_name) == 0) 2760Sstevel@tonic-gate return (0); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate return (-1); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * check AUTH_SYS 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate static int 2850Sstevel@tonic-gate check_sys( 2860Sstevel@tonic-gate struct svc_req *rqstp, /* RPC stuff */ 2870Sstevel@tonic-gate int amode, /* R_OK | W_OK */ 2880Sstevel@tonic-gate mhd_error_t *mhep /* returned status */ 2890Sstevel@tonic-gate ) 2900Sstevel@tonic-gate { 2910Sstevel@tonic-gate static mutex_t mx = DEFAULTMUTEX; 2920Sstevel@tonic-gate struct authsys_parms *sys_credp; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* for read, anything is OK */ 2950Sstevel@tonic-gate if (! (amode & W_OK)) 2960Sstevel@tonic-gate return (0); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* single thread (not really needed if daemon stays single threaded) */ 2990Sstevel@tonic-gate mutex_lock(&mx); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* check for remote root or METAMHD_GID */ 3020Sstevel@tonic-gate /*LINTED*/ 3030Sstevel@tonic-gate sys_credp = (struct authsys_parms *)rqstp->rq_clntcred; 3040Sstevel@tonic-gate if ((check_gid14(sys_credp->aup_uid) == 0) || 3050Sstevel@tonic-gate (check_host(rqstp) == 0)) { 3060Sstevel@tonic-gate mutex_unlock(&mx); 3070Sstevel@tonic-gate return (0); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* return failure */ 3110Sstevel@tonic-gate mutex_unlock(&mx); 3120Sstevel@tonic-gate return (mhd_error(mhep, EACCES, myname)); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * setup RPC service 3170Sstevel@tonic-gate * 3180Sstevel@tonic-gate * if can't authenticate return < 0 3190Sstevel@tonic-gate * if any other error return > 0 3200Sstevel@tonic-gate */ 3210Sstevel@tonic-gate int 3220Sstevel@tonic-gate mhd_init( 3230Sstevel@tonic-gate struct svc_req *rqstp, /* RPC stuff */ 3240Sstevel@tonic-gate int amode, /* R_OK | W_OK */ 3250Sstevel@tonic-gate mhd_error_t *mhep /* returned status */ 3260Sstevel@tonic-gate ) 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate SVCXPRT *transp = rqstp->rq_xprt; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * initialize 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate (void) memset(mhep, 0, sizeof (*mhep)); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * check credentials 3370Sstevel@tonic-gate */ 3380Sstevel@tonic-gate switch (rqstp->rq_cred.oa_flavor) { 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate /* UNIX flavor */ 3410Sstevel@tonic-gate case AUTH_SYS: 3420Sstevel@tonic-gate { 3430Sstevel@tonic-gate if (check_sys(rqstp, amode, mhep) != 0) 3440Sstevel@tonic-gate return (1); /* error */ 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* can't authenticate anything else */ 3490Sstevel@tonic-gate default: 3500Sstevel@tonic-gate svcerr_weakauth(transp); 3510Sstevel@tonic-gate return (-1); /* weak authentication */ 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * (re)initialize 3570Sstevel@tonic-gate */ 3580Sstevel@tonic-gate if (mhd_init_daemon(mhep) != 0) 3590Sstevel@tonic-gate return (1); /* error */ 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* return success */ 3620Sstevel@tonic-gate return (0); 3630Sstevel@tonic-gate } 364