| | 19 | #def __init__(self): |
|---|
| | 20 | #if not os.path.exists(self.BaseDir): |
|---|
| | 21 | #os.mkdir(self.BaseDir) |
|---|
| | 22 | #if not os.path.exists(self.PolicyDir): |
|---|
| | 23 | #os.mkdir(self.PolicyDir) |
|---|
| | 24 | #if not os.path.exists(self.DataDir): |
|---|
| | 25 | #os.mkdir(self.DataDir) |
|---|
| | 26 | #self.config = ConfigParser.ConfigParser() |
|---|
| | 27 | #self.config.read([self.ModuleDataConfig]) |
|---|
| | 28 | #if self.config.has_section('modules'): |
|---|
| | 29 | #for module in self.config.options('modules'): |
|---|
| | 30 | #self.ModuleData[module] = self.config.get('modules', module) |
|---|
| | 31 | #def __del__(self): |
|---|
| | 32 | #assert not self.Modified |
|---|
| | 33 | #def write(self): |
|---|
| | 34 | #module_data_file = open(self.ModuleDataConfig, 'w') |
|---|
| | 35 | #self.config.write(module_data_file) |
|---|
| | 36 | #module_data_file.close() |
|---|
| | 37 | #self.Modified = False |
|---|
| | 38 | #def update(self): |
|---|
| | 39 | #if not self.Modified: |
|---|
| | 40 | #return |
|---|
| | 41 | #if not self.config.has_section('modules'): |
|---|
| | 42 | #self.config.add_section('modules') |
|---|
| | 43 | #self.Modified = True |
|---|
| | 44 | #insert_modules = [] |
|---|
| | 45 | #remove_modules = [] |
|---|
| | 46 | #reload_base = False |
|---|
| | 47 | #for module in self.ModuleData.keys(): |
|---|
| | 48 | #if not self.config.has_option('modules', module): |
|---|
| | 49 | #self.config.set('modules', module, self.ModuleData[module]) |
|---|
| | 50 | #self.Modified = True |
|---|
| | 51 | #if module == 'base.pp': |
|---|
| | 52 | #reload_base = True |
|---|
| | 53 | #else: |
|---|
| | 54 | #insert_modules.append(module) |
|---|
| | 55 | #elif self.ModuleData[module] != self.config.get('modules', module): |
|---|
| | 56 | #self.config.set('modules', module, self.ModuleData[module]) |
|---|
| | 57 | #self.Modified = True |
|---|
| | 58 | #if module == 'base.pp': |
|---|
| | 59 | #reload_base = True |
|---|
| | 60 | #else: |
|---|
| | 61 | #insert_modules.append(module) |
|---|
| | 62 | #for module in self.config.options('modules'): |
|---|
| | 63 | #if module not in self.ModuleData.keys(): |
|---|
| | 64 | #self.config.remove_option('modules', module) |
|---|
| | 65 | #self.Modified = True |
|---|
| | 66 | #remove_modules.append(module) |
|---|
| | 67 | #if self.Modified: |
|---|
| | 68 | #self.write() |
|---|
| | 69 | #semodule_command = '/usr/sbin/semodule' |
|---|
| | 70 | #if reload_base: |
|---|
| | 71 | #semodule_command += ' -b base.pp' |
|---|
| | 72 | #for module in insert_modules: |
|---|
| | 73 | #semodule_command += ' -i ' + module |
|---|
| | 74 | #for module in remove_modules: |
|---|
| | 75 | #semodule_command += ' -r ' + module |
|---|
| | 76 | #if semodule_command == '/usr/sbin/semodule': |
|---|
| | 77 | #semodule_command += ' -R' #no changes? just reload |
|---|
| | 78 | #cwd = os.getcwd() |
|---|
| | 79 | #os.chdir(self.PolicyDir) #avoid need to prepend path prefix |
|---|
| | 80 | #os.system(semodule_command) |
|---|
| | 81 | #os.chdir(cwd) |
|---|
| | 82 | #def set_module(self, module, version): |
|---|
| | 83 | #if int(version) == 0: |
|---|
| | 84 | #self.remove_module(module) |
|---|
| | 85 | #return |
|---|
| | 86 | #if not os.path.exists(os.path.join(self.PolicyDir,module)): |
|---|
| | 87 | #raise RuntimeError, 'No module \'' + module + '\' exists' |
|---|
| | 88 | #self.ModuleData[module] = version |
|---|
| | 89 | #self.Modified = True |
|---|
| | 90 | #def remove_module(self, module): |
|---|
| | 91 | #if not self.ModuleData.has_key(module): |
|---|
| | 92 | #return #not there nothing to do |
|---|
| | 93 | #del self.ModuleData[module] |
|---|
| | 94 | #os.remove(os.path.join(self.PolicyDir, module)) |
|---|
| | 95 | #self.Modified = True |
|---|
| | 96 | #def get_status(self): |
|---|
| | 97 | #self.update() |
|---|
| | 98 | #return self.ModuleData |
|---|
| | 99 | #def verify_module(self, module, version, md5sum): |
|---|
| | 100 | #if int(version) == 0: |
|---|
| | 101 | #return not self.ModuleData.has_key(module) |
|---|
| | 102 | #elif not self.ModuleData.has_key(module): |
|---|
| | 103 | #return False |
|---|
| | 104 | #if int(self.ModuleData[module]) != int(version): |
|---|
| | 105 | #return False |
|---|
| | 106 | #sum = file_util.checksum(os.path.join(self.PolicyDir, module)) |
|---|
| | 107 | #return sum == md5sum |
|---|
| | 108 | |
|---|
| | 109 | class AgentStorage: |
|---|
| | 110 | '''Manages the storage data for a single agent''' |
|---|
| 24 | | self.config.read([self.ModuleDataConfig]) |
|---|
| 25 | | if self.config.has_section('modules'): |
|---|
| 26 | | for module in self.config.options('modules'): |
|---|
| 27 | | self.ModuleData[module] = self.config.get('modules', module) |
|---|
| 28 | | def __del__(self): |
|---|
| 29 | | assert not self.Modified |
|---|
| 30 | | def write(self): |
|---|
| 31 | | module_data_file = open(self.ModuleDataConfig, 'w') |
|---|
| 32 | | self.config.write(module_data_file) |
|---|
| 33 | | module_data_file.close() |
|---|
| 34 | | self.Modified = False |
|---|
| | 117 | store = storage.Layer(self.store_path) |
|---|
| | 118 | store.control() |
|---|
| | 119 | store.update() |
|---|
| | 120 | self.config.add_section('general') |
|---|
| | 121 | self.config.add_section('bools') |
|---|
| | 122 | self.config.read(self.config_path) #safe even if config_path does not exist |
|---|
| | 123 | for b in self.config.options('bools'): |
|---|
| | 124 | self.bools[b] = self.config.getboolean('bools', b) |
|---|
| | 125 | if self.config.has_option('general', 'enforcing'): |
|---|
| | 126 | self.enforcing = self.config.get('general', 'enforcing') |
|---|
| | 127 | |
|---|
| | 128 | def set_file_status(self, name, group, version, local=False): |
|---|
| | 129 | '''Record a change to a file''' |
|---|
| | 130 | if int(version) == 0: |
|---|
| | 131 | self.remove_file(name) |
|---|
| | 132 | return |
|---|
| | 133 | store = storage.Layer(self.store_path) |
|---|
| | 134 | store.add(name, False, version) |
|---|
| | 135 | store.entries[name].local = local |
|---|
| | 136 | store.entries[name].group = group |
|---|
| | 137 | store.update() |
|---|
| | 138 | |
|---|
| 36 | | if not self.Modified: |
|---|
| 37 | | return |
|---|
| 38 | | if not self.config.has_section('modules'): |
|---|
| 39 | | self.config.add_section('modules') |
|---|
| 40 | | self.Modified = True |
|---|
| 41 | | insert_modules = [] |
|---|
| 42 | | remove_modules = [] |
|---|
| 43 | | reload_base = False |
|---|
| 44 | | for module in self.ModuleData.keys(): |
|---|
| 45 | | if not self.config.has_option('modules', module): |
|---|
| 46 | | self.config.set('modules', module, self.ModuleData[module]) |
|---|
| 47 | | self.Modified = True |
|---|
| 48 | | if module == 'base.pp': |
|---|
| 49 | | reload_base = True |
|---|
| 50 | | else: |
|---|
| 51 | | insert_modules.append(module) |
|---|
| 52 | | elif self.ModuleData[module] != self.config.get('modules', module): |
|---|
| 53 | | self.config.set('modules', module, self.ModuleData[module]) |
|---|
| 54 | | self.Modified = True |
|---|
| 55 | | if module == 'base.pp': |
|---|
| 56 | | reload_base = True |
|---|
| 57 | | else: |
|---|
| 58 | | insert_modules.append(module) |
|---|
| 59 | | for module in self.config.options('modules'): |
|---|
| 60 | | if module not in self.ModuleData.keys(): |
|---|
| 61 | | self.config.remove_option('modules', module) |
|---|
| 62 | | self.Modified = True |
|---|
| 63 | | remove_modules.append(module) |
|---|
| 64 | | if self.Modified: |
|---|
| 65 | | self.write() |
|---|
| 66 | | semodule_command = '/usr/sbin/semodule' |
|---|
| 67 | | if reload_base: |
|---|
| 68 | | semodule_command += ' -b base.pp' |
|---|
| 69 | | for module in insert_modules: |
|---|
| 70 | | semodule_command += ' -i ' + module |
|---|
| 71 | | for module in remove_modules: |
|---|
| 72 | | semodule_command += ' -r ' + module |
|---|
| 73 | | if semodule_command == '/usr/sbin/semodule': |
|---|
| 74 | | semodule_command += ' -R' #no changes? just reload |
|---|
| 75 | | cwd = os.getcwd() |
|---|
| 76 | | os.chdir(self.PolicyDir) #avoid need to prepend path prefix |
|---|
| 77 | | os.system(semodule_command) |
|---|
| 78 | | os.chdir(cwd) |
|---|
| 79 | | def set_module(self, module, version): |
|---|
| | 140 | '''Sync all data to disk''' |
|---|
| | 141 | store = storage.Layer(self.store_path) |
|---|
| | 142 | store.update() |
|---|
| | 143 | self.config.set('general', 'enforcing', self.enforcing) |
|---|
| | 144 | for b,v in self.bools.items(): |
|---|
| | 145 | self.config.set('bools', b, str(v)) |
|---|
| | 146 | data_file = open(self.config_path, 'w') |
|---|
| | 147 | self.config.write(data_file) |
|---|
| | 148 | data_file.close() |
|---|
| | 149 | |
|---|
| | 150 | def apply_local_files(self): |
|---|
| | 151 | '''Record that all local changes have been sent to the server.''' |
|---|
| | 152 | store = storage.Layer(self.store_path) |
|---|
| | 153 | store.rollback() |
|---|
| | 154 | store.update() |
|---|
| | 155 | self.update() |
|---|
| | 156 | |
|---|
| | 157 | def remove_file(self, name): |
|---|
| | 158 | '''remove a file from storage''' |
|---|
| | 159 | store = storage.Layer(self.store_path) |
|---|
| | 160 | store.remove(name) |
|---|
| | 161 | store.update() |
|---|
| | 162 | |
|---|
| | 163 | def list_newer_files(self): |
|---|
| | 164 | store = storage.Layer(self.store_path) |
|---|
| | 165 | flist = store.list_local_newer() |
|---|
| | 166 | store.update() |
|---|
| | 167 | return flist |
|---|
| | 168 | |
|---|
| | 169 | def verify_file(self, name, version, md5sum): |
|---|
| | 170 | '''Verify that the file name has the same version and md5sum''' |
|---|
| | 171 | store = storage.Layer(self.store_path) |
|---|
| | 172 | e = stroe.get_entry(name) |
|---|
| 81 | | self.remove_module(module) |
|---|
| 82 | | return |
|---|
| 83 | | if not os.path.exists(os.path.join(self.PolicyDir,module)): |
|---|
| 84 | | raise RuntimeError, 'No module \'' + module + '\' exists' |
|---|
| 85 | | self.ModuleData[module] = version |
|---|
| 86 | | self.Modified = True |
|---|
| 87 | | def remove_module(self, module): |
|---|
| 88 | | if not self.ModuleData.has_key(module): |
|---|
| 89 | | return #not there nothing to do |
|---|
| 90 | | del self.ModuleData[module] |
|---|
| 91 | | os.remove(os.path.join(self.PolicyDir, module)) |
|---|
| 92 | | self.Modified = True |
|---|
| 93 | | def get_status(self): |
|---|
| 94 | | self.update() |
|---|
| 95 | | return self.ModuleData |
|---|
| 96 | | def verify_module(self, module, version, md5sum): |
|---|
| 97 | | if int(version) == 0: |
|---|
| 98 | | return not self.ModuleData.has_key(module) |
|---|
| 99 | | elif not self.ModuleData.has_key(module): |
|---|
| | 174 | store.update() |
|---|
| | 175 | return e is None |
|---|
| | 176 | elif e.applied != int(version): |
|---|