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