Skip to the content of the web site.

Linux | WhiteListProgram

#!/usr/bin/python
# whitelist copyright David Collie 2004
import sys
import getopt
import re

def write_prefs(filename, prefs, hosts, mode):
    if mode == 'disable':
        prefs['network.proxy.type'] = '0'
        prefs['network.proxy.no_proxies_on'] = '""'
    elif mode == 'append':
        prefs['network.proxy.type'] = '1'
        if prefs['network.proxy.no_proxies_on'] == '""':
            prefs['network.proxy.no_proxies_on'] = '"' + hosts + '"'
        else:
            prefs['network.proxy.no_proxies_on'] = prefs['network.proxy.no_proxi
es_on'][:-1] + ', ' + hosts + '"'
    elif mode == 'create':
        prefs['network.proxy.type'] = '1'
        prefs['network.proxy.no_proxies_on'] = '"' + hosts + '"'
    elif mode == 'website':
        prefs['browser.startup.homepage'] = '"' + hosts + '"'
        prefs['browser.throbber.url'] = '"' + hosts + '"'
    else:
 print 'ERROR incorrect mode passed to write_prefs: %s\n' %(mode)
 return 1

    try:
        fsock = open(filename, 'r')
        lines = fsock.readlines()
        fsock.close()
        fsock = open(filename, 'w')

        try:
            fsock.writelines(lines[:11]) # write initial comment
            for key, value in prefs.iteritems():
                output = 'user_pref("' + key + '", ' + value + ');\n'
                fsock.write(output)

        finally:
            fsock.close()
            return 0
    except IOError:
        print "ERROR opening file"
        return 2

def read_prefs(filename):
    # initialize default setting
    prefs = { 'network.proxy.http': '"This machine is a kiosk"',
              'network.proxy.http_port': '80',
'network.proxy.no_proxies_on': '""',
              'network.proxy.ssl': '"This machine is a kiosk"',
              'network.proxy.ssl_port': '443',
              'network.proxy.type': '0',
              'browser.startup.homepage': '"about:blank"',
              'browser.throbber.url': '"about:blank"' }

    try:
        fsock = open(filename, "r")
        try:
            current_prefs = fsock.readlines()
            # loop through prefs if they are proxy settings
            # read them into the prefs dictionary
            for pref in current_prefs:
                if pref[0] != 'u':
                    continue

                opt = re.findall('\(\".*\"\,', pref)
                opt = opt[0][2:-2] # strip ("opt",
                arg = re.findall(' .*', pref)
                arg = arg[0][1:-2] # strip ); and space

                prefs[opt] = arg
            else:
                pass

        finally:
            fsock.close()
            return 0
    except IOError:
        print "ERORR reading file"
        return 2

    return prefs

def usage():
    print "Usage: whiteplist.py [OPTIONS]... [FILE]"
    print "Set kiosk whitelist preferences"
    print "-d --disable\tdisable the kiosk whitelist"
    print "-a --append ...\tappend a host to the whitelst"
    print "-c --create ...\tcreate a list of hosts for the whitelist"
    print "-l --list\tlists the current hosts"
    print "-w --website\tset the homepage"
    print "-h --help\tprint this message"
    print "When specifiying hosts enclose them in double quotes"

def main(argv):
    hosts = ""
    mode = "disable"

    try:
        opts, args = getopt.getopt(argv, "da:c:hw:l", ["disable", "append=", "cr
eate=", "help", "website=",  "list"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-d", "--disable"):
            mode = "disable"
        elif opt in ("-a", "--append"):
            mode = "append"
            hosts = arg
        elif opt in ("-c", "--create"):
            mode = "create"
  hosts = arg
        elif opt in ("-l", "--list"):
            mode = "list"
        elif opt in ("-w", "--website"):
            mode = "website"
            hosts = arg
        else:
            usage()

    source = "".join(args)

    if source == "":
        usage()
    else:
        prefs = read_prefs(source)
        if prefs != 0: sys.exit(2)

        if mode != 'list':
            if write_prefs(source, prefs, hosts,  mode) != 0: sys.exit(3)
        else:
            hosts = prefs['network.proxy.no_proxies_on']
            hosts = hosts[1:-1]
 website = prefs['browser.throbber.url']
            website = website[1:-1]
            if hosts == '':
                print "No hosts set"
            else:
                print 'Allowed hosts: %s' %(hosts)

            if website == '':
                print "No homepage set"
            else:
                print 'Homepage: %s' %(website)

    sys.exit()

if __name__ == "__main__":
    main(sys.argv[1:])

-- DavidCollie - 13 Oct 2004