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