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 25d84c4292SBrooks Davis #include <sys/param.h> 26d84c4292SBrooks Davis #include <sys/cpuset.h> 270ebec5d3SMark Murray #include <sys/mac.h> 287cc027a3SDag-Erling Smørgrav #include <sys/resource.h> 290ebec5d3SMark Murray #include <sys/rtprio.h> 307cc027a3SDag-Erling Smørgrav #include <sys/stat.h> 317cc027a3SDag-Erling Smørgrav #include <sys/time.h> 327cc027a3SDag-Erling Smørgrav 337cc027a3SDag-Erling Smørgrav #include <ctype.h> 347cc027a3SDag-Erling Smørgrav #include <err.h> 350ebec5d3SMark Murray #include <errno.h> 3668bbf3adSDavid Nugent #include <fcntl.h> 3768bbf3adSDavid Nugent #include <login_cap.h> 3868bbf3adSDavid Nugent #include <paths.h> 390ebec5d3SMark Murray #include <pwd.h> 40e99c28e9SOlivier Certner #include <stdint.h> 410ebec5d3SMark Murray #include <stdio.h> 420ebec5d3SMark Murray #include <stdlib.h> 430ebec5d3SMark Murray #include <string.h> 440ebec5d3SMark Murray #include <syslog.h> 450ebec5d3SMark Murray #include <unistd.h> 4668bbf3adSDavid Nugent 4768bbf3adSDavid Nugent 4868bbf3adSDavid Nugent static struct login_res { 4968bbf3adSDavid Nugent const char *what; 5068bbf3adSDavid Nugent rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 5168bbf3adSDavid Nugent int why; 5268bbf3adSDavid Nugent } resources[] = { 5368bbf3adSDavid Nugent { "cputime", login_getcaptime, RLIMIT_CPU }, 5468bbf3adSDavid Nugent { "filesize", login_getcapsize, RLIMIT_FSIZE }, 5568bbf3adSDavid Nugent { "datasize", login_getcapsize, RLIMIT_DATA }, 5668bbf3adSDavid Nugent { "stacksize", login_getcapsize, RLIMIT_STACK }, 5768bbf3adSDavid Nugent { "memoryuse", login_getcapsize, RLIMIT_RSS }, 5868bbf3adSDavid Nugent { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 5968bbf3adSDavid Nugent { "maxproc", login_getcapnum, RLIMIT_NPROC }, 6068bbf3adSDavid Nugent { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 6156c04344SDavid Nugent { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 620c697857SSheldon Hearn { "sbsize", login_getcapsize, RLIMIT_SBSIZE }, 6367577126SMatthew Dillon { "vmemoryuse", login_getcapsize, RLIMIT_VMEM }, 64bc093719SEd Schouten { "pseudoterminals", login_getcapnum, RLIMIT_NPTS }, 65c9253e93SKonstantin Belousov { "swapuse", login_getcapsize, RLIMIT_SWAP }, 6685a0ddfdSKonstantin Belousov { "kqueues", login_getcapsize, RLIMIT_KQUEUES }, 67250d9fd8SKonstantin Belousov { "umtxp", login_getcapnum, RLIMIT_UMTXP }, 6868bbf3adSDavid Nugent { NULL, 0, 0 } 6968bbf3adSDavid Nugent }; 7068bbf3adSDavid Nugent 7168bbf3adSDavid Nugent 7268bbf3adSDavid Nugent void 7368bbf3adSDavid Nugent setclassresources(login_cap_t *lc) 7468bbf3adSDavid Nugent { 7556c04344SDavid Nugent struct login_res *lr; 7668bbf3adSDavid Nugent 771c594de5SDavid Nugent if (lc == NULL) 781c594de5SDavid Nugent return; 791c594de5SDavid Nugent 8056c04344SDavid Nugent for (lr = resources; lr->what != NULL; ++lr) { 8156c04344SDavid Nugent struct rlimit rlim; 8268bbf3adSDavid Nugent 8368bbf3adSDavid Nugent /* 8468bbf3adSDavid Nugent * The login.conf file can have <limit>, <limit>-max, and 8568bbf3adSDavid Nugent * <limit>-cur entries. 8668bbf3adSDavid Nugent * What we do is get the current current- and maximum- limits. 8768bbf3adSDavid Nugent * Then, we try to get an entry for <limit> from the capability, 8868bbf3adSDavid Nugent * using the current and max limits we just got as the 8968bbf3adSDavid Nugent * default/error values. 9068bbf3adSDavid Nugent * *Then*, we try looking for <limit>-cur and <limit>-max, 9168bbf3adSDavid Nugent * again using the appropriate values as the default/error 9268bbf3adSDavid Nugent * conditions. 9368bbf3adSDavid Nugent */ 9468bbf3adSDavid Nugent 9556c04344SDavid Nugent if (getrlimit(lr->why, &rlim) != 0) 9656c04344SDavid Nugent syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 9756c04344SDavid Nugent else { 9856c04344SDavid Nugent char name_cur[40]; 9956c04344SDavid Nugent char name_max[40]; 10056c04344SDavid Nugent rlim_t rcur = rlim.rlim_cur; 10156c04344SDavid Nugent rlim_t rmax = rlim.rlim_max; 10256c04344SDavid Nugent 10356c04344SDavid Nugent sprintf(name_cur, "%s-cur", lr->what); 10456c04344SDavid Nugent sprintf(name_max, "%s-max", lr->what); 10568bbf3adSDavid Nugent 10668bbf3adSDavid Nugent rcur = (*lr->who)(lc, lr->what, rcur, rcur); 10768bbf3adSDavid Nugent rmax = (*lr->who)(lc, lr->what, rmax, rmax); 10856c04344SDavid Nugent rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 10956c04344SDavid Nugent rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 11068bbf3adSDavid Nugent 11156c04344SDavid Nugent if (setrlimit(lr->why, &rlim) == -1) 11268bbf3adSDavid Nugent syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 11356c04344SDavid Nugent } 11456c04344SDavid Nugent } 11556c04344SDavid Nugent } 11668bbf3adSDavid Nugent 11756c04344SDavid Nugent 11868bbf3adSDavid Nugent 11968bbf3adSDavid Nugent static struct login_vars { 12068bbf3adSDavid Nugent const char *tag; 12168bbf3adSDavid Nugent const char *var; 12268bbf3adSDavid Nugent const char *def; 123cc1b8dcbSAndrey A. Chernov int overwrite; 12468bbf3adSDavid Nugent } pathvars[] = { 125cc1b8dcbSAndrey A. Chernov { "path", "PATH", NULL, 1}, 126cc1b8dcbSAndrey A. Chernov { "cdpath", "CDPATH", NULL, 1}, 127cc1b8dcbSAndrey A. Chernov { "manpath", "MANPATH", NULL, 1}, 128cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 12968bbf3adSDavid Nugent }, envars[] = { 130cc1b8dcbSAndrey A. Chernov { "lang", "LANG", NULL, 1}, 131cc1b8dcbSAndrey A. Chernov { "charset", "MM_CHARSET", NULL, 1}, 132acf2957eSKyle Evans { "mail", "MAIL", 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 232eeaf9d56SYuri Pankov if (p != NULL && p != *set_env) { /* Discard invalid entries */ 233eeaf9d56SYuri Pankov const char *ep; 2341c594de5SDavid Nugent char *np; 2351c594de5SDavid Nugent 2361c594de5SDavid Nugent *p++ = '\0'; 237eeaf9d56SYuri Pankov /* Strip leading spaces from variable name */ 238eeaf9d56SYuri Pankov ep = *set_env; 239eeaf9d56SYuri Pankov while (*ep == ' ' || *ep == '\t') 240eeaf9d56SYuri Pankov ep++; 2411c594de5SDavid Nugent if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 242eeaf9d56SYuri Pankov setenv(ep, np, 1); 24368bbf3adSDavid Nugent free(np); 24468bbf3adSDavid Nugent } 24568bbf3adSDavid Nugent } 24668bbf3adSDavid Nugent ++set_env; 24768bbf3adSDavid Nugent } 24868bbf3adSDavid Nugent } 24968bbf3adSDavid Nugent } 25068bbf3adSDavid Nugent } 25168bbf3adSDavid Nugent 25268bbf3adSDavid Nugent 253d84c4292SBrooks Davis static int 254d84c4292SBrooks Davis list2cpuset(const char *list, cpuset_t *mask) 255d84c4292SBrooks Davis { 256d84c4292SBrooks Davis enum { NONE, NUM, DASH } state; 257d84c4292SBrooks Davis int lastnum; 258d84c4292SBrooks Davis int curnum; 259d84c4292SBrooks Davis const char *l; 260d84c4292SBrooks Davis 261d84c4292SBrooks Davis state = NONE; 262d84c4292SBrooks Davis curnum = lastnum = 0; 263d84c4292SBrooks Davis for (l = list; *l != '\0';) { 264d84c4292SBrooks Davis if (isdigit(*l)) { 265d84c4292SBrooks Davis curnum = atoi(l); 266d84c4292SBrooks Davis if (curnum > CPU_SETSIZE) 267d84c4292SBrooks Davis errx(EXIT_FAILURE, 268d84c4292SBrooks Davis "Only %d cpus supported", CPU_SETSIZE); 269d84c4292SBrooks Davis while (isdigit(*l)) 270d84c4292SBrooks Davis l++; 271d84c4292SBrooks Davis switch (state) { 272d84c4292SBrooks Davis case NONE: 273d84c4292SBrooks Davis lastnum = curnum; 274d84c4292SBrooks Davis state = NUM; 275d84c4292SBrooks Davis break; 276d84c4292SBrooks Davis case DASH: 277d84c4292SBrooks Davis for (; lastnum <= curnum; lastnum++) 278d84c4292SBrooks Davis CPU_SET(lastnum, mask); 279d84c4292SBrooks Davis state = NONE; 280d84c4292SBrooks Davis break; 281d84c4292SBrooks Davis case NUM: 282d84c4292SBrooks Davis default: 283d84c4292SBrooks Davis return (0); 284d84c4292SBrooks Davis } 285d84c4292SBrooks Davis continue; 286d84c4292SBrooks Davis } 287d84c4292SBrooks Davis switch (*l) { 288d84c4292SBrooks Davis case ',': 289d84c4292SBrooks Davis switch (state) { 290d84c4292SBrooks Davis case NONE: 291d84c4292SBrooks Davis break; 292d84c4292SBrooks Davis case NUM: 293d84c4292SBrooks Davis CPU_SET(curnum, mask); 294d84c4292SBrooks Davis state = NONE; 295d84c4292SBrooks Davis break; 296d84c4292SBrooks Davis case DASH: 297d84c4292SBrooks Davis return (0); 298d84c4292SBrooks Davis break; 299d84c4292SBrooks Davis } 300d84c4292SBrooks Davis break; 301d84c4292SBrooks Davis case '-': 302d84c4292SBrooks Davis if (state != NUM) 303d84c4292SBrooks Davis return (0); 304d84c4292SBrooks Davis state = DASH; 305d84c4292SBrooks Davis break; 306d84c4292SBrooks Davis default: 307d84c4292SBrooks Davis return (0); 308d84c4292SBrooks Davis } 309d84c4292SBrooks Davis l++; 310d84c4292SBrooks Davis } 311d84c4292SBrooks Davis switch (state) { 312d84c4292SBrooks Davis case NONE: 313d84c4292SBrooks Davis break; 314d84c4292SBrooks Davis case NUM: 315d84c4292SBrooks Davis CPU_SET(curnum, mask); 316d84c4292SBrooks Davis break; 317d84c4292SBrooks Davis case DASH: 318d84c4292SBrooks Davis return (0); 319d84c4292SBrooks Davis } 3202d057ca6SDag-Erling Smørgrav return (1); 321d84c4292SBrooks Davis } 322d84c4292SBrooks Davis 323d84c4292SBrooks Davis 324d84c4292SBrooks Davis void 325d84c4292SBrooks Davis setclasscpumask(login_cap_t *lc) 326d84c4292SBrooks Davis { 327d84c4292SBrooks Davis const char *maskstr; 328d84c4292SBrooks Davis cpuset_t maskset; 329d84c4292SBrooks Davis cpusetid_t setid; 330d84c4292SBrooks Davis 331d84c4292SBrooks Davis maskstr = login_getcapstr(lc, "cpumask", NULL, NULL); 332d84c4292SBrooks Davis CPU_ZERO(&maskset); 333d84c4292SBrooks Davis if (maskstr == NULL) 334d84c4292SBrooks Davis return; 335d84c4292SBrooks Davis if (strcasecmp("default", maskstr) == 0) 336d84c4292SBrooks Davis return; 337d84c4292SBrooks Davis if (!list2cpuset(maskstr, &maskset)) { 338d84c4292SBrooks Davis syslog(LOG_WARNING, 339d84c4292SBrooks Davis "list2cpuset(%s) invalid mask specification", maskstr); 340d84c4292SBrooks Davis return; 341d84c4292SBrooks Davis } 342d84c4292SBrooks Davis 343d84c4292SBrooks Davis if (cpuset(&setid) != 0) { 344d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset(): %s", strerror(errno)); 345d84c4292SBrooks Davis return; 346d84c4292SBrooks Davis } 347d84c4292SBrooks Davis 348d84c4292SBrooks Davis if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, 349d84c4292SBrooks Davis sizeof(maskset), &maskset) != 0) 350d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr, 351d84c4292SBrooks Davis strerror(errno)); 352d84c4292SBrooks Davis } 353d84c4292SBrooks Davis 354d84c4292SBrooks Davis 35568bbf3adSDavid Nugent /* 35668bbf3adSDavid Nugent * setclasscontext() 35768bbf3adSDavid Nugent * 35868bbf3adSDavid Nugent * For the login class <class>, set various class context values 35968bbf3adSDavid Nugent * (limits, mainly) to the values for that class. Which values are 36068bbf3adSDavid Nugent * set are controlled by <flags> -- see <login_class.h> for the 36168bbf3adSDavid Nugent * possible values. 36268bbf3adSDavid Nugent * 36368bbf3adSDavid Nugent * setclasscontext() can only set resources, priority, and umask. 36468bbf3adSDavid Nugent */ 36568bbf3adSDavid Nugent 36668bbf3adSDavid Nugent int 36768bbf3adSDavid Nugent setclasscontext(const char *classname, unsigned int flags) 36868bbf3adSDavid Nugent { 36968bbf3adSDavid Nugent int rc; 37056c04344SDavid Nugent login_cap_t *lc; 37156c04344SDavid Nugent 37256c04344SDavid Nugent lc = login_getclassbyname(classname, NULL); 37356c04344SDavid Nugent 37456c04344SDavid Nugent flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 37556c04344SDavid Nugent LOGIN_SETUMASK | LOGIN_SETPATH; 37656c04344SDavid Nugent 37756c04344SDavid Nugent rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 37868bbf3adSDavid Nugent login_close(lc); 3792d057ca6SDag-Erling Smørgrav return (rc); 38068bbf3adSDavid Nugent } 38168bbf3adSDavid Nugent 38268bbf3adSDavid Nugent 383c328e6c6SOlivier Certner static const char * const inherit_enum[] = { 384c328e6c6SOlivier Certner "inherit", 385c328e6c6SOlivier Certner NULL 386c328e6c6SOlivier Certner }; 387c328e6c6SOlivier Certner 38856c04344SDavid Nugent /* 3890dd1705fSOlivier Certner * Private function setting umask from the login class. 39056c04344SDavid Nugent */ 391e99c28e9SOlivier Certner static void 3920dd1705fSOlivier Certner setclassumask(login_cap_t *lc, const struct passwd *pwd) 39356c04344SDavid Nugent { 394e99c28e9SOlivier Certner /* 395e99c28e9SOlivier Certner * Make it unlikely that someone would input our default sentinel 396e99c28e9SOlivier Certner * indicating no specification. 397e99c28e9SOlivier Certner */ 398e99c28e9SOlivier Certner const rlim_t def_val = INT64_MIN + 1, err_val = INT64_MIN; 399c328e6c6SOlivier Certner rlim_t val; 400c328e6c6SOlivier Certner 401c328e6c6SOlivier Certner /* If value is "inherit", nothing to change. */ 402c328e6c6SOlivier Certner if (login_getcapenum(lc, "umask", inherit_enum) == 0) 403c328e6c6SOlivier Certner return; 404c328e6c6SOlivier Certner 405c328e6c6SOlivier Certner val = login_getcapnum(lc, "umask", def_val, err_val); 406e99c28e9SOlivier Certner 407e99c28e9SOlivier Certner if (val != def_val) { 408e99c28e9SOlivier Certner if (val < 0 || val > UINT16_MAX) { 409e99c28e9SOlivier Certner /* We get here also on 'err_val'. */ 410e99c28e9SOlivier Certner syslog(LOG_WARNING, 411e99c28e9SOlivier Certner "%s%s%sLogin class '%s': " 412e99c28e9SOlivier Certner "Invalid umask specification: '%s'", 413e99c28e9SOlivier Certner pwd ? "Login '" : "", 414e99c28e9SOlivier Certner pwd ? pwd->pw_name : "", 415e99c28e9SOlivier Certner pwd ? "': " : "", 416e99c28e9SOlivier Certner lc->lc_class, 417e99c28e9SOlivier Certner login_getcapstr(lc, "umask", "", "")); 418e99c28e9SOlivier Certner } else { 419e99c28e9SOlivier Certner const mode_t mode = val; 420e99c28e9SOlivier Certner 421e99c28e9SOlivier Certner umask(mode); 422e99c28e9SOlivier Certner } 423e99c28e9SOlivier Certner } 424e99c28e9SOlivier Certner } 4250dd1705fSOlivier Certner 4260dd1705fSOlivier Certner /* 4270dd1705fSOlivier Certner * Private function which takes care of processing 4280dd1705fSOlivier Certner */ 4290dd1705fSOlivier Certner 4300dd1705fSOlivier Certner static void 4310dd1705fSOlivier Certner setlogincontext(login_cap_t *lc, const struct passwd *pwd, unsigned long flags) 4320dd1705fSOlivier Certner { 433*771d5c93SOlivier Certner if (lc == NULL) 434*771d5c93SOlivier Certner return; 435*771d5c93SOlivier Certner 436*771d5c93SOlivier Certner /* Set resources. */ 437*771d5c93SOlivier Certner if ((flags & LOGIN_SETRESOURCES) != 0) 4380dd1705fSOlivier Certner setclassresources(lc); 439*771d5c93SOlivier Certner 440*771d5c93SOlivier Certner /* See if there's a umask override. */ 441*771d5c93SOlivier Certner if ((flags & LOGIN_SETUMASK) != 0) 4420dd1705fSOlivier Certner setclassumask(lc, pwd); 443*771d5c93SOlivier Certner 444*771d5c93SOlivier Certner /* Set paths. */ 445*771d5c93SOlivier Certner if ((flags & LOGIN_SETPATH) != 0) 44656c04344SDavid Nugent setclassenvironment(lc, pwd, 1); 447*771d5c93SOlivier Certner 448*771d5c93SOlivier Certner /* Set environment. */ 449*771d5c93SOlivier Certner if ((flags & LOGIN_SETENV) != 0) 45056c04344SDavid Nugent setclassenvironment(lc, pwd, 0); 451*771d5c93SOlivier Certner 452*771d5c93SOlivier Certner /* Set cpu affinity. */ 453*771d5c93SOlivier Certner if ((flags & LOGIN_SETCPUMASK) != 0) 454d84c4292SBrooks Davis setclasscpumask(lc); 45556c04344SDavid Nugent } 45656c04344SDavid Nugent 45756c04344SDavid Nugent 45856c04344SDavid Nugent 45968bbf3adSDavid Nugent /* 46068bbf3adSDavid Nugent * setusercontext() 46168bbf3adSDavid Nugent * 46268bbf3adSDavid Nugent * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 46368bbf3adSDavid Nugent * set the context as in setclasscontext(). <flags> controls which 46468bbf3adSDavid Nugent * values are set. 46568bbf3adSDavid Nugent * 46668bbf3adSDavid Nugent * The difference between setclasscontext() and setusercontext() is 46768bbf3adSDavid Nugent * that the former sets things up for an already-existing process, 46868bbf3adSDavid Nugent * while the latter sets things up from a root context. Such as might 46968bbf3adSDavid Nugent * be called from login(1). 47068bbf3adSDavid Nugent * 47168bbf3adSDavid Nugent */ 47268bbf3adSDavid Nugent 47368bbf3adSDavid Nugent int 47468bbf3adSDavid Nugent setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 47568bbf3adSDavid Nugent { 476b149798eSDimitry Andric rlim_t p; 47768bbf3adSDavid Nugent login_cap_t *llc = NULL; 478e172f0e5SSteve Price struct rtprio rtp; 47984333872SRobert Watson int error; 48068bbf3adSDavid Nugent 48156c04344SDavid Nugent if (lc == NULL) { 48256c04344SDavid Nugent if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 48368bbf3adSDavid Nugent llc = lc; /* free this when we're done */ 48468bbf3adSDavid Nugent } 48568bbf3adSDavid Nugent 48668bbf3adSDavid Nugent if (flags & LOGIN_SETPATH) 48768bbf3adSDavid Nugent pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 48868bbf3adSDavid Nugent 48956c04344SDavid Nugent /* we need a passwd entry to set these */ 49056c04344SDavid Nugent if (pwd == NULL) 491433c28e0SRobert Watson flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 49256c04344SDavid Nugent 49356c04344SDavid Nugent /* Set the process priority */ 49468bbf3adSDavid Nugent if (flags & LOGIN_SETPRIORITY) { 49556c04344SDavid Nugent p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 49656c04344SDavid Nugent 497e172f0e5SSteve Price if (p > PRIO_MAX) { 498e172f0e5SSteve Price rtp.type = RTP_PRIO_IDLE; 499bd572be7SOlivier Certner p += RTP_PRIO_MIN - (PRIO_MAX + 1); 500b149798eSDimitry Andric rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p; 501e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 502e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 5030bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 5040bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 505e172f0e5SSteve Price } else if (p < PRIO_MIN) { 506e172f0e5SSteve Price rtp.type = RTP_PRIO_REALTIME; 507bd572be7SOlivier Certner p += RTP_PRIO_MAX - (PRIO_MIN - 1); 508b149798eSDimitry Andric rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p; 509e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 510e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 5110bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 5120bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 513e172f0e5SSteve Price } else { 51456c04344SDavid Nugent if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 51556c04344SDavid Nugent syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 5160bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 5170bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 51868bbf3adSDavid Nugent } 519e172f0e5SSteve Price } 52068bbf3adSDavid Nugent 52156c04344SDavid Nugent /* Setup the user's group permissions */ 52256c04344SDavid Nugent if (flags & LOGIN_SETGROUP) { 52356c04344SDavid Nugent if (setgid(pwd->pw_gid) != 0) { 5249f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 52556c04344SDavid Nugent login_close(llc); 5262d057ca6SDag-Erling Smørgrav return (-1); 52756c04344SDavid Nugent } 52856c04344SDavid Nugent if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 5299f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 5309f3a9c3aSAndrey A. Chernov (u_long)pwd->pw_gid); 53156c04344SDavid Nugent login_close(llc); 5322d057ca6SDag-Erling Smørgrav return (-1); 53356c04344SDavid Nugent } 53456c04344SDavid Nugent } 53568bbf3adSDavid Nugent 53684333872SRobert Watson /* Set up the user's MAC label. */ 53784333872SRobert Watson if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 53884333872SRobert Watson const char *label_string; 53984333872SRobert Watson mac_t label; 54084333872SRobert Watson 54184333872SRobert Watson label_string = login_getcapstr(lc, "label", NULL, NULL); 54284333872SRobert Watson if (label_string != NULL) { 54384333872SRobert Watson if (mac_from_text(&label, label_string) == -1) { 54484333872SRobert Watson syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 54584333872SRobert Watson pwd->pw_name, label_string); 5462d057ca6SDag-Erling Smørgrav return (-1); 54784333872SRobert Watson } 54884333872SRobert Watson if (mac_set_proc(label) == -1) 54984333872SRobert Watson error = errno; 55084333872SRobert Watson else 55184333872SRobert Watson error = 0; 55284333872SRobert Watson mac_free(label); 55384333872SRobert Watson if (error != 0) { 55484333872SRobert Watson syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 55584333872SRobert Watson label_string, pwd->pw_name, strerror(error)); 5562d057ca6SDag-Erling Smørgrav return (-1); 55784333872SRobert Watson } 55884333872SRobert Watson } 55984333872SRobert Watson } 56084333872SRobert Watson 56156c04344SDavid Nugent /* Set the sessions login */ 56268bbf3adSDavid Nugent if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 56356c04344SDavid Nugent syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 56468bbf3adSDavid Nugent login_close(llc); 5652d057ca6SDag-Erling Smørgrav return (-1); 56668bbf3adSDavid Nugent } 56768bbf3adSDavid Nugent 5682bfc50bcSEdward Tomasz Napierala /* Inform the kernel about current login class */ 5692bfc50bcSEdward Tomasz Napierala if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) { 5702bfc50bcSEdward Tomasz Napierala error = setloginclass(lc->lc_class); 5712bfc50bcSEdward Tomasz Napierala if (error != 0) { 5722bfc50bcSEdward Tomasz Napierala syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class); 5732bfc50bcSEdward Tomasz Napierala #ifdef notyet 5742bfc50bcSEdward Tomasz Napierala login_close(llc); 5752bfc50bcSEdward Tomasz Napierala return (-1); 5762bfc50bcSEdward Tomasz Napierala #endif 5772bfc50bcSEdward Tomasz Napierala } 5782bfc50bcSEdward Tomasz Napierala } 5792bfc50bcSEdward Tomasz Napierala 580e99c28e9SOlivier Certner setlogincontext(lc, pwd, flags); 5811c594de5SDavid Nugent login_close(llc); 58256c04344SDavid Nugent 58356c04344SDavid Nugent /* This needs to be done after anything that needs root privs */ 58456c04344SDavid Nugent if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 5859f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 5862d057ca6SDag-Erling Smørgrav return (-1); /* Paranoia again */ 5871c594de5SDavid Nugent } 5881c594de5SDavid Nugent 58956c04344SDavid Nugent /* 59056c04344SDavid Nugent * Now, we repeat some of the above for the user's private entries 59156c04344SDavid Nugent */ 592892654feSOlivier Certner if (geteuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { 593e99c28e9SOlivier Certner setlogincontext(lc, pwd, flags); 59456c04344SDavid Nugent login_close(lc); 59556c04344SDavid Nugent } 59656c04344SDavid Nugent 5972d057ca6SDag-Erling Smørgrav return (0); 59868bbf3adSDavid Nugent } 599