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 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 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 { NULL, 0, 0 } 61 }; 62 63 64 void 65 setclassresources(login_cap_t *lc) 66 { 67 struct login_res *lr; 68 69 if (lc == NULL) 70 return; 71 72 for (lr = resources; lr->what != NULL; ++lr) { 73 struct rlimit rlim; 74 75 /* 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 84 * conditions. 85 */ 86 87 if (getrlimit(lr->why, &rlim) != 0) 88 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 89 else { 90 char name_cur[40]; 91 char name_max[40]; 92 rlim_t rcur = rlim.rlim_cur; 93 rlim_t rmax = rlim.rlim_max; 94 95 sprintf(name_cur, "%s-cur", lr->what); 96 sprintf(name_max, "%s-max", lr->what); 97 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); 102 103 if (setrlimit(lr->why, &rlim) == -1) 104 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 105 } 106 } 107 } 108 109 110 111 static struct login_vars { 112 const char *tag; 113 const char *var; 114 const char *def; 115 } pathvars[] = { 116 { "path", "PATH", NULL }, 117 { "cdpath", "CDPATH", NULL }, 118 { "manpath", "MANPATH", NULL }, 119 { NULL, NULL, NULL } 120 }, envars[] = { 121 { "lang", "LANG", NULL }, 122 { "charset", "MM_CHARSET", NULL }, 123 { "timezone", "TZ", NULL }, 124 { "term", "TERM", NULL }, 125 { NULL, NULL, NULL } 126 }; 127 128 static char * 129 substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 130 { 131 char *np = NULL; 132 133 if (var != NULL) { 134 int tildes = 0; 135 int dollas = 0; 136 char *p; 137 138 if (pwd != NULL) { 139 /* Count the number of ~'s in var to substitute */ 140 for (p = (char *)var; (p = strchr(p, '~')) != NULL; p++) 141 ++tildes; 142 /* Count the number of $'s in var to substitute */ 143 for (p = (char *)var; (p = strchr(p, '$')) != NULL; p++) 144 ++dollas; 145 } 146 147 np = malloc(strlen(var) + (dollas * nlen) 148 - dollas + (tildes * (pch+hlen)) 149 - tildes + 1); 150 151 if (np != NULL) { 152 p = strcpy(np, var); 153 154 if (pwd != NULL) { 155 /* 156 * This loop does user username and homedir substitutions 157 * for unescaped $ (username) and ~ (homedir) 158 */ 159 while (*(p += strcspn(p, "~$")) != '\0') { 160 int l = strlen(p); 161 162 if (p > np && *(p-1) == '\\') /* Escaped: */ 163 memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 164 else if (*p == '~') { 165 int v = pch && *(p+1) != '/'; /* Avoid double // */ 166 memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 167 memmove(p, pwd->pw_dir, hlen); 168 if (v) 169 p[hlen] = '/'; 170 p += hlen + v; 171 } 172 else /* if (*p == '$') */ { 173 memmove(p + nlen, p + 1, l); /* Subst username */ 174 memmove(p, pwd->pw_name, nlen); 175 p += nlen; 176 } 177 } 178 } 179 } 180 } 181 182 return np; 183 } 184 185 186 void 187 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 188 { 189 struct login_vars *vars = paths ? pathvars : envars; 190 int hlen = pwd ? strlen(pwd->pw_dir) : 0; 191 int nlen = pwd ? strlen(pwd->pw_name) : 0; 192 char pch = 0; 193 194 if (hlen && pwd->pw_dir[hlen-1] != '/') 195 ++pch; 196 197 while (vars->tag != NULL) { 198 const char * var = paths ? login_getpath(lc, vars->tag, NULL) 199 : login_getcapstr(lc, vars->tag, NULL, NULL); 200 201 char * np = substvar(var, pwd, hlen, pch, nlen); 202 203 if (np != NULL) { 204 setenv(vars->var, np, 1); 205 free(np); 206 } else if (vars->def != NULL) { 207 setenv(vars->var, vars->def, 0); 208 } 209 ++vars; 210 } 211 212 /* 213 * If we're not processing paths, then see if there is a setenv list by 214 * which the admin and/or user may set an arbitrary set of env vars. 215 */ 216 if (!paths) { 217 char **set_env = login_getcaplist(lc, "setenv", ","); 218 219 if (set_env != NULL) { 220 while (*set_env != NULL) { 221 char *p = strchr(*set_env, '='); 222 223 if (p != NULL) { /* Discard invalid entries */ 224 char *np; 225 226 *p++ = '\0'; 227 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 228 setenv(*set_env, np, 1); 229 free(np); 230 } 231 } 232 ++set_env; 233 } 234 } 235 } 236 } 237 238 239 /* 240 * setclasscontext() 241 * 242 * For the login class <class>, set various class context values 243 * (limits, mainly) to the values for that class. Which values are 244 * set are controlled by <flags> -- see <login_class.h> for the 245 * possible values. 246 * 247 * setclasscontext() can only set resources, priority, and umask. 248 */ 249 250 int 251 setclasscontext(const char *classname, unsigned int flags) 252 { 253 int rc; 254 login_cap_t *lc; 255 256 lc = login_getclassbyname(classname, NULL); 257 258 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 259 LOGIN_SETUMASK | LOGIN_SETPATH; 260 261 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 262 login_close(lc); 263 return rc; 264 } 265 266 267 268 /* 269 * Private functionw which takes care of processing 270 */ 271 272 static mode_t 273 setlogincontext(login_cap_t *lc, const struct passwd *pwd, 274 mode_t mymask, unsigned long flags) 275 { 276 if (lc) { 277 /* Set resources */ 278 if (flags & LOGIN_SETRESOURCES) 279 setclassresources(lc); 280 /* See if there's a umask override */ 281 if (flags & LOGIN_SETUMASK) 282 mymask = (mode_t)login_getcapnum(lc, "umask", mymask, mymask); 283 /* Set paths */ 284 if (flags & LOGIN_SETPATH) 285 setclassenvironment(lc, pwd, 1); 286 /* Set environment */ 287 if (flags & LOGIN_SETENV) 288 setclassenvironment(lc, pwd, 0); 289 } 290 return mymask; 291 } 292 293 294 295 /* 296 * setusercontext() 297 * 298 * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 299 * set the context as in setclasscontext(). <flags> controls which 300 * values are set. 301 * 302 * The difference between setclasscontext() and setusercontext() is 303 * that the former sets things up for an already-existing process, 304 * while the latter sets things up from a root context. Such as might 305 * be called from login(1). 306 * 307 */ 308 309 int 310 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 311 { 312 quad_t p; 313 mode_t mymask; 314 login_cap_t *llc = NULL; 315 #ifndef __NETBSD_SYSCALLS 316 struct rtprio rtp; 317 #endif 318 319 if (lc == NULL) { 320 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 321 llc = lc; /* free this when we're done */ 322 } 323 324 if (flags & LOGIN_SETPATH) 325 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 326 327 /* we need a passwd entry to set these */ 328 if (pwd == NULL) 329 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN); 330 331 /* Set the process priority */ 332 if (flags & LOGIN_SETPRIORITY) { 333 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 334 335 if(p > PRIO_MAX) { 336 #ifndef __NETBSD_SYSCALLS 337 rtp.type = RTP_PRIO_IDLE; 338 rtp.prio = p - PRIO_MAX - 1; 339 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p; 340 if(rtprio(RTP_SET, 0, &rtp)) 341 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 342 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 343 #endif 344 } else if(p < PRIO_MIN) { 345 #ifndef __NETBSD_SYSCALLS 346 rtp.type = RTP_PRIO_REALTIME; 347 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX); 348 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p; 349 if(rtprio(RTP_SET, 0, &rtp)) 350 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 351 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 352 #endif 353 } else { 354 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 355 syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 356 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 357 } 358 } 359 360 /* Setup the user's group permissions */ 361 if (flags & LOGIN_SETGROUP) { 362 if (setgid(pwd->pw_gid) != 0) { 363 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 364 login_close(llc); 365 return -1; 366 } 367 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 368 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 369 (u_long)pwd->pw_gid); 370 login_close(llc); 371 return -1; 372 } 373 } 374 375 /* Set the sessions login */ 376 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 377 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 378 login_close(llc); 379 return -1; 380 } 381 382 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 383 mymask = setlogincontext(lc, pwd, mymask, flags); 384 login_close(llc); 385 386 /* This needs to be done after anything that needs root privs */ 387 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 388 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 389 return -1; /* Paranoia again */ 390 } 391 392 /* 393 * Now, we repeat some of the above for the user's private entries 394 */ 395 if ((lc = login_getuserclass(pwd)) != NULL) { 396 mymask = setlogincontext(lc, pwd, mymask, flags); 397 login_close(lc); 398 } 399 400 /* Finally, set any umask we've found */ 401 if (flags & LOGIN_SETUMASK) 402 umask(mymask); 403 404 return 0; 405 } 406 407