int main( int argc, char **argv ) { char *rbuf = NULL, *rejbuf = NULL; FILE *rejfp; struct LDIFFP *ldiffp, ldifdummy = {0}; char *matched_msg, *error_msg; int rc, retval; int len; int i = 0; int lineno, nextline = 0, lmax = 0; LDAPControl c[1]; 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(); if ( rejfile != NULL ) { if (( rejfp = fopen( rejfile, "w" )) == NULL ) { perror( rejfile ); return( EXIT_FAILURE ); } } else { rejfp = NULL; } if ( infile != NULL ) { if (( ldiffp = ldif_open( infile, "r" )) == NULL ) { perror( infile ); return( EXIT_FAILURE ); } } else { ldifdummy.fp = stdin; ldiffp = &ldifdummy; } if ( debug ) ldif_debug = debug; ld = tool_conn_setup( dont, 0 ); if ( !dont ) { if ( pw_file || want_bindpw ) { if ( pw_file ) { rc = lutil_get_filed_password( pw_file, &passwd ); if( rc ) return EXIT_FAILURE; } else { passwd.bv_val = getpassphrase( _("Enter LDAP Password: "******"ldap_txn_start_s", rc, NULL, NULL, NULL, NULL ); if( txn > 1 ) return EXIT_FAILURE; txn = 0; } } #endif if ( 0 #ifdef LDAP_X_TXN || txn #endif ) { #ifdef LDAP_X_TXN if( txn ) { c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC; c[i].ldctl_value = *txn_id; c[i].ldctl_iscritical = 1; i++; } #endif } tool_server_controls( ld, c, i ); rc = 0; retval = 0; lineno = 1; while (( rc == 0 || contoper ) && ldif_read_record( ldiffp, &nextline, &rbuf, &lmax )) { if ( rejfp ) { len = strlen( rbuf ); if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) { perror( "malloc" ); exit( EXIT_FAILURE ); } memcpy( rejbuf, rbuf, len+1 ); } rc = process_ldif_rec( rbuf, lineno ); lineno = nextline+1; if ( rc ) retval = rc; if ( rc && rejfp ) { fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc); matched_msg = NULL; ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg); if ( matched_msg != NULL ) { if ( *matched_msg != '\0' ) { fprintf( rejfp, _(", matched DN: %s"), matched_msg ); } ldap_memfree( matched_msg ); } error_msg = NULL; ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg); if ( error_msg != NULL ) { if ( *error_msg != '\0' ) { fprintf( rejfp, _(", additional info: %s"), error_msg ); } ldap_memfree( error_msg ); } fprintf( rejfp, "\n%s\n", rejbuf ); } if (rejfp) ber_memfree( rejbuf ); } ber_memfree( rbuf ); #ifdef LDAP_X_TXN if( retval == 0 && txn ) { rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL ); if ( rc != LDAP_OPT_SUCCESS ) { fprintf( stderr, "Could not unset controls for ldap_txn_end\n"); } /* create transaction */ rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL ); if( rc != LDAP_SUCCESS ) { tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL ); retval = rc; } } #endif if ( !dont ) { tool_unbind( ld ); } if ( rejfp != NULL ) { fclose( rejfp ); } tool_destroy(); return( retval ); }
int slappasswd( int argc, char *argv[] ) { #ifdef LUTIL_SHA1_BYTES char *default_scheme = "{SSHA}"; #else char *default_scheme = "{SMD5}"; #endif char *scheme = default_scheme; char *newpw = NULL; char *pwfile = NULL; const char *text; const char *progname = "slappasswd"; int i; char *newline = "\n"; struct berval passwd = BER_BVNULL; struct berval hash; while( (i = getopt( argc, argv, "c:d:gh:ns:T:vu" )) != EOF ) { switch (i) { case 'c': /* crypt salt format */ scheme = "{CRYPT}"; lutil_salt_format( optarg ); break; case 'g': /* new password (generate) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -g incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else if ( lutil_passwd_generate( &passwd, 8 )) { fprintf( stderr, "Password generation failed\n" ); return EXIT_FAILURE; } break; case 'h': /* scheme */ if ( scheme != default_scheme ) { fprintf( stderr, "Scheme already provided\n" ); return EXIT_FAILURE; } else { scheme = ch_strdup( optarg ); } break; case 'n': newline = ""; break; case 's': /* new password (secret) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -s incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else { char* p; newpw = ch_strdup( optarg ); for( p = optarg; *p != '\0'; p++ ) { *p = '\0'; } } break; case 'T': /* password file */ if ( pwfile != NULL ) { fprintf( stderr, "Password file already provided\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "Option -T incompatible with -s/-g\n" ); return EXIT_FAILURE; } pwfile = optarg; break; case 'u': /* RFC2307 userPassword */ break; case 'v': /* verbose */ verbose++; break; default: usage ( progname ); } } if( argc - optind != 0 ) { usage( progname ); } if( pwfile != NULL ) { if( lutil_get_filed_password( pwfile, &passwd )) { return EXIT_FAILURE; } } else if ( BER_BVISEMPTY( &passwd )) { if( newpw == NULL ) { /* prompt for new password */ char *cknewpw; newpw = ch_strdup(getpassphrase("New password: "******"Re-enter new password: "******"Password values do not match\n" ); return EXIT_FAILURE; } } passwd.bv_val = newpw; passwd.bv_len = strlen(passwd.bv_val); } else { hash = passwd; goto print_pw; } lutil_passwd_hash( &passwd, scheme, &hash, &text ); if( hash.bv_val == NULL ) { fprintf( stderr, "Password generation failed for scheme %s: %s\n", scheme, text ? text : "" ); return EXIT_FAILURE; } if( lutil_passwd( &hash, &passwd, NULL, &text ) ) { fprintf( stderr, "Password verification failed. %s\n", text ? text : "" ); return EXIT_FAILURE; } print_pw:; printf( "%s%s" , hash.bv_val, newline ); return EXIT_SUCCESS; }
int slappasswd( int argc, char *argv[] ) { int rc = EXIT_SUCCESS; #ifdef LUTIL_SHA1_BYTES char *default_scheme = "{SSHA}"; #else char *default_scheme = "{SMD5}"; #endif char *scheme = default_scheme; char *newpw = NULL; char *pwfile = NULL; const char *text; const char *progname = "slappasswd"; int i; char *newline = "\n"; struct berval passwd = BER_BVNULL; struct berval hash; #ifdef LDAP_DEBUG /* tools default to "none", so that at least LDAP_DEBUG_ANY * messages show up; use -d 0 to reset */ slap_debug = LDAP_DEBUG_NONE; #endif ldap_syslog = 0; while( (i = getopt( argc, argv, "c:d:gh:no:s:T:vu" )) != EOF ) { switch (i) { case 'c': /* crypt salt format */ scheme = "{CRYPT}"; lutil_salt_format( optarg ); break; case 'g': /* new password (generate) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -g incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else if ( lutil_passwd_generate( &passwd, 8 )) { fprintf( stderr, "Password generation failed\n" ); return EXIT_FAILURE; } break; case 'h': /* scheme */ if ( scheme != default_scheme ) { fprintf( stderr, "Scheme already provided\n" ); return EXIT_FAILURE; } else { scheme = ch_strdup( optarg ); } break; case 'n': newline = ""; break; case 'o': if ( parse_slappasswdopt() ) { usage ( progname ); } break; case 's': /* new password (secret) */ if ( pwfile != NULL ) { fprintf( stderr, "Option -s incompatible with -T\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "New password already provided\n" ); return EXIT_FAILURE; } else { char* p; newpw = ch_strdup( optarg ); for( p = optarg; *p != '\0'; p++ ) { *p = '\0'; } } break; case 'T': /* password file */ if ( pwfile != NULL ) { fprintf( stderr, "Password file already provided\n" ); return EXIT_FAILURE; } else if ( newpw != NULL ) { fprintf( stderr, "Option -T incompatible with -s/-g\n" ); return EXIT_FAILURE; } pwfile = optarg; break; case 'u': /* RFC2307 userPassword */ break; case 'v': /* verbose */ verbose++; break; default: usage ( progname ); } } if( argc - optind != 0 ) { usage( progname ); } #ifdef SLAPD_MODULES if ( module_init() != 0 ) { fprintf( stderr, "%s: module_init failed\n", progname ); return EXIT_FAILURE; } if ( modulepath && module_path(modulepath) ) { rc = EXIT_FAILURE; goto destroy; } if ( moduleload && module_load(moduleload, 0, NULL) ) { rc = EXIT_FAILURE; goto destroy; } #endif if( pwfile != NULL ) { if( lutil_get_filed_password( pwfile, &passwd )) { rc = EXIT_FAILURE; goto destroy; } } else if ( BER_BVISEMPTY( &passwd )) { if( newpw == NULL ) { /* prompt for new password */ char *cknewpw; newpw = ch_strdup(getpassphrase("New password: "******"Re-enter new password: "******"Password values do not match\n" ); rc = EXIT_FAILURE; goto destroy; } } passwd.bv_val = newpw; passwd.bv_len = strlen(passwd.bv_val); } else { hash = passwd; goto print_pw; } lutil_passwd_hash( &passwd, scheme, &hash, &text ); if( hash.bv_val == NULL ) { fprintf( stderr, "Password generation failed for scheme %s: %s\n", scheme, text ? text : "" ); rc = EXIT_FAILURE; goto destroy; } if( lutil_passwd( &hash, &passwd, NULL, &text ) ) { fprintf( stderr, "Password verification failed. %s\n", text ? text : "" ); rc = EXIT_FAILURE; goto destroy; } print_pw:; printf( "%s%s" , hash.bv_val, newline ); destroy:; #ifdef SLAPD_MODULES module_kill(); #endif return rc; }
static int bdb_cf_gen( ConfigArgs *c ) { struct bdb_info *bdb = c->be->be_private; int rc; if ( c->op == SLAP_CONFIG_EMIT ) { rc = 0; switch( c->type ) { case BDB_MODE: { char buf[64]; struct berval bv; bv.bv_len = snprintf( buf, sizeof(buf), "0%o", bdb->bi_dbenv_mode ); if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) { bv.bv_val = buf; value_add_one( &c->rvalue_vals, &bv ); } else { rc = 1; } } break; case BDB_CHKPT: if ( bdb->bi_txn_cp ) { char buf[64]; struct berval bv; bv.bv_len = snprintf( buf, sizeof(buf), "%d %d", bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min ); if ( bv.bv_len > 0 && bv.bv_len < sizeof(buf) ) { bv.bv_val = buf; value_add_one( &c->rvalue_vals, &bv ); } else { rc = 1; } } else { rc = 1; } break; case BDB_CRYPTFILE: if ( bdb->bi_db_crypt_file ) { c->value_string = ch_strdup( bdb->bi_db_crypt_file ); } else { rc = 1; } break; /* If a crypt file has been set, its contents are copied here. * But we don't want the key to be incorporated here. */ case BDB_CRYPTKEY: if ( !bdb->bi_db_crypt_file && !BER_BVISNULL( &bdb->bi_db_crypt_key )) { value_add_one( &c->rvalue_vals, &bdb->bi_db_crypt_key ); } else { rc = 1; } break; case BDB_DIRECTORY: if ( bdb->bi_dbenv_home ) { c->value_string = ch_strdup( bdb->bi_dbenv_home ); } else { rc = 1; } break; case BDB_CONFIG: if ( !( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_db_config ) { char buf[SLAP_TEXT_BUFLEN]; FILE *f = fopen( bdb->bi_db_config_path, "r" ); struct berval bv; if ( f ) { bdb->bi_flags |= BDB_HAS_CONFIG; while ( fgets( buf, sizeof(buf), f )) { ber_str2bv( buf, 0, 1, &bv ); if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) { bv.bv_len--; bv.bv_val[bv.bv_len] = '\0'; } /* shouldn't need this, but ... */ if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) { bv.bv_len--; bv.bv_val[bv.bv_len] = '\0'; } ber_bvarray_add( &bdb->bi_db_config, &bv ); } fclose( f ); } } if ( bdb->bi_db_config ) { int i; struct berval bv; bv.bv_val = c->log; for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) { bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i, bdb->bi_db_config[i].bv_val ); value_add_one( &c->rvalue_vals, &bv ); } } if ( !c->rvalue_vals ) rc = 1; break; case BDB_NOSYNC: if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC ) c->value_int = 1; break; case BDB_CHECKSUM: if ( bdb->bi_flags & BDB_CHKSUM ) c->value_int = 1; break; case BDB_INDEX: bdb_attr_index_unparse( bdb, &c->rvalue_vals ); if ( !c->rvalue_vals ) rc = 1; break; case BDB_LOCKD: rc = 1; if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) { int i; for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) { if ( bdb->bi_lock_detect == (u_int32_t)bdb_lockd[i].mask ) { value_add_one( &c->rvalue_vals, &bdb_lockd[i].word ); rc = 0; break; } } } break; case BDB_SSTACK: c->value_int = bdb->bi_search_stack_depth; break; case BDB_PGSIZE: { struct bdb_db_pgsize *ps; char buf[SLAP_TEXT_BUFLEN]; struct berval bv; int rc = 1; bv.bv_val = buf; for ( ps = bdb->bi_pagesizes; ps; ps = ps->bdp_next ) { bv.bv_len = sprintf( buf, "%s %d", ps->bdp_name.bv_val, ps->bdp_size / 1024 ); value_add_one( &c->rvalue_vals, &bv ); rc = 0; } break; } } return rc; } else if ( c->op == LDAP_MOD_DELETE ) { rc = 0; switch( c->type ) { case BDB_MODE: #if 0 /* FIXME: does it make any sense to change the mode, * if we don't exec a chmod()? */ bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE; break; #endif /* single-valued no-ops */ case BDB_LOCKD: case BDB_SSTACK: break; case BDB_CHKPT: if ( bdb->bi_txn_cp_task ) { struct re_s *re = bdb->bi_txn_cp_task; bdb->bi_txn_cp_task = NULL; ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) ldap_pvt_runqueue_stoptask( &slapd_rq, re ); ldap_pvt_runqueue_remove( &slapd_rq, re ); ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); } bdb->bi_txn_cp = 0; break; case BDB_CONFIG: if ( c->valx < 0 ) { ber_bvarray_free( bdb->bi_db_config ); bdb->bi_db_config = NULL; } else { int i = c->valx; ch_free( bdb->bi_db_config[i].bv_val ); for (; bdb->bi_db_config[i].bv_val; i++) bdb->bi_db_config[i] = bdb->bi_db_config[i+1]; } bdb->bi_flags |= BDB_UPD_CONFIG; c->cleanup = bdb_cf_cleanup; break; /* Doesn't really make sense to change these on the fly; * the entire DB must be dumped and reloaded */ case BDB_CRYPTFILE: if ( bdb->bi_db_crypt_file ) { ch_free( bdb->bi_db_crypt_file ); bdb->bi_db_crypt_file = NULL; } /* FALLTHRU */ case BDB_CRYPTKEY: if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) { ch_free( bdb->bi_db_crypt_key.bv_val ); BER_BVZERO( &bdb->bi_db_crypt_key ); } break; case BDB_DIRECTORY: bdb->bi_flags |= BDB_RE_OPEN; bdb->bi_flags ^= BDB_HAS_CONFIG; ch_free( bdb->bi_dbenv_home ); bdb->bi_dbenv_home = NULL; ch_free( bdb->bi_db_config_path ); bdb->bi_db_config_path = NULL; c->cleanup = bdb_cf_cleanup; ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv ); break; case BDB_NOSYNC: bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 ); break; case BDB_CHECKSUM: bdb->bi_flags &= ~BDB_CHKSUM; break; case BDB_INDEX: if ( c->valx == -1 ) { int i; /* delete all (FIXME) */ for ( i = 0; i < bdb->bi_nattrs; i++ ) { bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING; } bdb->bi_flags |= BDB_DEL_INDEX; c->cleanup = bdb_cf_cleanup; } else { struct berval bv, def = BER_BVC("default"); char *ptr; for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++); bv.bv_val = c->line; bv.bv_len = ptr - bv.bv_val; if ( bvmatch( &bv, &def )) { bdb->bi_defaultmask = 0; } else { int i; char **attrs; char sep; sep = bv.bv_val[ bv.bv_len ]; bv.bv_val[ bv.bv_len ] = '\0'; attrs = ldap_str2charray( bv.bv_val, "," ); for ( i = 0; attrs[ i ]; i++ ) { AttributeDescription *ad = NULL; const char *text; AttrInfo *ai; slap_str2ad( attrs[ i ], &ad, &text ); /* if we got here... */ assert( ad != NULL ); ai = bdb_attr_mask( bdb, ad ); /* if we got here... */ assert( ai != NULL ); ai->ai_indexmask |= BDB_INDEX_DELETING; bdb->bi_flags |= BDB_DEL_INDEX; c->cleanup = bdb_cf_cleanup; } bv.bv_val[ bv.bv_len ] = sep; ldap_charray_free( attrs ); } } break; /* doesn't make sense on the fly; the DB file must be * recreated */ case BDB_PGSIZE: { struct bdb_db_pgsize *ps, **prev; int i; for ( i = 0, prev = &bdb->bi_pagesizes, ps = *prev; ps; prev = &ps->bdp_next, ps = ps->bdp_next, i++ ) { if ( c->valx == -1 || i == c->valx ) { *prev = ps->bdp_next; ch_free( ps ); ps = *prev; if ( i == c->valx ) break; } } } break; } return rc; } switch( c->type ) { case BDB_MODE: if ( ASCII_DIGIT( c->argv[1][0] ) ) { long mode; char *next; errno = 0; mode = strtol( c->argv[1], &next, 0 ); if ( errno != 0 || next == c->argv[1] || next[0] != '\0' ) { fprintf( stderr, "%s: " "unable to parse mode=\"%s\".\n", c->log, c->argv[1] ); return 1; } bdb->bi_dbenv_mode = mode; } else { char *m = c->argv[1]; int who, what, mode = 0; if ( strlen( m ) != STRLENOF("-rwxrwxrwx") ) { return 1; } if ( m[0] != '-' ) { return 1; } m++; for ( who = 0; who < 3; who++ ) { for ( what = 0; what < 3; what++, m++ ) { if ( m[0] == '-' ) { continue; } else if ( m[0] != "rwx"[what] ) { return 1; } mode += ((1 << (2 - what)) << 3*(2 - who)); } } bdb->bi_dbenv_mode = mode; } break; case BDB_CHKPT: { long l; bdb->bi_txn_cp = 1; if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) { fprintf( stderr, "%s: " "invalid kbyte \"%s\" in \"checkpoint\".\n", c->log, c->argv[1] ); return 1; } bdb->bi_txn_cp_kbyte = l; if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) { fprintf( stderr, "%s: " "invalid minutes \"%s\" in \"checkpoint\".\n", c->log, c->argv[2] ); return 1; } bdb->bi_txn_cp_min = l; /* If we're in server mode and time-based checkpointing is enabled, * submit a task to perform periodic checkpoints. */ if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) { struct re_s *re = bdb->bi_txn_cp_task; if ( re ) { re->interval.tv_sec = bdb->bi_txn_cp_min * 60; } else { if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) { fprintf( stderr, "%s: " "\"checkpoint\" must occur after \"suffix\".\n", c->log ); return 1; } ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb, LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val ); ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); } } } break; case BDB_CONFIG: { char *ptr = c->line; struct berval bv; if ( c->op == SLAP_CONFIG_ADD ) { ptr += STRLENOF("dbconfig"); while (!isspace((unsigned char)*ptr)) ptr++; while (isspace((unsigned char)*ptr)) ptr++; } if ( bdb->bi_flags & BDB_IS_OPEN ) { bdb->bi_flags |= BDB_UPD_CONFIG; c->cleanup = bdb_cf_cleanup; } else { /* If we're just starting up... */ FILE *f; /* If a DB_CONFIG file exists, or we don't know the path * to the DB_CONFIG file, ignore these directives */ if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path ) break; f = fopen( bdb->bi_db_config_path, "a" ); if ( f ) { /* FIXME: EBCDIC probably needs special handling */ fprintf( f, "%s\n", ptr ); fclose( f ); } } ber_str2bv( ptr, 0, 1, &bv ); ber_bvarray_add( &bdb->bi_db_config, &bv ); } break; case BDB_CRYPTFILE: rc = lutil_get_filed_password( c->value_string, &bdb->bi_db_crypt_key ); if ( rc == 0 ) { bdb->bi_db_crypt_file = c->value_string; } break; /* Cannot set key if file was already set */ case BDB_CRYPTKEY: if ( bdb->bi_db_crypt_file ) { rc = 1; } else { bdb->bi_db_crypt_key = c->value_bv; } break; case BDB_DIRECTORY: { FILE *f; char *ptr, *testpath; int len; len = strlen( c->value_string ); testpath = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DUMMY") + 1 ); ptr = lutil_strcopy( testpath, c->value_string ); *ptr++ = LDAP_DIRSEP[0]; strcpy( ptr, "DUMMY" ); f = fopen( testpath, "w" ); if ( f ) { fclose( f ); unlink( testpath ); } ch_free( testpath ); if ( !f ) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s", c->log, strerror( errno )); Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 ); return -1; } if ( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home ); bdb->bi_dbenv_home = c->value_string; /* See if a DB_CONFIG file already exists here */ if ( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path ); bdb->bi_db_config_path = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 ); ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home ); *ptr++ = LDAP_DIRSEP[0]; strcpy( ptr, "DB_CONFIG" ); f = fopen( bdb->bi_db_config_path, "r" ); if ( f ) { bdb->bi_flags |= BDB_HAS_CONFIG; fclose(f); } } break; case BDB_NOSYNC: if ( c->value_int ) bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC; else bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC; if ( bdb->bi_flags & BDB_IS_OPEN ) { bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, c->value_int ); } break; case BDB_CHECKSUM: if ( c->value_int ) bdb->bi_flags |= BDB_CHKSUM; else bdb->bi_flags &= ~BDB_CHKSUM; break; case BDB_INDEX: rc = bdb_attr_index_config( bdb, c->fname, c->lineno, c->argc - 1, &c->argv[1], &c->reply); if( rc != LDAP_SUCCESS ) return 1; if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) { /* Start the task as soon as we finish here. Set a long * interval (10 hours) so that it only gets scheduled once. */ if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) { fprintf( stderr, "%s: " "\"index\" must occur after \"suffix\".\n", c->log ); return 1; } ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000, bdb_online_index, c->be, LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val ); ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); } break; case BDB_LOCKD: rc = verb_to_mask( c->argv[1], bdb_lockd ); if ( BER_BVISNULL(&bdb_lockd[rc].word) ) { fprintf( stderr, "%s: " "bad policy (%s) in \"lockDetect <policy>\" line\n", c->log, c->argv[1] ); return 1; } bdb->bi_lock_detect = (u_int32_t)rc; break; case BDB_SSTACK: if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) { fprintf( stderr, "%s: depth %d too small, using %d\n", c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH ); c->value_int = MINIMUM_SEARCH_STACK_DEPTH; } bdb->bi_search_stack_depth = c->value_int; break; case BDB_PGSIZE: { struct bdb_db_pgsize *ps, **prev; int i, s; s = atoi(c->argv[2]); if ( s < 1 || s > 64 ) { snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: size must be > 0 and <= 64: %d", c->log, s ); Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 ); return -1; } i = strlen(c->argv[1]); ps = ch_malloc( sizeof(struct bdb_db_pgsize) + i + 1 ); ps->bdp_next = NULL; ps->bdp_name.bv_len = i; ps->bdp_name.bv_val = (char *)(ps+1); strcpy( ps->bdp_name.bv_val, c->argv[1] ); ps->bdp_size = s * 1024; for ( prev = &bdb->bi_pagesizes; *prev; prev = &(*prev)->bdp_next ) ; *prev = ps; } break; } return 0; }
int main(int argc, char **argv) { char *entrydn = NULL, *rdn = NULL, buf[ 4096 ]; FILE *fp; LDAP *ld; int rc, retval, havedn; prog = lutil_progname( "ldapmodrdn", argc, argv ); tool_args( argc, argv ); havedn = 0; if (argc - optind == 2) { if (( rdn = strdup( argv[argc - 1] )) == NULL ) { perror( "strdup" ); return( EXIT_FAILURE ); } if (( entrydn = strdup( argv[argc - 2] )) == NULL ) { perror( "strdup" ); return( EXIT_FAILURE ); } ++havedn; } else if ( argc - optind != 0 ) { fprintf( stderr, "%s: invalid number of arguments (%d), " "only two allowed\n", prog, argc-optind ); usage(); } if ( infile != NULL ) { if (( fp = fopen( infile, "r" )) == NULL ) { perror( infile ); return( EXIT_FAILURE ); } } else { fp = stdin; } ld = tool_conn_setup( 0, 0 ); if ( pw_file || want_bindpw ) { if ( pw_file ) { rc = lutil_get_filed_password( pw_file, &passwd ); if( rc ) return EXIT_FAILURE; } else { passwd.bv_val = getpassphrase( "Enter LDAP Password: "******"strdup" ); return( EXIT_FAILURE ); } rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN ); if ( rc != 0 ) retval = rc; havedn = 0; } else if ( !havedn ) { /* don't have DN yet */ if (( entrydn = strdup( buf )) == NULL ) { perror( "strdup" ); return( EXIT_FAILURE ); } ++havedn; } } } ldap_unbind( ld ); return( retval ); }
int main( int argc, char **argv ) { int i, j; char *uri = NULL; char *host = "localhost"; char *port = NULL; char *manager = NULL; char *passwd = NULL; char *dirname = NULL; char *progdir = NULL; int loops = LOOPS; char *outerloops = OUTERLOOPS; char *retries = RETRIES; char *delay = "0"; DIR *datadir; struct dirent *file; int friendly = 0; int chaserefs = 0; int noattrs = 0; int nobind = 0; int noinit = 1; char *ignore = NULL; /* search */ char *sfile = NULL; char *sreqs[MAXREQS]; char *sattrs[MAXREQS]; char *sbase[MAXREQS]; LDAPURLDesc *slud[MAXREQS]; int snum = 0; char *sargs[MAXARGS]; int sanum; int sextra_args = 0; char scmd[MAXPATHLEN]; int swamp = 0; char swampopt[sizeof("-SSS")]; /* static so that its address can be used in initializer below. */ static char sloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* read */ char *rfile = NULL; char *rreqs[MAXREQS]; int rnum = 0; char *rargs[MAXARGS]; char *rflts[MAXREQS]; int ranum; int rextra_args = 0; char rcmd[MAXPATHLEN]; static char rloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* addel */ char *afiles[MAXREQS]; int anum = 0; char *aargs[MAXARGS]; int aanum; char acmd[MAXPATHLEN]; static char aloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* modrdn */ char *nfile = NULL; char *nreqs[MAXREQS]; int nnum = 0; char *nargs[MAXARGS]; int nanum; char ncmd[MAXPATHLEN]; static char nloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* modify */ char *mfile = NULL; char *mreqs[MAXREQS]; char *mdn[MAXREQS]; int mnum = 0; char *margs[MAXARGS]; int manum; char mcmd[MAXPATHLEN]; static char mloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; /* bind */ char *bfile = NULL; char *breqs[MAXREQS]; char *bcreds[MAXREQS]; char *battrs[MAXREQS]; int bnum = 0; char *bargs[MAXARGS]; int banum; char bcmd[MAXPATHLEN]; static char bloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)]; char **bargs_extra = NULL; char *friendlyOpt = NULL; int pw_ask = 0; char *pw_file = NULL; /* extra action to do after bind... */ typedef struct extra_t { char *action; struct extra_t *next; } extra_t; extra_t *extra = NULL; int nextra = 0; tester_init( "slapd-tester", TESTER_TESTER ); sloops[0] = '\0'; rloops[0] = '\0'; aloops[0] = '\0'; nloops[0] = '\0'; mloops[0] = '\0'; bloops[0] = '\0'; while ( ( i = getopt( argc, argv, "AB:CD:d:FH:h:Ii:j:L:l:NP:p:r:St:Ww:y:" ) ) != EOF ) { switch ( i ) { case 'A': noattrs++; break; case 'B': { char **p, **b = ldap_str2charray( optarg, "," ); extra_t **epp; for ( epp = &extra; *epp; epp = &(*epp)->next ) ; for ( p = b; p[0]; p++ ) { *epp = calloc( 1, sizeof( extra_t ) ); (*epp)->action = p[0]; epp = &(*epp)->next; nextra++; } ldap_memfree( b ); } break; case 'C': chaserefs++; break; case 'D': /* slapd manager */ manager = ArgDup( optarg ); break; case 'd': /* data directory */ dirname = strdup( optarg ); break; case 'F': friendly++; break; case 'H': /* slapd uri */ uri = strdup( optarg ); break; case 'h': /* slapd host */ host = strdup( optarg ); break; case 'I': noinit = 0; break; case 'i': ignore = optarg; break; case 'j': /* the number of parallel clients */ if ( lutil_atoi( &maxkids, optarg ) != 0 ) { usage( argv[0], 'j' ); } break; case 'l': /* the number of loops per client */ if ( !isdigit( (unsigned char) optarg[0] ) ) { char **p, **l = ldap_str2charray( optarg, "," ); for ( p = l; p[0]; p++) { struct { struct berval type; char *buf; } types[] = { { BER_BVC( "add=" ), aloops }, { BER_BVC( "bind=" ), bloops }, { BER_BVC( "modify=" ), mloops }, { BER_BVC( "modrdn=" ), nloops }, { BER_BVC( "read=" ), rloops }, { BER_BVC( "search=" ), sloops }, { BER_BVNULL, NULL } }; int c, n; for ( c = 0; types[c].type.bv_val; c++ ) { if ( strncasecmp( p[0], types[c].type.bv_val, types[c].type.bv_len ) == 0 ) { break; } } if ( types[c].type.bv_val == NULL ) { usage( argv[0], 'l' ); } if ( lutil_atoi( &n, &p[0][types[c].type.bv_len] ) != 0 ) { usage( argv[0], 'l' ); } snprintf( types[c].buf, sizeof( aloops ), "%d", n ); } ldap_charray_free( l ); } else if ( lutil_atoi( &loops, optarg ) != 0 ) { usage( argv[0], 'l' ); } break; case 'L': /* the number of outerloops per client */ outerloops = strdup( optarg ); break; case 'N': nobind++; break; case 'P': /* prog directory */ progdir = strdup( optarg ); break; case 'p': /* the servers port number */ port = strdup( optarg ); break; case 'r': /* the number of retries in case of error */ retries = strdup( optarg ); break; case 'S': swamp++; break; case 't': /* the delay in seconds between each retry */ delay = strdup( optarg ); break; case 'w': /* the managers passwd */ passwd = ArgDup( optarg ); memset( optarg, '*', strlen( optarg ) ); break; case 'W': pw_ask++; break; case 'y': pw_file = optarg; break; default: usage( argv[0], '\0' ); break; } } if (( dirname == NULL ) || ( port == NULL && uri == NULL ) || ( manager == NULL ) || ( passwd == NULL ) || ( progdir == NULL )) { usage( argv[0], '\0' ); } #ifdef HAVE_WINSOCK children = malloc( maxkids * sizeof(HANDLE) ); #endif /* get the file list */ if ( ( datadir = opendir( dirname )) == NULL ) { fprintf( stderr, "%s: couldn't open data directory \"%s\".\n", argv[0], dirname ); exit( EXIT_FAILURE ); } /* look for search, read, modrdn, and add/delete files */ for ( file = readdir( datadir ); file; file = readdir( datadir )) { if ( !strcasecmp( file->d_name, TSEARCHFILE )) { sfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TREADFILE )) { rfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TMODRDNFILE )) { nfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TMODIFYFILE )) { mfile = get_file_name( dirname, file->d_name ); continue; } else if ( !strncasecmp( file->d_name, TADDFILE, strlen( TADDFILE )) && ( anum < MAXREQS )) { afiles[anum++] = get_file_name( dirname, file->d_name ); continue; } else if ( !strcasecmp( file->d_name, TBINDFILE )) { bfile = get_file_name( dirname, file->d_name ); continue; } } closedir( datadir ); if ( pw_ask ) { passwd = getpassphrase( _("Enter LDAP Password: "******"no data files found.\n" ); exit( EXIT_FAILURE ); } /* look for search requests */ if ( sfile ) { snum = get_search_filters( sfile, sreqs, sattrs, sbase, slud ); if ( snum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", sfile, -2*(snum + 1)); exit( EXIT_FAILURE ); } } /* look for read requests */ if ( rfile ) { rnum = get_read_entries( rfile, rreqs, rflts ); if ( rnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", rfile, -2*(rnum + 1) ); exit( EXIT_FAILURE ); } } /* look for modrdn requests */ if ( nfile ) { nnum = get_read_entries( nfile, nreqs, NULL ); if ( nnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", nfile, -2*(nnum + 1) ); exit( EXIT_FAILURE ); } } /* look for modify requests */ if ( mfile ) { mnum = get_search_filters( mfile, mreqs, NULL, mdn, NULL ); if ( mnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", mfile, -2*(mnum + 1) ); exit( EXIT_FAILURE ); } } /* look for bind requests */ if ( bfile ) { bnum = get_search_filters( bfile, bcreds, battrs, breqs, NULL ); if ( bnum < 0 ) { fprintf( stderr, "unable to parse file \"%s\" line %d\n", bfile, -2*(bnum + 1) ); exit( EXIT_FAILURE ); } } /* setup friendly option */ switch ( friendly ) { case 0: break; case 1: friendlyOpt = "-F"; break; default: /* NOTE: right now we don't need it more than twice */ case 2: friendlyOpt = "-FF"; break; } /* setup swamp option */ if ( swamp ) { swampopt[0] = '-'; if ( swamp > 3 ) swamp = 3; swampopt[swamp + 1] = '\0'; for ( ; swamp-- > 0; ) swampopt[swamp + 1] = 'S'; } /* setup loop options */ if ( sloops[0] == '\0' ) snprintf( sloops, sizeof( sloops ), "%d", 10 * loops ); if ( rloops[0] == '\0' ) snprintf( rloops, sizeof( rloops ), "%d", 20 * loops ); if ( aloops[0] == '\0' ) snprintf( aloops, sizeof( aloops ), "%d", loops ); if ( nloops[0] == '\0' ) snprintf( nloops, sizeof( nloops ), "%d", loops ); if ( mloops[0] == '\0' ) snprintf( mloops, sizeof( mloops ), "%d", loops ); if ( bloops[0] == '\0' ) snprintf( bloops, sizeof( bloops ), "%d", 20 * loops ); /* * generate the search clients */ sanum = 0; snprintf( scmd, sizeof scmd, "%s" LDAP_DIRSEP SEARCHCMD, progdir ); sargs[sanum++] = scmd; if ( uri ) { sargs[sanum++] = "-H"; sargs[sanum++] = uri; } else { sargs[sanum++] = "-h"; sargs[sanum++] = host; sargs[sanum++] = "-p"; sargs[sanum++] = port; } sargs[sanum++] = "-D"; sargs[sanum++] = manager; sargs[sanum++] = "-w"; sargs[sanum++] = passwd; sargs[sanum++] = "-l"; sargs[sanum++] = sloops; sargs[sanum++] = "-L"; sargs[sanum++] = outerloops; sargs[sanum++] = "-r"; sargs[sanum++] = retries; sargs[sanum++] = "-t"; sargs[sanum++] = delay; if ( friendly ) { sargs[sanum++] = friendlyOpt; } if ( chaserefs ) { sargs[sanum++] = "-C"; } if ( noattrs ) { sargs[sanum++] = "-A"; } if ( nobind ) { sargs[sanum++] = "-N"; } if ( ignore ) { sargs[sanum++] = "-i"; sargs[sanum++] = ignore; } if ( swamp ) { sargs[sanum++] = swampopt; } sargs[sanum++] = "-b"; sargs[sanum++] = NULL; /* will hold the search base */ sargs[sanum++] = "-s"; sargs[sanum++] = NULL; /* will hold the search scope */ sargs[sanum++] = "-f"; sargs[sanum++] = NULL; /* will hold the search request */ sargs[sanum++] = NULL; sargs[sanum++] = NULL; /* might hold the "attr" request */ sextra_args += 2; sargs[sanum] = NULL; /* * generate the read clients */ ranum = 0; snprintf( rcmd, sizeof rcmd, "%s" LDAP_DIRSEP READCMD, progdir ); rargs[ranum++] = rcmd; if ( uri ) { rargs[ranum++] = "-H"; rargs[ranum++] = uri; } else { rargs[ranum++] = "-h"; rargs[ranum++] = host; rargs[ranum++] = "-p"; rargs[ranum++] = port; } rargs[ranum++] = "-D"; rargs[ranum++] = manager; rargs[ranum++] = "-w"; rargs[ranum++] = passwd; rargs[ranum++] = "-l"; rargs[ranum++] = rloops; rargs[ranum++] = "-L"; rargs[ranum++] = outerloops; rargs[ranum++] = "-r"; rargs[ranum++] = retries; rargs[ranum++] = "-t"; rargs[ranum++] = delay; if ( friendly ) { rargs[ranum++] = friendlyOpt; } if ( chaserefs ) { rargs[ranum++] = "-C"; } if ( noattrs ) { rargs[ranum++] = "-A"; } if ( ignore ) { rargs[ranum++] = "-i"; rargs[ranum++] = ignore; } if ( swamp ) { rargs[ranum++] = swampopt; } rargs[ranum++] = "-e"; rargs[ranum++] = NULL; /* will hold the read entry */ rargs[ranum++] = NULL; rargs[ranum++] = NULL; /* might hold the filter arg */ rextra_args += 2; rargs[ranum] = NULL; /* * generate the modrdn clients */ nanum = 0; snprintf( ncmd, sizeof ncmd, "%s" LDAP_DIRSEP MODRDNCMD, progdir ); nargs[nanum++] = ncmd; if ( uri ) { nargs[nanum++] = "-H"; nargs[nanum++] = uri; } else { nargs[nanum++] = "-h"; nargs[nanum++] = host; nargs[nanum++] = "-p"; nargs[nanum++] = port; } nargs[nanum++] = "-D"; nargs[nanum++] = manager; nargs[nanum++] = "-w"; nargs[nanum++] = passwd; nargs[nanum++] = "-l"; nargs[nanum++] = nloops; nargs[nanum++] = "-L"; nargs[nanum++] = outerloops; nargs[nanum++] = "-r"; nargs[nanum++] = retries; nargs[nanum++] = "-t"; nargs[nanum++] = delay; if ( friendly ) { nargs[nanum++] = friendlyOpt; } if ( chaserefs ) { nargs[nanum++] = "-C"; } if ( ignore ) { nargs[nanum++] = "-i"; nargs[nanum++] = ignore; } nargs[nanum++] = "-e"; nargs[nanum++] = NULL; /* will hold the modrdn entry */ nargs[nanum] = NULL; /* * generate the modify clients */ manum = 0; snprintf( mcmd, sizeof mcmd, "%s" LDAP_DIRSEP MODIFYCMD, progdir ); margs[manum++] = mcmd; if ( uri ) { margs[manum++] = "-H"; margs[manum++] = uri; } else { margs[manum++] = "-h"; margs[manum++] = host; margs[manum++] = "-p"; margs[manum++] = port; } margs[manum++] = "-D"; margs[manum++] = manager; margs[manum++] = "-w"; margs[manum++] = passwd; margs[manum++] = "-l"; margs[manum++] = mloops; margs[manum++] = "-L"; margs[manum++] = outerloops; margs[manum++] = "-r"; margs[manum++] = retries; margs[manum++] = "-t"; margs[manum++] = delay; if ( friendly ) { margs[manum++] = friendlyOpt; } if ( chaserefs ) { margs[manum++] = "-C"; } if ( ignore ) { margs[manum++] = "-i"; margs[manum++] = ignore; } margs[manum++] = "-e"; margs[manum++] = NULL; /* will hold the modify entry */ margs[manum++] = "-a";; margs[manum++] = NULL; /* will hold the ava */ margs[manum] = NULL; /* * generate the add/delete clients */ aanum = 0; snprintf( acmd, sizeof acmd, "%s" LDAP_DIRSEP ADDCMD, progdir ); aargs[aanum++] = acmd; if ( uri ) { aargs[aanum++] = "-H"; aargs[aanum++] = uri; } else { aargs[aanum++] = "-h"; aargs[aanum++] = host; aargs[aanum++] = "-p"; aargs[aanum++] = port; } aargs[aanum++] = "-D"; aargs[aanum++] = manager; aargs[aanum++] = "-w"; aargs[aanum++] = passwd; aargs[aanum++] = "-l"; aargs[aanum++] = aloops; aargs[aanum++] = "-L"; aargs[aanum++] = outerloops; aargs[aanum++] = "-r"; aargs[aanum++] = retries; aargs[aanum++] = "-t"; aargs[aanum++] = delay; if ( friendly ) { aargs[aanum++] = friendlyOpt; } if ( chaserefs ) { aargs[aanum++] = "-C"; } if ( ignore ) { aargs[aanum++] = "-i"; aargs[aanum++] = ignore; } aargs[aanum++] = "-f"; aargs[aanum++] = NULL; /* will hold the add data file */ aargs[aanum] = NULL; /* * generate the bind clients */ banum = 0; snprintf( bcmd, sizeof bcmd, "%s" LDAP_DIRSEP BINDCMD, progdir ); bargs[banum++] = bcmd; if ( !noinit ) { bargs[banum++] = "-I"; /* init on each bind */ } if ( uri ) { bargs[banum++] = "-H"; bargs[banum++] = uri; } else { bargs[banum++] = "-h"; bargs[banum++] = host; bargs[banum++] = "-p"; bargs[banum++] = port; } bargs[banum++] = "-l"; bargs[banum++] = bloops; bargs[banum++] = "-L"; bargs[banum++] = outerloops; #if 0 bargs[banum++] = "-r"; bargs[banum++] = retries; bargs[banum++] = "-t"; bargs[banum++] = delay; #endif if ( friendly ) { bargs[banum++] = friendlyOpt; } if ( chaserefs ) { bargs[banum++] = "-C"; } if ( ignore ) { bargs[banum++] = "-i"; bargs[banum++] = ignore; } if ( nextra ) { bargs[banum++] = "-B"; bargs_extra = &bargs[banum++]; } bargs[banum++] = "-D"; bargs[banum++] = NULL; bargs[banum++] = "-w"; bargs[banum++] = NULL; bargs[banum] = NULL; #define DOREQ(n,j) ((n) && ((maxkids > (n)) ? ((j) < maxkids ) : ((j) < (n)))) for ( j = 0; j < MAXREQS; j++ ) { /* search */ if ( DOREQ( snum, j ) ) { int jj = j % snum; int x = sanum - sextra_args; /* base */ if ( sbase[jj] != NULL ) { sargs[sanum - 7] = sbase[jj]; } else { sargs[sanum - 7] = slud[jj]->lud_dn; } /* scope */ if ( slud[jj] != NULL ) { sargs[sanum - 5] = (char *)ldap_pvt_scope2str( slud[jj]->lud_scope ); } else { sargs[sanum - 5] = "sub"; } /* filter */ if ( sreqs[jj] != NULL ) { sargs[sanum - 3] = sreqs[jj]; } else if ( slud[jj]->lud_filter != NULL ) { sargs[sanum - 3] = slud[jj]->lud_filter; } else { sargs[sanum - 3] = "(objectClass=*)"; } /* extras */ sargs[x] = NULL; /* attr */ if ( sattrs[jj] != NULL ) { sargs[x++] = "-a"; sargs[x++] = sattrs[jj]; } /* attrs */ if ( slud[jj] != NULL && slud[jj]->lud_attrs != NULL ) { int i; for ( i = 0; slud[jj]->lud_attrs[ i ] != NULL && x + i < MAXARGS - 1; i++ ) { sargs[x + i] = slud[jj]->lud_attrs[ i ]; } sargs[x + i] = NULL; } fork_child( scmd, sargs ); } /* read */ if ( DOREQ( rnum, j ) ) { int jj = j % rnum; int x = ranum - rextra_args; rargs[ranum - 3] = rreqs[jj]; if ( rflts[jj] != NULL ) { rargs[x++] = "-f"; rargs[x++] = rflts[jj]; } rargs[x] = NULL; fork_child( rcmd, rargs ); } /* rename */ if ( j < nnum ) { nargs[nanum - 1] = nreqs[j]; fork_child( ncmd, nargs ); } /* modify */ if ( j < mnum ) { margs[manum - 3] = mdn[j]; margs[manum - 1] = mreqs[j]; fork_child( mcmd, margs ); } /* add/delete */ if ( j < anum ) { aargs[aanum - 1] = afiles[j]; fork_child( acmd, aargs ); } /* bind */ if ( DOREQ( bnum, j ) ) { int jj = j % bnum; if ( nextra ) { int n = ((double)nextra)*rand()/(RAND_MAX + 1.0); extra_t *e; for ( e = extra; n-- > 0; e = e->next ) ; *bargs_extra = e->action; } if ( battrs[jj] != NULL ) { bargs[banum - 3] = manager ? manager : ""; bargs[banum - 1] = passwd ? passwd : ""; bargs[banum + 0] = "-b"; bargs[banum + 1] = breqs[jj]; bargs[banum + 2] = "-f"; bargs[banum + 3] = bcreds[jj]; bargs[banum + 4] = "-a"; bargs[banum + 5] = battrs[jj]; bargs[banum + 6] = NULL; } else { bargs[banum - 3] = breqs[jj]; bargs[banum - 1] = bcreds[jj]; bargs[banum] = NULL; } fork_child( bcmd, bargs ); bargs[banum] = NULL; } } wait4kids( -1 ); exit( EXIT_SUCCESS ); }
int main( int argc, char *argv[] ) { int rc; char *user = NULL; LDAP *ld = NULL; struct berval bv = {0, NULL}; BerElement *ber = NULL; int id, code = LDAP_OTHER; LDAPMessage *res; char *matcheddn = NULL, *text = NULL, **refs = NULL; char *retoid = NULL; struct berval *retdata = NULL; prog = lutil_progname( "ldappasswd", argc, argv ); /* LDAPv3 only */ protocol = LDAP_VERSION3; tool_args( argc, argv ); if( argc - optind > 1 ) { usage(); } else if ( argc - optind == 1 ) { user = strdup( argv[optind] ); } else { user = NULL; } if( oldpwfile ) { rc = lutil_get_filed_password( prog, &oldpw ); if( rc ) return EXIT_FAILURE; } if( want_oldpw && oldpw.bv_val == NULL ) { /* prompt for old password */ char *ckoldpw; oldpw.bv_val = strdup(getpassphrase("Old password: "******"Re-enter old password: "******"passwords do not match\n" ); return EXIT_FAILURE; } oldpw.bv_len = strlen( oldpw.bv_val ); } if( newpwfile ) { rc = lutil_get_filed_password( prog, &newpw ); if( rc ) return EXIT_FAILURE; } if( want_newpw && newpw.bv_val == NULL ) { /* prompt for new password */ char *cknewpw; newpw.bv_val = strdup(getpassphrase("New password: "******"Re-enter new password: "******"passwords do not match\n" ); return EXIT_FAILURE; } newpw.bv_len = strlen( newpw.bv_val ); } if( want_bindpw && passwd.bv_val == NULL ) { /* handle bind password */ if ( pw_file ) { rc = lutil_get_filed_password( pw_file, &passwd ); if( rc ) return EXIT_FAILURE; } else { passwd.bv_val = getpassphrase( "Enter LDAP Password: "******"ber_alloc_t" ); ldap_unbind( ld ); return EXIT_FAILURE; } ber_printf( ber, "{" /*}*/ ); if( user != NULL ) { ber_printf( ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user ); free(user); } if( oldpw.bv_val != NULL ) { ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw ); free(oldpw.bv_val); } if( newpw.bv_val != NULL ) { ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw ); free(newpw.bv_val); } ber_printf( ber, /*{*/ "N}" ); rc = ber_flatten2( ber, &bv, 0 ); if( rc < 0 ) { perror( "ber_flatten2" ); ldap_unbind( ld ); return EXIT_FAILURE; } } if ( not ) { rc = LDAP_SUCCESS; goto skip; } rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL, NULL, NULL, &id ); ber_free( ber, 1 ); if( rc != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_extended_operation" ); ldap_unbind( ld ); return EXIT_FAILURE; } rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res ); if ( rc < 0 ) { ldap_perror( ld, "ldappasswd: ldap_result" ); return rc; } rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 0 ); if( rc != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_parse_result" ); return rc; } rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); if( rc != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_parse_result" ); return rc; } if( retdata != NULL ) { ber_tag_t tag; char *s; ber = ber_init( retdata ); if( ber == NULL ) { perror( "ber_init" ); ldap_unbind( ld ); return EXIT_FAILURE; } /* we should check the tag */ tag = ber_scanf( ber, "{a}", &s); if( tag == LBER_ERROR ) { perror( "ber_scanf" ); } else { printf("New password: %s\n", s); free( s ); } ber_free( ber, 1 ); } if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) { printf( "Result: %s (%d)\n", ldap_err2string( code ), code ); if( text && *text ) { printf( "Additional info: %s\n", text ); } if( matcheddn && *matcheddn ) { printf( "Matched DN: %s\n", matcheddn ); } if( refs ) { int i; for( i=0; refs[i]; i++ ) { printf("Referral: %s\n", refs[i] ); } } } ber_memfree( text ); ber_memfree( matcheddn ); ber_memvfree( (void **) refs ); ber_memfree( retoid ); ber_bvfree( retdata ); skip: /* disconnect from server */ ldap_unbind (ld); return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; }
int main( int argc, char *argv[] ) { int rc; char *user = NULL; LDAP *ld = NULL; struct berval bv = {0, NULL}; BerElement *ber = NULL; int id, code = LDAP_OTHER; LDAPMessage *res; char *matcheddn = NULL, *text = NULL, **refs = NULL; char *retoid = NULL; struct berval *retdata = NULL; LDAPControl **ctrls = NULL; tool_init( TOOL_PASSWD ); prog = lutil_progname( "ldappasswd", argc, argv ); /* LDAPv3 only */ protocol = LDAP_VERSION3; tool_args( argc, argv ); if( argc - optind > 1 ) { usage(); } else if ( argc - optind == 1 ) { user = strdup( argv[optind] ); } else { user = NULL; } if( oldpwfile ) { rc = lutil_get_filed_password( oldpwfile, &oldpw ); if( rc ) { rc = EXIT_FAILURE; goto done; } } if( want_oldpw && oldpw.bv_val == NULL ) { /* prompt for old password */ char *ckoldpw; oldpw.bv_val = strdup(getpassphrase(_("Old password: "******"Re-enter old password: "******"passwords do not match\n") ); rc = EXIT_FAILURE; goto done; } oldpw.bv_len = strlen( oldpw.bv_val ); } if( newpwfile ) { rc = lutil_get_filed_password( newpwfile, &newpw ); if( rc ) { rc = EXIT_FAILURE; goto done; } } if( want_newpw && newpw.bv_val == NULL ) { /* prompt for new password */ char *cknewpw; newpw.bv_val = strdup(getpassphrase(_("New password: "******"Re-enter new password: "******"passwords do not match\n") ); rc = EXIT_FAILURE; goto done; } newpw.bv_len = strlen( newpw.bv_val ); } ld = tool_conn_setup( 0, 0 ); tool_bind( ld ); if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) { /* build the password modify request data */ ber = ber_alloc_t( LBER_USE_DER ); if( ber == NULL ) { perror( "ber_alloc_t" ); rc = EXIT_FAILURE; goto done; } ber_printf( ber, "{" /*}*/ ); if( user != NULL ) { ber_printf( ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user ); free(user); } if( oldpw.bv_val != NULL ) { ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &oldpw ); free(oldpw.bv_val); } if( newpw.bv_val != NULL ) { ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &newpw ); free(newpw.bv_val); } ber_printf( ber, /*{*/ "N}" ); rc = ber_flatten2( ber, &bv, 0 ); if( rc < 0 ) { perror( "ber_flatten2" ); rc = EXIT_FAILURE; goto done; } } if ( dont ) { rc = LDAP_SUCCESS; goto done; } tool_server_controls( ld, NULL, 0); rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL, NULL, NULL, &id ); ber_free( ber, 1 ); if( rc != LDAP_SUCCESS ) { tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } for ( ; ; ) { struct timeval tv; if ( tool_check_abandon( ld, id ) ) { tool_exit( ld, LDAP_CANCELLED ); } tv.tv_sec = 0; tv.tv_usec = 100000; rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); tool_exit( ld, rc ); } if ( rc != 0 ) { break; } } rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 0 ); if( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); if( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto done; } if( retdata != NULL ) { ber_tag_t tag; char *s; ber = ber_init( retdata ); if( ber == NULL ) { perror( "ber_init" ); rc = EXIT_FAILURE; goto done; } /* we should check the tag */ tag = ber_scanf( ber, "{a}", &s); if( tag == LBER_ERROR ) { perror( "ber_scanf" ); } else { printf(_("New password: %s\n"), s); ber_memfree( s ); } ber_free( ber, 1 ); } else if ( code == LDAP_SUCCESS && newpw.bv_val == NULL ) { tool_perror( "ldap_parse_extended_result", LDAP_DECODING_ERROR, " new password expected", NULL, NULL, NULL ); } if( verbose || code != LDAP_SUCCESS || ( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls ) { printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); if( text && *text ) { printf( _("Additional info: %s\n"), text ); } if( matcheddn && *matcheddn ) { printf( _("Matched DN: %s\n"), matcheddn ); } if( refs ) { int i; for( i=0; refs[i]; i++ ) { printf(_("Referral: %s\n"), refs[i] ); } } if( ctrls ) { tool_print_ctrls( ld, ctrls ); ldap_controls_free( ctrls ); } } ber_memfree( text ); ber_memfree( matcheddn ); ber_memvfree( (void **) refs ); ber_memfree( retoid ); ber_bvfree( retdata ); rc = ( code == LDAP_SUCCESS ) ? EXIT_SUCCESS : EXIT_FAILURE; done: /* disconnect from server */ tool_exit( ld, rc ); }
int main( int argc, char **argv ) { char buf[ 4096 ]; FILE *fp; LDAP *ld; int rc, retval; fp = NULL; tool_init( TOOL_DELETE ); prog = lutil_progname( "ldapdelete", argc, argv ); tool_args( argc, argv ); if ( infile != NULL ) { if (( fp = fopen( infile, "r" )) == NULL ) { perror( optarg ); exit( EXIT_FAILURE ); } } else { if ( optind >= argc ) { fp = stdin; } } ld = tool_conn_setup( 0, &private_conn_setup ); if ( pw_file || want_bindpw ) { if ( pw_file ) { rc = lutil_get_filed_password( pw_file, &passwd ); if( rc ) return EXIT_FAILURE; } else { passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") ); passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0; } } tool_bind( ld ); tool_server_controls( ld, NULL, 0 ); retval = rc = 0; if ( fp == NULL ) { for ( ; optind < argc; ++optind ) { rc = dodelete( ld, argv[ optind ] ); /* Stop on error and no -c option */ if( rc != 0 ) { retval = rc; if( contoper == 0 ) break; } } } else { while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) { buf[ strlen( buf ) - 1 ] = '\0'; /* remove trailing newline */ if ( *buf != '\0' ) { rc = dodelete( ld, buf ); if ( rc != 0 ) retval = rc; } } } tool_unbind( ld ); tool_destroy(); return retval; }
int main( int argc, char *argv[] ) { int rc; LDAP *ld = NULL; char *matcheddn = NULL, *text = NULL, **refs = NULL; char *retoid = NULL; struct berval *retdata = NULL; int id, code = 0; LDAPMessage *res; LDAPControl **ctrls = NULL; tool_init( TOOL_WHOAMI ); prog = lutil_progname( "ldapwhoami", argc, argv ); /* LDAPv3 only */ protocol = LDAP_VERSION3; tool_args( argc, argv ); if( argc - optind > 0 ) { usage(); } if ( pw_file || want_bindpw ) { if ( pw_file ) { rc = lutil_get_filed_password( pw_file, &passwd ); if( rc ) return EXIT_FAILURE; } else { passwd.bv_val = getpassphrase( _("Enter LDAP Password: "******"ldap_whoami", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } for ( ; ; ) { struct timeval tv; if ( tool_check_abandon( ld, id ) ) { return LDAP_CANCELLED; } tv.tv_sec = 0; tv.tv_usec = 100000; rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res ); if ( rc < 0 ) { tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL ); return rc; } if ( rc != 0 ) { break; } } rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, &ctrls, 0 ); if ( rc == LDAP_SUCCESS ) { rc = code; } if ( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs ); rc = EXIT_FAILURE; goto skip; } rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 ); if( rc != LDAP_SUCCESS ) { tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL ); rc = EXIT_FAILURE; goto skip; } if( retdata != NULL ) { if( retdata->bv_len == 0 ) { printf(_("anonymous\n") ); } else { printf("%s\n", retdata->bv_val ); } } skip: if ( verbose || ( code != LDAP_SUCCESS ) || matcheddn || text || refs || ctrls ) { printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code ); if( text && *text ) { printf( _("Additional info: %s\n"), text ); } if( matcheddn && *matcheddn ) { printf( _("Matched DN: %s\n"), matcheddn ); } if( refs ) { int i; for( i=0; refs[i]; i++ ) { printf(_("Referral: %s\n"), refs[i] ); } } if (ctrls) { tool_print_ctrls( ld, ctrls ); ldap_controls_free( ctrls ); } } ber_memfree( text ); ber_memfree( matcheddn ); ber_memvfree( (void **) refs ); ber_memfree( retoid ); ber_bvfree( retdata ); /* disconnect from server */ tool_unbind( ld ); tool_destroy(); return code == LDAP_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; }