VERSION: Bump version number up to 4.1.9...
[Samba.git] / source4 / libnet / py_net.c
blob7981aad02257e8e876758121542e07c16f0723ec
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
6 Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <Python.h>
23 #include "includes.h"
24 #include <pyldb.h>
25 #include <pytalloc.h>
26 #include "libnet.h"
27 #include "auth/credentials/pycredentials.h"
28 #include "libcli/security/security.h"
29 #include "lib/events/events.h"
30 #include "param/pyparam.h"
31 #include "auth/gensec/gensec.h"
32 #include "librpc/rpc/pyrpc_util.h"
33 #include "libcli/resolve/resolve.h"
34 #include "libcli/finddc.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "py_net.h"
37 #include "librpc/rpc/pyrpc_util.h"
39 void initnet(void);
41 static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
43 struct libnet_Join_member r;
44 int _level = 0;
45 NTSTATUS status;
46 PyObject *result;
47 TALLOC_CTX *mem_ctx;
48 const char *kwnames[] = { "domain_name", "netbios_name", "level", "machinepass", NULL };
50 ZERO_STRUCT(r);
52 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssi|z:Join", discard_const_p(char *, kwnames),
53 &r.in.domain_name, &r.in.netbios_name,
54 &_level,
55 &r.in.account_pass)) {
56 return NULL;
58 r.in.level = _level;
60 mem_ctx = talloc_new(self->mem_ctx);
61 if (mem_ctx == NULL) {
62 PyErr_NoMemory();
63 return NULL;
66 status = libnet_Join_member(self->libnet_ctx, mem_ctx, &r);
67 if (NT_STATUS_IS_ERR(status)) {
68 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
69 talloc_free(mem_ctx);
70 return NULL;
73 result = Py_BuildValue("sss", r.out.join_password,
74 dom_sid_string(mem_ctx, r.out.domain_sid),
75 r.out.domain_name);
77 talloc_free(mem_ctx);
79 return result;
82 static const char py_net_join_member_doc[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
83 "Join the domain with the specified name.";
85 static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
87 union libnet_ChangePassword r;
88 NTSTATUS status;
89 TALLOC_CTX *mem_ctx;
90 struct tevent_context *ev;
91 const char *kwnames[] = { "newpassword", NULL };
93 ZERO_STRUCT(r);
95 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:change_password",
96 discard_const_p(char *, kwnames),
97 &r.generic.in.newpassword)) {
98 return NULL;
101 r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
102 r.generic.in.account_name = cli_credentials_get_username(self->libnet_ctx->cred);
103 r.generic.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
104 r.generic.in.oldpassword = cli_credentials_get_password(self->libnet_ctx->cred);
106 /* FIXME: we really need to get a context from the caller or we may end
107 * up with 2 event contexts */
108 ev = s4_event_context_init(NULL);
110 mem_ctx = talloc_new(ev);
111 if (mem_ctx == NULL) {
112 PyErr_NoMemory();
113 return NULL;
116 status = libnet_ChangePassword(self->libnet_ctx, mem_ctx, &r);
117 if (NT_STATUS_IS_ERR(status)) {
118 PyErr_SetString(PyExc_RuntimeError,
119 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
120 talloc_free(mem_ctx);
121 return NULL;
124 talloc_free(mem_ctx);
126 Py_RETURN_NONE;
129 static const char py_net_change_password_doc[] = "change_password(newpassword) -> True\n\n" \
130 "Change password for a user. You must supply credential with enough rights to do this.\n\n" \
131 "Sample usage is:\n" \
132 "net.set_password(newpassword=<new_password>\n";
135 static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
137 union libnet_SetPassword r;
138 NTSTATUS status;
139 TALLOC_CTX *mem_ctx;
140 struct tevent_context *ev;
141 const char *kwnames[] = { "account_name", "domain_name", "newpassword", NULL };
143 ZERO_STRUCT(r);
145 r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
147 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss:set_password",
148 discard_const_p(char *, kwnames),
149 &r.generic.in.account_name,
150 &r.generic.in.domain_name,
151 &r.generic.in.newpassword)) {
152 return NULL;
155 /* FIXME: we really need to get a context from the caller or we may end
156 * up with 2 event contexts */
157 ev = s4_event_context_init(NULL);
159 mem_ctx = talloc_new(ev);
160 if (mem_ctx == NULL) {
161 PyErr_NoMemory();
162 return NULL;
165 status = libnet_SetPassword(self->libnet_ctx, mem_ctx, &r);
166 if (NT_STATUS_IS_ERR(status)) {
167 PyErr_SetString(PyExc_RuntimeError,
168 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
169 talloc_free(mem_ctx);
170 return NULL;
173 talloc_free(mem_ctx);
175 Py_RETURN_NONE;
178 static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
179 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
180 "Sample usage is:\n" \
181 "net.set_password(account_name=<account_name>,\n" \
182 " domain_name=domain_name,\n" \
183 " newpassword=new_pass)\n";
186 static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwargs)
188 const char *kwnames[] = { "server_name", NULL };
189 union libnet_RemoteTOD r;
190 NTSTATUS status;
191 TALLOC_CTX *mem_ctx;
192 char timestr[64];
193 PyObject *ret;
194 struct tm *tm;
196 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s",
197 discard_const_p(char *, kwnames), &r.generic.in.server_name))
198 return NULL;
200 r.generic.level = LIBNET_REMOTE_TOD_GENERIC;
202 mem_ctx = talloc_new(NULL);
203 if (mem_ctx == NULL) {
204 PyErr_NoMemory();
205 return NULL;
208 status = libnet_RemoteTOD(self->libnet_ctx, mem_ctx, &r);
209 if (!NT_STATUS_IS_OK(status)) {
210 PyErr_SetString(PyExc_RuntimeError,
211 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
212 talloc_free(mem_ctx);
213 return NULL;
216 ZERO_STRUCT(timestr);
217 tm = localtime(&r.generic.out.time);
218 strftime(timestr, sizeof(timestr)-1, "%c %Z",tm);
220 ret = PyString_FromString(timestr);
222 talloc_free(mem_ctx);
224 return ret;
227 static const char py_net_time_doc[] = "time(server_name) -> timestr\n"
228 "Retrieve the remote time on a server";
230 static PyObject *py_net_user_create(py_net_Object *self, PyObject *args, PyObject *kwargs)
232 const char *kwnames[] = { "username", NULL };
233 NTSTATUS status;
234 TALLOC_CTX *mem_ctx;
235 struct libnet_CreateUser r;
237 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames),
238 &r.in.user_name))
239 return NULL;
241 r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
243 mem_ctx = talloc_new(NULL);
244 if (mem_ctx == NULL) {
245 PyErr_NoMemory();
246 return NULL;
249 status = libnet_CreateUser(self->libnet_ctx, mem_ctx, &r);
250 if (!NT_STATUS_IS_OK(status)) {
251 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
252 talloc_free(mem_ctx);
253 return NULL;
256 talloc_free(mem_ctx);
258 Py_RETURN_NONE;
261 static const char py_net_create_user_doc[] = "create_user(username)\n"
262 "Create a new user.";
264 static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObject *kwargs)
266 const char *kwnames[] = { "username", NULL };
267 NTSTATUS status;
268 TALLOC_CTX *mem_ctx;
269 struct libnet_DeleteUser r;
271 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames),
272 &r.in.user_name))
273 return NULL;
275 r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
277 mem_ctx = talloc_new(NULL);
278 if (mem_ctx == NULL) {
279 PyErr_NoMemory();
280 return NULL;
283 status = libnet_DeleteUser(self->libnet_ctx, mem_ctx, &r);
284 if (!NT_STATUS_IS_OK(status)) {
285 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
286 talloc_free(mem_ctx);
287 return NULL;
290 talloc_free(mem_ctx);
292 Py_RETURN_NONE;
295 static const char py_net_delete_user_doc[] = "delete_user(username)\n"
296 "Delete a user.";
298 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
300 PyObject *mod_security, *dom_sid_Type;
302 mod_security = PyImport_ImportModule("samba.dcerpc.security");
303 if (mod_security == NULL)
304 return NULL;
306 dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
307 if (dom_sid_Type == NULL)
308 return NULL;
310 return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
313 static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *kwargs)
315 const char *kwnames[] = { "domain", "target_dir", NULL };
316 NTSTATUS status;
317 TALLOC_CTX *mem_ctx;
318 PyObject *ret;
319 struct libnet_Vampire r;
321 ZERO_STRUCT(r);
323 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z", discard_const_p(char *, kwnames),
324 &r.in.domain_name, &r.in.targetdir)) {
325 return NULL;
328 r.in.netbios_name = lpcfg_netbios_name(self->libnet_ctx->lp_ctx);
329 r.out.error_string = NULL;
331 mem_ctx = talloc_new(NULL);
332 if (mem_ctx == NULL) {
333 PyErr_NoMemory();
334 return NULL;
337 status = libnet_Vampire(self->libnet_ctx, mem_ctx, &r);
339 if (!NT_STATUS_IS_OK(status)) {
340 PyErr_SetString(PyExc_RuntimeError,
341 r.out.error_string ? r.out.error_string : nt_errstr(status));
342 talloc_free(mem_ctx);
343 return NULL;
346 ret = Py_BuildValue("(sO)", r.out.domain_name, py_dom_sid_FromSid(r.out.domain_sid));
348 talloc_free(mem_ctx);
350 return ret;
353 struct replicate_state {
354 void *vampire_state;
355 dcerpc_InterfaceObject *drs_pipe;
356 struct libnet_BecomeDC_StoreChunk chunk;
357 DATA_BLOB gensec_skey;
358 struct libnet_BecomeDC_Partition partition;
359 struct libnet_BecomeDC_Forest forest;
360 struct libnet_BecomeDC_DestDSA dest_dsa;
364 setup for replicate_chunk() calls
366 static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyObject *kwargs)
368 const char *kwnames[] = { "samdb", "lp", "drspipe", "invocation_id", NULL };
369 PyObject *py_ldb, *py_lp, *py_drspipe, *py_invocation_id;
370 struct ldb_context *samdb;
371 struct loadparm_context *lp;
372 struct replicate_state *s;
373 NTSTATUS status;
375 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOOO",
376 discard_const_p(char *, kwnames),
377 &py_ldb, &py_lp, &py_drspipe,
378 &py_invocation_id)) {
379 return NULL;
382 s = talloc_zero(NULL, struct replicate_state);
383 if (!s) return NULL;
385 lp = lpcfg_from_py_object(s, py_lp);
386 if (lp == NULL) {
387 PyErr_SetString(PyExc_TypeError, "Expected lp object");
388 talloc_free(s);
389 return NULL;
392 samdb = pyldb_Ldb_AsLdbContext(py_ldb);
393 if (samdb == NULL) {
394 PyErr_SetString(PyExc_TypeError, "Expected ldb object");
395 talloc_free(s);
396 return NULL;
398 if (!py_check_dcerpc_type(py_invocation_id, "samba.dcerpc.misc", "GUID")) {
400 talloc_free(s);
401 return NULL;
403 s->dest_dsa.invocation_id = *pytalloc_get_type(py_invocation_id, struct GUID);
405 s->drs_pipe = (dcerpc_InterfaceObject *)(py_drspipe);
407 s->vampire_state = libnet_vampire_replicate_init(s, samdb, lp);
408 if (s->vampire_state == NULL) {
409 PyErr_SetString(PyExc_TypeError, "Failed to initialise vampire_state");
410 talloc_free(s);
411 return NULL;
414 status = gensec_session_key(s->drs_pipe->pipe->conn->security_state.generic_state,
416 &s->gensec_skey);
417 if (!NT_STATUS_IS_OK(status)) {
418 PyErr_Format(PyExc_RuntimeError, "Unable to get session key from drspipe: %s",
419 nt_errstr(status));
420 talloc_free(s);
421 return NULL;
424 s->forest.dns_name = samdb_dn_to_dns_domain(s, ldb_get_root_basedn(samdb));
425 s->forest.root_dn_str = ldb_dn_get_linearized(ldb_get_root_basedn(samdb));
426 s->forest.config_dn_str = ldb_dn_get_linearized(ldb_get_config_basedn(samdb));
427 s->forest.schema_dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(samdb));
429 s->chunk.gensec_skey = &s->gensec_skey;
430 s->chunk.partition = &s->partition;
431 s->chunk.forest = &s->forest;
432 s->chunk.dest_dsa = &s->dest_dsa;
434 return pytalloc_CObject_FromTallocPtr(s);
439 process one replication chunk
441 static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyObject *kwargs)
443 const char *kwnames[] = { "state", "level", "ctr",
444 "schema", "req_level", "req",
445 NULL };
446 PyObject *py_state, *py_ctr, *py_schema = Py_None, *py_req = Py_None;
447 struct replicate_state *s;
448 unsigned level;
449 unsigned req_level = 0;
450 NTSTATUS (*chunk_handler)(void *private_data, const struct libnet_BecomeDC_StoreChunk *c);
451 NTSTATUS status;
453 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|OIO",
454 discard_const_p(char *, kwnames),
455 &py_state, &level, &py_ctr,
456 &py_schema, &req_level, &py_req)) {
457 return NULL;
460 s = talloc_get_type(PyCObject_AsVoidPtr(py_state), struct replicate_state);
461 if (!s) {
462 PyErr_SetString(PyExc_TypeError, "Expected replication_state");
463 return NULL;
466 switch (level) {
467 case 1:
468 if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
469 return NULL;
471 s->chunk.ctr1 = pytalloc_get_ptr(py_ctr);
472 s->partition.nc = *s->chunk.ctr1->naming_context;
473 s->partition.more_data = s->chunk.ctr1->more_data;
474 s->partition.source_dsa_guid = s->chunk.ctr1->source_dsa_guid;
475 s->partition.source_dsa_invocation_id = s->chunk.ctr1->source_dsa_invocation_id;
476 s->partition.highwatermark = s->chunk.ctr1->new_highwatermark;
477 break;
478 case 6:
479 if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
480 return NULL;
482 s->chunk.ctr6 = pytalloc_get_ptr(py_ctr);
483 s->partition.nc = *s->chunk.ctr6->naming_context;
484 s->partition.more_data = s->chunk.ctr6->more_data;
485 s->partition.source_dsa_guid = s->chunk.ctr6->source_dsa_guid;
486 s->partition.source_dsa_invocation_id = s->chunk.ctr6->source_dsa_invocation_id;
487 s->partition.highwatermark = s->chunk.ctr6->new_highwatermark;
488 break;
489 default:
490 PyErr_Format(PyExc_TypeError, "Bad level %u in replicate_chunk", level);
491 return NULL;
494 s->chunk.req5 = NULL;
495 s->chunk.req8 = NULL;
496 s->chunk.req10 = NULL;
497 if (py_req) {
498 switch (req_level) {
499 case 0:
500 break;
501 case 5:
502 if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest5")) {
503 return NULL;
506 s->chunk.req5 = pytalloc_get_ptr(py_req);
507 break;
508 case 8:
509 if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest8")) {
510 return NULL;
513 s->chunk.req8 = pytalloc_get_ptr(py_req);
514 break;
515 case 10:
516 if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest10")) {
517 return NULL;
520 s->chunk.req10 = pytalloc_get_ptr(py_req);
521 break;
522 default:
523 PyErr_Format(PyExc_TypeError, "Bad req_level %u in replicate_chunk", req_level);
524 return NULL;
527 s->chunk.req_level = req_level;
529 chunk_handler = libnet_vampire_cb_store_chunk;
530 if (py_schema) {
531 if (!PyBool_Check(py_schema)) {
532 PyErr_SetString(PyExc_TypeError, "Expected boolean schema");
533 return NULL;
535 if (py_schema == Py_True) {
536 chunk_handler = libnet_vampire_cb_schema_chunk;
540 s->chunk.ctr_level = level;
542 status = chunk_handler(s->vampire_state, &s->chunk);
543 if (!NT_STATUS_IS_OK(status)) {
544 PyErr_Format(PyExc_TypeError, "Failed to process chunk: %s", nt_errstr(status));
545 return NULL;
548 Py_RETURN_NONE;
553 find a DC given a domain name and server type
555 static PyObject *py_net_finddc(py_net_Object *self, PyObject *args, PyObject *kwargs)
557 const char *domain = NULL, *address = NULL;
558 unsigned server_type;
559 NTSTATUS status;
560 struct finddcs *io;
561 TALLOC_CTX *mem_ctx;
562 PyObject *ret;
563 const char * const kwnames[] = { "flags", "domain", "address", NULL };
565 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "I|ss",
566 discard_const_p(char *, kwnames),
567 &server_type, &domain, &address)) {
568 return NULL;
571 mem_ctx = talloc_new(self->mem_ctx);
573 io = talloc_zero(mem_ctx, struct finddcs);
574 if (domain != NULL) {
575 io->in.domain_name = domain;
577 if (address != NULL) {
578 io->in.server_address = address;
580 io->in.minimum_dc_flags = server_type;
582 status = finddcs_cldap(io, io,
583 lpcfg_resolve_context(self->libnet_ctx->lp_ctx), self->ev);
584 if (NT_STATUS_IS_ERR(status)) {
585 PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
586 talloc_free(mem_ctx);
587 return NULL;
590 ret = py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
591 io, &io->out.netlogon.data.nt5_ex);
592 talloc_free(mem_ctx);
594 return ret;
598 static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n"
599 "Vampire a domain.";
601 static const char py_net_replicate_init_doc[] = "replicate_init(samdb, lp, drspipe)\n"
602 "Setup for replicate_chunk calls.";
604 static const char py_net_replicate_chunk_doc[] = "replicate_chunk(state, level, ctr, schema)\n"
605 "Process replication for one chunk";
607 static const char py_net_finddc_doc[] = "finddc(flags=server_type, domain=None, address=None)\n"
608 "Find a DC with the specified 'server_type' bits. The 'domain' and/or 'address' have to be used as additional search criteria. Returns the whole netlogon struct";
610 static PyMethodDef net_obj_methods[] = {
611 {"join_member", (PyCFunction)py_net_join_member, METH_VARARGS|METH_KEYWORDS, py_net_join_member_doc},
612 {"change_password", (PyCFunction)py_net_change_password, METH_VARARGS|METH_KEYWORDS, py_net_change_password_doc},
613 {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
614 {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc},
615 {"create_user", (PyCFunction)py_net_user_create, METH_VARARGS|METH_KEYWORDS, py_net_create_user_doc},
616 {"delete_user", (PyCFunction)py_net_user_delete, METH_VARARGS|METH_KEYWORDS, py_net_delete_user_doc},
617 {"vampire", (PyCFunction)py_net_vampire, METH_VARARGS|METH_KEYWORDS, py_net_vampire_doc},
618 {"replicate_init", (PyCFunction)py_net_replicate_init, METH_VARARGS|METH_KEYWORDS, py_net_replicate_init_doc},
619 {"replicate_chunk", (PyCFunction)py_net_replicate_chunk, METH_VARARGS|METH_KEYWORDS, py_net_replicate_chunk_doc},
620 {"finddc", (PyCFunction)py_net_finddc, METH_KEYWORDS, py_net_finddc_doc},
621 { NULL }
624 static void py_net_dealloc(py_net_Object *self)
626 talloc_free(self->mem_ctx);
627 PyObject_Del(self);
630 static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
632 PyObject *py_creds, *py_lp = Py_None;
633 const char *kwnames[] = { "creds", "lp", "server", NULL };
634 py_net_Object *ret;
635 struct loadparm_context *lp;
636 const char *server_address = NULL;
638 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oz",
639 discard_const_p(char *, kwnames), &py_creds, &py_lp,
640 &server_address))
641 return NULL;
643 ret = PyObject_New(py_net_Object, type);
644 if (ret == NULL) {
645 return NULL;
648 /* FIXME: we really need to get a context from the caller or we may end
649 * up with 2 event contexts */
650 ret->ev = s4_event_context_init(NULL);
651 ret->mem_ctx = talloc_new(ret->ev);
653 lp = lpcfg_from_py_object(ret->mem_ctx, py_lp);
654 if (lp == NULL) {
655 Py_DECREF(ret);
656 return NULL;
659 ret->libnet_ctx = libnet_context_init(ret->ev, lp);
660 if (ret->libnet_ctx == NULL) {
661 PyErr_SetString(PyExc_RuntimeError, "Unable to initialize net");
662 Py_DECREF(ret);
663 return NULL;
666 ret->libnet_ctx->server_address = server_address;
668 ret->libnet_ctx->cred = cli_credentials_from_py_object(py_creds);
669 if (ret->libnet_ctx->cred == NULL) {
670 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
671 Py_DECREF(ret);
672 return NULL;
675 return (PyObject *)ret;
679 PyTypeObject py_net_Type = {
680 PyObject_HEAD_INIT(NULL) 0,
681 .tp_name = "net.Net",
682 .tp_basicsize = sizeof(py_net_Object),
683 .tp_dealloc = (destructor)py_net_dealloc,
684 .tp_methods = net_obj_methods,
685 .tp_new = net_obj_new,
688 void initnet(void)
690 PyObject *m;
692 if (PyType_Ready(&py_net_Type) < 0)
693 return;
695 m = Py_InitModule3("net", NULL, NULL);
696 if (m == NULL)
697 return;
699 Py_INCREF(&py_net_Type);
700 PyModule_AddObject(m, "Net", (PyObject *)&py_net_Type);
701 PyModule_AddObject(m, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC));
702 PyModule_AddObject(m, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED));
703 PyModule_AddObject(m, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC));
704 PyModule_AddObject(m, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED));