Fix "status used uninitialized" warnings.
[Samba/nascimento.git] / source3 / groupdb / mapping_tdb.c
blob67e377c338c12b4e3e8f7fbc6101d38dff1b8893
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2006,
5 * Copyright (C) Jean François Micouleau 1998-2001.
6 * Copyright (C) Volker Lendecke 2006.
7 * Copyright (C) Gerald Carter 2006.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "groupdb/mapping.h"
26 static TDB_CONTEXT *tdb; /* used for driver files */
28 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
29 size_t *p_num_entries, bool unix_only);
30 static bool group_map_remove(const DOM_SID *sid);
32 /****************************************************************************
33 Open the group mapping tdb.
34 ****************************************************************************/
35 static bool init_group_mapping(void)
37 const char *vstring = "INFO/version";
38 int32 vers_id;
39 GROUP_MAP *map_table = NULL;
40 size_t num_entries = 0;
42 if (tdb)
43 return True;
45 tdb = tdb_open_log(state_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
46 if (!tdb) {
47 DEBUG(0,("Failed to open group mapping database\n"));
48 return False;
51 /* handle a Samba upgrade */
52 tdb_lock_bystring(tdb, vstring);
54 /* Cope with byte-reversed older versions of the db. */
55 vers_id = tdb_fetch_int32(tdb, vstring);
56 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
57 /* Written on a bigendian machine with old fetch_int code. Save as le. */
58 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
59 vers_id = DATABASE_VERSION_V2;
62 /* if its an unknown version we remove everthing in the db */
64 if (vers_id != DATABASE_VERSION_V2) {
65 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
66 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
69 tdb_unlock_bystring(tdb, vstring);
71 /* cleanup any map entries with a gid == -1 */
73 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) {
74 int i;
76 for ( i=0; i<num_entries; i++ ) {
77 if ( map_table[i].gid == -1 ) {
78 group_map_remove( &map_table[i].sid );
82 SAFE_FREE( map_table );
86 return True;
89 /****************************************************************************
90 ****************************************************************************/
91 static bool add_mapping_entry(GROUP_MAP *map, int flag)
93 TDB_DATA dbuf;
94 char *key = NULL;
95 char *buf = NULL;
96 fstring string_sid="";
97 int len;
98 bool ret;
100 sid_to_fstring(string_sid, &map->sid);
102 len = tdb_pack(NULL, sizeof(buf), "ddff",
103 map->gid, map->sid_name_use, map->nt_name, map->comment);
104 if (len) {
105 buf = SMB_MALLOC_ARRAY(char, len);
106 if (!buf) {
107 return false;
109 len = tdb_pack((uint8 *)buf, sizeof(buf), "ddff", map->gid,
110 map->sid_name_use, map->nt_name, map->comment);
113 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
114 SAFE_FREE(buf);
115 return false;
118 dbuf.dsize = len;
119 dbuf.dptr = (uint8 *)buf;
121 ret = (tdb_store_bystring(tdb, key, dbuf, flag) == 0);
123 SAFE_FREE(key);
124 SAFE_FREE(buf);
125 return ret;
129 /****************************************************************************
130 Return the sid and the type of the unix group.
131 ****************************************************************************/
133 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
135 TDB_DATA dbuf;
136 char *key = NULL;
137 fstring string_sid;
138 int ret = 0;
140 /* the key is the SID, retrieving is direct */
142 sid_to_fstring(string_sid, &sid);
143 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
144 return false;
147 dbuf = tdb_fetch_bystring(tdb, key);
148 if (!dbuf.dptr) {
149 SAFE_FREE(key);
150 return false;
153 SAFE_FREE(key);
155 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
156 &map->gid, &map->sid_name_use,
157 &map->nt_name, &map->comment);
159 SAFE_FREE(dbuf.dptr);
161 if ( ret == -1 ) {
162 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
163 return False;
166 sid_copy(&map->sid, &sid);
168 return True;
171 /****************************************************************************
172 Return the sid and the type of the unix group.
173 ****************************************************************************/
175 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
177 TDB_DATA kbuf, dbuf, newkey;
178 fstring string_sid;
179 int ret;
181 /* we need to enumerate the TDB to find the GID */
183 for (kbuf = tdb_firstkey(tdb);
184 kbuf.dptr;
185 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
187 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
189 dbuf = tdb_fetch(tdb, kbuf);
190 if (!dbuf.dptr)
191 continue;
193 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
195 string_to_sid(&map->sid, string_sid);
197 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
198 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
200 SAFE_FREE(dbuf.dptr);
202 if ( ret == -1 ) {
203 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
204 return False;
207 if (gid==map->gid) {
208 SAFE_FREE(kbuf.dptr);
209 return True;
213 return False;
216 /****************************************************************************
217 Return the sid and the type of the unix group.
218 ****************************************************************************/
220 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
222 TDB_DATA kbuf, dbuf, newkey;
223 fstring string_sid;
224 int ret;
226 /* we need to enumerate the TDB to find the name */
228 for (kbuf = tdb_firstkey(tdb);
229 kbuf.dptr;
230 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
232 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
234 dbuf = tdb_fetch(tdb, kbuf);
235 if (!dbuf.dptr)
236 continue;
238 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
240 string_to_sid(&map->sid, string_sid);
242 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
243 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
245 SAFE_FREE(dbuf.dptr);
247 if ( ret == -1 ) {
248 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
249 return False;
252 if ( strequal(name, map->nt_name) ) {
253 SAFE_FREE(kbuf.dptr);
254 return True;
258 return False;
261 /****************************************************************************
262 Remove a group mapping entry.
263 ****************************************************************************/
265 static bool group_map_remove(const DOM_SID *sid)
267 TDB_DATA dbuf;
268 char *key = NULL;
269 fstring string_sid;
270 bool ret;
272 /* the key is the SID, retrieving is direct */
274 sid_to_fstring(string_sid, sid);
275 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
276 return false;
279 dbuf = tdb_fetch_bystring(tdb, key);
280 if (!dbuf.dptr) {
281 SAFE_FREE(key);
282 return false;
285 SAFE_FREE(dbuf.dptr);
287 ret = (tdb_delete_bystring(tdb, key) == TDB_SUCCESS);
288 SAFE_FREE(key);
289 return ret;
292 /****************************************************************************
293 Enumerate the group mapping.
294 ****************************************************************************/
296 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
297 size_t *p_num_entries, bool unix_only)
299 TDB_DATA kbuf, dbuf, newkey;
300 fstring string_sid;
301 GROUP_MAP map;
302 GROUP_MAP *mapt;
303 int ret;
304 size_t entries=0;
305 DOM_SID grpsid;
306 uint32 rid;
308 *p_num_entries=0;
309 *pp_rmap=NULL;
311 for (kbuf = tdb_firstkey(tdb);
312 kbuf.dptr;
313 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
315 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
316 continue;
318 dbuf = tdb_fetch(tdb, kbuf);
319 if (!dbuf.dptr)
320 continue;
322 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
324 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
325 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
327 SAFE_FREE(dbuf.dptr);
329 if ( ret == -1 ) {
330 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
331 continue;
334 /* list only the type or everything if UNKNOWN */
335 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
336 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
337 continue;
340 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
341 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
342 continue;
345 string_to_sid(&grpsid, string_sid);
346 sid_copy( &map.sid, &grpsid );
348 sid_split_rid( &grpsid, &rid );
350 /* Only check the domain if we were given one */
352 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
353 DEBUG(11,("enum_group_mapping: group %s is not in "
354 "domain %s\n", string_sid,
355 sid_string_dbg(domsid)));
356 continue;
359 DEBUG(11,("enum_group_mapping: returning group %s of "
360 "type %s\n", map.nt_name,
361 sid_type_lookup(map.sid_name_use)));
363 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
364 if (!(*pp_rmap)) {
365 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
366 return False;
369 mapt = (*pp_rmap);
371 mapt[entries].gid = map.gid;
372 sid_copy( &mapt[entries].sid, &map.sid);
373 mapt[entries].sid_name_use = map.sid_name_use;
374 fstrcpy(mapt[entries].nt_name, map.nt_name);
375 fstrcpy(mapt[entries].comment, map.comment);
377 entries++;
381 *p_num_entries=entries;
383 return True;
386 /* This operation happens on session setup, so it should better be fast. We
387 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
389 static NTSTATUS one_alias_membership(const DOM_SID *member,
390 DOM_SID **sids, size_t *num)
392 fstring tmp;
393 fstring key;
394 char *string_sid;
395 TDB_DATA dbuf;
396 const char *p;
397 NTSTATUS status = NT_STATUS_OK;
398 TALLOC_CTX *frame;
400 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
401 sid_to_fstring(tmp, member));
403 dbuf = tdb_fetch_bystring(tdb, key);
405 if (dbuf.dptr == NULL) {
406 return NT_STATUS_OK;
409 p = (const char *)dbuf.dptr;
410 frame = talloc_stackframe();
411 while (next_token_talloc(frame, &p, &string_sid, " ")) {
412 DOM_SID alias;
414 if (!string_to_sid(&alias, string_sid))
415 continue;
417 status= add_sid_to_array_unique(NULL, &alias, sids, num);
418 if (!NT_STATUS_IS_OK(status)) {
419 goto done;
423 done:
424 TALLOC_FREE(frame);
425 SAFE_FREE(dbuf.dptr);
426 return status;
429 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
430 DOM_SID **sids, size_t *num)
432 size_t i;
434 *num = 0;
435 *sids = NULL;
437 for (i=0; i<num_members; i++) {
438 NTSTATUS status = one_alias_membership(&members[i], sids, num);
439 if (!NT_STATUS_IS_OK(status))
440 return status;
442 return NT_STATUS_OK;
445 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
447 DOM_SID *sids;
448 size_t i, num;
450 /* This feels the wrong way round, but the on-disk data structure
451 * dictates it this way. */
452 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
453 return False;
455 for (i=0; i<num; i++) {
456 if (sid_compare(alias, &sids[i]) == 0) {
457 TALLOC_FREE(sids);
458 return True;
461 TALLOC_FREE(sids);
462 return False;
466 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
468 GROUP_MAP map;
469 TDB_DATA dbuf;
470 char *key = NULL;
471 fstring string_sid;
472 char *new_memberstring;
473 int result;
475 if (!get_group_map_from_sid(*alias, &map))
476 return NT_STATUS_NO_SUCH_ALIAS;
478 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
479 (map.sid_name_use != SID_NAME_WKN_GRP) )
480 return NT_STATUS_NO_SUCH_ALIAS;
482 if (is_aliasmem(alias, member))
483 return NT_STATUS_MEMBER_IN_ALIAS;
485 sid_to_fstring(string_sid, member);
486 if (asprintf(&key, "%s%s", MEMBEROF_PREFIX, string_sid) < 0) {
487 return NT_STATUS_NO_MEMORY;
490 dbuf = tdb_fetch_bystring(tdb, key);
492 sid_to_fstring(string_sid, alias);
494 if (dbuf.dptr != NULL) {
495 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
496 string_sid);
497 } else {
498 new_memberstring = SMB_STRDUP(string_sid);
501 if (new_memberstring == NULL) {
502 SAFE_FREE(key);
503 return NT_STATUS_NO_MEMORY;
506 SAFE_FREE(dbuf.dptr);
507 dbuf = string_term_tdb_data(new_memberstring);
509 result = tdb_store_bystring(tdb, key, dbuf, 0);
511 SAFE_FREE(new_memberstring);
512 SAFE_FREE(key);
514 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
517 struct aliasmem_closure {
518 const DOM_SID *alias;
519 DOM_SID **sids;
520 size_t *num;
523 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
524 void *state)
526 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
527 const char *p;
528 char *alias_string;
529 TALLOC_CTX *frame;
531 if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
532 strlen(MEMBEROF_PREFIX)) != 0)
533 return 0;
535 p = (const char *)data.dptr;
537 frame = talloc_stackframe();
538 while (next_token_talloc(frame, &p, &alias_string, " ")) {
539 DOM_SID alias, member;
540 const char *member_string;
542 if (!string_to_sid(&alias, alias_string))
543 continue;
545 if (sid_compare(closure->alias, &alias) != 0)
546 continue;
548 /* Ok, we found the alias we're looking for in the membership
549 * list currently scanned. The key represents the alias
550 * member. Add that. */
552 member_string = strchr((const char *)key.dptr, '/');
554 /* Above we tested for MEMBEROF_PREFIX which includes the
555 * slash. */
557 SMB_ASSERT(member_string != NULL);
558 member_string += 1;
560 if (!string_to_sid(&member, member_string))
561 continue;
563 if (!NT_STATUS_IS_OK(add_sid_to_array(NULL, &member,
564 closure->sids,
565 closure->num)))
567 /* talloc fail. */
568 break;
572 TALLOC_FREE(frame);
573 return 0;
576 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
578 GROUP_MAP map;
579 struct aliasmem_closure closure;
581 if (!get_group_map_from_sid(*alias, &map))
582 return NT_STATUS_NO_SUCH_ALIAS;
584 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
585 (map.sid_name_use != SID_NAME_WKN_GRP) )
586 return NT_STATUS_NO_SUCH_ALIAS;
588 *sids = NULL;
589 *num = 0;
591 closure.alias = alias;
592 closure.sids = sids;
593 closure.num = num;
595 tdb_traverse(tdb, collect_aliasmem, &closure);
596 return NT_STATUS_OK;
599 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
601 NTSTATUS result;
602 DOM_SID *sids;
603 size_t i, num;
604 bool found = False;
605 char *member_string;
606 TDB_DATA dbuf;
607 char *key = NULL;
608 fstring sid_string;
610 result = alias_memberships(member, 1, &sids, &num);
612 if (!NT_STATUS_IS_OK(result))
613 return result;
615 for (i=0; i<num; i++) {
616 if (sid_compare(&sids[i], alias) == 0) {
617 found = True;
618 break;
622 if (!found) {
623 TALLOC_FREE(sids);
624 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
627 if (i < num)
628 sids[i] = sids[num-1];
630 num -= 1;
632 sid_to_fstring(sid_string, member);
633 if (asprintf(&key, "%s%s", MEMBEROF_PREFIX, sid_string) < 0) {
634 TALLOC_FREE(sids);
635 return NT_STATUS_NO_MEMORY;
638 if (num == 0) {
639 NTSTATUS ret = (tdb_delete_bystring(tdb, key) == 0 ?
640 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
641 TALLOC_FREE(sids);
642 SAFE_FREE(key);
643 return ret;
646 member_string = SMB_STRDUP("");
648 if (member_string == NULL) {
649 TALLOC_FREE(sids);
650 SAFE_FREE(key);
651 return NT_STATUS_NO_MEMORY;
654 for (i=0; i<num; i++) {
655 char *s = member_string;
657 sid_to_fstring(sid_string, &sids[i]);
658 asprintf(&member_string, "%s %s", s, sid_string);
660 SAFE_FREE(s);
661 if (member_string == NULL) {
662 TALLOC_FREE(sids);
663 SAFE_FREE(key);
664 return NT_STATUS_NO_MEMORY;
668 dbuf = string_term_tdb_data(member_string);
670 result = tdb_store_bystring(tdb, key, dbuf, 0) == 0 ?
671 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
673 TALLOC_FREE(sids);
674 SAFE_FREE(member_string);
675 SAFE_FREE(key);
677 return result;
681 static const struct mapping_backend tdb_backend = {
682 .add_mapping_entry = add_mapping_entry,
683 .get_group_map_from_sid = get_group_map_from_sid,
684 .get_group_map_from_gid = get_group_map_from_gid,
685 .get_group_map_from_ntname = get_group_map_from_ntname,
686 .group_map_remove = group_map_remove,
687 .enum_group_mapping = enum_group_mapping,
688 .one_alias_membership = one_alias_membership,
689 .add_aliasmem = add_aliasmem,
690 .del_aliasmem = del_aliasmem,
691 .enum_aliasmem = enum_aliasmem
695 initialise the tdb mapping backend
697 const struct mapping_backend *groupdb_tdb_init(void)
699 if (!init_group_mapping()) {
700 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
701 return NULL;
704 return &tdb_backend;