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