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