#!/usr/bin/env python

# This script allows the adminstrator to toggle SmartConnect
# support for DNS queries via TCP.  By disabling TCP support a
# problem where security scanners overwhelm SmartConnect when
# scanning a cluster.  Causing a DU when SmartConnect cannot
# respond in time.

import os
from optparse import OptionParser
import isi.net.flexnet as flx

def get_dns_tcp_enabled():
    config = flx.config_try_load()
    retval = config.dns_tcp_enabled
    config.close()
    return retval

def set_dns_tcp_enabled(value):
    config = flx.config_open_exclusive()
    config.dns_tcp_enabled = value
    config.write(False)
    config.close()

if __name__ == '__main__':

    try:
        usage = 'usage: %prog []'
        parser = OptionParser(usage)
        parser.set_defaults(tcp_enabled=False, display=False)
        parser.add_option('-e', '--enable', dest='tcp_enabled',
                          help='Enable TCP support in SmartConnect',
                          action='store_true')
        parser.add_option('-d', '--disable', dest='tcp_enabled',
                          help='Disable TCP support in SmartConnect (default action)',
                          action='store_false')
        parser.add_option('-p', '--print', dest='display',
                          help='Print the current SmartConnect TCP support state',
                          action='store_true')
        (options, args) = parser.parse_args()

        if options.display:
            print '%r' % get_dns_tcp_enabled()
        else:
            set_dns_tcp_enabled(options.tcp_enabled)
    except Exception as e:
        print '%s failed: %s' % (os.path.basename(__file__), str(e))
