| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "pam-ssh-add.h" |
| 9 |
|
| 10 |
#include "testlib/cockpittest.h" |
| 11 |
|
| 12 |
#include <sys/types.h> |
| 13 |
#include <sys/wait.h> |
| 14 |
|
| 15 |
#include <err.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <string.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
static int unexpected_message; |
| 23 |
|
| 24 |
/* Environment variables we set */ |
| 25 |
static const char *env_names[] = { |
| 26 |
"XDG_RUNTIME_DIR", |
| 27 |
"HOME", |
| 28 |
"PATH", |
| 29 |
"LC_ALL", |
| 30 |
"SSH_AUTH_SOCK", |
| 31 |
NULL |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
/* Holds environment values to set in pam context */ |
| 36 |
static char *env_saved[G_N_ELEMENTS (env_names)] = { NULL, }; |
| 37 |
|
| 38 |
/* Dummy PAM handle for testing purposes; pam_handle_t is opaque */ |
| 39 |
static char dummy_pamh_buf[256]; |
| 40 |
static pam_handle_t *dummy_pamh = (pam_handle_t *)dummy_pamh_buf; |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
const char *ssh_add; |
| 44 |
const char *ssh_add_arg; |
| 45 |
const char *ssh_agent; |
| 46 |
const char *ssh_agent_arg; |
| 47 |
const char *password; |
| 48 |
struct passwd *pw; |
| 49 |
GQueue *expected_messages; |
| 50 |
} Fixture; |
| 51 |
|
| 52 |
static void |
| 53 |
expect_message (Fixture *fix, |
| 54 |
const char *msg) |
| 55 |
{ |
| 56 |
g_queue_push_tail (fix->expected_messages, g_strdup (msg)); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
test_logger (int level, const char *msg) |
| 61 |
{ |
| 62 |
Fixture *fix = NULL; |
| 63 |
g_assert_nonnull (msg); |
| 64 |
|
| 65 |
/* We need to access the current fixture, stored in a global for this callback */ |
| 66 |
extern Fixture *current_fixture; |
| 67 |
fix = current_fixture; |
| 68 |
|
| 69 |
if (fix && !g_queue_is_empty (fix->expected_messages)) |
| 70 |
{ |
| 71 |
gchar *expected = g_queue_pop_head (fix->expected_messages); |
| 72 |
g_assert_nonnull (strstr (msg, expected)); |
| 73 |
g_free (expected); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
warnx ("%s", msg); |
| 78 |
unexpected_message = 1; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/* Global pointer to current fixture for logger callback */ |
| 83 |
Fixture *current_fixture = NULL; |
| 84 |
|
| 85 |
static void |
| 86 |
save_environment (void) |
| 87 |
{ |
| 88 |
int i; |
| 89 |
|
| 90 |
for (i = 0; env_names[i] != NULL; i++) |
| 91 |
env_saved[i] = getenv (env_names[i]); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
restore_environment (void) |
| 96 |
{ |
| 97 |
int i; |
| 98 |
for (i = 0; env_names[i] != NULL; i++) |
| 99 |
{ |
| 100 |
if (env_saved[i]) |
| 101 |
setenv (env_names[i], env_saved[i], 1); |
| 102 |
else |
| 103 |
unsetenv (env_names[i]); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
setup (Fixture *fix, |
| 109 |
gconstpointer user_data) |
| 110 |
{ |
| 111 |
const Fixture *template = user_data; |
| 112 |
|
| 113 |
unexpected_message = 0; |
| 114 |
fix->expected_messages = g_queue_new (); |
| 115 |
current_fixture = fix; |
| 116 |
|
| 117 |
if (template) |
| 118 |
{ |
| 119 |
fix->ssh_add = template->ssh_add; |
| 120 |
fix->ssh_add_arg = template->ssh_add_arg; |
| 121 |
fix->ssh_agent = template->ssh_agent; |
| 122 |
fix->ssh_agent_arg = template->ssh_agent_arg; |
| 123 |
fix->password = template->password; |
| 124 |
} |
| 125 |
|
| 126 |
if (!fix->ssh_add) |
| 127 |
fix->ssh_add = SRCDIR "/src/pam-ssh-add/mock-ssh-add"; |
| 128 |
|
| 129 |
if (!fix->ssh_agent) |
| 130 |
fix->ssh_agent = SRCDIR "/src/pam-ssh-add/mock-ssh-agent"; |
| 131 |
|
| 132 |
pam_ssh_add_program = fix->ssh_add; |
| 133 |
pam_ssh_add_arg = fix->ssh_add_arg; |
| 134 |
pam_ssh_agent_program = fix->ssh_agent; |
| 135 |
pam_ssh_agent_arg = fix->ssh_agent_arg; |
| 136 |
fix->pw = getpwuid (getuid ()); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
teardown (Fixture *fix, |
| 141 |
gconstpointer user_data) |
| 142 |
{ |
| 143 |
gchar *msg; |
| 144 |
int missed = 0; |
| 145 |
|
| 146 |
/* restore original environment */ |
| 147 |
restore_environment (); |
| 148 |
|
| 149 |
while (!g_queue_is_empty (fix->expected_messages)) |
| 150 |
{ |
| 151 |
msg = g_queue_pop_head (fix->expected_messages); |
| 152 |
warnx ("message didn't get logged: %s", msg); |
| 153 |
g_free (msg); |
| 154 |
missed = 1; |
| 155 |
} |
| 156 |
|
| 157 |
g_queue_free (fix->expected_messages); |
| 158 |
current_fixture = NULL; |
| 159 |
|
| 160 |
if (missed) |
| 161 |
g_assert_not_reached (); |
| 162 |
|
| 163 |
if (unexpected_message) |
| 164 |
g_assert_not_reached (); |
| 165 |
} |
| 166 |
|
| 167 |
static Fixture default_fixture = { |
| 168 |
}; |
| 169 |
|
| 170 |
static Fixture environment_fixture = { |
| 171 |
.ssh_agent = SRCDIR "/src/pam-ssh-add/mock-environment", |
| 172 |
.ssh_agent_arg = NULL |
| 173 |
}; |
| 174 |
|
| 175 |
static void |
| 176 |
run_test_agent_environment (Fixture *fix, |
| 177 |
const char *xdg_runtime, |
| 178 |
const char *xdg_runtime_expect) |
| 179 |
{ |
| 180 |
int ret; |
| 181 |
g_autofree char *xdg_expect = NULL; |
| 182 |
g_autofree char *home_expect = NULL; |
| 183 |
|
| 184 |
if (xdg_runtime_expect) |
| 185 |
{ |
| 186 |
if (asprintf (&xdg_expect, "XDG_RUNTIME_DIR=%s", |
| 187 |
xdg_runtime_expect) < 0) |
| 188 |
warnx ("Couldn't allocate XDG_RUNTIME_DIR expect variable"); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
xdg_expect = strdup ("NO XDG_RUNTIME_DIR"); |
| 193 |
} |
| 194 |
|
| 195 |
if (asprintf (&home_expect, "HOME=%s", fix->pw->pw_dir) < 0) |
| 196 |
warnx ("Couldn't allocate HOME expect variable"); |
| 197 |
|
| 198 |
expect_message (fix, xdg_expect); |
| 199 |
expect_message (fix, home_expect); |
| 200 |
|
| 201 |
expect_message (fix, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
| 202 |
expect_message (fix, "LC_ALL=C"); |
| 203 |
expect_message (fix, "NO OTHER"); |
| 204 |
|
| 205 |
expect_message (fix, "NO SSH_AUTH_SOCK"); |
| 206 |
expect_message (fix, "Failed to start ssh-agent"); |
| 207 |
|
| 208 |
ret = pam_ssh_add_start_agent (dummy_pamh, fix->pw, xdg_runtime, NULL, NULL); |
| 209 |
|
| 210 |
g_assert_cmpint (ret, ==, 0); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
test_environment (Fixture *fix, |
| 215 |
gconstpointer user_data) |
| 216 |
{ |
| 217 |
run_test_agent_environment (fix, NULL, getenv ("XDG_RUNTIME_DIR")); |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
test_environment_env_overides (Fixture *fix, |
| 222 |
gconstpointer user_data) |
| 223 |
{ |
| 224 |
setenv ("PATH", "bad", 1); |
| 225 |
setenv ("LC_ALL", "bad", 1); |
| 226 |
setenv ("HOME", "bad", 1); |
| 227 |
setenv ("XDG_RUNTIME_DIR", "", 1); |
| 228 |
setenv ("SSH_AUTH_SOCK", "bad", 1); |
| 229 |
setenv ("OTHER", "bad", 1); |
| 230 |
|
| 231 |
run_test_agent_environment (fix, NULL, ""); |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
test_environment_overides (Fixture *fix, |
| 236 |
gconstpointer user_data) |
| 237 |
{ |
| 238 |
setenv ("XDG_RUNTIME_DIR", "bad", 1); |
| 239 |
run_test_agent_environment (fix, "xdgover", "xdgover"); |
| 240 |
} |
| 241 |
|
| 242 |
static void |
| 243 |
test_failed_agent (Fixture *fix, |
| 244 |
gconstpointer user_data) |
| 245 |
{ |
| 246 |
g_autofree char *sock = NULL; |
| 247 |
g_autofree char *pid = NULL; |
| 248 |
int ret; |
| 249 |
|
| 250 |
expect_message (fix, "Bad things"); |
| 251 |
expect_message (fix, "Failed to start ssh-agent"); |
| 252 |
ret = pam_ssh_add_start_agent (dummy_pamh, fix->pw, NULL, &sock, &pid); |
| 253 |
|
| 254 |
g_assert_cmpint (ret, ==, 0); |
| 255 |
g_assert_null (sock); |
| 256 |
g_assert_null (pid); |
| 257 |
} |
| 258 |
|
| 259 |
static Fixture bad_agent_fixture = { |
| 260 |
.ssh_agent_arg = "bad-vars", |
| 261 |
}; |
| 262 |
|
| 263 |
static void |
| 264 |
test_bad_agent_vars (Fixture *fix, |
| 265 |
gconstpointer user_data) |
| 266 |
{ |
| 267 |
g_autofree char *sock = NULL; |
| 268 |
g_autofree char *pid = NULL; |
| 269 |
int ret; |
| 270 |
|
| 271 |
expect_message (fix, "Expected agent environment variables not found"); |
| 272 |
ret = pam_ssh_add_start_agent (dummy_pamh, fix->pw, NULL, &sock, &pid); |
| 273 |
|
| 274 |
g_assert_cmpint (ret, ==, 0); |
| 275 |
g_assert_null (sock); |
| 276 |
g_assert_null (pid); |
| 277 |
} |
| 278 |
|
| 279 |
static Fixture good_agent_fixture = { |
| 280 |
.ssh_agent_arg = "good-vars", |
| 281 |
}; |
| 282 |
|
| 283 |
static void |
| 284 |
test_good_agent_vars (Fixture *fix, |
| 285 |
gconstpointer user_data) |
| 286 |
{ |
| 287 |
g_autofree char *sock = NULL; |
| 288 |
g_autofree char *pid = NULL; |
| 289 |
int ret; |
| 290 |
|
| 291 |
ret = pam_ssh_add_start_agent (dummy_pamh, fix->pw, NULL, &sock, &pid); |
| 292 |
|
| 293 |
g_assert_cmpint (ret, ==, 1); |
| 294 |
g_assert_cmpstr (sock, ==, "SSH_AUTH_SOCKET=socket"); |
| 295 |
g_assert_cmpstr (pid, ==, "SSH_AGENT_PID=100"); |
| 296 |
} |
| 297 |
|
| 298 |
static Fixture keys_password_fixture = { |
| 299 |
.ssh_add_arg = NULL, |
| 300 |
.password = "foobar", |
| 301 |
}; |
| 302 |
|
| 303 |
static Fixture keys_no_password_fixture = { |
| 304 |
.ssh_add_arg = NULL, |
| 305 |
.password = NULL, |
| 306 |
}; |
| 307 |
|
| 308 |
static Fixture keys_bad_password_fixture = { |
| 309 |
.ssh_add_arg = NULL, |
| 310 |
.password = "bad", |
| 311 |
}; |
| 312 |
|
| 313 |
static void |
| 314 |
test_keys (Fixture *fix, |
| 315 |
gconstpointer user_data) |
| 316 |
{ |
| 317 |
int ret; |
| 318 |
int expect = 1; |
| 319 |
const char *key_add_result; |
| 320 |
|
| 321 |
if (fix->password == NULL) |
| 322 |
{ |
| 323 |
key_add_result = "Correct password 0, bad password 0, password_blanks 3"; |
| 324 |
} |
| 325 |
else if (strcmp (fix->password, "foobar") == 0) |
| 326 |
{ |
| 327 |
expect = 0; |
| 328 |
key_add_result = "Correct password 3, bad password 0, password_blanks 0"; |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
key_add_result = "Correct password 0, bad password 3, password_blanks 3"; |
| 333 |
} |
| 334 |
|
| 335 |
expect_message (fix, key_add_result); |
| 336 |
if (expect) |
| 337 |
expect_message (fix, "Failed adding some keys"); |
| 338 |
|
| 339 |
ret = pam_ssh_add_load (dummy_pamh, fix->pw, "mock-socket", fix->password); |
| 340 |
|
| 341 |
g_assert_cmpint (ret, ==, 1); |
| 342 |
} |
| 343 |
|
| 344 |
static Fixture keys_environment_fixture = { |
| 345 |
.ssh_add = SRCDIR "/src/pam-ssh-add/mock-environment", |
| 346 |
.ssh_add_arg = NULL |
| 347 |
}; |
| 348 |
|
| 349 |
static void |
| 350 |
test_key_environment (Fixture *fix, |
| 351 |
gconstpointer user_data) |
| 352 |
{ |
| 353 |
int ret; |
| 354 |
g_autofree char *home_expect = NULL; |
| 355 |
|
| 356 |
expect_message (fix, "ssh-add requires an agent socket"); |
| 357 |
ret = pam_ssh_add_load (dummy_pamh, fix->pw, NULL, NULL); |
| 358 |
g_assert_cmpint (ret, ==, 0); |
| 359 |
|
| 360 |
if (asprintf (&home_expect, "HOME=%s", fix->pw->pw_dir) < 0) |
| 361 |
warnx ("Couldn't allocate HOME expect variable"); |
| 362 |
|
| 363 |
expect_message (fix, "NO XDG_RUNTIME_DIR"); |
| 364 |
expect_message (fix, home_expect); |
| 365 |
|
| 366 |
expect_message (fix, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
| 367 |
expect_message (fix, "LC_ALL=C"); |
| 368 |
expect_message (fix, "NO OTHER"); |
| 369 |
|
| 370 |
expect_message (fix, "SSH_AUTH_SOCK=mock-socket"); |
| 371 |
expect_message (fix, "Failed adding some keys"); |
| 372 |
|
| 373 |
ret = pam_ssh_add_load (dummy_pamh, fix->pw, "mock-socket", NULL); |
| 374 |
|
| 375 |
g_assert_cmpint (ret, ==, 1); |
| 376 |
} |
| 377 |
|
| 378 |
int |
| 379 |
main (int argc, |
| 380 |
char *argv[]) |
| 381 |
{ |
| 382 |
signal (SIGPIPE, SIG_IGN); |
| 383 |
|
| 384 |
save_environment (); |
| 385 |
|
| 386 |
pam_ssh_add_log_handler = &test_logger; |
| 387 |
pam_ssh_add_verbose_mode = 0; |
| 388 |
|
| 389 |
cockpit_test_init (&argc, &argv); |
| 390 |
|
| 391 |
g_test_add ("/pam-ssh-add/add-key-environment", Fixture, &keys_environment_fixture, |
| 392 |
setup, test_key_environment, teardown); |
| 393 |
g_test_add ("/pam-ssh-add/add-key-no-password", Fixture, &keys_no_password_fixture, |
| 394 |
setup, test_keys, teardown); |
| 395 |
g_test_add ("/pam-ssh-add/add-key-bad-password", Fixture, &keys_bad_password_fixture, |
| 396 |
setup, test_keys, teardown); |
| 397 |
g_test_add ("/pam-ssh-add/add-key-password", Fixture, &keys_password_fixture, |
| 398 |
setup, test_keys, teardown); |
| 399 |
|
| 400 |
g_test_add ("/pam-ssh-add/environment", Fixture, &environment_fixture, |
| 401 |
setup, test_environment, teardown); |
| 402 |
g_test_add ("/pam-ssh-add/environment-env-overides", Fixture, &environment_fixture, |
| 403 |
setup, test_environment_env_overides, teardown); |
| 404 |
g_test_add ("/pam-ssh-add/environment-overides", Fixture, &environment_fixture, |
| 405 |
setup, test_environment_overides, teardown); |
| 406 |
g_test_add ("/pam-ssh-add/good-agent-vars", Fixture, &good_agent_fixture, |
| 407 |
setup, test_good_agent_vars, teardown); |
| 408 |
g_test_add ("/pam-ssh-add/bad-agent-vars", Fixture, &bad_agent_fixture, |
| 409 |
setup, test_bad_agent_vars, teardown); |
| 410 |
g_test_add ("/pam-ssh-add/test-failed-agent", Fixture, &default_fixture, |
| 411 |
setup, test_failed_agent, teardown); |
| 412 |
|
| 413 |
return g_test_run (); |
| 414 |
} |