1 /* $OpenBSD: auth2-chall.c,v 1.28 2006/07/22 20:48:22 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.
40 extern ServerOptions options
;
42 static int auth2_challenge_start(Authctxt
*);
43 static int send_userauth_info_request(Authctxt
*);
44 static void input_userauth_info_response(int, u_int32_t
, void *);
47 extern KbdintDevice bsdauth_device
;
50 extern KbdintDevice sshpam_device
;
53 extern KbdintDevice skey_device
;
57 KbdintDevice
*devices
[] = {
71 typedef struct KbdintAuthctxt KbdintAuthctxt
;
82 remove_kbdint_device(const char *devname
)
86 for (i
= 0; devices
[i
] != NULL
; i
++)
87 if (strcmp(devices
[i
]->name
, devname
) == 0) {
88 for (j
= i
; devices
[j
] != NULL
; j
++)
89 devices
[j
] = devices
[j
+1];
95 static KbdintAuthctxt
*
96 kbdint_alloc(const char *devs
)
98 KbdintAuthctxt
*kbdintctxt
;
103 if (!options
.use_pam
)
104 remove_kbdint_device("pam");
107 kbdintctxt
= xmalloc(sizeof(KbdintAuthctxt
));
108 if (strcmp(devs
, "") == 0) {
110 for (i
= 0; devices
[i
]; i
++) {
111 if (buffer_len(&b
) > 0)
112 buffer_append(&b
, ",", 1);
113 buffer_append(&b
, devices
[i
]->name
,
114 strlen(devices
[i
]->name
));
116 buffer_append(&b
, "\0", 1);
117 kbdintctxt
->devices
= xstrdup(buffer_ptr(&b
));
120 kbdintctxt
->devices
= xstrdup(devs
);
122 debug("kbdint_alloc: devices '%s'", kbdintctxt
->devices
);
123 kbdintctxt
->ctxt
= NULL
;
124 kbdintctxt
->device
= NULL
;
125 kbdintctxt
->nreq
= 0;
130 kbdint_reset_device(KbdintAuthctxt
*kbdintctxt
)
132 if (kbdintctxt
->ctxt
) {
133 kbdintctxt
->device
->free_ctx(kbdintctxt
->ctxt
);
134 kbdintctxt
->ctxt
= NULL
;
136 kbdintctxt
->device
= NULL
;
139 kbdint_free(KbdintAuthctxt
*kbdintctxt
)
141 if (kbdintctxt
->device
)
142 kbdint_reset_device(kbdintctxt
);
143 if (kbdintctxt
->devices
) {
144 xfree(kbdintctxt
->devices
);
145 kbdintctxt
->devices
= NULL
;
149 /* get next device */
151 kbdint_next_device(KbdintAuthctxt
*kbdintctxt
)
157 if (kbdintctxt
->device
)
158 kbdint_reset_device(kbdintctxt
);
160 len
= kbdintctxt
->devices
?
161 strcspn(kbdintctxt
->devices
, ",") : 0;
165 for (i
= 0; devices
[i
]; i
++)
166 if (strncmp(kbdintctxt
->devices
, devices
[i
]->name
, len
) == 0)
167 kbdintctxt
->device
= devices
[i
];
168 t
= kbdintctxt
->devices
;
169 kbdintctxt
->devices
= t
[len
] ? xstrdup(t
+len
+1) : NULL
;
171 debug2("kbdint_next_device: devices %s", kbdintctxt
->devices
?
172 kbdintctxt
->devices
: "<empty>");
173 } while (kbdintctxt
->devices
&& !kbdintctxt
->device
);
175 return kbdintctxt
->device
? 1 : 0;
179 * try challenge-response, set authctxt->postponed if we have to
180 * wait for the response.
183 auth2_challenge(Authctxt
*authctxt
, char *devs
)
185 debug("auth2_challenge: user=%s devs=%s",
186 authctxt
->user
? authctxt
->user
: "<nouser>",
187 devs
? devs
: "<no devs>");
189 if (authctxt
->user
== NULL
|| !devs
)
191 if (authctxt
->kbdintctxt
== NULL
)
192 authctxt
->kbdintctxt
= kbdint_alloc(devs
);
193 return auth2_challenge_start(authctxt
);
196 /* unregister kbd-int callbacks and context */
198 auth2_challenge_stop(Authctxt
*authctxt
)
200 /* unregister callback */
201 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
, NULL
);
202 if (authctxt
->kbdintctxt
!= NULL
) {
203 kbdint_free(authctxt
->kbdintctxt
);
204 authctxt
->kbdintctxt
= NULL
;
208 /* side effect: sets authctxt->postponed if a reply was sent*/
210 auth2_challenge_start(Authctxt
*authctxt
)
212 KbdintAuthctxt
*kbdintctxt
= authctxt
->kbdintctxt
;
214 debug2("auth2_challenge_start: devices %s",
215 kbdintctxt
->devices
? kbdintctxt
->devices
: "<empty>");
217 if (kbdint_next_device(kbdintctxt
) == 0) {
218 auth2_challenge_stop(authctxt
);
221 debug("auth2_challenge_start: trying authentication method '%s'",
222 kbdintctxt
->device
->name
);
224 if ((kbdintctxt
->ctxt
= kbdintctxt
->device
->init_ctx(authctxt
)) == NULL
) {
225 auth2_challenge_stop(authctxt
);
228 if (send_userauth_info_request(authctxt
) == 0) {
229 auth2_challenge_stop(authctxt
);
232 dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE
,
233 &input_userauth_info_response
);
235 authctxt
->postponed
= 1;
240 send_userauth_info_request(Authctxt
*authctxt
)
242 KbdintAuthctxt
*kbdintctxt
;
243 char *name
, *instr
, **prompts
;
246 kbdintctxt
= authctxt
->kbdintctxt
;
247 if (kbdintctxt
->device
->query(kbdintctxt
->ctxt
,
248 &name
, &instr
, &kbdintctxt
->nreq
, &prompts
, &echo_on
))
251 packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST
);
252 packet_put_cstring(name
);
253 packet_put_cstring(instr
);
254 packet_put_cstring(""); /* language not used */
255 packet_put_int(kbdintctxt
->nreq
);
256 for (i
= 0; i
< kbdintctxt
->nreq
; i
++) {
257 packet_put_cstring(prompts
[i
]);
258 packet_put_char(echo_on
[i
]);
263 for (i
= 0; i
< kbdintctxt
->nreq
; i
++)
273 input_userauth_info_response(int type
, u_int32_t seq
, void *ctxt
)
275 Authctxt
*authctxt
= ctxt
;
276 KbdintAuthctxt
*kbdintctxt
;
277 int authenticated
= 0, res
, len
;
279 char **response
= NULL
, *method
;
281 if (authctxt
== NULL
)
282 fatal("input_userauth_info_response: no authctxt");
283 kbdintctxt
= authctxt
->kbdintctxt
;
284 if (kbdintctxt
== NULL
|| kbdintctxt
->ctxt
== NULL
)
285 fatal("input_userauth_info_response: no kbdintctxt");
286 if (kbdintctxt
->device
== NULL
)
287 fatal("input_userauth_info_response: no device");
289 authctxt
->postponed
= 0; /* reset */
290 nresp
= packet_get_int();
291 if (nresp
!= kbdintctxt
->nreq
)
292 fatal("input_userauth_info_response: wrong number of replies");
294 fatal("input_userauth_info_response: too many replies");
296 response
= xcalloc(nresp
, sizeof(char *));
297 for (i
= 0; i
< nresp
; i
++)
298 response
[i
] = packet_get_string(NULL
);
302 res
= kbdintctxt
->device
->respond(kbdintctxt
->ctxt
, nresp
, response
);
304 for (i
= 0; i
< nresp
; i
++) {
305 memset(response
[i
], 'r', strlen(response
[i
]));
314 authenticated
= authctxt
->valid
? 1 : 0;
317 /* Authentication needs further interaction */
318 if (send_userauth_info_request(authctxt
) == 1)
319 authctxt
->postponed
= 1;
326 len
= strlen("keyboard-interactive") + 2 +
327 strlen(kbdintctxt
->device
->name
);
328 method
= xmalloc(len
);
329 snprintf(method
, len
, "keyboard-interactive/%s",
330 kbdintctxt
->device
->name
);
332 if (!authctxt
->postponed
) {
334 auth2_challenge_stop(authctxt
);
336 /* start next device */
337 /* may set authctxt->postponed */
338 auth2_challenge_start(authctxt
);
341 userauth_finish(authctxt
, authenticated
, method
);
346 privsep_challenge_enable(void)
348 #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY)
352 extern KbdintDevice mm_bsdauth_device
;
355 extern KbdintDevice mm_sshpam_device
;
358 extern KbdintDevice mm_skey_device
;
362 devices
[n
++] = &mm_bsdauth_device
;
365 devices
[n
++] = &mm_sshpam_device
;
368 devices
[n
++] = &mm_skey_device
;