r12163: Change lookup_sid and lookup_name to return const char * instead of char *,
[ira/wip.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
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
27
28 static struct pdb_init_function_entry *backends = NULL;
29
30 static void lazy_initialize_passdb(void)
31 {
32         static BOOL initialized = False;
33         if(initialized)return;
34         static_init_pdb;
35         initialized = True;
36 }
37
38 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
39
40 /*******************************************************************
41  Clean up uninitialised passwords.  The only way to tell 
42  that these values are not 'real' is that they do not
43  have a valid last set time.  Instead, the value is fixed at 0. 
44  Therefore we use that as the key for 'is this a valid password'.
45  However, it is perfectly valid to have a 'default' last change
46  time, such LDAP with a missing attribute would produce.
47 ********************************************************************/
48
49 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass) 
50 {
51         const uint8 *lm_pwd, *nt_pwd;
52         
53         /* only reset a password if the last set time has been 
54            explicitly been set to zero.  A default last set time 
55            is ignored */
56
57         if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT) 
58                 && (pdb_get_pass_last_set_time(pass) == 0) ) 
59         {
60                 
61                 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT) 
62                 {
63                         lm_pwd = pdb_get_lanman_passwd(pass);
64                         if (lm_pwd) 
65                                 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
66                 }
67                 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT) 
68                 {
69                         nt_pwd = pdb_get_nt_passwd(pass);
70                         if (nt_pwd) 
71                                 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
72                 }
73         }
74
75         return;
76 }
77
78 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init) 
79 {
80         struct pdb_init_function_entry *entry = backends;
81
82         if(version != PASSDB_INTERFACE_VERSION) {
83                 DEBUG(0,("Can't register passdb backend!\n"
84                          "You tried to register a passdb module with PASSDB_INTERFACE_VERSION %d, "
85                          "while this version of samba uses version %d\n", 
86                          version,PASSDB_INTERFACE_VERSION));
87                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
88         }
89
90         if (!name || !init) {
91                 return NT_STATUS_INVALID_PARAMETER;
92         }
93
94         DEBUG(5,("Attempting to register passdb backend %s\n", name));
95
96         /* Check for duplicates */
97         if (pdb_find_backend_entry(name)) {
98                 DEBUG(0,("There already is a passdb backend registered with the name %s!\n", name));
99                 return NT_STATUS_OBJECT_NAME_COLLISION;
100         }
101
102         entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
103         entry->name = smb_xstrdup(name);
104         entry->init = init;
105
106         DLIST_ADD(backends, entry);
107         DEBUG(5,("Successfully added passdb backend '%s'\n", name));
108         return NT_STATUS_OK;
109 }
110
111 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
112 {
113         struct pdb_init_function_entry *entry = backends;
114
115         while(entry) {
116                 if (strcmp(entry->name, name)==0) return entry;
117                 entry = entry->next;
118         }
119
120         return NULL;
121 }
122
123 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update, uint16 acb_mask)
124 {
125         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
126
127         if (!context) {
128                 DEBUG(0, ("invalid pdb_context specified!\n"));
129                 return ret;
130         }
131
132         context->pwent_methods = context->pdb_methods;
133
134         if (!context->pwent_methods) {
135                 /* No passdbs at all */
136                 return ret;
137         }
138
139         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->setsampwent(context->pwent_methods, update, acb_mask))) {
140                 context->pwent_methods = context->pwent_methods->next;
141                 if (context->pwent_methods == NULL) 
142                         return NT_STATUS_UNSUCCESSFUL;
143         }
144         return ret;
145 }
146
147 static void context_endsampwent(struct pdb_context *context)
148 {
149         if ((!context)){
150                 DEBUG(0, ("invalid pdb_context specified!\n"));
151                 return;
152         }
153
154         if (context->pwent_methods && context->pwent_methods->endsampwent)
155                 context->pwent_methods->endsampwent(context->pwent_methods);
156
157         /* So we won't get strange data when calling getsampwent now */
158         context->pwent_methods = NULL;
159 }
160
161 static NTSTATUS context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
162 {
163         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
164
165         if ((!context) || (!context->pwent_methods)) {
166                 DEBUG(0, ("invalid pdb_context specified!\n"));
167                 return ret;
168         }
169         /* Loop until we find something useful */
170         while (NT_STATUS_IS_ERR(ret = context->pwent_methods->getsampwent(context->pwent_methods, user))) {
171
172                 context->pwent_methods->endsampwent(context->pwent_methods);
173
174                 context->pwent_methods = context->pwent_methods->next;
175
176                 /* All methods are checked now. There are no more entries */
177                 if (context->pwent_methods == NULL)
178                         return ret;
179         
180                 context->pwent_methods->setsampwent(context->pwent_methods, False, 0);
181         }
182         user->methods = context->pwent_methods;
183         pdb_force_pw_initialization(user);
184         return ret;
185 }
186
187 static NTSTATUS context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
188 {
189         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
190
191         struct pdb_methods *curmethods;
192         if ((!context)) {
193                 DEBUG(0, ("invalid pdb_context specified!\n"));
194                 return ret;
195         }
196         curmethods = context->pdb_methods;
197         while (curmethods){
198                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwnam(curmethods, sam_acct, username))) {
199                         pdb_force_pw_initialization(sam_acct);
200                         sam_acct->methods = curmethods;
201                         return ret;
202                 }
203                 curmethods = curmethods->next;
204         }
205
206         return ret;
207 }
208
209 static NTSTATUS context_getsampwsid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
210 {
211         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
212
213         struct pdb_methods *curmethods;
214         if ((!context)) {
215                 DEBUG(0, ("invalid pdb_context specified!\n"));
216                 return ret;
217         }
218         
219         curmethods = context->pdb_methods;
220
221         while (curmethods){
222                 if (NT_STATUS_IS_OK(ret = curmethods->getsampwsid(curmethods, sam_acct, sid))) {
223                         pdb_force_pw_initialization(sam_acct);
224                         sam_acct->methods = curmethods;
225                         return ret;
226                 }
227                 curmethods = curmethods->next;
228         }
229
230         return ret;
231 }
232
233 static NTSTATUS context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
234 {
235         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
236         const uint8 *lm_pw, *nt_pw;
237         uint16 acb_flags;
238
239         if ((!context) || (!context->pdb_methods)) {
240                 DEBUG(0, ("invalid pdb_context specified!\n"));
241                 return ret;
242         }
243
244         /* disable acccounts with no passwords (that has not 
245            been allowed by the  ACB_PWNOTREQ bit */
246         
247         lm_pw = pdb_get_lanman_passwd( sam_acct );
248         nt_pw = pdb_get_nt_passwd( sam_acct );
249         acb_flags = pdb_get_acct_ctrl( sam_acct );
250         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
251                 acb_flags |= ACB_DISABLED;
252                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
253         }
254         
255         /** @todo  This is where a 're-read on add' should be done */
256         /* We now add a new account to the first database listed. 
257          * Should we? */
258
259         return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
260 }
261
262 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
263 {
264         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
265         const uint8 *lm_pw, *nt_pw;
266         uint16 acb_flags;
267
268         if (!context) {
269                 DEBUG(0, ("invalid pdb_context specified!\n"));
270                 return ret;
271         }
272
273         if (!sam_acct || !sam_acct->methods){
274                 DEBUG(0, ("invalid sam_acct specified\n"));
275                 return ret;
276         }
277
278         /* disable acccounts with no passwords (that has not 
279            been allowed by the  ACB_PWNOTREQ bit */
280         
281         lm_pw = pdb_get_lanman_passwd( sam_acct );
282         nt_pw = pdb_get_nt_passwd( sam_acct );
283         acb_flags = pdb_get_acct_ctrl( sam_acct );
284         if ( !lm_pw && !nt_pw && !(acb_flags&ACB_PWNOTREQ) ) {
285                 acb_flags |= ACB_DISABLED;
286                 pdb_set_acct_ctrl( sam_acct, acb_flags, PDB_CHANGED );
287         }
288         
289         /** @todo  This is where a 're-read on update' should be done */
290
291         return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
292 }
293
294 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
295 {
296         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
297
298         struct pdb_methods *pdb_selected;
299         if (!context) {
300                 DEBUG(0, ("invalid pdb_context specified!\n"));
301                 return ret;
302         }
303
304         if (!sam_acct->methods){
305                 pdb_selected = context->pdb_methods;
306                 /* There's no passdb backend specified for this account.
307                  * Try to delete it in every passdb available 
308                  * Needed to delete accounts in smbpasswd that are not
309                  * in /etc/passwd.
310                  */
311                 while (pdb_selected){
312                         if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
313                                 return ret;
314                         }
315                         pdb_selected = pdb_selected->next;
316                 }
317                 return ret;
318         }
319
320         if (!sam_acct->methods->delete_sam_account){
321                 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
322                 return ret;
323         }
324         
325         return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
326 }
327
328 static NTSTATUS context_rename_sam_account(struct pdb_context *context, SAM_ACCOUNT *oldname, const char *newname)
329 {
330         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
331
332         struct pdb_methods *pdb_selected;
333         if (!context) {
334                 DEBUG(0, ("invalid pdb_context specified!\n"));
335                 return ret;
336         }
337
338         if (!oldname->methods){
339                 pdb_selected = context->pdb_methods;
340                 /* There's no passdb backend specified for this account.
341                  * Try to delete it in every passdb available 
342                  * Needed to delete accounts in smbpasswd that are not
343                  * in /etc/passwd.
344                  */
345                 while (pdb_selected){
346                         if (NT_STATUS_IS_OK(ret = pdb_selected->rename_sam_account(pdb_selected, oldname, newname))) {
347                                 return ret;
348                         }
349                         pdb_selected = pdb_selected->next;
350                 }
351                 return ret;
352         }
353
354         if (!oldname->methods->rename_sam_account){
355                 DEBUG(0,("invalid oldname->methods->rename_sam_account\n"));
356                 return ret;
357         }
358         
359         return oldname->methods->rename_sam_account(oldname->methods, oldname, newname);
360 }
361
362
363 static NTSTATUS context_update_login_attempts(struct pdb_context *context,
364                                                 SAM_ACCOUNT *sam_acct, BOOL success)
365 {
366         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
367
368         if (!context) {
369                 DEBUG(0, ("invalid pdb_context specified!\n"));
370                 return ret;
371         }
372
373         if (!sam_acct || !sam_acct->methods){
374                 DEBUG(0, ("invalid sam_acct specified\n"));
375                 return ret;
376         }
377
378         return sam_acct->methods->update_login_attempts(sam_acct->methods, sam_acct, success);
379 }
380
381 static NTSTATUS context_getgrsid(struct pdb_context *context,
382                                  GROUP_MAP *map, DOM_SID sid)
383 {
384         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
385
386         struct pdb_methods *curmethods;
387         if ((!context)) {
388                 DEBUG(0, ("invalid pdb_context specified!\n"));
389                 return ret;
390         }
391         curmethods = context->pdb_methods;
392         while (curmethods){
393                 ret = curmethods->getgrsid(curmethods, map, sid);
394                 if (NT_STATUS_IS_OK(ret)) {
395                         map->methods = curmethods;
396                         return ret;
397                 }
398                 curmethods = curmethods->next;
399         }
400
401         return ret;
402 }
403
404 static NTSTATUS context_getgrgid(struct pdb_context *context,
405                                  GROUP_MAP *map, gid_t gid)
406 {
407         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
408
409         struct pdb_methods *curmethods;
410         if ((!context)) {
411                 DEBUG(0, ("invalid pdb_context specified!\n"));
412                 return ret;
413         }
414         curmethods = context->pdb_methods;
415         while (curmethods){
416                 ret = curmethods->getgrgid(curmethods, map, gid);
417                 if (NT_STATUS_IS_OK(ret)) {
418                         map->methods = curmethods;
419                         return ret;
420                 }
421                 curmethods = curmethods->next;
422         }
423
424         return ret;
425 }
426
427 static NTSTATUS context_getgrnam(struct pdb_context *context,
428                                  GROUP_MAP *map, const char *name)
429 {
430         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
431
432         struct pdb_methods *curmethods;
433         if ((!context)) {
434                 DEBUG(0, ("invalid pdb_context specified!\n"));
435                 return ret;
436         }
437         curmethods = context->pdb_methods;
438         while (curmethods){
439                 ret = curmethods->getgrnam(curmethods, map, name);
440                 if (NT_STATUS_IS_OK(ret)) {
441                         map->methods = curmethods;
442                         return ret;
443                 }
444                 curmethods = curmethods->next;
445         }
446
447         return ret;
448 }
449
450 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
451                                                 GROUP_MAP *map)
452 {
453         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
454
455         if ((!context) || (!context->pdb_methods)) {
456                 DEBUG(0, ("invalid pdb_context specified!\n"));
457                 return ret;
458         }
459
460         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
461                                                              map);
462 }
463
464 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
465                                                    GROUP_MAP *map)
466 {
467         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
468
469         if ((!context) || (!context->pdb_methods)) {
470                 DEBUG(0, ("invalid pdb_context specified!\n"));
471                 return ret;
472         }
473
474         return context->
475                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
476 }
477
478 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
479                                                    DOM_SID sid)
480 {
481         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
482
483         if ((!context) || (!context->pdb_methods)) {
484                 DEBUG(0, ("invalid pdb_context specified!\n"));
485                 return ret;
486         }
487
488         return context->
489                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
490 }
491
492 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
493                                            enum SID_NAME_USE sid_name_use,
494                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
495                                            BOOL unix_only)
496 {
497         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
498
499         if ((!context) || (!context->pdb_methods)) {
500                 DEBUG(0, ("invalid pdb_context specified!\n"));
501                 return ret;
502         }
503
504         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
505                                                         sid_name_use, pp_rmap,
506                                                         p_num_entries, unix_only);
507 }
508
509 static NTSTATUS context_enum_group_members(struct pdb_context *context,
510                                            TALLOC_CTX *mem_ctx,
511                                            const DOM_SID *group,
512                                            uint32 **pp_member_rids,
513                                            size_t *p_num_members)
514 {
515         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
516
517         if ((!context) || (!context->pdb_methods)) {
518                 DEBUG(0, ("invalid pdb_context specified!\n"));
519                 return ret;
520         }
521
522         return context->pdb_methods->enum_group_members(context->pdb_methods,
523                                                         mem_ctx, group,
524                                                         pp_member_rids,
525                                                         p_num_members);
526 }
527
528 static NTSTATUS context_enum_group_memberships(struct pdb_context *context,
529                                                const char *username,
530                                                gid_t primary_gid,
531                                                DOM_SID **pp_sids, gid_t **pp_gids,
532                                                size_t *p_num_groups)
533 {
534         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
535
536         if ((!context) || (!context->pdb_methods)) {
537                 DEBUG(0, ("invalid pdb_context specified!\n"));
538                 return ret;
539         }
540
541         return context->pdb_methods->
542                 enum_group_memberships(context->pdb_methods, username,
543                                        primary_gid, pp_sids, pp_gids, p_num_groups);
544 }
545
546 static NTSTATUS context_find_alias(struct pdb_context *context,
547                                    const char *name, DOM_SID *sid)
548 {
549         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
550
551         if ((!context) || (!context->pdb_methods)) {
552                 DEBUG(0, ("invalid pdb_context specified!\n"));
553                 return ret;
554         }
555
556         return context->pdb_methods->find_alias(context->pdb_methods,
557                                                 name, sid);
558 }
559
560 static NTSTATUS context_create_alias(struct pdb_context *context,
561                                      const char *name, uint32 *rid)
562 {
563         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
564
565         if ((!context) || (!context->pdb_methods)) {
566                 DEBUG(0, ("invalid pdb_context specified!\n"));
567                 return ret;
568         }
569
570         return context->pdb_methods->create_alias(context->pdb_methods,
571                                                   name, rid);
572 }
573
574 static NTSTATUS context_delete_alias(struct pdb_context *context,
575                                      const DOM_SID *sid)
576 {
577         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
578
579         if ((!context) || (!context->pdb_methods)) {
580                 DEBUG(0, ("invalid pdb_context specified!\n"));
581                 return ret;
582         }
583
584         return context->pdb_methods->delete_alias(context->pdb_methods, sid);
585 }
586
587 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
588                                       const DOM_SID *sid,
589                                       struct acct_info *info)
590 {
591         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
592
593         if ((!context) || (!context->pdb_methods)) {
594                 DEBUG(0, ("invalid pdb_context specified!\n"));
595                 return ret;
596         }
597
598         return context->pdb_methods->get_aliasinfo(context->pdb_methods,
599                                                    sid, info);
600 }
601
602 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
603                                       const DOM_SID *sid,
604                                       struct acct_info *info)
605 {
606         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
607
608         if ((!context) || (!context->pdb_methods)) {
609                 DEBUG(0, ("invalid pdb_context specified!\n"));
610                 return ret;
611         }
612
613         return context->pdb_methods->set_aliasinfo(context->pdb_methods,
614                                                    sid, info);
615 }
616
617 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
618                                      const DOM_SID *alias,
619                                      const DOM_SID *member)
620 {
621         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
622
623         if ((!context) || (!context->pdb_methods)) {
624                 DEBUG(0, ("invalid pdb_context specified!\n"));
625                 return ret;
626         }
627
628         return context->pdb_methods->add_aliasmem(context->pdb_methods,
629                                                   alias, member);
630 }
631         
632 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
633                                      const DOM_SID *alias,
634                                      const DOM_SID *member)
635 {
636         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
637
638         if ((!context) || (!context->pdb_methods)) {
639                 DEBUG(0, ("invalid pdb_context specified!\n"));
640                 return ret;
641         }
642
643         return context->pdb_methods->del_aliasmem(context->pdb_methods,
644                                                   alias, member);
645 }
646         
647 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
648                                       const DOM_SID *alias, DOM_SID **pp_members,
649                                       size_t *p_num)
650 {
651         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
652
653         if ((!context) || (!context->pdb_methods)) {
654                 DEBUG(0, ("invalid pdb_context specified!\n"));
655                 return ret;
656         }
657
658         return context->pdb_methods->enum_aliasmem(context->pdb_methods,
659                                                    alias, pp_members, p_num);
660 }
661         
662 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
663                                                TALLOC_CTX *mem_ctx,
664                                                const DOM_SID *domain_sid,
665                                                const DOM_SID *members,
666                                                size_t num_members,
667                                                uint32 **pp_alias_rids,
668                                                size_t *p_num_alias_rids)
669 {
670         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
671
672         if ((!context) || (!context->pdb_methods)) {
673                 DEBUG(0, ("invalid pdb_context specified!\n"));
674                 return ret;
675         }
676
677         return context->pdb_methods->
678                 enum_alias_memberships(context->pdb_methods, mem_ctx,
679                                        domain_sid, members, num_members,
680                                        pp_alias_rids, p_num_alias_rids);
681 }
682
683 static NTSTATUS context_lookup_rids(struct pdb_context *context,
684                                     const DOM_SID *domain_sid,
685                                     size_t num_rids,
686                                     uint32 *rids,
687                                     const char **pp_names,
688                                     uint32 *pp_attrs)
689 {
690         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
691
692         if ((!context) || (!context->pdb_methods)) {
693                 DEBUG(0, ("invalid pdb_context specified!\n"));
694                 return ret;
695         }
696
697         return context->pdb_methods->lookup_rids(context->pdb_methods,
698                                                  domain_sid, num_rids,
699                                                  rids, pp_names, pp_attrs);
700 }
701
702 static NTSTATUS context_lookup_names(struct pdb_context *context,
703                                      const DOM_SID *domain_sid,
704                                      size_t num_names,
705                                      const char **pp_names,
706                                      uint32 *rids,
707                                      uint32 *pp_attrs)
708 {
709         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
710
711         if ((!context) || (!context->pdb_methods)) {
712                 DEBUG(0, ("invalid pdb_context specified!\n"));
713                 return ret;
714         }
715
716         return context->pdb_methods->lookup_names(context->pdb_methods,
717                                                   domain_sid, num_names,
718                                                   pp_names, rids, pp_attrs);
719 }
720
721 static NTSTATUS context_get_account_policy(struct pdb_context *context,
722                                            int policy_index, uint32 *value)
723 {
724         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
725
726         if ((!context) || (!context->pdb_methods)) {
727                 DEBUG(0, ("invalid pdb_context specified!\n"));
728                 return ret;
729         }
730
731         return context->pdb_methods->get_account_policy(context->pdb_methods,
732                                                         policy_index, value);
733 }
734
735 static NTSTATUS context_set_account_policy(struct pdb_context *context,
736                                            int policy_index, uint32 value)
737 {
738         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
739
740         if ((!context) || (!context->pdb_methods)) {
741                 DEBUG(0, ("invalid pdb_context specified!\n"));
742                 return ret;
743         }
744
745         return context->pdb_methods->set_account_policy(context->pdb_methods,
746                                                         policy_index, value);
747 }
748
749 static NTSTATUS context_get_seq_num(struct pdb_context *context, time_t *seq_num)
750 {
751         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
752
753         if ((!context) || (!context->pdb_methods)) {
754                 DEBUG(0, ("invalid pdb_context specified!\n"));
755                 return ret;
756         }
757
758         return context->pdb_methods->get_seq_num(context->pdb_methods, seq_num);
759 }
760         
761 /******************************************************************
762   Free and cleanup a pdb context, any associated data and anything
763   that the attached modules might have associated.
764  *******************************************************************/
765
766 static void free_pdb_context(struct pdb_context **context)
767 {
768         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
769
770         while (pdb_selected){
771                 if(pdb_selected->free_private_data)
772                         pdb_selected->free_private_data(&(pdb_selected->private_data));
773                 pdb_selected = pdb_selected->next;
774         }
775
776         talloc_destroy((*context)->mem_ctx);
777         *context = NULL;
778 }
779
780 static BOOL context_search_users(struct pdb_context *context,
781                                  struct pdb_search *search, uint16 acct_flags)
782 {
783         if ((!context) || (!context->pdb_methods)) {
784                 DEBUG(0, ("invalid pdb_context specified!\n"));
785                 return False;
786         }
787
788         return context->pdb_methods->search_users(context->pdb_methods,
789                                                   search, acct_flags);
790 }
791
792 static BOOL context_search_groups(struct pdb_context *context,
793                                   struct pdb_search *search)
794 {
795         if ((!context) || (!context->pdb_methods)) {
796                 DEBUG(0, ("invalid pdb_context specified!\n"));
797                 return False;
798         }
799
800         return context->pdb_methods->search_groups(context->pdb_methods,
801                                                    search);
802 }
803
804 static BOOL context_search_aliases(struct pdb_context *context,
805                                    struct pdb_search *search,
806                                    const DOM_SID *sid)
807 {
808         if ((!context) || (!context->pdb_methods)) {
809                 DEBUG(0, ("invalid pdb_context specified!\n"));
810                 return False;
811         }
812
813         return context->pdb_methods->search_aliases(context->pdb_methods,
814                                                     search, sid);
815 }
816
817 /******************************************************************
818   Make a pdb_methods from scratch
819  *******************************************************************/
820
821 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
822 {
823         char *module_name = smb_xstrdup(selected);
824         char *module_location = NULL, *p;
825         struct pdb_init_function_entry *entry;
826         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
827
828         lazy_initialize_passdb();
829
830         p = strchr(module_name, ':');
831
832         if (p) {
833                 *p = 0;
834                 module_location = p+1;
835                 trim_char(module_location, ' ', ' ');
836         }
837
838         trim_char(module_name, ' ', ' ');
839
840
841         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
842
843         entry = pdb_find_backend_entry(module_name);
844         
845         /* Try to find a module that contains this module */
846         if (!entry) { 
847                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
848                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
849                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
850                         SAFE_FREE(module_name);
851                         return NT_STATUS_UNSUCCESSFUL;
852                 }
853         }
854         
855         /* No such backend found */
856         if(!entry) { 
857                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
858                 SAFE_FREE(module_name);
859                 return NT_STATUS_INVALID_PARAMETER;
860         }
861
862         DEBUG(5,("Found pdb backend %s\n", module_name));
863         nt_status = entry->init(context, methods, module_location);
864         if (NT_STATUS_IS_OK(nt_status)) {
865                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
866         } else {
867                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
868         }
869         SAFE_FREE(module_name);
870         return nt_status;
871 }
872
873 /******************************************************************
874   Make a pdb_context from scratch.
875  *******************************************************************/
876
877 static NTSTATUS make_pdb_context(struct pdb_context **context) 
878 {
879         TALLOC_CTX *mem_ctx;
880
881         mem_ctx = talloc_init("pdb_context internal allocation context");
882
883         if (!mem_ctx) {
884                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
885                 return NT_STATUS_NO_MEMORY;
886         }               
887
888         *context = TALLOC_P(mem_ctx, struct pdb_context);
889         if (!*context) {
890                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
891                 return NT_STATUS_NO_MEMORY;
892         }
893
894         ZERO_STRUCTP(*context);
895
896         (*context)->mem_ctx = mem_ctx;
897
898         (*context)->pdb_setsampwent = context_setsampwent;
899         (*context)->pdb_endsampwent = context_endsampwent;
900         (*context)->pdb_getsampwent = context_getsampwent;
901         (*context)->pdb_getsampwnam = context_getsampwnam;
902         (*context)->pdb_getsampwsid = context_getsampwsid;
903         (*context)->pdb_add_sam_account = context_add_sam_account;
904         (*context)->pdb_update_sam_account = context_update_sam_account;
905         (*context)->pdb_delete_sam_account = context_delete_sam_account;
906         (*context)->pdb_rename_sam_account = context_rename_sam_account;
907         (*context)->pdb_update_login_attempts = context_update_login_attempts;
908         (*context)->pdb_getgrsid = context_getgrsid;
909         (*context)->pdb_getgrgid = context_getgrgid;
910         (*context)->pdb_getgrnam = context_getgrnam;
911         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
912         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
913         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
914         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
915         (*context)->pdb_enum_group_members = context_enum_group_members;
916         (*context)->pdb_enum_group_memberships = context_enum_group_memberships;
917
918         (*context)->pdb_find_alias = context_find_alias;
919         (*context)->pdb_create_alias = context_create_alias;
920         (*context)->pdb_delete_alias = context_delete_alias;
921         (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
922         (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
923         (*context)->pdb_add_aliasmem = context_add_aliasmem;
924         (*context)->pdb_del_aliasmem = context_del_aliasmem;
925         (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
926         (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
927         (*context)->pdb_lookup_rids = context_lookup_rids;
928         (*context)->pdb_lookup_names = context_lookup_names;
929
930         (*context)->pdb_get_account_policy = context_get_account_policy;
931         (*context)->pdb_set_account_policy = context_set_account_policy;
932
933         (*context)->pdb_get_seq_num = context_get_seq_num;
934
935         (*context)->pdb_search_users = context_search_users;
936         (*context)->pdb_search_groups = context_search_groups;
937         (*context)->pdb_search_aliases = context_search_aliases;
938
939         (*context)->free_fn = free_pdb_context;
940
941         return NT_STATUS_OK;
942 }
943
944
945 /******************************************************************
946   Make a pdb_context, given an array of strings
947  *******************************************************************/
948
949 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
950 {
951         int i = 0;
952         struct pdb_methods *curmethods, *tmpmethods;
953         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
954         BOOL have_guest = False;
955
956         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
957                 return nt_status;
958         }
959
960         if (!selected) {
961                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
962                 return nt_status;
963         }
964
965         while (selected[i]){
966                 if (strcmp(selected[i], "guest") == 0) {
967                         have_guest = True;
968                 }
969                 /* Try to initialise pdb */
970                 DEBUG(5,("Trying to load: %s\n", selected[i]));
971                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
972                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
973                         free_pdb_context(context);
974                         return nt_status;
975                 }
976                 curmethods->parent = *context;
977                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
978                 i++;
979         }
980
981         if (have_guest)
982                 return NT_STATUS_OK;
983
984         if ( (lp_guestaccount() == NULL) ||
985              (*lp_guestaccount() == '\0') ) {
986                 /* We explicitly don't want guest access. No idea what
987                    else that breaks, but be it that way. */
988                 return NT_STATUS_OK;
989         }
990
991         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
992                                                                *context,
993                                                                "guest"))) {
994                 DEBUG(1, ("Loading guest module failed!\n"));
995                 free_pdb_context(context);
996                 return nt_status;
997         }
998
999         curmethods->parent = *context;
1000         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
1001         
1002         return NT_STATUS_OK;
1003 }
1004
1005 /******************************************************************
1006   Make a pdb_context, given a text string.
1007  *******************************************************************/
1008
1009 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
1010 {
1011         NTSTATUS ret;
1012         char **newsel = str_list_make(selected, NULL);
1013         ret = make_pdb_context_list(context, (const char **)newsel);
1014         str_list_free(&newsel);
1015         return ret;
1016 }
1017
1018 /******************************************************************
1019  Return an already initialised pdb_context, to facilitate backward 
1020  compatibility (see functions below).
1021 *******************************************************************/
1022
1023 static struct pdb_context *pdb_get_static_context(BOOL reload) 
1024 {
1025         static struct pdb_context *pdb_context = NULL;
1026
1027         if ((pdb_context) && (reload)) {
1028                 pdb_context->free_fn(&pdb_context);
1029                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1030                         return NULL;
1031                 }
1032         }
1033
1034         if (!pdb_context) {
1035                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1036                         return NULL;
1037                 }
1038         }
1039
1040         return pdb_context;
1041 }
1042
1043 /******************************************************************
1044  Backward compatibility functions for the original passdb interface
1045 *******************************************************************/
1046
1047 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask) 
1048 {
1049         struct pdb_context *pdb_context = pdb_get_static_context(False);
1050
1051         if (!pdb_context) {
1052                 return False;
1053         }
1054
1055         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update, acb_mask));
1056 }
1057
1058 void pdb_endsampwent(void) 
1059 {
1060         struct pdb_context *pdb_context = pdb_get_static_context(False);
1061
1062         if (!pdb_context) {
1063                 return;
1064         }
1065
1066         pdb_context->pdb_endsampwent(pdb_context);
1067 }
1068
1069 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
1070 {
1071         struct pdb_context *pdb_context = pdb_get_static_context(False);
1072
1073         if (!pdb_context) {
1074                 return False;
1075         }
1076
1077         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
1078 }
1079
1080 static SAM_ACCOUNT *sam_account_cache = NULL;
1081
1082 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
1083 {
1084         struct pdb_context *pdb_context = pdb_get_static_context(False);
1085
1086         if (!pdb_context) {
1087                 return False;
1088         }
1089
1090         if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
1091                                                           sam_acct, username)))
1092                 return False;
1093
1094         if (sam_account_cache != NULL) {
1095                 pdb_free_sam(&sam_account_cache);
1096                 sam_account_cache = NULL;
1097         }
1098
1099         pdb_copy_sam_account(sam_acct, &sam_account_cache);
1100         return True;
1101 }
1102
1103 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
1104 {
1105         struct pdb_context *pdb_context = pdb_get_static_context(False);
1106
1107         if (!pdb_context) {
1108                 return False;
1109         }
1110
1111         if ((sam_account_cache != NULL) &&
1112             (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
1113                 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
1114
1115         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1116 }
1117
1118 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
1119 {
1120         struct pdb_context *pdb_context = pdb_get_static_context(False);
1121
1122         if (!pdb_context) {
1123                 return False;
1124         }
1125         
1126         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1127 }
1128
1129 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
1130 {
1131         struct pdb_context *pdb_context = pdb_get_static_context(False);
1132
1133         if (!pdb_context) {
1134                 return False;
1135         }
1136
1137         if (sam_account_cache != NULL) {
1138                 pdb_free_sam(&sam_account_cache);
1139                 sam_account_cache = NULL;
1140         }
1141
1142         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1143 }
1144
1145 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
1146 {
1147         struct pdb_context *pdb_context = pdb_get_static_context(False);
1148
1149         if (!pdb_context) {
1150                 return False;
1151         }
1152
1153         if (sam_account_cache != NULL) {
1154                 pdb_free_sam(&sam_account_cache);
1155                 sam_account_cache = NULL;
1156         }
1157
1158         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1159 }
1160
1161 NTSTATUS pdb_rename_sam_account(SAM_ACCOUNT *oldname, const char *newname)
1162 {
1163         struct pdb_context *pdb_context = pdb_get_static_context(False);
1164
1165         if (!pdb_context) {
1166                 return NT_STATUS_NOT_IMPLEMENTED;
1167         }
1168
1169         if (sam_account_cache != NULL) {
1170                 pdb_free_sam(&sam_account_cache);
1171                 sam_account_cache = NULL;
1172         }
1173
1174         return pdb_context->pdb_rename_sam_account(pdb_context, oldname, newname);
1175 }
1176
1177 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
1178 {
1179         struct pdb_context *pdb_context = pdb_get_static_context(False);
1180
1181         if (!pdb_context) {
1182                 return NT_STATUS_NOT_IMPLEMENTED;
1183         }
1184
1185         return pdb_context->pdb_update_login_attempts(pdb_context, sam_acct, success);
1186 }
1187
1188 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1189 {
1190         struct pdb_context *pdb_context = pdb_get_static_context(False);
1191
1192         if (!pdb_context) {
1193                 return False;
1194         }
1195
1196         return NT_STATUS_IS_OK(pdb_context->
1197                                pdb_getgrsid(pdb_context, map, sid));
1198 }
1199
1200 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1201 {
1202         struct pdb_context *pdb_context = pdb_get_static_context(False);
1203
1204         if (!pdb_context) {
1205                 return False;
1206         }
1207
1208         return NT_STATUS_IS_OK(pdb_context->
1209                                pdb_getgrgid(pdb_context, map, gid));
1210 }
1211
1212 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1213 {
1214         struct pdb_context *pdb_context = pdb_get_static_context(False);
1215
1216         if (!pdb_context) {
1217                 return False;
1218         }
1219
1220         return NT_STATUS_IS_OK(pdb_context->
1221                                pdb_getgrnam(pdb_context, map, name));
1222 }
1223
1224 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1225 {
1226         struct pdb_context *pdb_context = pdb_get_static_context(False);
1227
1228         if (!pdb_context) {
1229                 return False;
1230         }
1231
1232         return NT_STATUS_IS_OK(pdb_context->
1233                                pdb_add_group_mapping_entry(pdb_context, map));
1234 }
1235
1236 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1237 {
1238         struct pdb_context *pdb_context = pdb_get_static_context(False);
1239
1240         if (!pdb_context) {
1241                 return False;
1242         }
1243
1244         return NT_STATUS_IS_OK(pdb_context->
1245                                pdb_update_group_mapping_entry(pdb_context, map));
1246 }
1247
1248 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1249 {
1250         struct pdb_context *pdb_context = pdb_get_static_context(False);
1251
1252         if (!pdb_context) {
1253                 return False;
1254         }
1255
1256         return NT_STATUS_IS_OK(pdb_context->
1257                                pdb_delete_group_mapping_entry(pdb_context, sid));
1258 }
1259
1260 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
1261                             size_t *p_num_entries, BOOL unix_only)
1262 {
1263         struct pdb_context *pdb_context = pdb_get_static_context(False);
1264
1265         if (!pdb_context) {
1266                 return False;
1267         }
1268
1269         return NT_STATUS_IS_OK(pdb_context->
1270                                pdb_enum_group_mapping(pdb_context, sid_name_use,
1271                                                       pp_rmap, p_num_entries, unix_only));
1272 }
1273
1274 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
1275                                 const DOM_SID *sid,
1276                                 uint32 **pp_member_rids,
1277                                 size_t *p_num_members)
1278 {
1279         struct pdb_context *pdb_context = pdb_get_static_context(False);
1280
1281         if (!pdb_context) {
1282                 return NT_STATUS_UNSUCCESSFUL;
1283         }
1284
1285         return pdb_context->pdb_enum_group_members(pdb_context, mem_ctx, sid, 
1286                                                    pp_member_rids, p_num_members);
1287 }
1288
1289 NTSTATUS pdb_enum_group_memberships(const char *username, gid_t primary_gid,
1290                                     DOM_SID **pp_sids, gid_t **pp_gids,
1291                                     size_t *p_num_groups)
1292 {
1293         struct pdb_context *pdb_context = pdb_get_static_context(False);
1294
1295         if (!pdb_context) {
1296                 return NT_STATUS_UNSUCCESSFUL;
1297         }
1298
1299         return pdb_context->pdb_enum_group_memberships(pdb_context, username,
1300                                                        primary_gid, pp_sids, pp_gids,
1301                                                        p_num_groups);
1302 }
1303
1304 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1305 {
1306         struct pdb_context *pdb_context = pdb_get_static_context(False);
1307
1308         if (!pdb_context) {
1309                 return False;
1310         }
1311
1312         return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1313                                                              name, sid));
1314 }
1315
1316 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1317 {
1318         struct pdb_context *pdb_context = pdb_get_static_context(False);
1319
1320         if (!pdb_context) {
1321                 return NT_STATUS_NOT_IMPLEMENTED;
1322         }
1323
1324         return pdb_context->pdb_create_alias(pdb_context, name, rid);
1325 }
1326
1327 BOOL pdb_delete_alias(const DOM_SID *sid)
1328 {
1329         struct pdb_context *pdb_context = pdb_get_static_context(False);
1330
1331         if (!pdb_context) {
1332                 return False;
1333         }
1334
1335         return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1336                                                              sid));
1337                                                             
1338 }
1339
1340 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1341 {
1342         struct pdb_context *pdb_context = pdb_get_static_context(False);
1343
1344         if (!pdb_context) {
1345                 return False;
1346         }
1347
1348         return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1349                                                               info));
1350 }
1351
1352 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1353 {
1354         struct pdb_context *pdb_context = pdb_get_static_context(False);
1355
1356         if (!pdb_context) {
1357                 return False;
1358         }
1359
1360         return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1361                                                               info));
1362 }
1363
1364 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1365 {
1366         struct pdb_context *pdb_context = pdb_get_static_context(False);
1367
1368         if (!pdb_context) {
1369                 return False;
1370         }
1371
1372         return NT_STATUS_IS_OK(pdb_context->
1373                                pdb_add_aliasmem(pdb_context, alias, member));
1374 }
1375
1376 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1377 {
1378         struct pdb_context *pdb_context = pdb_get_static_context(False);
1379
1380         if (!pdb_context) {
1381                 return False;
1382         }
1383
1384         return NT_STATUS_IS_OK(pdb_context->
1385                                pdb_del_aliasmem(pdb_context, alias, member));
1386 }
1387
1388 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1389                        DOM_SID **pp_members, size_t *p_num_members)
1390 {
1391         struct pdb_context *pdb_context = pdb_get_static_context(False);
1392
1393         if (!pdb_context) {
1394                 return False;
1395         }
1396
1397         return NT_STATUS_IS_OK(pdb_context->
1398                                pdb_enum_aliasmem(pdb_context, alias,
1399                                                  pp_members, p_num_members));
1400 }
1401
1402 BOOL pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
1403                                 const DOM_SID *members, size_t num_members,
1404                                 uint32 **pp_alias_rids, size_t *p_num_alias_rids)
1405 {
1406         struct pdb_context *pdb_context = pdb_get_static_context(False);
1407
1408         if (!pdb_context) {
1409                 return False;
1410         }
1411
1412         return NT_STATUS_IS_OK(pdb_context->
1413                                pdb_enum_alias_memberships(pdb_context, mem_ctx,
1414                                                           domain_sid,
1415                                                           members, num_members,
1416                                                           pp_alias_rids,
1417                                                           p_num_alias_rids));
1418 }
1419
1420 NTSTATUS pdb_lookup_rids(const DOM_SID *domain_sid,
1421                          int num_rids,
1422                          uint32 *rids,
1423                          const char **names,
1424                          uint32 *attrs)
1425 {
1426         struct pdb_context *pdb_context = pdb_get_static_context(False);
1427
1428         if (!pdb_context) {
1429                 return NT_STATUS_NOT_IMPLEMENTED;
1430         }
1431
1432         return pdb_context->pdb_lookup_rids(pdb_context, domain_sid,
1433                                             num_rids, rids, names, attrs);
1434 }
1435
1436 NTSTATUS pdb_lookup_names(const DOM_SID *domain_sid,
1437                           int num_names,
1438                           const char **names,
1439                           uint32 *rids,
1440                           uint32 *attrs)
1441 {
1442         struct pdb_context *pdb_context = pdb_get_static_context(False);
1443
1444         if (!pdb_context) {
1445                 return NT_STATUS_NOT_IMPLEMENTED;
1446         }
1447
1448         return pdb_context->pdb_lookup_names(pdb_context, domain_sid,
1449                                              num_names, names, rids, attrs);
1450 }
1451
1452 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1453 {
1454         struct pdb_context *pdb_context = pdb_get_static_context(False);
1455
1456         if (!pdb_context) {
1457                 return False;
1458         }
1459
1460         return NT_STATUS_IS_OK(pdb_context->
1461                                pdb_get_account_policy(pdb_context, policy_index, value));
1462 }
1463
1464 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1465 {
1466         struct pdb_context *pdb_context = pdb_get_static_context(False);
1467
1468         if (!pdb_context) {
1469                 return False;
1470         }
1471
1472         return NT_STATUS_IS_OK(pdb_context->
1473                                pdb_set_account_policy(pdb_context, policy_index, value));
1474 }
1475
1476 BOOL pdb_get_seq_num(time_t *seq_num)
1477 {
1478         struct pdb_context *pdb_context = pdb_get_static_context(False);
1479
1480         if (!pdb_context) {
1481                 return False;
1482         }
1483
1484         return NT_STATUS_IS_OK(pdb_context->
1485                                pdb_get_seq_num(pdb_context, seq_num));
1486 }
1487 /***************************************************************
1488   Initialize the static context (at smbd startup etc). 
1489
1490   If uninitialised, context will auto-init on first use.
1491  ***************************************************************/
1492
1493 BOOL initialize_password_db(BOOL reload)
1494 {       
1495         return (pdb_get_static_context(reload) != NULL);
1496 }
1497
1498
1499 /***************************************************************************
1500   Default implementations of some functions.
1501  ****************************************************************************/
1502
1503 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1504 {
1505         return NT_STATUS_NO_SUCH_USER;
1506 }
1507
1508 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1509 {
1510         return NT_STATUS_NO_SUCH_USER;
1511 }
1512
1513 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1514 {
1515         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1516         return NT_STATUS_NOT_IMPLEMENTED;
1517 }
1518
1519 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1520 {
1521         return NT_STATUS_NOT_IMPLEMENTED;
1522 }
1523
1524 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1525 {
1526         return NT_STATUS_NOT_IMPLEMENTED;
1527 }
1528
1529 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd, const char *newname)
1530 {
1531         return NT_STATUS_NOT_IMPLEMENTED;
1532 }
1533
1534 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1535 {
1536         return NT_STATUS_OK;
1537 }
1538
1539 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1540 {
1541         return NT_STATUS_NOT_IMPLEMENTED;
1542 }
1543
1544 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1545 {
1546         return NT_STATUS_NOT_IMPLEMENTED;
1547 }
1548
1549 static void pdb_default_endsampwent(struct pdb_methods *methods)
1550 {
1551         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1552 }
1553
1554 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1555 {
1556         return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1557 }
1558
1559 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1560 {
1561         return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1562 }
1563
1564 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1565 {
1566         *seq_num = time(NULL);
1567         return NT_STATUS_OK;
1568 }
1569
1570 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1571                                     uid_t uid, uid_t **pp_uids, size_t *p_num)
1572 {
1573         size_t i;
1574
1575         for (i=0; i<*p_num; i++) {
1576                 if ((*pp_uids)[i] == uid)
1577                         return;
1578         }
1579         
1580         *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1581
1582         if (*pp_uids == NULL)
1583                 return;
1584
1585         (*pp_uids)[*p_num] = uid;
1586         *p_num += 1;
1587 }
1588
1589 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1590 {
1591         struct group *grp;
1592         char **gr;
1593         struct sys_pwent *userlist, *user;
1594  
1595         *pp_uids = NULL;
1596         *p_num = 0;
1597
1598         /* We only look at our own sam, so don't care about imported stuff */
1599
1600         winbind_off();
1601
1602         if ((grp = getgrgid(gid)) == NULL) {
1603                 winbind_on();
1604                 return False;
1605         }
1606
1607         /* Primary group members */
1608
1609         userlist = getpwent_list();
1610
1611         for (user = userlist; user != NULL; user = user->next) {
1612                 if (user->pw_gid != gid)
1613                         continue;
1614                 add_uid_to_array_unique(mem_ctx, user->pw_uid, pp_uids, p_num);
1615         }
1616
1617         pwent_free(userlist);
1618
1619         /* Secondary group members */
1620
1621         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1622                 struct passwd *pw = getpwnam(*gr);
1623
1624                 if (pw == NULL)
1625                         continue;
1626                 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1627         }
1628
1629         winbind_on();
1630
1631         return True;
1632 }
1633
1634 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1635                                         TALLOC_CTX *mem_ctx,
1636                                         const DOM_SID *group,
1637                                         uint32 **pp_member_rids,
1638                                         size_t *p_num_members)
1639 {
1640         gid_t gid;
1641         uid_t *uids;
1642         size_t i, num_uids;
1643
1644         *pp_member_rids = NULL;
1645         *p_num_members = 0;
1646
1647         if (!NT_STATUS_IS_OK(sid_to_gid(group, &gid)))
1648                 return NT_STATUS_NO_SUCH_GROUP;
1649
1650         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1651                 return NT_STATUS_NO_SUCH_GROUP;
1652
1653         if (num_uids == 0)
1654                 return NT_STATUS_OK;
1655
1656         *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1657
1658         for (i=0; i<num_uids; i++) {
1659                 DOM_SID sid;
1660
1661                 if (!NT_STATUS_IS_OK(uid_to_sid(&sid, uids[i]))) {
1662                         DEBUG(1, ("Could not map member uid to SID\n"));
1663                         continue;
1664                 }
1665
1666                 if (!sid_check_is_in_our_domain(&sid)) {
1667                         DEBUG(1, ("Inconsistent SAM -- group member uid not "
1668                                   "in our domain\n"));
1669                         continue;
1670                 }
1671
1672                 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1673                 *p_num_members += 1;
1674         }
1675
1676         return NT_STATUS_OK;
1677 }
1678
1679 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1680                                  const DOM_SID *domain_sid,
1681                                  int num_rids,
1682                                  uint32 *rids,
1683                                  const char **names,
1684                                  uint32 *attrs)
1685 {
1686         int i;
1687         NTSTATUS result;
1688         BOOL have_mapped = False;
1689         BOOL have_unmapped = False;
1690
1691         if (sid_check_is_builtin(domain_sid)) {
1692
1693                 for (i=0; i<num_rids; i++) {
1694                         const char *name;
1695
1696                         if (lookup_builtin_rid(names, rids[i], &name)) {
1697                                 attrs[i] = SID_NAME_ALIAS;
1698                                 names[i] = name;
1699                                 DEBUG(5,("lookup_rids: %s:%d\n",
1700                                          names[i], attrs[i]));
1701                                 have_mapped = True;
1702                         } else {
1703                                 have_unmapped = True;
1704                                 attrs[i] = SID_NAME_UNKNOWN;
1705                         }
1706                 }
1707                 goto done;
1708         }
1709
1710         /* Should not happen, but better check once too many */
1711         if (!sid_check_is_domain(domain_sid)) {
1712                 return NT_STATUS_INVALID_HANDLE;
1713         }
1714
1715         for (i = 0; i < num_rids; i++) {
1716                 const char *name;
1717
1718                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i])) {
1719                         names[i] = name;
1720                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1721                         have_mapped = True;
1722                 } else {
1723                         have_unmapped = True;
1724                         attrs[i] = SID_NAME_UNKNOWN;
1725                 }
1726         }
1727
1728  done:
1729
1730         result = NT_STATUS_NONE_MAPPED;
1731
1732         if (have_mapped)
1733                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1734
1735         return result;
1736 }
1737
1738 NTSTATUS pdb_default_lookup_names(struct pdb_methods *methods,
1739                                   const DOM_SID *domain_sid,
1740                                   int num_names,
1741                                   const char **names,
1742                                   uint32 *rids,
1743                                   uint32 *attrs)
1744 {
1745         int i;
1746         NTSTATUS result;
1747         BOOL have_mapped = False;
1748         BOOL have_unmapped = False;
1749
1750         if (sid_check_is_builtin(domain_sid)) {
1751
1752                 for (i=0; i<num_names; i++) {
1753                         uint32 rid;
1754
1755                         if (lookup_builtin_name(names[i], &rid)) {
1756                                 attrs[i] = SID_NAME_ALIAS;
1757                                 rids[i] = rid;
1758                                 DEBUG(5,("lookup_rids: %s:%d\n",
1759                                          names[i], attrs[i]));
1760                                 have_mapped = True;
1761                         } else {
1762                                 have_unmapped = True;
1763                                 attrs[i] = SID_NAME_UNKNOWN;
1764                         }
1765                 }
1766                 goto done;
1767         }
1768
1769         /* Should not happen, but better check once too many */
1770         if (!sid_check_is_domain(domain_sid)) {
1771                 return NT_STATUS_INVALID_HANDLE;
1772         }
1773
1774         for (i = 0; i < num_names; i++) {
1775                 const char *name;
1776
1777                 if (lookup_global_sam_rid(names, rids[i], &name, &attrs[i])) {
1778                         names[i] = name;
1779                         DEBUG(5,("lookup_rids: %s:%d\n", names[i], attrs[i]));
1780                         have_mapped = True;
1781                 } else {
1782                         have_unmapped = True;
1783                         attrs[i] = SID_NAME_UNKNOWN;
1784                 }
1785         }
1786
1787  done:
1788
1789         result = NT_STATUS_NONE_MAPPED;
1790
1791         if (have_mapped)
1792                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1793
1794         return result;
1795 }
1796
1797 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1798 {
1799         TALLOC_CTX *mem_ctx;
1800         struct pdb_search *result;
1801
1802         mem_ctx = talloc_init("pdb_search");
1803         if (mem_ctx == NULL) {
1804                 DEBUG(0, ("talloc_init failed\n"));
1805                 return NULL;
1806         }
1807
1808         result = TALLOC_P(mem_ctx, struct pdb_search);
1809         if (result == NULL) {
1810                 DEBUG(0, ("talloc failed\n"));
1811                 return NULL;
1812         }
1813
1814         result->mem_ctx = mem_ctx;
1815         result->type = type;
1816         result->cache = NULL;
1817         result->num_entries = 0;
1818         result->cache_size = 0;
1819         result->search_ended = False;
1820
1821         /* Segfault appropriately if not initialized */
1822         result->next_entry = NULL;
1823         result->search_end = NULL;
1824
1825         return result;
1826 }
1827
1828 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1829                               uint16 acct_flags,
1830                               const char *account_name,
1831                               const char *fullname,
1832                               const char *description,
1833                               struct samr_displayentry *entry)
1834 {
1835         entry->rid = rid;
1836         entry->acct_flags = acct_flags;
1837
1838         if (account_name != NULL)
1839                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1840         else
1841                 entry->account_name = "";
1842
1843         if (fullname != NULL)
1844                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1845         else
1846                 entry->fullname = "";
1847
1848         if (description != NULL)
1849                 entry->description = talloc_strdup(mem_ctx, description);
1850         else
1851                 entry->description = "";
1852 }
1853
1854 static BOOL user_search_in_progress = False;
1855 struct user_search {
1856         uint16 acct_flags;
1857 };
1858
1859 static BOOL next_entry_users(struct pdb_search *s,
1860                              struct samr_displayentry *entry)
1861 {
1862         struct user_search *state = s->private_data;
1863         SAM_ACCOUNT *user = NULL;
1864         NTSTATUS status;
1865
1866  next:
1867         status = pdb_init_sam(&user);
1868         if (!NT_STATUS_IS_OK(status)) {
1869                 DEBUG(0, ("Could not pdb_init_sam\n"));
1870                 return False;
1871         }
1872
1873         if (!pdb_getsampwent(user)) {
1874                 pdb_free_sam(&user);
1875                 return False;
1876         }
1877
1878         if ((state->acct_flags != 0) &&
1879             ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1880                 pdb_free_sam(&user);
1881                 goto next;
1882         }
1883
1884         fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1885                           pdb_get_acct_ctrl(user), pdb_get_username(user),
1886                           pdb_get_fullname(user), pdb_get_acct_desc(user),
1887                           entry);
1888
1889         pdb_free_sam(&user);
1890         return True;
1891 }
1892
1893 static void search_end_users(struct pdb_search *search)
1894 {
1895         pdb_endsampwent();
1896         user_search_in_progress = False;
1897 }
1898
1899 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1900                                      struct pdb_search *search,
1901                                      uint16 acct_flags)
1902 {
1903         struct user_search *state;
1904
1905         if (user_search_in_progress) {
1906                 DEBUG(1, ("user search in progress\n"));
1907                 return False;
1908         }
1909
1910         if (!pdb_setsampwent(False, acct_flags)) {
1911                 DEBUG(5, ("Could not start search\n"));
1912                 return False;
1913         }
1914
1915         user_search_in_progress = True;
1916
1917         state = TALLOC_P(search->mem_ctx, struct user_search);
1918         if (state == NULL) {
1919                 DEBUG(0, ("talloc failed\n"));
1920                 return False;
1921         }
1922
1923         state->acct_flags = acct_flags;
1924
1925         search->private_data = state;
1926         search->next_entry = next_entry_users;
1927         search->search_end = search_end_users;
1928         return True;
1929 }
1930
1931 struct group_search {
1932         GROUP_MAP *groups;
1933         size_t num_groups, current_group;
1934 };
1935
1936 static BOOL next_entry_groups(struct pdb_search *s,
1937                               struct samr_displayentry *entry)
1938 {
1939         struct group_search *state = s->private_data;
1940         uint32 rid;
1941         GROUP_MAP *map = &state->groups[state->current_group];
1942
1943         if (state->current_group == state->num_groups)
1944                 return False;
1945
1946         sid_peek_rid(&map->sid, &rid);
1947
1948         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1949                           entry);
1950
1951         state->current_group += 1;
1952         return True;
1953 }
1954
1955 static void search_end_groups(struct pdb_search *search)
1956 {
1957         struct group_search *state = search->private_data;
1958         SAFE_FREE(state->groups);
1959 }
1960
1961 static BOOL pdb_search_grouptype(struct pdb_search *search,
1962                                  enum SID_NAME_USE type)
1963 {
1964         struct group_search *state;
1965
1966         state = TALLOC_P(search->mem_ctx, struct group_search);
1967         if (state == NULL) {
1968                 DEBUG(0, ("talloc failed\n"));
1969                 return False;
1970         }
1971
1972         if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1973                                     True)) {
1974                 DEBUG(0, ("Could not enum groups\n"));
1975                 return False;
1976         }
1977
1978         state->current_group = 0;
1979         search->private_data = state;
1980         search->next_entry = next_entry_groups;
1981         search->search_end = search_end_groups;
1982         return True;
1983 }
1984
1985 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1986                                       struct pdb_search *search)
1987 {
1988         return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1989 }
1990
1991 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1992                                        struct pdb_search *search,
1993                                        const DOM_SID *sid)
1994 {
1995
1996         if (sid_equal(sid, get_global_sam_sid()))
1997                 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1998
1999         if (sid_equal(sid, &global_sid_Builtin))
2000                 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
2001
2002         DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
2003         return False;
2004 }
2005
2006 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
2007                                                      uint32 idx)
2008 {
2009         if (idx < search->num_entries)
2010                 return &search->cache[idx];
2011
2012         if (search->search_ended)
2013                 return NULL;
2014
2015         while (idx >= search->num_entries) {
2016                 struct samr_displayentry entry;
2017
2018                 if (!search->next_entry(search, &entry)) {
2019                         search->search_end(search);
2020                         search->search_ended = True;
2021                         break;
2022                 }
2023
2024                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
2025                                    entry, &search->cache, &search->num_entries,
2026                                    &search->cache_size);
2027         }
2028
2029         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
2030 }
2031
2032 struct pdb_search *pdb_search_users(uint16 acct_flags)
2033 {
2034         struct pdb_context *pdb_context = pdb_get_static_context(False);
2035         struct pdb_search *result;
2036
2037         if (pdb_context == NULL) return NULL;
2038
2039         result = pdb_search_init(PDB_USER_SEARCH);
2040         if (result == NULL) return NULL;
2041
2042         if (!pdb_context->pdb_search_users(pdb_context, result, acct_flags)) {
2043                 talloc_destroy(result->mem_ctx);
2044                 return NULL;
2045         }
2046         return result;
2047 }
2048
2049 struct pdb_search *pdb_search_groups(void)
2050 {
2051         struct pdb_context *pdb_context = pdb_get_static_context(False);
2052         struct pdb_search *result;
2053
2054         if (pdb_context == NULL) return NULL;
2055
2056         result = pdb_search_init(PDB_GROUP_SEARCH);
2057         if (result == NULL) return NULL;
2058
2059         if (!pdb_context->pdb_search_groups(pdb_context, result)) {
2060                 talloc_destroy(result->mem_ctx);
2061                 return NULL;
2062         }
2063         return result;
2064 }
2065
2066 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
2067 {
2068         struct pdb_context *pdb_context = pdb_get_static_context(False);
2069         struct pdb_search *result;
2070
2071         if (pdb_context == NULL) return NULL;
2072
2073         result = pdb_search_init(PDB_ALIAS_SEARCH);
2074         if (result == NULL) return NULL;
2075
2076         if (!pdb_context->pdb_search_aliases(pdb_context, result, sid)) {
2077                 talloc_destroy(result->mem_ctx);
2078                 return NULL;
2079         }
2080         return result;
2081 }
2082
2083 uint32 pdb_search_entries(struct pdb_search *search,
2084                           uint32 start_idx, uint32 max_entries,
2085                           struct samr_displayentry **result)
2086 {
2087         struct samr_displayentry *end_entry;
2088         uint32 end_idx = start_idx+max_entries-1;
2089
2090         /* The first entry needs to be searched after the last. Otherwise the
2091          * first entry might have moved due to a realloc during the search for
2092          * the last entry. */
2093
2094         end_entry = pdb_search_getentry(search, end_idx);
2095         *result = pdb_search_getentry(search, start_idx);
2096
2097         if (end_entry != NULL)
2098                 return max_entries;
2099
2100         if (start_idx >= search->num_entries)
2101                 return 0;
2102
2103         return search->num_entries - start_idx;
2104 }
2105
2106 void pdb_search_destroy(struct pdb_search *search)
2107 {
2108         if (search == NULL)
2109                 return;
2110
2111         if (!search->search_ended)
2112                 search->search_end(search);
2113
2114         talloc_destroy(search->mem_ctx);
2115 }
2116
2117 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
2118 {
2119         *methods = TALLOC_P(mem_ctx, struct pdb_methods);
2120
2121         if (!*methods) {
2122                 return NT_STATUS_NO_MEMORY;
2123         }
2124
2125         ZERO_STRUCTP(*methods);
2126
2127         (*methods)->setsampwent = pdb_default_setsampwent;
2128         (*methods)->endsampwent = pdb_default_endsampwent;
2129         (*methods)->getsampwent = pdb_default_getsampwent;
2130         (*methods)->getsampwnam = pdb_default_getsampwnam;
2131         (*methods)->getsampwsid = pdb_default_getsampwsid;
2132         (*methods)->add_sam_account = pdb_default_add_sam_account;
2133         (*methods)->update_sam_account = pdb_default_update_sam_account;
2134         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2135         (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2136         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2137
2138         (*methods)->getgrsid = pdb_default_getgrsid;
2139         (*methods)->getgrgid = pdb_default_getgrgid;
2140         (*methods)->getgrnam = pdb_default_getgrnam;
2141         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2142         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2143         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2144         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2145         (*methods)->enum_group_members = pdb_default_enum_group_members;
2146         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2147         (*methods)->find_alias = pdb_default_find_alias;
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
2161         (*methods)->search_users = pdb_default_search_users;
2162         (*methods)->search_groups = pdb_default_search_groups;
2163         (*methods)->search_aliases = pdb_default_search_aliases;
2164
2165         return NT_STATUS_OK;
2166 }