aboutsummaryrefslogtreecommitdiff
path: root/lib/beam/ldap.sh
blob: 40b9a4b8632e28b0e539be9a283a92f12f14f858 (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
#! /bin/sh
# This file is part of BEAM
# Copyright (C) 2012-2014 Sergey Poznyakoff
#
# BEAM is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# BEAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BEAM.  If not, see <http://www.gnu.org/licenses/>.

# Configuration keywords:
#
# item_type=ldap                 [mandatory]
# item_database_directory=string [mandatory] 
# item_database_number=n         [optional *]
# item_database_suffix=suf       [optional *]
# item_uri=str                   [optional]
# item_user=name                 [optional]
# item_slapcat_options=string    [optional internal]
# item_slapadd_options=string    [optional internal]
# 
# * - mutually exclusive keywords.


# ldap_check item
ldap_check() {
    local rc=0 dbno dbsuf dbdir
    eval dbno=\$${1}_database_number \
         dbsuf=\$${1}_database_suffix \
         dbdir=\$${1}_database_directory
    if test -n "$dbno" && test -n "$dbsuf"; then 
        error "$1: both ${1}_database_number and ${1}_database_suffix are set"
        rc=1 
    fi
    if test -z "$dbdir"; then
	error "${1}_database_directory not configured"
	rc=1
    fi
    if ! test -d "$dbdir"; then
	error "LDAP database directory $dbdir does not exist"
	error "(set by ${1}_database_directory)"
    fi
    return $rc
}

# ldap_list item prefix
ldap_list() {
    local dbid
    
    eval dbid=\$${1}_database_number
    if [ -n "$dbid" ]; then
	dbid=" number $dbid"
    else
        eval dbid=\$${1}_database_suffix
        if [ -n "$dbid" ]; then
	    dbid=" suffixed with $dbid"
	fi
    fi

    echo "${2}LDAP database"$dbid
}

# ldap_backup item
ldap_backup() {
    local options dbno dbsuf uri

    logit "backing up LDAP database $1"
    
    eval options=\$${1}_slapcat_options \
         dbno=\$${1}_database_number \
         dbsuf=\$${1}_database_suffix

    if [ -n "$dbno" ]; then
	options="$options -n$dbnum"
    elif [ -n "$dbsuf" ]; then
	options="$options -b$slapcat"
    fi

    eval uri=\"\$${1}_uri\"
    if [ -n "$uri" ]; then
	options="$options -H\"$uri\""
    fi

    if [ -z "$dry_run" ]; then
	slapcat $options > $backup_snapshot_dir/$1-$week-$round-$level
    else
	echo "slapcat $options > $backup_snapshot_dir/$1-$week-$round-$level"
    fi

    if [ $? -ne 0 ]; then
	tarerror=$((tarerror + 1))
        logit "failed"
    else
        logit "creating $1-$week-$round-$level.$tar_suffix"
        $dry_run tar $verbose $taroptions \
                -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix \
                -C $backup_snapshot_dir $1-$week-$round-$level
        tarcode $?
        $dry_run rm $backup_snapshot_dir/$1-$week-$round-$level
    fi
}

# ldap_restore item
ldap_restore() {
    local options dbno dbsuf dbdir u user
    
    logit "extracting LDAP database dump $1"
    
    eval options=\$${1}_slapadd_options dbno=\$${1}_database_number \
         dbsuf=\$${1}_database_suffix dbdir=\$${1}_database_directory \
         user=\$${1}_user

    if test -z "$dbdir"; then
	error "${1}_database_directory not configured"
	tarerror=$((tarerror + 1))
	return
    fi
    if [ -n "$dbno" ]; then
	options="$options -n$dbnum"
    elif [ -n "$dbsuf" ]; then
	options="$options -b$slapcat"
    fi
  
    u=$(umask)
    trap "umask $u" 1 2 3 13 15
    umask 077
    $dry_run tar $verbose $taroptions \
           -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix
    e=$?
    tarcode $e
    if [ $e -eq 0 ]; then
	prevdbtar=$backup_snapshot_dir/ldap-$1-$(date +%Y%m%dT%H%M%S).tar
	logit "archiving the previous database contents to $prevdbtar"
	$dry_run tar $verbose -c -f $prevdbtar $dbdir
	e=$?
	tarcode $e
	umask $u
	if [ $e -ne 0 ]; then
	    error "Failed to backup the prior database contents to $prevdbtar"
	    return
        fi
	logit "removing old database files in $dbdir"
	$dry_run rm $dbdir/*
        logit "restoring database $1 from the dump"
        if test -n "$user"; then
            su $user -c "$dry_run slapadd $options $verbose -l $1-$week-$round-$level"
	    su $user -c "$dry_run slapindex"
        else
            $dry_run slapadd $options $verbose -l $1-$week-$round-$level
	    $dry_run slapindex
        fi 
        # FIXME: error checking
        $dry_run rm $1-$week-$round-$level  
    fi
    umask $u
    trap - 1 2 3 13 15
}

Return to:

Send suggestions and report system problems to the System administrator.