Add Nicira extension for remote command execution.
[sliver-openvswitch.git] / secchan / executer.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "executer.h"
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <fnmatch.h>
39 #include <poll.h>
40 #include <signal.h>
41 #include <stdlib.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "dynamic-string.h"
47 #include "fatal-signal.h"
48 #include "nicira-ext.h"
49 #include "ofpbuf.h"
50 #include "openflow.h"
51 #include "poll-loop.h"
52 #include "rconn.h"
53 #include "secchan.h"
54 #include "socket-util.h"
55 #include "util.h"
56 #include "vconn.h"
57
58 #define THIS_MODULE VLM_executer
59 #include "vlog.h"
60
61 #define MAX_CHILDREN 8
62
63 struct child {
64     /* Information about child process. */
65     char *name;                 /* argv[0] passed to child. */
66     pid_t pid;                  /* Child's process ID. */
67
68     /* For sending a reply to the controller when the child dies. */
69     struct relay *relay;
70     uint32_t xid;               /* Transaction ID used by controller. */
71
72     /* We read up to MAX_OUTPUT bytes of output and send them back to the
73      * controller when the child dies. */
74 #define MAX_OUTPUT 4096
75     int output_fd;              /* FD from which to read child's output. */
76     uint8_t *output;            /* Output data. */
77     size_t output_size;         /* Number of bytes of output data so far. */
78 };
79
80 struct executer {
81     const struct settings *s;
82
83     /* Children. */
84     struct child children[MAX_CHILDREN];
85     size_t n_children;
86
87     /* File descriptors. */
88     int wait_fd;                /* Pipe FD for wakeup when on SIGCHLD. */
89     int null_fd;                /* FD for /dev/null. */
90 };
91
92 /* Returns true if 'cmd' is allowed by 'acl', which is a command-separated
93  * access control list in the format described for --command-acl in
94  * secchan(8). */
95 static bool
96 executer_is_permitted(const char *acl_, const char *cmd)
97 {
98     char *acl, *save_ptr, *pattern;
99     bool allowed, denied;
100
101     /* Verify that 'cmd' consists only of alphanumerics plus _ or -. */
102     if (cmd[strspn(cmd, "abcdefghijklmnopqrstuvwxyz"
103                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")] != '\0') {
104         VLOG_WARN("rejecting command name \"%s\" that contain forbidden "
105                   "characters", cmd);
106         return false;
107     }
108
109     /* Check 'cmd' against 'acl'. */
110     acl = xstrdup(acl_);
111     save_ptr = acl;
112     allowed = denied = false;
113     while ((pattern = strsep(&save_ptr, ",")) != NULL && !denied) {
114         if (pattern[0] != '!' && !fnmatch(pattern, cmd, 0)) {
115             allowed = true;
116         } else if (pattern[0] == '!' && !fnmatch(pattern + 1, cmd, 0)) {
117             denied = true;
118         }
119     }
120     free(acl);
121
122     /* Check the command white/blacklisted state. */
123     if (allowed && !denied) {
124         VLOG_WARN("permitting command execution: \"%s\" is whitelisted", cmd);
125     } else if (allowed && denied) {
126         VLOG_WARN("denying command execution: \"%s\" is both blacklisted "
127                   "and whitelisted", cmd);
128     } else if (!allowed) {
129         VLOG_WARN("denying command execution: \"%s\" is not whitelisted", cmd);
130     } else if (denied) {
131         VLOG_WARN("denying command execution: \"%s\" is blacklisted", cmd);
132     }
133     return allowed && !denied;
134 }
135
136 static bool
137 executer_remote_packet_cb(struct relay *r, void *e_)
138 {
139     struct executer *e = e_;
140     struct ofpbuf *msg = r->halves[HALF_REMOTE].rxbuf;
141     struct nicira_header *request;
142     char **argv;
143     char *args;
144     char *exec_file = NULL;
145     int max_fds;
146     struct stat s;
147     size_t args_size;
148     size_t argc;
149     size_t i;
150     pid_t pid;
151     int output_fds[2];
152
153     /* Check for NXT_COMMAND_REQUEST vendor extension. */
154     if (msg->size < sizeof(struct nicira_header)) {
155         return false;
156     }
157     request = msg->data;
158     if (request->header.type != OFPT_VENDOR
159         || request->vendor != htonl(NX_VENDOR_ID)
160         || request->subtype != htonl(NXT_COMMAND_REQUEST)) {
161         return false;
162     }
163
164     /* Verify limit on children not exceeded.
165      * XXX should probably kill children when the connection drops? */
166     if (e->n_children >= MAX_CHILDREN) {
167         VLOG_WARN("limit of %d child processes reached, dropping request",
168                   MAX_CHILDREN);
169         return false;
170     }
171
172     /* Copy argument buffer, adding a null terminator at the end.  Now every
173      * argument is null-terminated, instead of being merely null-delimited. */
174     args_size = msg->size - sizeof *request;
175     args = xmemdup0((const void *) (request + 1), args_size);
176
177     /* Count arguments. */
178     argc = 0;
179     for (i = 0; i <= args_size; i++) {
180         argc += args[i] == '\0';
181     }
182
183     /* Set argv[*] to point to each argument. */
184     argv = xmalloc((argc + 1) * sizeof *argv);
185     argv[0] = args;
186     for (i = 1; i < argc; i++) {
187         argv[i] = strchr(argv[i - 1], '\0') + 1;
188     }
189     argv[argc] = NULL;
190
191     /* Check permissions. */
192     if (!executer_is_permitted(e->s->command_acl, argv[0])) {
193         goto done;
194     }
195
196     /* Find the executable. */
197     exec_file = xasprintf("%s/%s", e->s->command_dir, argv[0]);
198     if (stat(exec_file, &s)) {
199         VLOG_WARN("failed to stat \"%s\": %s", exec_file, strerror(errno));
200         goto done;
201     }
202     if (!S_ISREG(s.st_mode)) {
203         VLOG_WARN("\"%s\" is not a regular file", exec_file);
204         goto done;
205     }
206     argv[0] = exec_file;
207
208     /* Arrange to capture output. */
209     if (pipe(output_fds)) {
210         VLOG_WARN("pipe failed: %s", strerror(errno));
211         goto done;
212     }
213
214     pid = fork();
215     if (!pid) {
216         /* Running in child.
217          * XXX should run in new process group so that we can signal all
218          * subprocesses at once?  Would also want to catch fatal signals and
219          * kill them at the same time though. */
220         fatal_signal_fork();
221         dup2(e->null_fd, 0);
222         dup2(output_fds[1], 1);
223         dup2(e->null_fd, 2);
224         max_fds = get_max_fds();
225         for (i = 3; i < max_fds; i++) {
226             close(i);
227         }
228         if (chdir(e->s->command_dir)) {
229             printf("could not change directory to \"%s\": %s",
230                    e->s->command_dir, strerror(errno));
231             exit(EXIT_FAILURE);
232         }
233         execv(argv[0], argv);
234         printf("failed to start \"%s\": %s\n", argv[0], strerror(errno));
235         exit(EXIT_FAILURE);
236     } else if (pid > 0) {
237         /* Running in parent. */
238         struct child *child;
239
240         VLOG_WARN("started \"%s\" subprocess", argv[0]);
241         child = &e->children[e->n_children++];
242         child->name = xstrdup(argv[0]);
243         child->pid = pid;
244         child->relay = r;
245         child->xid = request->header.xid;
246         child->output_fd = output_fds[0];
247         child->output = xmalloc(MAX_OUTPUT);
248         child->output_size = 0;
249         set_nonblocking(output_fds[0]);
250         close(output_fds[1]);
251     } else {
252         VLOG_WARN("fork failed: %s", strerror(errno));
253         close(output_fds[0]);
254         close(output_fds[1]);
255     }
256
257 done:
258     free(exec_file);
259     free(args);
260     free(argv);
261     return true;
262 }
263
264 /* 'child' died with 'status' as its return code.  Deal with it. */
265 static void
266 child_terminated(struct child *child, int status)
267 {
268     /* Log how it terminated. */
269     struct ds ds = DS_EMPTY_INITIALIZER;
270     if (WIFEXITED(status)) {
271         ds_put_format(&ds, "normally with status %d", WEXITSTATUS(status));
272     } else if (WIFSIGNALED(status)) {
273 #ifdef HAVE_STRSIGNAL
274         ds_put_format(&ds, "by signal %s", strsignal(WTERMSIG(status)));
275 #else
276         ds_put_format(&ds, "by signal %d", WTERMSIG(status));
277 #endif
278     }
279     if (WCOREDUMP(status)) {
280         ds_put_cstr(&ds, " (core dumped)");
281     }
282     VLOG_WARN("child process \"%s\" with pid %ld terminated %s",
283               child->name, (long int) child->pid, ds_cstr(&ds));
284     ds_destroy(&ds);
285
286     /* Send a status message back to the controller that requested the
287      * command. */
288     if (child->relay) {
289         struct nx_command_reply *r;
290         struct ofpbuf *buffer;
291
292         r = make_openflow_xid(sizeof *r, OFPT_VENDOR, child->xid, &buffer);
293         r->nxh.vendor = htonl(NX_VENDOR_ID);
294         r->nxh.subtype = htonl(NXT_COMMAND_REPLY);
295         if (WIFEXITED(status)) {
296             r->status = htonl(WEXITSTATUS(status) | NXT_STATUS_EXITED);
297         } else if (WIFSIGNALED(status)) {
298             r->status = htonl(WTERMSIG(status) | NXT_STATUS_SIGNALED);
299         } else {
300             r->status = htonl(NXT_STATUS_UNKNOWN);
301         }
302         if (WCOREDUMP(status)) {
303             r->status |= htonl(NXT_STATUS_COREDUMP);
304         }
305         ofpbuf_put(buffer, child->output, child->output_size);
306         update_openflow_length(buffer);
307         if (rconn_send(child->relay->halves[HALF_REMOTE].rconn, buffer,
308                        NULL)) {
309             ofpbuf_delete(buffer);
310         }
311     }
312 }
313
314 /* Read output from 'child' and append it to its output buffer. */
315 static void
316 poll_child(struct child *child)
317 {
318     ssize_t n;
319
320     if (child->output_fd < 0) {
321         return;
322     }
323
324     do {
325         n = read(child->output_fd, child->output + child->output_size,
326                  MAX_OUTPUT - child->output_size);
327     } while (n < 0 && errno == EINTR);
328     if (n > 0) {
329         child->output_size += n;
330         if (child->output_size < MAX_OUTPUT) {
331             return;
332         }
333     } else if (n < 0 && errno == EAGAIN) {
334         return;
335     }
336     close(child->output_fd);
337     child->output_fd = -1;
338 }
339
340 static void
341 executer_periodic_cb(void *e_)
342 {
343     struct executer *e = e_;
344     char buffer[MAX_CHILDREN];
345     size_t i;
346
347     if (!e->n_children) {
348         return;
349     }
350
351     /* Read output from children. */
352     for (i = 0; i < e->n_children; i++) {
353         struct child *child = &e->children[i];
354         poll_child(child);
355     }
356
357     /* If SIGCHLD was received, reap dead children. */
358     if (read(e->wait_fd, buffer, sizeof buffer) <= 0) {
359         return;
360     }
361     for (;;) {
362         int status;
363         pid_t pid;
364
365         /* Get dead child in 'pid' and its return code in 'status'. */
366         pid = waitpid(WAIT_ANY, &status, WNOHANG);
367         if (pid < 0 && errno == EINTR) {
368             continue;
369         } else if (pid <= 0) {
370             return;
371         }
372
373         /* Find child with given 'pid' and drop it from the list. */
374         for (i = 0; i < e->n_children; i++) {
375             struct child *child = &e->children[i];
376             if (child->pid == pid) {
377                 poll_child(child);
378                 child_terminated(child, status);
379                 free(child->name);
380                 free(child->output);
381                 *child = e->children[--e->n_children];
382                 goto found;
383             }
384         }
385         VLOG_WARN("child with unknown pid %ld terminated", (long int) pid);
386     found:;
387     }
388
389 }
390
391 static void
392 executer_wait_cb(void *e_)
393 {
394     struct executer *e = e_;
395     if (e->n_children) {
396         size_t i;
397
398         /* Wake up on SIGCHLD. */
399         poll_fd_wait(e->wait_fd, POLLIN);
400
401         /* Wake up when we get output from a child. */
402         for (i = 0; i < e->n_children; i++) {
403             struct child *child = &e->children[i];
404             if (child->output_fd >= 0) {
405                 poll_fd_wait(e->wait_fd, POLLIN);
406             }
407         }
408     }
409 }
410
411 static void
412 executer_closing_cb(struct relay *r, void *e_)
413 {
414     struct executer *e = e_;
415     size_t i;
416
417     /* If any of our children was connected to 'r', then disconnect it so we
418      * don't try to reference a dead connection when the process terminates
419      * later.
420      * XXX kill the children started by 'r'? */
421     for (i = 0; i < e->n_children; i++) {
422         if (e->children[i].relay == r) {
423             e->children[i].relay = NULL;
424         }
425     }
426 }
427
428 static int child_fd;
429
430 static void
431 sigchld_handler(int signr UNUSED)
432 {
433     write(child_fd, "", 1);
434 }
435
436 static struct hook_class executer_hook_class = {
437     NULL,                       /* local_packet_cb */
438     executer_remote_packet_cb,  /* remote_packet_cb */
439     executer_periodic_cb,       /* periodic_cb */
440     executer_wait_cb,           /* wait_cb */
441     executer_closing_cb,        /* closing_cb */
442 };
443
444 void
445 executer_start(struct secchan *secchan, const struct settings *settings)
446 {
447     struct executer *e;
448     struct sigaction sa;
449     int fds[2], null_fd;
450
451     /* Create pipe for notifying us that SIGCHLD was invoked. */
452     if (pipe(fds)) {
453         ofp_fatal(errno, "pipe failed");
454     }
455     set_nonblocking(fds[0]);
456     set_nonblocking(fds[1]);
457     child_fd = fds[1];
458
459     /* Open /dev/null. */
460     null_fd = open("/dev/null", O_RDWR);
461     if (null_fd < 0) {
462         ofp_fatal(errno, "could not open /dev/null");
463     }
464
465     /* Set up signal handler. */
466     memset(&sa, 0, sizeof sa);
467     sa.sa_handler = sigchld_handler;
468     sigemptyset(&sa.sa_mask);
469     sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
470     if (sigaction(SIGCHLD, &sa, NULL)) {
471         ofp_fatal(errno, "sigaction(SIGCHLD) faile");
472     }
473
474     /* Add hook. */
475     e = xcalloc(1, sizeof *e);
476     e->s = settings;
477     e->n_children = 0;
478     e->wait_fd = fds[0];
479     e->null_fd = null_fd;
480     add_hook(secchan, &executer_hook_class, e);
481 }