r6351: This is quite a large and intrusive patch, but there are not many pieces that
[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
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_update_login_attempts(struct pdb_context *context,
329                                                 SAM_ACCOUNT *sam_acct, BOOL success)
330 {
331         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
332
333         if (!context) {
334                 DEBUG(0, ("invalid pdb_context specified!\n"));
335                 return ret;
336         }
337
338         if (!sam_acct || !sam_acct->methods){
339                 DEBUG(0, ("invalid sam_acct specified\n"));
340                 return ret;
341         }
342
343         return sam_acct->methods->update_login_attempts(sam_acct->methods, sam_acct, success);
344 }
345
346 static NTSTATUS context_getgrsid(struct pdb_context *context,
347                                  GROUP_MAP *map, DOM_SID sid)
348 {
349         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
350
351         struct pdb_methods *curmethods;
352         if ((!context)) {
353                 DEBUG(0, ("invalid pdb_context specified!\n"));
354                 return ret;
355         }
356         curmethods = context->pdb_methods;
357         while (curmethods){
358                 ret = curmethods->getgrsid(curmethods, map, sid);
359                 if (NT_STATUS_IS_OK(ret)) {
360                         map->methods = curmethods;
361                         return ret;
362                 }
363                 curmethods = curmethods->next;
364         }
365
366         return ret;
367 }
368
369 static NTSTATUS context_getgrgid(struct pdb_context *context,
370                                  GROUP_MAP *map, gid_t gid)
371 {
372         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
373
374         struct pdb_methods *curmethods;
375         if ((!context)) {
376                 DEBUG(0, ("invalid pdb_context specified!\n"));
377                 return ret;
378         }
379         curmethods = context->pdb_methods;
380         while (curmethods){
381                 ret = curmethods->getgrgid(curmethods, map, gid);
382                 if (NT_STATUS_IS_OK(ret)) {
383                         map->methods = curmethods;
384                         return ret;
385                 }
386                 curmethods = curmethods->next;
387         }
388
389         return ret;
390 }
391
392 static NTSTATUS context_getgrnam(struct pdb_context *context,
393                                  GROUP_MAP *map, const char *name)
394 {
395         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
396
397         struct pdb_methods *curmethods;
398         if ((!context)) {
399                 DEBUG(0, ("invalid pdb_context specified!\n"));
400                 return ret;
401         }
402         curmethods = context->pdb_methods;
403         while (curmethods){
404                 ret = curmethods->getgrnam(curmethods, map, name);
405                 if (NT_STATUS_IS_OK(ret)) {
406                         map->methods = curmethods;
407                         return ret;
408                 }
409                 curmethods = curmethods->next;
410         }
411
412         return ret;
413 }
414
415 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
416                                                 GROUP_MAP *map)
417 {
418         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
419
420         if ((!context) || (!context->pdb_methods)) {
421                 DEBUG(0, ("invalid pdb_context specified!\n"));
422                 return ret;
423         }
424
425         return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
426                                                              map);
427 }
428
429 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
430                                                    GROUP_MAP *map)
431 {
432         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
433
434         if ((!context) || (!context->pdb_methods)) {
435                 DEBUG(0, ("invalid pdb_context specified!\n"));
436                 return ret;
437         }
438
439         return context->
440                 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
441 }
442
443 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
444                                                    DOM_SID sid)
445 {
446         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
447
448         if ((!context) || (!context->pdb_methods)) {
449                 DEBUG(0, ("invalid pdb_context specified!\n"));
450                 return ret;
451         }
452
453         return context->
454                 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
455 }
456
457 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
458                                            enum SID_NAME_USE sid_name_use,
459                                            GROUP_MAP **rmap, int *num_entries,
460                                            BOOL unix_only)
461 {
462         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
463
464         if ((!context) || (!context->pdb_methods)) {
465                 DEBUG(0, ("invalid pdb_context specified!\n"));
466                 return ret;
467         }
468
469         return context->pdb_methods->enum_group_mapping(context->pdb_methods,
470                                                         sid_name_use, rmap,
471                                                         num_entries, unix_only);
472 }
473
474 static NTSTATUS context_enum_group_members(struct pdb_context *context,
475                                            TALLOC_CTX *mem_ctx,
476                                            const DOM_SID *group,
477                                            uint32 **member_rids,
478                                            int *num_members)
479 {
480         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
481
482         if ((!context) || (!context->pdb_methods)) {
483                 DEBUG(0, ("invalid pdb_context specified!\n"));
484                 return ret;
485         }
486
487         return context->pdb_methods->enum_group_members(context->pdb_methods,
488                                                         mem_ctx, group,
489                                                         member_rids,
490                                                         num_members);
491 }
492
493 static NTSTATUS context_enum_group_memberships(struct pdb_context *context,
494                                                const char *username,
495                                                gid_t primary_gid,
496                                                DOM_SID **sids, gid_t **gids,
497                                                int *num_groups)
498 {
499         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
500
501         if ((!context) || (!context->pdb_methods)) {
502                 DEBUG(0, ("invalid pdb_context specified!\n"));
503                 return ret;
504         }
505
506         return context->pdb_methods->
507                 enum_group_memberships(context->pdb_methods, username,
508                                        primary_gid, sids, gids, num_groups);
509 }
510
511 static NTSTATUS context_find_alias(struct pdb_context *context,
512                                    const char *name, DOM_SID *sid)
513 {
514         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
515
516         if ((!context) || (!context->pdb_methods)) {
517                 DEBUG(0, ("invalid pdb_context specified!\n"));
518                 return ret;
519         }
520
521         return context->pdb_methods->find_alias(context->pdb_methods,
522                                                 name, sid);
523 }
524
525 static NTSTATUS context_create_alias(struct pdb_context *context,
526                                      const char *name, uint32 *rid)
527 {
528         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
529
530         if ((!context) || (!context->pdb_methods)) {
531                 DEBUG(0, ("invalid pdb_context specified!\n"));
532                 return ret;
533         }
534
535         return context->pdb_methods->create_alias(context->pdb_methods,
536                                                   name, rid);
537 }
538
539 static NTSTATUS context_delete_alias(struct pdb_context *context,
540                                      const DOM_SID *sid)
541 {
542         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
543
544         if ((!context) || (!context->pdb_methods)) {
545                 DEBUG(0, ("invalid pdb_context specified!\n"));
546                 return ret;
547         }
548
549         return context->pdb_methods->delete_alias(context->pdb_methods, sid);
550 }
551
552 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
553                                       const DOM_SID *sid,
554                                       struct acct_info *info)
555 {
556         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
557
558         if ((!context) || (!context->pdb_methods)) {
559                 DEBUG(0, ("invalid pdb_context specified!\n"));
560                 return ret;
561         }
562
563         return context->pdb_methods->get_aliasinfo(context->pdb_methods,
564                                                    sid, info);
565 }
566
567 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
568                                       const DOM_SID *sid,
569                                       struct acct_info *info)
570 {
571         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
572
573         if ((!context) || (!context->pdb_methods)) {
574                 DEBUG(0, ("invalid pdb_context specified!\n"));
575                 return ret;
576         }
577
578         return context->pdb_methods->set_aliasinfo(context->pdb_methods,
579                                                    sid, info);
580 }
581
582 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
583                                      const DOM_SID *alias,
584                                      const DOM_SID *member)
585 {
586         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
587
588         if ((!context) || (!context->pdb_methods)) {
589                 DEBUG(0, ("invalid pdb_context specified!\n"));
590                 return ret;
591         }
592
593         return context->pdb_methods->add_aliasmem(context->pdb_methods,
594                                                   alias, member);
595 }
596         
597 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
598                                      const DOM_SID *alias,
599                                      const DOM_SID *member)
600 {
601         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
602
603         if ((!context) || (!context->pdb_methods)) {
604                 DEBUG(0, ("invalid pdb_context specified!\n"));
605                 return ret;
606         }
607
608         return context->pdb_methods->del_aliasmem(context->pdb_methods,
609                                                   alias, member);
610 }
611         
612 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
613                                       const DOM_SID *alias, DOM_SID **members,
614                                       int *num)
615 {
616         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
617
618         if ((!context) || (!context->pdb_methods)) {
619                 DEBUG(0, ("invalid pdb_context specified!\n"));
620                 return ret;
621         }
622
623         return context->pdb_methods->enum_aliasmem(context->pdb_methods,
624                                                    alias, members, num);
625 }
626         
627 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
628                                                TALLOC_CTX *mem_ctx,
629                                                const DOM_SID *domain_sid,
630                                                const DOM_SID *members,
631                                                int num_members,
632                                                uint32 **alias_rids,
633                                                int *num_alias_rids)
634 {
635         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
636
637         if ((!context) || (!context->pdb_methods)) {
638                 DEBUG(0, ("invalid pdb_context specified!\n"));
639                 return ret;
640         }
641
642         return context->pdb_methods->
643                 enum_alias_memberships(context->pdb_methods, mem_ctx,
644                                        domain_sid, members, num_members,
645                                        alias_rids, num_alias_rids);
646 }
647
648 static NTSTATUS context_lookup_rids(struct pdb_context *context,
649                                     TALLOC_CTX *mem_ctx,
650                                     const DOM_SID *domain_sid,
651                                     int num_rids,
652                                     uint32 *rids,
653                                     const char ***names,
654                                     uint32 **attrs)
655 {
656         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
657
658         if ((!context) || (!context->pdb_methods)) {
659                 DEBUG(0, ("invalid pdb_context specified!\n"));
660                 return ret;
661         }
662
663         return context->pdb_methods->lookup_rids(context->pdb_methods,
664                                                  mem_ctx, domain_sid, num_rids,
665                                                  rids, names, attrs);
666 }
667
668 static BOOL context_search_users(struct pdb_context *context,
669                                  struct pdb_search *search, uint16 acct_flags)
670 {
671         if ((!context) || (!context->pdb_methods)) {
672                 DEBUG(0, ("invalid pdb_context specified!\n"));
673                 return False;
674         }
675
676         return context->pdb_methods->search_users(context->pdb_methods,
677                                                   search, acct_flags);
678 }
679
680 static BOOL context_search_groups(struct pdb_context *context,
681                                   struct pdb_search *search)
682 {
683         if ((!context) || (!context->pdb_methods)) {
684                 DEBUG(0, ("invalid pdb_context specified!\n"));
685                 return False;
686         }
687
688         return context->pdb_methods->search_groups(context->pdb_methods,
689                                                    search);
690 }
691
692 static BOOL context_search_aliases(struct pdb_context *context,
693                                    struct pdb_search *search,
694                                    const DOM_SID *sid)
695 {
696         if ((!context) || (!context->pdb_methods)) {
697                 DEBUG(0, ("invalid pdb_context specified!\n"));
698                 return False;
699         }
700
701         return context->pdb_methods->search_aliases(context->pdb_methods,
702                                                     search, sid);
703 }
704
705 static BOOL context_search_next_entry(struct pdb_context *context,
706                                       struct pdb_search *search,
707                                       struct samr_displayentry *entry)
708 {
709         if ((!context) || (!context->pdb_methods)) {
710                 DEBUG(0, ("invalid pdb_context specified!\n"));
711                 return False;
712         }
713
714         return context->pdb_methods->search_next_entry(context->pdb_methods,
715                                                        search, entry);
716 }
717
718 static void context_search_end(struct pdb_context *context,
719                                struct pdb_search *search)
720 {
721         if ((!context) || (!context->pdb_methods)) {
722                 DEBUG(0, ("invalid pdb_context specified!\n"));
723                 return;
724         }
725
726         context->pdb_methods->search_end(context->pdb_methods, search);
727         return;
728 }
729
730 /******************************************************************
731   Free and cleanup a pdb context, any associated data and anything
732   that the attached modules might have associated.
733  *******************************************************************/
734
735 static void free_pdb_context(struct pdb_context **context)
736 {
737         struct pdb_methods *pdb_selected = (*context)->pdb_methods;
738
739         while (pdb_selected){
740                 if(pdb_selected->free_private_data)
741                         pdb_selected->free_private_data(&(pdb_selected->private_data));
742                 pdb_selected = pdb_selected->next;
743         }
744
745         talloc_destroy((*context)->mem_ctx);
746         *context = NULL;
747 }
748
749 /******************************************************************
750   Make a pdb_methods from scratch
751  *******************************************************************/
752
753 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
754 {
755         char *module_name = smb_xstrdup(selected);
756         char *module_location = NULL, *p;
757         struct pdb_init_function_entry *entry;
758         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
759
760         lazy_initialize_passdb();
761
762         p = strchr(module_name, ':');
763
764         if (p) {
765                 *p = 0;
766                 module_location = p+1;
767                 trim_char(module_location, ' ', ' ');
768         }
769
770         trim_char(module_name, ' ', ' ');
771
772
773         DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
774
775         entry = pdb_find_backend_entry(module_name);
776         
777         /* Try to find a module that contains this module */
778         if (!entry) { 
779                 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
780                 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
781                         DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
782                         SAFE_FREE(module_name);
783                         return NT_STATUS_UNSUCCESSFUL;
784                 }
785         }
786         
787         /* No such backend found */
788         if(!entry) { 
789                 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
790                 SAFE_FREE(module_name);
791                 return NT_STATUS_INVALID_PARAMETER;
792         }
793
794         DEBUG(5,("Found pdb backend %s\n", module_name));
795         nt_status = entry->init(context, methods, module_location);
796         if (NT_STATUS_IS_OK(nt_status)) {
797                 DEBUG(5,("pdb backend %s has a valid init\n", selected));
798         } else {
799                 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
800         }
801         SAFE_FREE(module_name);
802         return nt_status;
803 }
804
805 /******************************************************************
806   Make a pdb_context from scratch.
807  *******************************************************************/
808
809 static NTSTATUS make_pdb_context(struct pdb_context **context) 
810 {
811         TALLOC_CTX *mem_ctx;
812
813         mem_ctx = talloc_init("pdb_context internal allocation context");
814
815         if (!mem_ctx) {
816                 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
817                 return NT_STATUS_NO_MEMORY;
818         }               
819
820         *context = TALLOC_P(mem_ctx, struct pdb_context);
821         if (!*context) {
822                 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
823                 return NT_STATUS_NO_MEMORY;
824         }
825
826         ZERO_STRUCTP(*context);
827
828         (*context)->mem_ctx = mem_ctx;
829
830         (*context)->pdb_setsampwent = context_setsampwent;
831         (*context)->pdb_endsampwent = context_endsampwent;
832         (*context)->pdb_getsampwent = context_getsampwent;
833         (*context)->pdb_getsampwnam = context_getsampwnam;
834         (*context)->pdb_getsampwsid = context_getsampwsid;
835         (*context)->pdb_add_sam_account = context_add_sam_account;
836         (*context)->pdb_update_sam_account = context_update_sam_account;
837         (*context)->pdb_delete_sam_account = context_delete_sam_account;
838         (*context)->pdb_update_login_attempts = context_update_login_attempts;
839         (*context)->pdb_getgrsid = context_getgrsid;
840         (*context)->pdb_getgrgid = context_getgrgid;
841         (*context)->pdb_getgrnam = context_getgrnam;
842         (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
843         (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
844         (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
845         (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
846         (*context)->pdb_enum_group_members = context_enum_group_members;
847         (*context)->pdb_enum_group_memberships = context_enum_group_memberships;
848
849         (*context)->pdb_find_alias = context_find_alias;
850         (*context)->pdb_create_alias = context_create_alias;
851         (*context)->pdb_delete_alias = context_delete_alias;
852         (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
853         (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
854         (*context)->pdb_add_aliasmem = context_add_aliasmem;
855         (*context)->pdb_del_aliasmem = context_del_aliasmem;
856         (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
857         (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
858         (*context)->pdb_lookup_rids = context_lookup_rids;
859
860         (*context)->pdb_search_users = context_search_users;
861         (*context)->pdb_search_groups = context_search_groups;
862         (*context)->pdb_search_aliases = context_search_aliases;
863         (*context)->pdb_search_next_entry = context_search_next_entry;
864         (*context)->pdb_search_end = context_search_end;
865
866         (*context)->free_fn = free_pdb_context;
867
868         return NT_STATUS_OK;
869 }
870
871
872 /******************************************************************
873   Make a pdb_context, given an array of strings
874  *******************************************************************/
875
876 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected) 
877 {
878         int i = 0;
879         struct pdb_methods *curmethods, *tmpmethods;
880         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
881         BOOL have_guest = False;
882
883         if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
884                 return nt_status;
885         }
886
887         if (!selected) {
888                 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
889                 return nt_status;
890         }
891
892         while (selected[i]){
893                 if (strcmp(selected[i], "guest") == 0) {
894                         have_guest = True;
895                 }
896                 /* Try to initialise pdb */
897                 DEBUG(5,("Trying to load: %s\n", selected[i]));
898                 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
899                         DEBUG(1, ("Loading %s failed!\n", selected[i]));
900                         free_pdb_context(context);
901                         return nt_status;
902                 }
903                 curmethods->parent = *context;
904                 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
905                 i++;
906         }
907
908         if (have_guest)
909                 return NT_STATUS_OK;
910
911         if ( (lp_guestaccount() == NULL) ||
912              (*lp_guestaccount() == '\0') ) {
913                 /* We explicitly don't want guest access. No idea what
914                    else that breaks, but be it that way. */
915                 return NT_STATUS_OK;
916         }
917
918         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
919                                                                *context,
920                                                                "guest"))) {
921                 DEBUG(1, ("Loading guest module failed!\n"));
922                 free_pdb_context(context);
923                 return nt_status;
924         }
925
926         curmethods->parent = *context;
927         DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
928         
929         return NT_STATUS_OK;
930 }
931
932 /******************************************************************
933   Make a pdb_context, given a text string.
934  *******************************************************************/
935
936 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected) 
937 {
938         NTSTATUS ret;
939         char **newsel = str_list_make(selected, NULL);
940         ret = make_pdb_context_list(context, (const char **)newsel);
941         str_list_free(&newsel);
942         return ret;
943 }
944
945 /******************************************************************
946  Return an already initialised pdb_context, to facilitate backward 
947  compatibility (see functions below).
948 *******************************************************************/
949
950 static struct pdb_context *pdb_get_static_context(BOOL reload) 
951 {
952         static struct pdb_context *pdb_context = NULL;
953
954         if ((pdb_context) && (reload)) {
955                 pdb_context->free_fn(&pdb_context);
956                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
957                         return NULL;
958                 }
959         }
960
961         if (!pdb_context) {
962                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
963                         return NULL;
964                 }
965         }
966
967         return pdb_context;
968 }
969
970 /******************************************************************
971  Backward compatibility functions for the original passdb interface
972 *******************************************************************/
973
974 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask) 
975 {
976         struct pdb_context *pdb_context = pdb_get_static_context(False);
977
978         if (!pdb_context) {
979                 return False;
980         }
981
982         return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update, acb_mask));
983 }
984
985 void pdb_endsampwent(void) 
986 {
987         struct pdb_context *pdb_context = pdb_get_static_context(False);
988
989         if (!pdb_context) {
990                 return;
991         }
992
993         pdb_context->pdb_endsampwent(pdb_context);
994 }
995
996 BOOL pdb_getsampwent(SAM_ACCOUNT *user) 
997 {
998         struct pdb_context *pdb_context = pdb_get_static_context(False);
999
1000         if (!pdb_context) {
1001                 return False;
1002         }
1003
1004         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
1005 }
1006
1007 static SAM_ACCOUNT *sam_account_cache = NULL;
1008
1009 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username) 
1010 {
1011         struct pdb_context *pdb_context = pdb_get_static_context(False);
1012
1013         if (!pdb_context) {
1014                 return False;
1015         }
1016
1017         if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
1018                                                           sam_acct, username)))
1019                 return False;
1020
1021         if (sam_account_cache != NULL) {
1022                 pdb_free_sam(&sam_account_cache);
1023                 sam_account_cache = NULL;
1024         }
1025
1026         pdb_copy_sam_account(sam_acct, &sam_account_cache);
1027         return True;
1028 }
1029
1030 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid) 
1031 {
1032         struct pdb_context *pdb_context = pdb_get_static_context(False);
1033
1034         if (!pdb_context) {
1035                 return False;
1036         }
1037
1038         if ((sam_account_cache != NULL) &&
1039             (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
1040                 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
1041
1042         return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1043 }
1044
1045 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct) 
1046 {
1047         struct pdb_context *pdb_context = pdb_get_static_context(False);
1048
1049         if (!pdb_context) {
1050                 return False;
1051         }
1052         
1053         return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1054 }
1055
1056 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct) 
1057 {
1058         struct pdb_context *pdb_context = pdb_get_static_context(False);
1059
1060         if (!pdb_context) {
1061                 return False;
1062         }
1063
1064         if (sam_account_cache != NULL) {
1065                 pdb_free_sam(&sam_account_cache);
1066                 sam_account_cache = NULL;
1067         }
1068
1069         return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1070 }
1071
1072 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct) 
1073 {
1074         struct pdb_context *pdb_context = pdb_get_static_context(False);
1075
1076         if (!pdb_context) {
1077                 return False;
1078         }
1079
1080         if (sam_account_cache != NULL) {
1081                 pdb_free_sam(&sam_account_cache);
1082                 sam_account_cache = NULL;
1083         }
1084
1085         return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1086 }
1087
1088 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
1089 {
1090         struct pdb_context *pdb_context = pdb_get_static_context(False);
1091
1092         if (!pdb_context) {
1093                 return NT_STATUS_NOT_IMPLEMENTED;
1094         }
1095
1096         return pdb_context->pdb_update_login_attempts(pdb_context, sam_acct, success);
1097 }
1098
1099 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1100 {
1101         struct pdb_context *pdb_context = pdb_get_static_context(False);
1102
1103         if (!pdb_context) {
1104                 return False;
1105         }
1106
1107         return NT_STATUS_IS_OK(pdb_context->
1108                                pdb_getgrsid(pdb_context, map, sid));
1109 }
1110
1111 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1112 {
1113         struct pdb_context *pdb_context = pdb_get_static_context(False);
1114
1115         if (!pdb_context) {
1116                 return False;
1117         }
1118
1119         return NT_STATUS_IS_OK(pdb_context->
1120                                pdb_getgrgid(pdb_context, map, gid));
1121 }
1122
1123 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1124 {
1125         struct pdb_context *pdb_context = pdb_get_static_context(False);
1126
1127         if (!pdb_context) {
1128                 return False;
1129         }
1130
1131         return NT_STATUS_IS_OK(pdb_context->
1132                                pdb_getgrnam(pdb_context, map, name));
1133 }
1134
1135 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1136 {
1137         struct pdb_context *pdb_context = pdb_get_static_context(False);
1138
1139         if (!pdb_context) {
1140                 return False;
1141         }
1142
1143         return NT_STATUS_IS_OK(pdb_context->
1144                                pdb_add_group_mapping_entry(pdb_context, map));
1145 }
1146
1147 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1148 {
1149         struct pdb_context *pdb_context = pdb_get_static_context(False);
1150
1151         if (!pdb_context) {
1152                 return False;
1153         }
1154
1155         return NT_STATUS_IS_OK(pdb_context->
1156                                pdb_update_group_mapping_entry(pdb_context, map));
1157 }
1158
1159 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1160 {
1161         struct pdb_context *pdb_context = pdb_get_static_context(False);
1162
1163         if (!pdb_context) {
1164                 return False;
1165         }
1166
1167         return NT_STATUS_IS_OK(pdb_context->
1168                                pdb_delete_group_mapping_entry(pdb_context, sid));
1169 }
1170
1171 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
1172                             int *num_entries, BOOL unix_only)
1173 {
1174         struct pdb_context *pdb_context = pdb_get_static_context(False);
1175
1176         if (!pdb_context) {
1177                 return False;
1178         }
1179
1180         return NT_STATUS_IS_OK(pdb_context->
1181                                pdb_enum_group_mapping(pdb_context, sid_name_use,
1182                                                       rmap, num_entries, unix_only));
1183 }
1184
1185 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
1186                                 const DOM_SID *sid,
1187                                 uint32 **member_rids,
1188                                 int *num_members)
1189 {
1190         struct pdb_context *pdb_context = pdb_get_static_context(False);
1191
1192         if (!pdb_context) {
1193                 return NT_STATUS_UNSUCCESSFUL;
1194         }
1195
1196         return pdb_context->pdb_enum_group_members(pdb_context, mem_ctx, sid, 
1197                                                    member_rids, num_members);
1198 }
1199
1200 NTSTATUS pdb_enum_group_memberships(const char *username, gid_t primary_gid,
1201                                     DOM_SID **sids, gid_t **gids,
1202                                     int *num_groups)
1203 {
1204         struct pdb_context *pdb_context = pdb_get_static_context(False);
1205
1206         if (!pdb_context) {
1207                 return NT_STATUS_UNSUCCESSFUL;
1208         }
1209
1210         return pdb_context->pdb_enum_group_memberships(pdb_context, username,
1211                                                        primary_gid, sids, gids,
1212                                                        num_groups);
1213 }
1214
1215 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1216 {
1217         struct pdb_context *pdb_context = pdb_get_static_context(False);
1218
1219         if (!pdb_context) {
1220                 return False;
1221         }
1222
1223         return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1224                                                              name, sid));
1225 }
1226
1227 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1228 {
1229         struct pdb_context *pdb_context = pdb_get_static_context(False);
1230
1231         if (!pdb_context) {
1232                 return NT_STATUS_NOT_IMPLEMENTED;
1233         }
1234
1235         return pdb_context->pdb_create_alias(pdb_context, name, rid);
1236 }
1237
1238 BOOL pdb_delete_alias(const DOM_SID *sid)
1239 {
1240         struct pdb_context *pdb_context = pdb_get_static_context(False);
1241
1242         if (!pdb_context) {
1243                 return False;
1244         }
1245
1246         return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1247                                                              sid));
1248                                                             
1249 }
1250
1251 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1252 {
1253         struct pdb_context *pdb_context = pdb_get_static_context(False);
1254
1255         if (!pdb_context) {
1256                 return False;
1257         }
1258
1259         return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1260                                                               info));
1261 }
1262
1263 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1264 {
1265         struct pdb_context *pdb_context = pdb_get_static_context(False);
1266
1267         if (!pdb_context) {
1268                 return False;
1269         }
1270
1271         return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1272                                                               info));
1273 }
1274
1275 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1276 {
1277         struct pdb_context *pdb_context = pdb_get_static_context(False);
1278
1279         if (!pdb_context) {
1280                 return False;
1281         }
1282
1283         return NT_STATUS_IS_OK(pdb_context->
1284                                pdb_add_aliasmem(pdb_context, alias, member));
1285 }
1286
1287 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1288 {
1289         struct pdb_context *pdb_context = pdb_get_static_context(False);
1290
1291         if (!pdb_context) {
1292                 return False;
1293         }
1294
1295         return NT_STATUS_IS_OK(pdb_context->
1296                                pdb_del_aliasmem(pdb_context, alias, member));
1297 }
1298
1299 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1300                        DOM_SID **members, int *num_members)
1301 {
1302         struct pdb_context *pdb_context = pdb_get_static_context(False);
1303
1304         if (!pdb_context) {
1305                 return False;
1306         }
1307
1308         return NT_STATUS_IS_OK(pdb_context->
1309                                pdb_enum_aliasmem(pdb_context, alias,
1310                                                  members, num_members));
1311 }
1312
1313 BOOL pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
1314                                 const DOM_SID *members, int num_members,
1315                                 uint32 **alias_rids, int *num_alias_rids)
1316 {
1317         struct pdb_context *pdb_context = pdb_get_static_context(False);
1318
1319         if (!pdb_context) {
1320                 return False;
1321         }
1322
1323         return NT_STATUS_IS_OK(pdb_context->
1324                                pdb_enum_alias_memberships(pdb_context, mem_ctx,
1325                                                           domain_sid,
1326                                                           members, num_members,
1327                                                           alias_rids,
1328                                                           num_alias_rids));
1329 }
1330
1331 NTSTATUS pdb_lookup_rids(TALLOC_CTX *mem_ctx,
1332                          const DOM_SID *domain_sid,
1333                          int num_rids,
1334                          uint32 *rids,
1335                          const char ***names,
1336                          uint32 **attrs)
1337 {
1338         struct pdb_context *pdb_context = pdb_get_static_context(False);
1339
1340         if (!pdb_context) {
1341                 return NT_STATUS_NOT_IMPLEMENTED;
1342         }
1343
1344         return pdb_context->pdb_lookup_rids(pdb_context, mem_ctx, domain_sid,
1345                                             num_rids, rids, names, attrs);
1346 }
1347
1348 /***************************************************************
1349   Initialize the static context (at smbd startup etc). 
1350
1351   If uninitialised, context will auto-init on first use.
1352  ***************************************************************/
1353
1354 BOOL initialize_password_db(BOOL reload)
1355 {       
1356         return (pdb_get_static_context(reload) != NULL);
1357 }
1358
1359
1360 /***************************************************************************
1361   Default implementations of some functions.
1362  ****************************************************************************/
1363
1364 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1365 {
1366         return NT_STATUS_NO_SUCH_USER;
1367 }
1368
1369 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1370 {
1371         return NT_STATUS_NO_SUCH_USER;
1372 }
1373
1374 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1375 {
1376         DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1377         return NT_STATUS_NOT_IMPLEMENTED;
1378 }
1379
1380 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1381 {
1382         return NT_STATUS_NOT_IMPLEMENTED;
1383 }
1384
1385 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1386 {
1387         return NT_STATUS_NOT_IMPLEMENTED;
1388 }
1389
1390 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1391 {
1392         return NT_STATUS_OK;
1393 }
1394
1395 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1396 {
1397         return NT_STATUS_NOT_IMPLEMENTED;
1398 }
1399
1400 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1401 {
1402         return NT_STATUS_NOT_IMPLEMENTED;
1403 }
1404
1405 static void pdb_default_endsampwent(struct pdb_methods *methods)
1406 {
1407         return; /* NT_STATUS_NOT_IMPLEMENTED; */
1408 }
1409
1410 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1411                                     uid_t uid, uid_t **uids, int *num)
1412 {
1413         int i;
1414
1415         for (i=0; i<*num; i++) {
1416                 if ((*uids)[i] == uid)
1417                         return;
1418         }
1419         
1420         *uids = TALLOC_REALLOC_ARRAY(mem_ctx, *uids, uid_t, *num+1);
1421
1422         if (*uids == NULL)
1423                 return;
1424
1425         (*uids)[*num] = uid;
1426         *num += 1;
1427 }
1428
1429 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **uids,
1430                            int *num)
1431 {
1432         struct group *grp;
1433         char **gr;
1434         struct sys_pwent *userlist, *user;
1435  
1436         *uids = NULL;
1437         *num = 0;
1438
1439         /* We only look at our own sam, so don't care about imported stuff */
1440
1441         winbind_off();
1442
1443         if ((grp = getgrgid(gid)) == NULL) {
1444                 winbind_on();
1445                 return False;
1446         }
1447
1448         /* Primary group members */
1449
1450         userlist = getpwent_list();
1451
1452         for (user = userlist; user != NULL; user = user->next) {
1453                 if (user->pw_gid != gid)
1454                         continue;
1455                 add_uid_to_array_unique(mem_ctx, user->pw_uid, uids, num);
1456         }
1457
1458         pwent_free(userlist);
1459
1460         /* Secondary group members */
1461
1462         for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1463                 struct passwd *pw = getpwnam(*gr);
1464
1465                 if (pw == NULL)
1466                         continue;
1467                 add_uid_to_array_unique(mem_ctx, pw->pw_uid, uids, num);
1468         }
1469
1470         winbind_on();
1471
1472         return True;
1473 }
1474
1475 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1476                                         TALLOC_CTX *mem_ctx,
1477                                         const DOM_SID *group,
1478                                         uint32 **member_rids,
1479                                         int *num_members)
1480 {
1481         gid_t gid;
1482         uid_t *uids;
1483         int i, num_uids;
1484
1485         *member_rids = NULL;
1486         *num_members = 0;
1487
1488         if (!NT_STATUS_IS_OK(sid_to_gid(group, &gid)))
1489                 return NT_STATUS_NO_SUCH_GROUP;
1490
1491         if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1492                 return NT_STATUS_NO_SUCH_GROUP;
1493
1494         if (num_uids == 0)
1495                 return NT_STATUS_OK;
1496
1497         *member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1498
1499         for (i=0; i<num_uids; i++) {
1500                 DOM_SID sid;
1501
1502                 if (!NT_STATUS_IS_OK(uid_to_sid(&sid, uids[i]))) {
1503                         DEBUG(1, ("Could not map member uid to SID\n"));
1504                         continue;
1505                 }
1506
1507                 if (!sid_check_is_in_our_domain(&sid)) {
1508                         DEBUG(1, ("Inconsistent SAM -- group member uid not "
1509                                   "in our domain\n"));
1510                         continue;
1511                 }
1512
1513                 sid_peek_rid(&sid, &(*member_rids)[*num_members]);
1514                 *num_members += 1;
1515         }
1516
1517         return NT_STATUS_OK;
1518 }
1519
1520 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1521                                  TALLOC_CTX *mem_ctx,
1522                                  const DOM_SID *domain_sid,
1523                                  int num_rids,
1524                                  uint32 *rids,
1525                                  const char ***names,
1526                                  uint32 **attrs)
1527 {
1528         int i;
1529         NTSTATUS result;
1530         BOOL have_mapped = False;
1531         BOOL have_unmapped = False;
1532
1533         (*names) = TALLOC_ZERO_ARRAY(mem_ctx, const char *, num_rids);
1534         (*attrs) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_rids);
1535
1536         if ((num_rids != 0) && (((*names) == NULL) || ((*attrs) == NULL)))
1537                 return NT_STATUS_NO_MEMORY;
1538
1539         if (!sid_equal(domain_sid, get_global_sam_sid())) {
1540                 /* TODO: Sooner or later we need to look up BUILTIN rids as
1541                  * well. -- vl */
1542                 goto done;
1543         }
1544
1545         for (i = 0; i < num_rids; i++) {
1546                 fstring tmpname;
1547                 fstring domname;
1548                 DOM_SID sid;
1549                 enum SID_NAME_USE type;
1550
1551                 (*attrs)[i] = SID_NAME_UNKNOWN;
1552
1553                 sid_copy(&sid, domain_sid);
1554                 sid_append_rid(&sid, rids[i]);
1555
1556                 if (lookup_sid(&sid, domname, tmpname, &type)) {
1557                         (*attrs)[i] = (uint32)type;
1558                         (*names)[i] = talloc_strdup(mem_ctx, tmpname);
1559                         if ((*names)[i] == NULL)
1560                                 return NT_STATUS_NO_MEMORY;
1561                         DEBUG(5,("lookup_rids: %s:%d\n", (*names)[i],
1562                                  (*attrs)[i]));
1563                         have_mapped = True;
1564                 } else {
1565                         have_unmapped = True;
1566                 }
1567         }
1568
1569  done:
1570
1571         result = NT_STATUS_NONE_MAPPED;
1572
1573         if (have_mapped)
1574                 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1575
1576         return result;
1577 }
1578
1579 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1580 {
1581         TALLOC_CTX *mem_ctx;
1582         struct pdb_search *result;
1583
1584         mem_ctx = talloc_init("pdb_search");
1585         if (mem_ctx == NULL) {
1586                 DEBUG(0, ("talloc_init failed\n"));
1587                 return NULL;
1588         }
1589
1590         result = TALLOC_P(mem_ctx, struct pdb_search);
1591         if (result == NULL) {
1592                 DEBUG(0, ("talloc failed\n"));
1593                 return NULL;
1594         }
1595
1596         result->mem_ctx = mem_ctx;
1597         result->type = type;
1598         result->cache = NULL;
1599         result->num_entries = 0;
1600         result->cache_size = 0;
1601         result->search_ended = False;
1602
1603         return result;
1604 }
1605
1606 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1607                               uint16 acct_flags,
1608                               const char *account_name,
1609                               const char *fullname,
1610                               const char *description,
1611                               struct samr_displayentry *entry)
1612 {
1613         entry->rid = rid;
1614         entry->acct_flags = acct_flags;
1615
1616         if (account_name != NULL)
1617                 entry->account_name = talloc_strdup(mem_ctx, account_name);
1618         else
1619                 entry->account_name = "";
1620
1621         if (fullname != NULL)
1622                 entry->fullname = talloc_strdup(mem_ctx, fullname);
1623         else
1624                 entry->fullname = "";
1625
1626         if (description != NULL)
1627                 entry->description = talloc_strdup(mem_ctx, description);
1628         else
1629                 entry->description = "";
1630 }
1631
1632 static BOOL user_search_in_progress = False;
1633 struct user_search {
1634         uint16 acct_flags;
1635 };
1636
1637 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1638                                      struct pdb_search *search,
1639                                      uint16 acct_flags)
1640 {
1641         struct user_search *state;
1642
1643         if (user_search_in_progress) {
1644                 DEBUG(1, ("user search in progress\n"));
1645                 return False;
1646         }
1647
1648         if (!pdb_setsampwent(False, acct_flags)) {
1649                 DEBUG(5, ("Could not start search\n"));
1650                 return False;
1651         }
1652
1653         user_search_in_progress = True;
1654
1655         state = TALLOC_P(search->mem_ctx, struct user_search);
1656         if (state == NULL) {
1657                 DEBUG(0, ("talloc failed\n"));
1658                 return False;
1659         }
1660
1661         state->acct_flags = acct_flags;
1662
1663         search->private = state;
1664         return True;
1665 }
1666
1667 static BOOL pdb_search_next_entry_users(struct pdb_search *s,
1668                                         struct samr_displayentry *entry)
1669 {
1670         struct user_search *state = s->private;
1671         SAM_ACCOUNT *user = NULL;
1672         NTSTATUS status;
1673
1674  next:
1675         status = pdb_init_sam(&user);
1676         if (!NT_STATUS_IS_OK(status)) {
1677                 DEBUG(0, ("Could not pdb_init_sam\n"));
1678                 return False;
1679         }
1680
1681         if (!pdb_getsampwent(user)) {
1682                 pdb_free_sam(&user);
1683                 return False;
1684         }
1685
1686         if ((state->acct_flags != 0) &&
1687             ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1688                 pdb_free_sam(&user);
1689                 goto next;
1690         }
1691
1692         fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1693                           pdb_get_acct_ctrl(user), pdb_get_username(user),
1694                           pdb_get_fullname(user), pdb_get_acct_desc(user),
1695                           entry);
1696
1697         pdb_free_sam(&user);
1698         return True;
1699 }
1700
1701 static void pdb_search_end_users(struct pdb_search *search)
1702 {
1703         pdb_endsampwent();
1704         user_search_in_progress = False;
1705 }
1706
1707 struct group_search {
1708         GROUP_MAP *groups;
1709         int num_groups, current_group;
1710 };
1711
1712 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1713                                       struct pdb_search *search)
1714 {
1715         struct group_search *state;
1716
1717         state = TALLOC_P(search->mem_ctx, struct group_search);
1718         if (state == NULL) {
1719                 DEBUG(0, ("talloc failed\n"));
1720                 return False;
1721         }
1722
1723         if (!pdb_enum_group_mapping(SID_NAME_DOM_GRP, &state->groups,
1724                                     &state->num_groups, True)) {
1725                 DEBUG(0, ("Could not enum groups\n"));
1726                 return False;
1727         }
1728
1729         state->current_group = 0;
1730         search->private = state;
1731         return True;
1732 }
1733
1734 static BOOL pdb_search_next_entry_group(struct pdb_search *s,
1735                                         struct samr_displayentry *entry)
1736 {
1737         struct group_search *state = s->private;
1738         uint32 rid;
1739         GROUP_MAP *map = &state->groups[state->current_group];
1740
1741         if (state->current_group == state->num_groups)
1742                 return False;
1743
1744         sid_peek_rid(&map->sid, &rid);
1745
1746         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1747                           entry);
1748
1749         state->current_group += 1;
1750         return True;
1751 }
1752
1753 static void pdb_search_end_groups(struct pdb_search *search)
1754 {
1755         struct group_search *state = search->private;
1756         SAFE_FREE(state->groups);
1757 }
1758
1759 struct alias_search {
1760         GROUP_MAP *aliases;
1761         int num_aliases, current_alias;
1762 };
1763
1764 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1765                                        struct pdb_search *search,
1766                                        const DOM_SID *sid)
1767 {
1768         struct alias_search *state;
1769         enum SID_NAME_USE type = SID_NAME_UNKNOWN;
1770
1771         if (sid_equal(sid, get_global_sam_sid()))
1772                 type = SID_NAME_ALIAS;
1773
1774         if (sid_equal(sid, &global_sid_Builtin))
1775                 type = SID_NAME_WKN_GRP;
1776
1777         if (type == SID_NAME_UNKNOWN) {
1778                 DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1779                 return False;
1780         }
1781
1782         state = TALLOC_P(search->mem_ctx, struct alias_search);
1783         if (state == NULL) {
1784                 DEBUG(0, ("talloc failed\n"));
1785                 return False;
1786         }
1787
1788         if (!pdb_enum_group_mapping(type, &state->aliases,
1789                                     &state->num_aliases, False)) {
1790                 DEBUG(0, ("Could not enum aliases\n"));
1791                 return False;
1792         }
1793
1794         state->current_alias = 0;
1795         search->private = state;
1796         return True;
1797 }
1798
1799 static BOOL pdb_search_next_entry_alias(struct pdb_search *s,
1800                                         struct samr_displayentry *entry)
1801 {
1802         struct alias_search *state = s->private;
1803         uint32 rid;
1804         GROUP_MAP *map = &state->aliases[state->current_alias];
1805
1806         if (state->current_alias == state->num_aliases)
1807                 return False;
1808
1809         sid_peek_rid(&map->sid, &rid);
1810
1811         fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1812                           entry);
1813
1814         state->current_alias += 1;
1815         return True;
1816 }
1817
1818 static void pdb_search_end_aliases(struct pdb_search *search)
1819 {
1820         struct alias_search *state = search->private;
1821         SAFE_FREE(state->aliases);
1822 }
1823
1824 static BOOL pdb_default_search_next_entry(struct pdb_methods *pdb_methods,
1825                                           struct pdb_search *search,
1826                                           struct samr_displayentry *entry)
1827 {
1828         BOOL result = False;
1829         switch (search->type) {
1830         case PDB_USER_SEARCH:
1831                 result = pdb_search_next_entry_users(search, entry);
1832                 break;
1833         case PDB_GROUP_SEARCH:
1834                 result = pdb_search_next_entry_group(search, entry);
1835                 break;
1836         case PDB_ALIAS_SEARCH:
1837                 result = pdb_search_next_entry_alias(search, entry);
1838                 break;
1839         default:
1840                 DEBUG(0, ("unknown search type: %d\n", search->type));
1841                 break;
1842         }
1843         return result;
1844 }
1845
1846 static BOOL pdb_search_next_entry(struct pdb_search *search,
1847                                   struct samr_displayentry *entry)
1848 {
1849         struct pdb_context *pdb_context = pdb_get_static_context(False);
1850
1851         if (pdb_context == NULL) return False;
1852
1853         return pdb_context->pdb_search_next_entry(pdb_context, search, entry);
1854 }
1855
1856 static void pdb_default_search_end(struct pdb_methods *pdb_methods,
1857                                    struct pdb_search *search)
1858 {
1859         switch (search->type) {
1860         case PDB_USER_SEARCH:
1861                 pdb_search_end_users(search);
1862                 break;
1863         case PDB_GROUP_SEARCH:
1864                 pdb_search_end_groups(search);
1865                 break;
1866         case PDB_ALIAS_SEARCH:
1867                 pdb_search_end_aliases(search);
1868                 break;
1869         default:
1870                 DEBUG(0, ("unknown search type: %d\n", search->type));
1871                 break;
1872         }
1873 }
1874
1875 static void pdb_search_end(struct pdb_search *search)
1876 {
1877         struct pdb_context *pdb_context = pdb_get_static_context(False);
1878
1879         if (pdb_context == NULL) return;
1880
1881         pdb_context->pdb_search_end(pdb_context, search);
1882 }
1883
1884 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1885                                                      uint32 idx)
1886 {
1887         if (idx < search->num_entries)
1888                 return &search->cache[idx];
1889
1890         if (search->search_ended)
1891                 return NULL;
1892
1893         while (idx >= search->num_entries) {
1894                 struct samr_displayentry entry;
1895
1896                 if (!pdb_search_next_entry(search, &entry)) {
1897                         pdb_search_end(search);
1898                         search->search_ended = True;
1899                         break;
1900                 }
1901
1902                 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1903                                    entry, &search->cache, &search->num_entries,
1904                                    &search->cache_size);
1905         }
1906
1907         return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1908 }
1909
1910 struct pdb_search *pdb_search_users(uint16 acct_flags)
1911 {
1912         struct pdb_context *pdb_context = pdb_get_static_context(False);
1913         struct pdb_search *result;
1914
1915         if (pdb_context == NULL) return NULL;
1916
1917         result = pdb_search_init(PDB_USER_SEARCH);
1918         if (result == NULL) return NULL;
1919
1920         if (!pdb_context->pdb_search_users(pdb_context, result, acct_flags)) {
1921                 talloc_destroy(result->mem_ctx);
1922                 return NULL;
1923         }
1924         return result;
1925 }
1926
1927 struct pdb_search *pdb_search_groups(void)
1928 {
1929         struct pdb_context *pdb_context = pdb_get_static_context(False);
1930         struct pdb_search *result;
1931
1932         if (pdb_context == NULL) return NULL;
1933
1934         result = pdb_search_init(PDB_GROUP_SEARCH);
1935         if (result == NULL) return NULL;
1936
1937         if (!pdb_context->pdb_search_groups(pdb_context, result)) {
1938                 talloc_destroy(result->mem_ctx);
1939                 return NULL;
1940         }
1941         return result;
1942 }
1943
1944 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1945 {
1946         struct pdb_context *pdb_context = pdb_get_static_context(False);
1947         struct pdb_search *result;
1948
1949         if (pdb_context == NULL) return NULL;
1950
1951         result = pdb_search_init(PDB_ALIAS_SEARCH);
1952         if (result == NULL) return NULL;
1953
1954         if (!pdb_context->pdb_search_aliases(pdb_context, result, sid)) {
1955                 talloc_destroy(result->mem_ctx);
1956                 return NULL;
1957         }
1958         return result;
1959 }
1960
1961 uint32 pdb_search_entries(struct pdb_search *search,
1962                           uint32 start_idx, uint32 max_entries,
1963                           struct samr_displayentry **result)
1964 {
1965         struct samr_displayentry *end_entry;
1966         uint32 end_idx = start_idx+max_entries-1;
1967
1968         /* The first entry needs to be searched after the last. Otherwise the
1969          * first entry might have moved due to a realloc during the search for
1970          * the last entry. */
1971
1972         end_entry = pdb_search_getentry(search, end_idx);
1973         *result = pdb_search_getentry(search, start_idx);
1974
1975         if (end_entry != NULL)
1976                 return max_entries;
1977
1978         if (start_idx >= search->num_entries)
1979                 return 0;
1980
1981         return search->num_entries - start_idx;
1982 }
1983
1984 void pdb_search_destroy(struct pdb_search *search)
1985 {
1986         if (search == NULL)
1987                 return;
1988
1989         if (!search->search_ended)
1990                 pdb_search_end(search);
1991
1992         talloc_destroy(search->mem_ctx);
1993 }
1994
1995 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods) 
1996 {
1997         *methods = TALLOC_P(mem_ctx, struct pdb_methods);
1998
1999         if (!*methods) {
2000                 return NT_STATUS_NO_MEMORY;
2001         }
2002
2003         ZERO_STRUCTP(*methods);
2004
2005         (*methods)->setsampwent = pdb_default_setsampwent;
2006         (*methods)->endsampwent = pdb_default_endsampwent;
2007         (*methods)->getsampwent = pdb_default_getsampwent;
2008         (*methods)->getsampwnam = pdb_default_getsampwnam;
2009         (*methods)->getsampwsid = pdb_default_getsampwsid;
2010         (*methods)->add_sam_account = pdb_default_add_sam_account;
2011         (*methods)->update_sam_account = pdb_default_update_sam_account;
2012         (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2013         (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2014
2015         (*methods)->getgrsid = pdb_default_getgrsid;
2016         (*methods)->getgrgid = pdb_default_getgrgid;
2017         (*methods)->getgrnam = pdb_default_getgrnam;
2018         (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2019         (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2020         (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2021         (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2022         (*methods)->enum_group_members = pdb_default_enum_group_members;
2023         (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2024         (*methods)->find_alias = pdb_default_find_alias;
2025         (*methods)->create_alias = pdb_default_create_alias;
2026         (*methods)->delete_alias = pdb_default_delete_alias;
2027         (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2028         (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2029         (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2030         (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2031         (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2032         (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2033         (*methods)->lookup_rids = pdb_default_lookup_rids;
2034         (*methods)->search_users = pdb_default_search_users;
2035         (*methods)->search_groups = pdb_default_search_groups;
2036         (*methods)->search_aliases = pdb_default_search_aliases;
2037         (*methods)->search_next_entry = pdb_default_search_next_entry;
2038         (*methods)->search_end = pdb_default_search_end;
2039
2040         return NT_STATUS_OK;
2041 }