s4-samba-tool: Add --principal argument to samba-tool domain exportkeytab
[kai/samba.git] / source4 / libnet / py_net.c
index 0b1eb7b9164498a7f2d6db2f135da3b7b5e78175..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
 
 
 #include <Python.h>
 #include "includes.h"
+#include <pyldb.h>
 #include "libnet.h"
 #include "auth/credentials/pycredentials.h"
 #include "libcli/security/security.h"
 #include "lib/events/events.h"
-#include "param/param.h"
 #include "param/pyparam.h"
-#include "lib/ldb/pyldb.h"
 #include "auth/gensec/gensec.h"
-#include "librpc/rpc/pyrpc.h"
+#include "librpc/rpc/pyrpc_util.h"
+#include "libcli/resolve/resolve.h"
+#include "libcli/finddc.h"
+#include "dsdb/samdb/samdb.h"
+
+void initnet(void);
 
 typedef struct {
        PyObject_HEAD
@@ -37,22 +42,29 @@ typedef struct {
        struct tevent_context *ev;
 } py_net_Object;
 
-static PyObject *py_net_join(py_net_Object *self, PyObject *args, PyObject *kwargs)
+static PyObject *py_net_join_member(py_net_Object *self, PyObject *args, PyObject *kwargs)
 {
-       struct libnet_Join r;
+       struct libnet_Join_member r;
+       int _level = 0;
        NTSTATUS status;
        PyObject *result;
        TALLOC_CTX *mem_ctx;
-       const char *kwnames[] = { "domain_name", "netbios_name", "join_type", "level", NULL };
+       const char *kwnames[] = { "domain_name", "netbios_name", "level", NULL };
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssii:Join", discard_const_p(char *, kwnames), 
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ssi:Join", discard_const_p(char *, kwnames),
                                         &r.in.domain_name, &r.in.netbios_name, 
-                                        &r.in.join_type, &r.in.level))
+                                        &_level)) {
                return NULL;
+       }
+       r.in.level = _level;
 
        mem_ctx = talloc_new(self->mem_ctx);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
 
-       status = libnet_Join(self->libnet_ctx, mem_ctx, &r);
+       status = libnet_Join_member(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
                PyErr_SetString(PyExc_RuntimeError, r.out.error_string?r.out.error_string:nt_errstr(status));
                talloc_free(mem_ctx);
@@ -68,30 +80,88 @@ static PyObject *py_net_join(py_net_Object *self, PyObject *args, PyObject *kwar
        return result;
 }
 
-static const char py_net_join_doc[] = "join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name)\n\n" \
+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;
        }
 
        /* 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_SetPassword(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
@@ -118,15 +188,21 @@ 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;
        }
 
        mem_ctx = talloc_new(self->mem_ctx);
+       if (mem_ctx == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
 
        status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
        if (NT_STATUS_IS_ERR(status)) {
@@ -268,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)
@@ -279,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;
@@ -347,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);
@@ -364,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",
@@ -372,12 +451,17 @@ static PyObject *py_net_replicate_init(py_net_Object *self, PyObject *args, PyOb
                return NULL;
        }
 
+       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);
 }
 
 
@@ -386,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;
        }
 
@@ -407,11 +495,10 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
 
        switch (level) {
        case 1:
-               if (strcmp("drsuapi.DsGetNCChangesCtr1", Py_TYPE(py_ctr)->tp_name) != 0) {
-                       PyErr_SetString(PyExc_TypeError, "Expected DsGetNCChangesCtr1 type for ctr");
+               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;
@@ -419,11 +506,10 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
                s->partition.highwatermark            = s->chunk.ctr1->new_highwatermark;
                break;
        case 6:
-               if (strcmp("drsuapi.DsGetNCChangesCtr6", Py_TYPE(py_ctr)->tp_name) != 0) {
-                       PyErr_SetString(PyExc_TypeError, "Expected DsGetNCChangesCtr6 type for ctr");
+               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;
@@ -435,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)) {
@@ -454,9 +575,56 @@ static PyObject *py_net_replicate_chunk(py_net_Object *self, PyObject *args, PyO
                return NULL;
        }
 
-       return Py_None;
+       Py_RETURN_NONE;
+}
+
+
+/*
+  find a DC given a domain name and server type
+ */
+static PyObject *py_net_finddc(py_net_Object *self, PyObject *args, PyObject *kwargs)
+{
+       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_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);
+       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,
+                              lpcfg_resolve_context(self->libnet_ctx->lp_ctx), self->ev);
+       if (NT_STATUS_IS_ERR(status)) {
+               PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+       ret = py_return_ndr_struct("samba.dcerpc.nbt", "NETLOGON_SAM_LOGON_RESPONSE_EX",
+                                  io, &io->out.netlogon.data.nt5_ex);
+       talloc_free(mem_ctx);
+
+       return ret;
 }
 
+
 static const char py_net_vampire_doc[] = "vampire(domain, target_dir=None)\n"
                                         "Vampire a domain.";
 
@@ -466,8 +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(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", (PyCFunction)py_net_join, METH_VARARGS|METH_KEYWORDS, py_net_join_doc},
+       {"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},
@@ -476,23 +648,27 @@ 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_KEYWORDS, py_net_finddc_doc},
        { NULL }
 };
 
 static void py_net_dealloc(py_net_Object *self)
 {
        talloc_free(self->mem_ctx);
+       PyObject_Del(self);
 }
 
 static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
        PyObject *py_creds, *py_lp = Py_None;
-       const char *kwnames[] = { "creds", "lp", NULL };
+       const char *kwnames[] = { "creds", "lp", "server", NULL };
        py_net_Object *ret;
        struct loadparm_context *lp;
+       const char *server_address = NULL;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", 
-                       discard_const_p(char *, kwnames), &py_creds, &py_lp))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oz",
+                                        discard_const_p(char *, kwnames), &py_creds, &py_lp,
+                                        &server_address))
                return NULL;
 
        ret = PyObject_New(py_net_Object, type);
@@ -518,6 +694,8 @@ static PyObject *net_obj_new(PyTypeObject *type, PyObject *args, PyObject *kwarg
                return NULL;
        }
 
+       ret->libnet_ctx->server_address = server_address;
+
        ret->libnet_ctx->cred = cli_credentials_from_py_object(py_creds);
        if (ret->libnet_ctx->cred == NULL) {
                PyErr_SetString(PyExc_TypeError, "Expected credentials object");