aboutsummaryrefslogtreecommitdiffstats
path: root/ldap/Makefile
blob: 4dd0faa17948da02e6b43e643e5c8df4c634c138 (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
DIR := $(shell grep -i '^olcDbDirectory: ' database.ldif | sed -e 's/^olcDbDirectory: //')
SUFFIX := $(shell grep -i '^olcSuffix: ' database.ldif | sed -e 's/^olcSuffix: //')
TMPSLAPD := /tmp/$(shell mktemp -u slapd.d-XXXXXX)
BACKUPDB := /tmp/$(shell mktemp -u db-XXXXXX.ldif)
BACKUPCONFIG := /tmp/$(shell mktemp -u config-XXXXXX.ldif)
NUM := $(shell ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b "cn=config" "olcSuffix=$(SUFFIX)" dn | sed -e '/^$$/d' -e 's/^dn: //')
NUM2 := $(shell echo "$(NUM)" | sed -re 's/(.*),(cn=config)$$/\2\/\1/')
SCHEMA := $(shell grep -i '^dn: ' fripost.ldif | sed -re 's/^dn: cn=([^,]+),.*/\1/')

all:
	@echo "make install-schema: install the LDAP schema"
	@echo "make install-db:		install the database"
	@echo "make install-mx:		"


init:
	@echo "Suffix:   \`$(SUFFIX)\'"
	@echo "Location: \`$(DIR)\'"
	@test -e "$(DIR)" && (echo "Error: \`$(DIR)' exists." ; false); true
	@mkdir -m 0700 "$(DIR)"
	@chown openldap:openldap "$(DIR)"
	@ldapadd -Q -Y EXTERNAL -H ldapi:/// -f database.ldif
#	@if test -d "$(SUFFIX)" ; then echo nop ; fi


install-mx:

	rm -f -- "$(BACKUPDB)" "$(BACKUPCONFIG)"
	@mkdir -m 0700 "$(BACKUPSLAPD)"

	@rmdir "$(BACKUPSLAPD)"

install:
	# ldapadd -Y EXTERNAL -H ldapi:/// -f fripost.ldif
	# ldapadd -Y EXTERNAL -H ldapi:/// -f index.ldif
	# ldapadd -Y EXTERNAL -H ldapi:/// -f constraint.ldif
	# ldapadd -Y EXTERNAL -H ldapi:/// -f acl.ldif
	# ldapadd -Y EXTERNAL -H ldapi:/// -f base.ldif
	# ldapadd -Y EXTERNAL -H ldapi:/// -f populate.ldif

install-schema:
	@ldapadd -Q -Y EXTERNAL -H ldapi:/// -f fripost.ldif


install-acl:
	@sed "s/^dn: olcDatabase={.*}hdb,cn=config$$/dn: $(NUM)/" acl.ldif | ldapmodify -Q -Y EXTERNAL -H ldapi:///


uninstall:
	@echo "The database - suffix \"$(SUFFIX)\" - will be saved into \`$(BACKUPDB)' (if non-empty), and then *cleared* (but not deleted)."
	@echo "Also, its ACLs, indexes and constraints will be cleared as well, if there are any."
	@echo "Global configuration will be saved into \`$(BACKUPCONFIG)'."
	@echo "slapd will be offline for a moment - at best."
	@/bin/echo -n "Are you sure you want to continue [y/N] "
	@read A && if [ "x$$A" = "xY" -o "x$$A" = "xy" ]; then true; else echo Aborted; false; fi
#
	@if slapcat -b "$(SUFFIX)" 2>/dev/null | grep -q '.'; then \
		echo "Saving database" && slapcat -b "$(SUFFIX)" -l "$(BACKUPDB)" && echo "Clearing database" && ldapdelete -Q -Y EXTERNAL -H ldapi:/// -r "$(SUFFIX)" \
	;else \
		echo "Empty database, not saving anything." \
	;fi
#
	@echo "Saving configuration"; slapcat -n0 -l "$(BACKUPCONFIG)"
#
	@if (ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -s base -b "$(NUM)" "(olcAccess=*)" | grep -q '^dn: '); then \
		echo "Removing ACLs" && /bin/echo -e "dn: $(NUM)\nchangetype: modify\ndelete: olcAccess" | ldapmodify -Q -Y EXTERNAL -H ldapi:/// >/dev/null \
	;else \
		echo "No ACLs to remove" \
	;fi
#
	@if (ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -s base -b "$(NUM)" "(olcDbIndex=*)" | grep -q '^dn: '); then \
		echo "Removing indexes" && /bin/echo -e "dn: $(NUM)\nchangetype: modify\ndelete: olcDbIndex" | ldapmodify -Q -Y EXTERNAL -H ldapi:/// >/dev/null \
	;else \
		echo "No indexes to remove" \
	;fi
#
	@echo "Making a new configuration directory at \`$(TMPSLAPD)'"
	@mkdir -m 0700 "$(TMPSLAPD)" && slapcat -n0 | slapadd -F "$(TMPSLAPD)" -n0 && chown -R 'openldap:openldap' "$(TMPSLAPD)"
#
	@echo "Deleting schema \"cn=$(SCHEMA),cn=config\"" && find "$(TMPSLAPD)/cn=config/cn=schema/" -type f -name "cn={*}$(SCHEMA).ldif" -delete
#
	@if test -d "$(TMPSLAPD)/$(NUM2)"; then \
		@echo "Deleting constraints" && find "$(TMPSLAPD)/$(NUM2)/" -type f -name "olcOverlay={*}constraint.ldif" -delete \
	;fi
#
	@/etc/init.d/slapd stop
#
	@echo "Replacing the old \`slapd.d'"
	rm -rf /etc/ldap/slapd.d/ && mv "$(TMPSLAPD)" /etc/ldap/slapd.d/
#
	@/etc/init.d/slapd start
#
	@echo "Don't forget to repopulate the database (if non-empty) from \`$(BACKUPDB)'"






.PHONY: all backup-db backup-config