(**kwargs)
Helper function to check permissions and symlink status

    This function will take a list of file paths, resolve them to their
    actual location (for symlinks), and determine if they are writeable
    by the current user. Will also alert on broken symlinks and whether
    the target directory for the broken link is writeable.

    Expected kwargs: file_paths, files_message_text, dirs_message_text,
    message_box
Helper function to check permissions and symlink status
Helper function to check permissions and symlink status
def check_file_permissions(**kwargs):
    """Helper function to check permissions and symlink status

    This function will take a list of file paths, resolve them to their
    actual location (for symlinks), and determine if they are writeable
    by the current user. Will also alert on broken symlinks and whether
    the target directory for the broken link is writeable.

    Expected kwargs: file_paths, files_message_text, dirs_message_text,
    message_box
    """
    # Start deuplicated sets for interesting files and directories.
    writeable_files = set()
    writeable_dirs = set()

    for file_name in kwargs['file_paths']:

        # Ignore known not-vulnerable targets
        if file_name in NOT_VULN:
            continue

        # Is it a symlink? If so, get the real path and check permissions.
        # If it is broken, check permissions on the parent directory.
        if os.path.islink(file_name):
            target = os.readlink(file_name)

            # Some symlinks use relative path names. Find these and prepend
            # the directory name so we can investigate properly.
            if target[0] == '.':
                parent_dir = os.path.dirname(file_name)
                target = parent_dir + '/' + target

            if os.path.exists(target) and os.access(target, os.W_OK):
                writeable_files.add('{} -- symlink --> {}'
                                    .format(file_name, target))
            else:
                parent_dir = os.path.dirname(target)
                if os.access(parent_dir, os.W_OK):
                    writeable_dirs.add((file_name, target))

        # OK, not a symlink. Just check permissions.
        else:
            if os.access(file_name, os.W_OK):
                writeable_files.add(file_name)

    if writeable_files:
        # Print to console and log the interesting findings.
        tee("")
        tee(kwargs['files_message_text'], box=kwargs['message_box'])
        for item in writeable_files:
            tee("  {}".format(item))

    if writeable_dirs:
        # Print to console and log the interesting findings.
        tee("")
        tee(kwargs['dirs_message_text'], box=kwargs['message_box'])
        for item in writeable_dirs:
            tee("  {} --> {}".format(item[0], item[1]))

    if not writeable_files and not writeable_dirs:
        tee("")
        tee("No writeable targets. This is expected...",
            box='note')
def check_file_permissions ( * * kwargs ) : # Start deuplicated sets for interesting files and directories. writeable_files = set ( ) writeable_dirs = set ( ) for file_name in kwargs [ 'file_paths' ] : # Ignore known not-vulnerable targets if file_name in NOT_VULN : continue # Is it a symlink? If so, get the real path and check permissions. # If it is broken, check permissions on the parent directory. if os . path . islink ( file_name ) : target = os . readlink ( file_name ) # Some symlinks use relative path names. Find these and prepend # the directory name so we can investigate properly. if target [ 0 ] == '.' : parent_dir = os . path . dirname ( file_name ) target = parent_dir + '/' + target if os . path . exists ( target ) and os . access ( target , os . W_OK ) : writeable_files . add ( '{} -- symlink --> {}' . format ( file_name , target ) ) else : parent_dir = os . path . dirname ( target ) if os . access ( parent_dir , os . W_OK ) : writeable_dirs . add ( ( file_name , target ) ) # OK, not a symlink. Just check permissions. else : if os . access ( file_name , os . W_OK ) : writeable_files . add ( file_name ) if writeable_files : # Print to console and log the interesting findings. tee ( "" ) tee ( kwargs [ 'files_message_text' ] , box = kwargs [ 'message_box' ] ) for item in writeable_files : tee ( "  {}" . format ( item ) ) if writeable_dirs : # Print to console and log the interesting findings. tee ( "" ) tee ( kwargs [ 'dirs_message_text' ] , box = kwargs [ 'message_box' ] ) for item in writeable_dirs : tee ( "  {} --> {}" . format ( item [ 0 ] , item [ 1 ] ) ) if not writeable_files and not writeable_dirs : tee ( "" ) tee ( "No writeable targets. This is expected..." , box = 'note' )