Fix #6130: Don't crash in winbindd_rpc lookup_groupmem() on unmapped members
[ira/wip.git] / source3 / winbindd / winbindd_locator.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - miscellaneous other functions
5
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Andrew Bartlett 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29
30 static const struct winbindd_child_dispatch_table locator_dispatch_table[];
31
32 static struct winbindd_child static_locator_child;
33
34 void init_locator_child(void)
35 {
36         setup_child(&static_locator_child,
37                     locator_dispatch_table,
38                     "log.winbindd", "locator");
39 }
40
41 struct winbindd_child *locator_child(void)
42 {
43         return &static_locator_child;
44 }
45
46 void winbindd_dsgetdcname(struct winbindd_cli_state *state)
47 {
48         state->request.data.dsgetdcname.domain_name
49                 [sizeof(state->request.data.dsgetdcname.domain_name)-1] = '\0';
50         state->request.data.dsgetdcname.site_name
51                 [sizeof(state->request.data.dsgetdcname.site_name)-1] = '\0';
52         state->request.data.dsgetdcname.domain_guid
53                 [sizeof(state->request.data.dsgetdcname.domain_guid)-1] = '\0';
54
55         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
56                   state->request.data.dsgetdcname.domain_name));
57
58         sendto_child(state, locator_child());
59 }
60
61 struct wbc_flag_map {
62         uint32_t wbc_dc_flag;
63         uint32_t ds_dc_flags;
64 };
65
66 static uint32_t get_dsgetdc_flags(uint32_t wbc_flags)
67 {
68         struct wbc_flag_map lookup_dc_flags[] = {
69                 { WBC_LOOKUP_DC_FORCE_REDISCOVERY, DS_FORCE_REDISCOVERY },
70                 { WBC_LOOKUP_DC_DS_REQUIRED, DS_DIRECTORY_SERVICE_REQUIRED },
71                 { WBC_LOOKUP_DC_DS_PREFERRED, DS_DIRECTORY_SERVICE_PREFERRED},
72                 { WBC_LOOKUP_DC_GC_SERVER_REQUIRED, DS_GC_SERVER_REQUIRED },
73                 { WBC_LOOKUP_DC_PDC_REQUIRED,  DS_PDC_REQUIRED},
74                 { WBC_LOOKUP_DC_BACKGROUND_ONLY, DS_BACKGROUND_ONLY  },
75                 { WBC_LOOKUP_DC_IP_REQUIRED, DS_IP_REQUIRED },
76                 { WBC_LOOKUP_DC_KDC_REQUIRED, DS_KDC_REQUIRED },
77                 { WBC_LOOKUP_DC_TIMESERV_REQUIRED, DS_TIMESERV_REQUIRED },
78                 { WBC_LOOKUP_DC_WRITABLE_REQUIRED,  DS_WRITABLE_REQUIRED },
79                 { WBC_LOOKUP_DC_GOOD_TIMESERV_PREFERRED, DS_GOOD_TIMESERV_PREFERRED },
80                 { WBC_LOOKUP_DC_AVOID_SELF, DS_AVOID_SELF },
81                 { WBC_LOOKUP_DC_ONLY_LDAP_NEEDED, DS_ONLY_LDAP_NEEDED },
82                 { WBC_LOOKUP_DC_IS_FLAT_NAME, DS_IS_FLAT_NAME },
83                 { WBC_LOOKUP_DC_IS_DNS_NAME, DS_IS_DNS_NAME },
84                 { WBC_LOOKUP_DC_TRY_NEXTCLOSEST_SITE, DS_TRY_NEXTCLOSEST_SITE },
85                 { WBC_LOOKUP_DC_DS_6_REQUIRED, DS_DIRECTORY_SERVICE_6_REQUIRED },
86                 { WBC_LOOKUP_DC_RETURN_DNS_NAME, DS_RETURN_DNS_NAME },
87                 { WBC_LOOKUP_DC_RETURN_FLAT_NAME, DS_RETURN_FLAT_NAME }
88         };
89         uint32_t ds_flags = 0;
90         int i = 0 ;
91         int num_entries = sizeof(lookup_dc_flags) / sizeof(struct wbc_flag_map);
92
93         for (i=0; i<num_entries; i++) {
94                 if (wbc_flags & lookup_dc_flags[i].wbc_dc_flag)
95                         ds_flags |= lookup_dc_flags[i].ds_dc_flags;
96         }
97
98         return ds_flags;
99 }
100
101 static enum winbindd_result dual_dsgetdcname(struct winbindd_domain *domain,
102                                              struct winbindd_cli_state *state)
103 {
104         NTSTATUS result;
105         struct netr_DsRGetDCNameInfo *info = NULL;
106         uint32_t ds_flags = 0;
107         struct GUID guid, *guid_ptr = NULL;
108         const char *guid_str = NULL;
109
110         state->request.data.dsgetdcname.domain_name
111                 [sizeof(state->request.data.dsgetdcname.domain_name)-1] = '\0';
112         state->request.data.dsgetdcname.site_name
113                 [sizeof(state->request.data.dsgetdcname.site_name)-1] = '\0';
114         state->request.data.dsgetdcname.domain_guid
115                 [sizeof(state->request.data.dsgetdcname.domain_guid)-1] = '\0';
116
117         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
118                   state->request.data.dsgetdcname.domain_name));
119
120         ds_flags = get_dsgetdc_flags(state->request.flags);
121
122         result = GUID_from_string(state->request.data.dsgetdcname.domain_guid,
123                                   &guid);
124         if (NT_STATUS_IS_OK(result) && !GUID_all_zero(&guid)) {
125                 guid_ptr = &guid;
126         }
127
128         result = dsgetdcname(state->mem_ctx,
129                              winbind_messaging_context(),
130                              state->request.data.dsgetdcname.domain_name,
131                              guid_ptr,
132                              state->request.data.dsgetdcname.site_name,
133                              ds_flags,
134                              &info);
135
136         if (!NT_STATUS_IS_OK(result)) {
137                 return WINBINDD_ERROR;
138         }
139
140         guid_str = GUID_string(state->mem_ctx, &info->domain_guid);
141         if (!guid_str) {
142                 return WINBINDD_ERROR;
143         }
144
145         fstrcpy(state->response.data.dsgetdcname.dc_unc, info->dc_unc);
146         fstrcpy(state->response.data.dsgetdcname.dc_address, info->dc_address);
147         state->response.data.dsgetdcname.dc_address_type = info->dc_address_type;
148         fstrcpy(state->response.data.dsgetdcname.domain_guid, guid_str);
149         fstrcpy(state->response.data.dsgetdcname.domain_name, info->domain_name);
150         fstrcpy(state->response.data.dsgetdcname.forest_name, info->forest_name);
151         state->response.data.dsgetdcname.dc_flags = info->dc_flags;
152         fstrcpy(state->response.data.dsgetdcname.dc_site_name, info->dc_site_name);
153         fstrcpy(state->response.data.dsgetdcname.client_site_name, info->client_site_name);
154
155         return WINBINDD_OK;
156 }
157
158 static const struct winbindd_child_dispatch_table locator_dispatch_table[] = {
159         {
160                 .name           = "DSGETDCNAME",
161                 .struct_cmd     = WINBINDD_DSGETDCNAME,
162                 .struct_fn      = dual_dsgetdcname,
163         },{
164                 .name           = NULL,
165         }
166 };