static void options_callback( int option, char *optarg ) { char *s, *temp_arg, *ps_ptr, *ps_arg; switch( option ) { case 'u': /* include UFN */ ++includeufn; break; case 't': /* write attribute values to /tmp files */ ++vals2tmp; break; case 'U': /* produce file URLs in conjunction with -t */ ++produce_file_urls; break; case 'e': /* minimize base-64 encoding of values */ ++minimize_base64; break; case 'A': /* retrieve attribute names only -- no values */ ++attrsonly; break; case 'L': /* print entries in LDIF format -- now the default */ break; #ifdef SOLARIS_LDAP_CMD case 'r': /* print entries in the old format */ ldif = 0; break; #endif /* SOLARIS_LDAP_CMD */ #ifdef HAVE_SASL_OPTIONS #ifdef HAVE_SASL_OPTIONS_2 case 'o': /* print entries using old ldapsearch format */ ldif = 0; break; #endif #else case 'o': /* print entries using old ldapsearch format */ ldif = 0; break; #endif case 'B': /* allow binary values to be printed, use old format */ ++allow_binary; ldif = 0; break; case '1': /* omit leading "version: #" line from LDIF output */ write_ldif_version = 0; break; case 's': /* search scope */ if ( strncasecmp( optarg, "base", 4 ) == 0 ) { scope = LDAP_SCOPE_BASE; } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) { scope = LDAP_SCOPE_ONELEVEL; } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) { scope = LDAP_SCOPE_SUBTREE; } else { fprintf( stderr, gettext("scope should be base, one, or sub\n") ); usage(); } break; case 'a': /* set alias deref option */ if ( strncasecmp( optarg, "never", 5 ) == 0 ) { deref = LDAP_DEREF_NEVER; } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) { deref = LDAP_DEREF_SEARCHING; } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) { deref = LDAP_DEREF_FINDING; } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) { deref = LDAP_DEREF_ALWAYS; } else { fprintf( stderr, gettext("alias deref should be never, search, find, or always\n") ); usage(); } break; case 'F': /* field separator */ sep = strdup( optarg ); ldif = 0; break; #ifndef SOLARIS_LDAP_CMD case 'c': if ( optarg && optarg[0] == '\0' ) { /* -c "" means "This user" */ get_effectiverights_control_target_dn = NULL; do_effective_rights_control = 1; }else if ( strlen(optarg) < 3 || (strncasecmp(optarg, "dn:", 3) != 0) ) { fprintf(stderr, gettext("-c wrong format--should be \"\" or \"dn:...\".\n" "\"dn:\" means anonymous user.")); usage(); } else { get_effectiverights_control_target_dn = strdup(optarg); do_effective_rights_control = 1; } break; case 'X': get_effectiverights_control_attrlist = get_effectiverights_attrlist(optarg); do_effective_rights_control = 1; break; #endif /* SOLARIS_LDAP_CMD */ case 'b': /* searchbase */ base = strdup( optarg ); break; case 'l': /* time limit */ timelimit = atoi( optarg ); break; case 'x': /* server sorting requested */ server_sort = 1; break; case 'z': /* size limit */ sizelimit = atoi( optarg ); break; case 'S': /* sort attribute */ ldap_charray_add( &sortattr, strdup( optarg ) ); break; case 'T': /* don't fold lines */ fold = 0; break; case 'G': /* do the virtual list setup */ use_vlv++; s = strchr(optarg, VLV_PARAM_SEP ); if (s != NULL) { vlv_before = atoi(optarg); s++; vlv_after = atoi( s ); s = strchr(s, VLV_PARAM_SEP ); if (s != NULL) { s++; /* below is a small set of logic to implement the following cases * -G23:23:wilber * -G23:23:"wilber:wright" * -G23:23:'wilber' * -G23:23:wilber wright * all of the above are before, after, value - NOTE: a colon not in a quoted * string will break the parser!!!! * -G23:23:45:600 * above is index, count encoding */ if (*s == '\'' || *s == '"') { vlv_value = strdup( s ); } else { if (strchr( s, VLV_PARAM_SEP )) { /* we have an index + count option */ vlv_index = atoi( s ); vlv_count = atoi( strchr( s, VLV_PARAM_SEP) + 1); } else { /* we don't have a quote surrounding the assertion value * do we need to??? */ vlv_value = strdup( s ); } } } else { fprintf( stderr,gettext("Illegal 'after' paramater for virtual list\n") ); exit( LDAP_PARAM_ERROR ); } } else { fprintf( stderr,gettext("Illegal 'before' paramater for virtual list\n") ); exit( LDAP_PARAM_ERROR ); } break; case 'C': use_psearch++; if ( (ps_arg = strdup( optarg)) == NULL ) { perror ("strdup"); exit (LDAP_NO_MEMORY); } ps_ptr=strtok(ps_arg, ":"); if (ps_ptr == NULL || (strcasecmp(ps_ptr, "ps")) ) { fprintf (stderr, gettext("Invalid argument for -C\n")); usage(); } if (NULL != (ps_ptr=strtok(NULL, ":"))) { if ( (temp_arg = strdup( ps_ptr )) == NULL ) { perror ("strdup"); exit (LDAP_NO_MEMORY); } } else { fprintf (stderr, gettext("Invalid argument for -C\n")); usage(); } if (NULL != (ps_ptr=strtok(NULL, ":"))) { if ( (changesonly = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) { fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr); usage(); } } if (NULL != (ps_ptr=strtok(NULL, ":"))) { if ( (return_echg_ctls = ldaptool_boolean_str2value(ps_ptr, 0)) == -1) { fprintf(stderr, gettext("Invalid option value: %s\n"), ps_ptr); usage(); } } /* Now parse the temp_arg and build chgtype as * the changetypes are encountered */ if ((ps_ptr = strtok( temp_arg, "," )) == NULL) { usage(); } else { while ( ps_ptr ) { if ((strcasecmp(ps_ptr, "add"))==0) chgtype |= LDAP_CHANGETYPE_ADD; else if ((strcasecmp(ps_ptr, "delete"))==0) chgtype |= LDAP_CHANGETYPE_DELETE; else if ((strcasecmp(ps_ptr, "modify"))==0) chgtype |= LDAP_CHANGETYPE_MODIFY; else if ((strcasecmp(ps_ptr, "moddn"))==0) chgtype |= LDAP_CHANGETYPE_MODDN; else if ((strcasecmp(ps_ptr, "any"))==0) chgtype = LDAP_CHANGETYPE_ANY; else { fprintf(stderr, gettext("Unknown changetype: %s\n"), ps_ptr); usage(); } ps_ptr = strtok( NULL, "," ); } } break; default: usage(); break; } }
static int interaction( unsigned flags, sasl_interact_t *interact, lutilSASLdefaults *defaults ) { const char *dflt = interact->defresult; char input[1024]; int noecho=0; int challenge=0; switch( interact->id ) { case SASL_CB_GETREALM: if( defaults ) dflt = defaults->realm; break; case SASL_CB_AUTHNAME: if( defaults ) dflt = defaults->authcid; break; case SASL_CB_PASS: if( defaults ) dflt = defaults->passwd; noecho = 1; break; case SASL_CB_USER: if( defaults ) dflt = defaults->authzid; break; case SASL_CB_NOECHOPROMPT: noecho = 1; challenge = 1; break; case SASL_CB_ECHOPROMPT: challenge = 1; break; } if( dflt && !*dflt ) dflt = NULL; if( flags != LDAP_SASL_INTERACTIVE && ( dflt || interact->id == SASL_CB_USER ) ) { goto use_default; } if( flags == LDAP_SASL_QUIET ) { /* don't prompt */ return LDAP_OTHER; } if( challenge ) { if( interact->challenge ) { fprintf( stderr, _("Challenge: %s\n"), interact->challenge ); } } if( dflt ) { fprintf( stderr, _("Default: %s\n"), dflt ); } snprintf( input, sizeof input, "%s: ", interact->prompt ? interact->prompt : _("Interact") ); if( noecho ) { interact->result = (char *) getpassphrase( input ); interact->len = interact->result ? strlen( interact->result ) : 0; } else { /* prompt user */ fputs( input, stderr ); /* get input */ interact->result = fgets( input, sizeof(input), stdin ); if( interact->result == NULL ) { interact->len = 0; return LDAP_UNAVAILABLE; } /* len of input */ interact->len = strlen(input); if( interact->len > 0 && input[interact->len - 1] == '\n' ) { /* input includes '\n', trim it */ interact->len--; input[interact->len] = '\0'; } } if( interact->len > 0 ) { /* duplicate */ char *p = (char *)interact->result; ldap_charray_add(&defaults->resps, interact->result); interact->result = defaults->resps[defaults->nresps++]; /* zap */ memset( p, '\0', interact->len ); } else { use_default: /* input must be empty */ interact->result = (dflt && *dflt) ? dflt : ""; interact->len = strlen( interact->result ); } return LDAP_SUCCESS; }
int main( int argc, char **argv ) { char *filtpattern, **attrs; int rc, optind, i, first, free_filtpattern; LDAP *ld; #ifdef SOLARIS_LDAP_CMD char *locale = setlocale(LC_ALL, ""); textdomain(TEXT_DOMAIN); ldaptool_require_binddn = 0; #endif /* SOLARIS_LDAP_CMD */ free_filtpattern = 0; deref = LDAP_DEREF_NEVER; allow_binary = vals2tmp = attrsonly = 0; minimize_base64 = produce_file_urls = 0; ldif = 1; fold = 1; sizelimit = timelimit = 0; scope = LDAP_SCOPE_SUBTREE; server_sort = 0; #ifdef notdef #ifdef HPUX11 #ifndef __LP64__ _main( argc, argv); #endif /* __LP64_ */ #endif /* HPUX11 */ #endif ldaptool_reset_control_array( ldaptool_request_ctrls ); #ifdef HAVE_SASL_OPTIONS #ifdef SOLARIS_LDAP_CMD optind = ldaptool_process_args( argc, argv, "ABLTU1etuxra:b:F:G:l:S:s:z:C:", 0, options_callback ); #else optind = ldaptool_process_args( argc, argv, "ABLTU1etuxa:b:F:G:l:S:s:z:C:c:", 0, options_callback ); #endif /* SOLARIS_LDAP_CMD */ #else optind = ldaptool_process_args( argc, argv, "ABLTU1eotuxa:b:F:G:l:S:s:z:C:c:", 0, options_callback ); #endif /* HAVE_SASL_OPTIONS */ if ( optind == -1 ) { usage(); } if ( base == NULL ) { if (( base = getenv( "LDAP_BASEDN" )) == NULL ) { usage(); } } if ( sortattr ) { for ( sortsize = 0; sortattr[sortsize] != NULL; sortsize++ ) { ; /* NULL */ } sortsize++; /* add in the final NULL field */ skipsortattr = (int *) malloc( sortsize * sizeof(int *) ); if ( skipsortattr == NULL ) { fprintf( stderr, gettext("Out of memory\n") ); exit( LDAP_NO_MEMORY ); } memset( (char *) skipsortattr, 0, sortsize * sizeof(int *) ); } else if ( server_sort ) { server_sort = 0; /* ignore this option if no sortattrs were given */ } if ( argc - optind < 1 ) { if ( ldaptool_fp == NULL ) { usage(); } attrs = NULL; filtpattern = "%s"; } else { /* there are additional args (filter + attrs) */ if ( ldaptool_fp == NULL || strstr( argv[ optind ], "%s" ) != NULL ) { filtpattern = ldaptool_local2UTF8( argv[ optind ] ); /* since local2UTF8 always allocates something, we should free it */ free_filtpattern = 1; ++optind; } else { filtpattern = "%s"; } if ( argv[ optind ] == NULL ) { attrs = NULL; } else if ( sortattr == NULL || *sortattr == '\0' || server_sort) { attrs = &argv[ optind ]; } else { attrs = ldap_charray_dup( &argv[ optind ] ); if ( attrs == NULL ) { fprintf( stderr, gettext("Out of memory\n") ); exit( LDAP_NO_MEMORY ); } for ( i = 0; i < (sortsize - 1); i++ ) { if ( !ldap_charray_inlist( attrs, sortattr[i] ) ) { if ( ldap_charray_add( &attrs, sortattr[i] ) != 0 ) { fprintf( stderr, gettext("Out of memory\n") ); exit( LDAP_NO_MEMORY ); } /* * attribute in the search list only for the purpose of * sorting */ skipsortattr[i] = 1; } } } } ld = ldaptool_ldap_init( 0 ); if ( !ldaptool_not ) { ldap_set_option( ld, LDAP_OPT_DEREF, &deref ); ldap_set_option( ld, LDAP_OPT_TIMELIMIT, &timelimit ); ldap_set_option( ld, LDAP_OPT_SIZELIMIT, &sizelimit ); } ldaptool_bind( ld ); if ( ldaptool_verbose ) { printf( gettext("filter pattern: %s\nreturning: "), filtpattern ); if ( attrs == NULL ) { printf( gettext("ALL") ); } else { for ( i = 0; attrs[ i ] != NULL; ++i ) { printf( "%s ", attrs[ i ] ); } } putchar( '\n' ); } if ( ldaptool_fp == NULL ) { char *conv; conv = ldaptool_local2UTF8( base ); rc = dosearch( ld, conv, scope, attrs, attrsonly, filtpattern, "" ); if( conv != NULL ) free( conv ); } else { int done = 0; rc = LDAP_SUCCESS; first = 1; while ( rc == LDAP_SUCCESS && !done ) { char *linep = NULL; int increment = 0; int c, index; /* allocate initial block of memory */ if ((linep = (char *)malloc(BUFSIZ)) == NULL) { fprintf( stderr, gettext("Out of memory\n") ); exit( LDAP_NO_MEMORY ); } increment++; index = 0; while ((c = fgetc( ldaptool_fp )) != '\n' && c != EOF) { /* check if we will overflow the buffer */ if ((c != EOF) && (index == ((increment * BUFSIZ) -1))) { /* if we did, add another BUFSIZ worth of bytes */ if ((linep = (char *) realloc(linep, (increment + 1) * BUFSIZ)) == NULL) { fprintf( stderr, gettext("Out of memory\n") ); exit( LDAP_NO_MEMORY ); } increment++; } linep[index++] = c; } if (c == EOF) { done = 1; break; } linep[index] = '\0'; if ( !first ) { putchar( '\n' ); } else { first = 0; } rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, linep ); free (linep); } } ldaptool_cleanup( ld ); if (free_filtpattern != 0 && filtpattern != NULL) { free (filtpattern); } /* check for and report output error */ fflush( stdout ); rc = ldaptool_check_ferror( stdout, rc, gettext("output error (output might be incomplete)") ); return( rc ); }