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