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