Debian Codegraph @

debian/libnss-ldapd.postinst

raw
1 #!/bin/sh
2
3 set -e
4
5 # editing nsswitch.conf seems to be ok
6 # http://lists.debian.org/debian-devel/2007/02/msg00076.html
7
8 # check whether the name is configure to do lookups through
9 # LDAP
10 # Note: this function is in libnss-ldapd.postinst and libnss-ldapd.postrm
11 nss_is_enabled()
12 {
13 name="$1"
14 grep -q '^[[:space:]]*'$name'[[:space:]]*:\(.*[[:space:]]\)\?ldap.*' /etc/nsswitch.conf
15 }
16
17 # check to see if name is configured to do lookups through
18 # LDAP and enable if not
19 nss_enable()
20 {
21 name="$1"
22 if ! nss_is_enabled "$name"
23 then
24 echo "/etc/nsswitch.conf: enable LDAP lookups for $name" >&2
25 if grep -q '^[[:space:]]*'$name'[[:space:]]*:' /etc/nsswitch.conf
26 then
27 # modify an existing entry by just adding ldap to the end
28 sed -i 's/^\([[:space:]]*'$name'[[:space:]]*:.*[^[:space:]]\)[[:space:]]*$/\1 ldap/' /etc/nsswitch.conf
29 else
30 # append a new line
31 printf '%-15s ldap\n' $name':' >> /etc/nsswitch.conf
32 fi
33 # invalidate nscd cache
34 nscd -i "$name" > /dev/null 2>&1 || true
35 fi
36 # we're done
37 return 0
38 }
39
40 # remove NSS lookups though LDAP for the specified service
41 # Note: this function is in libnss-ldapd.postinst and libnss-ldapd.postrm
42 nss_disable()
43 {
44 name="$1"
45 # these functions also remove the lookup result handling part
46 # of the ldap entry (see nsswitch.conf(5))
47 if nss_is_enabled "$name"
48 then
49 echo "/etc/nsswitch.conf: disable LDAP lookups for $name" >&2
50 if [ -n "`sed -n '/^[[:space:]]*'$name'[[:space:]]*:[[:space:]]*ldap[[:space:]]*\(\[[^]]*\]\)*[[:space:]]*$/p' /etc/nsswitch.conf`" ]
51 then
52 # the name service only maps to ldap, remove the whole line
53 sed -i '/^[[:space:]]*'$name'[[:space:]]*:[[:space:]]*ldap[[:space:]]*\(\[[^]]*\]\)*[[:space:]]*$/d' /etc/nsswitch.conf
54 else
55 # remove ldap part from existing line, keeping other methods intact
56 # TODO: remove trailing space
57 sed -i 's/^\([[:space:]]*'$name'[[:space:]]*:\(.*[[:space:]]\)\?\)ldap[[:space:]]*\(\[[^]]*\]\)*[[:space:]]*\(.*\)$/\1\4/' /etc/nsswitch.conf
58 fi
59 # invalidate nscd cache
60 nscd -i "$name" > /dev/null 2>&1 || true
61 fi
62 # we're done
63 return 0
64 }
65
66 # real functions begin here
67 if [ "$1" = "configure" ]
68 then
69 # get configuration data from debconf
70 . /usr/share/debconf/confmodule
71 # modify /etc/nsswitch.conf
72 db_get libnss-ldapd/nsswitch
73 enablenss=`echo "$RET" | sed 's/,//g'`
74 # Force C locale (after loading debconf module) for consistent text processing
75 export LC_ALL=C
76 for n in aliases ethers group hosts netgroup networks passwd protocols rpc services shadow
77 do
78 if echo ' '$enablenss' ' | grep -q ' '$n' '
79 then
80 nss_enable $n
81 else
82 nss_disable $n
83 fi
84 done
85 # we're done
86 db_stop
87 fi
88
89 #DEBHELPER#
90
91 exit 0