GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/pflogd/privsep_fdpass.c Lines: 0 42 0.0 %
Date: 2016-12-06 Branches: 0 20 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: privsep_fdpass.c,v 1.8 2014/11/18 20:51:00 krw Exp $	*/
2
3
/*
4
 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5
 * All rights reserved.
6
 *
7
 * Copyright (c) 2002 Matthieu Herrb
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 *
14
 *    - Redistributions of source code must retain the above copyright
15
 *      notice, this list of conditions and the following disclaimer.
16
 *    - Redistributions in binary form must reproduce the above
17
 *      copyright notice, this list of conditions and the following
18
 *      disclaimer in the documentation and/or other materials provided
19
 *      with the distribution.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
#include <sys/types.h>
36
#include <sys/uio.h>
37
#include <sys/socket.h>
38
#include <sys/stat.h>
39
#include <err.h>
40
#include <errno.h>
41
#include <fcntl.h>
42
#include <signal.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <unistd.h>
47
#include "pflogd.h"
48
49
void
50
send_fd(int sock, int fd)
51
{
52
	struct msghdr msg;
53
	union {
54
		struct cmsghdr hdr;
55
		char buf[CMSG_SPACE(sizeof(int))];
56
	} cmsgbuf;
57
	struct cmsghdr *cmsg;
58
	struct iovec vec;
59
	int result = 0;
60
	ssize_t n;
61
62
	memset(&msg, 0, sizeof(msg));
63
64
	if (fd >= 0) {
65
		msg.msg_control = (caddr_t)&cmsgbuf.buf;
66
		msg.msg_controllen = sizeof(cmsgbuf.buf);
67
		cmsg = CMSG_FIRSTHDR(&msg);
68
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69
		cmsg->cmsg_level = SOL_SOCKET;
70
		cmsg->cmsg_type = SCM_RIGHTS;
71
		*(int *)CMSG_DATA(cmsg) = fd;
72
	} else {
73
		result = errno;
74
	}
75
76
	vec.iov_base = &result;
77
	vec.iov_len = sizeof(int);
78
	msg.msg_iov = &vec;
79
	msg.msg_iovlen = 1;
80
81
	if ((n = sendmsg(sock, &msg, 0)) == -1)
82
		warn("%s: sendmsg(%d)", __func__, sock);
83
	if (n != sizeof(int))
84
		warnx("%s: sendmsg: expected sent 1 got %ld",
85
		    __func__, (long)n);
86
}
87
88
int
89
receive_fd(int sock)
90
{
91
	struct msghdr msg;
92
	union {
93
		struct cmsghdr hdr;
94
		char buf[CMSG_SPACE(sizeof(int))];
95
	} cmsgbuf;
96
	struct cmsghdr *cmsg;
97
	struct iovec vec;
98
	ssize_t n;
99
	int result;
100
	int fd;
101
102
	memset(&msg, 0, sizeof(msg));
103
	vec.iov_base = &result;
104
	vec.iov_len = sizeof(int);
105
	msg.msg_iov = &vec;
106
	msg.msg_iovlen = 1;
107
	msg.msg_control = &cmsgbuf.buf;
108
	msg.msg_controllen = sizeof(cmsgbuf.buf);
109
110
	if ((n = recvmsg(sock, &msg, 0)) == -1) {
111
		warn("%s: recvmsg", __func__);
112
		return -1;
113
	}
114
	if (n != sizeof(int))
115
		warnx("%s: recvmsg: expected received 1 got %ld",
116
		    __func__, (long)n);
117
	if (result == 0) {
118
		cmsg = CMSG_FIRSTHDR(&msg);
119
		if (cmsg == NULL) {
120
			warnx("%s: no message header", __func__);
121
			return -1;
122
		}
123
		if (cmsg->cmsg_type != SCM_RIGHTS)
124
			warnx("%s: expected type %d got %d", __func__,
125
			    SCM_RIGHTS, cmsg->cmsg_type);
126
		fd = (*(int *)CMSG_DATA(cmsg));
127
		return fd;
128
	} else {
129
		errno = result;
130
		return -1;
131
	}
132
}