blob: b0e9effb6d2d351f6097d134ff7e2c1e296c5d9d (
plain)
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
|
echo "[ 1/9] Setting up Git"
if ! id git >/dev/null 2>&1; then
useradd -m -b /srv git
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] Git user already created."
fi
GIT_HOME=$( grep git /etc/passwd | cut -d: -f6 )
if [ "$GIT_HOME" = "" ]; then
echo "[ERROR] No git home!"
exit 1
fi
echo "[INFO] Git home: $GIT_HOME"
if ! command -v git >/dev/null 2>&1; then
apt-get install -y git
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] Git already installed."
fi
if ls /usr/lib/git-core/git-http-backend >/dev/null 2>&1; then
GIT_HTTP_BACKEND=/usr/lib/git-core/git-http-backend
elif ls /usr/libexec/git-core/git-http-backend >/dev/null 2>&1; then
GIT_HTTP_BACKEND=/usr/libexec/git-core/git-http-backend
else
echo "[ERROR] git-http-backend not found!"
exit 1
fi
echo "[INFO] git-http-backend: $GIT_HTTP_BACKEND"
echo "[ 2/9] Setting up cgit"
if [ ! command -v /usr/share/webapps/cgit/cgit.cgi >/dev/null 2>&1 ] &&
[ ! command -v /usr/lib/cgit/cgit.cgi >/dev/null 2>&1 ]; then
apt-get install -y cgit
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] cgit already installed."
fi
if command -v /usr/share/webapps/cgit/cgit.cgi >/dev/null 2>&1; then
CGIT_CGI=/usr/share/webapps/cgit/cgit.cgi
elif command -v /usr/lib/cgit/cgit.cgi >/dev/null 2>&1; then
CGIT_CGI=/usr/lib/cgit/cgit.cgi
else
echo "[ERROR] cgit not found!"
exit 1
fi
echo "[INFO] Found cgit: $CGIT_CGI"
echo "[ 3/9] Setting up nginx"
if ! command -v nginx >/dev/null 2>&1; then
apt-get install -y nginx
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] nginx already installed."
fi
echo "[ 4/9] Setting up certbot"
if ! command -v certbot >/dev/null 2>&1; then
apt-get install -y python3-certbot-nginx
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] certbot already installed."
fi
echo "[ 5/9] Setting up fcgiwrap"
if ! command -v fcgiwrap >/dev/null 2>&1; then
apt-get install -y fcgiwrap
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] fcgiwrap already installed."
fi
echo "[ 6/9] Setting up htpasswd"
if ! command -v htpasswd >/dev/null 2>&1; then
apt-get install -y apache2-utils
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] htpasswd already installed."
fi
echo "[ 7/9] Setting up GCC"
if ! command -v gcc >/dev/null 2>&1; then
apt-get install -y gcc
[ $? -eq 0 ] || exit $?
else
echo "[SKIP] GCC already installed."
fi
echo "[ 8/9] Compiling and setting up CGI program"
COMPILE="\
-Wno-old-style-declaration \
-Wno-missing-field-initializers -Wno-missing-braces \
-Wall -Wextra -Werror -pedantic -mshstk \
-O3 -o main main.c"
SAN=-fsanitize=address,undefined,leak
if gcc $SAN $COMPILE >/dev/null 2>&1; then
gcc $SAN $COMPILE
[ $? -eq 0 ] || exit $?
else
echo "[INFO] Sanitizers are disabled."
gcc $COMPILE
[ $? -eq 0 ] || exit $?
fi
chown root:root main
[ $? -eq 0 ] || exit $?
mv -f main /srv/
[ $? -eq 0 ] || exit $?
mv -f static /srv/static
[ $? -eq 0 ] || exit $?
echo "[ 9/9] Configuring"
if [ ! -d /etc/nginx/sites-available ]; then
mkdir /etc/nginx/sites-available
[ $? -eq 0 ] || exit $?
fi
if [ ! -d /etc/nginx/sites-enabled ]; then
mkdir /etc/nginx/sites-enabled
[ $? -eq 0 ] || exit $?
fi
if [ ! -f /etc/nginx/sites-enabled/default ]; then
ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
[ $? -eq 0 ] || exit $?
fi
cat <<EOF >/etc/nginx/sites-available/default
server {
listen 80;
listen [::]:80;
server_name _;
location ~ ^/git_write/ {
rewrite ^/git_write/(.*) /\$1 break;
auth_basic "Git";
auth_basic_user_file $GIT_HOME.htpasswd;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $GIT_HTTP_BACKEND;
fastcgi_param GIT_PROJECT_ROOT $GIT_HOME;
fastcgi_param PATH_INFO \$uri;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location ~ ^/git_read/ {
rewrite ^/git_read/(.*) /\$1 break;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $GIT_HTTP_BACKEND;
fastcgi_param GIT_PROJECT_ROOT $GIT_HOME;
fastcgi_param PATH_INFO \$uri;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location ~ \\.git {
if (\$arg_service = git-receive-pack) {
rewrite /(.*) /git_write/\$1 last;
}
if (\$uri ~ ^/.*/git-receive-pack\$) {
rewrite /(.*) /git_write/\$1 last;
}
if (\$arg_service = git-upload-pack) {
rewrite /(.*) /git_read/\$1 last;
}
if (\$uri ~ ^/.*/git-upload-pack\$) {
rewrite /(.*) /git_read/\$1 last;
}
}
location ^~ /git/ {
rewrite ^/git/(.*) /\$1 break;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $CGIT_CGI;
fastcgi_param PATH_INFO \$uri;
fastcgi_param QUERY_STRING \$args;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location ~* \\.(txt|asc|htm|css|svg|jpg|png|gif|ico|woff|woff2|js|wasm|mp3)\$ {
rewrite ^/(.*) /static/plain/\$1 break;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $CGIT_CGI;
fastcgi_param PATH_INFO \$uri;
fastcgi_param QUERY_STRING \$args;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
location / {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /srv/main.cgi;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
}
EOF
[ $? -eq 0 ] || exit $?
echo "[INFO] Written /etc/nginx/sites-available/default:"
cat /etc/nginx/sites-available/default
cat <<EOF >/etc/nginx/mime.types
types {
text/plain txt;
text/plain asc;
text/html htm;
text/css css;
image/svg+xml svg;
image/jpeg jpg;
image/png png;
image/gif gif;
image/x-icon ico;
application/font-woff woff;
application/font-woff2 woff2;
application/javascript js;
application/wasm wasm;
audio/mpeg mp3;
}
EOF
[ $? -eq 0 ] || exit $?
echo "[INFO] Written /etc/nginx/mime.types:"
cat /etc/nginx/mime.types
cat <<EOF >/etc/nginx/nginx.conf
user git;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
server_names_hash_bucket_size 256;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
EOF
[ $? -eq 0 ] || exit $?
echo "[INFO] Written /etc/nginx/nginx.conf:"
cat /etc/nginx/nginx.conf
echo "[INFO] All done!"
|