1 /* $NetBSD: options.c,v 1.2 2020/08/11 13:15:37 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2020 The OpenLDAP Foundation. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted only as authorized by the OpenLDAP 11 * Public License. 12 * 13 * A copy of this license is available in the file LICENSE in the 14 * top-level directory of the distribution or, alternatively, at 15 * <http://www.OpenLDAP.org/license.html>. 16 */ 17 18 #include <sys/cdefs.h> 19 __RCSID("$NetBSD: options.c,v 1.2 2020/08/11 13:15:37 christos Exp $"); 20 21 #include "portable.h" 22 23 #include <ac/stdlib.h> 24 #include <ac/string.h> 25 #include <ac/stdarg.h> 26 #include "lber-int.h" 27 28 char ber_pvt_opt_on; /* used to get a non-NULL address for *_OPT_ON */ 29 30 struct lber_options ber_int_options = { 31 LBER_UNINITIALIZED, 0, 0 }; 32 33 static BerMemoryFunctions ber_int_memory_fns_datum; 34 35 int 36 ber_get_option( 37 void *item, 38 int option, 39 void *outvalue) 40 { 41 const BerElement *ber; 42 const Sockbuf *sb; 43 44 if(outvalue == NULL) { 45 /* no place to get to */ 46 ber_errno = LBER_ERROR_PARAM; 47 return LBER_OPT_ERROR; 48 } 49 50 if(item == NULL) { 51 switch ( option ) { 52 case LBER_OPT_BER_DEBUG: 53 * (int *) outvalue = ber_int_debug; 54 return LBER_OPT_SUCCESS; 55 56 case LBER_OPT_MEMORY_INUSE: 57 /* The memory inuse is a global variable on kernal implementations. 58 * This means that memory debug is shared by all LDAP processes 59 * so for this variable to have much meaning, only one LDAP process 60 * should be running and memory inuse should be initialized to zero 61 * using the lber_set_option() function during startup. 62 * The counter is not accurate for multithreaded ldap applications. 63 */ 64 #ifdef LDAP_MEMORY_DEBUG 65 * (int *) outvalue = ber_int_meminuse; 66 return LBER_OPT_SUCCESS; 67 #else 68 return LBER_OPT_ERROR; 69 #endif 70 71 case LBER_OPT_LOG_PRINT_FILE: 72 *((FILE**)outvalue) = (FILE*)ber_pvt_err_file; 73 return LBER_OPT_SUCCESS; 74 } 75 76 ber_errno = LBER_ERROR_PARAM; 77 return LBER_OPT_ERROR; 78 } 79 80 ber = item; 81 sb = item; 82 83 switch(option) { 84 case LBER_OPT_BER_OPTIONS: 85 assert( LBER_VALID( ber ) ); 86 * (int *) outvalue = ber->ber_options; 87 return LBER_OPT_SUCCESS; 88 89 case LBER_OPT_BER_DEBUG: 90 assert( LBER_VALID( ber ) ); 91 * (int *) outvalue = ber->ber_debug; 92 return LBER_OPT_SUCCESS; 93 94 case LBER_OPT_BER_REMAINING_BYTES: 95 assert( LBER_VALID( ber ) ); 96 *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber); 97 return LBER_OPT_SUCCESS; 98 99 case LBER_OPT_BER_TOTAL_BYTES: 100 assert( LBER_VALID( ber ) ); 101 *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber); 102 return LBER_OPT_SUCCESS; 103 104 case LBER_OPT_BER_BYTES_TO_WRITE: 105 assert( LBER_VALID( ber ) ); 106 *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber); 107 return LBER_OPT_SUCCESS; 108 109 case LBER_OPT_BER_MEMCTX: 110 assert( LBER_VALID( ber ) ); 111 *((void **) outvalue) = ber->ber_memctx; 112 return LBER_OPT_SUCCESS; 113 114 default: 115 /* bad param */ 116 ber_errno = LBER_ERROR_PARAM; 117 break; 118 } 119 120 return LBER_OPT_ERROR; 121 } 122 123 int 124 ber_set_option( 125 void *item, 126 int option, 127 LDAP_CONST void *invalue) 128 { 129 BerElement *ber; 130 Sockbuf *sb; 131 132 if(invalue == NULL) { 133 /* no place to set from */ 134 ber_errno = LBER_ERROR_PARAM; 135 return LBER_OPT_ERROR; 136 } 137 138 if(item == NULL) { 139 switch ( option ) { 140 case LBER_OPT_BER_DEBUG: 141 ber_int_debug = * (const int *) invalue; 142 return LBER_OPT_SUCCESS; 143 144 case LBER_OPT_LOG_PRINT_FN: 145 ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue; 146 return LBER_OPT_SUCCESS; 147 148 case LBER_OPT_LOG_PRINT_FILE: 149 ber_pvt_err_file = (void *) invalue; 150 return LBER_OPT_SUCCESS; 151 152 case LBER_OPT_MEMORY_INUSE: 153 /* The memory inuse is a global variable on kernal implementations. 154 * This means that memory debug is shared by all LDAP processes 155 * so for this variable to have much meaning, only one LDAP process 156 * should be running and memory inuse should be initialized to zero 157 * using the lber_set_option() function during startup. 158 * The counter is not accurate for multithreaded applications. 159 */ 160 #ifdef LDAP_MEMORY_DEBUG 161 ber_int_meminuse = * (int *) invalue; 162 return LBER_OPT_SUCCESS; 163 #else 164 return LBER_OPT_ERROR; 165 #endif 166 case LBER_OPT_MEMORY_FNS: 167 if ( ber_int_memory_fns == NULL ) 168 { 169 const BerMemoryFunctions *f = 170 (const BerMemoryFunctions *) invalue; 171 /* make sure all functions are provided */ 172 if(!( f->bmf_malloc && f->bmf_calloc 173 && f->bmf_realloc && f->bmf_free )) 174 { 175 ber_errno = LBER_ERROR_PARAM; 176 return LBER_OPT_ERROR; 177 } 178 179 ber_int_memory_fns = &ber_int_memory_fns_datum; 180 181 AC_MEMCPY(ber_int_memory_fns, f, 182 sizeof(BerMemoryFunctions)); 183 184 return LBER_OPT_SUCCESS; 185 } 186 break; 187 188 case LBER_OPT_LOG_PROC: 189 ber_int_log_proc = (BER_LOG_FN)invalue; 190 return LBER_OPT_SUCCESS; 191 } 192 193 ber_errno = LBER_ERROR_PARAM; 194 return LBER_OPT_ERROR; 195 } 196 197 ber = item; 198 sb = item; 199 200 switch(option) { 201 case LBER_OPT_BER_OPTIONS: 202 assert( LBER_VALID( ber ) ); 203 ber->ber_options = * (const int *) invalue; 204 return LBER_OPT_SUCCESS; 205 206 case LBER_OPT_BER_DEBUG: 207 assert( LBER_VALID( ber ) ); 208 ber->ber_debug = * (const int *) invalue; 209 return LBER_OPT_SUCCESS; 210 211 case LBER_OPT_BER_REMAINING_BYTES: 212 assert( LBER_VALID( ber ) ); 213 ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue]; 214 return LBER_OPT_SUCCESS; 215 216 case LBER_OPT_BER_TOTAL_BYTES: 217 assert( LBER_VALID( ber ) ); 218 ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue]; 219 return LBER_OPT_SUCCESS; 220 221 case LBER_OPT_BER_BYTES_TO_WRITE: 222 assert( LBER_VALID( ber ) ); 223 ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue]; 224 return LBER_OPT_SUCCESS; 225 226 case LBER_OPT_BER_MEMCTX: 227 assert( LBER_VALID( ber ) ); 228 ber->ber_memctx = *(void **)invalue; 229 return LBER_OPT_SUCCESS; 230 231 default: 232 /* bad param */ 233 ber_errno = LBER_ERROR_PARAM; 234 break; 235 } 236 237 return LBER_OPT_ERROR; 238 } 239