lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[bbaumbach/samba-autobuild/.git] / source3 / winbindd / idmap_tdb2.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap TDB2 backend, used for clustered Samba setups.
5
6    This uses dbwrap to access tdb files. The location can be set
7    using tdb:idmap2.tdb =" in smb.conf
8
9    Copyright (C) Andrew Tridgell 2007
10
11    This is heavily based upon idmap_tdb.c, which is:
12
13    Copyright (C) Tim Potter 2000
14    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
15    Copyright (C) Jeremy Allison 2006
16    Copyright (C) Simo Sorce 2003-2006
17    Copyright (C) Michael Adam 2009-2010
18
19    This program is free software; you can redistribute it and/or modify
20    it under the terms of the GNU General Public License as published by
21    the Free Software Foundation; either version 2 of the License, or
22    (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27    GNU General Public License for more details.
28
29    You should have received a copy of the GNU General Public License
30    along with this program; if not, write to the Free Software
31    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
34 #include "includes.h"
35 #include "system/filesys.h"
36 #include "winbindd.h"
37 #include "idmap.h"
38 #include "idmap_rw.h"
39 #include "dbwrap/dbwrap.h"
40 #include "dbwrap/dbwrap_open.h"
41 #include "../libcli/security/dom_sid.h"
42 #include "util_tdb.h"
43 #include "idmap_tdb_common.h"
44
45 #undef DBGC_CLASS
46 #define DBGC_CLASS DBGC_IDMAP
47
48 struct idmap_tdb2_context {
49         const char *script; /* script to provide idmaps */
50 };
51
52 /* High water mark keys */
53 #define HWM_GROUP  "GROUP HWM"
54 #define HWM_USER   "USER HWM"
55
56 /*
57  * check and initialize high/low water marks in the db
58  */
59 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
60 {
61         NTSTATUS status;
62         uint32_t low_id;
63         struct idmap_tdb_common_context *ctx;
64
65         ctx = talloc_get_type(dom->private_data,
66                               struct idmap_tdb_common_context);
67
68         /* Create high water marks for group and user id */
69
70         status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_USER, &low_id);
71         if (!NT_STATUS_IS_OK(status) || (low_id < dom->low_id)) {
72                 status = dbwrap_trans_store_uint32_bystring(ctx->db, HWM_USER,
73                                                             dom->low_id);
74                 if (!NT_STATUS_IS_OK(status)) {
75                         DEBUG(0, ("Unable to initialise user hwm in idmap "
76                                   "database: %s\n", nt_errstr(status)));
77                         return NT_STATUS_INTERNAL_DB_ERROR;
78                 }
79         }
80
81         status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_GROUP, &low_id);
82         if (!NT_STATUS_IS_OK(status) || (low_id < dom->low_id)) {
83                 status = dbwrap_trans_store_uint32_bystring(ctx->db, HWM_GROUP,
84                                                             dom->low_id);
85                 if (!NT_STATUS_IS_OK(status)) {
86                         DEBUG(0, ("Unable to initialise group hwm in idmap "
87                                   "database: %s\n", nt_errstr(status)));
88                         return NT_STATUS_INTERNAL_DB_ERROR;
89                 }
90         }
91
92         return NT_STATUS_OK;
93 }
94
95
96 /*
97   open the permanent tdb
98  */
99 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
100 {
101         char *db_path;
102         struct idmap_tdb_common_context *ctx;
103
104         ctx = talloc_get_type(dom->private_data,
105                               struct idmap_tdb_common_context);
106
107         if (ctx->db) {
108                 /* its already open */
109                 return NT_STATUS_OK;
110         }
111
112         db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
113         NT_STATUS_HAVE_NO_MEMORY(db_path);
114
115         /* Open idmap repository */
116         ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
117                           DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
118         TALLOC_FREE(db_path);
119
120         if (ctx->db == NULL) {
121                 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
122                           db_path));
123                 return NT_STATUS_UNSUCCESSFUL;
124         }
125
126         return idmap_tdb2_init_hwm(dom);
127 }
128
129 /**
130  * store a mapping in the database.
131  */
132
133 struct idmap_tdb2_set_mapping_context {
134         const char *ksidstr;
135         const char *kidstr;
136 };
137
138 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
139                                               void *private_data)
140 {
141         TDB_DATA data;
142         NTSTATUS ret;
143         struct idmap_tdb2_set_mapping_context *state;
144         TALLOC_CTX *tmp_ctx = talloc_stackframe();
145
146         state = (struct idmap_tdb2_set_mapping_context *)private_data;
147
148         DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
149
150         /* check wheter sid mapping is already present in db */
151         ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
152         if (NT_STATUS_IS_OK(ret)) {
153                 ret = NT_STATUS_OBJECT_NAME_COLLISION;
154                 goto done;
155         }
156
157         ret = dbwrap_store_bystring(db, state->ksidstr,
158                                     string_term_tdb_data(state->kidstr),
159                                     TDB_INSERT);
160         if (!NT_STATUS_IS_OK(ret)) {
161                 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
162                 goto done;
163         }
164
165         ret = dbwrap_store_bystring(db, state->kidstr,
166                                     string_term_tdb_data(state->ksidstr),
167                                     TDB_INSERT);
168         if (!NT_STATUS_IS_OK(ret)) {
169                 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
170                 /* try to remove the previous stored SID -> ID map */
171                 dbwrap_delete_bystring(db, state->ksidstr);
172                 goto done;
173         }
174
175         DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
176
177 done:
178         talloc_free(tmp_ctx);
179         return ret;
180 }
181
182 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
183 {
184         struct idmap_tdb2_context *ctx;
185         NTSTATUS ret;
186         char *ksidstr, *kidstr;
187         struct idmap_tdb_common_context *commonctx;
188         struct idmap_tdb2_set_mapping_context state;
189
190         if (!map || !map->sid) {
191                 return NT_STATUS_INVALID_PARAMETER;
192         }
193
194         ksidstr = kidstr = NULL;
195
196         /* TODO: should we filter a set_mapping using low/high filters ? */
197
198         commonctx = talloc_get_type(dom->private_data,
199                                     struct idmap_tdb_common_context);
200
201         ctx = talloc_get_type(commonctx->private_data,
202                               struct idmap_tdb2_context);
203
204         switch (map->xid.type) {
205
206         case ID_TYPE_UID:
207                 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
208                 break;
209
210         case ID_TYPE_GID:
211                 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
212                 break;
213
214         default:
215                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
216                 return NT_STATUS_INVALID_PARAMETER;
217         }
218
219         if (kidstr == NULL) {
220                 DEBUG(0, ("ERROR: Out of memory!\n"));
221                 ret = NT_STATUS_NO_MEMORY;
222                 goto done;
223         }
224
225         ksidstr = sid_string_talloc(ctx, map->sid);
226         if (ksidstr == NULL) {
227                 DEBUG(0, ("Out of memory!\n"));
228                 ret = NT_STATUS_NO_MEMORY;
229                 goto done;
230         }
231
232         state.ksidstr = ksidstr;
233         state.kidstr = kidstr;
234
235         ret = dbwrap_trans_do(commonctx->db, idmap_tdb2_set_mapping_action,
236                               &state);
237
238 done:
239         talloc_free(ksidstr);
240         talloc_free(kidstr);
241         return ret;
242 }
243
244 /*
245   run a script to perform a mapping
246
247   The script should the following command lines:
248
249       SIDTOID S-1-xxxx
250       IDTOSID UID xxxx
251       IDTOSID GID xxxx
252
253   and should return one of the following as a single line of text
254      UID:xxxx
255      GID:xxxx
256      SID:xxxx
257      ERR:xxxx
258  */
259 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx,
260                                   struct id_map *map, const char *fmt, ...)
261                                   PRINTF_ATTRIBUTE(3,4);
262
263 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
264                                   const char *fmt, ...)
265 {
266         va_list ap;
267         char *cmd;
268         FILE *p;
269         char line[64];
270         unsigned long v;
271
272         cmd = talloc_asprintf(ctx, "%s ", ctx->script);
273         NT_STATUS_HAVE_NO_MEMORY(cmd);  
274
275         va_start(ap, fmt);
276         cmd = talloc_vasprintf_append(cmd, fmt, ap);
277         va_end(ap);
278         NT_STATUS_HAVE_NO_MEMORY(cmd);
279
280         p = popen(cmd, "r");
281         talloc_free(cmd);
282         if (p == NULL) {
283                 return NT_STATUS_NONE_MAPPED;
284         }
285
286         if (fgets(line, sizeof(line)-1, p) == NULL) {
287                 pclose(p);
288                 return NT_STATUS_NONE_MAPPED;
289         }
290         pclose(p);
291
292         DEBUG(10,("idmap script gave: %s\n", line));
293
294         if (sscanf(line, "UID:%lu", &v) == 1) {
295                 map->xid.id   = v;
296                 map->xid.type = ID_TYPE_UID;
297         } else if (sscanf(line, "GID:%lu", &v) == 1) {
298                 map->xid.id   = v;
299                 map->xid.type = ID_TYPE_GID;            
300         } else if (strncmp(line, "SID:S-", 6) == 0) {
301                 if (!string_to_sid(map->sid, &line[4])) {
302                         DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
303                                  line, ctx->script));
304                         return NT_STATUS_NONE_MAPPED;                   
305                 }
306         } else {
307                 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
308                          line, ctx->script));
309                 return NT_STATUS_NONE_MAPPED;
310         }
311
312         return NT_STATUS_OK;
313 }
314
315
316
317 /*
318   Single id to sid lookup function. 
319 */
320 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
321 {
322         NTSTATUS ret;
323         TDB_DATA data;
324         char *keystr;
325         NTSTATUS status;
326         struct idmap_tdb_common_context *commonctx;
327         struct idmap_tdb2_context *ctx;
328
329
330         if (!dom || !map) {
331                 return NT_STATUS_INVALID_PARAMETER;
332         }
333
334         status = idmap_tdb2_open_db(dom);
335         NT_STATUS_NOT_OK_RETURN(status);
336
337         commonctx = talloc_get_type(dom->private_data,
338                                     struct idmap_tdb_common_context);
339
340         ctx = talloc_get_type(commonctx->private_data,
341                               struct idmap_tdb2_context);
342
343         /* apply filters before checking */
344         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
345                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
346                                 map->xid.id, dom->low_id, dom->high_id));
347                 return NT_STATUS_NONE_MAPPED;
348         }
349
350         switch (map->xid.type) {
351
352         case ID_TYPE_UID:
353                 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
354                 break;
355
356         case ID_TYPE_GID:
357                 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
358                 break;
359
360         default:
361                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
362                 return NT_STATUS_INVALID_PARAMETER;
363         }
364
365         if (keystr == NULL) {
366                 DEBUG(0, ("Out of memory!\n"));
367                 ret = NT_STATUS_NO_MEMORY;
368                 goto done;
369         }
370
371         DEBUG(10,("Fetching record %s\n", keystr));
372
373         /* Check if the mapping exists */
374         status = dbwrap_fetch_bystring(commonctx->db, keystr, keystr, &data);
375
376         if (!NT_STATUS_IS_OK(status)) {
377                 char *sidstr;
378                 struct idmap_tdb2_set_mapping_context store_state;
379
380                 DEBUG(10,("Record %s not found\n", keystr));
381                 if (ctx->script == NULL) {
382                         ret = NT_STATUS_NONE_MAPPED;
383                         goto done;
384                 }
385
386                 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
387                 if (!NT_STATUS_IS_OK(ret)) {
388                         goto done;
389                 }
390
391                 sidstr = sid_string_talloc(keystr, map->sid);
392                 if (!sidstr) {
393                         ret = NT_STATUS_NO_MEMORY;
394                         goto done;
395                 }
396
397                 store_state.ksidstr = sidstr;
398                 store_state.kidstr = keystr;
399
400                 ret = dbwrap_trans_do(commonctx->db,
401                                       idmap_tdb2_set_mapping_action,
402                                       &store_state);
403                 goto done;
404         }
405
406         if (!string_to_sid(map->sid, (const char *)data.dptr)) {
407                 DEBUG(10,("INVALID SID (%s) in record %s\n",
408                         (const char *)data.dptr, keystr));
409                 ret = NT_STATUS_INTERNAL_DB_ERROR;
410                 goto done;
411         }
412
413         DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
414         ret = NT_STATUS_OK;
415
416 done:
417         talloc_free(keystr);
418         return ret;
419 }
420
421
422 /*
423  Single sid to id lookup function. 
424 */
425 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
426 {
427         NTSTATUS ret;
428         TDB_DATA data;
429         char *keystr;
430         unsigned long rec_id = 0;
431         struct idmap_tdb_common_context *commonctx;
432         struct idmap_tdb2_context *ctx;
433         TALLOC_CTX *tmp_ctx = talloc_stackframe();
434
435         ret = idmap_tdb2_open_db(dom);
436         NT_STATUS_NOT_OK_RETURN(ret);
437
438         commonctx = talloc_get_type(dom->private_data,
439                                     struct idmap_tdb_common_context);
440
441         ctx = talloc_get_type(commonctx->private_data,
442                               struct idmap_tdb2_context);
443
444         keystr = sid_string_talloc(tmp_ctx, map->sid);
445         if (keystr == NULL) {
446                 DEBUG(0, ("Out of memory!\n"));
447                 ret = NT_STATUS_NO_MEMORY;
448                 goto done;
449         }
450
451         DEBUG(10,("Fetching record %s\n", keystr));
452
453         /* Check if sid is present in database */
454         ret = dbwrap_fetch_bystring(commonctx->db, tmp_ctx, keystr, &data);
455         if (!NT_STATUS_IS_OK(ret)) {
456                 char *idstr;
457                 struct idmap_tdb2_set_mapping_context store_state;
458
459                 DEBUG(10,(__location__ " Record %s not found\n", keystr));
460
461                 if (ctx->script == NULL) {
462                         ret = NT_STATUS_NONE_MAPPED;
463                         goto done;
464                 }
465
466                 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
467                 if (!NT_STATUS_IS_OK(ret)) {
468                         goto done;
469                 }
470
471                 /* apply filters before returning result */
472                 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
473                         DEBUG(5, ("Script returned id (%u) out of range "
474                                   "(%u - %u). Filtered!\n",
475                                   map->xid.id, dom->low_id, dom->high_id));
476                         ret = NT_STATUS_NONE_MAPPED;
477                         goto done;
478                 }
479
480                 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
481                                         map->xid.type == ID_TYPE_UID?'U':'G',
482                                         (unsigned long)map->xid.id);
483                 if (idstr == NULL) {
484                         ret = NT_STATUS_NO_MEMORY;
485                         goto done;
486                 }
487
488                 store_state.ksidstr = keystr;
489                 store_state.kidstr = idstr;
490
491                 ret = dbwrap_trans_do(commonctx->db,
492                                       idmap_tdb2_set_mapping_action,
493                                       &store_state);
494                 goto done;
495         }
496
497         /* What type of record is this ? */
498         if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
499                 map->xid.id = rec_id;
500                 map->xid.type = ID_TYPE_UID;
501                 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
502                 ret = NT_STATUS_OK;
503
504         } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
505                 map->xid.id = rec_id;
506                 map->xid.type = ID_TYPE_GID;
507                 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
508                 ret = NT_STATUS_OK;
509
510         } else { /* Unknown record type ! */
511                 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
512                 ret = NT_STATUS_INTERNAL_DB_ERROR;
513                 goto done;
514         }
515
516         /* apply filters before returning result */
517         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
518                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
519                                 map->xid.id, dom->low_id, dom->high_id));
520                 ret = NT_STATUS_NONE_MAPPED;
521         }
522
523 done:
524         talloc_free(tmp_ctx);
525         return ret;
526 }
527
528 /*
529   Initialise idmap database.
530 */
531 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
532 {
533         NTSTATUS ret;
534         struct idmap_tdb_common_context *commonctx;
535         struct idmap_tdb2_context *ctx;
536         const char * idmap_script = NULL;
537
538         commonctx = talloc_zero(dom, struct idmap_tdb_common_context);
539         if(!commonctx) {
540                 DEBUG(0, ("Out of memory!\n"));
541                 return NT_STATUS_NO_MEMORY;
542         }
543
544         commonctx->rw_ops = talloc_zero(commonctx, struct idmap_rw_ops);
545         if (commonctx->rw_ops == NULL) {
546                 DEBUG(0, ("Out of memory!\n"));
547                 ret = NT_STATUS_NO_MEMORY;
548                 goto failed;
549         }
550
551         ctx = talloc_zero(commonctx, struct idmap_tdb2_context);
552         if (!ctx) {
553                 DEBUG(0, ("Out of memory!\n"));
554                 ret = NT_STATUS_NO_MEMORY;
555                 goto failed;
556         }
557
558         ctx->script = idmap_config_const_string(dom->name, "script", NULL);
559
560         idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
561         if (idmap_script != NULL) {
562                 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
563                           " Please use 'idmap config * : script' instead!\n"));
564         }
565
566         if (strequal(dom->name, "*") && ctx->script == NULL) {
567                 /* fall back to idmap:script for backwards compatibility */
568                 ctx->script = idmap_script;
569         }
570
571         if (ctx->script) {
572                 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
573         }
574
575         commonctx->max_id = dom->high_id;
576         commonctx->hwmkey_uid = HWM_USER;
577         commonctx->hwmkey_gid = HWM_GROUP;
578
579         commonctx->sid_to_unixid_fn = idmap_tdb2_sid_to_id;
580         commonctx->unixid_to_sid_fn = idmap_tdb2_id_to_sid;
581
582         commonctx->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
583         commonctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
584
585         commonctx->private_data = ctx;
586         dom->private_data = commonctx;
587
588         ret = idmap_tdb2_open_db(dom);
589         if (!NT_STATUS_IS_OK(ret)) {
590                 goto failed;
591         }
592
593         return NT_STATUS_OK;
594
595 failed:
596         talloc_free(commonctx);
597         return ret;
598 }
599
600
601 static struct idmap_methods db_methods = {
602         .init            = idmap_tdb2_db_init,
603         .unixids_to_sids = idmap_tdb_common_unixids_to_sids,
604         .sids_to_unixids = idmap_tdb_common_sids_to_unixids,
605         .allocate_id     = idmap_tdb_common_get_new_id
606 };
607
608 static_decl_idmap;
609 NTSTATUS idmap_tdb2_init(TALLOC_CTX *ctx)
610 {
611         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);
612 }