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.24 2005/07/17 07:17:54 djm Exp $");
39 extern ServerOptions options;
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 *);
46 extern KbdintDevice bsdauth_device;
49 extern KbdintDevice sshpam_device;
52 extern KbdintDevice skey_device;
56 KbdintDevice *devices[] = {
70 typedef struct KbdintAuthctxt KbdintAuthctxt;
81 remove_kbdint_device(const char *devname)
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];
94 static KbdintAuthctxt *
95 kbdint_alloc(const char *devs)
97 KbdintAuthctxt *kbdintctxt;
102 if (!options.use_pam)
103 remove_kbdint_device("pam");
106 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
107 if (strcmp(devs, "") == 0) {
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));
115 buffer_append(&b, "\0", 1);
116 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
119 kbdintctxt->devices = xstrdup(devs);
121 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
122 kbdintctxt->ctxt = NULL;
123 kbdintctxt->device = NULL;
124 kbdintctxt->nreq = 0;
129 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
131 if (kbdintctxt->ctxt) {
132 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
133 kbdintctxt->ctxt = NULL;
135 kbdintctxt->device = NULL;
138 kbdint_free(KbdintAuthctxt *kbdintctxt)
140 if (kbdintctxt->device)
141 kbdint_reset_device(kbdintctxt);
142 if (kbdintctxt->devices) {
143 xfree(kbdintctxt->devices);
144 kbdintctxt->devices = NULL;
148 /* get next device */
150 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
156 if (kbdintctxt->device)
157 kbdint_reset_device(kbdintctxt);
159 len = kbdintctxt->devices ?
160 strcspn(kbdintctxt->devices, ",") : 0;
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;
170 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
171 kbdintctxt->devices : "<empty>");
172 } while (kbdintctxt->devices && !kbdintctxt->device);
174 return kbdintctxt->device ? 1 : 0;
178 * try challenge-response, set authctxt->postponed if we have to
179 * wait for the response.
182 auth2_challenge(Authctxt *authctxt, char *devs)
184 debug("auth2_challenge: user=%s devs=%s",
185 authctxt->user ? authctxt->user : "<nouser>",
186 devs ? devs : "<no devs>");
188 if (authctxt->user == NULL || !devs)
190 if (authctxt->kbdintctxt == NULL)
191 authctxt->kbdintctxt = kbdint_alloc(devs);
192 return auth2_challenge_start(authctxt);
195 /* unregister kbd-int callbacks and context */
197 auth2_challenge_stop(Authctxt *authctxt)
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;
207 /* side effect: sets authctxt->postponed if a reply was sent*/
209 auth2_challenge_start(Authctxt *authctxt)
211 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
213 debug2("auth2_challenge_start: devices %s",
214 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
216 if (kbdint_next_device(kbdintctxt) == 0) {
217 auth2_challenge_stop(authctxt);
220 debug("auth2_challenge_start: trying authentication method '%s'",
221 kbdintctxt->device->name);
223 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
224 auth2_challenge_stop(authctxt);
227 if (send_userauth_info_request(authctxt) == 0) {
228 auth2_challenge_stop(authctxt);
231 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
232 &input_userauth_info_response);
234 authctxt->postponed = 1;
239 send_userauth_info_request(Authctxt *authctxt)
241 KbdintAuthctxt *kbdintctxt;
242 char *name, *instr, **prompts;
245 kbdintctxt = authctxt->kbdintctxt;
246 if (kbdintctxt->device->query(kbdintctxt->ctxt,
247 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
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]);
262 for (i = 0; i < kbdintctxt->nreq; i++)
272 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
274 Authctxt *authctxt = ctxt;
275 KbdintAuthctxt *kbdintctxt;
276 int authenticated = 0, res, len;
278 char **response = NULL, *method;
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");
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");
293 fatal("input_userauth_info_response: too many replies");
295 response = xmalloc(nresp * sizeof(char *));
296 for (i = 0; i < nresp; i++)
297 response[i] = packet_get_string(NULL);
301 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
303 for (i = 0; i < nresp; i++) {
304 memset(response[i], 'r', strlen(response[i]));
313 authenticated = authctxt->valid ? 1 : 0;
316 /* Authentication needs further interaction */
317 if (send_userauth_info_request(authctxt) == 1)
318 authctxt->postponed = 1;
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);
331 if (!authctxt->postponed) {
333 auth2_challenge_stop(authctxt);
335 /* start next device */
336 /* may set authctxt->postponed */
337 auth2_challenge_start(authctxt);
340 userauth_finish(authctxt, authenticated, method);
345 privsep_challenge_enable(void)
347 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
351 extern KbdintDevice mm_bsdauth_device;
354 extern KbdintDevice mm_sshpam_device;
357 extern KbdintDevice mm_skey_device;
361 devices[n++] = &mm_bsdauth_device;
364 devices[n++] = &mm_sshpam_device;
367 devices[n++] = &mm_skey_device;