]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - git_acl.c
Allow tools to ref repopath.git as repopath
[repo_shell.git] / git_acl.c
index 4bb75ee46fe715788da76b44ae6415db956ffbf7..3c740dd10a4ac368fae242b0acf7ce41575486d3 100644 (file)
--- a/git_acl.c
+++ b/git_acl.c
  */
 
 #include <stdbool.h>
-//#include <stdio.h>
-//#include <errno.h>
-//#include <stdlib.h>
-//#include <sys/types.h>
-//#include <fcntl.h>
-//#include <unistd.h>
-//#include <pwd.h>
+#include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include "ini.h"
 #include "utility.h"
-#include "mystrtok.h"
+#include "stringutils.h"
 #include "stra.h"
-#include "match.h"
 #include "git_acl.h"
 
 enum {
@@ -115,21 +109,24 @@ static acl_clear(acl_t *acl)
   stra_destroy(&acl->userids);
 }
 
-static bool str_has_word(const char* string, const char* word)
+/* git tools match /path/to/repo against /path/to/repo.git when the former
+ * doesn't exist and the latter does.  repo_shell addresses this by stripping
+ * the .git prefix off all repopath's read in from .gitacls and the SSH comand
+ * line.  This mimics the expected git tool behavior except when /path/to/repo
+ * and /path/to/repo.git both exist.  This case shouldn't ever be seen anyway.
+ */
+static char *strip_repo(const char *repo_name)
 {
-  char *_s = xstrdup(string);
-  char *s = _s;
-  char *p = my_strtok(&s, " \t\n");
-
-  while (p) {
-    if (match(p, word)) {
-      free(_s);
-      return true;
-    }
-    p = my_strtok(&s, " \t\n");
+  if (!repo_name)
+    return NULL;
+  else {
+    char *dot = rindex(repo_name, '.');
+
+    if (dot && !strcmp(dot, ".git"))
+      return xstrndup(repo_name, dot - repo_name);
+    else
+      return xstrdup(repo_name);
   }
-  free(_s);
-  return false;
 }
 
 static int acl_handler(void* user, const char* section, const char* name,
@@ -148,15 +145,18 @@ static int acl_handler(void* user, const char* section, const char* name,
       stra_add(&acl->userids, name);
     }
   } else if (!strcmp(section, "repo_groups")) {
-    if (str_has_word(value, acl->repo)) {
+    char *v = strip_repo(value);
+    if (str_has_word(v, acl->repo)) {
       //debug("repoids += '%s'", name);
       stra_add(&acl->repoids, name);
     }
+    free(v);
   } else if (!strncmp(section, "repo", 4)) {
     char *_p = xstrdup(section + 4);
     char *p = _p;
-    char *repo = my_strtok(&p, " \t\n");
+    char *repo = strip_repo(my_strtok(&p, " \t\n"));
 
+    debug("repo '%s'", repo);
     if (!repo || my_strtok(&p, " \t\n"))
       die("acl_handler: badly formatted section '%s'", section);
     /* repo is repo name, name is userid, value is permission */
@@ -169,6 +169,7 @@ static int acl_handler(void* user, const char* section, const char* name,
       //debug("match: repoid='%s', userid='%s', perms='%s'(%u)", repo, name,
       //    value, acl->perms);
     }
+    free(repo);
     free(_p);
   } else
     die("acl_handler: unknown section='%s' name='%s'", section, name);
@@ -178,6 +179,7 @@ static int acl_handler(void* user, const char* section, const char* name,
 int git_acl(const char *user, const char *repo, const char *file)
 {
   acl_t acl;
+  char *r;
 
   if (!file || !*file || !user || !*user || !repo || !*repo) {
     die("git_acl: invalid args user='%s', repo='%s', file='%s'", user, repo,
@@ -191,7 +193,9 @@ int git_acl(const char *user, const char *repo, const char *file)
   acl.user = (char*)user;
   acl.repo = (char*)repo;
   stra_add(&acl.userids, acl.user);
-  stra_add(&acl.repoids, acl.repo);
+  r = strip_repo(acl.repo);
+  stra_add(&acl.repoids, r);
+  free(r);
 
   //debug("Searching for '%s'@'%s'", acl.user, acl.repo);
   if (ini_parse(file, acl_handler, &acl) < 0)