root/source4/librpc/rpc/pyrpc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. py_dcerpc_run_function
  2. py_dcerpc_call_wrapper
  3. PyInterface_AddNdrRpcMethods
  4. PyString_AsGUID
  5. ndr_syntax_from_py_object
  6. py_iface_server_name
  7. py_ndr_syntax_id
  8. py_iface_abstract_syntax
  9. py_iface_transfer_syntax
  10. PyErr_SetDCERPCStatus
  11. py_iface_request
  12. py_iface_alter_context
  13. py_dcerpc_interface_init_helper
  14. dcerpc_interface_dealloc
  15. dcerpc_interface_new
  16. initbase

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    Samba utility functions
   4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
   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 3 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
  17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18 */
  19 
  20 #include "includes.h"
  21 #include <Python.h>
  22 #include <structmember.h>
  23 #include "librpc/rpc/pyrpc.h"
  24 #include "librpc/rpc/dcerpc.h"
  25 #include "lib/events/events.h"
  26 #include "param/pyparam.h"
  27 
  28 #ifndef Py_RETURN_NONE
  29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  30 #endif
  31 
  32 static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface,
     /* [<][>][^][v][top][bottom][index][help] */
  33                                         const struct PyNdrRpcMethodDef *md,
  34                                         PyObject *args, PyObject *kwargs)
  35 {
  36         TALLOC_CTX *mem_ctx;
  37         NTSTATUS status;
  38         void *r;
  39         PyObject *result = Py_None;
  40 
  41         if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
  42                 PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
  43                 return NULL;
  44         }
  45 
  46         mem_ctx = talloc_new(NULL);
  47         if (mem_ctx == NULL) {
  48                 PyErr_NoMemory();
  49                 return NULL;
  50         }
  51 
  52         r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
  53         if (r == NULL) {
  54                 PyErr_NoMemory();
  55                 return NULL;
  56         }
  57 
  58         if (!md->pack_in_data(args, kwargs, r)) {
  59                 talloc_free(mem_ctx);
  60                 return NULL;
  61         }
  62 
  63         status = md->call(iface->pipe, mem_ctx, r);
  64         if (NT_STATUS_IS_ERR(status)) {
  65                 PyErr_SetDCERPCStatus(iface->pipe, status);
  66                 talloc_free(mem_ctx);
  67                 return NULL;
  68         }
  69 
  70         result = md->unpack_out_data(r);
  71 
  72         talloc_free(mem_ctx);
  73         return result;
  74 }
  75 
  76 static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
  77 {       
  78         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
  79         const struct PyNdrRpcMethodDef *md = (const struct PyNdrRpcMethodDef *)wrapped;
  80 
  81         return py_dcerpc_run_function(iface, md, args, kwargs);
  82 }
  83 
  84 
  85 bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
     /* [<][>][^][v][top][bottom][index][help] */
  86 {
  87         int i;
  88         for (i = 0; mds[i].name; i++) {
  89                 PyObject *ret;
  90                 struct wrapperbase *wb = (struct wrapperbase *)calloc(sizeof(struct wrapperbase), 1);
  91 
  92                 wb->name = discard_const_p(char, mds[i].name);
  93                 wb->flags = PyWrapperFlag_KEYWORDS;
  94                 wb->wrapper = (wrapperfunc)py_dcerpc_call_wrapper;
  95                 wb->doc = discard_const_p(char, mds[i].doc);
  96                 
  97                 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
  98 
  99                 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name, 
 100                                      (PyObject *)ret);
 101         }
 102 
 103         return true;
 104 }
 105 
 106 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
     /* [<][>][^][v][top][bottom][index][help] */
 107 {
 108         NTSTATUS status;
 109         status = GUID_from_string(PyString_AsString(object), uuid);
 110         if (NT_STATUS_IS_ERR(status)) {
 111                 PyErr_SetNTSTATUS(status);
 112                 return false;
 113         }
 114         return true;
 115 }
 116 
 117 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
     /* [<][>][^][v][top][bottom][index][help] */
 118 {
 119         ZERO_STRUCTP(syntax_id);
 120 
 121         if (PyString_Check(object)) {
 122                 return PyString_AsGUID(object, &syntax_id->uuid);
 123         } else if (PyTuple_Check(object)) {
 124                 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
 125                         PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
 126                         return false;
 127                 }
 128 
 129                 if (!PyString_Check(PyTuple_GetItem(object, 0))) {
 130                         PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
 131                         return false;
 132                 }
 133 
 134                 if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) 
 135                         return false;
 136 
 137                 if (!PyInt_Check(PyTuple_GetItem(object, 1))) {
 138                         PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
 139                         return false;
 140                 }
 141 
 142                 syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1));
 143                 return true;
 144         }
 145 
 146         PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
 147         return false;
 148 }       
 149 
 150 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
     /* [<][>][^][v][top][bottom][index][help] */
 151 {
 152         const char *server_name;
 153         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
 154         
 155         server_name = dcerpc_server_name(iface->pipe);
 156         if (server_name == NULL)
 157                 Py_RETURN_NONE;
 158 
 159         return PyString_FromString(server_name);
 160 }
 161 
 162 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
     /* [<][>][^][v][top][bottom][index][help] */
 163 {
 164         PyObject *ret;
 165         char *uuid_str;
 166 
 167         uuid_str = GUID_string(NULL, &syntax_id->uuid);
 168         if (uuid_str == NULL)
 169                 return NULL;
 170 
 171         ret = Py_BuildValue("(s,i)", uuid_str, syntax_id->if_version);
 172 
 173         talloc_free(uuid_str);
 174 
 175         return ret;
 176 }
 177 
 178 static PyObject *py_iface_abstract_syntax(PyObject *obj, void *closure)
     /* [<][>][^][v][top][bottom][index][help] */
 179 {
 180         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
 181 
 182         return py_ndr_syntax_id(&iface->pipe->syntax);
 183 }
 184 
 185 static PyObject *py_iface_transfer_syntax(PyObject *obj, void *closure)
     /* [<][>][^][v][top][bottom][index][help] */
 186 {
 187         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
 188 
 189         return py_ndr_syntax_id(&iface->pipe->transfer_syntax);
 190 }
 191 
 192 static PyGetSetDef dcerpc_interface_getsetters[] = {
 193         { discard_const_p(char, "server_name"), py_iface_server_name, NULL,
 194           discard_const_p(char, "name of the server, if connected over SMB") },
 195         { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax, NULL, 
 196           discard_const_p(char, "syntax id of the abstract syntax") },
 197         { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax, NULL, 
 198           discard_const_p(char, "syntax id of the transfersyntax") },
 199         { NULL }
 200 };
 201 
 202 static PyMemberDef dcerpc_interface_members[] = {
 203         { discard_const_p(char, "request_timeout"), T_INT, 
 204           offsetof(struct dcerpc_pipe, request_timeout), 0,
 205           discard_const_p(char, "request timeout, in seconds") },
 206         { NULL }
 207 };
 208 
 209 void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
     /* [<][>][^][v][top][bottom][index][help] */
 210 {
 211         if (p != NULL && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
 212                 const char *errstr = dcerpc_errstr(NULL, p->last_fault_code);
 213                 PyErr_SetObject(PyExc_RuntimeError, 
 214                         Py_BuildValue("(i,s)", p->last_fault_code,
 215                                       errstr));
 216         } else {
 217                 PyErr_SetNTSTATUS(status);
 218         }
 219 }
 220 
 221 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
 222 {
 223         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
 224         int opnum;
 225         DATA_BLOB data_in, data_out;
 226         NTSTATUS status;
 227         char *in_data;
 228         int in_length;
 229         PyObject *ret;
 230         PyObject *object = NULL;
 231         struct GUID object_guid;
 232         TALLOC_CTX *mem_ctx = talloc_new(NULL);
 233         const char *kwnames[] = { "opnum", "data", "object", NULL };
 234 
 235         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request", 
 236                 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
 237                 return NULL;
 238         }
 239 
 240         data_in.data = (uint8_t *)talloc_memdup(mem_ctx, in_data, in_length);
 241         data_in.length = in_length;
 242 
 243         ZERO_STRUCT(data_out);
 244 
 245         if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
 246                 return NULL;
 247         }
 248 
 249         status = dcerpc_request(iface->pipe, object?&object_guid:NULL,
 250                                 opnum, false, mem_ctx, &data_in, &data_out);
 251 
 252         if (NT_STATUS_IS_ERR(status)) {
 253                 PyErr_SetDCERPCStatus(iface->pipe, status);
 254                 talloc_free(mem_ctx);
 255                 return NULL;
 256         }
 257 
 258         ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
 259 
 260         talloc_free(mem_ctx);
 261         return ret;
 262 }
 263 
 264 static PyObject *py_iface_alter_context(PyObject *self, PyObject *args, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
 265 {
 266         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
 267         NTSTATUS status;
 268         const char *kwnames[] = { "abstract_syntax", "transfer_syntax", NULL };
 269         PyObject *py_abstract_syntax = Py_None, *py_transfer_syntax = Py_None;
 270         struct ndr_syntax_id abstract_syntax, transfer_syntax;
 271 
 272         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:alter_context", 
 273                 discard_const_p(char *, kwnames), &py_abstract_syntax,
 274                 &py_transfer_syntax)) {
 275                 return NULL;
 276         }
 277 
 278         if (!ndr_syntax_from_py_object(py_abstract_syntax, &abstract_syntax))
 279                 return NULL;
 280 
 281         if (py_transfer_syntax == Py_None) {
 282                 transfer_syntax = ndr_transfer_syntax;
 283         } else {
 284                 if (!ndr_syntax_from_py_object(py_transfer_syntax, 
 285                                                &transfer_syntax))
 286                         return NULL;
 287         }
 288 
 289         status = dcerpc_alter_context(iface->pipe, iface->pipe, &abstract_syntax, 
 290                                       &transfer_syntax);
 291 
 292         if (NT_STATUS_IS_ERR(status)) {
 293                 PyErr_SetDCERPCStatus(iface->pipe, status);
 294                 return NULL;
 295         }
 296 
 297         Py_RETURN_NONE;
 298 }
 299 
 300 PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs, const struct ndr_interface_table *table)
     /* [<][>][^][v][top][bottom][index][help] */
 301 {
 302         dcerpc_InterfaceObject *ret;
 303         const char *binding_string;
 304         struct cli_credentials *credentials;
 305         struct loadparm_context *lp_ctx = NULL;
 306         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
 307         TALLOC_CTX *mem_ctx = NULL;
 308         struct tevent_context *event_ctx;
 309         NTSTATUS status;
 310 
 311         const char *kwnames[] = {
 312                 "binding", "lp_ctx", "credentials", "basis_connection", NULL
 313         };
 314         extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
 315 
 316         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {
 317                 return NULL;
 318         }
 319 
 320         lp_ctx = lp_from_py_object(py_lp_ctx);
 321         if (lp_ctx == NULL) {
 322                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
 323                 return NULL;
 324         }
 325 
 326         status = dcerpc_init(lp_ctx);
 327         if (!NT_STATUS_IS_OK(status)) {
 328                 PyErr_SetNTSTATUS(status);
 329                 return NULL;
 330         }
 331         credentials = cli_credentials_from_py_object(py_credentials);
 332         if (credentials == NULL) {
 333                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
 334                 return NULL;
 335         }
 336         ret = PyObject_New(dcerpc_InterfaceObject, type);
 337 
 338         event_ctx = event_context_init(mem_ctx);
 339 
 340         if (py_basis != Py_None) {
 341                 struct dcerpc_pipe *base_pipe;
 342 
 343                 if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {
 344                         PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection");
 345                         talloc_free(mem_ctx);
 346                         return NULL;
 347                 }
 348 
 349                 base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;
 350 
 351                 status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
 352         } else {
 353                 status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, 
 354                              table, credentials, event_ctx, lp_ctx);
 355         }
 356         if (NT_STATUS_IS_ERR(status)) {
 357                 PyErr_SetNTSTATUS(status);
 358                 talloc_free(mem_ctx);
 359                 return NULL;
 360         }
 361 
 362         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
 363         return (PyObject *)ret;
 364 }
 365 
 366 static PyMethodDef dcerpc_interface_methods[] = {
 367         { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
 368         { "alter_context", (PyCFunction)py_iface_alter_context, METH_VARARGS|METH_KEYWORDS, "S.alter_context(syntax)\nChange to a different interface" },
 369         { NULL, NULL, 0, NULL },
 370 };
 371 
 372 
 373 static void dcerpc_interface_dealloc(PyObject* self)
     /* [<][>][^][v][top][bottom][index][help] */
 374 {
 375         dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
 376         talloc_free(interface->pipe);
 377         PyObject_Del(self);
 378 }
 379 
 380 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
 381 {
 382         dcerpc_InterfaceObject *ret;
 383         const char *binding_string;
 384         struct cli_credentials *credentials;
 385         struct loadparm_context *lp_ctx = NULL;
 386         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
 387         TALLOC_CTX *mem_ctx = NULL;
 388         struct tevent_context *event_ctx;
 389         NTSTATUS status;
 390 
 391         PyObject *syntax, *py_basis = Py_None;
 392         const char *kwnames[] = {
 393                 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
 394         };
 395         extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
 396         struct ndr_interface_table *table;
 397 
 398         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) {
 399                 return NULL;
 400         }
 401 
 402         lp_ctx = lp_from_py_object(py_lp_ctx);
 403         if (lp_ctx == NULL) {
 404                 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
 405                 return NULL;
 406         }
 407 
 408         credentials = cli_credentials_from_py_object(py_credentials);
 409         if (credentials == NULL) {
 410                 PyErr_SetString(PyExc_TypeError, "Expected credentials");
 411                 return NULL;
 412         }
 413         ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
 414 
 415         event_ctx = s4_event_context_init(mem_ctx);
 416 
 417         /* Create a dummy interface table struct. TODO: In the future, we should rather just allow 
 418          * connecting without requiring an interface table.
 419          */
 420 
 421         table = talloc_zero(mem_ctx, struct ndr_interface_table);
 422 
 423         if (table == NULL) {
 424                 PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
 425                 return NULL;
 426         }
 427 
 428         if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
 429                 return NULL;
 430         }
 431 
 432         ret->pipe = NULL;
 433 
 434         if (py_basis != Py_None) {
 435                 struct dcerpc_pipe *base_pipe;
 436 
 437                 if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {
 438                         PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection");
 439                         talloc_free(mem_ctx);
 440                         return NULL;
 441                 }
 442 
 443                 base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;
 444 
 445                 status = dcerpc_secondary_context(base_pipe, &ret->pipe, 
 446                                      table);
 447                 ret->pipe = talloc_steal(NULL, ret->pipe);
 448         } else {
 449                 status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, 
 450                              table, credentials, event_ctx, lp_ctx);
 451         }
 452 
 453         if (NT_STATUS_IS_ERR(status)) {
 454                 PyErr_SetDCERPCStatus(ret->pipe, status);
 455                 talloc_free(mem_ctx);
 456                 return NULL;
 457         }
 458         ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
 459         return (PyObject *)ret;
 460 }
 461 
 462 PyTypeObject dcerpc_InterfaceType = {
 463         PyObject_HEAD_INIT(NULL) 0,
 464         .tp_name = "dcerpc.ClientConnection",
 465         .tp_basicsize = sizeof(dcerpc_InterfaceObject),
 466         .tp_dealloc = dcerpc_interface_dealloc,
 467         .tp_getset = dcerpc_interface_getsetters,
 468         .tp_members = dcerpc_interface_members,
 469         .tp_methods = dcerpc_interface_methods,
 470         .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
 471 "\n"
 472 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
 473 "syntax should be a tuple with a GUID and version number of an interface\n"
 474 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
 475 "credentials should be a credentials.Credentials object.\n\n",
 476         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
 477         .tp_new = dcerpc_interface_new,
 478 };
 479 
 480 void initbase(void)
     /* [<][>][^][v][top][bottom][index][help] */
 481 {
 482         PyObject *m;
 483 
 484         if (PyType_Ready(&dcerpc_InterfaceType) < 0)
 485                 return;
 486 
 487         m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
 488         if (m == NULL)
 489                 return;
 490 
 491         Py_INCREF((PyObject *)&dcerpc_InterfaceType);
 492         PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
 493 }

/* [<][>][^][v][top][bottom][index][help] */