1 /*- 2 * Copyright (c) 1996 by 3 * Sean Eric Fagan <sef@kithrup.com> 4 * David Nugent <davidn@blaze.net.au> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, is permitted provided that the following conditions 9 * are met: 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 20 * conditions are met. 21 * 22 * High-level routines relating to use of the user capabilities database 23 * 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 $ 26 */ 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <unistd.h> 32 #include <errno.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <sys/time.h> 36 #include <sys/resource.h> 37 #include <fcntl.h> 38 #include <pwd.h> 39 #include <syslog.h> 40 #include <login_cap.h> 41 #include <paths.h> 42 #include <sys/rtprio.h> 43 44 45 static struct login_res { 46 const char *what; 47 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 48 int why; 49 } resources[] = { 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 }, 63 #endif 64 { NULL, 0, 0 } 65 }; 66 67 68 void 69 setclassresources(login_cap_t *lc) 70 { 71 struct login_res *lr; 72 73 if (lc == NULL) 74 return; 75 76 for (lr = resources; lr->what != NULL; ++lr) { 77 struct rlimit rlim; 78 79 /* 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 88 * conditions. 89 */ 90 91 if (getrlimit(lr->why, &rlim) != 0) 92 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 93 else { 94 char name_cur[40]; 95 char name_max[40]; 96 rlim_t rcur = rlim.rlim_cur; 97 rlim_t rmax = rlim.rlim_max; 98 99 sprintf(name_cur, "%s-cur", lr->what); 100 sprintf(name_max, "%s-max", lr->what); 101 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); 106 107 if (setrlimit(lr->why, &rlim) == -1) 108 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 109 } 110 } 111 } 112 113 114 115 static struct login_vars { 116 const char *tag; 117 const char *var; 118 const char *def; 119 int overwrite; 120 } pathvars[] = { 121 { "path", "PATH", NULL, 1}, 122 { "cdpath", "CDPATH", NULL, 1}, 123 { "manpath", "MANPATH", NULL, 1}, 124 { NULL, NULL, NULL, 0} 125 }, envars[] = { 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} 131 }; 132 133 static char * 134 substvar(char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 135 { 136 char *np = NULL; 137 138 if (var != NULL) { 139 int tildes = 0; 140 int dollas = 0; 141 char *p; 142 143 if (pwd != NULL) { 144 /* Count the number of ~'s in var to substitute */ 145 p = var; 146 for (p = var; (p = strchr(p, '~')) != NULL; p++) 147 ++tildes; 148 /* Count the number of $'s in var to substitute */ 149 p = var; 150 for (p = var; (p = strchr(p, '$')) != NULL; p++) 151 ++dollas; 152 } 153 154 np = malloc(strlen(var) + (dollas * nlen) 155 - dollas + (tildes * (pch+hlen)) 156 - tildes + 1); 157 158 if (np != NULL) { 159 p = strcpy(np, var); 160 161 if (pwd != NULL) { 162 /* 163 * This loop does user username and homedir substitutions 164 * for unescaped $ (username) and ~ (homedir) 165 */ 166 while (*(p += strcspn(p, "~$")) != '\0') { 167 int l = strlen(p); 168 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); 175 if (v) 176 p[hlen] = '/'; 177 p += hlen + v; 178 } 179 else /* if (*p == '$') */ { 180 memmove(p + nlen, p + 1, l); /* Subst username */ 181 memmove(p, pwd->pw_name, nlen); 182 p += nlen; 183 } 184 } 185 } 186 } 187 } 188 189 return np; 190 } 191 192 193 void 194 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 195 { 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; 199 char pch = 0; 200 201 if (hlen && pwd->pw_dir[hlen-1] != '/') 202 ++pch; 203 204 while (vars->tag != NULL) { 205 char * var = paths ? login_getpath(lc, vars->tag, NULL) 206 : login_getcapstr(lc, vars->tag, NULL, NULL); 207 208 char * np = substvar(var, pwd, hlen, pch, nlen); 209 210 if (np != NULL) { 211 setenv(vars->var, np, vars->overwrite); 212 free(np); 213 } else if (vars->def != NULL) { 214 setenv(vars->var, vars->def, 0); 215 } 216 ++vars; 217 } 218 219 /* 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. 222 */ 223 if (!paths) { 224 char **set_env = login_getcaplist(lc, "setenv", ","); 225 226 if (set_env != NULL) { 227 while (*set_env != NULL) { 228 char *p = strchr(*set_env, '='); 229 230 if (p != NULL) { /* Discard invalid entries */ 231 char *np; 232 233 *p++ = '\0'; 234 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 235 setenv(*set_env, np, 1); 236 free(np); 237 } 238 } 239 ++set_env; 240 } 241 } 242 } 243 } 244 245 246 /* 247 * setclasscontext() 248 * 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 252 * possible values. 253 * 254 * setclasscontext() can only set resources, priority, and umask. 255 */ 256 257 int 258 setclasscontext(const char *classname, unsigned int flags) 259 { 260 int rc; 261 login_cap_t *lc; 262 263 lc = login_getclassbyname(classname, NULL); 264 265 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 266 LOGIN_SETUMASK | LOGIN_SETPATH; 267 268 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 269 login_close(lc); 270 return rc; 271 } 272 273 274 275 /* 276 * Private functionw which takes care of processing 277 */ 278 279 static mode_t 280 setlogincontext(login_cap_t *lc, const struct passwd *pwd, 281 mode_t mymask, unsigned long flags) 282 { 283 if (lc) { 284 /* Set resources */ 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); 290 /* Set paths */ 291 if (flags & LOGIN_SETPATH) 292 setclassenvironment(lc, pwd, 1); 293 /* Set environment */ 294 if (flags & LOGIN_SETENV) 295 setclassenvironment(lc, pwd, 0); 296 } 297 return mymask; 298 } 299 300 301 302 /* 303 * setusercontext() 304 * 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 307 * values are set. 308 * 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). 313 * 314 */ 315 316 int 317 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 318 { 319 quad_t p; 320 mode_t mymask; 321 login_cap_t *llc = NULL; 322 #ifndef __NETBSD_SYSCALLS 323 struct rtprio rtp; 324 #endif 325 326 if (lc == NULL) { 327 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 328 llc = lc; /* free this when we're done */ 329 } 330 331 if (flags & LOGIN_SETPATH) 332 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 333 334 /* we need a passwd entry to set these */ 335 if (pwd == NULL) 336 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN); 337 338 /* Set the process priority */ 339 if (flags & LOGIN_SETPRIORITY) { 340 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 341 342 if(p > PRIO_MAX) { 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); 350 #endif 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); 359 #endif 360 } else { 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); 364 } 365 } 366 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); 371 login_close(llc); 372 return -1; 373 } 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); 377 login_close(llc); 378 return -1; 379 } 380 } 381 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); 385 login_close(llc); 386 return -1; 387 } 388 389 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 390 mymask = setlogincontext(lc, pwd, mymask, flags); 391 login_close(llc); 392 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 */ 397 } 398 399 /* 400 * Now, we repeat some of the above for the user's private entries 401 */ 402 if ((lc = login_getuserclass(pwd)) != NULL) { 403 mymask = setlogincontext(lc, pwd, mymask, flags); 404 login_close(lc); 405 } 406 407 /* Finally, set any umask we've found */ 408 if (flags & LOGIN_SETUMASK) 409 umask(mymask); 410 411 return 0; 412 } 413 414