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