27 28#include <sys/types.h> 29#include <sys/stat.h> 30#include <sys/time.h> 31#include <sys/resource.h> 32#include <sys/mac.h> 33#include <sys/rtprio.h> 34#include <errno.h> 35#include <fcntl.h> 36#include <login_cap.h> 37#include <paths.h> 38#include <pwd.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <syslog.h> 43#include <unistd.h> 44 45 46static 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 { "memoryuse", login_getcapsize, RLIMIT_RSS }, 56 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 57 { "maxproc", login_getcapnum, RLIMIT_NPROC }, 58 { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 59 { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 60 { "sbsize", login_getcapsize, RLIMIT_SBSIZE }, 61 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM }, 62 { NULL, 0, 0 } 63}; 64 65 66void 67setclassresources(login_cap_t *lc) 68{ 69 struct login_res *lr; 70 71 if (lc == NULL) 72 return; 73 74 for (lr = resources; lr->what != NULL; ++lr) { 75 struct rlimit rlim; 76 77 /* 78 * The login.conf file can have <limit>, <limit>-max, and 79 * <limit>-cur entries. 80 * What we do is get the current current- and maximum- limits. 81 * Then, we try to get an entry for <limit> from the capability, 82 * using the current and max limits we just got as the 83 * default/error values. 84 * *Then*, we try looking for <limit>-cur and <limit>-max, 85 * again using the appropriate values as the default/error 86 * conditions. 87 */ 88 89 if (getrlimit(lr->why, &rlim) != 0) 90 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 91 else { 92 char name_cur[40]; 93 char name_max[40]; 94 rlim_t rcur = rlim.rlim_cur; 95 rlim_t rmax = rlim.rlim_max; 96 97 sprintf(name_cur, "%s-cur", lr->what); 98 sprintf(name_max, "%s-max", lr->what); 99 100 rcur = (*lr->who)(lc, lr->what, rcur, rcur); 101 rmax = (*lr->who)(lc, lr->what, rmax, rmax); 102 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 103 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 104 105 if (setrlimit(lr->why, &rlim) == -1) 106 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 107 } 108 } 109} 110 111 112 113static struct login_vars { 114 const char *tag; 115 const char *var; 116 const char *def; 117 int overwrite; 118} pathvars[] = { 119 { "path", "PATH", NULL, 1}, 120 { "cdpath", "CDPATH", NULL, 1}, 121 { "manpath", "MANPATH", NULL, 1}, 122 { NULL, NULL, NULL, 0} 123}, envars[] = { 124 { "lang", "LANG", NULL, 1}, 125 { "charset", "MM_CHARSET", NULL, 1}, 126 { "timezone", "TZ", NULL, 1}, 127 { "term", "TERM", NULL, 0}, 128 { NULL, NULL, NULL, 0} 129}; 130 131static char * 132substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 133{ 134 char *np = NULL; 135 136 if (var != NULL) { 137 int tildes = 0; 138 int dollas = 0; 139 char *p; 140 141 if (pwd != NULL) { 142 /* Count the number of ~'s in var to substitute */ 143 for (p = (char *)var; (p = strchr(p, '~')) != NULL; p++) 144 ++tildes; 145 /* Count the number of $'s in var to substitute */ 146 for (p = (char *)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 > 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); 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 const 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, vars->overwrite); 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) {
| 27 28#include <sys/types.h> 29#include <sys/stat.h> 30#include <sys/time.h> 31#include <sys/resource.h> 32#include <sys/mac.h> 33#include <sys/rtprio.h> 34#include <errno.h> 35#include <fcntl.h> 36#include <login_cap.h> 37#include <paths.h> 38#include <pwd.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <syslog.h> 43#include <unistd.h> 44 45 46static 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 { "memoryuse", login_getcapsize, RLIMIT_RSS }, 56 { "memorylocked", login_getcapsize, RLIMIT_MEMLOCK }, 57 { "maxproc", login_getcapnum, RLIMIT_NPROC }, 58 { "openfiles", login_getcapnum, RLIMIT_NOFILE }, 59 { "coredumpsize", login_getcapsize, RLIMIT_CORE }, 60 { "sbsize", login_getcapsize, RLIMIT_SBSIZE }, 61 { "vmemoryuse", login_getcapsize, RLIMIT_VMEM }, 62 { NULL, 0, 0 } 63}; 64 65 66void 67setclassresources(login_cap_t *lc) 68{ 69 struct login_res *lr; 70 71 if (lc == NULL) 72 return; 73 74 for (lr = resources; lr->what != NULL; ++lr) { 75 struct rlimit rlim; 76 77 /* 78 * The login.conf file can have <limit>, <limit>-max, and 79 * <limit>-cur entries. 80 * What we do is get the current current- and maximum- limits. 81 * Then, we try to get an entry for <limit> from the capability, 82 * using the current and max limits we just got as the 83 * default/error values. 84 * *Then*, we try looking for <limit>-cur and <limit>-max, 85 * again using the appropriate values as the default/error 86 * conditions. 87 */ 88 89 if (getrlimit(lr->why, &rlim) != 0) 90 syslog(LOG_ERR, "getting %s resource limit: %m", lr->what); 91 else { 92 char name_cur[40]; 93 char name_max[40]; 94 rlim_t rcur = rlim.rlim_cur; 95 rlim_t rmax = rlim.rlim_max; 96 97 sprintf(name_cur, "%s-cur", lr->what); 98 sprintf(name_max, "%s-max", lr->what); 99 100 rcur = (*lr->who)(lc, lr->what, rcur, rcur); 101 rmax = (*lr->who)(lc, lr->what, rmax, rmax); 102 rlim.rlim_cur = (*lr->who)(lc, name_cur, rcur, rcur); 103 rlim.rlim_max = (*lr->who)(lc, name_max, rmax, rmax); 104 105 if (setrlimit(lr->why, &rlim) == -1) 106 syslog(LOG_WARNING, "set class '%s' resource limit %s: %m", lc->lc_class, lr->what); 107 } 108 } 109} 110 111 112 113static struct login_vars { 114 const char *tag; 115 const char *var; 116 const char *def; 117 int overwrite; 118} pathvars[] = { 119 { "path", "PATH", NULL, 1}, 120 { "cdpath", "CDPATH", NULL, 1}, 121 { "manpath", "MANPATH", NULL, 1}, 122 { NULL, NULL, NULL, 0} 123}, envars[] = { 124 { "lang", "LANG", NULL, 1}, 125 { "charset", "MM_CHARSET", NULL, 1}, 126 { "timezone", "TZ", NULL, 1}, 127 { "term", "TERM", NULL, 0}, 128 { NULL, NULL, NULL, 0} 129}; 130 131static char * 132substvar(const char * var, const struct passwd * pwd, int hlen, int pch, int nlen) 133{ 134 char *np = NULL; 135 136 if (var != NULL) { 137 int tildes = 0; 138 int dollas = 0; 139 char *p; 140 141 if (pwd != NULL) { 142 /* Count the number of ~'s in var to substitute */ 143 for (p = (char *)var; (p = strchr(p, '~')) != NULL; p++) 144 ++tildes; 145 /* Count the number of $'s in var to substitute */ 146 for (p = (char *)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 > 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); 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 const 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, vars->overwrite); 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) {
|
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#ifndef __NETBSD_SYSCALLS 319 struct rtprio rtp; 320#endif 321 int error; 322 323 if (lc == NULL) { 324 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 325 llc = lc; /* free this when we're done */ 326 } 327 328 if (flags & LOGIN_SETPATH) 329 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 330 331 /* we need a passwd entry to set these */ 332 if (pwd == NULL) 333 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 334 335 /* Set the process priority */ 336 if (flags & LOGIN_SETPRIORITY) { 337 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 338 339 if(p > PRIO_MAX) { 340#ifndef __NETBSD_SYSCALLS 341 rtp.type = RTP_PRIO_IDLE; 342 rtp.prio = p - PRIO_MAX - 1; 343 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p; 344 if(rtprio(RTP_SET, 0, &rtp)) 345 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 346 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 347#endif 348 } else if(p < PRIO_MIN) { 349#ifndef __NETBSD_SYSCALLS 350 rtp.type = RTP_PRIO_REALTIME; 351 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX); 352 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p; 353 if(rtprio(RTP_SET, 0, &rtp)) 354 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 355 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 356#endif 357 } else { 358 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 359 syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 360 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 361 } 362 } 363 364 /* Setup the user's group permissions */ 365 if (flags & LOGIN_SETGROUP) { 366 if (setgid(pwd->pw_gid) != 0) { 367 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 368 login_close(llc); 369 return -1; 370 } 371 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 372 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 373 (u_long)pwd->pw_gid); 374 login_close(llc); 375 return -1; 376 } 377 } 378 379 /* Set up the user's MAC label. */ 380 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 381 const char *label_string; 382 mac_t label; 383 384 label_string = login_getcapstr(lc, "label", NULL, NULL); 385 if (label_string != NULL) { 386 if (mac_from_text(&label, label_string) == -1) { 387 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 388 pwd->pw_name, label_string); 389 return -1; 390 } 391 if (mac_set_proc(label) == -1) 392 error = errno; 393 else 394 error = 0; 395 mac_free(label); 396 if (error != 0) { 397 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 398 label_string, pwd->pw_name, strerror(error)); 399 return -1; 400 } 401 } 402 } 403 404 /* Set the sessions login */ 405 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 406 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 407 login_close(llc); 408 return -1; 409 } 410 411 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 412 mymask = setlogincontext(lc, pwd, mymask, flags); 413 login_close(llc); 414 415 /* This needs to be done after anything that needs root privs */ 416 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 417 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 418 return -1; /* Paranoia again */ 419 } 420 421 /* 422 * Now, we repeat some of the above for the user's private entries 423 */ 424 if ((lc = login_getuserclass(pwd)) != NULL) { 425 mymask = setlogincontext(lc, pwd, mymask, flags); 426 login_close(lc); 427 } 428 429 /* Finally, set any umask we've found */ 430 if (flags & LOGIN_SETUMASK) 431 umask(mymask); 432 433 return 0; 434} 435
| 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#ifndef __NETBSD_SYSCALLS 319 struct rtprio rtp; 320#endif 321 int error; 322 323 if (lc == NULL) { 324 if (pwd != NULL && (lc = login_getpwclass(pwd)) != NULL) 325 llc = lc; /* free this when we're done */ 326 } 327 328 if (flags & LOGIN_SETPATH) 329 pathvars[0].def = uid ? _PATH_DEFPATH : _PATH_STDPATH; 330 331 /* we need a passwd entry to set these */ 332 if (pwd == NULL) 333 flags &= ~(LOGIN_SETGROUP | LOGIN_SETLOGIN | LOGIN_SETMAC); 334 335 /* Set the process priority */ 336 if (flags & LOGIN_SETPRIORITY) { 337 p = login_getcapnum(lc, "priority", LOGIN_DEFPRI, LOGIN_DEFPRI); 338 339 if(p > PRIO_MAX) { 340#ifndef __NETBSD_SYSCALLS 341 rtp.type = RTP_PRIO_IDLE; 342 rtp.prio = p - PRIO_MAX - 1; 343 p = (rtp.prio > RTP_PRIO_MAX) ? 31 : p; 344 if(rtprio(RTP_SET, 0, &rtp)) 345 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 346 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 347#endif 348 } else if(p < PRIO_MIN) { 349#ifndef __NETBSD_SYSCALLS 350 rtp.type = RTP_PRIO_REALTIME; 351 rtp.prio = abs(p - PRIO_MIN + RTP_PRIO_MAX); 352 p = (rtp.prio > RTP_PRIO_MAX) ? 1 : p; 353 if(rtprio(RTP_SET, 0, &rtp)) 354 syslog(LOG_WARNING, "rtprio '%s' (%s): %m", 355 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 356#endif 357 } else { 358 if (setpriority(PRIO_PROCESS, 0, (int)p) != 0) 359 syslog(LOG_WARNING, "setpriority '%s' (%s): %m", 360 pwd->pw_name, lc ? lc->lc_class : LOGIN_DEFCLASS); 361 } 362 } 363 364 /* Setup the user's group permissions */ 365 if (flags & LOGIN_SETGROUP) { 366 if (setgid(pwd->pw_gid) != 0) { 367 syslog(LOG_ERR, "setgid(%lu): %m", (u_long)pwd->pw_gid); 368 login_close(llc); 369 return -1; 370 } 371 if (initgroups(pwd->pw_name, pwd->pw_gid) == -1) { 372 syslog(LOG_ERR, "initgroups(%s,%lu): %m", pwd->pw_name, 373 (u_long)pwd->pw_gid); 374 login_close(llc); 375 return -1; 376 } 377 } 378 379 /* Set up the user's MAC label. */ 380 if ((flags & LOGIN_SETMAC) && mac_is_present(NULL) == 1) { 381 const char *label_string; 382 mac_t label; 383 384 label_string = login_getcapstr(lc, "label", NULL, NULL); 385 if (label_string != NULL) { 386 if (mac_from_text(&label, label_string) == -1) { 387 syslog(LOG_ERR, "mac_from_text('%s') for %s: %m", 388 pwd->pw_name, label_string); 389 return -1; 390 } 391 if (mac_set_proc(label) == -1) 392 error = errno; 393 else 394 error = 0; 395 mac_free(label); 396 if (error != 0) { 397 syslog(LOG_ERR, "mac_set_proc('%s') for %s: %s", 398 label_string, pwd->pw_name, strerror(error)); 399 return -1; 400 } 401 } 402 } 403 404 /* Set the sessions login */ 405 if ((flags & LOGIN_SETLOGIN) && setlogin(pwd->pw_name) != 0) { 406 syslog(LOG_ERR, "setlogin(%s): %m", pwd->pw_name); 407 login_close(llc); 408 return -1; 409 } 410 411 mymask = (flags & LOGIN_SETUMASK) ? umask(LOGIN_DEFUMASK) : 0; 412 mymask = setlogincontext(lc, pwd, mymask, flags); 413 login_close(llc); 414 415 /* This needs to be done after anything that needs root privs */ 416 if ((flags & LOGIN_SETUSER) && setuid(uid) != 0) { 417 syslog(LOG_ERR, "setuid(%lu): %m", (u_long)uid); 418 return -1; /* Paranoia again */ 419 } 420 421 /* 422 * Now, we repeat some of the above for the user's private entries 423 */ 424 if ((lc = login_getuserclass(pwd)) != NULL) { 425 mymask = setlogincontext(lc, pwd, mymask, flags); 426 login_close(lc); 427 } 428 429 /* Finally, set any umask we've found */ 430 if (flags & LOGIN_SETUMASK) 431 umask(mymask); 432 433 return 0; 434} 435
|