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