rc: new ubus object for handling /etc/init.d/ scripts
[project/rpcd.git] / rc.c
1 // SPDX-License-Identifier: ISC OR MIT
2 /*
3 * rpcd - UBUS RPC server
4 *
5 * Copyright (C) 2020 Rafał Miłecki <rafal@milecki.pl>
6 */
7
8 #include <dirent.h>
9 #include <fcntl.h>
10 #include <linux/limits.h>
11 #include <sys/stat.h>
12 #include <sys/wait.h>
13
14 #include <libubox/blobmsg.h>
15 #include <libubox/ulog.h>
16 #include <libubox/uloop.h>
17 #include <libubus.h>
18
19 #include <rpcd/rc.h>
20
21 #define RC_LIST_EXEC_TIMEOUT_MS 3000
22
23 enum {
24 RC_INIT_NAME,
25 RC_INIT_ACTION,
26 __RC_INIT_MAX
27 };
28
29 static const struct blobmsg_policy rc_init_policy[] = {
30 [RC_INIT_NAME] = { "name", BLOBMSG_TYPE_STRING },
31 [RC_INIT_ACTION] = { "action", BLOBMSG_TYPE_STRING },
32 };
33
34 struct rc_list_context {
35 struct uloop_process process;
36 struct uloop_timeout timeout;
37 struct ubus_context *ctx;
38 struct ubus_request_data req;
39 struct blob_buf *buf;
40 DIR *dir;
41
42 /* Info about currently processed init.d entry */
43 struct {
44 char path[PATH_MAX];
45 const char *d_name;
46 unsigned int start;
47 unsigned int stop;
48 bool enabled;
49 bool running;
50 } entry;
51 };
52
53 static void rc_list_readdir(struct rc_list_context *c);
54
55 /**
56 * rc_check_script - check if script is safe to execute as root
57 *
58 * Check if it's owned by root and if only root can modify it.
59 */
60 static int rc_check_script(const char *path)
61 {
62 struct stat s;
63
64 if (stat(path, &s))
65 return UBUS_STATUS_NOT_FOUND;
66
67 if (s.st_uid != 0 || s.st_gid != 0 || !(s.st_mode & S_IXUSR) || (s.st_mode & S_IWOTH))
68 return UBUS_STATUS_PERMISSION_DENIED;
69
70 return UBUS_STATUS_OK;
71 }
72
73 static void rc_list_add_table(struct rc_list_context *c)
74 {
75 void *e;
76
77 e = blobmsg_open_table(c->buf, c->entry.d_name);
78
79 if (c->entry.start)
80 blobmsg_add_u16(c->buf, "start", c->entry.start);
81 if (c->entry.stop)
82 blobmsg_add_u16(c->buf, "stop", c->entry.stop);
83 blobmsg_add_u8(c->buf, "enabled", c->entry.enabled);
84 blobmsg_add_u8(c->buf, "running", c->entry.running);
85
86 blobmsg_close_table(c->buf, e);
87 }
88
89 static void rpc_list_exec_timeout_cb(struct uloop_timeout *t)
90 {
91 struct rc_list_context *c = container_of(t, struct rc_list_context, timeout);
92
93 ULOG_WARN("Timeout waiting for %s\n", c->entry.path);
94
95 uloop_process_delete(&c->process);
96 kill(c->process.pid, SIGKILL);
97
98 rc_list_readdir(c);
99 }
100
101 /**
102 * rc_exec - execute a file and call callback on complete
103 */
104 static int rc_list_exec(struct rc_list_context *c, const char *action, uloop_process_handler cb)
105 {
106 pid_t pid;
107 int err;
108 int fd;
109
110 pid = fork();
111 switch (pid) {
112 case -1:
113 return -errno;
114 case 0:
115 /* Set stdin, stdout & stderr to /dev/null */
116 fd = open("/dev/null", O_RDWR);
117 if (fd >= 0) {
118 dup2(fd, 0);
119 dup2(fd, 1);
120 dup2(fd, 2);
121 if (fd > 2)
122 close(fd);
123 }
124
125 uloop_end();
126
127 execl(c->entry.path, c->entry.path, action, NULL);
128 exit(errno);
129 default:
130 c->process.pid = pid;
131 c->process.cb = cb;
132
133 err = uloop_process_add(&c->process);
134 if (err)
135 return err;
136
137 c->timeout.cb = rpc_list_exec_timeout_cb;
138 err = uloop_timeout_set(&c->timeout, RC_LIST_EXEC_TIMEOUT_MS);
139 if (err) {
140 uloop_process_delete(&c->process);
141 return err;
142 }
143
144 return 0;
145 }
146 }
147
148 static void rc_list_exec_running_cb(struct uloop_process *p, int stat)
149 {
150 struct rc_list_context *c = container_of(p, struct rc_list_context, process);
151
152 uloop_timeout_cancel(&c->timeout);
153
154 c->entry.running = !stat;
155 rc_list_add_table(c);
156
157 rc_list_readdir(c);
158 }
159
160 static void rc_list_readdir(struct rc_list_context *c)
161 {
162 struct dirent *e;
163 FILE *fp;
164
165 e = readdir(c->dir);
166 if (!e) {
167 closedir(c->dir);
168 ubus_send_reply(c->ctx, &c->req, c->buf->head);
169 ubus_complete_deferred_request(c->ctx, &c->req, UBUS_STATUS_OK);
170 return;
171 }
172
173 if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
174 goto next;
175
176 memset(&c->entry, 0, sizeof(c->entry));
177
178 snprintf(c->entry.path, sizeof(c->entry.path), "/etc/init.d/%s", e->d_name);
179 if (rc_check_script(c->entry.path))
180 goto next;
181
182 c->entry.d_name = e->d_name;
183
184 fp = fopen(c->entry.path, "r");
185 if (fp) {
186 struct stat s;
187 char path[PATH_MAX];
188 char line[32];
189 bool beginning;
190
191 beginning = true;
192 while (!c->entry.start && !c->entry.stop && fgets(line, sizeof(line), fp)) {
193 if (beginning) {
194 if (!strncmp(line, "START=", 6)) {
195 c->entry.start = strtoul(line + 6, NULL, 0);
196 } else if (!strncmp(line, "STOP=", 5)) {
197 c->entry.stop = strtoul(line + 5, NULL, 0);
198 }
199 }
200
201 beginning = !!strchr(line, '\n');
202 }
203 fclose(fp);
204
205 snprintf(path, sizeof(path), "/etc/rc.d/S%02d%s", c->entry.start, c->entry.d_name);
206 if (!stat(path, &s) && (s.st_mode & S_IXUSR))
207 c->entry.enabled = true;
208 }
209
210 if (rc_list_exec(c, "running", rc_list_exec_running_cb))
211 goto next;
212
213 return;
214 next:
215 rc_list_readdir(c);
216 }
217
218 /**
219 * rc_list - allocate listing context and start reading directory
220 */
221 static int rc_list(struct ubus_context *ctx, struct ubus_object *obj,
222 struct ubus_request_data *req, const char *method,
223 struct blob_attr *msg)
224 {
225 static struct blob_buf buf;
226 struct rc_list_context *c;
227
228 blob_buf_init(&buf, 0);
229
230 c = calloc(1, sizeof(*c));
231 if (!c)
232 return UBUS_STATUS_UNKNOWN_ERROR;
233
234 c->ctx = ctx;
235 c->buf = &buf;
236 c->dir = opendir("/etc/init.d");
237 if (!c->dir) {
238 free(c);
239 return UBUS_STATUS_UNKNOWN_ERROR;
240 }
241
242 ubus_defer_request(ctx, req, &c->req);
243
244 rc_list_readdir(c);
245
246 return 0; /* Deferred */
247 }
248
249 struct rc_init_context {
250 struct uloop_process process;
251 struct ubus_context *ctx;
252 struct ubus_request_data req;
253 };
254
255 static void rc_init_cb(struct uloop_process *p, int stat)
256 {
257 struct rc_init_context *c = container_of(p, struct rc_init_context, process);
258
259 ubus_complete_deferred_request(c->ctx, &c->req, UBUS_STATUS_OK);
260
261 free(c);
262 }
263
264 static int rc_init(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
267 {
268 struct blob_attr *tb[__RC_INIT_MAX];
269 struct rc_init_context *c;
270 char path[PATH_MAX];
271 const char *action;
272 const char *name;
273 const char *chr;
274 pid_t pid;
275 int err;
276 int fd;
277
278 blobmsg_parse(rc_init_policy, __RC_INIT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
279
280 if (!tb[RC_INIT_NAME] || !tb[RC_INIT_ACTION])
281 return UBUS_STATUS_INVALID_ARGUMENT;
282
283 name = blobmsg_get_string(tb[RC_INIT_NAME]);
284
285 /* Validate script name */
286 for (chr = name; (chr = strchr(chr, '.')); chr++) {
287 if (*(chr + 1) == '.')
288 return UBUS_STATUS_INVALID_ARGUMENT;
289 }
290 if (strchr(name, '/'))
291 return UBUS_STATUS_INVALID_ARGUMENT;
292
293 snprintf(path, sizeof(path), "/etc/init.d/%s", name);
294
295 /* Validate script privileges */
296 err = rc_check_script(path);
297 if (err)
298 return err;
299
300 action = blobmsg_get_string(tb[RC_INIT_ACTION]);
301 if (strcmp(action, "disable") &&
302 strcmp(action, "enable") &&
303 strcmp(action, "stop") &&
304 strcmp(action, "start") &&
305 strcmp(action, "restart") &&
306 strcmp(action, "reload"))
307 return UBUS_STATUS_INVALID_ARGUMENT;
308
309 c = calloc(1, sizeof(*c));
310 if (!c)
311 return UBUS_STATUS_UNKNOWN_ERROR;
312
313 pid = fork();
314 switch (pid) {
315 case -1:
316 free(c);
317 return UBUS_STATUS_UNKNOWN_ERROR;
318 case 0:
319 /* Set stdin, stdout & stderr to /dev/null */
320 fd = open("/dev/null", O_RDWR);
321 if (fd >= 0) {
322 dup2(fd, 0);
323 dup2(fd, 1);
324 dup2(fd, 2);
325 if (fd > 2)
326 close(fd);
327 }
328
329 uloop_end();
330
331 execl(path, path, action, NULL);
332 exit(errno);
333 default:
334 c->ctx = ctx;
335 c->process.pid = pid;
336 c->process.cb = rc_init_cb;
337 uloop_process_add(&c->process);
338
339 ubus_defer_request(ctx, req, &c->req);
340
341 return 0; /* Deferred */
342 }
343 }
344
345 int rpc_rc_api_init(struct ubus_context *ctx)
346 {
347 static const struct ubus_method rc_methods[] = {
348 UBUS_METHOD_NOARG("list", rc_list),
349 UBUS_METHOD("init", rc_init, rc_init_policy),
350 };
351
352 static struct ubus_object_type rc_type =
353 UBUS_OBJECT_TYPE("rc", rc_methods);
354
355 static struct ubus_object obj = {
356 .name = "rc",
357 .type = &rc_type,
358 .methods = rc_methods,
359 .n_methods = ARRAY_SIZE(rc_methods),
360 };
361
362 return ubus_add_object(ctx, &obj);
363 }