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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
31 RCSID("$OpenBSD: auth2-chall.c,v 1.20 2002/06/30 21:59:45 deraadt Exp $");
43 static void 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 skey_device
;
55 KbdintDevice
*devices
[] = {
66 typedef struct KbdintAuthctxt KbdintAuthctxt
;
75 static KbdintAuthctxt
*
76 kbdint_alloc(const char *devs
)
78 KbdintAuthctxt
*kbdintctxt
;
82 kbdintctxt
= xmalloc(sizeof(KbdintAuthctxt
));
83 if (strcmp(devs
, "") == 0) {
85 for (i
= 0; devices
[i
]; i
++) {
86 if (buffer_len(&b
) > 0)
87 buffer_append(&b
, ",", 1);
88 buffer_append(&b
, devices
[i
]->name
,
89 strlen(devices
[i
]->name
));
91 buffer_append(&b
, "\0", 1);
92 kbdintctxt
->devices
= xstrdup(buffer_ptr(&b
));
95 kbdintctxt
->devices
= xstrdup(devs
);
97 debug("kbdint_alloc: devices '%s'", kbdintctxt
->devices
);
98 kbdintctxt
->ctxt
= NULL
;
99 kbdintctxt
->device
= NULL
;
100 kbdintctxt
->nreq
= 0;
105 kbdint_reset_device(KbdintAuthctxt
*kbdintctxt
)
107 if (kbdintctxt
->ctxt
) {
108 kbdintctxt
->device
->free_ctx(kbdintctxt
->ctxt
);
109 kbdintctxt
->ctxt
= NULL
;
111 kbdintctxt
->device
= NULL
;
114 kbdint_free(KbdintAuthctxt
*kbdintctxt
)
116 if (kbdintctxt
->device
)
117 kbdint_reset_device(kbdintctxt
);
118 if (kbdintctxt
->devices
) {
119 xfree(kbdintctxt
->devices
);
120 kbdintctxt
->devices
= NULL
;
124 /* get next device */
126 kbdint_next_device(KbdintAuthctxt
*kbdintctxt
)
132 if (kbdintctxt
->device
)
133 kbdint_reset_device(kbdintctxt
);
135 len
= kbdintctxt
->devices
?
136 strcspn(kbdintctxt
->devices
, ",") : 0;
140 for (i
= 0; devices
[i
]; i
++)
141 if (strncmp(kbdintctxt
->devices
, devices
[i
]->name
, len
) == 0)
142 kbdintctxt
->device
= devices
[i
];
143 t
= kbdintctxt
->devices
;
144 kbdintctxt
->devices
= t
[len
] ? xstrdup(t
+len
+1) : NULL
;
146 debug2("kbdint_next_device: devices %s", kbdintctxt
->devices
?
147 kbdintctxt
->devices
: "<empty>");
148 } while (kbdintctxt
->devices
&& !kbdintctxt
->device
);
150 return kbdintctxt
->device
? 1 : 0;
154 * try challenge-response, set authctxt->method->postponed if we have to
155 * wait for the response.
158 auth2_challenge(Authctxt
*authctxt
, char *devs
)
160 debug("auth2_challenge: user=%s devs=%s",
161 authctxt
->user
? authctxt
->user
: "<nouser>",
162 devs
? devs
: "<no devs>");
164 if (authctxt
->user
== NULL
|| !devs
)
166 if (authctxt
->method
->method_data
!= NULL
) {
167 auth2_challenge_abandon(authctxt
);
168 authctxt
->method
->abandoned
= 0;
170 authctxt
->method
->method_data
= (void *) kbdint_alloc(devs
);
171 auth2_challenge_start(authctxt
);
174 /* unregister kbd-int callbacks and context */
176 auth2_challenge_stop(Authctxt
*authctxt
)
178 /* unregister callback */
179 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
, NULL
);
180 if (authctxt
->method
->method_data
!= NULL
) {
181 kbdint_free((KbdintAuthctxt
*) authctxt
->method
->method_data
);
182 authctxt
->method
->method_data
= NULL
;
187 auth2_challenge_abandon(Authctxt
*authctxt
)
189 auth2_challenge_stop(authctxt
);
190 authctxt
->method
->abandoned
= 1;
191 authctxt
->method
->postponed
= 0;
192 authctxt
->method
->authenticated
= 0;
193 authctxt
->method
->abandons
++;
194 authctxt
->method
->attempts
++;
197 /* side effect: sets authctxt->method->postponed if a reply was sent*/
199 auth2_challenge_start(Authctxt
*authctxt
)
201 KbdintAuthctxt
*kbdintctxt
= (KbdintAuthctxt
*)
202 authctxt
->method
->method_data
;
204 debug2("auth2_challenge_start: devices %s",
205 kbdintctxt
->devices
? kbdintctxt
->devices
: "<empty>");
207 if (kbdint_next_device(kbdintctxt
) == 0) {
208 auth2_challenge_stop(authctxt
);
211 debug("auth2_challenge_start: trying authentication method '%s'",
212 kbdintctxt
->device
->name
);
214 if ((kbdintctxt
->ctxt
= kbdintctxt
->device
->init_ctx(authctxt
)) == NULL
) {
215 auth2_challenge_stop(authctxt
);
218 if (send_userauth_info_request(authctxt
) == 0) {
219 auth2_challenge_stop(authctxt
);
222 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
,
223 &input_userauth_info_response
);
225 authctxt
->method
->postponed
= 1;
229 send_userauth_info_request(Authctxt
*authctxt
)
231 KbdintAuthctxt
*kbdintctxt
;
232 char *name
, *instr
, **prompts
;
236 kbdintctxt
= (KbdintAuthctxt
*) authctxt
->method
->method_data
;
237 if (kbdintctxt
->device
->query(kbdintctxt
->ctxt
,
238 &name
, &instr
, &kbdintctxt
->nreq
, &prompts
, &echo_on
))
241 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST
);
242 packet_put_cstring(name
);
243 packet_put_utf8_cstring(instr
);
244 packet_put_cstring(""); /* language not used */
245 packet_put_int(kbdintctxt
->nreq
);
246 for (i
= 0; i
< kbdintctxt
->nreq
; i
++) {
247 packet_put_utf8_cstring(prompts
[i
]);
248 packet_put_char(echo_on
[i
]);
253 for (i
= 0; i
< kbdintctxt
->nreq
; i
++)
263 input_userauth_info_response(int type
, u_int32_t seq
, void *ctxt
)
265 Authctxt
*authctxt
= ctxt
;
266 KbdintAuthctxt
*kbdintctxt
;
269 char **response
= NULL
, *method
;
271 if (authctxt
== NULL
)
272 fatal("input_userauth_info_response: no authctxt");
273 kbdintctxt
= (KbdintAuthctxt
*) authctxt
->method
->method_data
;
274 if (kbdintctxt
== NULL
|| kbdintctxt
->ctxt
== NULL
)
275 fatal("input_userauth_info_response: no kbdintctxt");
276 if (kbdintctxt
->device
== NULL
)
277 fatal("input_userauth_info_response: no device");
279 nresp
= packet_get_int();
280 if (nresp
!= kbdintctxt
->nreq
)
281 fatal("input_userauth_info_response: wrong number of replies");
283 fatal("input_userauth_info_response: too many replies");
285 response
= xmalloc(nresp
* sizeof(char *));
286 for (i
= 0; i
< nresp
; i
++)
287 response
[i
] = packet_get_string(NULL
);
291 if (authctxt
->valid
) {
292 res
= kbdintctxt
->device
->respond(kbdintctxt
->ctxt
,
298 for (i
= 0; i
< nresp
; i
++) {
299 memset(response
[i
], 'r', strlen(response
[i
]));
305 authctxt
->method
->postponed
= 0; /* reset */
309 authctxt
->method
->authenticated
= 1;
312 /* Authentication needs further interaction */
313 if (send_userauth_info_request(authctxt
) == 1) {
314 authctxt
->method
->postponed
= 1;
323 len
= strlen("keyboard-interactive") + 2 +
324 strlen(kbdintctxt
->device
->name
);
325 method
= xmalloc(len
);
326 snprintf(method
, len
, "keyboard-interactive/%s",
327 kbdintctxt
->device
->name
);
329 if (authctxt
->method
->authenticated
|| authctxt
->method
->abandoned
) {
330 auth2_challenge_stop(authctxt
);
332 /* start next device */
333 /* may set authctxt->method->postponed */
334 auth2_challenge_start(authctxt
);
336 userauth_finish(authctxt
, method
);
341 privsep_challenge_enable(void)
344 extern KbdintDevice mm_bsdauth_device
;
347 extern KbdintDevice mm_skey_device
;
349 /* As long as SSHv1 has devices[0] hard coded this is fine */
351 devices
[0] = &mm_bsdauth_device
;
354 devices
[0] = &mm_skey_device
;