#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Fix permissions on theargentimes.com website.

Due to a legal dispute, the staff of theargentimes.com had FTP access
to their web site, but not shell access. The web host was configured
such that PHP files that aren’t executable would refuse to
run. Unfortunately, upgrading WordPress results in replacing existing
.php files with other, non-executable .php files.

So this program walks the directory tree looking for non-executable
.php files and issues a nonstandard 'chmod 755' command over the FTP
connection, which the web host's FTP daemon happened to accept.

"""

import sys, ftplib, re, traceback

dirline = 'drwxr-xr-x    3 22608      uv0237           4096 Jun 20  2009 
Templates'
wpconfig_line = '-rwxr-xr-x    1 22608      uv0237           1715 Jun 19 10:15 
wp-config.php'
wpcomments_line = '-rw-r--r--    1 22608      uv0237           3926 Jun 19 
13:06 wp-comments-post.php'

filename = lambda line: line[62:]
assert filename(dirline) == 'Templates'
assert filename(wpconfig_line) == 'wp-config.php'

is_dir = lambda line: line.startswith('d')
assert is_dir(dirline)
assert not is_dir(wpconfig_line)

is_executable = lambda line: line[3] == 'x'
assert is_executable(dirline)
assert is_executable(wpconfig_line)
assert not is_executable(wpcomments_line)

def chmod_755(ftp, php_file):
    print "chmod 755", php_file
    try:
        ftp.sendcmd('chmod 755 ' + php_file)
    except ftplib.error_perm:
        traceback.print_exc()

def process_directory(ftp, dirname):
    print "handling", dirname
    if dirname == '//images':
        print "ignoring //images"
        return

    dirs = []
    php_files = []

    def handle_ls_line(ls_line):
        fname = dirname + '/' + filename(ls_line)
        if is_dir(ls_line):
            dirs.append(fname)
        elif fname.endswith('.php') and not is_executable(ls_line):
            php_files.append(fname)

    ftp.retrlines('LIST ' + dirname, handle_ls_line)
    for php_file in php_files:
        chmod_755(ftp, php_file)
    for subdir in dirs:
        process_directory(ftp, subdir)

def main(password):
    ftp = ftplib.FTP('theargentimes.com')
    ftp.login('theargentimes.com_comercial', password)
    process_directory(ftp, '/')

if __name__ == '__main__':
    main(password=sys.argv[1])
-- 
To unsubscribe: http://lists.canonical.org/mailman/listinfo/kragen-hacks

Reply via email to