4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/socket.h>
28 #include <sys/types.h>
37 #include <avahi-core/log.h>
38 #include <libdaemon/dfork.h>
44 AVAHI_CHROOT_SUCCESS = 0,
46 AVAHI_CHROOT_GET_RESOLV_CONF,
48 AVAHI_CHROOT_GET_SERVER_INTROSPECT,
49 AVAHI_CHROOT_GET_ENTRY_GROUP_INTROSPECT,
50 AVAHI_CHROOT_GET_ADDRESS_RESOLVER_INTROSPECT,
51 AVAHI_CHROOT_GET_DOMAIN_BROWSER_INTROSPECT,
52 AVAHI_CHROOT_GET_HOST_NAME_RESOLVER_INTROSPECT,
53 AVAHI_CHROOT_GET_SERVICE_BROWSER_INTROSPECT,
54 AVAHI_CHROOT_GET_SERVICE_RESOLVER_INTROSPECT,
55 AVAHI_CHROOT_GET_SERVICE_TYPE_BROWSER_INTROSPECT,
57 AVAHI_CHROOT_UNLINK_PID,
58 AVAHI_CHROOT_UNLINK_SOCKET,
62 static const char* const get_file_name_table[AVAHI_CHROOT_MAX] = {
67 AVAHI_DBUS_INTROSPECTION_DIR"/Server.introspect",
68 AVAHI_DBUS_INTROSPECTION_DIR"/EntryGroup.introspect",
69 AVAHI_DBUS_INTROSPECTION_DIR"/AddressResolver.introspect",
70 AVAHI_DBUS_INTROSPECTION_DIR"/DomainBrowser.introspect",
71 AVAHI_DBUS_INTROSPECTION_DIR"/HostNameResolver.introspect",
72 AVAHI_DBUS_INTROSPECTION_DIR"/ServiceBrowser.introspect",
73 AVAHI_DBUS_INTROSPECTION_DIR"/ServiceResolver.introspect",
74 AVAHI_DBUS_INTROSPECTION_DIR"/ServiceTypeBrowser.introspect",
80 static const char *const unlink_file_name_table[AVAHI_CHROOT_MAX] = {
94 AVAHI_DAEMON_RUNTIME_DIR"/pid",
98 static int helper_fd = -1;
100 static int send_fd(int fd, int payload_fd) {
101 uint8_t dummy = AVAHI_CHROOT_SUCCESS;
106 char buf[CMSG_SPACE(sizeof(int))];
109 /* Send a file descriptor over the socket */
111 memset(&iov, 0, sizeof(iov));
112 memset(&msg, 0, sizeof(msg));
113 memset(&cmsg, 0, sizeof(cmsg));
115 iov.iov_base = &dummy;
116 iov.iov_len = sizeof(dummy);
123 msg.msg_control = &cmsg;
124 msg.msg_controllen = sizeof(cmsg);
127 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
128 cmsg.hdr.cmsg_level = SOL_SOCKET;
129 cmsg.hdr.cmsg_type = SCM_RIGHTS;
130 *((int*) CMSG_DATA(&cmsg.hdr)) = payload_fd;
132 if (sendmsg(fd, &msg, 0) < 0) {
133 avahi_log_error("sendmsg() failed: %s", strerror(errno));
140 static int recv_fd(int fd) {
146 char buf[CMSG_SPACE(sizeof(int))];
149 /* Receive a file descriptor from a socket */
151 memset(&iov, 0, sizeof(iov));
152 memset(&msg, 0, sizeof(msg));
153 memset(&cmsg, 0, sizeof(cmsg));
155 iov.iov_base = &dummy;
156 iov.iov_len = sizeof(dummy);
163 msg.msg_control = cmsg.buf;
164 msg.msg_controllen = sizeof(cmsg);
167 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
168 cmsg.hdr.cmsg_level = SOL_SOCKET;
169 cmsg.hdr.cmsg_type = SCM_RIGHTS;
170 *((int*) CMSG_DATA(&cmsg.hdr)) = -1;
172 if (recvmsg(fd, &msg, 0) <= 0) {
173 avahi_log_error("recvmsg() failed: %s", strerror(errno));
178 if (dummy != AVAHI_CHROOT_SUCCESS) {
183 if (!(h = CMSG_FIRSTHDR(&msg))) {
184 avahi_log_error("recvmsg() sent no fd.");
189 assert(h->cmsg_len = CMSG_LEN(sizeof(int)));
190 assert(h->cmsg_level = SOL_SOCKET);
191 assert(h->cmsg_type == SCM_RIGHTS);
193 return *((int*)CMSG_DATA(h));
197 static int helper_main(int fd) {
201 /* This is the main function of our helper process which is forked
202 * off to access files outside the chroot environment. Keep in
203 * mind that this code is security sensitive! */
205 avahi_log_debug(__FILE__": chroot() helper started");
211 if ((r = read(fd, &command, sizeof(command))) <= 0) {
217 avahi_log_error(__FILE__": read() failed: %s", strerror(errno));
221 assert(r == sizeof(command));
223 avahi_log_debug(__FILE__": chroot() helper got command %02x", command);
227 case AVAHI_CHROOT_GET_SERVER_INTROSPECT:
228 case AVAHI_CHROOT_GET_ENTRY_GROUP_INTROSPECT:
229 case AVAHI_CHROOT_GET_ADDRESS_RESOLVER_INTROSPECT:
230 case AVAHI_CHROOT_GET_DOMAIN_BROWSER_INTROSPECT:
231 case AVAHI_CHROOT_GET_HOST_NAME_RESOLVER_INTROSPECT:
232 case AVAHI_CHROOT_GET_SERVICE_BROWSER_INTROSPECT:
233 case AVAHI_CHROOT_GET_SERVICE_RESOLVER_INTROSPECT:
234 case AVAHI_CHROOT_GET_SERVICE_TYPE_BROWSER_INTROSPECT:
236 case AVAHI_CHROOT_GET_RESOLV_CONF: {
239 if ((payload = open(get_file_name_table[(int) command], O_RDONLY)) < 0) {
240 uint8_t c = AVAHI_CHROOT_FAILURE;
242 avahi_log_error(__FILE__": open() failed: %s", strerror(errno));
244 if (write(fd, &c, sizeof(c)) != sizeof(c)) {
245 avahi_log_error(__FILE__": write() failed: %s\n", strerror(errno));
252 if (send_fd(fd, payload) < 0)
260 case AVAHI_CHROOT_UNLINK_SOCKET:
261 case AVAHI_CHROOT_UNLINK_PID: {
262 uint8_t c = AVAHI_CHROOT_SUCCESS;
264 unlink(unlink_file_name_table[(int) command]);
266 if (write(fd, &c, sizeof(c)) != sizeof(c)) {
267 avahi_log_error(__FILE__": write() failed: %s\n", strerror(errno));
275 avahi_log_error(__FILE__": Unknown command %02x.", command);
284 avahi_log_debug(__FILE__": chroot() helper exiting with return value %i", ret);
289 int avahi_chroot_helper_start(void) {
293 assert(helper_fd < 0);
295 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock) < 0) {
296 avahi_log_error("socketpair() failed: %s", strerror(errno));
300 if ((pid = daemon_fork()) < 0) {
303 avahi_log_error(__FILE__": Failed to fork()");
305 } else if (pid == 0) {
307 /* Drop all remaining capabilities */
308 avahi_caps_drop_all();
311 helper_main(sock[1]);
321 void avahi_chroot_helper_shutdown(void) {
330 int avahi_chroot_helper_get_fd(const char *fname) {
332 if (helper_fd >= 0) {
335 for (command = 2; command < AVAHI_CHROOT_MAX; command++)
336 if (get_file_name_table[(int) command] &&
337 strcmp(fname, get_file_name_table[(int) command]) == 0)
340 if (command >= AVAHI_CHROOT_MAX) {
341 avahi_log_error("chroot() helper accessed for invalid file name");
346 assert(get_file_name_table[(int) command]);
348 if (write(helper_fd, &command, sizeof(command)) < 0) {
349 avahi_log_error("write() failed: %s\n", strerror(errno));
353 return recv_fd(helper_fd);
356 return open(fname, O_RDONLY);
360 FILE *avahi_chroot_helper_get_file(const char *fname) {
364 if ((fd = avahi_chroot_helper_get_fd(fname)) < 0)
373 int avahi_chroot_helper_unlink(const char *fname) {
375 if (helper_fd >= 0) {
379 for (command = 2; command < AVAHI_CHROOT_MAX; command++)
380 if (unlink_file_name_table[(int) command] &&
381 strcmp(fname, unlink_file_name_table[(int) command]) == 0)
384 if (command >= AVAHI_CHROOT_MAX) {
385 avahi_log_error("chroot() helper accessed for invalid file name");
390 if (write(helper_fd, &command, sizeof(command)) < 0) {
391 avahi_log_error("write() failed: %s\n", strerror(errno));
395 if ((r = read(helper_fd, &c, sizeof(c))) < 0) {
396 avahi_log_error("read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
404 return unlink(fname);