autorid: Add allocation from above in alloc range for well known sids
[obnox/samba/samba-obnox.git] / source3 / winbindd / idmap_autorid.c
1 /*
2  *  idmap_autorid: static map between Active Directory/NT RIDs
3  *  and RFC 2307 accounts
4  *
5  *  based on the idmap_rid module, but this module defines the ranges
6  *  for the domains by automatically allocating a range for each domain
7  *
8  *  Copyright (C) Christian Ambach, 2010-2012
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 /*
26  * This module allocates ranges for domains to be used in a
27  * algorithmic mode like idmap_rid. Multiple ranges are supported
28  * for a single domain: If a rid exceeds the range size, a matching
29  * range is allocated to hold the rid's id.
30  *
31  * Here are the formulas applied:
32  *
33  *
34  * For a sid of the form domain_sid-rid, we have
35  *
36  *   rid = reduced_rid + domain_range_index * range_size
37  *
38  * with
39  *   reduced_rid := rid % range_size
40  *   domain_range_index := rid / range_size
41  *
42  * And reduced_rid fits into a range.
43  *
44  * In the database, we associate a range_number to
45  * the pair domain_sid,domain_range_index.
46  *
47  * Now the unix id for the given sid calculates as:
48  *
49  *   id = reduced_rid + range_low_id
50  *
51  * with
52  *
53  *   range_low_id = low_id + range_number * range_size
54  *
55  *
56  * The inverse calculation goes like this:
57  *
58  * Given a unix id, let
59  *
60  *   normalized_id := id - low_id
61  *   reduced_rid := normalized_id % range_size
62  *   range_number = normalized_id / range_size
63  *
64  * Then we have
65  *
66  *   id = reduced_rid + low_id + range_number * range_size
67  *
68  * From the database, get the domain_sid,domain_range_index pair
69  * belonging to the range_number (if there is already one).
70  *
71  * Then the rid for the unix id calculates as:
72  *
73  *   rid = reduced_rid + domain_range_index * range_size
74  */
75
76 #include "idmap_autorid_tdb.h"
77 #include "winbindd.h"
78 #include "idmap.h"
79 #include "idmap_rw.h"
80 #include "../libcli/security/dom_sid.h"
81
82 #undef DBGC_CLASS
83 #define DBGC_CLASS DBGC_IDMAP
84
85 #define IDMAP_AUTORID_ALLOC_RESERVED 500
86
87 /* handle to the tdb storing domain <-> range assignments */
88 static struct db_context *autorid_db;
89
90 static bool ignore_builtin = false;
91
92 static NTSTATUS idmap_autorid_get_alloc_range(struct idmap_domain *dom,
93                                         struct autorid_range_config *range)
94 {
95         NTSTATUS status;
96
97         ZERO_STRUCT(*range);
98
99         fstrcpy(range->domsid, ALLOC_RANGE);
100
101         status = idmap_autorid_get_domainrange(autorid_db,
102                                                range,
103                                                dom->read_only);
104
105         return status;
106 }
107
108 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
109                                           struct unixid *xid) {
110
111         NTSTATUS ret;
112         struct autorid_range_config range;
113
114         if (dom->read_only) {
115                 DEBUG(3, ("Backend is read-only, refusing "
116                           "new allocation request\n"));
117                 return NT_STATUS_UNSUCCESSFUL;
118         }
119
120         /* fetch the range for the allocation pool */
121
122         ret = idmap_autorid_get_alloc_range(dom, &range);
123         if (!NT_STATUS_IS_OK(ret)) {
124                 DEBUG(3, ("Could not determine range for allocation pool, "
125                           "check previous messages for reason\n"));
126                 return ret;
127         }
128
129         ret = idmap_tdb_common_get_new_id(dom, xid);
130
131         if (!NT_STATUS_IS_OK(ret)) {
132                 DEBUG(1, ("Fatal error while allocating new ID!\n"));
133                 return ret;
134         }
135
136         xid->id = xid->id + range.low_id;
137
138         DEBUG(10, ("Returned new %s %d from allocation range\n",
139                    (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
140
141         return ret;
142 }
143
144 /*
145  * map a SID to xid using the idmap_tdb like pool
146  */
147 static NTSTATUS idmap_autorid_id_to_sid_alloc(struct idmap_domain *dom,
148                                               struct id_map *map)
149 {
150         NTSTATUS ret;
151
152         /* look out for the mapping */
153         ret = idmap_tdb_common_unixid_to_sid(dom, map);
154
155         if (NT_STATUS_IS_OK(ret)) {
156                 map->status = ID_MAPPED;
157                 return ret;
158         }
159
160         map->status = ID_UNKNOWN;
161
162         DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
163
164         return ret;
165 }
166
167 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
168                                         struct idmap_domain *dom,
169                                         struct id_map *map)
170 {
171         uint32_t range_number;
172         uint32_t domain_range_index = 0;
173         uint32_t normalized_id;
174         uint32_t reduced_rid;
175         uint32_t rid;
176         TDB_DATA data = tdb_null;
177         char *keystr;
178         struct dom_sid domsid;
179         NTSTATUS status;
180         bool ok;
181         const char *q = NULL;
182
183         /* can this be one of our ids? */
184         if (map->xid.id < cfg->minvalue) {
185                 DEBUG(10, ("id %d is lower than minimum value, "
186                            "ignoring mapping request\n", map->xid.id));
187                 map->status = ID_UNKNOWN;
188                 return NT_STATUS_OK;
189         }
190
191         if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
192                 DEBUG(10, ("id %d is outside of maximum id value, "
193                            "ignoring mapping request\n", map->xid.id));
194                 map->status = ID_UNKNOWN;
195                 return NT_STATUS_OK;
196         }
197
198         /* determine the range of this uid */
199
200         normalized_id = map->xid.id - cfg->minvalue;
201         range_number = normalized_id / cfg->rangesize;
202
203         keystr = talloc_asprintf(talloc_tos(), "%u", range_number);
204         if (!keystr) {
205                 return NT_STATUS_NO_MEMORY;
206         }
207
208         status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
209         TALLOC_FREE(keystr);
210
211         if (!NT_STATUS_IS_OK(status)) {
212                 DEBUG(4, ("id %d belongs to range %d which does not have "
213                           "domain mapping, ignoring mapping request\n",
214                           map->xid.id, range_number));
215                 TALLOC_FREE(data.dptr);
216                 map->status = ID_UNKNOWN;
217                 return NT_STATUS_OK;
218         }
219
220         if (strncmp((const char *)data.dptr,
221                     ALLOC_RANGE,
222                     strlen(ALLOC_RANGE)) == 0) {
223                 /*
224                  * this is from the alloc range, check if there is a mapping
225                  */
226                 DEBUG(5, ("id %d belongs to allocation range, "
227                           "checking for mapping\n",
228                           map->xid.id));
229                 TALLOC_FREE(data.dptr);
230                 return idmap_autorid_id_to_sid_alloc(dom, map);
231         }
232
233         ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
234         TALLOC_FREE(data.dptr);
235         if (!ok) {
236                 map->status = ID_UNKNOWN;
237                 return NT_STATUS_OK;
238         }
239         if ((q != NULL) && (*q != '\0'))
240                 if (sscanf(q+1, "%"SCNu32, &domain_range_index) != 1) {
241                         DEBUG(10, ("Domain range index not found, "
242                                    "ignoring mapping request\n"));
243                         map->status = ID_UNKNOWN;
244                         return NT_STATUS_OK;
245                 }
246
247         reduced_rid = normalized_id % cfg->rangesize;
248         rid = reduced_rid + domain_range_index * cfg->rangesize;
249
250         sid_compose(map->sid, &domsid, rid);
251
252         /* We **really** should have some way of validating
253            the SID exists and is the correct type here.  But
254            that is a deficiency in the idmap_rid design. */
255
256         map->status = ID_MAPPED;
257         map->xid.type = ID_TYPE_BOTH;
258
259         return NT_STATUS_OK;
260 }
261
262 /**********************************
263  Single sid to id lookup function.
264 **********************************/
265
266 static NTSTATUS idmap_autorid_sid_to_id_rid(
267                                         struct autorid_global_config *global,
268                                         struct autorid_range_config *range,
269                                         struct id_map *map)
270 {
271         uint32_t rid;
272         uint32_t reduced_rid;
273
274         sid_peek_rid(map->sid, &rid);
275
276         reduced_rid = rid % global->rangesize;
277
278         map->xid.id = reduced_rid + range->low_id;
279         map->xid.type = ID_TYPE_BOTH;
280         map->status = ID_MAPPED;
281
282         return NT_STATUS_OK;
283 }
284
285 /**********************************
286  lookup a set of unix ids.
287 **********************************/
288
289 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
290                                               struct id_map **ids)
291 {
292         struct idmap_tdb_common_context *commoncfg;
293         struct autorid_global_config *globalcfg;
294         NTSTATUS ret;
295         int i;
296         int num_tomap = 0;
297         int num_mapped = 0;
298
299         /* initialize the status to avoid surprise */
300         for (i = 0; ids[i]; i++) {
301                 ids[i]->status = ID_UNKNOWN;
302                 num_tomap++;
303         }
304
305         commoncfg =
306             talloc_get_type_abort(dom->private_data,
307                                   struct idmap_tdb_common_context);
308
309         globalcfg = talloc_get_type(commoncfg->private_data,
310                                     struct autorid_global_config);
311
312         for (i = 0; ids[i]; i++) {
313
314                 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
315
316                 if ((!NT_STATUS_IS_OK(ret)) &&
317                     (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
318                         /* some fatal error occurred, log it */
319                         DEBUG(3, ("Unexpected error resolving an ID "
320                                   " (%d)\n", ids[i]->xid.id));
321                         goto failure;
322                 }
323
324                 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
325                         num_mapped++;
326                 }
327
328         }
329
330         if (num_tomap == num_mapped) {
331                 return NT_STATUS_OK;
332         } else if (num_mapped == 0) {
333                 return NT_STATUS_NONE_MAPPED;
334         }
335
336         return STATUS_SOME_UNMAPPED;
337
338
339       failure:
340         return ret;
341 }
342
343 static bool idmap_autorid_sid_is_special(struct dom_sid *sid)
344 {
345         bool match;
346
347         match = sid_check_is_in_wellknown_domain(sid);
348         if (match) {
349                 return true;
350         }
351
352         return false;
353 }
354
355 static NTSTATUS idmap_autorid_sid_to_id_special(struct idmap_domain *dom,
356                                                 struct id_map *map)
357 {
358         struct idmap_tdb_common_context *common =
359                 talloc_get_type_abort(dom->private_data,
360                                       struct idmap_tdb_common_context);
361         uint32_t count;
362         struct autorid_range_config range;
363         NTSTATUS status;
364         uint32_t free_id;
365
366         status = idmap_autorid_get_alloc_range(dom, &range);
367         if (!NT_STATUS_IS_OK(status)) {
368                 return status;
369         }
370
371         /* Take the next free ID, counting from the top */
372         free_id = 0;
373         for (count = 0; count < IDMAP_AUTORID_ALLOC_RESERVED; count++) {
374                 struct id_map test_map;
375                 struct dom_sid sid;
376
377                 test_map.sid = &sid;
378                 test_map.xid.type = map->xid.type;
379                 test_map.xid.id = range.high_id - count;
380                 test_map.status = ID_UNKNOWN;
381
382                 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
383                 if (NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, status)) {
384                         free_id = test_map.xid.id;
385                         break;
386                 }
387
388                 if (!NT_STATUS_IS_OK(status)) {
389                         /* error - get out */
390                         return status;
391                 }
392
393                 /* mapping exists - try next ID */
394         }
395
396         if (free_id == 0) {
397                 return NT_STATUS_NONE_MAPPED;
398         }
399
400         map->status = ID_MAPPED;
401         map->xid.id = free_id;
402
403         status = common->rw_ops->set_mapping(dom, map);
404         if (!NT_STATUS_IS_OK(status)) {
405                 DEBUG(2, ("Error storing new mapping: %s\n",
406                           nt_errstr(status)));
407                 return status;
408         }
409
410         return NT_STATUS_OK;
411 }
412
413 struct idmap_autorid_sid_to_id_alloc_ctx {
414         struct idmap_domain *dom;
415         struct id_map *map;
416 };
417
418 static NTSTATUS idmap_autorid_sid_to_id_alloc_action(
419                                 struct db_context *db,
420                                 void *private_data)
421 {
422         struct idmap_autorid_sid_to_id_alloc_ctx *ctx;
423
424         ctx = (struct idmap_autorid_sid_to_id_alloc_ctx *)private_data;
425
426         if (idmap_autorid_sid_is_special(ctx->map->sid)) {
427                 NTSTATUS ret;
428
429                 ret = idmap_autorid_sid_to_id_special(ctx->dom, ctx->map);
430                 if (NT_STATUS_IS_OK(ret)) {
431                         return NT_STATUS_OK;
432                 }
433                 if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, ret)) {
434                         return ret;
435                 }
436
437                 DEBUG(10, ("Sepecial sid %s not mapped. falling back to "
438                            "regular allocation\n",
439                            sid_string_dbg(ctx->map->sid)));
440         }
441
442         return idmap_tdb_common_new_mapping(ctx->dom, ctx->map);
443 }
444
445 /*
446  * map a SID to xid using the idmap_tdb like pool
447  */
448 static NTSTATUS idmap_autorid_sid_to_id_alloc(
449                                         struct idmap_tdb_common_context *ctx,
450                                         struct idmap_domain *dom,
451                                         struct id_map *map)
452 {
453         NTSTATUS ret;
454         struct idmap_autorid_sid_to_id_alloc_ctx alloc_ctx;
455
456         map->status = ID_UNKNOWN;
457
458         /* see if we already have a mapping */
459         ret = idmap_tdb_common_sid_to_unixid(dom, map);
460
461         if (NT_STATUS_IS_OK(ret)) {
462                 map->status = ID_MAPPED;
463                 return ret;
464         }
465
466         /* bad things happened */
467         if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
468                 DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
469                           sid_string_dbg(map->sid), nt_errstr(ret)));
470                 return ret;
471         }
472
473         if (dom->read_only) {
474                 DEBUG(3, ("Not allocating new mapping for %s, because backend "
475                           "is read-only\n", sid_string_dbg(map->sid)));
476                 map->status = ID_UNMAPPED;
477                 return NT_STATUS_NONE_MAPPED;
478         }
479
480         DEBUG(10, ("Creating new mapping in pool for %s\n",
481                    sid_string_dbg(map->sid)));
482
483         alloc_ctx.dom = dom;
484         alloc_ctx.map = map;
485
486         ret = dbwrap_trans_do(ctx->db, idmap_autorid_sid_to_id_alloc_action,
487                               &alloc_ctx);
488         if (!NT_STATUS_IS_OK(ret)) {
489                 DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
490                           nt_errstr(ret)));
491                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
492         }
493
494         map->status = ID_MAPPED;
495         return NT_STATUS_OK;
496 }
497
498 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid *sid)
499 {
500         bool match;
501
502         match = sid_check_is_wellknown_domain(sid, NULL);
503         if (match) {
504                 return true;
505         }
506
507         return false;
508 }
509
510 static NTSTATUS idmap_autorid_sid_to_id(struct idmap_tdb_common_context *common,
511                                         struct idmap_domain *dom,
512                                         struct id_map *map)
513 {
514         struct autorid_global_config *global =
515                 talloc_get_type_abort(common->private_data,
516                                       struct autorid_global_config);
517         struct winbindd_tdc_domain *domain;
518         struct autorid_range_config range;
519         uint32_t rid;
520         struct dom_sid domainsid;
521         NTSTATUS ret;
522
523         ZERO_STRUCT(range);
524         map->status = ID_UNKNOWN;
525
526         DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map->sid)));
527
528         sid_copy(&domainsid, map->sid);
529         if (!sid_split_rid(&domainsid, &rid)) {
530                 DEBUG(4, ("Could not determine domain SID from %s, "
531                           "ignoring mapping request\n",
532                           sid_string_dbg(map->sid)));
533                 map->status = ID_UNMAPPED;
534                 return NT_STATUS_NONE_MAPPED;
535         }
536
537         if (idmap_autorid_domsid_is_for_alloc(&domainsid)) {
538                 DEBUG(10, ("SID %s is for ALLOC range.\n",
539                            sid_string_dbg(map->sid)));
540
541                 return idmap_autorid_sid_to_id_alloc(common, dom, map);
542         }
543
544         if (dom_sid_equal(&domainsid, &global_sid_Builtin) && ignore_builtin) {
545                 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
546                 map->status = ID_UNMAPPED;
547                 return NT_STATUS_NONE_MAPPED;
548         }
549
550         /*
551          * Check if the domain is around
552          */
553         domain = wcache_tdc_fetch_domainbysid(talloc_tos(),
554                                               &domainsid);
555         if (domain == NULL) {
556                 DEBUG(10, ("Ignoring unknown domain sid %s\n",
557                            sid_string_dbg(&domainsid)));
558                 map->status = ID_UNMAPPED;
559                 return NT_STATUS_NONE_MAPPED;
560         }
561         TALLOC_FREE(domain);
562
563         sid_to_fstring(range.domsid, &domainsid);
564
565         range.domain_range_index = rid / (global->rangesize);
566
567         ret = idmap_autorid_get_domainrange(autorid_db, &range, dom->read_only);
568         if (NT_STATUS_EQUAL(ret, NT_STATUS_NOT_FOUND) && dom->read_only) {
569                 DEBUG(10, ("read-only is enabled, did not allocate "
570                            "new range for domain %s\n",
571                            sid_string_dbg(&domainsid)));
572                 map->status = ID_UNMAPPED;
573                 return NT_STATUS_NONE_MAPPED;
574         }
575         if (!NT_STATUS_IS_OK(ret)) {
576                 DEBUG(3, ("Could not determine range for domain, "
577                           "check previous messages for reason\n"));
578                 return ret;
579         }
580
581         return idmap_autorid_sid_to_id_rid(global, &range, map);
582 }
583
584 /**********************************
585  lookup a set of sids.
586 **********************************/
587
588 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
589                                               struct id_map **ids)
590 {
591         struct idmap_tdb_common_context *commoncfg;
592         NTSTATUS ret;
593         int i;
594         int num_tomap = 0;
595         int num_mapped = 0;
596
597         /* initialize the status to avoid surprise */
598         for (i = 0; ids[i]; i++) {
599                 ids[i]->status = ID_UNKNOWN;
600                 num_tomap++;
601         }
602
603         commoncfg =
604             talloc_get_type_abort(dom->private_data,
605                                   struct idmap_tdb_common_context);
606
607         for (i = 0; ids[i]; i++) {
608                 ret = idmap_autorid_sid_to_id(commoncfg, dom, ids[i]);
609                 if ((!NT_STATUS_IS_OK(ret)) &&
610                     (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
611                         /* some fatal error occurred, log it */
612                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
613                                   sid_string_dbg(ids[i]->sid)));
614                         return ret;
615                 }
616
617                 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
618                         num_mapped++;
619                 }
620         }
621
622         if (num_tomap == num_mapped) {
623                 return NT_STATUS_OK;
624         } else if (num_mapped == 0) {
625                 return NT_STATUS_NONE_MAPPED;
626         }
627
628         return STATUS_SOME_UNMAPPED;
629 }
630
631 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
632 {
633         const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
634                 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
635                 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
636                 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
637                 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
638                 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
639         };
640
641         struct id_map **maps;
642         int i, num;
643         NTSTATUS status;
644
645         if (dom->read_only) {
646                 return NT_STATUS_OK;
647         }
648
649         num = ARRAY_SIZE(groups);
650
651         maps = talloc_array(talloc_tos(), struct id_map*, num+1);
652         if (!maps) {
653                 return NT_STATUS_NO_MEMORY;
654         }
655
656         for (i = 0; i < num; i++) {
657                 maps[i] = talloc(maps, struct id_map);
658                 if (maps[i] == NULL) {
659                         talloc_free(maps);
660                         return NT_STATUS_NO_MEMORY;
661                 }
662                 maps[i]->xid.type = ID_TYPE_GID;
663                 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
664         }
665
666         maps[num] = NULL;
667
668         status = idmap_autorid_sids_to_unixids(dom, maps);
669
670         DEBUG(10,("Preallocation run finished with status %s\n",
671                   nt_errstr(status)));
672
673         talloc_free(maps);
674
675         return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
676 }
677
678 static NTSTATUS idmap_autorid_initialize_action(struct db_context *db,
679                                                 void *private_data)
680 {
681         struct idmap_domain *dom;
682         struct idmap_tdb_common_context *common;
683         struct autorid_global_config *config;
684         NTSTATUS status;
685
686         dom = (struct idmap_domain *)private_data;
687         common = (struct idmap_tdb_common_context *)dom->private_data;
688         config = (struct autorid_global_config *)common->private_data;
689
690         status = idmap_autorid_init_hwms(db);
691         if (!NT_STATUS_IS_OK(status)) {
692                 return status;
693         }
694
695         status = idmap_autorid_saveconfig(db, config);
696         if (!NT_STATUS_IS_OK(status)) {
697                 DEBUG(1, ("Failed to store configuration data!\n"));
698                 return status;
699         }
700
701         status = idmap_autorid_preallocate_wellknown(dom);
702         if (!NT_STATUS_IS_OK(status)) {
703                 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
704                           nt_errstr(status)));
705                 return status;
706         }
707
708         return NT_STATUS_OK;
709 }
710
711 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
712 {
713         struct idmap_tdb_common_context *commonconfig;
714         struct autorid_global_config *config;
715         NTSTATUS status;
716
717         if (!strequal(dom->name, "*")) {
718                 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
719                           "for domain '%s'. But autorid can only be used for "
720                           "the default idmap configuration.\n", dom->name));
721                 return NT_STATUS_INVALID_PARAMETER;
722         }
723
724         commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
725         if (!commonconfig) {
726                 DEBUG(0, ("Out of memory!\n"));
727                 return NT_STATUS_NO_MEMORY;
728         }
729         dom->private_data = commonconfig;
730
731         commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
732         if (commonconfig->rw_ops == NULL) {
733                 DEBUG(0, ("Out of memory!\n"));
734                 return NT_STATUS_NO_MEMORY;
735         }
736
737         config = talloc_zero(commonconfig, struct autorid_global_config);
738         if (!config) {
739                 DEBUG(0, ("Out of memory!\n"));
740                 return NT_STATUS_NO_MEMORY;
741         }
742         commonconfig->private_data = config;
743
744         config->minvalue = dom->low_id;
745         config->rangesize = lp_parm_int(-1, "idmap config *",
746                                         "rangesize", 100000);
747
748         config->maxranges = (dom->high_id - dom->low_id + 1) /
749             config->rangesize;
750
751         if (config->maxranges == 0) {
752                 DEBUG(1, ("Allowed uid range is smaller than rangesize. "
753                           "Increase uid range or decrease rangesize.\n"));
754                 status = NT_STATUS_INVALID_PARAMETER;
755                 goto error;
756         }
757
758         /* check if the high-low limit is a multiple of the rangesize */
759         if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
760                 DEBUG(5, ("High uid-low uid difference of %d "
761                           "is not a multiple of the rangesize %d, "
762                           "limiting ranges to lower boundary number of %d\n",
763                           (dom->high_id - dom->low_id + 1), config->rangesize,
764                           config->maxranges));
765         }
766
767         DEBUG(5, ("%d domain ranges with a size of %d are available\n",
768                   config->maxranges, config->rangesize));
769
770         ignore_builtin = lp_parm_bool(-1, "idmap config *",
771                                       "ignore builtin", false);
772
773         /* fill the TDB common configuration */
774
775         commonconfig->max_id = config->rangesize - 1
776                              - IDMAP_AUTORID_ALLOC_RESERVED;
777         commonconfig->hwmkey_uid = ALLOC_HWM_UID;
778         commonconfig->hwmkey_gid = ALLOC_HWM_GID;
779         commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
780         commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
781
782         status = idmap_autorid_db_open(state_path("autorid.tdb"),
783                                        NULL, /* TALLOC_CTX */
784                                        &autorid_db);
785         if (!NT_STATUS_IS_OK(status)) {
786                 goto error;
787         }
788
789         commonconfig->db = autorid_db;
790
791         status = dbwrap_trans_do(autorid_db,
792                                  idmap_autorid_initialize_action,
793                                  dom);
794         if (!NT_STATUS_IS_OK(status)) {
795                 DEBUG(1, ("Failed to init the idmap database: %s\n",
796                           nt_errstr(status)));
797                 goto error;
798         }
799
800         goto done;
801
802 error:
803         talloc_free(config);
804
805 done:
806         return status;
807 }
808
809 /*
810   Close the idmap tdb instance
811 */
812 static struct idmap_methods autorid_methods = {
813         .init = idmap_autorid_initialize,
814         .unixids_to_sids = idmap_autorid_unixids_to_sids,
815         .sids_to_unixids = idmap_autorid_sids_to_unixids,
816         .allocate_id     = idmap_autorid_allocate_id
817 };
818
819 NTSTATUS idmap_autorid_init(void)
820 {
821         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
822                                   "autorid", &autorid_methods);
823 }