/* * ldap_search - initiate an ldap (and X.500) search operation. Parameters: * * ld LDAP descriptor * base DN of the base object * scope the search scope - one of LDAP_SCOPE_BASE, * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE * filter a string containing the search filter * (e.g., "(|(cn=bob)(sn=bob))") * attrs list of attribute types to return for matches * attrsonly 1 => attributes only 0 => attributes and values * * Example: * char *attrs[] = { "mail", "title", 0 }; * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob", * attrs, attrsonly ); */ int ldap_search( LDAP *ld, char *base, int scope, char *filter, char **attrs, int attrsonly ) { BerElement *ber; Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 ); if (( ber = ldap_build_search_req( ld, base, scope, filter, attrs, attrsonly )) == NULLBER ) { return( -1 ); } #ifndef NO_CACHE if ( ld->ld_cache != NULL ) { if ( check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) { ber_free( ber, 1 ); ld->ld_errno = LDAP_SUCCESS; return( ld->ld_msgid ); } add_request_to_cache( ld, LDAP_REQ_SEARCH, ber ); } #endif /* NO_CACHE */ /* send the message */ return ( send_initial_request( ld, LDAP_REQ_SEARCH, base, ber )); }
int main(void) { WSADATA Data; SOCKADDR_IN recvSockAddr; SOCKET recvSocket; int status; int numrcv = 0; struct hostent * remoteHost; char * ip; const char * host_name = "pb-homework.appspot.com"; char * buffer = malloc(sizeof(char) * MAXBUFLEN);; memset(buffer,0,MAXBUFLEN); // Initialize Windows Socket DLL status = WSAStartup(MAKEWORD(2, 2), &Data); if(status != 0) { printf("ERROR: WSAStartup unsuccessful\r\n"); return 0; } // Get IP address from host name remoteHost = gethostbyname(host_name); ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list); printf("IP address is: %s.\n", ip); memset(&recvSockAddr, 0, sizeof(recvSockAddr)); // zero the sockaddr_in structure recvSockAddr.sin_port=htons(PORT); // specify the port portion of the address recvSockAddr.sin_family=AF_INET; // specify the address family as Internet recvSockAddr.sin_addr.s_addr= inet_addr(ip); // specify ip address // Create socket recvSocket = socket_new(); // Connect connect_to_server(recvSocket, recvSockAddr); // Send request send_initial_request(recvSocket, host_name); receive_response(recvSocket, buffer); // Receieve printf("%s\r\n", buffer); send_secret_request(recvSocket, host_name, buffer); // Get and make free(buffer); buffer = malloc(sizeof(char) * MAXBUFLEN); receive_response(recvSocket, buffer); printf("%s\r\n", buffer); inverse_str(buffer); // Send send_post(recvSocket, host_name, buffer); // Get answer free(buffer); buffer = malloc(sizeof(char) * MAXBUFLEN); receive_response(recvSocket, buffer); printf("\nServer response:\n\n%s\n", buffer); closesocket(recvSocket); getchar(); // Print out receieved socket data system("pause"); return 0; }
/* * ldap_compare - perform an ldap (and X.500) compare operation. The dn * of the entry to compare to and the attribute and value to compare (in * attr and value) are supplied. The msgid of the response is returned. * * Example: * ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" ) */ int ldap_compare( LDAP *ld, char *dn, char *attr, char *value ) { BerElement *ber; struct berval bv; int rv; /* The compare request looks like this: * CompareRequest ::= SEQUENCE { * entry DistinguishedName, * ava SEQUENCE { * type AttributeType, * value AttributeValue * } * } * and must be wrapped in an LDAPMessage. */ #ifdef _REENTRANT LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 128, "ldap_compare\n"), 0, 0, 0 ); bv.bv_val = value; bv.bv_len = strlen(value); if ((ber = ldap_build_compare_req(ld, dn, attr, &bv, NULL)) == NULLBER) { #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (-1); } #ifndef NO_CACHE if ( ld->ld_cache != NULL ) { if ( check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) { ber_free( ber, 1 ); ld->ld_errno = LDAP_SUCCESS; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return( ld->ld_msgid ); } add_request_to_cache( ld, LDAP_REQ_COMPARE, ber ); } #endif /* NO_CACHE */ /* send the message */ rv = send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber ); #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); }
/* * ldap_modrdn - initiate an ldap (and X.500) modifyRDN operation. Parameters: * * ld LDAP descriptor * dn DN of the object to modify * newrdn RDN to give the object * deleteoldrdn nonzero means to delete old rdn values from the entry * * Example: * msgid = ldap_modrdn( ld, dn, newrdn ); */ int ldap_modrdn( LDAP *ld, char *dn, char *newrdn, int deleteoldrdn ) { BerElement *ber; int rv; /* * A modify rdn request looks like this: * ModifyRDNRequest ::= SEQUENCE { * entry DistinguishedName, * newrdn RelativeDistinguishedName, * deleteoldrdn BOOLEAN * } */ #ifdef _REENTRANT LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 193, "ldap_modrdn\n"), 0, 0, 0 ); /* create a message to send */ if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) { #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return( -1 ); } if ( ber_printf( ber, "{it{ssb}}", ++ld->ld_msgid, LDAP_REQ_MODRDN, dn, newrdn, deleteoldrdn ) == -1 ) { ld->ld_errno = LDAP_ENCODING_ERROR; ber_free( ber, 1 ); #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return( -1 ); } /* send the message */ rv = send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber ); #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return ( rv ); }
int ldap_add_ext(LDAP *ld, char *dn, LDAPMod **attrs, LDAPControl ** serverctrls, LDAPControl **clientctrls, int *msgidp) { BerElement *ber; int i, rc; int rv; #ifdef _REENTRANT LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 ); if ((ber = ldap_build_add_req(ld, dn, attrs, serverctrls)) == NULLBER){ rv = ld->ld_errno; if (rv == LDAP_SUCCESS) rv = LDAP_OTHER; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); } /* send the message */ rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber ); if (rv == -1) { rv = ld->ld_errno; if (rv == LDAP_SUCCESS){ rv = LDAP_OTHER; } #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); } *msgidp = rv; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (LDAP_SUCCESS); }
/* * ldap_add - initiate an ldap (and X.500) add operation. Parameters: * * ld LDAP descriptor * dn DN of the entry to add * mods List of attributes for the entry. This is a null- * terminated array of pointers to LDAPMod structures. * only the type and values in the structures need be * filled in. * * Example: * LDAPMod *attrs[] = { * { 0, "cn", { "babs jensen", "babs", 0 } }, * { 0, "sn", { "jensen", 0 } }, * { 0, "objectClass", { "person", 0 } }, * 0 * } * msgid = ldap_add( ld, dn, attrs ); */ int ldap_add( LDAP *ld, char *dn, LDAPMod **attrs ) { BerElement *ber; int rv; #ifdef _REENTRANT LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 ); if ((ber = ldap_build_add_req(ld, dn, attrs, NULL)) == NULLBER){ #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (-1); } /* send the message */ rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber ); #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); }
/* LDAPv3 API extensions */ int ldap_compare_ext(LDAP *ld, char *dn, char *attr, struct berval *bvalue, LDAPControl ** serverctrls, LDAPControl **clientctrls, int *msgidp) { BerElement *ber; struct berval bv; int rv; /* The compare request looks like this: * CompareRequest ::= SEQUENCE { * entry DistinguishedName, * ava SEQUENCE { * type AttributeType, * value AttributeValue * } * } * and must be wrapped in an LDAPMessage. */ #ifdef _REENTRANT LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 128, "ldap_compare\n"), 0, 0, 0 ); if ((ber = ldap_build_compare_req(ld, dn, attr, bvalue, NULL)) == NULLBER) { rv = ld->ld_errno; if (rv == LDAP_SUCCESS) rv = LDAP_OTHER; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); } #ifndef NO_CACHE if ( ld->ld_cache != NULL ) { if ( check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) { ber_free( ber, 1 ); ld->ld_errno = LDAP_SUCCESS; *msgidp = ld->ld_msgid; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return( LDAP_SUCCESS ); } add_request_to_cache( ld, LDAP_REQ_COMPARE, ber ); } #endif /* NO_CACHE */ /* send the message */ rv = send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber ); if (rv == -1) { rv = ld->ld_errno; if (rv == LDAP_SUCCESS){ rv = LDAP_OTHER; } #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (rv); } *msgidp = rv; #ifdef _REENTRANT UNLOCK_LDAP(ld); #endif return (LDAP_SUCCESS); }
/* * ldap_kerberos_bind1 - initiate a bind to the ldap server using * kerberos authentication. The dn is supplied. It is assumed the user * already has a valid ticket granting ticket. The msgid of the * request is returned on success (suitable for passing to ldap_result()), * -1 is returned if there's trouble. * * Example: * ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" ) */ int ldap_kerberos_bind1( LDAP *ld, char *dn ) { BerElement *ber; char *cred; int rc, credlen; char *get_kerberosv4_credentials(); #ifdef STR_TRANSLATION int str_translation_on; #endif /* STR_TRANSLATION */ /* * The bind request looks like this: * BindRequest ::= SEQUENCE { * version INTEGER, * name DistinguishedName, * authentication CHOICE { * krbv42ldap [1] OCTET STRING * krbv42dsa [2] OCTET STRING * } * } * all wrapped up in an LDAPMessage sequence. */ #if defined( SUN ) && defined( _REENTRANT ) int rv; LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 186, "ldap_kerberos_bind1\n"), 0, 0, 0 ); if ( dn == NULL ) dn = ""; if ( (cred = get_kerberosv4_credentials( ld, dn, "ldapserver", &credlen )) == NULL ) { #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); /* ld_errno should already be set */ } /* create a message to send */ if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) { free( cred ); #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); } #ifdef STR_TRANSLATION if (( str_translation_on = (( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */ ber->ber_options &= ~LBER_TRANSLATE_STRINGS; } #endif /* STR_TRANSLATION */ /* fill it in */ rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND, ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen ); #ifdef STR_TRANSLATION if ( str_translation_on ) { /* restore translation */ ber->ber_options |= LBER_TRANSLATE_STRINGS; } #endif /* STR_TRANSLATION */ if ( rc == -1 ) { free( cred ); ber_free( ber, 1 ); ld->ld_errno = LDAP_ENCODING_ERROR; #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); } free( cred ); #ifndef NO_CACHE if ( ld->ld_cache != NULL ) { ldap_flush_cache( ld ); } #endif /* !NO_CACHE */ /* send the message */ #if defined( SUN ) && defined( _REENTRANT ) rv = send_initial_request( ld, LDAP_REQ_BIND, dn, ber ); UNLOCK_LDAP(ld); return ( rv ); #else return ( send_initial_request( ld, LDAP_REQ_BIND, dn, ber )); #endif }
/* * ldap_kerberos_bind2 - initiate a bind to the X.500 server using * kerberos authentication. The dn is supplied. It is assumed the user * already has a valid ticket granting ticket. The msgid of the * request is returned on success (suitable for passing to ldap_result()), * -1 is returned if there's trouble. * * Example: * ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" ) */ int ldap_kerberos_bind2( LDAP *ld, char *dn ) { BerElement *ber; char *cred; int rc, credlen; char *get_kerberosv4_credentials(); #ifdef STR_TRANSLATION int str_translation_on; #endif /* STR_TRANSLATION */ #if defined( SUN ) && defined( _REENTRANT ) int rv; LOCK_LDAP(ld); #endif Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 188, "ldap_kerberos_bind2\n"), 0, 0, 0 ); if ( dn == NULL ) dn = ""; if ( (cred = get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen )) == NULL ) { #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); /* ld_errno should already be set */ } /* create a message to send */ if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) { free( cred ); #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); } #ifdef STR_TRANSLATION if (( str_translation_on = (( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */ ber->ber_options &= ~LBER_TRANSLATE_STRINGS; } #endif /* STR_TRANSLATION */ /* fill it in */ rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND, ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen ); #ifdef STR_TRANSLATION if ( str_translation_on ) { /* restore translation */ ber->ber_options |= LBER_TRANSLATE_STRINGS; } #endif /* STR_TRANSLATION */ free( cred ); if ( rc == -1 ) { ber_free( ber, 1 ); ld->ld_errno = LDAP_ENCODING_ERROR; #if defined( SUN ) && defined( _REENTRANT ) UNLOCK_LDAP(ld); #endif return( -1 ); } /* send the message */ #if defined( SUN ) && defined( _REENTRANT ) rv = send_initial_request( ld, LDAP_REQ_BIND, dn, ber ); UNLOCK_LDAP(ld); return ( rv ); #endif return ( send_initial_request( ld, LDAP_REQ_BIND, dn, ber )); }