]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - crypto/openssh/auth2-chall.c
Fix resource exhaustion in TCP reassembly. [SA-15:15]
[FreeBSD/stable/8.git] / crypto / openssh / auth2-chall.c
1 /* $OpenBSD: auth2-chall.c,v 1.34 2008/12/09 04:32:22 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
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 *
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 = xmalloc(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
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
147 kbdint_free(KbdintAuthctxt *kbdintctxt)
148 {
149         if (kbdintctxt->device)
150                 kbdint_reset_device(kbdintctxt);
151         if (kbdintctxt->devices) {
152                 xfree(kbdintctxt->devices);
153                 kbdintctxt->devices = NULL;
154         }
155         xfree(kbdintctxt);
156 }
157 /* get next device */
158 static int
159 kbdint_next_device(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                                 continue;
176                         if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) {
177                                 kbdintctxt->device = devices[i];
178                                 kbdintctxt->devices_done |= 1 << i;
179                         }
180                 }
181                 t = kbdintctxt->devices;
182                 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
183                 xfree(t);
184                 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
185                     kbdintctxt->devices : "<empty>");
186         } while (kbdintctxt->devices && !kbdintctxt->device);
187
188         return kbdintctxt->device ? 1 : 0;
189 }
190
191 /*
192  * try challenge-response, set authctxt->postponed if we have to
193  * wait for the response.
194  */
195 int
196 auth2_challenge(Authctxt *authctxt, char *devs)
197 {
198         debug("auth2_challenge: user=%s devs=%s",
199             authctxt->user ? authctxt->user : "<nouser>",
200             devs ? devs : "<no devs>");
201
202         if (authctxt->user == NULL || !devs)
203                 return 0;
204         if (authctxt->kbdintctxt == NULL)
205                 authctxt->kbdintctxt = kbdint_alloc(devs);
206         return auth2_challenge_start(authctxt);
207 }
208
209 /* unregister kbd-int callbacks and context */
210 void
211 auth2_challenge_stop(Authctxt *authctxt)
212 {
213         /* unregister callback */
214         dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
215         if (authctxt->kbdintctxt != NULL) {
216                 kbdint_free(authctxt->kbdintctxt);
217                 authctxt->kbdintctxt = NULL;
218         }
219 }
220
221 /* side effect: sets authctxt->postponed if a reply was sent*/
222 static int
223 auth2_challenge_start(Authctxt *authctxt)
224 {
225         KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
226
227         debug2("auth2_challenge_start: devices %s",
228             kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
229
230         if (kbdint_next_device(kbdintctxt) == 0) {
231                 auth2_challenge_stop(authctxt);
232                 return 0;
233         }
234         debug("auth2_challenge_start: trying authentication method '%s'",
235             kbdintctxt->device->name);
236
237         if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
238                 auth2_challenge_stop(authctxt);
239                 return 0;
240         }
241         if (send_userauth_info_request(authctxt) == 0) {
242                 auth2_challenge_stop(authctxt);
243                 return 0;
244         }
245         dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
246             &input_userauth_info_response);
247
248         authctxt->postponed = 1;
249         return 0;
250 }
251
252 static int
253 send_userauth_info_request(Authctxt *authctxt)
254 {
255         KbdintAuthctxt *kbdintctxt;
256         char *name, *instr, **prompts;
257         u_int i, *echo_on;
258
259         kbdintctxt = authctxt->kbdintctxt;
260         if (kbdintctxt->device->query(kbdintctxt->ctxt,
261             &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
262                 return 0;
263
264         packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
265         packet_put_cstring(name);
266         packet_put_cstring(instr);
267         packet_put_cstring("");         /* language not used */
268         packet_put_int(kbdintctxt->nreq);
269         for (i = 0; i < kbdintctxt->nreq; i++) {
270                 packet_put_cstring(prompts[i]);
271                 packet_put_char(echo_on[i]);
272         }
273         packet_send();
274         packet_write_wait();
275
276         for (i = 0; i < kbdintctxt->nreq; i++)
277                 xfree(prompts[i]);
278         xfree(prompts);
279         xfree(echo_on);
280         xfree(name);
281         xfree(instr);
282         return 1;
283 }
284
285 static void
286 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
287 {
288         Authctxt *authctxt = ctxt;
289         KbdintAuthctxt *kbdintctxt;
290         int authenticated = 0, res;
291         u_int i, nresp;
292         char **response = NULL, *method;
293
294         if (authctxt == NULL)
295                 fatal("input_userauth_info_response: no authctxt");
296         kbdintctxt = authctxt->kbdintctxt;
297         if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
298                 fatal("input_userauth_info_response: no kbdintctxt");
299         if (kbdintctxt->device == NULL)
300                 fatal("input_userauth_info_response: no device");
301
302         authctxt->postponed = 0;        /* reset */
303         nresp = packet_get_int();
304         if (nresp != kbdintctxt->nreq)
305                 fatal("input_userauth_info_response: wrong number of replies");
306         if (nresp > 100)
307                 fatal("input_userauth_info_response: too many replies");
308         if (nresp > 0) {
309                 response = xcalloc(nresp, sizeof(char *));
310                 for (i = 0; i < nresp; i++)
311                         response[i] = packet_get_string(NULL);
312         }
313         packet_check_eom();
314
315         res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
316
317         for (i = 0; i < nresp; i++) {
318                 memset(response[i], 'r', strlen(response[i]));
319                 xfree(response[i]);
320         }
321         if (response)
322                 xfree(response);
323
324         switch (res) {
325         case 0:
326                 /* Success! */
327                 authenticated = authctxt->valid ? 1 : 0;
328                 break;
329         case 1:
330                 /* Authentication needs further interaction */
331                 if (send_userauth_info_request(authctxt) == 1)
332                         authctxt->postponed = 1;
333                 break;
334         default:
335                 /* Failure! */
336                 break;
337         }
338
339         xasprintf(&method, "keyboard-interactive/%s", kbdintctxt->device->name);
340
341         if (!authctxt->postponed) {
342                 if (authenticated) {
343                         auth2_challenge_stop(authctxt);
344                 } else {
345                         /* start next device */
346                         /* may set authctxt->postponed */
347                         auth2_challenge_start(authctxt);
348                 }
349         }
350         userauth_finish(authctxt, authenticated, method);
351         xfree(method);
352 }
353
354 void
355 privsep_challenge_enable(void)
356 {
357 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
358         int n = 0;
359 #endif
360 #ifdef BSD_AUTH
361         extern KbdintDevice mm_bsdauth_device;
362 #endif
363 #ifdef USE_PAM
364         extern KbdintDevice mm_sshpam_device;
365 #endif
366 #ifdef SKEY
367         extern KbdintDevice mm_skey_device;
368 #endif
369
370 #ifdef BSD_AUTH
371         devices[n++] = &mm_bsdauth_device;
372 #else
373 #ifdef USE_PAM
374         devices[n++] = &mm_sshpam_device;
375 #endif
376 #ifdef SKEY
377         devices[n++] = &mm_skey_device;
378 #endif
379 #endif
380 }