📄 slapi_utils.c
字号:
void slapi_send_ldap_result( Slapi_PBlock *pb, int err, char *matched, char *text, int nentries, struct berval **urls ) { SlapReply *rs; PBLOCK_ASSERT_OP( pb, 0 ); rs = pb->pb_rs; rs->sr_err = err; rs->sr_matched = matched; rs->sr_text = text; rs->sr_ref = NULL; if ( err == LDAP_SASL_BIND_IN_PROGRESS ) { send_ldap_sasl( pb->pb_op, rs ); } else if ( rs->sr_rspoid != NULL ) { send_ldap_extended( pb->pb_op, rs ); } else { if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH ) rs->sr_nentries = nentries; send_ldap_result( pb->pb_op, rs ); }}int slapi_send_ldap_search_entry( Slapi_PBlock *pb, Slapi_Entry *e, LDAPControl **ectrls, char **attrs, int attrsonly ){ SlapReply rs = { REP_SEARCH }; int i = 0; AttributeName *an = NULL; const char *text; int rc; assert( pb->pb_op != NULL ); if ( attrs != NULL ) { for ( i = 0; attrs[ i ] != NULL; i++ ) { ; /* empty */ } } if ( i ) { an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) ); for ( i = 0; attrs[i] != NULL; i++ ) { an[i].an_name.bv_val = attrs[i]; an[i].an_name.bv_len = strlen( attrs[i] ); an[i].an_desc = NULL; rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ); if ( rs.sr_err != LDAP_SUCCESS) { slapi_ch_free( (void **)&an ); return -1; } } an[i].an_name.bv_len = 0; an[i].an_name.bv_val = NULL; } rs.sr_err = LDAP_SUCCESS; rs.sr_matched = NULL; rs.sr_text = NULL; rs.sr_ref = NULL; rs.sr_ctrls = ectrls; rs.sr_attrs = an; rs.sr_operational_attrs = NULL; rs.sr_entry = e; rs.sr_v2ref = NULL; rs.sr_flags = 0; rc = send_search_entry( pb->pb_op, &rs ); slapi_ch_free( (void **)&an ); return rc;}int slapi_send_ldap_search_reference( Slapi_PBlock *pb, Slapi_Entry *e, struct berval **references, LDAPControl **ectrls, struct berval **v2refs ){ SlapReply rs = { REP_SEARCHREF }; int rc; rs.sr_err = LDAP_SUCCESS; rs.sr_matched = NULL; rs.sr_text = NULL; rc = bvptr2obj( references, &rs.sr_ref ); if ( rc != LDAP_SUCCESS ) { return rc; } rs.sr_ctrls = ectrls; rs.sr_attrs = NULL; rs.sr_operational_attrs = NULL; rs.sr_entry = e; if ( v2refs != NULL ) { rc = bvptr2obj( v2refs, &rs.sr_v2ref ); if ( rc != LDAP_SUCCESS ) { slapi_ch_free( (void **)&rs.sr_ref ); return rc; } } else { rs.sr_v2ref = NULL; } rc = send_search_reference( pb->pb_op, &rs ); slapi_ch_free( (void **)&rs.sr_ref ); slapi_ch_free( (void **)&rs.sr_v2ref ); return rc;}Slapi_Filter *slapi_str2filter( char *str ) { return str2filter( str );}void slapi_filter_free( Slapi_Filter *f, int recurse ) { filter_free( f );}Slapi_Filter *slapi_filter_dup( Slapi_Filter *filter ){ return filter_dup( filter, NULL );}int slapi_filter_get_choice( Slapi_Filter *f ){ int rc; if ( f != NULL ) { rc = f->f_choice; } else { rc = 0; } return rc;}int slapi_filter_get_ava( Slapi_Filter *f, char **type, struct berval **bval ){ int ftype; int rc = LDAP_SUCCESS; assert( type != NULL ); assert( bval != NULL ); *type = NULL; *bval = NULL; ftype = f->f_choice; if ( ftype == LDAP_FILTER_EQUALITY || ftype == LDAP_FILTER_GE || ftype == LDAP_FILTER_LE || ftype == LDAP_FILTER_APPROX ) { /* * According to the SLAPI Reference Manual these are * not duplicated. */ *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val; *bval = &f->f_un.f_un_ava->aa_value; } else { /* filter type not supported */ rc = -1; } return rc;}Slapi_Filter *slapi_filter_list_first( Slapi_Filter *f ){ int ftype; if ( f == NULL ) { return NULL; } ftype = f->f_choice; if ( ftype == LDAP_FILTER_AND || ftype == LDAP_FILTER_OR || ftype == LDAP_FILTER_NOT ) { return (Slapi_Filter *)f->f_list; } else { return NULL; }}Slapi_Filter *slapi_filter_list_next( Slapi_Filter *f, Slapi_Filter *fprev ){ int ftype; if ( f == NULL ) { return NULL; } ftype = f->f_choice; if ( ftype == LDAP_FILTER_AND || ftype == LDAP_FILTER_OR || ftype == LDAP_FILTER_NOT ) { return fprev->f_next; } return NULL;}intslapi_filter_get_attribute_type( Slapi_Filter *f, char **type ){ if ( f == NULL ) { return -1; } switch ( f->f_choice ) { case LDAP_FILTER_GE: case LDAP_FILTER_LE: case LDAP_FILTER_EQUALITY: case LDAP_FILTER_APPROX: *type = f->f_av_desc->ad_cname.bv_val; break; case LDAP_FILTER_SUBSTRINGS: *type = f->f_sub_desc->ad_cname.bv_val; break; case LDAP_FILTER_PRESENT: *type = f->f_desc->ad_cname.bv_val; break; case LDAP_FILTER_EXT: *type = f->f_mr_desc->ad_cname.bv_val; break; default: /* Complex filters need not apply. */ *type = NULL; return -1; } return 0;}intslapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type ){ AttributeDescription **adp, *ad = NULL; const char *text; int rc; if ( f == NULL ) { return -1; } switch ( f->f_choice ) { case LDAP_FILTER_GE: case LDAP_FILTER_LE: case LDAP_FILTER_EQUALITY: case LDAP_FILTER_APPROX: adp = &f->f_av_desc; break; case LDAP_FILTER_SUBSTRINGS: adp = &f->f_sub_desc; break; case LDAP_FILTER_PRESENT: adp = &f->f_desc; break; case LDAP_FILTER_EXT: adp = &f->f_mr_desc; break; default: /* Complex filters need not apply. */ return -1; } rc = slap_str2ad( type, &ad, &text ); if ( rc == LDAP_SUCCESS ) *adp = ad; return ( rc == LDAP_SUCCESS ) ? 0 : -1;}intslapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial, char ***any, char **final ){ int i; if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) { return -1; } /* * The caller shouldn't free but we can't return an * array of char *s from an array of bervals without * allocating memory, so we may as well be consistent. * XXX */ *type = f->f_sub_desc->ad_cname.bv_val; *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL; if ( f->f_sub_any != NULL ) { for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) ; *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) ); for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) { (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val); } (*any)[i] = NULL; } else { *any = NULL; } *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL; return 0;}Slapi_Filter *slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 ){ Slapi_Filter *f = NULL; if ( ftype == LDAP_FILTER_AND || ftype == LDAP_FILTER_OR || ftype == LDAP_FILTER_NOT ) { f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) ); f->f_choice = ftype; f->f_list = f1; f->f_list->f_next = f2; f->f_next = NULL; } return f;}intslapi_x_filter_append( int ftype, Slapi_Filter **pContainingFilter, /* NULL on first call */ Slapi_Filter **pNextFilter, Slapi_Filter *filterToAppend ){ if ( ftype == LDAP_FILTER_AND || ftype == LDAP_FILTER_OR || ftype == LDAP_FILTER_NOT ) { if ( *pContainingFilter == NULL ) { *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) ); (*pContainingFilter)->f_choice = ftype; (*pContainingFilter)->f_list = filterToAppend; (*pContainingFilter)->f_next = NULL; } else { if ( (*pContainingFilter)->f_choice != ftype ) { /* Sanity check */ return -1; } (*pNextFilter)->f_next = filterToAppend; } *pNextFilter = filterToAppend; return 0; } return -1;}intslapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f, int verify_access ){ Operation *op; int rc; if ( f == NULL ) { /* spec says return zero if no filter. */ return 0; } if ( verify_access ) { op = pb->pb_op; if ( op == NULL ) return LDAP_PARAM_ERROR; } else { op = NULL; } /* * According to acl.c it is safe to call test_filter() with * NULL arguments... */ rc = test_filter( op, e, f ); switch (rc) { case LDAP_COMPARE_TRUE: rc = 0; break; case LDAP_COMPARE_FALSE: break; case SLAPD_COMPARE_UNDEFINED: rc = LDAP_OTHER; break; case LDAP_PROTOCOL_ERROR: /* filter type unknown: spec says return -1 */ rc = -1; break; } return rc;}intslapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f){ return slapi_filter_test( NULL, e, f, 0 );}intslapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code ){ switch ( f->f_choice ) { case LDAP_FILTER_AND: case LDAP_FILTER_NOT: case LDAP_FILTER_OR: { int rc; /* * FIXME: altering f; should we use a temporary? */ for ( f = f->f_list; f != NULL; f = f->f_next ) { rc = slapi_filter_apply( f, fn, arg, error_code ); if ( rc != 0 ) { return rc; } if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) { break; } } break; } case LDAP_FILTER_EQUALITY: case LDAP_FILTER_SUBSTRINGS: case LDAP_FILTER_GE: case LDAP_FILTER_LE: case LDAP_FILTER_PRESENT: case LDAP_FILTER_APPROX: case LDAP_FILTER_EXT: *error_code = fn( f, arg ); break; default: *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE; } if ( *error_code == SLAPI_FILTER_SCAN_NOMORE || *error_code == SLAPI_FILTER_SCAN_CONTINUE ) { return 0; } return -1;}int slapi_pw_find( struct berval **vals, struct berval *v ) { /* * FIXME: what's the point? */ return 1;}#define MAX_HOSTNAME 512char *slapi_get_hostname( void ) { char *hn = NULL; static int been_here = 0; static char *static_hn = NULL; ldap_pvt_thread_mutex_lock( &slapi_hn_mutex ); if ( !been_here ) { static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME ); if ( static_hn == NULL) { slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname", "Cannot allocate memory for hostname\n" ); static_hn = NULL; ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); return hn; } else { if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) { slapi_log_error( SLAPI_LOG_FATAL, "SLAPI", "can't get hostname\n" ); slapi_ch_free( (void **)&static_hn ); static_hn = NULL; ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); return hn; } else { been_here = 1; } } } ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex ); hn = ch_strdup( static_hn ); return hn;}/* * FIXME: this should go in an appropriate header ... */extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );int slapi_log_error( int severity, char *subsystem, char *fmt, ... ) { int rc = LDAP_SUCCESS; va_list arglist; va_start( arglist, fmt ); rc = slapi_int_log_error( severity, subsystem, fmt, arglist ); va_end( arglist ); return rc;}unsigned longslapi_timer_current_time( void ) { static int first_time = 1;#if !defined (_WIN32) struct timeval now; unsigned long ret; ldap_pvt_thread_mutex_lock( &slapi_time_mutex ); if (first_time) { first_time = 0; gettimeofday( &base_time, NULL ); } gettimeofday( &now, NULL ); ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 + (now.tv_usec - base_time.tv_usec); ldap_pvt_thread_mutex_unlock( &slapi_time_mutex ); return ret; /* * Ain't it better? return (slap_get_time() - starttime) * 1000000; */#else /* _WIN32 */ LARGE_INTEGER now; if ( first_time ) { first_time = 0; performance_counter_present = QueryPerformanceCounter( &base_time ); QueryPerformanceFrequency( &performance_freq ); } if ( !performance_counter_present ) return 0; QueryPerformanceCounter( &now ); return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;#endif /* _WIN32 */}/* * FIXME ? */unsigned longslapi_timer_get_time( char *label ) { unsigned long start = slapi_timer_current_time(); printf("%10ld %10d usec %s\n", start, 0, label); return start;}/* * FIXME ? */voidslapi_timer_elapsed_time( char *label, unsigned long start ) { unsigned long stop = slapi_timer_current_time(); printf ("%10ld %10ld usec %s\n", stop, stop - start, label);}voidslapi_free_search_results_internal( Slapi_PBlock *pb ) { Slapi_Entry **entries; int k = 0, nEnt = 0; slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt ); slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries ); if ( nEnt == 0 || entries == NULL ) { return; } for ( k = 0; k < nEnt; k++ ) { slapi_entry_free( entries[k] ); entries[k] = NULL; } slapi_ch_free( (void **)&entries );}int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL ){ if ( pb == NULL ) return LDAP_PARAM_ERROR; if ( pb->pb_conn == NULL ) return LDAP_PARAM_ERROR;#ifdef HAVE_TLS *isSSL = pb->pb_conn->c_is_tls;#else *isSSL = 0;#endif return LDAP_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -