1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * 3*7c478bd9Sstevel@tonic-gate * Portions Copyright %G% Sun Microsystems, Inc. All Rights Reserved 4*7c478bd9Sstevel@tonic-gate * 5*7c478bd9Sstevel@tonic-gate */ 6*7c478bd9Sstevel@tonic-gate 7*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 8*7c478bd9Sstevel@tonic-gate 9*7c478bd9Sstevel@tonic-gate #include <stdio.h> 10*7c478bd9Sstevel@tonic-gate #include <ctype.h> 11*7c478bd9Sstevel@tonic-gate #include <string.h> 12*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 13*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 14*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 15*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 16*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 17*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 18*7c478bd9Sstevel@tonic-gate #include <unistd.h> 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #include "lber.h" 21*7c478bd9Sstevel@tonic-gate #include "ldap.h" 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate #define MOD_USE_BVALS 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate #ifdef NEEDPROTOS 26*7c478bd9Sstevel@tonic-gate static void handle_result( LDAP *ld, LDAPMessage *lm ); 27*7c478bd9Sstevel@tonic-gate static void print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s ); 28*7c478bd9Sstevel@tonic-gate static void print_search_entry( LDAP *ld, LDAPMessage *res ); 29*7c478bd9Sstevel@tonic-gate static void free_list( char **list ); 30*7c478bd9Sstevel@tonic-gate #else 31*7c478bd9Sstevel@tonic-gate static void handle_result(); 32*7c478bd9Sstevel@tonic-gate static void print_ldap_result(); 33*7c478bd9Sstevel@tonic-gate static void print_search_entry(); 34*7c478bd9Sstevel@tonic-gate static void free_list(); 35*7c478bd9Sstevel@tonic-gate #endif /* NEEDPROTOS */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #define NOCACHEERRMSG "don't compile with -DNO_CACHE if you desire local caching" 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate char *dnsuffix; 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate static char * 42*7c478bd9Sstevel@tonic-gate getline( char *line, int len, FILE *fp, char *prompt ) 43*7c478bd9Sstevel@tonic-gate { 44*7c478bd9Sstevel@tonic-gate printf(prompt); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate if ( fgets( line, len, fp ) == NULL ) 47*7c478bd9Sstevel@tonic-gate return( NULL ); 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate line[ strlen( line ) - 1 ] = '\0'; 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate return( line ); 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate static char ** 55*7c478bd9Sstevel@tonic-gate get_list( char *prompt ) 56*7c478bd9Sstevel@tonic-gate { 57*7c478bd9Sstevel@tonic-gate static char buf[256]; 58*7c478bd9Sstevel@tonic-gate int num; 59*7c478bd9Sstevel@tonic-gate char **result; 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate num = 0; 62*7c478bd9Sstevel@tonic-gate result = (char **) 0; 63*7c478bd9Sstevel@tonic-gate while ( 1 ) { 64*7c478bd9Sstevel@tonic-gate getline( buf, sizeof(buf), stdin, prompt ); 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate if ( *buf == '\0' ) 67*7c478bd9Sstevel@tonic-gate break; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate if ( result == (char **) 0 ) 70*7c478bd9Sstevel@tonic-gate result = (char **) malloc( sizeof(char *) ); 71*7c478bd9Sstevel@tonic-gate else 72*7c478bd9Sstevel@tonic-gate result = (char **) realloc( result, 73*7c478bd9Sstevel@tonic-gate sizeof(char *) * (num + 1) ); 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate result[num++] = (char *) strdup( buf ); 76*7c478bd9Sstevel@tonic-gate } 77*7c478bd9Sstevel@tonic-gate if ( result == (char **) 0 ) 78*7c478bd9Sstevel@tonic-gate return( NULL ); 79*7c478bd9Sstevel@tonic-gate result = (char **) realloc( result, sizeof(char *) * (num + 1) ); 80*7c478bd9Sstevel@tonic-gate result[num] = NULL; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate return( result ); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate static void 87*7c478bd9Sstevel@tonic-gate free_list( char **list ) 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate int i; 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate if ( list != NULL ) { 92*7c478bd9Sstevel@tonic-gate for ( i = 0; list[ i ] != NULL; ++i ) { 93*7c478bd9Sstevel@tonic-gate free( list[ i ] ); 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate free( (char *)list ); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS 101*7c478bd9Sstevel@tonic-gate static int 102*7c478bd9Sstevel@tonic-gate file_read( char *path, struct berval *bv ) 103*7c478bd9Sstevel@tonic-gate { 104*7c478bd9Sstevel@tonic-gate FILE *fp; 105*7c478bd9Sstevel@tonic-gate long rlen; 106*7c478bd9Sstevel@tonic-gate int eof; 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate if (( fp = fopen( path, "r" )) == NULL ) { 109*7c478bd9Sstevel@tonic-gate perror( path ); 110*7c478bd9Sstevel@tonic-gate return( -1 ); 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate if ( fseek( fp, 0L, SEEK_END ) != 0 ) { 114*7c478bd9Sstevel@tonic-gate perror( path ); 115*7c478bd9Sstevel@tonic-gate fclose( fp ); 116*7c478bd9Sstevel@tonic-gate return( -1 ); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate bv->bv_len = ftell( fp ); 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) { 122*7c478bd9Sstevel@tonic-gate perror( "malloc" ); 123*7c478bd9Sstevel@tonic-gate fclose( fp ); 124*7c478bd9Sstevel@tonic-gate return( -1 ); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate if ( fseek( fp, 0L, SEEK_SET ) != 0 ) { 128*7c478bd9Sstevel@tonic-gate perror( path ); 129*7c478bd9Sstevel@tonic-gate fclose( fp ); 130*7c478bd9Sstevel@tonic-gate return( -1 ); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate rlen = fread( bv->bv_val, 1, bv->bv_len, fp ); 134*7c478bd9Sstevel@tonic-gate eof = feof( fp ); 135*7c478bd9Sstevel@tonic-gate fclose( fp ); 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate if ( rlen != bv->bv_len ) { 138*7c478bd9Sstevel@tonic-gate perror( path ); 139*7c478bd9Sstevel@tonic-gate free( bv->bv_val ); 140*7c478bd9Sstevel@tonic-gate return( -1 ); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate return( bv->bv_len ); 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */ 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate static LDAPMod ** 149*7c478bd9Sstevel@tonic-gate get_modlist( char *prompt1, char *prompt2, char *prompt3 ) 150*7c478bd9Sstevel@tonic-gate { 151*7c478bd9Sstevel@tonic-gate static char buf[256]; 152*7c478bd9Sstevel@tonic-gate int num; 153*7c478bd9Sstevel@tonic-gate LDAPMod tmp; 154*7c478bd9Sstevel@tonic-gate LDAPMod **result; 155*7c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS 156*7c478bd9Sstevel@tonic-gate struct berval **bvals; 157*7c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */ 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate num = 0; 160*7c478bd9Sstevel@tonic-gate result = NULL; 161*7c478bd9Sstevel@tonic-gate while ( 1 ) { 162*7c478bd9Sstevel@tonic-gate if ( prompt1 ) { 163*7c478bd9Sstevel@tonic-gate getline( buf, sizeof(buf), stdin, prompt1 ); 164*7c478bd9Sstevel@tonic-gate tmp.mod_op = atoi( buf ); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate if ( tmp.mod_op == -1 || buf[0] == '\0' ) 167*7c478bd9Sstevel@tonic-gate break; 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate getline( buf, sizeof(buf), stdin, prompt2 ); 171*7c478bd9Sstevel@tonic-gate if ( buf[0] == '\0' ) 172*7c478bd9Sstevel@tonic-gate break; 173*7c478bd9Sstevel@tonic-gate tmp.mod_type = strdup( buf ); 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate tmp.mod_values = get_list( prompt3 ); 176*7c478bd9Sstevel@tonic-gate #ifdef MOD_USE_BVALS 177*7c478bd9Sstevel@tonic-gate if ( tmp.mod_values != NULL ) { 178*7c478bd9Sstevel@tonic-gate int i; 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate for ( i = 0; tmp.mod_values[i] != NULL; ++i ) 181*7c478bd9Sstevel@tonic-gate ; 182*7c478bd9Sstevel@tonic-gate bvals = (struct berval **)calloc( i + 1, 183*7c478bd9Sstevel@tonic-gate sizeof( struct berval *)); 184*7c478bd9Sstevel@tonic-gate for ( i = 0; tmp.mod_values[i] != NULL; ++i ) { 185*7c478bd9Sstevel@tonic-gate bvals[i] = (struct berval *)malloc( 186*7c478bd9Sstevel@tonic-gate sizeof( struct berval )); 187*7c478bd9Sstevel@tonic-gate if ( strncmp( tmp.mod_values[i], "{FILE}", 188*7c478bd9Sstevel@tonic-gate 6 ) == 0 ) { 189*7c478bd9Sstevel@tonic-gate if ( file_read( tmp.mod_values[i] + 6, 190*7c478bd9Sstevel@tonic-gate bvals[i] ) < 0 ) { 191*7c478bd9Sstevel@tonic-gate return( NULL ); 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate } else { 194*7c478bd9Sstevel@tonic-gate bvals[i]->bv_val = tmp.mod_values[i]; 195*7c478bd9Sstevel@tonic-gate bvals[i]->bv_len = 196*7c478bd9Sstevel@tonic-gate strlen( tmp.mod_values[i] ); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate } 199*7c478bd9Sstevel@tonic-gate tmp.mod_bvalues = bvals; 200*7c478bd9Sstevel@tonic-gate tmp.mod_op |= LDAP_MOD_BVALUES; 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate #endif /* MOD_USE_BVALS */ 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate if ( result == NULL ) 205*7c478bd9Sstevel@tonic-gate result = (LDAPMod **) malloc( sizeof(LDAPMod *) ); 206*7c478bd9Sstevel@tonic-gate else 207*7c478bd9Sstevel@tonic-gate result = (LDAPMod **) realloc( result, 208*7c478bd9Sstevel@tonic-gate sizeof(LDAPMod *) * (num + 1) ); 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) ); 211*7c478bd9Sstevel@tonic-gate *(result[num]) = tmp; /* struct copy */ 212*7c478bd9Sstevel@tonic-gate num++; 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate if ( result == NULL ) 215*7c478bd9Sstevel@tonic-gate return( NULL ); 216*7c478bd9Sstevel@tonic-gate result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) ); 217*7c478bd9Sstevel@tonic-gate result[num] = NULL; 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate return( result ); 220*7c478bd9Sstevel@tonic-gate } 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate int 224*7c478bd9Sstevel@tonic-gate bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp, 225*7c478bd9Sstevel@tonic-gate int freeit ) 226*7c478bd9Sstevel@tonic-gate { 227*7c478bd9Sstevel@tonic-gate static char dn[256], passwd[256]; 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate if ( !freeit ) { 230*7c478bd9Sstevel@tonic-gate #ifdef KERBEROS 231*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, 232*7c478bd9Sstevel@tonic-gate "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " ); 233*7c478bd9Sstevel@tonic-gate if (( *authmethodp = atoi( dn )) == 3 ) { 234*7c478bd9Sstevel@tonic-gate *authmethodp = LDAP_AUTH_KRBV4; 235*7c478bd9Sstevel@tonic-gate } else { 236*7c478bd9Sstevel@tonic-gate *authmethodp |= 0x80; 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate #else /* KERBEROS */ 239*7c478bd9Sstevel@tonic-gate *authmethodp = LDAP_AUTH_SIMPLE; 240*7c478bd9Sstevel@tonic-gate #endif /* KERBEROS */ 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "re-bind dn? " ); 243*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 244*7c478bd9Sstevel@tonic-gate *dnp = dn; 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) { 247*7c478bd9Sstevel@tonic-gate getline( passwd, sizeof(passwd), stdin, 248*7c478bd9Sstevel@tonic-gate "re-bind password? " ); 249*7c478bd9Sstevel@tonic-gate } else { 250*7c478bd9Sstevel@tonic-gate passwd[0] = '\0'; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate *passwdp = passwd; 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate return( LDAP_SUCCESS ); 256*7c478bd9Sstevel@tonic-gate } 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate int 260*7c478bd9Sstevel@tonic-gate main(int argc, char **argv ) 261*7c478bd9Sstevel@tonic-gate { 262*7c478bd9Sstevel@tonic-gate LDAP *ld; 263*7c478bd9Sstevel@tonic-gate int i, c, port, cldapflg, errflg, method, id, 264*7c478bd9Sstevel@tonic-gate msgtype, delrdn, theInt, sizelimit, err; 265*7c478bd9Sstevel@tonic-gate char line[256], command1, command2, command3; 266*7c478bd9Sstevel@tonic-gate char passwd[64], dn[256], rdn[64], attr[64], value[256]; 267*7c478bd9Sstevel@tonic-gate char filter[256], *host, **types; 268*7c478bd9Sstevel@tonic-gate char *mechanism; 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate char **exdn; 271*7c478bd9Sstevel@tonic-gate char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n"; 272*7c478bd9Sstevel@tonic-gate int bound, all, scope, attrsonly; 273*7c478bd9Sstevel@tonic-gate LDAPMessage *res; 274*7c478bd9Sstevel@tonic-gate LDAPMod **mods, **attrs; 275*7c478bd9Sstevel@tonic-gate struct timeval timeout, timelimit; 276*7c478bd9Sstevel@tonic-gate char *copyfname = NULL; 277*7c478bd9Sstevel@tonic-gate int copyoptions = 0, resultusetimelimit = 0; 278*7c478bd9Sstevel@tonic-gate LDAPURLDesc *ludp; 279*7c478bd9Sstevel@tonic-gate struct berval bv, cred, *srvcrds = NULL; 280*7c478bd9Sstevel@tonic-gate extern char *optarg; 281*7c478bd9Sstevel@tonic-gate extern int optind; 282*7c478bd9Sstevel@tonic-gate LDAPControl *ctrls[2]; 283*7c478bd9Sstevel@tonic-gate LDAPControl aCtrl; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate #ifdef MACOS 287*7c478bd9Sstevel@tonic-gate if (( argv = get_list( "cmd line arg?" )) == NULL ) { 288*7c478bd9Sstevel@tonic-gate exit( 1 ); 289*7c478bd9Sstevel@tonic-gate } 290*7c478bd9Sstevel@tonic-gate for ( argc = 0; argv[ argc ] != NULL; ++argc ) { 291*7c478bd9Sstevel@tonic-gate ; 292*7c478bd9Sstevel@tonic-gate } 293*7c478bd9Sstevel@tonic-gate #endif /* MACOS */ 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate host = NULL; 296*7c478bd9Sstevel@tonic-gate port = LDAP_PORT; 297*7c478bd9Sstevel@tonic-gate dnsuffix = ""; 298*7c478bd9Sstevel@tonic-gate cldapflg = errflg = 0; 299*7c478bd9Sstevel@tonic-gate ctrls[0] = &aCtrl; 300*7c478bd9Sstevel@tonic-gate ctrls[1] = NULL; 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) { 303*7c478bd9Sstevel@tonic-gate switch( c ) { 304*7c478bd9Sstevel@tonic-gate case 'u': 305*7c478bd9Sstevel@tonic-gate #ifdef CLDAP 306*7c478bd9Sstevel@tonic-gate cldapflg++; 307*7c478bd9Sstevel@tonic-gate #else /* CLDAP */ 308*7c478bd9Sstevel@tonic-gate printf( "Compile with -DCLDAP for UDP support\n" ); 309*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 310*7c478bd9Sstevel@tonic-gate break; 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate case 'd': 313*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 314*7c478bd9Sstevel@tonic-gate ldap_debug = atoi( optarg ); 315*7c478bd9Sstevel@tonic-gate if ( ldap_debug & LDAP_DEBUG_PACKETS ) { 316*7c478bd9Sstevel@tonic-gate lber_debug = ldap_debug; 317*7c478bd9Sstevel@tonic-gate } 318*7c478bd9Sstevel@tonic-gate #else 319*7c478bd9Sstevel@tonic-gate printf( "Compile with -DLDAP_DEBUG for debugging\n" ); 320*7c478bd9Sstevel@tonic-gate #endif 321*7c478bd9Sstevel@tonic-gate break; 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate case 'h': 324*7c478bd9Sstevel@tonic-gate host = optarg; 325*7c478bd9Sstevel@tonic-gate break; 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate case 's': 328*7c478bd9Sstevel@tonic-gate dnsuffix = optarg; 329*7c478bd9Sstevel@tonic-gate break; 330*7c478bd9Sstevel@tonic-gate 331*7c478bd9Sstevel@tonic-gate case 'p': 332*7c478bd9Sstevel@tonic-gate port = atoi( optarg ); 333*7c478bd9Sstevel@tonic-gate break; 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS) 336*7c478bd9Sstevel@tonic-gate case 't': /* copy ber's to given file */ 337*7c478bd9Sstevel@tonic-gate copyfname = strdup( optarg ); 338*7c478bd9Sstevel@tonic-gate copyoptions = LBER_TO_FILE; 339*7c478bd9Sstevel@tonic-gate break; 340*7c478bd9Sstevel@tonic-gate 341*7c478bd9Sstevel@tonic-gate case 'T': /* only output ber's to given file */ 342*7c478bd9Sstevel@tonic-gate copyfname = strdup( optarg ); 343*7c478bd9Sstevel@tonic-gate copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); 344*7c478bd9Sstevel@tonic-gate break; 345*7c478bd9Sstevel@tonic-gate #endif 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate default: 348*7c478bd9Sstevel@tonic-gate ++errflg; 349*7c478bd9Sstevel@tonic-gate } 350*7c478bd9Sstevel@tonic-gate } 351*7c478bd9Sstevel@tonic-gate 352*7c478bd9Sstevel@tonic-gate if ( host == NULL && optind == argc - 1 ) { 353*7c478bd9Sstevel@tonic-gate host = argv[ optind ]; 354*7c478bd9Sstevel@tonic-gate ++optind; 355*7c478bd9Sstevel@tonic-gate } 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate if ( errflg || optind < argc - 1 ) { 358*7c478bd9Sstevel@tonic-gate fprintf( stderr, usage, argv[ 0 ] ); 359*7c478bd9Sstevel@tonic-gate exit( 1 ); 360*7c478bd9Sstevel@tonic-gate } 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init", 363*7c478bd9Sstevel@tonic-gate host == NULL ? "(null)" : host, port ); 364*7c478bd9Sstevel@tonic-gate 365*7c478bd9Sstevel@tonic-gate if ( cldapflg ) { 366*7c478bd9Sstevel@tonic-gate #ifdef CLDAP 367*7c478bd9Sstevel@tonic-gate ld = cldap_open( host, port ); 368*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 369*7c478bd9Sstevel@tonic-gate } else { 370*7c478bd9Sstevel@tonic-gate ld = ldap_init( host, port ); 371*7c478bd9Sstevel@tonic-gate } 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate if ( ld == NULL ) { 374*7c478bd9Sstevel@tonic-gate perror( "ldap_init" ); 375*7c478bd9Sstevel@tonic-gate exit(1); 376*7c478bd9Sstevel@tonic-gate } 377*7c478bd9Sstevel@tonic-gate 378*7c478bd9Sstevel@tonic-gate #if !defined(MACOS) && !defined(DOS) 379*7c478bd9Sstevel@tonic-gate if ( copyfname != NULL ) { 380*7c478bd9Sstevel@tonic-gate if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT, 381*7c478bd9Sstevel@tonic-gate 0600 )) == -1 ) { 382*7c478bd9Sstevel@tonic-gate perror( copyfname ); 383*7c478bd9Sstevel@tonic-gate exit ( 1 ); 384*7c478bd9Sstevel@tonic-gate } 385*7c478bd9Sstevel@tonic-gate ld->ld_sb.sb_options = copyoptions; 386*7c478bd9Sstevel@tonic-gate } 387*7c478bd9Sstevel@tonic-gate #endif 388*7c478bd9Sstevel@tonic-gate 389*7c478bd9Sstevel@tonic-gate bound = 0; 390*7c478bd9Sstevel@tonic-gate timeout.tv_sec = 0; 391*7c478bd9Sstevel@tonic-gate timeout.tv_usec = 0; 392*7c478bd9Sstevel@tonic-gate timelimit.tv_sec = 0; 393*7c478bd9Sstevel@tonic-gate timelimit.tv_usec = 0; 394*7c478bd9Sstevel@tonic-gate 395*7c478bd9Sstevel@tonic-gate (void) memset( line, '\0', sizeof(line) ); 396*7c478bd9Sstevel@tonic-gate while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) { 397*7c478bd9Sstevel@tonic-gate command1 = line[0]; 398*7c478bd9Sstevel@tonic-gate command2 = line[1]; 399*7c478bd9Sstevel@tonic-gate command3 = line[2]; 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate switch ( command1 ) { 402*7c478bd9Sstevel@tonic-gate case 'a': /* add or abandon */ 403*7c478bd9Sstevel@tonic-gate switch ( command2 ) { 404*7c478bd9Sstevel@tonic-gate case 'd': /* add */ 405*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 406*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 407*7c478bd9Sstevel@tonic-gate if ( (attrs = get_modlist( NULL, "attr? ", 408*7c478bd9Sstevel@tonic-gate "value? " )) == NULL ) 409*7c478bd9Sstevel@tonic-gate break; 410*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 411*7c478bd9Sstevel@tonic-gate if ((err = ldap_add_ext( ld, dn, attrs, NULL, NULL, &id )) != LDAP_SUCCESS ) 412*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err) ); 413*7c478bd9Sstevel@tonic-gate else 414*7c478bd9Sstevel@tonic-gate printf( "Add initiated with id %d\n", id ); 415*7c478bd9Sstevel@tonic-gate } 416*7c478bd9Sstevel@tonic-gate else { 417*7c478bd9Sstevel@tonic-gate if ( (id = ldap_add( ld, dn, attrs )) == -1 ) 418*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_add" ); 419*7c478bd9Sstevel@tonic-gate else 420*7c478bd9Sstevel@tonic-gate printf( "Add initiated with id %d\n", id ); 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate 423*7c478bd9Sstevel@tonic-gate break; 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate case 'b': /* abandon */ 426*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "msgid? " ); 427*7c478bd9Sstevel@tonic-gate id = atoi( line ); 428*7c478bd9Sstevel@tonic-gate if ( ldap_abandon( ld, id ) != 0 ) 429*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_abandon" ); 430*7c478bd9Sstevel@tonic-gate else 431*7c478bd9Sstevel@tonic-gate printf( "Abandon successful\n" ); 432*7c478bd9Sstevel@tonic-gate break; 433*7c478bd9Sstevel@tonic-gate default: 434*7c478bd9Sstevel@tonic-gate printf( "Possibilities: [ad]d, [ab]ort\n" ); 435*7c478bd9Sstevel@tonic-gate } 436*7c478bd9Sstevel@tonic-gate break; 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate case 'b': /* asynch bind */ 439*7c478bd9Sstevel@tonic-gate #ifdef KERBEROS 440*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 441*7c478bd9Sstevel@tonic-gate "method (0->simple, 1->krbv41, 2->krbv42)? " ); 442*7c478bd9Sstevel@tonic-gate method = atoi( line ) | 0x80; 443*7c478bd9Sstevel@tonic-gate #else /* KERBEROS */ 444*7c478bd9Sstevel@tonic-gate method = LDAP_AUTH_SIMPLE; 445*7c478bd9Sstevel@tonic-gate #endif /* KERBEROS */ 446*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 447*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 448*7c478bd9Sstevel@tonic-gate 449*7c478bd9Sstevel@tonic-gate if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) 450*7c478bd9Sstevel@tonic-gate getline( passwd, sizeof(passwd), stdin, 451*7c478bd9Sstevel@tonic-gate "password? " ); 452*7c478bd9Sstevel@tonic-gate else 453*7c478bd9Sstevel@tonic-gate passwd[0] = '\0'; 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate if ( ldap_bind( ld, dn, passwd, method ) == -1 ) { 456*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ldap_bind failed\n" ); 457*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_bind" ); 458*7c478bd9Sstevel@tonic-gate } else { 459*7c478bd9Sstevel@tonic-gate printf( "Bind initiated\n" ); 460*7c478bd9Sstevel@tonic-gate bound = 1; 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate break; 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate case 'B': /* synch bind */ 465*7c478bd9Sstevel@tonic-gate #ifdef KERBEROS 466*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 467*7c478bd9Sstevel@tonic-gate "method 0->simple 1->krbv41 2->krbv42 3->krb? " ); 468*7c478bd9Sstevel@tonic-gate method = atoi( line ); 469*7c478bd9Sstevel@tonic-gate if ( method == 3 ) 470*7c478bd9Sstevel@tonic-gate method = LDAP_AUTH_KRBV4; 471*7c478bd9Sstevel@tonic-gate else 472*7c478bd9Sstevel@tonic-gate method = method | 0x80; 473*7c478bd9Sstevel@tonic-gate #else /* KERBEROS */ 474*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 475*7c478bd9Sstevel@tonic-gate "method 0->simple, 1->SASL? "); 476*7c478bd9Sstevel@tonic-gate method = atoi (line); 477*7c478bd9Sstevel@tonic-gate if (method == 1){ 478*7c478bd9Sstevel@tonic-gate method = LDAP_AUTH_SASL; 479*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 480*7c478bd9Sstevel@tonic-gate "mechanism 0->CRAM_MD5, 1->TLS? "); 481*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 482*7c478bd9Sstevel@tonic-gate if (theInt == 0){ 483*7c478bd9Sstevel@tonic-gate mechanism = LDAP_SASL_CRAM_MD5; 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate else{ 486*7c478bd9Sstevel@tonic-gate mechanism = LDAP_SASL_X511_STRONG; 487*7c478bd9Sstevel@tonic-gate } 488*7c478bd9Sstevel@tonic-gate } else { 489*7c478bd9Sstevel@tonic-gate method = LDAP_AUTH_SIMPLE; 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate #endif /* KERBEROS */ 493*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 494*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 495*7c478bd9Sstevel@tonic-gate 496*7c478bd9Sstevel@tonic-gate if ( dn[0] != '\0' ) 497*7c478bd9Sstevel@tonic-gate getline( passwd, sizeof(passwd), stdin, 498*7c478bd9Sstevel@tonic-gate "password? " ); 499*7c478bd9Sstevel@tonic-gate else 500*7c478bd9Sstevel@tonic-gate passwd[0] = '\0'; 501*7c478bd9Sstevel@tonic-gate 502*7c478bd9Sstevel@tonic-gate if (method == LDAP_AUTH_SIMPLE) { 503*7c478bd9Sstevel@tonic-gate if ( ldap_bind_s( ld, dn, passwd, method ) != 504*7c478bd9Sstevel@tonic-gate LDAP_SUCCESS ) { 505*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ldap_bind_s failed\n" ); 506*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_bind_s" ); 507*7c478bd9Sstevel@tonic-gate } else { 508*7c478bd9Sstevel@tonic-gate printf( "Bind successful\n" ); 509*7c478bd9Sstevel@tonic-gate bound = 1; 510*7c478bd9Sstevel@tonic-gate } 511*7c478bd9Sstevel@tonic-gate } else { 512*7c478bd9Sstevel@tonic-gate if (strcmp(mechanism, LDAP_SASL_CRAM_MD5) == 0){ 513*7c478bd9Sstevel@tonic-gate cred.bv_val = passwd; 514*7c478bd9Sstevel@tonic-gate cred.bv_len = strlen(passwd); 515*7c478bd9Sstevel@tonic-gate 516*7c478bd9Sstevel@tonic-gate if ( ldap_sasl_cram_md5_bind_s(ld, dn, &cred, NULL, NULL) != LDAP_SUCCESS ){ 517*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ldap_sasl_cram_md5_bind_s failed\n" ); 518*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_sasl_cram_md5_bind_s" ); 519*7c478bd9Sstevel@tonic-gate } else { 520*7c478bd9Sstevel@tonic-gate printf ( "Bind successful\n"); 521*7c478bd9Sstevel@tonic-gate bound = 1; 522*7c478bd9Sstevel@tonic-gate } 523*7c478bd9Sstevel@tonic-gate } else { 524*7c478bd9Sstevel@tonic-gate if (ldap_sasl_bind_s(ld, dn, mechanism, &cred, NULL, NULL, &srvcrds ) != LDAP_SUCCESS){ 525*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ldap_sasl_bind_s failed\n" ); 526*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_sasl_bind_s" ); 527*7c478bd9Sstevel@tonic-gate } 528*7c478bd9Sstevel@tonic-gate } 529*7c478bd9Sstevel@tonic-gate } 530*7c478bd9Sstevel@tonic-gate break; 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate case 'c': /* compare */ 533*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 534*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 535*7c478bd9Sstevel@tonic-gate getline( attr, sizeof(attr), stdin, "attr? " ); 536*7c478bd9Sstevel@tonic-gate getline( value, sizeof(value), stdin, "value? " ); 537*7c478bd9Sstevel@tonic-gate 538*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 539*7c478bd9Sstevel@tonic-gate bv.bv_val = value; 540*7c478bd9Sstevel@tonic-gate bv.bv_len = strlen(value); 541*7c478bd9Sstevel@tonic-gate if ((err = ldap_compare_ext( ld, dn, attr, &bv, NULL, NULL, &id )) != LDAP_SUCCESS ) 542*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err) ); 543*7c478bd9Sstevel@tonic-gate else 544*7c478bd9Sstevel@tonic-gate printf( "Compare initiated with id %d\n", id ); 545*7c478bd9Sstevel@tonic-gate } else { 546*7c478bd9Sstevel@tonic-gate if ( (id = ldap_compare( ld, dn, attr, value )) == -1 ) 547*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_compare" ); 548*7c478bd9Sstevel@tonic-gate else 549*7c478bd9Sstevel@tonic-gate printf( "Compare initiated with id %d\n", id ); 550*7c478bd9Sstevel@tonic-gate } 551*7c478bd9Sstevel@tonic-gate break; 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate case 'd': /* turn on debugging */ 554*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 555*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "debug level? " ); 556*7c478bd9Sstevel@tonic-gate ldap_debug = atoi( line ); 557*7c478bd9Sstevel@tonic-gate if ( ldap_debug & LDAP_DEBUG_PACKETS ) { 558*7c478bd9Sstevel@tonic-gate lber_debug = ldap_debug; 559*7c478bd9Sstevel@tonic-gate } 560*7c478bd9Sstevel@tonic-gate #else 561*7c478bd9Sstevel@tonic-gate printf( "Compile with -DLDAP_DEBUG for debugging\n" ); 562*7c478bd9Sstevel@tonic-gate #endif 563*7c478bd9Sstevel@tonic-gate break; 564*7c478bd9Sstevel@tonic-gate 565*7c478bd9Sstevel@tonic-gate case 'E': /* explode a dn */ 566*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "dn? " ); 567*7c478bd9Sstevel@tonic-gate exdn = ldap_explode_dn( line, 0 ); 568*7c478bd9Sstevel@tonic-gate for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) { 569*7c478bd9Sstevel@tonic-gate printf( "\t%s\n", exdn[i] ); 570*7c478bd9Sstevel@tonic-gate } 571*7c478bd9Sstevel@tonic-gate break; 572*7c478bd9Sstevel@tonic-gate 573*7c478bd9Sstevel@tonic-gate case 'g': /* set next msgid */ 574*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "msgid? " ); 575*7c478bd9Sstevel@tonic-gate ld->ld_msgid = atoi( line ); 576*7c478bd9Sstevel@tonic-gate break; 577*7c478bd9Sstevel@tonic-gate 578*7c478bd9Sstevel@tonic-gate case 'v': /* set version number */ 579*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "version? " ); 580*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 581*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt); 582*7c478bd9Sstevel@tonic-gate break; 583*7c478bd9Sstevel@tonic-gate 584*7c478bd9Sstevel@tonic-gate case 'm': /* modify or modifyrdn */ 585*7c478bd9Sstevel@tonic-gate if ( strncmp( line, "modify", 4 ) == 0 ) { 586*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 587*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 588*7c478bd9Sstevel@tonic-gate if ( (mods = get_modlist( 589*7c478bd9Sstevel@tonic-gate "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ", 590*7c478bd9Sstevel@tonic-gate "attribute type? ", "attribute value? " )) 591*7c478bd9Sstevel@tonic-gate == NULL ) 592*7c478bd9Sstevel@tonic-gate break; 593*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 594*7c478bd9Sstevel@tonic-gate if ((err = ldap_modify_ext( ld, dn, mods, NULL, NULL, &id )) != LDAP_SUCCESS ) 595*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err) ); 596*7c478bd9Sstevel@tonic-gate else 597*7c478bd9Sstevel@tonic-gate printf( "Modify initiated with id %d\n", id ); 598*7c478bd9Sstevel@tonic-gate } 599*7c478bd9Sstevel@tonic-gate else { 600*7c478bd9Sstevel@tonic-gate if ( (id = ldap_modify( ld, dn, mods )) == -1 ) 601*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_modify" ); 602*7c478bd9Sstevel@tonic-gate else 603*7c478bd9Sstevel@tonic-gate printf( "Modify initiated with id %d\n", id ); 604*7c478bd9Sstevel@tonic-gate } 605*7c478bd9Sstevel@tonic-gate } else if ( strncmp( line, "modrdn", 4 ) == 0 ) { 606*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 607*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 608*7c478bd9Sstevel@tonic-gate getline( rdn, sizeof(rdn), stdin, "newrdn? " ); 609*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "delete old rdn (0=>no, 1=>yes)?"); 610*7c478bd9Sstevel@tonic-gate delrdn = atoi(line); 611*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 612*7c478bd9Sstevel@tonic-gate if ((err = ldap_rename(ld, dn, rdn, NULL, delrdn, NULL,NULL, &id)) != LDAP_SUCCESS){ 613*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err)); 614*7c478bd9Sstevel@tonic-gate } 615*7c478bd9Sstevel@tonic-gate else 616*7c478bd9Sstevel@tonic-gate printf( "Modrdn initiated with id %d\n", id ); 617*7c478bd9Sstevel@tonic-gate } 618*7c478bd9Sstevel@tonic-gate else { 619*7c478bd9Sstevel@tonic-gate if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 ) 620*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_modrdn" ); 621*7c478bd9Sstevel@tonic-gate else 622*7c478bd9Sstevel@tonic-gate printf( "Modrdn initiated with id %d\n", id ); 623*7c478bd9Sstevel@tonic-gate } 624*7c478bd9Sstevel@tonic-gate } else { 625*7c478bd9Sstevel@tonic-gate printf( "Possibilities: [modi]fy, [modr]dn\n" ); 626*7c478bd9Sstevel@tonic-gate } 627*7c478bd9Sstevel@tonic-gate break; 628*7c478bd9Sstevel@tonic-gate 629*7c478bd9Sstevel@tonic-gate case 'q': /* quit */ 630*7c478bd9Sstevel@tonic-gate #ifdef CLDAP 631*7c478bd9Sstevel@tonic-gate if ( cldapflg ) 632*7c478bd9Sstevel@tonic-gate cldap_close( ld ); 633*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 634*7c478bd9Sstevel@tonic-gate if ( !cldapflg ) 635*7c478bd9Sstevel@tonic-gate ldap_unbind( ld ); 636*7c478bd9Sstevel@tonic-gate exit( 0 ); 637*7c478bd9Sstevel@tonic-gate break; 638*7c478bd9Sstevel@tonic-gate 639*7c478bd9Sstevel@tonic-gate case 'r': /* result or remove */ 640*7c478bd9Sstevel@tonic-gate switch ( command3 ) { 641*7c478bd9Sstevel@tonic-gate case 's': /* result */ 642*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 643*7c478bd9Sstevel@tonic-gate "msgid (-1=>any)? " ); 644*7c478bd9Sstevel@tonic-gate if ( line[0] == '\0' ) 645*7c478bd9Sstevel@tonic-gate id = -1; 646*7c478bd9Sstevel@tonic-gate else 647*7c478bd9Sstevel@tonic-gate id = atoi( line ); 648*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 649*7c478bd9Sstevel@tonic-gate "all (0=>any, 1=>all)? " ); 650*7c478bd9Sstevel@tonic-gate if ( line[0] == '\0' ) 651*7c478bd9Sstevel@tonic-gate all = 1; 652*7c478bd9Sstevel@tonic-gate else 653*7c478bd9Sstevel@tonic-gate all = atoi( line ); 654*7c478bd9Sstevel@tonic-gate 655*7c478bd9Sstevel@tonic-gate if (( msgtype = ldap_result( ld, id, all, 656*7c478bd9Sstevel@tonic-gate resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) { 657*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_result" ); 658*7c478bd9Sstevel@tonic-gate break; 659*7c478bd9Sstevel@tonic-gate } 660*7c478bd9Sstevel@tonic-gate printf( "\nresult: msgtype %d msgid %d\n", 661*7c478bd9Sstevel@tonic-gate msgtype, res->lm_msgid ); 662*7c478bd9Sstevel@tonic-gate handle_result( ld, res ); 663*7c478bd9Sstevel@tonic-gate if (all || msgtype == LDAP_RES_SEARCH_RESULT) 664*7c478bd9Sstevel@tonic-gate resultusetimelimit = 0; 665*7c478bd9Sstevel@tonic-gate res = NULLMSG; 666*7c478bd9Sstevel@tonic-gate break; 667*7c478bd9Sstevel@tonic-gate 668*7c478bd9Sstevel@tonic-gate case 'm': /* remove */ 669*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "dn? " ); 670*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 671*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 672*7c478bd9Sstevel@tonic-gate if ((err = ldap_delete_ext( ld, dn, NULL, NULL, &id )) != LDAP_SUCCESS ) 673*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err) ); 674*7c478bd9Sstevel@tonic-gate else 675*7c478bd9Sstevel@tonic-gate printf( "Remove initiated with id %d\n", id ); 676*7c478bd9Sstevel@tonic-gate } else { 677*7c478bd9Sstevel@tonic-gate if ( (id = ldap_delete( ld, dn )) == -1 ) 678*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_delete" ); 679*7c478bd9Sstevel@tonic-gate else 680*7c478bd9Sstevel@tonic-gate printf( "Remove initiated with id %d\n", id ); 681*7c478bd9Sstevel@tonic-gate } 682*7c478bd9Sstevel@tonic-gate break; 683*7c478bd9Sstevel@tonic-gate 684*7c478bd9Sstevel@tonic-gate default: 685*7c478bd9Sstevel@tonic-gate printf( "Possibilities: [rem]ove, [res]ult\n" ); 686*7c478bd9Sstevel@tonic-gate break; 687*7c478bd9Sstevel@tonic-gate } 688*7c478bd9Sstevel@tonic-gate break; 689*7c478bd9Sstevel@tonic-gate 690*7c478bd9Sstevel@tonic-gate case 's': /* search */ 691*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "searchbase? " ); 692*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 693*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 694*7c478bd9Sstevel@tonic-gate "scope (0=Base, 1=One Level, 2=Subtree)? " ); 695*7c478bd9Sstevel@tonic-gate scope = atoi( line ); 696*7c478bd9Sstevel@tonic-gate getline( filter, sizeof(filter), stdin, 697*7c478bd9Sstevel@tonic-gate "search filter (e.g. sn=jones)? " ); 698*7c478bd9Sstevel@tonic-gate types = get_list( "attrs to return? " ); 699*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 700*7c478bd9Sstevel@tonic-gate "attrsonly (0=attrs&values, 1=attrs only)? " ); 701*7c478bd9Sstevel@tonic-gate attrsonly = atoi( line ); 702*7c478bd9Sstevel@tonic-gate 703*7c478bd9Sstevel@tonic-gate if ( cldapflg ) { 704*7c478bd9Sstevel@tonic-gate #ifdef CLDAP 705*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 706*7c478bd9Sstevel@tonic-gate "Requestor DN (for logging)? " ); 707*7c478bd9Sstevel@tonic-gate if ( cldap_search_s( ld, dn, scope, filter, types, 708*7c478bd9Sstevel@tonic-gate attrsonly, &res, line ) != 0 ) { 709*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "cldap_search_s" ); 710*7c478bd9Sstevel@tonic-gate } else { 711*7c478bd9Sstevel@tonic-gate printf( "\nresult: msgid %d\n", 712*7c478bd9Sstevel@tonic-gate res->lm_msgid ); 713*7c478bd9Sstevel@tonic-gate handle_result( ld, res ); 714*7c478bd9Sstevel@tonic-gate res = NULLMSG; 715*7c478bd9Sstevel@tonic-gate } 716*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 717*7c478bd9Sstevel@tonic-gate } else { 718*7c478bd9Sstevel@tonic-gate theInt = 0; 719*7c478bd9Sstevel@tonic-gate if (ldap_get_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i) == LDAP_SUCCESS && i == LDAP_VERSION3){ 720*7c478bd9Sstevel@tonic-gate resultusetimelimit = 1; 721*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 722*7c478bd9Sstevel@tonic-gate "ldap_search_ext (0=>no, 1=>yes - default: yes)? " ); 723*7c478bd9Sstevel@tonic-gate if (line[0] == '\0') 724*7c478bd9Sstevel@tonic-gate theInt = 1; 725*7c478bd9Sstevel@tonic-gate else 726*7c478bd9Sstevel@tonic-gate theInt = atoi( line ); 727*7c478bd9Sstevel@tonic-gate } 728*7c478bd9Sstevel@tonic-gate if (theInt){ 729*7c478bd9Sstevel@tonic-gate getline(line, sizeof(line), stdin, "time limit?"); 730*7c478bd9Sstevel@tonic-gate timelimit.tv_sec = atoi(line); 731*7c478bd9Sstevel@tonic-gate resultusetimelimit = 1; 732*7c478bd9Sstevel@tonic-gate getline(line, sizeof(line), stdin, "size limit?"); 733*7c478bd9Sstevel@tonic-gate sizelimit = atoi(line); 734*7c478bd9Sstevel@tonic-gate if (( err = ldap_search_ext(ld, dn, scope, filter, types, attrsonly, NULL, NULL, 735*7c478bd9Sstevel@tonic-gate &timelimit, sizelimit, &id)) != LDAP_SUCCESS){ 736*7c478bd9Sstevel@tonic-gate printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err)); 737*7c478bd9Sstevel@tonic-gate } else { 738*7c478bd9Sstevel@tonic-gate printf( "Search initiated with id %d\n", id ); 739*7c478bd9Sstevel@tonic-gate } 740*7c478bd9Sstevel@tonic-gate } else { 741*7c478bd9Sstevel@tonic-gate if (( id = ldap_search( ld, dn, scope, filter, 742*7c478bd9Sstevel@tonic-gate types, attrsonly )) == -1 ) { 743*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_search" ); 744*7c478bd9Sstevel@tonic-gate } else { 745*7c478bd9Sstevel@tonic-gate printf( "Search initiated with id %d\n", id ); 746*7c478bd9Sstevel@tonic-gate } 747*7c478bd9Sstevel@tonic-gate } 748*7c478bd9Sstevel@tonic-gate } 749*7c478bd9Sstevel@tonic-gate free_list( types ); 750*7c478bd9Sstevel@tonic-gate break; 751*7c478bd9Sstevel@tonic-gate 752*7c478bd9Sstevel@tonic-gate case 't': /* set timeout value */ 753*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "timeout? " ); 754*7c478bd9Sstevel@tonic-gate timeout.tv_sec = atoi( line ); 755*7c478bd9Sstevel@tonic-gate break; 756*7c478bd9Sstevel@tonic-gate 757*7c478bd9Sstevel@tonic-gate case 'U': /* set ufn search prefix */ 758*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "ufn prefix? " ); 759*7c478bd9Sstevel@tonic-gate ldap_ufn_setprefix( ld, line ); 760*7c478bd9Sstevel@tonic-gate break; 761*7c478bd9Sstevel@tonic-gate 762*7c478bd9Sstevel@tonic-gate case 'u': /* user friendly search w/optional timeout */ 763*7c478bd9Sstevel@tonic-gate getline( dn, sizeof(dn), stdin, "ufn? " ); 764*7c478bd9Sstevel@tonic-gate strcat( dn, dnsuffix ); 765*7c478bd9Sstevel@tonic-gate types = get_list( "attrs to return? " ); 766*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 767*7c478bd9Sstevel@tonic-gate "attrsonly (0=attrs&values, 1=attrs only)? " ); 768*7c478bd9Sstevel@tonic-gate attrsonly = atoi( line ); 769*7c478bd9Sstevel@tonic-gate 770*7c478bd9Sstevel@tonic-gate if ( command2 == 't' ) { 771*7c478bd9Sstevel@tonic-gate id = ldap_ufn_search_c( ld, dn, types, 772*7c478bd9Sstevel@tonic-gate attrsonly, &res, ldap_ufn_timeout, 773*7c478bd9Sstevel@tonic-gate &timeout ); 774*7c478bd9Sstevel@tonic-gate } else { 775*7c478bd9Sstevel@tonic-gate id = ldap_ufn_search_s( ld, dn, types, 776*7c478bd9Sstevel@tonic-gate attrsonly, &res ); 777*7c478bd9Sstevel@tonic-gate } 778*7c478bd9Sstevel@tonic-gate if ( res == NULL ) 779*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_ufn_search" ); 780*7c478bd9Sstevel@tonic-gate else { 781*7c478bd9Sstevel@tonic-gate printf( "\nresult: err %d\n", id ); 782*7c478bd9Sstevel@tonic-gate handle_result( ld, res ); 783*7c478bd9Sstevel@tonic-gate res = NULLMSG; 784*7c478bd9Sstevel@tonic-gate } 785*7c478bd9Sstevel@tonic-gate free_list( types ); 786*7c478bd9Sstevel@tonic-gate break; 787*7c478bd9Sstevel@tonic-gate 788*7c478bd9Sstevel@tonic-gate case 'l': /* URL search */ 789*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 790*7c478bd9Sstevel@tonic-gate "attrsonly (0=attrs&values, 1=attrs only)? " ); 791*7c478bd9Sstevel@tonic-gate attrsonly = atoi( line ); 792*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "LDAP URL? " ); 793*7c478bd9Sstevel@tonic-gate if (( id = ldap_url_search( ld, line, attrsonly )) 794*7c478bd9Sstevel@tonic-gate == -1 ) { 795*7c478bd9Sstevel@tonic-gate ldap_perror( ld, "ldap_url_search" ); 796*7c478bd9Sstevel@tonic-gate } else { 797*7c478bd9Sstevel@tonic-gate printf( "URL search initiated with id %d\n", id ); 798*7c478bd9Sstevel@tonic-gate } 799*7c478bd9Sstevel@tonic-gate break; 800*7c478bd9Sstevel@tonic-gate 801*7c478bd9Sstevel@tonic-gate case 'p': /* parse LDAP URL */ 802*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "LDAP URL? " ); 803*7c478bd9Sstevel@tonic-gate if (( i = ldap_url_parse( line, &ludp )) != 0 ) { 804*7c478bd9Sstevel@tonic-gate fprintf( stderr, "ldap_url_parse: error %d\n", i ); 805*7c478bd9Sstevel@tonic-gate } else { 806*7c478bd9Sstevel@tonic-gate printf( "\t host: " ); 807*7c478bd9Sstevel@tonic-gate if ( ludp->lud_host == NULL ) { 808*7c478bd9Sstevel@tonic-gate printf( "DEFAULT\n" ); 809*7c478bd9Sstevel@tonic-gate } else { 810*7c478bd9Sstevel@tonic-gate printf( "<%s>\n", ludp->lud_host ); 811*7c478bd9Sstevel@tonic-gate } 812*7c478bd9Sstevel@tonic-gate printf( "\t port: " ); 813*7c478bd9Sstevel@tonic-gate if ( ludp->lud_port == 0 ) { 814*7c478bd9Sstevel@tonic-gate printf( "DEFAULT\n" ); 815*7c478bd9Sstevel@tonic-gate } else { 816*7c478bd9Sstevel@tonic-gate printf( "%d\n", ludp->lud_port ); 817*7c478bd9Sstevel@tonic-gate } 818*7c478bd9Sstevel@tonic-gate printf( "\t dn: <%s>\n", ludp->lud_dn ); 819*7c478bd9Sstevel@tonic-gate printf( "\t attrs:" ); 820*7c478bd9Sstevel@tonic-gate if ( ludp->lud_attrs == NULL ) { 821*7c478bd9Sstevel@tonic-gate printf( " ALL" ); 822*7c478bd9Sstevel@tonic-gate } else { 823*7c478bd9Sstevel@tonic-gate for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) { 824*7c478bd9Sstevel@tonic-gate printf( " <%s>", ludp->lud_attrs[ i ] ); 825*7c478bd9Sstevel@tonic-gate } 826*7c478bd9Sstevel@tonic-gate } 827*7c478bd9Sstevel@tonic-gate printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_UNKNOWN ? "DEFAULT (base)" : 828*7c478bd9Sstevel@tonic-gate ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "ONE" : 829*7c478bd9Sstevel@tonic-gate ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" : 830*7c478bd9Sstevel@tonic-gate ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" ); 831*7c478bd9Sstevel@tonic-gate printf( "\tfilter: <%s>\n", ludp->lud_filter ? ludp->lud_filter : "NONE"); 832*7c478bd9Sstevel@tonic-gate if (ludp->lud_extensions){ 833*7c478bd9Sstevel@tonic-gate printf("\textensions: \n"); 834*7c478bd9Sstevel@tonic-gate for (i = 0; ludp->lud_extensions[i] != NULL; i++) 835*7c478bd9Sstevel@tonic-gate printf("\t\t%s (%s)\n", ludp->lud_extensions[i]->lue_type, 836*7c478bd9Sstevel@tonic-gate ludp->lud_extensions[i]->lue_iscritical ? "Critical" : "Non critical"); 837*7c478bd9Sstevel@tonic-gate } 838*7c478bd9Sstevel@tonic-gate 839*7c478bd9Sstevel@tonic-gate ldap_free_urldesc( ludp ); 840*7c478bd9Sstevel@tonic-gate } 841*7c478bd9Sstevel@tonic-gate break; 842*7c478bd9Sstevel@tonic-gate 843*7c478bd9Sstevel@tonic-gate case 'n': /* set dn suffix, for convenience */ 844*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "DN suffix? " ); 845*7c478bd9Sstevel@tonic-gate strcpy( dnsuffix, line ); 846*7c478bd9Sstevel@tonic-gate break; 847*7c478bd9Sstevel@tonic-gate 848*7c478bd9Sstevel@tonic-gate case 'e': /* enable cache */ 849*7c478bd9Sstevel@tonic-gate #ifdef NO_CACHE 850*7c478bd9Sstevel@tonic-gate printf( NOCACHEERRMSG ); 851*7c478bd9Sstevel@tonic-gate #else /* NO_CACHE */ 852*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "Cache timeout (secs)? " ); 853*7c478bd9Sstevel@tonic-gate i = atoi( line ); 854*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " ); 855*7c478bd9Sstevel@tonic-gate if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) { 856*7c478bd9Sstevel@tonic-gate printf( "local cache is on\n" ); 857*7c478bd9Sstevel@tonic-gate } else { 858*7c478bd9Sstevel@tonic-gate printf( "ldap_enable_cache failed\n" ); 859*7c478bd9Sstevel@tonic-gate } 860*7c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */ 861*7c478bd9Sstevel@tonic-gate break; 862*7c478bd9Sstevel@tonic-gate 863*7c478bd9Sstevel@tonic-gate case 'x': /* uncache entry */ 864*7c478bd9Sstevel@tonic-gate #ifdef NO_CACHE 865*7c478bd9Sstevel@tonic-gate printf( NOCACHEERRMSG ); 866*7c478bd9Sstevel@tonic-gate #else /* NO_CACHE */ 867*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "DN? " ); 868*7c478bd9Sstevel@tonic-gate ldap_uncache_entry( ld, line ); 869*7c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */ 870*7c478bd9Sstevel@tonic-gate break; 871*7c478bd9Sstevel@tonic-gate 872*7c478bd9Sstevel@tonic-gate case 'X': /* uncache request */ 873*7c478bd9Sstevel@tonic-gate #ifdef NO_CACHE 874*7c478bd9Sstevel@tonic-gate printf( NOCACHEERRMSG ); 875*7c478bd9Sstevel@tonic-gate #else /* NO_CACHE */ 876*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "request msgid? " ); 877*7c478bd9Sstevel@tonic-gate ldap_uncache_request( ld, atoi( line )); 878*7c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */ 879*7c478bd9Sstevel@tonic-gate break; 880*7c478bd9Sstevel@tonic-gate 881*7c478bd9Sstevel@tonic-gate case 'o': /* set ldap options */ 882*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" ); 883*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 884*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_DEREF, &theInt ); 885*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "timelimit?" ); 886*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 887*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &theInt); 888*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "sizelimit?" ); 889*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 890*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt); 891*7c478bd9Sstevel@tonic-gate 892*7c478bd9Sstevel@tonic-gate ld->ld_options = 0; 893*7c478bd9Sstevel@tonic-gate 894*7c478bd9Sstevel@tonic-gate #ifdef STR_TRANSLATION 895*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 896*7c478bd9Sstevel@tonic-gate "Automatic translation of T.61 strings (0=no, 1=yes)?" ); 897*7c478bd9Sstevel@tonic-gate if ( atoi( line ) == 0 ) { 898*7c478bd9Sstevel@tonic-gate ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS; 899*7c478bd9Sstevel@tonic-gate } else { 900*7c478bd9Sstevel@tonic-gate ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS; 901*7c478bd9Sstevel@tonic-gate #ifdef LDAP_CHARSET_8859 902*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 903*7c478bd9Sstevel@tonic-gate "Translate to/from ISO-8859 (0=no, 1=yes?" ); 904*7c478bd9Sstevel@tonic-gate if ( atoi( line ) != 0 ) { 905*7c478bd9Sstevel@tonic-gate ldap_set_string_translators( ld, 906*7c478bd9Sstevel@tonic-gate ldap_8859_to_t61, 907*7c478bd9Sstevel@tonic-gate ldap_t61_to_8859 ); 908*7c478bd9Sstevel@tonic-gate } 909*7c478bd9Sstevel@tonic-gate #endif /* LDAP_CHARSET_8859 */ 910*7c478bd9Sstevel@tonic-gate } 911*7c478bd9Sstevel@tonic-gate #endif /* STR_TRANSLATION */ 912*7c478bd9Sstevel@tonic-gate 913*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DNS 914*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 915*7c478bd9Sstevel@tonic-gate "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" ); 916*7c478bd9Sstevel@tonic-gate if ( atoi( line ) != 0 ) { 917*7c478bd9Sstevel@tonic-gate ld->ld_options |= LDAP_OPT_DNS; 918*7c478bd9Sstevel@tonic-gate } 919*7c478bd9Sstevel@tonic-gate #endif /* LDAP_DNS */ 920*7c478bd9Sstevel@tonic-gate 921*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 922*7c478bd9Sstevel@tonic-gate "Recognize and chase referrals (0=no, 1=yes)?" ); 923*7c478bd9Sstevel@tonic-gate if ( atoi( line ) != 0 ) { 924*7c478bd9Sstevel@tonic-gate theInt = LDAP_OPT_ON; 925*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 926*7c478bd9Sstevel@tonic-gate "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" ); 927*7c478bd9Sstevel@tonic-gate if ( atoi( line ) != 0 ) { 928*7c478bd9Sstevel@tonic-gate ldap_set_option( ld, LDAP_OPT_REBIND_FN, bind_prompt ); 929*7c478bd9Sstevel@tonic-gate } 930*7c478bd9Sstevel@tonic-gate } else { 931*7c478bd9Sstevel@tonic-gate theInt = LDAP_OPT_OFF; 932*7c478bd9Sstevel@tonic-gate } 933*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt); 934*7c478bd9Sstevel@tonic-gate break; 935*7c478bd9Sstevel@tonic-gate 936*7c478bd9Sstevel@tonic-gate case 'k': /* Set some controls */ 937*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 938*7c478bd9Sstevel@tonic-gate "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?"); 939*7c478bd9Sstevel@tonic-gate theInt = atoi(line); 940*7c478bd9Sstevel@tonic-gate switch (theInt){ 941*7c478bd9Sstevel@tonic-gate case 0: 942*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL); 943*7c478bd9Sstevel@tonic-gate break; 944*7c478bd9Sstevel@tonic-gate case 1: 945*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_oid = "2.16.840.1.113730.3.4.2"; 946*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_iscritical = 1; 947*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_value = NULL; 948*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls); 949*7c478bd9Sstevel@tonic-gate break; 950*7c478bd9Sstevel@tonic-gate case 2: 951*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 952*7c478bd9Sstevel@tonic-gate "Preferred Language Control : lang ?"); 953*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_oid = "1.3.6.1.4.1.1466.20035"; 954*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_iscritical = 1; 955*7c478bd9Sstevel@tonic-gate bv.bv_val = strdup(line); 956*7c478bd9Sstevel@tonic-gate bv.bv_len = strlen(line); 957*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_value = &bv; 958*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls); 959*7c478bd9Sstevel@tonic-gate break; 960*7c478bd9Sstevel@tonic-gate default: 961*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, 962*7c478bd9Sstevel@tonic-gate "Bad Control is critical (0=false, 1=true)?"); 963*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_oid = "1.1.1.1.1.1"; 964*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_iscritical = atoi(line); 965*7c478bd9Sstevel@tonic-gate aCtrl.ldctl_value = NULL; 966*7c478bd9Sstevel@tonic-gate ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, ctrls); 967*7c478bd9Sstevel@tonic-gate break; 968*7c478bd9Sstevel@tonic-gate } 969*7c478bd9Sstevel@tonic-gate break; 970*7c478bd9Sstevel@tonic-gate 971*7c478bd9Sstevel@tonic-gate case 'O': /* set cache options */ 972*7c478bd9Sstevel@tonic-gate #ifdef NO_CACHE 973*7c478bd9Sstevel@tonic-gate printf( NOCACHEERRMSG ); 974*7c478bd9Sstevel@tonic-gate #else /* NO_CACHE */ 975*7c478bd9Sstevel@tonic-gate getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" ); 976*7c478bd9Sstevel@tonic-gate switch( atoi( line )) { 977*7c478bd9Sstevel@tonic-gate case 0: 978*7c478bd9Sstevel@tonic-gate ldap_set_cache_options( ld, 0 ); 979*7c478bd9Sstevel@tonic-gate break; 980*7c478bd9Sstevel@tonic-gate case 1: 981*7c478bd9Sstevel@tonic-gate ldap_set_cache_options( ld, 982*7c478bd9Sstevel@tonic-gate LDAP_CACHE_OPT_CACHENOERRS ); 983*7c478bd9Sstevel@tonic-gate break; 984*7c478bd9Sstevel@tonic-gate case 2: 985*7c478bd9Sstevel@tonic-gate ldap_set_cache_options( ld, 986*7c478bd9Sstevel@tonic-gate LDAP_CACHE_OPT_CACHEALLERRS ); 987*7c478bd9Sstevel@tonic-gate break; 988*7c478bd9Sstevel@tonic-gate default: 989*7c478bd9Sstevel@tonic-gate printf( "not a valid cache option\n" ); 990*7c478bd9Sstevel@tonic-gate } 991*7c478bd9Sstevel@tonic-gate #endif /* NO_CACHE */ 992*7c478bd9Sstevel@tonic-gate break; 993*7c478bd9Sstevel@tonic-gate 994*7c478bd9Sstevel@tonic-gate case '?': /* help */ 995*7c478bd9Sstevel@tonic-gate printf( "Commands: [ad]d [ab]andon [b]ind\n" ); 996*7c478bd9Sstevel@tonic-gate printf( " [B]ind async [c]ompare [l]URL search\n" ); 997*7c478bd9Sstevel@tonic-gate printf( " [modi]fy [modr]dn [rem]ove\n" ); 998*7c478bd9Sstevel@tonic-gate printf( " [res]ult [s]earch [q]uit/unbind\n\n" ); 999*7c478bd9Sstevel@tonic-gate printf( " [u]fn search [ut]fn search with timeout\n" ); 1000*7c478bd9Sstevel@tonic-gate printf( " [d]ebug [e]nable cache set ms[g]id\n" ); 1001*7c478bd9Sstevel@tonic-gate printf( " d[n]suffix [t]imeout [v]ersion\n" ); 1002*7c478bd9Sstevel@tonic-gate printf( " [U]fn prefix [x]uncache entry [X]uncache request\n" ); 1003*7c478bd9Sstevel@tonic-gate printf( " [?]help [o]ptions [O]cache options\n" ); 1004*7c478bd9Sstevel@tonic-gate printf( " [E]xplode dn [p]arse LDAP URL\n" ); 1005*7c478bd9Sstevel@tonic-gate break; 1006*7c478bd9Sstevel@tonic-gate 1007*7c478bd9Sstevel@tonic-gate default: 1008*7c478bd9Sstevel@tonic-gate printf( "Invalid command. Type ? for help.\n" ); 1009*7c478bd9Sstevel@tonic-gate break; 1010*7c478bd9Sstevel@tonic-gate } 1011*7c478bd9Sstevel@tonic-gate 1012*7c478bd9Sstevel@tonic-gate (void) memset( line, '\0', sizeof(line) ); 1013*7c478bd9Sstevel@tonic-gate } 1014*7c478bd9Sstevel@tonic-gate 1015*7c478bd9Sstevel@tonic-gate return( 0 ); 1016*7c478bd9Sstevel@tonic-gate } 1017*7c478bd9Sstevel@tonic-gate 1018*7c478bd9Sstevel@tonic-gate static void 1019*7c478bd9Sstevel@tonic-gate handle_result( LDAP *ld, LDAPMessage *lm ) 1020*7c478bd9Sstevel@tonic-gate { 1021*7c478bd9Sstevel@tonic-gate switch ( lm->lm_msgtype ) { 1022*7c478bd9Sstevel@tonic-gate case LDAP_RES_COMPARE: 1023*7c478bd9Sstevel@tonic-gate printf( "Compare result\n" ); 1024*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "compare" ); 1025*7c478bd9Sstevel@tonic-gate break; 1026*7c478bd9Sstevel@tonic-gate 1027*7c478bd9Sstevel@tonic-gate case LDAP_RES_SEARCH_RESULT: 1028*7c478bd9Sstevel@tonic-gate printf( "Search result\n" ); 1029*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "search" ); 1030*7c478bd9Sstevel@tonic-gate break; 1031*7c478bd9Sstevel@tonic-gate 1032*7c478bd9Sstevel@tonic-gate case LDAP_RES_SEARCH_REFERENCE: 1033*7c478bd9Sstevel@tonic-gate printf( "Search reference\n" ); 1034*7c478bd9Sstevel@tonic-gate print_search_entry( ld, lm ); 1035*7c478bd9Sstevel@tonic-gate break; 1036*7c478bd9Sstevel@tonic-gate 1037*7c478bd9Sstevel@tonic-gate case LDAP_RES_SEARCH_ENTRY: 1038*7c478bd9Sstevel@tonic-gate printf( "Search entry\n" ); 1039*7c478bd9Sstevel@tonic-gate print_search_entry( ld, lm ); 1040*7c478bd9Sstevel@tonic-gate break; 1041*7c478bd9Sstevel@tonic-gate 1042*7c478bd9Sstevel@tonic-gate case LDAP_RES_ADD: 1043*7c478bd9Sstevel@tonic-gate printf( "Add result\n" ); 1044*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "add" ); 1045*7c478bd9Sstevel@tonic-gate break; 1046*7c478bd9Sstevel@tonic-gate 1047*7c478bd9Sstevel@tonic-gate case LDAP_RES_DELETE: 1048*7c478bd9Sstevel@tonic-gate printf( "Delete result\n" ); 1049*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "delete" ); 1050*7c478bd9Sstevel@tonic-gate break; 1051*7c478bd9Sstevel@tonic-gate 1052*7c478bd9Sstevel@tonic-gate case LDAP_RES_MODIFY: 1053*7c478bd9Sstevel@tonic-gate printf( "Modify result\n" ); 1054*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "modify" ); 1055*7c478bd9Sstevel@tonic-gate break; 1056*7c478bd9Sstevel@tonic-gate 1057*7c478bd9Sstevel@tonic-gate case LDAP_RES_MODRDN: 1058*7c478bd9Sstevel@tonic-gate printf( "ModRDN result\n" ); 1059*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "modrdn" ); 1060*7c478bd9Sstevel@tonic-gate break; 1061*7c478bd9Sstevel@tonic-gate 1062*7c478bd9Sstevel@tonic-gate case LDAP_RES_BIND: 1063*7c478bd9Sstevel@tonic-gate printf( "Bind result\n" ); 1064*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "bind" ); 1065*7c478bd9Sstevel@tonic-gate break; 1066*7c478bd9Sstevel@tonic-gate 1067*7c478bd9Sstevel@tonic-gate default: 1068*7c478bd9Sstevel@tonic-gate printf( "Unknown result type 0x%x\n", lm->lm_msgtype ); 1069*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, lm, "unknown" ); 1070*7c478bd9Sstevel@tonic-gate } 1071*7c478bd9Sstevel@tonic-gate } 1072*7c478bd9Sstevel@tonic-gate 1073*7c478bd9Sstevel@tonic-gate static void 1074*7c478bd9Sstevel@tonic-gate print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s ) 1075*7c478bd9Sstevel@tonic-gate { 1076*7c478bd9Sstevel@tonic-gate int rc, i; 1077*7c478bd9Sstevel@tonic-gate int errCode; 1078*7c478bd9Sstevel@tonic-gate char *matched = NULL, *errMsg = NULL, **referrals = NULL; 1079*7c478bd9Sstevel@tonic-gate LDAPControl **srvctrls = NULL; 1080*7c478bd9Sstevel@tonic-gate 1081*7c478bd9Sstevel@tonic-gate if ((rc = ldap_parse_result(ld, lm, &errCode, &matched, &errMsg, &referrals, &srvctrls, 0)) != LDAP_SUCCESS){ 1082*7c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: error while parsing result (%s)\n", s, ldap_err2string(rc)); 1083*7c478bd9Sstevel@tonic-gate return; 1084*7c478bd9Sstevel@tonic-gate } 1085*7c478bd9Sstevel@tonic-gate 1086*7c478bd9Sstevel@tonic-gate 1087*7c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: %s\n", s, ldap_err2string(errCode)); 1088*7c478bd9Sstevel@tonic-gate if (errCode == LDAP_REFERRAL){ 1089*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\tReferrals returned: \n"); 1090*7c478bd9Sstevel@tonic-gate for (i = 0; referrals[i] != NULL; i++) 1091*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t%s\n", referrals[i]); 1092*7c478bd9Sstevel@tonic-gate } 1093*7c478bd9Sstevel@tonic-gate if (errMsg && *errMsg) 1094*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\tAdditional info: %s\n", errMsg); 1095*7c478bd9Sstevel@tonic-gate free(errMsg); 1096*7c478bd9Sstevel@tonic-gate if (NAME_ERROR(errCode) && matched && *matched){ 1097*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\tMatched DN: %s\n", matched); 1098*7c478bd9Sstevel@tonic-gate free(matched); 1099*7c478bd9Sstevel@tonic-gate } 1100*7c478bd9Sstevel@tonic-gate if (srvctrls != NULL){ 1101*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\tLDAPControls returned: \n"); 1102*7c478bd9Sstevel@tonic-gate for (i=0;srvctrls[i] != NULL; i++) 1103*7c478bd9Sstevel@tonic-gate fprintf(stderr, "\t\t%s (%s)\n", srvctrls[i]->ldctl_oid, srvctrls[i]->ldctl_iscritical ? "Critical" : "Not critical"); 1104*7c478bd9Sstevel@tonic-gate } 1105*7c478bd9Sstevel@tonic-gate return; 1106*7c478bd9Sstevel@tonic-gate } 1107*7c478bd9Sstevel@tonic-gate 1108*7c478bd9Sstevel@tonic-gate static void 1109*7c478bd9Sstevel@tonic-gate print_search_entry( LDAP *ld, LDAPMessage *res ) 1110*7c478bd9Sstevel@tonic-gate { 1111*7c478bd9Sstevel@tonic-gate BerElement *ber; 1112*7c478bd9Sstevel@tonic-gate char *a, *dn, *ufn; 1113*7c478bd9Sstevel@tonic-gate struct berval **vals; 1114*7c478bd9Sstevel@tonic-gate int i; 1115*7c478bd9Sstevel@tonic-gate LDAPMessage *e; 1116*7c478bd9Sstevel@tonic-gate 1117*7c478bd9Sstevel@tonic-gate for ( e = ldap_first_message( ld, res ); e != NULLMSG; 1118*7c478bd9Sstevel@tonic-gate e = ldap_next_message( ld, e ) ) { 1119*7c478bd9Sstevel@tonic-gate if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT ) 1120*7c478bd9Sstevel@tonic-gate break; 1121*7c478bd9Sstevel@tonic-gate 1122*7c478bd9Sstevel@tonic-gate dn = ldap_get_dn( ld, e ); 1123*7c478bd9Sstevel@tonic-gate printf( "\tDN: %s\n", dn ); 1124*7c478bd9Sstevel@tonic-gate 1125*7c478bd9Sstevel@tonic-gate ufn = ldap_dn2ufn( dn ); 1126*7c478bd9Sstevel@tonic-gate printf( "\tUFN: %s\n", ufn ); 1127*7c478bd9Sstevel@tonic-gate free( dn ); 1128*7c478bd9Sstevel@tonic-gate free( ufn ); 1129*7c478bd9Sstevel@tonic-gate 1130*7c478bd9Sstevel@tonic-gate if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){ 1131*7c478bd9Sstevel@tonic-gate char **urls = ldap_get_reference_urls(ld, e); 1132*7c478bd9Sstevel@tonic-gate if (urls == NULL){ 1133*7c478bd9Sstevel@tonic-gate printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno)); 1134*7c478bd9Sstevel@tonic-gate } else { 1135*7c478bd9Sstevel@tonic-gate for (i=0;urls[i] != NULL;i++) 1136*7c478bd9Sstevel@tonic-gate printf("\t\tURL: %s\n", urls[i]); 1137*7c478bd9Sstevel@tonic-gate } 1138*7c478bd9Sstevel@tonic-gate } else { 1139*7c478bd9Sstevel@tonic-gate for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL; 1140*7c478bd9Sstevel@tonic-gate a = ldap_next_attribute( ld, e, ber ) ) { 1141*7c478bd9Sstevel@tonic-gate printf( "\t\tATTR: %s\n", a ); 1142*7c478bd9Sstevel@tonic-gate if ( (vals = ldap_get_values_len( ld, e, a )) 1143*7c478bd9Sstevel@tonic-gate == NULL ) { 1144*7c478bd9Sstevel@tonic-gate printf( "\t\t\t(no values)\n" ); 1145*7c478bd9Sstevel@tonic-gate } else { 1146*7c478bd9Sstevel@tonic-gate for ( i = 0; vals[i] != NULL; i++ ) { 1147*7c478bd9Sstevel@tonic-gate int j, nonascii; 1148*7c478bd9Sstevel@tonic-gate 1149*7c478bd9Sstevel@tonic-gate nonascii = 0; 1150*7c478bd9Sstevel@tonic-gate for ( j = 0; j < vals[i]->bv_len; j++ ) 1151*7c478bd9Sstevel@tonic-gate if ( !isascii( vals[i]->bv_val[j] ) ) { 1152*7c478bd9Sstevel@tonic-gate nonascii = 1; 1153*7c478bd9Sstevel@tonic-gate break; 1154*7c478bd9Sstevel@tonic-gate } 1155*7c478bd9Sstevel@tonic-gate 1156*7c478bd9Sstevel@tonic-gate if ( nonascii ) { 1157*7c478bd9Sstevel@tonic-gate printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len ); 1158*7c478bd9Sstevel@tonic-gate #ifdef BPRINT_NONASCII 1159*7c478bd9Sstevel@tonic-gate lber_bprint( vals[i]->bv_val, 1160*7c478bd9Sstevel@tonic-gate vals[i]->bv_len ); 1161*7c478bd9Sstevel@tonic-gate #endif /* BPRINT_NONASCII */ 1162*7c478bd9Sstevel@tonic-gate continue; 1163*7c478bd9Sstevel@tonic-gate } 1164*7c478bd9Sstevel@tonic-gate printf( "\t\t\tlength (%ld) %s\n", 1165*7c478bd9Sstevel@tonic-gate vals[i]->bv_len, vals[i]->bv_val ); 1166*7c478bd9Sstevel@tonic-gate } 1167*7c478bd9Sstevel@tonic-gate ber_bvecfree( vals ); 1168*7c478bd9Sstevel@tonic-gate } 1169*7c478bd9Sstevel@tonic-gate } 1170*7c478bd9Sstevel@tonic-gate } 1171*7c478bd9Sstevel@tonic-gate } 1172*7c478bd9Sstevel@tonic-gate 1173*7c478bd9Sstevel@tonic-gate if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT 1174*7c478bd9Sstevel@tonic-gate || res->lm_chain != NULLMSG ) 1175*7c478bd9Sstevel@tonic-gate print_ldap_result( ld, res, "search" ); 1176*7c478bd9Sstevel@tonic-gate } 1177