2 * rpcd - UBUS RPC server
4 * Copyright (C) 2013-2014 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.
21 #include <rpcd/exec.h>
22 #include <rpcd/plugin.h>
23 #include <rpcd/session.h>
24 #include <sys/reboot.h>
26 static const struct rpc_daemon_ops
*ops
;
34 static const struct blobmsg_policy rpc_password_policy
[__RPC_P_MAX
] = {
35 [RPC_P_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
36 [RPC_P_PASSWORD
] = { .name
= "password", .type
= BLOBMSG_TYPE_STRING
},
44 static const struct blobmsg_policy rpc_upgrade_policy
[__RPC_UPGRADE_MAX
] = {
45 [RPC_UPGRADE_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
49 rpc_errno_status(void)
54 return UBUS_STATUS_PERMISSION_DENIED
;
57 return UBUS_STATUS_INVALID_ARGUMENT
;
60 return UBUS_STATUS_NOT_FOUND
;
63 return UBUS_STATUS_INVALID_ARGUMENT
;
66 return UBUS_STATUS_UNKNOWN_ERROR
;
71 rpc_cgi_password_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
72 struct ubus_request_data
*req
, const char *method
,
73 struct blob_attr
*msg
)
78 struct blob_attr
*tb
[__RPC_P_MAX
];
82 blobmsg_parse(rpc_password_policy
, __RPC_P_MAX
, tb
,
83 blob_data(msg
), blob_len(msg
));
85 if (!tb
[RPC_P_USER
] || !tb
[RPC_P_PASSWORD
])
86 return UBUS_STATUS_INVALID_ARGUMENT
;
88 if (stat("/usr/bin/passwd", &s
))
89 return UBUS_STATUS_NOT_FOUND
;
91 if (!(s
.st_mode
& S_IXUSR
))
92 return UBUS_STATUS_PERMISSION_DENIED
;
95 return rpc_errno_status();
97 switch ((pid
= fork()))
102 return rpc_errno_status();
111 if ((fd
= open("/dev/null", O_RDWR
)) > -1)
120 return rpc_errno_status();
122 if (execl("/usr/bin/passwd", "/usr/bin/passwd",
123 blobmsg_data(tb
[RPC_P_USER
]), NULL
))
124 return rpc_errno_status();
129 n
= write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
130 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
132 return rpc_errno_status();
134 n
= write(fds
[1], "\n", 1);
136 return rpc_errno_status();
140 n
= write(fds
[1], blobmsg_data(tb
[RPC_P_PASSWORD
]),
141 blobmsg_data_len(tb
[RPC_P_PASSWORD
]) - 1);
143 return rpc_errno_status();
144 n
= write(fds
[1], "\n", 1);
146 return rpc_errno_status();
150 waitpid(pid
, NULL
, 0);
157 rpc_sys_upgrade_test(struct ubus_context
*ctx
, struct ubus_object
*obj
,
158 struct ubus_request_data
*req
, const char *method
,
159 struct blob_attr
*msg
)
161 const char *cmd
[4] = { "sysupgrade", "--test", "/tmp/firmware.bin", NULL
};
162 return ops
->exec(cmd
, NULL
, NULL
, NULL
, NULL
, NULL
, ctx
, req
);
166 rpc_sys_upgrade_start(struct ubus_context
*ctx
, struct ubus_object
*obj
,
167 struct ubus_request_data
*req
, const char *method
,
168 struct blob_attr
*msg
)
170 struct blob_attr
*tb
[__RPC_UPGRADE_MAX
];
171 char * const cmd
[4] = { "/sbin/sysupgrade", "-n", "/tmp/firmware.bin", NULL
};
172 char * const cmd_keep
[3] = { "/sbin/sysupgrade", "/tmp/firmware.bin", NULL
};
173 char * const * c
= cmd
;
175 blobmsg_parse(rpc_upgrade_policy
, __RPC_UPGRADE_MAX
, tb
,
176 blob_data(msg
), blob_len(msg
));
178 if (tb
[RPC_UPGRADE_KEEP
] && blobmsg_get_bool(tb
[RPC_UPGRADE_KEEP
]))
182 /* wait for the RPC call to complete */
184 return execv(c
[0], c
);
191 rpc_sys_upgrade_clean(struct ubus_context
*ctx
, struct ubus_object
*obj
,
192 struct ubus_request_data
*req
, const char *method
,
193 struct blob_attr
*msg
)
195 if (unlink("/tmp/firmware.bin"))
196 return rpc_errno_status();
202 rpc_sys_factory(struct ubus_context
*ctx
, struct ubus_object
*obj
,
203 struct ubus_request_data
*req
, const char *method
,
204 struct blob_attr
*msg
)
206 char * const cmd
[4] = { "/sbin/jffs2reset", "-y", "-r", NULL
};
209 /* wait for the RPC call to complete */
211 return execv(cmd
[0], cmd
);
218 rpc_sys_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
219 struct ubus_request_data
*req
, const char *method
,
220 struct blob_attr
*msg
)
234 rpc_sys_api_init(const struct rpc_daemon_ops
*o
, struct ubus_context
*ctx
)
236 static const struct ubus_method sys_methods
[] = {
237 UBUS_METHOD("password_set", rpc_cgi_password_set
, rpc_password_policy
),
238 UBUS_METHOD_NOARG("upgrade_test", rpc_sys_upgrade_test
),
239 UBUS_METHOD("upgrade_start", rpc_sys_upgrade_start
,
241 UBUS_METHOD_NOARG("upgrade_clean", rpc_sys_upgrade_clean
),
242 UBUS_METHOD_NOARG("factory", rpc_sys_factory
),
243 UBUS_METHOD_NOARG("reboot", rpc_sys_reboot
),
246 static struct ubus_object_type sys_type
=
247 UBUS_OBJECT_TYPE("luci-rpc-sys", sys_methods
);
249 static struct ubus_object obj
= {
252 .methods
= sys_methods
,
253 .n_methods
= ARRAY_SIZE(sys_methods
),
258 return ubus_add_object(ctx
, &obj
);
261 struct rpc_plugin rpc_plugin
= {
262 .init
= rpc_sys_api_init