r6370: get volker's latest passdb changes from 3.0
[Samba.git] / source / passdb / pdb_interface.c
blobc6880b1d50babcefb68a8e3884f3f8e3a01c5c13
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
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.
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.
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.
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_PASSDB
28 static struct pdb_init_function_entry *backends = NULL;
30 static void lazy_initialize_passdb(void)
32 static BOOL initialized = False;
33 if(initialized)return;
34 static_init_pdb;
35 initialized = True;
38 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name);
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 ********************************************************************/
49 static void pdb_force_pw_initialization(SAM_ACCOUNT *pass)
51 const uint8 *lm_pwd, *nt_pwd;
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 */
57 if ( (pdb_get_init_flags(pass, PDB_PASSLASTSET) != PDB_DEFAULT)
58 && (pdb_get_pass_last_set_time(pass) == 0) )
61 if (pdb_get_init_flags(pass, PDB_LMPASSWD) != PDB_DEFAULT)
63 lm_pwd = pdb_get_lanman_passwd(pass);
64 if (lm_pwd)
65 pdb_set_lanman_passwd(pass, NULL, PDB_CHANGED);
67 if (pdb_get_init_flags(pass, PDB_NTPASSWD) != PDB_DEFAULT)
69 nt_pwd = pdb_get_nt_passwd(pass);
70 if (nt_pwd)
71 pdb_set_nt_passwd(pass, NULL, PDB_CHANGED);
75 return;
78 NTSTATUS smb_register_passdb(int version, const char *name, pdb_init_function init)
80 struct pdb_init_function_entry *entry = backends;
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;
90 if (!name || !init) {
91 return NT_STATUS_INVALID_PARAMETER;
94 DEBUG(5,("Attempting to register passdb backend %s\n", name));
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;
102 entry = SMB_XMALLOC_P(struct pdb_init_function_entry);
103 entry->name = smb_xstrdup(name);
104 entry->init = init;
106 DLIST_ADD(backends, entry);
107 DEBUG(5,("Successfully added passdb backend '%s'\n", name));
108 return NT_STATUS_OK;
111 static struct pdb_init_function_entry *pdb_find_backend_entry(const char *name)
113 struct pdb_init_function_entry *entry = backends;
115 while(entry) {
116 if (strcmp(entry->name, name)==0) return entry;
117 entry = entry->next;
120 return NULL;
123 static NTSTATUS context_setsampwent(struct pdb_context *context, BOOL update, uint16 acb_mask)
125 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
127 if (!context) {
128 DEBUG(0, ("invalid pdb_context specified!\n"));
129 return ret;
132 context->pwent_methods = context->pdb_methods;
134 if (!context->pwent_methods) {
135 /* No passdbs at all */
136 return ret;
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;
144 return ret;
147 static void context_endsampwent(struct pdb_context *context)
149 if ((!context)){
150 DEBUG(0, ("invalid pdb_context specified!\n"));
151 return;
154 if (context->pwent_methods && context->pwent_methods->endsampwent)
155 context->pwent_methods->endsampwent(context->pwent_methods);
157 /* So we won't get strange data when calling getsampwent now */
158 context->pwent_methods = NULL;
161 static NTSTATUS context_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
163 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
165 if ((!context) || (!context->pwent_methods)) {
166 DEBUG(0, ("invalid pdb_context specified!\n"));
167 return ret;
169 /* Loop until we find something useful */
170 while (NT_STATUS_IS_ERR(ret = context->pwent_methods->getsampwent(context->pwent_methods, user))) {
172 context->pwent_methods->endsampwent(context->pwent_methods);
174 context->pwent_methods = context->pwent_methods->next;
176 /* All methods are checked now. There are no more entries */
177 if (context->pwent_methods == NULL)
178 return ret;
180 context->pwent_methods->setsampwent(context->pwent_methods, False, 0);
182 user->methods = context->pwent_methods;
183 pdb_force_pw_initialization(user);
184 return ret;
187 static NTSTATUS context_getsampwnam(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const char *username)
189 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
191 struct pdb_methods *curmethods;
192 if ((!context)) {
193 DEBUG(0, ("invalid pdb_context specified!\n"));
194 return ret;
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;
203 curmethods = curmethods->next;
206 return ret;
209 static NTSTATUS context_getsampwsid(struct pdb_context *context, SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
211 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
213 struct pdb_methods *curmethods;
214 if ((!context)) {
215 DEBUG(0, ("invalid pdb_context specified!\n"));
216 return ret;
219 curmethods = context->pdb_methods;
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;
227 curmethods = curmethods->next;
230 return ret;
233 static NTSTATUS context_add_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
235 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
236 const uint8 *lm_pw, *nt_pw;
237 uint16 acb_flags;
239 if ((!context) || (!context->pdb_methods)) {
240 DEBUG(0, ("invalid pdb_context specified!\n"));
241 return ret;
244 /* disable acccounts with no passwords (that has not
245 been allowed by the ACB_PWNOTREQ bit */
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 );
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? */
259 return context->pdb_methods->add_sam_account(context->pdb_methods, sam_acct);
262 static NTSTATUS context_update_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
264 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
265 const uint8 *lm_pw, *nt_pw;
266 uint16 acb_flags;
268 if (!context) {
269 DEBUG(0, ("invalid pdb_context specified!\n"));
270 return ret;
273 if (!sam_acct || !sam_acct->methods){
274 DEBUG(0, ("invalid sam_acct specified\n"));
275 return ret;
278 /* disable acccounts with no passwords (that has not
279 been allowed by the ACB_PWNOTREQ bit */
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 );
289 /** @todo This is where a 're-read on update' should be done */
291 return sam_acct->methods->update_sam_account(sam_acct->methods, sam_acct);
294 static NTSTATUS context_delete_sam_account(struct pdb_context *context, SAM_ACCOUNT *sam_acct)
296 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
298 struct pdb_methods *pdb_selected;
299 if (!context) {
300 DEBUG(0, ("invalid pdb_context specified!\n"));
301 return ret;
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.
311 while (pdb_selected){
312 if (NT_STATUS_IS_OK(ret = pdb_selected->delete_sam_account(pdb_selected, sam_acct))) {
313 return ret;
315 pdb_selected = pdb_selected->next;
317 return ret;
320 if (!sam_acct->methods->delete_sam_account){
321 DEBUG(0,("invalid sam_acct->methods->delete_sam_account\n"));
322 return ret;
325 return sam_acct->methods->delete_sam_account(sam_acct->methods, sam_acct);
328 static NTSTATUS context_update_login_attempts(struct pdb_context *context,
329 SAM_ACCOUNT *sam_acct, BOOL success)
331 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
333 if (!context) {
334 DEBUG(0, ("invalid pdb_context specified!\n"));
335 return ret;
338 if (!sam_acct || !sam_acct->methods){
339 DEBUG(0, ("invalid sam_acct specified\n"));
340 return ret;
343 return sam_acct->methods->update_login_attempts(sam_acct->methods, sam_acct, success);
346 static NTSTATUS context_getgrsid(struct pdb_context *context,
347 GROUP_MAP *map, DOM_SID sid)
349 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
351 struct pdb_methods *curmethods;
352 if ((!context)) {
353 DEBUG(0, ("invalid pdb_context specified!\n"));
354 return ret;
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;
363 curmethods = curmethods->next;
366 return ret;
369 static NTSTATUS context_getgrgid(struct pdb_context *context,
370 GROUP_MAP *map, gid_t gid)
372 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
374 struct pdb_methods *curmethods;
375 if ((!context)) {
376 DEBUG(0, ("invalid pdb_context specified!\n"));
377 return ret;
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;
386 curmethods = curmethods->next;
389 return ret;
392 static NTSTATUS context_getgrnam(struct pdb_context *context,
393 GROUP_MAP *map, const char *name)
395 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
397 struct pdb_methods *curmethods;
398 if ((!context)) {
399 DEBUG(0, ("invalid pdb_context specified!\n"));
400 return ret;
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;
409 curmethods = curmethods->next;
412 return ret;
415 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
416 GROUP_MAP *map)
418 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
420 if ((!context) || (!context->pdb_methods)) {
421 DEBUG(0, ("invalid pdb_context specified!\n"));
422 return ret;
425 return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
426 map);
429 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
430 GROUP_MAP *map)
432 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
434 if ((!context) || (!context->pdb_methods)) {
435 DEBUG(0, ("invalid pdb_context specified!\n"));
436 return ret;
439 return context->
440 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
443 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
444 DOM_SID sid)
446 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
448 if ((!context) || (!context->pdb_methods)) {
449 DEBUG(0, ("invalid pdb_context specified!\n"));
450 return ret;
453 return context->
454 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
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)
462 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
464 if ((!context) || (!context->pdb_methods)) {
465 DEBUG(0, ("invalid pdb_context specified!\n"));
466 return ret;
469 return context->pdb_methods->enum_group_mapping(context->pdb_methods,
470 sid_name_use, rmap,
471 num_entries, unix_only);
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)
480 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
482 if ((!context) || (!context->pdb_methods)) {
483 DEBUG(0, ("invalid pdb_context specified!\n"));
484 return ret;
487 return context->pdb_methods->enum_group_members(context->pdb_methods,
488 mem_ctx, group,
489 member_rids,
490 num_members);
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)
499 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
501 if ((!context) || (!context->pdb_methods)) {
502 DEBUG(0, ("invalid pdb_context specified!\n"));
503 return ret;
506 return context->pdb_methods->
507 enum_group_memberships(context->pdb_methods, username,
508 primary_gid, sids, gids, num_groups);
511 static NTSTATUS context_find_alias(struct pdb_context *context,
512 const char *name, DOM_SID *sid)
514 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
516 if ((!context) || (!context->pdb_methods)) {
517 DEBUG(0, ("invalid pdb_context specified!\n"));
518 return ret;
521 return context->pdb_methods->find_alias(context->pdb_methods,
522 name, sid);
525 static NTSTATUS context_create_alias(struct pdb_context *context,
526 const char *name, uint32 *rid)
528 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
530 if ((!context) || (!context->pdb_methods)) {
531 DEBUG(0, ("invalid pdb_context specified!\n"));
532 return ret;
535 return context->pdb_methods->create_alias(context->pdb_methods,
536 name, rid);
539 static NTSTATUS context_delete_alias(struct pdb_context *context,
540 const DOM_SID *sid)
542 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
544 if ((!context) || (!context->pdb_methods)) {
545 DEBUG(0, ("invalid pdb_context specified!\n"));
546 return ret;
549 return context->pdb_methods->delete_alias(context->pdb_methods, sid);
552 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
553 const DOM_SID *sid,
554 struct acct_info *info)
556 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
558 if ((!context) || (!context->pdb_methods)) {
559 DEBUG(0, ("invalid pdb_context specified!\n"));
560 return ret;
563 return context->pdb_methods->get_aliasinfo(context->pdb_methods,
564 sid, info);
567 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
568 const DOM_SID *sid,
569 struct acct_info *info)
571 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
573 if ((!context) || (!context->pdb_methods)) {
574 DEBUG(0, ("invalid pdb_context specified!\n"));
575 return ret;
578 return context->pdb_methods->set_aliasinfo(context->pdb_methods,
579 sid, info);
582 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
583 const DOM_SID *alias,
584 const DOM_SID *member)
586 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
588 if ((!context) || (!context->pdb_methods)) {
589 DEBUG(0, ("invalid pdb_context specified!\n"));
590 return ret;
593 return context->pdb_methods->add_aliasmem(context->pdb_methods,
594 alias, member);
597 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
598 const DOM_SID *alias,
599 const DOM_SID *member)
601 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
603 if ((!context) || (!context->pdb_methods)) {
604 DEBUG(0, ("invalid pdb_context specified!\n"));
605 return ret;
608 return context->pdb_methods->del_aliasmem(context->pdb_methods,
609 alias, member);
612 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
613 const DOM_SID *alias, DOM_SID **members,
614 int *num)
616 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
618 if ((!context) || (!context->pdb_methods)) {
619 DEBUG(0, ("invalid pdb_context specified!\n"));
620 return ret;
623 return context->pdb_methods->enum_aliasmem(context->pdb_methods,
624 alias, members, num);
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)
635 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
637 if ((!context) || (!context->pdb_methods)) {
638 DEBUG(0, ("invalid pdb_context specified!\n"));
639 return ret;
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);
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)
656 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
658 if ((!context) || (!context->pdb_methods)) {
659 DEBUG(0, ("invalid pdb_context specified!\n"));
660 return ret;
663 return context->pdb_methods->lookup_rids(context->pdb_methods,
664 mem_ctx, domain_sid, num_rids,
665 rids, names, attrs);
668 static BOOL context_search_users(struct pdb_context *context,
669 struct pdb_search *search, uint16 acct_flags)
671 if ((!context) || (!context->pdb_methods)) {
672 DEBUG(0, ("invalid pdb_context specified!\n"));
673 return False;
676 return context->pdb_methods->search_users(context->pdb_methods,
677 search, acct_flags);
680 static BOOL context_search_groups(struct pdb_context *context,
681 struct pdb_search *search)
683 if ((!context) || (!context->pdb_methods)) {
684 DEBUG(0, ("invalid pdb_context specified!\n"));
685 return False;
688 return context->pdb_methods->search_groups(context->pdb_methods,
689 search);
692 static BOOL context_search_aliases(struct pdb_context *context,
693 struct pdb_search *search,
694 const DOM_SID *sid)
696 if ((!context) || (!context->pdb_methods)) {
697 DEBUG(0, ("invalid pdb_context specified!\n"));
698 return False;
701 return context->pdb_methods->search_aliases(context->pdb_methods,
702 search, sid);
705 /******************************************************************
706 Free and cleanup a pdb context, any associated data and anything
707 that the attached modules might have associated.
708 *******************************************************************/
710 static void free_pdb_context(struct pdb_context **context)
712 struct pdb_methods *pdb_selected = (*context)->pdb_methods;
714 while (pdb_selected){
715 if(pdb_selected->free_private_data)
716 pdb_selected->free_private_data(&(pdb_selected->private_data));
717 pdb_selected = pdb_selected->next;
720 talloc_destroy((*context)->mem_ctx);
721 *context = NULL;
724 /******************************************************************
725 Make a pdb_methods from scratch
726 *******************************************************************/
728 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
730 char *module_name = smb_xstrdup(selected);
731 char *module_location = NULL, *p;
732 struct pdb_init_function_entry *entry;
733 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
735 lazy_initialize_passdb();
737 p = strchr(module_name, ':');
739 if (p) {
740 *p = 0;
741 module_location = p+1;
742 trim_char(module_location, ' ', ' ');
745 trim_char(module_name, ' ', ' ');
748 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
750 entry = pdb_find_backend_entry(module_name);
752 /* Try to find a module that contains this module */
753 if (!entry) {
754 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
755 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
756 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
757 SAFE_FREE(module_name);
758 return NT_STATUS_UNSUCCESSFUL;
762 /* No such backend found */
763 if(!entry) {
764 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
765 SAFE_FREE(module_name);
766 return NT_STATUS_INVALID_PARAMETER;
769 DEBUG(5,("Found pdb backend %s\n", module_name));
770 nt_status = entry->init(context, methods, module_location);
771 if (NT_STATUS_IS_OK(nt_status)) {
772 DEBUG(5,("pdb backend %s has a valid init\n", selected));
773 } else {
774 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
776 SAFE_FREE(module_name);
777 return nt_status;
780 /******************************************************************
781 Make a pdb_context from scratch.
782 *******************************************************************/
784 static NTSTATUS make_pdb_context(struct pdb_context **context)
786 TALLOC_CTX *mem_ctx;
788 mem_ctx = talloc_init("pdb_context internal allocation context");
790 if (!mem_ctx) {
791 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
792 return NT_STATUS_NO_MEMORY;
795 *context = TALLOC_P(mem_ctx, struct pdb_context);
796 if (!*context) {
797 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
798 return NT_STATUS_NO_MEMORY;
801 ZERO_STRUCTP(*context);
803 (*context)->mem_ctx = mem_ctx;
805 (*context)->pdb_setsampwent = context_setsampwent;
806 (*context)->pdb_endsampwent = context_endsampwent;
807 (*context)->pdb_getsampwent = context_getsampwent;
808 (*context)->pdb_getsampwnam = context_getsampwnam;
809 (*context)->pdb_getsampwsid = context_getsampwsid;
810 (*context)->pdb_add_sam_account = context_add_sam_account;
811 (*context)->pdb_update_sam_account = context_update_sam_account;
812 (*context)->pdb_delete_sam_account = context_delete_sam_account;
813 (*context)->pdb_update_login_attempts = context_update_login_attempts;
814 (*context)->pdb_getgrsid = context_getgrsid;
815 (*context)->pdb_getgrgid = context_getgrgid;
816 (*context)->pdb_getgrnam = context_getgrnam;
817 (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
818 (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
819 (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
820 (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
821 (*context)->pdb_enum_group_members = context_enum_group_members;
822 (*context)->pdb_enum_group_memberships = context_enum_group_memberships;
824 (*context)->pdb_find_alias = context_find_alias;
825 (*context)->pdb_create_alias = context_create_alias;
826 (*context)->pdb_delete_alias = context_delete_alias;
827 (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
828 (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
829 (*context)->pdb_add_aliasmem = context_add_aliasmem;
830 (*context)->pdb_del_aliasmem = context_del_aliasmem;
831 (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
832 (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
833 (*context)->pdb_lookup_rids = context_lookup_rids;
835 (*context)->pdb_search_users = context_search_users;
836 (*context)->pdb_search_groups = context_search_groups;
837 (*context)->pdb_search_aliases = context_search_aliases;
839 (*context)->free_fn = free_pdb_context;
841 return NT_STATUS_OK;
845 /******************************************************************
846 Make a pdb_context, given an array of strings
847 *******************************************************************/
849 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected)
851 int i = 0;
852 struct pdb_methods *curmethods, *tmpmethods;
853 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
854 BOOL have_guest = False;
856 if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
857 return nt_status;
860 if (!selected) {
861 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
862 return nt_status;
865 while (selected[i]){
866 if (strcmp(selected[i], "guest") == 0) {
867 have_guest = True;
869 /* Try to initialise pdb */
870 DEBUG(5,("Trying to load: %s\n", selected[i]));
871 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
872 DEBUG(1, ("Loading %s failed!\n", selected[i]));
873 free_pdb_context(context);
874 return nt_status;
876 curmethods->parent = *context;
877 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
878 i++;
881 if (have_guest)
882 return NT_STATUS_OK;
884 if ( (lp_guestaccount() == NULL) ||
885 (*lp_guestaccount() == '\0') ) {
886 /* We explicitly don't want guest access. No idea what
887 else that breaks, but be it that way. */
888 return NT_STATUS_OK;
891 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
892 *context,
893 "guest"))) {
894 DEBUG(1, ("Loading guest module failed!\n"));
895 free_pdb_context(context);
896 return nt_status;
899 curmethods->parent = *context;
900 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
902 return NT_STATUS_OK;
905 /******************************************************************
906 Make a pdb_context, given a text string.
907 *******************************************************************/
909 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected)
911 NTSTATUS ret;
912 char **newsel = str_list_make(selected, NULL);
913 ret = make_pdb_context_list(context, (const char **)newsel);
914 str_list_free(&newsel);
915 return ret;
918 /******************************************************************
919 Return an already initialised pdb_context, to facilitate backward
920 compatibility (see functions below).
921 *******************************************************************/
923 static struct pdb_context *pdb_get_static_context(BOOL reload)
925 static struct pdb_context *pdb_context = NULL;
927 if ((pdb_context) && (reload)) {
928 pdb_context->free_fn(&pdb_context);
929 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
930 return NULL;
934 if (!pdb_context) {
935 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
936 return NULL;
940 return pdb_context;
943 /******************************************************************
944 Backward compatibility functions for the original passdb interface
945 *******************************************************************/
947 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
949 struct pdb_context *pdb_context = pdb_get_static_context(False);
951 if (!pdb_context) {
952 return False;
955 return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update, acb_mask));
958 void pdb_endsampwent(void)
960 struct pdb_context *pdb_context = pdb_get_static_context(False);
962 if (!pdb_context) {
963 return;
966 pdb_context->pdb_endsampwent(pdb_context);
969 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
971 struct pdb_context *pdb_context = pdb_get_static_context(False);
973 if (!pdb_context) {
974 return False;
977 return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
980 static SAM_ACCOUNT *sam_account_cache = NULL;
982 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
984 struct pdb_context *pdb_context = pdb_get_static_context(False);
986 if (!pdb_context) {
987 return False;
990 if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
991 sam_acct, username)))
992 return False;
994 if (sam_account_cache != NULL) {
995 pdb_free_sam(&sam_account_cache);
996 sam_account_cache = NULL;
999 pdb_copy_sam_account(sam_acct, &sam_account_cache);
1000 return True;
1003 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
1005 struct pdb_context *pdb_context = pdb_get_static_context(False);
1007 if (!pdb_context) {
1008 return False;
1011 if ((sam_account_cache != NULL) &&
1012 (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
1013 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
1015 return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1018 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
1020 struct pdb_context *pdb_context = pdb_get_static_context(False);
1022 if (!pdb_context) {
1023 return False;
1026 return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1029 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
1031 struct pdb_context *pdb_context = pdb_get_static_context(False);
1033 if (!pdb_context) {
1034 return False;
1037 if (sam_account_cache != NULL) {
1038 pdb_free_sam(&sam_account_cache);
1039 sam_account_cache = NULL;
1042 return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1045 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
1047 struct pdb_context *pdb_context = pdb_get_static_context(False);
1049 if (!pdb_context) {
1050 return False;
1053 if (sam_account_cache != NULL) {
1054 pdb_free_sam(&sam_account_cache);
1055 sam_account_cache = NULL;
1058 return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1061 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
1063 struct pdb_context *pdb_context = pdb_get_static_context(False);
1065 if (!pdb_context) {
1066 return NT_STATUS_NOT_IMPLEMENTED;
1069 return pdb_context->pdb_update_login_attempts(pdb_context, sam_acct, success);
1072 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1074 struct pdb_context *pdb_context = pdb_get_static_context(False);
1076 if (!pdb_context) {
1077 return False;
1080 return NT_STATUS_IS_OK(pdb_context->
1081 pdb_getgrsid(pdb_context, map, sid));
1084 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1086 struct pdb_context *pdb_context = pdb_get_static_context(False);
1088 if (!pdb_context) {
1089 return False;
1092 return NT_STATUS_IS_OK(pdb_context->
1093 pdb_getgrgid(pdb_context, map, gid));
1096 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1098 struct pdb_context *pdb_context = pdb_get_static_context(False);
1100 if (!pdb_context) {
1101 return False;
1104 return NT_STATUS_IS_OK(pdb_context->
1105 pdb_getgrnam(pdb_context, map, name));
1108 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1110 struct pdb_context *pdb_context = pdb_get_static_context(False);
1112 if (!pdb_context) {
1113 return False;
1116 return NT_STATUS_IS_OK(pdb_context->
1117 pdb_add_group_mapping_entry(pdb_context, map));
1120 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1122 struct pdb_context *pdb_context = pdb_get_static_context(False);
1124 if (!pdb_context) {
1125 return False;
1128 return NT_STATUS_IS_OK(pdb_context->
1129 pdb_update_group_mapping_entry(pdb_context, map));
1132 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1134 struct pdb_context *pdb_context = pdb_get_static_context(False);
1136 if (!pdb_context) {
1137 return False;
1140 return NT_STATUS_IS_OK(pdb_context->
1141 pdb_delete_group_mapping_entry(pdb_context, sid));
1144 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap,
1145 int *num_entries, BOOL unix_only)
1147 struct pdb_context *pdb_context = pdb_get_static_context(False);
1149 if (!pdb_context) {
1150 return False;
1153 return NT_STATUS_IS_OK(pdb_context->
1154 pdb_enum_group_mapping(pdb_context, sid_name_use,
1155 rmap, num_entries, unix_only));
1158 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
1159 const DOM_SID *sid,
1160 uint32 **member_rids,
1161 int *num_members)
1163 struct pdb_context *pdb_context = pdb_get_static_context(False);
1165 if (!pdb_context) {
1166 return NT_STATUS_UNSUCCESSFUL;
1169 return pdb_context->pdb_enum_group_members(pdb_context, mem_ctx, sid,
1170 member_rids, num_members);
1173 NTSTATUS pdb_enum_group_memberships(const char *username, gid_t primary_gid,
1174 DOM_SID **sids, gid_t **gids,
1175 int *num_groups)
1177 struct pdb_context *pdb_context = pdb_get_static_context(False);
1179 if (!pdb_context) {
1180 return NT_STATUS_UNSUCCESSFUL;
1183 return pdb_context->pdb_enum_group_memberships(pdb_context, username,
1184 primary_gid, sids, gids,
1185 num_groups);
1188 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1190 struct pdb_context *pdb_context = pdb_get_static_context(False);
1192 if (!pdb_context) {
1193 return False;
1196 return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1197 name, sid));
1200 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1202 struct pdb_context *pdb_context = pdb_get_static_context(False);
1204 if (!pdb_context) {
1205 return NT_STATUS_NOT_IMPLEMENTED;
1208 return pdb_context->pdb_create_alias(pdb_context, name, rid);
1211 BOOL pdb_delete_alias(const DOM_SID *sid)
1213 struct pdb_context *pdb_context = pdb_get_static_context(False);
1215 if (!pdb_context) {
1216 return False;
1219 return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1220 sid));
1224 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1226 struct pdb_context *pdb_context = pdb_get_static_context(False);
1228 if (!pdb_context) {
1229 return False;
1232 return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1233 info));
1236 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1238 struct pdb_context *pdb_context = pdb_get_static_context(False);
1240 if (!pdb_context) {
1241 return False;
1244 return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1245 info));
1248 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1250 struct pdb_context *pdb_context = pdb_get_static_context(False);
1252 if (!pdb_context) {
1253 return False;
1256 return NT_STATUS_IS_OK(pdb_context->
1257 pdb_add_aliasmem(pdb_context, alias, member));
1260 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1262 struct pdb_context *pdb_context = pdb_get_static_context(False);
1264 if (!pdb_context) {
1265 return False;
1268 return NT_STATUS_IS_OK(pdb_context->
1269 pdb_del_aliasmem(pdb_context, alias, member));
1272 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1273 DOM_SID **members, int *num_members)
1275 struct pdb_context *pdb_context = pdb_get_static_context(False);
1277 if (!pdb_context) {
1278 return False;
1281 return NT_STATUS_IS_OK(pdb_context->
1282 pdb_enum_aliasmem(pdb_context, alias,
1283 members, num_members));
1286 BOOL pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
1287 const DOM_SID *members, int num_members,
1288 uint32 **alias_rids, int *num_alias_rids)
1290 struct pdb_context *pdb_context = pdb_get_static_context(False);
1292 if (!pdb_context) {
1293 return False;
1296 return NT_STATUS_IS_OK(pdb_context->
1297 pdb_enum_alias_memberships(pdb_context, mem_ctx,
1298 domain_sid,
1299 members, num_members,
1300 alias_rids,
1301 num_alias_rids));
1304 NTSTATUS pdb_lookup_rids(TALLOC_CTX *mem_ctx,
1305 const DOM_SID *domain_sid,
1306 int num_rids,
1307 uint32 *rids,
1308 const char ***names,
1309 uint32 **attrs)
1311 struct pdb_context *pdb_context = pdb_get_static_context(False);
1313 if (!pdb_context) {
1314 return NT_STATUS_NOT_IMPLEMENTED;
1317 return pdb_context->pdb_lookup_rids(pdb_context, mem_ctx, domain_sid,
1318 num_rids, rids, names, attrs);
1321 /***************************************************************
1322 Initialize the static context (at smbd startup etc).
1324 If uninitialised, context will auto-init on first use.
1325 ***************************************************************/
1327 BOOL initialize_password_db(BOOL reload)
1329 return (pdb_get_static_context(reload) != NULL);
1333 /***************************************************************************
1334 Default implementations of some functions.
1335 ****************************************************************************/
1337 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1339 return NT_STATUS_NO_SUCH_USER;
1342 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1344 return NT_STATUS_NO_SUCH_USER;
1347 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1349 DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1350 return NT_STATUS_NOT_IMPLEMENTED;
1353 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1355 return NT_STATUS_NOT_IMPLEMENTED;
1358 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1360 return NT_STATUS_NOT_IMPLEMENTED;
1363 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1365 return NT_STATUS_OK;
1368 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1370 return NT_STATUS_NOT_IMPLEMENTED;
1373 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1375 return NT_STATUS_NOT_IMPLEMENTED;
1378 static void pdb_default_endsampwent(struct pdb_methods *methods)
1380 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1383 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1384 uid_t uid, uid_t **uids, int *num)
1386 int i;
1388 for (i=0; i<*num; i++) {
1389 if ((*uids)[i] == uid)
1390 return;
1393 *uids = TALLOC_REALLOC_ARRAY(mem_ctx, *uids, uid_t, *num+1);
1395 if (*uids == NULL)
1396 return;
1398 (*uids)[*num] = uid;
1399 *num += 1;
1402 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **uids,
1403 int *num)
1405 struct group *grp;
1406 char **gr;
1407 struct sys_pwent *userlist, *user;
1409 *uids = NULL;
1410 *num = 0;
1412 /* We only look at our own sam, so don't care about imported stuff */
1414 winbind_off();
1416 if ((grp = getgrgid(gid)) == NULL) {
1417 winbind_on();
1418 return False;
1421 /* Primary group members */
1423 userlist = getpwent_list();
1425 for (user = userlist; user != NULL; user = user->next) {
1426 if (user->pw_gid != gid)
1427 continue;
1428 add_uid_to_array_unique(mem_ctx, user->pw_uid, uids, num);
1431 pwent_free(userlist);
1433 /* Secondary group members */
1435 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1436 struct passwd *pw = getpwnam(*gr);
1438 if (pw == NULL)
1439 continue;
1440 add_uid_to_array_unique(mem_ctx, pw->pw_uid, uids, num);
1443 winbind_on();
1445 return True;
1448 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1449 TALLOC_CTX *mem_ctx,
1450 const DOM_SID *group,
1451 uint32 **member_rids,
1452 int *num_members)
1454 gid_t gid;
1455 uid_t *uids;
1456 int i, num_uids;
1458 *member_rids = NULL;
1459 *num_members = 0;
1461 if (!NT_STATUS_IS_OK(sid_to_gid(group, &gid)))
1462 return NT_STATUS_NO_SUCH_GROUP;
1464 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1465 return NT_STATUS_NO_SUCH_GROUP;
1467 if (num_uids == 0)
1468 return NT_STATUS_OK;
1470 *member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1472 for (i=0; i<num_uids; i++) {
1473 DOM_SID sid;
1475 if (!NT_STATUS_IS_OK(uid_to_sid(&sid, uids[i]))) {
1476 DEBUG(1, ("Could not map member uid to SID\n"));
1477 continue;
1480 if (!sid_check_is_in_our_domain(&sid)) {
1481 DEBUG(1, ("Inconsistent SAM -- group member uid not "
1482 "in our domain\n"));
1483 continue;
1486 sid_peek_rid(&sid, &(*member_rids)[*num_members]);
1487 *num_members += 1;
1490 return NT_STATUS_OK;
1493 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1494 TALLOC_CTX *mem_ctx,
1495 const DOM_SID *domain_sid,
1496 int num_rids,
1497 uint32 *rids,
1498 const char ***names,
1499 uint32 **attrs)
1501 int i;
1502 NTSTATUS result;
1503 BOOL have_mapped = False;
1504 BOOL have_unmapped = False;
1506 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, const char *, num_rids);
1507 (*attrs) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_rids);
1509 if ((num_rids != 0) && (((*names) == NULL) || ((*attrs) == NULL)))
1510 return NT_STATUS_NO_MEMORY;
1512 if (!sid_equal(domain_sid, get_global_sam_sid())) {
1513 /* TODO: Sooner or later we need to look up BUILTIN rids as
1514 * well. -- vl */
1515 goto done;
1518 for (i = 0; i < num_rids; i++) {
1519 fstring tmpname;
1520 fstring domname;
1521 DOM_SID sid;
1522 enum SID_NAME_USE type;
1524 (*attrs)[i] = SID_NAME_UNKNOWN;
1526 sid_copy(&sid, domain_sid);
1527 sid_append_rid(&sid, rids[i]);
1529 if (lookup_sid(&sid, domname, tmpname, &type)) {
1530 (*attrs)[i] = (uint32)type;
1531 (*names)[i] = talloc_strdup(mem_ctx, tmpname);
1532 if ((*names)[i] == NULL)
1533 return NT_STATUS_NO_MEMORY;
1534 DEBUG(5,("lookup_rids: %s:%d\n", (*names)[i],
1535 (*attrs)[i]));
1536 have_mapped = True;
1537 } else {
1538 have_unmapped = True;
1542 done:
1544 result = NT_STATUS_NONE_MAPPED;
1546 if (have_mapped)
1547 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1549 return result;
1552 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1554 TALLOC_CTX *mem_ctx;
1555 struct pdb_search *result;
1557 mem_ctx = talloc_init("pdb_search");
1558 if (mem_ctx == NULL) {
1559 DEBUG(0, ("talloc_init failed\n"));
1560 return NULL;
1563 result = TALLOC_P(mem_ctx, struct pdb_search);
1564 if (result == NULL) {
1565 DEBUG(0, ("talloc failed\n"));
1566 return NULL;
1569 result->mem_ctx = mem_ctx;
1570 result->type = type;
1571 result->cache = NULL;
1572 result->num_entries = 0;
1573 result->cache_size = 0;
1574 result->search_ended = False;
1576 /* Segfault appropriately if not initialized */
1577 result->next_entry = NULL;
1578 result->search_end = NULL;
1580 return result;
1583 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1584 uint16 acct_flags,
1585 const char *account_name,
1586 const char *fullname,
1587 const char *description,
1588 struct samr_displayentry *entry)
1590 entry->rid = rid;
1591 entry->acct_flags = acct_flags;
1593 if (account_name != NULL)
1594 entry->account_name = talloc_strdup(mem_ctx, account_name);
1595 else
1596 entry->account_name = "";
1598 if (fullname != NULL)
1599 entry->fullname = talloc_strdup(mem_ctx, fullname);
1600 else
1601 entry->fullname = "";
1603 if (description != NULL)
1604 entry->description = talloc_strdup(mem_ctx, description);
1605 else
1606 entry->description = "";
1609 static BOOL user_search_in_progress = False;
1610 struct user_search {
1611 uint16 acct_flags;
1614 static BOOL next_entry_users(struct pdb_search *s,
1615 struct samr_displayentry *entry)
1617 struct user_search *state = s->private;
1618 SAM_ACCOUNT *user = NULL;
1619 NTSTATUS status;
1621 next:
1622 status = pdb_init_sam(&user);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 DEBUG(0, ("Could not pdb_init_sam\n"));
1625 return False;
1628 if (!pdb_getsampwent(user)) {
1629 pdb_free_sam(&user);
1630 return False;
1633 if ((state->acct_flags != 0) &&
1634 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1635 pdb_free_sam(&user);
1636 goto next;
1639 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1640 pdb_get_acct_ctrl(user), pdb_get_username(user),
1641 pdb_get_fullname(user), pdb_get_acct_desc(user),
1642 entry);
1644 pdb_free_sam(&user);
1645 return True;
1648 static void search_end_users(struct pdb_search *search)
1650 pdb_endsampwent();
1651 user_search_in_progress = False;
1654 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1655 struct pdb_search *search,
1656 uint16 acct_flags)
1658 struct user_search *state;
1660 if (user_search_in_progress) {
1661 DEBUG(1, ("user search in progress\n"));
1662 return False;
1665 if (!pdb_setsampwent(False, acct_flags)) {
1666 DEBUG(5, ("Could not start search\n"));
1667 return False;
1670 user_search_in_progress = True;
1672 state = TALLOC_P(search->mem_ctx, struct user_search);
1673 if (state == NULL) {
1674 DEBUG(0, ("talloc failed\n"));
1675 return False;
1678 state->acct_flags = acct_flags;
1680 search->private = state;
1681 search->next_entry = next_entry_users;
1682 search->search_end = search_end_users;
1683 return True;
1686 struct group_search {
1687 GROUP_MAP *groups;
1688 int num_groups, current_group;
1691 static BOOL next_entry_groups(struct pdb_search *s,
1692 struct samr_displayentry *entry)
1694 struct group_search *state = s->private;
1695 uint32 rid;
1696 GROUP_MAP *map = &state->groups[state->current_group];
1698 if (state->current_group == state->num_groups)
1699 return False;
1701 sid_peek_rid(&map->sid, &rid);
1703 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1704 entry);
1706 state->current_group += 1;
1707 return True;
1710 static void search_end_groups(struct pdb_search *search)
1712 struct group_search *state = search->private;
1713 SAFE_FREE(state->groups);
1716 static BOOL pdb_search_grouptype(struct pdb_search *search,
1717 enum SID_NAME_USE type)
1719 struct group_search *state;
1721 state = TALLOC_P(search->mem_ctx, struct group_search);
1722 if (state == NULL) {
1723 DEBUG(0, ("talloc failed\n"));
1724 return False;
1727 if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1728 True)) {
1729 DEBUG(0, ("Could not enum groups\n"));
1730 return False;
1733 state->current_group = 0;
1734 search->private = state;
1735 search->next_entry = next_entry_groups;
1736 search->search_end = search_end_groups;
1737 return True;
1740 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1741 struct pdb_search *search)
1743 return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1746 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1747 struct pdb_search *search,
1748 const DOM_SID *sid)
1751 if (sid_equal(sid, get_global_sam_sid()))
1752 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1754 if (sid_equal(sid, &global_sid_Builtin))
1755 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
1757 DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1758 return False;
1761 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1762 uint32 idx)
1764 if (idx < search->num_entries)
1765 return &search->cache[idx];
1767 if (search->search_ended)
1768 return NULL;
1770 while (idx >= search->num_entries) {
1771 struct samr_displayentry entry;
1773 if (!search->next_entry(search, &entry)) {
1774 search->search_end(search);
1775 search->search_ended = True;
1776 break;
1779 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1780 entry, &search->cache, &search->num_entries,
1781 &search->cache_size);
1784 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1787 struct pdb_search *pdb_search_users(uint16 acct_flags)
1789 struct pdb_context *pdb_context = pdb_get_static_context(False);
1790 struct pdb_search *result;
1792 if (pdb_context == NULL) return NULL;
1794 result = pdb_search_init(PDB_USER_SEARCH);
1795 if (result == NULL) return NULL;
1797 if (!pdb_context->pdb_search_users(pdb_context, result, acct_flags)) {
1798 talloc_destroy(result->mem_ctx);
1799 return NULL;
1801 return result;
1804 struct pdb_search *pdb_search_groups(void)
1806 struct pdb_context *pdb_context = pdb_get_static_context(False);
1807 struct pdb_search *result;
1809 if (pdb_context == NULL) return NULL;
1811 result = pdb_search_init(PDB_GROUP_SEARCH);
1812 if (result == NULL) return NULL;
1814 if (!pdb_context->pdb_search_groups(pdb_context, result)) {
1815 talloc_destroy(result->mem_ctx);
1816 return NULL;
1818 return result;
1821 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1823 struct pdb_context *pdb_context = pdb_get_static_context(False);
1824 struct pdb_search *result;
1826 if (pdb_context == NULL) return NULL;
1828 result = pdb_search_init(PDB_ALIAS_SEARCH);
1829 if (result == NULL) return NULL;
1831 if (!pdb_context->pdb_search_aliases(pdb_context, result, sid)) {
1832 talloc_destroy(result->mem_ctx);
1833 return NULL;
1835 return result;
1838 uint32 pdb_search_entries(struct pdb_search *search,
1839 uint32 start_idx, uint32 max_entries,
1840 struct samr_displayentry **result)
1842 struct samr_displayentry *end_entry;
1843 uint32 end_idx = start_idx+max_entries-1;
1845 /* The first entry needs to be searched after the last. Otherwise the
1846 * first entry might have moved due to a realloc during the search for
1847 * the last entry. */
1849 end_entry = pdb_search_getentry(search, end_idx);
1850 *result = pdb_search_getentry(search, start_idx);
1852 if (end_entry != NULL)
1853 return max_entries;
1855 if (start_idx >= search->num_entries)
1856 return 0;
1858 return search->num_entries - start_idx;
1861 void pdb_search_destroy(struct pdb_search *search)
1863 if (search == NULL)
1864 return;
1866 if (!search->search_ended)
1867 search->search_end(search);
1869 talloc_destroy(search->mem_ctx);
1872 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
1874 *methods = TALLOC_P(mem_ctx, struct pdb_methods);
1876 if (!*methods) {
1877 return NT_STATUS_NO_MEMORY;
1880 ZERO_STRUCTP(*methods);
1882 (*methods)->setsampwent = pdb_default_setsampwent;
1883 (*methods)->endsampwent = pdb_default_endsampwent;
1884 (*methods)->getsampwent = pdb_default_getsampwent;
1885 (*methods)->getsampwnam = pdb_default_getsampwnam;
1886 (*methods)->getsampwsid = pdb_default_getsampwsid;
1887 (*methods)->add_sam_account = pdb_default_add_sam_account;
1888 (*methods)->update_sam_account = pdb_default_update_sam_account;
1889 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
1890 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
1892 (*methods)->getgrsid = pdb_default_getgrsid;
1893 (*methods)->getgrgid = pdb_default_getgrgid;
1894 (*methods)->getgrnam = pdb_default_getgrnam;
1895 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
1896 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
1897 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
1898 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
1899 (*methods)->enum_group_members = pdb_default_enum_group_members;
1900 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
1901 (*methods)->find_alias = pdb_default_find_alias;
1902 (*methods)->create_alias = pdb_default_create_alias;
1903 (*methods)->delete_alias = pdb_default_delete_alias;
1904 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
1905 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
1906 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
1907 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
1908 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
1909 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
1910 (*methods)->lookup_rids = pdb_default_lookup_rids;
1911 (*methods)->search_users = pdb_default_search_users;
1912 (*methods)->search_groups = pdb_default_search_groups;
1913 (*methods)->search_aliases = pdb_default_search_aliases;
1915 return NT_STATUS_OK;