1 // SPDX-License-Identifier: ISC OR MIT
3 * rpcd - UBUS RPC server
5 * Copyright (C) 2020 Rafał Miłecki <rafal@milecki.pl>
10 #include <linux/limits.h>
14 #include <libubox/blobmsg.h>
15 #include <libubox/ulog.h>
16 #include <libubox/uloop.h>
21 #define RC_LIST_EXEC_TIMEOUT_MS 3000
24 RC_LIST_SKIP_RUNNING_CHECK
,
28 static const struct blobmsg_policy rc_list_policy
[] = {
29 [RC_LIST_SKIP_RUNNING_CHECK
] = { "skip_running_check", BLOBMSG_TYPE_BOOL
},
38 static const struct blobmsg_policy rc_init_policy
[] = {
39 [RC_INIT_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
40 [RC_INIT_ACTION
] = { "action", BLOBMSG_TYPE_STRING
},
43 struct rc_list_context
{
44 struct uloop_process process
;
45 struct uloop_timeout timeout
;
46 struct ubus_context
*ctx
;
47 struct ubus_request_data req
;
50 bool skip_running_check
;
52 /* Info about currently processed init.d entry */
64 static void rc_list_readdir(struct rc_list_context
*c
);
67 * rc_check_script - check if script is safe to execute as root
69 * Check if it's owned by root and if only root can modify it.
71 static int rc_check_script(const char *path
)
76 return UBUS_STATUS_NOT_FOUND
;
78 if (s
.st_uid
!= 0 || s
.st_gid
!= 0 || !(s
.st_mode
& S_IXUSR
) || (s
.st_mode
& S_IWOTH
))
79 return UBUS_STATUS_PERMISSION_DENIED
;
81 return UBUS_STATUS_OK
;
84 static void rc_list_add_table(struct rc_list_context
*c
)
88 e
= blobmsg_open_table(c
->buf
, c
->entry
.d_name
);
90 if (c
->entry
.start
>= 0)
91 blobmsg_add_u16(c
->buf
, "start", c
->entry
.start
);
92 if (c
->entry
.stop
>= 0)
93 blobmsg_add_u16(c
->buf
, "stop", c
->entry
.stop
);
94 blobmsg_add_u8(c
->buf
, "enabled", c
->entry
.enabled
);
95 if (!c
->skip_running_check
&& c
->entry
.use_procd
)
96 blobmsg_add_u8(c
->buf
, "running", c
->entry
.running
);
98 blobmsg_close_table(c
->buf
, e
);
101 static void rpc_list_exec_timeout_cb(struct uloop_timeout
*t
)
103 struct rc_list_context
*c
= container_of(t
, struct rc_list_context
, timeout
);
105 ULOG_WARN("Timeout waiting for %s\n", c
->entry
.path
);
107 uloop_process_delete(&c
->process
);
108 kill(c
->process
.pid
, SIGKILL
);
114 * rc_exec - execute a file and call callback on complete
116 static int rc_list_exec(struct rc_list_context
*c
, const char *action
, uloop_process_handler cb
)
127 if (c
->skip_running_check
)
130 if (!c
->entry
.use_procd
)
133 /* Set stdin, stdout & stderr to /dev/null */
134 fd
= open("/dev/null", O_RDWR
);
145 execl(c
->entry
.path
, c
->entry
.path
, action
, NULL
);
148 c
->process
.pid
= pid
;
151 err
= uloop_process_add(&c
->process
);
155 c
->timeout
.cb
= rpc_list_exec_timeout_cb
;
156 err
= uloop_timeout_set(&c
->timeout
, RC_LIST_EXEC_TIMEOUT_MS
);
158 uloop_process_delete(&c
->process
);
166 static void rc_list_exec_running_cb(struct uloop_process
*p
, int stat
)
168 struct rc_list_context
*c
= container_of(p
, struct rc_list_context
, process
);
170 uloop_timeout_cancel(&c
->timeout
);
172 c
->entry
.running
= !stat
;
173 rc_list_add_table(c
);
178 static void rc_list_readdir(struct rc_list_context
*c
)
186 ubus_send_reply(c
->ctx
, &c
->req
, c
->buf
->head
);
187 ubus_complete_deferred_request(c
->ctx
, &c
->req
, UBUS_STATUS_OK
);
191 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
194 memset(&c
->entry
, 0, sizeof(c
->entry
));
198 snprintf(c
->entry
.path
, sizeof(c
->entry
.path
), "/etc/init.d/%s", e
->d_name
);
199 if (rc_check_script(c
->entry
.path
))
202 c
->entry
.d_name
= e
->d_name
;
204 fp
= fopen(c
->entry
.path
, "r");
213 while ((c
->entry
.start
< 0 || c
->entry
.stop
< 0 ||
214 (!c
->skip_running_check
&& !c
->entry
.use_procd
)) &&
215 count
<= 10 && fgets(line
, sizeof(line
), fp
)) {
217 if (!strncmp(line
, "START=", 6)) {
218 c
->entry
.start
= strtoul(line
+ 6, NULL
, 0);
219 } else if (!strncmp(line
, "STOP=", 5)) {
220 c
->entry
.stop
= strtoul(line
+ 5, NULL
, 0);
221 } else if (!c
->skip_running_check
&& !strncmp(line
, "USE_PROCD=", 10)) {
222 c
->entry
.use_procd
= !!strtoul(line
+ 10, NULL
, 0);
227 beginning
= !!strchr(line
, '\n');
231 if (c
->entry
.start
>= 0) {
232 snprintf(path
, sizeof(path
), "/etc/rc.d/S%02d%s", c
->entry
.start
, c
->entry
.d_name
);
233 if (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
))
234 c
->entry
.enabled
= true;
238 if (rc_list_exec(c
, "running", rc_list_exec_running_cb
))
247 * rc_list - allocate listing context and start reading directory
249 static int rc_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
250 struct ubus_request_data
*req
, const char *method
,
251 struct blob_attr
*msg
)
253 struct blob_attr
*tb
[__RC_LIST_MAX
];
254 static struct blob_buf buf
;
255 struct rc_list_context
*c
;
257 blobmsg_parse(rc_list_policy
, __RC_LIST_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
259 blob_buf_init(&buf
, 0);
261 c
= calloc(1, sizeof(*c
));
263 return UBUS_STATUS_UNKNOWN_ERROR
;
267 c
->dir
= opendir("/etc/init.d");
270 return UBUS_STATUS_UNKNOWN_ERROR
;
272 if (tb
[RC_LIST_SKIP_RUNNING_CHECK
])
273 c
->skip_running_check
= blobmsg_get_bool(tb
[RC_LIST_SKIP_RUNNING_CHECK
]);
275 ubus_defer_request(ctx
, req
, &c
->req
);
279 return 0; /* Deferred */
282 struct rc_init_context
{
283 struct uloop_process process
;
284 struct ubus_context
*ctx
;
285 struct ubus_request_data req
;
288 static void rc_init_cb(struct uloop_process
*p
, int stat
)
290 struct rc_init_context
*c
= container_of(p
, struct rc_init_context
, process
);
292 ubus_complete_deferred_request(c
->ctx
, &c
->req
, UBUS_STATUS_OK
);
297 static int rc_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
298 struct ubus_request_data
*req
, const char *method
,
299 struct blob_attr
*msg
)
301 struct blob_attr
*tb
[__RC_INIT_MAX
];
302 struct rc_init_context
*c
;
311 blobmsg_parse(rc_init_policy
, __RC_INIT_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
313 if (!tb
[RC_INIT_NAME
] || !tb
[RC_INIT_ACTION
])
314 return UBUS_STATUS_INVALID_ARGUMENT
;
316 name
= blobmsg_get_string(tb
[RC_INIT_NAME
]);
318 /* Validate script name */
319 for (chr
= name
; (chr
= strchr(chr
, '.')); chr
++) {
320 if (*(chr
+ 1) == '.')
321 return UBUS_STATUS_INVALID_ARGUMENT
;
323 if (strchr(name
, '/'))
324 return UBUS_STATUS_INVALID_ARGUMENT
;
326 snprintf(path
, sizeof(path
), "/etc/init.d/%s", name
);
328 /* Validate script privileges */
329 err
= rc_check_script(path
);
333 action
= blobmsg_get_string(tb
[RC_INIT_ACTION
]);
334 if (strcmp(action
, "disable") &&
335 strcmp(action
, "enable") &&
336 strcmp(action
, "stop") &&
337 strcmp(action
, "start") &&
338 strcmp(action
, "restart") &&
339 strcmp(action
, "reload"))
340 return UBUS_STATUS_INVALID_ARGUMENT
;
342 c
= calloc(1, sizeof(*c
));
344 return UBUS_STATUS_UNKNOWN_ERROR
;
350 return UBUS_STATUS_UNKNOWN_ERROR
;
352 /* Set stdin, stdout & stderr to /dev/null */
353 fd
= open("/dev/null", O_RDWR
);
364 execl(path
, path
, action
, NULL
);
368 c
->process
.pid
= pid
;
369 c
->process
.cb
= rc_init_cb
;
370 uloop_process_add(&c
->process
);
372 ubus_defer_request(ctx
, req
, &c
->req
);
374 return 0; /* Deferred */
378 int rpc_rc_api_init(struct ubus_context
*ctx
)
380 static const struct ubus_method rc_methods
[] = {
381 UBUS_METHOD("list", rc_list
, rc_list_policy
),
382 UBUS_METHOD("init", rc_init
, rc_init_policy
),
385 static struct ubus_object_type rc_type
=
386 UBUS_OBJECT_TYPE("rc", rc_methods
);
388 static struct ubus_object obj
= {
391 .methods
= rc_methods
,
392 .n_methods
= ARRAY_SIZE(rc_methods
),
395 return ubus_add_object(ctx
, &obj
);