2 * luci-rpcd - LuCI UBUS RPC server
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 static struct blob_buf buf
;
39 static const struct blobmsg_policy rpc_file_policy
[__RPC_F_MAX
] = {
40 [RPC_F_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
41 [RPC_F_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_STRING
},
52 static const struct blobmsg_policy rpc_exec_policy
[__RPC_E_MAX
] = {
53 [RPC_E_CMD
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
54 [RPC_E_PARM
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
55 [RPC_E_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_TABLE
},
56 [RPC_E_MODE
] = { .name
= "mode", .type
= BLOBMSG_TYPE_STRING
},
59 static const char *d_types
[] = {
62 [DT_DIR
] = "directory",
67 [DT_UNKNOWN
] = "unknown",
72 rpc_errno_status(void)
77 return UBUS_STATUS_PERMISSION_DENIED
;
80 return UBUS_STATUS_INVALID_ARGUMENT
;
83 return UBUS_STATUS_NOT_FOUND
;
86 return UBUS_STATUS_INVALID_ARGUMENT
;
89 return UBUS_STATUS_UNKNOWN_ERROR
;
93 static struct blob_attr
**
94 rpc_check_path(struct blob_attr
*msg
, char **path
, struct stat
*s
)
96 static struct blob_attr
*tb
[__RPC_F_MAX
];
98 blobmsg_parse(rpc_file_policy
, __RPC_F_MAX
, tb
, blob_data(msg
), blob_len(msg
));
106 *path
= blobmsg_data(tb
[RPC_F_PATH
]);
115 rpc_file_read(struct ubus_context
*ctx
, struct ubus_object
*obj
,
116 struct ubus_request_data
*req
, const char *method
,
117 struct blob_attr
*msg
)
124 if (!rpc_check_path(msg
, &path
, &s
))
125 return rpc_errno_status();
127 if (s
.st_size
>= RPC_FILE_MAX_SIZE
)
128 return UBUS_STATUS_NOT_SUPPORTED
;
130 if ((fd
= open(path
, O_RDONLY
)) < 0)
131 return rpc_errno_status();
133 /* some sysfs files do not report a length */
135 s
.st_size
= RPC_FILE_MIN_SIZE
;
137 blob_buf_init(&buf
, 0);
139 wbuf
= blobmsg_alloc_string_buffer(&buf
, "data", s
.st_size
+ 1);
143 rv
= UBUS_STATUS_UNKNOWN_ERROR
;
147 if ((len
= read(fd
, wbuf
, s
.st_size
)) <= 0)
149 rv
= UBUS_STATUS_NO_DATA
;
154 blobmsg_add_string_buffer(&buf
);
156 ubus_send_reply(ctx
, req
, buf
.head
);
165 rpc_file_write(struct ubus_context
*ctx
, struct ubus_object
*obj
,
166 struct ubus_request_data
*req
, const char *method
,
167 struct blob_attr
*msg
)
172 struct blob_attr
**tb
;
174 if (!(tb
= rpc_check_path(msg
, &path
, &s
)))
175 return rpc_errno_status();
178 return UBUS_STATUS_INVALID_ARGUMENT
;
180 if ((fd
= open(path
, O_WRONLY
)) < 0)
181 return rpc_errno_status();
183 write(fd
, blobmsg_data(tb
[RPC_F_DATA
]), blobmsg_data_len(tb
[RPC_F_DATA
]));
190 rpc_file_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
191 struct ubus_request_data
*req
, const char *method
,
192 struct blob_attr
*msg
)
200 if (!rpc_check_path(msg
, &path
, &s
))
201 return rpc_errno_status();
203 if ((fd
= opendir(path
)) == NULL
)
204 return rpc_errno_status();
206 blob_buf_init(&buf
, 0);
207 c
= blobmsg_open_array(&buf
, "entries");
209 while ((e
= readdir(fd
)) != NULL
)
211 if (!strcmp(e
->d_name
, ".") || !strcmp(e
->d_name
, ".."))
214 d
= blobmsg_open_table(&buf
, NULL
);
215 blobmsg_add_string(&buf
, "name", e
->d_name
);
216 blobmsg_add_string(&buf
, "type", d_types
[e
->d_type
]);
217 blobmsg_close_table(&buf
, d
);
220 blobmsg_close_array(&buf
, c
);
221 ubus_send_reply(ctx
, req
, buf
.head
);
227 rpc_file_stat(struct ubus_context
*ctx
, struct ubus_object
*obj
,
228 struct ubus_request_data
*req
, const char *method
,
229 struct blob_attr
*msg
)
235 if (!rpc_check_path(msg
, &path
, &s
))
236 return rpc_errno_status();
238 blob_buf_init(&buf
, 0);
240 type
= S_ISREG(s
.st_mode
) ? DT_REG
:
241 S_ISDIR(s
.st_mode
) ? DT_DIR
:
242 S_ISCHR(s
.st_mode
) ? DT_CHR
:
243 S_ISBLK(s
.st_mode
) ? DT_BLK
:
244 S_ISFIFO(s
.st_mode
) ? DT_FIFO
:
245 S_ISLNK(s
.st_mode
) ? DT_LNK
:
246 S_ISSOCK(s
.st_mode
) ? DT_SOCK
:
249 blobmsg_add_string(&buf
, "path", path
);
250 blobmsg_add_string(&buf
, "type", d_types
[type
]);
251 blobmsg_add_u32(&buf
, "size", s
.st_size
);
252 blobmsg_add_u32(&buf
, "mode", s
.st_mode
);
253 blobmsg_add_u32(&buf
, "atime", s
.st_atime
);
254 blobmsg_add_u32(&buf
, "mtime", s
.st_mtime
);
255 blobmsg_add_u32(&buf
, "ctime", s
.st_ctime
);
256 blobmsg_add_u32(&buf
, "inode", s
.st_ino
);
257 blobmsg_add_u32(&buf
, "uid", s
.st_uid
);
258 blobmsg_add_u32(&buf
, "gid", s
.st_gid
);
260 ubus_send_reply(ctx
, req
, buf
.head
);
266 rpc_file_exec_lookup(const char *cmd
)
269 int plen
= 0, clen
= strlen(cmd
) + 1;
271 static char path
[PATH_MAX
];
273 if (!stat(cmd
, &s
) && S_ISREG(s
.st_mode
))
276 search
= getenv("PATH");
279 search
= "/bin:/usr/bin:/sbin:/usr/sbin";
285 if (*p
!= ':' && *p
!= '\0')
290 if ((plen
+ clen
) >= sizeof(path
))
293 strncpy(path
, search
, plen
);
294 sprintf(path
+ plen
, "/%s", cmd
);
296 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
308 rpc_ustream_to_blobmsg(struct ustream
*s
, const char *name
)
313 if ((len
= ustream_pending_data(s
, false)) > 0)
315 wbuf
= blobmsg_alloc_string_buffer(&buf
, name
, len
+ 1);
320 ustream_for_each_read_buffer(s
, rbuf
, len
)
322 memcpy(wbuf
, rbuf
, len
);
327 blobmsg_add_string_buffer(&buf
);
332 rpc_file_exec_reply(struct rpc_file_exec_context
*c
, int rv
)
334 uloop_timeout_cancel(&c
->timeout
);
335 uloop_process_delete(&c
->process
);
337 if (rv
== UBUS_STATUS_OK
)
339 blob_buf_init(&buf
, 0);
341 blobmsg_add_u32(&buf
, "code", WEXITSTATUS(c
->stat
));
343 rpc_ustream_to_blobmsg(&c
->opipe
.stream
, "stdout");
344 rpc_ustream_to_blobmsg(&c
->epipe
.stream
, "stderr");
346 ubus_send_reply(c
->context
, &c
->request
, buf
.head
);
349 ubus_complete_deferred_request(c
->context
, &c
->request
, rv
);
351 ustream_free(&c
->opipe
.stream
);
352 ustream_free(&c
->epipe
.stream
);
354 close(c
->opipe
.fd
.fd
);
355 close(c
->epipe
.fd
.fd
);
361 rpc_file_exec_timeout_cb(struct uloop_timeout
*t
)
363 struct rpc_file_exec_context
*c
=
364 container_of(t
, struct rpc_file_exec_context
, timeout
);
366 kill(c
->process
.pid
, SIGKILL
);
367 rpc_file_exec_reply(c
, UBUS_STATUS_TIMEOUT
);
371 rpc_file_exec_process_cb(struct uloop_process
*p
, int stat
)
373 struct rpc_file_exec_context
*c
=
374 container_of(p
, struct rpc_file_exec_context
, process
);
378 ustream_poll(&c
->opipe
.stream
);
379 ustream_poll(&c
->epipe
.stream
);
383 rpc_file_exec_opipe_read_cb(struct ustream
*s
, int bytes
)
385 struct rpc_file_exec_context
*c
=
386 container_of(s
, struct rpc_file_exec_context
, opipe
);
388 if (ustream_read_buf_full(s
))
389 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
393 rpc_file_exec_epipe_read_cb(struct ustream
*s
, int bytes
)
395 struct rpc_file_exec_context
*c
=
396 container_of(s
, struct rpc_file_exec_context
, epipe
);
398 if (ustream_read_buf_full(s
))
399 rpc_file_exec_reply(c
, UBUS_STATUS_NOT_SUPPORTED
);
403 rpc_file_exec_opipe_state_cb(struct ustream
*s
)
405 struct rpc_file_exec_context
*c
=
406 container_of(s
, struct rpc_file_exec_context
, opipe
);
408 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
409 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
413 rpc_file_exec_epipe_state_cb(struct ustream
*s
)
415 struct rpc_file_exec_context
*c
=
416 container_of(s
, struct rpc_file_exec_context
, epipe
);
418 if (c
->opipe
.stream
.eof
&& c
->epipe
.stream
.eof
)
419 rpc_file_exec_reply(c
, UBUS_STATUS_OK
);
423 rpc_file_exec_run(const char *cmd
,
424 const struct blob_attr
*arg
, const struct blob_attr
*env
,
425 struct ubus_context
*ctx
, struct ubus_request_data
*req
)
433 struct blob_attr
*cur
;
438 struct rpc_file_exec_context
*c
;
440 cmd
= rpc_file_exec_lookup(cmd
);
443 return UBUS_STATUS_NOT_FOUND
;
445 c
= malloc(sizeof(*c
));
448 return UBUS_STATUS_UNKNOWN_ERROR
;
450 if (pipe(opipe
) || pipe(epipe
))
451 return rpc_errno_status();
453 switch ((pid
= fork()))
456 return rpc_errno_status();
471 args
= malloc(sizeof(char *) * arglen
);
474 return UBUS_STATUS_UNKNOWN_ERROR
;
476 args
[0] = (char *)cmd
;
481 blobmsg_for_each_attr(cur
, arg
, rem
)
483 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
488 if (!(args
= realloc(args
, sizeof(char *) * arglen
)))
489 return UBUS_STATUS_UNKNOWN_ERROR
;
491 args
[arglen
-2] = blobmsg_data(cur
);
492 args
[arglen
-1] = NULL
;
498 blobmsg_for_each_attr(cur
, env
, rem
)
500 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
503 setenv(blobmsg_name(cur
), blobmsg_data(cur
), 1);
507 if (execv(cmd
, args
))
508 return rpc_errno_status();
511 memset(c
, 0, sizeof(*c
));
513 ustream_declare(c
->opipe
, opipe
[0], exec_opipe
);
514 ustream_declare(c
->epipe
, epipe
[0], exec_epipe
);
516 c
->process
.pid
= pid
;
517 c
->process
.cb
= rpc_file_exec_process_cb
;
518 uloop_process_add(&c
->process
);
520 c
->timeout
.cb
= rpc_file_exec_timeout_cb
;
521 uloop_timeout_set(&c
->timeout
, RPC_FILE_MAX_RUNTIME
);
527 ubus_defer_request(ctx
, req
, &c
->request
);
530 return UBUS_STATUS_OK
;
534 rpc_file_exec(struct ubus_context
*ctx
, struct ubus_object
*obj
,
535 struct ubus_request_data
*req
, const char *method
,
536 struct blob_attr
*msg
)
538 struct blob_attr
*tb
[__RPC_E_MAX
];
540 blobmsg_parse(rpc_exec_policy
, __RPC_E_MAX
, tb
,
541 blob_data(msg
), blob_len(msg
));
544 return UBUS_STATUS_INVALID_ARGUMENT
;
546 return rpc_file_exec_run(blobmsg_data(tb
[RPC_E_CMD
]),
547 tb
[RPC_E_PARM
], tb
[RPC_E_ENV
], ctx
, req
);
551 int rpc_file_api_init(struct ubus_context
*ctx
)
553 static const struct ubus_method file_methods
[] = {
554 UBUS_METHOD("read", rpc_file_read
, rpc_file_policy
),
555 UBUS_METHOD("write", rpc_file_write
, rpc_file_policy
),
556 UBUS_METHOD("list", rpc_file_list
, rpc_file_policy
),
557 UBUS_METHOD("stat", rpc_file_stat
, rpc_file_policy
),
558 UBUS_METHOD("exec", rpc_file_exec
, rpc_exec_policy
),
561 static struct ubus_object_type file_type
=
562 UBUS_OBJECT_TYPE("luci-rpc-file", file_methods
);
564 static struct ubus_object obj
= {
567 .methods
= file_methods
,
568 .n_methods
= ARRAY_SIZE(file_methods
),
571 return ubus_add_object(ctx
, &obj
);