xref: /freebsd/lib/libutil/login_class.c (revision 2d057ca68cc302870200661677c31d13d7921d9d)
168bbf3adSDavid Nugent /*-
268bbf3adSDavid Nugent  * Copyright (c) 1996 by
368bbf3adSDavid Nugent  * Sean Eric Fagan <sef@kithrup.com>
468bbf3adSDavid Nugent  * David Nugent <davidn@blaze.net.au>
568bbf3adSDavid Nugent  * All rights reserved.
668bbf3adSDavid Nugent  *
768bbf3adSDavid Nugent  * Redistribution and use in source and binary forms, with or without
868bbf3adSDavid Nugent  * modification, is permitted provided that the following conditions
968bbf3adSDavid Nugent  * are met:
1068bbf3adSDavid Nugent  * 1. Redistributions of source code must retain the above copyright
1168bbf3adSDavid Nugent  *    notice immediately at the beginning of the file, without modification,
1268bbf3adSDavid Nugent  *    this list of conditions, and the following disclaimer.
1368bbf3adSDavid Nugent  * 2. Redistributions in binary form must reproduce the above copyright
1468bbf3adSDavid Nugent  *    notice, this list of conditions and the following disclaimer in the
1568bbf3adSDavid Nugent  *    documentation and/or other materials provided with the distribution.
1668bbf3adSDavid Nugent  * 3. This work was done expressly for inclusion into FreeBSD.  Other use
1768bbf3adSDavid Nugent  *    is permitted provided this notation is included.
1868bbf3adSDavid Nugent  * 4. Absolutely no warranty of function or purpose is made by the authors.
1968bbf3adSDavid Nugent  * 5. Modifications may be freely made to this file providing the above
2068bbf3adSDavid Nugent  *    conditions are met.
2168bbf3adSDavid Nugent  *
2268bbf3adSDavid Nugent  * High-level routines relating to use of the user capabilities database
2368bbf3adSDavid Nugent  */
2468bbf3adSDavid Nugent 
258719c58fSMatthew Dillon #include <sys/cdefs.h>
268719c58fSMatthew Dillon __FBSDID("$FreeBSD$");
278719c58fSMatthew Dillon 
28d84c4292SBrooks Davis #include <sys/param.h>
29d84c4292SBrooks Davis #include <sys/cpuset.h>
300ebec5d3SMark Murray #include <sys/mac.h>
317cc027a3SDag-Erling Smørgrav #include <sys/resource.h>
320ebec5d3SMark Murray #include <sys/rtprio.h>
337cc027a3SDag-Erling Smørgrav #include <sys/stat.h>
347cc027a3SDag-Erling Smørgrav #include <sys/time.h>
357cc027a3SDag-Erling Smørgrav 
367cc027a3SDag-Erling Smørgrav #include <ctype.h>
377cc027a3SDag-Erling Smørgrav #include <err.h>
380ebec5d3SMark Murray #include <errno.h>
3968bbf3adSDavid Nugent #include <fcntl.h>
4068bbf3adSDavid Nugent #include <login_cap.h>
4168bbf3adSDavid Nugent #include <paths.h>
420ebec5d3SMark Murray #include <pwd.h>
430ebec5d3SMark Murray #include <stdio.h>
440ebec5d3SMark Murray #include <stdlib.h>
450ebec5d3SMark Murray #include <string.h>
460ebec5d3SMark Murray #include <syslog.h>
470ebec5d3SMark Murray #include <unistd.h>
4868bbf3adSDavid Nugent 
4968bbf3adSDavid Nugent 
5068bbf3adSDavid Nugent static struct login_res {
5168bbf3adSDavid Nugent     const char *what;
5268bbf3adSDavid Nugent     rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t);
5368bbf3adSDavid Nugent     int why;
5468bbf3adSDavid Nugent } resources[] = {
5568bbf3adSDavid Nugent     { "cputime",         login_getcaptime, RLIMIT_CPU     },
5668bbf3adSDavid Nugent     { "filesize",        login_getcapsize, RLIMIT_FSIZE   },
5768bbf3adSDavid Nugent     { "datasize",        login_getcapsize, RLIMIT_DATA    },
5868bbf3adSDavid Nugent     { "stacksize",       login_getcapsize, RLIMIT_STACK   },
5968bbf3adSDavid Nugent     { "memoryuse",       login_getcapsize, RLIMIT_RSS     },
6068bbf3adSDavid Nugent     { "memorylocked",    login_getcapsize, RLIMIT_MEMLOCK },
6168bbf3adSDavid Nugent     { "maxproc",         login_getcapnum,  RLIMIT_NPROC   },
6268bbf3adSDavid Nugent     { "openfiles",       login_getcapnum,  RLIMIT_NOFILE  },
6356c04344SDavid Nugent     { "coredumpsize",    login_getcapsize, RLIMIT_CORE    },
640c697857SSheldon Hearn     { "sbsize",          login_getcapsize, RLIMIT_SBSIZE  },
6567577126SMatthew Dillon     { "vmemoryuse",      login_getcapsize, RLIMIT_VMEM    },
66bc093719SEd Schouten     { "pseudoterminals", login_getcapnum,  RLIMIT_NPTS    },
6768bbf3adSDavid Nugent     { NULL,              0,                0              }
6868bbf3adSDavid Nugent };
6968bbf3adSDavid Nugent 
7068bbf3adSDavid Nugent 
7168bbf3adSDavid Nugent void
7268bbf3adSDavid Nugent setclassresources(login_cap_t *lc)
7368bbf3adSDavid Nugent {
7456c04344SDavid Nugent     struct login_res *lr;
7568bbf3adSDavid Nugent 
761c594de5SDavid Nugent     if (lc == NULL)
771c594de5SDavid Nugent 	return;
781c594de5SDavid Nugent 
7956c04344SDavid Nugent     for (lr = resources; lr->what != NULL; ++lr) {
8056c04344SDavid Nugent 	struct rlimit	rlim;
8168bbf3adSDavid Nugent 
8268bbf3adSDavid Nugent 	/*
8368bbf3adSDavid Nugent 	 * The login.conf file can have <limit>, <limit>-max, and
8468bbf3adSDavid Nugent 	 * <limit>-cur entries.
8568bbf3adSDavid Nugent 	 * What we do is get the current current- and maximum- limits.
8668bbf3adSDavid Nugent 	 * Then, we try to get an entry for <limit> from the capability,
8768bbf3adSDavid Nugent 	 * using the current and max limits we just got as the
8868bbf3adSDavid Nugent 	 * default/error values.
8968bbf3adSDavid Nugent 	 * *Then*, we try looking for <limit>-cur and <limit>-max,
9068bbf3adSDavid Nugent 	 * again using the appropriate values as the default/error
9168bbf3adSDavid Nugent 	 * conditions.
9268bbf3adSDavid Nugent 	 */
9368bbf3adSDavid Nugent 
9456c04344SDavid Nugent 	if (getrlimit(lr->why, &rlim) != 0)
9556c04344SDavid Nugent 	    syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
9656c04344SDavid Nugent 	else {
9756c04344SDavid Nugent 	    char	name_cur[40];
9856c04344SDavid Nugent 	    char	name_max[40];
9956c04344SDavid Nugent 	    rlim_t	rcur = rlim.rlim_cur;
10056c04344SDavid Nugent 	    rlim_t	rmax = rlim.rlim_max;
10156c04344SDavid Nugent 
10256c04344SDavid Nugent 	    sprintf(name_cur, "%s-cur", lr->what);
10356c04344SDavid Nugent 	    sprintf(name_max, "%s-max", lr->what);
10468bbf3adSDavid Nugent 
10568bbf3adSDavid Nugent 	    rcur = (*lr->who)(lc, lr->what, rcur, rcur);
10668bbf3adSDavid Nugent 	    rmax = (*lr->who)(lc, lr->what, rmax, rmax);
10756c04344SDavid Nugent 	    rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
10856c04344SDavid Nugent 	    rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
10968bbf3adSDavid Nugent 
11056c04344SDavid Nugent 	    if (setrlimit(lr->why, &rlim) == -1)
11168bbf3adSDavid Nugent 		syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
11256c04344SDavid Nugent 	}
11356c04344SDavid Nugent     }
11456c04344SDavid Nugent }
11568bbf3adSDavid Nugent 
11656c04344SDavid Nugent 
11768bbf3adSDavid Nugent 
11868bbf3adSDavid Nugent static struct login_vars {
11968bbf3adSDavid Nugent     const char *tag;
12068bbf3adSDavid Nugent     const char *var;
12168bbf3adSDavid Nugent     const char *def;
122cc1b8dcbSAndrey A. Chernov     int overwrite;
12368bbf3adSDavid Nugent } pathvars[] = {
124cc1b8dcbSAndrey A. Chernov     { "path",           "PATH",       NULL, 1},
125cc1b8dcbSAndrey A. Chernov     { "cdpath",         "CDPATH",     NULL, 1},
126cc1b8dcbSAndrey A. Chernov     { "manpath",        "MANPATH",    NULL, 1},
127cc1b8dcbSAndrey A. Chernov     { NULL,             NULL,         NULL, 0}
12868bbf3adSDavid Nugent }, envars[] = {
129cc1b8dcbSAndrey A. Chernov     { "lang",           "LANG",       NULL, 1},
130cc1b8dcbSAndrey A. Chernov     { "charset",        "MM_CHARSET", NULL, 1},
131cc1b8dcbSAndrey A. Chernov     { "timezone",       "TZ",         NULL, 1},
132cc1b8dcbSAndrey A. Chernov     { "term",           "TERM",       NULL, 0},
133cc1b8dcbSAndrey A. Chernov     { NULL,             NULL,         NULL, 0}
13468bbf3adSDavid Nugent };
13568bbf3adSDavid Nugent 
13668bbf3adSDavid Nugent static char *
137b00ba4ccSRuslan Ermilov substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
13868bbf3adSDavid Nugent {
13968bbf3adSDavid Nugent     char    *np = NULL;
14068bbf3adSDavid Nugent 
14168bbf3adSDavid Nugent     if (var != NULL) {
14268bbf3adSDavid Nugent 	int	tildes = 0;
14368bbf3adSDavid Nugent 	int	dollas = 0;
14468bbf3adSDavid Nugent 	char	*p;
14568bbf3adSDavid Nugent 
14668bbf3adSDavid Nugent 	if (pwd != NULL) {
14756c04344SDavid Nugent 	    /* Count the number of ~'s in var to substitute */
148b00ba4ccSRuslan Ermilov 	    for (p = (char *)var; (p = strchr(p, '~')) != NULL; p++)
14968bbf3adSDavid Nugent 		++tildes;
15056c04344SDavid Nugent 	    /* Count the number of $'s in var to substitute */
151b00ba4ccSRuslan Ermilov 	    for (p = (char *)var; (p = strchr(p, '$')) != NULL; p++)
15268bbf3adSDavid Nugent 		++dollas;
15368bbf3adSDavid Nugent 	}
15468bbf3adSDavid Nugent 
15556c04344SDavid Nugent 	np = malloc(strlen(var) + (dollas * nlen)
15656c04344SDavid Nugent 		    - dollas + (tildes * (pch+hlen))
15756c04344SDavid Nugent 		    - tildes + 1);
15868bbf3adSDavid Nugent 
15968bbf3adSDavid Nugent 	if (np != NULL) {
16068bbf3adSDavid Nugent 	    p = strcpy(np, var);
16168bbf3adSDavid Nugent 
16268bbf3adSDavid Nugent 	    if (pwd != NULL) {
16368bbf3adSDavid Nugent 		/*
16468bbf3adSDavid Nugent 		 * This loop does user username and homedir substitutions
16568bbf3adSDavid Nugent 		 * for unescaped $ (username) and ~ (homedir)
16668bbf3adSDavid Nugent 		 */
16768bbf3adSDavid Nugent 		while (*(p += strcspn(p, "~$")) != '\0') {
16868bbf3adSDavid Nugent 		    int	l = strlen(p);
16968bbf3adSDavid Nugent 
170121ba32dSAndrey A. Chernov 		    if (p > np && *(p-1) == '\\')  /* Escaped: */
17168bbf3adSDavid Nugent 			memmove(p - 1, p, l + 1); /* Slide-out the backslash */
17268bbf3adSDavid Nugent 		    else if (*p == '~') {
1731c594de5SDavid Nugent 			int	v = pch && *(p+1) != '/'; /* Avoid double // */
1741c594de5SDavid Nugent 			memmove(p + hlen + v, p + 1, l);  /* Subst homedir */
17568bbf3adSDavid Nugent 			memmove(p, pwd->pw_dir, hlen);
1761c594de5SDavid Nugent 			if (v)
17768bbf3adSDavid Nugent 			    p[hlen] = '/';
1781c594de5SDavid Nugent 			p += hlen + v;
17968bbf3adSDavid Nugent 		    }
18068bbf3adSDavid Nugent 		    else /* if (*p == '$') */ {
1811c594de5SDavid Nugent 			memmove(p + nlen, p + 1, l);	/* Subst username */
18268bbf3adSDavid Nugent 			memmove(p, pwd->pw_name, nlen);
18368bbf3adSDavid Nugent 			p += nlen;
18468bbf3adSDavid Nugent 		    }
18568bbf3adSDavid Nugent 		}
18668bbf3adSDavid Nugent 	    }
18768bbf3adSDavid Nugent 	}
18868bbf3adSDavid Nugent     }
18956c04344SDavid Nugent 
1902d057ca6SDag-Erling Smørgrav     return (np);
19168bbf3adSDavid Nugent }
19268bbf3adSDavid Nugent 
19368bbf3adSDavid Nugent 
19468bbf3adSDavid Nugent void
19568bbf3adSDavid Nugent setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
19668bbf3adSDavid Nugent {
19768bbf3adSDavid Nugent     struct login_vars	*vars = paths ? pathvars : envars;
19868bbf3adSDavid Nugent     int			hlen = pwd ? strlen(pwd->pw_dir) : 0;
19968bbf3adSDavid Nugent     int			nlen = pwd ? strlen(pwd->pw_name) : 0;
20068bbf3adSDavid Nugent     char pch = 0;
20168bbf3adSDavid Nugent 
20268bbf3adSDavid Nugent     if (hlen && pwd->pw_dir[hlen-1] != '/')
20368bbf3adSDavid Nugent 	++pch;
20468bbf3adSDavid Nugent 
20568bbf3adSDavid Nugent     while (vars->tag != NULL) {
206b00ba4ccSRuslan Ermilov 	const char * var = paths ? login_getpath(lc, vars->tag, NULL)
20768bbf3adSDavid Nugent 				 : login_getcapstr(lc, vars->tag, NULL, NULL);
20868bbf3adSDavid Nugent 
20968bbf3adSDavid Nugent 	char * np  = substvar(var, pwd, hlen, pch, nlen);
21068bbf3adSDavid Nugent 
21168bbf3adSDavid Nugent 	if (np != NULL) {
212cc1b8dcbSAndrey A. Chernov 	    setenv(vars->var, np, vars->overwrite);
21368bbf3adSDavid Nugent 	    free(np);
21468bbf3adSDavid Nugent 	} else if (vars->def != NULL) {
21568bbf3adSDavid Nugent 	    setenv(vars->var, vars->def, 0);
21668bbf3adSDavid Nugent 	}
21768bbf3adSDavid Nugent 	++vars;
21868bbf3adSDavid Nugent     }
21968bbf3adSDavid Nugent 
22068bbf3adSDavid Nugent     /*
22168bbf3adSDavid Nugent      * If we're not processing paths, then see if there is a setenv list by
22268bbf3adSDavid Nugent      * which the admin and/or user may set an arbitrary set of env vars.
22368bbf3adSDavid Nugent      */
22468bbf3adSDavid Nugent     if (!paths) {
225547fa0d9SMark Murray 	const char	**set_env = login_getcaplist(lc, "setenv", ",");
2261c594de5SDavid Nugent 
22768bbf3adSDavid Nugent 	if (set_env != NULL) {
22868bbf3adSDavid Nugent 	    while (*set_env != NULL) {
22968bbf3adSDavid Nugent 		char	*p = strchr(*set_env, '=');
23056c04344SDavid Nugent 
2311c594de5SDavid Nugent 		if (p != NULL) {  /* Discard invalid entries */
2321c594de5SDavid Nugent 		    char	*np;
2331c594de5SDavid Nugent 
2341c594de5SDavid Nugent 		    *p++ = '\0';
2351c594de5SDavid Nugent 		    if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
2361c594de5SDavid Nugent 			setenv(*set_env, np, 1);
23768bbf3adSDavid Nugent 			free(np);
23868bbf3adSDavid Nugent 		    }
23968bbf3adSDavid Nugent 		}
24068bbf3adSDavid Nugent 		++set_env;
24168bbf3adSDavid Nugent 	    }
24268bbf3adSDavid Nugent 	}
24368bbf3adSDavid Nugent     }
24468bbf3adSDavid Nugent }
24568bbf3adSDavid Nugent 
24668bbf3adSDavid Nugent 
247d84c4292SBrooks Davis static int
248d84c4292SBrooks Davis list2cpuset(const char *list, cpuset_t *mask)
249d84c4292SBrooks Davis {
250d84c4292SBrooks Davis 	enum { NONE, NUM, DASH } state;
251d84c4292SBrooks Davis 	int lastnum;
252d84c4292SBrooks Davis 	int curnum;
253d84c4292SBrooks Davis 	const char *l;
254d84c4292SBrooks Davis 
255d84c4292SBrooks Davis 	state = NONE;
256d84c4292SBrooks Davis 	curnum = lastnum = 0;
257d84c4292SBrooks Davis 	for (l = list; *l != '\0';) {
258d84c4292SBrooks Davis 		if (isdigit(*l)) {
259d84c4292SBrooks Davis 			curnum = atoi(l);
260d84c4292SBrooks Davis 			if (curnum > CPU_SETSIZE)
261d84c4292SBrooks Davis 				errx(EXIT_FAILURE,
262d84c4292SBrooks Davis 				    "Only %d cpus supported", CPU_SETSIZE);
263d84c4292SBrooks Davis 			while (isdigit(*l))
264d84c4292SBrooks Davis 				l++;
265d84c4292SBrooks Davis 			switch (state) {
266d84c4292SBrooks Davis 			case NONE:
267d84c4292SBrooks Davis 				lastnum = curnum;
268d84c4292SBrooks Davis 				state = NUM;
269d84c4292SBrooks Davis 				break;
270d84c4292SBrooks Davis 			case DASH:
271d84c4292SBrooks Davis 				for (; lastnum <= curnum; lastnum++)
272d84c4292SBrooks Davis 					CPU_SET(lastnum, mask);
273d84c4292SBrooks Davis 				state = NONE;
274d84c4292SBrooks Davis 				break;
275d84c4292SBrooks Davis 			case NUM:
276d84c4292SBrooks Davis 			default:
277d84c4292SBrooks Davis 				return (0);
278d84c4292SBrooks Davis 			}
279d84c4292SBrooks Davis 			continue;
280d84c4292SBrooks Davis 		}
281d84c4292SBrooks Davis 		switch (*l) {
282d84c4292SBrooks Davis 		case ',':
283d84c4292SBrooks Davis 			switch (state) {
284d84c4292SBrooks Davis 			case NONE:
285d84c4292SBrooks Davis 				break;
286d84c4292SBrooks Davis 			case NUM:
287d84c4292SBrooks Davis 				CPU_SET(curnum, mask);
288d84c4292SBrooks Davis 				state = NONE;
289d84c4292SBrooks Davis 				break;
290d84c4292SBrooks Davis 			case DASH:
291d84c4292SBrooks Davis 				return (0);
292d84c4292SBrooks Davis 				break;
293d84c4292SBrooks Davis 			}
294d84c4292SBrooks Davis 			break;
295d84c4292SBrooks Davis 		case '-':
296d84c4292SBrooks Davis 			if (state != NUM)
297d84c4292SBrooks Davis 				return (0);
298d84c4292SBrooks Davis 			state = DASH;
299d84c4292SBrooks Davis 			break;
300d84c4292SBrooks Davis 		default:
301d84c4292SBrooks Davis 			return (0);
302d84c4292SBrooks Davis 		}
303d84c4292SBrooks Davis 		l++;
304d84c4292SBrooks Davis 	}
305d84c4292SBrooks Davis 	switch (state) {
306d84c4292SBrooks Davis 		case NONE:
307d84c4292SBrooks Davis 			break;
308d84c4292SBrooks Davis 		case NUM:
309d84c4292SBrooks Davis 			CPU_SET(curnum, mask);
310d84c4292SBrooks Davis 			break;
311d84c4292SBrooks Davis 		case DASH:
312d84c4292SBrooks Davis 			return (0);
313d84c4292SBrooks Davis 	}
3142d057ca6SDag-Erling Smørgrav 	return (1);
315d84c4292SBrooks Davis }
316d84c4292SBrooks Davis 
317d84c4292SBrooks Davis 
318d84c4292SBrooks Davis void
319d84c4292SBrooks Davis setclasscpumask(login_cap_t *lc)
320d84c4292SBrooks Davis {
321d84c4292SBrooks Davis 	const char *maskstr;
322d84c4292SBrooks Davis 	cpuset_t maskset;
323d84c4292SBrooks Davis 	cpusetid_t setid;
324d84c4292SBrooks Davis 
325d84c4292SBrooks Davis 	maskstr = login_getcapstr(lc, "cpumask", NULL, NULL);
326d84c4292SBrooks Davis 	CPU_ZERO(&maskset);
327d84c4292SBrooks Davis 	if (maskstr == NULL)
328d84c4292SBrooks Davis 		return;
329d84c4292SBrooks Davis 	if (strcasecmp("default", maskstr) == 0)
330d84c4292SBrooks Davis 		return;
331d84c4292SBrooks Davis 	if (!list2cpuset(maskstr, &maskset)) {
332d84c4292SBrooks Davis 		syslog(LOG_WARNING,
333d84c4292SBrooks Davis 		    "list2cpuset(%s) invalid mask specification", maskstr);
334d84c4292SBrooks Davis 		return;
335d84c4292SBrooks Davis 	}
336d84c4292SBrooks Davis 
337d84c4292SBrooks Davis 	if (cpuset(&setid) != 0) {
338d84c4292SBrooks Davis 		syslog(LOG_ERR, "cpuset(): %s", strerror(errno));
339d84c4292SBrooks Davis 		return;
340d84c4292SBrooks Davis 	}
341d84c4292SBrooks Davis 
342d84c4292SBrooks Davis 	if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
343d84c4292SBrooks Davis 	    sizeof(maskset), &maskset) != 0)
344d84c4292SBrooks Davis 		syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr,
345d84c4292SBrooks Davis 		    strerror(errno));
346d84c4292SBrooks Davis }
347d84c4292SBrooks Davis 
348d84c4292SBrooks Davis 
34968bbf3adSDavid Nugent /*
35068bbf3adSDavid Nugent  * setclasscontext()
35168bbf3adSDavid Nugent  *
35268bbf3adSDavid Nugent  * For the login class <class>, set various class context values
35368bbf3adSDavid Nugent  * (limits, mainly) to the values for that class.  Which values are
35468bbf3adSDavid Nugent  * set are controlled by <flags> -- see <login_class.h> for the
35568bbf3adSDavid Nugent  * possible values.
35668bbf3adSDavid Nugent  *
35768bbf3adSDavid Nugent  * setclasscontext() can only set resources, priority, and umask.
35868bbf3adSDavid Nugent  */
35968bbf3adSDavid Nugent 
36068bbf3adSDavid Nugent int
36168bbf3adSDavid Nugent setclasscontext(const char *classname, unsigned int flags)
36268bbf3adSDavid Nugent {
36368bbf3adSDavid Nugent     int		rc;
36456c04344SDavid Nugent     login_cap_t *lc;
36556c04344SDavid Nugent 
36656c04344SDavid Nugent     lc = login_getclassbyname(classname, NULL);
36756c04344SDavid Nugent 
36856c04344SDavid Nugent     flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
36956c04344SDavid Nugent 	    LOGIN_SETUMASK | LOGIN_SETPATH;
37056c04344SDavid Nugent 
37156c04344SDavid Nugent     rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
37268bbf3adSDavid Nugent     login_close(lc);
3732d057ca6SDag-Erling Smørgrav     return (rc);
37468bbf3adSDavid Nugent }
37568bbf3adSDavid Nugent 
37668bbf3adSDavid Nugent 
37756c04344SDavid Nugent 
37856c04344SDavid Nugent /*
379f855462aSYaroslav Tykhiy  * Private function which takes care of processing
38056c04344SDavid Nugent  */
38156c04344SDavid Nugent 
38256c04344SDavid Nugent static mode_t
38356c04344SDavid Nugent setlogincontext(login_cap_t *lc, const struct passwd *pwd,
38456c04344SDavid Nugent 		mode_t mymask, unsigned long flags)
38556c04344SDavid Nugent {
38656c04344SDavid Nugent     if (lc) {
38756c04344SDavid Nugent 	/* Set resources */
38856c04344SDavid Nugent 	if (flags & LOGIN_SETRESOURCES)
38956c04344SDavid Nugent 	    setclassresources(lc);
39056c04344SDavid Nugent 	/* See if there's a umask override */
39156c04344SDavid Nugent 	if (flags & LOGIN_SETUMASK)
39256c04344SDavid Nugent 	    mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
39356c04344SDavid Nugent 	/* Set paths */
39456c04344SDavid Nugent 	if (flags & LOGIN_SETPATH)
39556c04344SDavid Nugent 	    setclassenvironment(lc, pwd, 1);
39656c04344SDavid Nugent 	/* Set environment */
39756c04344SDavid Nugent 	if (flags & LOGIN_SETENV)
39856c04344SDavid Nugent 	    setclassenvironment(lc, pwd, 0);
399d84c4292SBrooks Davis 	/* Set cpu affinity */
400d84c4292SBrooks Davis 	if (flags & LOGIN_SETCPUMASK)
401d84c4292SBrooks Davis 	    setclasscpumask(lc);
40256c04344SDavid Nugent     }
4032d057ca6SDag-Erling Smørgrav     return (mymask);
40456c04344SDavid Nugent }
40556c04344SDavid Nugent 
40656c04344SDavid Nugent 
40756c04344SDavid Nugent 
40868bbf3adSDavid Nugent /*
40968bbf3adSDavid Nugent  * setusercontext()
41068bbf3adSDavid Nugent  *
41168bbf3adSDavid Nugent  * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
41268bbf3adSDavid Nugent  * set the context as in setclasscontext().  <flags> controls which
41368bbf3adSDavid Nugent  * values are set.
41468bbf3adSDavid Nugent  *
41568bbf3adSDavid Nugent  * The difference between setclasscontext() and setusercontext() is
41668bbf3adSDavid Nugent  * that the former sets things up for an already-existing process,
41768bbf3adSDavid Nugent  * while the latter sets things up from a root context.  Such as might
41868bbf3adSDavid Nugent  * be called from login(1).
41968bbf3adSDavid Nugent  *
42068bbf3adSDavid Nugent  */
42168bbf3adSDavid Nugent 
42268bbf3adSDavid Nugent int
42368bbf3adSDavid Nugent setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
42468bbf3adSDavid Nugent {
42556c04344SDavid Nugent     quad_t	p;
42656c04344SDavid Nugent     mode_t	mymask;
42768bbf3adSDavid Nugent     login_cap_t *llc = NULL;
428e172f0e5SSteve Price     struct rtprio rtp;
42984333872SRobert Watson     int error;
43068bbf3adSDavid Nugent 
43156c04344SDavid Nugent     if (lc == NULL) {
43256c04344SDavid Nugent 	if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
43368bbf3adSDavid Nugent 	    llc = lc; /* free this when we're done */
43468bbf3adSDavid Nugent     }
43568bbf3adSDavid Nugent 
43668bbf3adSDavid Nugent     if (flags & LOGIN_SETPATH)
43768bbf3adSDavid Nugent 	pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
43868bbf3adSDavid Nugent 
43956c04344SDavid Nugent     /* we need a passwd entry to set these */
44056c04344SDavid Nugent     if (pwd == NULL)
441433c28e0SRobert Watson 	flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
44256c04344SDavid Nugent 
44356c04344SDavid Nugent     /* Set the process priority */
44468bbf3adSDavid Nugent     if (flags & LOGIN_SETPRIORITY) {
44556c04344SDavid Nugent 	p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
44656c04344SDavid Nugent 
447e172f0e5SSteve Price 	if (p > PRIO_MAX) {
448e172f0e5SSteve Price 	    rtp.type = RTP_PRIO_IDLE;
449e172f0e5SSteve Price 	    rtp.prio = p - PRIO_MAX - 1;
450e172f0e5SSteve Price 	    p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p;
451e172f0e5SSteve Price 	    if (rtprio(RTP_SET, 0, &rtp))
452e172f0e5SSteve Price 		syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
453e172f0e5SSteve Price 		    pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
454e172f0e5SSteve Price 	} else if (p < PRIO_MIN) {
455e172f0e5SSteve Price 	    rtp.type = RTP_PRIO_REALTIME;
456e172f0e5SSteve Price 	    rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX);
457e172f0e5SSteve Price 	    p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p;
458e172f0e5SSteve Price 	    if (rtprio(RTP_SET, 0, &rtp))
459e172f0e5SSteve Price 		syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
460e172f0e5SSteve Price 		    pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
461e172f0e5SSteve Price 	} else {
46256c04344SDavid Nugent 	    if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
46356c04344SDavid Nugent 		syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
46456c04344SDavid Nugent 		    pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
46568bbf3adSDavid Nugent 	}
466e172f0e5SSteve Price     }
46768bbf3adSDavid Nugent 
46856c04344SDavid Nugent     /* Setup the user's group permissions */
46956c04344SDavid Nugent     if (flags & LOGIN_SETGROUP) {
47056c04344SDavid Nugent 	if (setgid(pwd->pw_gid) != 0) {
4719f3a9c3aSAndrey A. Chernov 	    syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
47256c04344SDavid Nugent 	    login_close(llc);
4732d057ca6SDag-Erling Smørgrav 	    return (-1);
47456c04344SDavid Nugent 	}
47556c04344SDavid Nugent 	if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
4769f3a9c3aSAndrey A. Chernov 	    syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
4779f3a9c3aSAndrey A. Chernov 		   (u_long)pwd->pw_gid);
47856c04344SDavid Nugent 	    login_close(llc);
4792d057ca6SDag-Erling Smørgrav 	    return (-1);
48056c04344SDavid Nugent 	}
48156c04344SDavid Nugent     }
48268bbf3adSDavid Nugent 
48384333872SRobert Watson     /* Set up the user's MAC label. */
48484333872SRobert Watson     if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
48584333872SRobert Watson 	const char *label_string;
48684333872SRobert Watson 	mac_t label;
48784333872SRobert Watson 
48884333872SRobert Watson 	label_string = login_getcapstr(lc, "label", NULL, NULL);
48984333872SRobert Watson 	if (label_string != NULL) {
49084333872SRobert Watson 	    if (mac_from_text(&label, label_string) == -1) {
49184333872SRobert Watson 		syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
49284333872SRobert Watson 		    pwd->pw_name, label_string);
4932d057ca6SDag-Erling Smørgrav 		return (-1);
49484333872SRobert Watson 	    }
49584333872SRobert Watson 	    if (mac_set_proc(label) == -1)
49684333872SRobert Watson 		error = errno;
49784333872SRobert Watson 	    else
49884333872SRobert Watson 		error = 0;
49984333872SRobert Watson 	    mac_free(label);
50084333872SRobert Watson 	    if (error != 0) {
50184333872SRobert Watson 		syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
50284333872SRobert Watson 		    label_string, pwd->pw_name, strerror(error));
5032d057ca6SDag-Erling Smørgrav 		return (-1);
50484333872SRobert Watson 	    }
50584333872SRobert Watson 	}
50684333872SRobert Watson     }
50784333872SRobert Watson 
50856c04344SDavid Nugent     /* Set the sessions login */
50968bbf3adSDavid Nugent     if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
51056c04344SDavid Nugent 	syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
51168bbf3adSDavid Nugent 	login_close(llc);
5122d057ca6SDag-Erling Smørgrav 	return (-1);
51368bbf3adSDavid Nugent     }
51468bbf3adSDavid Nugent 
51556c04344SDavid Nugent     mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
51656c04344SDavid Nugent     mymask = setlogincontext(lc, pwd, mymask, flags);
5171c594de5SDavid Nugent     login_close(llc);
51856c04344SDavid Nugent 
51956c04344SDavid Nugent     /* This needs to be done after anything that needs root privs */
52056c04344SDavid Nugent     if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
5219f3a9c3aSAndrey A. Chernov 	syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
5222d057ca6SDag-Erling Smørgrav 	return (-1);	/* Paranoia again */
5231c594de5SDavid Nugent     }
5241c594de5SDavid Nugent 
52556c04344SDavid Nugent     /*
52656c04344SDavid Nugent      * Now, we repeat some of the above for the user's private entries
52756c04344SDavid Nugent      */
52856c04344SDavid Nugent     if ((lc = login_getuserclass(pwd)) != NULL) {
52956c04344SDavid Nugent 	mymask = setlogincontext(lc, pwd, mymask, flags);
53056c04344SDavid Nugent 	login_close(lc);
53156c04344SDavid Nugent     }
53256c04344SDavid Nugent 
53356c04344SDavid Nugent     /* Finally, set any umask we've found */
53456c04344SDavid Nugent     if (flags & LOGIN_SETUMASK)
53556c04344SDavid Nugent 	umask(mymask);
53656c04344SDavid Nugent 
5372d057ca6SDag-Erling Smørgrav     return (0);
53868bbf3adSDavid Nugent }
539