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