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