xref: /openbsd-src/sbin/isakmpd/monitor_fdpass.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
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 <sys/types.h>
27 #include <sys/uio.h>
28 #include <errno.h>
29 #include <string.h>
30 
31 #include "log.h"
32 #include "monitor.h"
33 
34 int
35 mm_send_fd (int socket, int fd)
36 {
37   struct msghdr msg;
38   char tmp[CMSG_SPACE (sizeof (int))];
39   struct cmsghdr *cmsg;
40   struct iovec vec;
41   char ch = '\0';
42   ssize_t n;
43 
44   memset(&msg, 0, sizeof msg);
45   msg.msg_control = (caddr_t)tmp;
46   msg.msg_controllen = CMSG_LEN (sizeof (int));
47   cmsg = CMSG_FIRSTHDR (&msg);
48   cmsg->cmsg_len = CMSG_LEN (sizeof (int));
49   cmsg->cmsg_level = SOL_SOCKET;
50   cmsg->cmsg_type = SCM_RIGHTS;
51   *(int *)CMSG_DATA (cmsg) = fd;
52 
53   vec.iov_base = &ch;
54   vec.iov_len = 1;
55   msg.msg_iov = &vec;
56   msg.msg_iovlen = 1;
57 
58   if ((n = sendmsg (socket, &msg, 0)) == -1)
59     {
60       log_error ("%s: sendmsg(%d)", __func__, fd);
61       return -1;
62     }
63   if (n != 1)
64     {
65       log_error ("%s: sendmsg: expected sent 1 got %ld", __func__, (long)n);
66       return -1;
67     }
68   return 0;
69 }
70 
71 int
72 mm_receive_fd (int socket)
73 {
74   struct msghdr msg;
75   char tmp[CMSG_SPACE (sizeof (int))];
76   struct cmsghdr *cmsg;
77   struct iovec vec;
78   ssize_t n;
79   char ch;
80   int fd;
81 
82   memset (&msg, 0, sizeof msg);
83   vec.iov_base = &ch;
84   vec.iov_len = 1;
85   msg.msg_iov = &vec;
86   msg.msg_iovlen = 1;
87   msg.msg_control = tmp;
88   msg.msg_controllen = sizeof tmp;
89 
90   if ((n = recvmsg(socket, &msg, 0)) == -1)
91     {
92       log_error ("%s: recvmsg", __func__);
93       return -1;
94     }
95   if (n != 1)
96     {
97       log_error ("%s: recvmsg: expected received 1 got %ld", __func__,
98 		 (long)n);
99       return -1;
100     }
101 
102   cmsg = CMSG_FIRSTHDR (&msg);
103   if (cmsg->cmsg_type != SCM_RIGHTS)
104     {
105       log_error ("%s: expected type %d got %d", __func__, SCM_RIGHTS,
106 		 cmsg->cmsg_type);
107       return -1;
108     }
109   fd = (*(int *)CMSG_DATA (cmsg));
110 
111   return fd;
112 }
113