Skip to content

Commit

Permalink
Refactoring ISSUE Yubico#230
Browse files Browse the repository at this point in the history
  • Loading branch information
baimard committed Jul 28, 2021
1 parent f6339ed commit fc2dc08
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -164,27 +162,28 @@ 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)
{
if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
{
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;
}
}

Expand All @@ -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));
Expand All @@ -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
Expand Down

0 comments on commit fc2dc08

Please sign in to comment.