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