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