login_class.c revision 25670
1271947Sdes/*- 2141098Sdes * Copyright (c) 1996 by 3228692Sdes * Sean Eric Fagan <sef@kithrup.com> 4271947Sdes * David Nugent <davidn@blaze.net.au> 5271947Sdes * All rights reserved. 6255369Sdes * 7228692Sdes * Redistribution and use in source and binary forms, with or without 8228692Sdes * modification, is permitted provided that the following conditions 9228692Sdes * are met: 10141098Sdes * 1. Redistributions of source code must retain the above copyright 11228692Sdes * notice immediately at the beginning of the file, without modification, 12174832Sdes * this list of conditions, and the following disclaimer. 13228692Sdes * 2. Redistributions in binary form must reproduce the above copyright 14228692Sdes * notice, this list of conditions and the following disclaimer in the 15228692Sdes * documentation and/or other materials provided with the distribution. 16228692Sdes * 3. This work was done expressly for inclusion into FreeBSD. Other use 17228692Sdes * is permitted provided this notation is included. 18228692Sdes * 4. Absolutely no warranty of function or purpose is made by the authors. 19141098Sdes * 5. Modifications may be freely made to this file providing the above 20141098Sdes * conditions are met. 21228692Sdes * 22228692Sdes * High-level routines relating to use of the user capabilities database 23228692Sdes * 24228692Sdes * $Id: login_class.c,v 1.5 1997/02/22 15:08:22 peter Exp $ 25228692Sdes */ 26228692Sdes 27228692Sdes#include <stdio.h> 28141098Sdes#include <stdlib.h> 29141098Sdes#include <string.h> 30141098Sdes#include <unistd.h> 31141098Sdes#include <errno.h> 32228692Sdes#include <sys/types.h> 33228692Sdes#include <sys/stat.h> 34255376Sdes#include <sys/time.h> 35255376Sdes#include <sys/resource.h> 36174832Sdes#include <fcntl.h> 37228692Sdes#include <pwd.h> 38228692Sdes#include <syslog.h> 39228692Sdes#include <login_cap.h> 40228692Sdes#include <paths.h> 41228692Sdes 42228692Sdes 43228692Sdes#undef UNKNOWN 44228692Sdes#define UNKNOWN "su" 45141098Sdes 46228692Sdes 47228692Sdesstatic struct login_res { 48228692Sdes const char *what; 49228692Sdes rlim_t (*who)(login_cap_t *, const char *, rlim_t, rlim_t); 50228692Sdes int why; 51255376Sdes} resources[] = { 52255376Sdes { "cputime", login_getcaptime, RLIMIT_CPU }, 53255376Sdes { "filesize", login_getcapsize, RLIMIT_FSIZE }, 54255376Sdes { "datasize", login_getcapsize, RLIMIT_DATA }, 55141098Sdes { "stacksize", login_getcapsize, RLIMIT_STACK }, 56255376Sdes { "memoryuse", login_getcapsize, RLIMIT_RSS }, 57141098Sdes { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 58228692Sdes { "maxproc", login_getcapnum, RLIMIT_NPROC }, 59228692Sdes { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 60228692Sdes { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 61228692Sdes { NULL, 0, 0 } 62228692Sdes}; 63141098Sdes 64228692Sdes 65255376Sdesvoid 66228692Sdessetclassresources(login_cap_t *lc) 67228692Sdes{ 68228692Sdes struct login_res *lr; 69141098Sdes 70228692Sdes if (lc == NULL) 71228692Sdes return; 72228692Sdes 73228692Sdes for (lr = resources; lr->what != NULL; ++lr) { 74228692Sdes struct rlimit rlim; 75228692Sdes 76174832Sdes /* 77228692Sdes * The login.conf file can have <limit>, <limit>-max, and 78228692Sdes * <limit>-cur entries. 79228692Sdes * What we do is get the current current- and maximum- limits. 80228692Sdes * Then, we try to get an entry for <limit> from the capability, 81174832Sdes * using the current and max limits we just got as the 82228692Sdes * default/error values. 83141098Sdes * *Then*, we try looking for <limit>-cur and <limit>-max, 84255376Sdes * again using the appropriate values as the default/error 85255376Sdes * conditions. 86255376Sdes */ 87255376Sdes 88271947Sdes if (getrlimit(lr->why, &rlim) != 0) 89141098Sdes syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 90228692Sdes else { 91228692Sdes char name_cur[40]; 92228692Sdes char name_max[40]; 93228692Sdes rlim_t rcur = rlim.rlim_cur; 94228692Sdes rlim_t rmax = rlim.rlim_max; 95141098Sdes 96141098Sdes sprintf(name_cur, "%s-cur", lr->what); 97228692Sdes sprintf(name_max, "%s-max", lr->what); 98228692Sdes 99228692Sdes rcur = (*lr->who)(lc, lr->what, rcur, rcur); 100228692Sdes rmax = (*lr->who)(lc, lr->what, rmax, rmax); 101228692Sdes rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 102141098Sdes rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 103141098Sdes 104255376Sdes if (setrlimit(lr->why, &rlim) == -1) 105255376Sdes syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 106255376Sdes } 107255376Sdes } 108255376Sdes} 109255376Sdes 110255376Sdes 111228692Sdes 112228692Sdesstatic struct login_vars { 113228692Sdes const char *tag; 114228692Sdes const char *var; 115228692Sdes const char *def; 116228692Sdes} pathvars[] = { 117228692Sdes { "path", "PATH", NULL }, 118228692Sdes { "cdpath", "CDPATH", NULL }, 119228692Sdes { "manpath", "MANPATH", NULL }, 120141098Sdes { NULL, NULL, NULL } 121174832Sdes}, envars[] = { 122236109Sdes { "lang", "LANG", NULL }, 123228692Sdes { "charset", "MM_CHARSET", NULL }, 124236109Sdes { "timezone", "TZ", NULL }, 125228692Sdes { "term", "TERM", UNKNOWN }, 126228692Sdes { NULL, NULL, NULL } 127236109Sdes}; 128236109Sdes 129228692Sdesstatic char * 130228692Sdessubstvar(char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 131255376Sdes{ 132255369Sdes char *np = NULL; 133228692Sdes 134228692Sdes if (var != NULL) { 135228692Sdes int tildes = 0; 136236109Sdes int dollas = 0; 137236109Sdes char *p; 138174832Sdes 139236109Sdes if (pwd != NULL) { 140255376Sdes /* Count the number of ~'s in var to substitute */ 141141098Sdes p = var; 142 for (p = var; (p = strchr(p, '~')) != NULL; p++) 143 ++tildes; 144 /* Count the number of $'s in var to substitute */ 145 p = var; 146 for (p = var; (p = strchr(p, '$')) != NULL; p++) 147 ++dollas; 148 } 149 150 np = malloc(strlen(var) + (dollas * nlen) 151 - dollas + (tildes * (pch+hlen)) 152 - tildes + 1); 153 154 if (np != NULL) { 155 p = strcpy(np, var); 156 157 if (pwd != NULL) { 158 /* 159 * This loop does user username and homedir substitutions 160 * for unescaped $ (username) and ~ (homedir) 161 */ 162 while (*(p += strcspn(p, "~$")) != '\0') { 163 int l = strlen(p); 164 165 if (p > var && *(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); 171 if (v) 172 p[hlen] = '/'; 173 p += hlen + v; 174 } 175 else /* if (*p == '$') */ { 176 memmove(p + nlen, p + 1, l); /* Subst username */ 177 memmove(p, pwd->pw_name, nlen); 178 p += nlen; 179 } 180 } 181 } 182 } 183 } 184 185 return np; 186} 187 188 189void 190setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) 191{ 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; 195 char pch = 0; 196 197 if (hlen && pwd->pw_dir[hlen-1] != '/') 198 ++pch; 199 200 while (vars->tag != NULL) { 201 char * var = paths ? login_getpath(lc, vars->tag, NULL) 202 : login_getcapstr(lc, vars->tag, NULL, NULL); 203 204 char * np = substvar(var, pwd, hlen, pch, nlen); 205 206 if (np != NULL) { 207 setenv(vars->var, np, 1); 208 free(np); 209 } else if (vars->def != NULL) { 210 setenv(vars->var, vars->def, 0); 211 } 212 ++vars; 213 } 214 215 /* 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. 218 */ 219 if (!paths) { 220 char **set_env = login_getcaplist(lc, "setenv", ","); 221 222 if (set_env != NULL) { 223 while (*set_env != NULL) { 224 char *p = strchr(*set_env, '='); 225 226 if (p != NULL) { /* Discard invalid entries */ 227 char *np; 228 229 *p++ = '\0'; 230 if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { 231 setenv(*set_env, np, 1); 232 free(np); 233 } 234 } 235 ++set_env; 236 } 237 } 238 } 239} 240 241 242/* 243 * setclasscontext() 244 * 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 248 * possible values. 249 * 250 * setclasscontext() can only set resources, priority, and umask. 251 */ 252 253int 254setclasscontext(const char *classname, unsigned int flags) 255{ 256 int rc; 257 login_cap_t *lc; 258 259 lc = login_getclassbyname(classname, NULL); 260 261 flags &= LOGIN_SETRESOURCES | LOGIN_SETPRIORITY | 262 LOGIN_SETUMASK | LOGIN_SETPATH; 263 264 rc = lc ? setusercontext(lc, NULL, 0, flags) : -1; 265 login_close(lc); 266 return rc; 267} 268 269 270 271/* 272 * Private functionw which takes care of processing 273 */ 274 275static mode_t 276setlogincontext(login_cap_t *lc, const struct passwd *pwd, 277 mode_t mymask, unsigned long flags) 278{ 279 if (lc) { 280 /* Set resources */ 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); 286 /* Set paths */ 287 if (flags & LOGIN_SETPATH) 288 setclassenvironment(lc, pwd, 1); 289 /* Set environment */ 290 if (flags & LOGIN_SETENV) 291 setclassenvironment(lc, pwd, 0); 292 } 293 return mymask; 294} 295 296 297 298/* 299 * setusercontext() 300 * 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 303 * values are set. 304 * 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). 309 * 310 */ 311 312int 313setusercontext(login_cap_t *lc, const struct passwd *pwd, uid_t uid, unsigned int flags) 314{ 315 quad_t p; 316 mode_t mymask; 317 login_cap_t *llc = NULL; 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 p = (p < PRIO_MIN || p > PRIO_MAX) ? LOGIN_DEFPRI : p; 336 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 337 syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 338 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 339 } 340 341 /* Setup the user's group permissions */ 342 if (flags & LOGIN_SETGROUP) { 343 if (setgid(pwd->pw_gid) != 0) { 344 syslog(LOG_ERR, "setgid(%ld): %m", (long)pwd->pw_gid); 345 login_close(llc); 346 return -1; 347 } 348 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 349 syslog(LOG_ERR, "initgroups(%s,%ld): %m", pwd->pw_name, 350 pwd->pw_gid); 351 login_close(llc); 352 return -1; 353 } 354 } 355 356 /* Set the sessions login */ 357 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 358 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 359 login_close(llc); 360 return -1; 361 } 362 363 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 364 mymask = setlogincontext(lc, pwd, mymask, flags); 365 login_close(llc); 366 367 /* This needs to be done after anything that needs root privs */ 368 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 369 syslog(LOG_ERR, "setuid(%ld): %m", uid); 370 return -1; /* Paranoia again */ 371 } 372 373 /* 374 * Now, we repeat some of the above for the user's private entries 375 */ 376 if ((lc = login_getuserclass(pwd)) != NULL) { 377 mymask = setlogincontext(lc, pwd, mymask, flags); 378 login_close(lc); 379 } 380 381 /* Finally, set any umask we've found */ 382 if (flags & LOGIN_SETUMASK) 383 umask(mymask); 384 385 return 0; 386} 387 388