]> git.proxmox.com Git - pve-cluster.git/blobdiff - data/src/pmxcfs.c
implement chown and chmod for user root group www-data and perm 0640
[pve-cluster.git] / data / src / pmxcfs.c
index be90618d2adb039f6511bfbf5a956b6835b12997..0f0993785757b2c991af5fce0923b5195f02aeae 100644 (file)
@@ -41,7 +41,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+
 #include <qb/qbdefs.h>
 #include <qb/qbutil.h>
 #include <qb/qblog.h>
@@ -59,7 +59,7 @@
 #define DBFILENAME VARLIBDIR "/config.db"
 #define LOCKFILE VARLIBDIR "/.pmxcfs.lockfile"
 
-#define CFSDIR "/etc/pve" 
+#define CFSDIR "/etc/pve"
 
 cfs_t cfs = {
        .debug = 0,
@@ -108,7 +108,7 @@ static cfs_plug_t *find_plug(const char *path, char **sub)
 
        cfs_debug("find_plug end %s = %p (%s)", path, plug, subpath);
 
-       if (subpath && subpath[0]) 
+       if (subpath && subpath[0])
                *sub = g_strdup(subpath);
 
        g_free(tmppath);
@@ -129,7 +129,7 @@ static int cfs_fuse_getattr(const char *path, struct stat *stbuf)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (plug && plug->ops && plug->ops->getattr) {
                ret = plug->ops->getattr(plug, subpath ? subpath : "", stbuf);
 
@@ -171,7 +171,7 @@ static int cfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -179,10 +179,44 @@ static int cfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                ret = plug->ops->readdir(plug, subpath ? subpath : "", buf, filler, 0, fi);
 ret:
        cfs_debug("leave cfs_fuse_readdir %s (%d)", path, ret);
-               
+
        if (subpath)
                g_free(subpath);
-       
+
+       return ret;
+}
+
+static int cfs_fuse_chmod(const char *path, mode_t mode)
+{
+       int ret = -EPERM;
+
+       cfs_debug("enter cfs_fuse_chmod %s", path);
+
+       mode_t allowed_mode = (S_IRUSR | S_IWUSR);
+       if (!path_is_private(path))
+               allowed_mode |= (S_IRGRP);
+
+       // allow only setting our supported modes (0600 for priv, 0640 for rest)
+       if (mode == allowed_mode)
+               ret = 0;
+
+       cfs_debug("leave cfs_fuse_chmod %s (%d) mode: %o", path, ret, (int)mode);
+
+       return ret;
+}
+
+static int cfs_fuse_chown(const char *path, uid_t user, gid_t group)
+{
+       int ret = -EPERM;
+
+       cfs_debug("enter cfs_fuse_chown %s", path);
+
+       // we get -1 if no change should be made
+       if ((user == 0 || user == -1) && (group == cfs.gid || group == -1))
+               ret = 0;
+
+       cfs_debug("leave cfs_fuse_chown %s (%d) (uid: %d; gid: %d)", path, ret, user, group);
+
        return ret;
 }
 
@@ -194,7 +228,7 @@ static int cfs_fuse_mkdir(const char *path, mode_t mode)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -218,7 +252,7 @@ static int cfs_fuse_rmdir(const char *path)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -275,8 +309,8 @@ static int cfs_fuse_open(const char *path, struct fuse_file_info *fi)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->open) {
                        ret = plug->ops->open(plug, subpath ? subpath : "", fi);
                }
@@ -295,14 +329,14 @@ static int cfs_fuse_read(const char *path, char *buf, size_t size, off_t offset,
 {
        (void) fi;
 
-       cfs_debug("enter cfs_fuse_read %s %lu %ld", path, size, offset);
+       cfs_debug("enter cfs_fuse_read %s %zu %jd", path, size, offset);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->read)
                        ret = plug->ops->read(plug, subpath ? subpath : "", buf, size, offset, fi);
        }
@@ -320,19 +354,19 @@ static int cfs_fuse_write(const char *path, const char *buf, size_t size,
 {
        (void) fi;
 
-       cfs_debug("enter cfs_fuse_write %s %lu %ld", path, size, offset);
+       cfs_debug("enter cfs_fuse_write %s %zu %jd", path, size, offset);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->write)
-               ret = plug->ops->write(plug, subpath ? subpath : "", 
+               ret = plug->ops->write(plug, subpath ? subpath : "",
                                       buf, size, offset, fi);
        }
-       
+
        cfs_debug("leave cfs_fuse_write %s (%d)", path, ret);
 
        if (subpath)
@@ -343,14 +377,14 @@ static int cfs_fuse_write(const char *path, const char *buf, size_t size,
 
 static int cfs_fuse_truncate(const char *path, off_t size)
 {
-       cfs_debug("enter cfs_fuse_truncate %s %ld", path, size);
+       cfs_debug("enter cfs_fuse_truncate %s %jd", path, size);
 
        int ret = -EACCES;
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
-       if (plug && plug->ops) { 
+
+       if (plug && plug->ops) {
                if ((subpath || !plug->ops->readdir) && plug->ops->truncate)
                        ret = plug->ops->truncate(plug, subpath ? subpath : "", size);
        }
@@ -371,7 +405,7 @@ static int cfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -395,7 +429,7 @@ static int cfs_fuse_unlink(const char *path)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
@@ -407,7 +441,7 @@ ret:
 
        if (subpath)
                g_free(subpath);
-       
+
        return ret;
 }
 
@@ -419,13 +453,13 @@ static int cfs_fuse_readlink(const char *path, char *buf, size_t max)
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
        if (plug->ops && plug->ops->readlink)
                ret = plug->ops->readlink(plug, subpath ? subpath : "", buf, max);
-       
+
 ret:
        cfs_debug("leave cfs_fuse_readlink %s (%d)", path, ret);
 
@@ -443,13 +477,13 @@ static int cfs_fuse_utimens(const char *path, const struct timespec tv[2])
 
        char *subpath = NULL;
        cfs_plug_t *plug = find_plug(path, &subpath);
-       
+
        if (!plug)
                goto ret;
 
        if (plug->ops && plug->ops->utimens)
                ret = plug->ops->utimens(plug, subpath ? subpath : "", tv);
-       
+
 ret:
        cfs_debug("leave cfs_fuse_utimens %s (%d)", path, ret);
 
@@ -488,7 +522,9 @@ static struct fuse_operations fuse_ops = {
        .readlink = cfs_fuse_readlink,
        .utimens = cfs_fuse_utimens,
        .statfs = cfs_fuse_statfs,
-       .init = cfs_fuse_init
+       .init = cfs_fuse_init,
+       .chown = cfs_fuse_chown,
+       .chmod = cfs_fuse_chmod
 };
 
 static char *
@@ -566,7 +602,7 @@ create_dot_clusterlog_cb(cfs_plug_t *plug)
 static char *
 read_debug_setting_cb(cfs_plug_t *plug)
 {
-       return g_strdup_printf("%d\n", !!cfs.debug); 
+       return g_strdup_printf("%d\n", !!cfs.debug);
 }
 
 static void
@@ -596,8 +632,8 @@ my_qb_log_filter(struct qb_log_callsite *cs)
        }
 }
 
-static void 
-update_qb_log_settings(void) 
+static void
+update_qb_log_settings(void)
 {
        qb_log_filter_fn_set(my_qb_log_filter);
 
@@ -610,9 +646,9 @@ update_qb_log_settings(void)
        }
 }
 
-static int 
+static int
 write_debug_setting_cb(
-       cfs_plug_t *plug, 
+       cfs_plug_t *plug,
        const char *buf,
        size_t size)
 {
@@ -640,7 +676,7 @@ write_debug_setting_cb(
        return res;
 }
 
-static void 
+static void
 create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
 {
        g_return_if_fail(bplug != NULL);
@@ -655,7 +691,7 @@ create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
        lnk = cfs_plug_link_new("qemu-server", lnktarget);
        g_free(lnktarget);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk);
-       
+
        lnktarget = g_strdup_printf("nodes/%s/openvz", nodename);
        lnk = cfs_plug_link_new("openvz", lnktarget);
        g_free(lnktarget);
@@ -668,7 +704,7 @@ create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
 
        cfs_plug_func_t *fplug = cfs_plug_func_new(".version", 0440, create_dot_version_cb, NULL);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug);
-       
+
        fplug = cfs_plug_func_new(".members", 0440, create_dot_members_cb, NULL);
        cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug);
 
@@ -688,11 +724,12 @@ create_symlinks(cfs_plug_base_t *bplug, const char *nodename)
 }
 
 static char *
-lookup_node_ip(const char *nodename) 
+lookup_node_ip(const char *nodename)
 {
        char buf[INET6_ADDRSTRLEN];
        struct addrinfo *ainfo;
        struct addrinfo ahints;
+       char *res = NULL;
        memset(&ahints, 0, sizeof(ahints));
 
        if (getaddrinfo(nodename, NULL, &ahints, &ainfo))
@@ -702,22 +739,22 @@ lookup_node_ip(const char *nodename)
                struct sockaddr_in *sa = (struct sockaddr_in *)ainfo->ai_addr;
                inet_ntop(ainfo->ai_family, &sa->sin_addr, buf, sizeof(buf));
                if (strncmp(buf, "127.", 4) != 0) {
-                       return g_strdup(buf);
+                       res = g_strdup(buf);
                }
-       }
-
-       if (ainfo->ai_family == AF_INET6) {
+       } else if (ainfo->ai_family == AF_INET6) {
                struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ainfo->ai_addr;
                inet_ntop(ainfo->ai_family, &sa->sin6_addr, buf, sizeof(buf));
                if (strcmp(buf, "::1") != 0) {
-                       return g_strdup(buf);
+                       res = g_strdup(buf);
                }
        }
 
-       return NULL;
+       freeaddrinfo(ainfo);
+
+       return res;
 }
 
-static const char* 
+static const char*
 log_tags_stringify(uint32_t tags) {
        if (qb_bit_is_set(tags, QB_LOG_TAG_LIBQB_MSG_BIT)) {
                return "libqb";
@@ -746,7 +783,7 @@ int main(int argc, char *argv[])
 
        qb_log_init("pmxcfs", LOG_DAEMON, LOG_DEBUG);
        /* remove default filter */
-       qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE, 
+       qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE,
                          QB_LOG_FILTER_FILE, "*", LOG_DEBUG);
 
        qb_log_tags_stringify_fn_set(log_tags_stringify);
@@ -764,7 +801,7 @@ int main(int argc, char *argv[])
        GOptionEntry entries[] = {
                { "debug", 'd', 0, G_OPTION_ARG_NONE, &cfs.debug, "Turn on debug messages", NULL },
                { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Do not daemonize server", NULL },
-               { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode, 
+               { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode,
                  "Force local mode (ignore corosync.conf, force quorum)", NULL },
                { NULL },
        };
@@ -798,14 +835,14 @@ int main(int argc, char *argv[])
                qb_log_fini();
                exit (-1);
        }
-       
-       for (int i=0; i < sizeof(utsname.nodename); i++) {
-               if (utsname.nodename[i] =='.') utsname.nodename[i] = 0;
-       }
+
+       char *dot = strchr(utsname.nodename, '.');
+       if (dot)
+               *dot = 0;
 
        cfs.nodename = g_strdup(utsname.nodename);
 
-       if (!(cfs.ip = lookup_node_ip(cfs.nodename))) { 
+       if (!(cfs.ip = lookup_node_ip(cfs.nodename))) {
                cfs_critical("Unable to get local IP address");
                qb_log_fini();
                exit(-1);
@@ -885,7 +922,7 @@ int main(int argc, char *argv[])
        if (conf_data) g_free(conf_data);
 
        cfs_plug_memdb_t *config = cfs_plug_memdb_new("memdb", memdb, dcdb);
-       
+
        cfs_plug_base_t *bplug = cfs_plug_base_new("", (cfs_plug_t *)config);
 
        create_symlinks(bplug, cfs.nodename);
@@ -895,11 +932,11 @@ int main(int argc, char *argv[])
        umount2(CFSDIR, MNT_FORCE);
 
        mkdir(CFSDIR, 0755);
-       
+
        char *fa[] = { "-f", "-odefault_permissions", "-oallow_other", NULL};
 
-       struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa); 
-       
+       struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa);
+
        struct fuse_chan *fuse_chan = fuse_mount(CFSDIR, &fuse_args);
        if (!fuse_chan) {
                cfs_critical("fuse_mount error: %s", strerror(errno));
@@ -1013,13 +1050,13 @@ int main(int argc, char *argv[])
        sleep(1); /* do not restart too fast */
  ret:
 
-       if (status_fsm) 
+       if (status_fsm)
                dfsm_destroy(status_fsm);
 
        if (dcdb)
                dfsm_destroy(dcdb);
 
-       if (memdb) 
+       if (memdb)
                memdb_close(memdb);
 
        if (wrote_pidfile)