<s> import os <EOL> import sys <EOL> import re <EOL> import time <EOL> import web <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . db . access . machine import findbyhost1 <EOL> from karesansui . lib . utils import get_ifconfig_info , dict_ksort , is_param <EOL> from karesansui . lib . conf import read_conf , write_conf <EOL> from karesansui . lib . iptables import iptables_lint_contents <EOL> from karesansui . lib . checker import Checker , CHECK_EMPTY , CHECK_VALID <EOL> def validates_iptables_save ( obj , host ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . iptables_save , <EOL> CHECK_EMPTY | CHECK_VALID , <EOL> '<STR_LIT>' , <EOL> None , <EOL> None , <EOL> ) and check <EOL> if check is True : <EOL> ret = iptables_lint_contents ( obj . input . iptables_save , obj , host ) <EOL> if str ( ret ) != "<STR_LIT>" : <EOL> check = False <EOL> checker . add_error ( ret ) <EOL> """<STR_LIT>""" <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> class HostBy1Iptables ( Rest ) : <EOL> @ auth <EOL> def _GET ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> self . view . host_id = host_id <EOL> self . view . current = get_ifconfig_info ( ) <EOL> modules = [ "<STR_LIT>" ] <EOL> host = findbyhost1 ( self . orm , host_id ) <EOL> dop = read_conf ( modules , self , host ) <EOL> if dop is False : <EOL> return web . internalerror ( '<STR_LIT>' ) <EOL> config = dop . get ( "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> status = dop . get ( "<STR_LIT>" , [ "<STR_LIT:status>" ] ) <EOL> lint = dop . get ( "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> policies = { } <EOL> for _aline in status : <EOL> m = re . match ( "<STR_LIT>" , _aline . rstrip ( ) ) <EOL> if m : <EOL> table = m . group ( "<STR_LIT>" ) <EOL> policies [ table ] = { } <EOL> else : <EOL> m = re . match ( "<STR_LIT>" , _aline . rstrip ( ) ) <EOL> if m : <EOL> chain = m . group ( "<STR_LIT>" ) <EOL> policy = m . group ( "<STR_LIT>" ) <EOL> policies [ table ] [ chain ] = policy <EOL> self . view . config = "<STR_LIT:\n>" . join ( config ) <EOL> self . view . status = "<STR_LIT:\n>" . join ( status ) <EOL> self . view . lint = lint <EOL> self . view . policies = policies <EOL> self . view . result_js = "<STR_LIT>" <EOL> return True <EOL> @ auth <EOL> def _PUT ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> host = findbyhost1 ( self . orm , host_id ) <EOL> if not validates_iptables_save ( self , host ) : <EOL> return web . badrequest ( self . view . alert ) <EOL> from karesansui . lib . dict_op import DictOp <EOL> dop = DictOp ( ) <EOL> dop . addconf ( "<STR_LIT>" , { } ) <EOL> dop . set ( "<STR_LIT>" , [ "<STR_LIT>" ] , self . input . iptables_save . split ( "<STR_LIT:\r\n>" ) ) <EOL> retval = write_conf ( dop , self , host ) <EOL> if retval is False : <EOL> return web . internalerror ( '<STR_LIT>' ) <EOL> return web . accepted ( url = web . ctx . path ) <EOL> urls = ( <EOL> '<STR_LIT>' , HostBy1Iptables , <EOL> ) </s>
