]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> | |
3 | * Copyright 2002 Markus Friedl <markus@openbsd.org> | |
4 | * 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 | */ | |
26 | ||
27 | #include "includes.h" | |
28 | RCSID("$OpenBSD: monitor.c,v 1.51 2003/11/04 08:54:09 djm Exp $"); | |
29 | ||
30 | #include <openssl/dh.h> | |
31 | ||
32 | #ifdef SKEY | |
33 | #include <skey.h> | |
34 | #endif | |
35 | ||
36 | #include "ssh.h" | |
37 | #include "auth.h" | |
38 | #include "kex.h" | |
39 | #include "dh.h" | |
40 | #include "zlib.h" | |
41 | #include "packet.h" | |
42 | #include "auth-options.h" | |
43 | #include "sshpty.h" | |
44 | #include "channels.h" | |
45 | #include "session.h" | |
46 | #include "sshlogin.h" | |
47 | #include "canohost.h" | |
48 | #include "log.h" | |
49 | #include "servconf.h" | |
50 | #include "monitor.h" | |
51 | #include "monitor_mm.h" | |
52 | #include "monitor_wrap.h" | |
53 | #include "monitor_fdpass.h" | |
54 | #include "xmalloc.h" | |
55 | #include "misc.h" | |
56 | #include "buffer.h" | |
57 | #include "bufaux.h" | |
58 | #include "compat.h" | |
59 | #include "ssh2.h" | |
60 | #include "mpaux.h" | |
61 | ||
62 | #ifdef GSSAPI | |
63 | #include "ssh-gss.h" | |
64 | static Gssctxt *gsscontext = NULL; | |
65 | #endif | |
66 | ||
67 | /* Imports */ | |
68 | extern ServerOptions options; | |
69 | extern u_int utmp_len; | |
70 | extern Newkeys *current_keys[]; | |
71 | extern z_stream incoming_stream; | |
72 | extern z_stream outgoing_stream; | |
73 | extern u_char session_id[]; | |
74 | extern Buffer input, output; | |
75 | extern Buffer auth_debug; | |
76 | extern int auth_debug_init; | |
77 | ||
78 | /* State exported from the child */ | |
79 | ||
80 | struct { | |
81 | z_stream incoming; | |
82 | z_stream outgoing; | |
83 | u_char *keyin; | |
84 | u_int keyinlen; | |
85 | u_char *keyout; | |
86 | u_int keyoutlen; | |
87 | u_char *ivin; | |
88 | u_int ivinlen; | |
89 | u_char *ivout; | |
90 | u_int ivoutlen; | |
91 | u_char *ssh1key; | |
92 | u_int ssh1keylen; | |
93 | int ssh1cipher; | |
94 | int ssh1protoflags; | |
95 | u_char *input; | |
96 | u_int ilen; | |
97 | u_char *output; | |
98 | u_int olen; | |
99 | } child_state; | |
100 | ||
101 | /* Functions on the monitor that answer unprivileged requests */ | |
102 | ||
103 | int mm_answer_moduli(int, Buffer *); | |
104 | int mm_answer_sign(int, Buffer *); | |
105 | int mm_answer_pwnamallow(int, Buffer *); | |
106 | int mm_answer_auth2_read_banner(int, Buffer *); | |
107 | int mm_answer_authserv(int, Buffer *); | |
108 | int mm_answer_authpassword(int, Buffer *); | |
109 | int mm_answer_bsdauthquery(int, Buffer *); | |
110 | int mm_answer_bsdauthrespond(int, Buffer *); | |
111 | int mm_answer_skeyquery(int, Buffer *); | |
112 | int mm_answer_skeyrespond(int, Buffer *); | |
113 | int mm_answer_keyallowed(int, Buffer *); | |
114 | int mm_answer_keyverify(int, Buffer *); | |
115 | int mm_answer_pty(int, Buffer *); | |
116 | int mm_answer_pty_cleanup(int, Buffer *); | |
117 | int mm_answer_term(int, Buffer *); | |
118 | int mm_answer_rsa_keyallowed(int, Buffer *); | |
119 | int mm_answer_rsa_challenge(int, Buffer *); | |
120 | int mm_answer_rsa_response(int, Buffer *); | |
121 | int mm_answer_sesskey(int, Buffer *); | |
122 | int mm_answer_sessid(int, Buffer *); | |
123 | ||
124 | #ifdef USE_PAM | |
125 | int mm_answer_pam_start(int, Buffer *); | |
126 | int mm_answer_pam_account(int, Buffer *); | |
127 | int mm_answer_pam_init_ctx(int, Buffer *); | |
128 | int mm_answer_pam_query(int, Buffer *); | |
129 | int mm_answer_pam_respond(int, Buffer *); | |
130 | int mm_answer_pam_free_ctx(int, Buffer *); | |
131 | #endif | |
132 | ||
133 | #ifdef GSSAPI | |
134 | int mm_answer_gss_setup_ctx(int, Buffer *); | |
135 | int mm_answer_gss_accept_ctx(int, Buffer *); | |
136 | int mm_answer_gss_userok(int, Buffer *); | |
137 | #endif | |
138 | ||
139 | static Authctxt *authctxt; | |
140 | static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */ | |
141 | ||
142 | /* local state for key verify */ | |
143 | static u_char *key_blob = NULL; | |
144 | static u_int key_bloblen = 0; | |
145 | static int key_blobtype = MM_NOKEY; | |
146 | static char *hostbased_cuser = NULL; | |
147 | static char *hostbased_chost = NULL; | |
148 | static char *auth_method = "unknown"; | |
149 | static u_int session_id2_len = 0; | |
150 | static u_char *session_id2 = NULL; | |
151 | static pid_t monitor_child_pid; | |
152 | ||
153 | struct mon_table { | |
154 | enum monitor_reqtype type; | |
155 | int flags; | |
156 | int (*f)(int, Buffer *); | |
157 | }; | |
158 | ||
159 | #define MON_ISAUTH 0x0004 /* Required for Authentication */ | |
160 | #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ | |
161 | #define MON_ONCE 0x0010 /* Disable after calling */ | |
162 | ||
163 | #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) | |
164 | ||
165 | #define MON_PERMIT 0x1000 /* Request is permitted */ | |
166 | ||
167 | struct mon_table mon_dispatch_proto20[] = { | |
168 | {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, | |
169 | {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, | |
170 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, | |
171 | {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, | |
172 | {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, | |
173 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, | |
174 | #ifdef USE_PAM | |
175 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, | |
176 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, | |
177 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, | |
178 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, | |
179 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, | |
180 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, | |
181 | #endif | |
182 | #ifdef BSD_AUTH | |
183 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, | |
184 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond}, | |
185 | #endif | |
186 | #ifdef SKEY | |
187 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, | |
188 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, | |
189 | #endif | |
190 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, | |
191 | {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, | |
192 | #ifdef GSSAPI | |
193 | {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, | |
194 | {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx}, | |
195 | {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok}, | |
196 | #endif | |
197 | {0, 0, NULL} | |
198 | }; | |
199 | ||
200 | struct mon_table mon_dispatch_postauth20[] = { | |
201 | {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, | |
202 | {MONITOR_REQ_SIGN, 0, mm_answer_sign}, | |
203 | {MONITOR_REQ_PTY, 0, mm_answer_pty}, | |
204 | {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, | |
205 | {MONITOR_REQ_TERM, 0, mm_answer_term}, | |
206 | {0, 0, NULL} | |
207 | }; | |
208 | ||
209 | struct mon_table mon_dispatch_proto15[] = { | |
210 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, | |
211 | {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey}, | |
212 | {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid}, | |
213 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, | |
214 | {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed}, | |
215 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, | |
216 | {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge}, | |
217 | {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response}, | |
218 | #ifdef BSD_AUTH | |
219 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, | |
220 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond}, | |
221 | #endif | |
222 | #ifdef SKEY | |
223 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, | |
224 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, | |
225 | #endif | |
226 | #ifdef USE_PAM | |
227 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, | |
228 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, | |
229 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, | |
230 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, | |
231 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, | |
232 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, | |
233 | #endif | |
234 | {0, 0, NULL} | |
235 | }; | |
236 | ||
237 | struct mon_table mon_dispatch_postauth15[] = { | |
238 | {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty}, | |
239 | {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup}, | |
240 | {MONITOR_REQ_TERM, 0, mm_answer_term}, | |
241 | {0, 0, NULL} | |
242 | }; | |
243 | ||
244 | struct mon_table *mon_dispatch; | |
245 | ||
246 | /* Specifies if a certain message is allowed at the moment */ | |
247 | ||
248 | static void | |
249 | monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) | |
250 | { | |
251 | while (ent->f != NULL) { | |
252 | if (ent->type == type) { | |
253 | ent->flags &= ~MON_PERMIT; | |
254 | ent->flags |= permit ? MON_PERMIT : 0; | |
255 | return; | |
256 | } | |
257 | ent++; | |
258 | } | |
259 | } | |
260 | ||
261 | static void | |
262 | monitor_permit_authentications(int permit) | |
263 | { | |
264 | struct mon_table *ent = mon_dispatch; | |
265 | ||
266 | while (ent->f != NULL) { | |
267 | if (ent->flags & MON_AUTH) { | |
268 | ent->flags &= ~MON_PERMIT; | |
269 | ent->flags |= permit ? MON_PERMIT : 0; | |
270 | } | |
271 | ent++; | |
272 | } | |
273 | } | |
274 | ||
275 | void | |
276 | monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) | |
277 | { | |
278 | struct mon_table *ent; | |
279 | int authenticated = 0; | |
280 | ||
281 | debug3("preauth child monitor started"); | |
282 | ||
283 | authctxt = _authctxt; | |
284 | memset(authctxt, 0, sizeof(*authctxt)); | |
285 | ||
286 | if (compat20) { | |
287 | mon_dispatch = mon_dispatch_proto20; | |
288 | ||
289 | /* Permit requests for moduli and signatures */ | |
290 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); | |
291 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); | |
292 | } else { | |
293 | mon_dispatch = mon_dispatch_proto15; | |
294 | ||
295 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); | |
296 | } | |
297 | ||
298 | /* The first few requests do not require asynchronous access */ | |
299 | while (!authenticated) { | |
300 | authenticated = monitor_read(pmonitor, mon_dispatch, &ent); | |
301 | if (authenticated) { | |
302 | if (!(ent->flags & MON_AUTHDECIDE)) | |
303 | fatal("%s: unexpected authentication from %d", | |
304 | __func__, ent->type); | |
305 | if (authctxt->pw->pw_uid == 0 && | |
306 | !auth_root_allowed(auth_method)) | |
307 | authenticated = 0; | |
308 | #ifdef USE_PAM | |
309 | /* PAM needs to perform account checks after auth */ | |
310 | if (options.use_pam) { | |
311 | Buffer m; | |
312 | ||
313 | buffer_init(&m); | |
314 | mm_request_receive_expect(pmonitor->m_sendfd, | |
315 | MONITOR_REQ_PAM_ACCOUNT, &m); | |
316 | authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m); | |
317 | buffer_free(&m); | |
318 | } | |
319 | #endif | |
320 | } | |
321 | ||
322 | if (ent->flags & MON_AUTHDECIDE) { | |
323 | auth_log(authctxt, authenticated, auth_method, | |
324 | compat20 ? " ssh2" : ""); | |
325 | if (!authenticated) | |
326 | authctxt->failures++; | |
327 | } | |
328 | } | |
329 | ||
330 | if (!authctxt->valid) | |
331 | fatal("%s: authenticated invalid user", __func__); | |
332 | ||
333 | debug("%s: %s has been authenticated by privileged process", | |
334 | __func__, authctxt->user); | |
335 | ||
336 | mm_get_keystate(pmonitor); | |
337 | } | |
338 | ||
339 | static void | |
340 | monitor_set_child_handler(pid_t pid) | |
341 | { | |
342 | monitor_child_pid = pid; | |
343 | } | |
344 | ||
345 | static void | |
346 | monitor_child_handler(int signal) | |
347 | { | |
348 | kill(monitor_child_pid, signal); | |
349 | } | |
350 | ||
351 | void | |
352 | monitor_child_postauth(struct monitor *pmonitor) | |
353 | { | |
354 | monitor_set_child_handler(pmonitor->m_pid); | |
355 | signal(SIGHUP, &monitor_child_handler); | |
356 | signal(SIGTERM, &monitor_child_handler); | |
357 | ||
358 | if (compat20) { | |
359 | mon_dispatch = mon_dispatch_postauth20; | |
360 | ||
361 | /* Permit requests for moduli and signatures */ | |
362 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); | |
363 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); | |
364 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); | |
365 | } else { | |
366 | mon_dispatch = mon_dispatch_postauth15; | |
367 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); | |
368 | } | |
369 | if (!no_pty_flag) { | |
370 | monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); | |
371 | monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); | |
372 | } | |
373 | ||
374 | for (;;) | |
375 | monitor_read(pmonitor, mon_dispatch, NULL); | |
376 | } | |
377 | ||
378 | void | |
379 | monitor_sync(struct monitor *pmonitor) | |
380 | { | |
381 | if (options.compression) { | |
382 | /* The member allocation is not visible, so sync it */ | |
383 | mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback); | |
384 | } | |
385 | } | |
386 | ||
387 | int | |
388 | monitor_read(struct monitor *pmonitor, struct mon_table *ent, | |
389 | struct mon_table **pent) | |
390 | { | |
391 | Buffer m; | |
392 | int ret; | |
393 | u_char type; | |
394 | ||
395 | buffer_init(&m); | |
396 | ||
397 | mm_request_receive(pmonitor->m_sendfd, &m); | |
398 | type = buffer_get_char(&m); | |
399 | ||
400 | debug3("%s: checking request %d", __func__, type); | |
401 | ||
402 | while (ent->f != NULL) { | |
403 | if (ent->type == type) | |
404 | break; | |
405 | ent++; | |
406 | } | |
407 | ||
408 | if (ent->f != NULL) { | |
409 | if (!(ent->flags & MON_PERMIT)) | |
410 | fatal("%s: unpermitted request %d", __func__, | |
411 | type); | |
412 | ret = (*ent->f)(pmonitor->m_sendfd, &m); | |
413 | buffer_free(&m); | |
414 | ||
415 | /* The child may use this request only once, disable it */ | |
416 | if (ent->flags & MON_ONCE) { | |
417 | debug2("%s: %d used once, disabling now", __func__, | |
418 | type); | |
419 | ent->flags &= ~MON_PERMIT; | |
420 | } | |
421 | ||
422 | if (pent != NULL) | |
423 | *pent = ent; | |
424 | ||
425 | return ret; | |
426 | } | |
427 | ||
428 | fatal("%s: unsupported request: %d", __func__, type); | |
429 | ||
430 | /* NOTREACHED */ | |
431 | return (-1); | |
432 | } | |
433 | ||
434 | /* allowed key state */ | |
435 | static int | |
436 | monitor_allowed_key(u_char *blob, u_int bloblen) | |
437 | { | |
438 | /* make sure key is allowed */ | |
439 | if (key_blob == NULL || key_bloblen != bloblen || | |
440 | memcmp(key_blob, blob, key_bloblen)) | |
441 | return (0); | |
442 | return (1); | |
443 | } | |
444 | ||
445 | static void | |
446 | monitor_reset_key_state(void) | |
447 | { | |
448 | /* reset state */ | |
449 | if (key_blob != NULL) | |
450 | xfree(key_blob); | |
451 | if (hostbased_cuser != NULL) | |
452 | xfree(hostbased_cuser); | |
453 | if (hostbased_chost != NULL) | |
454 | xfree(hostbased_chost); | |
455 | key_blob = NULL; | |
456 | key_bloblen = 0; | |
457 | key_blobtype = MM_NOKEY; | |
458 | hostbased_cuser = NULL; | |
459 | hostbased_chost = NULL; | |
460 | } | |
461 | ||
462 | int | |
463 | mm_answer_moduli(int socket, Buffer *m) | |
464 | { | |
465 | DH *dh; | |
466 | int min, want, max; | |
467 | ||
468 | min = buffer_get_int(m); | |
469 | want = buffer_get_int(m); | |
470 | max = buffer_get_int(m); | |
471 | ||
472 | debug3("%s: got parameters: %d %d %d", | |
473 | __func__, min, want, max); | |
474 | /* We need to check here, too, in case the child got corrupted */ | |
475 | if (max < min || want < min || max < want) | |
476 | fatal("%s: bad parameters: %d %d %d", | |
477 | __func__, min, want, max); | |
478 | ||
479 | buffer_clear(m); | |
480 | ||
481 | dh = choose_dh(min, want, max); | |
482 | if (dh == NULL) { | |
483 | buffer_put_char(m, 0); | |
484 | return (0); | |
485 | } else { | |
486 | /* Send first bignum */ | |
487 | buffer_put_char(m, 1); | |
488 | buffer_put_bignum2(m, dh->p); | |
489 | buffer_put_bignum2(m, dh->g); | |
490 | ||
491 | DH_free(dh); | |
492 | } | |
493 | mm_request_send(socket, MONITOR_ANS_MODULI, m); | |
494 | return (0); | |
495 | } | |
496 | ||
497 | int | |
498 | mm_answer_sign(int socket, Buffer *m) | |
499 | { | |
500 | Key *key; | |
501 | u_char *p; | |
502 | u_char *signature; | |
503 | u_int siglen, datlen; | |
504 | int keyid; | |
505 | ||
506 | debug3("%s", __func__); | |
507 | ||
508 | keyid = buffer_get_int(m); | |
509 | p = buffer_get_string(m, &datlen); | |
510 | ||
511 | if (datlen != 20) | |
512 | fatal("%s: data length incorrect: %u", __func__, datlen); | |
513 | ||
514 | /* save session id, it will be passed on the first call */ | |
515 | if (session_id2_len == 0) { | |
516 | session_id2_len = datlen; | |
517 | session_id2 = xmalloc(session_id2_len); | |
518 | memcpy(session_id2, p, session_id2_len); | |
519 | } | |
520 | ||
521 | if ((key = get_hostkey_by_index(keyid)) == NULL) | |
522 | fatal("%s: no hostkey from index %d", __func__, keyid); | |
523 | if (key_sign(key, &signature, &siglen, p, datlen) < 0) | |
524 | fatal("%s: key_sign failed", __func__); | |
525 | ||
526 | debug3("%s: signature %p(%u)", __func__, signature, siglen); | |
527 | ||
528 | buffer_clear(m); | |
529 | buffer_put_string(m, signature, siglen); | |
530 | ||
531 | xfree(p); | |
532 | xfree(signature); | |
533 | ||
534 | mm_request_send(socket, MONITOR_ANS_SIGN, m); | |
535 | ||
536 | /* Turn on permissions for getpwnam */ | |
537 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); | |
538 | ||
539 | return (0); | |
540 | } | |
541 | ||
542 | /* Retrieves the password entry and also checks if the user is permitted */ | |
543 | ||
544 | int | |
545 | mm_answer_pwnamallow(int socket, Buffer *m) | |
546 | { | |
547 | char *login; | |
548 | struct passwd *pwent; | |
549 | int allowed = 0; | |
550 | ||
551 | debug3("%s", __func__); | |
552 | ||
553 | if (authctxt->attempt++ != 0) | |
554 | fatal("%s: multiple attempts for getpwnam", __func__); | |
555 | ||
556 | login = buffer_get_string(m, NULL); | |
557 | ||
558 | pwent = getpwnamallow(login); | |
559 | ||
560 | authctxt->user = xstrdup(login); | |
561 | setproctitle("%s [priv]", pwent ? login : "unknown"); | |
562 | xfree(login); | |
563 | ||
564 | buffer_clear(m); | |
565 | ||
566 | if (pwent == NULL) { | |
567 | buffer_put_char(m, 0); | |
568 | goto out; | |
569 | } | |
570 | ||
571 | allowed = 1; | |
572 | authctxt->pw = pwent; | |
573 | authctxt->valid = 1; | |
574 | ||
575 | buffer_put_char(m, 1); | |
576 | buffer_put_string(m, pwent, sizeof(struct passwd)); | |
577 | buffer_put_cstring(m, pwent->pw_name); | |
578 | buffer_put_cstring(m, "*"); | |
579 | buffer_put_cstring(m, pwent->pw_gecos); | |
580 | #ifdef HAVE_PW_CLASS_IN_PASSWD | |
581 | buffer_put_cstring(m, pwent->pw_class); | |
582 | #endif | |
583 | buffer_put_cstring(m, pwent->pw_dir); | |
584 | buffer_put_cstring(m, pwent->pw_shell); | |
585 | ||
586 | out: | |
587 | debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); | |
588 | mm_request_send(socket, MONITOR_ANS_PWNAM, m); | |
589 | ||
590 | /* For SSHv1 allow authentication now */ | |
591 | if (!compat20) | |
592 | monitor_permit_authentications(1); | |
593 | else { | |
594 | /* Allow service/style information on the auth context */ | |
595 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); | |
596 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); | |
597 | } | |
598 | ||
599 | #ifdef USE_PAM | |
600 | if (options.use_pam) | |
601 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); | |
602 | #endif | |
603 | ||
604 | return (0); | |
605 | } | |
606 | ||
607 | int mm_answer_auth2_read_banner(int socket, Buffer *m) | |
608 | { | |
609 | char *banner; | |
610 | ||
611 | buffer_clear(m); | |
612 | banner = auth2_read_banner(); | |
613 | buffer_put_cstring(m, banner != NULL ? banner : ""); | |
614 | mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m); | |
615 | ||
616 | if (banner != NULL) | |
617 | xfree(banner); | |
618 | ||
619 | return (0); | |
620 | } | |
621 | ||
622 | int | |
623 | mm_answer_authserv(int socket, Buffer *m) | |
624 | { | |
625 | monitor_permit_authentications(1); | |
626 | ||
627 | authctxt->service = buffer_get_string(m, NULL); | |
628 | authctxt->style = buffer_get_string(m, NULL); | |
629 | debug3("%s: service=%s, style=%s", | |
630 | __func__, authctxt->service, authctxt->style); | |
631 | ||
632 | if (strlen(authctxt->style) == 0) { | |
633 | xfree(authctxt->style); | |
634 | authctxt->style = NULL; | |
635 | } | |
636 | ||
637 | return (0); | |
638 | } | |
639 | ||
640 | int | |
641 | mm_answer_authpassword(int socket, Buffer *m) | |
642 | { | |
643 | static int call_count; | |
644 | char *passwd; | |
645 | int authenticated; | |
646 | u_int plen; | |
647 | ||
648 | passwd = buffer_get_string(m, &plen); | |
649 | /* Only authenticate if the context is valid */ | |
650 | authenticated = options.password_authentication && | |
651 | auth_password(authctxt, passwd); | |
652 | memset(passwd, 0, strlen(passwd)); | |
653 | xfree(passwd); | |
654 | ||
655 | buffer_clear(m); | |
656 | buffer_put_int(m, authenticated); | |
657 | ||
658 | debug3("%s: sending result %d", __func__, authenticated); | |
659 | mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m); | |
660 | ||
661 | call_count++; | |
662 | if (plen == 0 && call_count == 1) | |
663 | auth_method = "none"; | |
664 | else | |
665 | auth_method = "password"; | |
666 | ||
667 | /* Causes monitor loop to terminate if authenticated */ | |
668 | return (authenticated); | |
669 | } | |
670 | ||
671 | #ifdef BSD_AUTH | |
672 | int | |
673 | mm_answer_bsdauthquery(int socket, Buffer *m) | |
674 | { | |
675 | char *name, *infotxt; | |
676 | u_int numprompts; | |
677 | u_int *echo_on; | |
678 | char **prompts; | |
679 | u_int success; | |
680 | ||
681 | success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, | |
682 | &prompts, &echo_on) < 0 ? 0 : 1; | |
683 | ||
684 | buffer_clear(m); | |
685 | buffer_put_int(m, success); | |
686 | if (success) | |
687 | buffer_put_cstring(m, prompts[0]); | |
688 | ||
689 | debug3("%s: sending challenge success: %u", __func__, success); | |
690 | mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m); | |
691 | ||
692 | if (success) { | |
693 | xfree(name); | |
694 | xfree(infotxt); | |
695 | xfree(prompts); | |
696 | xfree(echo_on); | |
697 | } | |
698 | ||
699 | return (0); | |
700 | } | |
701 | ||
702 | int | |
703 | mm_answer_bsdauthrespond(int socket, Buffer *m) | |
704 | { | |
705 | char *response; | |
706 | int authok; | |
707 | ||
708 | if (authctxt->as == 0) | |
709 | fatal("%s: no bsd auth session", __func__); | |
710 | ||
711 | response = buffer_get_string(m, NULL); | |
712 | authok = options.challenge_response_authentication && | |
713 | auth_userresponse(authctxt->as, response, 0); | |
714 | authctxt->as = NULL; | |
715 | debug3("%s: <%s> = <%d>", __func__, response, authok); | |
716 | xfree(response); | |
717 | ||
718 | buffer_clear(m); | |
719 | buffer_put_int(m, authok); | |
720 | ||
721 | debug3("%s: sending authenticated: %d", __func__, authok); | |
722 | mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m); | |
723 | ||
724 | auth_method = "bsdauth"; | |
725 | ||
726 | return (authok != 0); | |
727 | } | |
728 | #endif | |
729 | ||
730 | #ifdef SKEY | |
731 | int | |
732 | mm_answer_skeyquery(int socket, Buffer *m) | |
733 | { | |
734 | struct skey skey; | |
735 | char challenge[1024]; | |
736 | u_int success; | |
737 | ||
738 | success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1; | |
739 | ||
740 | buffer_clear(m); | |
741 | buffer_put_int(m, success); | |
742 | if (success) | |
743 | buffer_put_cstring(m, challenge); | |
744 | ||
745 | debug3("%s: sending challenge success: %u", __func__, success); | |
746 | mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m); | |
747 | ||
748 | return (0); | |
749 | } | |
750 | ||
751 | int | |
752 | mm_answer_skeyrespond(int socket, Buffer *m) | |
753 | { | |
754 | char *response; | |
755 | int authok; | |
756 | ||
757 | response = buffer_get_string(m, NULL); | |
758 | ||
759 | authok = (options.challenge_response_authentication && | |
760 | authctxt->valid && | |
761 | skey_haskey(authctxt->pw->pw_name) == 0 && | |
762 | skey_passcheck(authctxt->pw->pw_name, response) != -1); | |
763 | ||
764 | xfree(response); | |
765 | ||
766 | buffer_clear(m); | |
767 | buffer_put_int(m, authok); | |
768 | ||
769 | debug3("%s: sending authenticated: %d", __func__, authok); | |
770 | mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m); | |
771 | ||
772 | auth_method = "skey"; | |
773 | ||
774 | return (authok != 0); | |
775 | } | |
776 | #endif | |
777 | ||
778 | #ifdef USE_PAM | |
779 | int | |
780 | mm_answer_pam_start(int socket, Buffer *m) | |
781 | { | |
782 | char *user; | |
783 | ||
784 | if (!options.use_pam) | |
785 | fatal("UsePAM not set, but ended up in %s anyway", __func__); | |
786 | ||
787 | user = buffer_get_string(m, NULL); | |
788 | ||
789 | start_pam(user); | |
790 | ||
791 | xfree(user); | |
792 | ||
793 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); | |
794 | ||
795 | return (0); | |
796 | } | |
797 | ||
798 | int | |
799 | mm_answer_pam_account(int socket, Buffer *m) | |
800 | { | |
801 | u_int ret; | |
802 | ||
803 | if (!options.use_pam) | |
804 | fatal("UsePAM not set, but ended up in %s anyway", __func__); | |
805 | ||
806 | ret = do_pam_account(); | |
807 | ||
808 | buffer_put_int(m, ret); | |
809 | ||
810 | mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m); | |
811 | ||
812 | return (ret); | |
813 | } | |
814 | ||
815 | static void *sshpam_ctxt, *sshpam_authok; | |
816 | extern KbdintDevice sshpam_device; | |
817 | ||
818 | int | |
819 | mm_answer_pam_init_ctx(int socket, Buffer *m) | |
820 | { | |
821 | ||
822 | debug3("%s", __func__); | |
823 | authctxt->user = buffer_get_string(m, NULL); | |
824 | sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); | |
825 | sshpam_authok = NULL; | |
826 | buffer_clear(m); | |
827 | if (sshpam_ctxt != NULL) { | |
828 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); | |
829 | buffer_put_int(m, 1); | |
830 | } else { | |
831 | buffer_put_int(m, 0); | |
832 | } | |
833 | mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m); | |
834 | return (0); | |
835 | } | |
836 | ||
837 | int | |
838 | mm_answer_pam_query(int socket, Buffer *m) | |
839 | { | |
840 | char *name, *info, **prompts; | |
841 | u_int num, *echo_on; | |
842 | int i, ret; | |
843 | ||
844 | debug3("%s", __func__); | |
845 | sshpam_authok = NULL; | |
846 | ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); | |
847 | if (ret == 0 && num == 0) | |
848 | sshpam_authok = sshpam_ctxt; | |
849 | if (num > 1 || name == NULL || info == NULL) | |
850 | ret = -1; | |
851 | buffer_clear(m); | |
852 | buffer_put_int(m, ret); | |
853 | buffer_put_cstring(m, name); | |
854 | xfree(name); | |
855 | buffer_put_cstring(m, info); | |
856 | xfree(info); | |
857 | buffer_put_int(m, num); | |
858 | for (i = 0; i < num; ++i) { | |
859 | buffer_put_cstring(m, prompts[i]); | |
860 | xfree(prompts[i]); | |
861 | buffer_put_int(m, echo_on[i]); | |
862 | } | |
863 | if (prompts != NULL) | |
864 | xfree(prompts); | |
865 | if (echo_on != NULL) | |
866 | xfree(echo_on); | |
867 | mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m); | |
868 | return (0); | |
869 | } | |
870 | ||
871 | int | |
872 | mm_answer_pam_respond(int socket, Buffer *m) | |
873 | { | |
874 | char **resp; | |
875 | u_int num; | |
876 | int i, ret; | |
877 | ||
878 | debug3("%s", __func__); | |
879 | sshpam_authok = NULL; | |
880 | num = buffer_get_int(m); | |
881 | if (num > 0) { | |
882 | resp = xmalloc(num * sizeof(char *)); | |
883 | for (i = 0; i < num; ++i) | |
884 | resp[i] = buffer_get_string(m, NULL); | |
885 | ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); | |
886 | for (i = 0; i < num; ++i) | |
887 | xfree(resp[i]); | |
888 | xfree(resp); | |
889 | } else { | |
890 | ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); | |
891 | } | |
892 | buffer_clear(m); | |
893 | buffer_put_int(m, ret); | |
894 | mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m); | |
895 | auth_method = "keyboard-interactive/pam"; | |
896 | if (ret == 0) | |
897 | sshpam_authok = sshpam_ctxt; | |
898 | return (0); | |
899 | } | |
900 | ||
901 | int | |
902 | mm_answer_pam_free_ctx(int socket, Buffer *m) | |
903 | { | |
904 | ||
905 | debug3("%s", __func__); | |
906 | (sshpam_device.free_ctx)(sshpam_ctxt); | |
907 | buffer_clear(m); | |
908 | mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m); | |
909 | return (sshpam_authok == sshpam_ctxt); | |
910 | } | |
911 | #endif | |
912 | ||
913 | static void | |
914 | mm_append_debug(Buffer *m) | |
915 | { | |
916 | if (auth_debug_init && buffer_len(&auth_debug)) { | |
917 | debug3("%s: Appending debug messages for child", __func__); | |
918 | buffer_append(m, buffer_ptr(&auth_debug), | |
919 | buffer_len(&auth_debug)); | |
920 | buffer_clear(&auth_debug); | |
921 | } | |
922 | } | |
923 | ||
924 | int | |
925 | mm_answer_keyallowed(int socket, Buffer *m) | |
926 | { | |
927 | Key *key; | |
928 | char *cuser, *chost; | |
929 | u_char *blob; | |
930 | u_int bloblen; | |
931 | enum mm_keytype type = 0; | |
932 | int allowed = 0; | |
933 | ||
934 | debug3("%s entering", __func__); | |
935 | ||
936 | type = buffer_get_int(m); | |
937 | cuser = buffer_get_string(m, NULL); | |
938 | chost = buffer_get_string(m, NULL); | |
939 | blob = buffer_get_string(m, &bloblen); | |
940 | ||
941 | key = key_from_blob(blob, bloblen); | |
942 | ||
943 | if ((compat20 && type == MM_RSAHOSTKEY) || | |
944 | (!compat20 && type != MM_RSAHOSTKEY)) | |
945 | fatal("%s: key type and protocol mismatch", __func__); | |
946 | ||
947 | debug3("%s: key_from_blob: %p", __func__, key); | |
948 | ||
949 | if (key != NULL && authctxt->valid) { | |
950 | switch(type) { | |
951 | case MM_USERKEY: | |
952 | allowed = options.pubkey_authentication && | |
953 | user_key_allowed(authctxt->pw, key); | |
954 | break; | |
955 | case MM_HOSTKEY: | |
956 | allowed = options.hostbased_authentication && | |
957 | hostbased_key_allowed(authctxt->pw, | |
958 | cuser, chost, key); | |
959 | break; | |
960 | case MM_RSAHOSTKEY: | |
961 | key->type = KEY_RSA1; /* XXX */ | |
962 | allowed = options.rhosts_rsa_authentication && | |
963 | auth_rhosts_rsa_key_allowed(authctxt->pw, | |
964 | cuser, chost, key); | |
965 | break; | |
966 | default: | |
967 | fatal("%s: unknown key type %d", __func__, type); | |
968 | break; | |
969 | } | |
970 | } | |
971 | if (key != NULL) | |
972 | key_free(key); | |
973 | ||
974 | /* clear temporarily storage (used by verify) */ | |
975 | monitor_reset_key_state(); | |
976 | ||
977 | if (allowed) { | |
978 | /* Save temporarily for comparison in verify */ | |
979 | key_blob = blob; | |
980 | key_bloblen = bloblen; | |
981 | key_blobtype = type; | |
982 | hostbased_cuser = cuser; | |
983 | hostbased_chost = chost; | |
984 | } | |
985 | ||
986 | debug3("%s: key %p is %s", | |
987 | __func__, key, allowed ? "allowed" : "disallowed"); | |
988 | ||
989 | buffer_clear(m); | |
990 | buffer_put_int(m, allowed); | |
991 | buffer_put_int(m, forced_command != NULL); | |
992 | ||
993 | mm_append_debug(m); | |
994 | ||
995 | mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m); | |
996 | ||
997 | if (type == MM_RSAHOSTKEY) | |
998 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); | |
999 | ||
1000 | return (0); | |
1001 | } | |
1002 | ||
1003 | static int | |
1004 | monitor_valid_userblob(u_char *data, u_int datalen) | |
1005 | { | |
1006 | Buffer b; | |
1007 | char *p; | |
1008 | u_int len; | |
1009 | int fail = 0; | |
1010 | ||
1011 | buffer_init(&b); | |
1012 | buffer_append(&b, data, datalen); | |
1013 | ||
1014 | if (datafellows & SSH_OLD_SESSIONID) { | |
1015 | p = buffer_ptr(&b); | |
1016 | len = buffer_len(&b); | |
1017 | if ((session_id2 == NULL) || | |
1018 | (len < session_id2_len) || | |
1019 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1020 | fail++; | |
1021 | buffer_consume(&b, session_id2_len); | |
1022 | } else { | |
1023 | p = buffer_get_string(&b, &len); | |
1024 | if ((session_id2 == NULL) || | |
1025 | (len != session_id2_len) || | |
1026 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1027 | fail++; | |
1028 | xfree(p); | |
1029 | } | |
1030 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) | |
1031 | fail++; | |
1032 | p = buffer_get_string(&b, NULL); | |
1033 | if (strcmp(authctxt->user, p) != 0) { | |
1034 | logit("wrong user name passed to monitor: expected %s != %.100s", | |
1035 | authctxt->user, p); | |
1036 | fail++; | |
1037 | } | |
1038 | xfree(p); | |
1039 | buffer_skip_string(&b); | |
1040 | if (datafellows & SSH_BUG_PKAUTH) { | |
1041 | if (!buffer_get_char(&b)) | |
1042 | fail++; | |
1043 | } else { | |
1044 | p = buffer_get_string(&b, NULL); | |
1045 | if (strcmp("publickey", p) != 0) | |
1046 | fail++; | |
1047 | xfree(p); | |
1048 | if (!buffer_get_char(&b)) | |
1049 | fail++; | |
1050 | buffer_skip_string(&b); | |
1051 | } | |
1052 | buffer_skip_string(&b); | |
1053 | if (buffer_len(&b) != 0) | |
1054 | fail++; | |
1055 | buffer_free(&b); | |
1056 | return (fail == 0); | |
1057 | } | |
1058 | ||
1059 | static int | |
1060 | monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, | |
1061 | char *chost) | |
1062 | { | |
1063 | Buffer b; | |
1064 | char *p; | |
1065 | u_int len; | |
1066 | int fail = 0; | |
1067 | ||
1068 | buffer_init(&b); | |
1069 | buffer_append(&b, data, datalen); | |
1070 | ||
1071 | p = buffer_get_string(&b, &len); | |
1072 | if ((session_id2 == NULL) || | |
1073 | (len != session_id2_len) || | |
1074 | (memcmp(p, session_id2, session_id2_len) != 0)) | |
1075 | fail++; | |
1076 | xfree(p); | |
1077 | ||
1078 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) | |
1079 | fail++; | |
1080 | p = buffer_get_string(&b, NULL); | |
1081 | if (strcmp(authctxt->user, p) != 0) { | |
1082 | logit("wrong user name passed to monitor: expected %s != %.100s", | |
1083 | authctxt->user, p); | |
1084 | fail++; | |
1085 | } | |
1086 | xfree(p); | |
1087 | buffer_skip_string(&b); /* service */ | |
1088 | p = buffer_get_string(&b, NULL); | |
1089 | if (strcmp(p, "hostbased") != 0) | |
1090 | fail++; | |
1091 | xfree(p); | |
1092 | buffer_skip_string(&b); /* pkalg */ | |
1093 | buffer_skip_string(&b); /* pkblob */ | |
1094 | ||
1095 | /* verify client host, strip trailing dot if necessary */ | |
1096 | p = buffer_get_string(&b, NULL); | |
1097 | if (((len = strlen(p)) > 0) && p[len - 1] == '.') | |
1098 | p[len - 1] = '\0'; | |
1099 | if (strcmp(p, chost) != 0) | |
1100 | fail++; | |
1101 | xfree(p); | |
1102 | ||
1103 | /* verify client user */ | |
1104 | p = buffer_get_string(&b, NULL); | |
1105 | if (strcmp(p, cuser) != 0) | |
1106 | fail++; | |
1107 | xfree(p); | |
1108 | ||
1109 | if (buffer_len(&b) != 0) | |
1110 | fail++; | |
1111 | buffer_free(&b); | |
1112 | return (fail == 0); | |
1113 | } | |
1114 | ||
1115 | int | |
1116 | mm_answer_keyverify(int socket, Buffer *m) | |
1117 | { | |
1118 | Key *key; | |
1119 | u_char *signature, *data, *blob; | |
1120 | u_int signaturelen, datalen, bloblen; | |
1121 | int verified = 0; | |
1122 | int valid_data = 0; | |
1123 | ||
1124 | blob = buffer_get_string(m, &bloblen); | |
1125 | signature = buffer_get_string(m, &signaturelen); | |
1126 | data = buffer_get_string(m, &datalen); | |
1127 | ||
1128 | if (hostbased_cuser == NULL || hostbased_chost == NULL || | |
1129 | !monitor_allowed_key(blob, bloblen)) | |
1130 | fatal("%s: bad key, not previously allowed", __func__); | |
1131 | ||
1132 | key = key_from_blob(blob, bloblen); | |
1133 | if (key == NULL) | |
1134 | fatal("%s: bad public key blob", __func__); | |
1135 | ||
1136 | switch (key_blobtype) { | |
1137 | case MM_USERKEY: | |
1138 | valid_data = monitor_valid_userblob(data, datalen); | |
1139 | break; | |
1140 | case MM_HOSTKEY: | |
1141 | valid_data = monitor_valid_hostbasedblob(data, datalen, | |
1142 | hostbased_cuser, hostbased_chost); | |
1143 | break; | |
1144 | default: | |
1145 | valid_data = 0; | |
1146 | break; | |
1147 | } | |
1148 | if (!valid_data) | |
1149 | fatal("%s: bad signature data blob", __func__); | |
1150 | ||
1151 | verified = key_verify(key, signature, signaturelen, data, datalen); | |
1152 | debug3("%s: key %p signature %s", | |
1153 | __func__, key, verified ? "verified" : "unverified"); | |
1154 | ||
1155 | key_free(key); | |
1156 | xfree(blob); | |
1157 | xfree(signature); | |
1158 | xfree(data); | |
1159 | ||
1160 | auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; | |
1161 | ||
1162 | monitor_reset_key_state(); | |
1163 | ||
1164 | buffer_clear(m); | |
1165 | buffer_put_int(m, verified); | |
1166 | mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m); | |
1167 | ||
1168 | return (verified); | |
1169 | } | |
1170 | ||
1171 | static void | |
1172 | mm_record_login(Session *s, struct passwd *pw) | |
1173 | { | |
1174 | socklen_t fromlen; | |
1175 | struct sockaddr_storage from; | |
1176 | ||
1177 | /* | |
1178 | * Get IP address of client. If the connection is not a socket, let | |
1179 | * the address be 0.0.0.0. | |
1180 | */ | |
1181 | memset(&from, 0, sizeof(from)); | |
1182 | fromlen = sizeof(from); | |
1183 | if (packet_connection_is_on_socket()) { | |
1184 | if (getpeername(packet_get_connection_in(), | |
1185 | (struct sockaddr *) & from, &fromlen) < 0) { | |
1186 | debug("getpeername: %.100s", strerror(errno)); | |
1187 | cleanup_exit(255); | |
1188 | } | |
1189 | } | |
1190 | /* Record that there was a login on that tty from the remote host. */ | |
1191 | record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, | |
1192 | get_remote_name_or_ip(utmp_len, options.use_dns), | |
1193 | (struct sockaddr *)&from, fromlen); | |
1194 | } | |
1195 | ||
1196 | static void | |
1197 | mm_session_close(Session *s) | |
1198 | { | |
1199 | debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); | |
1200 | if (s->ttyfd != -1) { | |
1201 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); | |
1202 | session_pty_cleanup2(s); | |
1203 | } | |
1204 | s->used = 0; | |
1205 | } | |
1206 | ||
1207 | int | |
1208 | mm_answer_pty(int socket, Buffer *m) | |
1209 | { | |
1210 | extern struct monitor *pmonitor; | |
1211 | Session *s; | |
1212 | int res, fd0; | |
1213 | ||
1214 | debug3("%s entering", __func__); | |
1215 | ||
1216 | buffer_clear(m); | |
1217 | s = session_new(); | |
1218 | if (s == NULL) | |
1219 | goto error; | |
1220 | s->authctxt = authctxt; | |
1221 | s->pw = authctxt->pw; | |
1222 | s->pid = pmonitor->m_pid; | |
1223 | res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); | |
1224 | if (res == 0) | |
1225 | goto error; | |
1226 | pty_setowner(authctxt->pw, s->tty); | |
1227 | ||
1228 | buffer_put_int(m, 1); | |
1229 | buffer_put_cstring(m, s->tty); | |
1230 | mm_request_send(socket, MONITOR_ANS_PTY, m); | |
1231 | ||
1232 | mm_send_fd(socket, s->ptyfd); | |
1233 | mm_send_fd(socket, s->ttyfd); | |
1234 | ||
1235 | /* We need to trick ttyslot */ | |
1236 | if (dup2(s->ttyfd, 0) == -1) | |
1237 | fatal("%s: dup2", __func__); | |
1238 | ||
1239 | mm_record_login(s, authctxt->pw); | |
1240 | ||
1241 | /* Now we can close the file descriptor again */ | |
1242 | close(0); | |
1243 | ||
1244 | /* make sure nothing uses fd 0 */ | |
1245 | if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) | |
1246 | fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); | |
1247 | if (fd0 != 0) | |
1248 | error("%s: fd0 %d != 0", __func__, fd0); | |
1249 | ||
1250 | /* slave is not needed */ | |
1251 | close(s->ttyfd); | |
1252 | s->ttyfd = s->ptyfd; | |
1253 | /* no need to dup() because nobody closes ptyfd */ | |
1254 | s->ptymaster = s->ptyfd; | |
1255 | ||
1256 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); | |
1257 | ||
1258 | return (0); | |
1259 | ||
1260 | error: | |
1261 | if (s != NULL) | |
1262 | mm_session_close(s); | |
1263 | buffer_put_int(m, 0); | |
1264 | mm_request_send(socket, MONITOR_ANS_PTY, m); | |
1265 | return (0); | |
1266 | } | |
1267 | ||
1268 | int | |
1269 | mm_answer_pty_cleanup(int socket, Buffer *m) | |
1270 | { | |
1271 | Session *s; | |
1272 | char *tty; | |
1273 | ||
1274 | debug3("%s entering", __func__); | |
1275 | ||
1276 | tty = buffer_get_string(m, NULL); | |
1277 | if ((s = session_by_tty(tty)) != NULL) | |
1278 | mm_session_close(s); | |
1279 | buffer_clear(m); | |
1280 | xfree(tty); | |
1281 | return (0); | |
1282 | } | |
1283 | ||
1284 | int | |
1285 | mm_answer_sesskey(int socket, Buffer *m) | |
1286 | { | |
1287 | BIGNUM *p; | |
1288 | int rsafail; | |
1289 | ||
1290 | /* Turn off permissions */ | |
1291 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); | |
1292 | ||
1293 | if ((p = BN_new()) == NULL) | |
1294 | fatal("%s: BN_new", __func__); | |
1295 | ||
1296 | buffer_get_bignum2(m, p); | |
1297 | ||
1298 | rsafail = ssh1_session_key(p); | |
1299 | ||
1300 | buffer_clear(m); | |
1301 | buffer_put_int(m, rsafail); | |
1302 | buffer_put_bignum2(m, p); | |
1303 | ||
1304 | BN_clear_free(p); | |
1305 | ||
1306 | mm_request_send(socket, MONITOR_ANS_SESSKEY, m); | |
1307 | ||
1308 | /* Turn on permissions for sessid passing */ | |
1309 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); | |
1310 | ||
1311 | return (0); | |
1312 | } | |
1313 | ||
1314 | int | |
1315 | mm_answer_sessid(int socket, Buffer *m) | |
1316 | { | |
1317 | int i; | |
1318 | ||
1319 | debug3("%s entering", __func__); | |
1320 | ||
1321 | if (buffer_len(m) != 16) | |
1322 | fatal("%s: bad ssh1 session id", __func__); | |
1323 | for (i = 0; i < 16; i++) | |
1324 | session_id[i] = buffer_get_char(m); | |
1325 | ||
1326 | /* Turn on permissions for getpwnam */ | |
1327 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); | |
1328 | ||
1329 | return (0); | |
1330 | } | |
1331 | ||
1332 | int | |
1333 | mm_answer_rsa_keyallowed(int socket, Buffer *m) | |
1334 | { | |
1335 | BIGNUM *client_n; | |
1336 | Key *key = NULL; | |
1337 | u_char *blob = NULL; | |
1338 | u_int blen = 0; | |
1339 | int allowed = 0; | |
1340 | ||
1341 | debug3("%s entering", __func__); | |
1342 | ||
1343 | if (options.rsa_authentication && authctxt->valid) { | |
1344 | if ((client_n = BN_new()) == NULL) | |
1345 | fatal("%s: BN_new", __func__); | |
1346 | buffer_get_bignum2(m, client_n); | |
1347 | allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); | |
1348 | BN_clear_free(client_n); | |
1349 | } | |
1350 | buffer_clear(m); | |
1351 | buffer_put_int(m, allowed); | |
1352 | buffer_put_int(m, forced_command != NULL); | |
1353 | ||
1354 | /* clear temporarily storage (used by generate challenge) */ | |
1355 | monitor_reset_key_state(); | |
1356 | ||
1357 | if (allowed && key != NULL) { | |
1358 | key->type = KEY_RSA; /* cheat for key_to_blob */ | |
1359 | if (key_to_blob(key, &blob, &blen) == 0) | |
1360 | fatal("%s: key_to_blob failed", __func__); | |
1361 | buffer_put_string(m, blob, blen); | |
1362 | ||
1363 | /* Save temporarily for comparison in verify */ | |
1364 | key_blob = blob; | |
1365 | key_bloblen = blen; | |
1366 | key_blobtype = MM_RSAUSERKEY; | |
1367 | } | |
1368 | if (key != NULL) | |
1369 | key_free(key); | |
1370 | ||
1371 | mm_append_debug(m); | |
1372 | ||
1373 | mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m); | |
1374 | ||
1375 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); | |
1376 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); | |
1377 | return (0); | |
1378 | } | |
1379 | ||
1380 | int | |
1381 | mm_answer_rsa_challenge(int socket, Buffer *m) | |
1382 | { | |
1383 | Key *key = NULL; | |
1384 | u_char *blob; | |
1385 | u_int blen; | |
1386 | ||
1387 | debug3("%s entering", __func__); | |
1388 | ||
1389 | if (!authctxt->valid) | |
1390 | fatal("%s: authctxt not valid", __func__); | |
1391 | blob = buffer_get_string(m, &blen); | |
1392 | if (!monitor_allowed_key(blob, blen)) | |
1393 | fatal("%s: bad key, not previously allowed", __func__); | |
1394 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) | |
1395 | fatal("%s: key type mismatch", __func__); | |
1396 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1397 | fatal("%s: received bad key", __func__); | |
1398 | ||
1399 | if (ssh1_challenge) | |
1400 | BN_clear_free(ssh1_challenge); | |
1401 | ssh1_challenge = auth_rsa_generate_challenge(key); | |
1402 | ||
1403 | buffer_clear(m); | |
1404 | buffer_put_bignum2(m, ssh1_challenge); | |
1405 | ||
1406 | debug3("%s sending reply", __func__); | |
1407 | mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m); | |
1408 | ||
1409 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); | |
1410 | ||
1411 | xfree(blob); | |
1412 | key_free(key); | |
1413 | return (0); | |
1414 | } | |
1415 | ||
1416 | int | |
1417 | mm_answer_rsa_response(int socket, Buffer *m) | |
1418 | { | |
1419 | Key *key = NULL; | |
1420 | u_char *blob, *response; | |
1421 | u_int blen, len; | |
1422 | int success; | |
1423 | ||
1424 | debug3("%s entering", __func__); | |
1425 | ||
1426 | if (!authctxt->valid) | |
1427 | fatal("%s: authctxt not valid", __func__); | |
1428 | if (ssh1_challenge == NULL) | |
1429 | fatal("%s: no ssh1_challenge", __func__); | |
1430 | ||
1431 | blob = buffer_get_string(m, &blen); | |
1432 | if (!monitor_allowed_key(blob, blen)) | |
1433 | fatal("%s: bad key, not previously allowed", __func__); | |
1434 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) | |
1435 | fatal("%s: key type mismatch: %d", __func__, key_blobtype); | |
1436 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1437 | fatal("%s: received bad key", __func__); | |
1438 | response = buffer_get_string(m, &len); | |
1439 | if (len != 16) | |
1440 | fatal("%s: received bad response to challenge", __func__); | |
1441 | success = auth_rsa_verify_response(key, ssh1_challenge, response); | |
1442 | ||
1443 | xfree(blob); | |
1444 | key_free(key); | |
1445 | xfree(response); | |
1446 | ||
1447 | auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; | |
1448 | ||
1449 | /* reset state */ | |
1450 | BN_clear_free(ssh1_challenge); | |
1451 | ssh1_challenge = NULL; | |
1452 | monitor_reset_key_state(); | |
1453 | ||
1454 | buffer_clear(m); | |
1455 | buffer_put_int(m, success); | |
1456 | mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m); | |
1457 | ||
1458 | return (success); | |
1459 | } | |
1460 | ||
1461 | int | |
1462 | mm_answer_term(int socket, Buffer *req) | |
1463 | { | |
1464 | extern struct monitor *pmonitor; | |
1465 | int res, status; | |
1466 | ||
1467 | debug3("%s: tearing down sessions", __func__); | |
1468 | ||
1469 | /* The child is terminating */ | |
1470 | session_destroy_all(&mm_session_close); | |
1471 | ||
1472 | while (waitpid(pmonitor->m_pid, &status, 0) == -1) | |
1473 | if (errno != EINTR) | |
1474 | exit(1); | |
1475 | ||
1476 | res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; | |
1477 | ||
1478 | /* Terminate process */ | |
1479 | exit (res); | |
1480 | } | |
1481 | ||
1482 | void | |
1483 | monitor_apply_keystate(struct monitor *pmonitor) | |
1484 | { | |
1485 | if (compat20) { | |
1486 | set_newkeys(MODE_IN); | |
1487 | set_newkeys(MODE_OUT); | |
1488 | } else { | |
1489 | packet_set_protocol_flags(child_state.ssh1protoflags); | |
1490 | packet_set_encryption_key(child_state.ssh1key, | |
1491 | child_state.ssh1keylen, child_state.ssh1cipher); | |
1492 | xfree(child_state.ssh1key); | |
1493 | } | |
1494 | ||
1495 | /* for rc4 and other stateful ciphers */ | |
1496 | packet_set_keycontext(MODE_OUT, child_state.keyout); | |
1497 | xfree(child_state.keyout); | |
1498 | packet_set_keycontext(MODE_IN, child_state.keyin); | |
1499 | xfree(child_state.keyin); | |
1500 | ||
1501 | if (!compat20) { | |
1502 | packet_set_iv(MODE_OUT, child_state.ivout); | |
1503 | xfree(child_state.ivout); | |
1504 | packet_set_iv(MODE_IN, child_state.ivin); | |
1505 | xfree(child_state.ivin); | |
1506 | } | |
1507 | ||
1508 | memcpy(&incoming_stream, &child_state.incoming, | |
1509 | sizeof(incoming_stream)); | |
1510 | memcpy(&outgoing_stream, &child_state.outgoing, | |
1511 | sizeof(outgoing_stream)); | |
1512 | ||
1513 | /* Update with new address */ | |
1514 | if (options.compression) | |
1515 | mm_init_compression(pmonitor->m_zlib); | |
1516 | ||
1517 | /* Network I/O buffers */ | |
1518 | /* XXX inefficient for large buffers, need: buffer_init_from_string */ | |
1519 | buffer_clear(&input); | |
1520 | buffer_append(&input, child_state.input, child_state.ilen); | |
1521 | memset(child_state.input, 0, child_state.ilen); | |
1522 | xfree(child_state.input); | |
1523 | ||
1524 | buffer_clear(&output); | |
1525 | buffer_append(&output, child_state.output, child_state.olen); | |
1526 | memset(child_state.output, 0, child_state.olen); | |
1527 | xfree(child_state.output); | |
1528 | } | |
1529 | ||
1530 | static Kex * | |
1531 | mm_get_kex(Buffer *m) | |
1532 | { | |
1533 | Kex *kex; | |
1534 | void *blob; | |
1535 | u_int bloblen; | |
1536 | ||
1537 | kex = xmalloc(sizeof(*kex)); | |
1538 | memset(kex, 0, sizeof(*kex)); | |
1539 | kex->session_id = buffer_get_string(m, &kex->session_id_len); | |
1540 | if ((session_id2 == NULL) || | |
1541 | (kex->session_id_len != session_id2_len) || | |
1542 | (memcmp(kex->session_id, session_id2, session_id2_len) != 0)) | |
1543 | fatal("mm_get_get: internal error: bad session id"); | |
1544 | kex->we_need = buffer_get_int(m); | |
1545 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; | |
1546 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; | |
1547 | kex->server = 1; | |
1548 | kex->hostkey_type = buffer_get_int(m); | |
1549 | kex->kex_type = buffer_get_int(m); | |
1550 | blob = buffer_get_string(m, &bloblen); | |
1551 | buffer_init(&kex->my); | |
1552 | buffer_append(&kex->my, blob, bloblen); | |
1553 | xfree(blob); | |
1554 | blob = buffer_get_string(m, &bloblen); | |
1555 | buffer_init(&kex->peer); | |
1556 | buffer_append(&kex->peer, blob, bloblen); | |
1557 | xfree(blob); | |
1558 | kex->done = 1; | |
1559 | kex->flags = buffer_get_int(m); | |
1560 | kex->client_version_string = buffer_get_string(m, NULL); | |
1561 | kex->server_version_string = buffer_get_string(m, NULL); | |
1562 | kex->load_host_key=&get_hostkey_by_type; | |
1563 | kex->host_key_index=&get_hostkey_index; | |
1564 | ||
1565 | return (kex); | |
1566 | } | |
1567 | ||
1568 | /* This function requries careful sanity checking */ | |
1569 | ||
1570 | void | |
1571 | mm_get_keystate(struct monitor *pmonitor) | |
1572 | { | |
1573 | Buffer m; | |
1574 | u_char *blob, *p; | |
1575 | u_int bloblen, plen; | |
1576 | u_int32_t seqnr, packets; | |
1577 | u_int64_t blocks; | |
1578 | ||
1579 | debug3("%s: Waiting for new keys", __func__); | |
1580 | ||
1581 | buffer_init(&m); | |
1582 | mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m); | |
1583 | if (!compat20) { | |
1584 | child_state.ssh1protoflags = buffer_get_int(&m); | |
1585 | child_state.ssh1cipher = buffer_get_int(&m); | |
1586 | child_state.ssh1key = buffer_get_string(&m, | |
1587 | &child_state.ssh1keylen); | |
1588 | child_state.ivout = buffer_get_string(&m, | |
1589 | &child_state.ivoutlen); | |
1590 | child_state.ivin = buffer_get_string(&m, &child_state.ivinlen); | |
1591 | goto skip; | |
1592 | } else { | |
1593 | /* Get the Kex for rekeying */ | |
1594 | *pmonitor->m_pkex = mm_get_kex(&m); | |
1595 | } | |
1596 | ||
1597 | blob = buffer_get_string(&m, &bloblen); | |
1598 | current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen); | |
1599 | xfree(blob); | |
1600 | ||
1601 | debug3("%s: Waiting for second key", __func__); | |
1602 | blob = buffer_get_string(&m, &bloblen); | |
1603 | current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen); | |
1604 | xfree(blob); | |
1605 | ||
1606 | /* Now get sequence numbers for the packets */ | |
1607 | seqnr = buffer_get_int(&m); | |
1608 | blocks = buffer_get_int64(&m); | |
1609 | packets = buffer_get_int(&m); | |
1610 | packet_set_state(MODE_OUT, seqnr, blocks, packets); | |
1611 | seqnr = buffer_get_int(&m); | |
1612 | blocks = buffer_get_int64(&m); | |
1613 | packets = buffer_get_int(&m); | |
1614 | packet_set_state(MODE_IN, seqnr, blocks, packets); | |
1615 | ||
1616 | skip: | |
1617 | /* Get the key context */ | |
1618 | child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen); | |
1619 | child_state.keyin = buffer_get_string(&m, &child_state.keyinlen); | |
1620 | ||
1621 | debug3("%s: Getting compression state", __func__); | |
1622 | /* Get compression state */ | |
1623 | p = buffer_get_string(&m, &plen); | |
1624 | if (plen != sizeof(child_state.outgoing)) | |
1625 | fatal("%s: bad request size", __func__); | |
1626 | memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing)); | |
1627 | xfree(p); | |
1628 | ||
1629 | p = buffer_get_string(&m, &plen); | |
1630 | if (plen != sizeof(child_state.incoming)) | |
1631 | fatal("%s: bad request size", __func__); | |
1632 | memcpy(&child_state.incoming, p, sizeof(child_state.incoming)); | |
1633 | xfree(p); | |
1634 | ||
1635 | /* Network I/O buffers */ | |
1636 | debug3("%s: Getting Network I/O buffers", __func__); | |
1637 | child_state.input = buffer_get_string(&m, &child_state.ilen); | |
1638 | child_state.output = buffer_get_string(&m, &child_state.olen); | |
1639 | ||
1640 | buffer_free(&m); | |
1641 | } | |
1642 | ||
1643 | ||
1644 | /* Allocation functions for zlib */ | |
1645 | void * | |
1646 | mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) | |
1647 | { | |
1648 | size_t len = (size_t) size * ncount; | |
1649 | void *address; | |
1650 | ||
1651 | if (len == 0 || ncount > SIZE_T_MAX / size) | |
1652 | fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); | |
1653 | ||
1654 | address = mm_malloc(mm, len); | |
1655 | ||
1656 | return (address); | |
1657 | } | |
1658 | ||
1659 | void | |
1660 | mm_zfree(struct mm_master *mm, void *address) | |
1661 | { | |
1662 | mm_free(mm, address); | |
1663 | } | |
1664 | ||
1665 | void | |
1666 | mm_init_compression(struct mm_master *mm) | |
1667 | { | |
1668 | outgoing_stream.zalloc = (alloc_func)mm_zalloc; | |
1669 | outgoing_stream.zfree = (free_func)mm_zfree; | |
1670 | outgoing_stream.opaque = mm; | |
1671 | ||
1672 | incoming_stream.zalloc = (alloc_func)mm_zalloc; | |
1673 | incoming_stream.zfree = (free_func)mm_zfree; | |
1674 | incoming_stream.opaque = mm; | |
1675 | } | |
1676 | ||
1677 | /* XXX */ | |
1678 | ||
1679 | #define FD_CLOSEONEXEC(x) do { \ | |
1680 | if (fcntl(x, F_SETFD, 1) == -1) \ | |
1681 | fatal("fcntl(%d, F_SETFD)", x); \ | |
1682 | } while (0) | |
1683 | ||
1684 | static void | |
1685 | monitor_socketpair(int *pair) | |
1686 | { | |
1687 | #ifdef HAVE_SOCKETPAIR | |
1688 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) | |
1689 | fatal("%s: socketpair", __func__); | |
1690 | #else | |
1691 | fatal("%s: UsePrivilegeSeparation=yes not supported", | |
1692 | __func__); | |
1693 | #endif | |
1694 | FD_CLOSEONEXEC(pair[0]); | |
1695 | FD_CLOSEONEXEC(pair[1]); | |
1696 | } | |
1697 | ||
1698 | #define MM_MEMSIZE 65536 | |
1699 | ||
1700 | struct monitor * | |
1701 | monitor_init(void) | |
1702 | { | |
1703 | struct monitor *mon; | |
1704 | int pair[2]; | |
1705 | ||
1706 | mon = xmalloc(sizeof(*mon)); | |
1707 | ||
1708 | monitor_socketpair(pair); | |
1709 | ||
1710 | mon->m_recvfd = pair[0]; | |
1711 | mon->m_sendfd = pair[1]; | |
1712 | ||
1713 | /* Used to share zlib space across processes */ | |
1714 | if (options.compression) { | |
1715 | mon->m_zback = mm_create(NULL, MM_MEMSIZE); | |
1716 | mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); | |
1717 | ||
1718 | /* Compression needs to share state across borders */ | |
1719 | mm_init_compression(mon->m_zlib); | |
1720 | } | |
1721 | ||
1722 | return mon; | |
1723 | } | |
1724 | ||
1725 | void | |
1726 | monitor_reinit(struct monitor *mon) | |
1727 | { | |
1728 | int pair[2]; | |
1729 | ||
1730 | monitor_socketpair(pair); | |
1731 | ||
1732 | mon->m_recvfd = pair[0]; | |
1733 | mon->m_sendfd = pair[1]; | |
1734 | } | |
1735 | ||
1736 | #ifdef GSSAPI | |
1737 | int | |
1738 | mm_answer_gss_setup_ctx(int socket, Buffer *m) | |
1739 | { | |
1740 | gss_OID_desc oid; | |
1741 | OM_uint32 major; | |
1742 | u_int len; | |
1743 | ||
1744 | oid.elements = buffer_get_string(m, &len); | |
1745 | oid.length = len; | |
1746 | ||
1747 | major = ssh_gssapi_server_ctx(&gsscontext, &oid); | |
1748 | ||
1749 | xfree(oid.elements); | |
1750 | ||
1751 | buffer_clear(m); | |
1752 | buffer_put_int(m, major); | |
1753 | ||
1754 | mm_request_send(socket,MONITOR_ANS_GSSSETUP, m); | |
1755 | ||
1756 | /* Now we have a context, enable the step */ | |
1757 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); | |
1758 | ||
1759 | return (0); | |
1760 | } | |
1761 | ||
1762 | int | |
1763 | mm_answer_gss_accept_ctx(int socket, Buffer *m) | |
1764 | { | |
1765 | gss_buffer_desc in; | |
1766 | gss_buffer_desc out = GSS_C_EMPTY_BUFFER; | |
1767 | OM_uint32 major,minor; | |
1768 | OM_uint32 flags = 0; /* GSI needs this */ | |
1769 | u_int len; | |
1770 | ||
1771 | in.value = buffer_get_string(m, &len); | |
1772 | in.length = len; | |
1773 | major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); | |
1774 | xfree(in.value); | |
1775 | ||
1776 | buffer_clear(m); | |
1777 | buffer_put_int(m, major); | |
1778 | buffer_put_string(m, out.value, out.length); | |
1779 | buffer_put_int(m, flags); | |
1780 | mm_request_send(socket, MONITOR_ANS_GSSSTEP, m); | |
1781 | ||
1782 | gss_release_buffer(&minor, &out); | |
1783 | ||
1784 | /* Complete - now we can do signing */ | |
1785 | if (major==GSS_S_COMPLETE) { | |
1786 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); | |
1787 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); | |
1788 | } | |
1789 | return (0); | |
1790 | } | |
1791 | ||
1792 | int | |
1793 | mm_answer_gss_userok(int socket, Buffer *m) | |
1794 | { | |
1795 | int authenticated; | |
1796 | ||
1797 | authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); | |
1798 | ||
1799 | buffer_clear(m); | |
1800 | buffer_put_int(m, authenticated); | |
1801 | ||
1802 | debug3("%s: sending result %d", __func__, authenticated); | |
1803 | mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m); | |
1804 | ||
1805 | auth_method="gssapi"; | |
1806 | ||
1807 | /* Monitor loop will terminate if authenticated */ | |
1808 | return (authenticated); | |
1809 | } | |
1810 | #endif /* GSSAPI */ |