Debian Codegraph @

contrib/copy-schema-to-ca-RHEL6.py

raw symbols SCIP
1 #!/usr/bin/python2
2
3 """Copy the IPA schema to the CA directory server instance
4
5 You need to run this script to prepare a 2.2 or 3.0 IPA master for
6 installation of a 3.1 replica.
7
8 Once a 3.1 replica is in the domain, every older CA master will emit schema
9 replication errors until this script is run on it.
10
11 """
12
13 # DO NOT TOUCH THIS CODE, IT MUST BE COMPATIBLE WITH RHEL6
14 # disable pylint because current codebase didn't match RHEL6 code
15 # pylint: disable=all
16
17 import os
18 import sys
19 import pwd
20 import shutil
21
22 from hashlib import sha1
23
24 from ipapython import ipautil
25 from ipapython.ipa_log_manager import root_logger, standard_logging_setup
26 from ipaserver.install.dsinstance import schema_dirname
27 from ipalib import api
28
29 # oh dear, this is an old IPA (3.0+)
30 from ipaserver.install.dsinstance import DS_USER
31 from ipaserver.install.cainstance import PKI_USER
32 from ipapython import services
33
34 # for mod_nss
35 from ipaserver.install.httpinstance import NSS_CONF
36 from ipaserver.install.httpinstance import HTTPInstance
37 from ipaserver.install import installutils
38 from ipapython import sysrestore
39
40 SERVERID = "PKI-IPA"
41 SCHEMA_FILENAMES = (
42 "60kerberos.ldif",
43 "60samba.ldif",
44 "60ipaconfig.ldif",
45 "60basev2.ldif",
46 "60basev3.ldif",
47 "60ipadns.ldif",
48 "61kerberos-ipav3.ldif",
49 "65ipacertstore.ldif",
50 "65ipasudo.ldif",
51 "70ipaotp.ldif",
52 "05rfc2247.ldif",
53 )
54
55
56 def _sha1_file(filename):
57 with open(filename, 'rb') as f:
58 return sha1(f.read()).hexdigest()
59
60
61 def add_ca_schema():
62 """Copy IPA schema files into the CA DS instance
63 """
64 pki_pent = pwd.getpwnam(PKI_USER)
65 ds_pent = pwd.getpwnam(DS_USER)
66 for schema_fname in SCHEMA_FILENAMES:
67 source_fname = os.path.join(ipautil.SHARE_DIR, schema_fname)
68 target_fname = os.path.join(schema_dirname(SERVERID), schema_fname)
69 if not os.path.exists(source_fname):
70 root_logger.debug('File does not exist: %s', source_fname)
71 continue
72 if os.path.exists(target_fname):
73 target_sha1 = _sha1_file(target_fname)
74 source_sha1 = _sha1_file(source_fname)
75 if target_sha1 != source_sha1:
76 target_size = os.stat(target_fname).st_size
77 source_size = os.stat(source_fname).st_size
78 root_logger.info('Target file %s exists but the content is '
79 'different', target_fname)
80 root_logger.info('\tTarget file: sha1: %s, size: %s B',
81 target_sha1, target_size)
82 root_logger.info('\tSource file: sha1: %s, size: %s B',
83 source_sha1, source_size)
84 if not ipautil.user_input("Do you want replace %s file?" %
85 target_fname, True):
86 continue
87
88 else:
89 root_logger.info(
90 'Target exists, not overwriting: %s', target_fname)
91 continue
92 try:
93 shutil.copyfile(source_fname, target_fname)
94 except IOError as e:
95 root_logger.warning('Could not install %s: %s', target_fname, e)
96 else:
97 root_logger.info('Installed %s', target_fname)
98 os.chmod(target_fname, 0o440) # read access for dirsrv user/group
99 os.chown(target_fname, pki_pent.pw_uid, ds_pent.pw_gid)
100
101
102 def restart_pki_ds():
103 """Restart the CA DS instance to pick up schema changes
104 """
105 root_logger.info('Restarting CA DS')
106 services.service('dirsrv').restart(SERVERID)
107
108
109 # The ipa-3-0 set_directive() has very loose comparision of directive
110 # which would cause multiple NSSCipherSuite to be added so provide
111 # a custom function for it.
112 def set_directive(filename, directive, value, quotes=True, separator=' '):
113 """Set a name/value pair directive in a configuration file.
114
115 A value of None means to drop the directive.
116
117 This has only been tested with nss.conf
118 """
119 valueset = False
120 st = os.stat(filename)
121 fd = open(filename)
122 newfile = []
123 for line in fd:
124 if line.lstrip().startswith(directive):
125 valueset = True
126 if value is not None:
127 if quotes:
128 newfile.append('%s%s"%s"\n' %
129 (directive, separator, value))
130 else:
131 newfile.append('%s%s%s\n' % (directive, separator, value))
132 else:
133 newfile.append(line)
134 fd.close()
135 if not valueset:
136 if value is not None:
137 if quotes:
138 newfile.append('%s%s"%s"\n' % (directive, separator, value))
139 else:
140 newfile.append('%s%s%s\n' % (directive, separator, value))
141
142 fd = open(filename, "w")
143 fd.write("".join(newfile))
144 fd.close()
145 os.chown(filename, st.st_uid, st.st_gid) # reset perms
146
147
148 def update_mod_nss_cipher_suite():
149 add_ciphers = ['ecdhe_rsa_aes_128_sha', 'ecdhe_rsa_aes_256_sha']
150 ciphers = installutils.get_directive(NSS_CONF, 'NSSCipherSuite')
151
152 # Run through once to see if any of the new ciphers are there but
153 # disabled. If they are then enable them.
154 lciphers = ciphers.split(',')
155 new_ciphers = []
156 for cipher in lciphers:
157 for add in add_ciphers:
158 if cipher.endswith(add):
159 if cipher.startswith('-'):
160 cipher = '+%s' % add
161 new_ciphers.append(cipher)
162
163 # Run through again and add remaining ciphers as enabled.
164 for add in add_ciphers:
165 if add not in ciphers:
166 new_ciphers.append('+%s' % add)
167
168 ciphers = ','.join(new_ciphers)
169 set_directive(NSS_CONF, 'NSSCipherSuite', ciphers, False)
170 root_logger.info('Updated Apache cipher list')
171
172
173 def restart_http():
174 root_logger.info('Restarting HTTP')
175 fstore = sysrestore.FileStore('/var/lib/ipa/sysrestore')
176 http = HTTPInstance(fstore)
177 http.restart()
178
179
180 def main():
181 if os.getegid() != 0:
182 sys.exit("Must be root to run this script")
183 standard_logging_setup(verbose=True)
184
185 # In 3.0, restarting needs access to api.env
186 api.bootstrap_with_global_options(context='server')
187
188 add_ca_schema()
189 restart_pki_ds()
190 update_mod_nss_cipher_suite()
191 restart_http()
192
193 root_logger.info('Schema updated successfully')
194
195
196 if __name__ == '__main__':
197 main()