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