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