]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - repo_shell.c
repo_shell uses utility.c
[repo_shell.git] / repo_shell.c
index 7fb31d4c9f6340efff9bfde16c2979deb0a25b49..df342c87399e0f18f200d7dd8c2c86135cb4a2f4 100644 (file)
@@ -8,6 +8,7 @@
 #include <pwd.h>
 #include <string.h>
 #include "ini.h"
+#include "utility.h"
 #include "version.h"
 
 #define CFG_FILE "/etc/repo_shell.cfg"
@@ -20,48 +21,7 @@ typedef struct {
        char *owner;
 } cfg_t;
 
-#undef USE_DEFAULTS
-#ifdef USE_DEFAULTS /* perhaps we want defaults?  Not sure */
-static cfg_t cfg {
-       svn_root: "/var/lib/svn/repositories",
-       git_root: "/var/lib/git",
-       owner: "repo"
-};
-#else
 static cfg_t cfg;
-#endif
-
-static void die(const char *fmt, ...)
-{
-       va_list ap;
-
-       va_start(ap, fmt);
-       fprintf(stderr, "error: ");
-       vfprintf(stderr, fmt, ap);
-       fprintf(stderr, "\n" );
-       va_end(ap);
-       exit(1);
-}
-
-char *xstrdup(const char *str)
-{
-       char *ret = strdup(str);
-       if (!ret)
-               die("out of memory");
-       return ret;
-}
-
-void *xmalloc(size_t size)
-{
-       void *ret;
-
-       ret = malloc(size);
-       if (!ret && !size)
-               ret = malloc(1);
-       if (!ret)
-               die("out of memory");
-       return ret;
-}
 
 static uid_t user_uid(char *user)
 {
@@ -95,17 +55,16 @@ static char *dequote(char *arg)
 
 static char *add_prefix(char *prefix, char* arg)
 {
-       int size;
-
-       if (arg && prefix && strlen(prefix)) {
-               char *n = xmalloc(sizeof(char *) *
-                       (strlen(prefix) + strlen(arg) + 2));
-               strcpy(n, prefix);
-               strcat(n, "/");
-               strcat(n, arg);
-               arg = n;
+       char *narg = arg;
+        int i;
+
+       if (arg && prefix && (i = strlen(prefix))) {
+               narg = xmalloc(sizeof(char *) * (i + strlen(arg) + 2));
+               strcpy(narg, prefix);
+               strcpy(narg + i++, "/");
+               strcpy(narg + i, arg);
        }
-       return arg;
+       return narg;
 }
 
 static int check_ssh_interactive(uid_t uid)
@@ -143,12 +102,11 @@ static int git_acl(const char *user, const char *repo)
 
 static int git_check_access(const char *cmd, const char *repo, const char *user)
 {
-       int rw = 1; /* 0=no access, 1=read only, 2=read/write */
-
-       /* What access is required per the incoming command? */
-       if (!strcmp(cmd, "git-upload-pack") ||
-                       !strcmp(cmd, "git-upload-archive"))
-               rw = 2;
+       /* What access is required per the incoming command?
+        * 0=none, 1=read-only, 2=read-write
+        */
+       int rw = (!strcmp(cmd, "git-upload-pack") ||
+                       !strcmp(cmd, "git-upload-archive")) ? 2 : 1;
 
        /* Return true (1) if the user permissions >= those required */
        return (git_acl(user, repo) >= rw) ? 1 : 0;
@@ -194,7 +152,7 @@ static void cd_to_homedir(void)
 {
        const char *home = getenv("HOME");
        if (!home)
-               die("could not determine user's home directory; HOME is unset");
+               die("user variable HOME is unset");
        if (chdir(home) == -1)
                die("could not chdir to user's home directory");
 }
@@ -210,7 +168,7 @@ static struct commands {
        { NULL },
 };
 
-static int handler(void* user, const char* section, const char* name,
+static int ini_handler(void* user, const char* section, const char* name,
                const char* value)
 {
        cfg_t* pconfig = (cfg_t*)user;
@@ -259,12 +217,8 @@ int main(int argc, char **argv)
                execvp(argv[0], (char *const *) argv);
        }
 
-#ifdef USE_DEFAULTS
-       ini_parse("repo_shell.cfg", handler, &cfg);
-#else
-       if (ini_parse(CFG_FILE, handler, &cfg) < 0)
+       if (ini_parse(CFG_FILE, ini_handler, &cfg) < 0)
                die("cannot read config file %s", CFG_FILE);
-#endif
 
        prog = xstrdup(argv[2]);
        if (!strncmp(prog, "git", 3) && isspace(prog[3]))