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
24 * $FreeBSD: src/lib/libutil/login_class.c,v 1.14.2.3 2002/08/06 07:07:52 ache Exp $
25 * $DragonFly: src/lib/libutil/login_class.c,v 1.3 2004/04/22 16:52:55 dillon Exp $
33 #include <sys/types.h>
36 #include <sys/resource.h>
40 #include <login_cap.h>
42 #include <sys/rtprio.h>
45 static struct login_res {
47 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t);
50 { "cputime", login_getcaptime, RLIMIT_CPU },
51 { "filesize", login_getcapsize, RLIMIT_FSIZE },
52 { "datasize", login_getcapsize, RLIMIT_DATA },
53 { "stacksize", login_getcapsize, RLIMIT_STACK },
54 { "memoryuse", login_getcapsize, RLIMIT_RSS },
55 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK },
56 { "maxproc", login_getcapnum, RLIMIT_NPROC },
57 { "openfiles", login_getcapnum, RLIMIT_NOFILE },
58 { "coredumpsize", login_getcapsize, RLIMIT_CORE },
59 { "sbsize", login_getcapsize, RLIMIT_SBSIZE },
60 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM },
61 #ifdef RLIMIT_POSIXLOCKS
62 { "posixlocks", login_getcapnum, RLIMIT_POSIXLOCKS },
69 setclassresources(login_cap_t *lc)
76 for (lr = resources; lr->what != NULL; ++lr) {
80 * The login.conf file can have <limit>, <limit>-max, and
81 * <limit>-cur entries.
82 * What we do is get the current current- and maximum- limits.
83 * Then, we try to get an entry for <limit> from the capability,
84 * using the current and max limits we just got as the
85 * default/error values.
86 * *Then*, we try looking for <limit>-cur and <limit>-max,
87 * again using the appropriate values as the default/error
91 if (getrlimit(lr->why, &rlim) != 0)
92 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
96 rlim_t rcur = rlim.rlim_cur;
97 rlim_t rmax = rlim.rlim_max;
99 sprintf(name_cur, "%s-cur", lr->what);
100 sprintf(name_max, "%s-max", lr->what);
102 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
103 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
104 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
105 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
107 if (setrlimit(lr->why, &rlim) == -1)
108 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
115 static struct login_vars {
121 { "path", "PATH", NULL, 1},
122 { "cdpath", "CDPATH", NULL, 1},
123 { "manpath", "MANPATH", NULL, 1},
124 { NULL, NULL, NULL, 0}
126 { "lang", "LANG", NULL, 1},
127 { "charset", "MM_CHARSET", NULL, 1},
128 { "timezone", "TZ", NULL, 1},
129 { "term", "TERM", NULL, 0},
130 { NULL, NULL, NULL, 0}
134 substvar(char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
144 /* Count the number of ~'s in var to substitute */
146 for (p = var; (p = strchr(p, '~')) != NULL; p++)
148 /* Count the number of $'s in var to substitute */
150 for (p = var; (p = strchr(p, '$')) != NULL; p++)
154 np = malloc(strlen(var) + (dollas * nlen)
155 - dollas + (tildes * (pch+hlen))
163 * This loop does user username and homedir substitutions
164 * for unescaped $ (username) and ~ (homedir)
166 while (*(p += strcspn(p, "~$")) != '\0') {
169 if (p > np && *(p-1) == '\\') /* Escaped: */
170 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
171 else if (*p == '~') {
172 int v = pch && *(p+1) != '/'; /* Avoid double // */
173 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
174 memmove(p, pwd->pw_dir, hlen);
179 else /* if (*p == '$') */ {
180 memmove(p + nlen, p + 1, l); /* Subst username */
181 memmove(p, pwd->pw_name, nlen);
194 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
196 struct login_vars *vars = paths ? pathvars : envars;
197 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
198 int nlen = pwd ? strlen(pwd->pw_name) : 0;
201 if (hlen && pwd->pw_dir[hlen-1] != '/')
204 while (vars->tag != NULL) {
205 char * var = paths ? login_getpath(lc, vars->tag, NULL)
206 : login_getcapstr(lc, vars->tag, NULL, NULL);
208 char * np = substvar(var, pwd, hlen, pch, nlen);
211 setenv(vars->var, np, vars->overwrite);
213 } else if (vars->def != NULL) {
214 setenv(vars->var, vars->def, 0);
220 * If we're not processing paths, then see if there is a setenv list by
221 * which the admin and/or user may set an arbitrary set of env vars.
224 char **set_env = login_getcaplist(lc, "setenv", ",");
226 if (set_env != NULL) {
227 while (*set_env != NULL) {
228 char *p = strchr(*set_env, '=');
230 if (p != NULL) { /* Discard invalid entries */
234 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
235 setenv(*set_env, np, 1);
249 * For the login class <class>, set various class context values
250 * (limits, mainly) to the values for that class. Which values are
251 * set are controlled by <flags> -- see <login_class.h> for the
254 * setclasscontext() can only set resources, priority, and umask.
258 setclasscontext(const char *classname, unsigned int flags)
263 lc = login_getclassbyname(classname, NULL);
265 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
266 LOGIN_SETUMASK | LOGIN_SETPATH;
268 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
276 * Private functionw which takes care of processing
280 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
281 mode_t mymask, unsigned long flags)
285 if (flags & LOGIN_SETRESOURCES)
286 setclassresources(lc);
287 /* See if there's a umask override */
288 if (flags & LOGIN_SETUMASK)
289 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
291 if (flags & LOGIN_SETPATH)
292 setclassenvironment(lc, pwd, 1);
293 /* Set environment */
294 if (flags & LOGIN_SETENV)
295 setclassenvironment(lc, pwd, 0);
305 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
306 * set the context as in setclasscontext(). <flags> controls which
309 * The difference between setclasscontext() and setusercontext() is
310 * that the former sets things up for an already-existing process,
311 * while the latter sets things up from a root context. Such as might
312 * be called from login(1).
317 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
321 login_cap_t *llc = NULL;
322 #ifndef __NETBSD_SYSCALLS
327 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
328 llc = lc; /* free this when we're done */
331 if (flags & LOGIN_SETPATH)
332 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
334 /* we need a passwd entry to set these */
336 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN);
338 /* Set the process priority */
339 if (flags & LOGIN_SETPRIORITY) {
340 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
343 #ifndef __NETBSD_SYSCALLS
344 rtp.type = RTP_PRIO_IDLE;
345 rtp.prio = p - PRIO_MAX - 1;
346 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p;
347 if(rtprio(RTP_SET, 0, &rtp))
348 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
349 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
351 } else if(p < PRIO_MIN) {
352 #ifndef __NETBSD_SYSCALLS
353 rtp.type = RTP_PRIO_REALTIME;
354 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX);
355 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p;
356 if(rtprio(RTP_SET, 0, &rtp))
357 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
358 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
361 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
362 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
363 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
367 /* Setup the user's group permissions */
368 if (flags & LOGIN_SETGROUP) {
369 if (setgid(pwd->pw_gid) != 0) {
370 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
374 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
375 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
376 (u_long)pwd->pw_gid);
382 /* Set the sessions login */
383 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
384 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
389 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
390 mymask = setlogincontext(lc, pwd, mymask, flags);
393 /* This needs to be done after anything that needs root privs */
394 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
395 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
396 return -1; /* Paranoia again */
400 * Now, we repeat some of the above for the user's private entries
402 if ((lc = login_getuserclass(pwd)) != NULL) {
403 mymask = setlogincontext(lc, pwd, mymask, flags);
407 /* Finally, set any umask we've found */
408 if (flags & LOGIN_SETUMASK)