Skip to content

Commit c284296

Browse files
committed
Refactoring for ISSUE Yubico#230
1 parent 7794925 commit c284296

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

pam_yubico.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ authorize_user_token (struct cfg *cfg,
177177
/* Administrator had configured the database and specified is name
178178
as an argument for this module.
179179
*/
180-
DBG ("Using Mariadb or Mysql Database");
180+
DBG ("Using Mariadb or Mysql Database V1");
181181
retval = check_user_token_mysql(cfg->mysql_server, cfg->mysql_port, cfg->mysql_user, cfg->mysql_password, cfg->mysql_database, username, otp_id, cfg->debug, cfg->debug_file);
182182
#else
183183
DBG (("Trying to use MYSQL, but this function is not compiled in pam_yubico!!"));

util.c

+34-28
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,16 @@ check_user_token_mysql(const char *mysql_server,
138138
int int_data;
139139
int row_count;
140140

141-
if(mysql_library_init(0, NULL, NULL)){
142-
if(verbose){
143-
D (debug_file, "could not initialize MySQL client library");
144-
}
145-
141+
if(mysql_library_init(0, NULL, NULL))
142+
{
143+
if(verbose)
144+
D (debug_file, "could not initialize MySQL client library");
146145
return retval;
147146
}
148147

149148
con = mysql_init(con);
150-
if(!con) {
149+
if(!con)
150+
{
151151
if(verbose)
152152
D (debug_file, "out of memorys");
153153
return retval;
@@ -164,27 +164,30 @@ check_user_token_mysql(const char *mysql_server,
164164
if(!stmt)
165165
{
166166
if(verbose)
167-
D (debug_file, "Connection failed ... 2");
168-
return retval;
167+
D (debug_file, "Handler failed ...");
168+
169+
goto end_connection;
169170
}
170171

171-
const char *sql = "SELECT count(username) FROM yubikey_mappings WHERE username = ?;";
172-
const char *sql2 = "SELECT count(username) FROM yubikey_mappings WHERE username = ? and otp_id = ?;";
172+
const char *sql = "SELECT count(username) FROM yubikey_mappings WHERE username = ?";
173+
const char *sql2 = "SELECT count(username) FROM yubikey_mappings WHERE username = ? and otp_id = ?";
173174

174175
if(otp_id == NULL)
175176
{
176177
if(mysql_stmt_prepare(stmt, sql, strlen(sql)))
177178
{
178179
if(verbose)
179180
D (debug_file, "mysql_stmt_prepare() failed %s", mysql_stmt_error(stmt));
180-
return retval;
181+
goto end_connection;
181182
}
182-
}else{
183+
}
184+
else
185+
{
183186
if(mysql_stmt_prepare(stmt, sql2, strlen(sql2)))
184187
{
185188
if(verbose)
186189
D (debug_file, "mysql_stmt_prepare() failed %s", mysql_stmt_error(stmt));
187-
return retval;
190+
goto end_connection;
188191
}
189192
}
190193

@@ -208,14 +211,14 @@ check_user_token_mysql(const char *mysql_server,
208211
{
209212
if(verbose)
210213
D (debug_file, "mysql_stmt_bind_param() failed %s", mysql_stmt_error(stmt));
211-
return retval;
214+
goto end_connection;
212215
}
213216

214217
if(mysql_stmt_execute(stmt))
215218
{
216219
if(verbose)
217220
D (debug_file, "mysql_stmt_execute() failed %s", mysql_stmt_error(stmt));
218-
return retval;
221+
goto end_connection;
219222
}
220223

221224
memset(bind, 0, sizeof(bind));
@@ -227,57 +230,60 @@ check_user_token_mysql(const char *mysql_server,
227230
{
228231
if(verbose)
229232
D (debug_file, "mysql_stmt_bind_result() failed %s", mysql_stmt_error(stmt));
233+
goto end_connection;
230234
}
231235

232236
if(mysql_stmt_store_result(stmt))
233237
{
234238
if(verbose)
235239
D (debug_file, "mysql_stmt_store_result() failed %s", mysql_stmt_error(stmt));
236-
return retval;
240+
goto end_connection;
241+
}
242+
243+
if(mysql_stmt_close(stmt))
244+
{
245+
if(verbose)
246+
D (debug_file, "mysql_stmt_close() failed %s", mysql_stmt_error(stmt));
247+
goto end_connection;
237248
}
238249

239250
while(!mysql_stmt_fetch(stmt))
240251
{
241252
if(bind[0].is_null_value)
242253
{
254+
if(verbose)
243255
D (debug_file, "mysql_stmt_fetch() failed");
256+
goto end_connection;
244257
}
245258
else
246259
{
247260
if(otp_id != NULL){
248261
if(int_data)
249262
{
250-
return AUTH_FOUND;
263+
retval = AUTH_FOUND; /* User and token verified */
251264
}
252265
else
253266
{
254-
return AUTH_NOT_FOUND;
267+
retval = AUTH_NOT_FOUND; /* User ok but bad token */
255268
}
256269
}
257270
else if(otp_id == NULL)
258271
{
259272
if(int_data)
260273
{
261-
return AUTH_NOT_FOUND;
274+
retval = AUTH_NOT_FOUND; /* We found at least one line for the user */
262275
}
263276
else
264277
{
265-
return AUTH_NO_TOKENS;
278+
retval = AUTH_NO_TOKENS; /* We not found at least any line for the user */
266279
}
267280
}
268281
}
269282
}
270283

271-
if(mysql_stmt_close(stmt))
272-
{
273-
if(verbose)
274-
D (debug_file, "mysql_stmt_close() failed %s", mysql_stmt_error(stmt));
275-
return retval;
276-
}
277-
284+
end_connection:
278285
mysql_close(con);
279286
mysql_library_end();
280-
281287
return retval;
282288
}
283289
#endif

0 commit comments

Comments
 (0)