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.21 2004/06/01 14:20:45 dtucker Exp $");
36 static int auth2_challenge_start(Authctxt
*);
37 static int send_userauth_info_request(Authctxt
*);
38 static void input_userauth_info_response(int, u_int32_t
, void *);
41 extern KbdintDevice bsdauth_device
;
44 extern KbdintDevice sshpam_device
;
47 extern KbdintDevice skey_device
;
51 KbdintDevice
*devices
[] = {
65 typedef struct KbdintAuthctxt KbdintAuthctxt
;
74 static KbdintAuthctxt
*
75 kbdint_alloc(const char *devs
)
77 KbdintAuthctxt
*kbdintctxt
;
81 kbdintctxt
= xmalloc(sizeof(KbdintAuthctxt
));
82 if (strcmp(devs
, "") == 0) {
84 for (i
= 0; devices
[i
]; i
++) {
85 if (buffer_len(&b
) > 0)
86 buffer_append(&b
, ",", 1);
87 buffer_append(&b
, devices
[i
]->name
,
88 strlen(devices
[i
]->name
));
90 buffer_append(&b
, "\0", 1);
91 kbdintctxt
->devices
= xstrdup(buffer_ptr(&b
));
94 kbdintctxt
->devices
= xstrdup(devs
);
96 debug("kbdint_alloc: devices '%s'", kbdintctxt
->devices
);
97 kbdintctxt
->ctxt
= NULL
;
98 kbdintctxt
->device
= NULL
;
104 kbdint_reset_device(KbdintAuthctxt
*kbdintctxt
)
106 if (kbdintctxt
->ctxt
) {
107 kbdintctxt
->device
->free_ctx(kbdintctxt
->ctxt
);
108 kbdintctxt
->ctxt
= NULL
;
110 kbdintctxt
->device
= NULL
;
113 kbdint_free(KbdintAuthctxt
*kbdintctxt
)
115 if (kbdintctxt
->device
)
116 kbdint_reset_device(kbdintctxt
);
117 if (kbdintctxt
->devices
) {
118 xfree(kbdintctxt
->devices
);
119 kbdintctxt
->devices
= NULL
;
123 /* get next device */
125 kbdint_next_device(KbdintAuthctxt
*kbdintctxt
)
131 if (kbdintctxt
->device
)
132 kbdint_reset_device(kbdintctxt
);
134 len
= kbdintctxt
->devices
?
135 strcspn(kbdintctxt
->devices
, ",") : 0;
139 for (i
= 0; devices
[i
]; i
++)
140 if (strncmp(kbdintctxt
->devices
, devices
[i
]->name
, len
) == 0)
141 kbdintctxt
->device
= devices
[i
];
142 t
= kbdintctxt
->devices
;
143 kbdintctxt
->devices
= t
[len
] ? xstrdup(t
+len
+1) : NULL
;
145 debug2("kbdint_next_device: devices %s", kbdintctxt
->devices
?
146 kbdintctxt
->devices
: "<empty>");
147 } while (kbdintctxt
->devices
&& !kbdintctxt
->device
);
149 return kbdintctxt
->device
? 1 : 0;
153 * try challenge-response, set authctxt->postponed if we have to
154 * wait for the response.
157 auth2_challenge(Authctxt
*authctxt
, char *devs
)
159 debug("auth2_challenge: user=%s devs=%s",
160 authctxt
->user
? authctxt
->user
: "<nouser>",
161 devs
? devs
: "<no devs>");
163 if (authctxt
->user
== NULL
|| !devs
)
165 if (authctxt
->kbdintctxt
== NULL
)
166 authctxt
->kbdintctxt
= kbdint_alloc(devs
);
167 return auth2_challenge_start(authctxt
);
170 /* unregister kbd-int callbacks and context */
172 auth2_challenge_stop(Authctxt
*authctxt
)
174 /* unregister callback */
175 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
, NULL
);
176 if (authctxt
->kbdintctxt
!= NULL
) {
177 kbdint_free(authctxt
->kbdintctxt
);
178 authctxt
->kbdintctxt
= NULL
;
182 /* side effect: sets authctxt->postponed if a reply was sent*/
184 auth2_challenge_start(Authctxt
*authctxt
)
186 KbdintAuthctxt
*kbdintctxt
= authctxt
->kbdintctxt
;
188 debug2("auth2_challenge_start: devices %s",
189 kbdintctxt
->devices
? kbdintctxt
->devices
: "<empty>");
191 if (kbdint_next_device(kbdintctxt
) == 0) {
192 auth2_challenge_stop(authctxt
);
195 debug("auth2_challenge_start: trying authentication method '%s'",
196 kbdintctxt
->device
->name
);
198 if ((kbdintctxt
->ctxt
= kbdintctxt
->device
->init_ctx(authctxt
)) == NULL
) {
199 auth2_challenge_stop(authctxt
);
202 if (send_userauth_info_request(authctxt
) == 0) {
203 auth2_challenge_stop(authctxt
);
206 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
,
207 &input_userauth_info_response
);
209 authctxt
->postponed
= 1;
214 send_userauth_info_request(Authctxt
*authctxt
)
216 KbdintAuthctxt
*kbdintctxt
;
217 char *name
, *instr
, **prompts
;
221 kbdintctxt
= authctxt
->kbdintctxt
;
222 if (kbdintctxt
->device
->query(kbdintctxt
->ctxt
,
223 &name
, &instr
, &kbdintctxt
->nreq
, &prompts
, &echo_on
))
226 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST
);
227 packet_put_cstring(name
);
228 packet_put_cstring(instr
);
229 packet_put_cstring(""); /* language not used */
230 packet_put_int(kbdintctxt
->nreq
);
231 for (i
= 0; i
< kbdintctxt
->nreq
; i
++) {
232 packet_put_cstring(prompts
[i
]);
233 packet_put_char(echo_on
[i
]);
238 for (i
= 0; i
< kbdintctxt
->nreq
; i
++)
248 input_userauth_info_response(int type
, u_int32_t seq
, void *ctxt
)
250 Authctxt
*authctxt
= ctxt
;
251 KbdintAuthctxt
*kbdintctxt
;
252 int i
, authenticated
= 0, res
, len
;
254 char **response
= NULL
, *method
;
256 if (authctxt
== NULL
)
257 fatal("input_userauth_info_response: no authctxt");
258 kbdintctxt
= authctxt
->kbdintctxt
;
259 if (kbdintctxt
== NULL
|| kbdintctxt
->ctxt
== NULL
)
260 fatal("input_userauth_info_response: no kbdintctxt");
261 if (kbdintctxt
->device
== NULL
)
262 fatal("input_userauth_info_response: no device");
264 authctxt
->postponed
= 0; /* reset */
265 nresp
= packet_get_int();
266 if (nresp
!= kbdintctxt
->nreq
)
267 fatal("input_userauth_info_response: wrong number of replies");
269 fatal("input_userauth_info_response: too many replies");
271 response
= xmalloc(nresp
* sizeof(char *));
272 for (i
= 0; i
< nresp
; i
++)
273 response
[i
] = packet_get_string(NULL
);
277 if (authctxt
->valid
) {
278 res
= kbdintctxt
->device
->respond(kbdintctxt
->ctxt
,
284 for (i
= 0; i
< nresp
; i
++) {
285 memset(response
[i
], 'r', strlen(response
[i
]));
297 /* Authentication needs further interaction */
298 if (send_userauth_info_request(authctxt
) == 1)
299 authctxt
->postponed
= 1;
306 len
= strlen("keyboard-interactive") + 2 +
307 strlen(kbdintctxt
->device
->name
);
308 method
= xmalloc(len
);
309 snprintf(method
, len
, "keyboard-interactive/%s",
310 kbdintctxt
->device
->name
);
312 if (!authctxt
->postponed
) {
314 auth2_challenge_stop(authctxt
);
316 /* start next device */
317 /* may set authctxt->postponed */
318 auth2_challenge_start(authctxt
);
321 userauth_finish(authctxt
, authenticated
, method
);
326 privsep_challenge_enable(void)
328 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
332 extern KbdintDevice mm_bsdauth_device
;
335 extern KbdintDevice mm_sshpam_device
;
338 extern KbdintDevice mm_skey_device
;
342 devices
[n
++] = &mm_bsdauth_device
;
345 devices
[n
++] = &mm_sshpam_device
;
348 devices
[n
++] = &mm_skey_device
;