xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/sbind.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $OpenLDAP: pkg/ldap/libraries/libldap/sbind.c,v 1.25.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18 
19 /*
20  *	BindRequest ::= SEQUENCE {
21  *		version		INTEGER,
22  *		name		DistinguishedName,	 -- who
23  *		authentication	CHOICE {
24  *			simple		[0] OCTET STRING -- passwd
25  *			krbv42ldap	[1] OCTET STRING  -- OBSOLETE
26  *			krbv42dsa	[2] OCTET STRING  -- OBSOLETE
27  *			sasl		[3] SaslCredentials	-- LDAPv3
28  *		}
29  *	}
30  *
31  *	BindResponse ::= SEQUENCE {
32  *		COMPONENTS OF LDAPResult,
33  *		serverSaslCreds		OCTET STRING OPTIONAL -- LDAPv3
34  *	}
35  *
36  */
37 
38 #include "portable.h"
39 
40 #include <stdio.h>
41 
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45 
46 #include "ldap-int.h"
47 
48 /*
49  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
50  * password of the entry to which to bind are supplied.  The message id
51  * of the request initiated is returned.
52  *
53  * Example:
54  *	ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
55  *	    "secret" )
56  */
57 
58 int
59 ldap_simple_bind(
60 	LDAP *ld,
61 	LDAP_CONST char *dn,
62 	LDAP_CONST char *passwd )
63 {
64 	int rc;
65 	int msgid;
66 	struct berval cred;
67 
68 	Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
69 
70 	assert( ld != NULL );
71 	assert( LDAP_VALID( ld ) );
72 
73 	if ( passwd != NULL ) {
74 		cred.bv_val = (char *) passwd;
75 		cred.bv_len = strlen( passwd );
76 	} else {
77 		cred.bv_val = "";
78 		cred.bv_len = 0;
79 	}
80 
81 	rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
82 		NULL, NULL, &msgid );
83 
84 	return rc == LDAP_SUCCESS ? msgid : -1;
85 }
86 
87 /*
88  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
89  * authentication.  The dn and password of the entry to which to bind are
90  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
91  * otherwise.
92  *
93  * Example:
94  *	ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
95  *	    "secret" )
96  */
97 
98 int
99 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
100 {
101 	struct berval cred;
102 
103 	Debug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
104 
105 	if ( passwd != NULL ) {
106 		cred.bv_val = (char *) passwd;
107 		cred.bv_len = strlen( passwd );
108 	} else {
109 		cred.bv_val = "";
110 		cred.bv_len = 0;
111 	}
112 
113 	return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
114 		NULL, NULL, NULL );
115 }
116