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