RIP BOOL. Convert BOOL -> bool. I found a few interesting
[ira/wip.git] / source3 / groupdb / mapping_tdb.c
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.
13  *  
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.
18  *  
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/>.
21  */
22
23 #include "includes.h"
24 #include "groupdb/mapping.h"
25
26 static TDB_CONTEXT *tdb; /* used for driver files */
27
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);
31         
32 /****************************************************************************
33  Open the group mapping tdb.
34 ****************************************************************************/
35 static bool init_group_mapping(void)
36 {
37         const char *vstring = "INFO/version";
38         int32 vers_id;
39         GROUP_MAP *map_table = NULL;
40         size_t num_entries = 0;
41         
42         if (tdb)
43                 return True;
44                 
45         tdb = tdb_open_log(lock_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;
49         }
50
51         /* handle a Samba upgrade */
52         tdb_lock_bystring(tdb, vstring);
53
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;
60         }
61
62         /* if its an unknown version we remove everthing in the db */
63         
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);
67         }
68
69         tdb_unlock_bystring(tdb, vstring);
70
71         /* cleanup any map entries with a gid == -1 */
72         
73         if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) {
74                 int i;
75                 
76                 for ( i=0; i<num_entries; i++ ) {
77                         if ( map_table[i].gid == -1 ) {
78                                 group_map_remove( &map_table[i].sid );
79                         }
80                 }
81                 
82                 SAFE_FREE( map_table );
83         }
84
85
86         return True;
87 }
88
89 /****************************************************************************
90 ****************************************************************************/
91 static bool add_mapping_entry(GROUP_MAP *map, int flag)
92 {
93         TDB_DATA dbuf;
94         pstring key, buf;
95         fstring string_sid="";
96         int len;
97
98         sid_to_string(string_sid, &map->sid);
99
100         len = tdb_pack((uint8 *)buf, sizeof(buf), "ddff",
101                         map->gid, map->sid_name_use, map->nt_name, map->comment);
102
103         if (len > sizeof(buf))
104                 return False;
105
106         slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
107
108         dbuf.dsize = len;
109         dbuf.dptr = (uint8 *)buf;
110         if (tdb_store_bystring(tdb, key, dbuf, flag) != 0) return False;
111
112         return True;
113 }
114
115
116 /****************************************************************************
117  Return the sid and the type of the unix group.
118 ****************************************************************************/
119
120 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
121 {
122         TDB_DATA dbuf;
123         pstring key;
124         fstring string_sid;
125         int ret = 0;
126         
127         /* the key is the SID, retrieving is direct */
128
129         sid_to_string(string_sid, &sid);
130         slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
131
132         dbuf = tdb_fetch_bystring(tdb, key);
133         if (!dbuf.dptr)
134                 return False;
135
136         ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
137                                 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
138
139         SAFE_FREE(dbuf.dptr);
140         
141         if ( ret == -1 ) {
142                 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
143                 return False;
144         }
145
146         sid_copy(&map->sid, &sid);
147         
148         return True;
149 }
150
151 /****************************************************************************
152  Return the sid and the type of the unix group.
153 ****************************************************************************/
154
155 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
156 {
157         TDB_DATA kbuf, dbuf, newkey;
158         fstring string_sid;
159         int ret;
160
161         /* we need to enumerate the TDB to find the GID */
162
163         for (kbuf = tdb_firstkey(tdb); 
164              kbuf.dptr; 
165              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
166
167                 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
168                 
169                 dbuf = tdb_fetch(tdb, kbuf);
170                 if (!dbuf.dptr)
171                         continue;
172
173                 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
174
175                 string_to_sid(&map->sid, string_sid);
176                 
177                 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
178                                  &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
179
180                 SAFE_FREE(dbuf.dptr);
181
182                 if ( ret == -1 ) {
183                         DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
184                         return False;
185                 }
186         
187                 if (gid==map->gid) {
188                         SAFE_FREE(kbuf.dptr);
189                         return True;
190                 }
191         }
192
193         return False;
194 }
195
196 /****************************************************************************
197  Return the sid and the type of the unix group.
198 ****************************************************************************/
199
200 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
201 {
202         TDB_DATA kbuf, dbuf, newkey;
203         fstring string_sid;
204         int ret;
205
206         /* we need to enumerate the TDB to find the name */
207
208         for (kbuf = tdb_firstkey(tdb); 
209              kbuf.dptr; 
210              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
211
212                 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
213                 
214                 dbuf = tdb_fetch(tdb, kbuf);
215                 if (!dbuf.dptr)
216                         continue;
217
218                 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
219
220                 string_to_sid(&map->sid, string_sid);
221                 
222                 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
223                                  &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
224
225                 SAFE_FREE(dbuf.dptr);
226                 
227                 if ( ret == -1 ) {
228                         DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
229                         return False;
230                 }
231
232                 if ( strequal(name, map->nt_name) ) {
233                         SAFE_FREE(kbuf.dptr);
234                         return True;
235                 }
236         }
237
238         return False;
239 }
240
241 /****************************************************************************
242  Remove a group mapping entry.
243 ****************************************************************************/
244
245 static bool group_map_remove(const DOM_SID *sid)
246 {
247         TDB_DATA dbuf;
248         pstring key;
249         fstring string_sid;
250         
251         /* the key is the SID, retrieving is direct */
252
253         sid_to_string(string_sid, sid);
254         slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
255
256         dbuf = tdb_fetch_bystring(tdb, key);
257         if (!dbuf.dptr)
258                 return False;
259         
260         SAFE_FREE(dbuf.dptr);
261
262         if(tdb_delete_bystring(tdb, key) != TDB_SUCCESS)
263                 return False;
264
265         return True;
266 }
267
268 /****************************************************************************
269  Enumerate the group mapping.
270 ****************************************************************************/
271
272 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
273                         size_t *p_num_entries, bool unix_only)
274 {
275         TDB_DATA kbuf, dbuf, newkey;
276         fstring string_sid;
277         GROUP_MAP map;
278         GROUP_MAP *mapt;
279         int ret;
280         size_t entries=0;
281         DOM_SID grpsid;
282         uint32 rid;
283
284         *p_num_entries=0;
285         *pp_rmap=NULL;
286
287         for (kbuf = tdb_firstkey(tdb); 
288              kbuf.dptr; 
289              newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
290
291                 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
292                         continue;
293
294                 dbuf = tdb_fetch(tdb, kbuf);
295                 if (!dbuf.dptr)
296                         continue;
297
298                 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
299                                 
300                 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
301                                  &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
302
303                 SAFE_FREE(dbuf.dptr);
304
305                 if ( ret == -1 ) {
306                         DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
307                         continue;
308                 }
309         
310                 /* list only the type or everything if UNKNOWN */
311                 if (sid_name_use!=SID_NAME_UNKNOWN  && sid_name_use!=map.sid_name_use) {
312                         DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
313                         continue;
314                 }
315
316                 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
317                         DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
318                         continue;
319                 }
320
321                 string_to_sid(&grpsid, string_sid);
322                 sid_copy( &map.sid, &grpsid );
323                 
324                 sid_split_rid( &grpsid, &rid );
325
326                 /* Only check the domain if we were given one */
327
328                 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
329                         DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n", 
330                                 string_sid, sid_string_static(domsid)));
331                         continue;
332                 }
333
334                 DEBUG(11,("enum_group_mapping: returning group %s of "
335                           "type %s\n", map.nt_name,
336                           sid_type_lookup(map.sid_name_use)));
337
338                 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
339                 if (!(*pp_rmap)) {
340                         DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
341                         return False;
342                 }
343
344                 mapt = (*pp_rmap);
345
346                 mapt[entries].gid = map.gid;
347                 sid_copy( &mapt[entries].sid, &map.sid);
348                 mapt[entries].sid_name_use = map.sid_name_use;
349                 fstrcpy(mapt[entries].nt_name, map.nt_name);
350                 fstrcpy(mapt[entries].comment, map.comment);
351
352                 entries++;
353
354         }
355
356         *p_num_entries=entries;
357
358         return True;
359 }
360
361 /* This operation happens on session setup, so it should better be fast. We
362  * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
363
364 static NTSTATUS one_alias_membership(const DOM_SID *member,
365                                DOM_SID **sids, size_t *num)
366 {
367         fstring key, string_sid;
368         TDB_DATA dbuf;
369         const char *p;
370
371         sid_to_string(string_sid, member);
372         slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
373
374         dbuf = tdb_fetch_bystring(tdb, key);
375
376         if (dbuf.dptr == NULL) {
377                 return NT_STATUS_OK;
378         }
379
380         p = (const char *)dbuf.dptr;
381
382         while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
383
384                 DOM_SID alias;
385
386                 if (!string_to_sid(&alias, string_sid))
387                         continue;
388
389                 if (!add_sid_to_array_unique(NULL, &alias, sids, num)) {
390                         return NT_STATUS_NO_MEMORY;
391                 }
392         }
393
394         SAFE_FREE(dbuf.dptr);
395         return NT_STATUS_OK;
396 }
397
398 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
399                                   DOM_SID **sids, size_t *num)
400 {
401         size_t i;
402
403         *num = 0;
404         *sids = NULL;
405
406         for (i=0; i<num_members; i++) {
407                 NTSTATUS status = one_alias_membership(&members[i], sids, num);
408                 if (!NT_STATUS_IS_OK(status))
409                         return status;
410         }
411         return NT_STATUS_OK;
412 }
413
414 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
415 {
416         DOM_SID *sids;
417         size_t i, num;
418
419         /* This feels the wrong way round, but the on-disk data structure
420          * dictates it this way. */
421         if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
422                 return False;
423
424         for (i=0; i<num; i++) {
425                 if (sid_compare(alias, &sids[i]) == 0) {
426                         TALLOC_FREE(sids);
427                         return True;
428                 }
429         }
430         TALLOC_FREE(sids);
431         return False;
432 }
433
434
435 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
436 {
437         GROUP_MAP map;
438         TDB_DATA dbuf;
439         pstring key;
440         fstring string_sid;
441         char *new_memberstring;
442         int result;
443
444         if (!get_group_map_from_sid(*alias, &map))
445                 return NT_STATUS_NO_SUCH_ALIAS;
446
447         if ( (map.sid_name_use != SID_NAME_ALIAS) &&
448              (map.sid_name_use != SID_NAME_WKN_GRP) )
449                 return NT_STATUS_NO_SUCH_ALIAS;
450
451         if (is_aliasmem(alias, member))
452                 return NT_STATUS_MEMBER_IN_ALIAS;
453
454         sid_to_string(string_sid, member);
455         slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
456
457         dbuf = tdb_fetch_bystring(tdb, key);
458
459         sid_to_string(string_sid, alias);
460
461         if (dbuf.dptr != NULL) {
462                 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
463                          string_sid);
464         } else {
465                 new_memberstring = SMB_STRDUP(string_sid);
466         }
467
468         if (new_memberstring == NULL)
469                 return NT_STATUS_NO_MEMORY;
470
471         SAFE_FREE(dbuf.dptr);
472         dbuf = string_term_tdb_data(new_memberstring);
473
474         result = tdb_store_bystring(tdb, key, dbuf, 0);
475
476         SAFE_FREE(new_memberstring);
477
478         return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
479 }
480
481 struct aliasmem_closure {
482         const DOM_SID *alias;
483         DOM_SID **sids;
484         size_t *num;
485 };
486
487 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
488                             void *state)
489 {
490         struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
491         const char *p;
492         fstring alias_string;
493
494         if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
495                     strlen(MEMBEROF_PREFIX)) != 0)
496                 return 0;
497
498         p = (const char *)data.dptr;
499
500         while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
501
502                 DOM_SID alias, member;
503                 const char *member_string;
504                 
505
506                 if (!string_to_sid(&alias, alias_string))
507                         continue;
508
509                 if (sid_compare(closure->alias, &alias) != 0)
510                         continue;
511
512                 /* Ok, we found the alias we're looking for in the membership
513                  * list currently scanned. The key represents the alias
514                  * member. Add that. */
515
516                 member_string = strchr((const char *)key.dptr, '/');
517
518                 /* Above we tested for MEMBEROF_PREFIX which includes the
519                  * slash. */
520
521                 SMB_ASSERT(member_string != NULL);
522                 member_string += 1;
523
524                 if (!string_to_sid(&member, member_string))
525                         continue;
526                 
527                 if (!add_sid_to_array(NULL, &member, closure->sids, closure->num)) {
528                         /* talloc fail. */
529                         break;
530                 }
531         }
532
533         return 0;
534 }
535
536 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
537 {
538         GROUP_MAP map;
539         struct aliasmem_closure closure;
540
541         if (!get_group_map_from_sid(*alias, &map))
542                 return NT_STATUS_NO_SUCH_ALIAS;
543
544         if ( (map.sid_name_use != SID_NAME_ALIAS) &&
545              (map.sid_name_use != SID_NAME_WKN_GRP) )
546                 return NT_STATUS_NO_SUCH_ALIAS;
547
548         *sids = NULL;
549         *num = 0;
550
551         closure.alias = alias;
552         closure.sids = sids;
553         closure.num = num;
554
555         tdb_traverse(tdb, collect_aliasmem, &closure);
556         return NT_STATUS_OK;
557 }
558
559 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
560 {
561         NTSTATUS result;
562         DOM_SID *sids;
563         size_t i, num;
564         bool found = False;
565         char *member_string;
566         TDB_DATA dbuf;
567         pstring key;
568         fstring sid_string;
569
570         result = alias_memberships(member, 1, &sids, &num);
571
572         if (!NT_STATUS_IS_OK(result))
573                 return result;
574
575         for (i=0; i<num; i++) {
576                 if (sid_compare(&sids[i], alias) == 0) {
577                         found = True;
578                         break;
579                 }
580         }
581
582         if (!found) {
583                 TALLOC_FREE(sids);
584                 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
585         }
586
587         if (i < num)
588                 sids[i] = sids[num-1];
589
590         num -= 1;
591
592         sid_to_string(sid_string, member);
593         slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
594
595         if (num == 0)
596                 return tdb_delete_bystring(tdb, key) == 0 ?
597                         NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
598
599         member_string = SMB_STRDUP("");
600
601         if (member_string == NULL) {
602                 TALLOC_FREE(sids);
603                 return NT_STATUS_NO_MEMORY;
604         }
605
606         for (i=0; i<num; i++) {
607                 char *s = member_string;
608
609                 sid_to_string(sid_string, &sids[i]);
610                 asprintf(&member_string, "%s %s", s, sid_string);
611
612                 SAFE_FREE(s);
613                 if (member_string == NULL) {
614                         TALLOC_FREE(sids);
615                         return NT_STATUS_NO_MEMORY;
616                 }
617         }
618
619         dbuf = string_term_tdb_data(member_string);
620
621         result = tdb_store_bystring(tdb, key, dbuf, 0) == 0 ?
622                 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
623
624         TALLOC_FREE(sids);
625         SAFE_FREE(member_string);
626
627         return result;
628 }
629
630
631 static const struct mapping_backend tdb_backend = {
632         .add_mapping_entry         = add_mapping_entry,
633         .get_group_map_from_sid    = get_group_map_from_sid,
634         .get_group_map_from_gid    = get_group_map_from_gid,
635         .get_group_map_from_ntname = get_group_map_from_ntname,
636         .group_map_remove          = group_map_remove,
637         .enum_group_mapping        = enum_group_mapping,
638         .one_alias_membership      = one_alias_membership,
639         .add_aliasmem              = add_aliasmem,
640         .del_aliasmem              = del_aliasmem,
641         .enum_aliasmem             = enum_aliasmem      
642 };
643
644 /*
645   initialise the tdb mapping backend
646  */
647 const struct mapping_backend *groupdb_tdb_init(void)
648 {
649         if (!init_group_mapping()) {
650                 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
651                 return NULL;
652         }
653
654         return &tdb_backend;
655 }