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