uci: copy permisions of /etc/config/ files for temp files
authorEtienne CHAMPETIER <etienne.champetier@free.fr>
Tue, 5 Feb 2013 17:09:34 +0000 (17:09 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 24 Apr 2013 09:23:35 +0000 (11:23 +0200)
Hi

Using uci as a non root user i can get network configuration but not network state (for exemple).
The idea of this patch is to copy permission from config file, or if it doesn't exist use UCI_FILEMODE / UCI_DIRMODE

Tested on tplink wr1043nd. (you have to remove some mkdir from /etc/init.d/boot to fully see this patch work)
If you apply this patch (or a modified version), please also backport to AA

Signed-off-by: Etienne CHAMPETIER <etienne.champetier@free.fr>
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

delta.c
util.c

diff --git a/delta.c b/delta.c
index a041f541dcc404986a677e9bd4976ebdefecdac7..50efc07d3cd82b2d6ab3ae6da1ae6a78aa177753 100644 (file)
--- a/delta.c
+++ b/delta.c
@@ -425,10 +425,15 @@ int uci_save(struct uci_context *ctx, struct uci_package *p)
        if (uci_list_empty(&p->delta))
                return 0;
 
        if (uci_list_empty(&p->delta))
                return 0;
 
-       if (stat(ctx->savedir, &statbuf) < 0)
-               mkdir(ctx->savedir, UCI_DIRMODE);
-       else if ((statbuf.st_mode & S_IFMT) != S_IFDIR)
+       if (stat(ctx->savedir, &statbuf) < 0) {
+               if (stat(ctx->confdir, &statbuf) == 0) {
+                       mkdir(ctx->savedir, statbuf.st_mode);
+               } else {
+                       mkdir(ctx->savedir, UCI_DIRMODE);
+               }
+       } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
                UCI_THROW(ctx, UCI_ERR_IO);
                UCI_THROW(ctx, UCI_ERR_IO);
+       }
 
        if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
                UCI_THROW(ctx, UCI_ERR_MEM);
 
        if ((asprintf(&filename, "%s/%s", ctx->savedir, p->e.name) < 0) || !filename)
                UCI_THROW(ctx, UCI_ERR_MEM);
diff --git a/util.c b/util.c
index 3725ec19a6d49780394be6bafb0299bf96ec959b..7cad0d1f408bd72d3b61d30107de817a4ecd5524 100644 (file)
--- a/util.c
+++ b/util.c
@@ -182,17 +182,30 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, i
        struct stat statbuf;
        FILE *file = NULL;
        int fd, ret;
        struct stat statbuf;
        FILE *file = NULL;
        int fd, ret;
-       int mode = (write ? O_RDWR : O_RDONLY);
-
-       if (create)
-               mode |= O_CREAT;
+       int flags = (write ? O_RDWR : O_RDONLY);
+       mode_t mode = UCI_FILEMODE;
+       char *name = NULL;
+       char *filename2 = NULL;
+
+       if (create) {
+               flags |= O_CREAT;
+               name = basename(filename);
+               if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
+                       UCI_THROW(ctx, UCI_ERR_MEM);
+               } else {
+                       if (stat(filename2,&statbuf) == 0)
+                               mode = statbuf.st_mode;
+
+                       free(filename2);
+               }
+       }
 
        if (!write && ((stat(filename, &statbuf) < 0) ||
                ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
        }
 
 
        if (!write && ((stat(filename, &statbuf) < 0) ||
                ((statbuf.st_mode &  S_IFMT) != S_IFREG))) {
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
        }
 
-       fd = open(filename, mode, UCI_FILEMODE);
+       fd = open(filename, flags, mode);
        if (fd < 0)
                goto error;
 
        if (fd < 0)
                goto error;