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