SMFList_T *smf_settings_group_get_list(SMFSettings_T *settings, char *group_name, char *key) { char *tmp = NULL; char *s = NULL; char **sl = NULL; char **p = NULL; SMFList_T *list = NULL; assert(settings); assert(group_name); assert(key); if (smf_list_new(&list,smf_internal_string_list_destroy)!=0) return NULL; asprintf(&tmp,"%s:%s",group_name,key); s = smf_dict_get(settings->groups,tmp); free(tmp); sl = smf_core_strsplit(s, ";", NULL); p = sl; while(*p != NULL) { tmp = smf_core_strstrip(*p); smf_list_append(list, tmp); p++; } free(sl); return list; }
SMFSession_T *smf_session_new(void) { SMFSession_T *session; TRACE(TRACE_DEBUG,"initialize session data"); session = (SMFSession_T *)calloc((size_t)1, sizeof(SMFSession_T)); if (smf_list_new(&session->local_users,smf_internal_user_data_list_destroy)!=0) { free(session); return NULL; } session->helo = NULL; session->xforward_addr = NULL; session->message_file = NULL; session->message_size = 0; session->response_msg = NULL; session->envelope = smf_envelope_new(); session->id = smf_internal_generate_sid(); TRACE(TRACE_INFO,"start new session SID %s",session->id); return session; }
int main (int argc, char const *argv[]) { SMFList_T *l; char *out; char *data; char *pop; SMFListElem_T *e; printf("Start SMFList_T tests...\n"); printf("* testing smf_list_new()...\t\t\t\t"); if (smf_list_new(&l,list_destroy)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_append()...\t\t\t\t"); if (smf_list_append(l,TEST_STRING1)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_data()...\t\t\t\t"); out = (char *)smf_list_data(smf_list_head(l)); if (strcmp(TEST_STRING1,out)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_size()...\t\t\t\t"); if (smf_list_size(l)!=1) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_head()...\t\t\t\t"); e = smf_list_head(l); if (smf_list_is_head(e)!=1) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_insert_next()...\t\t\t"); if (smf_list_insert_next(l,e,TEST_STRING2)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_tail()...\t\t\t\t"); e = smf_list_tail(l); if (smf_list_is_tail(e)!=1) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_data()...\t\t\t\t"); out = (char *)smf_list_data(e); if (strcmp(TEST_STRING2,out)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_insert_prev()...\t\t\t"); if (smf_list_insert_prev(l,e,TEST_STRING3)!=0) { printf("failed\n"); return -1; } out = (char *)smf_list_data(smf_list_prev(e)); if (strcmp(TEST_STRING3,out)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_prepend()...\t\t\t\t"); if (smf_list_prepend(l,TEST_STRING4)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_map()...\t\t\t\t"); smf_list_map(l,list_char_printer,NULL); printf("passed\n"); printf("* testing smf_list_remove()...\t\t\t\t"); e = smf_list_head(l); if (smf_list_remove(l,e,(void *)&data)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_pop_head()...\t\t\t"); pop = smf_list_pop_head(l); assert(pop); if (strcmp(pop,TEST_STRING1)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_pop_tail()...\t\t\t"); pop = smf_list_pop_tail(l); assert(pop); if (strcmp(pop,TEST_STRING2)!=0) { printf("failed\n"); return -1; } printf("passed\n"); printf("* testing smf_list_free()...\t\t\t\t"); if (smf_list_free(l)!=0) { printf("failed\n"); return -1; } printf("passed\n"); return 0; }
SMFSettings_T *smf_settings_new(void) { SMFSettings_T *settings = NULL; if (!(settings = (SMFSettings_T *)calloc(1, sizeof(SMFSettings_T)))) return NULL; settings->debug = 0; settings->config_file = NULL; settings->queue_dir = NULL; settings->engine = NULL; if (smf_list_new(&settings->modules, _mod_list_destroy) != 0) { TRACE(TRACE_ERR,"failed to allocate space for settings->modules"); free(settings); return NULL; } settings->nexthop = NULL; settings->nexthop_fail_msg = NULL; settings->backend = NULL; settings->backend_connection = NULL; settings->lib_dir = NULL; settings->pid_file = NULL; settings->bind_ip = NULL; settings->bind_port = 10025; settings->listen_backlog = 511; settings->foreground = 0; settings->user = NULL; settings->group = NULL; settings->max_childs = 10; settings->spare_childs = 2; settings->lookup_persistent = 0; settings->syslog_facility = LOG_MAIL; settings->smtp_codes = smf_dict_new(); settings->smtpd_timeout = 300; settings->sql_driver = NULL; settings->sql_name = NULL; if (smf_list_new(&settings->sql_host, smf_internal_string_list_destroy) != 0) { TRACE(TRACE_ERR,"failed to allocate space for settings->sql_host"); smf_list_free(settings->modules); free(settings); return NULL; } settings->sql_user = NULL; settings->sql_pass = NULL; settings->sql_user_query = NULL; settings->sql_encoding = NULL; settings->ldap_uri = NULL; if (smf_list_new(&settings->ldap_host, smf_internal_string_list_destroy) != 0) { TRACE(TRACE_ERR,"failed to allocate space for settings->ldap_host"); smf_list_free(settings->modules); smf_list_free(settings->sql_host); free(settings); return NULL; } settings->ldap_binddn = NULL; settings->ldap_bindpw = NULL; settings->ldap_base = NULL; settings->ldap_scope = NULL; settings->ldap_referrals = 0; settings->ldap_user_query = NULL; settings->module_fail = 3; settings->nexthop_fail_code = 451; settings->add_header = 1; settings->max_size = 0; settings->tls = 0; settings->sql_max_connections = 3; settings->sql_port = 0; settings->ldap_port = 0; settings->lookup_connection = NULL; settings->groups = smf_dict_new(); return settings; }
void _set_config_value(SMFSettings_T **settings, char *section, char *key, char *val) { char **sl = NULL; char **p = NULL; char *s = NULL; char *tmp = NULL; int i; if (val==NULL || strlen(val) == 0) return; /** global section **/ if (strcmp(section,"global")==0) { /** [global]debug **/ if (strcmp(key,"debug")==0) { (*settings)->debug = _get_boolean(val); configure_debug((*settings)->debug); /** [global]queue_dir **/ } else if (strcmp(key,"queue_dir")==0) { if ((*settings)->queue_dir!=NULL) free((*settings)->queue_dir); (*settings)->queue_dir = strdup(val); /** [global]modules **/ } else if (strcmp(key, "modules")==0) { if (smf_list_size((*settings)->modules) > 0) { if (smf_list_free((*settings)->modules)!=0) TRACE(TRACE_ERR,"failed to free modules list"); else if (smf_list_new(&((*settings)->modules),_mod_list_destroy)!=0) TRACE(TRACE_ERR,"failed to create modules list"); } sl = _get_list(val); p = sl; while(*p != NULL) { s = smf_core_strstrip(*p); smf_settings_add_module((*settings),s); free(s); p++; } free(sl); /** [global]engine **/ } else if (strcmp(key,"engine")==0) { if ((*settings)->engine!=NULL) free((*settings)->engine); (*settings)->engine = strdup(val); /** [global]module_fail **/ } else if (strcmp(key,"module_fail")==0) { i = _get_integer(val); /** check allowed values... */ if (i==1 || i==2 || i==3) (*settings)->module_fail = i; /** [global]nexthop **/ } else if (strcmp(key,"nexthop")==0) { if ((*settings)->nexthop!=NULL) free((*settings)->nexthop); (*settings)->nexthop = strdup(val); /** [global]backend **/ } else if (strcmp(key,"backend")==0) { if ((*settings)->backend!=NULL) free((*settings)->backend); if ((strcmp(val,"sql")==0)||(strcmp(val,"ldap")==0)) (*settings)->backend = strdup(val); /** [global]backend_connection **/ } else if (strcmp(key,"backend_connection")==0) { if ((*settings)->backend_connection!=NULL) free((*settings)->backend_connection); if ((strcmp(val,"balance")==0)||(strcmp(val,"failover")==0)) (*settings)->backend_connection = strdup(val); /** [global]add_header **/ } else if (strcmp(key,"add_header")==0) { (*settings)->add_header = _get_boolean(val); /** [global]max_size **/ } else if (strcmp(key,"max_size")==0) { (*settings)->max_size = _get_integer(val); /** [global]tls_enable **/ } else if (strcmp(key,"tls_enable")==0) { i = _get_integer(val); /** check allowed values... */ if (i==0 || i==1 || i==2) (*settings)->tls = i; /** [global]lib_dir **/ } else if (strcmp(key,"lib_dir")==0) { if ((*settings)->lib_dir!=NULL) free((*settings)->lib_dir); (*settings)->lib_dir = strdup(val); /** [global]pid_file **/ } else if (strcmp(key,"pid_file")==0) { if ((*settings)->pid_file!=NULL) free((*settings)->pid_file); (*settings)->pid_file = strdup(val); /** [global]bind_ip **/ } else if (strcmp(key,"bind_ip")==0) { if ((*settings)->bind_ip!=NULL) free((*settings)->bind_ip); (*settings)->bind_ip = strdup(val); /** [global]bind_port **/ } else if (strcmp(key,"bind_port")==0) { (*settings)->bind_port = _get_integer(val); /** [global]listen_backlog **/ } else if (strcmp(key,"listen_backlog")==0) { (*settings)->listen_backlog = _get_integer(val); /** [global]foreground **/ } else if (strcmp(key,"foreground")==0) { int fg = _get_boolean(val); (*settings)->foreground = fg; configure_trace_destination(fg ? TRACE_DEST_STDERR : TRACE_DEST_SYSLOG); /** [global]user **/ } else if (strcmp(key,"user")==0) { if ((*settings)->user!=NULL) free((*settings)->user); (*settings)->user = strdup(val); /** [global]group **/ } else if (strcmp(key,"group")==0) { if ((*settings)->group!=NULL) free((*settings)->group); (*settings)->group = strdup(val); /** [global]max_childs **/ } else if (strcmp(key,"max_childs")==0) { (*settings)->max_childs = _get_integer(val); /** [global]spare_childs **/ } else if (strcmp(key,"spare_childs")==0) { (*settings)->spare_childs = _get_integer(val); /** [global]lookup_persistent **/ } else if (strcmp(key,"lookup_persistent")==0) { (*settings)->lookup_persistent = _get_boolean(val); } else if (strcmp(key,"syslog_facility")==0) { smf_settings_set_syslog_facility((*settings), val); } /** sql section **/ } else if (strcmp(section,"sql")==0) { /** [sql]driver **/ if (strcmp(key,"driver")==0) { if ((*settings)->sql_driver != NULL) free((*settings)->sql_driver); if ((strcmp(val,"mysql")==0)||(strcmp(val,"pgsql")==0)||(strcmp(val,"sqlite")==0)) (*settings)->sql_driver = strdup(val); /** [sql]name **/ } else if (strcmp(key, "name")==0) { if ((*settings)->sql_name != NULL) free((*settings)->sql_name); (*settings)->sql_name = strdup(val); /** [sql]host **/ } else if (strcmp(key, "host")==0) { if (smf_list_size((*settings)->sql_host) > 0) { if (smf_list_free((*settings)->sql_host)!=0) TRACE(TRACE_ERR,"failed to free host list"); else if (smf_list_new(&((*settings)->sql_host),smf_internal_string_list_destroy)!=0) TRACE(TRACE_ERR,"failed to create host list"); } sl = _get_list(val); p = sl; while(*p != NULL) { s = smf_core_strstrip(*p); smf_list_append((*settings)->sql_host, s); p++; } free(sl); /** [sql]user **/ } else if (strcmp(key, "user")==0) { if ((*settings)->sql_user != NULL) free((*settings)->sql_user); (*settings)->sql_user = strdup(val); /** [sql]pass **/ } else if (strcmp(key, "pass")==0) { if ((*settings)->sql_pass != NULL) free((*settings)->sql_pass); (*settings)->sql_pass = strdup(val); /** [sql]user_query **/ } else if (strcmp(key, "user_query")==0) { if ((*settings)->sql_user_query != NULL) free((*settings)->sql_user_query); (*settings)->sql_user_query = strdup(val); /** [sql]encoding **/ } else if (strcmp(key, "encoding")==0) { if ((*settings)->sql_encoding != NULL) free((*settings)->sql_encoding); (*settings)->sql_encoding = strdup(val); /** [sql]max_connections **/ } else if (strcmp(key,"max_connections")==0) { (*settings)->sql_max_connections = _get_integer(val); /** [sql]port **/ } else if (strcmp(key,"port")==0) { (*settings)->sql_port = _get_integer(val); } /** ldap section **/ } else if (strcmp(section,"ldap")==0) { /** [ldap]uri **/ if (strcmp(key, "uri")==0) { if ((*settings)->ldap_uri != NULL) free((*settings)->ldap_uri); (*settings)->ldap_uri = strdup(val); /** [ldap]host **/ } else if (strcmp(key, "host")==0) { if (smf_list_size((*settings)->ldap_host) > 0) { if (smf_list_free((*settings)->ldap_host)!=0) TRACE(TRACE_ERR,"failed to free host list"); else if (smf_list_new(&((*settings)->ldap_host),smf_internal_string_list_destroy)!=0) TRACE(TRACE_ERR,"failed to create host list"); } sl = _get_list(val); p = sl; while(*p != NULL) { s = smf_core_strstrip(*p); smf_list_append((*settings)->ldap_host, s); p++; } free(sl); /** [ldap]port **/ } else if (strcmp(key,"port")==0) { (*settings)->ldap_port = _get_integer(val); /** [ldap]binddn **/ } else if (strcmp(key, "binddn")==0) { if ((*settings)->ldap_binddn != NULL) free((*settings)->ldap_binddn); (*settings)->ldap_binddn = strdup(val); /** [ldap]bindpw **/ } else if (strcmp(key, "bindpw")==0) { if ((*settings)->ldap_bindpw != NULL) free((*settings)->ldap_bindpw); (*settings)->ldap_bindpw = strdup(val); /** [ldap]base **/ } else if (strcmp(key, "base")==0) { if ((*settings)->ldap_base != NULL) free((*settings)->ldap_base); (*settings)->ldap_base = strdup(val); /** [ldap]user_query **/ } else if (strcmp(key, "user_query")==0) { if ((*settings)->ldap_user_query != NULL) free((*settings)->ldap_user_query); (*settings)->ldap_user_query = strdup(val); /** [ldap]scope **/ } else if (strcmp(key, "scope")==0) { if ((*settings)->ldap_scope != NULL) free((*settings)->ldap_scope); (*settings)->ldap_scope = strdup(val); /** [ldap]referrals **/ } else if (strcmp(key,"referrals")==0) { (*settings)->ldap_referrals = _get_boolean(val); } /** smtpd section **/ } else if (strcmp(section,"smtpd")==0) { /** [smtpd]nexthop_fail_msg **/ if (strcmp(key, "nexthop_fail_msg")==0) { if ((*settings)->nexthop_fail_msg != NULL) free((*settings)->nexthop_fail_msg); (*settings)->nexthop_fail_msg = strdup(val); /** [smtpd]nexthop_fail_code **/ } else if (strcmp(key, "nexthop_fail_code")==0) { (*settings)->nexthop_fail_code = _get_integer(val); } else if (strcmp(key, "smtpd_timeout")==0) { (*settings)->smtpd_timeout = _get_integer(val); /** smtp code **/ } else { i = _get_integer(key); if (i >= 250 && i < 600) { smf_dict_set((*settings)->smtp_codes,key,val); } } /** custom sections */ } else { asprintf(&tmp,"%s:%s",section,key); smf_dict_set((*settings)->groups,tmp,val); } }