/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
WARN:=-O -Wall -Wextra -Wdouble-promotion -Wformat=2 -Winit-self \
	-Wmissing-include-dirs -Wswitch-default -Wswitch-enum \
	-Wunused -Wuninitialized -Wstrict-overflow=5 \
	-Wsuggest-attribute=pure -Wsuggest-attribute=const \
	-Wsuggest-attribute=noreturn -Wfloat-equal -Wundef -Wshadow \
	-Wunsafe-loop-optimizations -Wpointer-arith \
	-Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings \
	-Wconversion -Wlogical-op -Waggregate-return \
	-Wstrict-prototypes -Wold-style-definition \
	-Wmissing-format-attribute -Wnormalized=nfc -Wpacked \
	-Wredundant-decls -Wnested-externs -Winline -Wvla \
	-Wvolatile-register-var -Woverlength-strings

#DEBUG:=-ggdb3 -fsanitize=address $(SANITIZE)
## Check which sanitizing options can be used
#SANITIZE:=$(foreach option,$(ALL_SANITIZE_OPTIONS),$(shell \
#	echo 'int main(){}' | $(CC) --language=c $(option) \
#	/dev/stdin -o /dev/null >/dev/null 2>&1 && echo $(option)))
# <https://developerblog.redhat.com/2014/10/16/gcc-undefined-behavior-sanitizer-ubsan/>
ALL_SANITIZE_OPTIONS:=-fsanitize=leak -fsanitize=undefined \
        -fsanitize=shift -fsanitize=integer-divide-by-zero \
        -fsanitize=unreachable -fsanitize=vla-bound -fsanitize=null \
        -fsanitize=return -fsanitize=signed-integer-overflow \
        -fsanitize=bounds -fsanitize=alignment \
        -fsanitize=object-size -fsanitize=float-divide-by-zero \
        -fsanitize=float-cast-overflow -fsanitize=nonnull-attribute \
        -fsanitize=returns-nonnull-attribute -fsanitize=bool \
        -fsanitize=enum -fsanitize-address-use-after-scope

# For info about _FORTIFY_SOURCE, see feature_test_macros(7)
# and <https://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html>.
FORTIFY:=-fstack-protector-all -fPIC
CPPFLAGS+=-D_FORTIFY_SOURCE=3
LINK_FORTIFY_LD:=-z relro -z now
LINK_FORTIFY:=

# If BROKEN_PIE is set, do not build with -pie
ifndef BROKEN_PIE
FORTIFY += -fPIE
LINK_FORTIFY += -pie
endif
#COVERAGE=--coverage
OPTIMIZE:=-Os -fno-strict-aliasing
LANGUAGE:=-std=gnu11
CPPFLAGS+=-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64
htmldir:=man
version:=1.8.16
SED:=sed
PKG_CONFIG?=pkg-config

USER:=$(firstword $(subst :, ,$(shell getent passwd _mandos \
	|| getent passwd nobody || echo 65534)))
GROUP:=$(firstword $(subst :, ,$(shell getent group _mandos \
	|| getent group nogroup || echo 65534)))

LINUXVERSION:=$(shell uname --kernel-release)

## Use these settings for a traditional /usr/local install
# PREFIX:=$(DESTDIR)/usr/local
# CONFDIR:=$(DESTDIR)/etc/mandos
# KEYDIR:=$(DESTDIR)/etc/mandos/keys
# MANDIR:=$(PREFIX)/man
# INITRAMFSTOOLS:=$(DESTDIR)/etc/initramfs-tools
# DRACUTMODULE:=$(DESTDIR)/usr/lib/dracut/modules.d/90mandos
# STATEDIR:=$(DESTDIR)/var/lib/mandos
# LIBDIR:=$(PREFIX)/lib
# DBUSPOLICYDIR:=$(DESTDIR)/etc/dbus-1/system.d
##

## These settings are for a package-type install
PREFIX:=$(DESTDIR)/usr
CONFDIR:=$(DESTDIR)/etc/mandos
KEYDIR:=$(DESTDIR)/etc/keys/mandos
MANDIR:=$(PREFIX)/share/man
INITRAMFSTOOLS:=$(DESTDIR)/usr/share/initramfs-tools
DRACUTMODULE:=$(DESTDIR)/usr/lib/dracut/modules.d/90mandos
STATEDIR:=$(DESTDIR)/var/lib/mandos
LIBDIR:=$(shell \
	for d in \
	"/usr/lib/`dpkg-architecture \
			-qDEB_HOST_MULTIARCH 2>/dev/null`" \
	"`rpm --eval='%{_libdir}' 2>/dev/null`" /usr/lib; do \
		if [ -d "$$d" -a "$$d" = "$${d%/}" ]; then \
			echo "$(DESTDIR)$$d"; \
			break; \
		fi; \
	done)
DBUSPOLICYDIR:=$(DESTDIR)/usr/share/dbus-1/system.d
##

SYSTEMD:=$(DESTDIR)$(shell $(PKG_CONFIG) systemd \
			--variable=systemdsystemunitdir)
TMPFILES:=$(DESTDIR)$(shell $(PKG_CONFIG) systemd \
			--variable=tmpfilesdir)
SYSUSERS:=$(DESTDIR)$(shell $(PKG_CONFIG) systemd \
			--variable=sysusersdir)

GNUTLS_CFLAGS:=$(shell $(PKG_CONFIG) --cflags-only-I gnutls)
GNUTLS_LIBS:=$(shell $(PKG_CONFIG) --libs gnutls)
AVAHI_CFLAGS:=$(shell $(PKG_CONFIG) --cflags-only-I avahi-core)
AVAHI_LIBS:=$(shell $(PKG_CONFIG) --libs avahi-core)
GPGME_CFLAGS:=$(shell $(PKG_CONFIG) --cflags-only-I gpgme 2>/dev/null \
	|| gpgme-config --cflags; getconf LFS_CFLAGS)
GPGME_LIBS:=$(shell $(PKG_CONFIG) --libs gpgme 2>/dev/null \
	|| gpgme-config --libs; getconf LFS_LIBS; \
	getconf LFS_LDFLAGS)
LIBNL3_CFLAGS:=$(shell $(PKG_CONFIG) --cflags-only-I libnl-route-3.0)
LIBNL3_LIBS:=$(shell $(PKG_CONFIG) --libs libnl-route-3.0)
GLIB_CFLAGS:=$(shell $(PKG_CONFIG) --cflags glib-2.0)
GLIB_LIBS:=$(shell $(PKG_CONFIG) --libs glib-2.0)

# Do not change these two
CFLAGS+=$(WARN) $(DEBUG) $(FORTIFY) $(COVERAGE) $(OPTIMIZE) \
	$(LANGUAGE) -DVERSION='"$(version)"'
LDFLAGS+=-Xlinker --as-needed $(COVERAGE) $(LINK_FORTIFY) $(strip \
	) $(foreach flag,$(LINK_FORTIFY_LD),-Xlinker $(flag))

# Commands to format a DocBook <refentry> document into a manual page
DOCBOOKTOMAN=$(strip cd $(dir $<); xsltproc --nonet --xinclude \
	--param man.charmap.use.subset		0 \
	--param make.year.ranges		1 \
	--param make.single.year.ranges		1 \
	--param man.output.quietly		1 \
	--param man.authors.section.enabled	0 \
	/usr/share/xml/docbook/stylesheet/nwalsh/manpages/docbook.xsl \
	$(notdir $<); \
	if locale --all 2>/dev/null | grep --regexp='^en_US\.utf8$$' \
	&& command -v man >/dev/null; then LANG=en_US.UTF-8 \
	MANWIDTH=80 man --warnings --encoding=UTF-8 --local-file \
	$(notdir $@); fi >/dev/null)

DOCBOOKTOHTML=$(strip xsltproc --nonet --xinclude \
	--param make.year.ranges		1 \
	--param make.single.year.ranges		1 \
	--param man.output.quietly		1 \
	--param man.authors.section.enabled	0 \
	--param citerefentry.link		1 \
	--output $@ \
	/usr/share/xml/docbook/stylesheet/nwalsh/xhtml/docbook.xsl \
	$<; $(HTMLPOST) $@)
# Fix citerefentry links
HTMLPOST:=$(SED) --in-place \
	--expression='s/\(<a class="citerefentry" href="\)\("><span class="citerefentry"><span class="refentrytitle">\)\([^<]*\)\(<\/span>(\)\([^)]*\)\()<\/span><\/a>\)/\1\3.\5\2\3\4\5\6/g'

PLUGINS:=plugins.d/password-prompt plugins.d/mandos-client \
	plugins.d/usplash plugins.d/splashy plugins.d/askpass-fifo \
	plugins.d/plymouth
PLUGIN_HELPERS:=plugin-helpers/mandos-client-iprouteadddel
CPROGS:=plugin-runner dracut-module/password-agent $(PLUGINS) \
	$(PLUGIN_HELPERS)
PROGS:=mandos mandos-keygen mandos-ctl mandos-monitor $(CPROGS)
DOCS:=mandos.8 mandos-keygen.8 mandos-monitor.8 mandos-ctl.8 \
	mandos.conf.5 mandos-clients.conf.5 plugin-runner.8mandos \
	dracut-module/password-agent.8mandos \
	plugins.d/mandos-client.8mandos \
	plugins.d/password-prompt.8mandos plugins.d/usplash.8mandos \
	plugins.d/splashy.8mandos plugins.d/askpass-fifo.8mandos \
	plugins.d/plymouth.8mandos intro.8mandos

htmldocs:=$(addsuffix .xhtml,$(DOCS))

objects:=$(addsuffix .o,$(CPROGS))

.PHONY: all
all: $(PROGS) mandos.lsm

.PHONY: doc
doc: $(DOCS)

.PHONY: html
html: $(htmldocs)

%.5: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOMAN)
%.5.xhtml: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOHTML)

%.8: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOMAN)
%.8.xhtml: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOHTML)

%.8mandos: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOMAN)
%.8mandos.xhtml: %.xml common.ent legalnotice.xml
	$(DOCBOOKTOHTML)

intro.8mandos: intro.xml common.ent legalnotice.xml
	$(DOCBOOKTOMAN)
intro.8mandos.xhtml: intro.xml common.ent legalnotice.xml
	$(DOCBOOKTOHTML)

mandos.8: mandos.xml common.ent mandos-options.xml overview.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
mandos.8.xhtml: mandos.xml common.ent mandos-options.xml \
		overview.xml legalnotice.xml
	$(DOCBOOKTOHTML)

mandos-keygen.8: mandos-keygen.xml common.ent overview.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
mandos-keygen.8.xhtml: mandos-keygen.xml common.ent overview.xml \
		 legalnotice.xml
	$(DOCBOOKTOHTML)

mandos-monitor.8: mandos-monitor.xml common.ent overview.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
mandos-monitor.8.xhtml: mandos-monitor.xml common.ent overview.xml \
		 legalnotice.xml
	$(DOCBOOKTOHTML)

mandos-ctl.8: mandos-ctl.xml common.ent overview.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
mandos-ctl.8.xhtml: mandos-ctl.xml common.ent overview.xml \
		 legalnotice.xml
	$(DOCBOOKTOHTML)

mandos.conf.5: mandos.conf.xml common.ent mandos-options.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
mandos.conf.5.xhtml: mandos.conf.xml common.ent mandos-options.xml \
		legalnotice.xml
	$(DOCBOOKTOHTML)

plugin-runner.8mandos: plugin-runner.xml common.ent overview.xml \
		legalnotice.xml
	$(DOCBOOKTOMAN)
plugin-runner.8mandos.xhtml: plugin-runner.xml common.ent \
		overview.xml legalnotice.xml
	$(DOCBOOKTOHTML)

dracut-module/password-agent.8mandos: \
		dracut-module/password-agent.xml common.ent \
		overview.xml legalnotice.xml
	$(DOCBOOKTOMAN)
dracut-module/password-agent.8mandos.xhtml: \
		dracut-module/password-agent.xml common.ent \
		overview.xml legalnotice.xml
	$(DOCBOOKTOHTML)

plugins.d/mandos-client.8mandos: plugins.d/mandos-client.xml \
					common.ent \
					mandos-options.xml \
					overview.xml legalnotice.xml
	$(DOCBOOKTOMAN)
plugins.d/mandos-client.8mandos.xhtml: plugins.d/mandos-client.xml \
					common.ent \
					mandos-options.xml \
					overview.xml legalnotice.xml
	$(DOCBOOKTOHTML)

# Update all these files with version number $(version)
common.ent: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(<!ENTITY version "\)[^"]*">$$/\1$(version)">/' \
		$@)

mandos: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(version = "\)[^"]*"$$/\1$(version)"/' \
		$@)

mandos-keygen: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(VERSION="\)[^"]*"$$/\1$(version)"/' \
		$@)

mandos-ctl: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(version = "\)[^"]*"$$/\1$(version)"/' \
		$@)

mandos-monitor: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(version = "\)[^"]*"$$/\1$(version)"/' \
		$@)

mandos.lsm: Makefile
	$(strip $(SED) --in-place \
		--expression='s/^\(Version:\).*/\1\t$(version)/' \
		$@)
	$(strip $(SED) --in-place \
		--expression='s/^\(Entered-date:\).*/\1\t$(shell date --rfc-3339=date --reference=Makefile)/' \
		$@)
	$(strip $(SED) --in-place \
		--expression='s/\(mandos_\)[0-9.]\+\(\.orig\.tar\.gz\)/\1$(version)\2/' \
		$@)

# Uses nested functions
plugin-runner: LDFLAGS += -Xlinker --no-warn-execstack
dracut-module/password-agent: LDFLAGS += -Xlinker --no-warn-execstack
plugins.d/password-prompt: LDFLAGS += -Xlinker --no-warn-execstack
plugins.d/mandos-client: LDFLAGS += -Xlinker --no-warn-execstack
plugins.d/plymouth: LDFLAGS += -Xlinker --no-warn-execstack

# Need to add the GnuTLS, Avahi and GPGME libraries
plugins.d/mandos-client: CFLAGS += $(GNUTLS_CFLAGS) $(strip \
	) $(AVAHI_CFLAGS) $(GPGME_CFLAGS)
plugins.d/mandos-client: LDLIBS += $(GNUTLS_LIBS) $(strip \
	) $(AVAHI_LIBS) $(GPGME_LIBS)

# Need to add the libnl-route library
plugin-helpers/mandos-client-iprouteadddel: CFLAGS += $(LIBNL3_CFLAGS)
plugin-helpers/mandos-client-iprouteadddel: LDLIBS += $(LIBNL3_LIBS)

# Need to add the GLib and pthread libraries
dracut-module/password-agent: CFLAGS += $(GLIB_CFLAGS)
# Note: -lpthread is unnecessary with the GNU C library 2.34 or later
dracut-module/password-agent: LDLIBS += $(GLIB_LIBS) -lpthread

.PHONY: clean
clean:
	-rm --force $(CPROGS) $(objects) $(htmldocs) $(DOCS) core

.PHONY: distclean
distclean: clean
.PHONY: mostlyclean
mostlyclean: clean
.PHONY: maintainer-clean
maintainer-clean: clean
	-rm --force --recursive keydir confdir statedir

.PHONY: check
check: all
	./mandos --check
	./mandos-ctl --check
	./mandos-keygen --version
	./plugin-runner --version
	./plugin-helpers/mandos-client-iprouteadddel --version
	./dracut-module/password-agent --test

# Run the client with a local config and key
.PHONY: run-client
run-client: all keydir/seckey.txt keydir/pubkey.txt \
			keydir/tls-privkey.pem keydir/tls-pubkey.pem
	@echo '######################################################'
	@echo '# The following error messages are harmless and can  #'
	@echo '#  be safely ignored:                                #'
	@echo '## From plugin-runner:                               #'
	@echo '# setgid: Operation not permitted                    #'
	@echo '# setuid: Operation not permitted                    #'
	@echo '## From askpass-fifo:                                #'
	@echo '# mkfifo: Permission denied                          #'
	@echo '## From mandos-client:                               #'
	@echo '# Failed to raise privileges: Operation not permi... #'
	@echo '# Warning: network hook "*" exited with status *     #'
	@echo '# ioctl SIOCSIFFLAGS +IFF_UP: Operation not permi... #'
	@echo '# Failed to bring up interface "*": Operation not... #'
	@echo '#                                                    #'
	@echo '# (The messages are caused by not running as root,   #'
	@echo '# but you should NOT run "make run-client" as root   #'
	@echo '# unless you also unpacked and compiled Mandos as    #'
	@echo '# root, which is also NOT recommended.)              #'
	@echo '######################################################'
# We set GNOME_KEYRING_CONTROL to block pam_gnome_keyring
	./plugin-runner --plugin-dir=plugins.d \
		--plugin-helper-dir=plugin-helpers \
		--config-file=plugin-runner.conf \
		--options-for=mandos-client:--seckey=keydir/seckey.txt,--pubkey=keydir/pubkey.txt,--tls-privkey=keydir/tls-privkey.pem,--tls-pubkey=keydir/tls-pubkey.pem,--network-hook-dir=network-hooks.d \
		--env-for=mandos-client:GNOME_KEYRING_CONTROL= \
		$(CLIENTARGS)

# Used by run-client
keydir/seckey.txt keydir/pubkey.txt keydir/tls-privkey.pem keydir/tls-pubkey.pem: mandos-keygen
	install --directory keydir
	./mandos-keygen --dir keydir --force
	if ! [ -e keydir/tls-privkey.pem ]; then \
		install --mode=u=rw /dev/null keydir/tls-privkey.pem; \
	fi
	if ! [ -e keydir/tls-pubkey.pem ]; then \
		install --mode=u=rw /dev/null keydir/tls-pubkey.pem; \
	fi

# Run the server with a local config
.PHONY: run-server
run-server: confdir/mandos.conf confdir/clients.conf statedir
	./mandos --debug --no-dbus --configdir=confdir \
		--statedir=statedir $(SERVERARGS)

# Used by run-server
confdir/mandos.conf: mandos.conf
	install -D --mode=u=rw,go=r $^ $@
confdir/clients.conf: clients.conf keydir/seckey.txt keydir/tls-pubkey.pem
	install -D --mode=u=rw $< $@
# Add a client password
	./mandos-keygen --dir keydir --password --no-ssh >> $@
statedir:
	install --directory statedir

.PHONY: install
install: install-server install-client-nokey

.PHONY: install-html
install-html: html
	install -D --mode=u=rw,go=r --target-directory=$(htmldir) \
		$(htmldocs)

.PHONY: install-server
install-server: doc
	if install --directory --mode=u=rwx --owner=$(USER) \
		--group=$(GROUP) $(STATEDIR); then \
		:; \
	elif install --directory --mode=u=rwx $(STATEDIR); then \
		chown -- $(USER):$(GROUP) $(STATEDIR) || :; \
	fi
	if [ "$(TMPFILES)" != "$(DESTDIR)" ]; then \
		install -D --mode=u=rw,go=r tmpfiles.d-mandos.conf \
			$(TMPFILES)/mandos.conf; \
	fi
	if [ "$(SYSUSERS)" != "$(DESTDIR)" ]; then \
		install -D --mode=u=rw,go=r sysusers.d-mandos.conf \
			$(SYSUSERS)/mandos.conf; \
	fi
	install --directory $(PREFIX)/sbin
	install --mode=u=rwx,go=rx --target-directory=$(PREFIX)/sbin \
		mandos
	install --mode=u=rwx,go=rx --target-directory=$(PREFIX)/sbin \
		mandos-ctl
	install --mode=u=rwx,go=rx --target-directory=$(PREFIX)/sbin \
		mandos-monitor
	install --directory $(CONFDIR)
	install --mode=u=rw,go=r --target-directory=$(CONFDIR) \
		mandos.conf
	install --mode=u=rw --target-directory=$(CONFDIR) \
		clients.conf
	install -D --mode=u=rw,go=r dbus-mandos.conf \
		$(DBUSPOLICYDIR)/mandos.conf
	install -D --mode=u=rwx,go=rx init.d-mandos \
		$(DESTDIR)/etc/init.d/mandos
	if [ "$(SYSTEMD)" != "$(DESTDIR)" ]; then \
		install -D --mode=u=rw,go=r mandos.service \
			$(SYSTEMD); \
	fi
	install -D --mode=u=rw,go=r default-mandos \
		$(DESTDIR)/etc/default/mandos
	if [ -z $(DESTDIR) ]; then \
		update-rc.d mandos defaults 25 15;\
	fi
	install --directory $(MANDIR)/man8 $(MANDIR)/man5
	gzip --best --to-stdout mandos.8 \
		> $(MANDIR)/man8/mandos.8.gz
	gzip --best --to-stdout mandos-monitor.8 \
		> $(MANDIR)/man8/mandos-monitor.8.gz
	gzip --best --to-stdout mandos-ctl.8 \
		> $(MANDIR)/man8/mandos-ctl.8.gz
	gzip --best --to-stdout mandos.conf.5 \
		> $(MANDIR)/man5/mandos.conf.5.gz
	gzip --best --to-stdout mandos-clients.conf.5 \
		> $(MANDIR)/man5/mandos-clients.conf.5.gz
	gzip --best --to-stdout intro.8mandos \
		> $(MANDIR)/man8/intro.8mandos.gz

.PHONY: install-client-nokey
install-client-nokey: all doc
	install --directory --mode=u=rwx $(KEYDIR) \
		$(LIBDIR)/mandos/plugins.d \
		$(LIBDIR)/mandos/plugin-helpers
	if [ "$(SYSUSERS)" != "$(DESTDIR)" ]; then \
		install -D --mode=u=rw,go=r sysusers.d-mandos.conf \
			$(SYSUSERS)/mandos-client.conf; \
	fi
	if [ "$(CONFDIR)" != "$(LIBDIR)/mandos" ]; then \
		install --directory \
			--mode=u=rwx "$(CONFDIR)/plugins.d" \
			"$(CONFDIR)/plugin-helpers"; \
	fi
	install --directory --mode=u=rwx,go=rx \
		"$(CONFDIR)/network-hooks.d"
	install --mode=u=rwx,go=rx \
		--target-directory=$(LIBDIR)/mandos plugin-runner
	install --mode=u=rwx,go=rx \
		--target-directory=$(LIBDIR)/mandos \
		mandos-to-cryptroot-unlock
	install --directory $(PREFIX)/sbin
	install --mode=u=rwx,go=rx --target-directory=$(PREFIX)/sbin \
		mandos-keygen
	install --mode=u=rwx,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/password-prompt
	install --mode=u=rwxs,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/mandos-client
	install --mode=u=rwxs,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/usplash
	install --mode=u=rwxs,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/splashy
	install --mode=u=rwxs,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/askpass-fifo
	install --mode=u=rwxs,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugins.d \
		plugins.d/plymouth
	install --mode=u=rwx,go=rx \
		--target-directory=$(LIBDIR)/mandos/plugin-helpers \
		plugin-helpers/mandos-client-iprouteadddel
	install -D initramfs-tools-hook \
		$(INITRAMFSTOOLS)/hooks/mandos
	install -D --mode=u=rw,go=r initramfs-tools-conf \
		$(INITRAMFSTOOLS)/conf.d/mandos-conf
	install -D --mode=u=rw,go=r initramfs-tools-conf-hook \
		$(INITRAMFSTOOLS)/conf-hooks.d/zz-mandos
	install -D initramfs-tools-script \
		$(INITRAMFSTOOLS)/scripts/init-premount/mandos
	install -D initramfs-tools-script-stop \
		$(INITRAMFSTOOLS)/scripts/local-premount/mandos
	install -D --mode=u=rw,go=r \
		--target-directory=$(DRACUTMODULE) \
		dracut-module/ask-password-mandos.path \
		dracut-module/ask-password-mandos.service
	install --mode=u=rwxs,go=rx \
		--target-directory=$(DRACUTMODULE) \
		dracut-module/module-setup.sh \
		dracut-module/cmdline-mandos.sh \
		dracut-module/password-agent
	install --mode=u=rw,go=r plugin-runner.conf $(CONFDIR)
	install --directory $(MANDIR)/man8
	gzip --best --to-stdout mandos-keygen.8 \
		> $(MANDIR)/man8/mandos-keygen.8.gz
	gzip --best --to-stdout plugin-runner.8mandos \
		> $(MANDIR)/man8/plugin-runner.8mandos.gz
	gzip --best --to-stdout plugins.d/mandos-client.8mandos \
		> $(MANDIR)/man8/mandos-client.8mandos.gz
	gzip --best --to-stdout plugins.d/password-prompt.8mandos \
		> $(MANDIR)/man8/password-prompt.8mandos.gz
	gzip --best --to-stdout plugins.d/usplash.8mandos \
		> $(MANDIR)/man8/usplash.8mandos.gz
	gzip --best --to-stdout plugins.d/splashy.8mandos \
		> $(MANDIR)/man8/splashy.8mandos.gz
	gzip --best --to-stdout plugins.d/askpass-fifo.8mandos \
		> $(MANDIR)/man8/askpass-fifo.8mandos.gz
	gzip --best --to-stdout plugins.d/plymouth.8mandos \
		> $(MANDIR)/man8/plymouth.8mandos.gz
	gzip --best --to-stdout dracut-module/password-agent.8mandos \
		> $(MANDIR)/man8/password-agent.8mandos.gz

.PHONY: install-client
install-client: install-client-nokey
# Post-installation stuff
	-$(PREFIX)/sbin/mandos-keygen --dir "$(KEYDIR)"
	if command -v update-initramfs >/dev/null; then \
	    update-initramfs -k all -u; \
	elif command -v dracut >/dev/null; then \
	    for initrd in $(DESTDIR)/boot/initr*-$(LINUXVERSION); do \
		if [ -w "$$initrd" ]; then \
		    chmod go-r "$$initrd"; \
		    dracut --force "$$initrd"; \
		fi; \
	    done; \
	fi
	echo "Now run mandos-keygen --password --dir $(KEYDIR)"

.PHONY: uninstall
uninstall: uninstall-server uninstall-client

.PHONY: uninstall-server
uninstall-server:
	-rm --force $(PREFIX)/sbin/mandos \
		$(PREFIX)/sbin/mandos-ctl \
		$(PREFIX)/sbin/mandos-monitor \
		$(MANDIR)/man8/mandos.8.gz \
		$(MANDIR)/man8/mandos-monitor.8.gz \
		$(MANDIR)/man8/mandos-ctl.8.gz \
		$(MANDIR)/man5/mandos.conf.5.gz \
		$(MANDIR)/man5/mandos-clients.conf.5.gz
	update-rc.d -f mandos remove
	-rmdir $(CONFDIR)

.PHONY: uninstall-client
uninstall-client:
# Refuse to uninstall client if /etc/crypttab is explicitly configured
# to use it.
	! grep --regexp='^ *[^ #].*keyscript=[^,=]*/mandos/' \
		$(DESTDIR)/etc/crypttab
	-rm --force $(PREFIX)/sbin/mandos-keygen \
		$(LIBDIR)/mandos/plugin-runner \
		$(LIBDIR)/mandos/plugins.d/password-prompt \
		$(LIBDIR)/mandos/plugins.d/mandos-client \
		$(LIBDIR)/mandos/plugins.d/usplash \
		$(LIBDIR)/mandos/plugins.d/splashy \
		$(LIBDIR)/mandos/plugins.d/askpass-fifo \
		$(LIBDIR)/mandos/plugins.d/plymouth \
		$(INITRAMFSTOOLS)/hooks/mandos \
		$(INITRAMFSTOOLS)/conf-hooks.d/mandos \
		$(INITRAMFSTOOLS)/scripts/init-premount/mandos \
		$(INITRAMFSTOOLS)/scripts/local-premount/mandos \
		$(DRACUTMODULE)/ask-password-mandos.path \
		$(DRACUTMODULE)/ask-password-mandos.service \
		$(DRACUTMODULE)/module-setup.sh \
		$(DRACUTMODULE)/cmdline-mandos.sh \
		$(DRACUTMODULE)/password-agent \
		$(MANDIR)/man8/mandos-keygen.8.gz \
		$(MANDIR)/man8/plugin-runner.8mandos.gz \
		$(MANDIR)/man8/mandos-client.8mandos.gz
		$(MANDIR)/man8/password-prompt.8mandos.gz \
		$(MANDIR)/man8/usplash.8mandos.gz \
		$(MANDIR)/man8/splashy.8mandos.gz \
		$(MANDIR)/man8/askpass-fifo.8mandos.gz \
		$(MANDIR)/man8/plymouth.8mandos.gz \
		$(MANDIR)/man8/password-agent.8mandos.gz \
	-rmdir $(LIBDIR)/mandos/plugins.d $(CONFDIR)/plugins.d \
		 $(LIBDIR)/mandos $(CONFDIR) $(KEYDIR) $(DRACUTMODULE)
	if command -v update-initramfs >/dev/null; then \
	    update-initramfs -k all -u; \
	elif command -v dracut >/dev/null; then \
	    for initrd in $(DESTDIR)/boot/initr*-$(LINUXVERSION); do \
		test -w "$$initrd" && dracut --force "$$initrd"; \
	    done; \
	fi

.PHONY: purge
purge: purge-server purge-client

.PHONY: purge-server
purge-server: uninstall-server
	-rm --force $(CONFDIR)/mandos.conf $(CONFDIR)/clients.conf \
		$(DESTDIR)/etc/dbus-1/system.d/mandos.conf
		$(DESTDIR)/etc/default/mandos \
		$(DESTDIR)/etc/init.d/mandos \
		$(DESTDIR)/run/mandos.pid \
		$(DESTDIR)/var/run/mandos.pid
	if [ "$(SYSTEMD)" != "$(DESTDIR)" -a -d "$(SYSTEMD)" ]; then \
		-rm --force -- $(SYSTEMD)/mandos.service; \
	fi
	-rmdir $(CONFDIR)

.PHONY: purge-client
purge-client: uninstall-client
	-shred --remove $(KEYDIR)/seckey.txt $(KEYDIR)/tls-privkey.pem
	-rm --force $(CONFDIR)/plugin-runner.conf \
		$(KEYDIR)/pubkey.txt $(KEYDIR)/seckey.txt \
		$(KEYDIR)/tls-pubkey.txt $(KEYDIR)/tls-privkey.txt
	-rmdir $(KEYDIR) $(CONFDIR)/plugins.d $(CONFDIR)