xref: /dragonfly/crypto/openssh/auth2-chall.c (revision ce74baca)
1 /* $OpenBSD: auth2-chall.c,v 1.48 2017/05/30 14:29:59 markus Exp $ */
2 /*
3  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2001 Per Allansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 
29 #include <sys/types.h>
30 
31 #include <stdarg.h>
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
91 remove_kbdint_device(const char *devname)
92 {
93 	int i, j;
94 
95 	for (i = 0; devices[i] != NULL; i++)
96 		if (strcmp(devices[i]->name, devname) == 0) {
97 			for (j = i; devices[j] != NULL; j++)
98 				devices[j] = devices[j+1];
99 			i--;
100 		}
101 }
102 #endif
103 
104 static KbdintAuthctxt *
105 kbdint_alloc(const char *devs)
106 {
107 	KbdintAuthctxt *kbdintctxt;
108 	Buffer b;
109 	int i;
110 
111 #ifdef USE_PAM
112 	if (!options.use_pam)
113 		remove_kbdint_device("pam");
114 #endif
115 
116 	kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
117 	if (strcmp(devs, "") == 0) {
118 		buffer_init(&b);
119 		for (i = 0; devices[i]; i++) {
120 			if (buffer_len(&b) > 0)
121 				buffer_append(&b, ",", 1);
122 			buffer_append(&b, devices[i]->name,
123 			    strlen(devices[i]->name));
124 		}
125 		if ((kbdintctxt->devices = sshbuf_dup_string(&b)) == NULL)
126 			fatal("%s: sshbuf_dup_string failed", __func__);
127 		buffer_free(&b);
128 	} else {
129 		kbdintctxt->devices = xstrdup(devs);
130 	}
131 	debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
132 	kbdintctxt->ctxt = NULL;
133 	kbdintctxt->device = NULL;
134 	kbdintctxt->nreq = 0;
135 
136 	return kbdintctxt;
137 }
138 static void
139 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
140 {
141 	if (kbdintctxt->ctxt) {
142 		kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
143 		kbdintctxt->ctxt = NULL;
144 	}
145 	kbdintctxt->device = NULL;
146 }
147 static void
148 kbdint_free(KbdintAuthctxt *kbdintctxt)
149 {
150 	if (kbdintctxt->device)
151 		kbdint_reset_device(kbdintctxt);
152 	free(kbdintctxt->devices);
153 	explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
154 	free(kbdintctxt);
155 }
156 /* get next device */
157 static int
158 kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
159 {
160 	size_t len;
161 	char *t;
162 	int i;
163 
164 	if (kbdintctxt->device)
165 		kbdint_reset_device(kbdintctxt);
166 	do {
167 		len = kbdintctxt->devices ?
168 		    strcspn(kbdintctxt->devices, ",") : 0;
169 
170 		if (len == 0)
171 			break;
172 		for (i = 0; devices[i]; i++) {
173 			if ((kbdintctxt->devices_done & (1 << i)) != 0 ||
174 			    !auth2_method_allowed(authctxt,
175 			    "keyboard-interactive", devices[i]->name))
176 				continue;
177 			if (strncmp(kbdintctxt->devices, devices[i]->name,
178 			    len) == 0) {
179 				kbdintctxt->device = devices[i];
180 				kbdintctxt->devices_done |= 1 << i;
181 			}
182 		}
183 		t = kbdintctxt->devices;
184 		kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
185 		free(t);
186 		debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
187 		    kbdintctxt->devices : "<empty>");
188 	} while (kbdintctxt->devices && !kbdintctxt->device);
189 
190 	return kbdintctxt->device ? 1 : 0;
191 }
192 
193 /*
194  * try challenge-response, set authctxt->postponed if we have to
195  * wait for the response.
196  */
197 int
198 auth2_challenge(struct ssh *ssh, char *devs)
199 {
200 	Authctxt *authctxt = ssh->authctxt;
201 	debug("auth2_challenge: user=%s devs=%s",
202 	    authctxt->user ? authctxt->user : "<nouser>",
203 	    devs ? devs : "<no devs>");
204 
205 	if (authctxt->user == NULL || !devs)
206 		return 0;
207 	if (authctxt->kbdintctxt == NULL)
208 		authctxt->kbdintctxt = kbdint_alloc(devs);
209 	return auth2_challenge_start(ssh);
210 }
211 
212 /* unregister kbd-int callbacks and context */
213 void
214 auth2_challenge_stop(struct ssh *ssh)
215 {
216 	Authctxt *authctxt = ssh->authctxt;
217 	/* unregister callback */
218 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
219 	if (authctxt->kbdintctxt != NULL) {
220 		kbdint_free(authctxt->kbdintctxt);
221 		authctxt->kbdintctxt = NULL;
222 	}
223 }
224 
225 /* side effect: sets authctxt->postponed if a reply was sent*/
226 static int
227 auth2_challenge_start(struct ssh *ssh)
228 {
229 	Authctxt *authctxt = ssh->authctxt;
230 	KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
231 
232 	debug2("auth2_challenge_start: devices %s",
233 	    kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
234 
235 	if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
236 		auth2_challenge_stop(ssh);
237 		return 0;
238 	}
239 	debug("auth2_challenge_start: trying authentication method '%s'",
240 	    kbdintctxt->device->name);
241 
242 	if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
243 		auth2_challenge_stop(ssh);
244 		return 0;
245 	}
246 	if (send_userauth_info_request(authctxt) == 0) {
247 		auth2_challenge_stop(ssh);
248 		return 0;
249 	}
250 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE,
251 	    &input_userauth_info_response);
252 
253 	authctxt->postponed = 1;
254 	return 0;
255 }
256 
257 static int
258 send_userauth_info_request(Authctxt *authctxt)
259 {
260 	KbdintAuthctxt *kbdintctxt;
261 	char *name, *instr, **prompts;
262 	u_int i, *echo_on;
263 
264 	kbdintctxt = authctxt->kbdintctxt;
265 	if (kbdintctxt->device->query(kbdintctxt->ctxt,
266 	    &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
267 		return 0;
268 
269 	packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
270 	packet_put_cstring(name);
271 	packet_put_cstring(instr);
272 	packet_put_cstring("");		/* language not used */
273 	packet_put_int(kbdintctxt->nreq);
274 	for (i = 0; i < kbdintctxt->nreq; i++) {
275 		packet_put_cstring(prompts[i]);
276 		packet_put_char(echo_on[i]);
277 	}
278 	packet_send();
279 	packet_write_wait();
280 
281 	for (i = 0; i < kbdintctxt->nreq; i++)
282 		free(prompts[i]);
283 	free(prompts);
284 	free(echo_on);
285 	free(name);
286 	free(instr);
287 	return 1;
288 }
289 
290 static int
291 input_userauth_info_response(int type, u_int32_t seq, struct ssh *ssh)
292 {
293 	Authctxt *authctxt = ssh->authctxt;
294 	KbdintAuthctxt *kbdintctxt;
295 	int authenticated = 0, res;
296 	u_int i, nresp;
297 	const char *devicename = NULL;
298 	char **response = NULL;
299 
300 	if (authctxt == NULL)
301 		fatal("input_userauth_info_response: no authctxt");
302 	kbdintctxt = authctxt->kbdintctxt;
303 	if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
304 		fatal("input_userauth_info_response: no kbdintctxt");
305 	if (kbdintctxt->device == NULL)
306 		fatal("input_userauth_info_response: no device");
307 
308 	authctxt->postponed = 0;	/* reset */
309 	nresp = packet_get_int();
310 	if (nresp != kbdintctxt->nreq)
311 		fatal("input_userauth_info_response: wrong number of replies");
312 	if (nresp > 100)
313 		fatal("input_userauth_info_response: too many replies");
314 	if (nresp > 0) {
315 		response = xcalloc(nresp, sizeof(char *));
316 		for (i = 0; i < nresp; i++)
317 			response[i] = packet_get_string(NULL);
318 	}
319 	packet_check_eom();
320 
321 	res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
322 
323 	for (i = 0; i < nresp; i++) {
324 		explicit_bzero(response[i], strlen(response[i]));
325 		free(response[i]);
326 	}
327 	free(response);
328 
329 	switch (res) {
330 	case 0:
331 		/* Success! */
332 		authenticated = authctxt->valid ? 1 : 0;
333 		break;
334 	case 1:
335 		/* Authentication needs further interaction */
336 		if (send_userauth_info_request(authctxt) == 1)
337 			authctxt->postponed = 1;
338 		break;
339 	default:
340 		/* Failure! */
341 		break;
342 	}
343 	devicename = kbdintctxt->device->name;
344 	if (!authctxt->postponed) {
345 		if (authenticated) {
346 			auth2_challenge_stop(ssh);
347 		} else {
348 			/* start next device */
349 			/* may set authctxt->postponed */
350 			auth2_challenge_start(ssh);
351 		}
352 	}
353 	userauth_finish(ssh, authenticated, "keyboard-interactive",
354 	    devicename);
355 	return 0;
356 }
357 
358 void
359 privsep_challenge_enable(void)
360 {
361 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
362 	int n = 0;
363 #endif
364 #ifdef BSD_AUTH
365 	extern KbdintDevice mm_bsdauth_device;
366 #endif
367 #ifdef USE_PAM
368 	extern KbdintDevice mm_sshpam_device;
369 #endif
370 #ifdef SKEY
371 	extern KbdintDevice mm_skey_device;
372 #endif
373 
374 #ifdef BSD_AUTH
375 	devices[n++] = &mm_bsdauth_device;
376 #else
377 #ifdef USE_PAM
378 	devices[n++] = &mm_sshpam_device;
379 #endif
380 #ifdef SKEY
381 	devices[n++] = &mm_skey_device;
382 #endif
383 #endif
384 }
385