lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[samba.git] / source3 / winbindd / idmap_hash / idmap_hash.c
1 /*
2  *  idmap_hash.c
3  *
4  * Copyright (C) Gerald Carter  <jerry@samba.org>      2007 - 2008
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
21 #include "includes.h"
22 #include "winbindd/winbindd.h"
23 #include "idmap.h"
24 #include "idmap_hash.h"
25 #include "ads.h"
26 #include "nss_info.h"
27 #include "../libcli/security/dom_sid.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_IDMAP
31
32 struct sid_hash_table {
33         struct dom_sid *sid;
34 };
35
36 /*********************************************************************
37  Hash a domain SID (S-1-5-12-aaa-bbb-ccc) to a 12bit number
38  ********************************************************************/
39
40 static uint32_t hash_domain_sid(const struct dom_sid *sid)
41 {
42         uint32_t hash;
43
44         if (sid->num_auths != 4)
45                 return 0;
46
47         /* XOR the last three subauths */
48
49         hash = ((sid->sub_auths[1] ^ sid->sub_auths[2]) ^ sid->sub_auths[3]);
50
51         /* Take all 32-bits into account when generating the 12-bit
52            hash value */
53         hash = (((hash & 0xFFF00000) >> 20)
54                 + ((hash & 0x000FFF00) >> 8)
55                 + (hash & 0x000000FF)) & 0x0000FFF;
56
57         /* return a 12-bit hash value */
58
59         return hash;
60 }
61
62 /*********************************************************************
63  Hash a Relative ID to a 20 bit number
64  ********************************************************************/
65
66 static uint32_t hash_rid(uint32_t rid)
67 {
68         /* 20 bits for the rid which allows us to support
69            the first 100K users/groups in a domain */
70
71         return (rid & 0x0007FFFF);
72 }
73
74 /*********************************************************************
75  ********************************************************************/
76
77 static uint32_t combine_hashes(uint32_t h_domain,
78                                uint32_t h_rid)
79 {
80         uint32_t return_id = 0;
81
82         /* shift the hash_domain 19 bits to the left and OR with the
83            hash_rid */
84
85         return_id = ((h_domain<<19) | h_rid);
86
87         return return_id;
88 }
89
90 /*********************************************************************
91  ********************************************************************/
92
93 static void separate_hashes(uint32_t id,
94                             uint32_t *h_domain,
95                             uint32_t *h_rid)
96 {
97         *h_rid = id & 0x0007FFFF;
98         *h_domain = (id & 0x7FF80000) >> 19;
99
100         return;
101 }
102
103
104 /*********************************************************************
105  ********************************************************************/
106
107 static NTSTATUS idmap_hash_initialize(struct idmap_domain *dom)
108 {
109         struct sid_hash_table *hashed_domains;
110         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
111         struct winbindd_tdc_domain *dom_list = NULL;
112         size_t num_domains = 0;
113         int i;
114
115         DBG_ERR("The idmap_hash module is deprecated and should not be used. "
116                 "Please migrate to a different plugin. This module will be "
117                 "removed in a future version of Samba\n");
118
119         if (!strequal(dom->name, "*")) {
120                 DBG_ERR("Error: idmap_hash configured for domain '%s'. "
121                         "But the hash module can only be used for the default "
122                         "idmap configuration.\n", dom->name);
123                 return NT_STATUS_INVALID_PARAMETER;
124         }
125
126         /* If the domain SID hash table has been initialized, assume
127            that we completed this function previously */
128
129         if (dom->private_data != NULL) {
130                 nt_status = NT_STATUS_OK;
131                 goto done;
132         }
133
134         if (!wcache_tdc_fetch_list(&dom_list, &num_domains)) {
135                 nt_status = NT_STATUS_TRUSTED_DOMAIN_FAILURE;
136                 BAIL_ON_NTSTATUS_ERROR(nt_status);
137         }
138
139         /* Create the hash table of domain SIDs */
140
141         hashed_domains = talloc_zero_array(dom, struct sid_hash_table, 4096);
142         BAIL_ON_PTR_NT_ERROR(hashed_domains, nt_status);
143
144         /* create the hash table of domain SIDs */
145
146         for (i=0; i<num_domains; i++) {
147                 uint32_t hash;
148
149                 if (is_null_sid(&dom_list[i].sid))
150                         continue;
151
152                 /*
153                  * Check if the domain from the list is not already configured
154                  * to use another idmap backend. Not checking this makes the
155                  * idmap_hash module map IDs for *all* domains implicitly.  This
156                  * is quite dangerous in setups that use multiple idmap
157                  * configurations.
158                  */
159
160                 if (domain_has_idmap_config(dom_list[i].domain_name)) {
161                         continue;
162                 }
163
164                 if ((hash = hash_domain_sid(&dom_list[i].sid)) == 0)
165                         continue;
166
167                 DBG_INFO("Adding %s (%s) -> %d\n",
168                          dom_list[i].domain_name,
169                          sid_string_dbg(&dom_list[i].sid),
170                          hash);
171
172                 hashed_domains[hash].sid = talloc(hashed_domains, struct dom_sid);
173                 sid_copy(hashed_domains[hash].sid, &dom_list[i].sid);
174         }
175
176         dom->private_data = hashed_domains;
177
178 done:
179         return nt_status;
180 }
181
182 /*********************************************************************
183  ********************************************************************/
184
185 static NTSTATUS unixids_to_sids(struct idmap_domain *dom,
186                                 struct id_map **ids)
187 {
188         struct sid_hash_table *hashed_domains = talloc_get_type_abort(
189                 dom->private_data, struct sid_hash_table);
190         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
191         int i;
192
193         if (!ids) {
194                 nt_status = NT_STATUS_INVALID_PARAMETER;
195                 BAIL_ON_NTSTATUS_ERROR(nt_status);
196         }
197
198         /* initialize the status to avoid suprise */
199         for (i = 0; ids[i]; i++) {
200                 ids[i]->status = ID_UNKNOWN;
201         }
202
203         nt_status = idmap_hash_initialize(dom);
204         BAIL_ON_NTSTATUS_ERROR(nt_status);
205
206         for (i=0; ids[i]; i++) {
207                 uint32_t h_domain, h_rid;
208
209                 ids[i]->status = ID_UNMAPPED;
210
211                 separate_hashes(ids[i]->xid.id, &h_domain, &h_rid);
212
213                 /* Make sure the caller allocated memor for us */
214
215                 if (!ids[i]->sid) {
216                         nt_status = NT_STATUS_INVALID_PARAMETER;
217                         BAIL_ON_NTSTATUS_ERROR(nt_status);
218                 }
219
220                 /* If the domain hash doesn't find a SID in the table,
221                    skip it */
222
223                 if (!hashed_domains[h_domain].sid)
224                         continue;
225
226                 sid_compose(ids[i]->sid, hashed_domains[h_domain].sid, h_rid);
227                 ids[i]->status = ID_MAPPED;
228         }
229
230 done:
231         return nt_status;
232 }
233
234 /*********************************************************************
235  ********************************************************************/
236
237 static NTSTATUS sids_to_unixids(struct idmap_domain *dom,
238                                 struct id_map **ids)
239 {
240         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
241         int i;
242
243         if (!ids) {
244                 nt_status = NT_STATUS_INVALID_PARAMETER;
245                 BAIL_ON_NTSTATUS_ERROR(nt_status);
246         }
247
248         /* initialize the status to avoid suprise */
249         for (i = 0; ids[i]; i++) {
250                 ids[i]->status = ID_UNKNOWN;
251         }
252
253         nt_status = idmap_hash_initialize(dom);
254         BAIL_ON_NTSTATUS_ERROR(nt_status);
255
256         for (i=0; ids[i]; i++) {
257                 struct dom_sid sid;
258                 uint32_t rid;
259                 uint32_t h_domain, h_rid;
260
261                 ids[i]->status = ID_UNMAPPED;
262
263                 sid_copy(&sid, ids[i]->sid);
264                 sid_split_rid(&sid, &rid);
265
266                 h_domain = hash_domain_sid(&sid);
267                 h_rid = hash_rid(rid);
268
269                 /* Check that both hashes are non-zero*/
270
271                 if (h_domain && h_rid) {
272                         ids[i]->xid.id = combine_hashes(h_domain, h_rid);
273                         ids[i]->status = ID_MAPPED;
274                 }
275         }
276
277 done:
278         return nt_status;
279 }
280
281 /*********************************************************************
282  ********************************************************************/
283
284 static NTSTATUS nss_hash_init(struct nss_domain_entry *e )
285 {
286         return NT_STATUS_OK;
287 }
288
289 /**********************************************************************
290  *********************************************************************/
291
292 static NTSTATUS nss_hash_map_to_alias(TALLOC_CTX *mem_ctx,
293                                         struct nss_domain_entry *e,
294                                         const char *name,
295                                         char **alias)
296 {
297         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
298         const char *value;
299
300         value = talloc_asprintf(mem_ctx, "%s\\%s", e->domain, name);
301         BAIL_ON_PTR_NT_ERROR(value, nt_status);
302
303         nt_status = mapfile_lookup_key(mem_ctx, value, alias);
304         BAIL_ON_NTSTATUS_ERROR(nt_status);
305
306 done:
307         return nt_status;
308 }
309
310 /**********************************************************************
311  *********************************************************************/
312
313 static NTSTATUS nss_hash_map_from_alias(TALLOC_CTX *mem_ctx,
314                                           struct nss_domain_entry *e,
315                                           const char *alias,
316                                           char **name)
317 {
318         return mapfile_lookup_value(mem_ctx, alias, name);
319 }
320
321 /**********************************************************************
322  *********************************************************************/
323
324 static NTSTATUS nss_hash_close(void)
325 {
326         return NT_STATUS_OK;
327 }
328
329 /*********************************************************************
330  Dispatch Tables for IDMap and NssInfo Methods
331 ********************************************************************/
332
333 static struct idmap_methods hash_idmap_methods = {
334         .init            = idmap_hash_initialize,
335         .unixids_to_sids = unixids_to_sids,
336         .sids_to_unixids = sids_to_unixids,
337 };
338
339 static struct nss_info_methods hash_nss_methods = {
340         .init           = nss_hash_init,
341         .map_to_alias   = nss_hash_map_to_alias,
342         .map_from_alias = nss_hash_map_from_alias,
343         .close_fn       = nss_hash_close
344 };
345
346 /**********************************************************************
347  Register with the idmap and idmap_nss subsystems. We have to protect
348  against the idmap and nss_info interfaces being in a half-registered
349  state.
350  **********************************************************************/
351
352 static_decl_idmap;
353 NTSTATUS idmap_hash_init(TALLOC_CTX *ctx)
354 {
355         static NTSTATUS idmap_status = NT_STATUS_UNSUCCESSFUL;
356         static NTSTATUS nss_status = NT_STATUS_UNSUCCESSFUL;
357
358         if ( !NT_STATUS_IS_OK(idmap_status) ) {
359                 idmap_status =  smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
360                                                    "hash", &hash_idmap_methods);
361
362                 if ( !NT_STATUS_IS_OK(idmap_status) ) {
363                         DEBUG(0,("Failed to register hash idmap plugin.\n"));
364                         return idmap_status;
365                 }
366         }
367
368         if ( !NT_STATUS_IS_OK(nss_status) ) {
369                 nss_status = smb_register_idmap_nss(SMB_NSS_INFO_INTERFACE_VERSION,
370                                                     "hash", &hash_nss_methods);
371                 if ( !NT_STATUS_IS_OK(nss_status) ) {
372                         DEBUG(0,("Failed to register hash idmap nss plugin.\n"));
373                         return nss_status;
374                 }
375         }
376
377         return NT_STATUS_OK;
378 }