| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Inspired by gnome-keyring: |
| 8 |
* Stef Walter <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "config.h" |
| 13 |
|
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/wait.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <assert.h> |
| 18 |
#include <err.h> |
| 19 |
#include <errno.h> |
| 20 |
#include <limits.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <string.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <syslog.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <pwd.h> |
| 29 |
#include <grp.h> |
| 30 |
|
| 31 |
#include <security/pam_modules.h> |
| 32 |
#include <security/pam_modutil.h> |
| 33 |
|
| 34 |
#include "pam-ssh-add.h" |
| 35 |
|
| 36 |
/* programs that can be overwidden in tests */ |
| 37 |
const char *pam_ssh_agent_program = PATH_SSH_AGENT; |
| 38 |
const char *pam_ssh_agent_arg = NULL; |
| 39 |
|
| 40 |
const char *pam_ssh_add_program = PATH_SSH_ADD; |
| 41 |
const char *pam_ssh_add_arg = NULL; |
| 42 |
|
| 43 |
static unsigned long ssh_agent_pid; |
| 44 |
static uid_t ssh_agent_uid; |
| 45 |
|
| 46 |
/* Environment */ |
| 47 |
#define ENVIRON_SIZE 5 |
| 48 |
#define PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
| 49 |
|
| 50 |
/* ssh-agent output variables we care about */ |
| 51 |
static const char *agent_vars[] = { |
| 52 |
"SSH_AUTH_SOCK", |
| 53 |
"SSH_AGENT_PID", |
| 54 |
NULL |
| 55 |
}; |
| 56 |
|
| 57 |
/* pre-set file descriptors */ |
| 58 |
#define STDIN 0 |
| 59 |
#define STDOUT 1 |
| 60 |
#define STDERR 2 |
| 61 |
|
| 62 |
/* read & write ends of a pipe */ |
| 63 |
#define READ_END 0 |
| 64 |
#define WRITE_END 1 |
| 65 |
|
| 66 |
/* attribute for stored auth */ |
| 67 |
#define STORED_AUTHTOK "pam_ssh_add_authtok" |
| 68 |
|
| 69 |
#ifndef debug |
| 70 |
#define debug(format, ...) \ |
| 71 |
do { if (pam_ssh_add_verbose_mode) \ |
| 72 |
syslog (LOG_INFO | LOG_AUTHPRIV, "pam_ssh_add: " format, ##__VA_ARGS__); \ |
| 73 |
} while (0) |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifndef error |
| 77 |
#define error(format, ...) \ |
| 78 |
do { message_handler (LOG_ERR, "pam_ssh_add: " format, ##__VA_ARGS__); \ |
| 79 |
} while (0) |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifndef message |
| 83 |
#define message(format, ...) \ |
| 84 |
do { message_handler (LOG_WARNING, "pam_ssh_add: " format, ##__VA_ARGS__); \ |
| 85 |
} while (0) |
| 86 |
#endif |
| 87 |
|
| 88 |
typedef int (* line_cb) (char *line, void *arg); |
| 89 |
int pam_ssh_add_verbose_mode = 0; |
| 90 |
pam_ssh_add_logger pam_ssh_add_log_handler = NULL; |
| 91 |
|
| 92 |
#ifndef message_handler |
| 93 |
#if __GNUC__ > 2 |
| 94 |
static void |
| 95 |
message_handler (int level, const char *format, ...) |
| 96 |
__attribute__((__format__(__printf__, 2, 3))); |
| 97 |
#endif |
| 98 |
|
| 99 |
static void |
| 100 |
default_logger (int level, const char *str) |
| 101 |
{ |
| 102 |
if (level == LOG_INFO) |
| 103 |
debug ("%s", str); |
| 104 |
else if (level == LOG_ERR) |
| 105 |
syslog (LOG_ERR, "%s", str); |
| 106 |
else |
| 107 |
syslog (LOG_WARNING, "%s", str); |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
message_handler (int level, |
| 112 |
const char *format, ...) |
| 113 |
{ |
| 114 |
va_list va; |
| 115 |
char *data = NULL; |
| 116 |
int res; |
| 117 |
|
| 118 |
if (!pam_ssh_add_log_handler) |
| 119 |
pam_ssh_add_log_handler = &default_logger; |
| 120 |
|
| 121 |
/* Fast path for simple messages */ |
| 122 |
if (!strchr (format, '%')) |
| 123 |
{ |
| 124 |
pam_ssh_add_log_handler (level, format); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
va_start (va, format); |
| 129 |
res = vasprintf (&data, format, va); |
| 130 |
va_end (va); |
| 131 |
|
| 132 |
if (res > 0) |
| 133 |
pam_ssh_add_log_handler (level, data); |
| 134 |
|
| 135 |
if (res >= 0) |
| 136 |
free (data); |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
static void |
| 141 |
close_safe (int fd) |
| 142 |
{ |
| 143 |
if (fd != -1) |
| 144 |
close (fd); |
| 145 |
} |
| 146 |
|
| 147 |
static char * |
| 148 |
strbtrim (char *data) |
| 149 |
{ |
| 150 |
assert (data); |
| 151 |
while (*data && isspace (*data)) |
| 152 |
++data; |
| 153 |
return (char*)data; |
| 154 |
} |
| 155 |
|
| 156 |
static int |
| 157 |
foreach_line (char *lines, |
| 158 |
line_cb cb, |
| 159 |
void *arg) |
| 160 |
{ |
| 161 |
char *line, *ctx; |
| 162 |
int ret = 1; |
| 163 |
|
| 164 |
assert (lines); |
| 165 |
|
| 166 |
/* Call cb for each line in the text block */ |
| 167 |
for (line = strtok_r (lines, "\n", &ctx); line != NULL; |
| 168 |
line = strtok_r (NULL, "\n", &ctx)) |
| 169 |
{ |
| 170 |
ret = (cb) (line, arg); |
| 171 |
if (!ret) |
| 172 |
return ret; |
| 173 |
} |
| 174 |
return ret; |
| 175 |
} |
| 176 |
|
| 177 |
static char * |
| 178 |
read_string (int fd, |
| 179 |
int consume) |
| 180 |
{ |
| 181 |
/* We only accept a max of 8K */ |
| 182 |
#define MAX_LENGTH 8192 |
| 183 |
#define BLOCK 256 |
| 184 |
|
| 185 |
char *ret = NULL; |
| 186 |
int r, len = 0; |
| 187 |
|
| 188 |
for (;;) |
| 189 |
{ |
| 190 |
char *n = realloc (ret, len + BLOCK); |
| 191 |
if (!n) |
| 192 |
{ |
| 193 |
free (ret); |
| 194 |
errno = ENOMEM; |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
memset (n + len, 0, BLOCK); |
| 199 |
ret = n; |
| 200 |
|
| 201 |
r = read (fd, ret + len, BLOCK-1); |
| 202 |
if (r < 0) |
| 203 |
{ |
| 204 |
if (errno == EAGAIN || errno == EINTR) |
| 205 |
continue; |
| 206 |
|
| 207 |
free (ret); |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
len = len + r; |
| 213 |
} |
| 214 |
|
| 215 |
if (r == 0 || len > MAX_LENGTH || consume == 0) |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
return ret; |
| 220 |
} |
| 221 |
|
| 222 |
static int |
| 223 |
write_string (int fd, |
| 224 |
const char *buf) |
| 225 |
{ |
| 226 |
size_t bytes = 0; |
| 227 |
int res, len = strlen (buf); |
| 228 |
|
| 229 |
while (bytes < len) |
| 230 |
{ |
| 231 |
res = write (fd, buf + bytes, len - bytes); |
| 232 |
if (res < 0) |
| 233 |
{ |
| 234 |
if (errno != EINTR && errno != EAGAIN) |
| 235 |
return -1; |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
bytes += res; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static int |
| 247 |
log_problem (char *line, |
| 248 |
void *arg) |
| 249 |
{ |
| 250 |
/* |
| 251 |
* Called for each stderr output line from the daemon. |
| 252 |
* Send it all to the log. |
| 253 |
*/ |
| 254 |
|
| 255 |
int *success; |
| 256 |
|
| 257 |
assert (line); |
| 258 |
assert (arg); |
| 259 |
|
| 260 |
success = (int*)arg; |
| 261 |
if (*success) |
| 262 |
message ("%s", line); |
| 263 |
else |
| 264 |
error ("%s", line); |
| 265 |
|
| 266 |
return 1; |
| 267 |
} |
| 268 |
|
| 269 |
static const char * |
| 270 |
get_optional_env (const char *name, |
| 271 |
const char *override) |
| 272 |
{ |
| 273 |
if (override) |
| 274 |
return override; |
| 275 |
|
| 276 |
return getenv (name); |
| 277 |
} |
| 278 |
|
| 279 |
static int |
| 280 |
build_environment (char **env, |
| 281 |
const char *first_key, ...) |
| 282 |
{ |
| 283 |
int i = 0; |
| 284 |
int res = 0; |
| 285 |
const char *key = first_key; |
| 286 |
va_list va; |
| 287 |
|
| 288 |
va_start (va, first_key); |
| 289 |
|
| 290 |
while (key != NULL) |
| 291 |
{ |
| 292 |
const char *value = va_arg (va, char*); |
| 293 |
if (value != NULL) |
| 294 |
{ |
| 295 |
if (asprintf (env + (i++), "%s=%s", key, value) < 0) |
| 296 |
{ |
| 297 |
error ("couldn't allocate environment"); |
| 298 |
goto out; |
| 299 |
} |
| 300 |
} |
| 301 |
key = va_arg (va, char*); |
| 302 |
} |
| 303 |
res = 1; |
| 304 |
|
| 305 |
out: |
| 306 |
va_end (va); |
| 307 |
return res; |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
setup_child (pam_handle_t *pamh, |
| 312 |
const char **args, |
| 313 |
char **env, |
| 314 |
struct passwd *pwd, |
| 315 |
int inp[2], |
| 316 |
int outp[2], |
| 317 |
int errp[2]) |
| 318 |
{ |
| 319 |
assert (pwd); |
| 320 |
assert (pwd->pw_dir); |
| 321 |
|
| 322 |
/* Fix up our end of the pipes */ |
| 323 |
if (dup2 (inp[READ_END], STDIN) < 0 || |
| 324 |
dup2 (outp[WRITE_END], STDOUT) < 0 || |
| 325 |
dup2 (errp[WRITE_END], STDERR) < 0) |
| 326 |
{ |
| 327 |
error ("couldn't setup pipes: %m"); |
| 328 |
exit (EXIT_FAILURE); |
| 329 |
} |
| 330 |
|
| 331 |
pam_modutil_sanitize_helper_fds (pamh, |
| 332 |
PAM_MODUTIL_IGNORE_FD, |
| 333 |
PAM_MODUTIL_IGNORE_FD, |
| 334 |
PAM_MODUTIL_IGNORE_FD); |
| 335 |
|
| 336 |
/* Close unnecessary file descriptors */ |
| 337 |
close (inp[READ_END]); |
| 338 |
close (inp[WRITE_END]); |
| 339 |
close (outp[READ_END]); |
| 340 |
close (outp[WRITE_END]); |
| 341 |
close (errp[READ_END]); |
| 342 |
close (errp[WRITE_END]); |
| 343 |
|
| 344 |
/* Start a new session, to detach from tty */ |
| 345 |
if (setsid() < 0) |
| 346 |
{ |
| 347 |
error ("failed to detach child process"); |
| 348 |
exit (EXIT_FAILURE); |
| 349 |
} |
| 350 |
|
| 351 |
/* We may be running effective as another user, revert that */ |
| 352 |
if (setegid (getgid ()) < 0 || seteuid (getuid ()) < 0) |
| 353 |
error ("failed to restore credentials"); |
| 354 |
|
| 355 |
/* Setup process credentials; if we actually change the group, drop any auxiliary groups too */ |
| 356 |
if ((getegid() != pwd->pw_gid ? initgroups(pwd->pw_name, pwd->pw_gid) < 0 : 0) || |
| 357 |
setgid (pwd->pw_gid) < 0 || setuid (pwd->pw_uid) < 0 || |
| 358 |
setegid (pwd->pw_gid) < 0 || seteuid (pwd->pw_uid) < 0) |
| 359 |
{ |
| 360 |
error ("couldn't setup credentials: %m"); |
| 361 |
exit (EXIT_FAILURE); |
| 362 |
} |
| 363 |
|
| 364 |
/* Now actually execute the process */ |
| 365 |
execve (args[0], (char **) args, env); |
| 366 |
error ("couldn't run %s: %m", args[0]); |
| 367 |
_exit (EXIT_FAILURE); |
| 368 |
} |
| 369 |
|
| 370 |
static void |
| 371 |
ignore_signals (struct sigaction *defsact, |
| 372 |
struct sigaction *oldsact, |
| 373 |
struct sigaction *ignpipe, |
| 374 |
struct sigaction *oldpipe) |
| 375 |
{ |
| 376 |
/* |
| 377 |
* Make sure that SIGCHLD occurs. Otherwise our waitpid below |
| 378 |
* doesn't work properly. We need to wait on the process to |
| 379 |
* get the daemon exit status. |
| 380 |
*/ |
| 381 |
memset (defsact, 0, sizeof (*defsact)); |
| 382 |
memset (oldsact, 0, sizeof (*oldsact)); |
| 383 |
defsact->sa_handler = SIG_DFL; |
| 384 |
sigaction (SIGCHLD, defsact, oldsact); |
| 385 |
|
| 386 |
/* |
| 387 |
* Make sure we don't exit with a SIGPIPE while doing this, that |
| 388 |
* would be very annoying to a user trying to log in. |
| 389 |
*/ |
| 390 |
memset (ignpipe, 0, sizeof (*ignpipe)); |
| 391 |
memset (oldpipe, 0, sizeof (*oldpipe)); |
| 392 |
ignpipe->sa_handler = SIG_IGN; |
| 393 |
sigaction (SIGPIPE, ignpipe, oldpipe); |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
restore_signals (struct sigaction *oldsact, |
| 398 |
struct sigaction *oldpipe) |
| 399 |
{ |
| 400 |
/* Restore old handler */ |
| 401 |
sigaction (SIGCHLD, oldsact, NULL); |
| 402 |
sigaction (SIGPIPE, oldpipe, NULL); |
| 403 |
} |
| 404 |
|
| 405 |
static pid_t |
| 406 |
run_as_user (pam_handle_t *pamh, |
| 407 |
const char **args, |
| 408 |
char **env, |
| 409 |
struct passwd *pwd, |
| 410 |
int inp[2], |
| 411 |
int outp[2], |
| 412 |
int errp[2]) |
| 413 |
{ |
| 414 |
pid_t pid = -1; |
| 415 |
|
| 416 |
/* Start up daemon child process */ |
| 417 |
switch (pid = fork ()) |
| 418 |
{ |
| 419 |
case -1: |
| 420 |
error ("couldn't fork: %m"); |
| 421 |
goto done; |
| 422 |
|
| 423 |
/* This is the child */ |
| 424 |
case 0: |
| 425 |
setup_child (pamh, args, env, pwd, inp, outp, errp); |
| 426 |
/* Should never be reached */ |
| 427 |
break; |
| 428 |
|
| 429 |
/* This is the parent */ |
| 430 |
default: |
| 431 |
break; |
| 432 |
}; |
| 433 |
|
| 434 |
done: |
| 435 |
return pid; |
| 436 |
} |
| 437 |
|
| 438 |
static int |
| 439 |
get_environ_vars_from_agent (char *line, |
| 440 |
void *arg) |
| 441 |
{ |
| 442 |
/* |
| 443 |
* ssh-agent outputs commands for exporting it's environment |
| 444 |
* variables. We want to return these variables so parse |
| 445 |
* them out and store them. |
| 446 |
*/ |
| 447 |
|
| 448 |
char *c = NULL; |
| 449 |
int i; |
| 450 |
int ret = 1; |
| 451 |
const char sep[] = "; export"; |
| 452 |
|
| 453 |
char **ret_array = (char**)arg; |
| 454 |
|
| 455 |
assert (line); |
| 456 |
assert (arg); |
| 457 |
|
| 458 |
line = strbtrim (line); |
| 459 |
debug ("got line: %s", line); |
| 460 |
c = strstr (line, sep); |
| 461 |
if (c) |
| 462 |
{ |
| 463 |
*c = '\0'; |
| 464 |
debug ("name/value is: %s", line); |
| 465 |
for (i = 0; agent_vars[i] != NULL; i++) |
| 466 |
{ |
| 467 |
if (strstr(line, agent_vars[i])) |
| 468 |
{ |
| 469 |
if (asprintf (ret_array + (i), "%s", line) < 0) |
| 470 |
{ |
| 471 |
error ("Error allocating output variable"); |
| 472 |
ret = 0; |
| 473 |
} |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
return ret; |
| 480 |
} |
| 481 |
|
| 482 |
int |
| 483 |
pam_ssh_add_load (pam_handle_t *pamh, |
| 484 |
struct passwd *pwd, |
| 485 |
const char *agent_socket, |
| 486 |
const char *password) |
| 487 |
{ |
| 488 |
struct sigaction defsact, oldsact, ignpipe, oldpipe; |
| 489 |
int i; |
| 490 |
int inp[2] = { -1, -1 }; |
| 491 |
int outp[2] = { -1, -1 }; |
| 492 |
int errp[2] = { -1, -1 }; |
| 493 |
|
| 494 |
char *env[ENVIRON_SIZE] = { NULL }; |
| 495 |
const char *args[] = { "/bin/sh", "-c", "$0 $1", |
| 496 |
pam_ssh_add_program, |
| 497 |
pam_ssh_add_arg, |
| 498 |
NULL }; |
| 499 |
|
| 500 |
pid_t pid; |
| 501 |
int success = 0; |
| 502 |
int force_stderr_debug = 1; |
| 503 |
|
| 504 |
siginfo_t result; |
| 505 |
|
| 506 |
ignore_signals (&defsact, &oldsact, &ignpipe, &oldpipe); |
| 507 |
|
| 508 |
assert (pwd); |
| 509 |
if (!agent_socket) |
| 510 |
{ |
| 511 |
message ("ssh-add requires an agent socket"); |
| 512 |
goto done; |
| 513 |
} |
| 514 |
|
| 515 |
if (!build_environment (env, |
| 516 |
"PATH", PATH, |
| 517 |
"LC_ALL", "C", |
| 518 |
"HOME", pwd->pw_dir, |
| 519 |
"SSH_AUTH_SOCK", agent_socket, |
| 520 |
NULL)) |
| 521 |
goto done; |
| 522 |
|
| 523 |
/* Create the necessary pipes */ |
| 524 |
if (pipe (inp) < 0 || pipe (outp) < 0 || pipe (errp) < 0) |
| 525 |
{ |
| 526 |
error ("couldn't create pipes: %m"); |
| 527 |
goto done; |
| 528 |
} |
| 529 |
|
| 530 |
pid = run_as_user (pamh, args, env, pwd, |
| 531 |
inp, outp, errp); |
| 532 |
if (pid < 1) |
| 533 |
goto done; |
| 534 |
|
| 535 |
/* in the parent, close our unneeded ends of the pipes */ |
| 536 |
close (inp[READ_END]); |
| 537 |
close (outp[WRITE_END]); |
| 538 |
close (errp[WRITE_END]); |
| 539 |
inp[READ_END] = outp[WRITE_END] = errp[WRITE_END] = -1; |
| 540 |
for (;;) |
| 541 |
{ |
| 542 |
/* ssh-add asks for password on stderr */ |
| 543 |
char *outerr = read_string (errp[READ_END], 0); |
| 544 |
if (outerr == NULL || outerr[0] == '\0') |
| 545 |
{ |
| 546 |
free (outerr); |
| 547 |
break; |
| 548 |
} |
| 549 |
|
| 550 |
if (strstr (outerr, "Enter passphrase") != NULL) |
| 551 |
{ |
| 552 |
debug ("Got password request"); |
| 553 |
if (password != NULL) |
| 554 |
write_string (inp[WRITE_END], password); |
| 555 |
write_string (inp[WRITE_END], "\n"); |
| 556 |
} |
| 557 |
else if (strstr (outerr, "Bad passphrase")) |
| 558 |
{ |
| 559 |
debug ("sent bad password"); |
| 560 |
write_string (inp[WRITE_END], "\n"); |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
foreach_line (outerr, log_problem, |
| 565 |
&force_stderr_debug); |
| 566 |
} |
| 567 |
|
| 568 |
free (outerr); |
| 569 |
} |
| 570 |
|
| 571 |
/* Wait for the initial process to exit */ |
| 572 |
while (waitid (P_PID, pid, &result, WEXITED) < 0) |
| 573 |
{ |
| 574 |
if (errno != EINTR) |
| 575 |
{ |
| 576 |
error ("couldn't wait on ssh-add process: %m"); |
| 577 |
goto done; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
success = result.si_code == CLD_EXITED && result.si_status == 0; |
| 582 |
/* Failure from process */ |
| 583 |
if (!success) |
| 584 |
{ |
| 585 |
/* key loading failed, don't report as an error */ |
| 586 |
if (result.si_code == CLD_EXITED && result.si_status == 1) |
| 587 |
{ |
| 588 |
success = 1; |
| 589 |
message ("Failed adding some keys"); |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
message ("Failed adding keys: %d", result.si_status); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
done: |
| 598 |
restore_signals (&oldsact, &oldpipe); |
| 599 |
|
| 600 |
close_safe (inp[0]); |
| 601 |
close_safe (inp[1]); |
| 602 |
close_safe (outp[0]); |
| 603 |
close_safe (outp[1]); |
| 604 |
close_safe (errp[0]); |
| 605 |
close_safe (errp[1]); |
| 606 |
|
| 607 |
for (i = 0; env[i] != NULL; i++) |
| 608 |
free (env[i]); |
| 609 |
|
| 610 |
return success; |
| 611 |
} |
| 612 |
|
| 613 |
int |
| 614 |
pam_ssh_add_start_agent (pam_handle_t *pamh, |
| 615 |
struct passwd *pwd, |
| 616 |
const char *xdg_runtime_overide, |
| 617 |
char **out_auth_sock_var, |
| 618 |
char **out_agent_pid_var) |
| 619 |
{ |
| 620 |
char *env[ENVIRON_SIZE] = { NULL }; |
| 621 |
const char *xdg_runtime; |
| 622 |
|
| 623 |
struct sigaction defsact, oldsact, ignpipe, oldpipe; |
| 624 |
siginfo_t result; |
| 625 |
|
| 626 |
int inp[2] = { -1, -1 }; |
| 627 |
int outp[2] = { -1, -1 }; |
| 628 |
int errp[2] = { -1, -1 }; |
| 629 |
pid_t pid; |
| 630 |
|
| 631 |
const char *args[] = { "/bin/sh", "-c", "$0 $1", |
| 632 |
pam_ssh_agent_program, |
| 633 |
pam_ssh_agent_arg, |
| 634 |
NULL }; |
| 635 |
|
| 636 |
char *output = NULL; |
| 637 |
char *outerr = NULL; |
| 638 |
int success = 0; |
| 639 |
int i = 0; |
| 640 |
|
| 641 |
char *save_vars[N_ELEMENTS (agent_vars)] = { NULL, }; |
| 642 |
|
| 643 |
assert (pwd); |
| 644 |
xdg_runtime = get_optional_env ("XDG_RUNTIME_DIR", |
| 645 |
xdg_runtime_overide); |
| 646 |
if (!build_environment (env, |
| 647 |
"PATH", PATH, |
| 648 |
"LC_ALL", "C", |
| 649 |
"HOME", pwd->pw_dir, |
| 650 |
"XDG_RUNTIME_DIR", xdg_runtime, |
| 651 |
NULL)) |
| 652 |
goto done; |
| 653 |
|
| 654 |
ignore_signals (&defsact, &oldsact, &ignpipe, &oldpipe); |
| 655 |
/* Create the necessary pipes */ |
| 656 |
if (pipe (inp) < 0 || pipe (outp) < 0 || pipe (errp) < 0) |
| 657 |
{ |
| 658 |
error ("couldn't create pipes: %m"); |
| 659 |
goto done; |
| 660 |
} |
| 661 |
|
| 662 |
pid = run_as_user (pamh, args, env, pwd, |
| 663 |
inp, outp, errp); |
| 664 |
if (pid < 1) |
| 665 |
goto done; |
| 666 |
|
| 667 |
/* in the parent, close our unneeded ends of the pipes */ |
| 668 |
close (inp[READ_END]); |
| 669 |
close (outp[WRITE_END]); |
| 670 |
close (errp[WRITE_END]); |
| 671 |
close (inp[WRITE_END]); |
| 672 |
|
| 673 |
inp[READ_END] = outp[WRITE_END] = errp[WRITE_END] = -1; |
| 674 |
|
| 675 |
/* Read any stdout and stderr data */ |
| 676 |
output = read_string (outp[READ_END], 1); |
| 677 |
outerr = read_string (errp[READ_END], 0); |
| 678 |
if (!output || !outerr) |
| 679 |
{ |
| 680 |
error ("couldn't read data from ssh-agent: %m"); |
| 681 |
goto done; |
| 682 |
} |
| 683 |
|
| 684 |
/* Wait for the initial process to exit */ |
| 685 |
while (waitid (P_PID, pid, &result, WEXITED) < 0) |
| 686 |
{ |
| 687 |
if (errno != EINTR) |
| 688 |
{ |
| 689 |
error ("couldn't wait on ssh-agent process: %m"); |
| 690 |
goto done; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
success = result.si_code == CLD_EXITED && result.si_status == 0; |
| 695 |
|
| 696 |
if (outerr && outerr[0]) |
| 697 |
foreach_line (outerr, log_problem, &success); |
| 698 |
|
| 699 |
foreach_line (output, get_environ_vars_from_agent, save_vars); |
| 700 |
|
| 701 |
/* Failure from process */ |
| 702 |
if (!success) |
| 703 |
{ |
| 704 |
error ("Failed to start ssh-agent"); |
| 705 |
} |
| 706 |
/* Failure to find vars */ |
| 707 |
else if (!save_vars[0] || !save_vars[1]) |
| 708 |
{ |
| 709 |
message ("Expected agent environment variables not found"); |
| 710 |
success = 0; |
| 711 |
} |
| 712 |
|
| 713 |
if (out_auth_sock_var && save_vars[0]) |
| 714 |
*out_auth_sock_var = strdup (save_vars[0]); |
| 715 |
|
| 716 |
if (out_agent_pid_var && save_vars[1]) |
| 717 |
*out_agent_pid_var = strdup (save_vars[1]); |
| 718 |
|
| 719 |
done: |
| 720 |
restore_signals (&oldsact, &oldpipe); |
| 721 |
|
| 722 |
close_safe (inp[0]); |
| 723 |
close_safe (inp[1]); |
| 724 |
close_safe (outp[0]); |
| 725 |
close_safe (outp[1]); |
| 726 |
close_safe (errp[0]); |
| 727 |
close_safe (errp[1]); |
| 728 |
|
| 729 |
free (output); |
| 730 |
free (outerr); |
| 731 |
|
| 732 |
/* save_vars may contain NULL |
| 733 |
* values use agent_vars as the |
| 734 |
* marker instead |
| 735 |
*/ |
| 736 |
for (i = 0; agent_vars[i] != NULL; i++) |
| 737 |
free (save_vars[i]); |
| 738 |
|
| 739 |
for (i = 0; env[i] != NULL; i++) |
| 740 |
free (env[i]); |
| 741 |
|
| 742 |
return success; |
| 743 |
} |
| 744 |
|
| 745 |
/* -------------------------------------------------------------------------------- |
| 746 |
* PAM Module |
| 747 |
*/ |
| 748 |
|
| 749 |
static void |
| 750 |
parse_args (int argc, |
| 751 |
const char **argv) |
| 752 |
{ |
| 753 |
int i; |
| 754 |
|
| 755 |
pam_ssh_add_verbose_mode = 0; |
| 756 |
|
| 757 |
/* Parse the arguments */ |
| 758 |
for (i = 0; i < argc; i++) |
| 759 |
{ |
| 760 |
if (strcmp (argv[i], "debug") == 0) |
| 761 |
{ |
| 762 |
pam_ssh_add_verbose_mode = 1; |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
message ("invalid option: %s", argv[i]); |
| 767 |
continue; |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
static void |
| 773 |
free_password (char *password) |
| 774 |
{ |
| 775 |
volatile char *vp; |
| 776 |
size_t len; |
| 777 |
|
| 778 |
if (!password) |
| 779 |
return; |
| 780 |
|
| 781 |
/* Defeats some optimizations */ |
| 782 |
len = strlen (password); |
| 783 |
memset (password, 0xAA, len); |
| 784 |
memset (password, 0xBB, len); |
| 785 |
|
| 786 |
/* Defeats others */ |
| 787 |
vp = (volatile char*)password; |
| 788 |
while (*vp) |
| 789 |
*(vp++) = 0xAA; |
| 790 |
|
| 791 |
free (password); |
| 792 |
} |
| 793 |
|
| 794 |
static void |
| 795 |
cleanup_free_password (pam_handle_t *pamh, |
| 796 |
void *data, |
| 797 |
int pam_end_status) |
| 798 |
{ |
| 799 |
free_password (data); |
| 800 |
} |
| 801 |
|
| 802 |
static char * |
| 803 |
strdupx (const char *string) |
| 804 |
{ |
| 805 |
char *copy = strdup (string); |
| 806 |
if (copy != NULL) |
| 807 |
return copy; |
| 808 |
|
| 809 |
warn ("failed to allocate memory for strdup"); |
| 810 |
abort (); |
| 811 |
} |
| 812 |
|
| 813 |
static int |
| 814 |
stash_password_for_session (pam_handle_t *pamh, |
| 815 |
const char *password) |
| 816 |
{ |
| 817 |
char *password_copy = strdupx (password); |
| 818 |
if (pam_set_data (pamh, STORED_AUTHTOK, password_copy, |
| 819 |
cleanup_free_password) != PAM_SUCCESS) |
| 820 |
{ |
| 821 |
free_password (password_copy); |
| 822 |
message ("error stashing password for session"); |
| 823 |
return PAM_AUTHTOK_RECOVER_ERR; |
| 824 |
} |
| 825 |
|
| 826 |
/* coverity[leaked_storage : FALSE] */ |
| 827 |
return PAM_SUCCESS; |
| 828 |
} |
| 829 |
|
| 830 |
static int |
| 831 |
start_agent (pam_handle_t *pamh, |
| 832 |
struct passwd *auth_pwd) |
| 833 |
{ |
| 834 |
char *auth_socket = NULL; |
| 835 |
char *auth_pid = NULL; |
| 836 |
int success = 0; |
| 837 |
int res; |
| 838 |
|
| 839 |
success = pam_ssh_add_start_agent (pamh, auth_pwd, |
| 840 |
pam_getenv (pamh, "XDG_RUNTIME_DIR"), |
| 841 |
&auth_socket, |
| 842 |
&auth_pid); |
| 843 |
|
| 844 |
/* Store pid and socket environment vars */ |
| 845 |
if (!success || !auth_socket || !auth_pid) |
| 846 |
{ |
| 847 |
res = PAM_SERVICE_ERR; |
| 848 |
} |
| 849 |
else |
| 850 |
{ |
| 851 |
res = pam_putenv (pamh, auth_socket); |
| 852 |
if (res == PAM_SUCCESS) |
| 853 |
res = pam_putenv (pamh, auth_pid); |
| 854 |
|
| 855 |
if (res != PAM_SUCCESS) |
| 856 |
{ |
| 857 |
error ("couldn't set agent environment: %s", |
| 858 |
pam_strerror (pamh, res)); |
| 859 |
} |
| 860 |
|
| 861 |
/* parse and store the agent pid for later cleanup */ |
| 862 |
if (strncmp (auth_pid, "SSH_AGENT_PID=", 14) == 0) |
| 863 |
{ |
| 864 |
char *endptr; |
| 865 |
unsigned long pid; |
| 866 |
|
| 867 |
errno = 0; |
| 868 |
pid = strtoul (auth_pid + 14, &endptr, 10); |
| 869 |
if (pid > 0 && *endptr == '\0' && !(pid == ULONG_MAX && errno == ERANGE)) |
| 870 |
{ |
| 871 |
ssh_agent_pid = pid; |
| 872 |
ssh_agent_uid = auth_pwd->pw_uid; |
| 873 |
} |
| 874 |
else |
| 875 |
{ |
| 876 |
error ("invalid SSH_AGENT_PID value: %s", auth_pid); |
| 877 |
} |
| 878 |
} |
| 879 |
else |
| 880 |
{ |
| 881 |
error ("unexpected agent pid format: %s", auth_pid); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
free (auth_socket); |
| 886 |
free (auth_pid); |
| 887 |
|
| 888 |
return res; |
| 889 |
} |
| 890 |
|
| 891 |
static int |
| 892 |
load_keys (pam_handle_t *pamh, |
| 893 |
struct passwd *auth_pwd) |
| 894 |
{ |
| 895 |
const char *password; |
| 896 |
int success = 0; |
| 897 |
|
| 898 |
/* Get the stored authtok here */ |
| 899 |
if (pam_get_data (pamh, STORED_AUTHTOK, |
| 900 |
(const void**)&password) != PAM_SUCCESS) |
| 901 |
{ |
| 902 |
password = NULL; |
| 903 |
} |
| 904 |
|
| 905 |
success = pam_ssh_add_load (pamh, auth_pwd, |
| 906 |
pam_getenv (pamh, "SSH_AUTH_SOCK"), |
| 907 |
password); |
| 908 |
|
| 909 |
return success ? PAM_SUCCESS : PAM_SERVICE_ERR; |
| 910 |
} |
| 911 |
|
| 912 |
PAM_EXTERN int |
| 913 |
pam_sm_open_session (pam_handle_t *pamh, |
| 914 |
int flags, |
| 915 |
int argc, |
| 916 |
const char *argv[]) |
| 917 |
{ |
| 918 |
int res; |
| 919 |
int o_res; |
| 920 |
|
| 921 |
struct passwd *auth_pwd; |
| 922 |
const char *user; |
| 923 |
|
| 924 |
parse_args (argc, argv); |
| 925 |
|
| 926 |
/* Lookup the user */ |
| 927 |
res = pam_get_user (pamh, &user, NULL); |
| 928 |
if (res != PAM_SUCCESS) |
| 929 |
{ |
| 930 |
message ("couldn't get pam user: %s", pam_strerror (pamh, res)); |
| 931 |
goto out; |
| 932 |
} |
| 933 |
|
| 934 |
auth_pwd = getpwnam (user); |
| 935 |
if (!auth_pwd) |
| 936 |
{ |
| 937 |
error ("error looking up user information"); |
| 938 |
res = PAM_SERVICE_ERR; |
| 939 |
goto out; |
| 940 |
} |
| 941 |
|
| 942 |
res = start_agent (pamh, auth_pwd); |
| 943 |
|
| 944 |
if (res == PAM_SUCCESS) |
| 945 |
res = load_keys (pamh, auth_pwd); |
| 946 |
|
| 947 |
out: |
| 948 |
/* Delete the stored password, |
| 949 |
unless we are not in start mode |
| 950 |
then we might still need it. |
| 951 |
*/ |
| 952 |
o_res = pam_set_data (pamh, STORED_AUTHTOK, |
| 953 |
NULL, cleanup_free_password); |
| 954 |
if (o_res != PAM_SUCCESS) |
| 955 |
{ |
| 956 |
message ("couldn't delete stored authtok: %s", |
| 957 |
pam_strerror (pamh, o_res)); |
| 958 |
} |
| 959 |
|
| 960 |
return res; |
| 961 |
} |
| 962 |
|
| 963 |
PAM_EXTERN int |
| 964 |
pam_sm_close_session (pam_handle_t *pamh, |
| 965 |
int flags, |
| 966 |
int argc, |
| 967 |
const char *argv[]) |
| 968 |
{ |
| 969 |
parse_args (argc, argv); |
| 970 |
|
| 971 |
/* Kill the ssh agent we started */ |
| 972 |
if (ssh_agent_pid > 0) |
| 973 |
{ |
| 974 |
debug ("Closing %lu", ssh_agent_pid); |
| 975 |
/* kill as user to guard against crashing ssh-agent and PID reuse */ |
| 976 |
if (setresuid (ssh_agent_uid, ssh_agent_uid, -1) < 0) |
| 977 |
{ |
| 978 |
error ("could not drop privileges for killing ssh agent: %m"); |
| 979 |
return PAM_SESSION_ERR; |
| 980 |
} |
| 981 |
if (kill (ssh_agent_pid, SIGTERM) < 0 && errno != ESRCH) |
| 982 |
message ("could not kill ssh agent %lu: %m", ssh_agent_pid); |
| 983 |
if (setresuid (0, 0, -1) < 0) |
| 984 |
{ |
| 985 |
error ("could not restore privileges after killing ssh agent: %m"); |
| 986 |
return PAM_SESSION_ERR; |
| 987 |
} |
| 988 |
} |
| 989 |
return PAM_SUCCESS; |
| 990 |
} |
| 991 |
|
| 992 |
PAM_EXTERN int |
| 993 |
pam_sm_authenticate (pam_handle_t *pamh, |
| 994 |
int unused, |
| 995 |
int argc, |
| 996 |
const char **argv) |
| 997 |
{ |
| 998 |
const char *password; |
| 999 |
int ret; |
| 1000 |
|
| 1001 |
parse_args (argc, argv); |
| 1002 |
|
| 1003 |
/* Look up the password and store it for later */ |
| 1004 |
ret = pam_get_item (pamh, PAM_AUTHTOK, |
| 1005 |
(const void**)&password); |
| 1006 |
if (ret != PAM_SUCCESS) |
| 1007 |
message ("no password is available: %s", |
| 1008 |
pam_strerror (pamh, ret)); |
| 1009 |
|
| 1010 |
if (password != NULL) |
| 1011 |
stash_password_for_session (pamh, password); |
| 1012 |
|
| 1013 |
/* We're not an authentication module */ |
| 1014 |
return PAM_CRED_INSUFFICIENT; |
| 1015 |
} |
| 1016 |
|
| 1017 |
PAM_EXTERN int |
| 1018 |
pam_sm_setcred (pam_handle_t *pamh, |
| 1019 |
int flags, |
| 1020 |
int argc, |
| 1021 |
const char *argv[]) |
| 1022 |
{ |
| 1023 |
return PAM_SUCCESS; |
| 1024 |
} |