def _parseCommand(self, command): parsed = [] re_runas = re.compile('^\s*\((.+)\)\s*(.*)') re_tag = re.compile('^\s*([NOEXEC|EXEC|LOG_INPUT|NOLOG_INPUT|LOG_OUTPUT|NOLOG_OUTPUT|MAIL|NOMAIL|NOPASSWD|PASSWD|SETENV|NOSETENV:]*):\s*(.*)') data = {} unparsed = command # Runas r = re_runas.search(unparsed) if r: runas = str(r.group(1).strip()) data['runas'] = create_list(runas) unparsed = str(r.group(2)) # Tag t = re_tag.search(unparsed) if t: if t.group(1): tag = str(t.group(1)) data['tag'] = create_list(tag, sep=':') unparsed = str(t.group(2)) data['command'] = create_list(unparsed.strip()) return data
def parse(self, lines): re_alias_user = re.compile(r'^\s*User_Alias\s+(\w+)\s*=\s*(.*)$') re_alias_host = re.compile(r'^\s*Host_Alias\s*([\w\d_]+)\s*=\s*(.*)$') re_alias_runas = re.compile(r'^\s*Runas_Alias\s+(\w+)\s*=\s*(.*)$') re_alias_cmnd = re.compile(r'^\s*Cmnd_Alias\s+(\w+)\s*=\s*(.*)$') re_rule = re.compile(r'^\s*([\w\d_%:@\.\-,]+)\s+(\+?[\w\d_\.\!/\-,]+)\s*=\s*(.*)$') data = {} data['alias'] = {} data['alias']['user'] = [] data['alias']['runas'] = [] data['alias']['host'] = [] data['alias']['cmnd'] = [] data['rules'] = [] lines = self._collapseLines(lines) lines = self._pre_parse(lines) for line in lines: match_alias_user = re_alias_user.match(line) match_alias_host = re_alias_host.match(line) match_alias_runas = re_alias_runas.match(line) match_alias_cmnd = re_alias_cmnd.match(line) match_rule = re_rule.match(line) if match_alias_user: d = {} d['type'] = 'user' d['name'] = match_alias_user.group(1) d['value'] = create_list(match_alias_user.group(2)) data['alias']['user'].append(d) elif match_alias_runas: d = {} d['type'] = 'runas' d['name'] = match_alias_runas.group(1) d['value'] = create_list(match_alias_runas.group(2)) data['alias']['runas'].append(d) elif match_alias_host: d = {} d['type'] = 'host' d['name'] = match_alias_host.group(1) d['value'] = create_list(match_alias_host.group(2)) data['alias']['host'].append(d) elif match_alias_cmnd: d = {} d['type'] = 'cmnd' d['name'] = match_alias_cmnd.group(1) d['value'] = create_list(match_alias_cmnd.group(2)) data['alias']['cmnd'].append(d) elif match_rule: m_user = match_rule.group(1).strip() m_host = match_rule.group(2).strip() m_command = match_rule.group(3).strip() command = self._parseCommand(m_command) rule = {} rule['user'] = create_list(m_user) rule['host'] = create_list(m_host) if 'runas' in command: rule['runas'] = command['runas'] if 'tag' in command: rule['tag'] = command['tag'] rule['cmnd'] = command['command'] data['rules'].append(rule) return data