Debian Codegraph @

debian/slapd.scripts-common

raw
1 # -*- sh -*-
2 # This file can be included with #SCRIPTSCOMMON#
3
4
5 # ===== Dumping and reloading using LDIF files ========================= {{{
6 #
7 # If incompatible changes are done to the database underlying a LDAP
8 # directory we need to dump the contents and reload the data into a newly
9 # created database after the new server was installed. The following
10 # functions deal with this functionality.
11
12
13 # ----- Configuration of this component -------------------------------- {{{
14 #
15 # Dumping the database can have negative effects on the system we are
16 # running on. If there is a lot of data dumping it might fill a partition
17 # for example. Therefore we must give the user exact control over what we
18 # are doing.
19
20 database_dumping_enabled() { # {{{
21 # Check if the user has enabled database dumping for the current situation.
22 # Return success if yes.
23 # Usage: if database_dumping_enabled; then ... fi
24
25 # If the package is being removed, dump unconditionally as we
26 # don't know whether the next version will require reload.
27 [ "$MODE" = remove ] && return 0
28
29 db_get slapd/dump_database
30 case "$RET" in
31 always)
32 ;;
33 "when needed")
34 database_format_changed || return 1
35 ;;
36 never)
37 return 1
38 ;;
39 *)
40 echo >&2 "Unknown value for slapd/dump_database: $RET"
41 echo >&2 "Please report!"
42 exit 1
43 ;;
44 esac
45 }
46
47 # }}}
48 database_format_changed() { # {{{
49 # Check if the database format has changed since the old installed version
50 # Return success if yes.
51 # Usage: if database_format_changed; then
52
53 if dpkg --compare-versions "$OLD_VERSION" lt-nl 2.5; then
54 return 0
55 else
56 return 1
57 fi
58 }
59
60 # }}}
61 database_dumping_destdir() { # {{{
62 # Figure out the directory we are dumping the database to and create it
63 # if it does not exist.
64 # Usage: destdir=`database_dumping_destdir`
65
66 local dir
67 db_get slapd/dump_database_destdir
68 dir=`echo "$RET"|sed -e "s/VERSION/$OLD_VERSION/"`
69 mkdir -p -m 700 "$dir"
70 echo $dir
71 }
72
73 # }}}
74 create_new_user() { # {{{
75 if [ -z "`getent group openldap`" ]; then
76 addgroup --quiet --system openldap
77 fi
78 if [ -z "`getent passwd openldap`" ]; then
79 echo -n " Creating new user openldap... " >&2
80 adduser --quiet --system --home /var/lib/ldap --shell /bin/false \
81 --ingroup openldap --disabled-password --disabled-login \
82 --gecos "OpenLDAP Server Account" openldap
83 echo "done." >&2
84 fi
85 }
86 # }}}
87 create_ldap_directories() { # {{{
88 if [ ! -d /var/lib/ldap ]; then
89 mkdir -m 0700 /var/lib/ldap
90 fi
91 update_permissions /var/lib/ldap
92 }
93 # }}}
94 update_permissions() { # {{{
95 local dir
96 dir="$1"
97 if [ -d "$dir" ]; then
98 [ -z "$SLAPD_USER" ] || chown -R -H "$SLAPD_USER" "$dir"
99 [ -z "$SLAPD_GROUP" ] || chgrp -R -H "$SLAPD_GROUP" "$dir"
100 fi
101 }
102 # }}}
103 update_databases_permissions() { # {{{
104 get_suffix | while read -r suffix; do
105 dbdir=`get_directory "$suffix"`
106 update_permissions "$dbdir"
107 done
108 }
109 # }}}
110 # }}}
111 # ----- Dumping and loading the data ------------------------------------ {{{
112
113 dump_config() { # {{{
114 # Dump the cn=config database to the backup directory.
115 # This is not the same as backup_config_once, which copies the slapd.d
116 # directory verbatim.
117 local dir
118
119 [ -d "$SLAPD_CONF" ] || return 0
120
121 dir="$(database_dumping_destdir)"
122 echo "Saving current slapd configuration to $dir..." >&2
123 slapcat -F "$SLAPD_CONF" -n0 -l "$dir/cn=config.ldif"
124 }
125 # }}}
126 dump_databases() { # {{{
127 # If the user wants us to dump the databases they are dumped to the
128 # configured directory.
129
130 local db suffix file dir failed slapcat_opts
131
132 database_dumping_enabled || return 0
133
134 dir=`database_dumping_destdir`
135 echo >&2 " Dumping to $dir: "
136 (get_suffix | while read -r suffix; do
137 dbdir=`get_directory "$suffix"`
138 if [ -n "$dbdir" ]; then
139 file="$dir/$suffix.ldif"
140 printf ' - directory %s... ' "$suffix" >&2
141 # Need to support slapd.d migration from preinst
142 if [ -f "${SLAPD_CONF}" ]; then
143 slapcat_opts="-g -f ${SLAPD_CONF}"
144 else
145 slapcat_opts="-g -F ${SLAPD_CONF}"
146 fi
147 slapcat ${slapcat_opts} -b "$suffix" > "$file" || failed=1
148 if [ "$failed" ]; then
149 rm -f "$file"
150 echo "failed." >&2
151 db_subst slapd/upgrade_slapcat_failure location "$dir" <&5
152 db_input critical slapd/upgrade_slapcat_failure <&5 || true
153 db_go <&5 || true
154 exit 1
155 fi
156 echo "done." >&2
157 fi
158 done) 5<&0 </dev/null
159 }
160
161 # }}}
162 load_databases() { # {{{
163 local dir file db dbdir backupdir slapadd_opts
164
165 dir=`database_dumping_destdir`
166 echo >&2 " Loading from $dir: "
167 # restore by increasing suffix length due to possibly glued databases
168 get_suffix | awk '{ print length, $0 }' | sort -n | cut -d ' ' -f 2- \
169 | while read -r suffix; do
170 dbdir=`get_directory "$suffix"`
171 if [ -z "$dbdir" ]; then
172 continue
173 fi
174 if ! is_empty_dir "$dbdir"; then
175 echo >&2 \
176 " Directory $dbdir for $suffix not empty, aborting."
177 exit 1
178 fi
179
180 file="$dir/$suffix.ldif"
181 printf ' - directory %s... ' "$suffix" >&2
182
183 # If there is an old DB_CONFIG file, restore it before
184 # running slapadd
185 backupdir="$(compute_backup_path -n "$suffix")"
186 if [ -e "$backupdir"/DB_CONFIG ]; then
187 cp -a "$backupdir"/DB_CONFIG "$dbdir"/
188 fi
189
190 if [ -f "${SLAPD_CONF}" ]; then
191 slapadd_opts="-g -f ${SLAPD_CONF}"
192 else
193 slapadd_opts="-g -F ${SLAPD_CONF}"
194 fi
195 capture_diagnostics slapadd ${slapadd_opts} \
196 -q -b "$suffix" -l "$file" || failed=1
197 if [ "$failed" ]; then
198 rm -f "$dbdir"/*
199 echo "failed." >&2
200 echo >&2
201 cat <<-EOF
202 Loading the database from the LDIF dump failed with the following
203 error while running slapadd:
204 EOF
205 release_diagnostics " "
206 return 1
207 fi
208 echo "done." >&2
209
210 if [ -n "$SLAPD_USER" ] || [ -n "$SLAPD_GROUP" ]; then
211 echo -n " - chowning database directory ($SLAPD_USER:$SLAPD_GROUP)... "
212 update_permissions "$dbdir"
213 echo "done";
214 fi
215 done
216 }
217
218 # }}}
219 move_incompatible_databases_away() { # {{{
220 echo >&2 " Moving old database directories to /var/backups:"
221 (get_suffix | while read -r suffix; do
222 dbdir=`get_directory "$suffix"`
223 move_old_database_away "$dbdir" "$suffix" <&5
224 done) 5<&0 </dev/null
225 }
226 # }}}
227 # }}}
228 # }}}
229
230 # ===== Parsing the slapd configuration file ============================ {{{
231 #
232 # For some operations we have to know the slapd configuration. These
233 # functions are for parsing the slapd configuration file.
234
235 # The following two functions need to support slapd.conf installations
236 # as long as upgrading from slapd.conf environment is supported.
237 # They're used to dump database in preinst which may have a slapd.conf file.
238 get_suffix() { # {{{
239 if [ -f "${SLAPD_CONF}" ]; then
240 for f in `get_all_slapd_conf_files`; do
241 sed -n -e '/^suffix[[:space:]]/ { s/^suffix[[:space:]]\+"*\([^"]\+\)"*/\1/; s/\\\\/\\/g; p }' $f
242 done
243 else
244 grep -h ^olcSuffix ${SLAPD_CONF}/cn\=config/olcDatabase*.ldif | cut -d: -f 2
245 fi | sort -u
246 }
247 # }}}
248 get_directory() { # {{{
249 # Returns the db directory for a given suffix
250 if [ -d "${SLAPD_CONF}" ] && get_suffix | grep -Fq "$1" ; then
251 sed -n 's/^olcDbDirectory: *//p' `grep -Flx "olcSuffix: $1" ${SLAPD_CONF}/cn\=config/olcDatabase*.ldif`
252 elif [ -f "${SLAPD_CONF}" ]; then
253 # Extract the directory for the given suffix ($1)
254 # Quote backslashes once for slapd.conf parser, again for awk
255 quoted="$(printf '%s' "$1" | sed 's/\\/\\\\\\\\/g')"
256 for f in `get_all_slapd_conf_files`; do
257 awk ' BEGIN { DB=0; SUF=""; DIR="" } ;
258 /^database/ { DB=1; SUF=""; DIR="" } ;
259 DB==1 && /^suffix[ \t]+"?'"$quoted"'"?$/ { SUF=$2 ; } ;
260 DB==1 && /^directory/ { DIR=$2 ;} ;
261 DB==1 && SUF!="" && DIR!="" { sub(/^"/,"",DIR) ; sub(/"$/,"",DIR) ; print DIR; SUF=""; DIR="" }' "${f}" | \
262 sed -e's/\([^\\]\|^\)"/\1/g; s/\\"/"/g; s/\\\\/\\/g'
263
264 done
265 else
266 return 1
267 fi
268 }
269 # }}}
270 get_all_slapd_conf_files() { # {{{
271 # Returns the list of all the config files: slapd.conf and included files.
272 echo ${SLAPD_CONF}
273 awk '
274 BEGIN { I=0 }
275 /^include/ {
276 sub(/include/," ");
277 I=1;
278 }
279 I==1 && /^[ \t]+/ {
280 split($0,F) ;
281 for (f in F)
282 if (!match(F[f],/schema/)) {
283 print F[f]
284 } ;
285 next;
286 }
287 I==1 { I=0 }
288 ' ${SLAPD_CONF}
289 }
290 # }}}
291 # }}}
292
293 compute_backup_path() { # {{{
294 # Compute the path to backup a database directory
295 # This is used when backing up actual database files, either just before
296 # reloading a dump, or before generating a new configuration.
297 # The directory used for dumping and reloading LDIF across upgrades, as
298 # well as for backing up the configuration before upgrading, is computed
299 # by database_dumping_destdir().
300 # Usage: compute_backup_path [-n] <basedn>
301
302 # XXX: should ask the user via debconf
303 # or maybe just use database_dumping_destdir
304
305 local basedn ok_exists
306 if [ "$1" = "-n" ]; then
307 ok_exists=yes
308 shift
309 fi
310 basedn="$1"
311
312 local id target
313 if [ "$MODE" = reconfigure ] || [ "$DEBCONF_RECONFIGURE" ]; then
314 # reconfigure: use OLD_VERSION plus a timestamp.
315 id="${OLD_VERSION:+$OLD_VERSION-}$(date +%Y%m%d-%H%M%S)"
316 else
317 # install/upgrade: use OLD_VERSION or a timestamp, not both.
318 id="${OLD_VERSION:-$(date +%Y%m%d-%H%M%S)}"
319 fi
320 target="/var/backups/$basedn-$id.ldapdb"
321 if [ -e "$target" ] && [ -z "$ok_exists" ]; then
322 echo >&2
323 echo >&2 " Backup path $target exists. Giving up..."
324 exit 1
325 fi
326
327 printf '%s' "$target"
328 }
329
330 # }}}
331 move_old_database_away() { # {{{
332 # Move the old database away if it is still there
333 #
334 # In fact this function makes sure that the database directory is empty
335 # with the exception of any DB_CONFIG file
336 # and can be populated with a new database. If something is in the way
337 # it is moved to a backup directory if the user accepted the debconf
338 # option slapd/move_old_database. Otherwise we output a warning and let
339 # the user fix it himself.
340 # Usage: move_old_database_away <dbdir> [<basedn>]
341
342 local databasedir backupdir
343 databasedir="$1"
344 suffix="${2:-unknown}"
345
346 if [ ! -e "$databasedir" ] || is_empty_dir "$databasedir"; then
347 return 0
348 fi
349
350 # Note that we can't just move the database dir as it might be
351 # a mount point. Instead me move the content which might
352 # include mount points as well anyway, but it's much less likely.
353 db_get slapd/move_old_database
354 if [ "$RET" = true ]; then
355 backupdir="$(compute_backup_path "$suffix")"
356 printf ' - directory %s... ' "$suffix" >&2
357 mkdir -p "$backupdir"
358 find -H "$databasedir" -mindepth 1 -maxdepth 1 -type f \
359 -exec mv {} "$backupdir" \;
360 echo done. >&2
361 else
362 cat >&2 <<EOF
363 There are leftover files in $databasedir. This will probably break
364 creating the initial directory. If that's the case please move away
365 stuff in there and retry the configuration.
366 EOF
367 fi
368 }
369 # }}}
370 manual_configuration_wanted() { # {{{
371 # Check if the user wants to configure everything himself (queries debconf)
372 # Returns success if yes.
373
374 db_get slapd/no_configuration
375 if [ "$RET" = "true" ]; then
376 return 0
377 else
378 return 1
379 fi
380 }
381 # }}}
382 create_new_configuration() { # {{{
383 # Create a new configuration and directory
384
385 local basedn dc
386
387 # For the domain really.argh.org we create the basedn
388 # dc=really,dc=argh,dc=org with the dc entry dc: really
389 db_get slapd/domain
390 basedn="dc=`echo $RET | sed 's/^\.//; s/\.$//; s/\./,dc=/g'`"
391 dc="`echo $RET | sed 's/^\.//; s/\..*$//'`"
392
393 backup_config_once
394 if [ -e "/var/lib/ldap" ] && ! is_empty_dir /var/lib/ldap; then
395 echo >&2 " Moving old database directory to /var/backups:"
396 move_old_database_away /var/lib/ldap
397 fi
398 create_ldap_directories
399 create_new_slapd_conf "$basedn"
400 create_new_directory "$basedn" "$dc"
401
402 # Put the right permissions on this directory.
403 update_permissions /var/lib/ldap
404
405 # Now that we created the new directory we don't need the passwords in the
406 # debconf database anymore. So wipe them.
407 wipe_admin_pass
408 }
409 # }}}
410 create_new_slapd_conf() { # {{{
411 # Create the new slapd.d directory (configuration)
412 # Usage: create_new_slapd_conf <basedn>
413
414 local initldif failed basedn adminpass
415
416 # Fetch configuration
417 basedn="$1"
418 db_get slapd/internal/adminpw
419 adminpass="$RET"
420
421 echo -n " Creating initial configuration... " >&2
422
423 # Create the slapd.d directory.
424 rm -rf ${SLAPD_CONF}/cn=config ${SLAPD_CONF}/cn=config.ldif
425 mkdir -p ${SLAPD_CONF}
426 initldif=`mktemp -t slapadd.XXXXXX`
427 cat /usr/share/slapd/slapd.init.ldif > ${initldif}
428
429 # Change some defaults
430 sed -i -e "s|@SUFFIX@|$basedn|g" ${initldif}
431 sed -i -e "s|@PASSWORD@|$adminpass|g" ${initldif}
432
433 capture_diagnostics slapadd -F "${SLAPD_CONF}" -b "cn=config" \
434 -l "${initldif}" || failed=1
435 if [ "$failed" ]; then
436 cat <<-EOF
437 Loading the initial configuration from the ldif file (${init_ldif}) failed with
438 the following error while running slapadd:
439 EOF
440 release_diagnostics " "
441 exit 1
442 fi
443
444 update_permissions "${SLAPD_CONF}"
445 rm -f "${initldif}"
446 echo "done." >&2
447 }
448 # }}}
449 create_new_directory() { # {{{
450 # Create a new directory. Takes the basedn and the dc value of that entry.
451 # Other information is extracted from debconf.
452 # Usage: create_new_directory <basedn> <dc>
453
454 local basedn dc organization adminpass
455 basedn="$1"
456 dc="$2"
457
458 db_get shared/organization
459 organization="$RET"
460 db_get slapd/internal/adminpw
461 adminpass="$RET"
462
463 echo -n " Creating LDAP directory... " >&2
464
465 initldif=`mktemp -t slapadd.XXXXXX`
466 cat <<-EOF > "${initldif}"
467 dn: $basedn
468 objectClass: top
469 objectClass: dcObject
470 objectClass: organization
471 o: $organization
472 dc: $dc
473
474 EOF
475
476 capture_diagnostics slapadd -F "${SLAPD_CONF}" -b "${basedn}" \
477 -l "${initldif}" || failed=1
478 if [ "$failed" ]; then
479 rm -f ${initldif}
480 echo "failed." >&2
481 cat <<-EOF
482 Loading the initial configuration from the ldif file (${init_ldif}) failed with
483 the following error while running slapadd:
484 EOF
485 release_diagnostics " "
486 exit 1
487 fi
488
489 rm -f ${initldif}
490 echo "done." >&2
491 }
492 # }}}
493 backup_config_once() { # {{{
494 # Create a backup of the current configuration files.
495 # Usage: backup_config_once
496
497 local backupdir
498
499 if [ -z "$FLAG_CONFIG_BACKED_UP" ]; then
500 if [ -e "$SLAPD_CONF" ]; then
501 backupdir=`database_dumping_destdir`
502 echo -n " Backing up $SLAPD_CONF in ${backupdir}... " >&2
503 cp -a "$SLAPD_CONF" "$backupdir"
504 echo done. >&2
505 fi
506 FLAG_CONFIG_BACKED_UP=yes
507 fi
508 }
509
510 # }}}
511
512
513 set_defaults_for_unseen_entries() { # {{{
514 # Set up the defaults for our templates
515 DOMAIN=`hostname -d 2>/dev/null` || true
516 if [ -z "$DOMAIN" ]; then DOMAIN='nodomain'; fi
517
518 db_fget slapd/domain seen
519 if [ "$RET" = false ]; then
520 db_set slapd/domain "$DOMAIN"
521 fi
522
523 db_fget shared/organization seen
524 if [ "$RET" = false ]; then
525 db_set shared/organization "$DOMAIN"
526 fi
527 }
528 # }}}
529 crypt_admin_pass() { # {{{
530 # Store the encrypted admin password into the debconf db
531 # Usage: crypt_admin_pass
532
533 local adminpw;
534
535 db_get slapd/password1
536 if [ ! -z "$RET" ]; then
537 db_set slapd/internal/adminpw "$(create_password_hash "$RET")"
538 else
539
540 # Set the password.
541 adminpw="$(generate_admin_pass)"
542 db_set slapd/internal/generated_adminpw "$adminpw"
543 db_set slapd/internal/adminpw "$(create_password_hash "$adminpw")"
544 fi
545 }
546
547 generate_admin_pass() {
548 # Generate a password, if no password given then generate one.
549 # Usage: generate_admin_pass
550
551 # 15 bytes of /dev/urandom provide 120 random bits, assuming the entropy pool is full enough.
552 # Coding these 15 bytes in base64 returns a 20 characters long password.
553 head -c 15 /dev/urandom | base64 | tr -d '[:space:]'
554 }
555
556 wipe_admin_pass() {
557 # Remove passwords after creating the initial ldap database.
558 # Usage: wipe_admin_pass
559 db_set slapd/password1 ""
560 db_set slapd/password2 ""
561 db_set slapd/internal/adminpw ""
562 db_set slapd/internal/generated_adminpw ""
563 }
564
565 # }}}
566 create_password_hash() { # {{{
567 # Create the password hash for the given password
568 # Usage: hash=`create_password_hash "$password"`
569
570 slappasswd -s "$1"
571 }
572
573 # }}}
574 previous_version_older() { # {{{
575 # Check if the previous version is newer than the reference version passed.
576 # If we are not upgrading the previous version is assumed to be newer than
577 # any reference version.
578 # Usage: previous_version_older <package version>
579
580 if dpkg --compare-versions "$OLD_VERSION" lt-nl "$1"; then
581 return 0
582 else
583 return 1
584 fi
585 }
586
587 # }}}
588 previous_version_newer() { # {{{
589 # Check if the previous version is newer than the reference version passed.
590 # If we are not upgrading the previous version is assumed to be newer than
591 # any reference version.
592 # Usage: previous_version_newer <package version>
593
594 if dpkg --compare-versions "$OLD_VERSION" gt-nl "$1"; then
595 return 0
596 else
597 return 1
598 fi
599 } # }}}
600
601 is_initial_configuration() { # {{{
602 # Check if this is the initial configuration and not an upgrade of an
603 # existing configuration
604 # Usage: if is_initial_configuration "$@"; then ... fi from top level
605
606 # Plain installation
607 if [ "$1" = configure ] && [ -z "$2" ]; then
608 return 0
609 fi
610 # Configuration via dpkg-reconfigure
611 if [ "$1" = reconfigure ] || [ "$DEBCONF_RECONFIGURE" ]; then
612 return 0
613 fi
614 # Upgrade but slapd.conf doesn't exist. If the user is doing this
615 # intentionally because they want to put it somewhere else, they
616 # should select manual configuration in debconf.
617 if [ "$1" = configure ] && [ ! -e "${SLAPD_CONF}" ]; then
618 return 0
619 fi
620 return 1
621 }
622
623 # }}}
624 is_empty_dir() { # {{{
625 # Check if a path refers to a directory that is "empty" from the POV of slapd
626 # (i.e., contains no files except for an optional DB_CONFIG).
627 # Usage: if is_empty_dir "$dir"; then ... fi
628
629 output=`find -H "$1" -mindepth 1 -maxdepth 1 -type f \! -name DB_CONFIG 2>/dev/null`
630 if [ -n "$output" ]; then
631 return 1
632 else
633 return 0
634 fi
635 }
636
637 # }}}
638
639 # ===== Global variables ================================================ {{{
640 #
641 # At some points we need to know which version we are upgrading from if
642 # any. More precisely we only care about the configuration and data we
643 # might have laying around. Some parts also want to know which mode the
644 # script is running in.
645
646 MODE="$1" # install, upgrade, etc. - see debian-policy
647 OLD_VERSION="$2"
648
649 # Source the init script configuration
650 # See example file debian/slapd.default for variables defined here
651 if [ -f "/etc/default/slapd" ]; then
652 . /etc/default/slapd
653 fi
654
655 # Load the default location of the slapd config file
656 if [ -z "$SLAPD_CONF" ]; then
657 if [ -f "/etc/ldap/slapd.conf" ] && \
658 [ ! -e "/etc/ldap/slapd.d" ]
659 then
660 SLAPD_CONF="/etc/ldap/slapd.conf"
661 else
662 SLAPD_CONF="/etc/ldap/slapd.d"
663 fi
664 fi
665
666 # }}}
667
668 # ----- Handling diagnostic output ------------------------------------ {{{
669 #
670 # Often you want to run a program while you are showing progress
671 # information to the user. If the program you are running outputs some
672 # diagnostics it will mess up your screen.
673 #
674 # This is what the following functions are designed for. When running the
675 # program, use capture_diagnostics to store what the program outputs to
676 # stderr and use release_diagnostics to write out the captured output.
677
678
679 capture_diagnostics() { # {{{
680 # Run the command passed and capture the diagnostic output in a temporary
681 # file. You can dump that file using release_diagnostics.
682
683 # Create the temporary file
684 local tmpfile
685 tmpfile=`mktemp`
686 exec 7<>"$tmpfile"
687 rm "$tmpfile"
688
689 # Run the program and capture stderr. If the program fails the
690 # function fails with the same status.
691 "$@" 2>&7 || return $?
692 }
693
694 # }}}
695 release_diagnostics() { # {{{
696 # Dump the diagnostic output captured via capture_diagnostics, optionally
697 # prefixing each line.
698 # Usage: release_diagnostics "prefix"
699
700 { exec < /dev/stdin ; sed -e "s/^/$1/" ; } <&7
701 }
702
703 # }}}
704
705
706 # }}}
707
708 # vim: set sw=8 foldmethod=marker:
709