]>
Commit | Line | Data |
---|---|---|
47686178 | 1 | /* $OpenBSD: auth2-chall.c,v 1.33 2007/09/21 08:15:29 djm Exp $ */ |
3c0ef626 | 2 | /* |
3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. | |
4 | * Copyright (c) 2001 Per Allansson. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
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. | |
14 | * | |
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. | |
25 | */ | |
9108f8d9 | 26 | |
3c0ef626 | 27 | #include "includes.h" |
3c0ef626 | 28 | |
9108f8d9 | 29 | #include <sys/types.h> |
30 | ||
31 | #include <stdarg.h> | |
32 | #include <stdio.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include "xmalloc.h" | |
3c0ef626 | 36 | #include "ssh2.h" |
9108f8d9 | 37 | #include "key.h" |
38 | #include "hostfile.h" | |
3c0ef626 | 39 | #include "auth.h" |
e9a17296 | 40 | #include "buffer.h" |
3c0ef626 | 41 | #include "packet.h" |
3c0ef626 | 42 | #include "dispatch.h" |
3c0ef626 | 43 | #include "log.h" |
996d5e62 | 44 | #include "servconf.h" |
45 | ||
46 | /* import */ | |
47 | extern ServerOptions options; | |
3c0ef626 | 48 | |
49 | static int auth2_challenge_start(Authctxt *); | |
50 | static int send_userauth_info_request(Authctxt *); | |
e9a17296 | 51 | static void input_userauth_info_response(int, u_int32_t, void *); |
3c0ef626 | 52 | |
53 | #ifdef BSD_AUTH | |
54 | extern KbdintDevice bsdauth_device; | |
55 | #else | |
0fff78ff | 56 | #ifdef USE_PAM |
57 | extern KbdintDevice sshpam_device; | |
58 | #endif | |
3c0ef626 | 59 | #ifdef SKEY |
60 | extern KbdintDevice skey_device; | |
61 | #endif | |
62 | #endif | |
63 | ||
64 | KbdintDevice *devices[] = { | |
65 | #ifdef BSD_AUTH | |
66 | &bsdauth_device, | |
67 | #else | |
0fff78ff | 68 | #ifdef USE_PAM |
69 | &sshpam_device, | |
70 | #endif | |
3c0ef626 | 71 | #ifdef SKEY |
72 | &skey_device, | |
73 | #endif | |
74 | #endif | |
75 | NULL | |
76 | }; | |
77 | ||
78 | typedef struct KbdintAuthctxt KbdintAuthctxt; | |
79 | struct KbdintAuthctxt | |
80 | { | |
81 | char *devices; | |
82 | void *ctxt; | |
83 | KbdintDevice *device; | |
680cee3b | 84 | u_int nreq; |
3c0ef626 | 85 | }; |
86 | ||
996d5e62 | 87 | #ifdef USE_PAM |
88 | void | |
89 | remove_kbdint_device(const char *devname) | |
90 | { | |
91 | int i, j; | |
92 | ||
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]; | |
97 | i--; | |
98 | } | |
99 | } | |
100 | #endif | |
101 | ||
3c0ef626 | 102 | static KbdintAuthctxt * |
103 | kbdint_alloc(const char *devs) | |
104 | { | |
105 | KbdintAuthctxt *kbdintctxt; | |
e9a17296 | 106 | Buffer b; |
3c0ef626 | 107 | int i; |
3c0ef626 | 108 | |
996d5e62 | 109 | #ifdef USE_PAM |
110 | if (!options.use_pam) | |
111 | remove_kbdint_device("pam"); | |
112 | #endif | |
113 | ||
3c0ef626 | 114 | kbdintctxt = xmalloc(sizeof(KbdintAuthctxt)); |
115 | if (strcmp(devs, "") == 0) { | |
e9a17296 | 116 | buffer_init(&b); |
3c0ef626 | 117 | for (i = 0; devices[i]; i++) { |
e9a17296 | 118 | if (buffer_len(&b) > 0) |
119 | buffer_append(&b, ",", 1); | |
120 | buffer_append(&b, devices[i]->name, | |
121 | strlen(devices[i]->name)); | |
3c0ef626 | 122 | } |
e9a17296 | 123 | buffer_append(&b, "\0", 1); |
124 | kbdintctxt->devices = xstrdup(buffer_ptr(&b)); | |
125 | buffer_free(&b); | |
3c0ef626 | 126 | } else { |
127 | kbdintctxt->devices = xstrdup(devs); | |
128 | } | |
e9a17296 | 129 | debug("kbdint_alloc: devices '%s'", kbdintctxt->devices); |
3c0ef626 | 130 | kbdintctxt->ctxt = NULL; |
131 | kbdintctxt->device = NULL; | |
680cee3b | 132 | kbdintctxt->nreq = 0; |
3c0ef626 | 133 | |
134 | return kbdintctxt; | |
135 | } | |
136 | static void | |
137 | kbdint_reset_device(KbdintAuthctxt *kbdintctxt) | |
138 | { | |
139 | if (kbdintctxt->ctxt) { | |
140 | kbdintctxt->device->free_ctx(kbdintctxt->ctxt); | |
141 | kbdintctxt->ctxt = NULL; | |
142 | } | |
143 | kbdintctxt->device = NULL; | |
144 | } | |
145 | static void | |
146 | kbdint_free(KbdintAuthctxt *kbdintctxt) | |
147 | { | |
148 | if (kbdintctxt->device) | |
149 | kbdint_reset_device(kbdintctxt); | |
150 | if (kbdintctxt->devices) { | |
151 | xfree(kbdintctxt->devices); | |
152 | kbdintctxt->devices = NULL; | |
153 | } | |
154 | xfree(kbdintctxt); | |
155 | } | |
156 | /* get next device */ | |
157 | static int | |
158 | kbdint_next_device(KbdintAuthctxt *kbdintctxt) | |
159 | { | |
160 | size_t len; | |
161 | char *t; | |
162 | int i; | |
163 | ||
164 | if (kbdintctxt->device) | |
165 | kbdint_reset_device(kbdintctxt); | |
166 | do { | |
167 | len = kbdintctxt->devices ? | |
168 | strcspn(kbdintctxt->devices, ",") : 0; | |
169 | ||
170 | if (len == 0) | |
171 | break; | |
172 | for (i = 0; devices[i]; i++) | |
173 | if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0) | |
174 | kbdintctxt->device = devices[i]; | |
175 | t = kbdintctxt->devices; | |
176 | kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL; | |
177 | xfree(t); | |
178 | debug2("kbdint_next_device: devices %s", kbdintctxt->devices ? | |
665a873d | 179 | kbdintctxt->devices : "<empty>"); |
3c0ef626 | 180 | } while (kbdintctxt->devices && !kbdintctxt->device); |
181 | ||
182 | return kbdintctxt->device ? 1 : 0; | |
183 | } | |
184 | ||
185 | /* | |
186 | * try challenge-response, set authctxt->postponed if we have to | |
187 | * wait for the response. | |
188 | */ | |
189 | int | |
190 | auth2_challenge(Authctxt *authctxt, char *devs) | |
191 | { | |
192 | debug("auth2_challenge: user=%s devs=%s", | |
193 | authctxt->user ? authctxt->user : "<nouser>", | |
194 | devs ? devs : "<no devs>"); | |
195 | ||
196 | if (authctxt->user == NULL || !devs) | |
197 | return 0; | |
e9a17296 | 198 | if (authctxt->kbdintctxt == NULL) |
3c0ef626 | 199 | authctxt->kbdintctxt = kbdint_alloc(devs); |
200 | return auth2_challenge_start(authctxt); | |
201 | } | |
202 | ||
e9a17296 | 203 | /* unregister kbd-int callbacks and context */ |
204 | void | |
205 | auth2_challenge_stop(Authctxt *authctxt) | |
206 | { | |
207 | /* unregister callback */ | |
208 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL); | |
799ae497 | 209 | if (authctxt->kbdintctxt != NULL) { |
e9a17296 | 210 | kbdint_free(authctxt->kbdintctxt); |
211 | authctxt->kbdintctxt = NULL; | |
212 | } | |
213 | } | |
214 | ||
3c0ef626 | 215 | /* side effect: sets authctxt->postponed if a reply was sent*/ |
216 | static int | |
217 | auth2_challenge_start(Authctxt *authctxt) | |
218 | { | |
219 | KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt; | |
220 | ||
221 | debug2("auth2_challenge_start: devices %s", | |
222 | kbdintctxt->devices ? kbdintctxt->devices : "<empty>"); | |
223 | ||
224 | if (kbdint_next_device(kbdintctxt) == 0) { | |
e9a17296 | 225 | auth2_challenge_stop(authctxt); |
3c0ef626 | 226 | return 0; |
227 | } | |
228 | debug("auth2_challenge_start: trying authentication method '%s'", | |
229 | kbdintctxt->device->name); | |
230 | ||
231 | if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) { | |
e9a17296 | 232 | auth2_challenge_stop(authctxt); |
3c0ef626 | 233 | return 0; |
234 | } | |
235 | if (send_userauth_info_request(authctxt) == 0) { | |
e9a17296 | 236 | auth2_challenge_stop(authctxt); |
3c0ef626 | 237 | return 0; |
238 | } | |
239 | dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, | |
240 | &input_userauth_info_response); | |
241 | ||
242 | authctxt->postponed = 1; | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static int | |
247 | send_userauth_info_request(Authctxt *authctxt) | |
248 | { | |
249 | KbdintAuthctxt *kbdintctxt; | |
250 | char *name, *instr, **prompts; | |
665a873d | 251 | u_int i, *echo_on; |
3c0ef626 | 252 | |
253 | kbdintctxt = authctxt->kbdintctxt; | |
254 | if (kbdintctxt->device->query(kbdintctxt->ctxt, | |
680cee3b | 255 | &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on)) |
3c0ef626 | 256 | return 0; |
257 | ||
258 | packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST); | |
259 | packet_put_cstring(name); | |
260 | packet_put_cstring(instr); | |
f5799ae1 | 261 | packet_put_cstring(""); /* language not used */ |
680cee3b | 262 | packet_put_int(kbdintctxt->nreq); |
263 | for (i = 0; i < kbdintctxt->nreq; i++) { | |
3c0ef626 | 264 | packet_put_cstring(prompts[i]); |
265 | packet_put_char(echo_on[i]); | |
266 | } | |
267 | packet_send(); | |
268 | packet_write_wait(); | |
269 | ||
680cee3b | 270 | for (i = 0; i < kbdintctxt->nreq; i++) |
3c0ef626 | 271 | xfree(prompts[i]); |
272 | xfree(prompts); | |
273 | xfree(echo_on); | |
274 | xfree(name); | |
275 | xfree(instr); | |
276 | return 1; | |
277 | } | |
278 | ||
279 | static void | |
e9a17296 | 280 | input_userauth_info_response(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 281 | { |
282 | Authctxt *authctxt = ctxt; | |
283 | KbdintAuthctxt *kbdintctxt; | |
665a873d | 284 | int authenticated = 0, res, len; |
285 | u_int i, nresp; | |
3c0ef626 | 286 | char **response = NULL, *method; |
287 | ||
288 | if (authctxt == NULL) | |
289 | fatal("input_userauth_info_response: no authctxt"); | |
290 | kbdintctxt = authctxt->kbdintctxt; | |
291 | if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL) | |
292 | fatal("input_userauth_info_response: no kbdintctxt"); | |
293 | if (kbdintctxt->device == NULL) | |
294 | fatal("input_userauth_info_response: no device"); | |
295 | ||
296 | authctxt->postponed = 0; /* reset */ | |
297 | nresp = packet_get_int(); | |
680cee3b | 298 | if (nresp != kbdintctxt->nreq) |
299 | fatal("input_userauth_info_response: wrong number of replies"); | |
300 | if (nresp > 100) | |
301 | fatal("input_userauth_info_response: too many replies"); | |
3c0ef626 | 302 | if (nresp > 0) { |
9108f8d9 | 303 | response = xcalloc(nresp, sizeof(char *)); |
3c0ef626 | 304 | for (i = 0; i < nresp; i++) |
305 | response[i] = packet_get_string(NULL); | |
306 | } | |
e9a17296 | 307 | packet_check_eom(); |
3c0ef626 | 308 | |
996d5e62 | 309 | res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response); |
3c0ef626 | 310 | |
311 | for (i = 0; i < nresp; i++) { | |
312 | memset(response[i], 'r', strlen(response[i])); | |
313 | xfree(response[i]); | |
314 | } | |
315 | if (response) | |
316 | xfree(response); | |
317 | ||
318 | switch (res) { | |
319 | case 0: | |
320 | /* Success! */ | |
996d5e62 | 321 | authenticated = authctxt->valid ? 1 : 0; |
3c0ef626 | 322 | break; |
323 | case 1: | |
324 | /* Authentication needs further interaction */ | |
e9a17296 | 325 | if (send_userauth_info_request(authctxt) == 1) |
326 | authctxt->postponed = 1; | |
3c0ef626 | 327 | break; |
328 | default: | |
329 | /* Failure! */ | |
330 | break; | |
331 | } | |
332 | ||
333 | len = strlen("keyboard-interactive") + 2 + | |
334 | strlen(kbdintctxt->device->name); | |
335 | method = xmalloc(len); | |
e9a17296 | 336 | snprintf(method, len, "keyboard-interactive/%s", |
337 | kbdintctxt->device->name); | |
3c0ef626 | 338 | |
339 | if (!authctxt->postponed) { | |
3c0ef626 | 340 | if (authenticated) { |
e9a17296 | 341 | auth2_challenge_stop(authctxt); |
3c0ef626 | 342 | } else { |
343 | /* start next device */ | |
344 | /* may set authctxt->postponed */ | |
345 | auth2_challenge_start(authctxt); | |
346 | } | |
347 | } | |
348 | userauth_finish(authctxt, authenticated, method); | |
349 | xfree(method); | |
350 | } | |
700318f3 | 351 | |
352 | void | |
353 | privsep_challenge_enable(void) | |
354 | { | |
0fff78ff | 355 | #if defined(BSD_AUTH) || defined(USE_PAM) || defined(SKEY) |
356 | int n = 0; | |
357 | #endif | |
700318f3 | 358 | #ifdef BSD_AUTH |
359 | extern KbdintDevice mm_bsdauth_device; | |
360 | #endif | |
0fff78ff | 361 | #ifdef USE_PAM |
362 | extern KbdintDevice mm_sshpam_device; | |
363 | #endif | |
700318f3 | 364 | #ifdef SKEY |
365 | extern KbdintDevice mm_skey_device; | |
366 | #endif | |
0fff78ff | 367 | |
700318f3 | 368 | #ifdef BSD_AUTH |
0fff78ff | 369 | devices[n++] = &mm_bsdauth_device; |
700318f3 | 370 | #else |
0fff78ff | 371 | #ifdef USE_PAM |
372 | devices[n++] = &mm_sshpam_device; | |
373 | #endif | |
700318f3 | 374 | #ifdef SKEY |
0fff78ff | 375 | devices[n++] = &mm_skey_device; |
700318f3 | 376 | #endif |
377 | #endif | |
378 | } |