blob: 68cff3edb5ce52d7629035fe55b9ab1c2e28fc45 (
about) (
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
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
|
(use-modules ((srfi srfi-26) #:select (cut))
((ice-9 match) #:select (match))
((ice-9 popen) #:select (open-pipe* close-pipe))
((ice-9 ports) #:select (OPEN_READ))
((ice-9 regex) #:select (match:substring string-match))
((ice-9 textual-ports) #:select (get-string-all))
((gnu bootloader) #:select (bootloader-configuration))
((gnu bootloader grub) #:select (grub-bootloader))
((gnu packages) #:select (specifications->packages))
((gnu packages admin) #:select (shadow))
((gnu packages autotools) #:select
(autoconf automake lawrence-boilerplate))
((gnu packages base) #:select (coreutils))
((gnu packages guile) #:select (guile-3.0))
((gnu packages guile-xyz) #:select (guile-cantius guile-lib))
((gnu packages pkg-config) #:select (pkg-config))
((gnu packages tls) #:select (openssl))
((gnu packages web) #:select (httpd))
((gnu services) #:select
(activation-service-type modify-services service service-extension
service-type simple-service))
((gnu services base) #:select
(guix-service-type guix-extension %base-services))
((gnu services desktop) #:select (elogind-service-type))
((gnu services mcron) #:select (cron-daemon-service-type))
((gnu services networking) #:select (dhcp-client-service-type))
((gnu services shepherd) #:select
(shepherd-root-service-type shepherd-service))
((gnu services ssh) #:select
(openssh-service-type openssh-configuration))
((gnu services web) #:prefix web:)
((gnu system) #:select
(operating-system %base-packages))
((gnu system accounts) #:select
(user-account user-extra-groups user-group))
((gnu system file-systems) #:prefix fs:)
((gnu system keyboard) #:select (keyboard-layout))
((gnu system shadow) #:select (%base-groups %base-user-accounts))
((guix build-system gnu)
#:select (%gnu-build-system-modules gnu-build-system))
((guix gexp) #:select
(file-append gexp local-file with-imported-modules))
((guix licenses) #:prefix license:)
((guix packages) #:select (package)))
(define %here (dirname (current-filename)))
(define %ctftilde-phases
(with-imported-modules '((guix build guile-build-system))
#~(modify-phases %standard-phases
(add-after 'install 'patch-executable
(lambda* (#:key inputs #:allow-other-keys)
(use-modules ((guix build guile-build-system) #:select
(target-guile-effective-version)))
(define* (find-subdirs subdir #:optional
(dirs (cons #$output (map cdr inputs))))
(filter-map (lambda (pkg)
(let ((path (string-append pkg subdir)))
(and (file-exists? path) path)))
dirs))
(let* ((gver (target-guile-effective-version))
(scmdir (format #f "/share/guile/site/~a" gver))
(gobjdir (format #f "/lib/guile/~a/site-ccache" gver))
(executable (format #f "~a/bin/ctftilde" #$output))
(bin-pkgs (map (cut assoc-ref inputs <>)
'("coreutils" "guile" "openssl")))
(sbin-pkgs (list (assoc-ref inputs "shadow")))
(path (append (find-subdirs "/bin" bin-pkgs)
(find-subdirs "/sbin" sbin-pkgs)))
(load-path (find-subdirs scmdir))
(compiled-path (find-subdirs gobjdir)))
(patch-shebang executable)
(wrap-program executable
(list "PATH" 'prefix path)
(list "GUILE_LOAD_PATH" 'prefix load-path)
(list "GUILE_LOAD_COMPILED_PATH" 'prefix compiled-path)))))
(delete 'strip))))
(define ctftilde
(package
(name "ctftilde")
(version "current")
(source (local-file (string-append %here "/ctftilde")
#:recursive? #t))
(build-system gnu-build-system)
(arguments
(list #:modules (cons* '(srfi srfi-1)
'(srfi srfi-26)
%gnu-build-system-modules)
#:phases %ctftilde-phases))
(native-inputs
(list autoconf
automake
guile-3.0
pkg-config
lawrence-boilerplate))
(inputs
(list coreutils guile-3.0 openssl shadow))
(propagated-inputs
(list guile-cantius guile-lib))
(home-page "https://ctftilde.koszko.org/")
(synopsis "Ctftilde website and user management.")
(description "Simple users management tool and website and built with
Cantius, part of a CTF competition VM.")
(license license:cc0)))
(define %services
%base-services)
(define-syntax-rule (prepend list item)
(define list
(cons item list)))
(prepend %services
(service dhcp-client-service-type))
(prepend %services
(simple-service 'cert-access-ctftilde activation-service-type
#~(let ((access-gid (group:gid (getgrnam "cert-ctftilde"))))
(for-each (lambda (file mode)
(let ((full-path (string-append "/etc/cert-ctftilde" file)))
(chown full-path 0 access-gid)
(chmod full-path mode)))
'("" "/fullchain.pem" "/privkey.pem")
'(#o750 #o640 #o640)))))
(prepend %services
(service elogind-service-type))
(prepend %services
(simple-service 'gemini-main-server-directory activation-service-type
#~(begin
(false-if-exception (delete-file "/srv/gemini"))
(mkdir-p "/srv")
(symlink #$(local-file (string-append %here "/gemini")
#:recursive? #t)
"/srv/gemini"))))
(prepend %services
(simple-service 'gemini-users-directory activation-service-type
#~(mkdir-p "/srv/gemini-users")))
(prepend %services
(service web:gmnisrv-service-type
(web:gmnisrv-configuration
(config-file (local-file "gmnisrv.ini")))))
(prepend %services
(simple-service 'guix-authorize-key guix-service-type
(guix-extension
(authorized-keys (list (local-file "guix-signing-key.pub"))))))
(prepend %services
(simple-service 'http-users-directory activation-service-type
#~(mkdir-p "/srv/http-users")))
(prepend %services
(service web:httpd-service-type
(web:httpd-configuration
(config
(web:httpd-config-file
(server-name "ctftilde.koszko.org")
(listen '("80" "443"))
(error-log "/var/log/httpd/error.log")
(modules (append
(map (lambda (name)
(let ((filename (format #f "/modules/mod_~a.so" name)))
(web:httpd-module
(name (string-append name "_module"))
(file (file-append httpd filename)))))
(list "cgid"
"headers"
"logio"
"proxy"
"proxy_http"
"rewrite"
"ssl"))
web:%default-httpd-modules))
(extra-config
(list "\
LogFormat \"%>s %u %t \\\"%r\\\" %I in %O out \
\\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" combined
CustomLog /var/log/httpd/access.log combined
ScriptSock /var/run/cgid.sock
")))))))
(prepend %services
(simple-service 'http-virtualhost-ctftilde web:httpd-service-type
(map (lambda (port)
(web:httpd-virtualhost
(format #f "*:~a" port)
`("\
ServerName ctftilde.koszko.org
ServerAlias www.ctftilde.koszko.org
ServerAdmin webmaster@ctftilde.koszko.org
RewriteEngine On
RewriteRule \"^/~([^/]+)(.*)\" \"/srv/http-users/$1$2\"
ProxyPassMatch ^/(([^~].*)?)$ http://127.0.0.1:8080/$1
ProxyPassReverse / http://127.0.0.1:8080/
"
. ,(if (= port 443)
'("\
SSLEngine on
SSLCertificateFile /etc/cert-ctftilde/fullchain.pem
SSLCertificateKeyFile /etc/cert-ctftilde/privkey.pem
")
'()))))
'(80 443))))
(prepend %services
(service cron-daemon-service-type))
(prepend %services
(service openssh-service-type
(openssh-configuration
(permit-root-login 'prohibit-password)
(authorized-keys `(("root" ,(local-file "owner.pub"))))
(port-number 22))))
(define %ctftilde-accounts
(list (user-group (name "ctftilde") (system? #t))
(user-account
(name "ctftilde")
(group "ctftilde")
(system? #t)
(comment "ctftilde http website server user")
(home-directory "/var/run/ctftilde")
(shell (file-append shadow "/sbin/nologin")))))
(define %ctftilde-activation
#~(begin
(mkdir-p "/var/db")
(let ((old-umask (umask)))
(dynamic-wind
noop
(lambda _
(umask #o077)
(mkdir-p "/var/db/ctftilde/users")
(mkdir-p "/var/db/ctftilde/users-to-delete"))
(lambda _
(umask old-umask))))
(system* (string-append #$ctftilde "/bin/ctftilde") "--users-recreate"
"--log-file" "/var/log/ctftilde")))
(define %ctftilde-shepherd-services
(list (shepherd-service
(requirement '(networking))
(provision '(ctftilde))
(start #~(make-forkexec-constructor
(list #$(file-append ctftilde "/bin/ctftilde")
"--site-host" "--log-file" "/var/log/ctftilde")))
(stop #~(make-kill-destructor))
(documentation
"ctftilde server daemon (user management & http site)."))))
(define ctftile-service-type
(service-type (name 'ctftilde)
(description "Host ctftilde main website and manage users.")
(extensions
(list (service-extension account-service-type
(const %ctftilde-accounts))
(service-extension activation-service-type
(const %ctftilde-activation))
(service-extension shepherd-root-service-type
(const %ctftilde-shepherd-services))))
(default-value #f)))
(prepend %services
(service ctftile-service-type))
(define bootloader-target
(or (getenv "CTFTILDE_DISK_DEV") "/dev/sda"))
(format #t "bootloader target: ~A~%" bootloader-target)
(operating-system
(host-name "ctftilde")
(timezone "Europe/Warsaw")
(locale "en_US.utf8")
(keyboard-layout (keyboard-layout "pl"))
(bootloader (bootloader-configuration
(bootloader grub-bootloader)
(targets (list bootloader-target))))
(users %base-user-accounts)
(groups (cons* (user-group (name "cert-ctftilde") (system? #t))
(user-group (name "hackers"))
%base-groups))
(extra-groups (list (user-extra-groups
(user "httpd")
(groups '("cert-ctftilde")))))
(file-systems (cons* (fs:file-system
(device (fs:file-system-label "ctftilde-root"))
(mount-point "/")
(type "ext4"))
fs:%base-file-systems))
(packages (append (specifications->packages
'("file"
"net-tools"
"man-pages-posix"
"emacs"))
(list ctftilde)
%base-packages))
(services %services))
;;; Local Variables:
;;; eval: (put 'prepend 'scheme-indent-function 1)
;;; eval: (put 'simple-service 'scheme-indent-function 2)
;;; End:
|