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