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 },
72 setclassresources(login_cap_t *lc)
79 for (lr = resources; lr->what != NULL; ++lr) {
83 * The login.conf file can have <limit>, <limit>-max, and
84 * <limit>-cur entries.
85 * What we do is get the current current- and maximum- limits.
86 * Then, we try to get an entry for <limit> from the capability,
87 * using the current and max limits we just got as the
88 * default/error values.
89 * *Then*, we try looking for <limit>-cur and <limit>-max,
90 * again using the appropriate values as the default/error
94 if (getrlimit(lr->why, &rlim) != 0)
95 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
99 rlim_t rcur = rlim.rlim_cur;
100 rlim_t rmax = rlim.rlim_max;
102 sprintf(name_cur, "%s-cur", lr->what);
103 sprintf(name_max, "%s-max", lr->what);
105 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
106 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
107 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
108 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
110 if (setrlimit(lr->why, &rlim) == -1)
111 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
118 static struct login_vars {
124 { "path", "PATH", NULL, 1},
125 { "cdpath", "CDPATH", NULL, 1},
126 { "manpath", "MANPATH", NULL, 1},
127 { NULL, NULL, NULL, 0}
129 { "lang", "LANG", NULL, 1},
130 { "charset", "MM_CHARSET", NULL, 1},
131 { "timezone", "TZ", NULL, 1},
132 { "term", "TERM", NULL, 0},
133 { NULL, NULL, NULL, 0}
137 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
147 /* Count the number of ~'s in var to substitute */
148 for (p = (char *)var; (p = strchr(p, '~')) != NULL; p++)
150 /* Count the number of $'s in var to substitute */
151 for (p = (char *)var; (p = strchr(p, '$')) != NULL; p++)
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;
449 rtp.prio = p - PRIO_MAX - 1;
450 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p;
451 if (rtprio(RTP_SET, 0, &rtp))
452 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
453 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
454 } else if (p < PRIO_MIN) {
455 rtp.type = RTP_PRIO_REALTIME;
456 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX);
457 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p;
458 if (rtprio(RTP_SET, 0, &rtp))
459 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
460 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
462 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
463 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
464 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
468 /* Setup the user's group permissions */
469 if (flags & LOGIN_SETGROUP) {
470 if (setgid(pwd->pw_gid) != 0) {
471 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
475 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
476 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
477 (u_long)pwd->pw_gid);
483 /* Set up the user's MAC label. */
484 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) {
485 const char *label_string;
488 label_string = login_getcapstr(lc, "label", NULL, NULL);
489 if (label_string != NULL) {
490 if (mac_from_text(&label, label_string) == -1) {
491 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m",
492 pwd->pw_name, label_string);
495 if (mac_set_proc(label) == -1)
501 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s",
502 label_string, pwd->pw_name, strerror(error));
508 /* Set the sessions login */
509 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
510 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
515 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
516 mymask = setlogincontext(lc, pwd, mymask, flags);
519 /* This needs to be done after anything that needs root privs */
520 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
521 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
522 return (-1); /* Paranoia again */
526 * Now, we repeat some of the above for the user's private entries
528 if ((lc = login_getuserclass(pwd)) != NULL) {
529 mymask = setlogincontext(lc, pwd, mymask, flags);
533 /* Finally, set any umask we've found */
534 if (flags & LOGIN_SETUMASK)