]> andersk Git - openssh.git/blob - monitor_wrap.c
- stevesk@cvs.openbsd.org 2006/08/01 23:22:48
[openssh.git] / monitor_wrap.c
1 /* $OpenBSD: monitor_wrap.c,v 1.49 2006/08/01 23:22:47 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
32 #include <openssl/bn.h>
33 #include <openssl/dh.h>
34
35 #include <errno.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "ssh.h"
41 #include "dh.h"
42 #include "kex.h"
43 #include "auth.h"
44 #include "auth-options.h"
45 #include "buffer.h"
46 #include "bufaux.h"
47 #include "packet.h"
48 #include "mac.h"
49 #include "log.h"
50 #ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
51 #undef TARGET_OS_MAC
52 #include "zlib.h"
53 #define TARGET_OS_MAC 1
54 #else
55 #include "zlib.h"
56 #endif
57 #include "monitor.h"
58 #include "monitor_wrap.h"
59 #include "xmalloc.h"
60 #include "atomicio.h"
61 #include "monitor_fdpass.h"
62 #include "misc.h"
63 #include "servconf.h"
64
65 #include "auth.h"
66 #include "channels.h"
67 #include "session.h"
68
69 #ifdef GSSAPI
70 #include "ssh-gss.h"
71 #endif
72
73 /* Imports */
74 extern int compat20;
75 extern Newkeys *newkeys[];
76 extern z_stream incoming_stream;
77 extern z_stream outgoing_stream;
78 extern struct monitor *pmonitor;
79 extern Buffer input, output;
80 extern Buffer loginmsg;
81 extern ServerOptions options;
82
83 int
84 mm_is_monitor(void)
85 {
86         /*
87          * m_pid is only set in the privileged part, and
88          * points to the unprivileged child.
89          */
90         return (pmonitor && pmonitor->m_pid > 0);
91 }
92
93 void
94 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
95 {
96         u_int mlen = buffer_len(m);
97         u_char buf[5];
98
99         debug3("%s entering: type %d", __func__, type);
100
101         put_u32(buf, mlen + 1);
102         buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
103         if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
104                 fatal("%s: write: %s", __func__, strerror(errno));
105         if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
106                 fatal("%s: write: %s", __func__, strerror(errno));
107 }
108
109 void
110 mm_request_receive(int sock, Buffer *m)
111 {
112         u_char buf[4];
113         u_int msg_len;
114
115         debug3("%s entering", __func__);
116
117         if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
118                 if (errno == EPIPE)
119                         cleanup_exit(255);
120                 fatal("%s: read: %s", __func__, strerror(errno));
121         }
122         msg_len = get_u32(buf);
123         if (msg_len > 256 * 1024)
124                 fatal("%s: read: bad msg_len %d", __func__, msg_len);
125         buffer_clear(m);
126         buffer_append_space(m, msg_len);
127         if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
128                 fatal("%s: read: %s", __func__, strerror(errno));
129 }
130
131 void
132 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
133 {
134         u_char rtype;
135
136         debug3("%s entering: type %d", __func__, type);
137
138         mm_request_receive(sock, m);
139         rtype = buffer_get_char(m);
140         if (rtype != type)
141                 fatal("%s: read: rtype %d != type %d", __func__,
142                     rtype, type);
143 }
144
145 DH *
146 mm_choose_dh(int min, int nbits, int max)
147 {
148         BIGNUM *p, *g;
149         int success = 0;
150         Buffer m;
151
152         buffer_init(&m);
153         buffer_put_int(&m, min);
154         buffer_put_int(&m, nbits);
155         buffer_put_int(&m, max);
156
157         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
158
159         debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
160         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
161
162         success = buffer_get_char(&m);
163         if (success == 0)
164                 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
165
166         if ((p = BN_new()) == NULL)
167                 fatal("%s: BN_new failed", __func__);
168         if ((g = BN_new()) == NULL)
169                 fatal("%s: BN_new failed", __func__);
170         buffer_get_bignum2(&m, p);
171         buffer_get_bignum2(&m, g);
172
173         debug3("%s: remaining %d", __func__, buffer_len(&m));
174         buffer_free(&m);
175
176         return (dh_new_group(g, p));
177 }
178
179 int
180 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
181 {
182         Kex *kex = *pmonitor->m_pkex;
183         Buffer m;
184
185         debug3("%s entering", __func__);
186
187         buffer_init(&m);
188         buffer_put_int(&m, kex->host_key_index(key));
189         buffer_put_string(&m, data, datalen);
190
191         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
192
193         debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
194         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
195         *sigp  = buffer_get_string(&m, lenp);
196         buffer_free(&m);
197
198         return (0);
199 }
200
201 struct passwd *
202 mm_getpwnamallow(const char *username)
203 {
204         Buffer m;
205         struct passwd *pw;
206         u_int pwlen;
207
208         debug3("%s entering", __func__);
209
210         buffer_init(&m);
211         buffer_put_cstring(&m, username);
212
213         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
214
215         debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
216         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
217
218         if (buffer_get_char(&m) == 0) {
219                 buffer_free(&m);
220                 return (NULL);
221         }
222         pw = buffer_get_string(&m, &pwlen);
223         if (pwlen != sizeof(struct passwd))
224                 fatal("%s: struct passwd size mismatch", __func__);
225         pw->pw_name = buffer_get_string(&m, NULL);
226         pw->pw_passwd = buffer_get_string(&m, NULL);
227         pw->pw_gecos = buffer_get_string(&m, NULL);
228 #ifdef HAVE_PW_CLASS_IN_PASSWD
229         pw->pw_class = buffer_get_string(&m, NULL);
230 #endif
231         pw->pw_dir = buffer_get_string(&m, NULL);
232         pw->pw_shell = buffer_get_string(&m, NULL);
233         buffer_free(&m);
234
235         return (pw);
236 }
237
238 char *
239 mm_auth2_read_banner(void)
240 {
241         Buffer m;
242         char *banner;
243
244         debug3("%s entering", __func__);
245
246         buffer_init(&m);
247         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
248         buffer_clear(&m);
249
250         mm_request_receive_expect(pmonitor->m_recvfd,
251             MONITOR_ANS_AUTH2_READ_BANNER, &m);
252         banner = buffer_get_string(&m, NULL);
253         buffer_free(&m);
254
255         /* treat empty banner as missing banner */
256         if (strlen(banner) == 0) {
257                 xfree(banner);
258                 banner = NULL;
259         }
260         return (banner);
261 }
262
263 /* Inform the privileged process about service and style */
264
265 void
266 mm_inform_authserv(char *service, char *style)
267 {
268         Buffer m;
269
270         debug3("%s entering", __func__);
271
272         buffer_init(&m);
273         buffer_put_cstring(&m, service);
274         buffer_put_cstring(&m, style ? style : "");
275
276         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
277
278         buffer_free(&m);
279 }
280
281 /* Do the password authentication */
282 int
283 mm_auth_password(Authctxt *authctxt, char *password)
284 {
285         Buffer m;
286         int authenticated = 0;
287
288         debug3("%s entering", __func__);
289
290         buffer_init(&m);
291         buffer_put_cstring(&m, password);
292         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
293
294         debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
295         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
296
297         authenticated = buffer_get_int(&m);
298
299         buffer_free(&m);
300
301         debug3("%s: user %sauthenticated",
302             __func__, authenticated ? "" : "not ");
303         return (authenticated);
304 }
305
306 int
307 mm_user_key_allowed(struct passwd *pw, Key *key)
308 {
309         return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
310 }
311
312 int
313 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
314     Key *key)
315 {
316         return (mm_key_allowed(MM_HOSTKEY, user, host, key));
317 }
318
319 int
320 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
321     char *host, Key *key)
322 {
323         int ret;
324
325         key->type = KEY_RSA; /* XXX hack for key_to_blob */
326         ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
327         key->type = KEY_RSA1;
328         return (ret);
329 }
330
331 static void
332 mm_send_debug(Buffer *m)
333 {
334         char *msg;
335
336         while (buffer_len(m)) {
337                 msg = buffer_get_string(m, NULL);
338                 debug3("%s: Sending debug: %s", __func__, msg);
339                 packet_send_debug("%s", msg);
340                 xfree(msg);
341         }
342 }
343
344 int
345 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
346 {
347         Buffer m;
348         u_char *blob;
349         u_int len;
350         int allowed = 0, have_forced = 0;
351
352         debug3("%s entering", __func__);
353
354         /* Convert the key to a blob and the pass it over */
355         if (!key_to_blob(key, &blob, &len))
356                 return (0);
357
358         buffer_init(&m);
359         buffer_put_int(&m, type);
360         buffer_put_cstring(&m, user ? user : "");
361         buffer_put_cstring(&m, host ? host : "");
362         buffer_put_string(&m, blob, len);
363         xfree(blob);
364
365         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
366
367         debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
368         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
369
370         allowed = buffer_get_int(&m);
371
372         /* fake forced command */
373         auth_clear_options();
374         have_forced = buffer_get_int(&m);
375         forced_command = have_forced ? xstrdup("true") : NULL;
376
377         /* Send potential debug messages */
378         mm_send_debug(&m);
379
380         buffer_free(&m);
381
382         return (allowed);
383 }
384
385 /*
386  * This key verify needs to send the key type along, because the
387  * privileged parent makes the decision if the key is allowed
388  * for authentication.
389  */
390
391 int
392 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
393 {
394         Buffer m;
395         u_char *blob;
396         u_int len;
397         int verified = 0;
398
399         debug3("%s entering", __func__);
400
401         /* Convert the key to a blob and the pass it over */
402         if (!key_to_blob(key, &blob, &len))
403                 return (0);
404
405         buffer_init(&m);
406         buffer_put_string(&m, blob, len);
407         buffer_put_string(&m, sig, siglen);
408         buffer_put_string(&m, data, datalen);
409         xfree(blob);
410
411         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
412
413         debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
414         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
415
416         verified = buffer_get_int(&m);
417
418         buffer_free(&m);
419
420         return (verified);
421 }
422
423 /* Export key state after authentication */
424 Newkeys *
425 mm_newkeys_from_blob(u_char *blob, int blen)
426 {
427         Buffer b;
428         u_int len;
429         Newkeys *newkey = NULL;
430         Enc *enc;
431         Mac *mac;
432         Comp *comp;
433
434         debug3("%s: %p(%d)", __func__, blob, blen);
435 #ifdef DEBUG_PK
436         dump_base64(stderr, blob, blen);
437 #endif
438         buffer_init(&b);
439         buffer_append(&b, blob, blen);
440
441         newkey = xmalloc(sizeof(*newkey));
442         enc = &newkey->enc;
443         mac = &newkey->mac;
444         comp = &newkey->comp;
445
446         /* Enc structure */
447         enc->name = buffer_get_string(&b, NULL);
448         buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
449         enc->enabled = buffer_get_int(&b);
450         enc->block_size = buffer_get_int(&b);
451         enc->key = buffer_get_string(&b, &enc->key_len);
452         enc->iv = buffer_get_string(&b, &len);
453         if (len != enc->block_size)
454                 fatal("%s: bad ivlen: expected %u != %u", __func__,
455                     enc->block_size, len);
456
457         if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
458                 fatal("%s: bad cipher name %s or pointer %p", __func__,
459                     enc->name, enc->cipher);
460
461         /* Mac structure */
462         mac->name = buffer_get_string(&b, NULL);
463         if (mac->name == NULL || mac_init(mac, mac->name) == -1)
464                 fatal("%s: can not init mac %s", __func__, mac->name);
465         mac->enabled = buffer_get_int(&b);
466         mac->key = buffer_get_string(&b, &len);
467         if (len > mac->key_len)
468                 fatal("%s: bad mac key length: %u > %d", __func__, len,
469                     mac->key_len);
470         mac->key_len = len;
471
472         /* Comp structure */
473         comp->type = buffer_get_int(&b);
474         comp->enabled = buffer_get_int(&b);
475         comp->name = buffer_get_string(&b, NULL);
476
477         len = buffer_len(&b);
478         if (len != 0)
479                 error("newkeys_from_blob: remaining bytes in blob %u", len);
480         buffer_free(&b);
481         return (newkey);
482 }
483
484 int
485 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
486 {
487         Buffer b;
488         int len;
489         Enc *enc;
490         Mac *mac;
491         Comp *comp;
492         Newkeys *newkey = newkeys[mode];
493
494         debug3("%s: converting %p", __func__, newkey);
495
496         if (newkey == NULL) {
497                 error("%s: newkey == NULL", __func__);
498                 return 0;
499         }
500         enc = &newkey->enc;
501         mac = &newkey->mac;
502         comp = &newkey->comp;
503
504         buffer_init(&b);
505         /* Enc structure */
506         buffer_put_cstring(&b, enc->name);
507         /* The cipher struct is constant and shared, you export pointer */
508         buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
509         buffer_put_int(&b, enc->enabled);
510         buffer_put_int(&b, enc->block_size);
511         buffer_put_string(&b, enc->key, enc->key_len);
512         packet_get_keyiv(mode, enc->iv, enc->block_size);
513         buffer_put_string(&b, enc->iv, enc->block_size);
514
515         /* Mac structure */
516         buffer_put_cstring(&b, mac->name);
517         buffer_put_int(&b, mac->enabled);
518         buffer_put_string(&b, mac->key, mac->key_len);
519
520         /* Comp structure */
521         buffer_put_int(&b, comp->type);
522         buffer_put_int(&b, comp->enabled);
523         buffer_put_cstring(&b, comp->name);
524
525         len = buffer_len(&b);
526         if (lenp != NULL)
527                 *lenp = len;
528         if (blobp != NULL) {
529                 *blobp = xmalloc(len);
530                 memcpy(*blobp, buffer_ptr(&b), len);
531         }
532         memset(buffer_ptr(&b), 0, len);
533         buffer_free(&b);
534         return len;
535 }
536
537 static void
538 mm_send_kex(Buffer *m, Kex *kex)
539 {
540         buffer_put_string(m, kex->session_id, kex->session_id_len);
541         buffer_put_int(m, kex->we_need);
542         buffer_put_int(m, kex->hostkey_type);
543         buffer_put_int(m, kex->kex_type);
544         buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
545         buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
546         buffer_put_int(m, kex->flags);
547         buffer_put_cstring(m, kex->client_version_string);
548         buffer_put_cstring(m, kex->server_version_string);
549 }
550
551 void
552 mm_send_keystate(struct monitor *monitor)
553 {
554         Buffer m;
555         u_char *blob, *p;
556         u_int bloblen, plen;
557         u_int32_t seqnr, packets;
558         u_int64_t blocks;
559
560         buffer_init(&m);
561
562         if (!compat20) {
563                 u_char iv[24];
564                 u_char *key;
565                 u_int ivlen, keylen;
566
567                 buffer_put_int(&m, packet_get_protocol_flags());
568
569                 buffer_put_int(&m, packet_get_ssh1_cipher());
570
571                 debug3("%s: Sending ssh1 KEY+IV", __func__);
572                 keylen = packet_get_encryption_key(NULL);
573                 key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
574                 keylen = packet_get_encryption_key(key);
575                 buffer_put_string(&m, key, keylen);
576                 memset(key, 0, keylen);
577                 xfree(key);
578
579                 ivlen = packet_get_keyiv_len(MODE_OUT);
580                 packet_get_keyiv(MODE_OUT, iv, ivlen);
581                 buffer_put_string(&m, iv, ivlen);
582                 ivlen = packet_get_keyiv_len(MODE_OUT);
583                 packet_get_keyiv(MODE_IN, iv, ivlen);
584                 buffer_put_string(&m, iv, ivlen);
585                 goto skip;
586         } else {
587                 /* Kex for rekeying */
588                 mm_send_kex(&m, *monitor->m_pkex);
589         }
590
591         debug3("%s: Sending new keys: %p %p",
592             __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
593
594         /* Keys from Kex */
595         if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
596                 fatal("%s: conversion of newkeys failed", __func__);
597
598         buffer_put_string(&m, blob, bloblen);
599         xfree(blob);
600
601         if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
602                 fatal("%s: conversion of newkeys failed", __func__);
603
604         buffer_put_string(&m, blob, bloblen);
605         xfree(blob);
606
607         packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
608         buffer_put_int(&m, seqnr);
609         buffer_put_int64(&m, blocks);
610         buffer_put_int(&m, packets);
611         packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
612         buffer_put_int(&m, seqnr);
613         buffer_put_int64(&m, blocks);
614         buffer_put_int(&m, packets);
615
616         debug3("%s: New keys have been sent", __func__);
617  skip:
618         /* More key context */
619         plen = packet_get_keycontext(MODE_OUT, NULL);
620         p = xmalloc(plen+1);
621         packet_get_keycontext(MODE_OUT, p);
622         buffer_put_string(&m, p, plen);
623         xfree(p);
624
625         plen = packet_get_keycontext(MODE_IN, NULL);
626         p = xmalloc(plen+1);
627         packet_get_keycontext(MODE_IN, p);
628         buffer_put_string(&m, p, plen);
629         xfree(p);
630
631         /* Compression state */
632         debug3("%s: Sending compression state", __func__);
633         buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
634         buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
635
636         /* Network I/O buffers */
637         buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
638         buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
639
640         mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
641         debug3("%s: Finished sending state", __func__);
642
643         buffer_free(&m);
644 }
645
646 int
647 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
648 {
649         Buffer m;
650         char *p, *msg;
651         int success = 0;
652
653         buffer_init(&m);
654         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
655
656         debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
657         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
658
659         success = buffer_get_int(&m);
660         if (success == 0) {
661                 debug3("%s: pty alloc failed", __func__);
662                 buffer_free(&m);
663                 return (0);
664         }
665         p = buffer_get_string(&m, NULL);
666         msg = buffer_get_string(&m, NULL);
667         buffer_free(&m);
668
669         strlcpy(namebuf, p, namebuflen); /* Possible truncation */
670         xfree(p);
671
672         buffer_append(&loginmsg, msg, strlen(msg));
673         xfree(msg);
674
675         *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
676         *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
677
678         /* Success */
679         return (1);
680 }
681
682 void
683 mm_session_pty_cleanup2(Session *s)
684 {
685         Buffer m;
686
687         if (s->ttyfd == -1)
688                 return;
689         buffer_init(&m);
690         buffer_put_cstring(&m, s->tty);
691         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
692         buffer_free(&m);
693
694         /* closed dup'ed master */
695         if (close(s->ptymaster) < 0)
696                 error("close(s->ptymaster): %s", strerror(errno));
697
698         /* unlink pty from session */
699         s->ttyfd = -1;
700 }
701
702 #ifdef USE_PAM
703 void
704 mm_start_pam(Authctxt *authctxt)
705 {
706         Buffer m;
707
708         debug3("%s entering", __func__);
709         if (!options.use_pam)
710                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
711
712         buffer_init(&m);
713         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
714
715         buffer_free(&m);
716 }
717
718 u_int
719 mm_do_pam_account(void)
720 {
721         Buffer m;
722         u_int ret;
723         char *msg;
724
725         debug3("%s entering", __func__);
726         if (!options.use_pam)
727                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
728
729         buffer_init(&m);
730         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
731
732         mm_request_receive_expect(pmonitor->m_recvfd,
733             MONITOR_ANS_PAM_ACCOUNT, &m);
734         ret = buffer_get_int(&m);
735         msg = buffer_get_string(&m, NULL);
736         buffer_append(&loginmsg, msg, strlen(msg));
737         xfree(msg);
738
739         buffer_free(&m);
740
741         debug3("%s returning %d", __func__, ret);
742
743         return (ret);
744 }
745
746 void *
747 mm_sshpam_init_ctx(Authctxt *authctxt)
748 {
749         Buffer m;
750         int success;
751
752         debug3("%s", __func__);
753         buffer_init(&m);
754         buffer_put_cstring(&m, authctxt->user);
755         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
756         debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
757         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
758         success = buffer_get_int(&m);
759         if (success == 0) {
760                 debug3("%s: pam_init_ctx failed", __func__);
761                 buffer_free(&m);
762                 return (NULL);
763         }
764         buffer_free(&m);
765         return (authctxt);
766 }
767
768 int
769 mm_sshpam_query(void *ctx, char **name, char **info,
770     u_int *num, char ***prompts, u_int **echo_on)
771 {
772         Buffer m;
773         u_int i;
774         int ret;
775
776         debug3("%s", __func__);
777         buffer_init(&m);
778         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
779         debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
780         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
781         ret = buffer_get_int(&m);
782         debug3("%s: pam_query returned %d", __func__, ret);
783         *name = buffer_get_string(&m, NULL);
784         *info = buffer_get_string(&m, NULL);
785         *num = buffer_get_int(&m);
786         if (*num > PAM_MAX_NUM_MSG)
787                 fatal("%s: recieved %u PAM messages, expected <= %u",
788                     __func__, *num, PAM_MAX_NUM_MSG);
789         *prompts = xcalloc((*num + 1), sizeof(char *));
790         *echo_on = xcalloc((*num + 1), sizeof(u_int));
791         for (i = 0; i < *num; ++i) {
792                 (*prompts)[i] = buffer_get_string(&m, NULL);
793                 (*echo_on)[i] = buffer_get_int(&m);
794         }
795         buffer_free(&m);
796         return (ret);
797 }
798
799 int
800 mm_sshpam_respond(void *ctx, u_int num, char **resp)
801 {
802         Buffer m;
803         u_int i;
804         int ret;
805
806         debug3("%s", __func__);
807         buffer_init(&m);
808         buffer_put_int(&m, num);
809         for (i = 0; i < num; ++i)
810                 buffer_put_cstring(&m, resp[i]);
811         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
812         debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
813         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
814         ret = buffer_get_int(&m);
815         debug3("%s: pam_respond returned %d", __func__, ret);
816         buffer_free(&m);
817         return (ret);
818 }
819
820 void
821 mm_sshpam_free_ctx(void *ctxtp)
822 {
823         Buffer m;
824
825         debug3("%s", __func__);
826         buffer_init(&m);
827         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
828         debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
829         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
830         buffer_free(&m);
831 }
832 #endif /* USE_PAM */
833
834 /* Request process termination */
835
836 void
837 mm_terminate(void)
838 {
839         Buffer m;
840
841         buffer_init(&m);
842         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
843         buffer_free(&m);
844 }
845
846 int
847 mm_ssh1_session_key(BIGNUM *num)
848 {
849         int rsafail;
850         Buffer m;
851
852         buffer_init(&m);
853         buffer_put_bignum2(&m, num);
854         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
855
856         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
857
858         rsafail = buffer_get_int(&m);
859         buffer_get_bignum2(&m, num);
860
861         buffer_free(&m);
862
863         return (rsafail);
864 }
865
866 static void
867 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
868     char ***prompts, u_int **echo_on)
869 {
870         *name = xstrdup("");
871         *infotxt = xstrdup("");
872         *numprompts = 1;
873         *prompts = xcalloc(*numprompts, sizeof(char *));
874         *echo_on = xcalloc(*numprompts, sizeof(u_int));
875         (*echo_on)[0] = 0;
876 }
877
878 int
879 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
880    u_int *numprompts, char ***prompts, u_int **echo_on)
881 {
882         Buffer m;
883         u_int success;
884         char *challenge;
885
886         debug3("%s: entering", __func__);
887
888         buffer_init(&m);
889         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
890
891         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
892             &m);
893         success = buffer_get_int(&m);
894         if (success == 0) {
895                 debug3("%s: no challenge", __func__);
896                 buffer_free(&m);
897                 return (-1);
898         }
899
900         /* Get the challenge, and format the response */
901         challenge  = buffer_get_string(&m, NULL);
902         buffer_free(&m);
903
904         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
905         (*prompts)[0] = challenge;
906
907         debug3("%s: received challenge: %s", __func__, challenge);
908
909         return (0);
910 }
911
912 int
913 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
914 {
915         Buffer m;
916         int authok;
917
918         debug3("%s: entering", __func__);
919         if (numresponses != 1)
920                 return (-1);
921
922         buffer_init(&m);
923         buffer_put_cstring(&m, responses[0]);
924         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
925
926         mm_request_receive_expect(pmonitor->m_recvfd,
927             MONITOR_ANS_BSDAUTHRESPOND, &m);
928
929         authok = buffer_get_int(&m);
930         buffer_free(&m);
931
932         return ((authok == 0) ? -1 : 0);
933 }
934
935 #ifdef SKEY
936 int
937 mm_skey_query(void *ctx, char **name, char **infotxt,
938    u_int *numprompts, char ***prompts, u_int **echo_on)
939 {
940         Buffer m;
941         int len;
942         u_int success;
943         char *p, *challenge;
944
945         debug3("%s: entering", __func__);
946
947         buffer_init(&m);
948         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
949
950         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
951             &m);
952         success = buffer_get_int(&m);
953         if (success == 0) {
954                 debug3("%s: no challenge", __func__);
955                 buffer_free(&m);
956                 return (-1);
957         }
958
959         /* Get the challenge, and format the response */
960         challenge  = buffer_get_string(&m, NULL);
961         buffer_free(&m);
962
963         debug3("%s: received challenge: %s", __func__, challenge);
964
965         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
966
967         xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
968         xfree(challenge);
969
970         return (0);
971 }
972
973 int
974 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
975 {
976         Buffer m;
977         int authok;
978
979         debug3("%s: entering", __func__);
980         if (numresponses != 1)
981                 return (-1);
982
983         buffer_init(&m);
984         buffer_put_cstring(&m, responses[0]);
985         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
986
987         mm_request_receive_expect(pmonitor->m_recvfd,
988             MONITOR_ANS_SKEYRESPOND, &m);
989
990         authok = buffer_get_int(&m);
991         buffer_free(&m);
992
993         return ((authok == 0) ? -1 : 0);
994 }
995 #endif /* SKEY */
996
997 void
998 mm_ssh1_session_id(u_char session_id[16])
999 {
1000         Buffer m;
1001         int i;
1002
1003         debug3("%s entering", __func__);
1004
1005         buffer_init(&m);
1006         for (i = 0; i < 16; i++)
1007                 buffer_put_char(&m, session_id[i]);
1008
1009         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1010         buffer_free(&m);
1011 }
1012
1013 int
1014 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1015 {
1016         Buffer m;
1017         Key *key;
1018         u_char *blob;
1019         u_int blen;
1020         int allowed = 0, have_forced = 0;
1021
1022         debug3("%s entering", __func__);
1023
1024         buffer_init(&m);
1025         buffer_put_bignum2(&m, client_n);
1026
1027         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1028         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1029
1030         allowed = buffer_get_int(&m);
1031
1032         /* fake forced command */
1033         auth_clear_options();
1034         have_forced = buffer_get_int(&m);
1035         forced_command = have_forced ? xstrdup("true") : NULL;
1036
1037         if (allowed && rkey != NULL) {
1038                 blob = buffer_get_string(&m, &blen);
1039                 if ((key = key_from_blob(blob, blen)) == NULL)
1040                         fatal("%s: key_from_blob failed", __func__);
1041                 *rkey = key;
1042                 xfree(blob);
1043         }
1044         mm_send_debug(&m);
1045         buffer_free(&m);
1046
1047         return (allowed);
1048 }
1049
1050 BIGNUM *
1051 mm_auth_rsa_generate_challenge(Key *key)
1052 {
1053         Buffer m;
1054         BIGNUM *challenge;
1055         u_char *blob;
1056         u_int blen;
1057
1058         debug3("%s entering", __func__);
1059
1060         if ((challenge = BN_new()) == NULL)
1061                 fatal("%s: BN_new failed", __func__);
1062
1063         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1064         if (key_to_blob(key, &blob, &blen) == 0)
1065                 fatal("%s: key_to_blob failed", __func__);
1066         key->type = KEY_RSA1;
1067
1068         buffer_init(&m);
1069         buffer_put_string(&m, blob, blen);
1070         xfree(blob);
1071
1072         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1073         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1074
1075         buffer_get_bignum2(&m, challenge);
1076         buffer_free(&m);
1077
1078         return (challenge);
1079 }
1080
1081 int
1082 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1083 {
1084         Buffer m;
1085         u_char *blob;
1086         u_int blen;
1087         int success = 0;
1088
1089         debug3("%s entering", __func__);
1090
1091         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1092         if (key_to_blob(key, &blob, &blen) == 0)
1093                 fatal("%s: key_to_blob failed", __func__);
1094         key->type = KEY_RSA1;
1095
1096         buffer_init(&m);
1097         buffer_put_string(&m, blob, blen);
1098         buffer_put_string(&m, response, 16);
1099         xfree(blob);
1100
1101         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1102         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1103
1104         success = buffer_get_int(&m);
1105         buffer_free(&m);
1106
1107         return (success);
1108 }
1109
1110 #ifdef SSH_AUDIT_EVENTS
1111 void
1112 mm_audit_event(ssh_audit_event_t event)
1113 {
1114         Buffer m;
1115
1116         debug3("%s entering", __func__);
1117
1118         buffer_init(&m);
1119         buffer_put_int(&m, event);
1120
1121         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1122         buffer_free(&m);
1123 }
1124
1125 void
1126 mm_audit_run_command(const char *command)
1127 {
1128         Buffer m;
1129
1130         debug3("%s entering command %s", __func__, command);
1131
1132         buffer_init(&m);
1133         buffer_put_cstring(&m, command);
1134
1135         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1136         buffer_free(&m);
1137 }
1138 #endif /* SSH_AUDIT_EVENTS */
1139
1140 #ifdef GSSAPI
1141 OM_uint32
1142 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1143 {
1144         Buffer m;
1145         OM_uint32 major;
1146
1147         /* Client doesn't get to see the context */
1148         *ctx = NULL;
1149
1150         buffer_init(&m);
1151         buffer_put_string(&m, goid->elements, goid->length);
1152
1153         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1154         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1155
1156         major = buffer_get_int(&m);
1157
1158         buffer_free(&m);
1159         return (major);
1160 }
1161
1162 OM_uint32
1163 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1164     gss_buffer_desc *out, OM_uint32 *flags)
1165 {
1166         Buffer m;
1167         OM_uint32 major;
1168         u_int len;
1169
1170         buffer_init(&m);
1171         buffer_put_string(&m, in->value, in->length);
1172
1173         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1174         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1175
1176         major = buffer_get_int(&m);
1177         out->value = buffer_get_string(&m, &len);
1178         out->length = len;
1179         if (flags)
1180                 *flags = buffer_get_int(&m);
1181
1182         buffer_free(&m);
1183
1184         return (major);
1185 }
1186
1187 OM_uint32
1188 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1189 {
1190         Buffer m;
1191         OM_uint32 major;
1192
1193         buffer_init(&m);
1194         buffer_put_string(&m, gssbuf->value, gssbuf->length);
1195         buffer_put_string(&m, gssmic->value, gssmic->length);
1196
1197         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1198         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1199             &m);
1200
1201         major = buffer_get_int(&m);
1202         buffer_free(&m);
1203         return(major);
1204 }
1205
1206 int
1207 mm_ssh_gssapi_userok(char *user)
1208 {
1209         Buffer m;
1210         int authenticated = 0;
1211
1212         buffer_init(&m);
1213
1214         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1215         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1216                                   &m);
1217
1218         authenticated = buffer_get_int(&m);
1219
1220         buffer_free(&m);
1221         debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1222         return (authenticated);
1223 }
1224 #endif /* GSSAPI */
This page took 0.133482 seconds and 5 git commands to generate.