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
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
},
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
;
42 /* Info about currently processed init.d entry */
53 static void rc_list_readdir(struct rc_list_context
*c
);
56 * rc_check_script - check if script is safe to execute as root
58 * Check if it's owned by root and if only root can modify it.
60 static int rc_check_script(const char *path
)
65 return UBUS_STATUS_NOT_FOUND
;
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
;
70 return UBUS_STATUS_OK
;
73 static void rc_list_add_table(struct rc_list_context
*c
)
77 e
= blobmsg_open_table(c
->buf
, c
->entry
.d_name
);
79 if (c
->entry
.start
>= 0)
80 blobmsg_add_u16(c
->buf
, "start", c
->entry
.start
);
81 if (c
->entry
.stop
>= 0)
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
);
86 blobmsg_close_table(c
->buf
, e
);
89 static void rpc_list_exec_timeout_cb(struct uloop_timeout
*t
)
91 struct rc_list_context
*c
= container_of(t
, struct rc_list_context
, timeout
);
93 ULOG_WARN("Timeout waiting for %s\n", c
->entry
.path
);
95 uloop_process_delete(&c
->process
);
96 kill(c
->process
.pid
, SIGKILL
);
102 * rc_exec - execute a file and call callback on complete
104 static int rc_list_exec(struct rc_list_context
*c
, const char *action
, uloop_process_handler cb
)
115 /* Set stdin, stdout & stderr to /dev/null */
116 fd
= open("/dev/null", O_RDWR
);
127 execl(c
->entry
.path
, c
->entry
.path
, action
, NULL
);
130 c
->process
.pid
= pid
;
133 err
= uloop_process_add(&c
->process
);
137 c
->timeout
.cb
= rpc_list_exec_timeout_cb
;
138 err
= uloop_timeout_set(&c
->timeout
, RC_LIST_EXEC_TIMEOUT_MS
);
140 uloop_process_delete(&c
->process
);
148 static void rc_list_exec_running_cb(struct uloop_process
*p
, int stat
)
150 struct rc_list_context
*c
= container_of(p
, struct rc_list_context
, process
);
152 uloop_timeout_cancel(&c
->timeout
);
154 c
->entry
.running
= !stat
;
155 rc_list_add_table(c
);
160 static void rc_list_readdir(struct rc_list_context
*c
)
168 ubus_send_reply(c
->ctx
, &c
->req
, c
->buf
->head
);
169 ubus_complete_deferred_request(c
->ctx
, &c
->req
, UBUS_STATUS_OK
);
173 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
176 memset(&c
->entry
, 0, sizeof(c
->entry
));
180 snprintf(c
->entry
.path
, sizeof(c
->entry
.path
), "/etc/init.d/%s", e
->d_name
);
181 if (rc_check_script(c
->entry
.path
))
184 c
->entry
.d_name
= e
->d_name
;
186 fp
= fopen(c
->entry
.path
, "r");
195 while ((c
->entry
.start
< 0 || c
->entry
.stop
< 0) &&
196 count
<= 10 && fgets(line
, sizeof(line
), fp
)) {
198 if (!strncmp(line
, "START=", 6)) {
199 c
->entry
.start
= strtoul(line
+ 6, NULL
, 0);
200 } else if (!strncmp(line
, "STOP=", 5)) {
201 c
->entry
.stop
= strtoul(line
+ 5, NULL
, 0);
206 beginning
= !!strchr(line
, '\n');
210 if (c
->entry
.start
>= 0) {
211 snprintf(path
, sizeof(path
), "/etc/rc.d/S%02d%s", c
->entry
.start
, c
->entry
.d_name
);
212 if (!stat(path
, &s
) && (s
.st_mode
& S_IXUSR
))
213 c
->entry
.enabled
= true;
217 if (rc_list_exec(c
, "running", rc_list_exec_running_cb
))
226 * rc_list - allocate listing context and start reading directory
228 static int rc_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
229 struct ubus_request_data
*req
, const char *method
,
230 struct blob_attr
*msg
)
232 static struct blob_buf buf
;
233 struct rc_list_context
*c
;
235 blob_buf_init(&buf
, 0);
237 c
= calloc(1, sizeof(*c
));
239 return UBUS_STATUS_UNKNOWN_ERROR
;
243 c
->dir
= opendir("/etc/init.d");
246 return UBUS_STATUS_UNKNOWN_ERROR
;
249 ubus_defer_request(ctx
, req
, &c
->req
);
253 return 0; /* Deferred */
256 struct rc_init_context
{
257 struct uloop_process process
;
258 struct ubus_context
*ctx
;
259 struct ubus_request_data req
;
262 static void rc_init_cb(struct uloop_process
*p
, int stat
)
264 struct rc_init_context
*c
= container_of(p
, struct rc_init_context
, process
);
266 ubus_complete_deferred_request(c
->ctx
, &c
->req
, UBUS_STATUS_OK
);
271 static int rc_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
272 struct ubus_request_data
*req
, const char *method
,
273 struct blob_attr
*msg
)
275 struct blob_attr
*tb
[__RC_INIT_MAX
];
276 struct rc_init_context
*c
;
285 blobmsg_parse(rc_init_policy
, __RC_INIT_MAX
, tb
, blobmsg_data(msg
), blobmsg_data_len(msg
));
287 if (!tb
[RC_INIT_NAME
] || !tb
[RC_INIT_ACTION
])
288 return UBUS_STATUS_INVALID_ARGUMENT
;
290 name
= blobmsg_get_string(tb
[RC_INIT_NAME
]);
292 /* Validate script name */
293 for (chr
= name
; (chr
= strchr(chr
, '.')); chr
++) {
294 if (*(chr
+ 1) == '.')
295 return UBUS_STATUS_INVALID_ARGUMENT
;
297 if (strchr(name
, '/'))
298 return UBUS_STATUS_INVALID_ARGUMENT
;
300 snprintf(path
, sizeof(path
), "/etc/init.d/%s", name
);
302 /* Validate script privileges */
303 err
= rc_check_script(path
);
307 action
= blobmsg_get_string(tb
[RC_INIT_ACTION
]);
308 if (strcmp(action
, "disable") &&
309 strcmp(action
, "enable") &&
310 strcmp(action
, "stop") &&
311 strcmp(action
, "start") &&
312 strcmp(action
, "restart") &&
313 strcmp(action
, "reload"))
314 return UBUS_STATUS_INVALID_ARGUMENT
;
316 c
= calloc(1, sizeof(*c
));
318 return UBUS_STATUS_UNKNOWN_ERROR
;
324 return UBUS_STATUS_UNKNOWN_ERROR
;
326 /* Set stdin, stdout & stderr to /dev/null */
327 fd
= open("/dev/null", O_RDWR
);
338 execl(path
, path
, action
, NULL
);
342 c
->process
.pid
= pid
;
343 c
->process
.cb
= rc_init_cb
;
344 uloop_process_add(&c
->process
);
346 ubus_defer_request(ctx
, req
, &c
->req
);
348 return 0; /* Deferred */
352 int rpc_rc_api_init(struct ubus_context
*ctx
)
354 static const struct ubus_method rc_methods
[] = {
355 UBUS_METHOD_NOARG("list", rc_list
),
356 UBUS_METHOD("init", rc_init
, rc_init_policy
),
359 static struct ubus_object_type rc_type
=
360 UBUS_OBJECT_TYPE("rc", rc_methods
);
362 static struct ubus_object obj
= {
365 .methods
= rc_methods
,
366 .n_methods
= ARRAY_SIZE(rc_methods
),
369 return ubus_add_object(ctx
, &obj
);