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