lib: Pass mem_ctx to state_path()
[samba.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 #include "libsmb/samlogon_cache.h"
82 #include "passdb/machine_sid.h"
83
84 #undef DBGC_CLASS
85 #define DBGC_CLASS DBGC_IDMAP
86
87 #define IDMAP_AUTORID_ALLOC_RESERVED 500
88
89 /* handle to the tdb storing domain <-> range assignments */
90 static struct db_context *autorid_db;
91
92 static bool ignore_builtin = false;
93
94 static NTSTATUS idmap_autorid_get_alloc_range(struct idmap_domain *dom,
95                                         struct autorid_range_config *range)
96 {
97         NTSTATUS status;
98
99         ZERO_STRUCT(*range);
100
101         fstrcpy(range->domsid, ALLOC_RANGE);
102
103         status = idmap_autorid_get_domainrange(autorid_db,
104                                                range,
105                                                dom->read_only);
106
107         return status;
108 }
109
110 static NTSTATUS idmap_autorid_allocate_id(struct idmap_domain *dom,
111                                           struct unixid *xid) {
112
113         NTSTATUS ret;
114         struct autorid_range_config range;
115
116         if (dom->read_only) {
117                 DEBUG(3, ("Backend is read-only, refusing "
118                           "new allocation request\n"));
119                 return NT_STATUS_UNSUCCESSFUL;
120         }
121
122         /* fetch the range for the allocation pool */
123
124         ret = idmap_autorid_get_alloc_range(dom, &range);
125         if (!NT_STATUS_IS_OK(ret)) {
126                 DEBUG(3, ("Could not determine range for allocation pool, "
127                           "check previous messages for reason\n"));
128                 return ret;
129         }
130
131         ret = idmap_tdb_common_get_new_id(dom, xid);
132
133         if (!NT_STATUS_IS_OK(ret)) {
134                 DEBUG(1, ("Fatal error while allocating new ID!\n"));
135                 return ret;
136         }
137
138         xid->id = xid->id + range.low_id;
139
140         DEBUG(10, ("Returned new %s %d from allocation range\n",
141                    (xid->type==ID_TYPE_UID)?"uid":"gid", xid->id));
142
143         return ret;
144 }
145
146 /*
147  * map a xid to SID using the idmap_tdb like pool
148  */
149 static NTSTATUS idmap_autorid_id_to_sid_alloc(struct idmap_domain *dom,
150                                               struct id_map *map)
151 {
152         NTSTATUS ret;
153
154         /* look out for the mapping */
155         ret = idmap_tdb_common_unixid_to_sid(dom, map);
156
157         if (NT_STATUS_IS_OK(ret)) {
158                 map->status = ID_MAPPED;
159                 return ret;
160         }
161
162         map->status = ID_UNKNOWN;
163
164         DEBUG(10, ("no ID->SID mapping for %d could be found\n", map->xid.id));
165
166         return ret;
167 }
168
169 static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
170                                         struct idmap_domain *dom,
171                                         struct id_map *map)
172 {
173         uint32_t range_number;
174         uint32_t domain_range_index;
175         uint32_t normalized_id;
176         uint32_t reduced_rid;
177         uint32_t rid;
178         TDB_DATA data = tdb_null;
179         char *keystr;
180         struct dom_sid domsid;
181         NTSTATUS status;
182         bool ok;
183         const char *q = NULL;
184
185         /* can this be one of our ids? */
186         if (map->xid.id < cfg->minvalue) {
187                 DEBUG(10, ("id %d is lower than minimum value, "
188                            "ignoring mapping request\n", map->xid.id));
189                 map->status = ID_UNKNOWN;
190                 return NT_STATUS_OK;
191         }
192
193         if (map->xid.id > (cfg->minvalue + cfg->rangesize * cfg->maxranges)) {
194                 DEBUG(10, ("id %d is outside of maximum id value, "
195                            "ignoring mapping request\n", map->xid.id));
196                 map->status = ID_UNKNOWN;
197                 return NT_STATUS_OK;
198         }
199
200         /* determine the range of this uid */
201
202         normalized_id = map->xid.id - cfg->minvalue;
203         range_number = normalized_id / cfg->rangesize;
204
205         keystr = talloc_asprintf(talloc_tos(), "%u", range_number);
206         if (!keystr) {
207                 return NT_STATUS_NO_MEMORY;
208         }
209
210         status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
211         TALLOC_FREE(keystr);
212
213         if (!NT_STATUS_IS_OK(status)) {
214                 DEBUG(4, ("id %d belongs to range %d which does not have "
215                           "domain mapping, ignoring mapping request\n",
216                           map->xid.id, range_number));
217                 TALLOC_FREE(data.dptr);
218                 map->status = ID_UNKNOWN;
219                 return NT_STATUS_OK;
220         }
221
222         if ((data.dsize == 0) || (data.dptr[data.dsize-1] != '\0')) {
223                 DBG_WARNING("Invalid range %"PRIu32"\n", range_number);
224                 TALLOC_FREE(data.dptr);
225                 map->status = ID_UNKNOWN;
226                 return NT_STATUS_OK;
227         }
228
229         if (strncmp((const char *)data.dptr,
230                     ALLOC_RANGE,
231                     strlen(ALLOC_RANGE)) == 0) {
232                 /*
233                  * this is from the alloc range, check if there is a mapping
234                  */
235                 DEBUG(5, ("id %d belongs to allocation range, "
236                           "checking for mapping\n",
237                           map->xid.id));
238                 TALLOC_FREE(data.dptr);
239                 return idmap_autorid_id_to_sid_alloc(dom, map);
240         }
241
242         ok = dom_sid_parse_endp((const char *)data.dptr, &domsid, &q);
243         if (!ok) {
244                 TALLOC_FREE(data.dptr);
245                 map->status = ID_UNKNOWN;
246                 return NT_STATUS_OK;
247         }
248
249         /*
250          * Allow for sid#range_index, just sid is range index 0
251          */
252
253         switch (*q) {
254             case '\0':
255                     domain_range_index = 0;
256                     break;
257             case '#':
258                     if (sscanf(q+1, "%"SCNu32, &domain_range_index) == 1) {
259                             break;
260                     }
261                     /* If we end up here, something weird is in the record. */
262
263                     FALL_THROUGH;
264             default:
265                     DBG_DEBUG("SID/domain range: %s\n",
266                               (const char *)data.dptr);
267                     TALLOC_FREE(data.dptr);
268                     map->status = ID_UNKNOWN;
269                     return NT_STATUS_OK;
270         }
271
272         TALLOC_FREE(data.dptr);
273
274         reduced_rid = normalized_id % cfg->rangesize;
275         rid = reduced_rid + domain_range_index * cfg->rangesize;
276
277         sid_compose(map->sid, &domsid, rid);
278
279         /* We **really** should have some way of validating
280            the SID exists and is the correct type here.  But
281            that is a deficiency in the idmap_rid design. */
282
283         map->status = ID_MAPPED;
284         map->xid.type = ID_TYPE_BOTH;
285
286         return NT_STATUS_OK;
287 }
288
289 /**********************************
290  Single sid to id lookup function.
291 **********************************/
292
293 static NTSTATUS idmap_autorid_sid_to_id_rid(
294                                         uint32_t rangesize,
295                                         uint32_t low_id,
296                                         struct id_map *map)
297 {
298         uint32_t rid;
299         uint32_t reduced_rid;
300
301         sid_peek_rid(map->sid, &rid);
302
303         reduced_rid = rid % rangesize;
304
305         map->xid.id = reduced_rid + low_id;
306         map->xid.type = ID_TYPE_BOTH;
307         map->status = ID_MAPPED;
308
309         return NT_STATUS_OK;
310 }
311
312 /**********************************
313  lookup a set of unix ids.
314 **********************************/
315
316 static NTSTATUS idmap_autorid_unixids_to_sids(struct idmap_domain *dom,
317                                               struct id_map **ids)
318 {
319         struct idmap_tdb_common_context *commoncfg;
320         struct autorid_global_config *globalcfg;
321         NTSTATUS ret;
322         int i;
323         int num_tomap = 0;
324         int num_mapped = 0;
325
326         /* initialize the status to avoid surprise */
327         for (i = 0; ids[i]; i++) {
328                 ids[i]->status = ID_UNKNOWN;
329                 num_tomap++;
330         }
331
332         commoncfg =
333             talloc_get_type_abort(dom->private_data,
334                                   struct idmap_tdb_common_context);
335
336         globalcfg = talloc_get_type(commoncfg->private_data,
337                                     struct autorid_global_config);
338
339         for (i = 0; ids[i]; i++) {
340
341                 ret = idmap_autorid_id_to_sid(globalcfg, dom, ids[i]);
342
343                 if ((!NT_STATUS_IS_OK(ret)) &&
344                     (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
345                         /* some fatal error occurred, log it */
346                         DBG_NOTICE("Unexpected error resolving an ID "
347                                    "(%d): %s\n", ids[i]->xid.id,
348                                    nt_errstr(ret));
349                         goto failure;
350                 }
351
352                 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
353                         num_mapped++;
354                 }
355
356         }
357
358         if (num_tomap == num_mapped) {
359                 return NT_STATUS_OK;
360         }
361         if (num_mapped == 0) {
362                 return NT_STATUS_NONE_MAPPED;
363         }
364
365         return STATUS_SOME_UNMAPPED;
366
367
368       failure:
369         return ret;
370 }
371
372 static bool idmap_autorid_sid_is_special(struct dom_sid *sid)
373 {
374         bool match;
375
376         match = sid_check_is_in_wellknown_domain(sid);
377         if (match) {
378                 return true;
379         }
380
381         return false;
382 }
383
384 static NTSTATUS idmap_autorid_sid_to_id_special(struct idmap_domain *dom,
385                                                 struct id_map *map)
386 {
387         struct idmap_tdb_common_context *common =
388                 talloc_get_type_abort(dom->private_data,
389                                       struct idmap_tdb_common_context);
390         uint32_t count;
391         struct autorid_range_config range;
392         NTSTATUS status;
393         uint32_t free_id;
394
395         status = idmap_autorid_get_alloc_range(dom, &range);
396         if (!NT_STATUS_IS_OK(status)) {
397                 return status;
398         }
399
400         /* Take the next free ID, counting from the top */
401         free_id = 0;
402         for (count = 0; count < IDMAP_AUTORID_ALLOC_RESERVED; count++) {
403                 struct id_map test_map;
404                 struct dom_sid sid;
405
406                 test_map.sid = &sid;
407                 test_map.xid.type = map->xid.type;
408                 test_map.xid.id = range.high_id - count;
409                 test_map.status = ID_UNKNOWN;
410
411                 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
412                 if (NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, status)) {
413                         free_id = test_map.xid.id;
414                         break;
415                 }
416
417                 if (!NT_STATUS_IS_OK(status)) {
418                         /* error - get out */
419                         return status;
420                 }
421
422                 /* mapping exists - try next ID */
423         }
424
425         if (free_id == 0) {
426                 return NT_STATUS_NONE_MAPPED;
427         }
428
429         map->status = ID_MAPPED;
430         map->xid.id = free_id;
431
432         status = common->rw_ops->set_mapping(dom, map);
433         if (!NT_STATUS_IS_OK(status)) {
434                 DEBUG(2, ("Error storing new mapping: %s\n",
435                           nt_errstr(status)));
436                 return status;
437         }
438
439         return NT_STATUS_OK;
440 }
441
442 struct idmap_autorid_sid_to_id_alloc_ctx {
443         struct idmap_domain *dom;
444         struct id_map *map;
445 };
446
447 static NTSTATUS idmap_autorid_sid_to_id_alloc_action(
448                                 struct db_context *db,
449                                 void *private_data)
450 {
451         struct idmap_autorid_sid_to_id_alloc_ctx *ctx;
452
453         ctx = (struct idmap_autorid_sid_to_id_alloc_ctx *)private_data;
454
455         if (idmap_autorid_sid_is_special(ctx->map->sid)) {
456                 NTSTATUS ret;
457
458                 ret = idmap_autorid_sid_to_id_special(ctx->dom, ctx->map);
459                 if (NT_STATUS_IS_OK(ret)) {
460                         return NT_STATUS_OK;
461                 }
462                 if (!NT_STATUS_EQUAL(NT_STATUS_NONE_MAPPED, ret)) {
463                         return ret;
464                 }
465
466                 DEBUG(10, ("Sepecial sid %s not mapped. falling back to "
467                            "regular allocation\n",
468                            sid_string_dbg(ctx->map->sid)));
469         }
470
471         return idmap_tdb_common_new_mapping(ctx->dom, ctx->map);
472 }
473
474 /*
475  * map a SID to xid using the idmap_tdb like pool
476  */
477 static NTSTATUS idmap_autorid_sid_to_id_alloc(
478                                         struct idmap_tdb_common_context *ctx,
479                                         struct idmap_domain *dom,
480                                         struct id_map *map)
481 {
482         NTSTATUS ret;
483         struct idmap_autorid_sid_to_id_alloc_ctx alloc_ctx;
484
485         map->status = ID_UNKNOWN;
486
487         /* see if we already have a mapping */
488         ret = idmap_tdb_common_sid_to_unixid(dom, map);
489
490         if (NT_STATUS_IS_OK(ret)) {
491                 map->status = ID_MAPPED;
492                 return ret;
493         }
494
495         /* bad things happened */
496         if (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
497                 DEBUG(1, ("Looking up SID->ID mapping for %s failed: %s\n",
498                           sid_string_dbg(map->sid), nt_errstr(ret)));
499                 return ret;
500         }
501
502         if (dom->read_only) {
503                 DEBUG(3, ("Not allocating new mapping for %s, because backend "
504                           "is read-only\n", sid_string_dbg(map->sid)));
505                 map->status = ID_UNMAPPED;
506                 return NT_STATUS_NONE_MAPPED;
507         }
508
509         DEBUG(10, ("Creating new mapping in pool for %s\n",
510                    sid_string_dbg(map->sid)));
511
512         alloc_ctx.dom = dom;
513         alloc_ctx.map = map;
514
515         ret = dbwrap_trans_do(ctx->db, idmap_autorid_sid_to_id_alloc_action,
516                               &alloc_ctx);
517         if (!NT_STATUS_IS_OK(ret)) {
518                 DEBUG(1, ("Failed to create a new mapping in alloc range: %s\n",
519                           nt_errstr(ret)));
520                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
521         }
522
523         map->status = ID_MAPPED;
524         return NT_STATUS_OK;
525 }
526
527 static bool idmap_autorid_domsid_is_for_alloc(struct dom_sid *sid)
528 {
529         bool match;
530
531         match = sid_check_is_wellknown_domain(sid, NULL);
532         if (match) {
533                 return true;
534         }
535
536         return false;
537 }
538
539 static NTSTATUS idmap_autorid_sid_to_id(struct idmap_tdb_common_context *common,
540                                         struct idmap_domain *dom,
541                                         struct id_map *map)
542 {
543         struct autorid_global_config *global =
544                 talloc_get_type_abort(common->private_data,
545                                       struct autorid_global_config);
546         struct autorid_range_config range;
547         uint32_t rid;
548         struct dom_sid domainsid;
549         NTSTATUS ret;
550
551         ZERO_STRUCT(range);
552         map->status = ID_UNKNOWN;
553
554         DEBUG(10, ("Trying to map %s\n", sid_string_dbg(map->sid)));
555
556         sid_copy(&domainsid, map->sid);
557         if (!sid_split_rid(&domainsid, &rid)) {
558                 DEBUG(4, ("Could not determine domain SID from %s, "
559                           "ignoring mapping request\n",
560                           sid_string_dbg(map->sid)));
561                 map->status = ID_UNMAPPED;
562                 return NT_STATUS_NONE_MAPPED;
563         }
564
565         if (idmap_autorid_domsid_is_for_alloc(&domainsid)) {
566                 DEBUG(10, ("SID %s is for ALLOC range.\n",
567                            sid_string_dbg(map->sid)));
568
569                 return idmap_autorid_sid_to_id_alloc(common, dom, map);
570         }
571
572         if (dom_sid_equal(&domainsid, &global_sid_Builtin) && ignore_builtin) {
573                 DEBUG(10, ("Ignoring request for BUILTIN domain\n"));
574                 map->status = ID_UNMAPPED;
575                 return NT_STATUS_NONE_MAPPED;
576         }
577
578         sid_to_fstring(range.domsid, &domainsid);
579
580         range.domain_range_index = rid / (global->rangesize);
581
582         ret = idmap_autorid_getrange(autorid_db, range.domsid,
583                                      range.domain_range_index,
584                                      &range.rangenum, &range.low_id);
585         if (NT_STATUS_IS_OK(ret)) {
586                 return idmap_autorid_sid_to_id_rid(
587                         global->rangesize, range.low_id, map);
588         }
589
590         if (dom->read_only) {
591                 DBG_DEBUG("read-only is enabled, did not allocate "
592                           "new range for domain %s\n", range.domsid);
593                 map->status = ID_UNMAPPED;
594                 return NT_STATUS_NONE_MAPPED;
595         }
596
597         /*
598          * Check if we should allocate a domain range. We need to
599          * protect against unknown domains to not fill our ranges
600          * needlessly.
601          */
602
603         if (sid_check_is_builtin(&domainsid) ||
604             sid_check_is_our_sam(&domainsid)) {
605                 goto allocate;
606         }
607
608         {
609                 struct winbindd_domain *domain;
610
611                 /*
612                  * Deterministic check for domain members: We can be
613                  * sure that the domain we are member of is worth to
614                  * add a mapping for.
615                  */
616
617                 domain = find_our_domain();
618                 if ((domain != NULL) &&
619                     dom_sid_equal(&domain->sid, &domainsid)) {
620                         goto allocate;
621                 }
622         }
623
624         /*
625          * If we have already allocated range index 0, this domain is
626          * worth allocating for in higher ranges.
627          */
628         if (range.domain_range_index != 0) {
629                 uint32_t zero_rangenum, zero_low_id;
630
631                 ret = idmap_autorid_getrange(autorid_db, range.domsid, 0,
632                                              &zero_rangenum, &zero_low_id);
633                 if (NT_STATUS_IS_OK(ret)) {
634                         goto allocate;
635                 }
636         }
637
638         /*
639          * If the caller already did a lookup sid and made sure the
640          * domain sid is valid, we can allocate a new range.
641          *
642          * Currently the winbindd parent already does a lookup sids
643          * first, but hopefully changes in future. If the
644          * caller knows the domain sid, ID_TYPE_BOTH should be
645          * passed instead of ID_TYPE_NOT_SPECIFIED.
646          */
647         if (map->xid.type != ID_TYPE_NOT_SPECIFIED) {
648                 goto allocate;
649         }
650
651         /*
652          * Check of last resort: A domain is valid if a user from that
653          * domain has recently logged in. The samlogon_cache these
654          * days also stores the domain sid.
655          *
656          * We used to check the list of trusted domains we received
657          * from "our" dc, but this is not reliable enough.
658          */
659         if (netsamlogon_cache_have(&domainsid)) {
660                 goto allocate;
661         }
662
663         /*
664          * Nobody knows this domain, so refuse to allocate a fresh
665          * range.
666          */
667
668         DBG_NOTICE("Allocating range for domain %s refused\n", range.domsid);
669         map->status = ID_UNMAPPED;
670         return NT_STATUS_NONE_MAPPED;
671
672 allocate:
673         ret = idmap_autorid_acquire_range(autorid_db, &range);
674         if (!NT_STATUS_IS_OK(ret)) {
675                 DBG_NOTICE("Could not determine range for domain: %s, "
676                            "check previous messages for reason\n",
677                            nt_errstr(ret));
678                 return ret;
679         }
680
681         return idmap_autorid_sid_to_id_rid(global->rangesize, range.low_id,
682                                            map);
683 }
684
685 /**********************************
686  lookup a set of sids.
687 **********************************/
688
689 static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
690                                               struct id_map **ids)
691 {
692         struct idmap_tdb_common_context *commoncfg;
693         NTSTATUS ret;
694         int i;
695         int num_tomap = 0;
696         int num_mapped = 0;
697
698         /* initialize the status to avoid surprise */
699         for (i = 0; ids[i]; i++) {
700                 ids[i]->status = ID_UNKNOWN;
701                 num_tomap++;
702         }
703
704         commoncfg =
705             talloc_get_type_abort(dom->private_data,
706                                   struct idmap_tdb_common_context);
707
708         for (i = 0; ids[i]; i++) {
709                 ret = idmap_autorid_sid_to_id(commoncfg, dom, ids[i]);
710                 if ((!NT_STATUS_IS_OK(ret)) &&
711                     (!NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED))) {
712                         /* some fatal error occurred, log it */
713                         DEBUG(3, ("Unexpected error resolving a SID (%s)\n",
714                                   sid_string_dbg(ids[i]->sid)));
715                         return ret;
716                 }
717
718                 if (NT_STATUS_IS_OK(ret) && ids[i]->status == ID_MAPPED) {
719                         num_mapped++;
720                 }
721         }
722
723         if (num_tomap == num_mapped) {
724                 return NT_STATUS_OK;
725         } else if (num_mapped == 0) {
726                 return NT_STATUS_NONE_MAPPED;
727         }
728
729         return STATUS_SOME_UNMAPPED;
730 }
731
732 static NTSTATUS idmap_autorid_preallocate_wellknown(struct idmap_domain *dom)
733 {
734         const char *groups[] = { "S-1-1-0", "S-1-2-0", "S-1-2-1",
735                 "S-1-3-0", "S-1-3-1", "S-1-3-2", "S-1-3-3", "S-1-3-4",
736                 "S-1-5-1", "S-1-5-2", "S-1-5-3", "S-1-5-4", "S-1-5-6",
737                 "S-1-5-7", "S-1-5-8", "S-1-5-9", "S-1-5-10", "S-1-5-11",
738                 "S-1-5-12", "S-1-5-13", "S-1-5-14", "S-1-5-15",
739                 "S-1-5-17", "S-1-5-18", "S-1-5-19", "S-1-5-20"
740         };
741
742         struct id_map **maps;
743         int i, num;
744         NTSTATUS status;
745
746         if (dom->read_only) {
747                 return NT_STATUS_OK;
748         }
749
750         num = ARRAY_SIZE(groups);
751
752         maps = talloc_array(talloc_tos(), struct id_map*, num+1);
753         if (!maps) {
754                 return NT_STATUS_NO_MEMORY;
755         }
756
757         for (i = 0; i < num; i++) {
758                 maps[i] = talloc(maps, struct id_map);
759                 if (maps[i] == NULL) {
760                         talloc_free(maps);
761                         return NT_STATUS_NO_MEMORY;
762                 }
763                 maps[i]->xid.type = ID_TYPE_GID;
764                 maps[i]->sid = dom_sid_parse_talloc(maps, groups[i]);
765         }
766
767         maps[num] = NULL;
768
769         status = idmap_autorid_sids_to_unixids(dom, maps);
770
771         DEBUG(10,("Preallocation run finished with status %s\n",
772                   nt_errstr(status)));
773
774         talloc_free(maps);
775
776         return NT_STATUS_IS_OK(status)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
777 }
778
779 static NTSTATUS idmap_autorid_initialize_action(struct db_context *db,
780                                                 void *private_data)
781 {
782         struct idmap_domain *dom;
783         struct idmap_tdb_common_context *common;
784         struct autorid_global_config *config;
785         NTSTATUS status;
786
787         dom = (struct idmap_domain *)private_data;
788         common = (struct idmap_tdb_common_context *)dom->private_data;
789         config = (struct autorid_global_config *)common->private_data;
790
791         status = idmap_autorid_init_hwms(db);
792         if (!NT_STATUS_IS_OK(status)) {
793                 return status;
794         }
795
796         status = idmap_autorid_saveconfig(db, config);
797         if (!NT_STATUS_IS_OK(status)) {
798                 DEBUG(1, ("Failed to store configuration data!\n"));
799                 return status;
800         }
801
802         status = idmap_autorid_preallocate_wellknown(dom);
803         if (!NT_STATUS_IS_OK(status)) {
804                 DEBUG(1, ("Failed to preallocate wellknown sids: %s\n",
805                           nt_errstr(status)));
806                 return status;
807         }
808
809         return NT_STATUS_OK;
810 }
811
812 static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
813 {
814         struct idmap_tdb_common_context *commonconfig;
815         struct autorid_global_config *config;
816         NTSTATUS status;
817         char *db_path;
818
819         if (!strequal(dom->name, "*")) {
820                 DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
821                           "for domain '%s'. But autorid can only be used for "
822                           "the default idmap configuration.\n", dom->name));
823                 return NT_STATUS_INVALID_PARAMETER;
824         }
825
826         commonconfig = talloc_zero(dom, struct idmap_tdb_common_context);
827         if (!commonconfig) {
828                 DEBUG(0, ("Out of memory!\n"));
829                 return NT_STATUS_NO_MEMORY;
830         }
831         dom->private_data = commonconfig;
832
833         commonconfig->rw_ops = talloc_zero(commonconfig, struct idmap_rw_ops);
834         if (commonconfig->rw_ops == NULL) {
835                 DEBUG(0, ("Out of memory!\n"));
836                 return NT_STATUS_NO_MEMORY;
837         }
838
839         config = talloc_zero(commonconfig, struct autorid_global_config);
840         if (!config) {
841                 DEBUG(0, ("Out of memory!\n"));
842                 return NT_STATUS_NO_MEMORY;
843         }
844         commonconfig->private_data = config;
845
846         config->minvalue = dom->low_id;
847         config->rangesize = idmap_config_int("*", "rangesize", 100000);
848
849         config->maxranges = (dom->high_id - dom->low_id + 1) /
850             config->rangesize;
851
852         if (config->maxranges == 0) {
853                 DEBUG(1, ("Allowed uid range is smaller than rangesize. "
854                           "Increase uid range or decrease rangesize.\n"));
855                 status = NT_STATUS_INVALID_PARAMETER;
856                 goto error;
857         }
858
859         /* check if the high-low limit is a multiple of the rangesize */
860         if ((dom->high_id - dom->low_id + 1) % config->rangesize != 0) {
861                 DEBUG(5, ("High uid-low uid difference of %d "
862                           "is not a multiple of the rangesize %d, "
863                           "limiting ranges to lower boundary number of %d\n",
864                           (dom->high_id - dom->low_id + 1), config->rangesize,
865                           config->maxranges));
866         }
867
868         DEBUG(5, ("%d domain ranges with a size of %d are available\n",
869                   config->maxranges, config->rangesize));
870
871         ignore_builtin = idmap_config_bool("*", "ignore builtin", false);
872
873         /* fill the TDB common configuration */
874
875         commonconfig->max_id = config->rangesize - 1
876                              - IDMAP_AUTORID_ALLOC_RESERVED;
877         commonconfig->hwmkey_uid = ALLOC_HWM_UID;
878         commonconfig->hwmkey_gid = ALLOC_HWM_GID;
879         commonconfig->rw_ops->get_new_id = idmap_autorid_allocate_id;
880         commonconfig->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
881
882         db_path = state_path(talloc_tos(), "autorid.tdb");
883         if (db_path == NULL) {
884                 status = NT_STATUS_NO_MEMORY;
885                 goto error;
886         }
887
888         status = idmap_autorid_db_open(db_path,
889                                        NULL, /* TALLOC_CTX */
890                                        &autorid_db);
891         TALLOC_FREE(db_path);
892         if (!NT_STATUS_IS_OK(status)) {
893                 goto error;
894         }
895
896         commonconfig->db = autorid_db;
897
898         status = dbwrap_trans_do(autorid_db,
899                                  idmap_autorid_initialize_action,
900                                  dom);
901         if (!NT_STATUS_IS_OK(status)) {
902                 DEBUG(1, ("Failed to init the idmap database: %s\n",
903                           nt_errstr(status)));
904                 goto error;
905         }
906
907         goto done;
908
909 error:
910         talloc_free(config);
911
912 done:
913         return status;
914 }
915
916 static struct idmap_methods autorid_methods = {
917         .init = idmap_autorid_initialize,
918         .unixids_to_sids = idmap_autorid_unixids_to_sids,
919         .sids_to_unixids = idmap_autorid_sids_to_unixids,
920         .allocate_id     = idmap_autorid_allocate_id
921 };
922
923 static_decl_idmap;
924 NTSTATUS idmap_autorid_init(TALLOC_CTX *ctx)
925 {
926         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
927                                   "autorid", &autorid_methods);
928 }