# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""A gather script that gathers specific files.

This is intended for config files where the actual contents is
important for compliance checking (e.g. postfix).

Clearly there is an issue with how the script decides which files to
gather.  We'll get to that later, when it will be specified (probably
implicitly) through policy statements; for now, it's a list hardcoded
in the script.

For now, we only gather the first 32K of each file listed.  That
should suffice for the typical config file...
"""

import os
import glob

wanted_patterns = {
    "posix": ["/etc/postfix/*.cf",
              "/etc/postfix/postfix-files",
              "/etc/postfix/aliases",
              "/etc/aliases",
              ],
    }

def Gather(collector):
    table = collector.table("files", ["filename", "contents"])
    for pattern in wanted_patterns.get(os.name) or []:
        for filename in glob.glob(pattern):
            try:
                f = open(filename, "rb")
            except IOError as err:
                continue
            contents = None
            try:
                contents = f.read(32*1024)
            finally:
                f.close()
            if contents is not None:
                table.add([filename, contents])

def _test():
    from sigma.gather import gathering
    gathering.test(Gather)

if __name__ == "__main__":
    _test()
