xref: /openbsd-src/sbin/isakmpd/monitor_fdpass.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: monitor_fdpass.c,v 1.16 2008/03/24 16:11:08 deraadt Exp $	*/
2 
3 /*
4  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
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 <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31 
32 #include <errno.h>
33 #include <string.h>
34 
35 #include "log.h"
36 #include "monitor.h"
37 
38 int
39 mm_send_fd(int socket, int fd)
40 {
41 	struct msghdr   msg;
42 	union {
43 		struct cmsghdr hdr;
44 		char buf[CMSG_SPACE(sizeof(int))];
45 	} cmsgbuf;
46 	char		ch = '\0';
47 	struct cmsghdr *cmsg;
48 	struct iovec    vec;
49 	ssize_t         n;
50 
51 	bzero(&msg, sizeof msg);
52 	msg.msg_control = (caddr_t)&cmsgbuf.buf;
53 	msg.msg_controllen = sizeof(cmsgbuf.buf);
54 	cmsg = CMSG_FIRSTHDR(&msg);
55 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
56 	cmsg->cmsg_level = SOL_SOCKET;
57 	cmsg->cmsg_type = SCM_RIGHTS;
58 	*(int *)CMSG_DATA(cmsg) = fd;
59 
60 	vec.iov_base = &ch;
61 	vec.iov_len = 1;
62 	msg.msg_iov = &vec;
63 	msg.msg_iovlen = 1;
64 
65 	if ((n = sendmsg(socket, &msg, 0)) == -1) {
66 		log_error("mm_send_fd: sendmsg(%d)", fd);
67 		return -1;
68 	}
69 	if (n != 1) {
70 		log_error("mm_send_fd: sendmsg: expected sent 1 got %ld",
71 		    (long)n);
72 		return -1;
73 	}
74 	return 0;
75 }
76 
77 int
78 mm_receive_fd(int socket)
79 {
80 	struct msghdr   msg;
81 	union {
82 		struct cmsghdr hdr;
83 		char buf[CMSG_SPACE(sizeof(int))];
84 	} cmsgbuf;
85 	char		ch;
86 	struct cmsghdr *cmsg;
87 	struct iovec    vec;
88 	ssize_t         n;
89 	int             fd;
90 
91 	bzero(&msg, sizeof msg);
92 	vec.iov_base = &ch;
93 	vec.iov_len = 1;
94 	msg.msg_iov = &vec;
95 	msg.msg_iovlen = 1;
96 	msg.msg_control = &cmsgbuf.buf;
97 	msg.msg_controllen = sizeof(cmsgbuf.buf);
98 
99 	if ((n = recvmsg(socket, &msg, 0)) == -1) {
100 		log_error("mm_receive_fd: recvmsg");
101 		return -1;
102 	}
103 	if (n != 1) {
104 		log_error("mm_receive_fd: recvmsg: expected received 1 got %ld",
105 		    (long)n);
106 		return -1;
107 	}
108 	cmsg = CMSG_FIRSTHDR(&msg);
109 	if (cmsg == NULL) {
110 		log_error("mm_receive_fd: no message header");
111 		return -1;
112 	}
113 	if (cmsg->cmsg_type != SCM_RIGHTS) {
114 		log_error("mm_receive_fd: expected type %d got %d", SCM_RIGHTS,
115 		    cmsg->cmsg_type);
116 		return -1;
117 	}
118 	fd = (*(int *)CMSG_DATA(cmsg));
119 	return fd;
120 }
121