1# -*- coding: utf-8 -*-
2'''
3 calc.py: DNS-based calculator
4
5 Copyright (c) 2009, Zdenek Vasicek (vasicek AT fit.vutbr.cz)
6                     Marek Vavrusa  (xvavru00 AT stud.fit.vutbr.cz)
7
8 This software is open source.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14    * Redistributions of source code must retain the above copyright notice,
15      this list of conditions and the following disclaimer.
16
17    * Redistributions in binary form must reproduce the above copyright notice,
18      this list of conditions and the following disclaimer in the documentation
19      and/or other materials provided with the distribution.
20
21    * Neither the name of the organization nor the names of its
22      contributors may be used to endorse or promote products derived from this
23      software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36'''
37
38#Try: dig @localhost 1*25._calc_.cz.
39
40def init(id, cfg): return True
41def deinit(id): return True
42def inform_super(id, qstate, superqstate, qdata): return True
43
44def operate(id, event, qstate, qdata):
45
46    if (event == MODULE_EVENT_NEW) or (event == MODULE_EVENT_PASS):
47
48        if qstate.qinfo.qname_str.endswith("._calc_.cz.") and not ("__" in qstate.qinfo.qname_str):
49            try:
50                # the second and third argument to eval attempt to restrict
51                # functions and variables available to stop code execution
52                # but it may not be safe either.  This is why __ substrings
53                # are excluded from evaluation.
54                res = eval(''.join(qstate.qinfo.qname_list[0:-3]),{"__builtins__":None},{})
55            except:
56                res = "exception"
57
58            msg = DNSMessage(qstate.qinfo.qname_str, RR_TYPE_TXT, RR_CLASS_IN, PKT_QR | PKT_RA | PKT_AA) #, 300)
59            msg.answer.append("%s 300 IN TXT \"%s\"" % (qstate.qinfo.qname_str,res))
60            if not msg.set_return_msg(qstate):
61               qstate.ext_state[id] = MODULE_ERROR
62               return True
63
64            qstate.return_rcode = RCODE_NOERROR
65            qstate.ext_state[id] = MODULE_FINISHED
66            return True
67
68        else:
69            #Pass on the unknown query to the iterator
70            qstate.ext_state[id] = MODULE_WAIT_MODULE
71            return True
72
73    elif event == MODULE_EVENT_MODDONE:
74        #the iterator has finished
75        qstate.ext_state[id] = MODULE_FINISHED
76        return True
77
78    log_err("pythonmod: Unknown event")
79    qstate.ext_state[id] = MODULE_ERROR
80    return True
81
82