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