multipath-tools: Perform socket client uid check on IPC commands.
[multipath-tools/.git] / multipathd / uxlsnr.c
1 /*
2  * Original author : tridge@samba.org, January 2002
3  *
4  * Copyright (c) 2005 Christophe Varoqui
5  * Copyright (c) 2005 Benjamin Marzinski, Redhat
6  */
7
8 /*
9  * A simple domain socket listener
10  */
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <poll.h>
23 #include <sys/time.h>
24 #include <signal.h>
25 #include <stdbool.h>
26 #include "checkers.h"
27 #include "memory.h"
28 #include "debug.h"
29 #include "vector.h"
30 #include "structs.h"
31 #include "structs_vec.h"
32 #include "uxsock.h"
33 #include "defaults.h"
34 #include "config.h"
35 #include "mpath_cmd.h"
36 #include "time-util.h"
37
38 #include "main.h"
39 #include "cli.h"
40 #include "uxlsnr.h"
41
42 struct timespec sleep_time = {5, 0};
43
44 struct client {
45         struct list_head node;
46         int fd;
47 };
48
49 #define MIN_POLLS 1023
50
51 LIST_HEAD(clients);
52 pthread_mutex_t client_lock = PTHREAD_MUTEX_INITIALIZER;
53 struct pollfd *polls;
54
55 static bool _socket_client_is_root(int fd);
56
57 static bool _socket_client_is_root(int fd)
58 {
59         socklen_t len = 0;
60         struct ucred uc;
61
62         len = sizeof(struct ucred);
63         if ((fd >= 0) &&
64             (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &len) == 0) &&
65             (uc.uid == 0))
66                         return true;
67
68         /* Treat error as not root client */
69         return false;
70 }
71
72 /*
73  * handle a new client joining
74  */
75 static void new_client(int ux_sock)
76 {
77         struct client *c;
78         struct sockaddr addr;
79         socklen_t len = sizeof(addr);
80         int fd;
81
82         fd = accept(ux_sock, &addr, &len);
83
84         if (fd == -1)
85                 return;
86
87         c = (struct client *)MALLOC(sizeof(*c));
88         if (!c) {
89                 close(fd);
90                 return;
91         }
92         memset(c, 0, sizeof(*c));
93         INIT_LIST_HEAD(&c->node);
94         c->fd = fd;
95
96         /* put it in our linked list */
97         pthread_mutex_lock(&client_lock);
98         list_add_tail(&c->node, &clients);
99         pthread_mutex_unlock(&client_lock);
100 }
101
102 /*
103  * kill off a dead client
104  */
105 static void dead_client(struct client *c)
106 {
107         pthread_mutex_lock(&client_lock);
108         list_del_init(&c->node);
109         pthread_mutex_unlock(&client_lock);
110         close(c->fd);
111         c->fd = -1;
112         FREE(c);
113 }
114
115 void free_polls (void)
116 {
117         if (polls)
118                 FREE(polls);
119 }
120
121 void check_timeout(struct timespec start_time, char *inbuf,
122                    unsigned int timeout)
123 {
124         struct timespec diff_time, end_time;
125
126         if (start_time.tv_sec &&
127             clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
128                 unsigned long msecs;
129
130                 timespecsub(&end_time, &start_time, &diff_time);
131                 msecs = diff_time.tv_sec * 1000 +
132                         diff_time.tv_nsec / (1000 * 1000);
133                 if (msecs > timeout)
134                         condlog(2, "cli cmd '%s' timeout reached "
135                                 "after %lu.%06lu secs", inbuf,
136                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
137         }
138 }
139
140 void uxsock_cleanup(void *arg)
141 {
142         cli_exit();
143         free_polls();
144 }
145
146 /*
147  * entry point
148  */
149 void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
150 {
151         int ux_sock;
152         int rlen;
153         char *inbuf;
154         char *reply;
155         sigset_t mask;
156         int old_clients = MIN_POLLS;
157
158         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
159
160         if (ux_sock == -1) {
161                 condlog(1, "could not create uxsock: %d", errno);
162                 return NULL;
163         }
164
165         pthread_cleanup_push(uxsock_cleanup, NULL);
166
167         condlog(3, "uxsock: startup listener");
168         polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
169         if (!polls) {
170                 condlog(0, "uxsock: failed to allocate poll fds");
171                 return NULL;
172         }
173         sigemptyset(&mask);
174         sigaddset(&mask, SIGINT);
175         sigaddset(&mask, SIGTERM);
176         sigaddset(&mask, SIGHUP);
177         sigaddset(&mask, SIGUSR1);
178         while (1) {
179                 struct client *c, *tmp;
180                 int i, poll_count, num_clients;
181
182                 /* setup for a poll */
183                 pthread_mutex_lock(&client_lock);
184                 num_clients = 0;
185                 list_for_each_entry(c, &clients, node) {
186                         num_clients++;
187                 }
188                 if (num_clients != old_clients) {
189                         struct pollfd *new;
190                         if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
191                                 new = REALLOC(polls, (1 + MIN_POLLS) *
192                                                 sizeof(struct pollfd));
193                         } else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
194                                 new = polls;
195                         } else {
196                                 new = REALLOC(polls, (1+num_clients) *
197                                                 sizeof(struct pollfd));
198                         }
199                         if (!new) {
200                                 pthread_mutex_unlock(&client_lock);
201                                 condlog(0, "%s: failed to realloc %d poll fds",
202                                         "uxsock", 1 + num_clients);
203                                 sched_yield();
204                                 continue;
205                         }
206                         old_clients = num_clients;
207                         polls = new;
208                 }
209                 polls[0].fd = ux_sock;
210                 polls[0].events = POLLIN;
211
212                 /* setup the clients */
213                 i = 1;
214                 list_for_each_entry(c, &clients, node) {
215                         polls[i].fd = c->fd;
216                         polls[i].events = POLLIN;
217                         i++;
218                 }
219                 pthread_mutex_unlock(&client_lock);
220
221                 /* most of our life is spent in this call */
222                 poll_count = ppoll(polls, i, &sleep_time, &mask);
223
224                 if (poll_count == -1) {
225                         if (errno == EINTR) {
226                                 handle_signals();
227                                 continue;
228                         }
229
230                         /* something went badly wrong! */
231                         condlog(0, "uxsock: poll failed with %d", errno);
232                         break;
233                 }
234
235                 if (poll_count == 0) {
236                         handle_signals();
237                         continue;
238                 }
239
240                 /* see if a client wants to speak to us */
241                 for (i = 1; i < num_clients + 1; i++) {
242                         if (polls[i].revents & POLLIN) {
243                                 struct timespec start_time;
244
245                                 c = NULL;
246                                 pthread_mutex_lock(&client_lock);
247                                 list_for_each_entry(tmp, &clients, node) {
248                                         if (tmp->fd == polls[i].fd) {
249                                                 c = tmp;
250                                                 break;
251                                         }
252                                 }
253                                 pthread_mutex_unlock(&client_lock);
254                                 if (!c) {
255                                         condlog(4, "cli%d: new fd %d",
256                                                 i, polls[i].fd);
257                                         continue;
258                                 }
259                                 if (clock_gettime(CLOCK_MONOTONIC, &start_time)
260                                     != 0)
261                                         start_time.tv_sec = 0;
262                                 if (recv_packet_from_client(c->fd, &inbuf,
263                                                             uxsock_timeout)
264                                     != 0) {
265                                         dead_client(c);
266                                         continue;
267                                 }
268                                 if (!inbuf) {
269                                         condlog(4, "recv_packet_from_client "
270                                                 "get null request");
271                                         continue;
272                                 }
273                                 condlog(4, "cli[%d]: Got request [%s]",
274                                         i, inbuf);
275                                 uxsock_trigger(inbuf, &reply, &rlen,
276                                                _socket_client_is_root(c->fd),
277                                                trigger_data);
278                                 if (reply) {
279                                         if (send_packet(c->fd,
280                                                         reply) != 0) {
281                                                 dead_client(c);
282                                         } else {
283                                                 condlog(4, "cli[%d]: "
284                                                         "Reply [%d bytes]",
285                                                         i, rlen);
286                                         }
287                                         FREE(reply);
288                                         reply = NULL;
289                                 }
290                                 check_timeout(start_time, inbuf,
291                                               uxsock_timeout);
292                                 FREE(inbuf);
293                         }
294                 }
295
296                 /* see if we got a new client */
297                 if (polls[0].revents & POLLIN) {
298                         new_client(ux_sock);
299                 }
300         }
301
302         pthread_cleanup_pop(1);
303         close(ux_sock);
304         return NULL;
305 }