X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=git_acl.c;h=9c39ec2c08f62742e1cd8edace873b6591159235;hb=f8294bed64126df0043e157d693267b5ce31ee4e;hp=6af2c2c03bd0ec896004f1e5d366d2d562f88cb2;hpb=324d66c100a844c9554e676bb4ca18109f609b04;p=repo_shell.git diff --git a/git_acl.c b/git_acl.c index 6af2c2c..9c39ec2 100644 --- a/git_acl.c +++ b/git_acl.c @@ -16,17 +16,12 @@ */ #include -//#include -//#include -//#include -//#include -//#include -//#include -//#include +#include #include +#include #include "ini.h" #include "utility.h" -#include "mystrtok.h" +#include "stringutils.h" #include "stra.h" #include "git_acl.h" @@ -42,9 +37,7 @@ typedef struct { perms_t perms; } acl_t; -const char* perm_str[PERMS_COUNT] = { - "NOTFOUND", "NONE", "READ", "READ_WRITE" -}; +const char* perms_str[PERMS_COUNT] = { "", "", "r", "rw" }; const char* lm_none = ""; static char *lm_repoid = NULL; @@ -81,7 +74,7 @@ static const char *perms_as_str(perms_t p) { if (p < PERMS_NOTFOUND || p >= PERMS_COUNT) die("perms_as_str: invalid perm %u", p); - return perm_str[p]; + return perms_str[p]; } static perms_t perms_from_str(const char *str) @@ -90,11 +83,11 @@ static perms_t perms_from_str(const char *str) if (!str) return PERMS_NOTFOUND; - else if (!*str) + else if (!strcmp(str, perms_str[PERMS_NONE])) return PERMS_NONE; - else if (!strcmp(str, "r")) + else if (!strcmp(str, perms_str[PERMS_READ])) return PERMS_READ; - else if (!strcmp(str, "rw")) + else if (!strcmp(str, perms_str[PERMS_READ_WRITE])) return PERMS_READ_WRITE; else die("Invalid perms value '%s'", str); @@ -114,21 +107,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 (!strcmp(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, @@ -147,20 +143,22 @@ 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")); 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 */ - if (stra_find(&acl->repoids, repo) >= 0 && - stra_find(&acl->userids, name) >= 0) { + if (stra_match(&acl->repoids, repo) >= 0 && + stra_match(&acl->userids, name) >= 0) { acl->perms = perms_from_str(value); set_lm_repoid(repo); set_lm_userid(name); @@ -168,6 +166,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); @@ -177,6 +176,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, @@ -190,7 +190,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)