r13494: Merge the stuff I've done in head the last days.
[vlendec/samba-autobuild/.git] / source3 / passdb / pdb_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett                        2002
5    Copyright (C) Jelmer Vernooij                        2002
6    Copyright (C) Simo Sorce                             2003
7    Copyright (C) Volker Lendecke                        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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_PASSDB
28
29 /* Cache of latest SAM lookup query */
30
31 static SAM_ACCOUNT *csamuser = NULL;
32
33 static struct pdb_init_function_entry *backends = NULL;
34
35 static void lazy_initialize_passdb(void)
36 {
37         static BOOL initialized = False;
38         if(initialized)return;
39         static_init_pdb;
40         initialized = True;
41 }
42
43 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
44 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
45                                   const char **name,
46                                   enum SID_NAME_USE *psid_name_use,
47                                   union unid_t *unix_id);
48 /*******************************************************************
49  Clean up uninitialised passwords.  The only way to tell 
50  that these values are not 'real' is that they do not
51  have a valid last set time.  Instead, the value is fixed at 0. 
52  Therefore we use that as the key for 'is this a valid password'.
53  However, it is perfectly valid to have a 'default' last change
54  time, such LDAP with a missing attribute would produce.
55 ********************************************************************/
56
57 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass) 
58 {
59         const uint8 *lm_pwd, *nt_pwd;
60         
61         /* only reset a password if the last set time has been 
62            explicitly been set to zero.  A default last set time 
63            is ignored */
64
65         if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT) 
66                 && (pdb_get_pass_last_set_time(pass) == 0) ) 
67         {
68                 
69                 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT) 
70                 {
71                         lm_pwd = pdb_get_lanman_passwd(pass);
72                         if (lm_pwd) 
73                                 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
74                 }
75                 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT) 
76                 {
77                         nt_pwd = pdb_get_nt_passwd(pass);
78                         if (nt_pwd) 
79                                 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
80                 }
81         }
82
83         return;
84 }
85
86 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
87 {
88         struct pdb_init_function_entry *entry = backends;
89
90         if(version != PASSDB_INTERFACE_VERSION) {
91                 DEBUG(0,("Can't register passdb backend!\n"
92                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
93                          "while this version of samba uses version %d\n", 
94                          version,PASSDB_INTERFACE_VERSION));
95                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
96         }
97
98         if (!name || !init) {
99                 return NT_STATUS_INVALID_PARAMETER;
100         }
101
102         DEBUG(5,("Attempting to register passdb backend %s\n", name));
103
104         /* Check for duplicates */
105         if (pdb_find_backend_entry(name)) {
106                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
107                 return NT_STATUS_OBJECT_NAME_COLLISION;
108         }
109
110         entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
111         entry->name = smb_xstrdup(name);
112         entry->init = init;
113
114         DLIST_ADD(backends, entry);
115         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
116         return NT_STATUS_OK;
117 }
118
119 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
120 {
121         struct pdb_init_function_entry *entry = backends;
122
123         while(entry) {
124                 if (strcmp(entry->name, name)==0) return entry;
125                 entry = entry->next;
126         }
127
128         return NULL;
129 }
130
131 /******************************************************************
132   Make a pdb_methods from scratch
133  *******************************************************************/
134
135 NTSTATUS make_pdb_method_name(struct pdb_methods **methods, const char *selected)
136 {
137         char *module_name = smb_xstrdup(selected);
138         char *module_location = NULL, *p;
139         struct pdb_init_function_entry *entry;
140         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
141
142         lazy_initialize_passdb();
143
144         p = strchr(module_name, ':');
145
146         if (p) {
147                 *p = 0;
148                 module_location = p+1;
149                 trim_char(module_location, ' ', ' ');
150         }
151
152         trim_char(module_name, ' ', ' ');
153
154
155         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
156
157         entry = pdb_find_backend_entry(module_name);
158         
159         /* Try to find a module that contains this module */
160         if (!entry) { 
161                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
162                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
163                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
164                         SAFE_FREE(module_name);
165                         return NT_STATUS_UNSUCCESSFUL;
166                 }
167         }
168         
169         /* No such backend found */
170         if(!entry) { 
171                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
172                 SAFE_FREE(module_name);
173                 return NT_STATUS_INVALID_PARAMETER;
174         }
175
176         DEBUG(5,("Found pdb backend %s\n", module_name));
177
178         if ( !NT_STATUS_IS_OK( nt_status = entry->init(methods, module_location) ) ) {
179                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", 
180                         selected, nt_errstr(nt_status)));
181                 SAFE_FREE(module_name);
182                 return nt_status;
183         }
184
185         SAFE_FREE(module_name);
186
187         DEBUG(5,("pdb backend %s has a valid init\n", selected));
188
189         return nt_status;
190 }
191
192 /******************************************************************
193  Return an already initialised pdn_methods structure
194 *******************************************************************/
195
196 static struct pdb_methods *pdb_get_methods( BOOL reload ) 
197 {
198         static struct pdb_methods *pdb = NULL;
199
200         if ( pdb && reload ) {
201                 pdb->free_private_data( &(pdb->private_data) );
202                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
203                         return NULL;
204                 }
205         }
206
207         if ( !pdb ) {
208                 if ( !NT_STATUS_IS_OK( make_pdb_method_name( &pdb, lp_passdb_backend() ) ) ) {
209                         return NULL;
210                 }
211         }
212
213         return pdb;
214 }
215
216 /******************************************************************
217  Backward compatibility functions for the original passdb interface
218 *******************************************************************/
219
220 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask) 
221 {
222         struct pdb_methods *pdb = pdb_get_methods(False);
223
224         if ( !pdb ) {
225                 return False;
226         }
227
228         return NT_STATUS_IS_OK(pdb->setsampwent(pdb, update, acb_mask));
229 }
230
231 void pdb_endsampwent(void) 
232 {
233         struct pdb_methods *pdb = pdb_get_methods(False);
234
235         if ( !pdb ) {
236                 return;
237         }
238
239         pdb->endsampwent(pdb);
240 }
241
242 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
243 {
244         struct pdb_methods *pdb = pdb_get_methods(False);
245
246         if ( !pdb ) {
247                 return False;
248         }
249
250         if ( !NT_STATUS_IS_OK(pdb->getsampwent(pdb, user) ) ) {
251                 return False;
252         }
253
254         pdb_force_pw_initialization( user );
255
256         return True;
257 }
258
259 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
260 {
261         struct pdb_methods *pdb = pdb_get_methods(False);
262
263         if ( !pdb ) {
264                 return False;
265         }
266
267         if (!NT_STATUS_IS_OK(pdb->getsampwnam(pdb, sam_acct, username))) {
268                 return False;
269         }
270
271         if (csamuser != NULL) {
272                 pdb_free_sam(&csamuser);
273                 csamuser = NULL;
274         }
275
276         pdb_force_pw_initialization( sam_acct );
277         pdb_copy_sam_account(sam_acct, &csamuser);
278
279         return True;
280 }
281
282 /**********************************************************************
283 **********************************************************************/
284
285 BOOL guest_user_info( SAM_ACCOUNT *user )
286 {
287         struct passwd *pwd;
288         NTSTATUS ntstatus;
289         const char *guestname = lp_guestaccount();
290         
291         if ( !(pwd = getpwnam_alloc( NULL, guestname ) ) ) {
292                 DEBUG(0,("guest_user_info: Unable to locate guest account [%s]!\n", 
293                         guestname));
294                 return False;
295         }
296         
297         /* fill in from the users information */
298         
299         ntstatus = pdb_fill_sam_pw( user, pwd );
300         
301         return NT_STATUS_IS_OK(ntstatus);
302
303 }
304
305 /**********************************************************************
306 **********************************************************************/
307
308 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
309 {
310         struct pdb_methods *pdb;
311         uint32 rid;
312
313         if ( !(pdb = pdb_get_methods(False)) ) {
314                 return False;
315         }
316
317         /* hard code the Guest RID of 501 */
318
319         if ( !sid_peek_check_rid( get_global_sam_sid(), sid, &rid ) )
320                 return False;
321
322         if ( rid == DOMAIN_USER_RID_GUEST ) {
323                 DEBUG(6,("pdb_getsampwsid: Building guest account\n"));
324                 return guest_user_info( sam_acct );
325         }
326         
327         /* check the cache first */
328         
329         if ( csamuser && sid_equal(sid, pdb_get_user_sid(csamuser) ) )
330                 return pdb_copy_sam_account(csamuser, &sam_acct);
331
332         return NT_STATUS_IS_OK(pdb->getsampwsid(pdb, sam_acct, sid));
333 }
334
335 static NTSTATUS pdb_default_create_user(struct pdb_methods *methods,
336                                         TALLOC_CTX *tmp_ctx, const char *name,
337                                         uint32 acb_info, uint32 *rid)
338 {
339         SAM_ACCOUNT *sam_pass = NULL;
340         NTSTATUS status;
341
342         if (Get_Pwnam_alloc(tmp_ctx, name) == NULL) {
343                 pstring add_script;
344                 int add_ret;
345
346                 if ((acb_info & ACB_NORMAL) && name[strlen(name)-1] != '$') {
347                         pstrcpy(add_script, lp_adduser_script());
348                 } else {
349                         pstrcpy(add_script, lp_addmachine_script());
350                 }
351
352                 if (add_script[0] == '\0') {
353                         DEBUG(3, ("Could not find user %s and no add script "
354                                   "defined\n", name));
355                         return NT_STATUS_NO_SUCH_USER;
356                 }
357
358                 all_string_sub(add_script, "%u", name, sizeof(add_script));
359                 add_ret = smbrun(add_script,NULL);
360                 DEBUG(add_ret ? 0 : 3, ("_samr_create_user: Running the "
361                                         "command `%s' gave %d\n",
362                                         add_script, add_ret));
363         }
364
365         /* implicit call to getpwnam() next.  we have a valid SID coming out
366          * of this call */
367
368         flush_pwnam_cache();
369         status = pdb_init_sam_new(&sam_pass, name);
370
371         if (!NT_STATUS_IS_OK(status)) {
372                 DEBUG(3, ("pdb_init_sam_new failed: %s\n", nt_errstr(status)));
373                 return status;
374         }
375
376         if (!sid_peek_check_rid(get_global_sam_sid(),
377                                 pdb_get_user_sid(sam_pass), rid)) {
378                 DEBUG(0, ("Could not get RID of fresh user\n"));
379                 return NT_STATUS_INTERNAL_ERROR;
380         }
381
382         /* Disable the account on creation, it does not have a reasonable
383          * password yet. */
384
385         acb_info |= ACB_DISABLED;
386
387         pdb_set_acct_ctrl(sam_pass, acb_info, PDB_CHANGED);
388
389         status = pdb_add_sam_account(sam_pass);
390
391         pdb_free_sam(&sam_pass);
392
393         return status;
394 }
395
396 NTSTATUS pdb_create_user(TALLOC_CTX *mem_ctx, const char *name, uint32 flags,
397                          uint32 *rid)
398 {
399         struct pdb_methods *pdb = pdb_get_methods(False);
400
401         if ( !pdb ) {
402                 return NT_STATUS_UNSUCCESSFUL;
403         }
404
405         return pdb->create_user(pdb, mem_ctx, name, flags, rid);
406 }
407
408 /****************************************************************************
409  Delete a UNIX user on demand.
410 ****************************************************************************/
411
412 static int smb_delete_user(const char *unix_user)
413 {
414         pstring del_script;
415         int ret;
416
417         pstrcpy(del_script, lp_deluser_script());
418         if (! *del_script)
419                 return -1;
420         all_string_sub(del_script, "%u", unix_user, sizeof(del_script));
421         ret = smbrun(del_script,NULL);
422         flush_pwnam_cache();
423         DEBUG(ret ? 0 : 3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
424
425         return ret;
426 }
427
428 static NTSTATUS pdb_default_delete_user(struct pdb_methods *methods,
429                                         TALLOC_CTX *mem_ctx,
430                                         SAM_ACCOUNT *sam_acct)
431 {
432         NTSTATUS status;
433
434         status = pdb_delete_sam_account(sam_acct);
435         if (!NT_STATUS_IS_OK(status)) {
436                 return status;
437         }
438
439         /*
440          * Now delete the unix side ....
441          * note: we don't check if the delete really happened as the script is
442          * not necessary present and maybe the sysadmin doesn't want to delete
443          * the unix side
444          */
445         smb_delete_user( pdb_get_username(sam_acct) );
446         
447         return status;
448 }
449
450 NTSTATUS pdb_delete_user(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *sam_acct)
451 {
452         struct pdb_methods *pdb = pdb_get_methods(False);
453
454         if ( !pdb ) {
455                 return NT_STATUS_UNSUCCESSFUL;
456         }
457
458         return pdb->delete_user(pdb, mem_ctx, sam_acct);
459 }
460
461 NTSTATUS pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
462 {
463         struct pdb_methods *pdb = pdb_get_methods(False);
464
465         if ( !pdb ) {
466                 return NT_STATUS_UNSUCCESSFUL;
467         }
468         
469         return pdb->add_sam_account(pdb, sam_acct);
470 }
471
472 NTSTATUS pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
473 {
474         struct pdb_methods *pdb = pdb_get_methods(False);
475
476         if ( !pdb ) {
477                 return NT_STATUS_UNSUCCESSFUL;
478         }
479
480         if (csamuser != NULL) {
481                 pdb_free_sam(&csamuser);
482                 csamuser = NULL;
483         }
484
485         return pdb->update_sam_account(pdb, sam_acct);
486 }
487
488 NTSTATUS pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
489 {
490         struct pdb_methods *pdb = pdb_get_methods(False);
491
492         if ( !pdb ) {
493                 return NT_STATUS_UNSUCCESSFUL;
494         }
495
496         if (csamuser != NULL) {
497                 pdb_free_sam(&csamuser);
498                 csamuser = NULL;
499         }
500
501         return pdb->delete_sam_account(pdb, sam_acct);
502 }
503
504 NTSTATUS pdb_rename_sam_account(SAM_ACCOUNT *oldname, const char *newname)
505 {
506         struct pdb_methods *pdb = pdb_get_methods(False);
507
508         if ( !pdb ) {
509                 return NT_STATUS_NOT_IMPLEMENTED;
510         }
511
512         if (csamuser != NULL) {
513                 pdb_free_sam(&csamuser);
514                 csamuser = NULL;
515         }
516
517         return pdb->rename_sam_account(pdb, oldname, newname);
518 }
519
520 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
521 {
522         struct pdb_methods *pdb = pdb_get_methods(False);
523
524         if ( !pdb ) {
525                 return NT_STATUS_NOT_IMPLEMENTED;
526         }
527
528         return pdb->update_login_attempts(pdb, sam_acct, success);
529 }
530
531 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
532 {
533         struct pdb_methods *pdb = pdb_get_methods(False);
534
535         if ( !pdb ) {
536                 return False;
537         }
538
539         return NT_STATUS_IS_OK(pdb->getgrsid(pdb, map, sid));
540 }
541
542 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
543 {
544         struct pdb_methods *pdb = pdb_get_methods(False);
545
546         if ( !pdb ) {
547                 return False;
548         }
549
550         return NT_STATUS_IS_OK(pdb->getgrgid(pdb, map, gid));
551 }
552
553 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
554 {
555         struct pdb_methods *pdb = pdb_get_methods(False);
556
557         if ( !pdb ) {
558                 return False;
559         }
560
561         return NT_STATUS_IS_OK(pdb->getgrnam(pdb, map, name));
562 }
563
564 static NTSTATUS pdb_default_create_dom_group(struct pdb_methods *methods,
565                                              TALLOC_CTX *mem_ctx,
566                                              const char *name,
567                                              uint32 *rid)
568 {
569         DOM_SID group_sid;
570         struct group *grp;
571
572         grp = getgrnam(name);
573
574         if (grp == NULL) {
575                 gid_t gid;
576
577                 if (smb_create_group(name, &gid) != 0) {
578                         return NT_STATUS_ACCESS_DENIED;
579                 }
580
581                 grp = getgrgid(gid);
582         }
583
584         if (grp == NULL) {
585                 return NT_STATUS_ACCESS_DENIED;
586         }
587
588         if (pdb_rid_algorithm()) {
589                 *rid = pdb_gid_to_group_rid( grp->gr_gid );
590         } else {
591                 if (!pdb_new_rid(rid)) {
592                         return NT_STATUS_ACCESS_DENIED;
593                 }
594         }
595
596         sid_compose(&group_sid, get_global_sam_sid(), *rid);
597                 
598         return add_initial_entry(grp->gr_gid, sid_string_static(&group_sid),
599                                  SID_NAME_DOM_GRP, name, NULL);
600 }
601
602 NTSTATUS pdb_create_dom_group(TALLOC_CTX *mem_ctx, const char *name,
603                               uint32 *rid)
604 {
605         struct pdb_methods *pdb = pdb_get_methods(False);
606
607         if ( !pdb ) {
608                 return NT_STATUS_UNSUCCESSFUL;
609         }
610
611         return pdb->create_dom_group(pdb, mem_ctx, name, rid);
612 }
613
614 static NTSTATUS pdb_default_delete_dom_group(struct pdb_methods *methods,
615                                              TALLOC_CTX *mem_ctx,
616                                              uint32 rid)
617 {
618         DOM_SID group_sid;
619         GROUP_MAP map;
620         NTSTATUS status;
621         struct group *grp;
622         const char *grp_name;
623
624         sid_compose(&group_sid, get_global_sam_sid(), rid);
625
626         if (!get_domain_group_from_sid(group_sid, &map)) {
627                 DEBUG(10, ("Could not find group for rid %d\n", rid));
628                 return NT_STATUS_NO_SUCH_GROUP;
629         }
630
631         /* We need the group name for the smb_delete_group later on */
632
633         if (map.gid == (gid_t)-1) {
634                 return NT_STATUS_NO_SUCH_GROUP;
635         }
636
637         grp = getgrgid(map.gid);
638         if (grp == NULL) {
639                 return NT_STATUS_NO_SUCH_GROUP;
640         }
641
642         /* Copy the name, no idea what pdb_delete_group_mapping_entry does.. */
643
644         grp_name = talloc_strdup(mem_ctx, grp->gr_name);
645         if (grp_name == NULL) {
646                 return NT_STATUS_NO_MEMORY;
647         }
648
649         status = pdb_delete_group_mapping_entry(group_sid);
650
651         if (!NT_STATUS_IS_OK(status)) {
652                 return status;
653         }
654
655         /* Don't check the result of smb_delete_group */
656         
657         smb_delete_group(grp_name);
658
659         return NT_STATUS_OK;
660 }
661
662 NTSTATUS pdb_delete_dom_group(TALLOC_CTX *mem_ctx, uint32 rid)
663 {
664         struct pdb_methods *pdb = pdb_get_methods(False);
665
666         if ( !pdb ) {
667                 return NT_STATUS_UNSUCCESSFUL;
668         }
669
670         return pdb->delete_dom_group(pdb, mem_ctx, rid);
671 }
672
673 NTSTATUS pdb_add_group_mapping_entry(GROUP_MAP *map)
674 {
675         struct pdb_methods *pdb = pdb_get_methods(False);
676
677         if ( !pdb ) {
678                 return NT_STATUS_UNSUCCESSFUL;
679         }
680
681         return pdb->add_group_mapping_entry(pdb, map);
682 }
683
684 NTSTATUS pdb_update_group_mapping_entry(GROUP_MAP *map)
685 {
686         struct pdb_methods *pdb = pdb_get_methods(False);
687
688         if ( !pdb ) {
689                 return NT_STATUS_UNSUCCESSFUL;
690         }
691
692         return pdb->update_group_mapping_entry(pdb, map);
693 }
694
695 NTSTATUS pdb_delete_group_mapping_entry(DOM_SID sid)
696 {
697         struct pdb_methods *pdb = pdb_get_methods(False);
698
699         if ( !pdb ) {
700                 return NT_STATUS_UNSUCCESSFUL;
701         }
702
703         return pdb->delete_group_mapping_entry(pdb, sid);
704 }
705
706 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
707                             size_t *p_num_entries, BOOL unix_only)
708 {
709         struct pdb_methods *pdb = pdb_get_methods(False);
710
711         if ( !pdb ) {
712                 return False;
713         }
714
715         return NT_STATUS_IS_OK(pdb-> enum_group_mapping(pdb, sid_name_use,
716                 pp_rmap, p_num_entries, unix_only));
717 }
718
719 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
720                                 const DOM_SID *sid,
721                                 uint32 **pp_member_rids,
722                                 size_t *p_num_members)
723 {
724         struct pdb_methods *pdb = pdb_get_methods(False);
725
726         if ( !pdb ) {
727                 return NT_STATUS_UNSUCCESSFUL;
728         }
729
730         return pdb->enum_group_members(pdb, mem_ctx, sid, 
731                                                    pp_member_rids, p_num_members);
732 }
733
734 NTSTATUS pdb_enum_group_memberships(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *user,
735                                     DOM_SID **pp_sids, gid_t **pp_gids,
736                                     size_t *p_num_groups)
737 {
738         struct pdb_methods *pdb = pdb_get_methods(False);
739
740         if ( !pdb ) {
741                 return NT_STATUS_UNSUCCESSFUL;
742         }
743
744         return pdb->enum_group_memberships(
745                 pdb, mem_ctx, user,
746                 pp_sids, pp_gids, p_num_groups);
747 }
748
749 static NTSTATUS pdb_default_set_unix_primary_group(struct pdb_methods *methods,
750                                                    TALLOC_CTX *mem_ctx,
751                                                    SAM_ACCOUNT *sampass)
752 {
753         struct group *grp;
754         gid_t gid;
755
756         if (!sid_to_gid(pdb_get_group_sid(sampass), &gid) ||
757             (grp = getgrgid(gid)) == NULL) {
758                 return NT_STATUS_INVALID_PRIMARY_GROUP;
759         }
760
761         if (smb_set_primary_group(grp->gr_name,
762                                   pdb_get_username(sampass)) != 0) {
763                 return NT_STATUS_ACCESS_DENIED;
764         }
765
766         return NT_STATUS_OK;
767 }
768
769 NTSTATUS pdb_set_unix_primary_group(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *user)
770 {
771         struct pdb_methods *pdb = pdb_get_methods(False);
772
773         if ( !pdb ) {
774                 return NT_STATUS_UNSUCCESSFUL;
775         }
776
777         return pdb->set_unix_primary_group(pdb, mem_ctx, user);
778 }
779
780 /*
781  * Helper function to see whether a user is in a group. We can't use
782  * user_in_group_sid here because this creates dependencies only smbd can
783  * fulfil.
784  */
785
786 static BOOL pdb_user_in_group(TALLOC_CTX *mem_ctx, SAM_ACCOUNT *account,
787                               const DOM_SID *group_sid)
788 {
789         DOM_SID *sids;
790         gid_t *gids;
791         size_t i, num_groups;
792
793         if (!NT_STATUS_IS_OK(pdb_enum_group_memberships(mem_ctx, account,
794                                                         &sids, &gids,
795                                                         &num_groups))) {
796                 return False;
797         }
798
799         for (i=0; i<num_groups; i++) {
800                 if (sid_equal(group_sid, &sids[i])) {
801                         return True;
802                 }
803         }
804         return False;
805 }
806
807 static NTSTATUS pdb_default_add_groupmem(struct pdb_methods *methods,
808                                          TALLOC_CTX *mem_ctx,
809                                          uint32 group_rid,
810                                          uint32 member_rid)
811 {
812         DOM_SID group_sid, member_sid;
813         SAM_ACCOUNT *account = NULL;
814         GROUP_MAP map;
815         struct group *grp;
816         struct passwd *pwd;
817         const char *group_name;
818         uid_t uid;
819         NTSTATUS status;
820
821         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
822         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
823
824         if (!get_domain_group_from_sid(group_sid, &map) ||
825             (map.gid == (gid_t)-1) ||
826             ((grp = getgrgid(map.gid)) == NULL)) {
827                 return NT_STATUS_NO_SUCH_GROUP;
828         }
829
830         group_name = talloc_strdup(mem_ctx, grp->gr_name);
831         if (group_name == NULL) {
832                 return NT_STATUS_NO_MEMORY;
833         }
834
835         if (!NT_STATUS_IS_OK(status = pdb_init_sam(&account))) {
836                 return status;
837         }
838
839         if (!pdb_getsampwsid(account, &member_sid) ||
840             !sid_to_uid(&member_sid, &uid) ||
841             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
842                 return NT_STATUS_NO_SUCH_USER;
843         }
844
845         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
846                 return NT_STATUS_MEMBER_IN_GROUP;
847         }
848
849         /* 
850          * ok, the group exist, the user exist, the user is not in the group,
851          * we can (finally) add it to the group !
852          */
853
854         smb_add_user_group(group_name, pwd->pw_name);
855
856         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
857                 return NT_STATUS_ACCESS_DENIED;
858         }
859
860         return NT_STATUS_OK;
861 }
862
863 NTSTATUS pdb_add_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
864                           uint32 member_rid)
865 {
866         struct pdb_methods *pdb = pdb_get_methods(False);
867
868         if ( !pdb ) {
869                 return NT_STATUS_UNSUCCESSFUL;
870         }
871
872         return pdb->add_groupmem(pdb, mem_ctx, group_rid, member_rid);
873 }
874
875 static NTSTATUS pdb_default_del_groupmem(struct pdb_methods *methods,
876                                          TALLOC_CTX *mem_ctx,
877                                          uint32 group_rid,
878                                          uint32 member_rid)
879 {
880         DOM_SID group_sid, member_sid;
881         SAM_ACCOUNT *account = NULL;
882         GROUP_MAP map;
883         struct group *grp;
884         struct passwd *pwd;
885         const char *group_name;
886         uid_t uid;
887         NTSTATUS status;
888
889         sid_compose(&group_sid, get_global_sam_sid(), group_rid);
890         sid_compose(&member_sid, get_global_sam_sid(), member_rid);
891
892         if (!get_domain_group_from_sid(group_sid, &map) ||
893             (map.gid == (gid_t)-1) ||
894             ((grp = getgrgid(map.gid)) == NULL)) {
895                 return NT_STATUS_NO_SUCH_GROUP;
896         }
897
898         group_name = talloc_strdup(mem_ctx, grp->gr_name);
899         if (group_name == NULL) {
900                 return NT_STATUS_NO_MEMORY;
901         }
902
903         if (!NT_STATUS_IS_OK(status = pdb_init_sam(&account))) {
904                 return status;
905         }
906
907         if (!pdb_getsampwsid(account, &member_sid) ||
908             !sid_to_uid(&member_sid, &uid) ||
909             ((pwd = getpwuid_alloc(mem_ctx, uid)) == NULL)) {
910                 return NT_STATUS_NO_SUCH_USER;
911         }
912
913         if (!pdb_user_in_group(mem_ctx, account, &group_sid)) {
914                 return NT_STATUS_MEMBER_NOT_IN_GROUP;
915         }
916
917         /* 
918          * ok, the group exist, the user exist, the user is in the group,
919          * we can (finally) delete it from the group!
920          */
921
922         smb_delete_user_group(group_name, pwd->pw_name);
923
924         if (pdb_user_in_group(mem_ctx, account, &group_sid)) {
925                 return NT_STATUS_ACCESS_DENIED;
926         }
927
928         return NT_STATUS_OK;
929 }
930
931 NTSTATUS pdb_del_groupmem(TALLOC_CTX *mem_ctx, uint32 group_rid,
932                           uint32 member_rid)
933 {
934         struct pdb_methods *pdb = pdb_get_methods(False);
935
936         if ( !pdb ) {
937                 return NT_STATUS_UNSUCCESSFUL;
938         }
939
940         return pdb->del_groupmem(pdb, mem_ctx, group_rid, member_rid);
941 }
942
943 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
944 {
945         struct pdb_methods *pdb = pdb_get_methods(False);
946
947         if ( !pdb ) {
948                 return False;
949         }
950
951         return NT_STATUS_IS_OK(pdb->find_alias(pdb,
952                                                              name, sid));
953 }
954
955 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
956 {
957         struct pdb_methods *pdb = pdb_get_methods(False);
958
959         if ( !pdb ) {
960                 return NT_STATUS_NOT_IMPLEMENTED;
961         }
962
963         return pdb->create_alias(pdb, name, rid);
964 }
965
966 BOOL pdb_delete_alias(const DOM_SID *sid)
967 {
968         struct pdb_methods *pdb = pdb_get_methods(False);
969
970         if ( !pdb ) {
971                 return False;
972         }
973
974         return NT_STATUS_IS_OK(pdb->delete_alias(pdb,
975                                                              sid));
976                                                             
977 }
978
979 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
980 {
981         struct pdb_methods *pdb = pdb_get_methods(False);
982
983         if ( !pdb ) {
984                 return False;
985         }
986
987         return NT_STATUS_IS_OK(pdb->get_aliasinfo(pdb, sid,
988                                                               info));
989 }
990
991 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
992 {
993         struct pdb_methods *pdb = pdb_get_methods(False);
994
995         if ( !pdb ) {
996                 return False;
997         }
998
999         return NT_STATUS_IS_OK(pdb->set_aliasinfo(pdb, sid,
1000                                                               info));
1001 }
1002
1003 NTSTATUS pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1004 {
1005         struct pdb_methods *pdb = pdb_get_methods(False);
1006
1007         if ( !pdb ) {
1008                 return NT_STATUS_UNSUCCESSFUL;
1009         }
1010
1011         return pdb->add_aliasmem(pdb, alias, member);
1012 }
1013
1014 NTSTATUS pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1015 {
1016         struct pdb_methods *pdb = pdb_get_methods(False);
1017
1018         if ( !pdb ) {
1019                 return NT_STATUS_UNSUCCESSFUL;
1020         }
1021
1022         return pdb->del_aliasmem(pdb, alias, member);
1023 }
1024
1025 NTSTATUS pdb_enum_aliasmem(const DOM_SID *alias,
1026                            DOM_SID **pp_members, size_t *p_num_members)
1027 {
1028         struct pdb_methods *pdb = pdb_get_methods(False);
1029
1030         if ( !pdb ) {
1031                 return NT_STATUS_UNSUCCESSFUL;
1032         }
1033
1034         return pdb->enum_aliasmem(pdb, alias,
1035                                               pp_members, p_num_members);
1036 }
1037
1038 NTSTATUS pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx,
1039                                     const DOM_SID *domain_sid,
1040                                     const DOM_SID *members, size_t num_members,
1041                                     uint32 **pp_alias_rids,
1042                                     size_t *p_num_alias_rids)
1043 {
1044         struct pdb_methods *pdb = pdb_get_methods(False);
1045
1046         if ( !pdb ) {
1047                 return NT_STATUS_NOT_IMPLEMENTED;
1048         }
1049
1050         return pdb->enum_alias_memberships(pdb, mem_ctx,
1051                                                        domain_sid,
1052                                                        members, num_members,
1053                                                        pp_alias_rids,
1054                                                        p_num_alias_rids);
1055 }
1056
1057 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
1058                          int num_rids,
1059                          uint32 *rids,
1060                          const char **names,
1061                          uint32 *attrs)
1062 {
1063         struct pdb_methods *pdb = pdb_get_methods(False);
1064
1065         if ( !pdb ) {
1066                 return NT_STATUS_NOT_IMPLEMENTED;
1067         }
1068
1069         return pdb->lookup_rids(pdb, domain_sid,
1070                                             num_rids, rids, names, attrs);
1071 }
1072
1073 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
1074                           int num_names,
1075                           const char **names,
1076                           uint32 *rids,
1077                           uint32 *attrs)
1078 {
1079         struct pdb_methods *pdb = pdb_get_methods(False);
1080
1081         if ( !pdb ) {
1082                 return NT_STATUS_NOT_IMPLEMENTED;
1083         }
1084
1085         return pdb->lookup_names(pdb, domain_sid,
1086                                              num_names, names, rids, attrs);
1087 }
1088
1089 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1090 {
1091         struct pdb_methods *pdb = pdb_get_methods(False);
1092
1093         if ( !pdb ) {
1094                 return False;
1095         }
1096
1097         return NT_STATUS_IS_OK(pdb->get_account_policy(pdb, policy_index, value));
1098 }
1099
1100 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1101 {
1102         struct pdb_methods *pdb = pdb_get_methods(False);
1103
1104         if ( !pdb ) {
1105                 return False;
1106         }
1107
1108         return NT_STATUS_IS_OK(pdb->set_account_policy(pdb, policy_index, value));
1109 }
1110
1111 BOOL pdb_get_seq_num(time_t *seq_num)
1112 {
1113         struct pdb_methods *pdb = pdb_get_methods(False);
1114
1115         if ( !pdb ) {
1116                 return False;
1117         }
1118
1119         return NT_STATUS_IS_OK(pdb->get_seq_num(pdb, seq_num));
1120 }
1121
1122 BOOL pdb_uid_to_rid(uid_t uid, uint32 *rid)
1123 {
1124         struct pdb_methods *pdb = pdb_get_methods(False);
1125
1126         if ( !pdb ) {
1127                 return False;
1128         }
1129
1130         return pdb->uid_to_rid(pdb, uid, rid);
1131 }
1132
1133 BOOL pdb_gid_to_sid(gid_t gid, DOM_SID *sid)
1134 {
1135         struct pdb_methods *pdb = pdb_get_methods(False);
1136
1137         if ( !pdb ) {
1138                 return False;
1139         }
1140
1141         return pdb->gid_to_sid(pdb, gid, sid);
1142 }
1143
1144 BOOL pdb_sid_to_id(const DOM_SID *sid, union unid_t *id,
1145                    enum SID_NAME_USE *type)
1146 {
1147         struct pdb_methods *pdb = pdb_get_methods(False);
1148
1149         if ( !pdb ) {
1150                 return False;
1151         }
1152
1153         return pdb->sid_to_id(pdb, sid, id, type);
1154 }
1155
1156 BOOL pdb_rid_algorithm(void)
1157 {
1158         struct pdb_methods *pdb = pdb_get_methods(False);
1159
1160         if ( !pdb ) {
1161                 return False;
1162         }
1163
1164         return pdb->rid_algorithm(pdb);
1165 }
1166
1167 BOOL pdb_new_rid(uint32 *rid)
1168 {
1169         struct pdb_methods *pdb = pdb_get_methods(False);
1170
1171         if ( !pdb ) {
1172                 return False;
1173         }
1174
1175         if (pdb_rid_algorithm()) {
1176                 DEBUG(0, ("Trying to allocate a RID when algorithmic RIDs "
1177                           "are active\n"));
1178                 return False;
1179         }
1180
1181         if (algorithmic_rid_base() != BASE_RID) {
1182                 DEBUG(0, ("'algorithmic rid base' is set but a passdb backend "
1183                           "without algorithmic RIDs is chosen.\n"));
1184                 DEBUGADD(0, ("Please map all used groups using 'net groupmap "
1185                              "add', set the maximum used RID using\n"));
1186                 DEBUGADD(0, ("'net setmaxrid' and remove the parameter\n"));
1187                 return False;
1188         }
1189
1190         return pdb->new_rid(pdb, rid);
1191 }
1192
1193 /***************************************************************
1194   Initialize the static context (at smbd startup etc). 
1195
1196   If uninitialised, context will auto-init on first use.
1197  ***************************************************************/
1198
1199 BOOL initialize_password_db(BOOL reload)
1200 {       
1201         return (pdb_get_methods(reload) != NULL);
1202 }
1203
1204
1205 /***************************************************************************
1206   Default implementations of some functions.
1207  ****************************************************************************/
1208
1209 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1210 {
1211         return NT_STATUS_NO_SUCH_USER;
1212 }
1213
1214 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1215 {
1216         return NT_STATUS_NO_SUCH_USER;
1217 }
1218
1219 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1220 {
1221         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1222         return NT_STATUS_NOT_IMPLEMENTED;
1223 }
1224
1225 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1226 {
1227         return NT_STATUS_NOT_IMPLEMENTED;
1228 }
1229
1230 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1231 {
1232         return NT_STATUS_NOT_IMPLEMENTED;
1233 }
1234
1235 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd, const char *newname)
1236 {
1237         return NT_STATUS_NOT_IMPLEMENTED;
1238 }
1239
1240 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1241 {
1242         return NT_STATUS_OK;
1243 }
1244
1245 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1246 {
1247         return NT_STATUS_NOT_IMPLEMENTED;
1248 }
1249
1250 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1251 {
1252         return NT_STATUS_NOT_IMPLEMENTED;
1253 }
1254
1255 static void pdb_default_endsampwent(struct pdb_methods *methods)
1256 {
1257         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1258 }
1259
1260 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1261 {
1262         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1263 }
1264
1265 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1266 {
1267         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1268 }
1269
1270 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1271 {
1272         *seq_num = time(NULL);
1273         return NT_STATUS_OK;
1274 }
1275
1276 static BOOL pdb_default_uid_to_rid(struct pdb_methods *methods, uid_t uid,
1277                                    uint32 *rid)
1278 {
1279         SAM_ACCOUNT *sampw = NULL;
1280         struct passwd *unix_pw;
1281         BOOL ret;
1282         
1283         unix_pw = sys_getpwuid( uid );
1284
1285         if ( !unix_pw ) {
1286                 DEBUG(4,("pdb_default_uid_to_rid: host has no idea of uid "
1287                          "%lu\n", (unsigned long)uid));
1288                 return False;
1289         }
1290         
1291         if ( !NT_STATUS_IS_OK(pdb_init_sam(&sampw)) ) {
1292                 DEBUG(0,("pdb_default_uid_to_rid: failed to allocate "
1293                          "SAM_ACCOUNT object\n"));
1294                 return False;
1295         }
1296         
1297         become_root();
1298         ret = NT_STATUS_IS_OK(
1299                 methods->getsampwnam(methods, sampw, unix_pw->pw_name ));
1300         unbecome_root();
1301
1302         if (!ret) {
1303                 DEBUG(5, ("pdb_default_uid_to_rid: Did not find user "
1304                           "%s (%d)\n", unix_pw->pw_name, uid));
1305                 pdb_free_sam(&sampw);
1306                 return False;
1307         }
1308
1309         ret = sid_peek_check_rid(get_global_sam_sid(),
1310                                  pdb_get_user_sid(sampw), rid);
1311
1312         if (!ret) {
1313                 DEBUG(1, ("Could not peek rid out of sid %s\n",
1314                           sid_string_static(pdb_get_user_sid(sampw))));
1315         }
1316
1317         pdb_free_sam(&sampw);
1318         return ret;
1319 }
1320
1321 static BOOL pdb_default_gid_to_sid(struct pdb_methods *methods, gid_t gid,
1322                                    DOM_SID *sid)
1323 {
1324         GROUP_MAP map;
1325
1326         if (!NT_STATUS_IS_OK(methods->getgrgid(methods, &map, gid))) {
1327                 return False;
1328         }
1329
1330         sid_copy(sid, &map.sid);
1331         return True;
1332 }
1333
1334 static BOOL pdb_default_sid_to_id(struct pdb_methods *methods,
1335                                   const DOM_SID *sid,
1336                                   union unid_t *id, enum SID_NAME_USE *type)
1337 {
1338         TALLOC_CTX *mem_ctx;
1339         BOOL ret = False;
1340         const char *name;
1341         uint32 rid;
1342
1343         mem_ctx = talloc_new(NULL);
1344
1345         if (mem_ctx == NULL) {
1346                 DEBUG(0, ("talloc_new failed\n"));
1347                 return False;
1348         }
1349
1350         if (sid_peek_check_rid(get_global_sam_sid(), sid, &rid)) {
1351                 /* Here we might have users as well as groups and aliases */
1352                 ret = lookup_global_sam_rid(mem_ctx, rid, &name, type, id);
1353                 goto done;
1354         }
1355
1356         if (sid_peek_check_rid(&global_sid_Builtin, sid, &rid)) {
1357                 /* Here we only have aliases */
1358                 GROUP_MAP map;
1359                 if (!NT_STATUS_IS_OK(methods->getgrsid(methods, &map, *sid))) {
1360                         DEBUG(10, ("Could not find map for sid %s\n",
1361                                    sid_string_static(sid)));
1362                         goto done;
1363                 }
1364                 if ((map.sid_name_use != SID_NAME_ALIAS) &&
1365                     (map.sid_name_use != SID_NAME_WKN_GRP)) {
1366                         DEBUG(10, ("Map for sid %s is a %s, expected an "
1367                                    "alias\n", sid_string_static(sid),
1368                                    sid_type_lookup(map.sid_name_use)));
1369                         goto done;
1370                 }
1371
1372                 id->gid = map.gid;
1373                 *type = SID_NAME_ALIAS;
1374                 ret = True;
1375                 goto done;
1376         }
1377
1378         DEBUG(5, ("Sid %s is neither ours nor builtin, don't know it\n",
1379                   sid_string_static(sid)));
1380
1381  done:
1382
1383         talloc_free(mem_ctx);
1384         return ret;
1385 }
1386
1387 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1388                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1389 {
1390         size_t i;
1391
1392         for (i=0; i<*p_num; i++) {
1393                 if ((*pp_uids)[i] == uid)
1394                         return;
1395         }
1396         
1397         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1398
1399         if (*pp_uids == NULL)
1400                 return;
1401
1402         (*pp_uids)[*p_num] = uid;
1403         *p_num += 1;
1404 }
1405
1406 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1407 {
1408         struct group *grp;
1409         char **gr;
1410         struct passwd *pwd;
1411  
1412         *pp_uids = NULL;
1413         *p_num = 0;
1414
1415         /* We only look at our own sam, so don't care about imported stuff */
1416
1417         winbind_off();
1418
1419         if ((grp = getgrgid(gid)) == NULL) {
1420                 winbind_on();
1421                 return False;
1422         }
1423
1424         /* Primary group members */
1425
1426         setpwent();
1427         while ((pwd = getpwent()) != NULL) {
1428                 if (pwd->pw_gid == gid) {
1429                         add_uid_to_array_unique(mem_ctx, pwd->pw_uid,
1430                                                 pp_uids, p_num);
1431                 }
1432         }
1433         endpwent();
1434
1435         /* Secondary group members */
1436
1437         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1438                 struct passwd *pw = getpwnam(*gr);
1439
1440                 if (pw == NULL)
1441                         continue;
1442                 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1443         }
1444
1445         winbind_on();
1446
1447         return True;
1448 }
1449
1450 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1451                                         TALLOC_CTX *mem_ctx,
1452                                         const DOM_SID *group,
1453                                         uint32 **pp_member_rids,
1454                                         size_t *p_num_members)
1455 {
1456         gid_t gid;
1457         uid_t *uids;
1458         size_t i, num_uids;
1459
1460         *pp_member_rids = NULL;
1461         *p_num_members = 0;
1462
1463         if (!sid_to_gid(group, &gid))
1464                 return NT_STATUS_NO_SUCH_GROUP;
1465
1466         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1467                 return NT_STATUS_NO_SUCH_GROUP;
1468
1469         if (num_uids == 0)
1470                 return NT_STATUS_OK;
1471
1472         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1473
1474         for (i=0; i<num_uids; i++) {
1475                 DOM_SID sid;
1476
1477                 uid_to_sid(&sid, uids[i]);
1478
1479                 if (!sid_check_is_in_our_domain(&sid)) {
1480                         DEBUG(5, ("Inconsistent SAM -- group member uid not "
1481                                   "in our domain\n"));
1482                         continue;
1483                 }
1484
1485                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1486                 *p_num_members += 1;
1487         }
1488
1489         return NT_STATUS_OK;
1490 }
1491
1492 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
1493                                             TALLOC_CTX *mem_ctx,
1494                                             SAM_ACCOUNT *user,
1495                                             DOM_SID **pp_sids,
1496                                             gid_t **pp_gids,
1497                                             size_t *p_num_groups)
1498 {
1499         size_t i;
1500         gid_t gid;
1501
1502         if (!sid_to_gid(pdb_get_group_sid(user), &gid)) {
1503                 DEBUG(10, ("sid_to_gid failed\n"));
1504                 return NT_STATUS_NO_SUCH_USER;
1505         }
1506
1507         if (!getgroups_unix_user(mem_ctx, pdb_get_username(user), gid,
1508                                  pp_gids, p_num_groups)) {
1509                 return NT_STATUS_NO_SUCH_USER;
1510         }
1511
1512         if (*p_num_groups == 0) {
1513                 smb_panic("primary group missing");
1514         }
1515
1516         *pp_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, *p_num_groups);
1517
1518         if (*pp_sids == NULL) {
1519                 talloc_free(*pp_gids);
1520                 return NT_STATUS_NO_MEMORY;
1521         }
1522
1523         for (i=0; i<*p_num_groups; i++) {
1524                 gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]);
1525         }
1526
1527         return NT_STATUS_OK;
1528 }
1529
1530 /*******************************************************************
1531  Look up a rid in the SAM we're responsible for (i.e. passdb)
1532  ********************************************************************/
1533
1534 static BOOL lookup_global_sam_rid(TALLOC_CTX *mem_ctx, uint32 rid,
1535                                   const char **name,
1536                                   enum SID_NAME_USE *psid_name_use,
1537                                   union unid_t *unix_id)
1538 {
1539         SAM_ACCOUNT *sam_account = NULL;
1540         GROUP_MAP map;
1541         BOOL ret;
1542         DOM_SID sid;
1543
1544         *psid_name_use = SID_NAME_UNKNOWN;
1545         
1546         DEBUG(5,("lookup_global_sam_rid: looking up RID %u.\n",
1547                  (unsigned int)rid));
1548
1549         sid_copy(&sid, get_global_sam_sid());
1550         sid_append_rid(&sid, rid);
1551         
1552         /* see if the passdb can help us with the name of the user */
1553         if (!NT_STATUS_IS_OK(pdb_init_sam(&sam_account))) {
1554                 return False;
1555         }
1556
1557         /* BEING ROOT BLLOCK */
1558         become_root();
1559         if (pdb_getsampwsid(sam_account, &sid)) {
1560                 struct passwd *pw;
1561
1562                 unbecome_root();                /* -----> EXIT BECOME_ROOT() */
1563                 *name = talloc_strdup(mem_ctx, pdb_get_username(sam_account));
1564                 *psid_name_use = SID_NAME_USER;
1565
1566                 pdb_free_sam(&sam_account);
1567
1568                 if (unix_id == NULL) {
1569                         return True;
1570                 }
1571
1572                 pw = Get_Pwnam(*name);
1573                 if (pw == NULL) {
1574                         return False;
1575                 }
1576                 unix_id->uid = pw->pw_uid;
1577                 return True;
1578         }
1579         pdb_free_sam(&sam_account);
1580         
1581         ret = pdb_getgrsid(&map, sid);
1582         unbecome_root();
1583         /* END BECOME_ROOT BLOCK */
1584         
1585         if ( ret ) {
1586                 if (map.gid!=(gid_t)-1) {
1587                         DEBUG(5,("lookup_global_sam_rid: mapped group %s to "
1588                                  "gid %u\n", map.nt_name,
1589                                  (unsigned int)map.gid));
1590                 } else {
1591                         DEBUG(5,("lookup_global_sam_rid: mapped group %s to "
1592                                  "no unix gid.  Returning name.\n",
1593                                  map.nt_name));
1594                 }
1595
1596                 *name = talloc_strdup(mem_ctx, map.nt_name);
1597                 *psid_name_use = map.sid_name_use;
1598
1599                 if (unix_id == NULL) {
1600                         return True;
1601                 }
1602
1603                 if (map.gid == (gid_t)-1) {
1604                         DEBUG(5, ("Can't find a unix id for an unmapped "
1605                                   "group\n"));
1606                         return False;
1607                 }
1608
1609                 unix_id->gid = map.gid;
1610                 return True;
1611         }
1612
1613         return False;
1614 }
1615
1616 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1617                                  const DOM_SID *domain_sid,
1618                                  int num_rids,
1619                                  uint32 *rids,
1620                                  const char **names,
1621                                  uint32 *attrs)
1622 {
1623         int i;
1624         NTSTATUS result;
1625         BOOL have_mapped = False;
1626         BOOL have_unmapped = False;
1627
1628         if (sid_check_is_builtin(domain_sid)) {
1629
1630                 for (i=0; i<num_rids; i++) {
1631                         const char *name;
1632
1633                         if (lookup_builtin_rid(names, rids[i], &name)) {
1634                                 attrs[i] = SID_NAME_ALIAS;
1635                                 names[i] = name;
1636                                 DEBUG(5,("lookup_rids: %s:%d\n",
1637                                          names[i], attrs[i]));
1638                                 have_mapped = True;
1639                         } else {
1640                                 have_unmapped = True;
1641                                 attrs[i] = SID_NAME_UNKNOWN;
1642                         }
1643                 }
1644                 goto done;
1645         }
1646
1647         /* Should not happen, but better check once too many */
1648         if (!sid_check_is_domain(domain_sid)) {
1649                 return NT_STATUS_INVALID_HANDLE;
1650         }
1651
1652         for (i = 0; i < num_rids; i++) {
1653                 const char *name;
1654
1655                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i],
1656                                           NULL)) {
1657                         names[i] = name;
1658                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1659                         have_mapped = True;
1660                 } else {
1661                         have_unmapped = True;
1662                         attrs[i] = SID_NAME_UNKNOWN;
1663                 }
1664         }
1665
1666  done:
1667
1668         result = NT_STATUS_NONE_MAPPED;
1669
1670         if (have_mapped)
1671                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1672
1673         return result;
1674 }
1675
1676 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1677                                   const DOM_SID *domain_sid,
1678                                   int num_names,
1679                                   const char **names,
1680                                   uint32 *rids,
1681                                   uint32 *attrs)
1682 {
1683         int i;
1684         NTSTATUS result;
1685         BOOL have_mapped = False;
1686         BOOL have_unmapped = False;
1687
1688         if (sid_check_is_builtin(domain_sid)) {
1689
1690                 for (i=0; i<num_names; i++) {
1691                         uint32 rid;
1692
1693                         if (lookup_builtin_name(names[i], &rid)) {
1694                                 attrs[i] = SID_NAME_ALIAS;
1695                                 rids[i] = rid;
1696                                 DEBUG(5,("lookup_rids: %s:%d\n",
1697                                          names[i], attrs[i]));
1698                                 have_mapped = True;
1699                         } else {
1700                                 have_unmapped = True;
1701                                 attrs[i] = SID_NAME_UNKNOWN;
1702                         }
1703                 }
1704                 goto done;
1705         }
1706
1707         /* Should not happen, but better check once too many */
1708         if (!sid_check_is_domain(domain_sid)) {
1709                 return NT_STATUS_INVALID_HANDLE;
1710         }
1711
1712         for (i = 0; i < num_names; i++) {
1713                 if (lookup_global_sam_name(names[i], 0, &rids[i], &attrs[i])) {
1714                         DEBUG(5,("lookup_names: %s-> %d:%d\n", names[i],
1715                                  rids[i], attrs[i]));
1716                         have_mapped = True;
1717                 } else {
1718                         have_unmapped = True;
1719                         attrs[i] = SID_NAME_UNKNOWN;
1720                 }
1721         }
1722
1723  done:
1724
1725         result = NT_STATUS_NONE_MAPPED;
1726
1727         if (have_mapped)
1728                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1729
1730         return result;
1731 }
1732
1733 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1734 {
1735         TALLOC_CTX *mem_ctx;
1736         struct pdb_search *result;
1737
1738         mem_ctx = talloc_init("pdb_search");
1739         if (mem_ctx == NULL) {
1740                 DEBUG(0, ("talloc_init failed\n"));
1741                 return NULL;
1742         }
1743
1744         result = TALLOC_P(mem_ctx, struct pdb_search);
1745         if (result == NULL) {
1746                 DEBUG(0, ("talloc failed\n"));
1747                 return NULL;
1748         }
1749
1750         result->mem_ctx = mem_ctx;
1751         result->type = type;
1752         result->cache = NULL;
1753         result->num_entries = 0;
1754         result->cache_size = 0;
1755         result->search_ended = False;
1756
1757         /* Segfault appropriately if not initialized */
1758         result->next_entry = NULL;
1759         result->search_end = NULL;
1760
1761         return result;
1762 }
1763
1764 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1765                               uint16 acct_flags,
1766                               const char *account_name,
1767                               const char *fullname,
1768                               const char *description,
1769                               struct samr_displayentry *entry)
1770 {
1771         entry->rid = rid;
1772         entry->acct_flags = acct_flags;
1773
1774         if (account_name != NULL)
1775                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1776         else
1777                 entry->account_name = "";
1778
1779         if (fullname != NULL)
1780                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1781         else
1782                 entry->fullname = "";
1783
1784         if (description != NULL)
1785                 entry->description = talloc_strdup(mem_ctx, description);
1786         else
1787                 entry->description = "";
1788 }
1789
1790 static BOOL user_search_in_progress = False;
1791 struct user_search {
1792         uint16 acct_flags;
1793 };
1794
1795 static BOOL next_entry_users(struct pdb_search *s,
1796                              struct samr_displayentry *entry)
1797 {
1798         struct user_search *state = s->private_data;
1799         SAM_ACCOUNT *user = NULL;
1800         NTSTATUS status;
1801
1802  next:
1803         status = pdb_init_sam(&user);
1804         if (!NT_STATUS_IS_OK(status)) {
1805                 DEBUG(0, ("Could not pdb_init_sam\n"));
1806                 return False;
1807         }
1808
1809         if (!pdb_getsampwent(user)) {
1810                 pdb_free_sam(&user);
1811                 return False;
1812         }
1813
1814         if ((state->acct_flags != 0) &&
1815             ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1816                 pdb_free_sam(&user);
1817                 goto next;
1818         }
1819
1820         fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1821                           pdb_get_acct_ctrl(user), pdb_get_username(user),
1822                           pdb_get_fullname(user), pdb_get_acct_desc(user),
1823                           entry);
1824
1825         pdb_free_sam(&user);
1826         return True;
1827 }
1828
1829 static void search_end_users(struct pdb_search *search)
1830 {
1831         pdb_endsampwent();
1832         user_search_in_progress = False;
1833 }
1834
1835 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1836                                      struct pdb_search *search,
1837                                      uint16 acct_flags)
1838 {
1839         struct user_search *state;
1840
1841         if (user_search_in_progress) {
1842                 DEBUG(1, ("user search in progress\n"));
1843                 return False;
1844         }
1845
1846         if (!pdb_setsampwent(False, acct_flags)) {
1847                 DEBUG(5, ("Could not start search\n"));
1848                 return False;
1849         }
1850
1851         user_search_in_progress = True;
1852
1853         state = TALLOC_P(search->mem_ctx, struct user_search);
1854         if (state == NULL) {
1855                 DEBUG(0, ("talloc failed\n"));
1856                 return False;
1857         }
1858
1859         state->acct_flags = acct_flags;
1860
1861         search->private_data = state;
1862         search->next_entry = next_entry_users;
1863         search->search_end = search_end_users;
1864         return True;
1865 }
1866
1867 struct group_search {
1868         GROUP_MAP *groups;
1869         size_t num_groups, current_group;
1870 };
1871
1872 static BOOL next_entry_groups(struct pdb_search *s,
1873                               struct samr_displayentry *entry)
1874 {
1875         struct group_search *state = s->private_data;
1876         uint32 rid;
1877         GROUP_MAP *map = &state->groups[state->current_group];
1878
1879         if (state->current_group == state->num_groups)
1880                 return False;
1881
1882         sid_peek_rid(&map->sid, &rid);
1883
1884         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1885                           entry);
1886
1887         state->current_group += 1;
1888         return True;
1889 }
1890
1891 static void search_end_groups(struct pdb_search *search)
1892 {
1893         struct group_search *state = search->private_data;
1894         SAFE_FREE(state->groups);
1895 }
1896
1897 static BOOL pdb_search_grouptype(struct pdb_search *search,
1898                                  enum SID_NAME_USE type)
1899 {
1900         struct group_search *state;
1901
1902         state = TALLOC_P(search->mem_ctx, struct group_search);
1903         if (state == NULL) {
1904                 DEBUG(0, ("talloc failed\n"));
1905                 return False;
1906         }
1907
1908         if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1909                                     True)) {
1910                 DEBUG(0, ("Could not enum groups\n"));
1911                 return False;
1912         }
1913
1914         state->current_group = 0;
1915         search->private_data = state;
1916         search->next_entry = next_entry_groups;
1917         search->search_end = search_end_groups;
1918         return True;
1919 }
1920
1921 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1922                                       struct pdb_search *search)
1923 {
1924         return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1925 }
1926
1927 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1928                                        struct pdb_search *search,
1929                                        const DOM_SID *sid)
1930 {
1931
1932         if (sid_equal(sid, get_global_sam_sid()))
1933                 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1934
1935         if (sid_equal(sid, &global_sid_Builtin))
1936                 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
1937
1938         DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1939         return False;
1940 }
1941
1942 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1943                                                      uint32 idx)
1944 {
1945         if (idx < search->num_entries)
1946                 return &search->cache[idx];
1947
1948         if (search->search_ended)
1949                 return NULL;
1950
1951         while (idx >= search->num_entries) {
1952                 struct samr_displayentry entry;
1953
1954                 if (!search->next_entry(search, &entry)) {
1955                         search->search_end(search);
1956                         search->search_ended = True;
1957                         break;
1958                 }
1959
1960                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1961                                    entry, &search->cache, &search->num_entries,
1962                                    &search->cache_size);
1963         }
1964
1965         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1966 }
1967
1968 struct pdb_search *pdb_search_users(uint16 acct_flags)
1969 {
1970         struct pdb_methods *pdb = pdb_get_methods(False);
1971         struct pdb_search *result;
1972
1973         if (pdb == NULL) return NULL;
1974
1975         result = pdb_search_init(PDB_USER_SEARCH);
1976         if (result == NULL) return NULL;
1977
1978         if (!pdb->search_users(pdb, result, acct_flags)) {
1979                 talloc_destroy(result->mem_ctx);
1980                 return NULL;
1981         }
1982         return result;
1983 }
1984
1985 struct pdb_search *pdb_search_groups(void)
1986 {
1987         struct pdb_methods *pdb = pdb_get_methods(False);
1988         struct pdb_search *result;
1989
1990         if (pdb == NULL) return NULL;
1991
1992         result = pdb_search_init(PDB_GROUP_SEARCH);
1993         if (result == NULL) return NULL;
1994
1995         if (!pdb->search_groups(pdb, result)) {
1996                 talloc_destroy(result->mem_ctx);
1997                 return NULL;
1998         }
1999         return result;
2000 }
2001
2002 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2003 {
2004         struct pdb_methods *pdb = pdb_get_methods(False);
2005         struct pdb_search *result;
2006
2007         if (pdb == NULL) return NULL;
2008
2009         result = pdb_search_init(PDB_ALIAS_SEARCH);
2010         if (result == NULL) return NULL;
2011
2012         if (!pdb->search_aliases(pdb, result, sid)) {
2013                 talloc_destroy(result->mem_ctx);
2014                 return NULL;
2015         }
2016         return result;
2017 }
2018
2019 uint32 pdb_search_entries(struct pdb_search *search,
2020                           uint32 start_idx, uint32 max_entries,
2021                           struct samr_displayentry **result)
2022 {
2023         struct samr_displayentry *end_entry;
2024         uint32 end_idx = start_idx+max_entries-1;
2025
2026         /* The first entry needs to be searched after the last. Otherwise the
2027          * first entry might have moved due to a realloc during the search for
2028          * the last entry. */
2029
2030         end_entry = pdb_search_getentry(search, end_idx);
2031         *result = pdb_search_getentry(search, start_idx);
2032
2033         if (end_entry != NULL)
2034                 return max_entries;
2035
2036         if (start_idx >= search->num_entries)
2037                 return 0;
2038
2039         return search->num_entries - start_idx;
2040 }
2041
2042 void pdb_search_destroy(struct pdb_search *search)
2043 {
2044         if (search == NULL)
2045                 return;
2046
2047         if (!search->search_ended)
2048                 search->search_end(search);
2049
2050         talloc_destroy(search->mem_ctx);
2051 }
2052
2053 /*******************************************************************
2054  Create a pdb_methods structure and initialize it with the default
2055  operations.  In this way a passdb module can simply implement
2056  the functionality it cares about.  However, normally this is done 
2057  in groups of related functions.
2058 *******************************************************************/
2059
2060 NTSTATUS make_pdb_method( struct pdb_methods **methods ) 
2061 {
2062         /* allocate memory for the structure as its own talloc CTX */
2063
2064         if ( !(*methods = TALLOC_ZERO_P(NULL, struct pdb_methods) ) ) {
2065                 return NT_STATUS_NO_MEMORY;
2066         }
2067
2068         (*methods)->setsampwent = pdb_default_setsampwent;
2069         (*methods)->endsampwent = pdb_default_endsampwent;
2070         (*methods)->getsampwent = pdb_default_getsampwent;
2071         (*methods)->getsampwnam = pdb_default_getsampwnam;
2072         (*methods)->getsampwsid = pdb_default_getsampwsid;
2073         (*methods)->create_user = pdb_default_create_user;
2074         (*methods)->delete_user = pdb_default_delete_user;
2075         (*methods)->add_sam_account = pdb_default_add_sam_account;
2076         (*methods)->update_sam_account = pdb_default_update_sam_account;
2077         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2078         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2079         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2080
2081         (*methods)->getgrsid = pdb_default_getgrsid;
2082         (*methods)->getgrgid = pdb_default_getgrgid;
2083         (*methods)->getgrnam = pdb_default_getgrnam;
2084         (*methods)->create_dom_group = pdb_default_create_dom_group;
2085         (*methods)->delete_dom_group = pdb_default_delete_dom_group;
2086         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2087         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2088         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2089         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2090         (*methods)->enum_group_members = pdb_default_enum_group_members;
2091         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2092         (*methods)->set_unix_primary_group = pdb_default_set_unix_primary_group;
2093         (*methods)->add_groupmem = pdb_default_add_groupmem;
2094         (*methods)->del_groupmem = pdb_default_del_groupmem;
2095         (*methods)->find_alias = pdb_default_find_alias;
2096         (*methods)->create_alias = pdb_default_create_alias;
2097         (*methods)->delete_alias = pdb_default_delete_alias;
2098         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2099         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2100         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2101         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2102         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2103         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2104         (*methods)->lookup_rids = pdb_default_lookup_rids;
2105         (*methods)->get_account_policy = pdb_default_get_account_policy;
2106         (*methods)->set_account_policy = pdb_default_set_account_policy;
2107         (*methods)->get_seq_num = pdb_default_get_seq_num;
2108         (*methods)->uid_to_rid = pdb_default_uid_to_rid;
2109         (*methods)->gid_to_sid = pdb_default_gid_to_sid;
2110         (*methods)->sid_to_id = pdb_default_sid_to_id;
2111
2112         (*methods)->search_users = pdb_default_search_users;
2113         (*methods)->search_groups = pdb_default_search_groups;
2114         (*methods)->search_aliases = pdb_default_search_aliases;
2115
2116         return NT_STATUS_OK;
2117 }