2 * Copyright (c) 1996 by
3 * Sean Eric Fagan <sef@kithrup.com>
4 * David Nugent <davidn@blaze.net.au>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, is permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. This work was done expressly for inclusion into FreeBSD. Other use
17 * is permitted provided this notation is included.
18 * 4. Absolutely no warranty of function or purpose is made by the authors.
19 * 5. Modifications may be freely made to this file providing the above
22 * High-level routines relating to use of the user capabilities database
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/cpuset.h>
31 #include <sys/resource.h>
32 #include <sys/rtprio.h>
40 #include <login_cap.h>
50 static struct login_res {
52 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t);
55 { "cputime", login_getcaptime, RLIMIT_CPU },
56 { "filesize", login_getcapsize, RLIMIT_FSIZE },
57 { "datasize", login_getcapsize, RLIMIT_DATA },
58 { "stacksize", login_getcapsize, RLIMIT_STACK },
59 { "memoryuse", login_getcapsize, RLIMIT_RSS },
60 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK },
61 { "maxproc", login_getcapnum, RLIMIT_NPROC },
62 { "openfiles", login_getcapnum, RLIMIT_NOFILE },
63 { "coredumpsize", login_getcapsize, RLIMIT_CORE },
64 { "sbsize", login_getcapsize, RLIMIT_SBSIZE },
65 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM },
66 { "pseudoterminals", login_getcapnum, RLIMIT_NPTS },
67 { "swapuse", login_getcapsize, RLIMIT_SWAP },
68 { "kqueues", login_getcapsize, RLIMIT_KQUEUES },
69 { "umtxp", login_getcapnum, RLIMIT_UMTXP },
75 setclassresources(login_cap_t *lc)
82 for (lr = resources; lr->what != NULL; ++lr) {
86 * The login.conf file can have <limit>, <limit>-max, and
87 * <limit>-cur entries.
88 * What we do is get the current current- and maximum- limits.
89 * Then, we try to get an entry for <limit> from the capability,
90 * using the current and max limits we just got as the
91 * default/error values.
92 * *Then*, we try looking for <limit>-cur and <limit>-max,
93 * again using the appropriate values as the default/error
97 if (getrlimit(lr->why, &rlim) != 0)
98 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
102 rlim_t rcur = rlim.rlim_cur;
103 rlim_t rmax = rlim.rlim_max;
105 sprintf(name_cur, "%s-cur", lr->what);
106 sprintf(name_max, "%s-max", lr->what);
108 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
109 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
110 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
111 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
113 if (setrlimit(lr->why, &rlim) == -1)
114 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
121 static struct login_vars {
127 { "path", "PATH", NULL, 1},
128 { "cdpath", "CDPATH", NULL, 1},
129 { "manpath", "MANPATH", NULL, 1},
130 { NULL, NULL, NULL, 0}
132 { "lang", "LANG", NULL, 1},
133 { "charset", "MM_CHARSET", NULL, 1},
134 { "mail", "MAIL", NULL, 1},
135 { "timezone", "TZ", NULL, 1},
136 { "term", "TERM", NULL, 0},
137 { NULL, NULL, NULL, 0}
141 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
152 for (q = var; *q != '\0'; ++q) {
153 tildes += (*q == '~');
154 dollas += (*q == '$');
158 np = malloc(strlen(var) + (dollas * nlen)
159 - dollas + (tildes * (pch+hlen))
167 * This loop does user username and homedir substitutions
168 * for unescaped $ (username) and ~ (homedir)
170 while (*(p += strcspn(p, "~$")) != '\0') {
173 if (p > np && *(p-1) == '\\') /* Escaped: */
174 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
175 else if (*p == '~') {
176 int v = pch && *(p+1) != '/'; /* Avoid double // */
177 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
178 memmove(p, pwd->pw_dir, hlen);
183 else /* if (*p == '$') */ {
184 memmove(p + nlen, p + 1, l); /* Subst username */
185 memmove(p, pwd->pw_name, nlen);
198 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
200 struct login_vars *vars = paths ? pathvars : envars;
201 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
202 int nlen = pwd ? strlen(pwd->pw_name) : 0;
205 if (hlen && pwd->pw_dir[hlen-1] != '/')
208 while (vars->tag != NULL) {
209 const char * var = paths ? login_getpath(lc, vars->tag, NULL)
210 : login_getcapstr(lc, vars->tag, NULL, NULL);
212 char * np = substvar(var, pwd, hlen, pch, nlen);
215 setenv(vars->var, np, vars->overwrite);
217 } else if (vars->def != NULL) {
218 setenv(vars->var, vars->def, 0);
224 * If we're not processing paths, then see if there is a setenv list by
225 * which the admin and/or user may set an arbitrary set of env vars.
228 const char **set_env = login_getcaplist(lc, "setenv", ",");
230 if (set_env != NULL) {
231 while (*set_env != NULL) {
232 char *p = strchr(*set_env, '=');
234 if (p != NULL && p != *set_env) { /* Discard invalid entries */
239 /* Strip leading spaces from variable name */
241 while (*ep == ' ' || *ep == '\t')
243 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
256 list2cpuset(const char *list, cpuset_t *mask)
258 enum { NONE, NUM, DASH } state;
264 curnum = lastnum = 0;
265 for (l = list; *l != '\0';) {
268 if (curnum > CPU_SETSIZE)
270 "Only %d cpus supported", CPU_SETSIZE);
279 for (; lastnum <= curnum; lastnum++)
280 CPU_SET(lastnum, mask);
295 CPU_SET(curnum, mask);
317 CPU_SET(curnum, mask);
327 setclasscpumask(login_cap_t *lc)
333 maskstr = login_getcapstr(lc, "cpumask", NULL, NULL);
337 if (strcasecmp("default", maskstr) == 0)
339 if (!list2cpuset(maskstr, &maskset)) {
341 "list2cpuset(%s) invalid mask specification", maskstr);
345 if (cpuset(&setid) != 0) {
346 syslog(LOG_ERR, "cpuset(): %s", strerror(errno));
350 if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
351 sizeof(maskset), &maskset) != 0)
352 syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr,
360 * For the login class <class>, set various class context values
361 * (limits, mainly) to the values for that class. Which values are
362 * set are controlled by <flags> -- see <login_class.h> for the
365 * setclasscontext() can only set resources, priority, and umask.
369 setclasscontext(const char *classname, unsigned int flags)
374 lc = login_getclassbyname(classname, NULL);
376 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
377 LOGIN_SETUMASK | LOGIN_SETPATH;
379 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
387 * Private function which takes care of processing
391 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
392 mode_t mymask, unsigned long flags)
396 if (flags & LOGIN_SETRESOURCES)
397 setclassresources(lc);
398 /* See if there's a umask override */
399 if (flags & LOGIN_SETUMASK)
400 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
402 if (flags & LOGIN_SETPATH)
403 setclassenvironment(lc, pwd, 1);
404 /* Set environment */
405 if (flags & LOGIN_SETENV)
406 setclassenvironment(lc, pwd, 0);
407 /* Set cpu affinity */
408 if (flags & LOGIN_SETCPUMASK)
419 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
420 * set the context as in setclasscontext(). <flags> controls which
423 * The difference between setclasscontext() and setusercontext() is
424 * that the former sets things up for an already-existing process,
425 * while the latter sets things up from a root context. Such as might
426 * be called from login(1).
431 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
435 login_cap_t *llc = NULL;
440 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
441 llc = lc; /* free this when we're done */
444 if (flags & LOGIN_SETPATH)
445 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
447 /* we need a passwd entry to set these */
449 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
451 /* Set the process priority */
452 if (flags & LOGIN_SETPRIORITY) {
453 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
456 rtp.type = RTP_PRIO_IDLE;
458 rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p;
459 if (rtprio(RTP_SET, 0, &rtp))
460 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
461 pwd ? pwd->pw_name : "-",
462 lc ? lc->lc_class : LOGIN_DEFCLASS);
463 } else if (p < PRIO_MIN) {
464 rtp.type = RTP_PRIO_REALTIME;
465 p -= PRIO_MIN - RTP_PRIO_MAX;
466 rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p;
467 if (rtprio(RTP_SET, 0, &rtp))
468 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
469 pwd ? pwd->pw_name : "-",
470 lc ? lc->lc_class : LOGIN_DEFCLASS);
472 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
473 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
474 pwd ? pwd->pw_name : "-",
475 lc ? lc->lc_class : LOGIN_DEFCLASS);
479 /* Setup the user's group permissions */
480 if (flags & LOGIN_SETGROUP) {
481 if (setgid(pwd->pw_gid) != 0) {
482 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
486 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
487 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
488 (u_long)pwd->pw_gid);
494 /* Set up the user's MAC label. */
495 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
496 const char *label_string;
499 label_string = login_getcapstr(lc, "label", NULL, NULL);
500 if (label_string != NULL) {
501 if (mac_from_text(&label, label_string) == -1) {
502 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
503 pwd->pw_name, label_string);
506 if (mac_set_proc(label) == -1)
512 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
513 label_string, pwd->pw_name, strerror(error));
519 /* Set the sessions login */
520 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
521 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
526 /* Inform the kernel about current login class */
527 if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) {
528 error = setloginclass(lc->lc_class);
530 syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class);
538 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
539 mymask = setlogincontext(lc, pwd, mymask, flags);
542 /* This needs to be done after anything that needs root privs */
543 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
544 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
545 return (-1); /* Paranoia again */
549 * Now, we repeat some of the above for the user's private entries
551 if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) {
552 mymask = setlogincontext(lc, pwd, mymask, flags);
556 /* Finally, set any umask we've found */
557 if (flags & LOGIN_SETUMASK)