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 { "mail", "MAIL", NULL, 1},
136 { "timezone", "TZ", NULL, 1},
137 { "term", "TERM", NULL, 0},
138 { NULL, NULL, NULL, 0}
142 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
153 for (q = var; *q != '\0'; ++q) {
154 tildes += (*q == '~');
155 dollas += (*q == '$');
159 np = malloc(strlen(var) + (dollas * nlen)
160 - dollas + (tildes * (pch+hlen))
168 * This loop does user username and homedir substitutions
169 * for unescaped $ (username) and ~ (homedir)
171 while (*(p += strcspn(p, "~$")) != '\0') {
174 if (p > np && *(p-1) == '\\') /* Escaped: */
175 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
176 else if (*p == '~') {
177 int v = pch && *(p+1) != '/'; /* Avoid double // */
178 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
179 memmove(p, pwd->pw_dir, hlen);
184 else /* if (*p == '$') */ {
185 memmove(p + nlen, p + 1, l); /* Subst username */
186 memmove(p, pwd->pw_name, nlen);
199 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
201 struct login_vars *vars = paths ? pathvars : envars;
202 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
203 int nlen = pwd ? strlen(pwd->pw_name) : 0;
206 if (hlen && pwd->pw_dir[hlen-1] != '/')
209 while (vars->tag != NULL) {
210 const char * var = paths ? login_getpath(lc, vars->tag, NULL)
211 : login_getcapstr(lc, vars->tag, NULL, NULL);
213 char * np = substvar(var, pwd, hlen, pch, nlen);
216 setenv(vars->var, np, vars->overwrite);
218 } else if (vars->def != NULL) {
219 setenv(vars->var, vars->def, 0);
225 * If we're not processing paths, then see if there is a setenv list by
226 * which the admin and/or user may set an arbitrary set of env vars.
229 const char **set_env = login_getcaplist(lc, "setenv", ",");
231 if (set_env != NULL) {
232 while (*set_env != NULL) {
233 char *p = strchr(*set_env, '=');
235 if (p != NULL) { /* Discard invalid entries */
239 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
240 setenv(*set_env, np, 1);
252 list2cpuset(const char *list, cpuset_t *mask)
254 enum { NONE, NUM, DASH } state;
260 curnum = lastnum = 0;
261 for (l = list; *l != '\0';) {
264 if (curnum > CPU_SETSIZE)
266 "Only %d cpus supported", CPU_SETSIZE);
275 for (; lastnum <= curnum; lastnum++)
276 CPU_SET(lastnum, mask);
291 CPU_SET(curnum, mask);
313 CPU_SET(curnum, mask);
323 setclasscpumask(login_cap_t *lc)
329 maskstr = login_getcapstr(lc, "cpumask", NULL, NULL);
333 if (strcasecmp("default", maskstr) == 0)
335 if (!list2cpuset(maskstr, &maskset)) {
337 "list2cpuset(%s) invalid mask specification", maskstr);
341 if (cpuset(&setid) != 0) {
342 syslog(LOG_ERR, "cpuset(): %s", strerror(errno));
346 if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
347 sizeof(maskset), &maskset) != 0)
348 syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr,
356 * For the login class <class>, set various class context values
357 * (limits, mainly) to the values for that class. Which values are
358 * set are controlled by <flags> -- see <login_class.h> for the
361 * setclasscontext() can only set resources, priority, and umask.
365 setclasscontext(const char *classname, unsigned int flags)
370 lc = login_getclassbyname(classname, NULL);
372 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
373 LOGIN_SETUMASK | LOGIN_SETPATH;
375 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
383 * Private function which takes care of processing
387 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
388 mode_t mymask, unsigned long flags)
392 if (flags & LOGIN_SETRESOURCES)
393 setclassresources(lc);
394 /* See if there's a umask override */
395 if (flags & LOGIN_SETUMASK)
396 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
398 if (flags & LOGIN_SETPATH)
399 setclassenvironment(lc, pwd, 1);
400 /* Set environment */
401 if (flags & LOGIN_SETENV)
402 setclassenvironment(lc, pwd, 0);
403 /* Set cpu affinity */
404 if (flags & LOGIN_SETCPUMASK)
415 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
416 * set the context as in setclasscontext(). <flags> controls which
419 * The difference between setclasscontext() and setusercontext() is
420 * that the former sets things up for an already-existing process,
421 * while the latter sets things up from a root context. Such as might
422 * be called from login(1).
427 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
431 login_cap_t *llc = NULL;
432 struct sigaction sa, prevsa;
437 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
438 llc = lc; /* free this when we're done */
441 if (flags & LOGIN_SETPATH)
442 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
444 /* we need a passwd entry to set these */
446 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
448 /* Set the process priority */
449 if (flags & LOGIN_SETPRIORITY) {
450 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
453 rtp.type = RTP_PRIO_IDLE;
455 rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p;
456 if (rtprio(RTP_SET, 0, &rtp))
457 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
458 pwd ? pwd->pw_name : "-",
459 lc ? lc->lc_class : LOGIN_DEFCLASS);
460 } else if (p < PRIO_MIN) {
461 rtp.type = RTP_PRIO_REALTIME;
462 p -= PRIO_MIN - RTP_PRIO_MAX;
463 rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : p;
464 if (rtprio(RTP_SET, 0, &rtp))
465 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
466 pwd ? pwd->pw_name : "-",
467 lc ? lc->lc_class : LOGIN_DEFCLASS);
469 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
470 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
471 pwd ? pwd->pw_name : "-",
472 lc ? lc->lc_class : LOGIN_DEFCLASS);
476 /* Setup the user's group permissions */
477 if (flags & LOGIN_SETGROUP) {
478 if (setgid(pwd->pw_gid) != 0) {
479 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
483 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
484 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
485 (u_long)pwd->pw_gid);
491 /* Set up the user's MAC label. */
492 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
493 const char *label_string;
496 label_string = login_getcapstr(lc, "label", NULL, NULL);
497 if (label_string != NULL) {
498 if (mac_from_text(&label, label_string) == -1) {
499 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
500 pwd->pw_name, label_string);
503 if (mac_set_proc(label) == -1)
509 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
510 label_string, pwd->pw_name, strerror(error));
516 /* Set the sessions login */
517 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
518 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
523 /* Inform the kernel about current login class */
524 if (lc != NULL && lc->lc_class != NULL && (flags & LOGIN_SETLOGINCLASS)) {
526 * XXX: This is a workaround to fail gracefully in case the kernel
527 * does not support setloginclass(2).
529 bzero(&sa, sizeof(sa));
530 sa.sa_handler = SIG_IGN;
531 sigfillset(&sa.sa_mask);
532 sigaction(SIGSYS, &sa, &prevsa);
533 error = setloginclass(lc->lc_class);
534 sigaction(SIGSYS, &prevsa, NULL);
536 syslog(LOG_ERR, "setloginclass(%s): %m", lc->lc_class);
544 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
545 mymask = setlogincontext(lc, pwd, mymask, flags);
548 /* This needs to be done after anything that needs root privs */
549 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
550 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
551 return (-1); /* Paranoia again */
555 * Now, we repeat some of the above for the user's private entries
557 if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) {
558 mymask = setlogincontext(lc, pwd, mymask, flags);
562 /* Finally, set any umask we've found */
563 if (flags & LOGIN_SETUMASK)