def ParseLine(cls, index, line) -> Optional[rdf_client.User]: precondition.AssertType(line, Text) fields = "username,password,uid,gid,fullname,homedir,shell".split(",") try: if not line: return None dat = dict(zip(fields, line.split(":"))) user = rdf_client.User(username=dat["username"], uid=int(dat["uid"]), homedir=dat["homedir"], shell=dat["shell"], gid=int(dat["gid"]), full_name=dat["fullname"]) return user except (IndexError, KeyError): raise parsers.ParseError("Invalid passwd file at line %d. %s" % ((index + 1), line))
def ParseLines(cls, lines): users = set() filter_regexes = [ re.compile(x) for x in config.CONFIG["Artifacts.netgroup_filter_regexes"] ] username_regex = re.compile(cls.USERNAME_REGEX) blacklist = config.CONFIG["Artifacts.netgroup_user_blacklist"] for index, line in enumerate(lines): if line.startswith("#"): continue splitline = line.split(" ") group_name = splitline[0] if filter_regexes: filter_match = False for regex in filter_regexes: if regex.search(group_name): filter_match = True break if not filter_match: continue for member in splitline[1:]: if member.startswith("("): try: _, user, _ = member.split(",") if user not in users and user not in blacklist: if not username_regex.match(user): yield rdf_anomaly.Anomaly( type="PARSER_ANOMALY", symptom="Invalid username: %s" % user) else: users.add(user) yield rdf_client.User( username=utils.SmartUnicode(user)) except ValueError: raise parsers.ParseError( "Invalid netgroup file at line %d: %s" % (index + 1, line))
def _ParseFile(self, file_obj, line_parser): """Process a file line by line. Args: file_obj: The file to parse. line_parser: The parser method used to process and store line content. Raises: parser.ParseError if the parser is unable to process the line. """ lines = [ l.strip() for l in utils.ReadFileBytesAsUnicode(file_obj).splitlines() ] try: for index, line in enumerate(lines): if line: line_parser(line) except (IndexError, KeyError) as e: raise parsers.ParseError("Invalid file at line %d: %s" % (index + 1, e))
def ParseResponse(self, knowledge_base, response, path_type): del knowledge_base, response, path_type # Unused. raise parsers.ParseError("It was bound to happen.")
def Parse(self, stat, _): value = stat.registry_data.GetValue() if value: yield rdfvalue.RDFString(value) else: raise parsers.ParseError("Invalid value for key %s" % stat.pathspec.path)