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