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