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