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