~fkhan-zivios/zivios/devel

« back to all changes in this revision

Viewing changes to application/library/Zivios/Installer/DnsHandler.php

  • Committer: Faraz Khan
  • Date: 2008-09-15 13:29:33 UTC
  • Revision ID: fkhan@zivios.org-20080915132933-d27jml5l29xw4gsr
Initial release to bazaar, code in sync with 0.5.0-release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright (c) 2008 Zivios, LLC.
 
4
 *
 
5
 * This file is part of Zivios.
 
6
 *
 
7
 * Zivios is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Zivios is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Zivios.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * @package             ZiviosInstaller
 
21
 * @copyright   Copyright (c) 2008 Zivios, LLC. (http://www.zivios.org)
 
22
 * @license             http://www.zivios.org/legal/license
 
23
 * @version             $Id$
 
24
 **/
 
25
 
 
26
class Zivios_Installer_DnsHandler extends Zivios_Installer_LinuxCore
 
27
{
 
28
        /**
 
29
         * We're hard coding certain constants for now. At some point
 
30
         * we'll make these user definable during the installation process.
 
31
         */
 
32
        const VERSION                   = '9.5.0-P2';
 
33
        const SRCFILE                   = 'bind-9.5.0-P2.tar.gz';
 
34
        const EXTDIR                    = '/usr/local/src/zvcompile';
 
35
        const COMPATVERSION             = '0.5.0';
 
36
        const SRCFILEPATH               = '/usr/local/src/zivios-0.5.0/spkgs/bind-9.5.0-P2.tar.gz';
 
37
        const PKGNAME                   = 'bind-9.5.0-P2';
 
38
        const SCRIPTSDIR                = '/usr/local/src/zivios-0.5.0/bind/conf';
 
39
        const TMPDIR                    = '/usr/local/src/zvcompile/bind';
 
40
        const TMPRNDCKEYDATA    = 'rndckeydata';
 
41
        const TMPRNDCKEY                = '/usr/local/src/zvcompile/bind/rndc.key';
 
42
        const TMPNAMEDCONF              = '/usr/local/src/zivios-0.5.0/bind/conf/named.conf';
 
43
        const TMPNAMEDLOCAL             = '/usr/local/src/zvcompile/bind/named.conf.local';
 
44
        const TMPNAMEDOPTIONS   = '/usr/local/src/zvcompile/bind/named.conf.options';
 
45
        const BINDDEFAULTS              = '/usr/local/src/zivios-0.5.0/bind/conf/defaults';
 
46
        const BINDINIT                  = '/usr/local/src/zivios-0.5.0/bind/init.d/zvbind';
 
47
 
 
48
        const ZVDNSSEC                  = '/opt/zivios/bind/sbin/dnssec-keygen';
 
49
        const ZVNAMEDCONF               = '/opt/zivios/bind/etc/named.conf';
 
50
        const ZVBINDINIT                = '/etc/init.d/zvbind';
 
51
        const ZVBINDETC                 = '/opt/zivios/bind/etc';
 
52
 
 
53
        /**
 
54
         * Status files
 
55
         */
 
56
        private $statusdone, $configdone;
 
57
 
 
58
        public function __construct()
 
59
        {
 
60
                parent::__construct();
 
61
                $this->statusdone = $this->appRoot . '/status/dns.install.stamp';
 
62
                $this->configdone = $this->appRoot . '/status/dnsconfig.install.stamp';
 
63
        }
 
64
 
 
65
        public function runInstall()
 
66
        {
 
67
                if (!file_exists(self::SRCFILEPATH) || !is_readable(self::SRCFILEPATH))
 
68
                        $this->raiseError(700);
 
69
 
 
70
                /**
 
71
                 * Try and extract OpenLDAP source.
 
72
                 */
 
73
                if (!$this->extractSource(self::SRCFILEPATH, self::EXTDIR,self::PKGNAME,1))
 
74
                        $this->raiseError(701);
 
75
 
 
76
                /**
 
77
                 * Source extraction complete. Copy over the required configuration script
 
78
                 * to the openldap extract folder.
 
79
                 */
 
80
                if (!$this->copyConfigureScript(0755))
 
81
                        $this->raiseError(702);
 
82
 
 
83
                /**
 
84
                 * Distribution specific pre-processing call.
 
85
                 */
 
86
                $srcFolder = self::EXTDIR . '/' . self::PKGNAME . '/';
 
87
                $execScript = 'zvconfig_' . $this->zvDistroId . '.sh';
 
88
 
 
89
                if (!$this->compile($srcFolder,$execScript,1,1,1))
 
90
                        $this->raiseError(703);
 
91
 
 
92
                /**
 
93
                 * Write install status success.
 
94
                 */
 
95
                $this->touchInstallStamp($this->statusdone, $this->getTimestamp() . "::Zivios DNS initialized.|" .
 
96
                        session_id() . "\n");
 
97
 
 
98
                return 1;
 
99
        }
 
100
 
 
101
        public function runConfig($data)
 
102
        {
 
103
                /**
 
104
                 * If work folder exists, purge it and recreate.
 
105
                 */
 
106
                /**
 
107
                 * Ensure the TMP dir exists.
 
108
                 */
 
109
                if (is_dir(self::TMPDIR)) {
 
110
                        $cmd = $this->removeRecursive . ' ' . self::TMPDIR;
 
111
                        $response = $this->execRcommand($cmd,true);
 
112
                        if ($response['exitcode'] != 0)
 
113
                                $this->raiseError(707);
 
114
                }
 
115
 
 
116
                /**
 
117
                 * Create TMP folder.
 
118
                 */
 
119
                $cmd = $this->mkdir . ' ' . self::TMPDIR;
 
120
                $response = $this->execRcommand($cmd);
 
121
                if ($response['exitcode'] != 0)
 
122
                        $this->raiseError(708);
 
123
 
 
124
 
 
125
                /**
 
126
                 * Copy over required configuration and initialization files.
 
127
                 */
 
128
                $cmd = $this->copy . ' ' . self::BINDINIT . ' ' . self::ZVBINDINIT;
 
129
 
 
130
                $response = $this->execRcommand($cmd,true);
 
131
                if ($response['exitcode'] != 0)
 
132
                        $this->raiseError(704);
 
133
 
 
134
                /**
 
135
                 * Copy over defaults, named.conf & db.* files to /opt/zivios/bind/etc
 
136
                 */
 
137
                $confFiles = array();
 
138
                $confFiles[] = 'defaults';
 
139
                $confFiles[] = 'named.conf';
 
140
                $confFiles[] = 'db.0';
 
141
                $confFiles[] = 'db.127';
 
142
                $confFiles[] = 'db.255';
 
143
                $confFiles[] = 'db.empty';
 
144
                $confFiles[] = 'db.local';
 
145
                $confFiles[] = 'db.root';
 
146
 
 
147
                foreach ($confFiles as $configFile) {
 
148
                        $cmd = $this->copy . " " . self::SCRIPTSDIR . "/". $configFile . " " . self::ZVBINDETC;
 
149
                        $response = $this->execRcommand($cmd, true);
 
150
                        if ($response['exitcode'] != 0)
 
151
                                $this->raiseError(705);
 
152
                }
 
153
 
 
154
                /**
 
155
                 * Generate the RNDC key file
 
156
                 */
 
157
                $cwd = getcwd();
 
158
 
 
159
                if (!chdir(self::TMPDIR))
 
160
                        $this->raiseError(709);
 
161
 
 
162
 
 
163
                $cmd = self::ZVDNSSEC . ' -a hmac-md5 -b 256 -n HOST ' . self::TMPRNDCKEYDATA;
 
164
                $response = $this->execRcommand($cmd);
 
165
                if ($response['exitcode'] != 0)
 
166
                        $this->raiseError(706);
 
167
 
 
168
                /**
 
169
                 * Restore CWD
 
170
                 */
 
171
                if (!chdir($cwd))
 
172
                        $this->raiseError(710);
 
173
 
 
174
                $d = dir(self::TMPDIR);
 
175
 
 
176
                while (false !== ($entry = $d->read())) {
 
177
                        if ($entry != '.' && $entry != '..') {
 
178
                                /**
 
179
                                 * Ensure the extension is .private
 
180
                                 */
 
181
                                if (substr($entry, strrpos($entry, '.') + 1) == 'private') {
 
182
                                        /**
 
183
                                         * Read file contents and get the hmac-md5 key
 
184
                                         */
 
185
                                        $fh = fopen(self::TMPDIR.'/'.$entry, "r");
 
186
                                        $contents = fread($fh, filesize(self::TMPDIR.'/'.$entry));
 
187
                                        fclose($fh);
 
188
                                }
 
189
                        }
 
190
                }
 
191
 
 
192
                $allC = split("\n",$contents);
 
193
                foreach ($allC as $c) {
 
194
                        $len = (strlen($c) - 4) * -1;
 
195
 
 
196
                        if (substr($c, 0, $len) == 'Key:') {
 
197
                                /**
 
198
                                 * get the key
 
199
                                 */
 
200
                                $rndcKeyString = substr($c, 4);
 
201
                                break;
 
202
                        }
 
203
                }
 
204
 
 
205
                /**
 
206
                 * Write templates to tmp folder and move to zivios/bind/etc/
 
207
                 */
 
208
                $namedlocal = $this->appRoot . '/library/Zivios/Installer/Basedata/templates/dns/'.
 
209
                        'named.conf.local.tmpl';
 
210
 
 
211
                $namedoptions = $this->appRoot . '/library/Zivios/Installer/Basedata/templates/dns/'.
 
212
                        'named.conf.options.tmpl';
 
213
 
 
214
                $rndckeyfile = $this->appRoot . '/library/Zivios/Installer/Basedata/templates/dns/'.
 
215
                        'rndc.key.tmpl';
 
216
 
 
217
                if (!file_exists($namedlocal) || !is_readable(($namedlocal)))
 
218
                        $this->raiseError(711);
 
219
 
 
220
                /**
 
221
                 * Generate template for named.conf.local and write to file.
 
222
                 * Move to zivios bind/etc/
 
223
                 */
 
224
                $vals = array();
 
225
                $vals['base_dn'] = $data['basedn'];
 
226
                $vals['bind_pass'] = $data['bindpass'];
 
227
                $vals['master_ip'] = $data['ipaddress'];
 
228
 
 
229
                $namedlocaltmpl = Ecl_Util::renderTmplToCfg($namedlocal, $vals);
 
230
 
 
231
                if (!$fp = fopen(self::TMPNAMEDLOCAL, "w"))
 
232
                        $this->raiseError(712);
 
233
 
 
234
                if (fwrite($fp, $namedlocaltmpl) === FALSE)
 
235
                        $this->raiseError(713);
 
236
 
 
237
                fclose($fp);
 
238
 
 
239
                $cmd = $this->copy . " " . self::TMPNAMEDLOCAL . " " . self::ZVBINDETC . "/named.conf.local";
 
240
                $response = $this->execRCommand($cmd, true);
 
241
                if ($response['exitcode'] != 0)
 
242
                        $this->raiseError(714);
 
243
 
 
244
                /**
 
245
                 * @todo: Give ownership to bind user & group.
 
246
                 */
 
247
 
 
248
 
 
249
                /**
 
250
                 * Generate named.conf.options.
 
251
                 */
 
252
                if (!file_exists($namedoptions) || !is_readable(($namedoptions)))
 
253
                        $this->raiseError(715);
 
254
 
 
255
                /**
 
256
                 * Generate template for named.conf.local and write to file.
 
257
                 * Move to zivios bind/etc/
 
258
                 */
 
259
                $vals = array();
 
260
                if (array_key_exists('forwarders', $data)) {
 
261
                        $forwarderLine = '';
 
262
                        foreach ($data['forwarders'] as $forwarder) {
 
263
                                $forwarderLine .= "\t\t".$forwarder.";\n";
 
264
                        }
 
265
                        $vals['forwarders'] = $forwarderLine;
 
266
                }
 
267
 
 
268
                $namedoptionstmpl = Ecl_Util::renderTmplToCfg($namedoptions, $vals);
 
269
 
 
270
                if (!$fp = fopen(self::TMPNAMEDOPTIONS, "w"))
 
271
                        $this->raiseError(716);
 
272
 
 
273
                if (fwrite($fp, $namedoptionstmpl) === FALSE)
 
274
                        $this->raiseError(717);
 
275
 
 
276
                fclose($fp);
 
277
 
 
278
                $cmd = $this->copy . " " . self::TMPNAMEDOPTIONS . " " . self::ZVBINDETC . "/named.conf.options";
 
279
                $response = $this->execRCommand($cmd, true);
 
280
                if ($response['exitcode'] != 0)
 
281
                        $this->raiseError(718);
 
282
 
 
283
                /**
 
284
                 * Write the RNDC key file.
 
285
                 */
 
286
                if (!file_exists($rndckeyfile) || !is_readable(($rndckeyfile)))
 
287
                        $this->raiseError(719);
 
288
 
 
289
                /**
 
290
                 * Generate template for named.conf.local and write to file.
 
291
                 * Move to zivios bind/etc/
 
292
                 */
 
293
                $vals = array();
 
294
                $vals['rndc_key'] = ltrim($rndcKeyString);
 
295
 
 
296
                $rndckeytmpl = Ecl_Util::renderTmplToCfg($rndckeyfile, $vals);
 
297
 
 
298
                if (!$fp = fopen(self::TMPRNDCKEY, "w"))
 
299
                        $this->raiseError(720);
 
300
 
 
301
                if (fwrite($fp, $rndckeytmpl) === FALSE)
 
302
                        $this->raiseError(721);
 
303
 
 
304
                fclose($fp);
 
305
 
 
306
                $cmd = $this->copy . " " . self::TMPRNDCKEY . " " . self::ZVBINDETC . "/rndc.key";
 
307
                $response = $this->execRCommand($cmd, true);
 
308
                if ($response['exitcode'] != 0)
 
309
                        $this->raiseError(722);
 
310
 
 
311
                $cmd = self::ZVBINDINIT . ' restart';
 
312
                $response = $this->execRcommand($cmd, true);
 
313
                if ($response['exitcode'] != 0)
 
314
                        $this->raiseError(723);
 
315
 
 
316
                $this->touchInstallStamp($this->configdone, $this->getTimestamp() . "::Zivios Bind DNS data Initialized.|" .
 
317
                        session_id() . "\n");
 
318
 
 
319
                return 1;
 
320
        }
 
321
 
 
322
 
 
323
        protected function copyConfigureScript($perms=0644)
 
324
        {
 
325
                $cfgFile = 'zvconfig_' . $this->zvDistroId . '.sh';
 
326
                $copySrc = self::SCRIPTSDIR . '/' . $cfgFile;
 
327
                $dstFile = self::EXTDIR  . '/' . self::PKGNAME . '/' . $cfgFile;
 
328
 
 
329
                if (copy($copySrc,$dstFile)) {
 
330
                        /**
 
331
                         * We set permissions on the copied file to 0644 unless
 
332
                         * otherwise specified
 
333
                         */
 
334
                        chmod($dstFile, $perms);
 
335
                        return 1;
 
336
                }
 
337
 
 
338
                return 0;
 
339
        }
 
340
}
 
 
b'\\ No newline at end of file'