13b6c3722Schristos# -*- coding: utf-8 -*- 23b6c3722Schristos''' 33b6c3722Schristos calc.py: DNS-based calculator 43b6c3722Schristos 53b6c3722Schristos Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz) 63b6c3722Schristos Marek Vavrusa (xvavru00 AT stud.fit.vutbr.cz) 73b6c3722Schristos 83b6c3722Schristos This software is open source. 93b6c3722Schristos 103b6c3722Schristos Redistribution and use in source and binary forms, with or without 113b6c3722Schristos modification, are permitted provided that the following conditions 123b6c3722Schristos are met: 133b6c3722Schristos 143b6c3722Schristos * Redistributions of source code must retain the above copyright notice, 153b6c3722Schristos this list of conditions and the following disclaimer. 163b6c3722Schristos 173b6c3722Schristos * Redistributions in binary form must reproduce the above copyright notice, 183b6c3722Schristos this list of conditions and the following disclaimer in the documentation 193b6c3722Schristos and/or other materials provided with the distribution. 203b6c3722Schristos 213b6c3722Schristos * Neither the name of the organization nor the names of its 223b6c3722Schristos contributors may be used to endorse or promote products derived from this 233b6c3722Schristos software without specific prior written permission. 243b6c3722Schristos 253b6c3722Schristos THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 263b6c3722Schristos "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 273b6c3722Schristos TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 283b6c3722Schristos PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 293b6c3722Schristos LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 303b6c3722Schristos CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 313b6c3722Schristos SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 323b6c3722Schristos INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 333b6c3722Schristos CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 343b6c3722Schristos ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 353b6c3722Schristos POSSIBILITY OF SUCH DAMAGE. 363b6c3722Schristos''' 373b6c3722Schristos 383b6c3722Schristos#Try: dig @localhost 1*25._calc_.cz. 393b6c3722Schristos 403b6c3722Schristosdef init(id, cfg): return True 413b6c3722Schristosdef deinit(id): return True 423b6c3722Schristosdef inform_super(id, qstate, superqstate, qdata): return True 433b6c3722Schristos 443b6c3722Schristosdef operate(id, event, qstate, qdata): 453b6c3722Schristos 463b6c3722Schristos if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS): 473b6c3722Schristos 48*01049ae6Schristos if qstate.qinfo.qname_str.endswith("._calc_.cz.") and not ("__" in qstate.qinfo.qname_str): 493b6c3722Schristos try: 50*01049ae6Schristos # the second and third argument to eval attempt to restrict 51*01049ae6Schristos # functions and variables available to stop code execution 52*01049ae6Schristos # but it may not be safe either. This is why __ substrings 53*01049ae6Schristos # are excluded from evaluation. 54*01049ae6Schristos res = eval(''.join(qstate.qinfo.qname_list[0:-3]),{"__builtins__":None},{}) 553b6c3722Schristos except: 563b6c3722Schristos res = "exception" 573b6c3722Schristos 583b6c3722Schristos msg = DNSMessage(qstate.qinfo.qname_str, RR_TYPE_TXT, RR_CLASS_IN, PKT_QR | PKT_RA | PKT_AA) #, 300) 593b6c3722Schristos msg.answer.append("%s 300 IN TXT \"%s\"" % (qstate.qinfo.qname_str,res)) 603b6c3722Schristos if not msg.set_return_msg(qstate): 613b6c3722Schristos qstate.ext_state[id] = MODULE_ERROR 623b6c3722Schristos return True 633b6c3722Schristos 643b6c3722Schristos qstate.return_rcode = RCODE_NOERROR 653b6c3722Schristos qstate.ext_state[id] = MODULE_FINISHED 663b6c3722Schristos return True 673b6c3722Schristos 683b6c3722Schristos else: 693b6c3722Schristos #Pass on the unknown query to the iterator 703b6c3722Schristos qstate.ext_state[id] = MODULE_WAIT_MODULE 713b6c3722Schristos return True 723b6c3722Schristos 733b6c3722Schristos elif event == MODULE_EVENT_MODDONE: 743b6c3722Schristos #the iterator has finished 753b6c3722Schristos qstate.ext_state[id] = MODULE_FINISHED 763b6c3722Schristos return True 773b6c3722Schristos 783b6c3722Schristos log_err("pythonmod: Unknown event") 793b6c3722Schristos qstate.ext_state[id] = MODULE_ERROR 803b6c3722Schristos return True 813b6c3722Schristos 82