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