| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#define _GNU_SOURCE |
| 50 |
|
| 51 |
#if defined(AUDIT_LOG_PRIV) && !defined(USE_AUDIT) |
| 52 |
#error AUDIT_LOG_PRIV needs the USE_AUDIT option |
| 53 |
#endif |
| 54 |
#if defined(NAMESPACE_PRIV) && !defined(USE_PAM) |
| 55 |
#error NAMESPACE_PRIV needs the USE_PAM option |
| 56 |
#endif |
| 57 |
|
| 58 |
#include <stdio.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <pwd.h> |
| 61 |
#include <ctype.h> |
| 62 |
#include <sys/types.h> |
| 63 |
#include <sys/wait.h> |
| 64 |
#include <getopt.h> |
| 65 |
#include <fcntl.h> |
| 66 |
#include <string.h> |
| 67 |
#include <errno.h> |
| 68 |
#include <selinux/selinux.h> |
| 69 |
#include <selinux/context.h> |
| 70 |
#include <selinux/get_default_type.h> |
| 71 |
#include <selinux/get_context_list.h> |
| 72 |
#include <signal.h> |
| 73 |
#include <unistd.h> |
| 74 |
#ifdef USE_AUDIT |
| 75 |
#include <libaudit.h> |
| 76 |
#endif |
| 77 |
#if defined(AUDIT_LOG_PRIV) || defined(NAMESPACE_PRIV) |
| 78 |
#include <sys/prctl.h> |
| 79 |
#include <cap-ng.h> |
| 80 |
#endif |
| 81 |
#ifdef USE_NLS |
| 82 |
#include <locale.h> |
| 83 |
#include <libintl.h> |
| 84 |
#define _(msgid) gettext (msgid) |
| 85 |
#else |
| 86 |
#define _(msgid) (msgid) |
| 87 |
#endif |
| 88 |
#ifndef PACKAGE |
| 89 |
#define PACKAGE "policycoreutils" |
| 90 |
#endif |
| 91 |
|
| 92 |
#define TRUE 1 |
| 93 |
#define FALSE 0 |
| 94 |
|
| 95 |
|
| 96 |
#define USAGE_STRING "USAGE: newrole [ -r role ] [ -t type ] [ -l level ] [ -p ] [ -V ] [ -- args ]" |
| 97 |
|
| 98 |
#ifdef USE_PAM |
| 99 |
#define PAM_SERVICE_CONFIG "/etc/selinux/newrole_pam.conf"; |
| 100 |
#endif |
| 101 |
|
| 102 |
#define DEFAULT_PATH "/usr/bin:/bin" |
| 103 |
#define DEFAULT_CONTEXT_SIZE 255 |
| 104 |
|
| 105 |
extern char **environ; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static char *build_new_range(char *newlevel, const char *range) |
| 119 |
{ |
| 120 |
char *newrangep = NULL; |
| 121 |
const char *tmpptr; |
| 122 |
size_t len; |
| 123 |
|
| 124 |
|
| 125 |
if (!range || !strlen(range) || !newlevel || !strlen(newlevel)) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
|
| 129 |
if (strchr(newlevel, '-')) { |
| 130 |
newrangep = strdup(newlevel); |
| 131 |
return newrangep; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
tmpptr = strchr(range, '-'); |
| 136 |
if (tmpptr) { |
| 137 |
|
| 138 |
len = strlen(newlevel) + 1 + strlen(tmpptr + 1) + 1; |
| 139 |
newrangep = (char *)malloc(len); |
| 140 |
if (!newrangep) |
| 141 |
return NULL; |
| 142 |
snprintf(newrangep, len, "%s-%s", newlevel, tmpptr + 1); |
| 143 |
} else { |
| 144 |
|
| 145 |
if (!strcmp(newlevel, range)) { |
| 146 |
newrangep = strdup(range); |
| 147 |
} else { |
| 148 |
len = strlen(newlevel) + 1 + strlen(range) + 1; |
| 149 |
newrangep = (char *)malloc(len); |
| 150 |
if (!newrangep) |
| 151 |
return NULL; |
| 152 |
snprintf(newrangep, len, "%s-%s", newlevel, range); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return newrangep; |
| 157 |
} |
| 158 |
|
| 159 |
#ifdef USE_PAM |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#include <security/pam_appl.h> |
| 167 |
#include <security/pam_misc.h> |
| 168 |
|
| 169 |
const char *service_name = "newrole"; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
int authenticate_via_pam(const char *ttyn, pam_handle_t * pam_handle) |
| 186 |
{ |
| 187 |
|
| 188 |
int result = 0; |
| 189 |
int pam_rc; |
| 190 |
const char *tty_name; |
| 191 |
|
| 192 |
if (ttyn) { |
| 193 |
if (strncmp(ttyn, "/dev/", 5) == 0) |
| 194 |
tty_name = ttyn + 5; |
| 195 |
else |
| 196 |
tty_name = ttyn; |
| 197 |
|
| 198 |
pam_rc = pam_set_item(pam_handle, PAM_TTY, tty_name); |
| 199 |
if (pam_rc != PAM_SUCCESS) { |
| 200 |
fprintf(stderr, _("failed to set PAM_TTY\n")); |
| 201 |
goto out; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
pam_rc = pam_authenticate(pam_handle, 0); |
| 207 |
if (pam_rc != PAM_SUCCESS) { |
| 208 |
goto out; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
pam_rc = pam_acct_mgmt(pam_handle, 0); |
| 213 |
if (pam_rc == PAM_SUCCESS) { |
| 214 |
result = 1; |
| 215 |
} |
| 216 |
|
| 217 |
out: |
| 218 |
return result; |
| 219 |
} |
| 220 |
|
| 221 |
#include "hashtab.h" |
| 222 |
|
| 223 |
static int free_hashtab_entry(hashtab_key_t key, hashtab_datum_t d, |
| 224 |
void *args __attribute__ ((unused))) |
| 225 |
{ |
| 226 |
free(key); |
| 227 |
free(d); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
static unsigned int reqsymhash(hashtab_t h, const_hashtab_key_t key) |
| 232 |
{ |
| 233 |
char *p, *keyp; |
| 234 |
size_t size; |
| 235 |
unsigned int val; |
| 236 |
|
| 237 |
val = 0; |
| 238 |
keyp = (char *)key; |
| 239 |
size = strlen(keyp); |
| 240 |
for (p = keyp; ((size_t) (p - keyp)) < size; p++) |
| 241 |
val = |
| 242 |
(val << 4 | (val >> (8 * sizeof(unsigned int) - 4))) ^ (*p); |
| 243 |
return val & (h->size - 1); |
| 244 |
} |
| 245 |
|
| 246 |
static int reqsymcmp(hashtab_t h |
| 247 |
__attribute__ ((unused)), const_hashtab_key_t key1, |
| 248 |
const_hashtab_key_t key2) |
| 249 |
{ |
| 250 |
return strcmp(key1, key2); |
| 251 |
} |
| 252 |
|
| 253 |
static hashtab_t app_service_names = NULL; |
| 254 |
#define PAM_SERVICE_SLOTS 64 |
| 255 |
|
| 256 |
static int process_pam_config(FILE * cfg) |
| 257 |
{ |
| 258 |
const char *config_file_path = PAM_SERVICE_CONFIG; |
| 259 |
char *line_buf = NULL; |
| 260 |
unsigned long lineno = 0; |
| 261 |
size_t len = 0; |
| 262 |
char *app = NULL; |
| 263 |
char *service = NULL; |
| 264 |
int ret; |
| 265 |
|
| 266 |
while (getline(&line_buf, &len, cfg) > 0) { |
| 267 |
char *buffer = line_buf; |
| 268 |
lineno++; |
| 269 |
while (isspace(*buffer)) |
| 270 |
buffer++; |
| 271 |
if (buffer[0] == '#') |
| 272 |
continue; |
| 273 |
if (buffer[0] == '\n' || buffer[0] == '\0') |
| 274 |
continue; |
| 275 |
|
| 276 |
app = service = NULL; |
| 277 |
ret = sscanf(buffer, "%ms %ms\n", &app, &service); |
| 278 |
if (ret < 2 || !app || !service) |
| 279 |
goto err; |
| 280 |
|
| 281 |
ret = hashtab_insert(app_service_names, app, service); |
| 282 |
if (ret == HASHTAB_OVERFLOW) { |
| 283 |
fprintf(stderr, |
| 284 |
_ |
| 285 |
("newrole: service name configuration hashtable overflow\n")); |
| 286 |
goto err; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
free(line_buf); |
| 291 |
return 0; |
| 292 |
err: |
| 293 |
free(app); |
| 294 |
free(service); |
| 295 |
fprintf(stderr, _("newrole: %s: error on line %lu.\n"), |
| 296 |
config_file_path, lineno); |
| 297 |
free(line_buf); |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
static int read_pam_config(void) |
| 307 |
{ |
| 308 |
const char *config_file_path = PAM_SERVICE_CONFIG; |
| 309 |
FILE *cfg = NULL; |
| 310 |
cfg = fopen(config_file_path, "r"); |
| 311 |
if (!cfg) |
| 312 |
return 0; |
| 313 |
app_service_names = |
| 314 |
hashtab_create(reqsymhash, reqsymcmp, PAM_SERVICE_SLOTS); |
| 315 |
if (!app_service_names) |
| 316 |
goto err; |
| 317 |
if (process_pam_config(cfg)) |
| 318 |
goto err; |
| 319 |
fclose(cfg); |
| 320 |
return 0; |
| 321 |
err: |
| 322 |
fclose(cfg); |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
#else |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
#include <shadow.h> |
| 334 |
#include <string.h> |
| 335 |
|
| 336 |
#define PASSWORD_PROMPT _("Password:") |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
int authenticate_via_shadow_passwd(const char *uname) |
| 352 |
{ |
| 353 |
struct spwd *p_shadow_line; |
| 354 |
char *unencrypted_password_s; |
| 355 |
char *encrypted_password_s; |
| 356 |
|
| 357 |
setspent(); |
| 358 |
p_shadow_line = getspnam(uname); |
| 359 |
endspent(); |
| 360 |
if (!(p_shadow_line)) { |
| 361 |
fprintf(stderr, _("Cannot find your entry in the shadow " |
| 362 |
"passwd file.\n")); |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
if (!(unencrypted_password_s = getpass(PASSWORD_PROMPT))) { |
| 368 |
fprintf(stderr, _("getpass cannot open /dev/tty\n")); |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
encrypted_password_s = crypt(unencrypted_password_s, |
| 374 |
p_shadow_line->sp_pwdp); |
| 375 |
memset(unencrypted_password_s, 0, strlen(unencrypted_password_s)); |
| 376 |
return (!strcmp(encrypted_password_s, p_shadow_line->sp_pwdp)); |
| 377 |
} |
| 378 |
#endif |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static int verify_shell(const char *shell_name) |
| 385 |
{ |
| 386 |
int found = 0; |
| 387 |
const char *buf; |
| 388 |
|
| 389 |
if (!(shell_name && shell_name[0])) |
| 390 |
return found; |
| 391 |
|
| 392 |
while ((buf = getusershell()) != NULL) { |
| 393 |
|
| 394 |
if (*buf == '#') |
| 395 |
continue; |
| 396 |
|
| 397 |
|
| 398 |
if (!strcmp(shell_name, buf)) { |
| 399 |
found = 1; |
| 400 |
break; |
| 401 |
} |
| 402 |
} |
| 403 |
endusershell(); |
| 404 |
return found; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
static int extract_pw_data(struct passwd *pw_copy) |
| 416 |
{ |
| 417 |
uid_t uid; |
| 418 |
struct passwd *pw; |
| 419 |
|
| 420 |
#ifdef USE_AUDIT |
| 421 |
uid = audit_getloginuid(); |
| 422 |
if (uid == (uid_t) - 1) |
| 423 |
uid = getuid(); |
| 424 |
#else |
| 425 |
uid = getuid(); |
| 426 |
#endif |
| 427 |
|
| 428 |
setpwent(); |
| 429 |
pw = getpwuid(uid); |
| 430 |
endpwent(); |
| 431 |
if (!(pw && pw->pw_name && pw->pw_name[0] && pw->pw_shell |
| 432 |
&& pw->pw_shell[0] && pw->pw_dir && pw->pw_dir[0])) { |
| 433 |
fprintf(stderr, |
| 434 |
_("cannot find valid entry in the passwd file.\n")); |
| 435 |
return -1; |
| 436 |
} |
| 437 |
|
| 438 |
*pw_copy = *pw; |
| 439 |
pw = pw_copy; |
| 440 |
pw->pw_name = strdup(pw->pw_name); |
| 441 |
pw->pw_dir = strdup(pw->pw_dir); |
| 442 |
pw->pw_shell = strdup(pw->pw_shell); |
| 443 |
|
| 444 |
if (!(pw->pw_name && pw->pw_dir && pw->pw_shell)) { |
| 445 |
fprintf(stderr, _("Out of memory!\n")); |
| 446 |
goto out_free; |
| 447 |
} |
| 448 |
|
| 449 |
if (verify_shell(pw->pw_shell) == 0) { |
| 450 |
fprintf(stderr, _("Error! Shell is not valid.\n")); |
| 451 |
goto out_free; |
| 452 |
} |
| 453 |
return 0; |
| 454 |
|
| 455 |
out_free: |
| 456 |
free(pw->pw_name); |
| 457 |
free(pw->pw_dir); |
| 458 |
free(pw->pw_shell); |
| 459 |
pw->pw_name = NULL; |
| 460 |
pw->pw_dir = NULL; |
| 461 |
pw->pw_shell = NULL; |
| 462 |
return -1; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
static int restore_environment(int preserve_environment, |
| 476 |
char **old_environ, const struct passwd *pw) |
| 477 |
{ |
| 478 |
char const *term_env; |
| 479 |
char const *display_env; |
| 480 |
char const *xauthority_env; |
| 481 |
char *term = NULL; |
| 482 |
char *display = NULL; |
| 483 |
char *xauthority = NULL; |
| 484 |
int rc; |
| 485 |
|
| 486 |
environ = old_environ; |
| 487 |
|
| 488 |
if (preserve_environment) |
| 489 |
return 0; |
| 490 |
|
| 491 |
term_env = getenv("TERM"); |
| 492 |
display_env = getenv("DISPLAY"); |
| 493 |
xauthority_env = getenv("XAUTHORITY"); |
| 494 |
|
| 495 |
|
| 496 |
if (term_env) |
| 497 |
term = strdup(term_env); |
| 498 |
if (display_env) |
| 499 |
display = strdup(display_env); |
| 500 |
if (xauthority_env) |
| 501 |
xauthority = strdup(xauthority_env); |
| 502 |
if ((term_env && !term) || (display_env && !display) || |
| 503 |
(xauthority_env && !xauthority)) { |
| 504 |
rc = -1; |
| 505 |
goto out; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
if ((rc = clearenv())) { |
| 510 |
fprintf(stderr, _("Unable to clear environment\n")); |
| 511 |
goto out; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
if (term) |
| 516 |
rc |= setenv("TERM", term, 1); |
| 517 |
if (display) |
| 518 |
rc |= setenv("DISPLAY", display, 1); |
| 519 |
if (xauthority) |
| 520 |
rc |= setenv("XAUTHORITY", xauthority, 1); |
| 521 |
rc |= setenv("HOME", pw->pw_dir, 1); |
| 522 |
rc |= setenv("SHELL", pw->pw_shell, 1); |
| 523 |
rc |= setenv("USER", pw->pw_name, 1); |
| 524 |
rc |= setenv("LOGNAME", pw->pw_name, 1); |
| 525 |
rc |= setenv("PATH", DEFAULT_PATH, 1); |
| 526 |
out: |
| 527 |
free(term); |
| 528 |
free(display); |
| 529 |
free(xauthority); |
| 530 |
return rc; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
#if defined(AUDIT_LOG_PRIV) && !defined(NAMESPACE_PRIV) |
| 542 |
static int drop_capabilities(int full) |
| 543 |
{ |
| 544 |
uid_t uid = getuid(); |
| 545 |
if (!uid) return 0; |
| 546 |
|
| 547 |
capng_setpid(getpid()); |
| 548 |
capng_clear(CAPNG_SELECT_CAPS); |
| 549 |
|
| 550 |
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) { |
| 551 |
fprintf(stderr, _("Error resetting KEEPCAPS, aborting\n")); |
| 552 |
return -1; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
if (setresuid(uid, uid, uid)) { |
| 557 |
fprintf(stderr, _("Error changing uid, aborting.\n")); |
| 558 |
return -1; |
| 559 |
} |
| 560 |
|
| 561 |
if (prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) < 0) { |
| 562 |
fprintf(stderr, _("Error resetting KEEPCAPS, aborting\n")); |
| 563 |
return -1; |
| 564 |
} |
| 565 |
|
| 566 |
if (! full) |
| 567 |
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, CAP_AUDIT_WRITE); |
| 568 |
return capng_apply(CAPNG_SELECT_CAPS); |
| 569 |
} |
| 570 |
#elif defined(NAMESPACE_PRIV) |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
static int drop_capabilities(int full) |
| 585 |
{ |
| 586 |
uid_t uid = getuid(); |
| 587 |
if (!uid) return 0; |
| 588 |
|
| 589 |
capng_setpid(getpid()); |
| 590 |
capng_clear(CAPNG_SELECT_CAPS); |
| 591 |
|
| 592 |
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) { |
| 593 |
fprintf(stderr, _("Error resetting KEEPCAPS, aborting\n")); |
| 594 |
return -1; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
if (setresuid(uid, uid, uid)) { |
| 599 |
fprintf(stderr, _("Error changing uid, aborting.\n")); |
| 600 |
return -1; |
| 601 |
} |
| 602 |
|
| 603 |
if (prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) < 0) { |
| 604 |
fprintf(stderr, _("Error resetting KEEPCAPS, aborting\n")); |
| 605 |
return -1; |
| 606 |
} |
| 607 |
|
| 608 |
if (! full) |
| 609 |
capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, CAP_SYS_ADMIN , CAP_FOWNER , CAP_CHOWN, CAP_DAC_OVERRIDE, CAP_AUDIT_WRITE, -1); |
| 610 |
|
| 611 |
return capng_apply(CAPNG_SELECT_CAPS); |
| 612 |
} |
| 613 |
|
| 614 |
#else |
| 615 |
static inline int drop_capabilities(__attribute__ ((__unused__)) int full) |
| 616 |
{ |
| 617 |
return 0; |
| 618 |
} |
| 619 |
#endif |
| 620 |
|
| 621 |
#ifdef NAMESPACE_PRIV |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
static int transition_to_caller_uid() |
| 627 |
{ |
| 628 |
uid_t uid = getuid(); |
| 629 |
|
| 630 |
if (prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0) < 0) { |
| 631 |
fprintf(stderr, _("Error resetting KEEPCAPS, aborting\n")); |
| 632 |
return -1; |
| 633 |
} |
| 634 |
|
| 635 |
if (setresuid(uid, uid, uid)) { |
| 636 |
fprintf(stderr, _("Error changing uid, aborting.\n")); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
return 0; |
| 640 |
} |
| 641 |
#endif |
| 642 |
|
| 643 |
#ifdef AUDIT_LOG_PRIV |
| 644 |
|
| 645 |
static |
| 646 |
int send_audit_message(int success, security_context_t old_context, |
| 647 |
security_context_t new_context, const char *ttyn) |
| 648 |
{ |
| 649 |
char *msg = NULL; |
| 650 |
int rc; |
| 651 |
int audit_fd = audit_open(); |
| 652 |
|
| 653 |
if (audit_fd < 0) { |
| 654 |
fprintf(stderr, _("Error connecting to audit system.\n")); |
| 655 |
return -1; |
| 656 |
} |
| 657 |
if (asprintf(&msg, "newrole: old-context=%s new-context=%s", |
| 658 |
old_context ? old_context : "?", |
| 659 |
new_context ? new_context : "?") < 0) { |
| 660 |
fprintf(stderr, _("Error allocating memory.\n")); |
| 661 |
rc = -1; |
| 662 |
goto out; |
| 663 |
} |
| 664 |
rc = audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE, |
| 665 |
msg, NULL, NULL, ttyn, success); |
| 666 |
if (rc <= 0) { |
| 667 |
fprintf(stderr, _("Error sending audit message.\n")); |
| 668 |
rc = -1; |
| 669 |
goto out; |
| 670 |
} |
| 671 |
rc = 0; |
| 672 |
out: |
| 673 |
free(msg); |
| 674 |
close(audit_fd); |
| 675 |
return rc; |
| 676 |
} |
| 677 |
#else |
| 678 |
static inline |
| 679 |
int send_audit_message(int success __attribute__ ((unused)), |
| 680 |
security_context_t old_context |
| 681 |
__attribute__ ((unused)), |
| 682 |
security_context_t new_context |
| 683 |
__attribute__ ((unused)), const char *ttyn |
| 684 |
__attribute__ ((unused))) |
| 685 |
{ |
| 686 |
return 0; |
| 687 |
} |
| 688 |
#endif |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
static int relabel_tty(const char *ttyn, security_context_t new_context, |
| 699 |
security_context_t * tty_context, |
| 700 |
security_context_t * new_tty_context) |
| 701 |
{ |
| 702 |
int fd, rc; |
| 703 |
int enforcing = security_getenforce(); |
| 704 |
security_context_t tty_con = NULL; |
| 705 |
security_context_t new_tty_con = NULL; |
| 706 |
|
| 707 |
if (!ttyn) |
| 708 |
return 0; |
| 709 |
|
| 710 |
if (enforcing < 0) { |
| 711 |
fprintf(stderr, _("Could not determine enforcing mode.\n")); |
| 712 |
return -1; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
fd = open(ttyn, O_RDWR | O_NONBLOCK); |
| 717 |
if (fd < 0) { |
| 718 |
fprintf(stderr, _("Error! Could not open %s.\n"), ttyn); |
| 719 |
return fd; |
| 720 |
} |
| 721 |
|
| 722 |
rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
| 723 |
if (rc) { |
| 724 |
fprintf(stderr, _("Error! Could not clear O_NONBLOCK on %s\n"), ttyn); |
| 725 |
close(fd); |
| 726 |
return rc; |
| 727 |
} |
| 728 |
|
| 729 |
if (fgetfilecon(fd, &tty_con) < 0) { |
| 730 |
fprintf(stderr, _("%s! Could not get current context " |
| 731 |
"for %s, not relabeling tty.\n"), |
| 732 |
enforcing ? "Error" : "Warning", ttyn); |
| 733 |
if (enforcing) |
| 734 |
goto close_fd; |
| 735 |
} |
| 736 |
|
| 737 |
if (tty_con && |
| 738 |
(security_compute_relabel(new_context, tty_con, |
| 739 |
string_to_security_class("chr_file"), &new_tty_con) < 0)) { |
| 740 |
fprintf(stderr, _("%s! Could not get new context for %s, " |
| 741 |
"not relabeling tty.\n"), |
| 742 |
enforcing ? "Error" : "Warning", ttyn); |
| 743 |
if (enforcing) |
| 744 |
goto close_fd; |
| 745 |
} |
| 746 |
|
| 747 |
if (new_tty_con) |
| 748 |
if (fsetfilecon(fd, new_tty_con) < 0) { |
| 749 |
fprintf(stderr, |
| 750 |
_("%s! Could not set new context for %s\n"), |
| 751 |
enforcing ? "Error" : "Warning", ttyn); |
| 752 |
freecon(new_tty_con); |
| 753 |
new_tty_con = NULL; |
| 754 |
if (enforcing) |
| 755 |
goto close_fd; |
| 756 |
} |
| 757 |
|
| 758 |
*tty_context = tty_con; |
| 759 |
*new_tty_context = new_tty_con; |
| 760 |
return fd; |
| 761 |
|
| 762 |
close_fd: |
| 763 |
freecon(tty_con); |
| 764 |
close(fd); |
| 765 |
return -1; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
static int restore_tty_label(int fd, const char *ttyn, |
| 778 |
security_context_t tty_context, |
| 779 |
security_context_t new_tty_context) |
| 780 |
{ |
| 781 |
int rc = 0; |
| 782 |
security_context_t chk_tty_context = NULL; |
| 783 |
|
| 784 |
if (!ttyn) |
| 785 |
goto skip_relabel; |
| 786 |
|
| 787 |
if (!new_tty_context) |
| 788 |
goto skip_relabel; |
| 789 |
|
| 790 |
|
| 791 |
if ((rc = fgetfilecon(fd, &chk_tty_context)) < 0) { |
| 792 |
fprintf(stderr, "Could not fgetfilecon %s.\n", ttyn); |
| 793 |
goto skip_relabel; |
| 794 |
} |
| 795 |
|
| 796 |
if ((rc = strcmp(chk_tty_context, new_tty_context))) { |
| 797 |
fprintf(stderr, _("%s changed labels.\n"), ttyn); |
| 798 |
goto skip_relabel; |
| 799 |
} |
| 800 |
|
| 801 |
if ((rc = fsetfilecon(fd, tty_context)) < 0) |
| 802 |
fprintf(stderr, |
| 803 |
_("Warning! Could not restore context for %s\n"), ttyn); |
| 804 |
skip_relabel: |
| 805 |
freecon(chk_tty_context); |
| 806 |
return rc; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
static int parse_command_line_arguments(int argc, char **argv, char *ttyn, |
| 819 |
security_context_t old_context, |
| 820 |
security_context_t * new_context, |
| 821 |
int *preserve_environment) |
| 822 |
{ |
| 823 |
int flag_index; |
| 824 |
int clflag; |
| 825 |
char *role_s = NULL; |
| 826 |
char *type_s = NULL; |
| 827 |
char *type_ptr = NULL; |
| 828 |
char *level_s = NULL; |
| 829 |
char *range_ptr = NULL; |
| 830 |
security_context_t new_con = NULL; |
| 831 |
security_context_t tty_con = NULL; |
| 832 |
context_t context = NULL; |
| 833 |
const struct option long_options[] = { |
| 834 |
{"role", 1, 0, 'r'}, |
| 835 |
{"type", 1, 0, 't'}, |
| 836 |
{"level", 1, 0, 'l'}, |
| 837 |
{"preserve-environment", 0, 0, 'p'}, |
| 838 |
{"version", 0, 0, 'V'}, |
| 839 |
{NULL, 0, 0, 0} |
| 840 |
}; |
| 841 |
|
| 842 |
*preserve_environment = 0; |
| 843 |
while (1) { |
| 844 |
clflag = getopt_long(argc, argv, "r:t:l:pV", long_options, |
| 845 |
&flag_index); |
| 846 |
if (clflag == -1) |
| 847 |
break; |
| 848 |
|
| 849 |
switch (clflag) { |
| 850 |
case 'V': |
| 851 |
printf("newrole: %s version %s\n", PACKAGE, VERSION); |
| 852 |
exit(0); |
| 853 |
break; |
| 854 |
case 'p': |
| 855 |
*preserve_environment = 1; |
| 856 |
break; |
| 857 |
case 'r': |
| 858 |
if (role_s) { |
| 859 |
fprintf(stderr, |
| 860 |
_("Error: multiple roles specified\n")); |
| 861 |
return -1; |
| 862 |
} |
| 863 |
role_s = optarg; |
| 864 |
break; |
| 865 |
case 't': |
| 866 |
if (type_s) { |
| 867 |
fprintf(stderr, |
| 868 |
_("Error: multiple types specified\n")); |
| 869 |
return -1; |
| 870 |
} |
| 871 |
type_s = optarg; |
| 872 |
break; |
| 873 |
case 'l': |
| 874 |
if (!is_selinux_mls_enabled()) { |
| 875 |
fprintf(stderr, _("Sorry, -l may be used with " |
| 876 |
"SELinux MLS support.\n")); |
| 877 |
return -1; |
| 878 |
} |
| 879 |
if (level_s) { |
| 880 |
fprintf(stderr, _("Error: multiple levels " |
| 881 |
"specified\n")); |
| 882 |
return -1; |
| 883 |
} |
| 884 |
if (ttyn) { |
| 885 |
if (fgetfilecon(STDIN_FILENO, &tty_con) >= 0) { |
| 886 |
if (selinux_check_securetty_context |
| 887 |
(tty_con) < 0) { |
| 888 |
fprintf(stderr, |
| 889 |
_ |
| 890 |
("Error: you are not allowed to change levels on a non secure terminal \n")); |
| 891 |
freecon(tty_con); |
| 892 |
return -1; |
| 893 |
} |
| 894 |
freecon(tty_con); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
level_s = optarg; |
| 899 |
break; |
| 900 |
default: |
| 901 |
fprintf(stderr, "%s\n", USAGE_STRING); |
| 902 |
return -1; |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
if (!(role_s || type_s || level_s)) { |
| 908 |
fprintf(stderr, "%s\n", USAGE_STRING); |
| 909 |
return -1; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
if (role_s && !type_s) { |
| 914 |
|
| 915 |
if (get_default_type(role_s, &type_ptr)) { |
| 916 |
fprintf(stderr, _("Couldn't get default type.\n")); |
| 917 |
send_audit_message(0, old_context, new_con, ttyn); |
| 918 |
return -1; |
| 919 |
} |
| 920 |
type_s = type_ptr; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
context = context_new(old_context); |
| 925 |
if (!context) { |
| 926 |
fprintf(stderr, _("failed to get new context.\n")); |
| 927 |
goto err_free; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
if (role_s) |
| 932 |
if (context_role_set(context, role_s)) { |
| 933 |
fprintf(stderr, _("failed to set new role %s\n"), |
| 934 |
role_s); |
| 935 |
goto err_free; |
| 936 |
} |
| 937 |
|
| 938 |
if (type_s) |
| 939 |
if (context_type_set(context, type_s)) { |
| 940 |
fprintf(stderr, _("failed to set new type %s\n"), |
| 941 |
type_s); |
| 942 |
goto err_free; |
| 943 |
} |
| 944 |
|
| 945 |
if (level_s) { |
| 946 |
range_ptr = |
| 947 |
build_new_range(level_s, context_range_get(context)); |
| 948 |
if (!range_ptr) { |
| 949 |
fprintf(stderr, |
| 950 |
_("failed to build new range with level %s\n"), |
| 951 |
level_s); |
| 952 |
goto err_free; |
| 953 |
} |
| 954 |
if (context_range_set(context, range_ptr)) { |
| 955 |
fprintf(stderr, _("failed to set new range %s\n"), |
| 956 |
range_ptr); |
| 957 |
goto err_free; |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
if (!(new_con = context_str(context))) { |
| 963 |
fprintf(stderr, _("failed to convert new context to string\n")); |
| 964 |
goto err_free; |
| 965 |
} |
| 966 |
|
| 967 |
if (security_check_context(new_con) < 0) { |
| 968 |
fprintf(stderr, _("%s is not a valid context\n"), new_con); |
| 969 |
send_audit_message(0, old_context, new_con, ttyn); |
| 970 |
goto err_free; |
| 971 |
} |
| 972 |
|
| 973 |
*new_context = strdup(new_con); |
| 974 |
if (!*new_context) { |
| 975 |
fprintf(stderr, _("Unable to allocate memory for new_context")); |
| 976 |
goto err_free; |
| 977 |
} |
| 978 |
|
| 979 |
free(type_ptr); |
| 980 |
free(range_ptr); |
| 981 |
context_free(context); |
| 982 |
return 0; |
| 983 |
|
| 984 |
err_free: |
| 985 |
free(type_ptr); |
| 986 |
free(range_ptr); |
| 987 |
|
| 988 |
context_free(context); |
| 989 |
return -1; |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
static int set_signal_handles(void) |
| 996 |
{ |
| 997 |
sigset_t empty; |
| 998 |
|
| 999 |
|
| 1000 |
if (sigemptyset(&empty)) { |
| 1001 |
fprintf(stderr, _("Unable to obtain empty signal set\n")); |
| 1002 |
return -1; |
| 1003 |
} |
| 1004 |
|
| 1005 |
(void)sigprocmask(SIG_SETMASK, &empty, NULL); |
| 1006 |
|
| 1007 |
|
| 1008 |
if (signal(SIGHUP, SIG_DFL) == SIG_ERR) { |
| 1009 |
fprintf(stderr, _("Unable to set SIGHUP handler\n")); |
| 1010 |
return -1; |
| 1011 |
} |
| 1012 |
|
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
int main(int argc, char *argv[]) |
| 1023 |
{ |
| 1024 |
security_context_t new_context = NULL; |
| 1025 |
security_context_t old_context = NULL; |
| 1026 |
security_context_t tty_context = NULL; |
| 1027 |
security_context_t new_tty_context = NULL; |
| 1028 |
|
| 1029 |
struct passwd pw; |
| 1030 |
char *ttyn = NULL; |
| 1031 |
|
| 1032 |
char **old_environ; |
| 1033 |
int preserve_environment; |
| 1034 |
|
| 1035 |
int fd; |
| 1036 |
pid_t childPid = 0; |
| 1037 |
char *shell_argv0 = NULL; |
| 1038 |
int rc; |
| 1039 |
|
| 1040 |
#ifdef USE_PAM |
| 1041 |
int pam_status; |
| 1042 |
pam_handle_t *pam_handle; |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
struct pam_conv pam_conversation = { |
| 1048 |
misc_conv, |
| 1049 |
NULL |
| 1050 |
}; |
| 1051 |
#endif |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
if (drop_capabilities(FALSE)) { |
| 1061 |
perror(_("Sorry, newrole failed to drop capabilities\n")); |
| 1062 |
return -1; |
| 1063 |
} |
| 1064 |
if (set_signal_handles()) |
| 1065 |
return -1; |
| 1066 |
|
| 1067 |
#ifdef USE_NLS |
| 1068 |
setlocale(LC_ALL, ""); |
| 1069 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 1070 |
textdomain(PACKAGE); |
| 1071 |
#endif |
| 1072 |
|
| 1073 |
old_environ = environ; |
| 1074 |
environ = NULL; |
| 1075 |
|
| 1076 |
if (!is_selinux_enabled()) { |
| 1077 |
fprintf(stderr, _("Sorry, newrole may be used only on " |
| 1078 |
"a SELinux kernel.\n")); |
| 1079 |
return -1; |
| 1080 |
} |
| 1081 |
|
| 1082 |
if (security_getenforce() < 0) { |
| 1083 |
fprintf(stderr, _("Could not determine enforcing mode.\n")); |
| 1084 |
return -1; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
if (getprevcon(&old_context)) { |
| 1094 |
fprintf(stderr, _("failed to get old_context.\n")); |
| 1095 |
return -1; |
| 1096 |
} |
| 1097 |
|
| 1098 |
ttyn = ttyname(STDIN_FILENO); |
| 1099 |
if (!ttyn || *ttyn == '\0') { |
| 1100 |
fprintf(stderr, |
| 1101 |
_("Warning! Could not retrieve tty information.\n")); |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (parse_command_line_arguments(argc, argv, ttyn, old_context, |
| 1105 |
&new_context, &preserve_environment)) |
| 1106 |
return -1; |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
memset(&pw, 0, sizeof(pw)); |
| 1117 |
if (extract_pw_data(&pw)) |
| 1118 |
goto err_free; |
| 1119 |
|
| 1120 |
#ifdef USE_PAM |
| 1121 |
if (read_pam_config()) { |
| 1122 |
fprintf(stderr, |
| 1123 |
_("error on reading PAM service configuration.\n")); |
| 1124 |
goto err_free; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (app_service_names != NULL && optind < argc) { |
| 1128 |
if (strcmp(argv[optind], "-c") == 0 && optind < (argc - 1)) { |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
char *cmd = NULL; |
| 1134 |
rc = sscanf(argv[optind + 1], "%ms", &cmd); |
| 1135 |
if (rc != EOF && cmd) { |
| 1136 |
char *app_service_name = |
| 1137 |
(char *)hashtab_search(app_service_names, |
| 1138 |
cmd); |
| 1139 |
free(cmd); |
| 1140 |
if (app_service_name != NULL) |
| 1141 |
service_name = app_service_name; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
pam_status = pam_start(service_name, pw.pw_name, &pam_conversation, |
| 1147 |
&pam_handle); |
| 1148 |
if (pam_status != PAM_SUCCESS) { |
| 1149 |
fprintf(stderr, _("failed to initialize PAM\n")); |
| 1150 |
goto err_free; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (!authenticate_via_pam(ttyn, pam_handle)) |
| 1154 |
#else |
| 1155 |
if (!authenticate_via_shadow_passwd(pw.pw_name)) |
| 1156 |
#endif |
| 1157 |
{ |
| 1158 |
fprintf(stderr, _("newrole: incorrect password for %s\n"), |
| 1159 |
pw.pw_name); |
| 1160 |
send_audit_message(0, old_context, new_context, ttyn); |
| 1161 |
goto err_close_pam; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
fd = relabel_tty(ttyn, new_context, &tty_context, &new_tty_context); |
| 1171 |
if (fd < 0) |
| 1172 |
goto err_close_pam; |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
childPid = fork(); |
| 1182 |
if (childPid < 0) { |
| 1183 |
|
| 1184 |
int errsv = errno; |
| 1185 |
fprintf(stderr, _("newrole: failure forking: %s"), |
| 1186 |
strerror(errsv)); |
| 1187 |
if (restore_tty_label(fd, ttyn, tty_context, new_tty_context)) |
| 1188 |
fprintf(stderr, _("Unable to restore tty label...\n")); |
| 1189 |
if (close(fd)) |
| 1190 |
fprintf(stderr, _("Failed to close tty properly\n")); |
| 1191 |
goto err_close_pam; |
| 1192 |
} else if (childPid) { |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
pid_t pid; |
| 1199 |
int exit_code = 0; |
| 1200 |
int status; |
| 1201 |
|
| 1202 |
do { |
| 1203 |
pid = wait(&status); |
| 1204 |
} while (pid < 0 && errno == EINTR); |
| 1205 |
|
| 1206 |
|
| 1207 |
if (WIFEXITED(status)) |
| 1208 |
exit_code = WEXITSTATUS(status); |
| 1209 |
|
| 1210 |
if (restore_tty_label(fd, ttyn, tty_context, new_tty_context)) { |
| 1211 |
fprintf(stderr, _("Unable to restore tty label...\n")); |
| 1212 |
exit_code = -1; |
| 1213 |
} |
| 1214 |
freecon(tty_context); |
| 1215 |
freecon(new_tty_context); |
| 1216 |
if (close(fd)) { |
| 1217 |
fprintf(stderr, _("Failed to close tty properly\n")); |
| 1218 |
exit_code = -1; |
| 1219 |
} |
| 1220 |
#ifdef USE_PAM |
| 1221 |
#ifdef NAMESPACE_PRIV |
| 1222 |
pam_status = pam_close_session(pam_handle, 0); |
| 1223 |
if (pam_status != PAM_SUCCESS) { |
| 1224 |
fprintf(stderr, "pam_close_session failed with %s\n", |
| 1225 |
pam_strerror(pam_handle, pam_status)); |
| 1226 |
exit_code = -1; |
| 1227 |
} |
| 1228 |
#endif |
| 1229 |
rc = pam_end(pam_handle, pam_status); |
| 1230 |
if (rc != PAM_SUCCESS) { |
| 1231 |
fprintf(stderr, "pam_end failed with %s\n", |
| 1232 |
pam_strerror(pam_handle, rc)); |
| 1233 |
exit_code = -1; |
| 1234 |
} |
| 1235 |
hashtab_map(app_service_names, free_hashtab_entry, NULL); |
| 1236 |
hashtab_destroy(app_service_names); |
| 1237 |
#endif |
| 1238 |
free(pw.pw_name); |
| 1239 |
free(pw.pw_dir); |
| 1240 |
free(pw.pw_shell); |
| 1241 |
free(shell_argv0); |
| 1242 |
return exit_code; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
if (ttyn) { |
| 1248 |
if (close(fd) || close(0) || close(1) || close(2)) { |
| 1249 |
fprintf(stderr, _("Could not close descriptors.\n")); |
| 1250 |
goto err_close_pam; |
| 1251 |
} |
| 1252 |
fd = open(ttyn, O_RDWR | O_NONBLOCK); |
| 1253 |
if (fd != 0) |
| 1254 |
goto err_close_pam; |
| 1255 |
rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
| 1256 |
if (rc) |
| 1257 |
goto err_close_pam; |
| 1258 |
|
| 1259 |
fd = open(ttyn, O_RDWR | O_NONBLOCK); |
| 1260 |
if (fd != 1) |
| 1261 |
goto err_close_pam; |
| 1262 |
rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
| 1263 |
if (rc) |
| 1264 |
goto err_close_pam; |
| 1265 |
|
| 1266 |
fd = open(ttyn, O_RDWR | O_NONBLOCK); |
| 1267 |
if (fd != 2) |
| 1268 |
goto err_close_pam; |
| 1269 |
rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
| 1270 |
if (rc) |
| 1271 |
goto err_close_pam; |
| 1272 |
|
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
if (optind < 1) |
| 1280 |
optind = 1; |
| 1281 |
|
| 1282 |
|
| 1283 |
if (asprintf(&shell_argv0, "-%s", pw.pw_shell) < 0) { |
| 1284 |
fprintf(stderr, _("Error allocating shell's argv0.\n")); |
| 1285 |
shell_argv0 = NULL; |
| 1286 |
goto err_close_pam; |
| 1287 |
} |
| 1288 |
argv[optind - 1] = shell_argv0; |
| 1289 |
|
| 1290 |
if (setexeccon(new_context)) { |
| 1291 |
fprintf(stderr, _("Could not set exec context to %s.\n"), |
| 1292 |
new_context); |
| 1293 |
goto err_close_pam; |
| 1294 |
} |
| 1295 |
#ifdef NAMESPACE_PRIV |
| 1296 |
|
| 1297 |
pam_status = pam_open_session(pam_handle, 0); |
| 1298 |
if (pam_status != PAM_SUCCESS) { |
| 1299 |
fprintf(stderr, "pam_open_session failed with %s\n", |
| 1300 |
pam_strerror(pam_handle, pam_status)); |
| 1301 |
goto err_close_pam; |
| 1302 |
} |
| 1303 |
#endif |
| 1304 |
|
| 1305 |
if (send_audit_message(1, old_context, new_context, ttyn)) { |
| 1306 |
fprintf(stderr, _("Failed to send audit message")); |
| 1307 |
goto err_close_pam_session; |
| 1308 |
} |
| 1309 |
freecon(old_context); old_context=NULL; |
| 1310 |
freecon(new_context); new_context=NULL; |
| 1311 |
|
| 1312 |
#ifdef NAMESPACE_PRIV |
| 1313 |
if (transition_to_caller_uid()) { |
| 1314 |
fprintf(stderr, _("Failed to transition to namespace\n")); |
| 1315 |
goto err_close_pam_session; |
| 1316 |
} |
| 1317 |
#endif |
| 1318 |
|
| 1319 |
if (drop_capabilities(TRUE)) { |
| 1320 |
fprintf(stderr, _("Failed to drop capabilities %m\n")); |
| 1321 |
goto err_close_pam_session; |
| 1322 |
} |
| 1323 |
|
| 1324 |
if (restore_environment(preserve_environment, old_environ, &pw)) { |
| 1325 |
fprintf(stderr, _("Unable to restore the environment, " |
| 1326 |
"aborting\n")); |
| 1327 |
goto err_close_pam_session; |
| 1328 |
} |
| 1329 |
execv(pw.pw_shell, argv + optind - 1); |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
perror(_("failed to exec shell\n")); |
| 1337 |
err_close_pam_session: |
| 1338 |
#ifdef NAMESPACE_PRIV |
| 1339 |
pam_status = pam_close_session(pam_handle, 0); |
| 1340 |
if (pam_status != PAM_SUCCESS) |
| 1341 |
fprintf(stderr, "pam_close_session failed with %s\n", |
| 1342 |
pam_strerror(pam_handle, pam_status)); |
| 1343 |
#endif |
| 1344 |
err_close_pam: |
| 1345 |
#ifdef USE_PAM |
| 1346 |
rc = pam_end(pam_handle, pam_status); |
| 1347 |
if (rc != PAM_SUCCESS) |
| 1348 |
fprintf(stderr, "pam_end failed with %s\n", |
| 1349 |
pam_strerror(pam_handle, rc)); |
| 1350 |
#endif |
| 1351 |
err_free: |
| 1352 |
freecon(tty_context); |
| 1353 |
freecon(new_tty_context); |
| 1354 |
freecon(old_context); |
| 1355 |
freecon(new_context); |
| 1356 |
free(pw.pw_name); |
| 1357 |
free(pw.pw_dir); |
| 1358 |
free(pw.pw_shell); |
| 1359 |
free(shell_argv0); |
| 1360 |
#ifdef USE_PAM |
| 1361 |
if (app_service_names) { |
| 1362 |
hashtab_map(app_service_names, free_hashtab_entry, NULL); |
| 1363 |
hashtab_destroy(app_service_names); |
| 1364 |
} |
| 1365 |
#endif |
| 1366 |
return -1; |
| 1367 |
} |