groupdb: make mapping_tdb compatible to mapping_ldb
[amitay/samba.git] / source3 / groupdb / mapping.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-2000,
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 const struct mapping_backend *backend;
27
28 /*
29   initialise a group mapping backend
30  */
31 static bool init_group_mapping(void)
32 {
33         const char *backend_string;
34
35         if (backend != NULL) {
36                 /* already initialised */
37                 return True;
38         }
39
40         /*
41          * default to using the ldb backend. This parameter should
42          * disappear in future versions of Samba3.
43          *
44          * But it's needed for cluster setups, because it's
45          * not yet possible to distribute a ldb inside a cluster.
46          */
47         backend_string = lp_parm_const_string(-1, "groupdb", "backend", "ldb");
48
49         if (strcmp(backend_string, "ldb") == 0) {
50                 backend = groupdb_ldb_init();
51         } else if (strcmp(backend_string, "tdb") == 0) {
52                 backend = groupdb_tdb_init();
53         } else {
54                 DEBUG(0,("Unknown groupdb backend '%s'\n", backend_string));
55                 smb_panic("Unknown groupdb backend");
56         }
57
58         return backend != NULL;
59 }
60
61 /****************************************************************************
62 initialise first time the mapping list
63 ****************************************************************************/
64 NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum lsa_SidType sid_name_use, const char *nt_name, const char *comment)
65 {
66         GROUP_MAP map;
67
68         if(!init_group_mapping()) {
69                 DEBUG(0,("failed to initialize group mapping\n"));
70                 return NT_STATUS_UNSUCCESSFUL;
71         }
72         
73         map.gid=gid;
74         if (!string_to_sid(&map.sid, sid)) {
75                 DEBUG(0, ("string_to_sid failed: %s", sid));
76                 return NT_STATUS_UNSUCCESSFUL;
77         }
78         
79         map.sid_name_use=sid_name_use;
80         fstrcpy(map.nt_name, nt_name);
81         fstrcpy(map.comment, comment);
82
83         return pdb_add_group_mapping_entry(&map);
84 }
85
86 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
87                                   DOM_SID **sids, size_t *num)
88 {
89         size_t i;
90
91         *num = 0;
92         *sids = NULL;
93
94         for (i=0; i<num_members; i++) {
95                 NTSTATUS status = backend->one_alias_membership(&members[i], sids, num);
96                 if (!NT_STATUS_IS_OK(status))
97                         return status;
98         }
99         return NT_STATUS_OK;
100 }
101
102 struct aliasmem_closure {
103         const DOM_SID *alias;
104         DOM_SID **sids;
105         size_t *num;
106 };
107
108
109
110 /*
111  *
112  * High level functions
113  * better to use them than the lower ones.
114  *
115  * we are checking if the group is in the mapping file
116  * and if the group is an existing unix group
117  *
118  */
119
120 /* get a domain group from it's SID */
121
122 bool get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
123 {
124         struct group *grp;
125         bool ret;
126         
127         if(!init_group_mapping()) {
128                 DEBUG(0,("failed to initialize group mapping\n"));
129                 return(False);
130         }
131
132         DEBUG(10, ("get_domain_group_from_sid\n"));
133
134         /* if the group is NOT in the database, it CAN NOT be a domain group */
135         
136         become_root();
137         ret = pdb_getgrsid(map, sid);
138         unbecome_root();
139         
140         /* special case check for rid 513 */
141         
142         if ( !ret ) {
143                 uint32 rid;
144                 
145                 sid_peek_rid( &sid, &rid );
146                 
147                 if ( rid == DOMAIN_GROUP_RID_USERS ) {
148                         fstrcpy( map->nt_name, "None" );
149                         fstrcpy( map->comment, "Ordinary Users" );
150                         sid_copy( &map->sid, &sid );
151                         map->sid_name_use = SID_NAME_DOM_GRP;
152                         map->gid = (gid_t)-1;
153                         
154                         return True;
155                 }
156                 
157                 return False;
158         }
159
160         DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
161
162         /* if it's not a domain group, continue */
163         if (map->sid_name_use!=SID_NAME_DOM_GRP) {
164                 return False;
165         }
166
167         DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
168         
169         if (map->gid==-1) {
170                 return False;
171         }
172
173         DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
174         
175         grp = getgrgid(map->gid);
176         if ( !grp ) {
177                 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
178                 return False;
179         }
180
181         DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
182
183         return True;
184 }
185
186 /****************************************************************************
187  Create a UNIX group on demand.
188 ****************************************************************************/
189
190 int smb_create_group(const char *unix_group, gid_t *new_gid)
191 {
192         char *add_script = NULL;
193         int     ret = -1;
194         int     fd = 0;
195
196         *new_gid = 0;
197
198         /* defer to scripts */
199
200         if ( *lp_addgroup_script() ) {
201                 TALLOC_CTX *ctx = talloc_tos();
202
203                 add_script = talloc_strdup(ctx,
204                                         lp_addgroup_script());
205                 if (!add_script) {
206                         return -1;
207                 }
208                 add_script = talloc_string_sub(ctx,
209                                 add_script, "%g", unix_group);
210                 if (!add_script) {
211                         return -1;
212                 }
213
214                 ret = smbrun(add_script, &fd);
215                 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
216                 if (ret == 0) {
217                         smb_nscd_flush_group_cache();
218                 }
219                 if (ret != 0)
220                         return ret;
221
222                 if (fd != 0) {
223                         fstring output;
224
225                         *new_gid = 0;
226                         if (read(fd, output, sizeof(output)) > 0) {
227                                 *new_gid = (gid_t)strtoul(output, NULL, 10);
228                         }
229
230                         close(fd);
231                 }
232
233         }
234
235         if (*new_gid == 0) {
236                 struct group *grp = getgrnam(unix_group);
237
238                 if (grp != NULL)
239                         *new_gid = grp->gr_gid;
240         }
241
242         return ret;
243 }
244
245 /****************************************************************************
246  Delete a UNIX group on demand.
247 ****************************************************************************/
248
249 int smb_delete_group(const char *unix_group)
250 {
251         char *del_script = NULL;
252         int ret = -1;
253
254         /* defer to scripts */
255
256         if ( *lp_delgroup_script() ) {
257                 TALLOC_CTX *ctx = talloc_tos();
258
259                 del_script = talloc_strdup(ctx,
260                                 lp_delgroup_script());
261                 if (!del_script) {
262                         return -1;
263                 }
264                 del_script = talloc_string_sub(ctx,
265                                 del_script, "%g", unix_group);
266                 if (!del_script) {
267                         return -1;
268                 }
269                 ret = smbrun(del_script,NULL);
270                 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
271                 if (ret == 0) {
272                         smb_nscd_flush_group_cache();
273                 }
274                 return ret;
275         }
276
277         return -1;
278 }
279
280 /****************************************************************************
281  Set a user's primary UNIX group.
282 ****************************************************************************/
283
284 int smb_set_primary_group(const char *unix_group, const char* unix_user)
285 {
286         char *add_script = NULL;
287         int ret = -1;
288
289         /* defer to scripts */
290
291         if ( *lp_setprimarygroup_script() ) {
292                 TALLOC_CTX *ctx = talloc_tos();
293
294                 add_script = talloc_strdup(ctx,
295                                 lp_setprimarygroup_script());
296                 if (!add_script) {
297                         return -1;
298                 }
299                 add_script = talloc_all_string_sub(ctx,
300                                         add_script,
301                                         "%g",
302                                         unix_group);
303                 if (!add_script) {
304                         return -1;
305                 }
306                 ret = smbrun(add_script,NULL);
307                 flush_pwnam_cache();
308                 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
309                          "Running the command `%s' gave %d\n",add_script,ret));
310                 if (ret == 0) {
311                         smb_nscd_flush_group_cache();
312                 }
313                 return ret;
314         }
315
316         return -1;
317 }
318
319 /****************************************************************************
320  Add a user to a UNIX group.
321 ****************************************************************************/
322
323 int smb_add_user_group(const char *unix_group, const char *unix_user)
324 {
325         char *add_script = NULL;
326         int ret = -1;
327
328         /* defer to scripts */
329
330         if ( *lp_addusertogroup_script() ) {
331                 TALLOC_CTX *ctx = talloc_tos();
332
333                 add_script = talloc_strdup(ctx,
334                                 lp_addusertogroup_script());
335                 if (!add_script) {
336                         return -1;
337                 }
338                 add_script = talloc_string_sub(ctx,
339                                 add_script, "%g", unix_group);
340                 if (!add_script) {
341                         return -1;
342                 }
343                 add_script = talloc_string_sub(ctx,
344                                 add_script, "%u", unix_user);
345                 if (!add_script) {
346                         return -1;
347                 }
348                 ret = smbrun(add_script,NULL);
349                 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
350                 if (ret == 0) {
351                         smb_nscd_flush_group_cache();
352                 }
353                 return ret;
354         }
355
356         return -1;
357 }
358
359 /****************************************************************************
360  Delete a user from a UNIX group
361 ****************************************************************************/
362
363 int smb_delete_user_group(const char *unix_group, const char *unix_user)
364 {
365         char *del_script = NULL;
366         int ret = -1;
367
368         /* defer to scripts */
369
370         if ( *lp_deluserfromgroup_script() ) {
371                 TALLOC_CTX *ctx = talloc_tos();
372
373                 del_script = talloc_strdup(ctx,
374                                 lp_deluserfromgroup_script());
375                 if (!del_script) {
376                         return -1;
377                 }
378                 del_script = talloc_string_sub(ctx,
379                                 del_script, "%g", unix_group);
380                 if (!del_script) {
381                         return -1;
382                 }
383                 del_script = talloc_string_sub(ctx,
384                                 del_script, "%u", unix_user);
385                 if (!del_script) {
386                         return -1;
387                 }
388                 ret = smbrun(del_script,NULL);
389                 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
390                 if (ret == 0) {
391                         smb_nscd_flush_group_cache();
392                 }
393                 return ret;
394         }
395
396         return -1;
397 }
398
399
400 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
401                                  DOM_SID sid)
402 {
403         if (!init_group_mapping()) {
404                 DEBUG(0,("failed to initialize group mapping\n"));
405                 return NT_STATUS_UNSUCCESSFUL;
406         }
407         return backend->get_group_map_from_sid(sid, map) ?
408                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
409 }
410
411 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
412                                  gid_t gid)
413 {
414         if (!init_group_mapping()) {
415                 DEBUG(0,("failed to initialize group mapping\n"));
416                 return NT_STATUS_UNSUCCESSFUL;
417         }
418         return backend->get_group_map_from_gid(gid, map) ?
419                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
420 }
421
422 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
423                                  const char *name)
424 {
425         if (!init_group_mapping()) {
426                 DEBUG(0,("failed to initialize group mapping\n"));
427                 return NT_STATUS_UNSUCCESSFUL;
428         }
429         return backend->get_group_map_from_ntname(name, map) ?
430                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
431 }
432
433 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
434                                                 GROUP_MAP *map)
435 {
436         if (!init_group_mapping()) {
437                 DEBUG(0,("failed to initialize group mapping\n"));
438                 return NT_STATUS_UNSUCCESSFUL;
439         }
440         return backend->add_mapping_entry(map, TDB_INSERT) ?
441                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
442 }
443
444 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
445                                                    GROUP_MAP *map)
446 {
447         if (!init_group_mapping()) {
448                 DEBUG(0,("failed to initialize group mapping\n"));
449                 return NT_STATUS_UNSUCCESSFUL;
450         }
451         return backend->add_mapping_entry(map, TDB_REPLACE) ?
452                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
453 }
454
455 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
456                                                    DOM_SID sid)
457 {
458         if (!init_group_mapping()) {
459                 DEBUG(0,("failed to initialize group mapping\n"));
460                 return NT_STATUS_UNSUCCESSFUL;
461         }
462         return backend->group_map_remove(&sid) ?
463                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
464 }
465
466 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
467                                            const DOM_SID *sid, enum lsa_SidType sid_name_use,
468                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
469                                            bool unix_only)
470 {
471         if (!init_group_mapping()) {
472                 DEBUG(0,("failed to initialize group mapping\n"));
473                 return NT_STATUS_UNSUCCESSFUL;
474         }
475         return backend->enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
476                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
477 }
478
479 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
480                                   const char *name, uint32 *rid)
481 {
482         DOM_SID sid;
483         enum lsa_SidType type;
484         uint32 new_rid;
485         gid_t gid;
486         bool exists;
487         GROUP_MAP map;
488         TALLOC_CTX *mem_ctx;
489         NTSTATUS status;
490
491         DEBUG(10, ("Trying to create alias %s\n", name));
492
493         mem_ctx = talloc_new(NULL);
494         if (mem_ctx == NULL) {
495                 return NT_STATUS_NO_MEMORY;
496         }
497
498         exists = lookup_name(mem_ctx, name, LOOKUP_NAME_LOCAL,
499                              NULL, NULL, &sid, &type);
500         TALLOC_FREE(mem_ctx);
501
502         if (exists) {
503                 return NT_STATUS_ALIAS_EXISTS;
504         }
505
506         if (!winbind_allocate_gid(&gid)) {
507                 DEBUG(3, ("Could not get a gid out of winbind\n"));
508                 return NT_STATUS_ACCESS_DENIED;
509         }
510
511         if (!pdb_new_rid(&new_rid)) {
512                 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
513                 return NT_STATUS_ACCESS_DENIED;
514         }
515
516         DEBUG(10, ("Creating alias %s with gid %d and rid %d\n",
517                    name, gid, new_rid));
518
519         sid_copy(&sid, get_global_sam_sid());
520         sid_append_rid(&sid, new_rid);
521
522         map.gid = gid;
523         sid_copy(&map.sid, &sid);
524         map.sid_name_use = SID_NAME_ALIAS;
525         fstrcpy(map.nt_name, name);
526         fstrcpy(map.comment, "");
527
528         status = pdb_add_group_mapping_entry(&map);
529
530         if (!NT_STATUS_IS_OK(status)) {
531                 DEBUG(0, ("Could not add group mapping entry for alias %s "
532                           "(%s)\n", name, nt_errstr(status)));
533                 return status;
534         }
535
536         *rid = new_rid;
537
538         return NT_STATUS_OK;
539 }
540
541 NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods,
542                                   const DOM_SID *sid)
543 {
544         return pdb_delete_group_mapping_entry(*sid);
545 }
546
547 NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods,
548                                    const DOM_SID *sid,
549                                    struct acct_info *info)
550 {
551         GROUP_MAP map;
552
553         if (!pdb_getgrsid(&map, *sid))
554                 return NT_STATUS_NO_SUCH_ALIAS;
555
556         if ((map.sid_name_use != SID_NAME_ALIAS) &&
557             (map.sid_name_use != SID_NAME_WKN_GRP)) {
558                 DEBUG(2, ("%s is a %s, expected an alias\n",
559                           sid_string_dbg(sid),
560                           sid_type_lookup(map.sid_name_use)));
561                 return NT_STATUS_NO_SUCH_ALIAS;
562         }
563
564         fstrcpy(info->acct_name, map.nt_name);
565         fstrcpy(info->acct_desc, map.comment);
566         sid_peek_rid(&map.sid, &info->rid);
567         return NT_STATUS_OK;
568 }
569
570 NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods,
571                                    const DOM_SID *sid,
572                                    struct acct_info *info)
573 {
574         GROUP_MAP map;
575
576         if (!pdb_getgrsid(&map, *sid))
577                 return NT_STATUS_NO_SUCH_ALIAS;
578
579         fstrcpy(map.nt_name, info->acct_name);
580         fstrcpy(map.comment, info->acct_desc);
581
582         return pdb_update_group_mapping_entry(&map);
583 }
584
585 NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods,
586                                   const DOM_SID *alias, const DOM_SID *member)
587 {
588         if (!init_group_mapping()) {
589                 DEBUG(0,("failed to initialize group mapping\n"));
590                 return NT_STATUS_UNSUCCESSFUL;
591         }
592         return backend->add_aliasmem(alias, member);
593 }
594
595 NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods,
596                                   const DOM_SID *alias, const DOM_SID *member)
597 {
598         if (!init_group_mapping()) {
599                 DEBUG(0,("failed to initialize group mapping\n"));
600                 return NT_STATUS_UNSUCCESSFUL;
601         }
602         return backend->del_aliasmem(alias, member);
603 }
604
605 NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods,
606                                    const DOM_SID *alias, DOM_SID **pp_members,
607                                    size_t *p_num_members)
608 {
609         if (!init_group_mapping()) {
610                 DEBUG(0,("failed to initialize group mapping\n"));
611                 return NT_STATUS_UNSUCCESSFUL;
612         }
613         return backend->enum_aliasmem(alias, pp_members, p_num_members);
614 }
615
616 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
617                                        TALLOC_CTX *mem_ctx,
618                                        const DOM_SID *domain_sid,
619                                        const DOM_SID *members,
620                                        size_t num_members,
621                                        uint32 **pp_alias_rids,
622                                        size_t *p_num_alias_rids)
623 {
624         DOM_SID *alias_sids;
625         size_t i, num_alias_sids;
626         NTSTATUS result;
627
628         if (!init_group_mapping()) {
629                 DEBUG(0,("failed to initialize group mapping\n"));
630                 return NT_STATUS_UNSUCCESSFUL;
631         }
632
633         alias_sids = NULL;
634         num_alias_sids = 0;
635
636         result = alias_memberships(members, num_members,
637                                    &alias_sids, &num_alias_sids);
638
639         if (!NT_STATUS_IS_OK(result))
640                 return result;
641
642         *p_num_alias_rids = 0;
643
644         if (num_alias_sids == 0) {
645                 TALLOC_FREE(alias_sids);
646                 return NT_STATUS_OK;
647         }
648
649         *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
650         if (*pp_alias_rids == NULL)
651                 return NT_STATUS_NO_MEMORY;
652
653         for (i=0; i<num_alias_sids; i++) {
654                 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
655                                         &(*pp_alias_rids)[*p_num_alias_rids]))
656                         continue;
657                 *p_num_alias_rids += 1;
658         }
659
660         TALLOC_FREE(alias_sids);
661
662         return NT_STATUS_OK;
663 }
664
665 /**********************************************************************
666  no ops for passdb backends that don't implement group mapping
667  *********************************************************************/
668
669 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
670                                  DOM_SID sid)
671 {
672         return NT_STATUS_UNSUCCESSFUL;
673 }
674
675 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
676                                  gid_t gid)
677 {
678         return NT_STATUS_UNSUCCESSFUL;
679 }
680
681 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
682                                  const char *name)
683 {
684         return NT_STATUS_UNSUCCESSFUL;
685 }
686
687 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
688                                                 GROUP_MAP *map)
689 {
690         return NT_STATUS_UNSUCCESSFUL;
691 }
692
693 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
694                                                    GROUP_MAP *map)
695 {
696         return NT_STATUS_UNSUCCESSFUL;
697 }
698
699 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
700                                                    DOM_SID sid)
701 {
702         return NT_STATUS_UNSUCCESSFUL;
703 }
704
705 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
706                                            enum lsa_SidType sid_name_use,
707                                            GROUP_MAP **rmap, size_t *num_entries,
708                                            bool unix_only)
709 {
710         return NT_STATUS_UNSUCCESSFUL;
711 }
712
713 /****************************************************************************
714  These need to be redirected through pdb_interface.c
715 ****************************************************************************/
716 bool pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
717 {
718         GROUP_MAP map;
719         bool res;
720
721         become_root();
722         res = get_domain_group_from_sid(*sid, &map);
723         unbecome_root();
724
725         if (!res)
726                 return False;
727
728         fstrcpy(info->acct_name, map.nt_name);
729         fstrcpy(info->acct_desc, map.comment);
730         sid_peek_rid(sid, &info->rid);
731         return True;
732 }
733
734 bool pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
735 {
736         GROUP_MAP map;
737
738         if (!get_domain_group_from_sid(*sid, &map))
739                 return False;
740
741         fstrcpy(map.nt_name, info->acct_name);
742         fstrcpy(map.comment, info->acct_desc);
743
744         return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
745 }
746
747 /********************************************************************
748  Really just intended to be called by smbd
749 ********************************************************************/
750
751 NTSTATUS pdb_create_builtin_alias(uint32 rid)
752 {
753         DOM_SID sid;
754         enum lsa_SidType type;
755         gid_t gid;
756         GROUP_MAP map;
757         TALLOC_CTX *mem_ctx;
758         NTSTATUS status;
759         const char *name = NULL;
760         fstring groupname;
761
762         DEBUG(10, ("Trying to create builtin alias %d\n", rid));
763         
764         if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
765                 return NT_STATUS_NO_SUCH_ALIAS;
766         }
767         
768         if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
769                 return NT_STATUS_NO_MEMORY;
770         }
771         
772         if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
773                 TALLOC_FREE( mem_ctx );
774                 return NT_STATUS_NO_SUCH_ALIAS;
775         }
776         
777         /* validate RID so copy the name and move on */
778                 
779         fstrcpy( groupname, name );
780         TALLOC_FREE( mem_ctx );
781
782         if (!winbind_allocate_gid(&gid)) {
783                 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
784                 return NT_STATUS_ACCESS_DENIED;
785         }
786
787         DEBUG(10,("Creating alias %s with gid %d\n", groupname, gid));
788
789         map.gid = gid;
790         sid_copy(&map.sid, &sid);
791         map.sid_name_use = SID_NAME_ALIAS;
792         fstrcpy(map.nt_name, groupname);
793         fstrcpy(map.comment, "");
794
795         status = pdb_add_group_mapping_entry(&map);
796
797         if (!NT_STATUS_IS_OK(status)) {
798                 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
799                           "(%s)\n", rid, nt_errstr(status)));
800         }
801
802         return status;
803 }
804
805