idmap_tdb_common: fix a debug message in idmap_tdb_common_set_mapping()
[bbaumbach/samba-autobuild/.git] / source3 / winbindd / idmap_tdb_common.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    common functions for TDB based idmapping backends
5
6    Copyright (C) Christian Ambach 2012
7
8    These functions were initially copied over from idmap_tdb.c and idmap_tdb2.c
9    which are:
10
11    Copyright (C) Tim Potter 2000
12    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
13    Copyright (C) Jeremy Allison 2006
14    Copyright (C) Simo Sorce 2003-2006
15    Copyright (C) Michael Adam 2009-2010
16    Copyright (C) Andrew Tridgell 2007
17
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License as published by
20    the Free Software Foundation; either version 2 of the License, or
21    (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26    GNU General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
33 #include "includes.h"
34 #include "idmap_tdb_common.h"
35 #include "dbwrap/dbwrap.h"
36 #include "util_tdb.h"
37 #include "idmap_rw.h"
38 #include "../libcli/security/dom_sid.h"
39
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_IDMAP
42
43 struct idmap_tdb_common_allocate_id_context {
44         const char *hwmkey;
45         const char *hwmtype;
46         uint32_t high_hwm;
47         uint32_t hwm;
48 };
49
50 static NTSTATUS idmap_tdb_common_allocate_id_action(struct db_context *db,
51                                                     void *private_data)
52 {
53         NTSTATUS ret;
54         struct idmap_tdb_common_allocate_id_context *state;
55         uint32_t hwm;
56
57         state = (struct idmap_tdb_common_allocate_id_context *)private_data;
58
59         ret = dbwrap_fetch_uint32_bystring(db, state->hwmkey, &hwm);
60         if (!NT_STATUS_IS_OK(ret)) {
61                 ret = NT_STATUS_INTERNAL_DB_ERROR;
62                 goto done;
63         }
64
65         /* check it is in the range */
66         if (hwm > state->high_hwm) {
67                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
68                           state->hwmtype, (unsigned long)state->high_hwm));
69                 ret = NT_STATUS_UNSUCCESSFUL;
70                 goto done;
71         }
72
73         /* fetch a new id and increment it */
74         ret = dbwrap_change_uint32_atomic_bystring(db, state->hwmkey, &hwm, 1);
75         if (!NT_STATUS_IS_OK(ret)) {
76                 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
77                           state->hwmtype));
78                 goto done;
79         }
80
81         /* recheck it is in the range */
82         if (hwm > state->high_hwm) {
83                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
84                           state->hwmtype, (unsigned long)state->high_hwm));
85                 ret = NT_STATUS_UNSUCCESSFUL;
86                 goto done;
87         }
88
89         ret = NT_STATUS_OK;
90         state->hwm = hwm;
91
92       done:
93         return ret;
94 }
95
96 static NTSTATUS idmap_tdb_common_allocate_id(struct idmap_domain *dom,
97                                              struct unixid *xid)
98 {
99         const char *hwmkey;
100         const char *hwmtype;
101         uint32_t hwm = 0;
102         NTSTATUS status;
103         struct idmap_tdb_common_allocate_id_context state;
104         struct idmap_tdb_common_context *ctx;
105
106         ctx =
107             talloc_get_type_abort(dom->private_data,
108                                   struct idmap_tdb_common_context);
109
110         /* Get current high water mark */
111         switch (xid->type) {
112
113         case ID_TYPE_UID:
114                 hwmkey = ctx->hwmkey_uid;
115                 hwmtype = "UID";
116                 break;
117
118         case ID_TYPE_GID:
119                 hwmkey = ctx->hwmkey_gid;
120                 hwmtype = "GID";
121                 break;
122
123         default:
124                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
125                 return NT_STATUS_INVALID_PARAMETER;
126         }
127
128         state.hwm = hwm;
129         state.high_hwm = ctx->max_id;
130         state.hwmtype = hwmtype;
131         state.hwmkey = hwmkey;
132
133         status = dbwrap_trans_do(ctx->db, idmap_tdb_common_allocate_id_action,
134                                  &state);
135
136         if (NT_STATUS_IS_OK(status)) {
137                 xid->id = state.hwm;
138                 DEBUG(10, ("New %s = %d\n", hwmtype, state.hwm));
139         } else {
140                 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
141         }
142
143         return status;
144 }
145
146 /**
147  * Allocate a new unix-ID.
148  * For now this is for the default idmap domain only.
149  * Should be extended later on.
150  */
151 NTSTATUS idmap_tdb_common_get_new_id(struct idmap_domain * dom,
152                                      struct unixid * id)
153 {
154         NTSTATUS ret;
155
156         if (!strequal(dom->name, "*")) {
157                 DEBUG(3, ("idmap_tdb_common_get_new_id: "
158                           "Refusing allocation of a new unixid for domain'%s'. "
159                           "Currently only supported for the default "
160                           "domain \"*\".\n", dom->name));
161                 return NT_STATUS_NOT_IMPLEMENTED;
162         }
163
164         ret = idmap_tdb_common_allocate_id(dom, id);
165
166         return ret;
167 }
168
169 /**
170  * store a mapping in the database.
171  */
172
173 struct idmap_tdb_common_set_mapping_context {
174         const char *ksidstr;
175         const char *kidstr;
176 };
177
178 static NTSTATUS idmap_tdb_common_set_mapping_action(struct db_context *db,
179                                                     void *private_data)
180 {
181         TDB_DATA data;
182         NTSTATUS ret;
183         struct idmap_tdb_common_set_mapping_context *state;
184         TALLOC_CTX *tmp_ctx = talloc_stackframe();
185
186         state = (struct idmap_tdb_common_set_mapping_context *)private_data;
187
188         DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
189
190         /* check whether sid mapping is already present in db */
191         ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
192         if (NT_STATUS_IS_OK(ret)) {
193                 ret = NT_STATUS_OBJECT_NAME_COLLISION;
194                 goto done;
195         }
196
197         ret = dbwrap_store_bystring(db, state->ksidstr,
198                                     string_term_tdb_data(state->kidstr),
199                                     TDB_INSERT);
200         if (!NT_STATUS_IS_OK(ret)) {
201                 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
202                 goto done;
203         }
204
205         ret = dbwrap_store_bystring(db, state->kidstr,
206                                     string_term_tdb_data(state->ksidstr),
207                                     TDB_INSERT);
208         if (!NT_STATUS_IS_OK(ret)) {
209                 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
210                 /* try to remove the previous stored SID -> ID map */
211                 dbwrap_delete_bystring(db, state->ksidstr);
212                 goto done;
213         }
214
215         DEBUG(10, ("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
216
217       done:
218         talloc_free(tmp_ctx);
219         return ret;
220 }
221
222 NTSTATUS idmap_tdb_common_set_mapping(struct idmap_domain * dom,
223                                       const struct id_map * map)
224 {
225         struct idmap_tdb_common_context *ctx;
226         struct idmap_tdb_common_set_mapping_context state;
227         NTSTATUS ret;
228         char *ksidstr, *kidstr;
229
230         if (!map || !map->sid) {
231                 return NT_STATUS_INVALID_PARAMETER;
232         }
233
234         ksidstr = kidstr = NULL;
235
236         /* TODO: should we filter a set_mapping using low/high filters ? */
237
238         ctx =
239             talloc_get_type_abort(dom->private_data,
240                                   struct idmap_tdb_common_context);
241
242         switch (map->xid.type) {
243
244         case ID_TYPE_UID:
245                 kidstr =
246                     talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
247                 break;
248
249         case ID_TYPE_GID:
250                 kidstr =
251                     talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
252                 break;
253
254         default:
255                 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map->xid.type));
256                 return NT_STATUS_INVALID_PARAMETER;
257         }
258
259         if (kidstr == NULL) {
260                 DEBUG(0, ("ERROR: Out of memory!\n"));
261                 ret = NT_STATUS_NO_MEMORY;
262                 goto done;
263         }
264
265         ksidstr = sid_string_talloc(ctx, map->sid);
266         if (ksidstr == NULL) {
267                 DEBUG(0, ("Out of memory!\n"));
268                 ret = NT_STATUS_NO_MEMORY;
269                 goto done;
270         }
271
272         state.ksidstr = ksidstr;
273         state.kidstr = kidstr;
274
275         ret = dbwrap_trans_do(ctx->db, idmap_tdb_common_set_mapping_action,
276                               &state);
277
278       done:
279         talloc_free(ksidstr);
280         talloc_free(kidstr);
281         return ret;
282 }
283
284 /*
285  * Create a new mapping for an unmapped SID, also allocating a new ID.
286  * This should be run inside a transaction.
287  *
288  * TODO:
289  *  Properly integrate this with multi domain idmap config:
290  *  Currently, the allocator is default-config only.
291  */
292 NTSTATUS idmap_tdb_common_new_mapping(struct idmap_domain * dom,
293                                       struct id_map * map)
294 {
295         NTSTATUS ret;
296         struct idmap_tdb_common_context *ctx;
297
298         ctx =
299             talloc_get_type_abort(dom->private_data,
300                                   struct idmap_tdb_common_context);
301
302         ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
303
304         return ret;
305 }
306
307 /*
308   lookup a set of unix ids
309 */
310 NTSTATUS idmap_tdb_common_unixids_to_sids(struct idmap_domain * dom,
311                                           struct id_map ** ids)
312 {
313         NTSTATUS ret;
314         int i, num_mapped = 0;
315         struct idmap_tdb_common_context *ctx;
316
317         NTSTATUS(*unixid_to_sid_fn) (struct idmap_domain * dom,
318                                      struct id_map * map);
319         ctx =
320             talloc_get_type_abort(dom->private_data,
321                                   struct idmap_tdb_common_context);
322
323         if (ctx->unixid_to_sid_fn == NULL) {
324                 unixid_to_sid_fn = idmap_tdb_common_unixid_to_sid;
325         } else {
326                 unixid_to_sid_fn = ctx->unixid_to_sid_fn;
327         }
328
329         /* initialize the status to avoid surprise */
330         for (i = 0; ids[i]; i++) {
331                 ids[i]->status = ID_UNKNOWN;
332         }
333
334         for (i = 0; ids[i]; i++) {
335                 ret = unixid_to_sid_fn(dom, ids[i]);
336                 if (!NT_STATUS_IS_OK(ret)) {
337
338                         /* if it is just a failed mapping continue */
339                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
340
341                                 /* make sure it is marked as unmapped */
342                                 ids[i]->status = ID_UNMAPPED;
343                                 continue;
344                         }
345
346                         /* some fatal error occurred, return immediately */
347                         goto done;
348                 }
349
350                 /* all ok, id is mapped */
351                 ids[i]->status = ID_MAPPED;
352                 num_mapped += 1;
353         }
354
355         ret = NT_STATUS_OK;
356
357 done:
358
359         if (NT_STATUS_IS_OK(ret)) {
360                 if (i == 0 || num_mapped == 0) {
361                         ret = NT_STATUS_NONE_MAPPED;
362                 } else if (num_mapped < i) {
363                         ret = STATUS_SOME_UNMAPPED;
364                 } else {
365                         ret = NT_STATUS_OK;
366                 }
367         }
368
369         return ret;
370 }
371
372 /*
373   default single id to sid lookup function
374 */
375 NTSTATUS idmap_tdb_common_unixid_to_sid(struct idmap_domain * dom,
376                                         struct id_map * map)
377 {
378         NTSTATUS ret;
379         TDB_DATA data;
380         char *keystr;
381         struct idmap_tdb_common_context *ctx;
382
383         if (!dom || !map) {
384                 return NT_STATUS_INVALID_PARAMETER;
385         }
386 /* TODO: do we really make sure the database is open again? should have been
387  * done while initing the module
388         status = idmap_tdb2_open_db(dom);
389         NT_STATUS_NOT_OK_RETURN(status);
390 */
391         ctx =
392             talloc_get_type_abort(dom->private_data,
393                                   struct idmap_tdb_common_context);
394
395         /* apply filters before checking */
396         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
397                 DEBUG(5,
398                       ("Requested id (%u) out of range (%u - %u). Filtered!\n",
399                        map->xid.id, dom->low_id, dom->high_id));
400                 return NT_STATUS_NONE_MAPPED;
401         }
402
403         switch (map->xid.type) {
404
405         case ID_TYPE_UID:
406                 keystr =
407                     talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
408                 break;
409
410         case ID_TYPE_GID:
411                 keystr =
412                     talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
413                 break;
414
415         default:
416                 DEBUG(2, ("INVALID unix ID type: 0x%02x\n", map->xid.type));
417                 return NT_STATUS_INVALID_PARAMETER;
418         }
419
420         if (keystr == NULL) {
421                 DEBUG(0, ("Out of memory!\n"));
422                 ret = NT_STATUS_NO_MEMORY;
423                 goto done;
424         }
425
426         DEBUG(10, ("Fetching record %s\n", keystr));
427
428         /* Check if the mapping exists */
429         ret = dbwrap_fetch_bystring(ctx->db, keystr, keystr, &data);
430
431         if (!NT_STATUS_IS_OK(ret)) {
432                 DEBUG(10, ("Record %s not found\n", keystr));
433                 ret = NT_STATUS_NONE_MAPPED;
434                 goto done;
435         }
436
437         if (!string_to_sid(map->sid, (const char *)data.dptr)) {
438                 DEBUG(10, ("INVALID SID (%s) in record %s\n",
439                            (const char *)data.dptr, keystr));
440                 ret = NT_STATUS_INTERNAL_DB_ERROR;
441                 goto done;
442         }
443
444         DEBUG(10, ("Found record %s -> %s\n", keystr, (const char *)data.dptr));
445         ret = NT_STATUS_OK;
446
447       done:
448         talloc_free(keystr);
449         return ret;
450 }
451
452 /**********************************
453  Single sid to id lookup function.
454 **********************************/
455
456 NTSTATUS idmap_tdb_common_sid_to_unixid(struct idmap_domain * dom,
457                                         struct id_map * map)
458 {
459         NTSTATUS ret;
460         TDB_DATA data;
461         char *keystr;
462         unsigned long rec_id = 0;
463         struct idmap_tdb_common_context *ctx;
464         TALLOC_CTX *tmp_ctx = talloc_stackframe();
465
466         if (!dom || !map) {
467                 talloc_free(tmp_ctx);
468                 return NT_STATUS_INVALID_PARAMETER;
469         }
470
471         ctx =
472             talloc_get_type_abort(dom->private_data,
473                                   struct idmap_tdb_common_context);
474
475         keystr = sid_string_talloc(tmp_ctx, map->sid);
476         if (keystr == NULL) {
477                 DEBUG(0, ("Out of memory!\n"));
478                 ret = NT_STATUS_NO_MEMORY;
479                 goto done;
480         }
481
482         DEBUG(10, ("Fetching record %s\n", keystr));
483
484         /* Check if sid is present in database */
485         ret = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr, &data);
486         if (!NT_STATUS_IS_OK(ret)) {
487                 DEBUG(10, ("Record %s not found\n", keystr));
488                 ret = NT_STATUS_NONE_MAPPED;
489                 goto done;
490         }
491
492         /* What type of record is this ? */
493         if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) {
494                 /* Try a UID record. */
495                 map->xid.id = rec_id;
496                 map->xid.type = ID_TYPE_UID;
497                 DEBUG(10,
498                       ("Found uid record %s -> %s \n", keystr,
499                        (const char *)data.dptr));
500                 ret = NT_STATUS_OK;
501
502         } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) {
503                 /* Try a GID record. */
504                 map->xid.id = rec_id;
505                 map->xid.type = ID_TYPE_GID;
506                 DEBUG(10,
507                       ("Found gid record %s -> %s \n", keystr,
508                        (const char *)data.dptr));
509                 ret = NT_STATUS_OK;
510
511         } else {                /* Unknown record type ! */
512                 DEBUG(2,
513                       ("Found INVALID record %s -> %s\n", keystr,
514                        (const char *)data.dptr));
515                 ret = NT_STATUS_INTERNAL_DB_ERROR;
516                 goto done;
517         }
518
519         /* apply filters before returning result */
520         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
521                 DEBUG(5,
522                       ("Requested id (%u) out of range (%u - %u). Filtered!\n",
523                        map->xid.id, dom->low_id, dom->high_id));
524                 ret = NT_STATUS_NONE_MAPPED;
525         }
526
527       done:
528         talloc_free(tmp_ctx);
529         return ret;
530 }
531
532 /**********************************
533  lookup a set of sids
534 **********************************/
535
536 struct idmap_tdb_common_sids_to_unixids_context {
537         struct idmap_domain *dom;
538         struct id_map **ids;
539         bool allocate_unmapped;
540          NTSTATUS(*sid_to_unixid_fn) (struct idmap_domain * dom,
541                                       struct id_map * map);
542 };
543
544 static NTSTATUS idmap_tdb_common_sids_to_unixids_action(struct db_context *db,
545                                                         void *private_data)
546 {
547         struct idmap_tdb_common_sids_to_unixids_context *state;
548         int i, num_mapped = 0;
549         NTSTATUS ret = NT_STATUS_OK;
550
551         state = (struct idmap_tdb_common_sids_to_unixids_context *)private_data;
552
553         DEBUG(10, ("idmap_tdb_common_sids_to_unixids: "
554                    " domain: [%s], allocate: %s\n",
555                    state->dom->name, state->allocate_unmapped ? "yes" : "no"));
556
557         for (i = 0; state->ids[i]; i++) {
558                 if ((state->ids[i]->status == ID_UNKNOWN) ||
559                     /* retry if we could not map in previous run: */
560                     (state->ids[i]->status == ID_UNMAPPED)) {
561                         NTSTATUS ret2;
562
563                         ret2 = state->sid_to_unixid_fn(state->dom,
564                                         state->ids[i]);
565
566                         if (!NT_STATUS_IS_OK(ret2)) {
567
568                                 /* if it is just a failed mapping, continue */
569                                 if (NT_STATUS_EQUAL
570                                     (ret2, NT_STATUS_NONE_MAPPED)) {
571
572                                         /* make sure it is marked as unmapped */
573                                         state->ids[i]->status = ID_UNMAPPED;
574                                         ret = STATUS_SOME_UNMAPPED;
575                                 } else {
576                                         /*
577                                          * some fatal error occurred,
578                                          * return immediately
579                                          */
580                                         ret = ret2;
581                                         goto done;
582                                 }
583                         } else {
584                                 /* all ok, id is mapped */
585                                 state->ids[i]->status = ID_MAPPED;
586                         }
587                 }
588
589                 if (state->ids[i]->status == ID_MAPPED) {
590                         num_mapped += 1;
591                 }
592
593                 if ((state->ids[i]->status == ID_UNMAPPED) &&
594                     state->allocate_unmapped) {
595                         ret =
596                             idmap_tdb_common_new_mapping(state->dom,
597                                                          state->ids[i]);
598                         if (!NT_STATUS_IS_OK(ret)) {
599                                 goto done;
600                         }
601                         num_mapped += 1;
602                 }
603         }
604
605 done:
606
607         if (NT_STATUS_IS_OK(ret) ||
608             NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED)) {
609                 if (i == 0 || num_mapped == 0) {
610                         ret = NT_STATUS_NONE_MAPPED;
611                 } else if (num_mapped < i) {
612                         ret = STATUS_SOME_UNMAPPED;
613                 } else {
614                         ret = NT_STATUS_OK;
615                 }
616         }
617
618         return ret;
619 }
620
621 NTSTATUS idmap_tdb_common_sids_to_unixids(struct idmap_domain * dom,
622                                           struct id_map ** ids)
623 {
624         NTSTATUS ret;
625         int i;
626         struct idmap_tdb_common_sids_to_unixids_context state;
627         struct idmap_tdb_common_context *ctx;
628
629         ctx =
630             talloc_get_type_abort(dom->private_data,
631                                   struct idmap_tdb_common_context);
632
633         /* initialize the status to avoid surprise */
634         for (i = 0; ids[i]; i++) {
635                 ids[i]->status = ID_UNKNOWN;
636         }
637
638         state.dom = dom;
639         state.ids = ids;
640         state.allocate_unmapped = false;
641         if (ctx->sid_to_unixid_fn == NULL) {
642                 state.sid_to_unixid_fn = idmap_tdb_common_sid_to_unixid;
643         } else {
644                 state.sid_to_unixid_fn = ctx->sid_to_unixid_fn;
645         }
646
647         ret = idmap_tdb_common_sids_to_unixids_action(ctx->db, &state);
648
649         if ( (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) ||
650               NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) &&
651              !dom->read_only) {
652                 state.allocate_unmapped = true;
653                 ret = dbwrap_trans_do(ctx->db,
654                                       idmap_tdb_common_sids_to_unixids_action,
655                                       &state);
656         }
657
658         return ret;
659 }