]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/auth2-chall.c
This commit was manufactured by cvs2svn to create branch 'RELENG_6'.
[FreeBSD/FreeBSD.git] / crypto / openssh / auth2-chall.c
1 /*
2  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3  * Copyright (c) 2001 Per Allansson.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "includes.h"
26 RCSID("$OpenBSD: auth2-chall.c,v 1.24 2005/07/17 07:17:54 djm Exp $");
27 RCSID("$FreeBSD$");
28
29 #include "ssh2.h"
30 #include "auth.h"
31 #include "buffer.h"
32 #include "packet.h"
33 #include "xmalloc.h"
34 #include "dispatch.h"
35 #include "log.h"
36 #include "servconf.h"
37
38 /* import */
39 extern ServerOptions options;
40
41 static int auth2_challenge_start(Authctxt *);
42 static int send_userauth_info_request(Authctxt *);
43 static void input_userauth_info_response(int, u_int32_t, void *);
44
45 #ifdef BSD_AUTH
46 extern KbdintDevice bsdauth_device;
47 #else
48 #ifdef USE_PAM
49 extern KbdintDevice sshpam_device;
50 #endif
51 #ifdef SKEY
52 extern KbdintDevice skey_device;
53 #endif
54 #endif
55
56 KbdintDevice *devices[] = {
57 #ifdef BSD_AUTH
58         &bsdauth_device,
59 #else
60 #ifdef USE_PAM
61         &sshpam_device,
62 #endif
63 #ifdef SKEY
64         &skey_device,
65 #endif
66 #endif
67         NULL
68 };
69
70 typedef struct KbdintAuthctxt KbdintAuthctxt;
71 struct KbdintAuthctxt
72 {
73         char *devices;
74         void *ctxt;
75         KbdintDevice *device;
76         u_int nreq;
77 };
78
79 #ifdef USE_PAM
80 void
81 remove_kbdint_device(const char *devname)
82 {
83         int i, j;
84
85         for (i = 0; devices[i] != NULL; i++)
86                 if (strcmp(devices[i]->name, devname) == 0) {
87                         for (j = i; devices[j] != NULL; j++)
88                                 devices[j] = devices[j+1];
89                         i--;
90                 }
91 }
92 #endif
93
94 static KbdintAuthctxt *
95 kbdint_alloc(const char *devs)
96 {
97         KbdintAuthctxt *kbdintctxt;
98         Buffer b;
99         int i;
100
101 #ifdef USE_PAM
102         if (!options.use_pam)
103                 remove_kbdint_device("pam");
104 #endif
105
106         kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
107         if (strcmp(devs, "") == 0) {
108                 buffer_init(&b);
109                 for (i = 0; devices[i]; i++) {
110                         if (buffer_len(&b) > 0)
111                                 buffer_append(&b, ",", 1);
112                         buffer_append(&b, devices[i]->name,
113                             strlen(devices[i]->name));
114                 }
115                 buffer_append(&b, "\0", 1);
116                 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
117                 buffer_free(&b);
118         } else {
119                 kbdintctxt->devices = xstrdup(devs);
120         }
121         debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
122         kbdintctxt->ctxt = NULL;
123         kbdintctxt->device = NULL;
124         kbdintctxt->nreq = 0;
125
126         return kbdintctxt;
127 }
128 static void
129 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
130 {
131         if (kbdintctxt->ctxt) {
132                 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
133                 kbdintctxt->ctxt = NULL;
134         }
135         kbdintctxt->device = NULL;
136 }
137 static void
138 kbdint_free(KbdintAuthctxt *kbdintctxt)
139 {
140         if (kbdintctxt->device)
141                 kbdint_reset_device(kbdintctxt);
142         if (kbdintctxt->devices) {
143                 xfree(kbdintctxt->devices);
144                 kbdintctxt->devices = NULL;
145         }
146         xfree(kbdintctxt);
147 }
148 /* get next device */
149 static int
150 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
151 {
152         size_t len;
153         char *t;
154         int i;
155
156         if (kbdintctxt->device)
157                 kbdint_reset_device(kbdintctxt);
158         do {
159                 len = kbdintctxt->devices ?
160                     strcspn(kbdintctxt->devices, ",") : 0;
161
162                 if (len == 0)
163                         break;
164                 for (i = 0; devices[i]; i++)
165                         if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
166                                 kbdintctxt->device = devices[i];
167                 t = kbdintctxt->devices;
168                 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
169                 xfree(t);
170                 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
171                     kbdintctxt->devices : "<empty>");
172         } while (kbdintctxt->devices && !kbdintctxt->device);
173
174         return kbdintctxt->device ? 1 : 0;
175 }
176
177 /*
178  * try challenge-response, set authctxt->postponed if we have to
179  * wait for the response.
180  */
181 int
182 auth2_challenge(Authctxt *authctxt, char *devs)
183 {
184         debug("auth2_challenge: user=%s devs=%s",
185             authctxt->user ? authctxt->user : "<nouser>",
186             devs ? devs : "<no devs>");
187
188         if (authctxt->user == NULL || !devs)
189                 return 0;
190         if (authctxt->kbdintctxt == NULL)
191                 authctxt->kbdintctxt = kbdint_alloc(devs);
192         return auth2_challenge_start(authctxt);
193 }
194
195 /* unregister kbd-int callbacks and context */
196 void
197 auth2_challenge_stop(Authctxt *authctxt)
198 {
199         /* unregister callback */
200         dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
201         if (authctxt->kbdintctxt != NULL)  {
202                 kbdint_free(authctxt->kbdintctxt);
203                 authctxt->kbdintctxt = NULL;
204         }
205 }
206
207 /* side effect: sets authctxt->postponed if a reply was sent*/
208 static int
209 auth2_challenge_start(Authctxt *authctxt)
210 {
211         KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
212
213         debug2("auth2_challenge_start: devices %s",
214             kbdintctxt->devices ?  kbdintctxt->devices : "<empty>");
215
216         if (kbdint_next_device(kbdintctxt) == 0) {
217                 auth2_challenge_stop(authctxt);
218                 return 0;
219         }
220         debug("auth2_challenge_start: trying authentication method '%s'",
221             kbdintctxt->device->name);
222
223         if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
224                 auth2_challenge_stop(authctxt);
225                 return 0;
226         }
227         if (send_userauth_info_request(authctxt) == 0) {
228                 auth2_challenge_stop(authctxt);
229                 return 0;
230         }
231         dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
232             &input_userauth_info_response);
233
234         authctxt->postponed = 1;
235         return 0;
236 }
237
238 static int
239 send_userauth_info_request(Authctxt *authctxt)
240 {
241         KbdintAuthctxt *kbdintctxt;
242         char *name, *instr, **prompts;
243         u_int i, *echo_on;
244
245         kbdintctxt = authctxt->kbdintctxt;
246         if (kbdintctxt->device->query(kbdintctxt->ctxt,
247             &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
248                 return 0;
249
250         packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
251         packet_put_cstring(name);
252         packet_put_cstring(instr);
253         packet_put_cstring("");         /* language not used */
254         packet_put_int(kbdintctxt->nreq);
255         for (i = 0; i < kbdintctxt->nreq; i++) {
256                 packet_put_cstring(prompts[i]);
257                 packet_put_char(echo_on[i]);
258         }
259         packet_send();
260         packet_write_wait();
261
262         for (i = 0; i < kbdintctxt->nreq; i++)
263                 xfree(prompts[i]);
264         xfree(prompts);
265         xfree(echo_on);
266         xfree(name);
267         xfree(instr);
268         return 1;
269 }
270
271 static void
272 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
273 {
274         Authctxt *authctxt = ctxt;
275         KbdintAuthctxt *kbdintctxt;
276         int authenticated = 0, res, len;
277         u_int i, nresp;
278         char **response = NULL, *method;
279
280         if (authctxt == NULL)
281                 fatal("input_userauth_info_response: no authctxt");
282         kbdintctxt = authctxt->kbdintctxt;
283         if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
284                 fatal("input_userauth_info_response: no kbdintctxt");
285         if (kbdintctxt->device == NULL)
286                 fatal("input_userauth_info_response: no device");
287
288         authctxt->postponed = 0;        /* reset */
289         nresp = packet_get_int();
290         if (nresp != kbdintctxt->nreq)
291                 fatal("input_userauth_info_response: wrong number of replies");
292         if (nresp > 100)
293                 fatal("input_userauth_info_response: too many replies");
294         if (nresp > 0) {
295                 response = xmalloc(nresp * sizeof(char *));
296                 for (i = 0; i < nresp; i++)
297                         response[i] = packet_get_string(NULL);
298         }
299         packet_check_eom();
300
301         res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
302
303         for (i = 0; i < nresp; i++) {
304                 memset(response[i], 'r', strlen(response[i]));
305                 xfree(response[i]);
306         }
307         if (response)
308                 xfree(response);
309
310         switch (res) {
311         case 0:
312                 /* Success! */
313                 authenticated = authctxt->valid ? 1 : 0;
314                 break;
315         case 1:
316                 /* Authentication needs further interaction */
317                 if (send_userauth_info_request(authctxt) == 1)
318                         authctxt->postponed = 1;
319                 break;
320         default:
321                 /* Failure! */
322                 break;
323         }
324
325         len = strlen("keyboard-interactive") + 2 +
326                 strlen(kbdintctxt->device->name);
327         method = xmalloc(len);
328         snprintf(method, len, "keyboard-interactive/%s",
329             kbdintctxt->device->name);
330
331         if (!authctxt->postponed) {
332                 if (authenticated) {
333                         auth2_challenge_stop(authctxt);
334                 } else {
335                         /* start next device */
336                         /* may set authctxt->postponed */
337                         auth2_challenge_start(authctxt);
338                 }
339         }
340         userauth_finish(authctxt, authenticated, method);
341         xfree(method);
342 }
343
344 void
345 privsep_challenge_enable(void)
346 {
347 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
348         int n = 0;
349 #endif
350 #ifdef BSD_AUTH
351         extern KbdintDevice mm_bsdauth_device;
352 #endif
353 #ifdef USE_PAM
354         extern KbdintDevice mm_sshpam_device;
355 #endif
356 #ifdef SKEY
357         extern KbdintDevice mm_skey_device;
358 #endif
359
360 #ifdef BSD_AUTH
361         devices[n++] = &mm_bsdauth_device;
362 #else
363 #ifdef USE_PAM
364         devices[n++] = &mm_sshpam_device;
365 #endif
366 #ifdef SKEY
367         devices[n++] = &mm_skey_device;
368 #endif
369 #endif
370 }