Remove "idmap alloc config : range" parameter
[amitay/samba.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.domain_name
49                 [sizeof(state->request.domain_name)-1] = '\0';
50
51         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
52                   state->request.domain_name));
53
54         sendto_child(state, locator_child());
55 }
56
57 struct wbc_flag_map {
58         uint32_t wbc_dc_flag;
59         uint32_t ds_dc_flags;
60 };
61
62 static uint32_t get_dsgetdc_flags(uint32_t wbc_flags)
63 {
64         struct wbc_flag_map lookup_dc_flags[] = {
65                 { WBC_LOOKUP_DC_FORCE_REDISCOVERY, DS_FORCE_REDISCOVERY },
66                 { WBC_LOOKUP_DC_DS_REQUIRED, DS_DIRECTORY_SERVICE_REQUIRED },
67                 { WBC_LOOKUP_DC_DS_PREFERRED, DS_DIRECTORY_SERVICE_PREFERRED},
68                 { WBC_LOOKUP_DC_GC_SERVER_REQUIRED, DS_GC_SERVER_REQUIRED },
69                 { WBC_LOOKUP_DC_PDC_REQUIRED,  DS_PDC_REQUIRED},
70                 { WBC_LOOKUP_DC_BACKGROUND_ONLY, DS_BACKGROUND_ONLY  },
71                 { WBC_LOOKUP_DC_IP_REQUIRED, DS_IP_REQUIRED },
72                 { WBC_LOOKUP_DC_KDC_REQUIRED, DS_KDC_REQUIRED },
73                 { WBC_LOOKUP_DC_TIMESERV_REQUIRED, DS_TIMESERV_REQUIRED },
74                 { WBC_LOOKUP_DC_WRITABLE_REQUIRED,  DS_WRITABLE_REQUIRED },
75                 { WBC_LOOKUP_DC_GOOD_TIMESERV_PREFERRED, DS_GOOD_TIMESERV_PREFERRED },
76                 { WBC_LOOKUP_DC_AVOID_SELF, DS_AVOID_SELF },
77                 { WBC_LOOKUP_DC_ONLY_LDAP_NEEDED, DS_ONLY_LDAP_NEEDED },
78                 { WBC_LOOKUP_DC_IS_FLAT_NAME, DS_IS_FLAT_NAME },
79                 { WBC_LOOKUP_DC_IS_DNS_NAME, DS_IS_DNS_NAME },
80                 { WBC_LOOKUP_DC_TRY_NEXTCLOSEST_SITE, DS_TRY_NEXTCLOSEST_SITE },
81                 { WBC_LOOKUP_DC_DS_6_REQUIRED, DS_DIRECTORY_SERVICE_6_REQUIRED },
82                 { WBC_LOOKUP_DC_RETURN_DNS_NAME, DS_RETURN_DNS_NAME },
83                 { WBC_LOOKUP_DC_RETURN_FLAT_NAME, DS_RETURN_FLAT_NAME }
84         };
85         uint32_t ds_flags = 0;
86         int i = 0 ;
87         int num_entries = sizeof(lookup_dc_flags) / sizeof(struct wbc_flag_map);
88
89         for (i=0; i<num_entries; i++) {
90                 if (wbc_flags & lookup_dc_flags[i].wbc_dc_flag)
91                         ds_flags |= lookup_dc_flags[i].ds_dc_flags;
92         }
93
94         return ds_flags;
95 }
96
97
98 static enum winbindd_result dual_dsgetdcname(struct winbindd_domain *domain,
99                                              struct winbindd_cli_state *state)
100 {
101         NTSTATUS result;
102         struct netr_DsRGetDCNameInfo *info = NULL;
103         const char *dc = NULL;
104         uint32_t ds_flags = 0;
105
106         state->request.domain_name
107                 [sizeof(state->request.domain_name)-1] = '\0';
108
109         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
110                   state->request.domain_name));
111
112         ds_flags = get_dsgetdc_flags(state->request.flags);
113
114         result = dsgetdcname(state->mem_ctx, winbind_messaging_context(),
115                              state->request.domain_name,
116                              NULL, NULL, ds_flags, &info);
117
118         if (!NT_STATUS_IS_OK(result)) {
119                 return WINBINDD_ERROR;
120         }
121
122         if (info->dc_address) {
123                 dc = strip_hostname(info->dc_address);
124         }
125
126         if ((!dc || !is_ipaddress_v4(dc)) && info->dc_unc) {
127                 dc = strip_hostname(info->dc_unc);
128         }
129
130         if (!dc || !*dc) {
131                 return WINBINDD_ERROR;
132         }
133
134         fstrcpy(state->response.data.dc_name, dc);
135
136         return WINBINDD_OK;
137 }
138
139 static const struct winbindd_child_dispatch_table locator_dispatch_table[] = {
140         {
141                 .name           = "DSGETDCNAME",
142                 .struct_cmd     = WINBINDD_DSGETDCNAME,
143                 .struct_fn      = dual_dsgetdcname,
144         },{
145                 .name           = NULL,
146         }
147 };