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

2015-03-16 Thread Luka Perkov
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
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


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

2015-03-15 Thread Luka Perkov
Signed-off-by: Luka Perkov l...@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);
+   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);
-- 
2.3.3
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


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

2015-03-15 Thread John Crispin


On 15/03/2015 22:00, Luka Perkov wrote:
 Signed-off-by: Luka Perkov l...@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);
 
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel