"Fossies" - the Fresh Open Source Software Archive

Member "zorp-7.0.4/lib/pycore.cc" (28 Oct 2019, 10440 Bytes) of package /linux/privat/zorp-7.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pycore.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 7.0.3_vs_7.0.4.

    1 /***************************************************************************
    2  *
    3  * Copyright (c) 2000-2015 BalaBit IT Ltd, Budapest, Hungary
    4  * Copyright (c) 2015-2018 BalaSys IT Ltd, Budapest, Hungary
    5  *
    6  * This program is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License along
   17  * with this program; if not, write to the Free Software Foundation, Inc.,
   18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   19  *
   20  *
   21  ***************************************************************************/
   22 
   23 /*
   24  * this module implements the interface with python
   25  */
   26 #include <zorp/pycore.h>
   27 #include <zorpll/log.h>
   28 #include <zorpll/io.h>
   29 #include <zorp/policy.h>
   30 #include <zorpll/streamfd.h>
   31 #include <zorpll/cap.h>
   32 #include <zorp/szig.h>
   33 #include <zorpll/io.h>
   34 #include <zorp/proxystack.h>
   35 
   36 #include <zorp/pystream.h>
   37 #include <zorp/pysockaddr.h>
   38 
   39 
   40 #include <netdb.h>
   41 
   42 PyObject *PyExc_LicenseException;
   43 
   44 /* exported python methods */
   45 
   46 /**
   47  * z_py_log:
   48  * @self: Python self argument
   49  * @args: Python args tuple
   50  *
   51  * Called by Python to send a message to the event log. There are several
   52  * alternative invocations of this function:
   53  *
   54  *   def log(class, verbosity, msg)
   55  *   def log(session_id, class, verbosity, logformat, args)
   56  *
   57  * Returns: Py_None
   58  **/
   59 static PyObject *
   60 z_py_log(PyObject * /* self */, PyObject *args)
   61 {
   62   unsigned long verbosity;
   63   char *class_, *msg;
   64   PyObject *py_session_id, *log_fmt, *log_args, *log_msg = NULL;
   65   gchar *session_id;
   66 
   67   if (!PyTuple_Check(args))
   68     {
   69       PyErr_SetString(PyExc_TypeError, "args must be a tuple");
   70       return NULL;
   71     }
   72   if (PyTuple_Size(args) == 3)
   73     {
   74       if (!PyArg_ParseTuple(args, "sis", &class_, &verbosity, &msg))
   75         return NULL;
   76       session_id = NULL;
   77     }
   78   else
   79     {
   80       log_args = NULL;
   81       if (!PyArg_ParseTuple(args, "OsiO|O", &py_session_id, &class_, &verbosity, &log_fmt, &log_args))
   82         return NULL;
   83 
   84       if (!z_log_enabled(class_, verbosity))
   85         {
   86           return z_policy_none_ref();
   87         }
   88 
   89       /* convert session ID */
   90       if (py_session_id == z_policy_none)
   91         {
   92           session_id = NULL;
   93         }
   94       else if (PyString_Check(py_session_id))
   95         {
   96           session_id = PyString_AsString(py_session_id);
   97         }
   98       else
   99         {
  100           PyErr_SetString(PyExc_TypeError, "Session ID must be string or None");
  101           return NULL;
  102         }
  103 
  104       if (!PyString_Check(log_fmt))
  105         {
  106           PyErr_SetString(PyExc_TypeError, "Format must be string");
  107           return NULL;
  108         }
  109 
  110       if (log_args != NULL && log_args != z_policy_none)
  111         {
  112           log_msg = PyString_Format(log_fmt, log_args);
  113           if (!log_msg)
  114             {
  115               return NULL;
  116             }
  117           msg = PyString_AsString(log_msg);
  118         }
  119       else
  120         {
  121           msg = PyString_AsString(log_fmt);
  122         }
  123     }
  124 
  125   /*NOLOG*/
  126   z_log(session_id, class_, verbosity, "%s", msg);
  127   Py_XDECREF(log_msg);
  128 
  129   return z_policy_none_ref();
  130 }
  131 
  132 /*+
  133   +*/
  134 static PyObject *
  135 z_py_quit(PyObject * /* self */, PyObject *args)
  136 {
  137   int exit_code;
  138 
  139   z_enter();
  140 
  141   if (!PyArg_ParseTuple(args, "i", &exit_code))
  142     z_return(NULL);
  143   z_main_loop_quit(exit_code);
  144 
  145   z_return(z_policy_none_ref());
  146 }
  147 
  148 
  149 static PyObject *
  150 z_py_stream_pair(PyObject * /* self */, PyObject *args)
  151 {
  152   int domain, type, proto = 0;
  153   int result[2];
  154   ZStream *streams[2];
  155   PyObject *pystreams[2], *res;
  156 
  157   z_enter();
  158   if (!PyArg_ParseTuple(args, "ii|i", &domain, &type, &proto))
  159     z_return(NULL);
  160   if (socketpair(domain, type, proto, result) == -1)
  161     {
  162       PyErr_SetString(PyExc_IOError, "I/O error during socketpair.");
  163       z_return(NULL);
  164     }
  165 
  166   streams[0] = z_stream_fd_new(result[0], "streamPair/A");
  167   streams[1] = z_stream_fd_new(result[1], "streamPair/B");
  168 
  169   pystreams[0] = z_policy_stream_new(streams[0]);
  170   pystreams[1] = z_policy_stream_new(streams[1]);
  171 
  172   z_stream_unref(streams[0]);
  173   z_stream_unref(streams[1]);
  174 
  175   res = z_policy_var_build("(OO)", pystreams[0], pystreams[1]);
  176   z_policy_var_unref(pystreams[0]);
  177   z_policy_var_unref(pystreams[1]);
  178   z_return(res);
  179 }
  180 
  181 static PyObject *
  182 z_py_get_instance_id(PyObject * /* self */, PyObject *args)
  183 {
  184   static GHashTable *instance_ids = NULL;
  185   gint *value;
  186   gchar *service_name;
  187 
  188   if (!PyArg_Parse(args, "(s)", &service_name))
  189     return NULL;
  190   if (instance_ids == NULL)
  191     instance_ids = g_hash_table_new(g_str_hash, g_str_equal);
  192 
  193   value = static_cast<gint *>(g_hash_table_lookup(instance_ids, service_name));
  194 
  195   if (!value)
  196     {
  197       value = g_new(gint, 1);
  198       *value = 0;
  199       g_hash_table_insert(instance_ids, g_strdup(service_name), value);
  200     }
  201   else
  202     {
  203       (*value)++;
  204     }
  205   return PyInt_FromLong(*value);
  206 }
  207 
  208 
  209 static PyObject *
  210 z_py_szig_event(PyObject * /* self */, PyObject *args)
  211 {
  212   gint event;
  213   guint type;
  214   PyObject *value, *value_repr;
  215   ZSzigValue *sv;
  216   GTimeVal tv;
  217 
  218   z_enter();
  219 
  220   if (!PyArg_Parse(args, "(iO)", &event, &value) ||
  221       !PyArg_Parse(value, "(iO)", &type, &value_repr))
  222     z_return(NULL);
  223 
  224   switch (type)
  225     {
  226     case Z_SZIG_TYPE_LONG:
  227       if (!PyInt_Check(value_repr))
  228         {
  229           PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_LONG requires an integer argument");
  230           z_return(NULL);
  231         }
  232       sv = z_szig_value_new_long(PyInt_AsLong(value_repr));
  233       break;
  234 
  235     case Z_SZIG_TYPE_TIME:
  236       if (!PyArg_Parse(value_repr, "(ii)", &tv.tv_sec, &tv.tv_usec))
  237         z_return(NULL);
  238       sv = z_szig_value_new_time(&tv);
  239       break;
  240 
  241     case Z_SZIG_TYPE_STRING:
  242       if (!PyString_Check(value_repr))
  243         {
  244           PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_STRING requires a string argument");
  245           z_return(NULL);
  246         }
  247       sv = z_szig_value_new_string(PyString_AsString(value_repr));
  248       break;
  249 
  250     case Z_SZIG_TYPE_PROPS:
  251       {
  252         gchar *name;
  253         PyObject *dict;
  254         PyObject *key, *value;
  255         Z_PYTHON_SIZE_TYPE i;
  256 
  257         if (!PyArg_Parse(value_repr, "(sO)", &name, &dict))
  258           z_return(NULL);
  259         if (!PyDict_Check(dict))
  260           {
  261             PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_PROPS requires a mapping as 2nd argument");
  262             z_return(NULL);
  263           }
  264 
  265         sv = z_szig_value_new_props(name, NULL);
  266         i = 0;
  267         while (PyDict_Next(dict, &i, &key, &value))
  268           {
  269             if (PyString_Check(key))
  270               {
  271                 if (PyString_Check(value))
  272                   {
  273                     z_szig_value_add_prop(sv, PyString_AsString(key), z_szig_value_new_string(PyString_AsString(value)));
  274                   }
  275                 else if (PyInt_Check(value))
  276                   {
  277                     z_szig_value_add_prop(sv, PyString_AsString(key), z_szig_value_new_long(PyInt_AsLong(value)));
  278                   }
  279                 else
  280                   {
  281                     z_szig_value_free(sv, TRUE);
  282                     PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_PROPS requires a string->string or string->int mapping");
  283                     z_return(NULL);
  284                   }
  285               }
  286             else
  287               {
  288                 z_szig_value_free(sv, TRUE);
  289                 PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_PROPS cannot handle not string keys");
  290                 z_return(NULL);
  291               }
  292           }
  293       }
  294     break;
  295 
  296     case Z_SZIG_TYPE_CONNECTION_PROPS:
  297       {
  298         gchar *service;
  299         gint instance_id, sec_conn_id, related_id;
  300         PyObject *dict;
  301         PyObject *key, *value;
  302         Z_PYTHON_SIZE_TYPE i;
  303 
  304         if (!PyArg_Parse(value_repr, "(siiiO)", &service, &instance_id, &sec_conn_id, &related_id, &dict))
  305           z_return(NULL);
  306         if (!PyDict_Check(dict))
  307           {
  308             PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_CONNECTION_PROPS requires a mapping as 5th argument");
  309             z_return(NULL);
  310           }
  311 
  312         sv = z_szig_value_new_connection_props(service, instance_id, sec_conn_id, related_id, NULL);
  313         i = 0;
  314         while (PyDict_Next(dict, &i, &key, &value))
  315           {
  316             if (!PyString_Check(key) || !PyString_Check(value))
  317               {
  318                 z_szig_value_free(sv, TRUE);
  319                 PyErr_SetString(PyExc_ValueError, "Z_SZIG_TYPE_CONNECTION_PROPS requires a string->string mapping");
  320                 z_return(NULL);
  321               }
  322             z_szig_value_add_connection_prop(sv, PyString_AsString(key), PyString_AsString(value));
  323           }
  324       }
  325     break;
  326 
  327     default:
  328       PyErr_SetString(PyExc_ValueError, "Unknown SZIG type;");
  329       z_return(NULL);
  330     }
  331   z_szig_event(event, sv);
  332 
  333   z_return(z_policy_none_ref());
  334 }
  335 
  336 
  337 /**
  338  *  **/
  339 static PyObject *
  340 z_policy_notify_event(PyObject * /* self */, PyObject * /* args */)
  341 {
  342   return z_policy_none_ref();
  343 }
  344 
  345 /**
  346  * z_py_is_initial_policy_load:
  347  * @self: Python self argument
  348  * @args: Python args tuple
  349  *
  350  * Called by Python during policy load to determine whether
  351  * this is the initial policy load or a reload.
  352  *
  353  * Returns: PyInt TRUE during the initial policy load, FALSE otherwise
  354  */
  355 static PyObject *
  356 z_py_is_initial_policy_load(PyObject * /* self */, PyObject * /* args */)
  357 {
  358   return PyInt_FromLong(z_main_loop_is_initial_policy_load() ? TRUE : FALSE);
  359 }
  360 
  361 static PyMethodDef common_funcs[] =
  362 {
  363   { "log", z_py_log, METH_VARARGS, NULL },
  364 };
  365 
  366 static PyMethodDef zorp_funcs[] =
  367 {
  368   { "quit", z_py_quit, METH_VARARGS, NULL },
  369   { "streamPair", z_py_stream_pair, METH_VARARGS, NULL },
  370   { "getInstanceId", z_py_get_instance_id, METH_VARARGS, NULL },
  371   { "szigEvent", z_py_szig_event, METH_VARARGS, NULL },
  372   { "isInitialPolicyLoad", z_py_is_initial_policy_load, METH_NOARGS, NULL },
  373   { "notifyEvent", z_policy_notify_event, METH_VARARGS, NULL },
  374   { NULL, NULL, 0, NULL }
  375 };
  376 
  377 void
  378 z_py_zorp_common_init(void)
  379 {
  380   Py_InitModule("Zorp.Common", common_funcs);
  381 }
  382 
  383 void
  384 z_py_zorp_core_init(void)
  385 {
  386   Py_InitModule("Zorp.Zorp", zorp_funcs);
  387 }