Skip to content

Commit b0ff573

Browse files
committed
Re-indent auth.c
1 parent 0566bf9 commit b0ff573

File tree

1 file changed

+134
-143
lines changed

1 file changed

+134
-143
lines changed

src/auth.c

Lines changed: 134 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,36 @@
4949
#include "client_list.h"
5050
#include "util.h"
5151

52-
5352
/** Launches a thread that periodically checks if any of the connections has timed out
5453
@param arg Must contain a pointer to a string containing the IP adress of the client to check to check
5554
@todo Also pass MAC adress?
5655
@todo This thread loops infinitely, need a watchdog to verify that it is still running?
57-
*/
56+
*/
5857
void
5958
thread_client_timeout_check(const void *arg)
6059
{
61-
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
62-
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
63-
struct timespec timeout;
64-
65-
while (1) {
66-
/* Sleep for config.checkinterval seconds... */
67-
timeout.tv_sec = time(NULL) + config_get_config()->checkinterval;
68-
timeout.tv_nsec = 0;
69-
70-
/* Mutex must be locked for pthread_cond_timedwait... */
71-
pthread_mutex_lock(&cond_mutex);
72-
73-
/* Thread safe "sleep" */
74-
pthread_cond_timedwait(&cond, &cond_mutex, &timeout);
75-
76-
/* No longer needs to be locked */
77-
pthread_mutex_unlock(&cond_mutex);
78-
79-
debug(LOG_DEBUG, "Running fw_counter()");
80-
81-
fw_sync_with_authserver();
82-
}
60+
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
61+
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
62+
struct timespec timeout;
63+
64+
while (1) {
65+
/* Sleep for config.checkinterval seconds... */
66+
timeout.tv_sec = time(NULL) + config_get_config()->checkinterval;
67+
timeout.tv_nsec = 0;
68+
69+
/* Mutex must be locked for pthread_cond_timedwait... */
70+
pthread_mutex_lock(&cond_mutex);
71+
72+
/* Thread safe "sleep" */
73+
pthread_cond_timedwait(&cond, &cond_mutex, &timeout);
74+
75+
/* No longer needs to be locked */
76+
pthread_mutex_unlock(&cond_mutex);
77+
78+
debug(LOG_DEBUG, "Running fw_counter()");
79+
80+
fw_sync_with_authserver();
81+
}
8382
}
8483

8584
/**
@@ -92,9 +91,9 @@ thread_client_timeout_check(const void *arg)
9291
* @param client Points to the client to be logged out
9392
*/
9493
void
95-
logout_client(t_client *client)
94+
logout_client(t_client * client)
9695
{
97-
t_authresponse authresponse;
96+
t_authresponse authresponse;
9897
const s_config *config = config_get_config();
9998
fw_deny(client);
10099
client_list_remove(client);
@@ -103,11 +102,10 @@ logout_client(t_client *client)
103102
if (config->auth_servers != NULL) {
104103
UNLOCK_CLIENT_LIST();
105104
auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT,
106-
client->ip, client->mac, client->token,
107-
client->counters.incoming,
108-
client->counters.outgoing);
105+
client->ip, client->mac, client->token,
106+
client->counters.incoming, client->counters.outgoing);
109107

110-
if (authresponse.authcode==AUTH_ERROR)
108+
if (authresponse.authcode == AUTH_ERROR)
111109
debug(LOG_WARNING, "Auth server error when reporting logout");
112110
LOCK_CLIENT_LIST();
113111
}
@@ -120,59 +118,59 @@ logout_client(t_client *client)
120118
@param r httpd request struct
121119
*/
122120
void
123-
authenticate_client(request *r)
121+
authenticate_client(request * r)
124122
{
125-
t_client *client, *tmp;
126-
t_authresponse auth_response;
127-
char *token;
128-
httpVar *var;
129-
char *urlFragment = NULL;
130-
s_config *config = NULL;
131-
t_auth_serv *auth_server = NULL;
132-
133-
LOCK_CLIENT_LIST();
134-
135-
client = client_dup(client_list_find_by_ip(r->clientAddr));
136-
123+
t_client *client, *tmp;
124+
t_authresponse auth_response;
125+
char *token;
126+
httpVar *var;
127+
char *urlFragment = NULL;
128+
s_config *config = NULL;
129+
t_auth_serv *auth_server = NULL;
130+
131+
LOCK_CLIENT_LIST();
132+
133+
client = client_dup(client_list_find_by_ip(r->clientAddr));
134+
137135
UNLOCK_CLIENT_LIST();
138136

139-
if (client == NULL) {
140-
debug(LOG_ERR, "authenticate_client(): Could not find client for %s", r->clientAddr);
141-
return;
142-
}
143-
144-
/* Users could try to log in(so there is a valid token in
145-
* request) even after they have logged in, try to deal with
146-
* this */
147-
if ((var = httpdGetVariableByName(r, "token")) != NULL) {
148-
token = safe_strdup(var->value);
149-
} else {
150-
token = safe_strdup(client->token);
151-
}
152-
153-
/*
154-
* At this point we've released the lock while we do an HTTP request since it could
155-
* take multiple seconds to do and the gateway would effectively be frozen if we
156-
* kept the lock.
157-
*/
158-
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, client->ip, client->mac, token, 0, 0);
159-
160-
LOCK_CLIENT_LIST();
161-
162-
/* can't trust the client to still exist after n seconds have passed */
163-
tmp = client_list_find_by_client(client);
164-
165-
if (NULL == tmp) {
166-
debug(LOG_ERR, "authenticate_client(): Could not find client node for %s (%s)", client->ip, client->mac);
167-
UNLOCK_CLIENT_LIST();
168-
client_list_destroy(client); /* Free the cloned client */
169-
free(token);
170-
return;
171-
}
172-
173-
client_list_destroy(client); /* Free the cloned client */
137+
if (client == NULL) {
138+
debug(LOG_ERR, "authenticate_client(): Could not find client for %s", r->clientAddr);
139+
return;
140+
}
141+
142+
/* Users could try to log in(so there is a valid token in
143+
* request) even after they have logged in, try to deal with
144+
* this */
145+
if ((var = httpdGetVariableByName(r, "token")) != NULL) {
146+
token = safe_strdup(var->value);
147+
} else {
148+
token = safe_strdup(client->token);
149+
}
150+
151+
/*
152+
* At this point we've released the lock while we do an HTTP request since it could
153+
* take multiple seconds to do and the gateway would effectively be frozen if we
154+
* kept the lock.
155+
*/
156+
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, client->ip, client->mac, token, 0, 0);
157+
158+
LOCK_CLIENT_LIST();
159+
160+
/* can't trust the client to still exist after n seconds have passed */
161+
tmp = client_list_find_by_client(client);
162+
163+
if (NULL == tmp) {
164+
debug(LOG_ERR, "authenticate_client(): Could not find client node for %s (%s)", client->ip, client->mac);
165+
UNLOCK_CLIENT_LIST();
166+
client_list_destroy(client); /* Free the cloned client */
167+
free(token);
168+
return;
169+
}
170+
171+
client_list_destroy(client); /* Free the cloned client */
174172
client = tmp;
175-
173+
176174
if (strcmp(token, client->token) != 0) {
177175
/* If token changed, save it. */
178176
free(client->token);
@@ -181,79 +179,72 @@ authenticate_client(request *r)
181179
free(token);
182180
}
183181

184-
/* Prepare some variables we'll need below */
185-
config = config_get_config();
186-
auth_server = get_auth_server();
187-
188-
switch(auth_response.authcode) {
189-
190-
case AUTH_ERROR:
191-
/* Error talking to central server */
192-
debug(LOG_ERR, "Got ERROR from central server authenticating token %s from %s at %s", client->token, client->ip, client->mac);
193-
send_http_page(r, "Error!", "Error: We did not get a valid answer from the central server");
194-
break;
195-
196-
case AUTH_DENIED:
197-
/* Central server said invalid token */
198-
debug(LOG_INFO, "Got DENIED from central server authenticating token %s from %s at %s - deleting from firewall and redirecting them to denied message", client->token, client->ip, client->mac);
199-
fw_deny(client);
200-
safe_asprintf(&urlFragment, "%smessage=%s",
201-
auth_server->authserv_msg_script_path_fragment,
202-
GATEWAY_MESSAGE_DENIED
203-
);
204-
http_send_redirect_to_auth(r, urlFragment, "Redirect to denied message");
205-
free(urlFragment);
206-
break;
182+
/* Prepare some variables we'll need below */
183+
config = config_get_config();
184+
auth_server = get_auth_server();
185+
186+
switch (auth_response.authcode) {
187+
188+
case AUTH_ERROR:
189+
/* Error talking to central server */
190+
debug(LOG_ERR, "Got ERROR from central server authenticating token %s from %s at %s", client->token, client->ip,
191+
client->mac);
192+
send_http_page(r, "Error!", "Error: We did not get a valid answer from the central server");
193+
break;
194+
195+
case AUTH_DENIED:
196+
/* Central server said invalid token */
197+
debug(LOG_INFO,
198+
"Got DENIED from central server authenticating token %s from %s at %s - deleting from firewall and redirecting them to denied message",
199+
client->token, client->ip, client->mac);
200+
fw_deny(client);
201+
safe_asprintf(&urlFragment, "%smessage=%s",
202+
auth_server->authserv_msg_script_path_fragment, GATEWAY_MESSAGE_DENIED);
203+
http_send_redirect_to_auth(r, urlFragment, "Redirect to denied message");
204+
free(urlFragment);
205+
break;
207206

208207
case AUTH_VALIDATION:
209-
/* They just got validated for X minutes to check their email */
210-
debug(LOG_INFO, "Got VALIDATION from central server authenticating token %s from %s at %s"
211-
"- adding to firewall and redirecting them to activate message", client->token,
212-
client->ip, client->mac);
213-
fw_allow(client, FW_MARK_PROBATION);
214-
safe_asprintf(&urlFragment, "%smessage=%s",
215-
auth_server->authserv_msg_script_path_fragment,
216-
GATEWAY_MESSAGE_ACTIVATE_ACCOUNT
217-
);
218-
http_send_redirect_to_auth(r, urlFragment, "Redirect to activate message");
219-
free(urlFragment);
220-
break;
208+
/* They just got validated for X minutes to check their email */
209+
debug(LOG_INFO, "Got VALIDATION from central server authenticating token %s from %s at %s"
210+
"- adding to firewall and redirecting them to activate message", client->token, client->ip, client->mac);
211+
fw_allow(client, FW_MARK_PROBATION);
212+
safe_asprintf(&urlFragment, "%smessage=%s",
213+
auth_server->authserv_msg_script_path_fragment, GATEWAY_MESSAGE_ACTIVATE_ACCOUNT);
214+
http_send_redirect_to_auth(r, urlFragment, "Redirect to activate message");
215+
free(urlFragment);
216+
break;
221217

222218
case AUTH_ALLOWED:
223-
/* Logged in successfully as a regular account */
224-
debug(LOG_INFO, "Got ALLOWED from central server authenticating token %s from %s at %s - "
225-
"adding to firewall and redirecting them to portal", client->token, client->ip, client->mac);
226-
fw_allow(client, FW_MARK_KNOWN);
219+
/* Logged in successfully as a regular account */
220+
debug(LOG_INFO, "Got ALLOWED from central server authenticating token %s from %s at %s - "
221+
"adding to firewall and redirecting them to portal", client->token, client->ip, client->mac);
222+
fw_allow(client, FW_MARK_KNOWN);
227223
served_this_session++;
228-
safe_asprintf(&urlFragment, "%sgw_id=%s",
229-
auth_server->authserv_portal_script_path_fragment,
230-
config->gw_id
231-
);
232-
http_send_redirect_to_auth(r, urlFragment, "Redirect to portal");
233-
free(urlFragment);
234-
break;
224+
safe_asprintf(&urlFragment, "%sgw_id=%s", auth_server->authserv_portal_script_path_fragment, config->gw_id);
225+
http_send_redirect_to_auth(r, urlFragment, "Redirect to portal");
226+
free(urlFragment);
227+
break;
235228

236229
case AUTH_VALIDATION_FAILED:
237-
/* Client had X minutes to validate account by email and didn't = too late */
238-
debug(LOG_INFO, "Got VALIDATION_FAILED from central server authenticating token %s from %s at %s "
239-
"- redirecting them to failed_validation message", client->token, client->ip, client->mac);
240-
safe_asprintf(&urlFragment, "%smessage=%s",
241-
auth_server->authserv_msg_script_path_fragment,
242-
GATEWAY_MESSAGE_ACCOUNT_VALIDATION_FAILED
243-
);
244-
http_send_redirect_to_auth(r, urlFragment, "Redirect to failed validation message");
245-
free(urlFragment);
246-
break;
230+
/* Client had X minutes to validate account by email and didn't = too late */
231+
debug(LOG_INFO, "Got VALIDATION_FAILED from central server authenticating token %s from %s at %s "
232+
"- redirecting them to failed_validation message", client->token, client->ip, client->mac);
233+
safe_asprintf(&urlFragment, "%smessage=%s",
234+
auth_server->authserv_msg_script_path_fragment, GATEWAY_MESSAGE_ACCOUNT_VALIDATION_FAILED);
235+
http_send_redirect_to_auth(r, urlFragment, "Redirect to failed validation message");
236+
free(urlFragment);
237+
break;
247238

248239
default:
249-
debug(LOG_WARNING, "I don't know what the validation code %d means for token %s from %s at %s - sending error message", auth_response.authcode, client->token, client->ip, client->mac);
250-
send_http_page(r, "Internal Error", "We can not validate your request at this time");
251-
break;
240+
debug(LOG_WARNING,
241+
"I don't know what the validation code %d means for token %s from %s at %s - sending error message",
242+
auth_response.authcode, client->token, client->ip, client->mac);
243+
send_http_page(r, "Internal Error", "We can not validate your request at this time");
244+
break;
252245

253-
}
246+
}
254247

255-
UNLOCK_CLIENT_LIST();
256-
return;
248+
UNLOCK_CLIENT_LIST();
249+
return;
257250
}
258-
259-

0 commit comments

Comments
 (0)