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.2 2003/06/17 04:26:52 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 },
66 setclassresources(login_cap_t *lc)
73 for (lr = resources; lr->what != NULL; ++lr) {
77 * The login.conf file can have <limit>, <limit>-max, and
78 * <limit>-cur entries.
79 * What we do is get the current current- and maximum- limits.
80 * Then, we try to get an entry for <limit> from the capability,
81 * using the current and max limits we just got as the
82 * default/error values.
83 * *Then*, we try looking for <limit>-cur and <limit>-max,
84 * again using the appropriate values as the default/error
88 if (getrlimit(lr->why, &rlim) != 0)
89 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
93 rlim_t rcur = rlim.rlim_cur;
94 rlim_t rmax = rlim.rlim_max;
96 sprintf(name_cur, "%s-cur", lr->what);
97 sprintf(name_max, "%s-max", lr->what);
99 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
100 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
101 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
102 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
104 if (setrlimit(lr->why, &rlim) == -1)
105 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
112 static struct login_vars {
118 { "path", "PATH", NULL, 1},
119 { "cdpath", "CDPATH", NULL, 1},
120 { "manpath", "MANPATH", NULL, 1},
121 { NULL, NULL, NULL, 0}
123 { "lang", "LANG", NULL, 1},
124 { "charset", "MM_CHARSET", NULL, 1},
125 { "timezone", "TZ", NULL, 1},
126 { "term", "TERM", NULL, 0},
127 { NULL, NULL, NULL, 0}
131 substvar(char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
141 /* Count the number of ~'s in var to substitute */
143 for (p = var; (p = strchr(p, '~')) != NULL; p++)
145 /* Count the number of $'s in var to substitute */
147 for (p = var; (p = strchr(p, '$')) != NULL; p++)
151 np = malloc(strlen(var) + (dollas * nlen)
152 - dollas + (tildes * (pch+hlen))
160 * This loop does user username and homedir substitutions
161 * for unescaped $ (username) and ~ (homedir)
163 while (*(p += strcspn(p, "~$")) != '\0') {
166 if (p > np && *(p-1) == '\\') /* Escaped: */
167 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
168 else if (*p == '~') {
169 int v = pch && *(p+1) != '/'; /* Avoid double // */
170 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
171 memmove(p, pwd->pw_dir, hlen);
176 else /* if (*p == '$') */ {
177 memmove(p + nlen, p + 1, l); /* Subst username */
178 memmove(p, pwd->pw_name, nlen);
191 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
193 struct login_vars *vars = paths ? pathvars : envars;
194 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
195 int nlen = pwd ? strlen(pwd->pw_name) : 0;
198 if (hlen && pwd->pw_dir[hlen-1] != '/')
201 while (vars->tag != NULL) {
202 char * var = paths ? login_getpath(lc, vars->tag, NULL)
203 : login_getcapstr(lc, vars->tag, NULL, NULL);
205 char * np = substvar(var, pwd, hlen, pch, nlen);
208 setenv(vars->var, np, vars->overwrite);
210 } else if (vars->def != NULL) {
211 setenv(vars->var, vars->def, 0);
217 * If we're not processing paths, then see if there is a setenv list by
218 * which the admin and/or user may set an arbitrary set of env vars.
221 char **set_env = login_getcaplist(lc, "setenv", ",");
223 if (set_env != NULL) {
224 while (*set_env != NULL) {
225 char *p = strchr(*set_env, '=');
227 if (p != NULL) { /* Discard invalid entries */
231 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
232 setenv(*set_env, np, 1);
246 * For the login class <class>, set various class context values
247 * (limits, mainly) to the values for that class. Which values are
248 * set are controlled by <flags> -- see <login_class.h> for the
251 * setclasscontext() can only set resources, priority, and umask.
255 setclasscontext(const char *classname, unsigned int flags)
260 lc = login_getclassbyname(classname, NULL);
262 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
263 LOGIN_SETUMASK | LOGIN_SETPATH;
265 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
273 * Private functionw which takes care of processing
277 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
278 mode_t mymask, unsigned long flags)
282 if (flags & LOGIN_SETRESOURCES)
283 setclassresources(lc);
284 /* See if there's a umask override */
285 if (flags & LOGIN_SETUMASK)
286 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
288 if (flags & LOGIN_SETPATH)
289 setclassenvironment(lc, pwd, 1);
290 /* Set environment */
291 if (flags & LOGIN_SETENV)
292 setclassenvironment(lc, pwd, 0);
302 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
303 * set the context as in setclasscontext(). <flags> controls which
306 * The difference between setclasscontext() and setusercontext() is
307 * that the former sets things up for an already-existing process,
308 * while the latter sets things up from a root context. Such as might
309 * be called from login(1).
314 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
318 login_cap_t *llc = NULL;
319 #ifndef __NETBSD_SYSCALLS
324 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
325 llc = lc; /* free this when we're done */
328 if (flags & LOGIN_SETPATH)
329 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
331 /* we need a passwd entry to set these */
333 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN);
335 /* Set the process priority */
336 if (flags & LOGIN_SETPRIORITY) {
337 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
340 #ifndef __NETBSD_SYSCALLS
341 rtp.type = RTP_PRIO_IDLE;
342 rtp.prio = p - PRIO_MAX - 1;
343 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p;
344 if(rtprio(RTP_SET, 0, &rtp))
345 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
346 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
348 } else if(p < PRIO_MIN) {
349 #ifndef __NETBSD_SYSCALLS
350 rtp.type = RTP_PRIO_REALTIME;
351 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX);
352 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p;
353 if(rtprio(RTP_SET, 0, &rtp))
354 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
355 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
358 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
359 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
360 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
364 /* Setup the user's group permissions */
365 if (flags & LOGIN_SETGROUP) {
366 if (setgid(pwd->pw_gid) != 0) {
367 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
371 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
372 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
373 (u_long)pwd->pw_gid);
379 /* Set the sessions login */
380 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
381 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
386 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
387 mymask = setlogincontext(lc, pwd, mymask, flags);
390 /* This needs to be done after anything that needs root privs */
391 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
392 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
393 return -1; /* Paranoia again */
397 * Now, we repeat some of the above for the user's private entries
399 if ((lc = login_getuserclass(pwd)) != NULL) {
400 mymask = setlogincontext(lc, pwd, mymask, flags);
404 /* Finally, set any umask we've found */
405 if (flags & LOGIN_SETUMASK)