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>
51 static struct login_res {
53 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t);
56 { "cputime", login_getcaptime, RLIMIT_CPU },
57 { "filesize", login_getcapsize, RLIMIT_FSIZE },
58 { "datasize", login_getcapsize, RLIMIT_DATA },
59 { "stacksize", login_getcapsize, RLIMIT_STACK },
60 { "memoryuse", login_getcapsize, RLIMIT_RSS },
61 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK },
62 { "maxproc", login_getcapnum, RLIMIT_NPROC },
63 { "openfiles", login_getcapnum, RLIMIT_NOFILE },
64 { "coredumpsize", login_getcapsize, RLIMIT_CORE },
65 { "sbsize", login_getcapsize, RLIMIT_SBSIZE },
66 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM },
67 { "pseudoterminals", login_getcapnum, RLIMIT_NPTS },
68 { "swapuse", login_getcapsize, RLIMIT_SWAP },
69 { "kqueues", login_getcapsize, RLIMIT_KQUEUES },
70 { "umtxp", login_getcapnum, RLIMIT_UMTXP },
76 setclassresources(login_cap_t *lc)
83 for (lr = resources; lr->what != NULL; ++lr) {
87 * The login.conf file can have <limit>, <limit>-max, and
88 * <limit>-cur entries.
89 * What we do is get the current current- and maximum- limits.
90 * Then, we try to get an entry for <limit> from the capability,
91 * using the current and max limits we just got as the
92 * default/error values.
93 * *Then*, we try looking for <limit>-cur and <limit>-max,
94 * again using the appropriate values as the default/error
98 if (getrlimit(lr->why, &rlim) != 0)
99 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
103 rlim_t rcur = rlim.rlim_cur;
104 rlim_t rmax = rlim.rlim_max;
106 sprintf(name_cur, "%s-cur", lr->what);
107 sprintf(name_max, "%s-max", lr->what);
109 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
110 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
111 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
112 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
114 if (setrlimit(lr->why, &rlim) == -1)
115 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
122 static struct login_vars {
128 { "path", "PATH", NULL, 1},
129 { "cdpath", "CDPATH", NULL, 1},
130 { "manpath", "MANPATH", NULL, 1},
131 { NULL, NULL, NULL, 0}
133 { "lang", "LANG", NULL, 1},
134 { "charset", "MM_CHARSET", 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) { /* Discard invalid entries */
238 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
239 setenv(*set_env, np, 1);
251 list2cpuset(const char *list, cpuset_t *mask)
253 enum { NONE, NUM, DASH } state;
259 curnum = lastnum = 0;
260 for (l = list; *l != '\0';) {
263 if (curnum > CPU_SETSIZE)
265 "Only %d cpus supported", CPU_SETSIZE);
274 for (; lastnum <= curnum; lastnum++)
275 CPU_SET(lastnum, mask);
290 CPU_SET(curnum, mask);
312 CPU_SET(curnum, mask);
322 setclasscpumask(login_cap_t *lc)
328 maskstr = login_getcapstr(lc, "cpumask", NULL, NULL);
332 if (strcasecmp("default", maskstr) == 0)
334 if (!list2cpuset(maskstr, &maskset)) {
336 "list2cpuset(%s) invalid mask specification", maskstr);
340 if (cpuset(&setid) != 0) {
341 syslog(LOG_ERR, "cpuset(): %s", strerror(errno));
345 if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
346 sizeof(maskset), &maskset) != 0)
347 syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr,
355 * For the login class <class>, set various class context values
356 * (limits, mainly) to the values for that class. Which values are
357 * set are controlled by <flags> -- see <login_class.h> for the
360 * setclasscontext() can only set resources, priority, and umask.
364 setclasscontext(const char *classname, unsigned int flags)
369 lc = login_getclassbyname(classname, NULL);
371 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
372 LOGIN_SETUMASK | LOGIN_SETPATH;
374 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
382 * Private function which takes care of processing
386 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
387 mode_t mymask, unsigned long flags)
391 if (flags & LOGIN_SETRESOURCES)
392 setclassresources(lc);
393 /* See if there's a umask override */
394 if (flags & LOGIN_SETUMASK)
395 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
397 if (flags & LOGIN_SETPATH)
398 setclassenvironment(lc, pwd, 1);
399 /* Set environment */
400 if (flags & LOGIN_SETENV)
401 setclassenvironment(lc, pwd, 0);
402 /* Set cpu affinity */
403 if (flags & LOGIN_SETCPUMASK)
414 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
415 * set the context as in setclasscontext(). <flags> controls which
418 * The difference between setclasscontext() and setusercontext() is
419 * that the former sets things up for an already-existing process,
420 * while the latter sets things up from a root context. Such as might
421 * be called from login(1).
426 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
430 login_cap_t *llc = NULL;
431 struct sigaction sa, prevsa;
436 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
437 llc = lc; /* free this when we're done */
440 if (flags & LOGIN_SETPATH)
441 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
443 /* we need a passwd entry to set these */
445 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
447 /* Set the process priority */
448 if (flags & LOGIN_SETPRIORITY) {
449 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
452 rtp.type = RTP_PRIO_IDLE;
454 rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p;
455 if (rtprio(RTP_SET, 0, &rtp))
456 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
457 pwd ? pwd->pw_name : "-",
458 lc ? lc->lc_class : LOGIN_DEFCLASS);
459 } else if (p < PRIO_MIN) {
460 rtp.type = RTP_PRIO_REALTIME;
461 p -= PRIO_MIN - RTP_PRIO_MAX;
462 rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p;
463 if (rtprio(RTP_SET, 0, &rtp))
464 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
465 pwd ? pwd->pw_name : "-",
466 lc ? lc->lc_class : LOGIN_DEFCLASS);
468 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
469 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
470 pwd ? pwd->pw_name : "-",
471 lc ? lc->lc_class : LOGIN_DEFCLASS);
475 /* Setup the user's group permissions */
476 if (flags & LOGIN_SETGROUP) {
477 if (setgid(pwd->pw_gid) != 0) {
478 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
482 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
483 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
484 (u_long)pwd->pw_gid);
490 /* Set up the user's MAC label. */
491 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
492 const char *label_string;
495 label_string = login_getcapstr(lc, "label", NULL, NULL);
496 if (label_string != NULL) {
497 if (mac_from_text(&label, label_string) == -1) {
498 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
499 pwd->pw_name, label_string);
502 if (mac_set_proc(label) == -1)
508 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
509 label_string, pwd->pw_name, strerror(error));
515 /* Set the sessions login */
516 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
517 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
522 /* Inform the kernel about current login class */
523 if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) {
525 * XXX: This is a workaround to fail gracefully in case the kernel
526 * does not support setloginclass(2).
528 bzero(&sa, sizeof(sa));
529 sa.sa_handler = SIG_IGN;
530 sigfillset(&sa.sa_mask);
531 sigaction(SIGSYS, &sa, &prevsa);
532 error = setloginclass(lc->lc_class);
533 sigaction(SIGSYS, &prevsa, NULL);
535 syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class);
543 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
544 mymask = setlogincontext(lc, pwd, mymask, flags);
547 /* This needs to be done after anything that needs root privs */
548 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
549 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
550 return (-1); /* Paranoia again */
554 * Now, we repeat some of the above for the user's private entries
556 if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) {
557 mymask = setlogincontext(lc, pwd, mymask, flags);
561 /* Finally, set any umask we've found */
562 if (flags & LOGIN_SETUMASK)