1#!/usr/bin/env python 2# Copyright (c) 2007, Secure64 Software Corporation 3# 4# Permission is hereby granted, free of charge, to any person obtaining a copy 5# of this software and associated documentation files (the "Software"), to deal 6# in the Software without restriction, including without limitation the rights 7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8# copies of the Software, and to permit persons to whom the Software is 9# furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice shall be included in 12# all copies or substantial portions of the Software. 13# 14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20# THE SOFTWARE. 21# 22# 23# class to represent all of the bind2nsd/syncem config items 24# 25 26import os 27import os.path 28import sys 29 30if os.path.exists('../pyDes-1.2'): 31 sys.path.append('../pyDes-1.2') 32import pyDes 33 34def mkcipher(val): 35 data = val.split() 36 ctxt = '' 37 for ii in range(0, len(data)): 38 ctxt += chr(int(data[ii])) 39 return ctxt 40 41def mkprintable(val): 42 cstr = '' 43 for ii in range(0, len(val)): 44 c = val[ii] 45 cstr += '%d ' % (ord(c)) 46 return cstr 47 48 49class Config: 50 51 def __init__(self): 52 #-- set all of the defaults 53 self.fname = '' 54 self.config = \ 55 { 'acl_list' : 'acl_list', 56 'bind2nsd' : '/usr/bin/bind2nsd', 57 'database' : '"nsd.db"', 58 'DEBUG' : False, 59 'DEMO-MODE' : False, 60 'destdir' : '/tmp/foobar', 61 'dest-ip' : '127.0.0.1', 62 'destuser' : 'dns', 63 'difffile' : '"ixfr.db"', 64 'dnspw' : 'iforgot', 65 'identity' : '"unknown"', 66 'ip-address' : '127.0.0.1', 67 'logfile' : '"log"', 68 'named-checkconf' : '/usr/sbin/named-checkconf', 69 'named-checkzone' : '/usr/sbin/named-checkzone', 70 'named_root' : '/etc/bind9', 71 'named_conf' : 'named.conf', 72 'named_watchlist' : '/etc/named.conf', 73 'nsd-checkconf' : '/usr/sbin/nsd-checkconf', 74 'nsd_conf' : 'nsd.conf', 75 'nsd_conf_dir' : '/etc/nsd/', 76 'nsd_preamble' : 'nsd.conf-preamble', 77 'password_file' : '/etc/bind2nsd/passwd', 78 'pidfile' : '"nsd.pid"', 79 'port' : '53', 80 'rebuild_cmd' : '/etc/init.d/nsdc rebuild', 81 'restart_cmd' : '/etc/init.d/nsdc restart', 82 'sleep_time' : '5', 83 'start_cmd' : '/etc/init.d/nsdc start', 84 'statistics' : '3600', 85 'stop_cmd' : '/etc/init.d/nsdc stop', 86 'syspw' : 'iforgot', 87 'tmpdir' : '/tmp/secure64/', # must have trailing '/' 88 'username' : 'nsd', 89 'version' : '0.5.0', 90 'xfrd-reload-timeout' : '10', 91 'zonec_cmd' : '/etc/init.d/zonec', 92 } 93 94 self.init() 95 self.read_passwords() 96 97 if self.config['DEBUG']: 98 self.dump() 99 return 100 101 def init(self): 102 fname = '' 103 if os.path.exists('bind2nsd.conf'): 104 self.fname = 'bind2nsd.conf' 105 else: 106 fname = os.getenv('HOME', '.') + '/bind2nsd.conf' 107 if os.path.exists(fname): 108 self.fname = fname 109 else: 110 if os.path.exists('/etc/bind2nsd/bind2nsd.conf'): 111 self.fname = '/etc/bind2nsd/bind2nsd.conf' 112 else: 113 print '? hrm. no config file found -- did you _mean_ that?' 114 115 #-- override the defaults 116 if self.fname != '': 117 fd = open(self.fname, 'r') 118 line = fd.readline() 119 while line: 120 if len(line) > 0: 121 info = line.split() 122 if line[0] == '#': 123 pass # ignore comments 124 elif len(info) > 0: 125 item = info[0].strip() 126 if info[1].strip() == '=': 127 if item in self.config: 128 self.config[item] = ' '.join(info[2:]) 129 else: 130 pass # ignore lines with only one field 131 else: 132 pass # ignore empty lines 133 line = fd.readline() 134 135 return 136 137 def read_passwords(self): 138 fname = self.config['password_file'] 139 if os.path.exists(fname): 140 fd = open(fname, 'r+') 141 syspw = fd.readline() 142 dnspw = fd.readline() 143 fd.close() 144 145 obj = pyDes.triple_des('aBcDeFgHiJkLmNoP', pyDes.ECB) 146 self.config['syspw'] = obj.decrypt(mkcipher(syspw), '#') 147 self.config['dnspw'] = obj.decrypt(mkcipher(dnspw), '#') 148 149 return 150 151 def getValue(self, item): 152 if item in self.config: 153 return self.config[item] 154 else: 155 return None 156 157 def setValue(self, item, val): 158 if item in self.config: 159 self.config[item] = val 160 else: 161 print '? no such config item "%s" (%s)' % (item, val) 162 return 163 164 def dump(self): 165 print '=> Config:' 166 print ' %-20s = %s' % ('fname', self.fname) 167 for ii in self.config: 168 print ' %-20s = %s' % (ii, self.config[ii]) 169 return 170 171