1/* 2 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Per Allansson. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25#include "includes.h"
|
26RCSID("$OpenBSD: auth2-chall.c,v 1.21 2004/06/01 14:20:45 dtucker Exp $");
27RCSID("$FreeBSD: head/crypto/openssh/auth2-chall.c 137019 2004-10-28 16:11:31Z des $");
|
26RCSID("$OpenBSD: auth2-chall.c,v 1.22 2005/01/19 13:11:47 dtucker Exp $"); 27RCSID("$FreeBSD: head/crypto/openssh/auth2-chall.c 147005 2005-06-05 15:46:09Z des $"); |
28 29#include "ssh2.h" 30#include "auth.h" 31#include "buffer.h" 32#include "packet.h" 33#include "xmalloc.h" 34#include "dispatch.h" 35#include "log.h"
|
36#include "servconf.h" |
37
|
38/* import */ 39extern ServerOptions options; 40 |
41static int auth2_challenge_start(Authctxt *); 42static int send_userauth_info_request(Authctxt *); 43static void input_userauth_info_response(int, u_int32_t, void *); 44 45#ifdef BSD_AUTH 46extern KbdintDevice bsdauth_device; 47#else 48#ifdef USE_PAM 49extern KbdintDevice sshpam_device; 50#endif 51#ifdef SKEY 52extern KbdintDevice skey_device; 53#endif 54#endif 55 56KbdintDevice *devices[] = { 57#ifdef BSD_AUTH 58 &bsdauth_device, 59#else 60#ifdef USE_PAM 61 &sshpam_device, 62#endif 63#ifdef SKEY 64 &skey_device, 65#endif 66#endif 67 NULL 68}; 69 70typedef struct KbdintAuthctxt KbdintAuthctxt; 71struct KbdintAuthctxt 72{ 73 char *devices; 74 void *ctxt; 75 KbdintDevice *device; 76 u_int nreq; 77}; 78
|
79#ifdef USE_PAM 80void 81remove_kbdint_device(const char *devname) 82{ 83 int i, j; 84 85 for (i = 0; devices[i] != NULL; i++) 86 if (strcmp(devices[i]->name, devname) == 0) { 87 for (j = i; devices[j] != NULL; j++) 88 devices[j] = devices[j+1]; 89 i--; 90 } 91} 92#endif 93 |
94static KbdintAuthctxt * 95kbdint_alloc(const char *devs) 96{ 97 KbdintAuthctxt *kbdintctxt; 98 Buffer b; 99 int i; 100
|
101#ifdef USE_PAM 102 if (!options.use_pam) 103 remove_kbdint_device("pam"); 104#endif 105 |
106 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt)); 107 if (strcmp(devs, "") == 0) { 108 buffer_init(&b); 109 for (i = 0; devices[i]; i++) { 110 if (buffer_len(&b) > 0) 111 buffer_append(&b, ",", 1); 112 buffer_append(&b, devices[i]->name, 113 strlen(devices[i]->name)); 114 } 115 buffer_append(&b, "\0", 1); 116 kbdintctxt->devices = xstrdup(buffer_ptr(&b)); 117 buffer_free(&b); 118 } else { 119 kbdintctxt->devices = xstrdup(devs); 120 } 121 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); 122 kbdintctxt->ctxt = NULL; 123 kbdintctxt->device = NULL; 124 kbdintctxt->nreq = 0; 125 126 return kbdintctxt; 127} 128static void 129kbdint_reset_device(KbdintAuthctxt *kbdintctxt) 130{ 131 if (kbdintctxt->ctxt) { 132 kbdintctxt->device->free_ctx(kbdintctxt->ctxt); 133 kbdintctxt->ctxt = NULL; 134 } 135 kbdintctxt->device = NULL; 136} 137static void 138kbdint_free(KbdintAuthctxt *kbdintctxt) 139{ 140 if (kbdintctxt->device) 141 kbdint_reset_device(kbdintctxt); 142 if (kbdintctxt->devices) { 143 xfree(kbdintctxt->devices); 144 kbdintctxt->devices = NULL; 145 } 146 xfree(kbdintctxt); 147} 148/* get next device */ 149static int 150kbdint_next_device(KbdintAuthctxt *kbdintctxt) 151{ 152 size_t len; 153 char *t; 154 int i; 155 156 if (kbdintctxt->device) 157 kbdint_reset_device(kbdintctxt); 158 do { 159 len = kbdintctxt->devices ? 160 strcspn(kbdintctxt->devices, ",") : 0; 161 162 if (len == 0) 163 break; 164 for (i = 0; devices[i]; i++) 165 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) 166 kbdintctxt->device = devices[i]; 167 t = kbdintctxt->devices; 168 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; 169 xfree(t); 170 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? 171 kbdintctxt->devices : "<empty>"); 172 } while (kbdintctxt->devices && !kbdintctxt->device); 173 174 return kbdintctxt->device ? 1 : 0; 175} 176 177/* 178 * try challenge-response, set authctxt->postponed if we have to 179 * wait for the response. 180 */ 181int 182auth2_challenge(Authctxt *authctxt, char *devs) 183{ 184 debug("auth2_challenge: user=%s devs=%s", 185 authctxt->user ? authctxt->user : "<nouser>", 186 devs ? devs : "<no devs>"); 187 188 if (authctxt->user == NULL || !devs) 189 return 0; 190 if (authctxt->kbdintctxt == NULL) 191 authctxt->kbdintctxt = kbdint_alloc(devs); 192 return auth2_challenge_start(authctxt); 193} 194 195/* unregister kbd-int callbacks and context */ 196void 197auth2_challenge_stop(Authctxt *authctxt) 198{ 199 /* unregister callback */ 200 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); 201 if (authctxt->kbdintctxt != NULL) { 202 kbdint_free(authctxt->kbdintctxt); 203 authctxt->kbdintctxt = NULL; 204 } 205} 206 207/* side effect: sets authctxt->postponed if a reply was sent*/ 208static int 209auth2_challenge_start(Authctxt *authctxt) 210{ 211 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; 212 213 debug2("auth2_challenge_start: devices %s", 214 kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); 215 216 if (kbdint_next_device(kbdintctxt) == 0) { 217 auth2_challenge_stop(authctxt); 218 return 0; 219 } 220 debug("auth2_challenge_start: trying authentication method '%s'", 221 kbdintctxt->device->name); 222 223 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { 224 auth2_challenge_stop(authctxt); 225 return 0; 226 } 227 if (send_userauth_info_request(authctxt) == 0) { 228 auth2_challenge_stop(authctxt); 229 return 0; 230 } 231 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, 232 &input_userauth_info_response); 233 234 authctxt->postponed = 1; 235 return 0; 236} 237 238static int 239send_userauth_info_request(Authctxt *authctxt) 240{ 241 KbdintAuthctxt *kbdintctxt; 242 char *name, *instr, **prompts; 243 int i; 244 u_int *echo_on; 245 246 kbdintctxt = authctxt->kbdintctxt; 247 if (kbdintctxt->device->query(kbdintctxt->ctxt, 248 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) 249 return 0; 250 251 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); 252 packet_put_cstring(name); 253 packet_put_cstring(instr); 254 packet_put_cstring(""); /* language not used */ 255 packet_put_int(kbdintctxt->nreq); 256 for (i = 0; i < kbdintctxt->nreq; i++) { 257 packet_put_cstring(prompts[i]); 258 packet_put_char(echo_on[i]); 259 } 260 packet_send(); 261 packet_write_wait(); 262 263 for (i = 0; i < kbdintctxt->nreq; i++) 264 xfree(prompts[i]); 265 xfree(prompts); 266 xfree(echo_on); 267 xfree(name); 268 xfree(instr); 269 return 1; 270} 271 272static void 273input_userauth_info_response(int type, u_int32_t seq, void *ctxt) 274{ 275 Authctxt *authctxt = ctxt; 276 KbdintAuthctxt *kbdintctxt; 277 int i, authenticated = 0, res, len; 278 u_int nresp; 279 char **response = NULL, *method; 280 281 if (authctxt == NULL) 282 fatal("input_userauth_info_response: no authctxt"); 283 kbdintctxt = authctxt->kbdintctxt; 284 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) 285 fatal("input_userauth_info_response: no kbdintctxt"); 286 if (kbdintctxt->device == NULL) 287 fatal("input_userauth_info_response: no device"); 288 289 authctxt->postponed = 0; /* reset */ 290 nresp = packet_get_int(); 291 if (nresp != kbdintctxt->nreq) 292 fatal("input_userauth_info_response: wrong number of replies"); 293 if (nresp > 100) 294 fatal("input_userauth_info_response: too many replies"); 295 if (nresp > 0) { 296 response = xmalloc(nresp * sizeof(char *)); 297 for (i = 0; i < nresp; i++) 298 response[i] = packet_get_string(NULL); 299 } 300 packet_check_eom(); 301
|
278 if (authctxt->valid) {
279 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
280 nresp, response);
281 } else {
282 res = -1;
283 }
|
302 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); |
303 304 for (i = 0; i < nresp; i++) { 305 memset(response[i], 'r', strlen(response[i])); 306 xfree(response[i]); 307 } 308 if (response) 309 xfree(response); 310 311 switch (res) { 312 case 0: 313 /* Success! */
|
295 authenticated = 1;
|
314 authenticated = authctxt->valid ? 1 : 0; |
315 break; 316 case 1: 317 /* Authentication needs further interaction */ 318 if (send_userauth_info_request(authctxt) == 1) 319 authctxt->postponed = 1; 320 break; 321 default: 322 /* Failure! */ 323 break; 324 } 325 326 len = strlen("keyboard-interactive") + 2 + 327 strlen(kbdintctxt->device->name); 328 method = xmalloc(len); 329 snprintf(method, len, "keyboard-interactive/%s", 330 kbdintctxt->device->name); 331 332 if (!authctxt->postponed) { 333 if (authenticated) { 334 auth2_challenge_stop(authctxt); 335 } else { 336 /* start next device */ 337 /* may set authctxt->postponed */ 338 auth2_challenge_start(authctxt); 339 } 340 } 341 userauth_finish(authctxt, authenticated, method); 342 xfree(method); 343} 344 345void 346privsep_challenge_enable(void) 347{ 348#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) 349 int n = 0; 350#endif 351#ifdef BSD_AUTH 352 extern KbdintDevice mm_bsdauth_device; 353#endif 354#ifdef USE_PAM 355 extern KbdintDevice mm_sshpam_device; 356#endif 357#ifdef SKEY 358 extern KbdintDevice mm_skey_device; 359#endif 360 361#ifdef BSD_AUTH 362 devices[n++] = &mm_bsdauth_device; 363#else 364#ifdef USE_PAM 365 devices[n++] = &mm_sshpam_device; 366#endif 367#ifdef SKEY 368 devices[n++] = &mm_skey_device; 369#endif 370#endif 371}
|