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