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.20 2002/06/30 21:59:45 deraadt 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;
69 static KbdintAuthctxt *
70 kbdint_alloc(const char *devs)
72 KbdintAuthctxt *kbdintctxt;
76 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
77 if (strcmp(devs, "") == 0) {
79 for (i = 0; devices[i]; i++) {
80 if (buffer_len(&b) > 0)
81 buffer_append(&b, ",", 1);
82 buffer_append(&b, devices[i]->name,
83 strlen(devices[i]->name));
85 buffer_append(&b, "\0", 1);
86 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
89 kbdintctxt->devices = xstrdup(devs);
91 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
92 kbdintctxt->ctxt = NULL;
93 kbdintctxt->device = NULL;
99 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
101 if (kbdintctxt->ctxt) {
102 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
103 kbdintctxt->ctxt = NULL;
105 kbdintctxt->device = NULL;
108 kbdint_free(KbdintAuthctxt *kbdintctxt)
110 if (kbdintctxt->device)
111 kbdint_reset_device(kbdintctxt);
112 if (kbdintctxt->devices) {
113 xfree(kbdintctxt->devices);
114 kbdintctxt->devices = NULL;
118 /* get next device */
120 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
126 if (kbdintctxt->device)
127 kbdint_reset_device(kbdintctxt);
129 len = kbdintctxt->devices ?
130 strcspn(kbdintctxt->devices, ",") : 0;
134 for (i = 0; devices[i]; i++)
135 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
136 kbdintctxt->device = devices[i];
137 t = kbdintctxt->devices;
138 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
140 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
141 kbdintctxt->devices : "<empty>");
142 } while (kbdintctxt->devices && !kbdintctxt->device);
144 return kbdintctxt->device ? 1 : 0;
148 * try challenge-response, set authctxt->postponed if we have to
149 * wait for the response.
152 auth2_challenge(Authctxt *authctxt, char *devs)
154 debug("auth2_challenge: user=%s devs=%s",
155 authctxt->user ? authctxt->user : "<nouser>",
156 devs ? devs : "<no devs>");
158 if (authctxt->user == NULL || !devs)
160 if (authctxt->kbdintctxt == NULL)
161 authctxt->kbdintctxt = kbdint_alloc(devs);
162 return auth2_challenge_start(authctxt);
165 /* unregister kbd-int callbacks and context */
167 auth2_challenge_stop(Authctxt *authctxt)
169 /* unregister callback */
170 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
171 if (authctxt->kbdintctxt != NULL) {
172 kbdint_free(authctxt->kbdintctxt);
173 authctxt->kbdintctxt = NULL;
177 /* side effect: sets authctxt->postponed if a reply was sent*/
179 auth2_challenge_start(Authctxt *authctxt)
181 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
183 debug2("auth2_challenge_start: devices %s",
184 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
186 if (kbdint_next_device(kbdintctxt) == 0) {
187 auth2_challenge_stop(authctxt);
190 debug("auth2_challenge_start: trying authentication method '%s'",
191 kbdintctxt->device->name);
193 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
194 auth2_challenge_stop(authctxt);
197 if (send_userauth_info_request(authctxt) == 0) {
198 auth2_challenge_stop(authctxt);
201 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
202 &input_userauth_info_response);
204 authctxt->postponed = 1;
209 send_userauth_info_request(Authctxt *authctxt)
211 KbdintAuthctxt *kbdintctxt;
212 char *name, *instr, **prompts;
216 kbdintctxt = authctxt->kbdintctxt;
217 if (kbdintctxt->device->query(kbdintctxt->ctxt,
218 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
221 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
222 packet_put_cstring(name);
223 packet_put_cstring(instr);
224 packet_put_cstring(""); /* language not used */
225 packet_put_int(kbdintctxt->nreq);
226 for (i = 0; i < kbdintctxt->nreq; i++) {
227 packet_put_cstring(prompts[i]);
228 packet_put_char(echo_on[i]);
233 for (i = 0; i < kbdintctxt->nreq; i++)
243 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
245 Authctxt *authctxt = ctxt;
246 KbdintAuthctxt *kbdintctxt;
247 int i, authenticated = 0, res, len;
249 char **response = NULL, *method;
251 if (authctxt == NULL)
252 fatal("input_userauth_info_response: no authctxt");
253 kbdintctxt = authctxt->kbdintctxt;
254 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
255 fatal("input_userauth_info_response: no kbdintctxt");
256 if (kbdintctxt->device == NULL)
257 fatal("input_userauth_info_response: no device");
259 authctxt->postponed = 0; /* reset */
260 nresp = packet_get_int();
261 if (nresp != kbdintctxt->nreq)
262 fatal("input_userauth_info_response: wrong number of replies");
264 fatal("input_userauth_info_response: too many replies");
266 response = xmalloc(nresp * sizeof(char *));
267 for (i = 0; i < nresp; i++)
268 response[i] = packet_get_string(NULL);
272 if (authctxt->valid) {
273 res = kbdintctxt->device->respond(kbdintctxt->ctxt,
279 for (i = 0; i < nresp; i++) {
280 memset(response[i], 'r', strlen(response[i]));
292 /* Authentication needs further interaction */
293 if (send_userauth_info_request(authctxt) == 1)
294 authctxt->postponed = 1;
301 len = strlen("keyboard-interactive") + 2 +
302 strlen(kbdintctxt->device->name);
303 method = xmalloc(len);
304 snprintf(method, len, "keyboard-interactive/%s",
305 kbdintctxt->device->name);
307 if (!authctxt->postponed) {
309 auth2_challenge_stop(authctxt);
311 /* start next device */
312 /* may set authctxt->postponed */
313 auth2_challenge_start(authctxt);
316 userauth_finish(authctxt, authenticated, method);
321 privsep_challenge_enable(void)
324 extern KbdintDevice mm_bsdauth_device;
327 extern KbdintDevice mm_skey_device;
329 /* As long as SSHv1 has devices[0] hard coded this is fine */
331 devices[0] = &mm_bsdauth_device;
334 devices[0] = &mm_skey_device;