kmx.io/git-auth

Diff from 1ebfdd669f520b95d7b084520c168fd68fb33135 to master


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
diff --git a/.gitignore b/.gitignore
index 9b68d7a..a1ab927 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
-*~
-*\#*
 obj/
+/.depend
+/git-auth
+*.d
diff --git a/Makefile b/Makefile
index 9087d47..936836c 100644
--- a/Makefile
+++ b/Makefile
@@ -23,8 +23,8 @@ GIT_SHELL ?= /usr/local/bin/git-shell
 CPPFLAGS = -DGIT_SHELL=\"${GIT_SHELL}\"
 DEBUG ?= -ggdb -DDEBUG
 WARNINGS ?= yes
-CDIAGFLAGS ?= -W -Wall -Werror
-LDFLAGS += -W -Wall -Werror
+CDIAGFLAGS ?= -W -Wall -Werror -std=c89 -pedantic
+LDFLAGS += -W -Wall -Werror -std=c89 -pedantic
 
 SRCS =	git-auth.c \
 	rule.c rule.h \
diff --git a/README.md b/README.md
index 609588e..47232bf 100644
--- a/README.md
+++ b/README.md
@@ -1,23 +1,26 @@
 # GIT-AUTH
 
+
 ## Synopsis
 
-    GIT_AUTH_ID=ID git-auth COMMAND [ARGS ...]</td>
+    GIT_AUTH_ID=ID git-auth COMMAND [ARGS ...]
+
 
 ## Description
 
-**git-auth** starts by reading rules from /etc/git-auth.conf, one rule
+**git-auth** starts by reading rules from /etc/git/auth.conf, one rule
 per line. Empty lines and lines starting with # are ignored. Each rule
 is made of tokens separated by one or more spaces.
 
 **git-auth** executes COMMAND and ARGS using execvp if the requested ID,
-COMMAND and ARGS match any of the rules defined in /etc/git-auth.conf.
+COMMAND and ARGS match any of the rules defined in /etc/git/auth.conf.
 
 **git-auth** matches a rule by matching all the rule's tokens with ID,
 COMMAND and ARGS in order. Comparison is case sensitive.
 
 A wildcard token "*" matches any string.
 
+
 ## Environment
 
  * **GIT_AUTH_ID** : an arbitrary token to match with the rules.
@@ -25,21 +28,48 @@ This is usually set for each public key in /home/git/.ssh/authorized_keys
 with
     environment="GIT_AUTH_ID=..." ssh-rsa ...
 
+
 ## Configuration
+
 Access control rules for each git repository go into
-`/etc/git-auth.conf`.
+`/etc/git/auth.conf`.
 
 The syntax of this file is one rule per line.
 Each rule is composed of symbols separated by spaces.
 In order :
  - A **GIT_AUTH_ID** value that was set in
    `/home/git/.ssh/authorized_keys`.
- - A `git-shell` command : either `git-upload-pack` for read access,
-   or `git-receive-pack` for write access.
+ - Permissions, either `rw` or `r`.
  - A repository path, relative to `/home/git`,
-   example: `thodg/config.git`
+   example: `cl-adams/adams.git`
+
 
 ## Files
 
- * /etc/git-auth.conf
+ * /etc/git/auth.conf
  * /home/git/.ssh/authorized_keys
+
+
+# See also
+
+[kmxgit](https://git.kmx.io/kmx.io/kmxgit)
+is a web interface on top of git-auth written in Elixir / Phoenix.
+
+
+# Copying
+
+git-auth - restrict git commands
+
+Copyright 2012,2021 Thomas de Grivel <thoxdg@gmail.com>
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/git-auth.1 b/git-auth.1
index c0e0020..1a6747a 100644
--- a/git-auth.1
+++ b/git-auth.1
@@ -8,7 +8,7 @@
 .Nm GIT_AUTH_ID=ID git-auth COMMAND [ARGS ...]
 .Sh DESCRIPTION
 .Nm
-starts by reading rules from /etc/git-auth.conf, one rule per line.
+starts by reading rules from /etc/git/auth.conf, one rule per line.
 Empty lines and lines starting with # are ignored.
 Each rule is made of tokens separated by one or more spaces.
 .Pp
@@ -28,6 +28,6 @@ This is usually set for each public key in /home/git/.ssh/authorized_keys
 with
 .Nm environment="GIT_AUTH_ID=..."
 .Sh FILES
-/etc/git-auth.conf
+/etc/git/auth.conf
 .Pp
 /home/git/.ssh/authorized_keys
diff --git a/git-auth.c b/git-auth.c
index ab1a5d6..6d43175 100644
--- a/git-auth.c
+++ b/git-auth.c
@@ -17,6 +17,7 @@
 
 #include <assert.h>
 #include <err.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,8 +33,33 @@
 #define GIT_SHELL "git-shell"
 #endif
 
-static void stracat (char *buf, size_t bufsz,
-                     int argc, const char **argv)
+#define CMD_BUFSZ 2048
+#define LOG_BUFSZ 2048
+
+static void stracat (char *buf, size_t bufsz, int argc, const char **argv)
+{
+        size_t b = 0;
+        int a;
+        assert(buf);
+        assert(bufsz);
+        assert(argc);
+        assert(argv);
+        for (a = 0; a < argc; a++) {
+                const char *arg;
+                if (b >= bufsz - strlen(argv[a]) - 2) {
+                        errno = ENOMEM;
+                        err(1, "stracat");
+                }
+                if (a)
+                        buf[b++] = ' ';
+                arg = argv[a];
+                while (*arg)
+                        buf[b++] = *(arg++);
+        }
+        buf[b] = 0;
+}
+
+static void stracat_quoted (char *buf, size_t bufsz, int argc, const char **argv)
 {
         size_t b = 0;
         int a;
@@ -41,11 +67,25 @@ static void stracat (char *buf, size_t bufsz,
         assert(bufsz);
         assert(argc);
         assert(argv);
-        for (a = 0; a < argc && b < bufsz; a++) {
+        for (a = 0; a < argc; a++) {
+                const char *arg;
+                if (b >= bufsz - strlen(argv[a]) * 2 - 2) {
+                        errno = ENOMEM;
+                        err(1, "stracat");
+                }
                 if (a)
                         buf[b++] = ' ';
-                b += strlcpy(buf + b, argv[a], bufsz - b);
+                buf[b++] = '"';
+                arg = argv[a];
+                while (*arg) {
+                        char c = *(arg++);
+                        if (c == '"')
+                                buf[b++] = '\\';
+                        buf[b++] = c;
+                }
+                buf[b++] = '"';
         }
+        buf[b] = 0;
 }
 
 static void log_args (const char *op, int argc, const char **argv)
@@ -57,59 +97,109 @@ static void log_args (const char *op, int argc, const char **argv)
 
 static void log_rule (const char *op, s_rule *rule)
 {
-        char msg[2048];
+
+        char msg[LOG_BUFSZ];
         size_t m = 0;
-        const char *mode;
+        const char *mode = "?";
         m += strlcpy(msg + m, GIT_AUTH_ID_ENV, sizeof(msg) - m);
+        if (m >= LOG_BUFSZ - 2)
+                goto overflow;
         msg[m++] = '=';
         m += strlcpy(msg + m, rule->user, sizeof(msg) - m);
+        if (m >= LOG_BUFSZ - 2)
+                goto overflow;
         msg[m++] = ' ';
         assert(1 <= rule->mode && rule->mode <= 3);
         switch (rule->mode) {
         case 1: mode = "r"; break;
         case 2: mode = "w"; break;
         case 3: mode = "rw"; break;
+        default: fprintf(stderr, "log_rule: invalid mode: %d\n", rule->mode);
         }
         m += strlcpy(msg + m, mode, sizeof(msg) - m);
+        if (m >= LOG_BUFSZ - 2)
+                goto overflow;
         msg[m++] = ' ';
         m += strlcpy(msg + m, rule->path, sizeof(msg) - m);
         syslog(LOG_INFO, "%s %s", op, msg);
+        return;
+ overflow:
+        fprintf(stderr, "git-auth: log_rule: buffer overflow !\n");
 }
 
+/*
+static void log_rules (s_rule rules[RULES_MAX])
+{
+        int r = 0;
+        while (rules[r].user) {
+                char buf[16];
+                snprintf(buf, 15, "%d", r);
+                log_rule(buf, &rules[r]);
+                r++;
+        }
+}
+*/
+
 static void log_cmd (const char *op, int argc, const char **argv)
 {
-        char msg[2048];
-        stracat(msg, sizeof(msg), argc, argv);
+        char msg[LOG_BUFSZ];
+        stracat(msg, LOG_BUFSZ, argc, argv);
         syslog(LOG_INFO, "%s %s", op, msg);
 }
 
+/*
+int unquote (char *buf, size_t bufsz, const char *str)
+{
+        size_t len = strlen(str);
+        assert(len < bufsz);
+        if (str[0] == '\'' && len > 1 && str[len - 1] == '\'') {
+                strlcpy(buf, str + 1, bufsz);
+                buf[len - 2] = 0;
+                return 1;
+        }
+        strlcpy(buf, str, bufsz);
+        return 0;
+}
+
+static int rule_match_path (const char *rule_path, const char *arg_path)
+{
+        char rp[1024];
+        char ap[1024];
+        unquote(rp, sizeof(rp), rule_path);
+        unquote(ap, sizeof(ap), arg_path);
+        return strcmp(rp, ap) == 0;
+}
+*/
+
 static int rule_match (s_rule *rule, int argc, const char **argv)
 {
-  assert(rule);
-  assert(argc);
-  assert(argv);
-  if (argc != 3)
-          return 0;
-  log_rule("MATCH", rule);
-  if (strcmp(rule->user, argv[0]))
-          return 0;
-  if (!strcmp(argv[1], "git-upload-pack") && !(rule->mode & 1))
-          return 0;
-  else if (!strcmp(argv[1], "git-receive-pack") && !(rule->mode & 2))
-          return 0;
-  if (strcmp(rule->path, argv[2]))
-          return 0;
-  return 1;
+        assert(rule);
+        assert(argc);
+        assert(argv);
+        if (argc != 3)
+                return 0;
+        if (strcmp(rule->user, argv[0]))
+                return 0;
+        if (!strcmp(argv[1], "git-upload-pack") && !(rule->mode & 1))
+                return 0;
+        else if (!strcmp(argv[1], "git-receive-pack") && !(rule->mode & 2))
+                return 0;
+        if (strcmp(rule->path, argv[2]))
+                return 0;
+        return 1;
 }
 
 static int auth (s_rule rules[RULES_MAX], int argc, const char **argv)
 {
-        s_rule *r = &rules[0];
-        while (r->user) {
-                if (rule_match(r, argc, argv)) {
-                        log_rule("ALLOW", r);
+        int r = 0;
+        while (rules[r].user) {
+                /* log_rule("MATCH", &rules[r]); */
+                if (rule_match(&rules[r], argc, argv)) {
+                        log_rule("ALLOW", &rules[r]);
                         return 1;
                 }
+                /* else
+                   log_rule("DENY", &rules[r]); */
                 r++;
         }
         return 0;
@@ -117,43 +207,46 @@ static int auth (s_rule rules[RULES_MAX], int argc, const char **argv)
 
 static void cleanup (void)
 {
-  closelog();
+        closelog();
 }
 
 static void exec_cmd (int argc, const char **argv)
 {
-  char buf[2048];
-  int cmd_argc;
-  const char *cmd_argv[4];
-  assert(argc);
-  assert(argv);
-  cmd_argc = 3;
-  cmd_argv[0] = GIT_SHELL;
-  cmd_argv[1] = "-c";
-  stracat(buf, sizeof(buf), argc - 1, argv + 1);
-  cmd_argv[2] = buf;
-  cmd_argv[3] = NULL;
-  log_cmd("EXEC", cmd_argc, cmd_argv);
-  cleanup();
-  execvp(cmd_argv[0], (char *const *) (cmd_argv + 1));
-  err(1, "%s", cmd_argv[0]);
+        char buf[CMD_BUFSZ];
+        const char *cmd_argv[4];
+        assert(argc);
+        assert(argv);
+        cmd_argv[0] = GIT_SHELL;
+        cmd_argv[1] = "-c";
+        stracat(buf, CMD_BUFSZ, argc - 1, argv + 1);
+        cmd_argv[2] = buf;
+        cmd_argv[3] = NULL;
+        log_cmd("EXEC", 3, cmd_argv);
+        cleanup();
+        execvp(cmd_argv[0], (char *const *) cmd_argv);
+        err(1, "%s", cmd_argv[0]);
 }
 
 static void usage (const char *argv0)
 {
-        fprintf(stderr, "Usage: %s=ID %s -c COMMAND\n",
+        fprintf(stderr, "Usage: %s=ID %s -c GIT_COMMAND PATH\n",
                 GIT_AUTH_ID_ENV, argv0);
         exit(1);
 }
 
 int main (int argc, char **argv)
 {
+        char buf[LOG_BUFSZ];
+        char *bs;
         s_rule rules[RULES_MAX];
         const char *git_auth_id;
         int auth_ok;
         const char *cmd_argv[3];
         if (argc != 3) {
-                fprintf(stderr, "git-auth: wrong number of arguments.\n");
+                char buf[LOG_BUFSZ];
+                fprintf(stderr, "git-auth: wrong number of arguments: %d.\n", argc);
+                stracat_quoted(buf, sizeof(buf), argc, (const char **) argv);
+                fprintf(stderr, "%s\n", buf);
                 usage(argv[0]);
         }
         if (strcmp(argv[1], "-c")) {
@@ -165,17 +258,21 @@ int main (int argc, char **argv)
                 fprintf(stderr, "missing %s.\n", GIT_AUTH_ID_ENV);
                 usage(argv[0]);
         }
+        cmd_argv[0] = git_auth_id;
+        strlcpy(buf, argv[2], LOG_BUFSZ);
+        cmd_argv[1] = buf;
+        bs = strchr(buf, ' ');
+        *bs++ = 0;
+        cmd_argv[2] = bs;
         openlog(argv[0], LOG_PID, LOG_AUTH);
         log_args("NEW", argc, (const char **) argv);
-        cmd_argv[0] = git_auth_id;
-        cmd_argv[1] = argv[2];
-        cmd_argv[2] = argv[3];
-        read_rules(rules, "/etc/git-auth.conf");
+        read_rules(rules, "/etc/git/auth.conf");
+        /* log_rules(rules); */
         auth_ok = auth(rules, 3, cmd_argv);
         log_cmd(auth_ok ? "ALLOW" : "DENY", 3, cmd_argv);
         if (auth_ok) {
                 exec_cmd(3, cmd_argv);
-                // never reached
+                /* never reached */
         }
         cleanup();
         return 1;
diff --git a/rule.c b/rule.c
index c956e95..16afde0 100644
--- a/rule.c
+++ b/rule.c
@@ -154,7 +154,9 @@ int read_rules (s_rule rules[RULES_MAX], const char *path)
                         if (ferror(fp))
                                 err(1, "%s", path);
                         bzero(&rules[r], sizeof(s_rule));
-                        fprintf(stderr, "fgets NULL. r: %d\n", r);
+                        if (error)
+                                exit(1);
+                        /* fprintf(stderr, "fgets NULL. r: %d\n", r); */
                         return r;
                 }
                 if (!(nl = strchr(buf, '\n'))) {
@@ -176,6 +178,6 @@ int read_rules (s_rule rules[RULES_MAX], const char *path)
         bzero(&rules[r], sizeof(s_rule));
         if (error)
                 exit(1);
-        fprintf(stderr, "EOF. r: %d\n", r);
+        /* fprintf(stderr, "EOF. r: %d\n", r); */
         return r;
 }