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