OSDN Git Service

improvement of grid nameservice cluster setup.
[metasearch/grid-chef-repo.git] / cookbooks / openldap / recipes / nss-ldapd.rb
1 #
2 # Cookbook Name:: openldap
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 if node['openldap']['with_ssl_cert_cookbook'] then
59   node.override['openldap']['nss-ldapd']['tls_cacertfile'] \
60     = node['ssl_cert']["#{node['openldap']['ssl_cert']['ca_name']}_cert_path"]
61 end
62
63 case node[:platform_family]
64   when 'debian'
65     %w{
66       libnss-ldapd
67       nscd
68     }.each {|pkg|
69       package pkg do
70         action :install
71       end
72     }
73
74     service 'nslcd' do
75       action [:enable]
76       supports :status => true, :restart => true, :reload => false
77     end
78
79     resources(:service => 'nscd') rescue service 'nscd' do
80       action [:enable]
81       supports :status => true, :restart => true, :reload => false
82     end
83
84     template '/etc/nslcd.conf' do
85       source 'etc/nslcd.conf'
86       owner 'root'
87       group 'nslcd'
88       mode '0640'
89       variables({'nslcd_conf_keys' => nslcd_conf_keys})
90       notifies :restart, 'service[nslcd]'
91       notifies :restart, 'service[nscd]'
92     end
93   when 'rhel'
94     package 'nss-pam-ldapd' do
95       action :install
96     end
97
98     service 'nslcd' do
99       action [:enable]
100       supports :status => true, :restart => true, :reload => true
101     end
102
103     resources(:service => 'nscd') rescue service 'nscd' do
104       action [:enable]
105       supports :status => true, :restart => true, :reload => true
106     end
107
108     template '/etc/nslcd.conf' do
109       source 'etc/nslcd.conf'
110       owner 'root'
111       group 'root'
112       mode '0600'
113       variables({'nslcd_conf_keys' => nslcd_conf_keys})
114       notifies :restart, 'service[nslcd]'
115       notifies :restart, 'service[nscd]'
116     end
117 end
118
119 ruby_block 'configuring_nameservices' do
120   block do
121     conf_file = '/etc/nsswitch.conf'
122     nameservices = node['openldap']['ldap_lookup_nameservices']
123     if !nameservices.nil? && !nameservices.empty? then
124       open(conf_file, 'r+') {|file|
125         file.flock(File::LOCK_EX)
126         is_modified = false
127         buf = ''
128         file.each {|line|
129           if line =~ /^(\w+):\s+(.*)$/ then
130             if nameservices.include?($1) && !$2.include?('ldap')
131               line.chomp! << " ldap\n"
132               is_modified = true
133             end
134           end
135           buf << line
136         }
137         if is_modified then
138           print "\nnew #{conf_file}: [#{buf}]"
139           file.rewind
140           file.puts buf
141           file.truncate(file.tell)
142         end
143       }
144     end
145   end
146   action :run
147   notifies :restart, 'service[nscd]'
148 end