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> 432bfc50bcSEdward 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 }, 69*85a0ddfdSKonstantin Belousov { "kqueues", login_getcapsize, RLIMIT_KQUEUES }, 7068bbf3adSDavid Nugent { NULL, 0, 0 } 7168bbf3adSDavid Nugent }; 7268bbf3adSDavid Nugent 7368bbf3adSDavid Nugent 7468bbf3adSDavid Nugent void 7568bbf3adSDavid Nugent setclassresources(login_cap_t *lc) 7668bbf3adSDavid Nugent { 7756c04344SDavid Nugent struct login_res *lr; 7868bbf3adSDavid Nugent 791c594de5SDavid Nugent if (lc == NULL) 801c594de5SDavid Nugent return; 811c594de5SDavid Nugent 8256c04344SDavid Nugent for (lr = resources; lr->what != NULL; ++lr) { 8356c04344SDavid Nugent struct rlimit rlim; 8468bbf3adSDavid Nugent 8568bbf3adSDavid Nugent /* 8668bbf3adSDavid Nugent * The login.conf file can have <limit>, <limit>-max, and 8768bbf3adSDavid Nugent * <limit>-cur entries. 8868bbf3adSDavid Nugent * What we do is get the current current- and maximum- limits. 8968bbf3adSDavid Nugent * Then, we try to get an entry for <limit> from the capability, 9068bbf3adSDavid Nugent * using the current and max limits we just got as the 9168bbf3adSDavid Nugent * default/error values. 9268bbf3adSDavid Nugent * *Then*, we try looking for <limit>-cur and <limit>-max, 9368bbf3adSDavid Nugent * again using the appropriate values as the default/error 9468bbf3adSDavid Nugent * conditions. 9568bbf3adSDavid Nugent */ 9668bbf3adSDavid Nugent 9756c04344SDavid Nugent if (getrlimit(lr->why, &rlim) != 0) 9856c04344SDavid Nugent syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 9956c04344SDavid Nugent else { 10056c04344SDavid Nugent char name_cur[40]; 10156c04344SDavid Nugent char name_max[40]; 10256c04344SDavid Nugent rlim_t rcur = rlim.rlim_cur; 10356c04344SDavid Nugent rlim_t rmax = rlim.rlim_max; 10456c04344SDavid Nugent 10556c04344SDavid Nugent sprintf(name_cur, "%s-cur", lr->what); 10656c04344SDavid Nugent sprintf(name_max, "%s-max", lr->what); 10768bbf3adSDavid Nugent 10868bbf3adSDavid Nugent rcur = (*lr->who)(lc, lr->what, rcur, rcur); 10968bbf3adSDavid Nugent rmax = (*lr->who)(lc, lr->what, rmax, rmax); 11056c04344SDavid Nugent rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 11156c04344SDavid Nugent rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 11268bbf3adSDavid Nugent 11356c04344SDavid Nugent if (setrlimit(lr->why, &rlim) == -1) 11468bbf3adSDavid Nugent syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 11556c04344SDavid Nugent } 11656c04344SDavid Nugent } 11756c04344SDavid Nugent } 11868bbf3adSDavid Nugent 11956c04344SDavid Nugent 12068bbf3adSDavid Nugent 12168bbf3adSDavid Nugent static struct login_vars { 12268bbf3adSDavid Nugent const char *tag; 12368bbf3adSDavid Nugent const char *var; 12468bbf3adSDavid Nugent const char *def; 125cc1b8dcbSAndrey A. Chernov int overwrite; 12668bbf3adSDavid Nugent } pathvars[] = { 127cc1b8dcbSAndrey A. Chernov { "path", "PATH", NULL, 1}, 128cc1b8dcbSAndrey A. Chernov { "cdpath", "CDPATH", NULL, 1}, 129cc1b8dcbSAndrey A. Chernov { "manpath", "MANPATH", NULL, 1}, 130cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13168bbf3adSDavid Nugent }, envars[] = { 132cc1b8dcbSAndrey A. Chernov { "lang", "LANG", NULL, 1}, 133cc1b8dcbSAndrey A. Chernov { "charset", "MM_CHARSET", NULL, 1}, 134cc1b8dcbSAndrey A. Chernov { "timezone", "TZ", NULL, 1}, 135cc1b8dcbSAndrey A. Chernov { "term", "TERM", NULL, 0}, 136cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13768bbf3adSDavid Nugent }; 13868bbf3adSDavid Nugent 13968bbf3adSDavid Nugent static char * 140b00ba4ccSRuslan Ermilov substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 14168bbf3adSDavid Nugent { 14268bbf3adSDavid Nugent char *np = NULL; 14368bbf3adSDavid Nugent 14468bbf3adSDavid Nugent if (var != NULL) { 14568bbf3adSDavid Nugent int tildes = 0; 14668bbf3adSDavid Nugent int dollas = 0; 14768bbf3adSDavid Nugent char *p; 148b8a5cd86SDag-Erling Smørgrav const char *q; 14968bbf3adSDavid Nugent 15068bbf3adSDavid Nugent if (pwd != NULL) { 151b8a5cd86SDag-Erling Smørgrav for (q = var; *q != '\0'; ++q) { 152b8a5cd86SDag-Erling Smørgrav tildes += (*q == '~'); 153b8a5cd86SDag-Erling Smørgrav dollas += (*q == '$'); 154b8a5cd86SDag-Erling Smørgrav } 15568bbf3adSDavid Nugent } 15668bbf3adSDavid Nugent 15756c04344SDavid Nugent np = malloc(strlen(var) + (dollas * nlen) 15856c04344SDavid Nugent - dollas + (tildes * (pch+hlen)) 15956c04344SDavid Nugent - tildes + 1); 16068bbf3adSDavid Nugent 16168bbf3adSDavid Nugent if (np != NULL) { 16268bbf3adSDavid Nugent p = strcpy(np, var); 16368bbf3adSDavid Nugent 16468bbf3adSDavid Nugent if (pwd != NULL) { 16568bbf3adSDavid Nugent /* 16668bbf3adSDavid Nugent * This loop does user username and homedir substitutions 16768bbf3adSDavid Nugent * for unescaped $ (username) and ~ (homedir) 16868bbf3adSDavid Nugent */ 16968bbf3adSDavid Nugent while (*(p += strcspn(p, "~$")) != '\0') { 17068bbf3adSDavid Nugent int l = strlen(p); 17168bbf3adSDavid Nugent 172121ba32dSAndrey A. Chernov if (p > np && *(p-1) == '\\') /* Escaped: */ 17368bbf3adSDavid Nugent memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 17468bbf3adSDavid Nugent else if (*p == '~') { 1751c594de5SDavid Nugent int v = pch && *(p+1) != '/'; /* Avoid double // */ 1761c594de5SDavid Nugent memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 17768bbf3adSDavid Nugent memmove(p, pwd->pw_dir, hlen); 1781c594de5SDavid Nugent if (v) 17968bbf3adSDavid Nugent p[hlen] = '/'; 1801c594de5SDavid Nugent p += hlen + v; 18168bbf3adSDavid Nugent } 18268bbf3adSDavid Nugent else /* if (*p == '$') */ { 1831c594de5SDavid Nugent memmove(p + nlen, p + 1, l); /* Subst username */ 18468bbf3adSDavid Nugent memmove(p, pwd->pw_name, nlen); 18568bbf3adSDavid Nugent p += nlen; 18668bbf3adSDavid Nugent } 18768bbf3adSDavid Nugent } 18868bbf3adSDavid Nugent } 18968bbf3adSDavid Nugent } 19068bbf3adSDavid Nugent } 19156c04344SDavid Nugent 1922d057ca6SDag-Erling Smørgrav return (np); 19368bbf3adSDavid Nugent } 19468bbf3adSDavid Nugent 19568bbf3adSDavid Nugent 19668bbf3adSDavid Nugent void 19768bbf3adSDavid Nugent setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 19868bbf3adSDavid Nugent { 19968bbf3adSDavid Nugent struct login_vars *vars = paths ? pathvars : envars; 20068bbf3adSDavid Nugent int hlen = pwd ? strlen(pwd->pw_dir) : 0; 20168bbf3adSDavid Nugent int nlen = pwd ? strlen(pwd->pw_name) : 0; 20268bbf3adSDavid Nugent char pch = 0; 20368bbf3adSDavid Nugent 20468bbf3adSDavid Nugent if (hlen && pwd->pw_dir[hlen-1] != '/') 20568bbf3adSDavid Nugent ++pch; 20668bbf3adSDavid Nugent 20768bbf3adSDavid Nugent while (vars->tag != NULL) { 208b00ba4ccSRuslan Ermilov const char * var = paths ? login_getpath(lc, vars->tag, NULL) 20968bbf3adSDavid Nugent : login_getcapstr(lc, vars->tag, NULL, NULL); 21068bbf3adSDavid Nugent 21168bbf3adSDavid Nugent char * np = substvar(var, pwd, hlen, pch, nlen); 21268bbf3adSDavid Nugent 21368bbf3adSDavid Nugent if (np != NULL) { 214cc1b8dcbSAndrey A. Chernov setenv(vars->var, np, vars->overwrite); 21568bbf3adSDavid Nugent free(np); 21668bbf3adSDavid Nugent } else if (vars->def != NULL) { 21768bbf3adSDavid Nugent setenv(vars->var, vars->def, 0); 21868bbf3adSDavid Nugent } 21968bbf3adSDavid Nugent ++vars; 22068bbf3adSDavid Nugent } 22168bbf3adSDavid Nugent 22268bbf3adSDavid Nugent /* 22368bbf3adSDavid Nugent * If we're not processing paths, then see if there is a setenv list by 22468bbf3adSDavid Nugent * which the admin and/or user may set an arbitrary set of env vars. 22568bbf3adSDavid Nugent */ 22668bbf3adSDavid Nugent if (!paths) { 227547fa0d9SMark Murray const char **set_env = login_getcaplist(lc, "setenv", ","); 2281c594de5SDavid Nugent 22968bbf3adSDavid Nugent if (set_env != NULL) { 23068bbf3adSDavid Nugent while (*set_env != NULL) { 23168bbf3adSDavid Nugent char *p = strchr(*set_env, '='); 23256c04344SDavid Nugent 2331c594de5SDavid Nugent if (p != NULL) { /* Discard invalid entries */ 2341c594de5SDavid Nugent char *np; 2351c594de5SDavid Nugent 2361c594de5SDavid Nugent *p++ = '\0'; 2371c594de5SDavid Nugent if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 2381c594de5SDavid Nugent setenv(*set_env, np, 1); 23968bbf3adSDavid Nugent free(np); 24068bbf3adSDavid Nugent } 24168bbf3adSDavid Nugent } 24268bbf3adSDavid Nugent ++set_env; 24368bbf3adSDavid Nugent } 24468bbf3adSDavid Nugent } 24568bbf3adSDavid Nugent } 24668bbf3adSDavid Nugent } 24768bbf3adSDavid Nugent 24868bbf3adSDavid Nugent 249d84c4292SBrooks Davis static int 250d84c4292SBrooks Davis list2cpuset(const char *list, cpuset_t *mask) 251d84c4292SBrooks Davis { 252d84c4292SBrooks Davis enum { NONE, NUM, DASH } state; 253d84c4292SBrooks Davis int lastnum; 254d84c4292SBrooks Davis int curnum; 255d84c4292SBrooks Davis const char *l; 256d84c4292SBrooks Davis 257d84c4292SBrooks Davis state = NONE; 258d84c4292SBrooks Davis curnum = lastnum = 0; 259d84c4292SBrooks Davis for (l = list; *l != '\0';) { 260d84c4292SBrooks Davis if (isdigit(*l)) { 261d84c4292SBrooks Davis curnum = atoi(l); 262d84c4292SBrooks Davis if (curnum > CPU_SETSIZE) 263d84c4292SBrooks Davis errx(EXIT_FAILURE, 264d84c4292SBrooks Davis "Only %d cpus supported", CPU_SETSIZE); 265d84c4292SBrooks Davis while (isdigit(*l)) 266d84c4292SBrooks Davis l++; 267d84c4292SBrooks Davis switch (state) { 268d84c4292SBrooks Davis case NONE: 269d84c4292SBrooks Davis lastnum = curnum; 270d84c4292SBrooks Davis state = NUM; 271d84c4292SBrooks Davis break; 272d84c4292SBrooks Davis case DASH: 273d84c4292SBrooks Davis for (; lastnum <= curnum; lastnum++) 274d84c4292SBrooks Davis CPU_SET(lastnum, mask); 275d84c4292SBrooks Davis state = NONE; 276d84c4292SBrooks Davis break; 277d84c4292SBrooks Davis case NUM: 278d84c4292SBrooks Davis default: 279d84c4292SBrooks Davis return (0); 280d84c4292SBrooks Davis } 281d84c4292SBrooks Davis continue; 282d84c4292SBrooks Davis } 283d84c4292SBrooks Davis switch (*l) { 284d84c4292SBrooks Davis case ',': 285d84c4292SBrooks Davis switch (state) { 286d84c4292SBrooks Davis case NONE: 287d84c4292SBrooks Davis break; 288d84c4292SBrooks Davis case NUM: 289d84c4292SBrooks Davis CPU_SET(curnum, mask); 290d84c4292SBrooks Davis state = NONE; 291d84c4292SBrooks Davis break; 292d84c4292SBrooks Davis case DASH: 293d84c4292SBrooks Davis return (0); 294d84c4292SBrooks Davis break; 295d84c4292SBrooks Davis } 296d84c4292SBrooks Davis break; 297d84c4292SBrooks Davis case '-': 298d84c4292SBrooks Davis if (state != NUM) 299d84c4292SBrooks Davis return (0); 300d84c4292SBrooks Davis state = DASH; 301d84c4292SBrooks Davis break; 302d84c4292SBrooks Davis default: 303d84c4292SBrooks Davis return (0); 304d84c4292SBrooks Davis } 305d84c4292SBrooks Davis l++; 306d84c4292SBrooks Davis } 307d84c4292SBrooks Davis switch (state) { 308d84c4292SBrooks Davis case NONE: 309d84c4292SBrooks Davis break; 310d84c4292SBrooks Davis case NUM: 311d84c4292SBrooks Davis CPU_SET(curnum, mask); 312d84c4292SBrooks Davis break; 313d84c4292SBrooks Davis case DASH: 314d84c4292SBrooks Davis return (0); 315d84c4292SBrooks Davis } 3162d057ca6SDag-Erling Smørgrav return (1); 317d84c4292SBrooks Davis } 318d84c4292SBrooks Davis 319d84c4292SBrooks Davis 320d84c4292SBrooks Davis void 321d84c4292SBrooks Davis setclasscpumask(login_cap_t *lc) 322d84c4292SBrooks Davis { 323d84c4292SBrooks Davis const char *maskstr; 324d84c4292SBrooks Davis cpuset_t maskset; 325d84c4292SBrooks Davis cpusetid_t setid; 326d84c4292SBrooks Davis 327d84c4292SBrooks Davis maskstr = login_getcapstr(lc, "cpumask", NULL, NULL); 328d84c4292SBrooks Davis CPU_ZERO(&maskset); 329d84c4292SBrooks Davis if (maskstr == NULL) 330d84c4292SBrooks Davis return; 331d84c4292SBrooks Davis if (strcasecmp("default", maskstr) == 0) 332d84c4292SBrooks Davis return; 333d84c4292SBrooks Davis if (!list2cpuset(maskstr, &maskset)) { 334d84c4292SBrooks Davis syslog(LOG_WARNING, 335d84c4292SBrooks Davis "list2cpuset(%s) invalid mask specification", maskstr); 336d84c4292SBrooks Davis return; 337d84c4292SBrooks Davis } 338d84c4292SBrooks Davis 339d84c4292SBrooks Davis if (cpuset(&setid) != 0) { 340d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset(): %s", strerror(errno)); 341d84c4292SBrooks Davis return; 342d84c4292SBrooks Davis } 343d84c4292SBrooks Davis 344d84c4292SBrooks Davis if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, 345d84c4292SBrooks Davis sizeof(maskset), &maskset) != 0) 346d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr, 347d84c4292SBrooks Davis strerror(errno)); 348d84c4292SBrooks Davis } 349d84c4292SBrooks Davis 350d84c4292SBrooks Davis 35168bbf3adSDavid Nugent /* 35268bbf3adSDavid Nugent * setclasscontext() 35368bbf3adSDavid Nugent * 35468bbf3adSDavid Nugent * For the login class <class>, set various class context values 35568bbf3adSDavid Nugent * (limits, mainly) to the values for that class. Which values are 35668bbf3adSDavid Nugent * set are controlled by <flags> -- see <login_class.h> for the 35768bbf3adSDavid Nugent * possible values. 35868bbf3adSDavid Nugent * 35968bbf3adSDavid Nugent * setclasscontext() can only set resources, priority, and umask. 36068bbf3adSDavid Nugent */ 36168bbf3adSDavid Nugent 36268bbf3adSDavid Nugent int 36368bbf3adSDavid Nugent setclasscontext(const char *classname, unsigned int flags) 36468bbf3adSDavid Nugent { 36568bbf3adSDavid Nugent int rc; 36656c04344SDavid Nugent login_cap_t *lc; 36756c04344SDavid Nugent 36856c04344SDavid Nugent lc = login_getclassbyname(classname, NULL); 36956c04344SDavid Nugent 37056c04344SDavid Nugent flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 37156c04344SDavid Nugent LOGIN_SETUMASK | LOGIN_SETPATH; 37256c04344SDavid Nugent 37356c04344SDavid Nugent rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 37468bbf3adSDavid Nugent login_close(lc); 3752d057ca6SDag-Erling Smørgrav return (rc); 37668bbf3adSDavid Nugent } 37768bbf3adSDavid Nugent 37868bbf3adSDavid Nugent 37956c04344SDavid Nugent 38056c04344SDavid Nugent /* 381f855462aSYaroslav Tykhiy * Private function which takes care of processing 38256c04344SDavid Nugent */ 38356c04344SDavid Nugent 38456c04344SDavid Nugent static mode_t 38556c04344SDavid Nugent setlogincontext(login_cap_t *lc, const struct passwd *pwd, 38656c04344SDavid Nugent mode_t mymask, unsigned long flags) 38756c04344SDavid Nugent { 38856c04344SDavid Nugent if (lc) { 38956c04344SDavid Nugent /* Set resources */ 39056c04344SDavid Nugent if (flags & LOGIN_SETRESOURCES) 39156c04344SDavid Nugent setclassresources(lc); 39256c04344SDavid Nugent /* See if there's a umask override */ 39356c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 39456c04344SDavid Nugent mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); 39556c04344SDavid Nugent /* Set paths */ 39656c04344SDavid Nugent if (flags & LOGIN_SETPATH) 39756c04344SDavid Nugent setclassenvironment(lc, pwd, 1); 39856c04344SDavid Nugent /* Set environment */ 39956c04344SDavid Nugent if (flags & LOGIN_SETENV) 40056c04344SDavid Nugent setclassenvironment(lc, pwd, 0); 401d84c4292SBrooks Davis /* Set cpu affinity */ 402d84c4292SBrooks Davis if (flags & LOGIN_SETCPUMASK) 403d84c4292SBrooks Davis setclasscpumask(lc); 40456c04344SDavid Nugent } 4052d057ca6SDag-Erling Smørgrav return (mymask); 40656c04344SDavid Nugent } 40756c04344SDavid Nugent 40856c04344SDavid Nugent 40956c04344SDavid Nugent 41068bbf3adSDavid Nugent /* 41168bbf3adSDavid Nugent * setusercontext() 41268bbf3adSDavid Nugent * 41368bbf3adSDavid Nugent * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 41468bbf3adSDavid Nugent * set the context as in setclasscontext(). <flags> controls which 41568bbf3adSDavid Nugent * values are set. 41668bbf3adSDavid Nugent * 41768bbf3adSDavid Nugent * The difference between setclasscontext() and setusercontext() is 41868bbf3adSDavid Nugent * that the former sets things up for an already-existing process, 41968bbf3adSDavid Nugent * while the latter sets things up from a root context. Such as might 42068bbf3adSDavid Nugent * be called from login(1). 42168bbf3adSDavid Nugent * 42268bbf3adSDavid Nugent */ 42368bbf3adSDavid Nugent 42468bbf3adSDavid Nugent int 42568bbf3adSDavid Nugent setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 42668bbf3adSDavid Nugent { 42756c04344SDavid Nugent quad_t p; 42856c04344SDavid Nugent mode_t mymask; 42968bbf3adSDavid Nugent login_cap_t *llc = NULL; 4302bfc50bcSEdward Tomasz Napierala struct sigaction sa, prevsa; 431e172f0e5SSteve Price struct rtprio rtp; 43284333872SRobert Watson int error; 43368bbf3adSDavid Nugent 43456c04344SDavid Nugent if (lc == NULL) { 43556c04344SDavid Nugent if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 43668bbf3adSDavid Nugent llc = lc; /* free this when we're done */ 43768bbf3adSDavid Nugent } 43868bbf3adSDavid Nugent 43968bbf3adSDavid Nugent if (flags & LOGIN_SETPATH) 44068bbf3adSDavid Nugent pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 44168bbf3adSDavid Nugent 44256c04344SDavid Nugent /* we need a passwd entry to set these */ 44356c04344SDavid Nugent if (pwd == NULL) 444433c28e0SRobert Watson flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 44556c04344SDavid Nugent 44656c04344SDavid Nugent /* Set the process priority */ 44768bbf3adSDavid Nugent if (flags & LOGIN_SETPRIORITY) { 44856c04344SDavid Nugent p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 44956c04344SDavid Nugent 450e172f0e5SSteve Price if (p > PRIO_MAX) { 451e172f0e5SSteve Price rtp.type = RTP_PRIO_IDLE; 452e172f0e5SSteve Price rtp.prio = p - PRIO_MAX - 1; 453e172f0e5SSteve Price p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p; 454e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 455e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 4560bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4570bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 458e172f0e5SSteve Price } else if (p < PRIO_MIN) { 459e172f0e5SSteve Price rtp.type = RTP_PRIO_REALTIME; 460e172f0e5SSteve Price rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX); 461e172f0e5SSteve Price p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p; 462e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 463e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 4640bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4650bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 466e172f0e5SSteve Price } else { 46756c04344SDavid Nugent if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 46856c04344SDavid Nugent syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 4690bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4700bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 47168bbf3adSDavid Nugent } 472e172f0e5SSteve Price } 47368bbf3adSDavid Nugent 47456c04344SDavid Nugent /* Setup the user's group permissions */ 47556c04344SDavid Nugent if (flags & LOGIN_SETGROUP) { 47656c04344SDavid Nugent if (setgid(pwd->pw_gid) != 0) { 4779f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 47856c04344SDavid Nugent login_close(llc); 4792d057ca6SDag-Erling Smørgrav return (-1); 48056c04344SDavid Nugent } 48156c04344SDavid Nugent if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 4829f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 4839f3a9c3aSAndrey A. Chernov (u_long)pwd->pw_gid); 48456c04344SDavid Nugent login_close(llc); 4852d057ca6SDag-Erling Smørgrav return (-1); 48656c04344SDavid Nugent } 48756c04344SDavid Nugent } 48868bbf3adSDavid Nugent 48984333872SRobert Watson /* Set up the user's MAC label. */ 49084333872SRobert Watson if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 49184333872SRobert Watson const char *label_string; 49284333872SRobert Watson mac_t label; 49384333872SRobert Watson 49484333872SRobert Watson label_string = login_getcapstr(lc, "label", NULL, NULL); 49584333872SRobert Watson if (label_string != NULL) { 49684333872SRobert Watson if (mac_from_text(&label, label_string) == -1) { 49784333872SRobert Watson syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 49884333872SRobert Watson pwd->pw_name, label_string); 4992d057ca6SDag-Erling Smørgrav return (-1); 50084333872SRobert Watson } 50184333872SRobert Watson if (mac_set_proc(label) == -1) 50284333872SRobert Watson error = errno; 50384333872SRobert Watson else 50484333872SRobert Watson error = 0; 50584333872SRobert Watson mac_free(label); 50684333872SRobert Watson if (error != 0) { 50784333872SRobert Watson syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 50884333872SRobert Watson label_string, pwd->pw_name, strerror(error)); 5092d057ca6SDag-Erling Smørgrav return (-1); 51084333872SRobert Watson } 51184333872SRobert Watson } 51284333872SRobert Watson } 51384333872SRobert Watson 51456c04344SDavid Nugent /* Set the sessions login */ 51568bbf3adSDavid Nugent if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 51656c04344SDavid Nugent syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 51768bbf3adSDavid Nugent login_close(llc); 5182d057ca6SDag-Erling Smørgrav return (-1); 51968bbf3adSDavid Nugent } 52068bbf3adSDavid Nugent 5212bfc50bcSEdward Tomasz Napierala /* Inform the kernel about current login class */ 5222bfc50bcSEdward Tomasz Napierala if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) { 5232bfc50bcSEdward Tomasz Napierala /* 5242bfc50bcSEdward Tomasz Napierala * XXX: This is a workaround to fail gracefully in case the kernel 5252bfc50bcSEdward Tomasz Napierala * does not support setloginclass(2). 5262bfc50bcSEdward Tomasz Napierala */ 5272bfc50bcSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 5282bfc50bcSEdward Tomasz Napierala sa.sa_handler = SIG_IGN; 5292bfc50bcSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 5302bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &sa, &prevsa); 5312bfc50bcSEdward Tomasz Napierala error = setloginclass(lc->lc_class); 5322bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &prevsa, NULL); 5332bfc50bcSEdward Tomasz Napierala if (error != 0) { 5342bfc50bcSEdward Tomasz Napierala syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class); 5352bfc50bcSEdward Tomasz Napierala #ifdef notyet 5362bfc50bcSEdward Tomasz Napierala login_close(llc); 5372bfc50bcSEdward Tomasz Napierala return (-1); 5382bfc50bcSEdward Tomasz Napierala #endif 5392bfc50bcSEdward Tomasz Napierala } 5402bfc50bcSEdward Tomasz Napierala } 5412bfc50bcSEdward Tomasz Napierala 54256c04344SDavid Nugent mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 54356c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 5441c594de5SDavid Nugent login_close(llc); 54556c04344SDavid Nugent 54656c04344SDavid Nugent /* This needs to be done after anything that needs root privs */ 54756c04344SDavid Nugent if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 5489f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 5492d057ca6SDag-Erling Smørgrav return (-1); /* Paranoia again */ 5501c594de5SDavid Nugent } 5511c594de5SDavid Nugent 55256c04344SDavid Nugent /* 55356c04344SDavid Nugent * Now, we repeat some of the above for the user's private entries 55456c04344SDavid Nugent */ 55535305a8dSDag-Erling Smørgrav if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { 55656c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 55756c04344SDavid Nugent login_close(lc); 55856c04344SDavid Nugent } 55956c04344SDavid Nugent 56056c04344SDavid Nugent /* Finally, set any umask we've found */ 56156c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 56256c04344SDavid Nugent umask(mymask); 56356c04344SDavid Nugent 5642d057ca6SDag-Erling Smørgrav return (0); 56568bbf3adSDavid Nugent } 566