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$ 25 */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 #include <errno.h> 32 #include <sys/types.h> 33 #include <sys/time.h> 34 #include <sys/resource.h> 35 #include <fcntl.h> 36 #include <pwd.h> 37 #include <syslog.h> 38 #include <login_cap.h> 39 #include <paths.h> 40 41 42 #undef UNKNOWN 43 #define UNKNOWN "su" 44 45 46 static struct login_res { 47 const char * what; 48 rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 49 int why; 50 } resources[] = { 51 { "cputime", login_getcaptime, RLIMIT_CPU }, 52 { "filesize", login_getcapsize, RLIMIT_FSIZE }, 53 { "datasize", login_getcapsize, RLIMIT_DATA }, 54 { "stacksize", login_getcapsize, RLIMIT_STACK }, 55 { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 56 { "memoryuse", login_getcapsize, RLIMIT_RSS }, 57 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 58 { "maxproc", login_getcapnum, RLIMIT_NPROC }, 59 { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 60 { NULL, 0, 0 } 61 }; 62 63 64 65 void 66 setclassresources(login_cap_t *lc) 67 { 68 struct login_res *lr = resources; 69 70 if (lc == NULL) 71 return; 72 73 while (lr->what != NULL) { 74 struct rlimit rlim, 75 newlim; 76 char cur[40], 77 max[40]; 78 rlim_t rcur, 79 rmax; 80 81 sprintf(cur, "%s-cur", lr->what); 82 sprintf(max, "%s-max", lr->what); 83 84 /* 85 * The login.conf file can have <limit>, <limit>-max, and 86 * <limit>-cur entries. 87 * What we do is get the current current- and maximum- limits. 88 * Then, we try to get an entry for <limit> from the capability, 89 * using the current and max limits we just got as the 90 * default/error values. 91 * *Then*, we try looking for <limit>-cur and <limit>-max, 92 * again using the appropriate values as the default/error 93 * conditions. 94 */ 95 96 getrlimit(lr->why, &rlim); 97 rcur = rlim.rlim_cur; 98 rmax = rlim.rlim_max; 99 100 rcur = (*lr->who)(lc, lr->what, rcur, rcur); 101 rmax = (*lr->who)(lc, lr->what, rmax, rmax); 102 newlim.rlim_cur = (*lr->who)(lc, cur, rcur, rcur); 103 newlim.rlim_max = (*lr->who)(lc, max, rmax, rmax); 104 105 if (setrlimit(lr->why, &newlim) == -1) 106 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 107 108 ++lr; 109 } 110 } 111 112 static struct login_vars { 113 const char * tag; 114 const char * var; 115 const char * def; 116 } pathvars[] = { 117 { "path", "PATH", 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", UNKNOWN }, 125 { NULL, NULL, NULL } 126 }; 127 128 static char * 129 substvar(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 /* 140 * Count the number of ~'s in var to substitute 141 */ 142 p = var; 143 while ((p = strchr(p, '~')) != NULL) { 144 ++tildes; 145 ++p; 146 } 147 148 /* 149 * Count the number of $'s in var to substitute 150 */ 151 p = var; 152 while ((p = strchr(p, '$')) != NULL) { 153 ++dollas; 154 ++p; 155 } 156 } 157 158 np = malloc(strlen(var) + (dollas * nlen) - dollas + (tildes * (pch+hlen)) - tildes + 1); 159 160 if (np != NULL) { 161 p = strcpy(np, var); 162 163 if (pwd != NULL) { 164 /* 165 * This loop does user username and homedir substitutions 166 * for unescaped $ (username) and ~ (homedir) 167 */ 168 while (*(p += strcspn(p, "~$")) != '\0') { 169 int l = strlen(p); 170 171 if (p > var && *(p-1) == '\\') /* Escaped: */ 172 memmove(p - 1, p, l + 1); /* Slide-out the backslash */ 173 else if (*p == '~') { 174 int v = pch && *(p+1) != '/'; /* Avoid double // */ 175 memmove(p + hlen + v, p + 1, l); /* Subst homedir */ 176 memmove(p, pwd->pw_dir, hlen); 177 if (v) 178 p[hlen] = '/'; 179 p += hlen + v; 180 } 181 else /* if (*p == '$') */ { 182 memmove(p + nlen, p + 1, l); /* Subst username */ 183 memmove(p, pwd->pw_name, nlen); 184 p += nlen; 185 } 186 } 187 } 188 } 189 } 190 return np; 191 } 192 193 194 void 195 setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 196 { 197 struct login_vars * vars = paths ? pathvars : envars; 198 int hlen = pwd ? strlen(pwd->pw_dir) : 0; 199 int nlen = pwd ? strlen(pwd->pw_name) : 0; 200 char pch = 0; 201 202 if (hlen && pwd->pw_dir[hlen-1] != '/') 203 ++pch; 204 205 while (vars->tag != NULL) { 206 char * var = paths ? login_getpath(lc, vars->tag, NULL) 207 : login_getcapstr(lc, vars->tag, NULL, NULL); 208 209 char * np = substvar(var, pwd, hlen, pch, nlen); 210 211 if (np != NULL) { 212 setenv(vars->var, np, 1); 213 free(np); 214 } else if (vars->def != NULL) { 215 setenv(vars->var, vars->def, 0); 216 } 217 ++vars; 218 } 219 220 /* 221 * If we're not processing paths, then see if there is a setenv list by 222 * which the admin and/or user may set an arbitrary set of env vars. 223 */ 224 if (!paths) { 225 char ** set_env = login_getcaplist(lc, "setenv", ","); 226 227 if (set_env != NULL) { 228 while (*set_env != NULL) { 229 char *p = strchr(*set_env, '='); 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 = login_getclassbyname(classname, NULL); 262 flags &= (LOGIN_SETRESOURCES| LOGIN_SETPRIORITY|LOGIN_SETUMASK); 263 rc = setusercontext(lc, NULL, 0, flags); 264 login_close(lc); 265 return rc; 266 } 267 268 269 /* 270 * setusercontext() 271 * 272 * Given a login class <lc> and a user in <pwd>, with a uid <uid>, 273 * set the context as in setclasscontext(). <flags> controls which 274 * values are set. 275 * 276 * The difference between setclasscontext() and setusercontext() is 277 * that the former sets things up for an already-existing process, 278 * while the latter sets things up from a root context. Such as might 279 * be called from login(1). 280 * 281 */ 282 283 int 284 setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 285 { 286 int i; 287 login_cap_t * llc = NULL; 288 289 if (lc == NULL) 290 { 291 if (pwd != NULL && (lc = login_getclass(pwd)) != NULL) 292 llc = lc; /* free this when we're done */ 293 } 294 295 if (flags & LOGIN_SETPATH) 296 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 297 298 /* 299 * Set the process priority 300 */ 301 if (flags & LOGIN_SETPRIORITY) { 302 int pri = (int)login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 303 pri = (pri < PRIO_MIN) ? PRIO_MIN : (pri > PRIO_MAX) ? PRIO_MAX : pri; 304 if (setpriority(PRIO_PROCESS, 0, pri) != 0) 305 syslog(LOG_WARNING, "setpriority '%s': %m", pwd->pw_name); 306 } 307 308 /* 309 * Set resources 310 */ 311 if (flags & LOGIN_SETRESOURCES) 312 setclassresources(lc); 313 314 /* 315 * Set the sessions login 316 */ 317 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 318 syslog(LOG_ERR, "setlogin '%s': %m", pwd->pw_name); 319 login_close(llc); 320 return -1; 321 } 322 323 /* 324 * Setup the user's group permissions 325 */ 326 if (flags & LOGIN_SETGROUP) { 327 if (setgid(pwd->pw_gid) != 0) 328 syslog(LOG_WARNING, "setgid %ld: %m", (long)pwd->pw_gid); 329 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) 330 syslog(LOG_WARNING, "initgroups '%s': %m", pwd->pw_name); 331 } 332 333 /* 334 * FreeBSD extension: here we (might) loop and do this twice. 335 * First, for the class we have been given, and next for 336 * any user overrides in ~/.login.conf the user's home directory. 337 */ 338 if (flags & LOGIN_SETUMASK) 339 umask(LOGIN_DEFUMASK); /* Set default umask up front */ 340 341 i = 0; 342 while (i < 2 && lc != NULL) { 343 344 if (flags & LOGIN_SETUMASK) { 345 rlim_t tmp = login_getcapnum(lc, "umask", RLIM_INFINITY, RLIM_INFINITY); 346 if (tmp != RLIM_INFINITY) 347 umask((mode_t)tmp); 348 } 349 350 if (flags & LOGIN_SETPATH) 351 setclassenvironment(lc, pwd, 1); 352 353 if (flags & LOGIN_SETENV) 354 setclassenvironment(lc, pwd, 0); 355 356 if (i++ == 0) /* Play it again, Sam */ 357 lc = (pwd == NULL) ? NULL : login_getuserclass(pwd); 358 } 359 360 login_close(lc); /* User's private 'me' class */ 361 login_close(llc); /* Class we opened */ 362 363 /* 364 * This needs to be done after all of the above. 365 * Do it last so we avoid getting killed and dropping core 366 */ 367 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 368 syslog(LOG_ERR, "setuid %ld: %m", uid); 369 login_close(llc); 370 return -1; /* Paranoia again */ 371 } 372 373 return 0; 374 } 375 376