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