1 /* $OpenBSD: auth2-chall.c,v 1.41 2014/02/02 03:44:31 djm Exp $ */
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Per Allansson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
47 extern ServerOptions options;
49 static int auth2_challenge_start(Authctxt *);
50 static int send_userauth_info_request(Authctxt *);
51 static void input_userauth_info_response(int, u_int32_t, void *);
54 extern KbdintDevice bsdauth_device;
57 extern KbdintDevice sshpam_device;
60 extern KbdintDevice skey_device;
64 KbdintDevice *devices[] = {
78 typedef struct KbdintAuthctxt KbdintAuthctxt;
89 remove_kbdint_device(const char *devname)
93 for (i = 0; devices[i] != NULL; i++)
94 if (strcmp(devices[i]->name, devname) == 0) {
95 for (j = i; devices[j] != NULL; j++)
96 devices[j] = devices[j+1];
102 static KbdintAuthctxt *
103 kbdint_alloc(const char *devs)
105 KbdintAuthctxt *kbdintctxt;
110 if (!options.use_pam)
111 remove_kbdint_device("pam");
114 kbdintctxt = xcalloc(1, sizeof(KbdintAuthctxt));
115 if (strcmp(devs, "") == 0) {
117 for (i = 0; devices[i]; i++) {
118 if (buffer_len(&b) > 0)
119 buffer_append(&b, ",", 1);
120 buffer_append(&b, devices[i]->name,
121 strlen(devices[i]->name));
123 buffer_append(&b, "\0", 1);
124 kbdintctxt->devices = xstrdup(buffer_ptr(&b));
127 kbdintctxt->devices = xstrdup(devs);
129 debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
130 kbdintctxt->ctxt = NULL;
131 kbdintctxt->device = NULL;
132 kbdintctxt->nreq = 0;
137 kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
139 if (kbdintctxt->ctxt) {
140 kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
141 kbdintctxt->ctxt = NULL;
143 kbdintctxt->device = NULL;
146 kbdint_free(KbdintAuthctxt *kbdintctxt)
148 if (kbdintctxt->device)
149 kbdint_reset_device(kbdintctxt);
150 free(kbdintctxt->devices);
151 explicit_bzero(kbdintctxt, sizeof(*kbdintctxt));
154 /* get next device */
156 kbdint_next_device(Authctxt *authctxt, KbdintAuthctxt *kbdintctxt)
162 if (kbdintctxt->device)
163 kbdint_reset_device(kbdintctxt);
165 len = kbdintctxt->devices ?
166 strcspn(kbdintctxt->devices, ",") : 0;
170 for (i = 0; devices[i]; i++) {
171 if (!auth2_method_allowed(authctxt,
172 "keyboard-interactive", devices[i]->name))
174 if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
175 kbdintctxt->device = devices[i];
177 t = kbdintctxt->devices;
178 kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
180 debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
181 kbdintctxt->devices : "<empty>");
182 } while (kbdintctxt->devices && !kbdintctxt->device);
184 return kbdintctxt->device ? 1 : 0;
188 * try challenge-response, set authctxt->postponed if we have to
189 * wait for the response.
192 auth2_challenge(Authctxt *authctxt, char *devs)
194 debug("auth2_challenge: user=%s devs=%s",
195 authctxt->user ? authctxt->user : "<nouser>",
196 devs ? devs : "<no devs>");
198 if (authctxt->user == NULL || !devs)
200 if (authctxt->kbdintctxt == NULL)
201 authctxt->kbdintctxt = kbdint_alloc(devs);
202 return auth2_challenge_start(authctxt);
205 /* unregister kbd-int callbacks and context */
207 auth2_challenge_stop(Authctxt *authctxt)
209 /* unregister callback */
210 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
211 if (authctxt->kbdintctxt != NULL) {
212 kbdint_free(authctxt->kbdintctxt);
213 authctxt->kbdintctxt = NULL;
217 /* side effect: sets authctxt->postponed if a reply was sent*/
219 auth2_challenge_start(Authctxt *authctxt)
221 KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
223 debug2("auth2_challenge_start: devices %s",
224 kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
226 if (kbdint_next_device(authctxt, kbdintctxt) == 0) {
227 auth2_challenge_stop(authctxt);
230 debug("auth2_challenge_start: trying authentication method '%s'",
231 kbdintctxt->device->name);
233 if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
234 auth2_challenge_stop(authctxt);
237 if (send_userauth_info_request(authctxt) == 0) {
238 auth2_challenge_stop(authctxt);
241 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
242 &input_userauth_info_response);
244 authctxt->postponed = 1;
249 send_userauth_info_request(Authctxt *authctxt)
251 KbdintAuthctxt *kbdintctxt;
252 char *name, *instr, **prompts;
255 kbdintctxt = authctxt->kbdintctxt;
256 if (kbdintctxt->device->query(kbdintctxt->ctxt,
257 &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
260 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
261 packet_put_cstring(name);
262 packet_put_cstring(instr);
263 packet_put_cstring(""); /* language not used */
264 packet_put_int(kbdintctxt->nreq);
265 for (i = 0; i < kbdintctxt->nreq; i++) {
266 packet_put_cstring(prompts[i]);
267 packet_put_char(echo_on[i]);
272 for (i = 0; i < kbdintctxt->nreq; i++)
282 input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
284 Authctxt *authctxt = ctxt;
285 KbdintAuthctxt *kbdintctxt;
286 int authenticated = 0, res;
288 const char *devicename = NULL;
289 char **response = NULL;
291 if (authctxt == NULL)
292 fatal("input_userauth_info_response: no authctxt");
293 kbdintctxt = authctxt->kbdintctxt;
294 if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
295 fatal("input_userauth_info_response: no kbdintctxt");
296 if (kbdintctxt->device == NULL)
297 fatal("input_userauth_info_response: no device");
299 authctxt->postponed = 0; /* reset */
300 nresp = packet_get_int();
301 if (nresp != kbdintctxt->nreq)
302 fatal("input_userauth_info_response: wrong number of replies");
304 fatal("input_userauth_info_response: too many replies");
306 response = xcalloc(nresp, sizeof(char *));
307 for (i = 0; i < nresp; i++)
308 response[i] = packet_get_string(NULL);
312 res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
314 for (i = 0; i < nresp; i++) {
315 explicit_bzero(response[i], strlen(response[i]));
323 authenticated = authctxt->valid ? 1 : 0;
326 /* Authentication needs further interaction */
327 if (send_userauth_info_request(authctxt) == 1)
328 authctxt->postponed = 1;
334 devicename = kbdintctxt->device->name;
335 if (!authctxt->postponed) {
337 auth2_challenge_stop(authctxt);
339 /* start next device */
340 /* may set authctxt->postponed */
341 auth2_challenge_start(authctxt);
344 userauth_finish(authctxt, authenticated, "keyboard-interactive",
349 privsep_challenge_enable(void)
351 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
355 extern KbdintDevice mm_bsdauth_device;
358 extern KbdintDevice mm_sshpam_device;
361 extern KbdintDevice mm_skey_device;
365 devices[n++] = &mm_bsdauth_device;
368 devices[n++] = &mm_sshpam_device;
371 devices[n++] = &mm_skey_device;