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> 43*2bfc50bcSEdward Tomasz Napierala #include <signal.h> 440ebec5d3SMark Murray #include <stdio.h> 450ebec5d3SMark Murray #include <stdlib.h> 460ebec5d3SMark Murray #include <string.h> 470ebec5d3SMark Murray #include <syslog.h> 480ebec5d3SMark Murray #include <unistd.h> 4968bbf3adSDavid Nugent 5068bbf3adSDavid Nugent 5168bbf3adSDavid Nugent static struct login_res { 5268bbf3adSDavid Nugent const char *what; 5368bbf3adSDavid Nugent rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 5468bbf3adSDavid Nugent int why; 5568bbf3adSDavid Nugent } resources[] = { 5668bbf3adSDavid Nugent { "cputime", login_getcaptime, RLIMIT_CPU }, 5768bbf3adSDavid Nugent { "filesize", login_getcapsize, RLIMIT_FSIZE }, 5868bbf3adSDavid Nugent { "datasize", login_getcapsize, RLIMIT_DATA }, 5968bbf3adSDavid Nugent { "stacksize", login_getcapsize, RLIMIT_STACK }, 6068bbf3adSDavid Nugent { "memoryuse", login_getcapsize, RLIMIT_RSS }, 6168bbf3adSDavid Nugent { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 6268bbf3adSDavid Nugent { "maxproc", login_getcapnum, RLIMIT_NPROC }, 6368bbf3adSDavid Nugent { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 6456c04344SDavid Nugent { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 650c697857SSheldon Hearn { "sbsize", login_getcapsize, RLIMIT_SBSIZE }, 6667577126SMatthew Dillon { "vmemoryuse", login_getcapsize, RLIMIT_VMEM }, 67bc093719SEd Schouten { "pseudoterminals", login_getcapnum, RLIMIT_NPTS }, 68c9253e93SKonstantin Belousov { "swapuse", login_getcapsize, RLIMIT_SWAP }, 6968bbf3adSDavid Nugent { NULL, 0, 0 } 7068bbf3adSDavid Nugent }; 7168bbf3adSDavid Nugent 7268bbf3adSDavid Nugent 7368bbf3adSDavid Nugent void 7468bbf3adSDavid Nugent setclassresources(login_cap_t *lc) 7568bbf3adSDavid Nugent { 7656c04344SDavid Nugent struct login_res *lr; 7768bbf3adSDavid Nugent 781c594de5SDavid Nugent if (lc == NULL) 791c594de5SDavid Nugent return; 801c594de5SDavid Nugent 8156c04344SDavid Nugent for (lr = resources; lr->what != NULL; ++lr) { 8256c04344SDavid Nugent struct rlimit rlim; 8368bbf3adSDavid Nugent 8468bbf3adSDavid Nugent /* 8568bbf3adSDavid Nugent * The login.conf file can have <limit>, <limit>-max, and 8668bbf3adSDavid Nugent * <limit>-cur entries. 8768bbf3adSDavid Nugent * What we do is get the current current- and maximum- limits. 8868bbf3adSDavid Nugent * Then, we try to get an entry for <limit> from the capability, 8968bbf3adSDavid Nugent * using the current and max limits we just got as the 9068bbf3adSDavid Nugent * default/error values. 9168bbf3adSDavid Nugent * *Then*, we try looking for <limit>-cur and <limit>-max, 9268bbf3adSDavid Nugent * again using the appropriate values as the default/error 9368bbf3adSDavid Nugent * conditions. 9468bbf3adSDavid Nugent */ 9568bbf3adSDavid Nugent 9656c04344SDavid Nugent if (getrlimit(lr->why, &rlim) != 0) 9756c04344SDavid Nugent syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 9856c04344SDavid Nugent else { 9956c04344SDavid Nugent char name_cur[40]; 10056c04344SDavid Nugent char name_max[40]; 10156c04344SDavid Nugent rlim_t rcur = rlim.rlim_cur; 10256c04344SDavid Nugent rlim_t rmax = rlim.rlim_max; 10356c04344SDavid Nugent 10456c04344SDavid Nugent sprintf(name_cur, "%s-cur", lr->what); 10556c04344SDavid Nugent sprintf(name_max, "%s-max", lr->what); 10668bbf3adSDavid Nugent 10768bbf3adSDavid Nugent rcur = (*lr->who)(lc, lr->what, rcur, rcur); 10868bbf3adSDavid Nugent rmax = (*lr->who)(lc, lr->what, rmax, rmax); 10956c04344SDavid Nugent rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 11056c04344SDavid Nugent rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 11168bbf3adSDavid Nugent 11256c04344SDavid Nugent if (setrlimit(lr->why, &rlim) == -1) 11368bbf3adSDavid Nugent syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 11456c04344SDavid Nugent } 11556c04344SDavid Nugent } 11656c04344SDavid Nugent } 11768bbf3adSDavid Nugent 11856c04344SDavid Nugent 11968bbf3adSDavid Nugent 12068bbf3adSDavid Nugent static struct login_vars { 12168bbf3adSDavid Nugent const char *tag; 12268bbf3adSDavid Nugent const char *var; 12368bbf3adSDavid Nugent const char *def; 124cc1b8dcbSAndrey A. Chernov int overwrite; 12568bbf3adSDavid Nugent } pathvars[] = { 126cc1b8dcbSAndrey A. Chernov { "path", "PATH", NULL, 1}, 127cc1b8dcbSAndrey A. Chernov { "cdpath", "CDPATH", NULL, 1}, 128cc1b8dcbSAndrey A. Chernov { "manpath", "MANPATH", NULL, 1}, 129cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13068bbf3adSDavid Nugent }, envars[] = { 131cc1b8dcbSAndrey A. Chernov { "lang", "LANG", NULL, 1}, 132cc1b8dcbSAndrey A. Chernov { "charset", "MM_CHARSET", NULL, 1}, 133cc1b8dcbSAndrey A. Chernov { "timezone", "TZ", NULL, 1}, 134cc1b8dcbSAndrey A. Chernov { "term", "TERM", NULL, 0}, 135cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13668bbf3adSDavid Nugent }; 13768bbf3adSDavid Nugent 13868bbf3adSDavid Nugent static char * 139b00ba4ccSRuslan Ermilov substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 14068bbf3adSDavid Nugent { 14168bbf3adSDavid Nugent char *np = NULL; 14268bbf3adSDavid Nugent 14368bbf3adSDavid Nugent if (var != NULL) { 14468bbf3adSDavid Nugent int tildes = 0; 14568bbf3adSDavid Nugent int dollas = 0; 14668bbf3adSDavid Nugent char *p; 147b8a5cd86SDag-Erling Smørgrav const char *q; 14868bbf3adSDavid Nugent 14968bbf3adSDavid Nugent if (pwd != NULL) { 150b8a5cd86SDag-Erling Smørgrav for (q = var; *q != '\0'; ++q) { 151b8a5cd86SDag-Erling Smørgrav tildes += (*q == '~'); 152b8a5cd86SDag-Erling Smørgrav dollas += (*q == '$'); 153b8a5cd86SDag-Erling Smørgrav } 15468bbf3adSDavid Nugent } 15568bbf3adSDavid Nugent 15656c04344SDavid Nugent np = malloc(strlen(var) + (dollas * nlen) 15756c04344SDavid Nugent - dollas + (tildes * (pch+hlen)) 15856c04344SDavid Nugent - tildes + 1); 15968bbf3adSDavid Nugent 16068bbf3adSDavid Nugent if (np != NULL) { 16168bbf3adSDavid Nugent p = strcpy(np, var); 16268bbf3adSDavid Nugent 16368bbf3adSDavid Nugent if (pwd != NULL) { 16468bbf3adSDavid Nugent /* 16568bbf3adSDavid Nugent * This loop does user username and homedir substitutions 16668bbf3adSDavid Nugent * for unescaped $ (username) and ~ (homedir) 16768bbf3adSDavid Nugent */ 16868bbf3adSDavid Nugent while (*(p += strcspn(p, "~$")) != '\0') { 16968bbf3adSDavid Nugent int l = strlen(p); 17068bbf3adSDavid Nugent 171121ba32dSAndrey A. Chernov if (p > np && *(p-1) == '\\') /* Escaped: */ 17268bbf3adSDavid Nugent memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 17368bbf3adSDavid Nugent else if (*p == '~') { 1741c594de5SDavid Nugent int v = pch && *(p+1) != '/'; /* Avoid double // */ 1751c594de5SDavid Nugent memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 17668bbf3adSDavid Nugent memmove(p, pwd->pw_dir, hlen); 1771c594de5SDavid Nugent if (v) 17868bbf3adSDavid Nugent p[hlen] = '/'; 1791c594de5SDavid Nugent p += hlen + v; 18068bbf3adSDavid Nugent } 18168bbf3adSDavid Nugent else /* if (*p == '$') */ { 1821c594de5SDavid Nugent memmove(p + nlen, p + 1, l); /* Subst username */ 18368bbf3adSDavid Nugent memmove(p, pwd->pw_name, nlen); 18468bbf3adSDavid Nugent p += nlen; 18568bbf3adSDavid Nugent } 18668bbf3adSDavid Nugent } 18768bbf3adSDavid Nugent } 18868bbf3adSDavid Nugent } 18968bbf3adSDavid Nugent } 19056c04344SDavid Nugent 1912d057ca6SDag-Erling Smørgrav return (np); 19268bbf3adSDavid Nugent } 19368bbf3adSDavid Nugent 19468bbf3adSDavid Nugent 19568bbf3adSDavid Nugent void 19668bbf3adSDavid Nugent setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 19768bbf3adSDavid Nugent { 19868bbf3adSDavid Nugent struct login_vars *vars = paths ? pathvars : envars; 19968bbf3adSDavid Nugent int hlen = pwd ? strlen(pwd->pw_dir) : 0; 20068bbf3adSDavid Nugent int nlen = pwd ? strlen(pwd->pw_name) : 0; 20168bbf3adSDavid Nugent char pch = 0; 20268bbf3adSDavid Nugent 20368bbf3adSDavid Nugent if (hlen && pwd->pw_dir[hlen-1] != '/') 20468bbf3adSDavid Nugent ++pch; 20568bbf3adSDavid Nugent 20668bbf3adSDavid Nugent while (vars->tag != NULL) { 207b00ba4ccSRuslan Ermilov const char * var = paths ? login_getpath(lc, vars->tag, NULL) 20868bbf3adSDavid Nugent : login_getcapstr(lc, vars->tag, NULL, NULL); 20968bbf3adSDavid Nugent 21068bbf3adSDavid Nugent char * np = substvar(var, pwd, hlen, pch, nlen); 21168bbf3adSDavid Nugent 21268bbf3adSDavid Nugent if (np != NULL) { 213cc1b8dcbSAndrey A. Chernov setenv(vars->var, np, vars->overwrite); 21468bbf3adSDavid Nugent free(np); 21568bbf3adSDavid Nugent } else if (vars->def != NULL) { 21668bbf3adSDavid Nugent setenv(vars->var, vars->def, 0); 21768bbf3adSDavid Nugent } 21868bbf3adSDavid Nugent ++vars; 21968bbf3adSDavid Nugent } 22068bbf3adSDavid Nugent 22168bbf3adSDavid Nugent /* 22268bbf3adSDavid Nugent * If we're not processing paths, then see if there is a setenv list by 22368bbf3adSDavid Nugent * which the admin and/or user may set an arbitrary set of env vars. 22468bbf3adSDavid Nugent */ 22568bbf3adSDavid Nugent if (!paths) { 226547fa0d9SMark Murray const char **set_env = login_getcaplist(lc, "setenv", ","); 2271c594de5SDavid Nugent 22868bbf3adSDavid Nugent if (set_env != NULL) { 22968bbf3adSDavid Nugent while (*set_env != NULL) { 23068bbf3adSDavid Nugent char *p = strchr(*set_env, '='); 23156c04344SDavid Nugent 2321c594de5SDavid Nugent if (p != NULL) { /* Discard invalid entries */ 2331c594de5SDavid Nugent char *np; 2341c594de5SDavid Nugent 2351c594de5SDavid Nugent *p++ = '\0'; 2361c594de5SDavid Nugent if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 2371c594de5SDavid Nugent setenv(*set_env, np, 1); 23868bbf3adSDavid Nugent free(np); 23968bbf3adSDavid Nugent } 24068bbf3adSDavid Nugent } 24168bbf3adSDavid Nugent ++set_env; 24268bbf3adSDavid Nugent } 24368bbf3adSDavid Nugent } 24468bbf3adSDavid Nugent } 24568bbf3adSDavid Nugent } 24668bbf3adSDavid Nugent 24768bbf3adSDavid Nugent 248d84c4292SBrooks Davis static int 249d84c4292SBrooks Davis list2cpuset(const char *list, cpuset_t *mask) 250d84c4292SBrooks Davis { 251d84c4292SBrooks Davis enum { NONE, NUM, DASH } state; 252d84c4292SBrooks Davis int lastnum; 253d84c4292SBrooks Davis int curnum; 254d84c4292SBrooks Davis const char *l; 255d84c4292SBrooks Davis 256d84c4292SBrooks Davis state = NONE; 257d84c4292SBrooks Davis curnum = lastnum = 0; 258d84c4292SBrooks Davis for (l = list; *l != '\0';) { 259d84c4292SBrooks Davis if (isdigit(*l)) { 260d84c4292SBrooks Davis curnum = atoi(l); 261d84c4292SBrooks Davis if (curnum > CPU_SETSIZE) 262d84c4292SBrooks Davis errx(EXIT_FAILURE, 263d84c4292SBrooks Davis "Only %d cpus supported", CPU_SETSIZE); 264d84c4292SBrooks Davis while (isdigit(*l)) 265d84c4292SBrooks Davis l++; 266d84c4292SBrooks Davis switch (state) { 267d84c4292SBrooks Davis case NONE: 268d84c4292SBrooks Davis lastnum = curnum; 269d84c4292SBrooks Davis state = NUM; 270d84c4292SBrooks Davis break; 271d84c4292SBrooks Davis case DASH: 272d84c4292SBrooks Davis for (; lastnum <= curnum; lastnum++) 273d84c4292SBrooks Davis CPU_SET(lastnum, mask); 274d84c4292SBrooks Davis state = NONE; 275d84c4292SBrooks Davis break; 276d84c4292SBrooks Davis case NUM: 277d84c4292SBrooks Davis default: 278d84c4292SBrooks Davis return (0); 279d84c4292SBrooks Davis } 280d84c4292SBrooks Davis continue; 281d84c4292SBrooks Davis } 282d84c4292SBrooks Davis switch (*l) { 283d84c4292SBrooks Davis case ',': 284d84c4292SBrooks Davis switch (state) { 285d84c4292SBrooks Davis case NONE: 286d84c4292SBrooks Davis break; 287d84c4292SBrooks Davis case NUM: 288d84c4292SBrooks Davis CPU_SET(curnum, mask); 289d84c4292SBrooks Davis state = NONE; 290d84c4292SBrooks Davis break; 291d84c4292SBrooks Davis case DASH: 292d84c4292SBrooks Davis return (0); 293d84c4292SBrooks Davis break; 294d84c4292SBrooks Davis } 295d84c4292SBrooks Davis break; 296d84c4292SBrooks Davis case '-': 297d84c4292SBrooks Davis if (state != NUM) 298d84c4292SBrooks Davis return (0); 299d84c4292SBrooks Davis state = DASH; 300d84c4292SBrooks Davis break; 301d84c4292SBrooks Davis default: 302d84c4292SBrooks Davis return (0); 303d84c4292SBrooks Davis } 304d84c4292SBrooks Davis l++; 305d84c4292SBrooks Davis } 306d84c4292SBrooks Davis switch (state) { 307d84c4292SBrooks Davis case NONE: 308d84c4292SBrooks Davis break; 309d84c4292SBrooks Davis case NUM: 310d84c4292SBrooks Davis CPU_SET(curnum, mask); 311d84c4292SBrooks Davis break; 312d84c4292SBrooks Davis case DASH: 313d84c4292SBrooks Davis return (0); 314d84c4292SBrooks Davis } 3152d057ca6SDag-Erling Smørgrav return (1); 316d84c4292SBrooks Davis } 317d84c4292SBrooks Davis 318d84c4292SBrooks Davis 319d84c4292SBrooks Davis void 320d84c4292SBrooks Davis setclasscpumask(login_cap_t *lc) 321d84c4292SBrooks Davis { 322d84c4292SBrooks Davis const char *maskstr; 323d84c4292SBrooks Davis cpuset_t maskset; 324d84c4292SBrooks Davis cpusetid_t setid; 325d84c4292SBrooks Davis 326d84c4292SBrooks Davis maskstr = login_getcapstr(lc, "cpumask", NULL, NULL); 327d84c4292SBrooks Davis CPU_ZERO(&maskset); 328d84c4292SBrooks Davis if (maskstr == NULL) 329d84c4292SBrooks Davis return; 330d84c4292SBrooks Davis if (strcasecmp("default", maskstr) == 0) 331d84c4292SBrooks Davis return; 332d84c4292SBrooks Davis if (!list2cpuset(maskstr, &maskset)) { 333d84c4292SBrooks Davis syslog(LOG_WARNING, 334d84c4292SBrooks Davis "list2cpuset(%s) invalid mask specification", maskstr); 335d84c4292SBrooks Davis return; 336d84c4292SBrooks Davis } 337d84c4292SBrooks Davis 338d84c4292SBrooks Davis if (cpuset(&setid) != 0) { 339d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset(): %s", strerror(errno)); 340d84c4292SBrooks Davis return; 341d84c4292SBrooks Davis } 342d84c4292SBrooks Davis 343d84c4292SBrooks Davis if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, 344d84c4292SBrooks Davis sizeof(maskset), &maskset) != 0) 345d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr, 346d84c4292SBrooks Davis strerror(errno)); 347d84c4292SBrooks Davis } 348d84c4292SBrooks Davis 349d84c4292SBrooks Davis 35068bbf3adSDavid Nugent /* 35168bbf3adSDavid Nugent * setclasscontext() 35268bbf3adSDavid Nugent * 35368bbf3adSDavid Nugent * For the login class <class>, set various class context values 35468bbf3adSDavid Nugent * (limits, mainly) to the values for that class. Which values are 35568bbf3adSDavid Nugent * set are controlled by <flags> -- see <login_class.h> for the 35668bbf3adSDavid Nugent * possible values. 35768bbf3adSDavid Nugent * 35868bbf3adSDavid Nugent * setclasscontext() can only set resources, priority, and umask. 35968bbf3adSDavid Nugent */ 36068bbf3adSDavid Nugent 36168bbf3adSDavid Nugent int 36268bbf3adSDavid Nugent setclasscontext(const char *classname, unsigned int flags) 36368bbf3adSDavid Nugent { 36468bbf3adSDavid Nugent int rc; 36556c04344SDavid Nugent login_cap_t *lc; 36656c04344SDavid Nugent 36756c04344SDavid Nugent lc = login_getclassbyname(classname, NULL); 36856c04344SDavid Nugent 36956c04344SDavid Nugent flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 37056c04344SDavid Nugent LOGIN_SETUMASK | LOGIN_SETPATH; 37156c04344SDavid Nugent 37256c04344SDavid Nugent rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 37368bbf3adSDavid Nugent login_close(lc); 3742d057ca6SDag-Erling Smørgrav return (rc); 37568bbf3adSDavid Nugent } 37668bbf3adSDavid Nugent 37768bbf3adSDavid Nugent 37856c04344SDavid Nugent 37956c04344SDavid Nugent /* 380f855462aSYaroslav Tykhiy * Private function which takes care of processing 38156c04344SDavid Nugent */ 38256c04344SDavid Nugent 38356c04344SDavid Nugent static mode_t 38456c04344SDavid Nugent setlogincontext(login_cap_t *lc, const struct passwd *pwd, 38556c04344SDavid Nugent mode_t mymask, unsigned long flags) 38656c04344SDavid Nugent { 38756c04344SDavid Nugent if (lc) { 38856c04344SDavid Nugent /* Set resources */ 38956c04344SDavid Nugent if (flags & LOGIN_SETRESOURCES) 39056c04344SDavid Nugent setclassresources(lc); 39156c04344SDavid Nugent /* See if there's a umask override */ 39256c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 39356c04344SDavid Nugent mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); 39456c04344SDavid Nugent /* Set paths */ 39556c04344SDavid Nugent if (flags & LOGIN_SETPATH) 39656c04344SDavid Nugent setclassenvironment(lc, pwd, 1); 39756c04344SDavid Nugent /* Set environment */ 39856c04344SDavid Nugent if (flags & LOGIN_SETENV) 39956c04344SDavid Nugent setclassenvironment(lc, pwd, 0); 400d84c4292SBrooks Davis /* Set cpu affinity */ 401d84c4292SBrooks Davis if (flags & LOGIN_SETCPUMASK) 402d84c4292SBrooks Davis setclasscpumask(lc); 40356c04344SDavid Nugent } 4042d057ca6SDag-Erling Smørgrav return (mymask); 40556c04344SDavid Nugent } 40656c04344SDavid Nugent 40756c04344SDavid Nugent 40856c04344SDavid Nugent 40968bbf3adSDavid Nugent /* 41068bbf3adSDavid Nugent * setusercontext() 41168bbf3adSDavid Nugent * 41268bbf3adSDavid Nugent * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 41368bbf3adSDavid Nugent * set the context as in setclasscontext(). <flags> controls which 41468bbf3adSDavid Nugent * values are set. 41568bbf3adSDavid Nugent * 41668bbf3adSDavid Nugent * The difference between setclasscontext() and setusercontext() is 41768bbf3adSDavid Nugent * that the former sets things up for an already-existing process, 41868bbf3adSDavid Nugent * while the latter sets things up from a root context. Such as might 41968bbf3adSDavid Nugent * be called from login(1). 42068bbf3adSDavid Nugent * 42168bbf3adSDavid Nugent */ 42268bbf3adSDavid Nugent 42368bbf3adSDavid Nugent int 42468bbf3adSDavid Nugent setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 42568bbf3adSDavid Nugent { 42656c04344SDavid Nugent quad_t p; 42756c04344SDavid Nugent mode_t mymask; 42868bbf3adSDavid Nugent login_cap_t *llc = NULL; 429*2bfc50bcSEdward Tomasz Napierala struct sigaction sa, prevsa; 430e172f0e5SSteve Price struct rtprio rtp; 43184333872SRobert Watson int error; 43268bbf3adSDavid Nugent 43356c04344SDavid Nugent if (lc == NULL) { 43456c04344SDavid Nugent if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 43568bbf3adSDavid Nugent llc = lc; /* free this when we're done */ 43668bbf3adSDavid Nugent } 43768bbf3adSDavid Nugent 43868bbf3adSDavid Nugent if (flags & LOGIN_SETPATH) 43968bbf3adSDavid Nugent pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 44068bbf3adSDavid Nugent 44156c04344SDavid Nugent /* we need a passwd entry to set these */ 44256c04344SDavid Nugent if (pwd == NULL) 443433c28e0SRobert Watson flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 44456c04344SDavid Nugent 44556c04344SDavid Nugent /* Set the process priority */ 44668bbf3adSDavid Nugent if (flags & LOGIN_SETPRIORITY) { 44756c04344SDavid Nugent p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 44856c04344SDavid Nugent 449e172f0e5SSteve Price if (p > PRIO_MAX) { 450e172f0e5SSteve Price rtp.type = RTP_PRIO_IDLE; 451e172f0e5SSteve Price rtp.prio = p - PRIO_MAX - 1; 452e172f0e5SSteve Price p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p; 453e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 454e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 455e172f0e5SSteve Price pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 456e172f0e5SSteve Price } else if (p < PRIO_MIN) { 457e172f0e5SSteve Price rtp.type = RTP_PRIO_REALTIME; 458e172f0e5SSteve Price rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX); 459e172f0e5SSteve Price p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p; 460e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 461e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 462e172f0e5SSteve Price pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 463e172f0e5SSteve Price } else { 46456c04344SDavid Nugent if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 46556c04344SDavid Nugent syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 46656c04344SDavid Nugent pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 46768bbf3adSDavid Nugent } 468e172f0e5SSteve Price } 46968bbf3adSDavid Nugent 47056c04344SDavid Nugent /* Setup the user's group permissions */ 47156c04344SDavid Nugent if (flags & LOGIN_SETGROUP) { 47256c04344SDavid Nugent if (setgid(pwd->pw_gid) != 0) { 4739f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 47456c04344SDavid Nugent login_close(llc); 4752d057ca6SDag-Erling Smørgrav return (-1); 47656c04344SDavid Nugent } 47756c04344SDavid Nugent if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 4789f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 4799f3a9c3aSAndrey A. Chernov (u_long)pwd->pw_gid); 48056c04344SDavid Nugent login_close(llc); 4812d057ca6SDag-Erling Smørgrav return (-1); 48256c04344SDavid Nugent } 48356c04344SDavid Nugent } 48468bbf3adSDavid Nugent 48584333872SRobert Watson /* Set up the user's MAC label. */ 48684333872SRobert Watson if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 48784333872SRobert Watson const char *label_string; 48884333872SRobert Watson mac_t label; 48984333872SRobert Watson 49084333872SRobert Watson label_string = login_getcapstr(lc, "label", NULL, NULL); 49184333872SRobert Watson if (label_string != NULL) { 49284333872SRobert Watson if (mac_from_text(&label, label_string) == -1) { 49384333872SRobert Watson syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 49484333872SRobert Watson pwd->pw_name, label_string); 4952d057ca6SDag-Erling Smørgrav return (-1); 49684333872SRobert Watson } 49784333872SRobert Watson if (mac_set_proc(label) == -1) 49884333872SRobert Watson error = errno; 49984333872SRobert Watson else 50084333872SRobert Watson error = 0; 50184333872SRobert Watson mac_free(label); 50284333872SRobert Watson if (error != 0) { 50384333872SRobert Watson syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 50484333872SRobert Watson label_string, pwd->pw_name, strerror(error)); 5052d057ca6SDag-Erling Smørgrav return (-1); 50684333872SRobert Watson } 50784333872SRobert Watson } 50884333872SRobert Watson } 50984333872SRobert Watson 51056c04344SDavid Nugent /* Set the sessions login */ 51168bbf3adSDavid Nugent if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 51256c04344SDavid Nugent syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 51368bbf3adSDavid Nugent login_close(llc); 5142d057ca6SDag-Erling Smørgrav return (-1); 51568bbf3adSDavid Nugent } 51668bbf3adSDavid Nugent 517*2bfc50bcSEdward Tomasz Napierala /* Inform the kernel about current login class */ 518*2bfc50bcSEdward Tomasz Napierala if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) { 519*2bfc50bcSEdward Tomasz Napierala /* 520*2bfc50bcSEdward Tomasz Napierala * XXX: This is a workaround to fail gracefully in case the kernel 521*2bfc50bcSEdward Tomasz Napierala * does not support setloginclass(2). 522*2bfc50bcSEdward Tomasz Napierala */ 523*2bfc50bcSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 524*2bfc50bcSEdward Tomasz Napierala sa.sa_handler = SIG_IGN; 525*2bfc50bcSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 526*2bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &sa, &prevsa); 527*2bfc50bcSEdward Tomasz Napierala error = setloginclass(lc->lc_class); 528*2bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &prevsa, NULL); 529*2bfc50bcSEdward Tomasz Napierala if (error != 0) { 530*2bfc50bcSEdward Tomasz Napierala syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class); 531*2bfc50bcSEdward Tomasz Napierala #ifdef notyet 532*2bfc50bcSEdward Tomasz Napierala login_close(llc); 533*2bfc50bcSEdward Tomasz Napierala return (-1); 534*2bfc50bcSEdward Tomasz Napierala #endif 535*2bfc50bcSEdward Tomasz Napierala } 536*2bfc50bcSEdward Tomasz Napierala } 537*2bfc50bcSEdward Tomasz Napierala 53856c04344SDavid Nugent mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 53956c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 5401c594de5SDavid Nugent login_close(llc); 54156c04344SDavid Nugent 54256c04344SDavid Nugent /* This needs to be done after anything that needs root privs */ 54356c04344SDavid Nugent if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 5449f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 5452d057ca6SDag-Erling Smørgrav return (-1); /* Paranoia again */ 5461c594de5SDavid Nugent } 5471c594de5SDavid Nugent 54856c04344SDavid Nugent /* 54956c04344SDavid Nugent * Now, we repeat some of the above for the user's private entries 55056c04344SDavid Nugent */ 55135305a8dSDag-Erling Smørgrav if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { 55256c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 55356c04344SDavid Nugent login_close(lc); 55456c04344SDavid Nugent } 55556c04344SDavid Nugent 55656c04344SDavid Nugent /* Finally, set any umask we've found */ 55756c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 55856c04344SDavid Nugent umask(mymask); 55956c04344SDavid Nugent 5602d057ca6SDag-Erling Smørgrav return (0); 56168bbf3adSDavid Nugent } 562