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