2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018 The FreeBSD Foundation
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/capsicum.h>
38 #include <sys/queue.h>
42 #include <netinet/in.h>
44 #include <capsicum_helpers.h>
50 #include <libcasper.h>
51 #include <libcasper_service.h>
56 * Run the script and return the write end of a pipe to the main process.
57 * Return -1 and set errno on error.
60 script_run(char **argv)
67 if ((pid = fork()) < 0)
71 null = open("/dev/null", O_RDWR);
74 if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
78 (void)execve(argv[0], argv, NULL);
87 cap_script_run(cap_channel_t *cap, const char *const *argv)
94 for (argc = 0; argv[argc] != NULL; argc++)
97 nvl = nvlist_create(0);
98 nvlist_add_string(nvl, "cmd", "script_run");
99 nvlist_add_string_array(nvl, "argv", argv, argc);
100 nvl = cap_xfer_nvlist(cap, nvl);
104 error = (int)dnvlist_get_number(nvl, "error", 0);
106 wfd = nvlist_take_descriptor(nvl, "fd");
110 return (error == 0 ? wfd : -1);
113 return (script_run(__DECONST(char **, argv)));
118 * Wait for a child process to exit, and return its status.
119 * Return -1 and set errno upon error.
122 script_wait(int *statusp)
126 error = wait(statusp);
127 return (error >= 0 ? 0 : -1);
131 cap_script_wait(cap_channel_t *cap, int *statusp)
137 nvl = nvlist_create(0);
138 nvlist_add_string(nvl, "cmd", "script_wait");
139 nvl = cap_xfer_nvlist(cap, nvl);
143 error = (int)dnvlist_get_number(nvl, "error", 0);
145 *statusp = (int)nvlist_get_number(nvl, "status");
149 return (error == 0 ? 0 : -1);
152 return (script_wait(statusp));
158 script_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
162 const char *const *iargv, *const *scripts;
164 size_t argc, i, nscripts;
167 if (strcmp(cmd, "script_wait") == 0) {
168 /* Wait for the result of a previous "script_run" command. */
169 if (script_wait(&status) == -1)
171 nvlist_add_number(nvlout, "status", status);
174 if (strcmp(cmd, "script_run") != 0)
178 * Validate the argv against the limits specified at initialization
181 iargv = nvlist_get_string_array(nvlin, "argv", &argc);
184 scripts = nvlist_get_string_array(limits, "scripts", &nscripts);
185 for (i = 0; i < nscripts; i++)
186 if (strcmp(iargv[0], scripts[i]) == 0)
192 * The nvlist API does not permit NULL pointers in an array, so we have
193 * to add the nul terminator ourselves. Yuck.
195 argv = calloc(argc + 1, sizeof(*argv));
198 memcpy(argv, iargv, sizeof(*argv) * argc);
200 fd = script_run(argv);
204 (void)caph_rights_limit(fd, cap_rights_init(&rights, CAP_WRITE));
205 nvlist_move_descriptor(nvlout, "fd", fd);
210 script_limit(const nvlist_t *oldlimits, const nvlist_t *newlimits)
217 /* Limits may only be set once. */
218 if (oldlimits != NULL)
219 return (ENOTCAPABLE);
223 while ((name = nvlist_next(newlimits, &nvtype, &cookie)) != NULL) {
224 if (nvtype == NV_TYPE_STRING_ARRAY &&
225 strcmp(name, "scripts") == 0)
235 CREATE_SERVICE("rtsold.script", script_limit, script_command, 0);
236 #endif /* WITH_CASPER */