]> andersk Git - openssh.git/blob - monitor_fdpass.c
Import of Niels Provos' 20020312 ssh-complete.diff
[openssh.git] / monitor_fdpass.c
1 /*
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "includes.h"
27 RCSID("$OpenBSD$");
28
29 #include <sys/uio.h>
30
31 #include "ssh.h"
32 #include "log.h"
33 #include "xmalloc.h"
34 #include "misc.h"
35 #include "monitor_fdpass.h"
36
37 void
38 mm_send_fd(int socket, int fd)
39 {
40         struct msghdr msg;
41         char tmp[CMSG_SPACE(sizeof(int))];
42         struct cmsghdr *cmsg;
43         struct iovec vec;
44         char ch;
45
46         memset(&msg, 0, sizeof(msg));
47         msg.msg_control = (caddr_t)tmp;
48         msg.msg_controllen = CMSG_LEN(sizeof(int));
49         cmsg = CMSG_FIRSTHDR(&msg);
50         cmsg->cmsg_len = CMSG_LEN(sizeof(int));
51         cmsg->cmsg_level = SOL_SOCKET;
52         cmsg->cmsg_type = SCM_RIGHTS;
53         *(int *)CMSG_DATA(cmsg) = fd;
54
55         vec.iov_base = &ch;
56         vec.iov_len = 1;
57         msg.msg_iov = &vec;
58         msg.msg_iovlen = 1;
59
60         if (sendmsg(socket, &msg, 0) == -1)
61                 fatal("%s: sendmsg(%d)", __FUNCTION__, fd);
62 }
63
64 int
65 mm_receive_fd(int socket)
66 {
67         struct msghdr msg;
68         char tmp[CMSG_SPACE(sizeof(int))];
69         struct cmsghdr *cmsg;
70         struct iovec vec;
71         char ch;
72
73         memset(&msg, 0, sizeof(msg));
74         vec.iov_base = &ch;
75         vec.iov_len = 1;
76         msg.msg_iov = &vec;
77         msg.msg_iovlen = 1;
78         msg.msg_control = tmp;
79         msg.msg_controllen = sizeof(tmp);
80
81         if (recvmsg(socket, &msg, 0) == -1)
82                 fatal("%s: recvmsg", __FUNCTION__);
83
84         cmsg = CMSG_FIRSTHDR(&msg);
85         if (cmsg->cmsg_type != SCM_RIGHTS)
86                 fatal("%s: expected type %d got %d", __FUNCTION__,
87                     SCM_RIGHTS, cmsg->cmsg_type);
88         return (*(int *)CMSG_DATA(cmsg));
89 }
This page took 0.051646 seconds and 5 git commands to generate.