diff mbox

[OpenWrt-Devel,3/3,rpcd] file: add support for setting mode when writing files

Message ID 1426453252-30326-3-git-send-email-luka@openwrt.org
State Changes Requested
Headers show

Commit Message

Luka Perkov March 15, 2015, 9 p.m. UTC
Signed-off-by: Luka Perkov <luka@openwrt.org>
---
 file.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Comments

John Crispin March 15, 2015, 9:27 p.m. UTC | #1
On 15/03/2015 22:00, Luka Perkov wrote:
> Signed-off-by: Luka Perkov <luka@openwrt.org>
> ---
>  file.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/file.c b/file.c
> index 54f5d8a..95dd4d4 100644
> --- a/file.c
> +++ b/file.c
> @@ -92,6 +92,7 @@ static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
>  enum {
>  	RPC_F_RW_PATH,
>  	RPC_F_RW_DATA,
> +	RPC_F_RW_MODE,
>  	RPC_F_RW_BASE64,
>  	__RPC_F_RW_MAX,
>  };
> @@ -99,6 +100,7 @@ enum {
>  static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
>  	[RPC_F_RW_PATH]   = { .name = "path",   .type = BLOBMSG_TYPE_STRING },
>  	[RPC_F_RW_DATA]   = { .name = "data",   .type = BLOBMSG_TYPE_STRING },
> +	[RPC_F_RW_MODE]   = { .name = "mode",   .type = BLOBMSG_TYPE_INT32  },
>  	[RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL   },
>  };
>  
> @@ -372,6 +374,7 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
>  {
>  	struct blob_attr *tb[__RPC_F_RW_MAX];
>  	bool base64 = false;
> +	mode_t mode = 0644;
>  	int fd, rv = 0;
>  	void *rbuf;
>  	size_t rbuf_len;
> @@ -382,12 +385,16 @@ rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
>  	if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
>  		return UBUS_STATUS_INVALID_ARGUMENT;
>  
> -	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC)) < 0)
> -		return rpc_errno_status();
> +	if (tb[RPC_F_RW_MODE])
> +		mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
>  
>  	if (tb[RPC_F_RW_BASE64])
>  		base64 = blobmsg_get_bool(tb[RPC_F_RW_BASE64]);
>  
> +	umask(0);

setting umask to 0 and then not resetting it to the old value afterwards
seems wrong.

->> main.c:	umask(0077);

> +	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC, mode)) < 0)
> +		return rpc_errno_status();
> +
>  	if (base64) {
>  		rbuf_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
>  		rbuf = b64decode(blobmsg_data(tb[RPC_F_RW_DATA]), &rbuf_len);
>
Luka Perkov March 16, 2015, 5:42 p.m. UTC | #2
Hi John,

On Sun, Mar 15, 2015 at 10:27:49PM +0100, John Crispin wrote:
> > +	umask(0);
> 
> setting umask to 0 and then not resetting it to the old value afterwards
> seems wrong.
> 
> ->> main.c:	umask(0077);

Good catch. Will send v2.

Luka
diff mbox

Patch

diff --git a/file.c b/file.c
index 54f5d8a..95dd4d4 100644
--- a/file.c
+++ b/file.c
@@ -92,6 +92,7 @@  static const struct blobmsg_policy rpc_file_rb_policy[__RPC_F_RB_MAX] = {
 enum {
 	RPC_F_RW_PATH,
 	RPC_F_RW_DATA,
+	RPC_F_RW_MODE,
 	RPC_F_RW_BASE64,
 	__RPC_F_RW_MAX,
 };
@@ -99,6 +100,7 @@  enum {
 static const struct blobmsg_policy rpc_file_rw_policy[__RPC_F_RW_MAX] = {
 	[RPC_F_RW_PATH]   = { .name = "path",   .type = BLOBMSG_TYPE_STRING },
 	[RPC_F_RW_DATA]   = { .name = "data",   .type = BLOBMSG_TYPE_STRING },
+	[RPC_F_RW_MODE]   = { .name = "mode",   .type = BLOBMSG_TYPE_INT32  },
 	[RPC_F_RW_BASE64] = { .name = "base64", .type = BLOBMSG_TYPE_BOOL   },
 };
 
@@ -372,6 +374,7 @@  rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
 {
 	struct blob_attr *tb[__RPC_F_RW_MAX];
 	bool base64 = false;
+	mode_t mode = 0644;
 	int fd, rv = 0;
 	void *rbuf;
 	size_t rbuf_len;
@@ -382,12 +385,16 @@  rpc_file_write(struct ubus_context *ctx, struct ubus_object *obj,
 	if (!tb[RPC_F_RW_PATH] || !tb[RPC_F_RW_DATA])
 		return UBUS_STATUS_INVALID_ARGUMENT;
 
-	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC)) < 0)
-		return rpc_errno_status();
+	if (tb[RPC_F_RW_MODE])
+		mode = blobmsg_get_u32(tb[RPC_F_RW_MODE]);
 
 	if (tb[RPC_F_RW_BASE64])
 		base64 = blobmsg_get_bool(tb[RPC_F_RW_BASE64]);
 
+	umask(0);
+	if ((fd = open(blobmsg_data(tb[RPC_F_RW_PATH]), O_CREAT | O_WRONLY | O_TRUNC, mode)) < 0)
+		return rpc_errno_status();
+
 	if (base64) {
 		rbuf_len = blobmsg_data_len(tb[RPC_F_RW_DATA]) - 1;
 		rbuf = b64decode(blobmsg_data(tb[RPC_F_RW_DATA]), &rbuf_len);