OSDN Git Service

refactoring.
[metasearch/grid-chef-repo.git] / cookbooks / openldap-grid / recipes / nss-ldapd.rb
1 #
2 # Cookbook Name:: openldap-grid
3 # Recipe:: nss-ldapd
4 #
5 # Copyright 2013-2016, whitestar
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 nslcd_conf_keys = [
21   'threads',
22   #'uid',
23   #'gid',
24   'uri',
25   'ldap_version',
26   'binddn',
27   'bindpw',
28   'rootpwmoddn',
29   'sasl_mech',
30   'sasl_realm',
31   'sasl_authcid',
32   'sasl_authzid',
33   'sasl_secprops',
34   'base',
35   'scope',
36   'deref',
37   'referrals',
38   'filter',
39   'map',
40   'bind_timelimit',
41   'timelimit',
42   'idle_timelimit',
43   'reconnect_sleeptime',
44   'reconnect_retrytime',
45   'ssl',
46   'tls_reqcert',
47   'tls_cacertdir',
48   'tls_cacertfile',
49   'tls_randfile',
50   'tls_ciphers',
51   'tls_cert',
52   'tls_key',
53   'pagesize',
54   'nss_initgroups_ignoreusers',
55   'pam_authz_search',
56 ]
57
58 tls_cacertfile = node['openldap']['nss-ldapd']['tls_cacertfile']
59 if node['openldap']['with_ssl_cert_cookbook'] \
60   && (tls_cacertfile.nil? || tls_cacertfile.empty?)
61   ::Chef::Recipe.send(:include, SSLCert::Helper)
62   node.force_override['openldap']['nss-ldapd']['tls_cacertfile'] \
63     = ca_cert_path(node['openldap']['ssl_cert']['ca_name'])
64 end
65
66 case node['platform_family']
67 when 'debian'
68   %w(
69     libnss-ldapd
70     nscd
71   ).each {|pkg|
72     package pkg do
73       action :install
74     end
75   }
76
77   service 'nslcd' do
78     action [:enable]
79     supports status: true, restart: true, reload: false
80   end
81
82   resources(service: 'nscd') rescue service 'nscd' do
83     action [:enable]
84     supports status: true, restart: true, reload: false
85   end
86
87   template '/etc/nslcd.conf' do
88     source 'etc/nslcd.conf'
89     owner 'root'
90     group 'nslcd'
91     mode '0640'
92     variables nslcd_conf_keys: nslcd_conf_keys
93     notifies :restart, 'service[nslcd]'
94     notifies :restart, 'service[nscd]'
95   end
96 when 'rhel'
97   package 'nss-pam-ldapd' do
98     action :install
99   end
100
101   service 'nslcd' do
102     action [:enable]
103     supports status: true, restart: true, reload: true
104   end
105
106   resources(service: 'nscd') rescue service 'nscd' do
107     action [:enable]
108     supports status: true, restart: true, reload: true
109   end
110
111   template '/etc/nslcd.conf' do
112     source 'etc/nslcd.conf'
113     owner 'root'
114     group 'root'
115     mode '0600'
116     variables nslcd_conf_keys: nslcd_conf_keys
117     notifies :restart, 'service[nslcd]'
118     notifies :restart, 'service[nscd]'
119   end
120 end
121
122 ruby_block 'configuring_nameservices' do
123   block do
124     conf_file = '/etc/nsswitch.conf'
125     nameservices = node['openldap']['ldap_lookup_nameservices']
126     if !nameservices.nil? && !nameservices.empty?
127       open(conf_file, 'r+') {|file|
128         file.flock(File::LOCK_EX)
129         is_modified = false
130         buf = ''
131         file.each {|line|
132           if line =~ /^(\w+):\s+(.*)$/ \
133             && nameservices.include?($1) && !$2.include?('ldap')
134             line.chomp! << " ldap\n"
135             is_modified = true
136           end
137           buf << line
138         }
139         if is_modified
140           print "\nnew #{conf_file}: [#{buf}]"
141           file.rewind
142           file.puts buf
143           file.truncate(file.tell)
144         end
145       }
146     end
147   end
148   action :run
149   notifies :restart, 'service[nscd]'
150 end