r11137: Compile with only 2 warnings (I'm still working on that code) on a gcc4
[Samba/bb.git] / source3 / passdb / pdb_interface.c
blob482f3e3c9b804b00e812a9d894bd5ebb4b497d70
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_rename_sam_account(struct pdb_context *context, SAM_ACCOUNT *oldname, const char *newname)
330 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
332 struct pdb_methods *pdb_selected;
333 if (!context) {
334 DEBUG(0, ("invalid pdb_context specified!\n"));
335 return ret;
338 if (!oldname->methods){
339 pdb_selected = context->pdb_methods;
340 /* There's no passdb backend specified for this account.
341 * Try to delete it in every passdb available
342 * Needed to delete accounts in smbpasswd that are not
343 * in /etc/passwd.
345 while (pdb_selected){
346 if (NT_STATUS_IS_OK(ret = pdb_selected->rename_sam_account(pdb_selected, oldname, newname))) {
347 return ret;
349 pdb_selected = pdb_selected->next;
351 return ret;
354 if (!oldname->methods->rename_sam_account){
355 DEBUG(0,("invalid oldname->methods->rename_sam_account\n"));
356 return ret;
359 return oldname->methods->rename_sam_account(oldname->methods, oldname, newname);
363 static NTSTATUS context_update_login_attempts(struct pdb_context *context,
364 SAM_ACCOUNT *sam_acct, BOOL success)
366 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
368 if (!context) {
369 DEBUG(0, ("invalid pdb_context specified!\n"));
370 return ret;
373 if (!sam_acct || !sam_acct->methods){
374 DEBUG(0, ("invalid sam_acct specified\n"));
375 return ret;
378 return sam_acct->methods->update_login_attempts(sam_acct->methods, sam_acct, success);
381 static NTSTATUS context_getgrsid(struct pdb_context *context,
382 GROUP_MAP *map, DOM_SID sid)
384 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
386 struct pdb_methods *curmethods;
387 if ((!context)) {
388 DEBUG(0, ("invalid pdb_context specified!\n"));
389 return ret;
391 curmethods = context->pdb_methods;
392 while (curmethods){
393 ret = curmethods->getgrsid(curmethods, map, sid);
394 if (NT_STATUS_IS_OK(ret)) {
395 map->methods = curmethods;
396 return ret;
398 curmethods = curmethods->next;
401 return ret;
404 static NTSTATUS context_getgrgid(struct pdb_context *context,
405 GROUP_MAP *map, gid_t gid)
407 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
409 struct pdb_methods *curmethods;
410 if ((!context)) {
411 DEBUG(0, ("invalid pdb_context specified!\n"));
412 return ret;
414 curmethods = context->pdb_methods;
415 while (curmethods){
416 ret = curmethods->getgrgid(curmethods, map, gid);
417 if (NT_STATUS_IS_OK(ret)) {
418 map->methods = curmethods;
419 return ret;
421 curmethods = curmethods->next;
424 return ret;
427 static NTSTATUS context_getgrnam(struct pdb_context *context,
428 GROUP_MAP *map, const char *name)
430 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
432 struct pdb_methods *curmethods;
433 if ((!context)) {
434 DEBUG(0, ("invalid pdb_context specified!\n"));
435 return ret;
437 curmethods = context->pdb_methods;
438 while (curmethods){
439 ret = curmethods->getgrnam(curmethods, map, name);
440 if (NT_STATUS_IS_OK(ret)) {
441 map->methods = curmethods;
442 return ret;
444 curmethods = curmethods->next;
447 return ret;
450 static NTSTATUS context_add_group_mapping_entry(struct pdb_context *context,
451 GROUP_MAP *map)
453 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
455 if ((!context) || (!context->pdb_methods)) {
456 DEBUG(0, ("invalid pdb_context specified!\n"));
457 return ret;
460 return context->pdb_methods->add_group_mapping_entry(context->pdb_methods,
461 map);
464 static NTSTATUS context_update_group_mapping_entry(struct pdb_context *context,
465 GROUP_MAP *map)
467 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
469 if ((!context) || (!context->pdb_methods)) {
470 DEBUG(0, ("invalid pdb_context specified!\n"));
471 return ret;
474 return context->
475 pdb_methods->update_group_mapping_entry(context->pdb_methods, map);
478 static NTSTATUS context_delete_group_mapping_entry(struct pdb_context *context,
479 DOM_SID sid)
481 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
483 if ((!context) || (!context->pdb_methods)) {
484 DEBUG(0, ("invalid pdb_context specified!\n"));
485 return ret;
488 return context->
489 pdb_methods->delete_group_mapping_entry(context->pdb_methods, sid);
492 static NTSTATUS context_enum_group_mapping(struct pdb_context *context,
493 enum SID_NAME_USE sid_name_use,
494 GROUP_MAP **pp_rmap, size_t *p_num_entries,
495 BOOL unix_only)
497 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
499 if ((!context) || (!context->pdb_methods)) {
500 DEBUG(0, ("invalid pdb_context specified!\n"));
501 return ret;
504 return context->pdb_methods->enum_group_mapping(context->pdb_methods,
505 sid_name_use, pp_rmap,
506 p_num_entries, unix_only);
509 static NTSTATUS context_enum_group_members(struct pdb_context *context,
510 TALLOC_CTX *mem_ctx,
511 const DOM_SID *group,
512 uint32 **pp_member_rids,
513 size_t *p_num_members)
515 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
517 if ((!context) || (!context->pdb_methods)) {
518 DEBUG(0, ("invalid pdb_context specified!\n"));
519 return ret;
522 return context->pdb_methods->enum_group_members(context->pdb_methods,
523 mem_ctx, group,
524 pp_member_rids,
525 p_num_members);
528 static NTSTATUS context_enum_group_memberships(struct pdb_context *context,
529 const char *username,
530 gid_t primary_gid,
531 DOM_SID **pp_sids, gid_t **pp_gids,
532 size_t *p_num_groups)
534 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
536 if ((!context) || (!context->pdb_methods)) {
537 DEBUG(0, ("invalid pdb_context specified!\n"));
538 return ret;
541 return context->pdb_methods->
542 enum_group_memberships(context->pdb_methods, username,
543 primary_gid, pp_sids, pp_gids, p_num_groups);
546 static NTSTATUS context_find_alias(struct pdb_context *context,
547 const char *name, DOM_SID *sid)
549 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
551 if ((!context) || (!context->pdb_methods)) {
552 DEBUG(0, ("invalid pdb_context specified!\n"));
553 return ret;
556 return context->pdb_methods->find_alias(context->pdb_methods,
557 name, sid);
560 static NTSTATUS context_create_alias(struct pdb_context *context,
561 const char *name, uint32 *rid)
563 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
565 if ((!context) || (!context->pdb_methods)) {
566 DEBUG(0, ("invalid pdb_context specified!\n"));
567 return ret;
570 return context->pdb_methods->create_alias(context->pdb_methods,
571 name, rid);
574 static NTSTATUS context_delete_alias(struct pdb_context *context,
575 const DOM_SID *sid)
577 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
579 if ((!context) || (!context->pdb_methods)) {
580 DEBUG(0, ("invalid pdb_context specified!\n"));
581 return ret;
584 return context->pdb_methods->delete_alias(context->pdb_methods, sid);
587 static NTSTATUS context_get_aliasinfo(struct pdb_context *context,
588 const DOM_SID *sid,
589 struct acct_info *info)
591 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
593 if ((!context) || (!context->pdb_methods)) {
594 DEBUG(0, ("invalid pdb_context specified!\n"));
595 return ret;
598 return context->pdb_methods->get_aliasinfo(context->pdb_methods,
599 sid, info);
602 static NTSTATUS context_set_aliasinfo(struct pdb_context *context,
603 const DOM_SID *sid,
604 struct acct_info *info)
606 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
608 if ((!context) || (!context->pdb_methods)) {
609 DEBUG(0, ("invalid pdb_context specified!\n"));
610 return ret;
613 return context->pdb_methods->set_aliasinfo(context->pdb_methods,
614 sid, info);
617 static NTSTATUS context_add_aliasmem(struct pdb_context *context,
618 const DOM_SID *alias,
619 const DOM_SID *member)
621 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
623 if ((!context) || (!context->pdb_methods)) {
624 DEBUG(0, ("invalid pdb_context specified!\n"));
625 return ret;
628 return context->pdb_methods->add_aliasmem(context->pdb_methods,
629 alias, member);
632 static NTSTATUS context_del_aliasmem(struct pdb_context *context,
633 const DOM_SID *alias,
634 const DOM_SID *member)
636 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
638 if ((!context) || (!context->pdb_methods)) {
639 DEBUG(0, ("invalid pdb_context specified!\n"));
640 return ret;
643 return context->pdb_methods->del_aliasmem(context->pdb_methods,
644 alias, member);
647 static NTSTATUS context_enum_aliasmem(struct pdb_context *context,
648 const DOM_SID *alias, DOM_SID **pp_members,
649 size_t *p_num)
651 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
653 if ((!context) || (!context->pdb_methods)) {
654 DEBUG(0, ("invalid pdb_context specified!\n"));
655 return ret;
658 return context->pdb_methods->enum_aliasmem(context->pdb_methods,
659 alias, pp_members, p_num);
662 static NTSTATUS context_enum_alias_memberships(struct pdb_context *context,
663 TALLOC_CTX *mem_ctx,
664 const DOM_SID *domain_sid,
665 const DOM_SID *members,
666 size_t num_members,
667 uint32 **pp_alias_rids,
668 size_t *p_num_alias_rids)
670 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
672 if ((!context) || (!context->pdb_methods)) {
673 DEBUG(0, ("invalid pdb_context specified!\n"));
674 return ret;
677 return context->pdb_methods->
678 enum_alias_memberships(context->pdb_methods, mem_ctx,
679 domain_sid, members, num_members,
680 pp_alias_rids, p_num_alias_rids);
683 static NTSTATUS context_lookup_rids(struct pdb_context *context,
684 TALLOC_CTX *mem_ctx,
685 const DOM_SID *domain_sid,
686 size_t num_rids,
687 uint32 *rids,
688 const char ***pp_names,
689 uint32 **pp_attrs)
691 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
693 if ((!context) || (!context->pdb_methods)) {
694 DEBUG(0, ("invalid pdb_context specified!\n"));
695 return ret;
698 return context->pdb_methods->lookup_rids(context->pdb_methods,
699 mem_ctx, domain_sid, num_rids,
700 rids, pp_names, pp_attrs);
703 static NTSTATUS context_get_account_policy(struct pdb_context *context,
704 int policy_index, uint32 *value)
706 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
708 if ((!context) || (!context->pdb_methods)) {
709 DEBUG(0, ("invalid pdb_context specified!\n"));
710 return ret;
713 return context->pdb_methods->get_account_policy(context->pdb_methods,
714 policy_index, value);
717 static NTSTATUS context_set_account_policy(struct pdb_context *context,
718 int policy_index, uint32 value)
720 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
722 if ((!context) || (!context->pdb_methods)) {
723 DEBUG(0, ("invalid pdb_context specified!\n"));
724 return ret;
727 return context->pdb_methods->set_account_policy(context->pdb_methods,
728 policy_index, value);
731 static NTSTATUS context_get_seq_num(struct pdb_context *context, time_t *seq_num)
733 NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
735 if ((!context) || (!context->pdb_methods)) {
736 DEBUG(0, ("invalid pdb_context specified!\n"));
737 return ret;
740 return context->pdb_methods->get_seq_num(context->pdb_methods, seq_num);
743 /******************************************************************
744 Free and cleanup a pdb context, any associated data and anything
745 that the attached modules might have associated.
746 *******************************************************************/
748 static void free_pdb_context(struct pdb_context **context)
750 struct pdb_methods *pdb_selected = (*context)->pdb_methods;
752 while (pdb_selected){
753 if(pdb_selected->free_private_data)
754 pdb_selected->free_private_data(&(pdb_selected->private_data));
755 pdb_selected = pdb_selected->next;
758 talloc_destroy((*context)->mem_ctx);
759 *context = NULL;
762 static BOOL context_search_users(struct pdb_context *context,
763 struct pdb_search *search, uint16 acct_flags)
765 if ((!context) || (!context->pdb_methods)) {
766 DEBUG(0, ("invalid pdb_context specified!\n"));
767 return False;
770 return context->pdb_methods->search_users(context->pdb_methods,
771 search, acct_flags);
774 static BOOL context_search_groups(struct pdb_context *context,
775 struct pdb_search *search)
777 if ((!context) || (!context->pdb_methods)) {
778 DEBUG(0, ("invalid pdb_context specified!\n"));
779 return False;
782 return context->pdb_methods->search_groups(context->pdb_methods,
783 search);
786 static BOOL context_search_aliases(struct pdb_context *context,
787 struct pdb_search *search,
788 const DOM_SID *sid)
790 if ((!context) || (!context->pdb_methods)) {
791 DEBUG(0, ("invalid pdb_context specified!\n"));
792 return False;
795 return context->pdb_methods->search_aliases(context->pdb_methods,
796 search, sid);
799 /******************************************************************
800 Make a pdb_methods from scratch
801 *******************************************************************/
803 static NTSTATUS make_pdb_methods_name(struct pdb_methods **methods, struct pdb_context *context, const char *selected)
805 char *module_name = smb_xstrdup(selected);
806 char *module_location = NULL, *p;
807 struct pdb_init_function_entry *entry;
808 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
810 lazy_initialize_passdb();
812 p = strchr(module_name, ':');
814 if (p) {
815 *p = 0;
816 module_location = p+1;
817 trim_char(module_location, ' ', ' ');
820 trim_char(module_name, ' ', ' ');
823 DEBUG(5,("Attempting to find an passdb backend to match %s (%s)\n", selected, module_name));
825 entry = pdb_find_backend_entry(module_name);
827 /* Try to find a module that contains this module */
828 if (!entry) {
829 DEBUG(2,("No builtin backend found, trying to load plugin\n"));
830 if(NT_STATUS_IS_OK(smb_probe_module("pdb", module_name)) && !(entry = pdb_find_backend_entry(module_name))) {
831 DEBUG(0,("Plugin is available, but doesn't register passdb backend %s\n", module_name));
832 SAFE_FREE(module_name);
833 return NT_STATUS_UNSUCCESSFUL;
837 /* No such backend found */
838 if(!entry) {
839 DEBUG(0,("No builtin nor plugin backend for %s found\n", module_name));
840 SAFE_FREE(module_name);
841 return NT_STATUS_INVALID_PARAMETER;
844 DEBUG(5,("Found pdb backend %s\n", module_name));
845 nt_status = entry->init(context, methods, module_location);
846 if (NT_STATUS_IS_OK(nt_status)) {
847 DEBUG(5,("pdb backend %s has a valid init\n", selected));
848 } else {
849 DEBUG(0,("pdb backend %s did not correctly init (error was %s)\n", selected, nt_errstr(nt_status)));
851 SAFE_FREE(module_name);
852 return nt_status;
855 /******************************************************************
856 Make a pdb_context from scratch.
857 *******************************************************************/
859 static NTSTATUS make_pdb_context(struct pdb_context **context)
861 TALLOC_CTX *mem_ctx;
863 mem_ctx = talloc_init("pdb_context internal allocation context");
865 if (!mem_ctx) {
866 DEBUG(0, ("make_pdb_context: talloc init failed!\n"));
867 return NT_STATUS_NO_MEMORY;
870 *context = TALLOC_P(mem_ctx, struct pdb_context);
871 if (!*context) {
872 DEBUG(0, ("make_pdb_context: talloc failed!\n"));
873 return NT_STATUS_NO_MEMORY;
876 ZERO_STRUCTP(*context);
878 (*context)->mem_ctx = mem_ctx;
880 (*context)->pdb_setsampwent = context_setsampwent;
881 (*context)->pdb_endsampwent = context_endsampwent;
882 (*context)->pdb_getsampwent = context_getsampwent;
883 (*context)->pdb_getsampwnam = context_getsampwnam;
884 (*context)->pdb_getsampwsid = context_getsampwsid;
885 (*context)->pdb_add_sam_account = context_add_sam_account;
886 (*context)->pdb_update_sam_account = context_update_sam_account;
887 (*context)->pdb_delete_sam_account = context_delete_sam_account;
888 (*context)->pdb_rename_sam_account = context_rename_sam_account;
889 (*context)->pdb_update_login_attempts = context_update_login_attempts;
890 (*context)->pdb_getgrsid = context_getgrsid;
891 (*context)->pdb_getgrgid = context_getgrgid;
892 (*context)->pdb_getgrnam = context_getgrnam;
893 (*context)->pdb_add_group_mapping_entry = context_add_group_mapping_entry;
894 (*context)->pdb_update_group_mapping_entry = context_update_group_mapping_entry;
895 (*context)->pdb_delete_group_mapping_entry = context_delete_group_mapping_entry;
896 (*context)->pdb_enum_group_mapping = context_enum_group_mapping;
897 (*context)->pdb_enum_group_members = context_enum_group_members;
898 (*context)->pdb_enum_group_memberships = context_enum_group_memberships;
900 (*context)->pdb_find_alias = context_find_alias;
901 (*context)->pdb_create_alias = context_create_alias;
902 (*context)->pdb_delete_alias = context_delete_alias;
903 (*context)->pdb_get_aliasinfo = context_get_aliasinfo;
904 (*context)->pdb_set_aliasinfo = context_set_aliasinfo;
905 (*context)->pdb_add_aliasmem = context_add_aliasmem;
906 (*context)->pdb_del_aliasmem = context_del_aliasmem;
907 (*context)->pdb_enum_aliasmem = context_enum_aliasmem;
908 (*context)->pdb_enum_alias_memberships = context_enum_alias_memberships;
909 (*context)->pdb_lookup_rids = context_lookup_rids;
911 (*context)->pdb_get_account_policy = context_get_account_policy;
912 (*context)->pdb_set_account_policy = context_set_account_policy;
914 (*context)->pdb_get_seq_num = context_get_seq_num;
916 (*context)->pdb_search_users = context_search_users;
917 (*context)->pdb_search_groups = context_search_groups;
918 (*context)->pdb_search_aliases = context_search_aliases;
920 (*context)->free_fn = free_pdb_context;
922 return NT_STATUS_OK;
926 /******************************************************************
927 Make a pdb_context, given an array of strings
928 *******************************************************************/
930 NTSTATUS make_pdb_context_list(struct pdb_context **context, const char **selected)
932 int i = 0;
933 struct pdb_methods *curmethods, *tmpmethods;
934 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
935 BOOL have_guest = False;
937 if (!NT_STATUS_IS_OK(nt_status = make_pdb_context(context))) {
938 return nt_status;
941 if (!selected) {
942 DEBUG(0, ("ERROR: empty passdb backend list!\n"));
943 return nt_status;
946 while (selected[i]){
947 if (strcmp(selected[i], "guest") == 0) {
948 have_guest = True;
950 /* Try to initialise pdb */
951 DEBUG(5,("Trying to load: %s\n", selected[i]));
952 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods, *context, selected[i]))) {
953 DEBUG(1, ("Loading %s failed!\n", selected[i]));
954 free_pdb_context(context);
955 return nt_status;
957 curmethods->parent = *context;
958 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
959 i++;
962 if (have_guest)
963 return NT_STATUS_OK;
965 if ( (lp_guestaccount() == NULL) ||
966 (*lp_guestaccount() == '\0') ) {
967 /* We explicitly don't want guest access. No idea what
968 else that breaks, but be it that way. */
969 return NT_STATUS_OK;
972 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods_name(&curmethods,
973 *context,
974 "guest"))) {
975 DEBUG(1, ("Loading guest module failed!\n"));
976 free_pdb_context(context);
977 return nt_status;
980 curmethods->parent = *context;
981 DLIST_ADD_END((*context)->pdb_methods, curmethods, tmpmethods);
983 return NT_STATUS_OK;
986 /******************************************************************
987 Make a pdb_context, given a text string.
988 *******************************************************************/
990 NTSTATUS make_pdb_context_string(struct pdb_context **context, const char *selected)
992 NTSTATUS ret;
993 char **newsel = str_list_make(selected, NULL);
994 ret = make_pdb_context_list(context, (const char **)newsel);
995 str_list_free(&newsel);
996 return ret;
999 /******************************************************************
1000 Return an already initialised pdb_context, to facilitate backward
1001 compatibility (see functions below).
1002 *******************************************************************/
1004 static struct pdb_context *pdb_get_static_context(BOOL reload)
1006 static struct pdb_context *pdb_context = NULL;
1008 if ((pdb_context) && (reload)) {
1009 pdb_context->free_fn(&pdb_context);
1010 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1011 return NULL;
1015 if (!pdb_context) {
1016 if (!NT_STATUS_IS_OK(make_pdb_context_list(&pdb_context, lp_passdb_backend()))) {
1017 return NULL;
1021 return pdb_context;
1024 /******************************************************************
1025 Backward compatibility functions for the original passdb interface
1026 *******************************************************************/
1028 BOOL pdb_setsampwent(BOOL update, uint16 acb_mask)
1030 struct pdb_context *pdb_context = pdb_get_static_context(False);
1032 if (!pdb_context) {
1033 return False;
1036 return NT_STATUS_IS_OK(pdb_context->pdb_setsampwent(pdb_context, update, acb_mask));
1039 void pdb_endsampwent(void)
1041 struct pdb_context *pdb_context = pdb_get_static_context(False);
1043 if (!pdb_context) {
1044 return;
1047 pdb_context->pdb_endsampwent(pdb_context);
1050 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
1052 struct pdb_context *pdb_context = pdb_get_static_context(False);
1054 if (!pdb_context) {
1055 return False;
1058 return NT_STATUS_IS_OK(pdb_context->pdb_getsampwent(pdb_context, user));
1061 static SAM_ACCOUNT *sam_account_cache = NULL;
1063 BOOL pdb_getsampwnam(SAM_ACCOUNT *sam_acct, const char *username)
1065 struct pdb_context *pdb_context = pdb_get_static_context(False);
1067 if (!pdb_context) {
1068 return False;
1071 if (!NT_STATUS_IS_OK(pdb_context->pdb_getsampwnam(pdb_context,
1072 sam_acct, username)))
1073 return False;
1075 if (sam_account_cache != NULL) {
1076 pdb_free_sam(&sam_account_cache);
1077 sam_account_cache = NULL;
1080 pdb_copy_sam_account(sam_acct, &sam_account_cache);
1081 return True;
1084 BOOL pdb_getsampwsid(SAM_ACCOUNT *sam_acct, const DOM_SID *sid)
1086 struct pdb_context *pdb_context = pdb_get_static_context(False);
1088 if (!pdb_context) {
1089 return False;
1092 if ((sam_account_cache != NULL) &&
1093 (sid_equal(sid, pdb_get_user_sid(sam_account_cache))))
1094 return pdb_copy_sam_account(sam_account_cache, &sam_acct);
1096 return NT_STATUS_IS_OK(pdb_context->pdb_getsampwsid(pdb_context, sam_acct, sid));
1099 BOOL pdb_add_sam_account(SAM_ACCOUNT *sam_acct)
1101 struct pdb_context *pdb_context = pdb_get_static_context(False);
1103 if (!pdb_context) {
1104 return False;
1107 return NT_STATUS_IS_OK(pdb_context->pdb_add_sam_account(pdb_context, sam_acct));
1110 BOOL pdb_update_sam_account(SAM_ACCOUNT *sam_acct)
1112 struct pdb_context *pdb_context = pdb_get_static_context(False);
1114 if (!pdb_context) {
1115 return False;
1118 if (sam_account_cache != NULL) {
1119 pdb_free_sam(&sam_account_cache);
1120 sam_account_cache = NULL;
1123 return NT_STATUS_IS_OK(pdb_context->pdb_update_sam_account(pdb_context, sam_acct));
1126 BOOL pdb_delete_sam_account(SAM_ACCOUNT *sam_acct)
1128 struct pdb_context *pdb_context = pdb_get_static_context(False);
1130 if (!pdb_context) {
1131 return False;
1134 if (sam_account_cache != NULL) {
1135 pdb_free_sam(&sam_account_cache);
1136 sam_account_cache = NULL;
1139 return NT_STATUS_IS_OK(pdb_context->pdb_delete_sam_account(pdb_context, sam_acct));
1142 NTSTATUS pdb_rename_sam_account(SAM_ACCOUNT *oldname, const char *newname)
1144 struct pdb_context *pdb_context = pdb_get_static_context(False);
1146 if (!pdb_context) {
1147 return NT_STATUS_NOT_IMPLEMENTED;
1150 if (sam_account_cache != NULL) {
1151 pdb_free_sam(&sam_account_cache);
1152 sam_account_cache = NULL;
1155 return pdb_context->pdb_rename_sam_account(pdb_context, oldname, newname);
1158 NTSTATUS pdb_update_login_attempts(SAM_ACCOUNT *sam_acct, BOOL success)
1160 struct pdb_context *pdb_context = pdb_get_static_context(False);
1162 if (!pdb_context) {
1163 return NT_STATUS_NOT_IMPLEMENTED;
1166 return pdb_context->pdb_update_login_attempts(pdb_context, sam_acct, success);
1169 BOOL pdb_getgrsid(GROUP_MAP *map, DOM_SID sid)
1171 struct pdb_context *pdb_context = pdb_get_static_context(False);
1173 if (!pdb_context) {
1174 return False;
1177 return NT_STATUS_IS_OK(pdb_context->
1178 pdb_getgrsid(pdb_context, map, sid));
1181 BOOL pdb_getgrgid(GROUP_MAP *map, gid_t gid)
1183 struct pdb_context *pdb_context = pdb_get_static_context(False);
1185 if (!pdb_context) {
1186 return False;
1189 return NT_STATUS_IS_OK(pdb_context->
1190 pdb_getgrgid(pdb_context, map, gid));
1193 BOOL pdb_getgrnam(GROUP_MAP *map, const char *name)
1195 struct pdb_context *pdb_context = pdb_get_static_context(False);
1197 if (!pdb_context) {
1198 return False;
1201 return NT_STATUS_IS_OK(pdb_context->
1202 pdb_getgrnam(pdb_context, map, name));
1205 BOOL pdb_add_group_mapping_entry(GROUP_MAP *map)
1207 struct pdb_context *pdb_context = pdb_get_static_context(False);
1209 if (!pdb_context) {
1210 return False;
1213 return NT_STATUS_IS_OK(pdb_context->
1214 pdb_add_group_mapping_entry(pdb_context, map));
1217 BOOL pdb_update_group_mapping_entry(GROUP_MAP *map)
1219 struct pdb_context *pdb_context = pdb_get_static_context(False);
1221 if (!pdb_context) {
1222 return False;
1225 return NT_STATUS_IS_OK(pdb_context->
1226 pdb_update_group_mapping_entry(pdb_context, map));
1229 BOOL pdb_delete_group_mapping_entry(DOM_SID sid)
1231 struct pdb_context *pdb_context = pdb_get_static_context(False);
1233 if (!pdb_context) {
1234 return False;
1237 return NT_STATUS_IS_OK(pdb_context->
1238 pdb_delete_group_mapping_entry(pdb_context, sid));
1241 BOOL pdb_enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **pp_rmap,
1242 size_t *p_num_entries, BOOL unix_only)
1244 struct pdb_context *pdb_context = pdb_get_static_context(False);
1246 if (!pdb_context) {
1247 return False;
1250 return NT_STATUS_IS_OK(pdb_context->
1251 pdb_enum_group_mapping(pdb_context, sid_name_use,
1252 pp_rmap, p_num_entries, unix_only));
1255 NTSTATUS pdb_enum_group_members(TALLOC_CTX *mem_ctx,
1256 const DOM_SID *sid,
1257 uint32 **pp_member_rids,
1258 size_t *p_num_members)
1260 struct pdb_context *pdb_context = pdb_get_static_context(False);
1262 if (!pdb_context) {
1263 return NT_STATUS_UNSUCCESSFUL;
1266 return pdb_context->pdb_enum_group_members(pdb_context, mem_ctx, sid,
1267 pp_member_rids, p_num_members);
1270 NTSTATUS pdb_enum_group_memberships(const char *username, gid_t primary_gid,
1271 DOM_SID **pp_sids, gid_t **pp_gids,
1272 size_t *p_num_groups)
1274 struct pdb_context *pdb_context = pdb_get_static_context(False);
1276 if (!pdb_context) {
1277 return NT_STATUS_UNSUCCESSFUL;
1280 return pdb_context->pdb_enum_group_memberships(pdb_context, username,
1281 primary_gid, pp_sids, pp_gids,
1282 p_num_groups);
1285 BOOL pdb_find_alias(const char *name, DOM_SID *sid)
1287 struct pdb_context *pdb_context = pdb_get_static_context(False);
1289 if (!pdb_context) {
1290 return False;
1293 return NT_STATUS_IS_OK(pdb_context->pdb_find_alias(pdb_context,
1294 name, sid));
1297 NTSTATUS pdb_create_alias(const char *name, uint32 *rid)
1299 struct pdb_context *pdb_context = pdb_get_static_context(False);
1301 if (!pdb_context) {
1302 return NT_STATUS_NOT_IMPLEMENTED;
1305 return pdb_context->pdb_create_alias(pdb_context, name, rid);
1308 BOOL pdb_delete_alias(const DOM_SID *sid)
1310 struct pdb_context *pdb_context = pdb_get_static_context(False);
1312 if (!pdb_context) {
1313 return False;
1316 return NT_STATUS_IS_OK(pdb_context->pdb_delete_alias(pdb_context,
1317 sid));
1321 BOOL pdb_get_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1323 struct pdb_context *pdb_context = pdb_get_static_context(False);
1325 if (!pdb_context) {
1326 return False;
1329 return NT_STATUS_IS_OK(pdb_context->pdb_get_aliasinfo(pdb_context, sid,
1330 info));
1333 BOOL pdb_set_aliasinfo(const DOM_SID *sid, struct acct_info *info)
1335 struct pdb_context *pdb_context = pdb_get_static_context(False);
1337 if (!pdb_context) {
1338 return False;
1341 return NT_STATUS_IS_OK(pdb_context->pdb_set_aliasinfo(pdb_context, sid,
1342 info));
1345 BOOL pdb_add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1347 struct pdb_context *pdb_context = pdb_get_static_context(False);
1349 if (!pdb_context) {
1350 return False;
1353 return NT_STATUS_IS_OK(pdb_context->
1354 pdb_add_aliasmem(pdb_context, alias, member));
1357 BOOL pdb_del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
1359 struct pdb_context *pdb_context = pdb_get_static_context(False);
1361 if (!pdb_context) {
1362 return False;
1365 return NT_STATUS_IS_OK(pdb_context->
1366 pdb_del_aliasmem(pdb_context, alias, member));
1369 BOOL pdb_enum_aliasmem(const DOM_SID *alias,
1370 DOM_SID **pp_members, size_t *p_num_members)
1372 struct pdb_context *pdb_context = pdb_get_static_context(False);
1374 if (!pdb_context) {
1375 return False;
1378 return NT_STATUS_IS_OK(pdb_context->
1379 pdb_enum_aliasmem(pdb_context, alias,
1380 pp_members, p_num_members));
1383 BOOL pdb_enum_alias_memberships(TALLOC_CTX *mem_ctx, const DOM_SID *domain_sid,
1384 const DOM_SID *members, size_t num_members,
1385 uint32 **pp_alias_rids, size_t *p_num_alias_rids)
1387 struct pdb_context *pdb_context = pdb_get_static_context(False);
1389 if (!pdb_context) {
1390 return False;
1393 return NT_STATUS_IS_OK(pdb_context->
1394 pdb_enum_alias_memberships(pdb_context, mem_ctx,
1395 domain_sid,
1396 members, num_members,
1397 pp_alias_rids,
1398 p_num_alias_rids));
1401 NTSTATUS pdb_lookup_rids(TALLOC_CTX *mem_ctx,
1402 const DOM_SID *domain_sid,
1403 int num_rids,
1404 uint32 *rids,
1405 const char ***names,
1406 uint32 **attrs)
1408 struct pdb_context *pdb_context = pdb_get_static_context(False);
1410 if (!pdb_context) {
1411 return NT_STATUS_NOT_IMPLEMENTED;
1414 return pdb_context->pdb_lookup_rids(pdb_context, mem_ctx, domain_sid,
1415 num_rids, rids, names, attrs);
1418 BOOL pdb_get_account_policy(int policy_index, uint32 *value)
1420 struct pdb_context *pdb_context = pdb_get_static_context(False);
1422 if (!pdb_context) {
1423 return False;
1426 return NT_STATUS_IS_OK(pdb_context->
1427 pdb_get_account_policy(pdb_context, policy_index, value));
1430 BOOL pdb_set_account_policy(int policy_index, uint32 value)
1432 struct pdb_context *pdb_context = pdb_get_static_context(False);
1434 if (!pdb_context) {
1435 return False;
1438 return NT_STATUS_IS_OK(pdb_context->
1439 pdb_set_account_policy(pdb_context, policy_index, value));
1442 BOOL pdb_get_seq_num(time_t *seq_num)
1444 struct pdb_context *pdb_context = pdb_get_static_context(False);
1446 if (!pdb_context) {
1447 return False;
1450 return NT_STATUS_IS_OK(pdb_context->
1451 pdb_get_seq_num(pdb_context, seq_num));
1453 /***************************************************************
1454 Initialize the static context (at smbd startup etc).
1456 If uninitialised, context will auto-init on first use.
1457 ***************************************************************/
1459 BOOL initialize_password_db(BOOL reload)
1461 return (pdb_get_static_context(reload) != NULL);
1465 /***************************************************************************
1466 Default implementations of some functions.
1467 ****************************************************************************/
1469 static NTSTATUS pdb_default_getsampwnam (struct pdb_methods *methods, SAM_ACCOUNT *user, const char *sname)
1471 return NT_STATUS_NO_SUCH_USER;
1474 static NTSTATUS pdb_default_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
1476 return NT_STATUS_NO_SUCH_USER;
1479 static NTSTATUS pdb_default_add_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1481 DEBUG(0,("this backend (%s) should not be listed as the first passdb backend! You can't add users to it.\n", methods->name));
1482 return NT_STATUS_NOT_IMPLEMENTED;
1485 static NTSTATUS pdb_default_update_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *newpwd)
1487 return NT_STATUS_NOT_IMPLEMENTED;
1490 static NTSTATUS pdb_default_delete_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd)
1492 return NT_STATUS_NOT_IMPLEMENTED;
1495 static NTSTATUS pdb_default_rename_sam_account (struct pdb_methods *methods, SAM_ACCOUNT *pwd, const char *newname)
1497 return NT_STATUS_NOT_IMPLEMENTED;
1500 static NTSTATUS pdb_default_update_login_attempts (struct pdb_methods *methods, SAM_ACCOUNT *newpwd, BOOL success)
1502 return NT_STATUS_OK;
1505 static NTSTATUS pdb_default_setsampwent(struct pdb_methods *methods, BOOL update, uint16 acb_mask)
1507 return NT_STATUS_NOT_IMPLEMENTED;
1510 static NTSTATUS pdb_default_getsampwent(struct pdb_methods *methods, SAM_ACCOUNT *user)
1512 return NT_STATUS_NOT_IMPLEMENTED;
1515 static void pdb_default_endsampwent(struct pdb_methods *methods)
1517 return; /* NT_STATUS_NOT_IMPLEMENTED; */
1520 static NTSTATUS pdb_default_get_account_policy(struct pdb_methods *methods, int policy_index, uint32 *value)
1522 return account_policy_get(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1525 static NTSTATUS pdb_default_set_account_policy(struct pdb_methods *methods, int policy_index, uint32 value)
1527 return account_policy_set(policy_index, value) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1530 static NTSTATUS pdb_default_get_seq_num(struct pdb_methods *methods, time_t *seq_num)
1532 *seq_num = time(NULL);
1533 return NT_STATUS_OK;
1536 static void add_uid_to_array_unique(TALLOC_CTX *mem_ctx,
1537 uid_t uid, uid_t **pp_uids, size_t *p_num)
1539 size_t i;
1541 for (i=0; i<*p_num; i++) {
1542 if ((*pp_uids)[i] == uid)
1543 return;
1546 *pp_uids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_uids, uid_t, *p_num+1);
1548 if (*pp_uids == NULL)
1549 return;
1551 (*pp_uids)[*p_num] = uid;
1552 *p_num += 1;
1555 static BOOL get_memberuids(TALLOC_CTX *mem_ctx, gid_t gid, uid_t **pp_uids, size_t *p_num)
1557 struct group *grp;
1558 char **gr;
1559 struct sys_pwent *userlist, *user;
1561 *pp_uids = NULL;
1562 *p_num = 0;
1564 /* We only look at our own sam, so don't care about imported stuff */
1566 winbind_off();
1568 if ((grp = getgrgid(gid)) == NULL) {
1569 winbind_on();
1570 return False;
1573 /* Primary group members */
1575 userlist = getpwent_list();
1577 for (user = userlist; user != NULL; user = user->next) {
1578 if (user->pw_gid != gid)
1579 continue;
1580 add_uid_to_array_unique(mem_ctx, user->pw_uid, pp_uids, p_num);
1583 pwent_free(userlist);
1585 /* Secondary group members */
1587 for (gr = grp->gr_mem; (*gr != NULL) && ((*gr)[0] != '\0'); gr += 1) {
1588 struct passwd *pw = getpwnam(*gr);
1590 if (pw == NULL)
1591 continue;
1592 add_uid_to_array_unique(mem_ctx, pw->pw_uid, pp_uids, p_num);
1595 winbind_on();
1597 return True;
1600 NTSTATUS pdb_default_enum_group_members(struct pdb_methods *methods,
1601 TALLOC_CTX *mem_ctx,
1602 const DOM_SID *group,
1603 uint32 **pp_member_rids,
1604 size_t *p_num_members)
1606 gid_t gid;
1607 uid_t *uids;
1608 size_t i, num_uids;
1610 *pp_member_rids = NULL;
1611 *p_num_members = 0;
1613 if (!NT_STATUS_IS_OK(sid_to_gid(group, &gid)))
1614 return NT_STATUS_NO_SUCH_GROUP;
1616 if(!get_memberuids(mem_ctx, gid, &uids, &num_uids))
1617 return NT_STATUS_NO_SUCH_GROUP;
1619 if (num_uids == 0)
1620 return NT_STATUS_OK;
1622 *pp_member_rids = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_uids);
1624 for (i=0; i<num_uids; i++) {
1625 DOM_SID sid;
1627 if (!NT_STATUS_IS_OK(uid_to_sid(&sid, uids[i]))) {
1628 DEBUG(1, ("Could not map member uid to SID\n"));
1629 continue;
1632 if (!sid_check_is_in_our_domain(&sid)) {
1633 DEBUG(1, ("Inconsistent SAM -- group member uid not "
1634 "in our domain\n"));
1635 continue;
1638 sid_peek_rid(&sid, &(*pp_member_rids)[*p_num_members]);
1639 *p_num_members += 1;
1642 return NT_STATUS_OK;
1645 NTSTATUS pdb_default_lookup_rids(struct pdb_methods *methods,
1646 TALLOC_CTX *mem_ctx,
1647 const DOM_SID *domain_sid,
1648 int num_rids,
1649 uint32 *rids,
1650 const char ***names,
1651 uint32 **attrs)
1653 int i;
1654 NTSTATUS result;
1655 BOOL have_mapped = False;
1656 BOOL have_unmapped = False;
1658 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, const char *, num_rids);
1659 (*attrs) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_rids);
1661 if ((num_rids != 0) && (((*names) == NULL) || ((*attrs) == NULL)))
1662 return NT_STATUS_NO_MEMORY;
1664 if (!sid_equal(domain_sid, get_global_sam_sid())) {
1665 /* TODO: Sooner or later we need to look up BUILTIN rids as
1666 * well. -- vl */
1667 goto done;
1670 for (i = 0; i < num_rids; i++) {
1671 fstring tmpname;
1672 fstring domname;
1673 DOM_SID sid;
1674 enum SID_NAME_USE type;
1676 (*attrs)[i] = SID_NAME_UNKNOWN;
1678 sid_copy(&sid, domain_sid);
1679 sid_append_rid(&sid, rids[i]);
1681 if (lookup_sid(&sid, domname, tmpname, &type)) {
1682 (*attrs)[i] = (uint32)type;
1683 (*names)[i] = talloc_strdup(mem_ctx, tmpname);
1684 if ((*names)[i] == NULL)
1685 return NT_STATUS_NO_MEMORY;
1686 DEBUG(5,("lookup_rids: %s:%d\n", (*names)[i],
1687 (*attrs)[i]));
1688 have_mapped = True;
1689 } else {
1690 have_unmapped = True;
1694 done:
1696 result = NT_STATUS_NONE_MAPPED;
1698 if (have_mapped)
1699 result = have_unmapped ? STATUS_SOME_UNMAPPED : NT_STATUS_OK;
1701 return result;
1704 static struct pdb_search *pdb_search_init(enum pdb_search_type type)
1706 TALLOC_CTX *mem_ctx;
1707 struct pdb_search *result;
1709 mem_ctx = talloc_init("pdb_search");
1710 if (mem_ctx == NULL) {
1711 DEBUG(0, ("talloc_init failed\n"));
1712 return NULL;
1715 result = TALLOC_P(mem_ctx, struct pdb_search);
1716 if (result == NULL) {
1717 DEBUG(0, ("talloc failed\n"));
1718 return NULL;
1721 result->mem_ctx = mem_ctx;
1722 result->type = type;
1723 result->cache = NULL;
1724 result->num_entries = 0;
1725 result->cache_size = 0;
1726 result->search_ended = False;
1728 /* Segfault appropriately if not initialized */
1729 result->next_entry = NULL;
1730 result->search_end = NULL;
1732 return result;
1735 static void fill_displayentry(TALLOC_CTX *mem_ctx, uint32 rid,
1736 uint16 acct_flags,
1737 const char *account_name,
1738 const char *fullname,
1739 const char *description,
1740 struct samr_displayentry *entry)
1742 entry->rid = rid;
1743 entry->acct_flags = acct_flags;
1745 if (account_name != NULL)
1746 entry->account_name = talloc_strdup(mem_ctx, account_name);
1747 else
1748 entry->account_name = "";
1750 if (fullname != NULL)
1751 entry->fullname = talloc_strdup(mem_ctx, fullname);
1752 else
1753 entry->fullname = "";
1755 if (description != NULL)
1756 entry->description = talloc_strdup(mem_ctx, description);
1757 else
1758 entry->description = "";
1761 static BOOL user_search_in_progress = False;
1762 struct user_search {
1763 uint16 acct_flags;
1766 static BOOL next_entry_users(struct pdb_search *s,
1767 struct samr_displayentry *entry)
1769 struct user_search *state = s->private_data;
1770 SAM_ACCOUNT *user = NULL;
1771 NTSTATUS status;
1773 next:
1774 status = pdb_init_sam(&user);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 DEBUG(0, ("Could not pdb_init_sam\n"));
1777 return False;
1780 if (!pdb_getsampwent(user)) {
1781 pdb_free_sam(&user);
1782 return False;
1785 if ((state->acct_flags != 0) &&
1786 ((pdb_get_acct_ctrl(user) & state->acct_flags) == 0)) {
1787 pdb_free_sam(&user);
1788 goto next;
1791 fill_displayentry(s->mem_ctx, pdb_get_user_rid(user),
1792 pdb_get_acct_ctrl(user), pdb_get_username(user),
1793 pdb_get_fullname(user), pdb_get_acct_desc(user),
1794 entry);
1796 pdb_free_sam(&user);
1797 return True;
1800 static void search_end_users(struct pdb_search *search)
1802 pdb_endsampwent();
1803 user_search_in_progress = False;
1806 static BOOL pdb_default_search_users(struct pdb_methods *methods,
1807 struct pdb_search *search,
1808 uint16 acct_flags)
1810 struct user_search *state;
1812 if (user_search_in_progress) {
1813 DEBUG(1, ("user search in progress\n"));
1814 return False;
1817 if (!pdb_setsampwent(False, acct_flags)) {
1818 DEBUG(5, ("Could not start search\n"));
1819 return False;
1822 user_search_in_progress = True;
1824 state = TALLOC_P(search->mem_ctx, struct user_search);
1825 if (state == NULL) {
1826 DEBUG(0, ("talloc failed\n"));
1827 return False;
1830 state->acct_flags = acct_flags;
1832 search->private_data = state;
1833 search->next_entry = next_entry_users;
1834 search->search_end = search_end_users;
1835 return True;
1838 struct group_search {
1839 GROUP_MAP *groups;
1840 size_t num_groups, current_group;
1843 static BOOL next_entry_groups(struct pdb_search *s,
1844 struct samr_displayentry *entry)
1846 struct group_search *state = s->private_data;
1847 uint32 rid;
1848 GROUP_MAP *map = &state->groups[state->current_group];
1850 if (state->current_group == state->num_groups)
1851 return False;
1853 sid_peek_rid(&map->sid, &rid);
1855 fill_displayentry(s->mem_ctx, rid, 0, map->nt_name, NULL, map->comment,
1856 entry);
1858 state->current_group += 1;
1859 return True;
1862 static void search_end_groups(struct pdb_search *search)
1864 struct group_search *state = search->private_data;
1865 SAFE_FREE(state->groups);
1868 static BOOL pdb_search_grouptype(struct pdb_search *search,
1869 enum SID_NAME_USE type)
1871 struct group_search *state;
1873 state = TALLOC_P(search->mem_ctx, struct group_search);
1874 if (state == NULL) {
1875 DEBUG(0, ("talloc failed\n"));
1876 return False;
1879 if (!pdb_enum_group_mapping(type, &state->groups, &state->num_groups,
1880 True)) {
1881 DEBUG(0, ("Could not enum groups\n"));
1882 return False;
1885 state->current_group = 0;
1886 search->private_data = state;
1887 search->next_entry = next_entry_groups;
1888 search->search_end = search_end_groups;
1889 return True;
1892 static BOOL pdb_default_search_groups(struct pdb_methods *methods,
1893 struct pdb_search *search)
1895 return pdb_search_grouptype(search, SID_NAME_DOM_GRP);
1898 static BOOL pdb_default_search_aliases(struct pdb_methods *methods,
1899 struct pdb_search *search,
1900 const DOM_SID *sid)
1903 if (sid_equal(sid, get_global_sam_sid()))
1904 return pdb_search_grouptype(search, SID_NAME_ALIAS);
1906 if (sid_equal(sid, &global_sid_Builtin))
1907 return pdb_search_grouptype(search, SID_NAME_WKN_GRP);
1909 DEBUG(3, ("unknown domain sid: %s\n", sid_string_static(sid)));
1910 return False;
1913 static struct samr_displayentry *pdb_search_getentry(struct pdb_search *search,
1914 uint32 idx)
1916 if (idx < search->num_entries)
1917 return &search->cache[idx];
1919 if (search->search_ended)
1920 return NULL;
1922 while (idx >= search->num_entries) {
1923 struct samr_displayentry entry;
1925 if (!search->next_entry(search, &entry)) {
1926 search->search_end(search);
1927 search->search_ended = True;
1928 break;
1931 ADD_TO_LARGE_ARRAY(search->mem_ctx, struct samr_displayentry,
1932 entry, &search->cache, &search->num_entries,
1933 &search->cache_size);
1936 return (search->num_entries > idx) ? &search->cache[idx] : NULL;
1939 struct pdb_search *pdb_search_users(uint16 acct_flags)
1941 struct pdb_context *pdb_context = pdb_get_static_context(False);
1942 struct pdb_search *result;
1944 if (pdb_context == NULL) return NULL;
1946 result = pdb_search_init(PDB_USER_SEARCH);
1947 if (result == NULL) return NULL;
1949 if (!pdb_context->pdb_search_users(pdb_context, result, acct_flags)) {
1950 talloc_destroy(result->mem_ctx);
1951 return NULL;
1953 return result;
1956 struct pdb_search *pdb_search_groups(void)
1958 struct pdb_context *pdb_context = pdb_get_static_context(False);
1959 struct pdb_search *result;
1961 if (pdb_context == NULL) return NULL;
1963 result = pdb_search_init(PDB_GROUP_SEARCH);
1964 if (result == NULL) return NULL;
1966 if (!pdb_context->pdb_search_groups(pdb_context, result)) {
1967 talloc_destroy(result->mem_ctx);
1968 return NULL;
1970 return result;
1973 struct pdb_search *pdb_search_aliases(const DOM_SID *sid)
1975 struct pdb_context *pdb_context = pdb_get_static_context(False);
1976 struct pdb_search *result;
1978 if (pdb_context == NULL) return NULL;
1980 result = pdb_search_init(PDB_ALIAS_SEARCH);
1981 if (result == NULL) return NULL;
1983 if (!pdb_context->pdb_search_aliases(pdb_context, result, sid)) {
1984 talloc_destroy(result->mem_ctx);
1985 return NULL;
1987 return result;
1990 uint32 pdb_search_entries(struct pdb_search *search,
1991 uint32 start_idx, uint32 max_entries,
1992 struct samr_displayentry **result)
1994 struct samr_displayentry *end_entry;
1995 uint32 end_idx = start_idx+max_entries-1;
1997 /* The first entry needs to be searched after the last. Otherwise the
1998 * first entry might have moved due to a realloc during the search for
1999 * the last entry. */
2001 end_entry = pdb_search_getentry(search, end_idx);
2002 *result = pdb_search_getentry(search, start_idx);
2004 if (end_entry != NULL)
2005 return max_entries;
2007 if (start_idx >= search->num_entries)
2008 return 0;
2010 return search->num_entries - start_idx;
2013 void pdb_search_destroy(struct pdb_search *search)
2015 if (search == NULL)
2016 return;
2018 if (!search->search_ended)
2019 search->search_end(search);
2021 talloc_destroy(search->mem_ctx);
2024 NTSTATUS make_pdb_methods(TALLOC_CTX *mem_ctx, PDB_METHODS **methods)
2026 *methods = TALLOC_P(mem_ctx, struct pdb_methods);
2028 if (!*methods) {
2029 return NT_STATUS_NO_MEMORY;
2032 ZERO_STRUCTP(*methods);
2034 (*methods)->setsampwent = pdb_default_setsampwent;
2035 (*methods)->endsampwent = pdb_default_endsampwent;
2036 (*methods)->getsampwent = pdb_default_getsampwent;
2037 (*methods)->getsampwnam = pdb_default_getsampwnam;
2038 (*methods)->getsampwsid = pdb_default_getsampwsid;
2039 (*methods)->add_sam_account = pdb_default_add_sam_account;
2040 (*methods)->update_sam_account = pdb_default_update_sam_account;
2041 (*methods)->delete_sam_account = pdb_default_delete_sam_account;
2042 (*methods)->rename_sam_account = pdb_default_rename_sam_account;
2043 (*methods)->update_login_attempts = pdb_default_update_login_attempts;
2045 (*methods)->getgrsid = pdb_default_getgrsid;
2046 (*methods)->getgrgid = pdb_default_getgrgid;
2047 (*methods)->getgrnam = pdb_default_getgrnam;
2048 (*methods)->add_group_mapping_entry = pdb_default_add_group_mapping_entry;
2049 (*methods)->update_group_mapping_entry = pdb_default_update_group_mapping_entry;
2050 (*methods)->delete_group_mapping_entry = pdb_default_delete_group_mapping_entry;
2051 (*methods)->enum_group_mapping = pdb_default_enum_group_mapping;
2052 (*methods)->enum_group_members = pdb_default_enum_group_members;
2053 (*methods)->enum_group_memberships = pdb_default_enum_group_memberships;
2054 (*methods)->find_alias = pdb_default_find_alias;
2055 (*methods)->create_alias = pdb_default_create_alias;
2056 (*methods)->delete_alias = pdb_default_delete_alias;
2057 (*methods)->get_aliasinfo = pdb_default_get_aliasinfo;
2058 (*methods)->set_aliasinfo = pdb_default_set_aliasinfo;
2059 (*methods)->add_aliasmem = pdb_default_add_aliasmem;
2060 (*methods)->del_aliasmem = pdb_default_del_aliasmem;
2061 (*methods)->enum_aliasmem = pdb_default_enum_aliasmem;
2062 (*methods)->enum_alias_memberships = pdb_default_alias_memberships;
2063 (*methods)->lookup_rids = pdb_default_lookup_rids;
2064 (*methods)->get_account_policy = pdb_default_get_account_policy;
2065 (*methods)->set_account_policy = pdb_default_set_account_policy;
2066 (*methods)->get_seq_num = pdb_default_get_seq_num;
2068 (*methods)->search_users = pdb_default_search_users;
2069 (*methods)->search_groups = pdb_default_search_groups;
2070 (*methods)->search_aliases = pdb_default_search_aliases;
2072 return NT_STATUS_OK;