diff --git a/util.c b/util.c index dd9e01e..688a108 100644 --- a/util.c +++ b/util.c @@ -139,10 +139,8 @@ check_user_token_mysql(const char *mysql_server, int row_count; if(mysql_library_init(0, NULL, NULL)){ - if(verbose){ - D (debug_file, "could not initialize MySQL client library"); - } - + if(verbose) + D (debug_file, "could not initialize MySQL client library"); return retval; } @@ -164,12 +162,13 @@ check_user_token_mysql(const char *mysql_server, if(!stmt) { if(verbose) - D (debug_file, "Connection failed ... 2"); - return retval; + D (debug_file, "Handler failed ..."); + + goto end_connection; } - const char *sql = "SELECT count(username) FROM yubikey_mappings WHERE username = ?;"; - const char *sql2 = "SELECT count(username) FROM yubikey_mappings WHERE username = ? and otp_id = ?;"; + const char *sql = "SELECT count(username) FROM yubikey_mappings WHERE username = ?"; + const char *sql2 = "SELECT count(username) FROM yubikey_mappings WHERE username = ? and otp_id = ?"; if(otp_id == NULL) { @@ -177,14 +176,14 @@ check_user_token_mysql(const char *mysql_server, { if(verbose) D (debug_file, "mysql_stmt_prepare() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } }else{ if(mysql_stmt_prepare(stmt, sql2, strlen(sql2))) { if(verbose) D (debug_file, "mysql_stmt_prepare() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } } @@ -208,14 +207,14 @@ check_user_token_mysql(const char *mysql_server, { if(verbose) D (debug_file, "mysql_stmt_bind_param() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } if(mysql_stmt_execute(stmt)) { if(verbose) D (debug_file, "mysql_stmt_execute() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } memset(bind, 0, sizeof(bind)); @@ -227,58 +226,60 @@ check_user_token_mysql(const char *mysql_server, { if(verbose) D (debug_file, "mysql_stmt_bind_result() failed %s", mysql_stmt_error(stmt)); + goto end_connection; } if(mysql_stmt_store_result(stmt)) { if(verbose) D (debug_file, "mysql_stmt_store_result() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } - /* we need to close the connection before the return */ if(mysql_stmt_close(stmt)) { if(verbose) D (debug_file, "mysql_stmt_close() failed %s", mysql_stmt_error(stmt)); - return retval; + goto end_connection; } - mysql_close(con); - mysql_library_end(); - while(!mysql_stmt_fetch(stmt)) { if(bind[0].is_null_value) { + if(verbose) D (debug_file, "mysql_stmt_fetch() failed"); + goto end_connection; } else { if(otp_id != NULL){ if(int_data) { - return AUTH_FOUND; /* User and token verified */ + retval = AUTH_FOUND; /* User and token verified */ } else { - return AUTH_NOT_FOUND; /* User ok but bad token */ + retval = AUTH_NOT_FOUND; /* User ok but bad token */ } } else if(otp_id == NULL) { if(int_data) { - return AUTH_NOT_FOUND; /* We found at least one line for the user */ + retval = AUTH_NOT_FOUND; /* We found at least one line for the user */ } else { - return AUTH_NO_TOKENS; /* We not found at least any line for the user */ + retval = AUTH_NO_TOKENS; /* We not found at least any line for the user */ } } } } +end_connection: + mysql_close(con); + mysql_library_end(); return retval; } #endif