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.15 2002/01/11 23:02:51 markus Exp $");
36 static int auth2_challenge_start(Authctxt *);
37 static int send_userauth_info_request(Authctxt *);
38 static void input_userauth_info_response(int, u_int32_t, void *);
41 extern KbdintDevice bsdauth_device;
44 extern KbdintDevice skey_device;
48 KbdintDevice *devices[] = {
59 typedef struct KbdintAuthctxt KbdintAuthctxt;
67 static KbdintAuthctxt *
68 kbdint_alloc(const char *devs)
70 KbdintAuthctxt *kbdintctxt;
74 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
75 if (strcmp(devs, "") == 0) {
77 for (i = 0; devices[i]; i++) {
79 strlcat(buf, ",", sizeof(buf));
80 strlcat(buf, devices[i]->name, sizeof(buf));
82 debug("kbdint_alloc: devices '%s'", buf);
83 kbdintctxt->devices = xstrdup(buf);
85 kbdintctxt->devices = xstrdup(devs);
87 kbdintctxt->ctxt = NULL;
88 kbdintctxt->device = NULL;
93 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
95 if (kbdintctxt->ctxt) {
96 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
97 kbdintctxt->ctxt = NULL;
99 kbdintctxt->device = NULL;
102 kbdint_free(KbdintAuthctxt *kbdintctxt)
104 if (kbdintctxt->device)
105 kbdint_reset_device(kbdintctxt);
106 if (kbdintctxt->devices) {
107 xfree(kbdintctxt->devices);
108 kbdintctxt->devices = NULL;
112 /* get next device */
114 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
120 if (kbdintctxt->device)
121 kbdint_reset_device(kbdintctxt);
123 len = kbdintctxt->devices ?
124 strcspn(kbdintctxt->devices, ",") : 0;
128 for (i = 0; devices[i]; i++)
129 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
130 kbdintctxt->device = devices[i];
131 t = kbdintctxt->devices;
132 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
134 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
135 kbdintctxt->devices : "<empty>");
136 } while (kbdintctxt->devices && !kbdintctxt->device);
138 return kbdintctxt->device ? 1 : 0;
142 * try challenge-response, set authctxt->postponed if we have to
143 * wait for the response.
146 auth2_challenge(Authctxt *authctxt, char *devs)
148 debug("auth2_challenge: user=%s devs=%s",
149 authctxt->user ? authctxt->user : "<nouser>",
150 devs ? devs : "<no devs>");
152 if (authctxt->user == NULL || !devs)
154 if (authctxt->kbdintctxt == NULL)
155 authctxt->kbdintctxt = kbdint_alloc(devs);
156 return auth2_challenge_start(authctxt);
159 /* unregister kbd-int callbacks and context */
161 auth2_challenge_stop(Authctxt *authctxt)
163 /* unregister callback */
164 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
165 if (authctxt->kbdintctxt != NULL) {
166 kbdint_free(authctxt->kbdintctxt);
167 authctxt->kbdintctxt = NULL;
171 /* side effect: sets authctxt->postponed if a reply was sent*/
173 auth2_challenge_start(Authctxt *authctxt)
175 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
177 debug2("auth2_challenge_start: devices %s",
178 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
180 if (kbdint_next_device(kbdintctxt) == 0) {
181 auth2_challenge_stop(authctxt);
184 debug("auth2_challenge_start: trying authentication method '%s'",
185 kbdintctxt->device->name);
187 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
188 auth2_challenge_stop(authctxt);
191 if (send_userauth_info_request(authctxt) == 0) {
192 auth2_challenge_stop(authctxt);
195 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
196 &input_userauth_info_response);
198 authctxt->postponed = 1;
203 send_userauth_info_request(Authctxt *authctxt)
205 KbdintAuthctxt *kbdintctxt;
206 char *name, *instr, **prompts;
208 u_int numprompts, *echo_on;
210 kbdintctxt = authctxt->kbdintctxt;
211 if (kbdintctxt->device->query(kbdintctxt->ctxt,
212 &name, &instr, &numprompts, &prompts, &echo_on))
215 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
216 packet_put_cstring(name);
217 packet_put_cstring(instr);
218 packet_put_cstring(""); /* language not used */
219 packet_put_int(numprompts);
220 for (i = 0; i < numprompts; i++) {
221 packet_put_cstring(prompts[i]);
222 packet_put_char(echo_on[i]);
227 for (i = 0; i < numprompts; i++)
237 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
239 Authctxt *authctxt = ctxt;
240 KbdintAuthctxt *kbdintctxt;
241 int i, authenticated = 0, res, len;
243 char **response = NULL, *method;
245 if (authctxt == NULL)
246 fatal("input_userauth_info_response: no authctxt");
247 kbdintctxt = authctxt->kbdintctxt;
248 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
249 fatal("input_userauth_info_response: no kbdintctxt");
250 if (kbdintctxt->device == NULL)
251 fatal("input_userauth_info_response: no device");
253 authctxt->postponed = 0; /* reset */
254 nresp = packet_get_int();
256 response = xmalloc(nresp * sizeof(char*));
257 for (i = 0; i < nresp; i++)
258 response[i] = packet_get_string(NULL);
262 if (authctxt->valid) {
263 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
269 for (i = 0; i < nresp; i++) {
270 memset(response[i], 'r', strlen(response[i]));
282 /* Authentication needs further interaction */
283 if (send_userauth_info_request(authctxt) == 1)
284 authctxt->postponed = 1;
291 len = strlen("keyboard-interactive") + 2 +
292 strlen(kbdintctxt->device->name);
293 method = xmalloc(len);
294 snprintf(method, len, "keyboard-interactive/%s",
295 kbdintctxt->device->name);
297 if (!authctxt->postponed) {
299 auth2_challenge_stop(authctxt);
301 /* start next device */
302 /* may set authctxt->postponed */
303 auth2_challenge_start(authctxt);
306 userauth_finish(authctxt, authenticated, method);