1323124Sdes/* $OpenBSD: auth2-chall.c,v 1.44 2016/05/02 08:49:03 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" 44295367Sdes#include "misc.h" 45147005Sdes#include "servconf.h" 4676259Sgreen 47147005Sdes/* import */ 48147005Sdesextern ServerOptions options; 49147005Sdes 5092555Sdesstatic int auth2_challenge_start(Authctxt *); 5192555Sdesstatic int send_userauth_info_request(Authctxt *); 52295367Sdesstatic int input_userauth_info_response(int, u_int32_t, void *); 5376259Sgreen 5492555Sdes#ifdef BSD_AUTH 5592555Sdesextern KbdintDevice bsdauth_device; 5698941Sdes#else 5799052Sdes#ifdef USE_PAM 58124211Sdesextern KbdintDevice sshpam_device; 5999052Sdes#endif 6098941Sdes#ifdef SKEY 6192555Sdesextern KbdintDevice skey_device; 6292555Sdes#endif 6398941Sdes#endif 6492555Sdes 6592555SdesKbdintDevice *devices[] = { 6692555Sdes#ifdef BSD_AUTH 6792555Sdes &bsdauth_device, 6898941Sdes#else 6999052Sdes#ifdef USE_PAM 70124211Sdes &sshpam_device, 7199052Sdes#endif 7298941Sdes#ifdef SKEY 7392555Sdes &skey_device, 7492555Sdes#endif 7598941Sdes#endif 7692555Sdes NULL 7792555Sdes}; 7892555Sdes 7992555Sdestypedef struct KbdintAuthctxt KbdintAuthctxt; 8092555Sdesstruct KbdintAuthctxt 8192555Sdes{ 8292555Sdes char *devices; 8392555Sdes void *ctxt; 8492555Sdes KbdintDevice *device; 8599063Sdes u_int nreq; 86285976Sdelphij u_int devices_done; 8792555Sdes}; 8892555Sdes 89147005Sdes#ifdef USE_PAM 90147005Sdesvoid 91147005Sdesremove_kbdint_device(const char *devname) 92147005Sdes{ 93147005Sdes int i, j; 94147005Sdes 95147005Sdes for (i = 0; devices[i] != NULL; i++) 96147005Sdes if (strcmp(devices[i]->name, devname) == 0) { 97147005Sdes for (j = i; devices[j] != NULL; j++) 98147005Sdes devices[j] = devices[j+1]; 99147005Sdes i--; 100147005Sdes } 101147005Sdes} 102147005Sdes#endif 103147005Sdes 10492555Sdesstatic KbdintAuthctxt * 10592555Sdeskbdint_alloc(const char *devs) 10692555Sdes{ 10792555Sdes KbdintAuthctxt *kbdintctxt; 10892555Sdes Buffer b; 10992555Sdes int i; 11092555Sdes 111147005Sdes#ifdef USE_PAM 112147005Sdes if (!options.use_pam) 113147005Sdes remove_kbdint_device("pam"); 114147005Sdes#endif 115147005Sdes 116258343Sdes kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt)); 11792555Sdes if (strcmp(devs, "") == 0) { 11892555Sdes buffer_init(&b); 11992555Sdes for (i = 0; devices[i]; i++) { 12092555Sdes if (buffer_len(&b) > 0) 12192555Sdes buffer_append(&b, ",", 1); 12292555Sdes buffer_append(&b, devices[i]->name, 12392555Sdes strlen(devices[i]->name)); 12492555Sdes } 125323124Sdes if ((kbdintctxt->devices = sshbuf_dup_string(&b)) == NULL) 126323124Sdes fatal("%s: sshbuf_dup_string failed", __func__); 12792555Sdes buffer_free(&b); 12892555Sdes } else { 12992555Sdes kbdintctxt->devices = xstrdup(devs); 13092555Sdes } 13192555Sdes debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); 13292555Sdes kbdintctxt->ctxt = NULL; 13392555Sdes kbdintctxt->device = NULL; 13499063Sdes kbdintctxt->nreq = 0; 13592555Sdes 13692555Sdes return kbdintctxt; 13792555Sdes} 13892555Sdesstatic void 13992555Sdeskbdint_reset_device(KbdintAuthctxt *kbdintctxt) 14092555Sdes{ 14192555Sdes if (kbdintctxt->ctxt) { 14292555Sdes kbdintctxt->device->free_ctx(kbdintctxt->ctxt); 14392555Sdes kbdintctxt->ctxt = NULL; 14492555Sdes } 14592555Sdes kbdintctxt->device = NULL; 14692555Sdes} 14792555Sdesstatic void 14892555Sdeskbdint_free(KbdintAuthctxt *kbdintctxt) 14992555Sdes{ 15092555Sdes if (kbdintctxt->device) 15192555Sdes kbdint_reset_device(kbdintctxt); 152255767Sdes free(kbdintctxt->devices); 153264377Sdes explicit_bzero(kbdintctxt, sizeof(*kbdintctxt)); 154255767Sdes free(kbdintctxt); 15592555Sdes} 15692555Sdes/* get next device */ 15792555Sdesstatic int 158255767Sdeskbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt) 15992555Sdes{ 16092555Sdes size_t len; 16192555Sdes char *t; 16292555Sdes int i; 16392555Sdes 16492555Sdes if (kbdintctxt->device) 16592555Sdes kbdint_reset_device(kbdintctxt); 16692555Sdes do { 16792555Sdes len = kbdintctxt->devices ? 16892555Sdes strcspn(kbdintctxt->devices, ",") : 0; 16992555Sdes 17092555Sdes if (len == 0) 17192555Sdes break; 172255767Sdes for (i = 0; devices[i]; i++) { 173285976Sdelphij if ((kbdintctxt->devices_done & (1 << i)) != 0 || 174285976Sdelphij !auth2_method_allowed(authctxt, 175255767Sdes "keyboard-interactive", devices[i]->name)) 176255767Sdes continue; 177285976Sdelphij if (strncmp(kbdintctxt->devices, devices[i]->name, 178285976Sdelphij len) == 0) { 17992555Sdes kbdintctxt->device = devices[i]; 180285976Sdelphij kbdintctxt->devices_done |= 1 << i; 181285976Sdelphij } 182255767Sdes } 18392555Sdes t = kbdintctxt->devices; 18492555Sdes kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; 185255767Sdes free(t); 18692555Sdes debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? 187149753Sdes kbdintctxt->devices : "<empty>"); 18892555Sdes } while (kbdintctxt->devices && !kbdintctxt->device); 18992555Sdes 19092555Sdes return kbdintctxt->device ? 1 : 0; 19192555Sdes} 19292555Sdes 19376259Sgreen/* 19492555Sdes * try challenge-response, set authctxt->postponed if we have to 19576259Sgreen * wait for the response. 19676259Sgreen */ 19776259Sgreenint 19876259Sgreenauth2_challenge(Authctxt *authctxt, char *devs) 19976259Sgreen{ 20092555Sdes debug("auth2_challenge: user=%s devs=%s", 20192555Sdes authctxt->user ? authctxt->user : "<nouser>", 20292555Sdes devs ? devs : "<no devs>"); 20376259Sgreen 20492555Sdes if (authctxt->user == NULL || !devs) 20576259Sgreen return 0; 20692555Sdes if (authctxt->kbdintctxt == NULL) 20792555Sdes authctxt->kbdintctxt = kbdint_alloc(devs); 20892555Sdes return auth2_challenge_start(authctxt); 20992555Sdes} 21092555Sdes 21192555Sdes/* unregister kbd-int callbacks and context */ 21292555Sdesvoid 21392555Sdesauth2_challenge_stop(Authctxt *authctxt) 21492555Sdes{ 21592555Sdes /* unregister callback */ 21692555Sdes dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); 217181111Sdes if (authctxt->kbdintctxt != NULL) { 21892555Sdes kbdint_free(authctxt->kbdintctxt); 21992555Sdes authctxt->kbdintctxt = NULL; 22092555Sdes } 22192555Sdes} 22292555Sdes 22392555Sdes/* side effect: sets authctxt->postponed if a reply was sent*/ 22492555Sdesstatic int 22592555Sdesauth2_challenge_start(Authctxt *authctxt) 22692555Sdes{ 22792555Sdes KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; 22892555Sdes 22992555Sdes debug2("auth2_challenge_start: devices %s", 23092555Sdes kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); 23192555Sdes 232255767Sdes if (kbdint_next_device(authctxt, kbdintctxt) == 0) { 23392555Sdes auth2_challenge_stop(authctxt); 23476259Sgreen return 0; 23592555Sdes } 23692555Sdes debug("auth2_challenge_start: trying authentication method '%s'", 23792555Sdes kbdintctxt->device->name); 23892555Sdes 23992555Sdes if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { 24092555Sdes auth2_challenge_stop(authctxt); 24192555Sdes return 0; 24292555Sdes } 24392555Sdes if (send_userauth_info_request(authctxt) == 0) { 24492555Sdes auth2_challenge_stop(authctxt); 24592555Sdes return 0; 24692555Sdes } 24776259Sgreen dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, 24876259Sgreen &input_userauth_info_response); 24992555Sdes 25076259Sgreen authctxt->postponed = 1; 25176259Sgreen return 0; 25276259Sgreen} 25376259Sgreen 25492555Sdesstatic int 25592555Sdessend_userauth_info_request(Authctxt *authctxt) 25676259Sgreen{ 25792555Sdes KbdintAuthctxt *kbdintctxt; 25892555Sdes char *name, *instr, **prompts; 259149753Sdes u_int i, *echo_on; 26076259Sgreen 26192555Sdes kbdintctxt = authctxt->kbdintctxt; 26292555Sdes if (kbdintctxt->device->query(kbdintctxt->ctxt, 26399063Sdes &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) 26492555Sdes return 0; 26592555Sdes 26676259Sgreen packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); 26792555Sdes packet_put_cstring(name); 26892555Sdes packet_put_cstring(instr); 26998684Sdes packet_put_cstring(""); /* language not used */ 27099063Sdes packet_put_int(kbdintctxt->nreq); 27199063Sdes for (i = 0; i < kbdintctxt->nreq; i++) { 27292555Sdes packet_put_cstring(prompts[i]); 27392555Sdes packet_put_char(echo_on[i]); 27492555Sdes } 27576259Sgreen packet_send(); 27676259Sgreen packet_write_wait(); 27792555Sdes 27899063Sdes for (i = 0; i < kbdintctxt->nreq; i++) 279255767Sdes free(prompts[i]); 280255767Sdes free(prompts); 281255767Sdes free(echo_on); 282255767Sdes free(name); 283255767Sdes free(instr); 28492555Sdes return 1; 28576259Sgreen} 28676259Sgreen 287295367Sdesstatic int 28892555Sdesinput_userauth_info_response(int type, u_int32_t seq, void *ctxt) 28976259Sgreen{ 29076259Sgreen Authctxt *authctxt = ctxt; 29192555Sdes KbdintAuthctxt *kbdintctxt; 292192595Sdes int authenticated = 0, res; 293149753Sdes u_int i, nresp; 294248619Sdes const char *devicename = NULL; 295248619Sdes char **response = NULL; 29676259Sgreen 29776259Sgreen if (authctxt == NULL) 29876259Sgreen fatal("input_userauth_info_response: no authctxt"); 29992555Sdes kbdintctxt = authctxt->kbdintctxt; 30092555Sdes if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) 30192555Sdes fatal("input_userauth_info_response: no kbdintctxt"); 30292555Sdes if (kbdintctxt->device == NULL) 30392555Sdes fatal("input_userauth_info_response: no device"); 30476259Sgreen 30576259Sgreen authctxt->postponed = 0; /* reset */ 30676259Sgreen nresp = packet_get_int(); 30799063Sdes if (nresp != kbdintctxt->nreq) 30899063Sdes fatal("input_userauth_info_response: wrong number of replies"); 30999063Sdes if (nresp > 100) 31099063Sdes fatal("input_userauth_info_response: too many replies"); 31192555Sdes if (nresp > 0) { 312162856Sdes response = xcalloc(nresp, sizeof(char *)); 31392555Sdes for (i = 0; i < nresp; i++) 31492555Sdes response[i] = packet_get_string(NULL); 31592555Sdes } 31692555Sdes packet_check_eom(); 31792555Sdes 318147005Sdes res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); 31992555Sdes 32092555Sdes for (i = 0; i < nresp; i++) { 321264377Sdes explicit_bzero(response[i], strlen(response[i])); 322255767Sdes free(response[i]); 32392555Sdes } 324255767Sdes free(response); 32592555Sdes 32692555Sdes switch (res) { 32792555Sdes case 0: 32892555Sdes /* Success! */ 329147005Sdes authenticated = authctxt->valid ? 1 : 0; 33092555Sdes break; 33192555Sdes case 1: 33292555Sdes /* Authentication needs further interaction */ 33392555Sdes if (send_userauth_info_request(authctxt) == 1) 33492555Sdes authctxt->postponed = 1; 33592555Sdes break; 33692555Sdes default: 33792555Sdes /* Failure! */ 33892555Sdes break; 33976259Sgreen } 340248619Sdes devicename = kbdintctxt->device->name; 34192555Sdes if (!authctxt->postponed) { 34292555Sdes if (authenticated) { 34392555Sdes auth2_challenge_stop(authctxt); 34492555Sdes } else { 34592555Sdes /* start next device */ 34692555Sdes /* may set authctxt->postponed */ 34792555Sdes auth2_challenge_start(authctxt); 34892555Sdes } 34992555Sdes } 350248619Sdes userauth_finish(authctxt, authenticated, "keyboard-interactive", 351248619Sdes devicename); 352295367Sdes return 0; 35376259Sgreen} 35498684Sdes 35598684Sdesvoid 35698684Sdesprivsep_challenge_enable(void) 35798684Sdes{ 358124211Sdes#if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) 359124211Sdes int n = 0; 360124211Sdes#endif 36198684Sdes#ifdef BSD_AUTH 36298684Sdes extern KbdintDevice mm_bsdauth_device; 36398684Sdes#endif 36499052Sdes#ifdef USE_PAM 365124211Sdes extern KbdintDevice mm_sshpam_device; 36699052Sdes#endif 36798684Sdes#ifdef SKEY 36898684Sdes extern KbdintDevice mm_skey_device; 36998684Sdes#endif 37099052Sdes 37198684Sdes#ifdef BSD_AUTH 37299052Sdes devices[n++] = &mm_bsdauth_device; 37398684Sdes#else 37499052Sdes#ifdef USE_PAM 375124211Sdes devices[n++] = &mm_sshpam_device; 37699052Sdes#endif 37798684Sdes#ifdef SKEY 37899052Sdes devices[n++] = &mm_skey_device; 37998684Sdes#endif 38098684Sdes#endif 38198684Sdes} 382