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