2 Unix SMB/CIFS mplementation.
3 NDS LDAP helper functions for SAMBA
4 Copyright (C) Vince Brimhall 2004-2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define NMASLDAP_GET_LOGIN_CONFIG_REQUEST "2.16.840.1.113719.1.39.42.100.3"
30 #define NMASLDAP_GET_LOGIN_CONFIG_RESPONSE "2.16.840.1.113719.1.39.42.100.4"
31 #define NMASLDAP_SET_PASSWORD_REQUEST "2.16.840.1.113719.1.39.42.100.11"
32 #define NMASLDAP_SET_PASSWORD_RESPONSE "2.16.840.1.113719.1.39.42.100.12"
33 #define NMASLDAP_GET_PASSWORD_REQUEST "2.16.840.1.113719.1.39.42.100.13"
34 #define NMASLDAP_GET_PASSWORD_RESPONSE "2.16.840.1.113719.1.39.42.100.14"
36 #define NMAS_LDAP_EXT_VERSION 1
38 /**********************************************************************
39 Take the request BER value and input data items and BER encodes the
40 data into the BER value
41 **********************************************************************/
43 static int berEncodePasswordData(
44 struct berval
**requestBV
,
47 const char *password2
)
50 BerElement
*requestBer
= NULL
;
52 const char * utf8ObjPtr
= NULL
;
54 const char * utf8PwdPtr
= NULL
;
56 const char * utf8Pwd2Ptr
= NULL
;
60 /* Convert objectDN and tag strings from Unicode to UTF-8 */
61 utf8ObjSize
= strlen(objectDN
)+1;
62 utf8ObjPtr
= objectDN
;
66 utf8PwdSize
= strlen(password
)+1;
67 utf8PwdPtr
= password
;
70 if (password2
!= NULL
)
72 utf8Pwd2Size
= strlen(password2
)+1;
73 utf8Pwd2Ptr
= password2
;
76 /* Allocate a BerElement for the request parameters. */
77 if((requestBer
= ber_alloc()) == NULL
)
79 err
= LDAP_ENCODING_ERROR
;
83 if (password
!= NULL
&& password2
!= NULL
)
85 /* BER encode the NMAS Version, the objectDN, and the password */
86 rc
= ber_printf(requestBer
, "{iooo}", NMAS_LDAP_EXT_VERSION
, utf8ObjPtr
, utf8ObjSize
, utf8PwdPtr
, utf8PwdSize
, utf8Pwd2Ptr
, utf8Pwd2Size
);
88 else if (password
!= NULL
)
90 /* BER encode the NMAS Version, the objectDN, and the password */
91 rc
= ber_printf(requestBer
, "{ioo}", NMAS_LDAP_EXT_VERSION
, utf8ObjPtr
, utf8ObjSize
, utf8PwdPtr
, utf8PwdSize
);
95 /* BER encode the NMAS Version and the objectDN */
96 rc
= ber_printf(requestBer
, "{io}", NMAS_LDAP_EXT_VERSION
, utf8ObjPtr
, utf8ObjSize
);
101 err
= LDAP_ENCODING_ERROR
;
109 /* Convert the BER we just built to a berval that we'll send with the extended request. */
110 if(ber_flatten(requestBer
, requestBV
) == LBER_ERROR
)
112 err
= LDAP_ENCODING_ERROR
;
120 ber_free(requestBer
, 1);
126 /**********************************************************************
127 Take the request BER value and input data items and BER encodes the
128 data into the BER value
129 **********************************************************************/
131 static int berEncodeLoginData(
132 struct berval
**requestBV
,
134 unsigned int methodIDLen
,
135 unsigned int *methodID
,
141 BerElement
*requestBer
= NULL
;
144 unsigned int elemCnt
= methodIDLen
/ sizeof(unsigned int);
146 char *utf8ObjPtr
=NULL
;
149 char *utf8TagPtr
= NULL
;
152 utf8ObjPtr
= objectDN
;
153 utf8ObjSize
= strlen(utf8ObjPtr
)+1;
156 utf8TagSize
= strlen(utf8TagPtr
)+1;
158 /* Allocate a BerElement for the request parameters. */
159 if((requestBer
= ber_alloc()) == NULL
)
161 err
= LDAP_ENCODING_ERROR
;
165 /* BER encode the NMAS Version and the objectDN */
166 err
= (ber_printf(requestBer
, "{io", NMAS_LDAP_EXT_VERSION
, utf8ObjPtr
, utf8ObjSize
) < 0) ? LDAP_ENCODING_ERROR
: 0;
168 /* BER encode the MethodID Length and value */
171 err
= (ber_printf(requestBer
, "{i{", methodIDLen
) < 0) ? LDAP_ENCODING_ERROR
: 0;
174 for (i
= 0; !err
&& i
< elemCnt
; i
++)
176 err
= (ber_printf(requestBer
, "i", methodID
[i
]) < 0) ? LDAP_ENCODING_ERROR
: 0;
181 err
= (ber_printf(requestBer
, "}}", 0) < 0) ? LDAP_ENCODING_ERROR
: 0;
186 /* BER Encode the the tag and data */
187 err
= (ber_printf(requestBer
, "oio}", utf8TagPtr
, utf8TagSize
, putDataLen
, putData
, putDataLen
) < 0) ? LDAP_ENCODING_ERROR
: 0;
191 /* BER Encode the the tag */
192 err
= (ber_printf(requestBer
, "o}", utf8TagPtr
, utf8TagSize
) < 0) ? LDAP_ENCODING_ERROR
: 0;
200 /* Convert the BER we just built to a berval that we'll send with the extended request. */
201 if(ber_flatten(requestBer
, requestBV
) == LBER_ERROR
)
203 err
= LDAP_ENCODING_ERROR
;
211 ber_free(requestBer
, 1);
217 /**********************************************************************
218 Takes the reply BER Value and decodes the NMAS server version and
219 return code and if a non null retData buffer was supplied, tries to
220 decode the the return data and length
221 **********************************************************************/
223 static int berDecodeLoginData(
224 struct berval
*replyBV
,
230 BerElement
*replyBer
= NULL
;
231 char *retOctStr
= NULL
;
232 size_t retOctStrLen
= 0;
234 if((replyBer
= ber_init(replyBV
)) == NULL
)
236 err
= LDAP_OPERATIONS_ERROR
;
242 retOctStrLen
= *retDataLen
+ 1;
243 retOctStr
= SMB_MALLOC_ARRAY(char, retOctStrLen
);
246 err
= LDAP_OPERATIONS_ERROR
;
250 if(ber_scanf(replyBer
, "{iis}", serverVersion
, &err
, retOctStr
, &retOctStrLen
) != -1)
252 if (*retDataLen
>= retOctStrLen
)
254 memcpy(retData
, retOctStr
, retOctStrLen
);
258 err
= LDAP_NO_MEMORY
;
261 *retDataLen
= retOctStrLen
;
265 err
= LDAP_DECODING_ERROR
;
270 if(ber_scanf(replyBer
, "{ii}", serverVersion
, &err
) == -1)
274 err
= LDAP_DECODING_ERROR
;
283 ber_free(replyBer
, 1);
286 if (retOctStr
!= NULL
)
288 memset(retOctStr
, 0, retOctStrLen
);
295 /**********************************************************************
296 Retrieves data in the login configuration of the specified object
297 that is tagged with the specified methodID and tag.
298 **********************************************************************/
300 static int getLoginConfig(
303 unsigned int methodIDLen
,
304 unsigned int *methodID
,
310 struct berval
*requestBV
= NULL
;
311 char *replyOID
= NULL
;
312 struct berval
*replyBV
= NULL
;
313 int serverVersion
= 0;
315 /* Validate unicode parameters. */
316 if((strlen(objectDN
) == 0) || ld
== NULL
)
318 return LDAP_NO_SUCH_ATTRIBUTE
;
321 err
= berEncodeLoginData(&requestBV
, objectDN
, methodIDLen
, methodID
, tag
, 0, NULL
);
327 /* Call the ldap_extended_operation (synchronously) */
328 if((err
= ldap_extended_operation_s(ld
, NMASLDAP_GET_LOGIN_CONFIG_REQUEST
,
329 requestBV
, NULL
, NULL
, &replyOID
, &replyBV
)))
334 /* Make sure there is a return OID */
337 err
= LDAP_NOT_SUPPORTED
;
341 /* Is this what we were expecting to get back. */
342 if(strcmp(replyOID
, NMASLDAP_GET_LOGIN_CONFIG_RESPONSE
))
344 err
= LDAP_NOT_SUPPORTED
;
348 /* Do we have a good returned berval? */
351 /* No; returned berval means we experienced a rather drastic error. */
352 /* Return operations error. */
353 err
= LDAP_OPERATIONS_ERROR
;
357 err
= berDecodeLoginData(replyBV
, &serverVersion
, dataLen
, data
);
359 if(serverVersion
!= NMAS_LDAP_EXT_VERSION
)
361 err
= LDAP_OPERATIONS_ERROR
;
372 /* Free the return OID string if one was returned. */
375 ldap_memfree(replyOID
);
378 /* Free memory allocated while building the request ber and berval. */
381 ber_bvfree(requestBV
);
384 /* Return the appropriate error/success code. */
388 /**********************************************************************
389 Attempts to get the Simple Password
390 **********************************************************************/
392 static int nmasldap_get_simple_pwd(
399 unsigned int methodID
= 0;
400 unsigned int methodIDLen
= sizeof(methodID
);
401 char tag
[] = {'P','A','S','S','W','O','R','D',' ','H','A','S','H',0};
403 size_t pwdBufLen
, bufferLen
;
405 bufferLen
= pwdBufLen
= pwdLen
+2;
406 pwdBuf
= SMB_MALLOC_ARRAY(char, pwdBufLen
); /* digest and null */
409 return LDAP_NO_MEMORY
;
412 err
= getLoginConfig(ld
, objectDN
, methodIDLen
, &methodID
, tag
, &pwdBufLen
, pwdBuf
);
417 pwdBuf
[pwdBufLen
] = 0; /* null terminate */
421 case 1: /* cleartext password */
423 case 2: /* SHA1 HASH */
425 case 4: /* UNIXCrypt_ID */
426 case 8: /* SSHA_ID */
427 default: /* Unknown digest */
428 err
= LDAP_INAPPROPRIATE_AUTH
; /* only return clear text */
434 if (pwdLen
>= pwdBufLen
-1)
436 memcpy(pwd
, &pwdBuf
[1], pwdBufLen
-1); /* skip digest tag and include null */
440 err
= LDAP_NO_MEMORY
;
448 memset(pwdBuf
, 0, bufferLen
);
456 /**********************************************************************
457 Attempts to set the Universal Password
458 **********************************************************************/
460 static int nmasldap_set_password(
462 const char *objectDN
,
467 struct berval
*requestBV
= NULL
;
468 char *replyOID
= NULL
;
469 struct berval
*replyBV
= NULL
;
472 /* Validate char parameters. */
473 if(objectDN
== NULL
|| (strlen(objectDN
) == 0) || pwd
== NULL
|| ld
== NULL
)
475 return LDAP_NO_SUCH_ATTRIBUTE
;
478 err
= berEncodePasswordData(&requestBV
, objectDN
, pwd
, NULL
);
484 /* Call the ldap_extended_operation (synchronously) */
485 if((err
= ldap_extended_operation_s(ld
, NMASLDAP_SET_PASSWORD_REQUEST
, requestBV
, NULL
, NULL
, &replyOID
, &replyBV
)))
490 /* Make sure there is a return OID */
493 err
= LDAP_NOT_SUPPORTED
;
497 /* Is this what we were expecting to get back. */
498 if(strcmp(replyOID
, NMASLDAP_SET_PASSWORD_RESPONSE
))
500 err
= LDAP_NOT_SUPPORTED
;
504 /* Do we have a good returned berval? */
507 /* No; returned berval means we experienced a rather drastic error. */
508 /* Return operations error. */
509 err
= LDAP_OPERATIONS_ERROR
;
513 err
= berDecodeLoginData(replyBV
, &serverVersion
, NULL
, NULL
);
515 if(serverVersion
!= NMAS_LDAP_EXT_VERSION
)
517 err
= LDAP_OPERATIONS_ERROR
;
528 /* Free the return OID string if one was returned. */
531 ldap_memfree(replyOID
);
534 /* Free memory allocated while building the request ber and berval. */
537 ber_bvfree(requestBV
);
540 /* Return the appropriate error/success code. */
544 /**********************************************************************
545 Attempts to get the Universal Password
546 **********************************************************************/
548 static int nmasldap_get_password(
551 size_t *pwdSize
, /* in bytes */
556 struct berval
*requestBV
= NULL
;
557 char *replyOID
= NULL
;
558 struct berval
*replyBV
= NULL
;
561 size_t pwdBufLen
, bufferLen
;
563 /* Validate char parameters. */
564 if(objectDN
== NULL
|| (strlen(objectDN
) == 0) || pwdSize
== NULL
|| ld
== NULL
)
566 return LDAP_NO_SUCH_ATTRIBUTE
;
569 bufferLen
= pwdBufLen
= *pwdSize
;
570 pwdBuf
= SMB_MALLOC_ARRAY(char, pwdBufLen
+2);
573 return LDAP_NO_MEMORY
;
576 err
= berEncodePasswordData(&requestBV
, objectDN
, NULL
, NULL
);
582 /* Call the ldap_extended_operation (synchronously) */
583 if((err
= ldap_extended_operation_s(ld
, NMASLDAP_GET_PASSWORD_REQUEST
, requestBV
, NULL
, NULL
, &replyOID
, &replyBV
)))
588 /* Make sure there is a return OID */
591 err
= LDAP_NOT_SUPPORTED
;
595 /* Is this what we were expecting to get back. */
596 if(strcmp(replyOID
, NMASLDAP_GET_PASSWORD_RESPONSE
))
598 err
= LDAP_NOT_SUPPORTED
;
602 /* Do we have a good returned berval? */
605 /* No; returned berval means we experienced a rather drastic error. */
606 /* Return operations error. */
607 err
= LDAP_OPERATIONS_ERROR
;
611 err
= berDecodeLoginData(replyBV
, &serverVersion
, &pwdBufLen
, pwdBuf
);
613 if(serverVersion
!= NMAS_LDAP_EXT_VERSION
)
615 err
= LDAP_OPERATIONS_ERROR
;
619 if (!err
&& pwdBufLen
!= 0)
621 if (*pwdSize
>= pwdBufLen
+1 && pwd
!= NULL
)
623 memcpy(pwd
, pwdBuf
, pwdBufLen
);
624 pwd
[pwdBufLen
] = 0; /* add null termination */
626 *pwdSize
= pwdBufLen
; /* does not include null termination */
636 /* Free the return OID string if one was returned. */
639 ldap_memfree(replyOID
);
642 /* Free memory allocated while building the request ber and berval. */
645 ber_bvfree(requestBV
);
650 memset(pwdBuf
, 0, bufferLen
);
654 /* Return the appropriate error/success code. */
658 /**********************************************************************
659 Get the user's password from NDS.
660 *********************************************************************/
662 int pdb_nds_get_password(
663 struct smbldap_state
*ldap_state
,
668 LDAP
*ld
= ldap_state
->ldap_struct
;
671 rc
= nmasldap_get_password(ld
, object_dn
, pwd_len
, (unsigned char *)pwd
);
672 if (rc
== LDAP_SUCCESS
) {
673 #ifdef DEBUG_PASSWORD
674 DEBUG(100,("nmasldap_get_password returned %s for %s\n", pwd
, object_dn
));
676 DEBUG(5, ("NDS Universal Password retrieved for %s\n", object_dn
));
678 DEBUG(3, ("NDS Universal Password NOT retrieved for %s\n", object_dn
));
681 if (rc
!= LDAP_SUCCESS
) {
682 rc
= nmasldap_get_simple_pwd(ld
, object_dn
, *pwd_len
, pwd
);
683 if (rc
== LDAP_SUCCESS
) {
684 #ifdef DEBUG_PASSWORD
685 DEBUG(100,("nmasldap_get_simple_pwd returned %s for %s\n", pwd
, object_dn
));
687 DEBUG(5, ("NDS Simple Password retrieved for %s\n", object_dn
));
689 /* We couldn't get the password */
690 DEBUG(3, ("NDS Simple Password NOT retrieved for %s\n", object_dn
));
691 return LDAP_INVALID_CREDENTIALS
;
695 /* We got the password */
699 /**********************************************************************
700 Set the users NDS, Universal and Simple passwords.
701 ********************************************************************/
703 int pdb_nds_set_password(
704 struct smbldap_state
*ldap_state
,
708 LDAP
*ld
= ldap_state
->ldap_struct
;
710 LDAPMod
**tmpmods
= NULL
;
712 rc
= nmasldap_set_password(ld
, object_dn
, pwd
);
713 if (rc
== LDAP_SUCCESS
) {
714 DEBUG(5,("NDS Universal Password changed for user %s\n", object_dn
));
716 char *ld_error
= NULL
;
717 ldap_get_option(ld
, LDAP_OPT_ERROR_STRING
, &ld_error
);
719 /* This will fail if Universal Password is not enabled for the user's context */
720 DEBUG(3,("NDS Universal Password could not be changed for user %s: %s (%s)\n",
721 object_dn
, ldap_err2string(rc
), ld_error
?ld_error
:"unknown"));
725 /* Set eDirectory Password */
726 smbldap_set_mod(&tmpmods
, LDAP_MOD_REPLACE
, "userPassword", pwd
);
727 rc
= smbldap_modify(ldap_state
, object_dn
, tmpmods
);
732 /**********************************************************************
733 Allow ldap server to update internal login attempt counters by
734 performing a simple bind. If the samba authentication failed attempt
735 the bind with a bogus, randomly generated password to count the
736 failed attempt. If the bind fails even though samba authentication
737 succeeded, this would indicate that the user's account is disabled,
738 time restrictions are in place or some other password policy
740 *********************************************************************/
742 static NTSTATUS
pdb_nds_update_login_attempts(struct pdb_methods
*methods
,
743 struct samu
*sam_acct
, bool success
)
745 struct ldapsam_privates
*ldap_state
;
747 if ((!methods
) || (!sam_acct
)) {
748 DEBUG(3,("pdb_nds_update_login_attempts: invalid parameter.\n"));
749 return NT_STATUS_MEMORY_NOT_ALLOCATED
;
752 ldap_state
= (struct ldapsam_privates
*)methods
->private_data
;
755 /* Attempt simple bind with user credentials to update eDirectory
759 LDAPMessage
*result
= NULL
;
760 LDAPMessage
*entry
= NULL
;
761 const char **attr_list
;
763 char clear_text_pw
[512];
765 const char *username
= pdb_get_username(sam_acct
);
766 bool got_clear_text_pw
= False
;
768 DEBUG(5,("pdb_nds_update_login_attempts: %s login for %s\n",
769 success
? "Successful" : "Failed", username
));
771 result
= (LDAPMessage
*)pdb_get_backend_private_data(sam_acct
, methods
);
773 attr_list
= get_userattr_list(NULL
,
774 ldap_state
->schema_ver
);
775 rc
= ldapsam_search_suffix_by_name(ldap_state
, username
, &result
, attr_list
);
776 TALLOC_FREE( attr_list
);
777 if (rc
!= LDAP_SUCCESS
) {
778 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
780 pdb_set_backend_private_data(sam_acct
, result
, NULL
,
781 methods
, PDB_CHANGED
);
782 talloc_autofree_ldapmsg(sam_acct
, result
);
785 if (ldap_count_entries(ldap_state
->smbldap_state
->ldap_struct
, result
) == 0) {
786 DEBUG(0, ("pdb_nds_update_login_attempts: No user to modify!\n"));
787 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
790 entry
= ldap_first_entry(ldap_state
->smbldap_state
->ldap_struct
, result
);
791 dn
= smbldap_get_dn(ldap_state
->smbldap_state
->ldap_struct
, entry
);
793 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
796 DEBUG(3, ("pdb_nds_update_login_attempts: username %s found dn '%s'\n", username
, dn
));
798 pwd_len
= sizeof(clear_text_pw
);
799 if (success
== True
) {
800 if (pdb_nds_get_password(ldap_state
->smbldap_state
, dn
, &pwd_len
, clear_text_pw
) == LDAP_SUCCESS
) {
801 /* Got clear text password. Use simple ldap bind */
802 got_clear_text_pw
= True
;
805 generate_random_buffer((unsigned char *)clear_text_pw
, 24);
806 clear_text_pw
[24] = '\0';
807 DEBUG(5,("pdb_nds_update_login_attempts: using random password %s\n", clear_text_pw
));
810 if((success
!= True
) || (got_clear_text_pw
== True
)) {
812 rc
= smb_ldap_setup_full_conn(&ld
, ldap_state
->location
);
814 return NT_STATUS_INVALID_CONNECTION
;
817 /* Attempt simple bind with real or bogus password */
818 rc
= ldap_simple_bind_s(ld
, dn
, clear_text_pw
);
820 if (rc
== LDAP_SUCCESS
) {
821 DEBUG(5,("pdb_nds_update_login_attempts: ldap_simple_bind_s Successful for %s\n", username
));
823 NTSTATUS nt_status
= NT_STATUS_ACCOUNT_RESTRICTION
;
824 DEBUG(5,("pdb_nds_update_login_attempts: ldap_simple_bind_s Failed for %s\n", username
));
826 case LDAP_INVALID_CREDENTIALS
:
827 nt_status
= NT_STATUS_WRONG_PASSWORD
;
829 case LDAP_UNWILLING_TO_PERFORM
:
830 /* eDir returns this if the account was disabled. */
831 /* The problem is we don't know if the given
832 password was correct for this account or
833 not. We have to return more info than we
834 should and tell the client NT_STATUS_ACCOUNT_DISABLED
835 so they don't think the password was bad. JRA. */
836 nt_status
= NT_STATUS_ACCOUNT_DISABLED
;
849 /**********************************************************************
850 Intitalise the parts of the pdb_methods structuire that are common
852 *********************************************************************/
854 static NTSTATUS
pdb_init_NDS_ldapsam_common(struct pdb_methods
**pdb_method
, const char *location
)
856 struct ldapsam_privates
*ldap_state
=
857 (struct ldapsam_privates
*)((*pdb_method
)->private_data
);
859 /* Mark this as eDirectory ldap */
860 ldap_state
->is_nds_ldap
= True
;
862 /* Add pdb_nds specific method for updating login attempts. */
863 (*pdb_method
)->update_login_attempts
= pdb_nds_update_login_attempts
;
865 /* Save location for use in pdb_nds_update_login_attempts */
866 ldap_state
->location
= SMB_STRDUP(location
);
872 /**********************************************************************
873 Initialise the 'nds compat' mode for pdb_ldap
874 *********************************************************************/
876 static NTSTATUS
pdb_init_NDS_ldapsam_compat(struct pdb_methods
**pdb_method
, const char *location
)
878 NTSTATUS nt_status
= pdb_init_ldapsam_compat(pdb_method
, location
);
880 (*pdb_method
)->name
= "NDS_ldapsam_compat";
882 pdb_init_NDS_ldapsam_common(pdb_method
, location
);
888 /**********************************************************************
889 Initialise the 'nds' normal mode for pdb_ldap
890 *********************************************************************/
892 static NTSTATUS
pdb_init_NDS_ldapsam(struct pdb_methods
**pdb_method
, const char *location
)
894 NTSTATUS nt_status
= pdb_init_ldapsam(pdb_method
, location
);
896 (*pdb_method
)->name
= "NDS_ldapsam";
898 pdb_init_NDS_ldapsam_common(pdb_method
, location
);
903 NTSTATUS
pdb_nds_init(void)
906 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "NDS_ldapsam", pdb_init_NDS_ldapsam
)))
909 if (!NT_STATUS_IS_OK(nt_status
= smb_register_passdb(PASSDB_INTERFACE_VERSION
, "NDS_ldapsam_compat", pdb_init_NDS_ldapsam_compat
)))