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 }, 6985a0ddfdSKonstantin Belousov { "kqueues", login_getcapsize, RLIMIT_KQUEUES }, 70*250d9fd8SKonstantin Belousov { "umtxp", login_getcapnum, RLIMIT_UMTXP }, 7168bbf3adSDavid Nugent { NULL, 0, 0 } 7268bbf3adSDavid Nugent }; 7368bbf3adSDavid Nugent 7468bbf3adSDavid Nugent 7568bbf3adSDavid Nugent void 7668bbf3adSDavid Nugent setclassresources(login_cap_t *lc) 7768bbf3adSDavid Nugent { 7856c04344SDavid Nugent struct login_res *lr; 7968bbf3adSDavid Nugent 801c594de5SDavid Nugent if (lc == NULL) 811c594de5SDavid Nugent return; 821c594de5SDavid Nugent 8356c04344SDavid Nugent for (lr = resources; lr->what != NULL; ++lr) { 8456c04344SDavid Nugent struct rlimit rlim; 8568bbf3adSDavid Nugent 8668bbf3adSDavid Nugent /* 8768bbf3adSDavid Nugent * The login.conf file can have <limit>, <limit>-max, and 8868bbf3adSDavid Nugent * <limit>-cur entries. 8968bbf3adSDavid Nugent * What we do is get the current current- and maximum- limits. 9068bbf3adSDavid Nugent * Then, we try to get an entry for <limit> from the capability, 9168bbf3adSDavid Nugent * using the current and max limits we just got as the 9268bbf3adSDavid Nugent * default/error values. 9368bbf3adSDavid Nugent * *Then*, we try looking for <limit>-cur and <limit>-max, 9468bbf3adSDavid Nugent * again using the appropriate values as the default/error 9568bbf3adSDavid Nugent * conditions. 9668bbf3adSDavid Nugent */ 9768bbf3adSDavid Nugent 9856c04344SDavid Nugent if (getrlimit(lr->why, &rlim) != 0) 9956c04344SDavid Nugent syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 10056c04344SDavid Nugent else { 10156c04344SDavid Nugent char name_cur[40]; 10256c04344SDavid Nugent char name_max[40]; 10356c04344SDavid Nugent rlim_t rcur = rlim.rlim_cur; 10456c04344SDavid Nugent rlim_t rmax = rlim.rlim_max; 10556c04344SDavid Nugent 10656c04344SDavid Nugent sprintf(name_cur, "%s-cur", lr->what); 10756c04344SDavid Nugent sprintf(name_max, "%s-max", lr->what); 10868bbf3adSDavid Nugent 10968bbf3adSDavid Nugent rcur = (*lr->who)(lc, lr->what, rcur, rcur); 11068bbf3adSDavid Nugent rmax = (*lr->who)(lc, lr->what, rmax, rmax); 11156c04344SDavid Nugent rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 11256c04344SDavid Nugent rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 11368bbf3adSDavid Nugent 11456c04344SDavid Nugent if (setrlimit(lr->why, &rlim) == -1) 11568bbf3adSDavid Nugent syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 11656c04344SDavid Nugent } 11756c04344SDavid Nugent } 11856c04344SDavid Nugent } 11968bbf3adSDavid Nugent 12056c04344SDavid Nugent 12168bbf3adSDavid Nugent 12268bbf3adSDavid Nugent static struct login_vars { 12368bbf3adSDavid Nugent const char *tag; 12468bbf3adSDavid Nugent const char *var; 12568bbf3adSDavid Nugent const char *def; 126cc1b8dcbSAndrey A. Chernov int overwrite; 12768bbf3adSDavid Nugent } pathvars[] = { 128cc1b8dcbSAndrey A. Chernov { "path", "PATH", NULL, 1}, 129cc1b8dcbSAndrey A. Chernov { "cdpath", "CDPATH", NULL, 1}, 130cc1b8dcbSAndrey A. Chernov { "manpath", "MANPATH", NULL, 1}, 131cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13268bbf3adSDavid Nugent }, envars[] = { 133cc1b8dcbSAndrey A. Chernov { "lang", "LANG", NULL, 1}, 134cc1b8dcbSAndrey A. Chernov { "charset", "MM_CHARSET", NULL, 1}, 135cc1b8dcbSAndrey A. Chernov { "timezone", "TZ", NULL, 1}, 136cc1b8dcbSAndrey A. Chernov { "term", "TERM", NULL, 0}, 137cc1b8dcbSAndrey A. Chernov { NULL, NULL, NULL, 0} 13868bbf3adSDavid Nugent }; 13968bbf3adSDavid Nugent 14068bbf3adSDavid Nugent static char * 141b00ba4ccSRuslan Ermilov substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 14268bbf3adSDavid Nugent { 14368bbf3adSDavid Nugent char *np = NULL; 14468bbf3adSDavid Nugent 14568bbf3adSDavid Nugent if (var != NULL) { 14668bbf3adSDavid Nugent int tildes = 0; 14768bbf3adSDavid Nugent int dollas = 0; 14868bbf3adSDavid Nugent char *p; 149b8a5cd86SDag-Erling Smørgrav const char *q; 15068bbf3adSDavid Nugent 15168bbf3adSDavid Nugent if (pwd != NULL) { 152b8a5cd86SDag-Erling Smørgrav for (q = var; *q != '\0'; ++q) { 153b8a5cd86SDag-Erling Smørgrav tildes += (*q == '~'); 154b8a5cd86SDag-Erling Smørgrav dollas += (*q == '$'); 155b8a5cd86SDag-Erling Smørgrav } 15668bbf3adSDavid Nugent } 15768bbf3adSDavid Nugent 15856c04344SDavid Nugent np = malloc(strlen(var) + (dollas * nlen) 15956c04344SDavid Nugent - dollas + (tildes * (pch+hlen)) 16056c04344SDavid Nugent - tildes + 1); 16168bbf3adSDavid Nugent 16268bbf3adSDavid Nugent if (np != NULL) { 16368bbf3adSDavid Nugent p = strcpy(np, var); 16468bbf3adSDavid Nugent 16568bbf3adSDavid Nugent if (pwd != NULL) { 16668bbf3adSDavid Nugent /* 16768bbf3adSDavid Nugent * This loop does user username and homedir substitutions 16868bbf3adSDavid Nugent * for unescaped $ (username) and ~ (homedir) 16968bbf3adSDavid Nugent */ 17068bbf3adSDavid Nugent while (*(p += strcspn(p, "~$")) != '\0') { 17168bbf3adSDavid Nugent int l = strlen(p); 17268bbf3adSDavid Nugent 173121ba32dSAndrey A. Chernov if (p > np && *(p-1) == '\\') /* Escaped: */ 17468bbf3adSDavid Nugent memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 17568bbf3adSDavid Nugent else if (*p == '~') { 1761c594de5SDavid Nugent int v = pch && *(p+1) != '/'; /* Avoid double // */ 1771c594de5SDavid Nugent memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 17868bbf3adSDavid Nugent memmove(p, pwd->pw_dir, hlen); 1791c594de5SDavid Nugent if (v) 18068bbf3adSDavid Nugent p[hlen] = '/'; 1811c594de5SDavid Nugent p += hlen + v; 18268bbf3adSDavid Nugent } 18368bbf3adSDavid Nugent else /* if (*p == '$') */ { 1841c594de5SDavid Nugent memmove(p + nlen, p + 1, l); /* Subst username */ 18568bbf3adSDavid Nugent memmove(p, pwd->pw_name, nlen); 18668bbf3adSDavid Nugent p += nlen; 18768bbf3adSDavid Nugent } 18868bbf3adSDavid Nugent } 18968bbf3adSDavid Nugent } 19068bbf3adSDavid Nugent } 19168bbf3adSDavid Nugent } 19256c04344SDavid Nugent 1932d057ca6SDag-Erling Smørgrav return (np); 19468bbf3adSDavid Nugent } 19568bbf3adSDavid Nugent 19668bbf3adSDavid Nugent 19768bbf3adSDavid Nugent void 19868bbf3adSDavid Nugent setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 19968bbf3adSDavid Nugent { 20068bbf3adSDavid Nugent struct login_vars *vars = paths ? pathvars : envars; 20168bbf3adSDavid Nugent int hlen = pwd ? strlen(pwd->pw_dir) : 0; 20268bbf3adSDavid Nugent int nlen = pwd ? strlen(pwd->pw_name) : 0; 20368bbf3adSDavid Nugent char pch = 0; 20468bbf3adSDavid Nugent 20568bbf3adSDavid Nugent if (hlen && pwd->pw_dir[hlen-1] != '/') 20668bbf3adSDavid Nugent ++pch; 20768bbf3adSDavid Nugent 20868bbf3adSDavid Nugent while (vars->tag != NULL) { 209b00ba4ccSRuslan Ermilov const char * var = paths ? login_getpath(lc, vars->tag, NULL) 21068bbf3adSDavid Nugent : login_getcapstr(lc, vars->tag, NULL, NULL); 21168bbf3adSDavid Nugent 21268bbf3adSDavid Nugent char * np = substvar(var, pwd, hlen, pch, nlen); 21368bbf3adSDavid Nugent 21468bbf3adSDavid Nugent if (np != NULL) { 215cc1b8dcbSAndrey A. Chernov setenv(vars->var, np, vars->overwrite); 21668bbf3adSDavid Nugent free(np); 21768bbf3adSDavid Nugent } else if (vars->def != NULL) { 21868bbf3adSDavid Nugent setenv(vars->var, vars->def, 0); 21968bbf3adSDavid Nugent } 22068bbf3adSDavid Nugent ++vars; 22168bbf3adSDavid Nugent } 22268bbf3adSDavid Nugent 22368bbf3adSDavid Nugent /* 22468bbf3adSDavid Nugent * If we're not processing paths, then see if there is a setenv list by 22568bbf3adSDavid Nugent * which the admin and/or user may set an arbitrary set of env vars. 22668bbf3adSDavid Nugent */ 22768bbf3adSDavid Nugent if (!paths) { 228547fa0d9SMark Murray const char **set_env = login_getcaplist(lc, "setenv", ","); 2291c594de5SDavid Nugent 23068bbf3adSDavid Nugent if (set_env != NULL) { 23168bbf3adSDavid Nugent while (*set_env != NULL) { 23268bbf3adSDavid Nugent char *p = strchr(*set_env, '='); 23356c04344SDavid Nugent 2341c594de5SDavid Nugent if (p != NULL) { /* Discard invalid entries */ 2351c594de5SDavid Nugent char *np; 2361c594de5SDavid Nugent 2371c594de5SDavid Nugent *p++ = '\0'; 2381c594de5SDavid Nugent if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 2391c594de5SDavid Nugent setenv(*set_env, np, 1); 24068bbf3adSDavid Nugent free(np); 24168bbf3adSDavid Nugent } 24268bbf3adSDavid Nugent } 24368bbf3adSDavid Nugent ++set_env; 24468bbf3adSDavid Nugent } 24568bbf3adSDavid Nugent } 24668bbf3adSDavid Nugent } 24768bbf3adSDavid Nugent } 24868bbf3adSDavid Nugent 24968bbf3adSDavid Nugent 250d84c4292SBrooks Davis static int 251d84c4292SBrooks Davis list2cpuset(const char *list, cpuset_t *mask) 252d84c4292SBrooks Davis { 253d84c4292SBrooks Davis enum { NONE, NUM, DASH } state; 254d84c4292SBrooks Davis int lastnum; 255d84c4292SBrooks Davis int curnum; 256d84c4292SBrooks Davis const char *l; 257d84c4292SBrooks Davis 258d84c4292SBrooks Davis state = NONE; 259d84c4292SBrooks Davis curnum = lastnum = 0; 260d84c4292SBrooks Davis for (l = list; *l != '\0';) { 261d84c4292SBrooks Davis if (isdigit(*l)) { 262d84c4292SBrooks Davis curnum = atoi(l); 263d84c4292SBrooks Davis if (curnum > CPU_SETSIZE) 264d84c4292SBrooks Davis errx(EXIT_FAILURE, 265d84c4292SBrooks Davis "Only %d cpus supported", CPU_SETSIZE); 266d84c4292SBrooks Davis while (isdigit(*l)) 267d84c4292SBrooks Davis l++; 268d84c4292SBrooks Davis switch (state) { 269d84c4292SBrooks Davis case NONE: 270d84c4292SBrooks Davis lastnum = curnum; 271d84c4292SBrooks Davis state = NUM; 272d84c4292SBrooks Davis break; 273d84c4292SBrooks Davis case DASH: 274d84c4292SBrooks Davis for (; lastnum <= curnum; lastnum++) 275d84c4292SBrooks Davis CPU_SET(lastnum, mask); 276d84c4292SBrooks Davis state = NONE; 277d84c4292SBrooks Davis break; 278d84c4292SBrooks Davis case NUM: 279d84c4292SBrooks Davis default: 280d84c4292SBrooks Davis return (0); 281d84c4292SBrooks Davis } 282d84c4292SBrooks Davis continue; 283d84c4292SBrooks Davis } 284d84c4292SBrooks Davis switch (*l) { 285d84c4292SBrooks Davis case ',': 286d84c4292SBrooks Davis switch (state) { 287d84c4292SBrooks Davis case NONE: 288d84c4292SBrooks Davis break; 289d84c4292SBrooks Davis case NUM: 290d84c4292SBrooks Davis CPU_SET(curnum, mask); 291d84c4292SBrooks Davis state = NONE; 292d84c4292SBrooks Davis break; 293d84c4292SBrooks Davis case DASH: 294d84c4292SBrooks Davis return (0); 295d84c4292SBrooks Davis break; 296d84c4292SBrooks Davis } 297d84c4292SBrooks Davis break; 298d84c4292SBrooks Davis case '-': 299d84c4292SBrooks Davis if (state != NUM) 300d84c4292SBrooks Davis return (0); 301d84c4292SBrooks Davis state = DASH; 302d84c4292SBrooks Davis break; 303d84c4292SBrooks Davis default: 304d84c4292SBrooks Davis return (0); 305d84c4292SBrooks Davis } 306d84c4292SBrooks Davis l++; 307d84c4292SBrooks Davis } 308d84c4292SBrooks Davis switch (state) { 309d84c4292SBrooks Davis case NONE: 310d84c4292SBrooks Davis break; 311d84c4292SBrooks Davis case NUM: 312d84c4292SBrooks Davis CPU_SET(curnum, mask); 313d84c4292SBrooks Davis break; 314d84c4292SBrooks Davis case DASH: 315d84c4292SBrooks Davis return (0); 316d84c4292SBrooks Davis } 3172d057ca6SDag-Erling Smørgrav return (1); 318d84c4292SBrooks Davis } 319d84c4292SBrooks Davis 320d84c4292SBrooks Davis 321d84c4292SBrooks Davis void 322d84c4292SBrooks Davis setclasscpumask(login_cap_t *lc) 323d84c4292SBrooks Davis { 324d84c4292SBrooks Davis const char *maskstr; 325d84c4292SBrooks Davis cpuset_t maskset; 326d84c4292SBrooks Davis cpusetid_t setid; 327d84c4292SBrooks Davis 328d84c4292SBrooks Davis maskstr = login_getcapstr(lc, "cpumask", NULL, NULL); 329d84c4292SBrooks Davis CPU_ZERO(&maskset); 330d84c4292SBrooks Davis if (maskstr == NULL) 331d84c4292SBrooks Davis return; 332d84c4292SBrooks Davis if (strcasecmp("default", maskstr) == 0) 333d84c4292SBrooks Davis return; 334d84c4292SBrooks Davis if (!list2cpuset(maskstr, &maskset)) { 335d84c4292SBrooks Davis syslog(LOG_WARNING, 336d84c4292SBrooks Davis "list2cpuset(%s) invalid mask specification", maskstr); 337d84c4292SBrooks Davis return; 338d84c4292SBrooks Davis } 339d84c4292SBrooks Davis 340d84c4292SBrooks Davis if (cpuset(&setid) != 0) { 341d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset(): %s", strerror(errno)); 342d84c4292SBrooks Davis return; 343d84c4292SBrooks Davis } 344d84c4292SBrooks Davis 345d84c4292SBrooks Davis if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, 346d84c4292SBrooks Davis sizeof(maskset), &maskset) != 0) 347d84c4292SBrooks Davis syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr, 348d84c4292SBrooks Davis strerror(errno)); 349d84c4292SBrooks Davis } 350d84c4292SBrooks Davis 351d84c4292SBrooks Davis 35268bbf3adSDavid Nugent /* 35368bbf3adSDavid Nugent * setclasscontext() 35468bbf3adSDavid Nugent * 35568bbf3adSDavid Nugent * For the login class <class>, set various class context values 35668bbf3adSDavid Nugent * (limits, mainly) to the values for that class. Which values are 35768bbf3adSDavid Nugent * set are controlled by <flags> -- see <login_class.h> for the 35868bbf3adSDavid Nugent * possible values. 35968bbf3adSDavid Nugent * 36068bbf3adSDavid Nugent * setclasscontext() can only set resources, priority, and umask. 36168bbf3adSDavid Nugent */ 36268bbf3adSDavid Nugent 36368bbf3adSDavid Nugent int 36468bbf3adSDavid Nugent setclasscontext(const char *classname, unsigned int flags) 36568bbf3adSDavid Nugent { 36668bbf3adSDavid Nugent int rc; 36756c04344SDavid Nugent login_cap_t *lc; 36856c04344SDavid Nugent 36956c04344SDavid Nugent lc = login_getclassbyname(classname, NULL); 37056c04344SDavid Nugent 37156c04344SDavid Nugent flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 37256c04344SDavid Nugent LOGIN_SETUMASK | LOGIN_SETPATH; 37356c04344SDavid Nugent 37456c04344SDavid Nugent rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 37568bbf3adSDavid Nugent login_close(lc); 3762d057ca6SDag-Erling Smørgrav return (rc); 37768bbf3adSDavid Nugent } 37868bbf3adSDavid Nugent 37968bbf3adSDavid Nugent 38056c04344SDavid Nugent 38156c04344SDavid Nugent /* 382f855462aSYaroslav Tykhiy * Private function which takes care of processing 38356c04344SDavid Nugent */ 38456c04344SDavid Nugent 38556c04344SDavid Nugent static mode_t 38656c04344SDavid Nugent setlogincontext(login_cap_t *lc, const struct passwd *pwd, 38756c04344SDavid Nugent mode_t mymask, unsigned long flags) 38856c04344SDavid Nugent { 38956c04344SDavid Nugent if (lc) { 39056c04344SDavid Nugent /* Set resources */ 39156c04344SDavid Nugent if (flags & LOGIN_SETRESOURCES) 39256c04344SDavid Nugent setclassresources(lc); 39356c04344SDavid Nugent /* See if there's a umask override */ 39456c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 39556c04344SDavid Nugent mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); 39656c04344SDavid Nugent /* Set paths */ 39756c04344SDavid Nugent if (flags & LOGIN_SETPATH) 39856c04344SDavid Nugent setclassenvironment(lc, pwd, 1); 39956c04344SDavid Nugent /* Set environment */ 40056c04344SDavid Nugent if (flags & LOGIN_SETENV) 40156c04344SDavid Nugent setclassenvironment(lc, pwd, 0); 402d84c4292SBrooks Davis /* Set cpu affinity */ 403d84c4292SBrooks Davis if (flags & LOGIN_SETCPUMASK) 404d84c4292SBrooks Davis setclasscpumask(lc); 40556c04344SDavid Nugent } 4062d057ca6SDag-Erling Smørgrav return (mymask); 40756c04344SDavid Nugent } 40856c04344SDavid Nugent 40956c04344SDavid Nugent 41056c04344SDavid Nugent 41168bbf3adSDavid Nugent /* 41268bbf3adSDavid Nugent * setusercontext() 41368bbf3adSDavid Nugent * 41468bbf3adSDavid Nugent * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 41568bbf3adSDavid Nugent * set the context as in setclasscontext(). <flags> controls which 41668bbf3adSDavid Nugent * values are set. 41768bbf3adSDavid Nugent * 41868bbf3adSDavid Nugent * The difference between setclasscontext() and setusercontext() is 41968bbf3adSDavid Nugent * that the former sets things up for an already-existing process, 42068bbf3adSDavid Nugent * while the latter sets things up from a root context. Such as might 42168bbf3adSDavid Nugent * be called from login(1). 42268bbf3adSDavid Nugent * 42368bbf3adSDavid Nugent */ 42468bbf3adSDavid Nugent 42568bbf3adSDavid Nugent int 42668bbf3adSDavid Nugent setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 42768bbf3adSDavid Nugent { 428b149798eSDimitry Andric rlim_t p; 42956c04344SDavid Nugent mode_t mymask; 43068bbf3adSDavid Nugent login_cap_t *llc = NULL; 4312bfc50bcSEdward Tomasz Napierala struct sigaction sa, prevsa; 432e172f0e5SSteve Price struct rtprio rtp; 43384333872SRobert Watson int error; 43468bbf3adSDavid Nugent 43556c04344SDavid Nugent if (lc == NULL) { 43656c04344SDavid Nugent if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 43768bbf3adSDavid Nugent llc = lc; /* free this when we're done */ 43868bbf3adSDavid Nugent } 43968bbf3adSDavid Nugent 44068bbf3adSDavid Nugent if (flags & LOGIN_SETPATH) 44168bbf3adSDavid Nugent pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 44268bbf3adSDavid Nugent 44356c04344SDavid Nugent /* we need a passwd entry to set these */ 44456c04344SDavid Nugent if (pwd == NULL) 445433c28e0SRobert Watson flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 44656c04344SDavid Nugent 44756c04344SDavid Nugent /* Set the process priority */ 44868bbf3adSDavid Nugent if (flags & LOGIN_SETPRIORITY) { 44956c04344SDavid Nugent p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 45056c04344SDavid Nugent 451e172f0e5SSteve Price if (p > PRIO_MAX) { 452e172f0e5SSteve Price rtp.type = RTP_PRIO_IDLE; 453b149798eSDimitry Andric p -= PRIO_MAX + 1; 454b149798eSDimitry Andric rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p; 455e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 456e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 4570bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4580bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 459e172f0e5SSteve Price } else if (p < PRIO_MIN) { 460e172f0e5SSteve Price rtp.type = RTP_PRIO_REALTIME; 461b149798eSDimitry Andric p -= PRIO_MIN - RTP_PRIO_MAX; 462b149798eSDimitry Andric rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p; 463e172f0e5SSteve Price if (rtprio(RTP_SET, 0, &rtp)) 464e172f0e5SSteve Price syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 4650bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4660bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 467e172f0e5SSteve Price } else { 46856c04344SDavid Nugent if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 46956c04344SDavid Nugent syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 4700bdc3ecfSEitan Adler pwd ? pwd->pw_name : "-", 4710bdc3ecfSEitan Adler lc ? lc->lc_class : LOGIN_DEFCLASS); 47268bbf3adSDavid Nugent } 473e172f0e5SSteve Price } 47468bbf3adSDavid Nugent 47556c04344SDavid Nugent /* Setup the user's group permissions */ 47656c04344SDavid Nugent if (flags & LOGIN_SETGROUP) { 47756c04344SDavid Nugent if (setgid(pwd->pw_gid) != 0) { 4789f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 47956c04344SDavid Nugent login_close(llc); 4802d057ca6SDag-Erling Smørgrav return (-1); 48156c04344SDavid Nugent } 48256c04344SDavid Nugent if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 4839f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 4849f3a9c3aSAndrey A. Chernov (u_long)pwd->pw_gid); 48556c04344SDavid Nugent login_close(llc); 4862d057ca6SDag-Erling Smørgrav return (-1); 48756c04344SDavid Nugent } 48856c04344SDavid Nugent } 48968bbf3adSDavid Nugent 49084333872SRobert Watson /* Set up the user's MAC label. */ 49184333872SRobert Watson if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 49284333872SRobert Watson const char *label_string; 49384333872SRobert Watson mac_t label; 49484333872SRobert Watson 49584333872SRobert Watson label_string = login_getcapstr(lc, "label", NULL, NULL); 49684333872SRobert Watson if (label_string != NULL) { 49784333872SRobert Watson if (mac_from_text(&label, label_string) == -1) { 49884333872SRobert Watson syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 49984333872SRobert Watson pwd->pw_name, label_string); 5002d057ca6SDag-Erling Smørgrav return (-1); 50184333872SRobert Watson } 50284333872SRobert Watson if (mac_set_proc(label) == -1) 50384333872SRobert Watson error = errno; 50484333872SRobert Watson else 50584333872SRobert Watson error = 0; 50684333872SRobert Watson mac_free(label); 50784333872SRobert Watson if (error != 0) { 50884333872SRobert Watson syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 50984333872SRobert Watson label_string, pwd->pw_name, strerror(error)); 5102d057ca6SDag-Erling Smørgrav return (-1); 51184333872SRobert Watson } 51284333872SRobert Watson } 51384333872SRobert Watson } 51484333872SRobert Watson 51556c04344SDavid Nugent /* Set the sessions login */ 51668bbf3adSDavid Nugent if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 51756c04344SDavid Nugent syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 51868bbf3adSDavid Nugent login_close(llc); 5192d057ca6SDag-Erling Smørgrav return (-1); 52068bbf3adSDavid Nugent } 52168bbf3adSDavid Nugent 5222bfc50bcSEdward Tomasz Napierala /* Inform the kernel about current login class */ 5232bfc50bcSEdward Tomasz Napierala if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) { 5242bfc50bcSEdward Tomasz Napierala /* 5252bfc50bcSEdward Tomasz Napierala * XXX: This is a workaround to fail gracefully in case the kernel 5262bfc50bcSEdward Tomasz Napierala * does not support setloginclass(2). 5272bfc50bcSEdward Tomasz Napierala */ 5282bfc50bcSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 5292bfc50bcSEdward Tomasz Napierala sa.sa_handler = SIG_IGN; 5302bfc50bcSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 5312bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &sa, &prevsa); 5322bfc50bcSEdward Tomasz Napierala error = setloginclass(lc->lc_class); 5332bfc50bcSEdward Tomasz Napierala sigaction(SIGSYS, &prevsa, NULL); 5342bfc50bcSEdward Tomasz Napierala if (error != 0) { 5352bfc50bcSEdward Tomasz Napierala syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class); 5362bfc50bcSEdward Tomasz Napierala #ifdef notyet 5372bfc50bcSEdward Tomasz Napierala login_close(llc); 5382bfc50bcSEdward Tomasz Napierala return (-1); 5392bfc50bcSEdward Tomasz Napierala #endif 5402bfc50bcSEdward Tomasz Napierala } 5412bfc50bcSEdward Tomasz Napierala } 5422bfc50bcSEdward Tomasz Napierala 54356c04344SDavid Nugent mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 54456c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 5451c594de5SDavid Nugent login_close(llc); 54656c04344SDavid Nugent 54756c04344SDavid Nugent /* This needs to be done after anything that needs root privs */ 54856c04344SDavid Nugent if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 5499f3a9c3aSAndrey A. Chernov syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 5502d057ca6SDag-Erling Smørgrav return (-1); /* Paranoia again */ 5511c594de5SDavid Nugent } 5521c594de5SDavid Nugent 55356c04344SDavid Nugent /* 55456c04344SDavid Nugent * Now, we repeat some of the above for the user's private entries 55556c04344SDavid Nugent */ 55635305a8dSDag-Erling Smørgrav if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) { 55756c04344SDavid Nugent mymask = setlogincontext(lc, pwd, mymask, flags); 55856c04344SDavid Nugent login_close(lc); 55956c04344SDavid Nugent } 56056c04344SDavid Nugent 56156c04344SDavid Nugent /* Finally, set any umask we've found */ 56256c04344SDavid Nugent if (flags & LOGIN_SETUMASK) 56356c04344SDavid Nugent umask(mymask); 56456c04344SDavid Nugent 5652d057ca6SDag-Erling Smørgrav return (0); 56668bbf3adSDavid Nugent } 567