I tried to use slowcgi to execute a binary in cgi-bin/ when accessing a
resource.

So the config looked a bit like this:

        location "/foo/bar" {
                fastcgi param SCRIPT_FILENAME "/cgi-bin/foobar"
                root ""
        }

With this slowcgi fails with:
slowcgi[98607]: execve cgi-bin/foobar: No such file or directory

Now looking at the code I'm wondering how this is supposed to work.
So because of the present '/' there will be a chdir to the directory where
the binary is but then slowcgi uses the full path to exec that binary
which is not going to work.

I attached my fix below which for me at least seems to be the intention of
the code. I bet I missed a lot of nitty gritty details though.
-- 
:wq Claudio

Index: slowcgi.c
===================================================================
RCS file: /cvs/src/usr.sbin/slowcgi/slowcgi.c,v
retrieving revision 1.58
diff -u -p -r1.58 slowcgi.c
--- slowcgi.c   8 Jan 2021 22:05:34 -0000       1.58
+++ slowcgi.c   14 Apr 2021 17:38:10 -0000
@@ -888,6 +888,7 @@ exec_cgi(struct request *c)
        char            *argv[2];
        char            **env;
        char            *path;
+       char            *script_name;
 
        i = 0;
 
@@ -940,6 +941,7 @@ exec_cgi(struct request *c)
                close(s_out[1]);
                close(s_err[1]);
 
+               script_name = c->script_name;
                path = strrchr(c->script_name, '/');
                if (path != NULL) {
                        if (path != c->script_name) {
@@ -948,6 +950,7 @@ exec_cgi(struct request *c)
                                        lwarn("cannot chdir to %s",
                                            c->script_name);
                                *path = '/';
+                               script_name = path + 1;
                        } else
                                if (chdir("/") == -1)
                                        lwarn("cannot chdir to /");
@@ -960,7 +963,7 @@ exec_cgi(struct request *c)
                SLIST_FOREACH(env_entry, &c->env, entry)
                        env[i++] = env_entry->val;
                env[i++] = NULL;
-               execve(c->script_name, argv, env);
+               execve(script_name, argv, env);
                lwarn("execve %s", c->script_name);
                _exit(1);
 

Reply via email to