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
|
/*
* blogc: A blog compiler.
* Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file LICENSE.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <squareball.h>
#include "shell-command-parser.h"
#include "shell.h"
int
bgr_shell(int argc, char *argv[])
{
int rv = 0;
char *repo = NULL;
char *quoted_repo = NULL;
// get shell path
char *self = getenv("SHELL");
if (self == NULL) {
fprintf(stderr, "error: failed to find blogc-git-receiver path\n");
rv = 3;
goto cleanup;
}
// get home path
char *home = getenv("HOME");
if (home == NULL) {
fprintf(stderr, "error: failed to find user home path\n");
rv = 3;
goto cleanup;
}
// validate command and extract git repository
char *tmp_repo = bgr_shell_command_parse(argv[2]);
if (tmp_repo == NULL) {
fprintf(stderr, "error: invalid git-shell command: %s\n", argv[2]);
rv = 3;
goto cleanup;
}
repo = sb_strdup_printf("%s/repos/%s", home, tmp_repo);
quoted_repo = sb_shell_quote(repo);
free(tmp_repo);
if (0 == strncmp(argv[2], "git-upload-", 11)) // no need to check len here
goto git_exec;
if (0 != access(repo, F_OK)) {
char *git_init_cmd = sb_strdup_printf(
"git init --bare %s > /dev/null", quoted_repo);
if (0 != system(git_init_cmd)) {
fprintf(stderr, "error: failed to create git repository: %s\n",
repo);
rv = 3;
free(git_init_cmd);
goto cleanup;
}
free(git_init_cmd);
}
if (0 != chdir(repo)) {
fprintf(stderr, "error: failed to chdir (%s): %s\n", repo,
strerror(errno));
rv = 3;
goto cleanup;
}
if (0 != access("hooks", F_OK)) {
// openwrt git package won't install git templates, then the git
// repositories created with it won't have the hooks/ directory.
if (0 != mkdir("hooks", 0777)) { // mkdir honors umask for us.
fprintf(stderr, "error: failed to create directory (%s/hooks): "
"%s\n", repo, strerror(errno));
rv = 3;
goto cleanup;
}
}
if (0 != chdir("hooks")) {
fprintf(stderr, "error: failed to chdir (%s/hooks): %s\n", repo,
strerror(errno));
rv = 3;
goto cleanup;
}
if (0 == access("pre-receive", F_OK)) {
if (0 != unlink("pre-receive")) {
fprintf(stderr, "error: failed to remove old symlink "
"(%s/hooks/pre-receive): %s\n", repo, strerror(errno));
rv = 3;
goto cleanup;
}
}
if (0 != symlink(self, "pre-receive")) {
fprintf(stderr, "error: failed to create symlink "
"(%s/hooks/pre-receive): %s\n", repo, strerror(errno));
rv = 3;
goto cleanup;
}
if (0 == access("post-receive", F_OK)) {
if (0 != unlink("post-receive")) {
fprintf(stderr, "error: failed to remove old symlink "
"(%s/hooks/post-receive): %s\n", repo, strerror(errno));
rv = 3;
goto cleanup;
}
}
if (0 != symlink(self, "post-receive")) {
fprintf(stderr, "error: failed to create symlink "
"(%s/hooks/post-receive): %s\n", repo, strerror(errno));
rv = 3;
goto cleanup;
}
git_exec:
if (0 != chdir(home)) {
fprintf(stderr, "error: failed to chdir (%s): %s\n", home,
strerror(errno));
rv = 3;
goto cleanup;
}
// static allocation instead of sb_strdup_printf to avoid leaks
char buffer[4096];
char *command = sb_strdup(argv[2]);
char *p;
for (p = command; *p != ' ' && *p != '\0'; p++);
if (*p == ' ')
*p = '\0';
if (sizeof(buffer) < (strlen(command) + strlen(quoted_repo) + 2)) {
fprintf(stderr, "error: git-shell command is too big\n");
rv = 3;
goto cleanup;
}
if (0 > snprintf(buffer, sizeof(buffer), "%s %s", command, quoted_repo)) {
fprintf(stderr, "error: failed to generate git-shell command\n");
rv = 3;
goto cleanup;
}
free(command);
free(repo);
free(quoted_repo);
// this is a hack. no memory handling should be done inside this block
if (NULL == getenv("__VALGRIND_ENABLED")) {
execlp("git-shell", "git-shell", "-c", buffer, NULL);
// execlp only returns on error, then something bad happened
fprintf(stderr, "error: failed to execute git-shell\n");
return 3;
}
printf("git-shell -c \"%s\"\n", buffer); // used by tests, ignore
return 0;
cleanup:
free(repo);
free(quoted_repo);
return rv;
}
|