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