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