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