s3-rpc_misc: clean out include/rpc_misc.h.
[nivanova/samba-autobuild/.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_tdb_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_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                         return True;
136                 }
137                 return False;
138         }
139
140         DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n"));
141
142         /* if it's not a domain group, continue */
143         if (map->sid_name_use!=SID_NAME_DOM_GRP) {
144                 return False;
145         }
146
147         DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n"));
148
149         if (map->gid==-1) {
150                 return False;
151         }
152
153         DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid));
154
155         grp = getgrgid(map->gid);
156         if ( !grp ) {
157                 DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n"));
158                 return False;
159         }
160
161         DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n"));
162
163         return True;
164 }
165
166 /****************************************************************************
167  Create a UNIX group on demand.
168 ****************************************************************************/
169
170 int smb_create_group(const char *unix_group, gid_t *new_gid)
171 {
172         char *add_script = NULL;
173         int     ret = -1;
174         int     fd = 0;
175
176         *new_gid = 0;
177
178         /* defer to scripts */
179
180         if ( *lp_addgroup_script() ) {
181                 TALLOC_CTX *ctx = talloc_tos();
182
183                 add_script = talloc_strdup(ctx,
184                                         lp_addgroup_script());
185                 if (!add_script) {
186                         return -1;
187                 }
188                 add_script = talloc_string_sub(ctx,
189                                 add_script, "%g", unix_group);
190                 if (!add_script) {
191                         return -1;
192                 }
193
194                 ret = smbrun(add_script, &fd);
195                 DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
196                 if (ret == 0) {
197                         smb_nscd_flush_group_cache();
198                 }
199                 if (ret != 0)
200                         return ret;
201
202                 if (fd != 0) {
203                         fstring output;
204
205                         *new_gid = 0;
206                         if (read(fd, output, sizeof(output)) > 0) {
207                                 *new_gid = (gid_t)strtoul(output, NULL, 10);
208                         }
209
210                         close(fd);
211                 }
212
213         }
214
215         if (*new_gid == 0) {
216                 struct group *grp = getgrnam(unix_group);
217
218                 if (grp != NULL)
219                         *new_gid = grp->gr_gid;
220         }
221
222         return ret;
223 }
224
225 /****************************************************************************
226  Delete a UNIX group on demand.
227 ****************************************************************************/
228
229 int smb_delete_group(const char *unix_group)
230 {
231         char *del_script = NULL;
232         int ret = -1;
233
234         /* defer to scripts */
235
236         if ( *lp_delgroup_script() ) {
237                 TALLOC_CTX *ctx = talloc_tos();
238
239                 del_script = talloc_strdup(ctx,
240                                 lp_delgroup_script());
241                 if (!del_script) {
242                         return -1;
243                 }
244                 del_script = talloc_string_sub(ctx,
245                                 del_script, "%g", unix_group);
246                 if (!del_script) {
247                         return -1;
248                 }
249                 ret = smbrun(del_script,NULL);
250                 DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
251                 if (ret == 0) {
252                         smb_nscd_flush_group_cache();
253                 }
254                 return ret;
255         }
256
257         return -1;
258 }
259
260 /****************************************************************************
261  Set a user's primary UNIX group.
262 ****************************************************************************/
263
264 int smb_set_primary_group(const char *unix_group, const char* unix_user)
265 {
266         char *add_script = NULL;
267         int ret = -1;
268
269         /* defer to scripts */
270
271         if ( *lp_setprimarygroup_script() ) {
272                 TALLOC_CTX *ctx = talloc_tos();
273
274                 add_script = talloc_strdup(ctx,
275                                 lp_setprimarygroup_script());
276                 if (!add_script) {
277                         return -1;
278                 }
279                 add_script = talloc_all_string_sub(ctx,
280                                 add_script, "%g", unix_group);
281                 if (!add_script) {
282                         return -1;
283                 }
284                 add_script = talloc_string_sub(ctx,
285                                 add_script, "%u", unix_user);
286                 if (!add_script) {
287                         return -1;
288                 }
289                 ret = smbrun(add_script,NULL);
290                 flush_pwnam_cache();
291                 DEBUG(ret ? 0 : 3,("smb_set_primary_group: "
292                          "Running the command `%s' gave %d\n",add_script,ret));
293                 if (ret == 0) {
294                         smb_nscd_flush_group_cache();
295                 }
296                 return ret;
297         }
298
299         return -1;
300 }
301
302 /****************************************************************************
303  Add a user to a UNIX group.
304 ****************************************************************************/
305
306 int smb_add_user_group(const char *unix_group, const char *unix_user)
307 {
308         char *add_script = NULL;
309         int ret = -1;
310
311         /* defer to scripts */
312
313         if ( *lp_addusertogroup_script() ) {
314                 TALLOC_CTX *ctx = talloc_tos();
315
316                 add_script = talloc_strdup(ctx,
317                                 lp_addusertogroup_script());
318                 if (!add_script) {
319                         return -1;
320                 }
321                 add_script = talloc_string_sub(ctx,
322                                 add_script, "%g", unix_group);
323                 if (!add_script) {
324                         return -1;
325                 }
326                 add_script = talloc_string_sub(ctx,
327                                 add_script, "%u", unix_user);
328                 if (!add_script) {
329                         return -1;
330                 }
331                 ret = smbrun(add_script,NULL);
332                 DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
333                 if (ret == 0) {
334                         smb_nscd_flush_group_cache();
335                 }
336                 return ret;
337         }
338
339         return -1;
340 }
341
342 /****************************************************************************
343  Delete a user from a UNIX group
344 ****************************************************************************/
345
346 int smb_delete_user_group(const char *unix_group, const char *unix_user)
347 {
348         char *del_script = NULL;
349         int ret = -1;
350
351         /* defer to scripts */
352
353         if ( *lp_deluserfromgroup_script() ) {
354                 TALLOC_CTX *ctx = talloc_tos();
355
356                 del_script = talloc_strdup(ctx,
357                                 lp_deluserfromgroup_script());
358                 if (!del_script) {
359                         return -1;
360                 }
361                 del_script = talloc_string_sub(ctx,
362                                 del_script, "%g", unix_group);
363                 if (!del_script) {
364                         return -1;
365                 }
366                 del_script = talloc_string_sub(ctx,
367                                 del_script, "%u", unix_user);
368                 if (!del_script) {
369                         return -1;
370                 }
371                 ret = smbrun(del_script,NULL);
372                 DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));
373                 if (ret == 0) {
374                         smb_nscd_flush_group_cache();
375                 }
376                 return ret;
377         }
378
379         return -1;
380 }
381
382
383 NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
384                                  DOM_SID sid)
385 {
386         if (!init_group_mapping()) {
387                 DEBUG(0,("failed to initialize group mapping\n"));
388                 return NT_STATUS_UNSUCCESSFUL;
389         }
390         return backend->get_group_map_from_sid(sid, map) ?
391                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
392 }
393
394 NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
395                                  gid_t gid)
396 {
397         if (!init_group_mapping()) {
398                 DEBUG(0,("failed to initialize group mapping\n"));
399                 return NT_STATUS_UNSUCCESSFUL;
400         }
401         return backend->get_group_map_from_gid(gid, map) ?
402                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
403 }
404
405 NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
406                                  const char *name)
407 {
408         if (!init_group_mapping()) {
409                 DEBUG(0,("failed to initialize group mapping\n"));
410                 return NT_STATUS_UNSUCCESSFUL;
411         }
412         return backend->get_group_map_from_ntname(name, map) ?
413                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
414 }
415
416 NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods,
417                                                 GROUP_MAP *map)
418 {
419         if (!init_group_mapping()) {
420                 DEBUG(0,("failed to initialize group mapping\n"));
421                 return NT_STATUS_UNSUCCESSFUL;
422         }
423         return backend->add_mapping_entry(map, TDB_INSERT) ?
424                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
425 }
426
427 NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods,
428                                                    GROUP_MAP *map)
429 {
430         if (!init_group_mapping()) {
431                 DEBUG(0,("failed to initialize group mapping\n"));
432                 return NT_STATUS_UNSUCCESSFUL;
433         }
434         return backend->add_mapping_entry(map, TDB_REPLACE) ?
435                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
436 }
437
438 NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods,
439                                                    DOM_SID sid)
440 {
441         if (!init_group_mapping()) {
442                 DEBUG(0,("failed to initialize group mapping\n"));
443                 return NT_STATUS_UNSUCCESSFUL;
444         }
445         return backend->group_map_remove(&sid) ?
446                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
447 }
448
449 NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods,
450                                            const DOM_SID *sid, enum lsa_SidType sid_name_use,
451                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
452                                            bool unix_only)
453 {
454         if (!init_group_mapping()) {
455                 DEBUG(0,("failed to initialize group mapping\n"));
456                 return NT_STATUS_UNSUCCESSFUL;
457         }
458         return backend->enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ?
459                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
460 }
461
462 NTSTATUS pdb_default_create_alias(struct pdb_methods *methods,
463                                   const char *name, uint32 *rid)
464 {
465         DOM_SID sid;
466         enum lsa_SidType type;
467         uint32 new_rid;
468         gid_t gid;
469         bool exists;
470         GROUP_MAP map;
471         TALLOC_CTX *mem_ctx;
472         NTSTATUS status;
473
474         DEBUG(10, ("Trying to create alias %s\n", name));
475
476         mem_ctx = talloc_new(NULL);
477         if (mem_ctx == NULL) {
478                 return NT_STATUS_NO_MEMORY;
479         }
480
481         exists = lookup_name(mem_ctx, name, LOOKUP_NAME_LOCAL,
482                              NULL, NULL, &sid, &type);
483         TALLOC_FREE(mem_ctx);
484
485         if (exists) {
486                 return NT_STATUS_ALIAS_EXISTS;
487         }
488
489         if (!winbind_allocate_gid(&gid)) {
490                 DEBUG(3, ("Could not get a gid out of winbind\n"));
491                 return NT_STATUS_ACCESS_DENIED;
492         }
493
494         if (!pdb_new_rid(&new_rid)) {
495                 DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n"));
496                 return NT_STATUS_ACCESS_DENIED;
497         }
498
499         DEBUG(10, ("Creating alias %s with gid %u and rid %u\n",
500                    name, (unsigned int)gid, (unsigned int)new_rid));
501
502         sid_compose(&sid, get_global_sam_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, TALLOC_CTX *mem_ctx,
589                                    DOM_SID **pp_members, 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, mem_ctx, pp_members,
596                                       p_num_members);
597 }
598
599 NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods,
600                                        TALLOC_CTX *mem_ctx,
601                                        const DOM_SID *domain_sid,
602                                        const DOM_SID *members,
603                                        size_t num_members,
604                                        uint32 **pp_alias_rids,
605                                        size_t *p_num_alias_rids)
606 {
607         DOM_SID *alias_sids;
608         size_t i, num_alias_sids;
609         NTSTATUS result;
610
611         if (!init_group_mapping()) {
612                 DEBUG(0,("failed to initialize group mapping\n"));
613                 return NT_STATUS_UNSUCCESSFUL;
614         }
615
616         alias_sids = NULL;
617         num_alias_sids = 0;
618
619         result = alias_memberships(members, num_members,
620                                    &alias_sids, &num_alias_sids);
621
622         if (!NT_STATUS_IS_OK(result))
623                 return result;
624
625         *p_num_alias_rids = 0;
626
627         if (num_alias_sids == 0) {
628                 TALLOC_FREE(alias_sids);
629                 return NT_STATUS_OK;
630         }
631
632         *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids);
633         if (*pp_alias_rids == NULL)
634                 return NT_STATUS_NO_MEMORY;
635
636         for (i=0; i<num_alias_sids; i++) {
637                 if (!sid_peek_check_rid(domain_sid, &alias_sids[i],
638                                         &(*pp_alias_rids)[*p_num_alias_rids]))
639                         continue;
640                 *p_num_alias_rids += 1;
641         }
642
643         TALLOC_FREE(alias_sids);
644
645         return NT_STATUS_OK;
646 }
647
648 /**********************************************************************
649  no ops for passdb backends that don't implement group mapping
650  *********************************************************************/
651
652 NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
653                                  DOM_SID sid)
654 {
655         return NT_STATUS_UNSUCCESSFUL;
656 }
657
658 NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map,
659                                  gid_t gid)
660 {
661         return NT_STATUS_UNSUCCESSFUL;
662 }
663
664 NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
665                                  const char *name)
666 {
667         return NT_STATUS_UNSUCCESSFUL;
668 }
669
670 NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods,
671                                                 GROUP_MAP *map)
672 {
673         return NT_STATUS_UNSUCCESSFUL;
674 }
675
676 NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods,
677                                                    GROUP_MAP *map)
678 {
679         return NT_STATUS_UNSUCCESSFUL;
680 }
681
682 NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods,
683                                                    DOM_SID sid)
684 {
685         return NT_STATUS_UNSUCCESSFUL;
686 }
687
688 NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods,
689                                            enum lsa_SidType sid_name_use,
690                                            GROUP_MAP **rmap, size_t *num_entries,
691                                            bool unix_only)
692 {
693         return NT_STATUS_UNSUCCESSFUL;
694 }
695
696 /****************************************************************************
697  These need to be redirected through pdb_interface.c
698 ****************************************************************************/
699 bool pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info)
700 {
701         GROUP_MAP map;
702         bool res;
703
704         become_root();
705         res = get_domain_group_from_sid(*sid, &map);
706         unbecome_root();
707
708         if (!res)
709                 return False;
710
711         fstrcpy(info->acct_name, map.nt_name);
712         fstrcpy(info->acct_desc, map.comment);
713         sid_peek_rid(sid, &info->rid);
714         return True;
715 }
716
717 bool pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info)
718 {
719         GROUP_MAP map;
720
721         if (!get_domain_group_from_sid(*sid, &map))
722                 return False;
723
724         fstrcpy(map.nt_name, info->acct_name);
725         fstrcpy(map.comment, info->acct_desc);
726
727         return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map));
728 }
729
730 /********************************************************************
731  Really just intended to be called by smbd
732 ********************************************************************/
733
734 NTSTATUS pdb_create_builtin_alias(uint32 rid)
735 {
736         DOM_SID sid;
737         enum lsa_SidType type;
738         gid_t gid;
739         GROUP_MAP map;
740         TALLOC_CTX *mem_ctx;
741         NTSTATUS status;
742         const char *name = NULL;
743         fstring groupname;
744
745         DEBUG(10, ("Trying to create builtin alias %d\n", rid));
746
747         if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) {
748                 return NT_STATUS_NO_SUCH_ALIAS;
749         }
750
751         if ( (mem_ctx = talloc_new(NULL)) == NULL ) {
752                 return NT_STATUS_NO_MEMORY;
753         }
754
755         if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) {
756                 TALLOC_FREE( mem_ctx );
757                 return NT_STATUS_NO_SUCH_ALIAS;
758         }
759
760         /* validate RID so copy the name and move on */
761
762         fstrcpy( groupname, name );
763         TALLOC_FREE( mem_ctx );
764
765         if (!winbind_allocate_gid(&gid)) {
766                 DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n"));
767                 return NT_STATUS_ACCESS_DENIED;
768         }
769
770         DEBUG(10,("Creating alias %s with gid %u\n", groupname, (unsigned int)gid));
771
772         map.gid = gid;
773         sid_copy(&map.sid, &sid);
774         map.sid_name_use = SID_NAME_ALIAS;
775         fstrcpy(map.nt_name, groupname);
776         fstrcpy(map.comment, "");
777
778         status = pdb_add_group_mapping_entry(&map);
779
780         if (!NT_STATUS_IS_OK(status)) {
781                 DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d "
782                           "(%s)\n", rid, nt_errstr(status)));
783         }
784
785         return status;
786 }
787
788