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