Debian Codegraph @

debian/pynslcd.postinst

raw
1 #!/bin/sh
2
3 set -e
4
5 CONFFILE="/etc/nslcd.conf"
6
7 # set an option in the configuration file to the specified value
8 cfg_set()
9 {
10 parameter="$1"
11 value="$2"
12 # check if the parameter is defined
13 line=`sed -n '/^'"$parameter"'[[:space:]]*\(aliases\|ethers\|group\|hosts\|netgroup\|networks\|passwd\|protocols\|rpc\|services\|shadow\)[[:space:]]/d;/^'"$parameter"'[[:space:]]/p' "$CONFFILE" | head -n 1`
14 if [ -z "$line" ]
15 then
16 # check if the parameter is commented out
17 line=`sed -n '/^#'"$parameter"'[[:space:]]*[aeghnprs][acedgihklopsrutwv]*[[:space:]]/d;/^#'"$parameter"'[[:space:]]/p' "$CONFFILE" | head -n 1`
18 fi
19 # decide what to do
20 if [ -z "$line" ]
21 then
22 # just append a new line
23 echo "$parameter $value" >> $CONFFILE
24 else
25 # escape line to replace
26 replace=`echo "$line" | sed 's#\\\#\\\\\\\#g;s#\([.*+?^$|[]\)#\\\\\1#g'`
27 # escape value (parameter doesn't have any special stuff)
28 value=`echo "$value" | sed 's#\\\#\\\\\\\#g;s#|#\\\|#g;s#&#\\\&#g'`
29 # replace the first occurrence of the line
30 sed -i '1,\|^'"$replace"'$| s|^'"$replace"'$|'"$parameter"' '"$value"'|i' "$CONFFILE"
31 fi
32 # we're done
33 return 0
34 }
35
36 # disable options in the configuration file by commenting them out
37 cfg_disable()
38 {
39 for parameter in $@
40 do
41 # handle bindpw option specially by removing value from config first
42 if [ "$parameter" = "bindpw" ] && grep -i -q "^bindpw " $CONFFILE
43 then
44 cfg_set bindpw "*removed*"
45 fi
46 # make matching of spaces better in parameter
47 param_re=`echo "$parameter" | sed 's#^#[[:space:]]*#;s#[[:space:]][[:space:]]*#[[:space:]][[:space:]]*#g'`
48 # lines to not match
49 nomatch_re="^$param_re[[:space:]][[:space:]]*\(aliases\|ethers\|group\|hosts\|netgroup\|networks\|passwd\|protocols\|rpc\|services\|shadow\)"
50 # comment out the option
51 sed -i '/'"$nomatch_re"'/!s/^'"$param_re"'[[:space:]].*$/#&/i' "$CONFFILE"
52 # we're done
53 done
54 return 0
55 }
56
57 # set the list of uris
58 cfg_uris()
59 {
60 uris="$1"
61 # escape all uri directives
62 sed -i 's/^uri /_uri_ /i' $CONFFILE
63 # set the uri options
64 echo "$uris" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/[[:space:]][[:space:]]*/\n/g' | while read uri
65 do
66 if grep -qi '^_uri_ ' $CONFFILE
67 then
68 # escape uri for use in regexp replacement
69 uri=`echo "$uri" | sed 's#\\\#\\\\\\\#g;s#|#\\\|#g;s#&#\\\&#g'`
70 # replace the first occurrence of _uri_
71 sed -i '1,/^_uri_ / s|^_uri_ .*$|uri '"$uri"'|i' "$CONFFILE"
72 else
73 # append new uri
74 echo "uri $uri" >> $CONFFILE
75 fi
76 done
77 # comment out the remaining escaped uris
78 sed -i 's/^_uri_ /#uri /' $CONFFILE
79 }
80
81 # create a default configuration file if nothing exists yet
82 create_config()
83 {
84 if [ ! -e "$CONFFILE" ]
85 then
86 # create a simple configuration file from this template
87 cat > "$CONFFILE" << EOM
88 # $CONFFILE
89 # nslcd configuration file. See nslcd.conf(5)
90 # for details.
91
92 # The user and group nslcd should run as.
93 uid nslcd
94 gid nslcd
95
96 # The location at which the LDAP server(s) should be reachable.
97 uri ldap://localhost/
98
99 # The search base that will be used for all queries.
100 base dc=example,dc=net
101
102 # The LDAP protocol version to use.
103 #ldap_version 3
104
105 # The DN to bind with for normal lookups.
106 #binddn cn=annonymous,dc=example,dc=net
107 #bindpw secret
108
109 # The DN used for password modifications by root.
110 #rootpwmoddn cn=admin,dc=example,dc=com
111
112 # SSL options
113 #ssl off
114 #tls_reqcert never
115 tls_cacertfile /etc/ssl/certs/ca-certificates.crt
116
117 # The search scope.
118 #scope sub
119
120 EOM
121 # fix permissions
122 chmod 640 "$CONFFILE"
123 chown root:nslcd "$CONFFILE"
124 fi
125 # we're done
126 return 0
127 }
128
129 # update a configuration parameter, based on the debconf key
130 update_config()
131 {
132 debconf_param="$1"
133 cfg_param="$2"
134 # update configuration option based on debconf value
135 db_get "$debconf_param"
136 if [ -n "$RET" ]
137 then
138 cfg_set "$cfg_param" "$RET"
139 else
140 cfg_disable "$cfg_param"
141 fi
142 }
143
144 # real functions begin here
145 if [ "$1" = "configure" ]
146 then
147 # get configuration data from debconf
148 . /usr/share/debconf/confmodule
149 # Force C locale (after loading debconf module) for consistent text processing
150 export LC_ALL=C
151 # check if the nslcd user exists
152 if getent passwd nslcd >/dev/null
153 then
154 :
155 else
156 # create nslcd user and group
157 adduser --system --group --home /run/nslcd --no-create-home \
158 --gecos "nslcd name service LDAP connection daemon" \
159 nslcd
160 # add uid/gid options to the config file if it exists
161 # (this is when we're upgrading)
162 if [ -f "$CONFFILE" ]
163 then
164 echo "Adding uid and gid options to $CONFFILE..." >&2
165 echo "# automatically added on upgrade of nslcd package" >> "$CONFFILE"
166 cfg_set uid nslcd
167 cfg_set gid nslcd
168 fi
169 fi
170 # create a default configuration
171 create_config
172 # rename tls_checkpeer to tls_reqcert
173 if grep -qi '^tls_checkpeer[[:space:]]' $CONFFILE
174 then
175 echo "Renaming tls_checkpeer to tls_reqcert in $CONFFILE..." >&2
176 sed -i 's/^tls_checkpeer[[:space:]]/tls_reqcert /' "$CONFFILE"
177 fi
178 # rename reconnect_maxsleeptime to reconnect_retrytime
179 if grep -qi '^reconnect_maxsleeptime[[:space:]]' $CONFFILE
180 then
181 echo "Renaming reconnect_maxsleeptime to reconnect_retrytime in $CONFFILE..." >&2
182 sed -i 's/^reconnect_maxsleeptime[[:space:]]/reconnect_retrytime /' "$CONFFILE"
183 fi
184 # rename tls_cacert to tls_cacertfile
185 if grep -qi '^tls_cacert[[:space:]]' $CONFFILE
186 then
187 echo "Renaming tls_cacert to tls_cacertfile in $CONFFILE..." >&2
188 sed -i 's/^tls_cacert[[:space:]]/tls_cacertfile /' "$CONFFILE"
189 fi
190 # comment out mapping of uniqueMember to member which is now used by default
191 if grep -qi '^map[[:space:]]*group[[:space:]]*uniqueMember[[:space:]]*member[[:space:]]*$' $CONFFILE
192 then
193 echo "Commenting out uniqueMember mapping due to rename in $CONFFILE..." >&2
194 sed -i '/^map[[:space:]]*group[[:space:]]*uniqueMember[[:space:]]*member[[:space:]]*$/i \
195 #off# automatically commented out next line because this is now the default
196 s/^map[[:space:]]*group[[:space:]]*uniqueMember[[:space:]]*member[[:space:]]*$/#off# &/' "$CONFFILE"
197 fi
198 # set server uri
199 db_get nslcd/ldap-uris
200 cfg_uris "$RET"
201 # update some options
202 update_config nslcd/ldap-base base
203 db_get nslcd/ldap-auth-type
204 authtype="$RET"
205 case "$authtype" in
206 simple)
207 update_config nslcd/ldap-binddn binddn
208 update_config nslcd/ldap-bindpw bindpw
209 cfg_disable sasl_mech sasl_realm sasl_authcid sasl_authzid sasl_secprops krb5_ccname
210 ;;
211 SASL)
212 update_config nslcd/ldap-sasl-mech sasl_mech
213 update_config nslcd/ldap-sasl-realm sasl_realm
214 # RFC4313 if SASL, binddn should be disabled
215 cfg_disable binddn
216 db_get nslcd/ldap-sasl-mech
217 saslmech="$RET"
218 case "$saslmech" in
219 GSSAPI)
220 update_config nslcd/ldap-sasl-krb5-ccname krb5_ccname
221 cfg_disable sasl_authcid
222 ;;
223 *)
224 update_config nslcd/ldap-sasl-authcid sasl_authcid
225 update_config nslcd/ldap-bindpw bindpw
226 cfg_disable krb5_ccname
227 ;;
228 esac
229 update_config nslcd/ldap-sasl-authzid sasl_authzid
230 update_config nslcd/ldap-sasl-secprops sasl_secprops
231 ;;
232 none)
233 cfg_disable binddn bindpw
234 cfg_disable sasl_mech sasl_realm sasl_authcid sasl_authzid sasl_secprops krb5_ccname
235 esac
236 update_config nslcd/ldap-reqcert tls_reqcert
237 update_config nslcd/ldap-cacertfile tls_cacertfile
238 # remove password from database
239 db_set nslcd/ldap-bindpw ""
240 # set ssl option
241 db_get nslcd/ldap-starttls
242 if [ "$RET" = "true" ]
243 then
244 cfg_set ssl "start_tls"
245 elif grep -qi '^ssl[[:space:]]*start_*tls' $CONFFILE
246 then
247 cfg_disable ssl
248 fi
249
250 # we're done
251 db_stop
252 fi
253
254 #DEBHELPER#
255
256 exit 0