xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/sbind.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: sbind.c,v 1.3 2021/08/14 16:14:56 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 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 <sys/cdefs.h>
41 __RCSID("$NetBSD: sbind.c,v 1.3 2021/08/14 16:14:56 christos Exp $");
42 
43 #include "portable.h"
44 
45 #include <stdio.h>
46 
47 #include <ac/socket.h>
48 #include <ac/string.h>
49 #include <ac/time.h>
50 
51 #include "ldap-int.h"
52 
53 /*
54  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
55  * password of the entry to which to bind are supplied.  The message id
56  * of the request initiated is returned.
57  *
58  * Example:
59  *	ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
60  *	    "secret" )
61  */
62 
63 int
ldap_simple_bind(LDAP * ld,LDAP_CONST char * dn,LDAP_CONST char * passwd)64 ldap_simple_bind(
65 	LDAP *ld,
66 	LDAP_CONST char *dn,
67 	LDAP_CONST char *passwd )
68 {
69 	int rc;
70 	int msgid;
71 	struct berval cred;
72 
73 	Debug0( LDAP_DEBUG_TRACE, "ldap_simple_bind\n" );
74 
75 	assert( ld != NULL );
76 	assert( LDAP_VALID( ld ) );
77 
78 	if ( passwd != NULL ) {
79 		cred.bv_val = (char *) passwd;
80 		cred.bv_len = strlen( passwd );
81 	} else {
82 		cred.bv_val = "";
83 		cred.bv_len = 0;
84 	}
85 
86 	rc = ldap_sasl_bind( ld, dn, LDAP_SASL_SIMPLE, &cred,
87 		NULL, NULL, &msgid );
88 
89 	return rc == LDAP_SUCCESS ? msgid : -1;
90 }
91 
92 /*
93  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
94  * authentication.  The dn and password of the entry to which to bind are
95  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
96  * otherwise.
97  *
98  * Example:
99  *	ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
100  *	    "secret" )
101  */
102 
103 int
ldap_simple_bind_s(LDAP * ld,LDAP_CONST char * dn,LDAP_CONST char * passwd)104 ldap_simple_bind_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *passwd )
105 {
106 	struct berval cred;
107 
108 	Debug0( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n" );
109 
110 	if ( passwd != NULL ) {
111 		cred.bv_val = (char *) passwd;
112 		cred.bv_len = strlen( passwd );
113 	} else {
114 		cred.bv_val = "";
115 		cred.bv_len = 0;
116 	}
117 
118 	return ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, &cred,
119 		NULL, NULL, NULL );
120 }
121