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