1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* pam_abl - a PAM module and program for automatic blacklisting of hosts and users
*
* Copyright (C) 2005-2012
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "typefun.h"
#include <stdlib.h>
#include <string.h>
#define BYTES_FREE(state) (state->m_usedSize - (size_t)((char*)state->m_current - (char*)state->m_data))
#define SIZE_SMALLER(state, size) BYTES_FREE(state) < (size)
//the space in the buffer before the actual attempts start
#define HEADER_SIZE (sizeof(int)+sizeof(unsigned int))
//when we allocate memory for the data, allocate this much more for future attempts
// this will save us a realloc at the cost of this much more bytes
#define SPARE_SPACE 100
#define INCREASE_NOFATTEMPTS(state, x) ((*((unsigned int *)(((int*)(state->m_data))+1))) += x)
#define SET_NOFATTEMPTS(state, x) ((*((unsigned int *)(((int*)(state->m_data))+1))) = x)
#define ATTEMPS_START(state) (((char*)state->m_data) + HEADER_SIZE)
int createEmptyState(BlockState blockState, AuthState **state) {
*state = NULL;
AuthState *retValue = malloc(sizeof(AuthState));
if (!retValue)
return 1;
memset(retValue, 0, sizeof(AuthState));
//allocate some bytes more then we actually need
//this way we can probably cope with an extra attempt without reallocating
size_t bufferSize = HEADER_SIZE + SPARE_SPACE;
void *allocatedData = malloc(bufferSize);
if (!allocatedData) {
free(retValue);
return 1;
}
retValue->m_data = allocatedData;
retValue->m_size = bufferSize;
retValue->m_usedSize = HEADER_SIZE;
char *bufferPtr = (char*)retValue->m_data;
*((int*)bufferPtr) = blockState;
bufferPtr += sizeof(int);
*((unsigned int*)bufferPtr) = 0;
bufferPtr += sizeof(int);
firstAttempt(retValue);
*state = retValue;
return 0;
}
int createAuthState(void *data, size_t size, AuthState **state) {
*state = NULL;
if (!data || !size)
return 1;
if (size < HEADER_SIZE)
return 1;
AuthState *retValue = malloc(sizeof(AuthState));
if (!retValue)
return 1;
memset(retValue, 0, sizeof(AuthState));
//allocate some bytes more then we actually need
//this way we can probably cope with an extra attempt without reallocating
size_t bufferSize = size + SPARE_SPACE;
void *allocatedData = malloc(bufferSize);
if (!allocatedData) {
free(retValue);
return 1;
}
memcpy(allocatedData, data, size);
retValue->m_data = allocatedData;
retValue->m_size = bufferSize;
retValue->m_usedSize = size;
//the state is the first field, we need to skip this before the Attempts begin
retValue->m_current = ATTEMPS_START(retValue);
*state = retValue;
return 0;
}
BlockState getState(AuthState *state) {
if (!state || !state->m_data)
return -1;
int stateEnum = *((int*)(state->m_data));
return stateEnum;
}
int setState(AuthState *state, BlockState blockState) {
if (!state || !state->m_data)
return 1;
*((int*)(state->m_data)) = blockState;
return 0;
}
unsigned int getNofAttempts(AuthState *state) {
if (!state || !state->m_data)
return 0;
unsigned int nofAttempts = *(unsigned int *)(((int*)(state->m_data))+1);
return nofAttempts;
}
int firstAttempt(AuthState *state) {
if (!state || !state->m_data)
return 1;
state->m_current = ATTEMPS_START(state);
return 0;
}
/*
What does the raw data look like, it should have the following structure
int: State
unsigned int: number of attempts int the bytes after this
(
time_t the time of the failed authentication
int blockreason
zero terminated user or host
zero terminated service
)*
*/
//TODO difference between a faulty layout and the end of the chain
int nextAttempt(AuthState *state, AuthAttempt *attempt) {
if (!state || !state->m_current)
return 1;
//is there still an attempt left to read?
if (SIZE_SMALLER(state, sizeof(time_t))) {
return 1;
}
time_t t = *((time_t*)state->m_current);
state->m_current = ((time_t*)state->m_current) + 1;
if (SIZE_SMALLER(state, sizeof(int))) {
state->m_current = NULL;
return 1;
}
int reason = *((int*)state->m_current);
state->m_current = ((int*)(state->m_current)) + 1;
char *userOrHost = (char*)state->m_current;
size_t bytesFree = BYTES_FREE(state);
size_t strLen = strnlen(userOrHost, bytesFree);
if (strLen == bytesFree) {
state->m_current = NULL;
return 1;
}
state->m_current = ((char*)state->m_current) + strLen + 1;
char *service = (char*)state->m_current;
bytesFree = BYTES_FREE(state);
strLen = strnlen(service, bytesFree);
if (strLen == bytesFree) {
state->m_current = NULL;
return 1;
}
state->m_current = ((char*)state->m_current) + strLen + 1;
//OK state->m_current should point to the next Attempt
if (attempt) {
attempt->m_time = t;
attempt->m_reason = reason;
attempt->m_service = service;
attempt->m_userOrHost = userOrHost;
}
return 0;
}
void destroyAuthState(AuthState *state) {
if (!state)
return;
free(state->m_data);
state->m_data = NULL;
state->m_current = NULL;
state->m_size = 0;
state->m_usedSize = 0;
free(state);
}
static int limitAttempts(AuthState *state, unsigned int limit) {
if (!state)
return 1;
unsigned int nofAttempts = getNofAttempts(state);
if (nofAttempts <= limit)
return 0;
if (firstAttempt(state))
return 1;
if (limit == 0) {
state->m_size = 0;
state->m_usedSize = HEADER_SIZE;
SET_NOFATTEMPTS(state, 0);
firstAttempt(state);
} else {
unsigned int toRemove = nofAttempts - limit;
AuthAttempt attempt;
while (nextAttempt(state, &attempt) == 0) {
--toRemove;
if (toRemove == 0)
break;
}
if (toRemove == 0) {
//iterating went fine, just remove the extra attempts
size_t bytesToMove = state->m_usedSize - (size_t)((char*)state->m_current - (char*)state->m_data);
memmove(ATTEMPS_START(state), state->m_current, bytesToMove);
SET_NOFATTEMPTS(state, limit);
state->m_usedSize = bytesToMove + HEADER_SIZE;
firstAttempt(state);
} else {
//iterating went wrong, pass the error along
return 1;
}
}
return 0;
}
int addAttempt(AuthState *state, BlockReason reason, time_t pTime, const char *userOrHost, const char *service, unsigned int lowerLimit, unsigned int upperLimit) {
if (!userOrHost || !service || !state)
return 1;
if (upperLimit > 0 && getNofAttempts(state) + 1 > upperLimit) {
if (limitAttempts(state, lowerLimit > 0 ? lowerLimit - 1 : 0))
return 1;
}
//calculate how many space we need, perhaps we need to reallocate
size_t userOrHostSize = strlen(userOrHost) + 1;
size_t serviceSize = strlen(service) + 1;
size_t neededSize = userOrHostSize + serviceSize + sizeof(time_t) + sizeof(int);
if (neededSize > state->m_size - state->m_usedSize) {
//damn, space left is too smalll, reallocate and move all the data
//calculate a good new size for the block of memory (let's leave some spare room, for further enlargements)
size_t newSize = state->m_usedSize + neededSize + SPARE_SPACE;
void *newMem = realloc(state->m_data, newSize);
if (!newMem)
return 1;
state->m_current = (char*)newMem + (size_t)((char*)state->m_current - (char*)state->m_data);
state->m_size = newSize;
state->m_data = newMem;
// state->m_usedSize remains the same
}
char *bufferPtr = (char*)state->m_data + state->m_usedSize;
*((time_t*)bufferPtr) = pTime;
bufferPtr += sizeof(time_t);
*((int*)bufferPtr) = reason;
bufferPtr += sizeof(int);
memcpy(bufferPtr, userOrHost, userOrHostSize);
bufferPtr += userOrHostSize;
memcpy(bufferPtr, service, serviceSize);
bufferPtr += serviceSize;
state->m_current = bufferPtr;
state->m_usedSize += neededSize;
INCREASE_NOFATTEMPTS(state, 1);
if (state->m_usedSize != (size_t)((char*)state->m_current - (char*)state->m_data)) {
return 0;
}
return 0;
}
void purgeAuthState(AuthState *state, time_t purgeTime) {
if (!state || !state->m_data || firstAttempt(state)) {
return;
}
AuthAttempt attempt;
int endReached = 1;
int nofAttemptsToRemove = 0;
void *lastOld = state->m_current;
while (!nextAttempt(state, &attempt)) {
if (attempt.m_time >= purgeTime) {
endReached = 0;
break;
}
++nofAttemptsToRemove;
lastOld = state->m_current;
}
if (endReached) {
state->m_usedSize = HEADER_SIZE;
SET_NOFATTEMPTS(state, 0);
} else {
//lastOld should point to the first Attempt with a 'valid' time
//first of all, do we need to move stuff?
if (ATTEMPS_START(state) != lastOld) {
size_t bytesToMove = state->m_usedSize - (size_t)((char*)lastOld - (char*)state->m_data);
memmove(ATTEMPS_START(state), lastOld, bytesToMove);
state->m_usedSize = bytesToMove + HEADER_SIZE;
}
if (nofAttemptsToRemove)
INCREASE_NOFATTEMPTS(state, -1*nofAttemptsToRemove);
}
state->m_current = ATTEMPS_START(state);
}
|