]> git.sur5r.net Git - openldap/commitdiff
rework control response handling
authorPierangelo Masarati <ando@openldap.org>
Sun, 8 Jan 2006 19:51:28 +0000 (19:51 +0000)
committerPierangelo Masarati <ando@openldap.org>
Sun, 8 Jan 2006 19:51:28 +0000 (19:51 +0000)
clients/tools/common.c
clients/tools/common.h
clients/tools/ldapcompare.c
clients/tools/ldapdelete.c
clients/tools/ldapexop.c
clients/tools/ldapmodify.c
clients/tools/ldapmodrdn.c
clients/tools/ldappasswd.c
clients/tools/ldapsearch.c
clients/tools/ldapwhoami.c

index ceaf0dfe7c5b450c457a078c904f442b374d27b8..42186ff69965dda2dbc29363267047f4c70fef12 100644 (file)
@@ -29,6 +29,7 @@
 #include <ac/stdlib.h>
 #include <ac/signal.h>
 #include <ac/string.h>
+#include <ac/ctype.h>
 #include <ac/unistd.h>
 #include <ac/errno.h>
 
@@ -63,54 +64,88 @@ ldap_bind LDAP_P((  /* deprecated */
 #endif
 #endif
 
-int   authmethod = -1;
-char *binddn = NULL;
-int   contoper = 0;
-int   debug = 0;
-char *infile = NULL;
-char *ldapuri = NULL;
-char *ldaphost = NULL;
-int   ldapport = 0;
+/* input-related vars */
+
+/* misc. parameters */
+tool_type_t    tool_type;
+int            contoper = 0;
+int            debug = 0;
+char           *infile = NULL;
+int            dont = 0;
+int            referrals = 0;
+int            verbose = 0;
+int            ldif = 0;
+char           *prog = NULL;
+
+/* connection */
+char           *ldapuri = NULL;
+char           *ldaphost = NULL;
+int            ldapport = 0;
+int            use_tls = 0;
+int            protocol = -1;
+int            version = 0;
+
+/* authc/authz */
+int            authmethod = -1;
+char           *binddn = NULL;
+int            want_bindpw = 0;
+struct berval  passwd = { 0, NULL };
+char           *pw_file = NULL;
 #ifdef HAVE_CYRUS_SASL
-unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
-char   *sasl_realm = NULL;
-char   *sasl_authc_id = NULL;
-char   *sasl_authz_id = NULL;
-char   *sasl_mech = NULL;
-char   *sasl_secprops = NULL;
+unsigned       sasl_flags = LDAP_SASL_AUTOMATIC;
+char           *sasl_realm = NULL;
+char           *sasl_authc_id = NULL;
+char           *sasl_authz_id = NULL;
+char           *sasl_mech = NULL;
+char           *sasl_secprops = NULL;
 #endif
-int   use_tls = 0;
-
-int      assertctl;
-char *assertion = NULL;
-char *authzid = NULL;
-int   manageDIT = 0;
-int   manageDSAit = 0;
-int   noop = 0;
-int   ppolicy = 0;
-int   preread = 0;
-char *preread_attrs = NULL;
-int   postread = 0;
-char *postread_attrs = NULL;
-
-int   not = 0;
-int   want_bindpw = 0;
-struct berval passwd = { 0, NULL };
-char *pw_file = NULL;
-int   referrals = 0;
-int   protocol = -1;
-int   verbose = 0;
-int   version = 0;
-int   ldif = 0;
 
+/* controls */
+int            assertctl;
+char           *assertion = NULL;
+char           *authzid = NULL;
+int            manageDIT = 0;
+int            manageDSAit = 0;
+int            noop = 0;
+int            ppolicy = 0;
+int            preread = 0;
+static char    *preread_attrs = NULL;
+int            postread = 0;
+static char    *postread_attrs = NULL;
+ber_int_t      pr_morePagedResults = 1;
+struct berval  pr_cookie = { 0, NULL };
 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
-int chaining = 0;
-static int chainingResolve = -1;
-static int chainingContinuation = -1;
+int            chaining = 0;
+static int     chainingResolve = -1;
+static int     chainingContinuation = -1;
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 
-static int gotintr;
-static int abcan;
+typedef int (*print_ctrl_fn)( LDAP *ld, LDAPControl *ctrl );
+
+static int print_preread( LDAP *ld, LDAPControl *ctrl );
+static int print_postread( LDAP *ld, LDAPControl *ctrl );
+static int print_paged_results( LDAP *ld, LDAPControl *ctrl );
+#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
+static int print_ppolicy( LDAP *ld, LDAPControl *ctrl );
+#endif
+
+static struct tool_ctrls_t {
+       const char      *oid;
+       unsigned        mask;
+       print_ctrl_fn   func;
+} tool_ctrl_response[] = {
+       { LDAP_CONTROL_PRE_READ,                        TOOL_ALL,       print_preread },
+       { LDAP_CONTROL_POST_READ,                       TOOL_ALL,       print_postread },
+       { LDAP_CONTROL_PAGEDRESULTS,                    TOOL_SEARCH,    print_paged_results },
+#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
+       { LDAP_CONTROL_PASSWORDPOLICYRESPONSE,          TOOL_ALL,       print_ppolicy },
+#endif
+       { NULL,                                         0,              NULL }
+};
+
+/* "features" */
+static int     gotintr;
+static int     abcan;
 
 RETSIGTYPE
 do_sig( int sig )
@@ -118,12 +153,10 @@ do_sig( int sig )
        gotintr = abcan;
 }
 
-/* Set in main() */
-char *prog = NULL;
-
 void
-tool_init( void )
+tool_init( tool_type_t type )
 {
+       tool_type = type;
        ldap_pvt_setlocale(LC_MESSAGES, "");
        ldap_pvt_bindtextdomain(OPENLDAP_PACKAGE, LDAP_LOCALEDIR);
        ldap_pvt_textdomain(OPENLDAP_PACKAGE);
@@ -513,7 +546,7 @@ tool_args( int argc, char **argv )
                        manageDSAit++;
                        break;
                case 'n':       /* print operations, don't actually do them */
-                       not++;
+                       dont++;
                        break;
                case 'O':
 #ifdef HAVE_CYRUS_SASL
@@ -828,7 +861,7 @@ tool_args( int argc, char **argv )
 
 
 LDAP *
-tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
+tool_conn_setup( int dont, void (*private_setup)( LDAP * ) )
 {
        LDAP *ld = NULL;
 
@@ -855,7 +888,7 @@ tool_conn_setup( int not, void (*private_setup)( LDAP * ) )
                SIGNAL( SIGINT, do_sig );
        }
 
-       if ( !not ) {
+       if ( !dont ) {
                int rc;
 
                if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
@@ -1303,6 +1336,168 @@ tool_check_abandon( LDAP *ld, int msgid )
        return 0;
 }
 
+static int
+print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
+{
+       BerElement      *ber;
+       struct berval   bv;
+
+       tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
+               what->bv_val, what->bv_len );
+       ber = ber_init( &ctrl->ldctl_value );
+       if ( ber == NULL ) {
+               /* error? */
+               return 1;
+
+       } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
+               /* error? */
+               return 1;
+
+       } else {
+               tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
+
+               while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
+                       int             i;
+                       BerVarray       vals = NULL;
+
+                       if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
+                               vals == NULL )
+                       {
+                               /* error? */
+                               return 1;
+                       }
+               
+                       for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
+                               tool_write_ldif(
+                                       ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
+                                       bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
+                       }
+
+                       ber_bvarray_free( vals );
+               }
+       }
+
+       if ( ber != NULL ) {
+               ber_free( ber, 1 );
+       }
+
+       tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
+               what->bv_val, what->bv_len );
+
+       return 0;
+}
+
+static int
+print_preread( LDAP *ld, LDAPControl *ctrl )
+{
+       static struct berval what = BER_BVC( "preread" );
+
+       return print_prepostread( ld, ctrl, &what );
+}
+
+static int
+print_postread( LDAP *ld, LDAPControl *ctrl )
+{
+       static struct berval what = BER_BVC( "postread" );
+
+       return print_prepostread( ld, ctrl, &what );
+}
+
+static int
+print_paged_results( LDAP *ld, LDAPControl *ctrl )
+{
+       BerElement      *ber;
+       ber_int_t       estimate;
+       
+       ber = ber_init( &ctrl->ldctl_value );
+       /* note: pr_cookie is being malloced; it's freed
+        * the next time the control is sent, but the last
+        * time it's not; we don't care too much, because
+        * the last time an empty value is returned... */
+       if ( ber_scanf( ber, "{io}", &estimate, &pr_cookie ) == LBER_ERROR ) {
+               /* error? */
+               return 1;
+
+       } else {
+               char    buf[ BUFSIZ ], *ptr = buf;
+
+               if ( estimate > 0 ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "estimate=%d", estimate );
+               }
+
+               if ( pr_cookie.bv_len > 0 ) {
+                       struct berval   bv;
+
+                       bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
+                               pr_cookie.bv_len ) + 1;
+                       bv.bv_val = ber_memalloc( bv.bv_len + 1 );
+
+                       bv.bv_len = lutil_b64_ntop(
+                               (unsigned char *) pr_cookie.bv_val,
+                               pr_cookie.bv_len,
+                               bv.bv_val, bv.bv_len );
+
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%scookie=%s", ptr == buf ? "" : " ",
+                               bv.bv_val );
+
+                       ber_memfree( bv.bv_val );
+
+                       pr_morePagedResults = 1;
+
+               } else {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%scookie=", ptr == buf ? "" : " " );
+               }
+
+               tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
+                       "pagedresults", buf, ptr - buf );
+       }
+
+       if ( ber != NULL ) {
+               ber_free( ber, 1 );
+       }
+
+       return 0;
+}
+
+#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
+static int
+print_ppolicy( LDAP *ld, LDAPControl *ctrl )
+{
+       int expire = 0, grace = 0, rc;
+       LDAPPasswordPolicyError pperr;
+
+       rc = ldap_parse_passwordpolicy_control( ld, ctrl,
+               &expire, &grace, &pperr );
+       if ( rc == LDAP_SUCCESS ) {
+               char    buf[ BUFSIZ ], *ptr = buf;
+
+               if ( expire != -1 ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "expire=%d", expire );
+               }
+
+               if ( grace != -1 ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%sgrace=%d", ptr == buf ? "" : " ", grace );
+               }
+
+               if ( pperr != PP_noError ) {
+                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
+                               "%serror=%s", ptr == buf ? "" : " ",
+                               ldap_passwordpolicy_err2txt( pperr ) );
+               }
+
+               tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
+                       "ppolicy", buf, ptr - buf );
+       }
+
+       return rc;
+}
+#endif
+
 void tool_print_ctrls(
        LDAP            *ld,
        LDAPControl     **ctrls )
@@ -1315,6 +1510,7 @@ void tool_print_ctrls(
                struct berval b64 = BER_BVNULL;
                ber_len_t len;
                char *str;
+               int j;
 
                len = ldif ? 2 : 0;
                len += strlen( ctrls[i]->ldctl_oid );
@@ -1363,135 +1559,18 @@ void tool_print_ctrls(
                }
 
                /* known controls */
-               if ( strcmp( ctrls[i]->ldctl_oid, LDAP_CONTROL_PRE_READ ) == 0
-                       || strcmp( ctrls[i]->ldctl_oid, LDAP_CONTROL_POST_READ ) == 0 )
-               {
-                       BerElement      *ber;
-                       struct berval   bv;
-                       struct berval   what;
-
-                       if ( strcmp( ctrls[i]->ldctl_oid, LDAP_CONTROL_PRE_READ ) == 0 ) {
-                               BER_BVSTR( &what, "preread" );
-
-                       } else {
-                               BER_BVSTR( &what, "postread" );
-                       }
-
-                       tool_write_ldif( LDIF_PUT_COMMENT, "==> ",
-                               what.bv_val, what.bv_len );
-                       ber = ber_init( &ctrls[i]->ldctl_value );
-                       if ( ber == NULL ) {
-                               /* ... */
-                       } else if ( ber_scanf( ber, "{m{" /*}}*/, &bv ) == LBER_ERROR ) {
-                               /* ... */
-                       } else {
-                               tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
-
-                               while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
-                                       int             i;
-                                       BerVarray       vals = NULL;
-
-                                       if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
-                                               vals == NULL )
-                                       {
-                                               /* error? */
-                                               continue;
-                                       }
-                               
-                                       for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
-                                               tool_write_ldif(
-                                                       ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                                                       bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
-                                       }
-
-                                       ber_bvarray_free( vals );
+               for ( j = 0; tool_ctrl_response[j].oid != NULL; j++ ) {
+                       if ( strcmp( tool_ctrl_response[j].oid, ctrls[i]->ldctl_oid ) == 0 ) {
+                               if ( !tool_ctrl_response[j].mask & tool_type ) {
+                                       /* this control should not appear
+                                        * with this tool; warning? */
                                }
+                               break;
                        }
+               }
 
-                       if ( ber != NULL ) {
-                               ber_free( ber, 1 );
-                       }
-
-                       tool_write_ldif( LDIF_PUT_COMMENT, "<== ",
-                               what.bv_val, what.bv_len );
-
-               } else if ( strcmp( ctrls[i]->ldctl_oid,
-                       LDAP_CONTROL_PAGEDRESULTS ) == 0 )
-               {
-                       BerElement      *ber;
-                       struct berval   cookie;
-                       int             size;
-                       
-                       ber = ber_init( &ctrls[i]->ldctl_value );
-                       if ( ber_scanf( ber, "{im}", &size, &cookie ) == LBER_ERROR ) {
-                               /* ... */
-                       } else {
-                               char    buf[ BUFSIZ ], *ptr = buf;
-
-                               if ( size > 0 ) {
-                                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
-                                               "estimate=%d", size );
-                               }
-
-                               if ( cookie.bv_len > 0 ) {
-                                       struct berval   bv;
-
-                                       bv.bv_len = LUTIL_BASE64_ENCODE_LEN(
-                                               cookie.bv_len ) + 1;
-                                       bv.bv_val = ber_memalloc( bv.bv_len + 1 );
-
-                                       bv.bv_len = lutil_b64_ntop(
-                                               (unsigned char *) cookie.bv_val,
-                                               cookie.bv_len,
-                                               bv.bv_val, bv.bv_len );
-
-                                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
-                                               "%scookie=%s", ptr == buf ? "" : " ",
-                                               bv.bv_val );
-
-                                       ber_memfree( bv.bv_val );
-                               }
-
-                               tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                                       "pagedresults", buf, ptr - buf );
-                       }
-
-                       if ( ber != NULL ) {
-                               ber_free( ber, 1 );
-                       }
-
-#ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
-               } else if ( strcmp( ctrls[i]->ldctl_oid,
-                       LDAP_CONTROL_PASSWORDPOLICYRESPONSE ) == 0 )
-               {
-                       int expire = 0, grace = 0, rc;
-                       LDAPPasswordPolicyError pperr;
-
-                       rc = ldap_parse_passwordpolicy_control( ld, ctrls[ i ],
-                               &expire, &grace, &pperr );
-                       if ( rc == LDAP_SUCCESS ) {
-                               char    buf[ BUFSIZ ], *ptr = buf;
-
-                               if ( expire != -1 ) {
-                                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
-                                               "expire=%d", expire );
-                               }
-
-                               if ( grace != -1 ) {
-                                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
-                                               "%sgrace=%d", ptr == buf ? "" : " ", grace );
-                               }
-
-                               if ( pperr != PP_noError ) {
-                                       ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ),
-                                               "%serror=%s", ptr == buf ? "" : " ",
-                                               ldap_passwordpolicy_err2txt( pperr ) );
-                               }
-
-                               tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                                       "ppolicy", buf, ptr - buf );
-                       }
-#endif /* LDAP_CONTROL_PASSWORDPOLICYREQUEST */
+               if ( tool_ctrl_response[j].oid != NULL && tool_ctrl_response[j].func ) {
+                       (void)tool_ctrl_response[j].func( ld, ctrls[i] );
                }
        }
 }
index b78c28cd2149b9992e8b489af619ead0ae8c3693..9f917843f55f19ce5b7ffc96c4189b1371fdbc8f 100644 (file)
 
 LDAP_BEGIN_DECL
 
-/* Defined and set in common.c */
-extern int   authmethod;
-extern char *binddn;
-extern int   contoper;
-extern int   debug;
-extern char *infile;
-extern char *ldapuri;
-extern char *ldaphost;
-extern int   ldapport;
+typedef enum tool_type_t {
+       TOOL_SEARCH     = 0x01U,
+       TOOL_COMPARE    = 0x02U,
+       TOOL_ADD        = 0x04U,
+       TOOL_DELETE     = 0x08U,
+       TOOL_MODIFY     = 0x10U,
+       TOOL_MODRDN     = 0x20U,
+
+       TOOL_EXOP       = 0x40U,
+
+       TOOL_WHOAMI     = TOOL_EXOP | 0x100U,
+       TOOL_PASSWD     = TOOL_EXOP | 0x200U,
+
+       TOOL_WRITE      = (TOOL_ADD|TOOL_DELETE|TOOL_MODIFY|TOOL_MODRDN),
+       TOOL_READ       = (TOOL_SEARCH|TOOL_COMPARE),
+
+       TOOL_ALL        = 0xFFU
+} tool_type_t;
+
+
+/* input-related vars */
+
+/* misc. parameters */
+extern tool_type_t     tool_type;
+extern int             contoper;
+extern int             debug;
+extern char            *infile;
+extern int             dont;
+extern int             referrals;
+extern int             verbose;
+extern int             ldif;
+extern char            *prog;
+
+/* connection */
+extern char            *ldapuri;
+extern char            *ldaphost;
+extern int             ldapport;
+extern int             use_tls;
+extern int             protocol;
+extern int             version;
+
+/* authc/authz */
+extern int             authmethod;
+extern char            *binddn;
+extern int             want_bindpw;
+extern struct berval   passwd;
+extern char            *pw_file;
 #ifdef HAVE_CYRUS_SASL
-extern unsigned sasl_flags;
-extern char    *sasl_realm;
-extern char    *sasl_authc_id;
-extern char    *sasl_authz_id;
-extern char    *sasl_mech;
-extern char    *sasl_secprops;
+extern unsigned                sasl_flags;
+extern char            *sasl_realm;
+extern char            *sasl_authc_id;
+extern char            *sasl_authz_id;
+extern char            *sasl_mech;
+extern char            *sasl_secprops;
 #endif
-extern int   use_tls;
-
-extern char *assertion;
-extern char *authzid;
-extern int   manageDIT;
-extern int   manageDSAit;
-extern int   noop;
-extern int   ppolicy;
-extern int     preread, postread;
+
+/* controls */
+extern char            *assertion;
+extern char            *authzid;
+extern int             manageDIT;
+extern int             manageDSAit;
+extern int             noop;
+extern int             ppolicy;
+extern int             preread, postread;
+extern ber_int_t       pr_morePagedResults;
+extern struct berval   pr_cookie;
 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
-extern int     chaining;
+extern int             chaining;
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 
-extern int   not;
-extern int   want_bindpw;
-extern struct berval passwd;
-extern char *pw_file;
-extern int   referrals;
-extern int   protocol;
-extern int   verbose;
-extern int   version;
-
-extern int   ldif;
-
 /* Defined in common.c, set in main() */
-extern char *prog;
-extern const char __Version[];
+extern const char      __Version[];
 
 /* Defined in main program */
-extern const char options[];
+extern const char      options[];
+
 void usage LDAP_P(( void )) LDAP_GCCATTR((noreturn));
 int handle_private_option LDAP_P(( int i ));
 
 /* Defined in common.c */
-void tool_init LDAP_P(( void ));
+void tool_init LDAP_P(( tool_type_t type ));
 void tool_common_usage LDAP_P(( void ));
 void tool_args LDAP_P(( int, char ** ));
 LDAP *tool_conn_setup LDAP_P(( int dont, void (*private_setup)( LDAP * ) ));
index 1bdd4c0ff5750e940bccd4d84f5dd45776bfa236..ea7570b0b014ae3f60ff5037186d419dc68c26e5 100644 (file)
@@ -187,7 +187,7 @@ main( int argc, char **argv )
        LDAP    *ld = NULL;
        struct berval bvalue = { 0, NULL };
 
-       tool_init();
+       tool_init( TOOL_COMPARE );
        prog = lutil_progname( "ldapcompare", argc, argv );
 
        tool_args( argc, argv );
@@ -291,7 +291,7 @@ static int docompare(
        char            *text;
        char            **refs;
 
-       if ( not ) {
+       if ( dont ) {
                return LDAP_SUCCESS;
        }
 
index 7485964e7083caa1ac3b7ca7198bee8ebffbcb2b..0b91b0847ea106f3751cd40cd34a7ed7f1d6237b 100644 (file)
@@ -140,7 +140,7 @@ main( int argc, char **argv )
 
     fp = NULL;
 
-       tool_init();
+       tool_init( TOOL_DELETE );
     prog = lutil_progname( "ldapdelete", argc, argv );
 
        tool_args( argc, argv );
@@ -215,10 +215,10 @@ static int dodelete(
 
        if ( verbose ) {
                printf( _("%sdeleting entry \"%s\"\n"),
-                       (not ? "!" : ""), dn );
+                       (dont ? "!" : ""), dn );
        }
 
-       if ( not ) {
+       if ( dont ) {
                return LDAP_SUCCESS;
        }
 
index c94256482d280e093f38c089906997f73c66eadd..1e08881afad9310f28aa3abd1f94a866e8b8f043 100644 (file)
@@ -74,7 +74,7 @@ main( int argc, char *argv[] )
        int             id, code;
        LDAPMessage     *res;
 
-       tool_init();
+       tool_init( TOOL_EXOP );
        prog = lutil_progname( "ldapexop", argc, argv );
 
        /* LDAPv3 only */
index 29cfd1027cd00167d4821b591857f58668595020..8752ce2fea01bc5ea163c8a0e3e95e3ca8ec6aa4 100644 (file)
@@ -251,12 +251,13 @@ main( int argc, char **argv )
        int             rc, retval;
        int count, len;
 
-       tool_init();
        prog = lutil_progname( "ldapmodify", argc, argv );
 
        /* strncmp instead of strcmp since NT binaries carry .exe extension */
        ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
 
+       tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
+
        tool_args( argc, argv );
 
        if ( argc != optind ) usage();
@@ -281,9 +282,9 @@ main( int argc, char **argv )
 
        if ( debug ) ldif_debug = debug;
 
-       ld = tool_conn_setup( not, 0 );
+       ld = tool_conn_setup( dont, 0 );
 
-       if ( !not ) {
+       if ( !dont ) {
                if ( pw_file || want_bindpw ) {
                        if ( pw_file ) {
                                rc = lutil_get_filed_password( pw_file, &passwd );
@@ -405,7 +406,7 @@ main( int argc, char **argv )
        }
 #endif
 
-       if ( !not ) {
+       if ( !dont ) {
                tool_unbind( ld );
        }
 
@@ -1024,12 +1025,12 @@ domodify(
        }
 
        if ( newentry ) {
-               printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
+               printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
        } else {
-               printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
+               printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
        }
 
-       if ( !not ) {
+       if ( !dont ) {
                int     msgid;
                if ( newentry ) {
                        rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
@@ -1067,8 +1068,8 @@ dodelete(
        int     rc;
        int msgid;
 
-       printf( _("%sdeleting entry \"%s\"\n"), not ? "!" : "", dn );
-       if ( !not ) {
+       printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
+       if ( !dont ) {
                rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
                if ( rc != LDAP_SUCCESS ) {
                        fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
@@ -1101,12 +1102,12 @@ dorename(
        int     rc;
        int msgid;
 
-       printf( _("%smodifying rdn of entry \"%s\"\n"), not ? "!" : "", dn );
+       printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
        if ( verbose ) {
                printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
                        newrdn, deleteoldrdn ? _("do not ") : "" );
        }
-       if ( !not ) {
+       if ( !dont ) {
                rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
                        pctrls, NULL, &msgid );
                if ( rc != LDAP_SUCCESS ) {
index b2627c30a19543cef9e44961cbd6eda2b0268df1..f37eae86af05d7f01d09351086dad9bb70d6af24 100644 (file)
@@ -154,7 +154,7 @@ main(int argc, char **argv)
     LDAP               *ld;
        int             rc, retval, havedn;
 
-    tool_init();
+    tool_init( TOOL_MODRDN );
     prog = lutil_progname( "ldapmodrdn", argc, argv );
 
        tool_args( argc, argv );
@@ -253,7 +253,7 @@ static int domodrdn(
                }
        }
 
-       if( not ) return LDAP_SUCCESS;
+       if( dont ) return LDAP_SUCCESS;
 
        rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
                NULL, NULL, &id );
index a8fecffd4d8082794fcf1d3f6ea3547d158d5692..ca656b45a36467d2126bda3907094bc33cec8e50 100644 (file)
@@ -178,7 +178,7 @@ main( int argc, char *argv[] )
        char    *retoid = NULL;
        struct berval *retdata = NULL;
 
-    tool_init();
+    tool_init( TOOL_PASSWD );
        prog = lutil_progname( "ldappasswd", argc, argv );
 
        /* LDAPv3 only */
@@ -305,7 +305,7 @@ main( int argc, char *argv[] )
                }
        }
 
-       if ( not ) {
+       if ( dont ) {
                rc = LDAP_SUCCESS;
                goto done;
        }
index b959d316686a661892244708990ee407fb553b8b..a229b56391a6311c115b230b48b94ea57584f8eb 100644 (file)
@@ -203,12 +203,11 @@ static int ldapsync = 0;
 static struct berval sync_cookie = { 0, NULL };
 static int sync_slimit = -1;
 
+/* cookie and morePagedResults moved to common.c */
 static int pagedResults = 0;
 static int pagePrompt = 1;
 static ber_int_t pageSize = 0;
 static ber_int_t entriesLeft = 0;
-static ber_int_t morePagedResults = 1;
-static struct berval page_cookie = { 0, NULL };
 static int npagedresponses;
 static int npagedentries;
 static int npagedreferences;
@@ -237,11 +236,6 @@ ctrl_add( void )
        return 0;
 }
 
-static int parse_page_control(
-       LDAP *ld,
-       LDAPMessage *result,
-       struct berval *cookie );
-
 static void
 urlize(char *url)
 {
@@ -638,7 +632,7 @@ main( int argc, char **argv )
        BerElement      *syncber = NULL;
        struct berval   *syncbvalp = NULL;
 
-       tool_init();
+       tool_init( TOOL_SEARCH );
 
        npagedresponses = npagedentries = npagedreferences =
                npagedextended = npagedpartial = 0;
@@ -861,13 +855,14 @@ getNextPage:
                                return EXIT_FAILURE;
                        }
 
-                       ber_printf( prber, "{iO}", pageSize, &page_cookie );
+                       ber_printf( prber, "{iO}", pageSize, &pr_cookie );
                        if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
                                return EXIT_FAILURE;
                        }
-                       if ( page_cookie.bv_val != NULL ) {
-                               ber_memfree( page_cookie.bv_val );
-                               page_cookie.bv_val = NULL;
+                       if ( pr_cookie.bv_val != NULL ) {
+                               ber_memfree( pr_cookie.bv_val );
+                               pr_cookie.bv_val = NULL;
+                               pr_cookie.bv_len = 0;
                        }
                        
                        c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
@@ -976,7 +971,7 @@ getNextPage:
                }
        }
 
-       if (( rc == LDAP_SUCCESS ) && pageSize && morePagedResults ) {
+       if (( rc == LDAP_SUCCESS ) && pageSize && pr_morePagedResults ) {
                char    buf[6];
                int     i, moreEntries, tmpSize;
 
@@ -1078,7 +1073,7 @@ static int dosearch(
                filter = value;
        }
 
-       if ( not ) {
+       if ( dont ) {
                return LDAP_SUCCESS;
        }
 
@@ -1150,17 +1145,10 @@ static int dosearch(
                                break;
 
                        case LDAP_RES_SEARCH_RESULT:
+                               /* pagedResults stuff is dealt with
+                                * in tool_print_ctrls(), called by
+                                * print_results(). */
                                rc = print_result( ld, msg, 1 );
-                               if ( pageSize != 0 ) {
-                                       if ( rc == LDAP_SUCCESS ) {
-                                               rc = parse_page_control( ld, msg, &page_cookie );
-                                       } else {
-                                               morePagedResults = 0;
-                                       }
-                               } else {
-                                       morePagedResults = 0;
-                               }
-
                                if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
                                        break;
                                }
@@ -1217,7 +1205,7 @@ done:
                npagedextended += nextended;
                npagedpartial += npartial;
                npagedreferences += nreferences;
-               if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
+               if ( ( pr_morePagedResults == 0 ) && ( ldif < 2 ) ) {
                        printf( _("\n# numResponses: %d\n"), npagedresponses );
                        if( npagedentries ) {
                                printf( _("# numEntries: %d\n"), npagedentries );
@@ -1544,6 +1532,8 @@ static int print_result(
                ber_memvfree( (void **) refs );
        }
 
+       pr_morePagedResults = 0;
+
        if( ctrls ) {
                tool_print_ctrls( ld, ctrls );
                ldap_controls_free( ctrls );
@@ -1552,75 +1542,3 @@ static int print_result(
        return err;
 }
 
-static int 
-parse_page_control(
-       LDAP *ld,
-       LDAPMessage *result,
-       struct berval *cookie )
-{
-       int rc;
-       int err;
-       LDAPControl **ctrl = NULL;
-       LDAPControl *ctrlp = NULL;
-       BerElement *ber;
-       ber_tag_t tag;
-
-       rc = ldap_parse_result( ld, result,
-               &err, NULL, NULL, NULL, &ctrl, 0 );
-
-       if ( rc != LDAP_SUCCESS ) {
-               tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
-               exit( EXIT_FAILURE );
-       }
-
-       if ( err != LDAP_SUCCESS ) {
-               fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
-       }
-
-       if ( ctrl ) {
-               /* There might be others, e.g. ppolicy... */
-               ctrlp = ldap_find_control( LDAP_CONTROL_PAGEDRESULTS, ctrl );
-       }
-
-       if ( ctrlp ) {
-               /* Parse the control value
-                * searchResult ::= SEQUENCE {
-                *              size    INTEGER (0..maxInt),
-                *                              -- result set size estimate from server - unused
-                *              cookie  OCTET STRING
-                * }
-                */
-               ctrlp = *ctrl;
-               ber = ber_init( &ctrlp->ldctl_value );
-               if ( ber == NULL ) {
-                       fprintf( stderr, _("Internal error.\n") );
-                       return EXIT_FAILURE;
-               }
-
-               tag = ber_scanf( ber, "{io}", &entriesLeft, cookie );
-               (void) ber_free( ber, 1 );
-
-               if( tag == LBER_ERROR ) {
-                       fprintf( stderr,
-                               _("Paged results response control could not be decoded.\n") );
-                       return EXIT_FAILURE;
-               }
-
-               if( entriesLeft < 0 ) {
-                       fprintf( stderr,
-                               _("Invalid entries estimate in paged results response.\n") );
-                       return EXIT_FAILURE;
-               }
-
-               if ( cookie->bv_len == 0 ) {
-                       morePagedResults = 0;
-               }
-
-               ldap_controls_free( ctrl );
-
-       } else {
-               morePagedResults = 0;
-       }
-
-       return err;
-}
index d46297a23734723d7e7860bcd4c56f9be382c070..a436c738e9d77286e56d983a625512d4c5f9040d 100644 (file)
@@ -119,7 +119,7 @@ main( int argc, char *argv[] )
        int             id, code;
        LDAPMessage     *res;
 
-       tool_init();
+       tool_init( TOOL_WHOAMI );
        prog = lutil_progname( "ldapwhoami", argc, argv );
 
        /* LDAPv3 only */
@@ -149,7 +149,7 @@ main( int argc, char *argv[] )
 
        tool_bind( ld );
 
-       if ( not ) {
+       if ( dont ) {
                rc = LDAP_SUCCESS;
                goto skip;
        }