| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "common/cockpitauthorize.h" |
| 9 |
#include "common/cockpitframe.h" |
| 10 |
|
| 11 |
#include <security/pam_appl.h> |
| 12 |
|
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
|
| 16 |
#include <assert.h> |
| 17 |
#include <err.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <stdio.h> |
| 23 |
|
| 24 |
#include "src/session/session-utils.h" |
| 25 |
|
| 26 |
#define DEBUG 0 |
| 27 |
#define EX 127 |
| 28 |
|
| 29 |
static const char *auth_prefix = "\n{\"command\":\"authorize\",\"cookie\":\"xxx\""; |
| 30 |
static const char *auth_suffix = "\"}"; |
| 31 |
|
| 32 |
static void |
| 33 |
write_authorize_challenge (const char *data) |
| 34 |
{ |
| 35 |
char *message = NULL; |
| 36 |
if (asprintf (&message, "%s,\"challenge\":\"%s%s", auth_prefix, data, auth_suffix) < 0) |
| 37 |
errx (EX, "out of memory writing string"); |
| 38 |
#if DEBUG |
| 39 |
fprintf (stderr, "mock-auth-command > %s\n", message); |
| 40 |
#endif |
| 41 |
if (cockpit_frame_write (STDOUT_FILENO, (unsigned char *)message, strlen (message)) < 0) |
| 42 |
err (EX, "couldn't write auth request"); |
| 43 |
free (message); |
| 44 |
} |
| 45 |
|
| 46 |
static void |
| 47 |
write_message (const char *message) |
| 48 |
{ |
| 49 |
if (cockpit_frame_write (STDOUT_FILENO, (unsigned char *)message, strlen (message)) < 0) |
| 50 |
err (EX, "coludn't write message"); |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
write_init_message (const char *data) |
| 55 |
{ |
| 56 |
char *message = NULL; |
| 57 |
if (asprintf (&message, "\n{\"command\":\"init\",%s,\"version\":1}", data) < 0) |
| 58 |
errx (EX, "out of memory writing string"); |
| 59 |
#if DEBUG |
| 60 |
fprintf (stderr, "mock-auth-command > %s\n", message); |
| 61 |
#endif |
| 62 |
write_message (message); |
| 63 |
free (message); |
| 64 |
} |
| 65 |
|
| 66 |
int |
| 67 |
main (int argc, |
| 68 |
char **argv) |
| 69 |
{ |
| 70 |
int success = 0; |
| 71 |
int launch_bridge = 0; |
| 72 |
const char *data = NULL; |
| 73 |
char *type; |
| 74 |
|
| 75 |
write_authorize_challenge ("*"); |
| 76 |
|
| 77 |
char *message = read_authorize_response ("authorization"); |
| 78 |
char *response = get_authorize_key (message, "response", true); |
| 79 |
data = cockpit_authorize_type (response, &type); |
| 80 |
assert (data != NULL); |
| 81 |
|
| 82 |
if (strcmp (data, "") == 0) |
| 83 |
{ |
| 84 |
write_init_message ("\"problem\":\"authentication-failed\""); |
| 85 |
} |
| 86 |
if (strcmp (data, "no-cookie") == 0) |
| 87 |
{ |
| 88 |
write_message ("\n{\"command\":\"authorize\",\"response\": \"user me\"}"); |
| 89 |
free (message); |
| 90 |
free (response); |
| 91 |
write_authorize_challenge ("*"); |
| 92 |
message = read_authorize_response ("no-cookie"); |
| 93 |
response = get_authorize_key (message, "response", true); |
| 94 |
if (!response || strcmp (response, "user me") != 0) |
| 95 |
{ |
| 96 |
write_init_message ("\"problem\": \"authentication-failed\""); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
write_init_message ("\"user\": \"me\""); |
| 101 |
success = 1; |
| 102 |
} |
| 103 |
} |
| 104 |
else if (strcmp (data, "failslow") == 0) |
| 105 |
{ |
| 106 |
sleep (2); |
| 107 |
write_init_message ("\"problem\":\"authentication-failed\""); |
| 108 |
} |
| 109 |
else if (strcmp (data, "fail") == 0) |
| 110 |
{ |
| 111 |
write_init_message ("\"problem\":\"authentication-failed\""); |
| 112 |
} |
| 113 |
else if (strcmp (data, "not-supported") == 0) |
| 114 |
{ |
| 115 |
write_init_message ("\"problem\": \"authentication-not-supported\", \"auth-method-results\": {}"); |
| 116 |
} |
| 117 |
else if (strcmp (data, "ssh-fail") == 0) |
| 118 |
{ |
| 119 |
write_init_message ("\"problem\": \"authentication-failed\", \"auth-method-results\": { \"password\": \"denied\"}"); |
| 120 |
} |
| 121 |
else if (strcmp (data, "denied") == 0) |
| 122 |
{ |
| 123 |
write_init_message ("\"problem\": \"access-denied\""); |
| 124 |
} |
| 125 |
else if (strcmp (data, "success") == 0) |
| 126 |
{ |
| 127 |
write_init_message ("\"user\": \"me\""); |
| 128 |
success = 1; |
| 129 |
} |
| 130 |
else if (strcmp (data, "ssh-remote-switch") == 0 && |
| 131 |
strcmp (argv[1], "machine") == 0) |
| 132 |
{ |
| 133 |
write_init_message ("\"user\": \"me\""); |
| 134 |
success = 1; |
| 135 |
} |
| 136 |
else if (strcmp (data, "ssh-alt-machine") == 0 && |
| 137 |
strcmp (argv[1], "machine") == 0) |
| 138 |
{ |
| 139 |
write_init_message ("\"user\": \"me\""); |
| 140 |
success = 1; |
| 141 |
} |
| 142 |
else if (strcmp (data, "ssh-alt-default") == 0 && |
| 143 |
strcmp (argv[1], "default-host") == 0) |
| 144 |
{ |
| 145 |
write_init_message ("\"user\": \"me\""); |
| 146 |
success = 1; |
| 147 |
} |
| 148 |
else if (type && strcmp (type, "basic") == 0 && |
| 149 |
strcmp (argv[1], "127.0.0.1") == 0) |
| 150 |
{ |
| 151 |
if (strcmp (data, "bWU6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=") == 0) |
| 152 |
{ |
| 153 |
write_init_message ("\"user\": \"me\""); |
| 154 |
success = 1; |
| 155 |
} |
| 156 |
else if (strcmp (data, "YnJpZGdlLXVzZXI6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=") == 0) |
| 157 |
{ |
| 158 |
launch_bridge = 1; |
| 159 |
success = 1; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
write_init_message ("\"problem\": \"authentication-failed\", \"auth-method-results\": { \"password\": \"denied\"}"); |
| 164 |
} |
| 165 |
} |
| 166 |
else if (type && strcmp (type, "basic") == 0 && |
| 167 |
strcmp (argv[1], "machine") == 0) |
| 168 |
{ |
| 169 |
if (strcmp (data, "cmVtb3RlLXVzZXI6dGhpcyBpcyB0aGUgbWFjaGluZSBwYXNzd29yZA==") == 0) |
| 170 |
{ |
| 171 |
write_init_message ("\"user\": \"remote-user\""); |
| 172 |
success = 1; |
| 173 |
} |
| 174 |
else if (strcmp (data, "YnJpZGdlLXVzZXI6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=") == 0) |
| 175 |
{ |
| 176 |
launch_bridge = 1; |
| 177 |
success = 1; |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
write_init_message ("\"problem\": \"authentication-failed\", \"auth-method-results\": { \"password\": \"denied\"}"); |
| 182 |
} |
| 183 |
} |
| 184 |
else if (type && strcmp (type, "basic") == 0) |
| 185 |
{ |
| 186 |
if (strcmp (data, "bWU6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=") == 0) |
| 187 |
{ |
| 188 |
write_init_message ("\"user\": \"me\""); |
| 189 |
success = 1; |
| 190 |
} |
| 191 |
else if (strcmp (data, "YnJpZGdlLXVzZXI6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=") == 0) |
| 192 |
{ |
| 193 |
launch_bridge = 1; |
| 194 |
success = 1; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
write_init_message ("\"problem\": \"authentication-failed\""); |
| 199 |
} |
| 200 |
} |
| 201 |
else if (strcmp (data, "data-then-success") == 0) |
| 202 |
{ |
| 203 |
write_message ("\n{\"command\":\"authorize\",\"challenge\":\"x-login-data\",\"cookie\":\"blah\",\"login-data\":{ \"login\": \"data\"}}"); |
| 204 |
write_init_message ("\"user\": \"me\""); |
| 205 |
success = 1; |
| 206 |
} |
| 207 |
else if (strcmp (data, "two-step") == 0) |
| 208 |
{ |
| 209 |
write_authorize_challenge ("X-Conversation conv dHlwZSB0d28="); |
| 210 |
free (message); |
| 211 |
free (response); |
| 212 |
message = read_authorize_response ("two-step"); |
| 213 |
response = get_authorize_key (message, "response", true); |
| 214 |
data = cockpit_authorize_type (response, NULL); |
| 215 |
if (!data || strcmp (data, "conv dHdv") != 0) |
| 216 |
{ |
| 217 |
write_init_message ("\"problem\": \"authentication-failed\""); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
write_init_message ("\"user\": \"me\""); |
| 222 |
success = 1; |
| 223 |
} |
| 224 |
} |
| 225 |
else if (strcmp (data, "three-step") == 0) |
| 226 |
{ |
| 227 |
write_authorize_challenge ("X-Conversation conv dHlwZSB0d28="); |
| 228 |
free (message); |
| 229 |
free (response); |
| 230 |
message = read_authorize_response ("three-step"); |
| 231 |
response = get_authorize_key (message, "response", true); |
| 232 |
data = cockpit_authorize_type (response, NULL); |
| 233 |
if (!data || strcmp (data, "conv dHdv") != 0) |
| 234 |
{ |
| 235 |
write_init_message ("\"problem\": \"authentication-failed\""); |
| 236 |
goto out; |
| 237 |
} |
| 238 |
|
| 239 |
write_authorize_challenge ("X-Conversation conv dHlwZSB0aHJlZQ=="); |
| 240 |
free (message); |
| 241 |
free (response); |
| 242 |
message = read_authorize_response ("three-step"); |
| 243 |
response = get_authorize_key (message, "response", true); |
| 244 |
data = cockpit_authorize_type (response, NULL); |
| 245 |
if (!data || strcmp (data, "conv dGhyZWU=") != 0) |
| 246 |
{ |
| 247 |
write_init_message ("\"problem\": \"authentication-failed\""); |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
write_init_message ("\"user\": \"me\""); |
| 252 |
success = 1; |
| 253 |
} |
| 254 |
} |
| 255 |
else if (strcmp (data, "success-bad-data") == 0) |
| 256 |
{ |
| 257 |
write_init_message ("\"user\": \"me\", \"login-data\": \"bad\""); |
| 258 |
success = 1; |
| 259 |
} |
| 260 |
else if (strcmp (data, "no-user") == 0) |
| 261 |
{ |
| 262 |
write_init_message ("\"other\":1"); |
| 263 |
} |
| 264 |
else if (strcmp (data, "with-error") == 0) |
| 265 |
{ |
| 266 |
write_init_message ("\"problem\": \"unknown\", \"message\": \"detail for error\""); |
| 267 |
} |
| 268 |
else if (strcmp (data, "with-error") == 0) |
| 269 |
{ |
| 270 |
write_init_message ("\"problem\": \"unknown\", \"message\": \"detail for error\""); |
| 271 |
} |
| 272 |
else if (strcmp (data, "too-slow") == 0) |
| 273 |
{ |
| 274 |
sleep (10); |
| 275 |
write_init_message ("\"user\": \"me\", \"login-data\": { \"login\": \"data\"}"); |
| 276 |
success = 1; |
| 277 |
} |
| 278 |
|
| 279 |
out: |
| 280 |
free (response); |
| 281 |
free (message); |
| 282 |
if (success) |
| 283 |
{ |
| 284 |
if (launch_bridge) |
| 285 |
execlp (BUILDDIR "/cockpit-bridge", BUILDDIR "/cockpit-bridge", NULL); |
| 286 |
else |
| 287 |
execlp ("cat", "cat", NULL); |
| 288 |
} |
| 289 |
exit (PAM_AUTH_ERR); |
| 290 |
} |