Back to index

lightning-sunbird  0.9+nobinonly
srvrsort.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Search the directory for all people. Ask the server to sort the entries
00040  * first by sn (last name) and then in reverse order by givenname (first name).
00041  */
00042 
00043 #include "examples.h"
00044 
00045 int
00046 main( int argc, char **argv )
00047 {
00048        LDAP                 *ld;
00049        LDAPMessage          *result, *e;
00050        char                 *attrfail, *matched = NULL, *errmsg = NULL;
00051        char                 **vals, **referrals;
00052        int                  rc, parse_rc, version;
00053        unsigned long        sortrc;
00054        LDAPControl          *sortctrl = NULL;
00055        LDAPControl          *requestctrls[ 2 ];
00056        LDAPControl          **resultctrls = NULL;
00057        LDAPsortkey          **sortkeylist;
00058 
00059        /* Arrange for all connections to use LDAPv3 */
00060        version = LDAP_VERSION3;
00061        if ( ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version )
00062            != 0 ) {
00063               fprintf( stderr,
00064                   "ldap_set_option protocol version to %d failed\n",
00065                   version );
00066               return( 1 );
00067        }
00068 
00069        /* Get a handle to an LDAP connection */
00070        if ( (ld = ldap_init( MY_HOST, MY_PORT ) ) == NULL ) {
00071               perror( "ldap_init" );
00072               return( 1 );
00073        }
00074 
00075        /* Authenticate as Directory Manager */
00076        if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
00077               ldap_perror( ld, "ldap_simple_bind_s" );
00078               ldap_unbind( ld );
00079               return( 1 );
00080        }
00081 
00082        /*
00083         * Create a sort key list that specifies the sort order of the results.
00084         * Sort the results by last name first, then by first name.
00085         */
00086        ldap_create_sort_keylist( &sortkeylist, "description -givenname" );
00087 
00088        /* Create the sort control. */
00089        rc = ldap_create_sort_control( ld, sortkeylist, 1, &sortctrl );
00090        if ( rc != LDAP_SUCCESS ) {
00091               fprintf( stderr, "ldap_create_sort_control: %s\n",
00092                   ldap_err2string( rc ) );
00093               ldap_unbind( ld );
00094               return( 1 );
00095        }
00096        requestctrls[ 0 ] = sortctrl;
00097        requestctrls[ 1 ] = NULL;
00098 
00099        /* Search for all entries in Sunnyvale */
00100 
00101        rc = ldap_search_ext_s( ld, PEOPLE_BASE, LDAP_SCOPE_SUBTREE,
00102            "(objectclass=person)", NULL, 0, requestctrls, NULL, NULL, 0,
00103            &result );
00104 
00105        if ( rc != LDAP_SUCCESS ) {
00106               fprintf( stderr, "ldap_search_ext_s: %s\n",
00107                   ldap_err2string( rc ) );
00108               ldap_unbind( ld );
00109               return( 1 );
00110        }
00111 
00112        parse_rc = ldap_parse_result( ld, result, &rc, &matched, &errmsg,
00113            &referrals, &resultctrls, 0 );
00114 
00115        if ( parse_rc != LDAP_SUCCESS ) {
00116               fprintf( stderr, "ldap_parse_result: %s\n",
00117                   ldap_err2string( parse_rc ) );
00118               ldap_unbind( ld );
00119               return( 1 );
00120        }
00121 
00122        if ( rc != LDAP_SUCCESS ) {
00123               fprintf( stderr, "ldap_search_ext_s: %s\n",
00124                   ldap_err2string( rc ) );
00125               if ( errmsg != NULL && *errmsg != '\0' ) {
00126                      fprintf( stderr, "%s\n", errmsg );
00127               }
00128 
00129               ldap_unbind( ld );
00130               return( 1 );
00131        }
00132 
00133        parse_rc = ldap_parse_sort_control( ld, resultctrls, &sortrc,
00134            &attrfail );
00135 
00136        if ( parse_rc != LDAP_SUCCESS ) {
00137               fprintf( stderr, "ldap_parse_sort_control: %s\n",
00138                   ldap_err2string( parse_rc ) );
00139               ldap_unbind( ld );
00140               return( 1 );
00141        }
00142 
00143        if ( sortrc != LDAP_SUCCESS ) {
00144               fprintf( stderr, "Sort error: %s\n", ldap_err2string( sortrc ));
00145               if ( attrfail != NULL && *attrfail != '\0' ) {
00146                      fprintf( stderr, "Bad attribute: %s\n", attrfail);
00147               }
00148               ldap_unbind( ld );
00149               return( 1 );
00150        }
00151 
00152        /* for each entry print out name + all attrs and values */
00153        for ( e = ldap_first_entry( ld, result ); e != NULL;
00154            e = ldap_next_entry( ld, e ) ) {
00155               if ((vals = ldap_get_values( ld, e, "sn")) != NULL ) {
00156                      if ( vals[0] != NULL ) {
00157                             printf( "%s", vals[0] );
00158                      }
00159                      ldap_value_free( vals );
00160               }
00161 
00162               if ((vals = ldap_get_values( ld, e, "givenname")) != NULL ) {
00163                      if ( vals[0] != NULL ) {
00164                             printf( "\t%s", vals[0] );
00165                      }
00166                      ldap_value_free( vals );
00167               }
00168 
00169               putchar( '\n' );
00170        }
00171 
00172        ldap_msgfree( result );
00173        ldap_free_sort_keylist( sortkeylist );
00174        ldap_control_free( sortctrl );
00175        ldap_controls_free( resultctrls );
00176        ldap_unbind( ld );
00177 
00178        return( 0 );
00179 }