r25446: Merge some changes I made on the way home from SFO:
[jelmer/samba4-debian.git] / source / libnet / libnet_lookup.c
index 0efda326033ba710b0becad7785bcefa17a009be..cac7086a60c7c05c2d9cba214ee95d3381089f53 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -35,6 +34,8 @@
 #include "librpc/gen_ndr/lsa.h"
 #include "librpc/gen_ndr/ndr_lsa_c.h"
 
+#include "param/param.h"
+
 struct lookup_state {
        struct nbt_name hostname;
        const char *address;
@@ -59,21 +60,13 @@ struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
        const char** methods;
 
        /* allocate context and state structures */
-       c = talloc_zero(NULL, struct composite_context);
+       c = composite_create(ctx, ctx->event_ctx);
        if (c == NULL) return NULL;
 
        s = talloc_zero(c, struct lookup_state);
-       if (s == NULL) {
-               composite_error(c, NT_STATUS_NO_MEMORY);
-               return c;
-       }
-       
-       /* prepare event context */
-       c->event_ctx = event_context_find(c);
-       if (c->event_ctx == NULL) {
-               composite_error(c, NT_STATUS_NO_MEMORY);
-               return c;
-       }
+       if (composite_nomem(s, c)) return c;
+
+       c->private_data = s;
 
        if (io == NULL || io->in.hostname == NULL) {
                composite_error(c, NT_STATUS_INVALID_PARAMETER);
@@ -82,6 +75,8 @@ struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
 
        /* parameters */
        s->hostname.name   = talloc_strdup(s, io->in.hostname);
+       if (composite_nomem(s->hostname.name, c)) return c;
+
        s->hostname.type   = io->in.type;
        s->hostname.scope  = NULL;
 
@@ -92,14 +87,11 @@ struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
                methods = ctx->name_res_methods;
        }
 
-       c->private_data = s;
-       c->state        = COMPOSITE_STATE_IN_PROGRESS;
-
        /* send resolve request */
        cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
+       if (composite_nomem(cresolve_req, c)) return c;
 
        composite_continue(c, cresolve_req, continue_name_resolved, c);
-
        return c;
 }
 
@@ -199,7 +191,8 @@ struct composite_context* libnet_LookupDCs_send(struct libnet_context *ctx,
                                                struct libnet_LookupDCs *io)
 {
        struct composite_context *c;
-       struct messaging_context *msg_ctx = messaging_client_init(mem_ctx, ctx->event_ctx);
+       struct messaging_context *msg_ctx = 
+               messaging_client_init(mem_ctx, lp_messaging_path(mem_ctx, global_loadparm), ctx->event_ctx);
 
        c = finddcs_send(mem_ctx, io->in.domain_name, io->in.name_type,
                         NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
@@ -227,45 +220,6 @@ NTSTATUS libnet_LookupDCs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
 }
 
 
-static struct composite_context* lsa_policy_opened(struct libnet_context *ctx,
-                                                  const char *domain_name,
-                                                  struct composite_context *parent_ctx,
-                                                  struct libnet_DomainOpen *domain_open,
-                                                  void (*continue_fn)(struct composite_context*),
-                                                  void (*monitor)(struct monitor_msg*))
-{
-       struct composite_context *domopen_req;
-
-       if (domain_name == NULL) {
-               if (policy_handle_empty(&ctx->lsa.handle)) {
-                       domain_open->in.type        = DOMAIN_LSA;
-                       domain_open->in.domain_name = cli_credentials_get_domain(ctx->cred);
-                       domain_open->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
-
-               } else {
-                       composite_error(parent_ctx, NT_STATUS_INVALID_PARAMETER);
-                       return parent_ctx;
-               }
-       } else {
-               if (policy_handle_empty(&ctx->lsa.handle) ||
-                   !strequal(domain_name, ctx->lsa.name)) {
-                       domain_open->in.type        = DOMAIN_LSA;
-                       domain_open->in.domain_name = domain_name;
-                       domain_open->in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
-
-               } else {
-                       return NULL;
-               }
-       }
-
-       domopen_req = libnet_DomainOpen_send(ctx, domain_open, monitor);
-       if (composite_nomem(domopen_req, parent_ctx)) return parent_ctx;
-
-       composite_continue(parent_ctx, domopen_req, continue_fn, parent_ctx);
-       return parent_ctx;
-}
-
-
 /**
  * Synchronous version of LookupDCs
  */
@@ -305,8 +259,8 @@ struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
 {
        struct composite_context *c;
        struct lookup_name_state *s;
-       struct composite_context *prereq_ctx;
        struct rpc_request *lookup_req;
+       BOOL prereq_met = False;
 
        c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return NULL;
@@ -320,9 +274,9 @@ struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
        s->monitor_fn = monitor;
        s->ctx = ctx;
 
-       prereq_ctx = lsa_policy_opened(ctx, io->in.domain_name, c, &s->domopen,
+       prereq_met = lsa_domain_opened(ctx, io->in.domain_name, &c, &s->domopen,
                                       continue_lookup_name, monitor);
-       if (prereq_ctx) return prereq_ctx;
+       if (!prereq_met) return c;
 
        if (!prepare_lookup_params(ctx, c, s)) return c;
 
@@ -386,13 +340,14 @@ static void continue_name_found(struct rpc_request *req)
        struct composite_context *c;
        struct lookup_name_state *s;
 
-       c = talloc_get_type(req->async.private, struct composite_context);
+       c = talloc_get_type(req->async.private_data, struct composite_context);
        s = talloc_get_type(c->private_data, struct lookup_name_state);
 
        c->status = dcerpc_ndr_request_recv(req);
        if (!composite_is_ok(c)) return;
 
        c->status = s->lookup.out.result;
+       if (!composite_is_ok(c)) return;
 
        composite_done(c);
 }
@@ -417,7 +372,11 @@ NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx
                        struct lsa_RefDomainList *domains = s->lookup.out.domains;
                        struct lsa_TransSidArray *sids = s->lookup.out.sids;
 
-                       /* TODO: verify if returned pointers are non-null */
+                       if (domains == NULL || sids == NULL) {
+                               status = NT_STATUS_UNSUCCESSFUL;
+                               io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
+                               goto done;
+                       }
 
                        if (sids->count > 0) {
                                io->out.rid        = sids->sids[0].rid;
@@ -437,6 +396,7 @@ NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx
                io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
        }
 
+done:
        talloc_free(c);
        return status;
 }