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 $");
38 extern ServerOptions options;
40 static int auth2_challenge_start(Authctxt *);
41 static int send_userauth_info_request(Authctxt *);
42 static void input_userauth_info_response(int, u_int32_t, void *);
45 extern KbdintDevice bsdauth_device;
48 extern KbdintDevice sshpam_device;
51 extern KbdintDevice skey_device;
55 KbdintDevice *devices[] = {
69 typedef struct KbdintAuthctxt KbdintAuthctxt;
80 remove_kbdint_device(const char *devname)
84 for (i = 0; devices[i] != NULL; i++)
85 if (strcmp(devices[i]->name, devname) == 0) {
86 for (j = i; devices[j] != NULL; j++)
87 devices[j] = devices[j+1];
93 static KbdintAuthctxt *
94 kbdint_alloc(const char *devs)
96 KbdintAuthctxt *kbdintctxt;
101 if (!options.use_pam)
102 remove_kbdint_device("pam");
105 kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
106 if (strcmp(devs, "") == 0) {
108 for (i = 0; devices[i]; i++) {
109 if (buffer_len(&b) > 0)
110 buffer_append(&b, ",", 1);
111 buffer_append(&b, devices[i]->name,
112 strlen(devices[i]->name));
114 buffer_append(&b, "\0", 1);
115 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
118 kbdintctxt->devices = xstrdup(devs);
120 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
121 kbdintctxt->ctxt = NULL;
122 kbdintctxt->device = NULL;
123 kbdintctxt->nreq = 0;
128 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
130 if (kbdintctxt->ctxt) {
131 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
132 kbdintctxt->ctxt = NULL;
134 kbdintctxt->device = NULL;
137 kbdint_free(KbdintAuthctxt *kbdintctxt)
139 if (kbdintctxt->device)
140 kbdint_reset_device(kbdintctxt);
141 if (kbdintctxt->devices) {
142 xfree(kbdintctxt->devices);
143 kbdintctxt->devices = NULL;
147 /* get next device */
149 kbdint_next_device(KbdintAuthctxt *kbdintctxt)
155 if (kbdintctxt->device)
156 kbdint_reset_device(kbdintctxt);
158 len = kbdintctxt->devices ?
159 strcspn(kbdintctxt->devices, ",") : 0;
163 for (i = 0; devices[i]; i++)
164 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
165 kbdintctxt->device = devices[i];
166 t = kbdintctxt->devices;
167 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
169 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
170 kbdintctxt->devices : "<empty>");
171 } while (kbdintctxt->devices && !kbdintctxt->device);
173 return kbdintctxt->device ? 1 : 0;
177 * try challenge-response, set authctxt->postponed if we have to
178 * wait for the response.
181 auth2_challenge(Authctxt *authctxt, char *devs)
183 debug("auth2_challenge: user=%s devs=%s",
184 authctxt->user ? authctxt->user : "<nouser>",
185 devs ? devs : "<no devs>");
187 if (authctxt->user == NULL || !devs)
189 if (authctxt->kbdintctxt == NULL)
190 authctxt->kbdintctxt = kbdint_alloc(devs);
191 return auth2_challenge_start(authctxt);
194 /* unregister kbd-int callbacks and context */
196 auth2_challenge_stop(Authctxt *authctxt)
198 /* unregister callback */
199 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
200 if (authctxt->kbdintctxt != NULL) {
201 kbdint_free(authctxt->kbdintctxt);
202 authctxt->kbdintctxt = NULL;
206 /* side effect: sets authctxt->postponed if a reply was sent*/
208 auth2_challenge_start(Authctxt *authctxt)
210 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
212 debug2("auth2_challenge_start: devices %s",
213 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
215 if (kbdint_next_device(kbdintctxt) == 0) {
216 auth2_challenge_stop(authctxt);
219 debug("auth2_challenge_start: trying authentication method '%s'",
220 kbdintctxt->device->name);
222 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
223 auth2_challenge_stop(authctxt);
226 if (send_userauth_info_request(authctxt) == 0) {
227 auth2_challenge_stop(authctxt);
230 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
231 &input_userauth_info_response);
233 authctxt->postponed = 1;
238 send_userauth_info_request(Authctxt *authctxt)
240 KbdintAuthctxt *kbdintctxt;
241 char *name, *instr, **prompts;
244 kbdintctxt = authctxt->kbdintctxt;
245 if (kbdintctxt->device->query(kbdintctxt->ctxt,
246 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
249 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
250 packet_put_cstring(name);
251 packet_put_cstring(instr);
252 packet_put_cstring(""); /* language not used */
253 packet_put_int(kbdintctxt->nreq);
254 for (i = 0; i < kbdintctxt->nreq; i++) {
255 packet_put_cstring(prompts[i]);
256 packet_put_char(echo_on[i]);
261 for (i = 0; i < kbdintctxt->nreq; i++)
271 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
273 Authctxt *authctxt = ctxt;
274 KbdintAuthctxt *kbdintctxt;
275 int authenticated = 0, res, len;
277 char **response = NULL, *method;
279 if (authctxt == NULL)
280 fatal("input_userauth_info_response: no authctxt");
281 kbdintctxt = authctxt->kbdintctxt;
282 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
283 fatal("input_userauth_info_response: no kbdintctxt");
284 if (kbdintctxt->device == NULL)
285 fatal("input_userauth_info_response: no device");
287 authctxt->postponed = 0; /* reset */
288 nresp = packet_get_int();
289 if (nresp != kbdintctxt->nreq)
290 fatal("input_userauth_info_response: wrong number of replies");
292 fatal("input_userauth_info_response: too many replies");
294 response = xmalloc(nresp * sizeof(char *));
295 for (i = 0; i < nresp; i++)
296 response[i] = packet_get_string(NULL);
300 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
302 for (i = 0; i < nresp; i++) {
303 memset(response[i], 'r', strlen(response[i]));
312 authenticated = authctxt->valid ? 1 : 0;
315 /* Authentication needs further interaction */
316 if (send_userauth_info_request(authctxt) == 1)
317 authctxt->postponed = 1;
324 len = strlen("keyboard-interactive") + 2 +
325 strlen(kbdintctxt->device->name);
326 method = xmalloc(len);
327 snprintf(method, len, "keyboard-interactive/%s",
328 kbdintctxt->device->name);
330 if (!authctxt->postponed) {
332 auth2_challenge_stop(authctxt);
334 /* start next device */
335 /* may set authctxt->postponed */
336 auth2_challenge_start(authctxt);
339 userauth_finish(authctxt, authenticated, method);
344 privsep_challenge_enable(void)
346 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
350 extern KbdintDevice mm_bsdauth_device;
353 extern KbdintDevice mm_sshpam_device;
356 extern KbdintDevice mm_skey_device;
360 devices[n++] = &mm_bsdauth_device;
363 devices[n++] = &mm_sshpam_device;
366 devices[n++] = &mm_skey_device;