launch: counter effect of set-gid
[src/app-framework-main.git] / src / afm-launch.c
index 3b79fbb..f2a5065 100644 (file)
@@ -54,6 +54,7 @@ extern char **environ;
 
 static const char *args_for_afb_daemon[] = {
        "/usr/bin/afb-daemon",
+       "--daemon",
        "--alias=/icons:%I",
        "--port=%P",
        "--rootdir=%D",
@@ -62,16 +63,18 @@ static const char *args_for_afb_daemon[] = {
 };
 
 static const char *args_for_qmlviewer[] = {
-       "/usr/bin/qt5/qmlviewer",
-       "-frameless",
+       "/usr/bin/qt5/qmlscene",
        "-fullscreen",
-       "-script",
+       "-I",
+       "%r",
+       "-I",
+       "%r/imports",
        "%r/%c",
        NULL
 };
 
 static const char *args_for_web_runtime[] = {
-       "/usr/share/qt5/examples/webkitwidgets/browser/browser",
+       "/usr/bin/web-runtime",
        "http://localhost:%P/%c?token=%S",
        NULL
 };
@@ -101,29 +104,32 @@ struct launchparam {
        const char **slave_args;
 };
 
+static gid_t groupid = 0;
+
 static char **instantiate_arguments(const char **args, struct afm_launch_desc *desc, struct launchparam *params)
 {
        const char **iter, *p, *v;
        char *data, **result, port[20], width[20], height[20], mini[3], c;
-       int n, s, x;
+       int n, s;
 
        /* init */
        mini[0] = '%';
        mini[2] = 0;
 
        /* loop that either compute the size and build the result */
-       n = s = x = 0;
+       data = NULL;
+       n = s = 0;
        for (;;) {
                iter = args;
                n = 0;
                while (*iter) {
                        p = *iter++;
-                       if (x)
+                       if (data)
                                result[n] = data;
                        n++;
                        while((c = *p++) != 0) {
                                if (c != '%') {
-                                       if (x)
+                                       if (data)
                                                *data++ = c;
                                        else
                                                s++;
@@ -131,7 +137,7 @@ static char **instantiate_arguments(const char **args, struct afm_launch_desc *d
                                        c = *p++;
                                        switch (c) {
                                        case 'I': v = FWK_ICON_DIR; break;
-                                       case 'P': if(!x) sprintf(port, "%d", params->port); v = port; break;
+                                       case 'P': if(!data) sprintf(port, "%d", params->port); v = port; break;
                                        case 'S': v = params->secret; break;
                                        case 'D': v = params->datadir; break;
                                        case 'r': v = desc->path; break;
@@ -142,23 +148,23 @@ static char **instantiate_arguments(const char **args, struct afm_launch_desc *d
                                        case 'm': v = desc->type; break;
                                        case 'n': v = desc->name; break;
                                        case 'p': v = "" /*desc->plugins*/; break;
-                                       case 'W': if(!x) sprintf(width, "%d", desc->width); v = width; break;
-                                       case 'H': if(!x) sprintf(height, "%d", desc->height); v = height; break;
+                                       case 'W': if(!data) sprintf(width, "%d", desc->width); v = width; break;
+                                       case 'H': if(!data) sprintf(height, "%d", desc->height); v = height; break;
                                        case '%': c = 0;
                                        default: mini[1] = c; v = mini; break;
                                        }
-                                       if (x)
+                                       if (data)
                                                data = stpcpy(data, v);
                                        else
                                                s += strlen(v);
                                }
                        }
-                       if (x)
+                       if (data)
                                *data++ = 0;
                        else
                                s++;
                }
-               if (x) {
+               if (data) {
                        result[n] = NULL;
                        return result;
                }
@@ -169,7 +175,6 @@ static char **instantiate_arguments(const char **args, struct afm_launch_desc *d
                        return NULL;
                }
                data = (char*)(&result[n + 1]);
-               x = 1;
        }
 }
 
@@ -207,6 +212,10 @@ static int launchexec1(struct afm_launch_desc *desc, pid_t children[2], struct l
        }
 
        /********* in the master child ************/
+
+       /* avoid set-gid effect */
+       setresgid(groupid, groupid, groupid);
+
        /* enter the process group */
        rc = setpgid(0, 0);
        if (rc) {
@@ -282,13 +291,12 @@ static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct l
                close(spipe[0]);
                /* wait the ready signal (that transmit the slave pid) */
                rc = read(mpipe[0], &children[1], sizeof children[1]);
-               if (rc  < 0) {
+               close(mpipe[0]);
+               if (rc  <= 0) {
                        ERROR("reading master pipe failed: %m");
-                       close(mpipe[0]);
                        close(spipe[1]);
                        return -1;
                }
-               close(mpipe[0]);
                assert(rc == sizeof children[1]);
                /* start the child */
                rc = write(spipe[1], "start", 5);
@@ -306,6 +314,9 @@ static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct l
        close(mpipe[0]);
        close(spipe[1]);
 
+       /* avoid set-gid effect */
+       setresgid(groupid, groupid, groupid);
+
        /* enter the process group */
        rc = setpgid(0, 0);
        if (rc) {
@@ -342,7 +353,7 @@ static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct l
                /********* in the slave child ************/
                close(mpipe[0]);
                rc = read(spipe[0], message, sizeof message);
-               if (rc < 0) {
+               if (rc <= 0) {
                        ERROR("reading slave pipe failed: %m");
                        _exit(1);
                }
@@ -366,7 +377,7 @@ static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct l
        }
        else {
                rc = write(mpipe[1], &children[1], sizeof children[1]);
-               if (rc < 0) {
+               if (rc <= 0) {
                        ERROR("can't write master pipe: %m");
                }
                else {
@@ -378,6 +389,18 @@ static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct l
        _exit(1);
 }
 
+static void afm_launch_init_group()
+{
+       if (!groupid) {
+               gid_t r, e, s;
+               getresgid(&r, &e, &s);
+               if (s && s != e)
+                       groupid = s;
+               else
+                       groupid = -1;
+       }
+}
+
 int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
 {
        char datadir[PATH_MAX];
@@ -385,6 +408,9 @@ int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
        char secret[9];
        struct launchparam params;
 
+       /* static init */
+       afm_launch_init_group();
+
        /* what launcher ? */
        ikl = 0;
        if (desc->type != NULL && *desc->type) {