LCOV - code coverage report
Current view: top level - source3/winbindd - winbindd_dsgetdcname.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 57 72 79.2 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    async implementation of WINBINDD_DSGETDCNAME
       4             :    Copyright (C) Volker Lendecke 2009
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "util/debug.h"
      22             : #include "winbindd.h"
      23             : #include "librpc/gen_ndr/ndr_winbind_c.h"
      24             : #include "lib/util/string_wrappers.h"
      25             : 
      26             : struct winbindd_dsgetdcname_state {
      27             :         struct GUID guid;
      28             :         struct netr_DsRGetDCNameInfo *dc_info;
      29             : };
      30             : 
      31             : static uint32_t get_dsgetdc_flags(uint32_t wbc_flags);
      32             : static void winbindd_dsgetdcname_done(struct tevent_req *subreq);
      33             : 
      34          10 : struct tevent_req *winbindd_dsgetdcname_send(TALLOC_CTX *mem_ctx,
      35             :                                              struct tevent_context *ev,
      36             :                                              struct winbindd_cli_state *cli,
      37             :                                              struct winbindd_request *request)
      38             : {
      39           0 :         struct tevent_req *req, *subreq;
      40          10 :         struct dcerpc_binding_handle *child_binding_handle = NULL;
      41           0 :         struct winbindd_dsgetdcname_state *state;
      42          10 :         struct GUID *guid_ptr = NULL;
      43          10 :         uint32_t ds_flags = 0;
      44           0 :         NTSTATUS status;
      45             : 
      46          10 :         req = tevent_req_create(mem_ctx, &state,
      47             :                                 struct winbindd_dsgetdcname_state);
      48          10 :         if (req == NULL) {
      49           0 :                 return NULL;
      50             :         }
      51             : 
      52          10 :         D_NOTICE("[%s (%u)] Winbind external command DSGETDCNAME start.\n",
      53             :                  cli->client_name,
      54             :                  (unsigned int)cli->pid);
      55             : 
      56           0 :         request->data.dsgetdcname.domain_name
      57          10 :                 [sizeof(request->data.dsgetdcname.domain_name)-1] = '\0';
      58           0 :         request->data.dsgetdcname.site_name
      59          10 :                 [sizeof(request->data.dsgetdcname.site_name)-1] = '\0';
      60           0 :         request->data.dsgetdcname.domain_guid
      61          10 :                 [sizeof(request->data.dsgetdcname.domain_guid)-1] = '\0';
      62             : 
      63          10 :         D_NOTICE("Calling DsGetDcName for domain '%s'.\n",
      64             :                  request->data.dsgetdcname.domain_name);
      65             : 
      66          10 :         ds_flags = get_dsgetdc_flags(request->data.dsgetdcname.flags);
      67             : 
      68          10 :         status = GUID_from_string(request->data.dsgetdcname.domain_guid,
      69          10 :                                   &state->guid);
      70          10 :         if (NT_STATUS_IS_OK(status) && !GUID_all_zero(&state->guid)) {
      71           0 :                 guid_ptr = &state->guid;
      72             :         }
      73             : 
      74          10 :         child_binding_handle = locator_child_handle();
      75             : 
      76          10 :         subreq = dcerpc_wbint_DsGetDcName_send(
      77             :                 state, ev, child_binding_handle,
      78          10 :                 request->data.dsgetdcname.domain_name, guid_ptr,
      79          10 :                 request->data.dsgetdcname.site_name,
      80          10 :                 ds_flags, &state->dc_info);
      81          10 :         if (tevent_req_nomem(subreq, req)) {
      82           0 :                 return tevent_req_post(req, ev);
      83             :         }
      84          10 :         tevent_req_set_callback(subreq, winbindd_dsgetdcname_done, req);
      85          10 :         return req;
      86             : }
      87             : 
      88          10 : static void winbindd_dsgetdcname_done(struct tevent_req *subreq)
      89             : {
      90          10 :         struct tevent_req *req = tevent_req_callback_data(
      91             :                 subreq, struct tevent_req);
      92          10 :         struct winbindd_dsgetdcname_state *state = tevent_req_data(
      93             :                 req, struct winbindd_dsgetdcname_state);
      94           0 :         NTSTATUS status, result;
      95             : 
      96          10 :         status = dcerpc_wbint_DsGetDcName_recv(subreq, state, &result);
      97          10 :         TALLOC_FREE(subreq);
      98          10 :         if (any_nt_status_not_ok(status, result, &status)) {
      99           6 :                 tevent_req_nterror(req, status);
     100           6 :                 return;
     101             :         }
     102           4 :         tevent_req_done(req);
     103             : }
     104             : 
     105          10 : NTSTATUS winbindd_dsgetdcname_recv(struct tevent_req *req,
     106             :                                    struct winbindd_response *response)
     107             : {
     108          10 :         struct winbindd_dsgetdcname_state *state = tevent_req_data(
     109             :                 req, struct winbindd_dsgetdcname_state);
     110           0 :         struct GUID_txt_buf guid_str_buf;
     111           0 :         char *guid_str;
     112           0 :         NTSTATUS status;
     113             : 
     114          10 :         D_NOTICE("Winbind external command DSGETDCNAME end.\n");
     115          10 :         if (tevent_req_is_nterror(req, &status)) {
     116           6 :                 D_WARNING("Failed with: %s\n", nt_errstr(status));
     117           6 :                 return status;
     118             :         }
     119             : 
     120           4 :         fstrcpy(response->data.dsgetdcname.dc_unc,
     121             :                 state->dc_info->dc_unc);
     122           4 :         fstrcpy(response->data.dsgetdcname.dc_address,
     123             :                 state->dc_info->dc_address);
     124           4 :         response->data.dsgetdcname.dc_address_type =
     125           4 :                 state->dc_info->dc_address_type;
     126             : 
     127           4 :         guid_str = GUID_buf_string(&state->dc_info->domain_guid,
     128             :                                    &guid_str_buf);
     129           4 :         fstrcpy(response->data.dsgetdcname.domain_guid, guid_str);
     130             : 
     131           4 :         fstrcpy(response->data.dsgetdcname.domain_name,
     132             :                 state->dc_info->domain_name);
     133           4 :         fstrcpy(response->data.dsgetdcname.forest_name,
     134             :                 state->dc_info->forest_name);
     135           4 :         response->data.dsgetdcname.dc_flags = state->dc_info->dc_flags;
     136           4 :         fstrcpy(response->data.dsgetdcname.dc_site_name,
     137             :                 state->dc_info->dc_site_name);
     138           4 :         fstrcpy(response->data.dsgetdcname.client_site_name,
     139             :                 state->dc_info->client_site_name);
     140             : 
     141           4 :         return NT_STATUS_OK;
     142             : }
     143             : 
     144          10 : static uint32_t get_dsgetdc_flags(uint32_t wbc_flags)
     145             : {
     146           0 :         struct wbc_flag_map {
     147             :                 uint32_t wbc_dc_flag;
     148             :                 uint32_t ds_dc_flags;
     149          10 :         } lookup_dc_flags[] = {
     150             :                 { WBC_LOOKUP_DC_FORCE_REDISCOVERY,
     151             :                   DS_FORCE_REDISCOVERY },
     152             :                 { WBC_LOOKUP_DC_DS_REQUIRED,
     153             :                   DS_DIRECTORY_SERVICE_REQUIRED },
     154             :                 { WBC_LOOKUP_DC_DS_PREFERRED,
     155             :                   DS_DIRECTORY_SERVICE_PREFERRED},
     156             :                 { WBC_LOOKUP_DC_GC_SERVER_REQUIRED,
     157             :                   DS_GC_SERVER_REQUIRED },
     158             :                 { WBC_LOOKUP_DC_PDC_REQUIRED,
     159             :                   DS_PDC_REQUIRED},
     160             :                 { WBC_LOOKUP_DC_BACKGROUND_ONLY,
     161             :                   DS_BACKGROUND_ONLY  },
     162             :                 { WBC_LOOKUP_DC_IP_REQUIRED,
     163             :                   DS_IP_REQUIRED },
     164             :                 { WBC_LOOKUP_DC_KDC_REQUIRED,
     165             :                   DS_KDC_REQUIRED },
     166             :                 { WBC_LOOKUP_DC_TIMESERV_REQUIRED,
     167             :                   DS_TIMESERV_REQUIRED },
     168             :                 { WBC_LOOKUP_DC_WRITABLE_REQUIRED,
     169             :                   DS_WRITABLE_REQUIRED },
     170             :                 { WBC_LOOKUP_DC_GOOD_TIMESERV_PREFERRED,
     171             :                   DS_GOOD_TIMESERV_PREFERRED },
     172             :                 { WBC_LOOKUP_DC_AVOID_SELF,
     173             :                   DS_AVOID_SELF },
     174             :                 { WBC_LOOKUP_DC_ONLY_LDAP_NEEDED,
     175             :                   DS_ONLY_LDAP_NEEDED },
     176             :                 { WBC_LOOKUP_DC_IS_FLAT_NAME,
     177             :                   DS_IS_FLAT_NAME },
     178             :                 { WBC_LOOKUP_DC_IS_DNS_NAME,
     179             :                   DS_IS_DNS_NAME },
     180             :                 { WBC_LOOKUP_DC_TRY_NEXTCLOSEST_SITE,
     181             :                   DS_TRY_NEXTCLOSEST_SITE },
     182             :                 { WBC_LOOKUP_DC_DS_6_REQUIRED,
     183             :                   DS_DIRECTORY_SERVICE_6_REQUIRED },
     184             :                 { WBC_LOOKUP_DC_RETURN_DNS_NAME,
     185             :                   DS_RETURN_DNS_NAME },
     186             :                 { WBC_LOOKUP_DC_RETURN_FLAT_NAME,
     187             :                   DS_RETURN_FLAT_NAME }
     188             :         };
     189             : 
     190          10 :         uint32_t ds_flags = 0;
     191          10 :         size_t i = 0;
     192             : 
     193         200 :         for (i=0; i<ARRAY_SIZE(lookup_dc_flags); i++) {
     194         190 :                 if (wbc_flags & lookup_dc_flags[i].wbc_dc_flag) {
     195           0 :                         ds_flags |= lookup_dc_flags[i].ds_dc_flags;
     196             :                 }
     197             :         }
     198             : 
     199          10 :         return ds_flags;
     200             : }

Generated by: LCOV version 1.14