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 },
73 setclassresources(login_cap_t *lc)
80 for (lr = resources; lr->what != NULL; ++lr) {
84 * The login.conf file can have <limit>, <limit>-max, and
85 * <limit>-cur entries.
86 * What we do is get the current current- and maximum- limits.
87 * Then, we try to get an entry for <limit> from the capability,
88 * using the current and max limits we just got as the
89 * default/error values.
90 * *Then*, we try looking for <limit>-cur and <limit>-max,
91 * again using the appropriate values as the default/error
95 if (getrlimit(lr->why, &rlim) != 0)
96 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
100 rlim_t rcur = rlim.rlim_cur;
101 rlim_t rmax = rlim.rlim_max;
103 sprintf(name_cur, "%s-cur", lr->what);
104 sprintf(name_max, "%s-max", lr->what);
106 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
107 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
108 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
109 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
111 if (setrlimit(lr->why, &rlim) == -1)
112 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
119 static struct login_vars {
125 { "path", "PATH", NULL, 1},
126 { "cdpath", "CDPATH", NULL, 1},
127 { "manpath", "MANPATH", NULL, 1},
128 { NULL, NULL, NULL, 0}
130 { "lang", "LANG", NULL, 1},
131 { "charset", "MM_CHARSET", NULL, 1},
132 { "timezone", "TZ", NULL, 1},
133 { "term", "TERM", NULL, 0},
134 { NULL, NULL, NULL, 0}
138 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
149 for (q = var; *q != '\0'; ++q) {
150 tildes += (*q == '~');
151 dollas += (*q == '$');
155 np = malloc(strlen(var) + (dollas * nlen)
156 - dollas + (tildes * (pch+hlen))
164 * This loop does user username and homedir substitutions
165 * for unescaped $ (username) and ~ (homedir)
167 while (*(p += strcspn(p, "~$")) != '\0') {
170 if (p > np && *(p-1) == '\\') /* Escaped: */
171 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
172 else if (*p == '~') {
173 int v = pch && *(p+1) != '/'; /* Avoid double // */
174 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
175 memmove(p, pwd->pw_dir, hlen);
180 else /* if (*p == '$') */ {
181 memmove(p + nlen, p + 1, l); /* Subst username */
182 memmove(p, pwd->pw_name, nlen);
195 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
197 struct login_vars *vars = paths ? pathvars : envars;
198 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
199 int nlen = pwd ? strlen(pwd->pw_name) : 0;
202 if (hlen && pwd->pw_dir[hlen-1] != '/')
205 while (vars->tag != NULL) {
206 const char * var = paths ? login_getpath(lc, vars->tag, NULL)
207 : login_getcapstr(lc, vars->tag, NULL, NULL);
209 char * np = substvar(var, pwd, hlen, pch, nlen);
212 setenv(vars->var, np, vars->overwrite);
214 } else if (vars->def != NULL) {
215 setenv(vars->var, vars->def, 0);
221 * If we're not processing paths, then see if there is a setenv list by
222 * which the admin and/or user may set an arbitrary set of env vars.
225 const char **set_env = login_getcaplist(lc, "setenv", ",");
227 if (set_env != NULL) {
228 while (*set_env != NULL) {
229 char *p = strchr(*set_env, '=');
231 if (p != NULL) { /* Discard invalid entries */
235 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
236 setenv(*set_env, np, 1);
248 list2cpuset(const char *list, cpuset_t *mask)
250 enum { NONE, NUM, DASH } state;
256 curnum = lastnum = 0;
257 for (l = list; *l != '\0';) {
260 if (curnum > CPU_SETSIZE)
262 "Only %d cpus supported", CPU_SETSIZE);
271 for (; lastnum <= curnum; lastnum++)
272 CPU_SET(lastnum, mask);
287 CPU_SET(curnum, mask);
309 CPU_SET(curnum, mask);
319 setclasscpumask(login_cap_t *lc)
325 maskstr = login_getcapstr(lc, "cpumask", NULL, NULL);
329 if (strcasecmp("default", maskstr) == 0)
331 if (!list2cpuset(maskstr, &maskset)) {
333 "list2cpuset(%s) invalid mask specification", maskstr);
337 if (cpuset(&setid) != 0) {
338 syslog(LOG_ERR, "cpuset(): %s", strerror(errno));
342 if (cpuset_setaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
343 sizeof(maskset), &maskset) != 0)
344 syslog(LOG_ERR, "cpuset_setaffinity(%s): %s", maskstr,
352 * For the login class <class>, set various class context values
353 * (limits, mainly) to the values for that class. Which values are
354 * set are controlled by <flags> -- see <login_class.h> for the
357 * setclasscontext() can only set resources, priority, and umask.
361 setclasscontext(const char *classname, unsigned int flags)
366 lc = login_getclassbyname(classname, NULL);
368 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
369 LOGIN_SETUMASK | LOGIN_SETPATH;
371 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
379 * Private function which takes care of processing
383 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
384 mode_t mymask, unsigned long flags)
388 if (flags & LOGIN_SETRESOURCES)
389 setclassresources(lc);
390 /* See if there's a umask override */
391 if (flags & LOGIN_SETUMASK)
392 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
394 if (flags & LOGIN_SETPATH)
395 setclassenvironment(lc, pwd, 1);
396 /* Set environment */
397 if (flags & LOGIN_SETENV)
398 setclassenvironment(lc, pwd, 0);
399 /* Set cpu affinity */
400 if (flags & LOGIN_SETCPUMASK)
411 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
412 * set the context as in setclasscontext(). <flags> controls which
415 * The difference between setclasscontext() and setusercontext() is
416 * that the former sets things up for an already-existing process,
417 * while the latter sets things up from a root context. Such as might
418 * be called from login(1).
423 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
427 login_cap_t *llc = NULL;
432 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
433 llc = lc; /* free this when we're done */
436 if (flags & LOGIN_SETPATH)
437 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
439 /* we need a passwd entry to set these */
441 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC);
443 /* Set the process priority */
444 if (flags & LOGIN_SETPRIORITY) {
445 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
448 rtp.type = RTP_PRIO_IDLE;
450 rtp.prio = p > RTP_PRIO_MAX ? RTP_PRIO_MAX : p;
451 if (rtprio(RTP_SET, 0, &rtp))
452 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
453 pwd ? pwd->pw_name : "-",
454 lc ? lc->lc_class : LOGIN_DEFCLASS);
455 } else if (p < PRIO_MIN) {
456 rtp.type = RTP_PRIO_REALTIME;
457 p -= PRIO_MIN - RTP_PRIO_MAX;
458 rtp.prio = p < RTP_PRIO_MIN ? RTP_PRIO_MIN : 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);
464 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
465 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
466 pwd ? pwd->pw_name : "-",
467 lc ? lc->lc_class : LOGIN_DEFCLASS);
471 /* Setup the user's group permissions */
472 if (flags & LOGIN_SETGROUP) {
473 if (setgid(pwd->pw_gid) != 0) {
474 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
478 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
479 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
480 (u_long)pwd->pw_gid);
486 /* Set up the user's MAC label. */
487 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
488 const char *label_string;
491 label_string = login_getcapstr(lc, "label", NULL, NULL);
492 if (label_string != NULL) {
493 if (mac_from_text(&label, label_string) == -1) {
494 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
495 pwd->pw_name, label_string);
498 if (mac_set_proc(label) == -1)
504 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
505 label_string, pwd->pw_name, strerror(error));
511 /* Set the sessions login */
512 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
513 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
518 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
519 mymask = setlogincontext(lc, pwd, mymask, flags);
522 /* This needs to be done after anything that needs root privs */
523 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
524 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
525 return (-1); /* Paranoia again */
529 * Now, we repeat some of the above for the user's private entries
531 if (getuid() == uid && (lc = login_getuserclass(pwd)) != NULL) {
532 mymask = setlogincontext(lc, pwd, mymask, flags);
536 /* Finally, set any umask we've found */
537 if (flags & LOGIN_SETUMASK)