2 * Copyright (c) 2001 Markus Friedl. All rights reserved.
3 * Copyright (c) 2001 Per Allansson. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2-chall.c,v 1.16 2002/01/13 17:57:37 markus Exp $");
37 static int auth2_challenge_start(Authctxt *);
38 static int send_userauth_info_request(Authctxt *);
39 static void input_userauth_info_response(int, u_int32_t, void *);
42 extern KbdintDevice bsdauth_device;
45 extern KbdintDevice skey_device;
49 KbdintDevice *devices[] = {
60 typedef struct KbdintAuthctxt KbdintAuthctxt;
68 static KbdintAuthctxt *
69 kbdint_alloc(const char *devs)
71 KbdintAuthctxt *kbdintctxt;
75 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
76 if (strcmp(devs, "") == 0) {
78 for (i = 0; devices[i]; i++) {
79 if (buffer_len(&b) > 0)
80 buffer_append(&b, ",", 1);
81 buffer_append(&b, devices[i]->name,
82 strlen(devices[i]->name));
84 buffer_append(&b, "\0", 1);
85 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
88 kbdintctxt->devices = xstrdup(devs);
90 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
91 kbdintctxt->ctxt = NULL;
92 kbdintctxt->device = NULL;
97 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
99 if (kbdintctxt->ctxt) {
100 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
101 kbdintctxt->ctxt = NULL;
103 kbdintctxt->device = NULL;
106 kbdint_free(KbdintAuthctxt *kbdintctxt)
108 if (kbdintctxt->device)
109 kbdint_reset_device(kbdintctxt);
110 if (kbdintctxt->devices) {
111 xfree(kbdintctxt->devices);
112 kbdintctxt->devices = NULL;
116 /* get next device */
118 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
124 if (kbdintctxt->device)
125 kbdint_reset_device(kbdintctxt);
127 len = kbdintctxt->devices ?
128 strcspn(kbdintctxt->devices, ",") : 0;
132 for (i = 0; devices[i]; i++)
133 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
134 kbdintctxt->device = devices[i];
135 t = kbdintctxt->devices;
136 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
138 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
139 kbdintctxt->devices : "<empty>");
140 } while (kbdintctxt->devices && !kbdintctxt->device);
142 return kbdintctxt->device ? 1 : 0;
146 * try challenge-response, set authctxt->postponed if we have to
147 * wait for the response.
150 auth2_challenge(Authctxt *authctxt, char *devs)
152 debug("auth2_challenge: user=%s devs=%s",
153 authctxt->user ? authctxt->user : "<nouser>",
154 devs ? devs : "<no devs>");
156 if (authctxt->user == NULL || !devs)
158 if (authctxt->kbdintctxt == NULL)
159 authctxt->kbdintctxt = kbdint_alloc(devs);
160 return auth2_challenge_start(authctxt);
163 /* unregister kbd-int callbacks and context */
165 auth2_challenge_stop(Authctxt *authctxt)
167 /* unregister callback */
168 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
169 if (authctxt->kbdintctxt != NULL) {
170 kbdint_free(authctxt->kbdintctxt);
171 authctxt->kbdintctxt = NULL;
175 /* side effect: sets authctxt->postponed if a reply was sent*/
177 auth2_challenge_start(Authctxt *authctxt)
179 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
181 debug2("auth2_challenge_start: devices %s",
182 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
184 if (kbdint_next_device(kbdintctxt) == 0) {
185 auth2_challenge_stop(authctxt);
188 debug("auth2_challenge_start: trying authentication method '%s'",
189 kbdintctxt->device->name);
191 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
192 auth2_challenge_stop(authctxt);
195 if (send_userauth_info_request(authctxt) == 0) {
196 auth2_challenge_stop(authctxt);
199 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
200 &input_userauth_info_response);
202 authctxt->postponed = 1;
207 send_userauth_info_request(Authctxt *authctxt)
209 KbdintAuthctxt *kbdintctxt;
210 char *name, *instr, **prompts;
212 u_int numprompts, *echo_on;
214 kbdintctxt = authctxt->kbdintctxt;
215 if (kbdintctxt->device->query(kbdintctxt->ctxt,
216 &name, &instr, &numprompts, &prompts, &echo_on))
219 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
220 packet_put_cstring(name);
221 packet_put_cstring(instr);
222 packet_put_cstring(""); /* language not used */
223 packet_put_int(numprompts);
224 for (i = 0; i < numprompts; i++) {
225 packet_put_cstring(prompts[i]);
226 packet_put_char(echo_on[i]);
231 for (i = 0; i < numprompts; i++)
241 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
243 Authctxt *authctxt = ctxt;
244 KbdintAuthctxt *kbdintctxt;
245 int i, authenticated = 0, res, len;
247 char **response = NULL, *method;
249 if (authctxt == NULL)
250 fatal("input_userauth_info_response: no authctxt");
251 kbdintctxt = authctxt->kbdintctxt;
252 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
253 fatal("input_userauth_info_response: no kbdintctxt");
254 if (kbdintctxt->device == NULL)
255 fatal("input_userauth_info_response: no device");
257 authctxt->postponed = 0; /* reset */
258 nresp = packet_get_int();
260 response = xmalloc(nresp * sizeof(char*));
261 for (i = 0; i < nresp; i++)
262 response[i] = packet_get_string(NULL);
266 if (authctxt->valid) {
267 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
273 for (i = 0; i < nresp; i++) {
274 memset(response[i], 'r', strlen(response[i]));
286 /* Authentication needs further interaction */
287 if (send_userauth_info_request(authctxt) == 1)
288 authctxt->postponed = 1;
295 len = strlen("keyboard-interactive") + 2 +
296 strlen(kbdintctxt->device->name);
297 method = xmalloc(len);
298 snprintf(method, len, "keyboard-interactive/%s",
299 kbdintctxt->device->name);
301 if (!authctxt->postponed) {
303 auth2_challenge_stop(authctxt);
305 /* start next device */
306 /* may set authctxt->postponed */
307 auth2_challenge_start(authctxt);
310 userauth_finish(authctxt, authenticated, method);