Skip to content

Commit 0566bf9

Browse files
committed
Simplify auth a little bit
1 parent 2d5f45f commit 0566bf9

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/auth.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* vim: set et sw=4 ts=4 sts=4 : */
12
/********************************************************************\
23
* This program is free software; you can redistribute it and/or *
34
* modify it under the terms of the GNU General Public License as *
@@ -121,64 +122,64 @@ logout_client(t_client *client)
121122
void
122123
authenticate_client(request *r)
123124
{
124-
t_client *client;
125+
t_client *client, *tmp;
125126
t_authresponse auth_response;
126-
char *mac,
127-
*token;
127+
char *token;
128128
httpVar *var;
129129
char *urlFragment = NULL;
130130
s_config *config = NULL;
131131
t_auth_serv *auth_server = NULL;
132132

133133
LOCK_CLIENT_LIST();
134134

135-
client = client_list_find_by_ip(r->clientAddr);
135+
client = client_dup(client_list_find_by_ip(r->clientAddr));
136+
137+
UNLOCK_CLIENT_LIST();
136138

137139
if (client == NULL) {
138140
debug(LOG_ERR, "authenticate_client(): Could not find client for %s", r->clientAddr);
139-
UNLOCK_CLIENT_LIST();
140141
return;
141142
}
142143

143-
mac = safe_strdup(client->mac);
144-
145144
/* Users could try to log in(so there is a valid token in
146145
* request) even after they have logged in, try to deal with
147146
* this */
148147
if ((var = httpdGetVariableByName(r, "token")) != NULL) {
149-
if (client->token)
150-
free(client->token);
151-
152-
client->token = safe_strdup(var->value);
153148
token = safe_strdup(var->value);
154149
} else {
155150
token = safe_strdup(client->token);
156151
}
157-
158-
UNLOCK_CLIENT_LIST();
159152

160153
/*
161154
* At this point we've released the lock while we do an HTTP request since it could
162155
* take multiple seconds to do and the gateway would effectively be frozen if we
163156
* kept the lock.
164157
*/
165-
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0);
158+
auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, client->ip, client->mac, token, 0, 0);
166159

167160
LOCK_CLIENT_LIST();
168161

169162
/* can't trust the client to still exist after n seconds have passed */
170-
client = client_list_find(r->clientAddr, mac);
163+
tmp = client_list_find_by_client(client);
171164

172-
if (client == NULL) {
173-
debug(LOG_ERR, "authenticate_client(): Could not find client node for %s (%s)", r->clientAddr, mac);
165+
if (NULL == tmp) {
166+
debug(LOG_ERR, "authenticate_client(): Could not find client node for %s (%s)", client->ip, client->mac);
174167
UNLOCK_CLIENT_LIST();
168+
client_list_destroy(client); /* Free the cloned client */
175169
free(token);
176-
free(mac);
177170
return;
178171
}
172+
173+
client_list_destroy(client); /* Free the cloned client */
174+
client = tmp;
179175

180-
free(token);
181-
free(mac);
176+
if (strcmp(token, client->token) != 0) {
177+
/* If token changed, save it. */
178+
free(client->token);
179+
client->token = token;
180+
} else {
181+
free(token);
182+
}
182183

183184
/* Prepare some variables we'll need below */
184185
config = config_get_config();

src/auth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* vim: set et sw=4 ts=4 sts=4 : */
12
/********************************************************************\
23
* This program is free software; you can redistribute it and/or *
34
* modify it under the terms of the GNU General Public License as *

0 commit comments

Comments
 (0)