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