chiark / gitweb /
d105a66fe84ece59cd5319c4b3f239f6bcabbf56
[elogind.git] / src / shared / socket-label.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <arpa/inet.h>
28 #include <stdio.h>
29 #include <net/if.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <stddef.h>
33 #include <sys/ioctl.h>
34
35 #include "macro.h"
36 #include "util.h"
37 #include "mkdir.h"
38 #include "socket-util.h"
39 #include "missing.h"
40 #include "label.h"
41
42 int socket_address_listen(
43                 const SocketAddress *a,
44                 int backlog,
45                 SocketAddressBindIPv6Only only,
46                 const char *bind_to_device,
47                 bool free_bind,
48                 bool transparent,
49                 mode_t directory_mode,
50                 mode_t socket_mode,
51                 const char *label,
52                 int *ret) {
53
54         int r, fd, one;
55
56         assert(a);
57         assert(ret);
58
59         if ((r = socket_address_verify(a)) < 0)
60                 return r;
61
62         if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
63                 return -EAFNOSUPPORT;
64
65         r = label_socket_set(label);
66         if (r < 0)
67                 return r;
68
69         fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, a->protocol);
70         r = fd < 0 ? -errno : 0;
71
72         label_socket_clear();
73
74         if (r < 0)
75                 return r;
76
77         if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
78                 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
79
80                 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
81                         goto fail;
82         }
83
84         if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
85                 if (bind_to_device)
86                         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
87                                 goto fail;
88
89                 if (free_bind) {
90                         one = 1;
91                         if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
92                                 log_warning("IP_FREEBIND failed: %m");
93                 }
94
95                 if (transparent) {
96                         one = 1;
97                         if (setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0)
98                                 log_warning("IP_TRANSPARENT failed: %m");
99                 }
100         }
101
102         one = 1;
103         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
104                 goto fail;
105
106         if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
107                 mode_t old_mask;
108
109                 /* Create parents */
110                 mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
111
112                 /* Enforce the right access mode for the socket*/
113                 old_mask = umask(~ socket_mode);
114
115                 /* Include the original umask in our mask */
116                 umask(~socket_mode | old_mask);
117
118                 r = label_bind(fd, &a->sockaddr.sa, a->size);
119
120                 if (r < 0 && errno == EADDRINUSE) {
121                         /* Unlink and try again */
122                         unlink(a->sockaddr.un.sun_path);
123                         r = bind(fd, &a->sockaddr.sa, a->size);
124                 }
125
126                 umask(old_mask);
127         } else
128                 r = bind(fd, &a->sockaddr.sa, a->size);
129
130         if (r < 0)
131                 goto fail;
132
133         if (socket_address_can_accept(a))
134                 if (listen(fd, backlog) < 0)
135                         goto fail;
136
137         *ret = fd;
138         return 0;
139
140 fail:
141         r = -errno;
142         close_nointr_nofail(fd);
143         return r;
144 }