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