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