xref: /onnv-gate/usr/src/lib/libldap4/common/sbind.c (revision 3857:21b9b714e4ab)
1 /*
2  *
3  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  */
7 
8 #pragma ident	"%Z%%M%	%I%	%E% SMI"
9 
10 /*
11  *  Copyright (c) 1993 Regents of the University of Michigan.
12  *  All rights reserved.
13  *
14  *  sbind.c
15  */
16 
17 #ifndef lint
18 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #endif
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #ifdef MACOS
25 #include "macos.h"
26 #endif /* MACOS */
27 
28 #if !defined( MACOS ) && !defined( DOS )
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #endif
32 
33 #include "lber.h"
34 #include "ldap.h"
35 #include "ldap-private.h"
36 #include "ldap-int.h"
37 
ldap_build_simple_bind_req(LDAP * ld,char * dn,char * passwd,LDAPControl ** serverctrls)38 BerElement * ldap_build_simple_bind_req(LDAP *ld, char *dn, char *passwd, LDAPControl **serverctrls)
39 {
40 	/*
41 	 * The bind request looks like this:
42 	 *	BindRequest ::= SEQUENCE {
43 	 *		version		INTEGER,
44 	 *		name		DistinguishedName,	 -- who
45 	 *		authentication	CHOICE {
46 	 *			simple		[0] OCTET STRING -- passwd
47 	 *		}
48 	 *	}
49 	 * all wrapped up in an LDAPMessage sequence.
50 	 */
51 
52 	BerElement *ber = NULL;
53 
54 	if ( dn == NULL )
55 		dn = "";
56 	if ( passwd == NULL )
57 		passwd = "";
58 
59 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
60 		return (NULLBER);
61 	}
62 
63 	/* fill it in */
64 	if ( ber_printf( ber, "{it{ists}", ++ld->ld_msgid, LDAP_REQ_BIND, ld->ld_version, dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
65 		ld->ld_errno = LDAP_ENCODING_ERROR;
66 		ber_free( ber, 1 );
67 		return( NULLBER );
68 	}
69 
70 	/* LDAPv3 */
71 	/* Code controls if any */
72 	if (serverctrls && serverctrls[0]) {
73 		if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
74 			ld->ld_errno = LDAP_ENCODING_ERROR;
75 			ber_free( ber, 1 );
76 			return( NULLBER );
77 		}
78 	} else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
79 		/* Otherwise, is there any global server ctrls ? */
80 		if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
81 			ld->ld_errno = LDAP_ENCODING_ERROR;
82 			ber_free( ber, 1 );
83 			return( NULLBER );
84 		}
85 	}
86 
87 	if ( ber_printf( ber, "}" ) == -1 ) {
88 		ld->ld_errno = LDAP_ENCODING_ERROR;
89 		ber_free( ber, 1 );
90 		return( NULLBER );
91 	}
92 
93 	return (ber);
94 }
95 
96 /*
97  * ldap_simple_bind - bind to the ldap server (and X.500).  The dn and
98  * password of the entry to which to bind are supplied.  The message id
99  * of the request initiated is returned.
100  *
101  * Example:
102  *	ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
103  *	    "secret" )
104  */
105 
106 int
ldap_simple_bind(LDAP * ld,char * dn,char * passwd)107 ldap_simple_bind( LDAP *ld, char *dn, char *passwd )
108 {
109 	BerElement	*ber;
110 	int rv;
111 
112 
113 #ifdef _REENTRANT
114 	LOCK_LDAP(ld);
115 #endif
116 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 240, "ldap_simple_bind\n"), 0, 0, 0 );
117 
118 	if ( dn == NULL )
119 		dn = "";
120 	if ( passwd == NULL )
121 		passwd = "";
122 
123 	/* create a message to send */
124 	if ( (ber = ldap_build_simple_bind_req( ld, dn, passwd, NULL )) == NULLBER ) {
125 #ifdef _REENTRANT
126 		UNLOCK_LDAP(ld);
127 #endif
128 		return( -1 );
129 	}
130 
131 #ifndef NO_CACHE
132 	if ( ld->ld_cache != NULL ) {
133 		ldap_flush_cache( ld );
134 	}
135 #endif /* !NO_CACHE */
136 
137 	/* send the message */
138 	rv = send_initial_request( ld, LDAP_REQ_BIND, dn, ber );
139 #ifdef _REENTRANT
140 	UNLOCK_LDAP(ld);
141 #endif
142 	return ( rv );
143 }
144 
145 /*
146  * ldap_simple_bind - bind to the ldap server (and X.500) using simple
147  * authentication.  The dn and password of the entry to which to bind are
148  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
149  * otherwise.
150  *
151  * Example:
152  *	ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
153  *	    "secret" )
154  */
155 
156 int
ldap_simple_bind_s(LDAP * ld,char * dn,char * passwd)157 ldap_simple_bind_s( LDAP *ld, char *dn, char *passwd )
158 {
159 	int		msgid;
160 	LDAPMessage	*result;
161 
162 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 241, "ldap_simple_bind_s\n"), 0, 0, 0 );
163 
164 	if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
165 		return( ld->ld_errno );
166 
167 	if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
168 		return( ld->ld_errno );	/* ldap_result sets ld_errno */
169 
170 	return( ldap_result2error( ld, result, 1 ) );
171 }
172