/*********************************************************************** * ldap_create_sort_controlW (WLDAP32.@) * * Create a control for server sorted search results. * * PARAMS * ld [I] Pointer to an LDAP context. * sortkey [I] Array of LDAPSortKey structures, each specifying an * attribute to use as a sort key, a matching rule and * the sort order (ascending or descending). * critical [I] Tells the server this control is critical to the * search operation. * control [O] LDAPControl created. * * RETURNS * Success: LDAP_SUCCESS * Failure: An LDAP error code. * * NOTES * Pass the created control as a server control in subsequent calls * to ldap_search_ext(_s) to obtain sorted search results. */ ULONG CDECL ldap_create_sort_controlW( WLDAP32_LDAP *ld, PLDAPSortKeyW *sortkey, UCHAR critical, PLDAPControlW *control ) { ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; #ifdef HAVE_LDAP LDAPSortKey **sortkeyU = NULL; LDAPControl *controlU = NULL; TRACE( "(%p, %p, 0x%02x, %p)\n", ld, sortkey, critical, control ); if (!ld || !sortkey || !control) return WLDAP32_LDAP_PARAM_ERROR; sortkeyU = sortkeyarrayWtoU( sortkey ); if (!sortkeyU) return WLDAP32_LDAP_NO_MEMORY; ret = map_error( ldap_create_sort_control( ld, sortkeyU, critical, &controlU )); *control = controlUtoW( controlU ); if (!*control) ret = WLDAP32_LDAP_NO_MEMORY; ldap_control_free( controlU ); sortkeyarrayfreeU( sortkeyU ); #endif return ret; }
int main( int argc, char **argv ) { LDAP *ld; LDAPMessage *result, *e; char *attrfail, *matched = NULL, *errmsg = NULL; char **vals, **referrals; int rc, parse_rc, version; unsigned long sortrc; LDAPControl *sortctrl = NULL; LDAPControl *requestctrls[ 2 ]; LDAPControl **resultctrls = NULL; LDAPsortkey **sortkeylist; /* Arrange for all connections to use LDAPv3 */ version = LDAP_VERSION3; if ( ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version ) != 0 ) { fprintf( stderr, "ldap_set_option protocol version to %d failed\n", version ); return( 1 ); } /* Get a handle to an LDAP connection */ if ( (ld = ldap_init( MY_HOST, MY_PORT ) ) == NULL ) { perror( "ldap_init" ); return( 1 ); } /* Authenticate as Directory Manager */ if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_simple_bind_s" ); ldap_unbind( ld ); return( 1 ); } /* * Create a sort key list that specifies the sort order of the results. * Sort the results by last name first, then by first name. */ ldap_create_sort_keylist( &sortkeylist, "description -givenname" ); /* Create the sort control. */ rc = ldap_create_sort_control( ld, sortkeylist, 1, &sortctrl ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_create_sort_control: %s\n", ldap_err2string( rc ) ); ldap_unbind( ld ); return( 1 ); } requestctrls[ 0 ] = sortctrl; requestctrls[ 1 ] = NULL; /* Search for all entries in Sunnyvale */ rc = ldap_search_ext_s( ld, PEOPLE_BASE, LDAP_SCOPE_SUBTREE, "(objectclass=person)", NULL, 0, requestctrls, NULL, NULL, 0, &result ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) ); ldap_unbind( ld ); return( 1 ); } parse_rc = ldap_parse_result( ld, result, &rc, &matched, &errmsg, &referrals, &resultctrls, 0 ); if ( parse_rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) ); ldap_unbind( ld ); return( 1 ); } if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) ); if ( errmsg != NULL && *errmsg != '\0' ) { fprintf( stderr, "%s\n", errmsg ); } ldap_unbind( ld ); return( 1 ); } parse_rc = ldap_parse_sort_control( ld, resultctrls, &sortrc, &attrfail ); if ( parse_rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_parse_sort_control: %s\n", ldap_err2string( parse_rc ) ); ldap_unbind( ld ); return( 1 ); } if ( sortrc != LDAP_SUCCESS ) { fprintf( stderr, "Sort error: %s\n", ldap_err2string( sortrc )); if ( attrfail != NULL && *attrfail != '\0' ) { fprintf( stderr, "Bad attribute: %s\n", attrfail); } ldap_unbind( ld ); return( 1 ); } /* for each entry print out name + all attrs and values */ for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) { if ((vals = ldap_get_values( ld, e, "sn")) != NULL ) { if ( vals[0] != NULL ) { printf( "%s", vals[0] ); } ldap_value_free( vals ); } if ((vals = ldap_get_values( ld, e, "givenname")) != NULL ) { if ( vals[0] != NULL ) { printf( "\t%s", vals[0] ); } ldap_value_free( vals ); } putchar( '\n' ); } ldap_msgfree( result ); ldap_free_sort_keylist( sortkeylist ); ldap_control_free( sortctrl ); ldap_controls_free( resultctrls ); ldap_unbind( ld ); return( 0 ); }
/* LDAP search function for internal use. Returns a Python list of LDAPEntries. The `basestr` is the base DN of the searching, `scope` is the search scope (BASE|ONELEVEL|SUB), `filterstr` is the LDAP search filter string, `attrs` is a null-terminated string list of attributes' names to get only the selected attributes. If `attrsonly` is 1 get only attributes' name without values. If `firstonly` is 1, get only the first LDAP entry of the messages. The `timeout` is an integer of seconds for timelimit, `sizelimit` is a limit for size. */ PyObject * LDAPConnection_Searching(LDAPConnection *self, PyObject *iterator) { int rc; int num_of_ctrls = 0; LDAPMessage *res, *entry; PyObject *entrylist = NULL; LDAPEntry *entryobj = NULL; LDAPControl *page_ctrl = NULL; LDAPControl *sort_ctrl = NULL; LDAPControl **server_ctrls = NULL; LDAPControl **returned_ctrls = NULL; LDAPSearchIter *search_iter = (LDAPSearchIter *)iterator; entrylist = PyList_New(0); if (entrylist == NULL) { return PyErr_NoMemory(); } /* Check the number of server controls and allocate it. */ if (self->page_size > 1) num_of_ctrls++; if (self->sort_list != NULL) num_of_ctrls++; if (num_of_ctrls > 0) { server_ctrls = (LDAPControl **)malloc(sizeof(LDAPControl *) * (num_of_ctrls + 1)); if (server_ctrls == NULL) return PyErr_NoMemory(); num_of_ctrls = 0; } if (self->page_size > 1) { /* Create page control and add to the server controls. */ rc = ldap_create_page_control(self->ld, (ber_int_t)(self->page_size), search_iter->cookie, 0, &page_ctrl); if (rc != LDAP_SUCCESS) { PyErr_BadInternalCall(); return NULL; } server_ctrls[num_of_ctrls++] = page_ctrl; server_ctrls[num_of_ctrls] = NULL; } if (self->sort_list != NULL) { rc = ldap_create_sort_control(self->ld, self->sort_list, 0, &sort_ctrl); if (rc != LDAP_SUCCESS) { PyErr_BadInternalCall(); return NULL; } server_ctrls[num_of_ctrls++] = sort_ctrl; server_ctrls[num_of_ctrls] = NULL; } rc = ldap_search_ext_s(self->ld, search_iter->base, search_iter->scope, search_iter->filter, search_iter->attrs, search_iter->attrsonly, server_ctrls, NULL, search_iter->timeout, search_iter->sizelimit, &res); if (rc == LDAP_NO_SUCH_OBJECT) { return entrylist; } if (rc != LDAP_SUCCESS && rc != LDAP_PARTIAL_RESULTS) { Py_DECREF(entrylist); PyObject *ldaperror = get_error_by_code(rc); PyErr_SetString(ldaperror, ldap_err2string(rc)); Py_DECREF(ldaperror); return NULL; } rc = ldap_parse_result(self->ld, res, NULL, NULL, NULL, NULL, &returned_ctrls, 0); #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) if (search_iter->cookie != NULL && search_iter->cookie->bv_val != NULL) { ber_bvfree(search_iter->cookie); search_iter->cookie = NULL; } rc = ldap_parse_page_control(self->ld, returned_ctrls, NULL, &(search_iter->cookie)); #else rc = ldap_parse_pageresponse_control(self->ld, ldap_control_find(LDAP_CONTROL_PAGEDRESULTS, returned_ctrls, NULL), NULL, search_iter->cookie); #endif /* Iterate over the response LDAP messages. */ for (entry = ldap_first_entry(self->ld, res); entry != NULL; entry = ldap_next_entry(self->ld, entry)) { entryobj = LDAPEntry_FromLDAPMessage(entry, self); if (entryobj == NULL) { Py_DECREF(entrylist); return NULL; } if ((entryobj == NULL) || (PyList_Append(entrylist, (PyObject *)entryobj)) != 0) { Py_XDECREF(entryobj); Py_DECREF(entrylist); return PyErr_NoMemory(); } Py_DECREF(entryobj); } /* Cleanup. */ if (returned_ctrls != NULL) ldap_controls_free(returned_ctrls); if (page_ctrl != NULL) ldap_control_free(page_ctrl); if (sort_ctrl != NULL) ldap_control_free(sort_ctrl); if (server_ctrls != NULL) free(server_ctrls); ldap_msgfree(res); return entrylist; }