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
|
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bglibs/str.h>
#include "auth-lib.h"
#include "ucspi-proxy.h"
const char filter_connfail_prefix[] = "-ERR ";
const char filter_connfail_suffix[] = "\r\n";
extern const char* local_name;
static bool saw_command = 0;
static str linebuf;
static void handle_user(void)
{
const char* ptr;
const char* end;
saw_command = 0;
ptr = linebuf.s + 5;
while (*ptr == ' ')
++ptr;
end = linebuf.s + linebuf.len - 1;
while (isspace(*end)) --end;
make_username(ptr, end - ptr + 1, "USER ");
str_copys(&linebuf, "USER ");
str_cat(&linebuf, &username);
str_catb(&linebuf, CRLF, 2);
}
static void handle_pass(void)
{
saw_command = 1;
}
static void filter_client_line(void)
{
if (!handle_auth_response(&linebuf, 0)) {
if (str_case_starts(&linebuf, "PASS "))
handle_pass();
else if (str_case_starts(&linebuf, "AUTH "))
saw_command = handle_auth_parameter(&linebuf, 5);
else if (str_case_starts(&linebuf, "USER "))
handle_user();
}
}
static void filter_client_data(char* data, ssize_t size)
{
char* lf;
while ((lf = memchr(data, LF, size)) != 0) {
str_catb(&linebuf, data, lf - data + 1);
filter_client_line();
write_server(linebuf.s, linebuf.len);
linebuf.len = 0;
size -= lf - data + 1;
data = lf + 1;
}
str_catb(&linebuf, data, size);
}
static void filter_server_data(char* data, ssize_t size)
{
if (saw_command) {
log_line(data, size);
if (!strncasecmp(data, "+OK ", 4)) {
accept_client(username.s);
saw_command = 0;
}
else if (!strncasecmp(data, "-ERR ", 5)) {
deny_client(username.s);
saw_command = 0;
}
}
write_client(data, size);
}
void pop3_filter_init(void)
{
set_filter(CLIENT_IN, filter_client_data, 0);
set_filter(SERVER_FD, filter_server_data, 0);
}
|