Finish removal of iconv_convenience in public API's.
[bbaumbach/samba-autobuild/.git] / source4 / libnet / libnet_lookup.c
index e8bceb9218f64f5e35c42879b2ed1e76b5ef2a79..21851d5ae89ef7616160ad2ea7714a1e3d16199d 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;
@@ -56,24 +57,16 @@ struct composite_context *libnet_Lookup_send(struct libnet_context *ctx,
        struct composite_context *c;
        struct lookup_state *s;
        struct composite_context *cresolve_req;
-       const char** methods;
+       struct resolve_context *resolve_ctx;
 
        /* 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,24 +75,23 @@ 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;
 
        /* name resolution methods */
-       if (io->in.methods) {
-               methods = io->in.methods;
+       if (io->in.resolve_ctx) {
+               resolve_ctx = io->in.resolve_ctx;
        } else {
-               methods = ctx->name_res_methods;
+               resolve_ctx = ctx->resolve_ctx;
        }
 
-       c->private_data = s;
-       c->state        = COMPOSITE_STATE_IN_PROGRESS;
-
        /* send resolve request */
-       cresolve_req = resolve_name_send(&s->hostname, c->event_ctx, methods);
+       cresolve_req = resolve_name_send(resolve_ctx, s, &s->hostname, c->event_ctx);
+       if (composite_nomem(cresolve_req, c)) return c;
 
        composite_continue(c, cresolve_req, continue_name_resolved, c);
-
        return c;
 }
 
@@ -137,7 +129,7 @@ NTSTATUS libnet_Lookup_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
        if (NT_STATUS_IS_OK(status)) {
                s = talloc_get_type(c->private_data, struct lookup_state);
 
-               io->out.address = str_list_make(mem_ctx, s->address, NULL);
+               io->out.address = (const char **)str_list_make_single(mem_ctx, s->address);
                NT_STATUS_HAVE_NO_MEMORY(io->out.address);
        }
 
@@ -199,10 +191,15 @@ 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);
-
-       c = finddcs_send(mem_ctx, io->in.domain_name, io->in.name_type,
-                        NULL, ctx->name_res_methods, ctx->event_ctx, msg_ctx);
+       struct messaging_context *msg_ctx = 
+               messaging_client_init(mem_ctx,
+                                                         lp_messaging_path(mem_ctx, ctx->lp_ctx), 
+                                                         ctx->event_ctx);
+
+       c = finddcs_send(mem_ctx, lp_netbios_name(ctx->lp_ctx),
+                                        lp_nbt_port(ctx->lp_ctx), io->in.domain_name,
+                                        io->in.name_type, NULL, ctx->resolve_ctx,
+                                        ctx->event_ctx, msg_ctx);
        return c;
 }
 
@@ -227,45 +224,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
  */
@@ -295,7 +253,7 @@ static bool prepare_lookup_params(struct libnet_context *ctx,
                                  struct composite_context *c,
                                  struct lookup_name_state *s);
 static void continue_lookup_name(struct composite_context *ctx);
-static void continue_name_found(struct rpc_request *req);
+static void continue_name_found(struct tevent_req *subreq);
 
 
 struct composite_context* libnet_LookupName_send(struct libnet_context *ctx,
@@ -305,8 +263,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;
+       struct tevent_req *subreq;
+       bool prereq_met = false;
 
        c = composite_create(mem_ctx, ctx->event_ctx);
        if (c == NULL) return NULL;
@@ -320,16 +278,18 @@ 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;
 
-       lookup_req = dcerpc_lsa_LookupNames_send(ctx->lsa.pipe, c, &s->lookup);
-       if (composite_nomem(lookup_req, c)) return c;
+       subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
+                                              ctx->lsa.pipe->binding_handle,
+                                              &s->lookup);
+       if (composite_nomem(subreq, c)) return c;
 
-       composite_continue_rpc(c, lookup_req, continue_name_found, c);
+       tevent_req_set_callback(subreq, continue_name_found, c);
        return c;
 }
 
@@ -355,6 +315,8 @@ static bool prepare_lookup_params(struct libnet_context *ctx,
        s->lookup.in.count     = &s->count;
        s->lookup.out.count    = &s->count;
        s->lookup.out.sids     = &s->sids;
+       s->lookup.out.domains  = talloc_zero(ctx, struct lsa_RefDomainList *);
+       if (composite_nomem(s->lookup.out.domains, c)) return false;
        
        return true;
 }
@@ -364,7 +326,7 @@ static void continue_lookup_name(struct composite_context *ctx)
 {
        struct composite_context *c;
        struct lookup_name_state *s;
-       struct rpc_request *lookup_req;
+       struct tevent_req *subreq;
 
        c = talloc_get_type(ctx->async.private_data, struct composite_context);
        s = talloc_get_type(c->private_data, struct lookup_name_state);
@@ -374,22 +336,25 @@ static void continue_lookup_name(struct composite_context *ctx)
        
        if (!prepare_lookup_params(s->ctx, c, s)) return;
 
-       lookup_req = dcerpc_lsa_LookupNames_send(s->ctx->lsa.pipe, c, &s->lookup);
-       if (composite_nomem(lookup_req, c)) return;
+       subreq = dcerpc_lsa_LookupNames_r_send(s, c->event_ctx,
+                                              s->ctx->lsa.pipe->binding_handle,
+                                              &s->lookup);
+       if (composite_nomem(subreq, c)) return;
        
-       composite_continue_rpc(c, lookup_req, continue_name_found, c);
+       tevent_req_set_callback(subreq, continue_name_found, c);
 }
 
 
-static void continue_name_found(struct rpc_request *req)
+static void continue_name_found(struct tevent_req *subreq)
 {
        struct composite_context *c;
        struct lookup_name_state *s;
 
-       c = talloc_get_type(req->async.private, struct composite_context);
+       c = tevent_req_callback_data(subreq, struct composite_context);
        s = talloc_get_type(c->private_data, struct lookup_name_state);
 
-       c->status = dcerpc_ndr_request_recv(req);
+       c->status = dcerpc_lsa_LookupNames_r_recv(subreq, s);
+       TALLOC_FREE(subreq);
        if (!composite_is_ok(c)) return;
 
        c->status = s->lookup.out.result;
@@ -415,11 +380,13 @@ NTSTATUS libnet_LookupName_recv(struct composite_context *c, TALLOC_CTX *mem_ctx
                io->out.sidstr = NULL;
 
                if (*s->lookup.out.count > 0) {
-                       struct lsa_RefDomainList *domains = s->lookup.out.domains;
+                       struct lsa_RefDomainList *domains = *s->lookup.out.domains;
                        struct lsa_TransSidArray *sids = s->lookup.out.sids;
 
                        if (domains == NULL || sids == NULL) {
-                               composite_error(c, NT_STATUS_UNSUCCESSFUL);
+                               status = NT_STATUS_UNSUCCESSFUL;
+                               io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
+                               goto done;
                        }
 
                        if (sids->count > 0) {
@@ -440,6 +407,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;
 }