# Buggy code
def need_deployment():
    """
    Salt thin needs to be deployed - prep the target directory and emit the
    delimiter and exit code that signals a required deployment.
    """
    if os.path.exists(OPTIONS.saltdir):
        shutil.rmtree(OPTIONS.saltdir)
    old_umask = os.umask(0o077)  # pylint: disable=blacklisted-function
    try:
        os.makedirs(OPTIONS.saltdir)
    finally:
        os.umask(old_umask)  # pylint: disable=blacklisted-function
    # Verify perms on saltdir
    if not is_windows():
        euid = os.geteuid()
        dstat = os.stat(OPTIONS.saltdir)
        if dstat.st_uid != euid:
            # Attack detected, try again
            need_deployment()
        if dstat.st_mode != 16832:
            # Attack detected
            need_deployment()
        # If SUDOing then also give the super user group write permissions
        sudo_gid = os.environ.get("SUDO_GID")
        if sudo_gid:
            try:
                os.chown(OPTIONS.saltdir, -1, int(sudo_gid))
                stt = os.stat(OPTIONS.saltdir)
                os.chmod(
                    OPTIONS.saltdir,
                    stt.st_mode | stat.S_IWGRP | stat.S_IRGRP | stat.S_IXGRP,
                )
            except OSError:
                sys.stdout.write(
                    "\n\nUnable to set permissions on thin directory.\nIf sudo_user is set "
                    "and is not root, be certain the user is in the same group\nas the login user"
                )
                sys.exit(1)

    # Delimiter emitted on stdout *only* to indicate shim message to master.
    sys.stdout.write("{0}\ndeploy\n".format(OPTIONS.delimiter))
    sys.exit(EX_THIN_DEPLOY)


# Traceback (RuntimeError)
root@salt:/root > salt-ssh aixhost mount.active
aixhost:
----------
retcode:
1
stderr:
/bin/sh[14]: python3:  not found.
/bin/sh[14]: python27:  not found.
WARNING: Unable to locate current thin  version: /tmp/.root_3664b7_salt/version.
Traceback (most recent call last):
File "<string>", line 141, in <module>
File "<string>", line 244, in <module>
File "<string>", line 174, in main
File "<string>", line 80, in need_deployment
File "<string>", line 80, in need_deployment
File "<string>", line 80, in need_deployment          ....
....
....
File "<string>", line 80, in need_deployment          ....
File "<string>", line 80, in need_deployment          ....
File "<string>", line 68, in need_deployment
File "/opt/freeware/lib/python2.7/shutil.py", line 228, in rmtree
if os.path.islink(path):
File "/usr/bin/../../opt/freeware/lib/python2.7/posixpath.py", line 145, in islink
return stat.S_ISLNK(st.st_mode)
File "/usr/bin/../../opt/freeware/lib/python2.7/stat.py", line 56, in S_ISLNK
return S_IFMT(mode) == S_IFLNK
RuntimeError: maximum recursion depth exceeded    stdout:
/usr/bin/scp

# Fixed code
def need_deployment():
    """
    Salt thin needs to be deployed - prep the target directory and emit the
    delimiter and exit code that signals a required deployment.
    """
    if os.path.exists(OPTIONS.saltdir):
        shutil.rmtree(OPTIONS.saltdir)
    old_umask = os.umask(0o077)  # pylint: disable=blacklisted-function
    try:
        os.makedirs(OPTIONS.saltdir)
    finally:
        os.umask(old_umask)  # pylint: disable=blacklisted-function
    # Verify perms on saltdir
    if not is_windows():
        euid = os.geteuid()
        dstat = os.stat(OPTIONS.saltdir)
        if dstat.st_uid != euid:
            # Attack detected, try again
            need_deployment()
        # AIX has non-POSIX bit 0o240700, isolate to 0o40700
        if dstat.st_mode & ~65536 != 16832:
            # Attack detected
            need_deployment()
        # If SUDOing then also give the super user group write permissions
        sudo_gid = os.environ.get("SUDO_GID")
        if sudo_gid:
            try:
                os.chown(OPTIONS.saltdir, -1, int(sudo_gid))
                stt = os.stat(OPTIONS.saltdir)
                os.chmod(
                    OPTIONS.saltdir,
                    stt.st_mode | stat.S_IWGRP | stat.S_IRGRP | stat.S_IXGRP,
                )
            except OSError:
                sys.stdout.write(
                    "\n\nUnable to set permissions on thin directory.\nIf sudo_user is set "
                    "and is not root, be certain the user is in the same group\nas the login user"
                )
                sys.exit(1)

    # Delimiter emitted on stdout *only* to indicate shim message to master.
    sys.stdout.write("{0}\ndeploy\n".format(OPTIONS.delimiter))
    sys.exit(EX_THIN_DEPLOY)