winbind: Speed up wbinfo -p
[samba.git] / source3 / winbindd / winbindd_misc.c
index ac8f1a7dfdaee3aea11fddc10a24ecf92e3fbae9..c101269e93afcb8f9ed500b359803d4f87b2ee85 100644 (file)
 
 #include "includes.h"
 #include "winbindd.h"
-#include "../librpc/gen_ndr/cli_netlogon.h"
+#include "libcli/security/dom_sid.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-/* Constants and helper functions for determining domain trust types */
+static char *get_trust_type_string(TALLOC_CTX *mem_ctx,
+                                  struct winbindd_tdc_domain *tdc,
+                                  struct winbindd_domain *domain)
+{
+       enum netr_SchannelType secure_channel_type = SEC_CHAN_NULL;
+       char *s = NULL;
 
-enum trust_type {
-       EXTERNAL = 0,
-       FOREST,
-       IN_FOREST,
-       NONE,
-};
+       if (domain != NULL) {
+               secure_channel_type = domain->secure_channel_type;
+       }
 
-const char *trust_type_strings[] = {"External", 
-                                   "Forest", 
-                                   "In Forest",
-                                   "None"};
+       switch (secure_channel_type) {
+       case SEC_CHAN_NULL: {
+               if (domain == NULL) {
+                       DBG_ERR("Missing domain [%s]\n",
+                               tdc->domain_name);
+                       return NULL;
+               }
+               if (domain->routing_domain == NULL) {
+                       DBG_ERR("Missing routing for domain [%s]\n",
+                               tdc->domain_name);
+                       return NULL;
+               }
+               s = talloc_asprintf(mem_ctx, "Routed (via %s)",
+                                   domain->routing_domain->name);
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+       }
 
-static enum trust_type get_trust_type(struct winbindd_tdc_domain *domain)
-{
-       if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN)   
-               return EXTERNAL;
-       else if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE)
-               return FOREST;
-       else if (((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) == NETR_TRUST_FLAG_IN_FOREST) &&
-           ((domain->trust_flags & NETR_TRUST_FLAG_PRIMARY) == 0x0))
-               return IN_FOREST;
-       return NONE;    
-}
+       case SEC_CHAN_LOCAL:
+               s = talloc_strdup(mem_ctx, "Local");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
 
-static const char *get_trust_type_string(struct winbindd_tdc_domain *domain)
-{
-       return trust_type_strings[get_trust_type(domain)];
+       case SEC_CHAN_WKSTA:
+               s = talloc_strdup(mem_ctx, "Workstation");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+
+       case SEC_CHAN_BDC: {
+               int role = lp_server_role();
+
+               if (role == ROLE_DOMAIN_PDC) {
+                       s = talloc_strdup(mem_ctx, "PDC");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+
+               if (role == ROLE_DOMAIN_BDC) {
+                       s = talloc_strdup(mem_ctx, "BDC");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+
+               s = talloc_strdup(mem_ctx, "RWDC");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+       }
+
+       case SEC_CHAN_RODC:
+               s = talloc_strdup(mem_ctx, "RODC");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+
+       case SEC_CHAN_DNS_DOMAIN:
+               if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
+                       s = talloc_strdup(mem_ctx, "External");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+               if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
+                       s = talloc_strdup(mem_ctx, "In Forest");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+               if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL) {
+                       s = talloc_strdup(mem_ctx, "External");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+               if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
+                       s = talloc_strdup(mem_ctx, "Forest");
+                       if (s == NULL) {
+                               return NULL;
+                       }
+                       break;
+               }
+               s = talloc_strdup(mem_ctx, "External");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+
+       case SEC_CHAN_DOMAIN:
+               s = talloc_strdup(mem_ctx, "External");
+               if (s == NULL) {
+                       return NULL;
+               }
+               break;
+
+       default:
+               DBG_ERR("Unhandled secure_channel_type %d for domain[%s]\n",
+                       secure_channel_type, tdc->domain_name);
+               return NULL;
+       }
+
+       return s;
 }
 
 static bool trust_is_inbound(struct winbindd_tdc_domain *domain)
 {
-       return (domain->trust_flags == 0x0) ||
-           ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
-            NETR_TRUST_FLAG_IN_FOREST) ||                      
-           ((domain->trust_flags & NETR_TRUST_FLAG_INBOUND) ==
-           NETR_TRUST_FLAG_INBOUND);           
+       if (domain->trust_flags & NETR_TRUST_FLAG_INBOUND) {
+               return true;
+       }
+       return false;
 }
 
 static bool trust_is_outbound(struct winbindd_tdc_domain *domain)
 {
-       return (domain->trust_flags == 0x0) ||
-           ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
-            NETR_TRUST_FLAG_IN_FOREST) ||                      
-           ((domain->trust_flags & NETR_TRUST_FLAG_OUTBOUND) ==
-           NETR_TRUST_FLAG_OUTBOUND);          
+       if (domain->trust_flags & NETR_TRUST_FLAG_OUTBOUND) {
+               return true;
+       }
+       return false;
 }
 
 static bool trust_is_transitive(struct winbindd_tdc_domain *domain)
 {
-       if ((domain->trust_attribs == NETR_TRUST_ATTRIBUTE_NON_TRANSITIVE) ||         
-           (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) ||
-           (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL))
-               return False;
-       return True;
+       bool transitive = false;
+
+       /*
+        * Beware: order matters
+        */
+
+       if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
+               transitive = true;
+       }
+
+       if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
+               transitive = true;
+       }
+
+       if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE) {
+               transitive = false;
+       }
+
+       if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
+               transitive = false;
+       }
+
+       if (domain->trust_flags & NETR_TRUST_FLAG_PRIMARY) {
+               transitive = true;
+       }
+
+       return transitive;
 }
 
 void winbindd_list_trusted_domains(struct winbindd_cli_state *state)
 {
        struct winbindd_tdc_domain *dom_list = NULL;
-       struct winbindd_tdc_domain *d = NULL;
        size_t num_domains = 0;
        int extra_data_len = 0;
        char *extra_data = NULL;
@@ -111,25 +228,37 @@ void winbindd_list_trusted_domains(struct winbindd_cli_state *state)
        for ( i = 0; i < num_domains; i++ ) {
                struct winbindd_domain *domain;
                bool is_online = true;          
+               struct winbindd_tdc_domain *d = NULL;
+               char *trust_type = NULL;
 
                d = &dom_list[i];
                domain = find_domain_from_name_noinit(d->domain_name);
                if (domain) {
                        is_online = domain->online;
                }
+
+               trust_type = get_trust_type_string(talloc_tos(), d, domain);
+               if (trust_type == NULL) {
+                       continue;
+               }
+
                extra_data = talloc_asprintf_append_buffer(
                        extra_data,
                        "%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s\n",
                        d->domain_name,
-                       d->dns_name ? d->dns_name : d->domain_name,
+                       d->dns_name ? d->dns_name : "",
                        sid_string_talloc(state->mem_ctx, &d->sid),
-                       get_trust_type_string(d),
+                       trust_type,
                        trust_is_transitive(d) ? "Yes" : "No",
                        trust_is_inbound(d) ? "Yes" : "No",
                        trust_is_outbound(d) ? "Yes" : "No",
                        is_online ? "Online" : "Offline" );
+
+               TALLOC_FREE(trust_type);
        }
 
+       state->response->data.num_entries = num_domains;
+
        extra_data_len = strlen(extra_data);
        if (extra_data_len > 0) {
 
@@ -158,8 +287,7 @@ enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *
        DEBUG(3, ("[%5lu]: list trusted domains\n",
                  (unsigned long)state->pid));
 
-       result = domain->methods->trusted_domains(domain, state->mem_ctx,
-                                                 &trusts);
+       result = wb_cache_trusted_domains(domain, state->mem_ctx, &trusts);
 
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(3, ("winbindd_dual_list_trusted_domains: trusted_domains returned %s\n",
@@ -170,12 +298,21 @@ enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *
        extra_data = talloc_strdup(state->mem_ctx, "");
 
        for (i=0; i<trusts.count; i++) {
+
+               if (trusts.array[i].sid == NULL) {
+                       continue;
+               }
+               if (dom_sid_equal(trusts.array[i].sid, &global_sid_NULL)) {
+                       continue;
+               }
+
                extra_data = talloc_asprintf_append_buffer(
-                       extra_data, "%s\\%s\\%s\n",
-                       trusts.array[i].netbios_name,
-                       trusts.array[i].dns_name,
-                       sid_string_talloc(state->mem_ctx,
-                                         trusts.array[i].sid));
+                   extra_data, "%s\\%s\\%s\\%u\\%u\\%u\n",
+                   trusts.array[i].netbios_name, trusts.array[i].dns_name,
+                   sid_string_talloc(state->mem_ctx, trusts.array[i].sid),
+                   trusts.array[i].trust_flags,
+                   (uint32_t)trusts.array[i].trust_type,
+                   trusts.array[i].trust_attributes);
        }
 
        /* add our primary domain */
@@ -190,7 +327,9 @@ enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *
        if (state->request->data.list_all_domains && !have_own_domain) {
                extra_data = talloc_asprintf_append_buffer(
                        extra_data, "%s\\%s\\%s\n", domain->name,
-                       domain->alt_name ? domain->alt_name : domain->name,
+                       domain->alt_name != NULL ?
+                               domain->alt_name :
+                               domain->name,
                        sid_string_talloc(state->mem_ctx, &domain->sid));
        }
 
@@ -201,31 +340,12 @@ enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *
                extra_data[extra_data_len-1] = '\0';
 
                state->response->extra_data.data = extra_data;
-               state->response->length += extra_data_len+1;
+               state->response->length += extra_data_len;
        }
 
        return WINBINDD_OK;
 }
 
-/* This is the child-only version of --sequence. It only allows for a single
- * domain (ie "our" one) to be displayed. */
-
-enum winbindd_result winbindd_dual_show_sequence(struct winbindd_domain *domain,
-                                                struct winbindd_cli_state *state)
-{
-       DEBUG(3, ("[%5lu]: show sequence\n", (unsigned long)state->pid));
-
-       /* Ensure null termination */
-       state->request->domain_name[sizeof(state->request->domain_name)-1]='\0';
-
-       domain->methods->sequence_number(domain, &domain->sequence_number);
-
-       state->response->data.sequence_number =
-               domain->sequence_number;
-
-       return WINBINDD_OK;
-}
-
 struct domain_info_state {
        struct winbindd_domain *domain;
        struct winbindd_cli_state *cli;
@@ -284,7 +404,7 @@ void winbindd_domain_info(struct winbindd_cli_state *cli)
         * Send a ping down. This implicitly initializes the domain.
         */
 
-       req = wb_domain_request_send(state, winbind_event_context(),
+       req = wb_domain_request_send(state, server_event_context(),
                                     domain, &state->ping_request);
        if (req == NULL) {
                DEBUG(3, ("wb_domain_request_send failed\n"));
@@ -332,6 +452,62 @@ static void domain_info_done(struct tevent_req *req)
        request_ok(state->cli);
 }
 
+void winbindd_dc_info(struct winbindd_cli_state *cli)
+{
+       struct winbindd_domain *domain;
+       char *dc_name, *dc_ip;
+
+       cli->request->domain_name[sizeof(cli->request->domain_name)-1] = '\0';
+
+       DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
+                 cli->request->domain_name));
+
+       if (cli->request->domain_name[0] != '\0') {
+               domain = find_trust_from_name_noinit(
+                       cli->request->domain_name);
+               if (domain == NULL) {
+                       DEBUG(10, ("Could not find domain %s\n",
+                                  cli->request->domain_name));
+                       request_error(cli);
+                       return;
+               }
+       } else {
+               domain = find_our_domain();
+       }
+
+       if (!fetch_current_dc_from_gencache(
+                   talloc_tos(), domain->name, &dc_name, &dc_ip)) {
+               DEBUG(10, ("fetch_current_dc_from_gencache(%s) failed\n",
+                          domain->name));
+               request_error(cli);
+               return;
+       }
+
+       cli->response->data.num_entries = 1;
+       cli->response->extra_data.data = talloc_asprintf(
+               cli->mem_ctx, "%s\n%s\n", dc_name, dc_ip);
+
+       TALLOC_FREE(dc_name);
+       TALLOC_FREE(dc_ip);
+
+       if (cli->response->extra_data.data == NULL) {
+               request_error(cli);
+               return;
+       }
+
+       /* must add one to length to copy the 0 for string termination */
+       cli->response->length +=
+               strlen((char *)cli->response->extra_data.data) + 1;
+
+       request_ok(cli);
+}
+
+void winbindd_ping(struct winbindd_cli_state *state)
+{
+       DEBUG(3, ("[%5lu]: ping\n", (unsigned long)state->pid));
+       request_ok(state);
+}
+
 /* List various tidbits of information */
 
 void winbindd_info(struct winbindd_cli_state *state)
@@ -348,8 +524,8 @@ void winbindd_info(struct winbindd_cli_state *state)
 
 void winbindd_interface_version(struct winbindd_cli_state *state)
 {
-       DEBUG(3, ("[%5lu]: request interface version\n",
-                 (unsigned long)state->pid));
+       DEBUG(3, ("[%5lu]: request interface version (version = %d)\n",
+                 (unsigned long)state->pid, WINBIND_INTERFACE_VERSION));
 
        state->response->data.interface_version = WINBIND_INTERFACE_VERSION;
        request_ok(state);
@@ -372,7 +548,7 @@ void winbindd_netbios_name(struct winbindd_cli_state *state)
        DEBUG(3, ("[%5lu]: request netbios name\n",
                  (unsigned long)state->pid));
 
-       fstrcpy(state->response->data.netbios_name, global_myname());
+       fstrcpy(state->response->data.netbios_name, lp_netbios_name());
        request_ok(state);
 }