]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - repo_shell.c
Add make install, uninstall targets
[repo_shell.git] / repo_shell.c
index 9d775593d7b3f039a0a62ddad71be41e0bd2f939..7ef610d247bf4c22cfd8591de37c93b97f418f99 100644 (file)
@@ -12,6 +12,7 @@
 #include "utility.h"
 #include "version.h"
 #include "git_acl.h"
+#include "stringutils.h"
 
 #define CFG_FILE "/etc/repo_shell.cfg"
 #define SHELL "/bin/bash"
@@ -58,7 +59,7 @@ static char *dequote(char *arg)
 static char *add_prefix(char *prefix, char* arg)
 {
   char *narg = arg;
-        int i;
+  int i;
 
   if (arg && prefix && (i = strlen(prefix))) {
     narg = xmalloc(sizeof(char *) * (i + strlen(arg) + 2));
@@ -113,6 +114,7 @@ static int do_git_cmd(const char *cmd, char *arg, char *user)
 
   ret = execvp(nargv[0], (char *const *) nargv);
   /* Code unreached if execv successful */
+  free((char*)nargv[1]);
   free(narg);
   return ret;
 }
@@ -186,24 +188,29 @@ int main(int argc, char **argv)
     die("opening /dev/null failed");
   close (devnull_fd);
 
-  if (argc == 2 && (!strcmp(argv[1], "-v") ||
-      !strcmp(argv[1], "--version"))) {
+  if (argc == 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
     fprintf(stderr, "%s\n", version);
     return 0;
   }
 
-  if (argc == 1 && check_ssh_interactive(getuid())) {
+  if (argc == 1) {
+    if (!check_ssh_interactive(getuid()))
+      die("only repository access is allowed");
     setuid(getuid());
     argv[0] = SHELL;
     execvp(argv[0], (char *const *) argv);
+    return 1;
   }
 
   if (ini_parse(CFG_FILE, ini_handler, &cfg) < 0)
     die("cannot read config file %s", CFG_FILE);
 
-  if (argc == 4 && (!strcmp(argv[1], "-t") ||
-      !strcmp(argv[1], "--test"))) {
-    perms_t p = git_acl(argv[2], argv[3], cfg.git_acl_file);
+  if ((!strcmp(argv[1], "-t") || !strcmp(argv[1], "--test"))) {
+    perms_t p;
+
+    if (argc !=4)
+      die("usage: %s -t <user> <repo>", argv[0]);
+    p = git_acl(argv[2], argv[3], cfg.git_acl_file);
     fprintf(stderr,
         "user '%s' repo '%s' perms '%s'\n  via userid '%s' repoid '%s'\n",
         argv[2], argv[3], git_acl_perms_as_str(p), git_acl_last_userid(),
@@ -211,31 +218,36 @@ int main(int argc, char **argv)
     return 0;
   }
 
-  prog = xstrdup(argv[2]);
-  if (!strncmp(prog, "git", 3) && isspace(prog[3]))
-    /* Accept "git foo" as if the caller said "git-foo". */
-    prog[3] = '-';
-
-  for (cmd = cmd_list ; cmd->name ; cmd++) {
-    int len = strlen(cmd->name);
-    char *arg;
-    struct passwd *pw;
-    if (strncmp(cmd->name, prog, len))
-      continue;
-    arg = NULL;
-    switch (prog[len]) {
-    case '\0':
+  if (argc == 3) {
+    /* argv[0] = repo_shell, argv[1] = -c, argv[2] = cmd
+     * cmd = "svnserve -t" or "git-xxx '/path/to/repo.git'"
+     */
+    prog = xstrdup(argv[2]);
+    if (!strncmp(prog, "git", 3) && isspace(prog[3]))
+      /* Accept "git foo" as if the caller said "git-foo". */
+      prog[3] = '-';
+
+    for (cmd = cmd_list ; cmd->name ; cmd++) {
+      int len = strlen(cmd->name);
+      char *arg;
+      struct passwd *pw;
+      if (strncmp(cmd->name, prog, len))
+        continue;
       arg = NULL;
-      break;
-    case ' ':
-      arg = prog + len + 1;
-      break;
-    default:
-      continue;
+      switch (prog[len]) {
+        case '\0':
+          arg = NULL;
+          break;
+        case ' ':
+          arg = prog + len + 1;
+          break;
+        default:
+          continue;
+      }
+
+      pw = getpwuid(getuid());
+      exit(cmd->exec(cmd->name, arg, pw->pw_name));
     }
-
-    pw = getpwuid(getuid());
-    exit(cmd->exec(cmd->name, arg, pw->pw_name));
   }
 
   if (!check_ssh_interactive(getuid()))