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 $
32 #include <sys/types.h>
35 #include <sys/resource.h>
39 #include <login_cap.h>
41 #include <sys/rtprio.h>
44 static struct login_res {
46 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t);
49 { "cputime", login_getcaptime, RLIMIT_CPU },
50 { "filesize", login_getcapsize, RLIMIT_FSIZE },
51 { "datasize", login_getcapsize, RLIMIT_DATA },
52 { "stacksize", login_getcapsize, RLIMIT_STACK },
53 { "memoryuse", login_getcapsize, RLIMIT_RSS },
54 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK },
55 { "maxproc", login_getcapnum, RLIMIT_NPROC },
56 { "openfiles", login_getcapnum, RLIMIT_NOFILE },
57 { "coredumpsize", login_getcapsize, RLIMIT_CORE },
58 { "sbsize", login_getcapsize, RLIMIT_SBSIZE },
59 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM },
65 setclassresources(login_cap_t *lc)
72 for (lr = resources; lr->what != NULL; ++lr) {
76 * The login.conf file can have <limit>, <limit>-max, and
77 * <limit>-cur entries.
78 * What we do is get the current current- and maximum- limits.
79 * Then, we try to get an entry for <limit> from the capability,
80 * using the current and max limits we just got as the
81 * default/error values.
82 * *Then*, we try looking for <limit>-cur and <limit>-max,
83 * again using the appropriate values as the default/error
87 if (getrlimit(lr->why, &rlim) != 0)
88 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what);
92 rlim_t rcur = rlim.rlim_cur;
93 rlim_t rmax = rlim.rlim_max;
95 sprintf(name_cur, "%s-cur", lr->what);
96 sprintf(name_max, "%s-max", lr->what);
98 rcur = (*lr->who)(lc, lr->what, rcur, rcur);
99 rmax = (*lr->who)(lc, lr->what, rmax, rmax);
100 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur);
101 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax);
103 if (setrlimit(lr->why, &rlim) == -1)
104 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what);
111 static struct login_vars {
117 { "path", "PATH", NULL, 1},
118 { "cdpath", "CDPATH", NULL, 1},
119 { "manpath", "MANPATH", NULL, 1},
120 { NULL, NULL, NULL, 0}
122 { "lang", "LANG", NULL, 1},
123 { "charset", "MM_CHARSET", NULL, 1},
124 { "timezone", "TZ", NULL, 1},
125 { "term", "TERM", NULL, 0},
126 { NULL, NULL, NULL, 0}
130 substvar(char * var, const struct passwd * pwd, int hlen, int pch, int nlen)
140 /* Count the number of ~'s in var to substitute */
142 for (p = var; (p = strchr(p, '~')) != NULL; p++)
144 /* Count the number of $'s in var to substitute */
146 for (p = var; (p = strchr(p, '$')) != NULL; p++)
150 np = malloc(strlen(var) + (dollas * nlen)
151 - dollas + (tildes * (pch+hlen))
159 * This loop does user username and homedir substitutions
160 * for unescaped $ (username) and ~ (homedir)
162 while (*(p += strcspn(p, "~$")) != '\0') {
165 if (p > np && *(p-1) == '\\') /* Escaped: */
166 memmove(p - 1, p, l + 1); /* Slide-out the backslash */
167 else if (*p == '~') {
168 int v = pch && *(p+1) != '/'; /* Avoid double // */
169 memmove(p + hlen + v, p + 1, l); /* Subst homedir */
170 memmove(p, pwd->pw_dir, hlen);
175 else /* if (*p == '$') */ {
176 memmove(p + nlen, p + 1, l); /* Subst username */
177 memmove(p, pwd->pw_name, nlen);
190 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths)
192 struct login_vars *vars = paths ? pathvars : envars;
193 int hlen = pwd ? strlen(pwd->pw_dir) : 0;
194 int nlen = pwd ? strlen(pwd->pw_name) : 0;
197 if (hlen && pwd->pw_dir[hlen-1] != '/')
200 while (vars->tag != NULL) {
201 char * var = paths ? login_getpath(lc, vars->tag, NULL)
202 : login_getcapstr(lc, vars->tag, NULL, NULL);
204 char * np = substvar(var, pwd, hlen, pch, nlen);
207 setenv(vars->var, np, vars->overwrite);
209 } else if (vars->def != NULL) {
210 setenv(vars->var, vars->def, 0);
216 * If we're not processing paths, then see if there is a setenv list by
217 * which the admin and/or user may set an arbitrary set of env vars.
220 char **set_env = login_getcaplist(lc, "setenv", ",");
222 if (set_env != NULL) {
223 while (*set_env != NULL) {
224 char *p = strchr(*set_env, '=');
226 if (p != NULL) { /* Discard invalid entries */
230 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) {
231 setenv(*set_env, np, 1);
245 * For the login class <class>, set various class context values
246 * (limits, mainly) to the values for that class. Which values are
247 * set are controlled by <flags> -- see <login_class.h> for the
250 * setclasscontext() can only set resources, priority, and umask.
254 setclasscontext(const char *classname, unsigned int flags)
259 lc = login_getclassbyname(classname, NULL);
261 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY |
262 LOGIN_SETUMASK | LOGIN_SETPATH;
264 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1;
272 * Private functionw which takes care of processing
276 setlogincontext(login_cap_t *lc, const struct passwd *pwd,
277 mode_t mymask, unsigned long flags)
281 if (flags & LOGIN_SETRESOURCES)
282 setclassresources(lc);
283 /* See if there's a umask override */
284 if (flags & LOGIN_SETUMASK)
285 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask);
287 if (flags & LOGIN_SETPATH)
288 setclassenvironment(lc, pwd, 1);
289 /* Set environment */
290 if (flags & LOGIN_SETENV)
291 setclassenvironment(lc, pwd, 0);
301 * Given a login class <lc> and a user in <pwd>, with a uid <uid>,
302 * set the context as in setclasscontext(). <flags> controls which
305 * The difference between setclasscontext() and setusercontext() is
306 * that the former sets things up for an already-existing process,
307 * while the latter sets things up from a root context. Such as might
308 * be called from login(1).
313 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags)
317 login_cap_t *llc = NULL;
318 #ifndef __NETBSD_SYSCALLS
323 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL)
324 llc = lc; /* free this when we're done */
327 if (flags & LOGIN_SETPATH)
328 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH;
330 /* we need a passwd entry to set these */
332 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN);
334 /* Set the process priority */
335 if (flags & LOGIN_SETPRIORITY) {
336 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI);
339 #ifndef __NETBSD_SYSCALLS
340 rtp.type = RTP_PRIO_IDLE;
341 rtp.prio = p - PRIO_MAX - 1;
342 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p;
343 if(rtprio(RTP_SET, 0, &rtp))
344 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
345 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
347 } else if(p < PRIO_MIN) {
348 #ifndef __NETBSD_SYSCALLS
349 rtp.type = RTP_PRIO_REALTIME;
350 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX);
351 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p;
352 if(rtprio(RTP_SET, 0, &rtp))
353 syslog(LOG_WARNING, "rtprio '%s' (%s): %m",
354 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
357 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0)
358 syslog(LOG_WARNING, "setpriority '%s' (%s): %m",
359 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS);
363 /* Setup the user's group permissions */
364 if (flags & LOGIN_SETGROUP) {
365 if (setgid(pwd->pw_gid) != 0) {
366 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid);
370 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) {
371 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name,
372 (u_long)pwd->pw_gid);
378 /* Set the sessions login */
379 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) {
380 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name);
385 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0;
386 mymask = setlogincontext(lc, pwd, mymask, flags);
389 /* This needs to be done after anything that needs root privs */
390 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) {
391 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid);
392 return -1; /* Paranoia again */
396 * Now, we repeat some of the above for the user's private entries
398 if ((lc = login_getuserclass(pwd)) != NULL) {
399 mymask = setlogincontext(lc, pwd, mymask, flags);
403 /* Finally, set any umask we've found */
404 if (flags & LOGIN_SETUMASK)