1 /* $OpenBSD: auth2-chall.c,v 1.29 2006/08/01 23:22:47 stevesk 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.
41 extern ServerOptions options
;
43 static int auth2_challenge_start(Authctxt
*);
44 static int send_userauth_info_request(Authctxt
*);
45 static void input_userauth_info_response(int, u_int32_t
, void *);
48 extern KbdintDevice bsdauth_device
;
51 extern KbdintDevice sshpam_device
;
54 extern KbdintDevice skey_device
;
58 KbdintDevice
*devices
[] = {
72 typedef struct KbdintAuthctxt KbdintAuthctxt
;
83 remove_kbdint_device(const char *devname
)
87 for (i
= 0; devices
[i
] != NULL
; i
++)
88 if (strcmp(devices
[i
]->name
, devname
) == 0) {
89 for (j
= i
; devices
[j
] != NULL
; j
++)
90 devices
[j
] = devices
[j
+1];
96 static KbdintAuthctxt
*
97 kbdint_alloc(const char *devs
)
99 KbdintAuthctxt
*kbdintctxt
;
104 if (!options
.use_pam
)
105 remove_kbdint_device("pam");
108 kbdintctxt
= xmalloc(sizeof(KbdintAuthctxt
));
109 if (strcmp(devs
, "") == 0) {
111 for (i
= 0; devices
[i
]; i
++) {
112 if (buffer_len(&b
) > 0)
113 buffer_append(&b
, ",", 1);
114 buffer_append(&b
, devices
[i
]->name
,
115 strlen(devices
[i
]->name
));
117 buffer_append(&b
, "\0", 1);
118 kbdintctxt
->devices
= xstrdup(buffer_ptr(&b
));
121 kbdintctxt
->devices
= xstrdup(devs
);
123 debug("kbdint_alloc: devices '%s'", kbdintctxt
->devices
);
124 kbdintctxt
->ctxt
= NULL
;
125 kbdintctxt
->device
= NULL
;
126 kbdintctxt
->nreq
= 0;
131 kbdint_reset_device(KbdintAuthctxt
*kbdintctxt
)
133 if (kbdintctxt
->ctxt
) {
134 kbdintctxt
->device
->free_ctx(kbdintctxt
->ctxt
);
135 kbdintctxt
->ctxt
= NULL
;
137 kbdintctxt
->device
= NULL
;
140 kbdint_free(KbdintAuthctxt
*kbdintctxt
)
142 if (kbdintctxt
->device
)
143 kbdint_reset_device(kbdintctxt
);
144 if (kbdintctxt
->devices
) {
145 xfree(kbdintctxt
->devices
);
146 kbdintctxt
->devices
= NULL
;
150 /* get next device */
152 kbdint_next_device(KbdintAuthctxt
*kbdintctxt
)
158 if (kbdintctxt
->device
)
159 kbdint_reset_device(kbdintctxt
);
161 len
= kbdintctxt
->devices
?
162 strcspn(kbdintctxt
->devices
, ",") : 0;
166 for (i
= 0; devices
[i
]; i
++)
167 if (strncmp(kbdintctxt
->devices
, devices
[i
]->name
, len
) == 0)
168 kbdintctxt
->device
= devices
[i
];
169 t
= kbdintctxt
->devices
;
170 kbdintctxt
->devices
= t
[len
] ? xstrdup(t
+len
+1) : NULL
;
172 debug2("kbdint_next_device: devices %s", kbdintctxt
->devices
?
173 kbdintctxt
->devices
: "<empty>");
174 } while (kbdintctxt
->devices
&& !kbdintctxt
->device
);
176 return kbdintctxt
->device
? 1 : 0;
180 * try challenge-response, set authctxt->postponed if we have to
181 * wait for the response.
184 auth2_challenge(Authctxt
*authctxt
, char *devs
)
186 debug("auth2_challenge: user=%s devs=%s",
187 authctxt
->user
? authctxt
->user
: "<nouser>",
188 devs
? devs
: "<no devs>");
190 if (authctxt
->user
== NULL
|| !devs
)
192 if (authctxt
->kbdintctxt
== NULL
)
193 authctxt
->kbdintctxt
= kbdint_alloc(devs
);
194 return auth2_challenge_start(authctxt
);
197 /* unregister kbd-int callbacks and context */
199 auth2_challenge_stop(Authctxt
*authctxt
)
201 /* unregister callback */
202 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
, NULL
);
203 if (authctxt
->kbdintctxt
!= NULL
) {
204 kbdint_free(authctxt
->kbdintctxt
);
205 authctxt
->kbdintctxt
= NULL
;
209 /* side effect: sets authctxt->postponed if a reply was sent*/
211 auth2_challenge_start(Authctxt
*authctxt
)
213 KbdintAuthctxt
*kbdintctxt
= authctxt
->kbdintctxt
;
215 debug2("auth2_challenge_start: devices %s",
216 kbdintctxt
->devices
? kbdintctxt
->devices
: "<empty>");
218 if (kbdint_next_device(kbdintctxt
) == 0) {
219 auth2_challenge_stop(authctxt
);
222 debug("auth2_challenge_start: trying authentication method '%s'",
223 kbdintctxt
->device
->name
);
225 if ((kbdintctxt
->ctxt
= kbdintctxt
->device
->init_ctx(authctxt
)) == NULL
) {
226 auth2_challenge_stop(authctxt
);
229 if (send_userauth_info_request(authctxt
) == 0) {
230 auth2_challenge_stop(authctxt
);
233 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
,
234 &input_userauth_info_response
);
236 authctxt
->postponed
= 1;
241 send_userauth_info_request(Authctxt
*authctxt
)
243 KbdintAuthctxt
*kbdintctxt
;
244 char *name
, *instr
, **prompts
;
247 kbdintctxt
= authctxt
->kbdintctxt
;
248 if (kbdintctxt
->device
->query(kbdintctxt
->ctxt
,
249 &name
, &instr
, &kbdintctxt
->nreq
, &prompts
, &echo_on
))
252 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST
);
253 packet_put_cstring(name
);
254 packet_put_cstring(instr
);
255 packet_put_cstring(""); /* language not used */
256 packet_put_int(kbdintctxt
->nreq
);
257 for (i
= 0; i
< kbdintctxt
->nreq
; i
++) {
258 packet_put_cstring(prompts
[i
]);
259 packet_put_char(echo_on
[i
]);
264 for (i
= 0; i
< kbdintctxt
->nreq
; i
++)
274 input_userauth_info_response(int type
, u_int32_t seq
, void *ctxt
)
276 Authctxt
*authctxt
= ctxt
;
277 KbdintAuthctxt
*kbdintctxt
;
278 int authenticated
= 0, res
, len
;
280 char **response
= NULL
, *method
;
282 if (authctxt
== NULL
)
283 fatal("input_userauth_info_response: no authctxt");
284 kbdintctxt
= authctxt
->kbdintctxt
;
285 if (kbdintctxt
== NULL
|| kbdintctxt
->ctxt
== NULL
)
286 fatal("input_userauth_info_response: no kbdintctxt");
287 if (kbdintctxt
->device
== NULL
)
288 fatal("input_userauth_info_response: no device");
290 authctxt
->postponed
= 0; /* reset */
291 nresp
= packet_get_int();
292 if (nresp
!= kbdintctxt
->nreq
)
293 fatal("input_userauth_info_response: wrong number of replies");
295 fatal("input_userauth_info_response: too many replies");
297 response
= xcalloc(nresp
, sizeof(char *));
298 for (i
= 0; i
< nresp
; i
++)
299 response
[i
] = packet_get_string(NULL
);
303 res
= kbdintctxt
->device
->respond(kbdintctxt
->ctxt
, nresp
, response
);
305 for (i
= 0; i
< nresp
; i
++) {
306 memset(response
[i
], 'r', strlen(response
[i
]));
315 authenticated
= authctxt
->valid
? 1 : 0;
318 /* Authentication needs further interaction */
319 if (send_userauth_info_request(authctxt
) == 1)
320 authctxt
->postponed
= 1;
327 len
= strlen("keyboard-interactive") + 2 +
328 strlen(kbdintctxt
->device
->name
);
329 method
= xmalloc(len
);
330 snprintf(method
, len
, "keyboard-interactive/%s",
331 kbdintctxt
->device
->name
);
333 if (!authctxt
->postponed
) {
335 auth2_challenge_stop(authctxt
);
337 /* start next device */
338 /* may set authctxt->postponed */
339 auth2_challenge_start(authctxt
);
342 userauth_finish(authctxt
, authenticated
, method
);
347 privsep_challenge_enable(void)
349 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
353 extern KbdintDevice mm_bsdauth_device
;
356 extern KbdintDevice mm_sshpam_device
;
359 extern KbdintDevice mm_skey_device
;
363 devices
[n
++] = &mm_bsdauth_device
;
366 devices
[n
++] = &mm_sshpam_device
;
369 devices
[n
++] = &mm_skey_device
;