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