s4-samba-tool: Add --principal argument to samba-tool domain exportkeytab
[kai/samba.git] / source4 / libnet / py_net.c
index ffcd60da27d0e5f8701bc36ac08db87d81b66648..cf37ccc3807fb4e657da48a84cabdbd73ded5cf6 100644 (file)
@@ -1,6 +1,7 @@
 /*
    Unix SMB/CIFS implementation.
    Samba utility functions
+
    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
 
@@ -20,7 +21,6 @@
 
 #include <Python.h>
 #include "includes.h"
-#include <ldb.h>
 #include <pyldb.h>
 #include "libnet.h"
 #include "auth/credentials/pycredentials.h"
@@ -29,8 +29,9 @@
 #include "param/pyparam.h"
 #include "auth/gensec/gensec.h"
 #include "librpc/rpc/pyrpc_util.h"
-#include "libcli/finddc.h"
 #include "libcli/resolve/resolve.h"
+#include "libcli/finddc.h"
+#include "dsdb/samdb/samdb.h"
 
 void initnet(void);
 
@@ -44,6 +45,7 @@ typedef struct {
 static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
        struct libnet_Join_member r;
+       int _level = 0;
        NTSTATUS status;
        PyObject *result;
        TALLOC_CTX *mem_ctx;
@@ -51,8 +53,10 @@ static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObjec
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssi:Join", discard_const_p(char *, kwnames),
                                         &r.in.domain_name, &r.in.netbios_name, 
-                                        &r.in.level))
+                                        &_level)) {
                return NULL;
+       }
+       r.in.level = _level;
 
        mem_ctx = talloc_new(self->mem_ctx);
        if (mem_ctx == NULL) {
@@ -79,20 +83,73 @@ static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObjec
 static const char py_net_join_member_doc[] = "join_member(domain_name, netbios_name, level) -> (join_password, domain_sid, domain_name)\n\n" \
 "Join the domain with the specified name.";
 
+static PyObject *py_net_change_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
+{
+       union libnet_ChangePassword r;
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx;
+       struct tevent_context *ev;
+       const char *kwnames[] = { "newpassword", NULL };
+
+       ZERO_STRUCT(r);
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:change_password",
+                                       discard_const_p(char *, kwnames),
+                                       &r.generic.in.newpassword)) {
+               return NULL;
+       }
+
+       r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
+       r.generic.in.account_name = cli_credentials_get_username(self->libnet_ctx->cred);
+       r.generic.in.domain_name = cli_credentials_get_domain(self->libnet_ctx->cred);
+       r.generic.in.oldpassword = cli_credentials_get_password(self->libnet_ctx->cred);
+
+       /* FIXME: we really need to get a context from the caller or we may end
+        * up with 2 event contexts */
+       ev = s4_event_context_init(NULL);
+
+       mem_ctx = talloc_new(ev);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+
+       status = libnet_ChangePassword(self->libnet_ctx, mem_ctx, &r);
+       if (NT_STATUS_IS_ERR(status)) {
+               PyErr_SetString(PyExc_RuntimeError,
+                               r.generic.out.error_string?r.generic.out.error_string:nt_errstr(status));
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       talloc_free(mem_ctx);
+
+       Py_RETURN_NONE;
+}
+
+static const char py_net_change_password_doc[] = "change_password(newpassword) -> True\n\n" \
+"Change password for a user. You must supply credential with enough rights to do this.\n\n" \
+"Sample usage is:\n" \
+"net.set_password(newpassword=<new_password>\n";
+
+
 static PyObject *py_net_set_password(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
        union libnet_SetPassword r;
        NTSTATUS status;
-       PyObject *py_creds;
        TALLOC_CTX *mem_ctx;
        struct tevent_context *ev;
-       const char *kwnames[] = { "account_name", "domain_name", "newpassword", "credentials", NULL };
+       const char *kwnames[] = { "account_name", "domain_name", "newpassword", NULL };
+
+       ZERO_STRUCT(r);
 
        r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssO:set_password", discard_const_p(char *, kwnames),
-                                        &r.generic.in.account_name, &r.generic.in.domain_name,
-                                        &r.generic.in.newpassword, &py_creds)) {
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sss:set_password",
+                                       discard_const_p(char *, kwnames),
+                                        &r.generic.in.account_name,
+                                        &r.generic.in.domain_name,
+                                        &r.generic.in.newpassword)) {
                return NULL;
        }
 
@@ -131,11 +188,13 @@ static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObj
 {
        struct libnet_export_keytab r;
        TALLOC_CTX *mem_ctx;
-       const char *kwnames[] = { "keytab", NULL };
+       const char *kwnames[] = { "keytab", "principal", NULL };
        NTSTATUS status;
+       r.in.principal = NULL;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:export_keytab", discard_const_p(char *, kwnames),
-                                        &r.in.keytab_name)) {
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z:export_keytab", discard_const_p(char *, kwnames),
+                                        &r.in.keytab_name,
+                                        &r.in.principal)) {
                return NULL;
        }
 
@@ -285,7 +344,7 @@ static PyObject *py_dom_sid_FromSid(struct dom_sid *sid)
        if (dom_sid_Type == NULL)
                return NULL;
 
-       return py_talloc_reference((PyTypeObject *)dom_sid_Type, sid);
+       return pytalloc_reference((PyTypeObject *)dom_sid_Type, sid);
 }
 
 static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *kwargs)
@@ -296,6 +355,8 @@ static PyObject *py_net_vampire(py_net_Object *self, PyObject *args, PyObject *k
        PyObject *ret;
        struct libnet_Vampire r;
 
+       ZERO_STRUCT(r);
+
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z", discard_const_p(char *, kwnames),
                                         &r.in.domain_name, &r.in.targetdir)) {
                return NULL;
@@ -364,7 +425,7 @@ static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyOb
                return NULL;
        }
 
-       samdb = PyLdb_AsLdbContext(py_ldb);
+       samdb = pyldb_Ldb_AsLdbContext(py_ldb);
        if (samdb == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected ldb object");
                talloc_free(s);
@@ -381,6 +442,7 @@ static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyOb
        }
 
        status = gensec_session_key(s->drs_pipe->pipe->conn->security_state.generic_state,
+                                   s,
                                    &s->gensec_skey);
        if (!NT_STATUS_IS_OK(status)) {
                PyErr_Format(PyExc_RuntimeError, "Unable to get session key from drspipe: %s",
@@ -389,14 +451,17 @@ static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyOb
                return NULL;
        }
 
-       s->forest.dns_name = lpcfg_dnsdomain(lp);
+       s->forest.dns_name = samdb_dn_to_dns_domain(s, ldb_get_root_basedn(samdb));
+       s->forest.root_dn_str = ldb_dn_get_linearized(ldb_get_root_basedn(samdb));
+       s->forest.config_dn_str = ldb_dn_get_linearized(ldb_get_config_basedn(samdb));
+       s->forest.schema_dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(samdb));
 
        s->chunk.gensec_skey = &s->gensec_skey;
        s->chunk.partition = &s->partition;
        s->chunk.forest = &s->forest;
        s->chunk.dest_dsa = &s->dest_dsa;
 
-       return PyCObject_FromTallocPtr(s);
+       return pytalloc_CObject_FromTallocPtr(s);
 }
 
 
@@ -405,16 +470,20 @@ static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyOb
  */
 static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       const char *kwnames[] = { "state", "level", "ctr", "schema", NULL };
-       PyObject *py_state, *py_ctr, *py_schema;
+       const char *kwnames[] = { "state", "level", "ctr",
+                                 "schema", "req_level", "req",
+                                 NULL };
+       PyObject *py_state, *py_ctr, *py_schema = Py_None, *py_req = Py_None;
        struct replicate_state *s;
        unsigned level;
+       unsigned req_level = 0;
        NTSTATUS (*chunk_handler)(void *private_data, const struct libnet_BecomeDC_StoreChunk *c);
        NTSTATUS status;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|O",
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIO|OIO",
                                         discard_const_p(char *, kwnames),
-                                        &py_state, &level, &py_ctr, &py_schema)) {
+                                        &py_state, &level, &py_ctr,
+                                        &py_schema, &req_level, &py_req)) {
                return NULL;
        }
 
@@ -429,7 +498,7 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
                if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr1")) {
                        return NULL;
                }
-               s->chunk.ctr1                         = py_talloc_get_ptr(py_ctr);
+               s->chunk.ctr1                         = pytalloc_get_ptr(py_ctr);
                s->partition.nc                       = *s->chunk.ctr1->naming_context;
                s->partition.more_data                = s->chunk.ctr1->more_data;
                s->partition.source_dsa_guid          = s->chunk.ctr1->source_dsa_guid;
@@ -440,7 +509,7 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
                if (!py_check_dcerpc_type(py_ctr, "samba.dcerpc.drsuapi", "DsGetNCChangesCtr6")) {
                        return NULL;
                }
-               s->chunk.ctr6                         = py_talloc_get_ptr(py_ctr);
+               s->chunk.ctr6                         = pytalloc_get_ptr(py_ctr);
                s->partition.nc                       = *s->chunk.ctr6->naming_context;
                s->partition.more_data                = s->chunk.ctr6->more_data;
                s->partition.source_dsa_guid          = s->chunk.ctr6->source_dsa_guid;
@@ -452,6 +521,41 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
                return NULL;
        }
 
+       s->chunk.req5 = NULL;
+       s->chunk.req8 = NULL;
+       s->chunk.req10 = NULL;
+       if (py_req) {
+               switch (req_level) {
+               case 0:
+                       break;
+               case 5:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest5")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req5 = pytalloc_get_ptr(py_req);
+                       break;
+               case 8:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest8")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req8 = pytalloc_get_ptr(py_req);
+                       break;
+               case 10:
+                       if (!py_check_dcerpc_type(py_req, "samba.dcerpc.drsuapi", "DsGetNCChangesRequest10")) {
+                               return NULL;
+                       }
+
+                       s->chunk.req10 = pytalloc_get_ptr(py_req);
+                       break;
+               default:
+                       PyErr_Format(PyExc_TypeError, "Bad req_level %u in replicate_chunk", req_level);
+                       return NULL;
+               }
+       }
+       s->chunk.req_level = req_level;
+
        chunk_handler = libnet_vampire_cb_store_chunk;
        if (py_schema) {
                if (!PyBool_Check(py_schema)) {
@@ -478,23 +582,31 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
 /*
   find a DC given a domain name and server type
  */
-static PyObject *py_net_finddc(py_net_Object *self, PyObject *args)
+static PyObject *py_net_finddc(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       const char *domain_name;
+       const char *domain = NULL, *address = NULL;
        unsigned server_type;
        NTSTATUS status;
        struct finddcs *io;
        TALLOC_CTX *mem_ctx;
        PyObject *ret;
+       const char * const kwnames[] = { "flags", "domain", "address", NULL };
 
-       if (!PyArg_ParseTuple(args, "sI", &domain_name, &server_type)) {
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "I|ss",
+                                        discard_const_p(char *, kwnames),
+                                        &server_type, &domain, &address)) {
                return NULL;
        }
 
        mem_ctx = talloc_new(self->mem_ctx);
 
        io = talloc_zero(mem_ctx, struct finddcs);
-       io->in.domain_name = domain_name;
+       if (domain != NULL) {
+               io->in.domain_name = domain;
+       }
+       if (address != NULL) {
+               io->in.server_address = address;
+       }
        io->in.minimum_dc_flags = server_type;
 
        status = finddcs_cldap(io, io,
@@ -522,11 +634,12 @@ static const char py_net_replicate_init_doc[] = "replicate_init(samdb, lp, drspi
 static const char py_net_replicate_chunk_doc[] = "replicate_chunk(state, level, ctr, schema)\n"
                                         "Process replication for one chunk";
 
-static const char py_net_finddc_doc[] = "finddc(domain, server_type)\n"
-                                        "find a DC with the specified server_type bits. Return the DNS name";
+static const char py_net_finddc_doc[] = "finddc(flags=server_type, domain=None, address=None)\n"
+                                        "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";
 
 static PyMethodDef net_obj_methods[] = {
        {"join_member", (PyCFunction)py_net_join_member, METH_VARARGS|METH_KEYWORDS, py_net_join_member_doc},
+       {"change_password", (PyCFunction)py_net_change_password, METH_VARARGS|METH_KEYWORDS, py_net_change_password_doc},
        {"set_password", (PyCFunction)py_net_set_password, METH_VARARGS|METH_KEYWORDS, py_net_set_password_doc},
        {"export_keytab", (PyCFunction)py_net_export_keytab, METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
        {"time", (PyCFunction)py_net_time, METH_VARARGS|METH_KEYWORDS, py_net_time_doc},
@@ -535,7 +648,7 @@ static PyMethodDef net_obj_methods[] = {
        {"vampire", (PyCFunction)py_net_vampire, METH_VARARGS|METH_KEYWORDS, py_net_vampire_doc},
        {"replicate_init", (PyCFunction)py_net_replicate_init, METH_VARARGS|METH_KEYWORDS, py_net_replicate_init_doc},
        {"replicate_chunk", (PyCFunction)py_net_replicate_chunk, METH_VARARGS|METH_KEYWORDS, py_net_replicate_chunk_doc},
-       {"finddc", (PyCFunction)py_net_finddc, METH_VARARGS, py_net_finddc_doc},
+       {"finddc", (PyCFunction)py_net_finddc, METH_KEYWORDS, py_net_finddc_doc},
        { NULL }
 };