def parse_authmap(fp):
    """
    Low level parser for authmap files. Takes a file pointer to an open authmap
    file and parses it into a dict.

    The returned dict will be composed as follows:
      - The keys are the names of local entities (users or groups)
      - The values are lists of authorized LDAP entities, represented as
        strings.
    """
    authmap = {}

    while True:
        line = line = fp.readline()
        if line == '':
            break

        line = line.strip()

        if line == '' or line[0] == '#' or line.rstrip() == '':
            continue

        localuser, entities = line.split(':')

        if not localuser in authmap:
            authmap[localuser] = []

        for entity in entities.split(','):
            authmap[localuser].append(entity.strip())

    return authmap