auth2-chall.c revision 255767
1255767Sdes/* $OpenBSD: auth2-chall.c,v 1.38 2013/05/17 00:13:13 djm Exp $ */ 276259Sgreen/* 376259Sgreen * Copyright (c) 2001 Markus Friedl. All rights reserved. 492555Sdes * Copyright (c) 2001 Per Allansson. All rights reserved. 576259Sgreen * 676259Sgreen * Redistribution and use in source and binary forms, with or without 776259Sgreen * modification, are permitted provided that the following conditions 876259Sgreen * are met: 976259Sgreen * 1. Redistributions of source code must retain the above copyright 1076259Sgreen * notice, this list of conditions and the following disclaimer. 1176259Sgreen * 2. Redistributions in binary form must reproduce the above copyright 1276259Sgreen * notice, this list of conditions and the following disclaimer in the 1376259Sgreen * documentation and/or other materials provided with the distribution. 1476259Sgreen * 1576259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1676259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1776259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1876259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1976259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2076259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2176259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2276259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2376259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2476259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2576259Sgreen */ 26162856Sdes 2776259Sgreen#include "includes.h" 2876259Sgreen 29162856Sdes#include <sys/types.h> 30162856Sdes 31162856Sdes#include <stdarg.h> 32162856Sdes#include <stdio.h> 33162856Sdes#include <string.h> 34162856Sdes 35162856Sdes#include "xmalloc.h" 3676259Sgreen#include "ssh2.h" 37162856Sdes#include "key.h" 38162856Sdes#include "hostfile.h" 3976259Sgreen#include "auth.h" 4092555Sdes#include "buffer.h" 4176259Sgreen#include "packet.h" 4276259Sgreen#include "dispatch.h" 4376259Sgreen#include "log.h" 44147005Sdes#include "servconf.h" 4576259Sgreen 46147005Sdes/* import */ 47147005Sdesextern ServerOptions options; 48147005Sdes 4992555Sdesstatic int auth2_challenge_start(Authctxt *); 5092555Sdesstatic int send_userauth_info_request(Authctxt *); 5192555Sdesstatic void input_userauth_info_response(int, u_int32_t, void *); 5276259Sgreen 5392555Sdes#ifdef BSD_AUTH 5492555Sdesextern KbdintDevice bsdauth_device; 5598941Sdes#else 5699052Sdes#ifdef USE_PAM 57124211Sdesextern KbdintDevice sshpam_device; 5899052Sdes#endif 5998941Sdes#ifdef SKEY 6092555Sdesextern KbdintDevice skey_device; 6192555Sdes#endif 6298941Sdes#endif 6392555Sdes 6492555SdesKbdintDevice *devices[] = { 6592555Sdes#ifdef BSD_AUTH 6692555Sdes &bsdauth_device, 6798941Sdes#else 6899052Sdes#ifdef USE_PAM 69124211Sdes &sshpam_device, 7099052Sdes#endif 7198941Sdes#ifdef SKEY 7292555Sdes &skey_device, 7392555Sdes#endif 7498941Sdes#endif 7592555Sdes NULL 7692555Sdes}; 7792555Sdes 7892555Sdestypedef struct KbdintAuthctxt KbdintAuthctxt; 7992555Sdesstruct KbdintAuthctxt 8092555Sdes{ 8192555Sdes char *devices; 8292555Sdes void *ctxt; 8392555Sdes KbdintDevice *device; 8499063Sdes u_int nreq; 8592555Sdes}; 8692555Sdes 87147005Sdes#ifdef USE_PAM 88147005Sdesvoid 89147005Sdesremove_kbdint_device(const char *devname) 90147005Sdes{ 91147005Sdes int i, j; 92147005Sdes 93147005Sdes for (i = 0; devices[i] != NULL; i++) 94147005Sdes if (strcmp(devices[i]->name, devname) == 0) { 95147005Sdes for (j = i; devices[j] != NULL; j++) 96147005Sdes devices[j] = devices[j+1]; 97147005Sdes i--; 98147005Sdes } 99147005Sdes} 100147005Sdes#endif 101147005Sdes 10292555Sdesstatic KbdintAuthctxt * 10392555Sdeskbdint_alloc(const char *devs) 10492555Sdes{ 10592555Sdes KbdintAuthctxt *kbdintctxt; 10692555Sdes Buffer b; 10792555Sdes int i; 10892555Sdes 109147005Sdes#ifdef USE_PAM 110147005Sdes if (!options.use_pam) 111147005Sdes remove_kbdint_device("pam"); 112147005Sdes#endif 113147005Sdes 11492555Sdes kbdintctxt = xmalloc(sizeof(KbdintAuthctxt)); 11592555Sdes if (strcmp(devs, "") == 0) { 11692555Sdes buffer_init(&b); 11792555Sdes for (i = 0; devices[i]; i++) { 11892555Sdes if (buffer_len(&b) > 0) 11992555Sdes buffer_append(&b, ",", 1); 12092555Sdes buffer_append(&b, devices[i]->name, 12192555Sdes strlen(devices[i]->name)); 12292555Sdes } 12392555Sdes buffer_append(&b, "\0", 1); 12492555Sdes kbdintctxt->devices = xstrdup(buffer_ptr(&b)); 12592555Sdes buffer_free(&b); 12692555Sdes } else { 12792555Sdes kbdintctxt->devices = xstrdup(devs); 12892555Sdes } 12992555Sdes debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); 13092555Sdes kbdintctxt->ctxt = NULL; 13192555Sdes kbdintctxt->device = NULL; 13299063Sdes kbdintctxt->nreq = 0; 13392555Sdes 13492555Sdes return kbdintctxt; 13592555Sdes} 13692555Sdesstatic void 13792555Sdeskbdint_reset_device(KbdintAuthctxt *kbdintctxt) 13892555Sdes{ 13992555Sdes if (kbdintctxt->ctxt) { 14092555Sdes kbdintctxt->device->free_ctx(kbdintctxt->ctxt); 14192555Sdes kbdintctxt->ctxt = NULL; 14292555Sdes } 14392555Sdes kbdintctxt->device = NULL; 14492555Sdes} 14592555Sdesstatic void 14692555Sdeskbdint_free(KbdintAuthctxt *kbdintctxt) 14792555Sdes{ 14892555Sdes if (kbdintctxt->device) 14992555Sdes kbdint_reset_device(kbdintctxt); 150255767Sdes free(kbdintctxt->devices); 151255767Sdes bzero(kbdintctxt, sizeof(*kbdintctxt)); 152255767Sdes free(kbdintctxt); 15392555Sdes} 15492555Sdes/* get next device */ 15592555Sdesstatic int 156255767Sdeskbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt) 15792555Sdes{ 15892555Sdes size_t len; 15992555Sdes char *t; 16092555Sdes int i; 16192555Sdes 16292555Sdes if (kbdintctxt->device) 16392555Sdes kbdint_reset_device(kbdintctxt); 16492555Sdes do { 16592555Sdes len = kbdintctxt->devices ? 16692555Sdes strcspn(kbdintctxt->devices, ",") : 0; 16792555Sdes 16892555Sdes if (len == 0) 16992555Sdes break; 170255767Sdes for (i = 0; devices[i]; i++) { 171255767Sdes if (!auth2_method_allowed(authctxt, 172255767Sdes "keyboard-interactive", devices[i]->name)) 173255767Sdes continue; 17492555Sdes if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) 17592555Sdes kbdintctxt->device = devices[i]; 176255767Sdes } 17792555Sdes t = kbdintctxt->devices; 17892555Sdes kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; 179255767Sdes free(t); 18092555Sdes debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? 181149753Sdes kbdintctxt->devices : "<empty>"); 18292555Sdes } while (kbdintctxt->devices && !kbdintctxt->device); 18392555Sdes 18492555Sdes return kbdintctxt->device ? 1 : 0; 18592555Sdes} 18692555Sdes 18776259Sgreen/* 18892555Sdes * try challenge-response, set authctxt->postponed if we have to 18976259Sgreen * wait for the response. 19076259Sgreen */ 19176259Sgreenint 19276259Sgreenauth2_challenge(Authctxt *authctxt, char *devs) 19376259Sgreen{ 19492555Sdes debug("auth2_challenge: user=%s devs=%s", 19592555Sdes authctxt->user ? authctxt->user : "<nouser>", 19692555Sdes devs ? devs : "<no devs>"); 19776259Sgreen 19892555Sdes if (authctxt->user == NULL || !devs) 19976259Sgreen return 0; 20092555Sdes if (authctxt->kbdintctxt == NULL) 20192555Sdes authctxt->kbdintctxt = kbdint_alloc(devs); 20292555Sdes return auth2_challenge_start(authctxt); 20392555Sdes} 20492555Sdes 20592555Sdes/* unregister kbd-int callbacks and context */ 20692555Sdesvoid 20792555Sdesauth2_challenge_stop(Authctxt *authctxt) 20892555Sdes{ 20992555Sdes /* unregister callback */ 21092555Sdes dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); 211181111Sdes if (authctxt->kbdintctxt != NULL) { 21292555Sdes kbdint_free(authctxt->kbdintctxt); 21392555Sdes authctxt->kbdintctxt = NULL; 21492555Sdes } 21592555Sdes} 21692555Sdes 21792555Sdes/* side effect: sets authctxt->postponed if a reply was sent*/ 21892555Sdesstatic int 21992555Sdesauth2_challenge_start(Authctxt *authctxt) 22092555Sdes{ 22192555Sdes KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; 22292555Sdes 22392555Sdes debug2("auth2_challenge_start: devices %s", 22492555Sdes kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); 22592555Sdes 226255767Sdes if (kbdint_next_device(authctxt, kbdintctxt) == 0) { 22792555Sdes auth2_challenge_stop(authctxt); 22876259Sgreen return 0; 22992555Sdes } 23092555Sdes debug("auth2_challenge_start: trying authentication method '%s'", 23192555Sdes kbdintctxt->device->name); 23292555Sdes 23392555Sdes if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { 23492555Sdes auth2_challenge_stop(authctxt); 23592555Sdes return 0; 23692555Sdes } 23792555Sdes if (send_userauth_info_request(authctxt) == 0) { 23892555Sdes auth2_challenge_stop(authctxt); 23992555Sdes return 0; 24092555Sdes } 24176259Sgreen dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, 24276259Sgreen &input_userauth_info_response); 24392555Sdes 24476259Sgreen authctxt->postponed = 1; 24576259Sgreen return 0; 24676259Sgreen} 24776259Sgreen 24892555Sdesstatic int 24992555Sdessend_userauth_info_request(Authctxt *authctxt) 25076259Sgreen{ 25192555Sdes KbdintAuthctxt *kbdintctxt; 25292555Sdes char *name, *instr, **prompts; 253149753Sdes u_int i, *echo_on; 25476259Sgreen 25592555Sdes kbdintctxt = authctxt->kbdintctxt; 25692555Sdes if (kbdintctxt->device->query(kbdintctxt->ctxt, 25799063Sdes &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) 25892555Sdes return 0; 25992555Sdes 26076259Sgreen packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); 26192555Sdes packet_put_cstring(name); 26292555Sdes packet_put_cstring(instr); 26398684Sdes packet_put_cstring(""); /* language not used */ 26499063Sdes packet_put_int(kbdintctxt->nreq); 26599063Sdes for (i = 0; i < kbdintctxt->nreq; i++) { 26692555Sdes packet_put_cstring(prompts[i]); 26792555Sdes packet_put_char(echo_on[i]); 26892555Sdes } 26976259Sgreen packet_send(); 27076259Sgreen packet_write_wait(); 27192555Sdes 27299063Sdes for (i = 0; i < kbdintctxt->nreq; i++) 273255767Sdes free(prompts[i]); 274255767Sdes free(prompts); 275255767Sdes free(echo_on); 276255767Sdes free(name); 277255767Sdes free(instr); 27892555Sdes return 1; 27976259Sgreen} 28076259Sgreen 28192555Sdesstatic void 28292555Sdesinput_userauth_info_response(int type, u_int32_t seq, void *ctxt) 28376259Sgreen{ 28476259Sgreen Authctxt *authctxt = ctxt; 28592555Sdes KbdintAuthctxt *kbdintctxt; 286192595Sdes int authenticated = 0, res; 287149753Sdes u_int i, nresp; 288248619Sdes const char *devicename = NULL; 289248619Sdes char **response = NULL; 29076259Sgreen 29176259Sgreen if (authctxt == NULL) 29276259Sgreen fatal("input_userauth_info_response: no authctxt"); 29392555Sdes kbdintctxt = authctxt->kbdintctxt; 29492555Sdes if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) 29592555Sdes fatal("input_userauth_info_response: no kbdintctxt"); 29692555Sdes if (kbdintctxt->device == NULL) 29792555Sdes fatal("input_userauth_info_response: no device"); 29876259Sgreen 29976259Sgreen authctxt->postponed = 0; /* reset */ 30076259Sgreen nresp = packet_get_int(); 30199063Sdes if (nresp != kbdintctxt->nreq) 30299063Sdes fatal("input_userauth_info_response: wrong number of replies"); 30399063Sdes if (nresp > 100) 30499063Sdes fatal("input_userauth_info_response: too many replies"); 30592555Sdes if (nresp > 0) { 306162856Sdes response = xcalloc(nresp, sizeof(char *)); 30792555Sdes for (i = 0; i < nresp; i++) 30892555Sdes response[i] = packet_get_string(NULL); 30992555Sdes } 31092555Sdes packet_check_eom(); 31192555Sdes 312147005Sdes res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); 31392555Sdes 31492555Sdes for (i = 0; i < nresp; i++) { 31592555Sdes memset(response[i], 'r', strlen(response[i])); 316255767Sdes free(response[i]); 31792555Sdes } 318255767Sdes free(response); 31992555Sdes 32092555Sdes switch (res) { 32192555Sdes case 0: 32292555Sdes /* Success! */ 323147005Sdes authenticated = authctxt->valid ? 1 : 0; 32492555Sdes break; 32592555Sdes case 1: 32692555Sdes /* Authentication needs further interaction */ 32792555Sdes if (send_userauth_info_request(authctxt) == 1) 32892555Sdes authctxt->postponed = 1; 32992555Sdes break; 33092555Sdes default: 33192555Sdes /* Failure! */ 33292555Sdes break; 33376259Sgreen } 334248619Sdes devicename = kbdintctxt->device->name; 33592555Sdes if (!authctxt->postponed) { 33692555Sdes if (authenticated) { 33792555Sdes auth2_challenge_stop(authctxt); 33892555Sdes } else { 33992555Sdes /* start next device */ 34092555Sdes /* may set authctxt->postponed */ 34192555Sdes auth2_challenge_start(authctxt); 34292555Sdes } 34392555Sdes } 344248619Sdes userauth_finish(authctxt, authenticated, "keyboard-interactive", 345248619Sdes devicename); 34676259Sgreen} 34798684Sdes 34898684Sdesvoid 34998684Sdesprivsep_challenge_enable(void) 35098684Sdes{ 351124211Sdes#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) 352124211Sdes int n = 0; 353124211Sdes#endif 35498684Sdes#ifdef BSD_AUTH 35598684Sdes extern KbdintDevice mm_bsdauth_device; 35698684Sdes#endif 35799052Sdes#ifdef USE_PAM 358124211Sdes extern KbdintDevice mm_sshpam_device; 35999052Sdes#endif 36098684Sdes#ifdef SKEY 36198684Sdes extern KbdintDevice mm_skey_device; 36298684Sdes#endif 36399052Sdes 36498684Sdes#ifdef BSD_AUTH 36599052Sdes devices[n++] = &mm_bsdauth_device; 36698684Sdes#else 36799052Sdes#ifdef USE_PAM 368124211Sdes devices[n++] = &mm_sshpam_device; 36999052Sdes#endif 37098684Sdes#ifdef SKEY 37199052Sdes devices[n++] = &mm_skey_device; 37298684Sdes#endif 37398684Sdes#endif 37498684Sdes} 375