xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth2-chall.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: auth2-chall.c,v 1.13 2017/10/07 19:39:19 christos Exp $	*/
2 /* $OpenBSD: auth2-chall.c,v 1.48 2017/05/30 14:29:59 markus 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.13 2017/10/07 19:39:19 christos Exp $");
30 #include <sys/types.h>
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 #include "xmalloc.h"
36 #include "ssh2.h"
37 #include "key.h"
38 #include "hostfile.h"
39 #include "auth.h"
40 #include "buffer.h"
41 #include "packet.h"
42 #include "dispatch.h"
43 #include "log.h"
44 #include "misc.h"
45 #include "servconf.h"
46 
47 /* import */
48 extern ServerOptions options;
49 
50 static int auth2_challenge_start(struct ssh *);
51 static int send_userauth_info_request(Authctxt *);
52 static int input_userauth_info_response(int, u_int32_t, struct ssh *);
53 
54 #ifdef BSD_AUTH
55 extern KbdintDevice bsdauth_device;
56 #else
57 #ifdef USE_PAM
58 extern KbdintDevice sshpam_device;
59 #endif
60 #ifdef SKEY
61 extern KbdintDevice skey_device;
62 #endif
63 #endif
64 
65 KbdintDevice *devices[] = {
66 #ifdef BSD_AUTH
67 	&bsdauth_device,
68 #else
69 #ifdef USE_PAM
70 	&sshpam_device,
71 #endif
72 #ifdef SKEY
73 	&skey_device,
74 #endif
75 #endif
76 	NULL
77 };
78 
79 typedef struct KbdintAuthctxt KbdintAuthctxt;
80 struct KbdintAuthctxt
81 {
82 	char *devices;
83 	void *ctxt;
84 	KbdintDevice *device;
85 	u_int nreq;
86 	u_int devices_done;
87 };
88 
89 #ifdef USE_PAM
90 void remove_kbdint_device(const char *);
91 void
92 remove_kbdint_device(const char *xdevname)
93 {
94 	int i, j;
95 
96 	for (i = 0; devices[i] != NULL; i++)
97 		if (strcmp(devices[i]->name, xdevname) == 0) {
98 			for (j = i; devices[j] != NULL; j++)
99 				devices[j] = devices[j+1];
100 			i--;
101 		}
102 }
103 #endif
104 
105 static KbdintAuthctxt *
106 kbdint_alloc(const char *devs)
107 {
108 	KbdintAuthctxt *kbdintctxt;
109 	Buffer b;
110 	int i;
111 
112 #ifdef USE_PAM
113 	if (!options.use_pam)
114 		remove_kbdint_device("pam");
115 #endif
116 
117 	kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
118 	if (strcmp(devs, "") == 0) {
119 		buffer_init(&b);
120 		for (i = 0; devices[i]; i++) {
121 			if (buffer_len(&b) > 0)
122 				buffer_append(&b, ",", 1);
123 			buffer_append(&b, devices[i]->name,
124 			    strlen(devices[i]->name));
125 		}
126 		if ((kbdintctxt->devices = sshbuf_dup_string(&b)) == NULL)
127 			fatal("%s: sshbuf_dup_string failed", __func__);
128 		buffer_free(&b);
129 	} else {
130 		kbdintctxt->devices = xstrdup(devs);
131 	}
132 	debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
133 	kbdintctxt->ctxt = NULL;
134 	kbdintctxt->device = NULL;
135 	kbdintctxt->nreq = 0;
136 
137 	return kbdintctxt;
138 }
139 static void
140 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
141 {
142 	if (kbdintctxt->ctxt) {
143 		kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
144 		kbdintctxt->ctxt = NULL;
145 	}
146 	kbdintctxt->device = NULL;
147 }
148 static void
149 kbdint_free(KbdintAuthctxt *kbdintctxt)
150 {
151 	if (kbdintctxt->device)
152 		kbdint_reset_device(kbdintctxt);
153 	free(kbdintctxt->devices);
154 	explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
155 	free(kbdintctxt);
156 }
157 /* get next device */
158 static int
159 kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
160 {
161 	size_t len;
162 	char *t;
163 	int i;
164 
165 	if (kbdintctxt->device)
166 		kbdint_reset_device(kbdintctxt);
167 	do {
168 		len = kbdintctxt->devices ?
169 		    strcspn(kbdintctxt->devices, ",") : 0;
170 
171 		if (len == 0)
172 			break;
173 		for (i = 0; devices[i]; i++) {
174 			if ((kbdintctxt->devices_done & (1 << i)) != 0 ||
175 			    !auth2_method_allowed(authctxt,
176 			    "keyboard-interactive", devices[i]->name))
177 				continue;
178 			if (strncmp(kbdintctxt->devices, devices[i]->name,
179 			    len) == 0) {
180 				kbdintctxt->device = devices[i];
181 				kbdintctxt->devices_done |= 1 << i;
182 			}
183 		}
184 		t = kbdintctxt->devices;
185 		kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
186 		free(t);
187 		debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
188 		    kbdintctxt->devices : "<empty>");
189 	} while (kbdintctxt->devices && !kbdintctxt->device);
190 
191 	return kbdintctxt->device ? 1 : 0;
192 }
193 
194 /*
195  * try challenge-response, set authctxt->postponed if we have to
196  * wait for the response.
197  */
198 int
199 auth2_challenge(struct ssh *ssh, char *devs)
200 {
201 	Authctxt *authctxt = ssh->authctxt;
202 	debug("auth2_challenge: user=%s devs=%s",
203 	    authctxt->user ? authctxt->user : "<nouser>",
204 	    devs ? devs : "<no devs>");
205 
206 	if (authctxt->user == NULL || !devs)
207 		return 0;
208 	if (authctxt->kbdintctxt == NULL)
209 		authctxt->kbdintctxt = kbdint_alloc(devs);
210 	return auth2_challenge_start(ssh);
211 }
212 
213 /* unregister kbd-int callbacks and context */
214 void
215 auth2_challenge_stop(struct ssh *ssh)
216 {
217 	Authctxt *authctxt = ssh->authctxt;
218 	/* unregister callback */
219 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
220 	if (authctxt->kbdintctxt != NULL) {
221 		kbdint_free(authctxt->kbdintctxt);
222 		authctxt->kbdintctxt = NULL;
223 	}
224 }
225 
226 /* side effect: sets authctxt->postponed if a reply was sent*/
227 static int
228 auth2_challenge_start(struct ssh *ssh)
229 {
230 	Authctxt *authctxt = ssh->authctxt;
231 	KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
232 
233 	debug2("auth2_challenge_start: devices %s",
234 	    kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
235 
236 	if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
237 		auth2_challenge_stop(ssh);
238 		return 0;
239 	}
240 	debug("auth2_challenge_start: trying authentication method '%s'",
241 	    kbdintctxt->device->name);
242 
243 	if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
244 		auth2_challenge_stop(ssh);
245 		return 0;
246 	}
247 	if (send_userauth_info_request(authctxt) == 0) {
248 		auth2_challenge_stop(ssh);
249 		return 0;
250 	}
251 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE,
252 	    &input_userauth_info_response);
253 
254 	authctxt->postponed = 1;
255 	return 0;
256 }
257 
258 static int
259 send_userauth_info_request(Authctxt *authctxt)
260 {
261 	KbdintAuthctxt *kbdintctxt;
262 	char *name, *instr, **prompts;
263 	u_int i, *echo_on;
264 
265 	kbdintctxt = authctxt->kbdintctxt;
266 	if (kbdintctxt->device->query(kbdintctxt->ctxt,
267 	    &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
268 		return 0;
269 
270 	packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
271 	packet_put_cstring(name);
272 	packet_put_cstring(instr);
273 	packet_put_cstring("");		/* language not used */
274 	packet_put_int(kbdintctxt->nreq);
275 	for (i = 0; i < kbdintctxt->nreq; i++) {
276 		packet_put_cstring(prompts[i]);
277 		packet_put_char(echo_on[i]);
278 	}
279 	packet_send();
280 	packet_write_wait();
281 
282 	for (i = 0; i < kbdintctxt->nreq; i++)
283 		free(prompts[i]);
284 	free(prompts);
285 	free(echo_on);
286 	free(name);
287 	free(instr);
288 	return 1;
289 }
290 
291 static int
292 input_userauth_info_response(int type, u_int32_t seq, struct ssh *ssh)
293 {
294 	Authctxt *authctxt = ssh->authctxt;
295 	KbdintAuthctxt *kbdintctxt;
296 	int authenticated = 0, res;
297 	u_int i, nresp;
298 	const char *devicename = NULL;
299 	char **response = NULL;
300 
301 	if (authctxt == NULL)
302 		fatal("input_userauth_info_response: no authctxt");
303 	kbdintctxt = authctxt->kbdintctxt;
304 	if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
305 		fatal("input_userauth_info_response: no kbdintctxt");
306 	if (kbdintctxt->device == NULL)
307 		fatal("input_userauth_info_response: no device");
308 
309 	authctxt->postponed = 0;	/* reset */
310 	nresp = packet_get_int();
311 	if (nresp != kbdintctxt->nreq)
312 		fatal("input_userauth_info_response: wrong number of replies");
313 	if (nresp > 100)
314 		fatal("input_userauth_info_response: too many replies");
315 	if (nresp > 0) {
316 		response = xcalloc(nresp, sizeof(char *));
317 		for (i = 0; i < nresp; i++)
318 			response[i] = packet_get_string(NULL);
319 	}
320 	packet_check_eom();
321 
322 	res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
323 
324 	for (i = 0; i < nresp; i++) {
325 		explicit_bzero(response[i], strlen(response[i]));
326 		free(response[i]);
327 	}
328 	free(response);
329 
330 	switch (res) {
331 	case 0:
332 		/* Success! */
333 		authenticated = authctxt->valid ? 1 : 0;
334 		break;
335 	case 1:
336 		/* Authentication needs further interaction */
337 		if (send_userauth_info_request(authctxt) == 1)
338 			authctxt->postponed = 1;
339 		break;
340 	default:
341 		/* Failure! */
342 		break;
343 	}
344 	devicename = kbdintctxt->device->name;
345 	if (!authctxt->postponed) {
346 		if (authenticated) {
347 			auth2_challenge_stop(ssh);
348 		} else {
349 			/* start next device */
350 			/* may set authctxt->postponed */
351 			auth2_challenge_start(ssh);
352 		}
353 	}
354 	userauth_finish(ssh, authenticated, "keyboard-interactive",
355 	    devicename);
356 	return 0;
357 }
358 
359 void
360 privsep_challenge_enable(void)
361 {
362 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
363 	int n = 0;
364 #endif
365 #ifdef BSD_AUTH
366 	extern KbdintDevice mm_bsdauth_device;
367 #endif
368 #ifdef USE_PAM
369 	extern KbdintDevice mm_sshpam_device;
370 #endif
371 #ifdef SKEY
372 	extern KbdintDevice mm_skey_device;
373 #endif
374 	/* As long as SSHv1 has devices[0] hard coded this is fine */
375 #ifdef BSD_AUTH
376 	devices[n++] = &mm_bsdauth_device;
377 #endif
378 #ifdef USE_PAM
379 	devices[n++] = &mm_sshpam_device;
380 #endif
381 #ifdef SKEY
382 	devices[n++] = &mm_skey_device;
383 #endif
384 }
385