gensec: clarify memory ownership for gensec_session_info() and gensec_session_key()
[samba.git] / source4 / libnet / py_net.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
5    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <Python.h>
22 #include "includes.h"
23 #include <ldb.h>
24 #include <pyldb.h>
25 #include "libnet.h"
26 #include "auth/credentials/pycredentials.h"
27 #include "libcli/security/security.h"
28 #include "lib/events/events.h"
29 #include "param/pyparam.h"
30 #include "auth/gensec/gensec.h"
31 #include "librpc/rpc/pyrpc_util.h"
32 #include "libcli/finddc.h"
33 #include "libcli/resolve/resolve.h"
34
35 void initnet(void);
36
37 typedef struct {
38         PyObject_HEAD
39         TALLOC_CTX *mem_ctx;
40         struct libnet_context *libnet_ctx;
41         struct tevent_context *ev;
42 } py_net_Object;
43
44 static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
45 {
46         struct libnet_Join_member r;
47         NTSTATUS status;
48         PyObject *result;
49         TALLOC_CTX *mem_ctx;
50         const char *kwnames[] = { "domain_name", "netbios_name", "level", NULL };
51
52         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssi:Join", discard_const_p(char *, kwnames),
53                                          &r.in.domain_name, &r.in.netbios_name, 
54                                          &r.in.level))
55                 return NULL;
56
57         mem_ctx = talloc_new(self->mem_ctx);
58         if (mem_ctx == NULL) {
59                 PyErr_NoMemory();
60                 return NULL;
61         }
62
63         status = libnet_Join_member(self->libnet_ctx, mem_ctx, &r);
64         if (NT_STATUS_IS_ERR(status)) {
65                 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
66                 talloc_free(mem_ctx);
67                 return NULL;
68         }
69
70         result = Py_BuildValue("sss", r.out.join_password,
71                                dom_sid_string(mem_ctx, r.out.domain_sid),
72                                r.out.domain_name);
73
74         talloc_free(mem_ctx);
75
76         return result;
77 }
78
79 static const char py_net_join_member_doc[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
80 "Join the domain with the specified name.";
81
82 static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
83 {
84         union libnet_ChangePassword r;
85         NTSTATUS status;
86         TALLOC_CTX *mem_ctx;
87         struct tevent_context *ev;
88         const char *kwnames[] = { "newpassword", NULL };
89
90         ZERO_STRUCT(r);
91
92         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:change_password",
93                                         discard_const_p(char *, kwnames),
94                                         &r.generic.in.newpassword)) {
95                 return NULL;
96         }
97
98         r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
99         r.generic.in.account_name = cli_credentials_get_username(self->libnet_ctx->cred);
100         r.generic.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
101         r.generic.in.oldpassword = cli_credentials_get_password(self->libnet_ctx->cred);
102
103         /* FIXME: we really need to get a context from the caller or we may end
104          * up with 2 event contexts */
105         ev = s4_event_context_init(NULL);
106
107         mem_ctx = talloc_new(ev);
108         if (mem_ctx == NULL) {
109                 PyErr_NoMemory();
110                 return NULL;
111         }
112
113         status = libnet_ChangePassword(self->libnet_ctx, mem_ctx, &r);
114         if (NT_STATUS_IS_ERR(status)) {
115                 PyErr_SetString(PyExc_RuntimeError,
116                                 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
117                 talloc_free(mem_ctx);
118                 return NULL;
119         }
120
121         talloc_free(mem_ctx);
122
123         Py_RETURN_NONE;
124 }
125
126 static const char py_net_change_password_doc[] = "change_password(newpassword) -> True\n\n" \
127 "Change password for a user. You must supply credential with enough rights to do this.\n\n" \
128 "Sample usage is:\n" \
129 "net.set_password(newpassword=<new_password>\n";
130
131
132 static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
133 {
134         union libnet_SetPassword r;
135         NTSTATUS status;
136         TALLOC_CTX *mem_ctx;
137         struct tevent_context *ev;
138         const char *kwnames[] = { "account_name", "domain_name", "newpassword", NULL };
139
140         ZERO_STRUCT(r);
141
142         r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
143
144         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss:set_password",
145                                         discard_const_p(char *, kwnames),
146                                          &r.generic.in.account_name,
147                                          &r.generic.in.domain_name,
148                                          &r.generic.in.newpassword)) {
149                 return NULL;
150         }
151
152         /* FIXME: we really need to get a context from the caller or we may end
153          * up with 2 event contexts */
154         ev = s4_event_context_init(NULL);
155
156         mem_ctx = talloc_new(ev);
157         if (mem_ctx == NULL) {
158                 PyErr_NoMemory();
159                 return NULL;
160         }
161
162         status = libnet_SetPassword(self->libnet_ctx, mem_ctx, &r);
163         if (NT_STATUS_IS_ERR(status)) {
164                 PyErr_SetString(PyExc_RuntimeError,
165                                 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
166                 talloc_free(mem_ctx);
167                 return NULL;
168         }
169
170         talloc_free(mem_ctx);
171
172         Py_RETURN_NONE;
173 }
174
175 static const char py_net_set_password_doc[] = "set_password(account_name, domain_name, newpassword) -> True\n\n" \
176 "Set password for a user. You must supply credential with enough rights to do this.\n\n" \
177 "Sample usage is:\n" \
178 "net.set_password(account_name=<account_name>,\n" \
179 "                domain_name=domain_name,\n" \
180 "                newpassword=new_pass)\n";
181
182
183 static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
184 {
185         struct libnet_export_keytab r;
186         TALLOC_CTX *mem_ctx;
187         const char *kwnames[] = { "keytab", NULL };
188         NTSTATUS status;
189
190         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:export_keytab", discard_const_p(char *, kwnames),
191                                          &r.in.keytab_name)) {
192                 return NULL;
193         }
194
195         mem_ctx = talloc_new(self->mem_ctx);
196         if (mem_ctx == NULL) {
197                 PyErr_NoMemory();
198                 return NULL;
199         }
200
201         status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
202         if (NT_STATUS_IS_ERR(status)) {
203                 PyErr_SetString(PyExc_RuntimeError,
204                                 r.out.error_string?r.out.error_string:nt_errstr(status));
205                 talloc_free(mem_ctx);
206                 return NULL;
207         }
208
209         talloc_free(mem_ctx);
210
211         Py_RETURN_NONE;
212 }
213
214 static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
215 "Export the DC keytab to a keytab file.";
216
217 static PyObject *py_net_time(py_net_Object *self, PyObject *args, PyObject *kwargs)
218 {
219         const char *kwnames[] = { "server_name", NULL };
220         union libnet_RemoteTOD r;
221         NTSTATUS status;
222         TALLOC_CTX *mem_ctx;
223         char timestr[64];
224         PyObject *ret;
225         struct tm *tm;
226
227         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s",
228                 discard_const_p(char *, kwnames), &r.generic.in.server_name))
229                 return NULL;
230
231         r.generic.level                 = LIBNET_REMOTE_TOD_GENERIC;
232
233         mem_ctx = talloc_new(NULL);
234         if (mem_ctx == NULL) {
235                 PyErr_NoMemory();
236                 return NULL;
237         }
238
239         status = libnet_RemoteTOD(self->libnet_ctx, mem_ctx, &r);
240         if (!NT_STATUS_IS_OK(status)) {
241                 PyErr_SetString(PyExc_RuntimeError,
242                                 r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
243                 talloc_free(mem_ctx);
244                 return NULL;
245         }
246
247         ZERO_STRUCT(timestr);
248         tm = localtime(&r.generic.out.time);
249         strftime(timestr, sizeof(timestr)-1, "%c %Z",tm);
250         
251         ret = PyString_FromString(timestr);
252
253         talloc_free(mem_ctx);
254
255         return ret;
256 }
257
258 static const char py_net_time_doc[] = "time(server_name) -> timestr\n"
259 "Retrieve the remote time on a server";
260
261 static PyObject *py_net_user_create(py_net_Object *self, PyObject *args, PyObject *kwargs)
262 {
263         const char *kwnames[] = { "username", NULL };
264         NTSTATUS status;
265         TALLOC_CTX *mem_ctx;
266         struct libnet_CreateUser r;
267
268         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames), 
269                                                                          &r.in.user_name))
270                 return NULL;
271
272         r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
273
274         mem_ctx = talloc_new(NULL);
275         if (mem_ctx == NULL) {
276                 PyErr_NoMemory();
277                 return NULL;
278         }
279
280         status = libnet_CreateUser(self->libnet_ctx, mem_ctx, &r);
281         if (!NT_STATUS_IS_OK(status)) {
282                 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
283                 talloc_free(mem_ctx);
284                 return NULL;
285         }
286
287         talloc_free(mem_ctx);
288         
289         Py_RETURN_NONE;
290 }
291
292 static const char py_net_create_user_doc[] = "create_user(username)\n"
293 "Create a new user.";
294
295 static PyObject *py_net_user_delete(py_net_Object *self, PyObject *args, PyObject *kwargs)
296 {
297         const char *kwnames[] = { "username", NULL };
298         NTSTATUS status;
299         TALLOC_CTX *mem_ctx;
300         struct libnet_DeleteUser r;
301
302         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", discard_const_p(char *, kwnames), 
303                                                                          &r.in.user_name))
304                 return NULL;
305
306         r.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
307
308         mem_ctx = talloc_new(NULL);
309         if (mem_ctx == NULL) {
310                 PyErr_NoMemory();
311                 return NULL;
312         }
313
314         status = libnet_DeleteUser(self->libnet_ctx, mem_ctx, &r);
315         if (!NT_STATUS_IS_OK(status)) {
316                 PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
317                 talloc_free(mem_ctx);
318                 return NULL;
319         }
320
321         talloc_free(mem_ctx);
322         
323         Py_RETURN_NONE;
324 }
325
326 static const char py_net_delete_user_doc[] = "delete_user(username)\n"
327 "Delete a user.";
328
329 static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
330 {
331         PyObject *mod_security, *dom_sid_Type;
332
333         mod_security = PyImport_ImportModule("samba.dcerpc.security");
334         if (mod_security == NULL)
335                 return NULL;
336
337         dom_sid_Type = PyObject_GetAttrString(mod_security, "dom_sid");
338         if (dom_sid_Type == NULL)
339                 return NULL;
340
341         return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
342 }
343
344 static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *kwargs)
345 {
346         const char *kwnames[] = { "domain", "target_dir", NULL };
347         NTSTATUS status;
348         TALLOC_CTX *mem_ctx;
349         PyObject *ret;
350         struct libnet_Vampire r;
351
352         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z", discard_const_p(char *, kwnames),
353                                          &r.in.domain_name, &r.in.targetdir)) {
354                 return NULL;
355         }
356
357         r.in.netbios_name  = lpcfg_netbios_name(self->libnet_ctx->lp_ctx);
358         r.out.error_string = NULL;
359
360         mem_ctx = talloc_new(NULL);
361         if (mem_ctx == NULL) {
362                 PyErr_NoMemory();
363                 return NULL;
364         }
365
366         status = libnet_Vampire(self->libnet_ctx, mem_ctx, &r);
367
368         if (!NT_STATUS_IS_OK(status)) {
369                 PyErr_SetString(PyExc_RuntimeError,
370                                 r.out.error_string ? r.out.error_string : nt_errstr(status));
371                 talloc_free(mem_ctx);
372                 return NULL;
373         }
374
375         ret = Py_BuildValue("(sO)", r.out.domain_name, py_dom_sid_FromSid(r.out.domain_sid));
376
377         talloc_free(mem_ctx);
378
379         return ret;
380 }
381
382 struct replicate_state {
383         void *vampire_state;
384         dcerpc_InterfaceObject *drs_pipe;
385         struct libnet_BecomeDC_StoreChunk chunk;
386         DATA_BLOB gensec_skey;
387         struct libnet_BecomeDC_Partition partition;
388         struct libnet_BecomeDC_Forest forest;
389         struct libnet_BecomeDC_DestDSA dest_dsa;
390 };
391
392 /*
393   setup for replicate_chunk() calls
394  */
395 static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyObject *kwargs)
396 {
397         const char *kwnames[] = { "samdb", "lp", "drspipe", NULL };
398         PyObject *py_ldb, *py_lp, *py_drspipe;
399         struct ldb_context *samdb;
400         struct loadparm_context *lp;
401         struct replicate_state *s;
402         NTSTATUS status;
403
404         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOO",
405                                          discard_const_p(char *, kwnames),
406                                          &py_ldb, &py_lp, &py_drspipe)) {
407                 return NULL;
408         }
409
410         s = talloc_zero(NULL, struct replicate_state);
411         if (!s) return NULL;
412
413         lp = lpcfg_from_py_object(s, py_lp);
414         if (lp == NULL) {
415                 PyErr_SetString(PyExc_TypeError, "Expected lp object");
416                 talloc_free(s);
417                 return NULL;
418         }
419
420         samdb = PyLdb_AsLdbContext(py_ldb);
421         if (samdb == NULL) {
422                 PyErr_SetString(PyExc_TypeError, "Expected ldb object");
423                 talloc_free(s);
424                 return NULL;
425         }
426
427         s->drs_pipe = (dcerpc_InterfaceObject *)(py_drspipe);
428
429         s->vampire_state = libnet_vampire_replicate_init(s, samdb, lp);
430         if (s->vampire_state == NULL) {
431                 PyErr_SetString(PyExc_TypeError, "Failed to initialise vampire_state");
432                 talloc_free(s);
433                 return NULL;
434         }
435
436         status = gensec_session_key(s->drs_pipe->pipe->conn->security_state.generic_state,
437                                     s,
438                                     &s->gensec_skey);
439         if (!NT_STATUS_IS_OK(status)) {
440                 PyErr_Format(PyExc_RuntimeError, "Unable to get session key from drspipe: %s",
441                              nt_errstr(status));
442                 talloc_free(s);
443                 return NULL;
444         }
445
446         s->forest.dns_name = lpcfg_dnsdomain(lp);
447
448         s->chunk.gensec_skey = &s->gensec_skey;
449         s->chunk.partition = &s->partition;
450         s->chunk.forest = &s->forest;
451         s->chunk.dest_dsa = &s->dest_dsa;
452
453         return PyCObject_FromTallocPtr(s);
454 }
455
456
457 /*
458   process one replication chunk
459  */
460 static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyObject *kwargs)
461 {
462         const char *kwnames[] = { "state", "level", "ctr", "schema", NULL };
463         PyObject *py_state, *py_ctr, *py_schema;
464         struct replicate_state *s;
465         unsigned level;
466         NTSTATUS (*chunk_handler)(void *private_data, const struct libnet_BecomeDC_StoreChunk *c);
467         NTSTATUS status;
468
469         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|O",
470                                          discard_const_p(char *, kwnames),
471                                          &py_state, &level, &py_ctr, &py_schema)) {
472                 return NULL;
473         }
474
475         s = talloc_get_type(PyCObject_AsVoidPtr(py_state), struct replicate_state);
476         if (!s) {
477                 PyErr_SetString(PyExc_TypeError, "Expected replication_state");
478                 return NULL;
479         }
480
481         switch (level) {
482         case 1:
483                 if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
484                         return NULL;
485                 }
486                 s->chunk.ctr1                         = py_talloc_get_ptr(py_ctr);
487                 s->partition.nc                       = *s->chunk.ctr1->naming_context;
488                 s->partition.more_data                = s->chunk.ctr1->more_data;
489                 s->partition.source_dsa_guid          = s->chunk.ctr1->source_dsa_guid;
490                 s->partition.source_dsa_invocation_id = s->chunk.ctr1->source_dsa_invocation_id;
491                 s->partition.highwatermark            = s->chunk.ctr1->new_highwatermark;
492                 break;
493         case 6:
494                 if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
495                         return NULL;
496                 }
497                 s->chunk.ctr6                         = py_talloc_get_ptr(py_ctr);
498                 s->partition.nc                       = *s->chunk.ctr6->naming_context;
499                 s->partition.more_data                = s->chunk.ctr6->more_data;
500                 s->partition.source_dsa_guid          = s->chunk.ctr6->source_dsa_guid;
501                 s->partition.source_dsa_invocation_id = s->chunk.ctr6->source_dsa_invocation_id;
502                 s->partition.highwatermark            = s->chunk.ctr6->new_highwatermark;
503                 break;
504         default:
505                 PyErr_Format(PyExc_TypeError, "Bad level %u in replicate_chunk", level);
506                 return NULL;
507         }
508
509         chunk_handler = libnet_vampire_cb_store_chunk;
510         if (py_schema) {
511                 if (!PyBool_Check(py_schema)) {
512                         PyErr_SetString(PyExc_TypeError, "Expected boolean schema");
513                         return NULL;
514                 }
515                 if (py_schema == Py_True) {
516                         chunk_handler = libnet_vampire_cb_schema_chunk;
517                 }
518         }
519
520         s->chunk.ctr_level = level;
521
522         status = chunk_handler(s->vampire_state, &s->chunk);
523         if (!NT_STATUS_IS_OK(status)) {
524                 PyErr_Format(PyExc_TypeError, "Failed to process chunk: %s", nt_errstr(status));
525                 return NULL;
526         }
527
528         Py_RETURN_NONE;
529 }
530
531
532 /*
533   find a DC given a domain name and server type
534  */
535 static PyObject *py_net_finddc(py_net_Object *self, PyObject *args)
536 {
537         const char *domain_name;
538         unsigned server_type;
539         NTSTATUS status;
540         struct finddcs *io;
541         TALLOC_CTX *mem_ctx;
542         PyObject *ret;
543
544         if (!PyArg_ParseTuple(args, "sI", &domain_name, &server_type)) {
545                 return NULL;
546         }
547
548         mem_ctx = talloc_new(self->mem_ctx);
549
550         io = talloc_zero(mem_ctx, struct finddcs);
551         io->in.domain_name = domain_name;
552         io->in.minimum_dc_flags = server_type;
553
554         status = finddcs_cldap(io, io,
555                                lpcfg_resolve_context(self->libnet_ctx->lp_ctx), self->ev);
556         if (NT_STATUS_IS_ERR(status)) {
557                 PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
558                 talloc_free(mem_ctx);
559                 return NULL;
560         }
561
562         ret = py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
563                                    io, &io->out.netlogon.data.nt5_ex);
564         talloc_free(mem_ctx);
565
566         return ret;
567 }
568
569
570 static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n"
571                                          "Vampire a domain.";
572
573 static const char py_net_replicate_init_doc[] = "replicate_init(samdb, lp, drspipe)\n"
574                                          "Setup for replicate_chunk calls.";
575
576 static const char py_net_replicate_chunk_doc[] = "replicate_chunk(state, level, ctr, schema)\n"
577                                          "Process replication for one chunk";
578
579 static const char py_net_finddc_doc[] = "finddc(domain, server_type)\n"
580                                          "find a DC with the specified server_type bits. Return the DNS name";
581
582 static PyMethodDef net_obj_methods[] = {
583         {"join_member", (PyCFunction)py_net_join_member, METH_VARARGS|METH_KEYWORDS, py_net_join_member_doc},
584         {"change_password", (PyCFunction)py_net_change_password, METH_VARARGS|METH_KEYWORDS, py_net_change_password_doc},
585         {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
586         {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
587         {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc},
588         {"create_user", (PyCFunction)py_net_user_create, METH_VARARGS|METH_KEYWORDS, py_net_create_user_doc},
589         {"delete_user", (PyCFunction)py_net_user_delete, METH_VARARGS|METH_KEYWORDS, py_net_delete_user_doc},
590         {"vampire", (PyCFunction)py_net_vampire, METH_VARARGS|METH_KEYWORDS, py_net_vampire_doc},
591         {"replicate_init", (PyCFunction)py_net_replicate_init, METH_VARARGS|METH_KEYWORDS, py_net_replicate_init_doc},
592         {"replicate_chunk", (PyCFunction)py_net_replicate_chunk, METH_VARARGS|METH_KEYWORDS, py_net_replicate_chunk_doc},
593         {"finddc", (PyCFunction)py_net_finddc, METH_VARARGS, py_net_finddc_doc},
594         { NULL }
595 };
596
597 static void py_net_dealloc(py_net_Object *self)
598 {
599         talloc_free(self->mem_ctx);
600         PyObject_Del(self);
601 }
602
603 static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
604 {
605         PyObject *py_creds, *py_lp = Py_None;
606         const char *kwnames[] = { "creds", "lp", "server", NULL };
607         py_net_Object *ret;
608         struct loadparm_context *lp;
609         const char *server_address = NULL;
610
611         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oz",
612                                          discard_const_p(char *, kwnames), &py_creds, &py_lp,
613                                          &server_address))
614                 return NULL;
615
616         ret = PyObject_New(py_net_Object, type);
617         if (ret == NULL) {
618                 return NULL;
619         }
620
621         /* FIXME: we really need to get a context from the caller or we may end
622          * up with 2 event contexts */
623         ret->ev = s4_event_context_init(NULL);
624         ret->mem_ctx = talloc_new(ret->ev);
625
626         lp = lpcfg_from_py_object(ret->mem_ctx, py_lp);
627         if (lp == NULL) {
628                 Py_DECREF(ret);
629                 return NULL;
630         }
631
632         ret->libnet_ctx = libnet_context_init(ret->ev, lp);
633         if (ret->libnet_ctx == NULL) {
634                 PyErr_SetString(PyExc_RuntimeError, "Unable to initialize net");
635                 Py_DECREF(ret);
636                 return NULL;
637         }
638
639         ret->libnet_ctx->server_address = server_address;
640
641         ret->libnet_ctx->cred = cli_credentials_from_py_object(py_creds);
642         if (ret->libnet_ctx->cred == NULL) {
643                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
644                 Py_DECREF(ret);
645                 return NULL;
646         }
647
648         return (PyObject *)ret;
649 }
650
651
652 PyTypeObject py_net_Type = {
653         PyObject_HEAD_INIT(NULL) 0,
654         .tp_name = "net.Net",
655         .tp_basicsize = sizeof(py_net_Object),
656         .tp_dealloc = (destructor)py_net_dealloc,
657         .tp_methods = net_obj_methods,
658         .tp_new = net_obj_new,
659 };
660
661 void initnet(void)
662 {
663         PyObject *m;
664
665         if (PyType_Ready(&py_net_Type) < 0)
666                 return;
667
668         m = Py_InitModule3("net", NULL, NULL);
669         if (m == NULL)
670                 return;
671
672         Py_INCREF(&py_net_Type);
673         PyModule_AddObject(m, "Net", (PyObject *)&py_net_Type);
674         PyModule_AddObject(m, "LIBNET_JOINDOMAIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOINDOMAIN_AUTOMATIC));
675         PyModule_AddObject(m, "LIBNET_JOINDOMAIN_SPECIFIED", PyInt_FromLong(LIBNET_JOINDOMAIN_SPECIFIED));
676         PyModule_AddObject(m, "LIBNET_JOIN_AUTOMATIC", PyInt_FromLong(LIBNET_JOIN_AUTOMATIC));
677         PyModule_AddObject(m, "LIBNET_JOIN_SPECIFIED", PyInt_FromLong(LIBNET_JOIN_SPECIFIED));
678 }