1 /*
2  * Common code for OTP authentication mechanisms.
3  *
4  * Copyright (c) 2006 Andrey Panin <pazke@donpac.ru>
5  *
6  * This software is released under the MIT license.
7  */
8 
9 #include "auth-common.h"
10 #include "hash.h"
11 #include "mech.h"
12 
13 #include "otp.h"
14 #include "mech-otp-common.h"
15 
16 static HASH_TABLE(char *, struct auth_request *) otp_lock_table;
17 
otp_lock_init(void)18 void otp_lock_init(void)
19 {
20 	if (hash_table_is_created(otp_lock_table))
21 		return;
22 
23 	hash_table_create(&otp_lock_table, default_pool, 128,
24 			  strcase_hash, strcasecmp);
25 }
26 
otp_try_lock(struct auth_request * auth_request)27 bool otp_try_lock(struct auth_request *auth_request)
28 {
29 	if (hash_table_lookup(otp_lock_table, auth_request->fields.user) != NULL)
30 		return FALSE;
31 
32 	hash_table_insert(otp_lock_table, auth_request->fields.user, auth_request);
33 	return TRUE;
34 }
35 
otp_unlock(struct auth_request * auth_request)36 void otp_unlock(struct auth_request *auth_request)
37 {
38 	struct otp_auth_request *request =
39 		(struct otp_auth_request *)auth_request;
40 
41 	if (!request->lock)
42 		return;
43 
44 	hash_table_remove(otp_lock_table, auth_request->fields.user);
45 	request->lock = FALSE;
46 }
47 
otp_set_credentials_callback(bool success,struct auth_request * auth_request)48 void otp_set_credentials_callback(bool success,
49 				  struct auth_request *auth_request)
50 {
51 	if (success)
52 		auth_request_success(auth_request, "", 0);
53 	else {
54 		auth_request_internal_failure(auth_request);
55 		otp_unlock(auth_request);
56 	}
57 
58 	otp_unlock(auth_request);
59 }
60 
mech_otp_auth_free(struct auth_request * auth_request)61 void mech_otp_auth_free(struct auth_request *auth_request)
62 {
63 	otp_unlock(auth_request);
64 
65 	pool_unref(&auth_request->pool);
66 }
67 
mech_otp_deinit(void)68 void mech_otp_deinit(void)
69 {
70 	hash_table_destroy(&otp_lock_table);
71 }
72