mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
Move PCRE/SSL to main code
This commit is contained in:
1 parent
a3fb7aff07
commit
8107f03062
42 files changed
+1088
-1121
No files matched your search
@@ -1,20 +0,0 @@
|
||||
# PCREPlugin - requires PCRE2
|
||||
|
||||
if(NOT PCRE2_FOUND)
|
||||
message(STATUS "PCREPlugin requires PCRE2, skipping")
|
||||
return()
|
||||
endif()
|
||||
|
||||
add_3proxy_plugin(PCREPlugin
|
||||
SOURCES pcre_plugin.c
|
||||
COMPILE_DEFINITIONS PCRE2_CODE_UNIT_WIDTH=8
|
||||
)
|
||||
|
||||
if(TARGET PCRE2::PCRE2)
|
||||
target_link_libraries(PCREPlugin PRIVATE PCRE2::PCRE2)
|
||||
else()
|
||||
target_link_libraries(PCREPlugin PRIVATE ${PCRE2_LIBRARIES})
|
||||
if(PCRE2_INCLUDE_DIRS)
|
||||
target_include_directories(PCREPlugin PRIVATE ${PCRE2_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
@@ -1 +0,0 @@
|
||||
include Makefile.var
|
||||
@@ -1,8 +0,0 @@
|
||||
all: $(BUILDDIR)PCREPlugin$(DLSUFFICS)
|
||||
|
||||
pcre_plugin$(OBJSUFFICS): pcre_plugin.c
|
||||
$(CC) $(DCFLAGS) $(CFLAGS) pcre_plugin.c
|
||||
|
||||
$(BUILDDIR)PCREPlugin$(DLSUFFICS): pcre_plugin$(OBJSUFFICS)
|
||||
$(LN) $(LNOUT)../../$(BUILDDIR)PCREPlugin$(DLSUFFICS) $(LDFLAGS) $(DLFLAGS) pcre_plugin$(OBJSUFFICS) $(LIBSPREFIX)pcre2-8$(LIBSSUFFIX)
|
||||
|
||||
@@ -1,545 +0,0 @@
|
||||
/*
|
||||
(c) 2007-2021 by Vladimir Dubrovin <3proxy@3proxy.org>
|
||||
|
||||
please read License Agreement
|
||||
|
||||
*/
|
||||
|
||||
#include "../../structures.h"
|
||||
#include <string.h>
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#define PCRE2_STATIC
|
||||
#include <pcre2.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef isnumber
|
||||
#define isnumber(i_n_arg) ((i_n_arg>='0')&&(i_n_arg<='9'))
|
||||
#endif
|
||||
|
||||
static struct pluginlink * pl;
|
||||
|
||||
static _3proxy_mutex_t pcre_mutex;
|
||||
|
||||
|
||||
static struct filter pcre_first_filter = {
|
||||
NULL,
|
||||
"Fake filter",
|
||||
NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL, NULL,
|
||||
NULL, NULL,
|
||||
NULL, NULL
|
||||
};
|
||||
|
||||
static struct filter *pcre_last_filter;
|
||||
static int pcre_loaded = 0;
|
||||
static uint32_t pcre_options = 0;
|
||||
|
||||
static struct pcreopt {
|
||||
char * name;
|
||||
uint32_t value;
|
||||
} pcreopts[]= {
|
||||
|
||||
{"PCRE2_ALLOW_EMPTY_CLASS", PCRE2_ALLOW_EMPTY_CLASS},
|
||||
{"PCRE2_ALT_BSUX", PCRE2_ALT_BSUX},
|
||||
{"PCRE2_AUTO_CALLOUT", PCRE2_AUTO_CALLOUT},
|
||||
{"PCRE2_CASELESS", PCRE2_CASELESS},
|
||||
{"PCRE2_DOLLAR_ENDONLY", PCRE2_DOLLAR_ENDONLY},
|
||||
{"PCRE2_DOTALL", PCRE2_DOTALL},
|
||||
{"PCRE2_DUPNAMES", PCRE2_DUPNAMES},
|
||||
{"PCRE2_EXTENDED", PCRE2_EXTENDED},
|
||||
{"PCRE2_FIRSTLINE", PCRE2_FIRSTLINE},
|
||||
{"PCRE2_MATCH_UNSET_BACKREF", PCRE2_MATCH_UNSET_BACKREF},
|
||||
{"PCRE2_MULTILINE", PCRE2_MULTILINE},
|
||||
{"PCRE2_NEVER_UCP", PCRE2_NEVER_UCP},
|
||||
{"PCRE2_NEVER_UTF", PCRE2_NEVER_UTF},
|
||||
{"PCRE2_NO_AUTO_CAPTURE", PCRE2_NO_AUTO_CAPTURE},
|
||||
{"PCRE2_NO_AUTO_POSSESS", PCRE2_NO_AUTO_POSSESS},
|
||||
{"PCRE2_NO_DOTSTAR_ANCHOR", PCRE2_NO_DOTSTAR_ANCHOR},
|
||||
{"PCRE2_NO_START_OPTIMIZE", PCRE2_NO_START_OPTIMIZE},
|
||||
{"PCRE2_UCP", PCRE2_UCP},
|
||||
{"PCRE2_UNGREEDY", PCRE2_UNGREEDY},
|
||||
{"PCRE2_UTF", PCRE2_UTF},
|
||||
{"PCRE2_NEVER_BACKSLASH_C", PCRE2_NEVER_BACKSLASH_C},
|
||||
{"PCRE2_ALT_CIRCUMFLEX", PCRE2_ALT_CIRCUMFLEX},
|
||||
{"PCRE2_ALT_VERBNAMES", PCRE2_ALT_VERBNAMES},
|
||||
{"PCRE2_USE_OFFSET_LIMIT", PCRE2_USE_OFFSET_LIMIT},
|
||||
{"PCRE2_EXTENDED_MORE", PCRE2_EXTENDED_MORE},
|
||||
{"PCRE2_LITERAL", PCRE2_LITERAL},
|
||||
{"PCRE2_MATCH_INVALID_UTF", PCRE2_MATCH_INVALID_UTF},
|
||||
|
||||
{"PCRE_CASELESS", PCRE2_CASELESS},
|
||||
{"PCRE_MULTILINE", PCRE2_MULTILINE},
|
||||
{"PCRE_DOTALL", PCRE2_DOTALL},
|
||||
{"PCRE_EXTENDED", PCRE2_EXTENDED},
|
||||
{"PCRE_ANCHORED", PCRE2_ANCHORED},
|
||||
{"PCRE_DOLLAR_ENDONLY", PCRE2_DOLLAR_ENDONLY},
|
||||
{"PCRE_EXTRA", PCRE2_EXTENDED_MORE},
|
||||
{"PCRE_NOTBOL", PCRE2_NOTBOL},
|
||||
{"PCRE_NOTEOL", PCRE2_NOTEOL},
|
||||
{"PCRE_UNGREEDY", PCRE2_UNGREEDY},
|
||||
{"PCRE_NOTEMPTY", PCRE2_NOTEMPTY},
|
||||
{"PCRE_UTF8", PCRE2_UTF},
|
||||
{"PCRE_NO_AUTO_CAPTURE", PCRE2_NO_AUTO_CAPTURE},
|
||||
{"PCRE_NO_UTF8_CHECK", PCRE2_MATCH_INVALID_UTF},
|
||||
{"PCRE_AUTO_CALLOUT", PCRE2_AUTO_CALLOUT},
|
||||
{"PCRE_PARTIAL", PCRE2_PARTIAL_SOFT},
|
||||
{"PCRE_DFA_SHORTEST", PCRE2_DFA_SHORTEST},
|
||||
{"PCRE_DFA_RESTART", PCRE2_DFA_RESTART},
|
||||
{"PCRE_FIRSTLINE", PCRE2_FIRSTLINE},
|
||||
{"PCRE_DUPNAMES", PCRE2_DUPNAMES},
|
||||
{"PCRE_NEWLINE_CR", PCRE2_NEWLINE_CR},
|
||||
{"PCRE_NEWLINE_LF", PCRE2_NEWLINE_LF},
|
||||
{"PCRE_NEWLINE_CRLF", PCRE2_NEWLINE_CRLF},
|
||||
{"PCRE_NEWLINE_ANY", PCRE2_NEWLINE_ANY},
|
||||
{"PCRE_NEWLINE_ANYCRLF", PCRE2_NEWLINE_ANYCRLF},
|
||||
{"PCRE_BSR_ANYCRLF", PCRE2_BSR_ANYCRLF},
|
||||
{"PCRE_BSR_UNICODE", PCRE2_BSR_UNICODE},
|
||||
|
||||
{NULL, 0}
|
||||
};
|
||||
|
||||
struct pcre_filter_data {
|
||||
int users;
|
||||
pcre2_code * re;
|
||||
pcre2_match_data * match_data;
|
||||
int action;
|
||||
char * replace;
|
||||
struct ace *acl;
|
||||
};
|
||||
|
||||
static void pcre_data_free(struct pcre_filter_data *pcrefd){
|
||||
_3proxy_mutex_lock(&pcre_mutex);
|
||||
pcrefd->users--;
|
||||
if(!pcrefd->users){
|
||||
if(pcrefd->match_data) pcre2_match_data_free(pcrefd->match_data);
|
||||
if(pcrefd->re) pcre2_code_free(pcrefd->re);
|
||||
if(pcrefd->acl) pl->freeacl(pcrefd->acl);
|
||||
if(pcrefd->replace) pl->freefunc(pcrefd->replace);
|
||||
pl->freefunc(pcrefd);
|
||||
}
|
||||
_3proxy_mutex_unlock(&pcre_mutex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void* pcre_filter_open(void * idata, struct srvparam * param){
|
||||
#define pcrefd ((struct pcre_filter_data *)idata)
|
||||
if(idata){
|
||||
_3proxy_mutex_lock(&pcre_mutex);
|
||||
pcrefd->users++;
|
||||
_3proxy_mutex_unlock(&pcre_mutex);
|
||||
}
|
||||
#undef pcrefd
|
||||
return idata;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static FILTER_ACTION pcre_filter_client(void *fo, struct clientparam * param, void** fc){
|
||||
int res;
|
||||
struct ace tmpace;
|
||||
|
||||
*fc = fo;
|
||||
if(!fo) return PASS;
|
||||
#define pcrefd ((struct pcre_filter_data *)fo)
|
||||
if(!pcrefd->acl) return CONTINUE;
|
||||
memset (&tmpace, 0, sizeof(struct ace));
|
||||
tmpace.src = pcrefd->acl->src;
|
||||
res = pl->ACLMatches(&tmpace, param);
|
||||
#undef pcrefd
|
||||
return (res)? CONTINUE:PASS;
|
||||
}
|
||||
|
||||
static FILTER_ACTION pcre_filter_buffer(void *fc, struct clientparam *param, unsigned char ** buf_p, int * bufsize_p, int offset, int * length_p){
|
||||
PCRE2_SIZE *ovector;
|
||||
int count = 0;
|
||||
struct ace *acl;
|
||||
int match = 0;
|
||||
int replen, num;
|
||||
char * replace;
|
||||
char * tmpbuf, *target, *newbuf;
|
||||
int nreplaces=0;
|
||||
#define pcrefd ((struct pcre_filter_data *)fc)
|
||||
|
||||
for(acl = pcrefd->acl; acl; acl=acl->next){
|
||||
if(pl->ACLMatches(pcrefd->acl, param)){
|
||||
match = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!match) return CONTINUE;
|
||||
if(!pcrefd->re) return pcrefd->action;
|
||||
for(; offset < *length_p; nreplaces++){
|
||||
|
||||
count = pcre2_match(pcrefd->re, (PCRE2_SPTR)*buf_p, *length_p, offset, 0, pcrefd->match_data, NULL);
|
||||
if(count <= 0) break;
|
||||
ovector = pcre2_get_ovector_pointer(pcrefd->match_data);
|
||||
if(!(replace = pcrefd->replace) || param->nooverwritefilter) return pcrefd->action;
|
||||
|
||||
replen = *length_p - ovector[1];
|
||||
while(*replace){
|
||||
if(*replace == '\\' && *(replace +1)){
|
||||
replace+=2;
|
||||
++replen;
|
||||
}
|
||||
else if(*replace == '$' && isnumber(*(replace+1))){
|
||||
replace ++;
|
||||
num = atoi(replace);
|
||||
while(isnumber(*replace)) replace++;
|
||||
if(num > (count - 1)) continue;
|
||||
replen += (ovector[(num<<1) + 1] - ovector[(num<<1)]);
|
||||
}
|
||||
else {
|
||||
replace++;
|
||||
replen++;
|
||||
}
|
||||
}
|
||||
|
||||
tmpbuf = pl->mallocfunc(replen);
|
||||
if(!tmpbuf) return CONTINUE;
|
||||
for(target = tmpbuf, replace = pcrefd->replace; *replace; ){
|
||||
if(*replace == '\\' && *(replace +1)){
|
||||
*target++ = replace[1];
|
||||
replace+=2;
|
||||
}
|
||||
else if(*replace == '$' && isnumber(*(replace+1))){
|
||||
replace ++;
|
||||
num = atoi(replace);
|
||||
if(num > (count - 1)) continue;
|
||||
memcpy(target, *buf_p + ovector[(num<<1)], ovector[(num<<1) + 1] - ovector[(num<<1)]);
|
||||
target += (ovector[(num<<1) + 1] - ovector[(num<<1)]);
|
||||
while(isnumber(*replace)) replace++;
|
||||
}
|
||||
else {
|
||||
*target++ = *replace++;
|
||||
}
|
||||
}
|
||||
memcpy(target, *buf_p + ovector[1], *length_p - ovector[1]);
|
||||
if((ovector[0] + replen + 1) > *bufsize_p){
|
||||
newbuf = pl->mallocfunc(ovector[0] + replen + 1);
|
||||
if(!newbuf){
|
||||
pl->freefunc(tmpbuf);
|
||||
return CONTINUE;
|
||||
}
|
||||
memcpy(newbuf, *buf_p, ovector[0]);
|
||||
pl->freefunc(*buf_p);
|
||||
*buf_p = (unsigned char *)newbuf;
|
||||
*bufsize_p = ovector[0] + replen + 1;
|
||||
}
|
||||
memcpy(*buf_p + ovector[0], tmpbuf, replen);
|
||||
pl->freefunc(tmpbuf);
|
||||
(*buf_p)[ovector[0] + replen] = 0;
|
||||
*length_p = ovector[0] + replen;
|
||||
if(ovector[0] + replen <= offset){
|
||||
break;
|
||||
}
|
||||
offset = ovector[0] + (int)strlen(pcrefd->replace);
|
||||
}
|
||||
return nreplaces? pcrefd->action : CONTINUE;
|
||||
#undef pcrefd
|
||||
}
|
||||
|
||||
static void pcre_filter_clear(void *fo){
|
||||
}
|
||||
|
||||
static void pcre_filter_close(void *fo){
|
||||
if(!fo) return;
|
||||
pcre_data_free((struct pcre_filter_data *)fo);
|
||||
}
|
||||
|
||||
static int h_pcre(int argc, unsigned char **argv){
|
||||
int action = 0;
|
||||
pcre2_code *re = NULL;
|
||||
pcre2_match_data *match_data = NULL;
|
||||
struct ace *acl;
|
||||
int errcode;
|
||||
PCRE2_SIZE erroffset;
|
||||
struct pcre_filter_data *flt;
|
||||
struct filter *newf;
|
||||
char *replace = NULL;
|
||||
|
||||
if(!strncmp((char *)argv[2], "allow",5)) action = PASS;
|
||||
else if(!strncmp((char *)argv[2], "deny",4)) action = REJECT;
|
||||
else if(!strncmp((char *)argv[2], "remove",6)) action = REMOVE;
|
||||
else if(!strncmp((char *)argv[2], "dunno",5)) action = CONTINUE;
|
||||
else return 1;
|
||||
if(!strncmp((char *)argv[0], "pcre_rewrite", 12)) {
|
||||
int i,j;
|
||||
replace = pl->strdupfunc((char *)argv[4]);
|
||||
if(!replace) return 9;
|
||||
for(i=0, j=0; replace[i]; i++, j++){
|
||||
if(replace[i] == '\\'){
|
||||
switch(replace[i+1]){
|
||||
case 'r':
|
||||
i++;
|
||||
replace[j] = '\r';
|
||||
break;
|
||||
case 'n':
|
||||
i++;
|
||||
replace[j] = '\n';
|
||||
break;
|
||||
case '0':
|
||||
i++;
|
||||
replace[j] = 0;
|
||||
break;
|
||||
case '\\':
|
||||
i++;
|
||||
default:
|
||||
replace[j] = '\\';
|
||||
break;
|
||||
}
|
||||
}
|
||||
else replace[j] = replace[i];
|
||||
}
|
||||
replace[j] = 0;
|
||||
}
|
||||
if(!(acl = pl->make_ace(argc - 4, argv + 4))) return 2;
|
||||
acl->nolog = (strstr((char *)argv[2],"log") == 0);
|
||||
if(*argv[3] && !(*argv[3] == '*' && !argv[3][1]) ){
|
||||
re = pcre2_compile((PCRE2_SPTR)argv[3], PCRE2_ZERO_TERMINATED, pcre_options, &errcode, &erroffset, NULL);
|
||||
if(!re) {
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
return 3;
|
||||
}
|
||||
match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
||||
if(!match_data) {
|
||||
pcre2_code_free(re);
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
flt = pl->mallocfunc(sizeof(struct pcre_filter_data));
|
||||
newf = pl->mallocfunc(sizeof(struct filter));
|
||||
|
||||
if(!flt || !newf) {
|
||||
if(match_data) pcre2_match_data_free(match_data);
|
||||
if(re) pcre2_code_free(re);
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
if(flt) pl->freefunc(flt);
|
||||
return 4;
|
||||
}
|
||||
memset(flt, 0, sizeof(struct pcre_filter_data));
|
||||
memset(newf, 0, sizeof(struct filter));
|
||||
flt->action = action;
|
||||
flt->re = re;
|
||||
flt->match_data = match_data;
|
||||
flt->acl = acl;
|
||||
flt->replace = replace;
|
||||
flt->users = 1;
|
||||
newf->instance = "pcre";
|
||||
newf->data = flt;
|
||||
newf->filter_open = pcre_filter_open;
|
||||
newf->filter_client = pcre_filter_client;
|
||||
if(strstr((char *)argv[1], "request"))newf->filter_request = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "cliheader"))newf->filter_header_cli = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "clidata"))newf->filter_data_cli = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "srvheader"))newf->filter_header_srv = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "srvdata"))newf->filter_data_srv = pcre_filter_buffer;
|
||||
newf->filter_clear = pcre_filter_clear;
|
||||
newf->filter_close = pcre_filter_close;
|
||||
|
||||
if(!pcre_last_filter){
|
||||
newf->next = pcre_first_filter.next;
|
||||
pcre_first_filter.next=newf;
|
||||
}
|
||||
else {
|
||||
newf->next = pcre_last_filter->next;
|
||||
pcre_last_filter->next = newf;
|
||||
}
|
||||
pcre_last_filter=newf;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_pcre_rewrite(int argc, unsigned char **argv){
|
||||
int action = 0;
|
||||
pcre2_code *re = NULL;
|
||||
pcre2_match_data *match_data = NULL;
|
||||
struct ace *acl;
|
||||
int errcode;
|
||||
PCRE2_SIZE erroffset;
|
||||
struct pcre_filter_data *flt;
|
||||
struct filter *newf;
|
||||
char *replace = NULL;
|
||||
|
||||
if(!strncmp((char *)argv[2], "allow",5)) action = PASS;
|
||||
else if(!strncmp((char *)argv[2], "deny",4)) action = REJECT;
|
||||
else if(!strncmp((char *)argv[2], "remove",6)) action = REMOVE;
|
||||
else if(!strncmp((char *)argv[2], "dunno",5)) action = CONTINUE;
|
||||
else return 1;
|
||||
{
|
||||
int i,j;
|
||||
replace = pl->strdupfunc((char *)argv[4]);
|
||||
if(!replace) return 9;
|
||||
for(i=0, j=0; replace[i]; i++, j++){
|
||||
if(replace[i] == '\\'){
|
||||
switch(replace[i+1]){
|
||||
case 'r':
|
||||
i++;
|
||||
replace[j] = '\r';
|
||||
break;
|
||||
case 'n':
|
||||
i++;
|
||||
replace[j] = '\n';
|
||||
break;
|
||||
case '0':
|
||||
i++;
|
||||
replace[j] = 0;
|
||||
break;
|
||||
case '\\':
|
||||
i++;
|
||||
default:
|
||||
replace[j] = '\\';
|
||||
break;
|
||||
}
|
||||
}
|
||||
else replace[j] = replace[i];
|
||||
}
|
||||
replace[j] = 0;
|
||||
}
|
||||
if(!(acl = pl->make_ace(argc - 5, argv + 5))) return 2;
|
||||
acl->nolog = (strstr((char *)argv[2],"log") == 0);
|
||||
if(*argv[3] && !(*argv[3] == '*' && !argv[3][1]) ){
|
||||
re = pcre2_compile((PCRE2_SPTR)argv[3], PCRE2_ZERO_TERMINATED, pcre_options, &errcode, &erroffset, NULL);
|
||||
if(!re) {
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
return 3;
|
||||
}
|
||||
match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
||||
if(!match_data) {
|
||||
pcre2_code_free(re);
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
flt = pl->mallocfunc(sizeof(struct pcre_filter_data));
|
||||
newf = pl->mallocfunc(sizeof(struct filter));
|
||||
|
||||
if(!flt || !newf) {
|
||||
if(match_data) pcre2_match_data_free(match_data);
|
||||
if(re) pcre2_code_free(re);
|
||||
pl->freefunc(acl);
|
||||
if(replace) pl->freefunc(replace);
|
||||
if(flt) pl->freefunc(flt);
|
||||
return 4;
|
||||
}
|
||||
memset(flt, 0, sizeof(struct pcre_filter_data));
|
||||
memset(newf, 0, sizeof(struct filter));
|
||||
flt->action = action;
|
||||
flt->re = re;
|
||||
flt->match_data = match_data;
|
||||
flt->acl = acl;
|
||||
flt->replace = replace;
|
||||
flt->users = 1;
|
||||
newf->instance = "pcre";
|
||||
newf->data = flt;
|
||||
newf->filter_open = pcre_filter_open;
|
||||
newf->filter_client = pcre_filter_client;
|
||||
if(strstr((char *)argv[1], "request"))newf->filter_request = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "cliheader"))newf->filter_header_cli = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "clidata"))newf->filter_data_cli = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "srvheader"))newf->filter_header_srv = pcre_filter_buffer;
|
||||
if(strstr((char *)argv[1], "srvdata"))newf->filter_data_srv = pcre_filter_buffer;
|
||||
newf->filter_clear = pcre_filter_clear;
|
||||
newf->filter_close = pcre_filter_close;
|
||||
|
||||
if(!pcre_last_filter){
|
||||
newf->next = pcre_first_filter.next;
|
||||
pcre_first_filter.next=newf;
|
||||
}
|
||||
else {
|
||||
newf->next = pcre_last_filter->next;
|
||||
pcre_last_filter->next = newf;
|
||||
}
|
||||
pcre_last_filter=newf;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_pcre_extend(int argc, unsigned char **argv){
|
||||
struct ace *acl;
|
||||
if(!pcre_last_filter || !pcre_last_filter->data) return 1;
|
||||
acl = ((struct pcre_filter_data *)pcre_last_filter->data)->acl;
|
||||
if(!acl) return 2;
|
||||
for(; acl->next; acl=acl->next);
|
||||
acl->next = (*pl->make_ace)(argc - 1, argv + 1);
|
||||
if(!acl->next) return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int h_pcre_options(int argc, unsigned char **argv){
|
||||
int i,j;
|
||||
|
||||
pcre_options = 0;
|
||||
for(j=1; j<argc; j++)
|
||||
for(i=0; pcreopts[i].name; i++)
|
||||
if(!strcmp(pcreopts[i].name, (char *)argv[j]))
|
||||
pcre_options |= pcreopts[i].value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static struct commands pcre_commandhandlers[] = {
|
||||
{pcre_commandhandlers+1, "pcre", h_pcre, 4, 0},
|
||||
{pcre_commandhandlers+2, "pcre_rewrite", h_pcre_rewrite, 5, 0},
|
||||
{pcre_commandhandlers+3, "pcre_extend", h_pcre_extend, 2, 0},
|
||||
{NULL, "pcre_options", h_pcre_options, 2, 0}
|
||||
};
|
||||
|
||||
static struct symbol regexp_symbols[] = {
|
||||
{regexp_symbols+1, "pcre2_compile", (void*) pcre2_compile},
|
||||
{regexp_symbols+2, "pcre2_match", (void*) pcre2_match},
|
||||
{NULL, "pcre_options", (void *)&pcre_options},
|
||||
};
|
||||
|
||||
#ifdef WATCOM
|
||||
#pragma aux pcre_plugin "*" parm caller [ ] value struct float struct routine [eax] modify [eax ecx edx]
|
||||
#undef PLUGINCALL
|
||||
#define PLUGINCALL
|
||||
#endif
|
||||
|
||||
PLUGINAPI int PLUGINCALL pcre_plugin (struct pluginlink * pluginlink,
|
||||
int argc, char** argv){
|
||||
|
||||
struct filter *flt, *tmpflt;
|
||||
pl = pluginlink;
|
||||
pcre_options = 0;
|
||||
if(!pcre_loaded){
|
||||
pcre_loaded = 1;
|
||||
_3proxy_mutex_init(&pcre_mutex);
|
||||
regexp_symbols[2].next = pl->symbols.next;
|
||||
pl->symbols.next = regexp_symbols;
|
||||
pcre_commandhandlers[3].next = pl->commandhandlers->next;
|
||||
pl->commandhandlers->next = pcre_commandhandlers;
|
||||
pcre_first_filter.next = pl->conf->filters;
|
||||
pl->conf->filters = &pcre_first_filter;
|
||||
}
|
||||
else if(pcre_last_filter){
|
||||
pcre_first_filter.next = pcre_last_filter->next;
|
||||
flt = pcre_first_filter.next;
|
||||
for(; flt; flt = tmpflt){
|
||||
tmpflt = flt->next;
|
||||
if(flt->data)
|
||||
pcre_data_free((struct pcre_filter_data *)flt->data);
|
||||
pl->freefunc(flt);
|
||||
if(flt == pcre_last_filter) break;
|
||||
}
|
||||
}
|
||||
pcre_last_filter = NULL;
|
||||
return 0;
|
||||
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,17 +0,0 @@
|
||||
# SSLPlugin - requires OpenSSL
|
||||
|
||||
if(NOT TARGET OpenSSL::SSL)
|
||||
message(STATUS "SSLPlugin requires OpenSSL, skipping")
|
||||
return()
|
||||
endif()
|
||||
|
||||
add_3proxy_plugin(SSLPlugin
|
||||
SOURCES
|
||||
ssl_plugin.c
|
||||
my_ssl.c
|
||||
LIBRARIES
|
||||
OpenSSL::SSL
|
||||
OpenSSL::Crypto
|
||||
COMPILE_DEFINITIONS
|
||||
WITH_SSL
|
||||
)
|
||||
@@ -1 +0,0 @@
|
||||
include Makefile.var
|
||||
@@ -1,14 +0,0 @@
|
||||
all: $(BUILDDIR)SSLPlugin$(DLSUFFICS)
|
||||
|
||||
|
||||
|
||||
ssl_plugin$(OBJSUFFICS): ssl_plugin.c
|
||||
$(CC) $(DCFLAGS) $(CFLAGS) ssl_plugin.c
|
||||
|
||||
my_ssl$(OBJSUFFICS): my_ssl.c
|
||||
$(CC) $(DCFLAGS) $(CFLAGS) my_ssl.c
|
||||
|
||||
|
||||
$(BUILDDIR)SSLPlugin$(DLSUFFICS): ssl_plugin$(OBJSUFFICS) my_ssl$(OBJSUFFICS)
|
||||
$(LN) $(LNOUT)../../$(BUILDDIR)SSLPlugin$(DLSUFFICS) $(LDFLAGS) $(DLFLAGS) ssl_plugin$(OBJSUFFICS) my_ssl$(OBJSUFFICS) $(LIBS)
|
||||
|
||||
@@ -1,298 +0,0 @@
|
||||
/*
|
||||
(c) 2002-2021 by Vladimir Dubrovin <3proxy@3proxy.org>
|
||||
|
||||
please read License Agreement
|
||||
|
||||
*/
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include "../../structures.h"
|
||||
#include <memory.h>
|
||||
#include <fcntl.h>
|
||||
#ifndef _WIN32
|
||||
#include <sys/file.h>
|
||||
#endif
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/x509v3.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "../../proxy.h"
|
||||
#include "my_ssl.h"
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct _ssl_conn {
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
} ssl_conn;
|
||||
|
||||
_3proxy_mutex_t ssl_file_mutex;
|
||||
|
||||
|
||||
static char errbuf[256];
|
||||
|
||||
static char hexMap[] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
static BIO *bio_err=NULL;
|
||||
|
||||
|
||||
|
||||
char * getSSLErr(){
|
||||
return ERR_error_string(ERR_get_error(), errbuf);
|
||||
}
|
||||
|
||||
static size_t bin2hex (const unsigned char* bin, size_t bin_length, char* str, size_t str_length)
|
||||
{
|
||||
char *p;
|
||||
size_t i;
|
||||
|
||||
if ( str_length < ( (bin_length*2)+1) )
|
||||
return 0;
|
||||
|
||||
p = str;
|
||||
for ( i=0; i < bin_length; ++i )
|
||||
{
|
||||
*p++ = hexMap[(*(unsigned char *)bin) >> 4];
|
||||
*p++ = hexMap[(*(unsigned char *)bin) & 0xf];
|
||||
++bin;
|
||||
}
|
||||
|
||||
*p = 0;
|
||||
|
||||
return p - str;
|
||||
}
|
||||
|
||||
static int add_ext(X509 *cert, int nid, char *value)
|
||||
{
|
||||
X509_EXTENSION *ex;
|
||||
X509V3_CTX ctx;
|
||||
/* This sets the 'context' of the extensions. */
|
||||
/* No configuration database */
|
||||
X509V3_set_ctx_nodb(&ctx);
|
||||
/* Issuer and subject certs: both the target since it is self signed,
|
||||
* no request and no CRL
|
||||
*/
|
||||
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
|
||||
ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
|
||||
if (!ex)
|
||||
return 0;
|
||||
|
||||
X509_add_ext(cert,ex,-1);
|
||||
X509_EXTENSION_free(ex);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void del_ext(X509 *dst_cert, int nid, int where){
|
||||
int ex;
|
||||
|
||||
ex = X509_get_ext_by_NID(dst_cert, nid, where);
|
||||
if(ex>=0){
|
||||
X509_EXTENSION *ext;
|
||||
if((ext = X509_delete_ext(dst_cert, ex))) X509_EXTENSION_free(ext);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
SSL_CERT ssl_copy_cert(SSL_CERT cert, SSL_CONFIG *config)
|
||||
{
|
||||
int err = -1;
|
||||
BIO *fcache;
|
||||
X509 *src_cert = (X509 *) cert;
|
||||
X509 *dst_cert = NULL;
|
||||
|
||||
EVP_PKEY *pk = NULL;
|
||||
RSA *rsa = NULL;
|
||||
|
||||
int hash_size = 20;
|
||||
unsigned char hash_sha1[20];
|
||||
char hash_name_sha1[(20*2) + 1];
|
||||
char cache_name[256];
|
||||
|
||||
err = X509_digest(src_cert, EVP_sha1(), hash_sha1, NULL);
|
||||
if(!err){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(config->certcache){
|
||||
bin2hex(hash_sha1, 20, hash_name_sha1, sizeof(hash_name_sha1));
|
||||
sprintf(cache_name, "%s%s.pem", config->certcache, hash_name_sha1);
|
||||
/* check if certificate is already cached */
|
||||
fcache = BIO_new_file(cache_name, "rb");
|
||||
if ( fcache != NULL ) {
|
||||
#ifndef _WIN32
|
||||
flock(BIO_get_fd(fcache, NULL), LOCK_SH);
|
||||
#endif
|
||||
dst_cert = PEM_read_bio_X509(fcache, &dst_cert, NULL, NULL);
|
||||
#ifndef _WIN32
|
||||
flock(BIO_get_fd(fcache, NULL), LOCK_UN);
|
||||
#endif
|
||||
BIO_free(fcache);
|
||||
if ( dst_cert != NULL ){
|
||||
return dst_cert;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* proceed if certificate is not cached */
|
||||
dst_cert = X509_dup(src_cert);
|
||||
if ( dst_cert == NULL ) {
|
||||
return NULL;
|
||||
}
|
||||
del_ext(dst_cert, NID_crl_distribution_points, -1);
|
||||
del_ext(dst_cert, NID_info_access, -1);
|
||||
del_ext(dst_cert, NID_authority_key_identifier, -1);
|
||||
del_ext(dst_cert, NID_certificate_policies, 0);
|
||||
|
||||
err = X509_set_pubkey(dst_cert, config->server_key?config->server_key:config->CA_key);
|
||||
if ( err == 0 ) {
|
||||
X509_free(dst_cert);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
err = X509_set_issuer_name(dst_cert, X509_get_subject_name(config->CA_cert));
|
||||
if(!err){
|
||||
X509_free(dst_cert);
|
||||
return NULL;
|
||||
}
|
||||
err = X509_sign(dst_cert, config->CA_key, EVP_sha256());
|
||||
if(!err){
|
||||
X509_free(dst_cert);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* write to cache */
|
||||
|
||||
if(config->certcache){
|
||||
fcache = BIO_new_file(cache_name, "wb");
|
||||
if ( fcache != NULL ) {
|
||||
#ifndef _WIN32
|
||||
flock(BIO_get_fd(fcache, NULL), LOCK_EX);
|
||||
#endif
|
||||
PEM_write_bio_X509(fcache, dst_cert);
|
||||
#ifndef _WIN32
|
||||
flock(BIO_get_fd(fcache, NULL), LOCK_UN);
|
||||
#endif
|
||||
BIO_free(fcache);
|
||||
}
|
||||
}
|
||||
return dst_cert;
|
||||
}
|
||||
|
||||
int ssl_read(SSL_CONN connection, void * buf, int bufsize)
|
||||
{
|
||||
ssl_conn *conn = (ssl_conn *) connection;
|
||||
|
||||
return SSL_read(conn->ssl, buf, bufsize);
|
||||
}
|
||||
|
||||
int ssl_write(SSL_CONN connection, void * buf, int bufsize)
|
||||
{
|
||||
ssl_conn *conn = (ssl_conn *) connection;
|
||||
|
||||
return SSL_write(conn->ssl, buf, bufsize);
|
||||
}
|
||||
int ssl_pending(SSL_CONN connection)
|
||||
{
|
||||
ssl_conn *conn = (ssl_conn *) connection;
|
||||
|
||||
return SSL_pending(conn->ssl);
|
||||
}
|
||||
|
||||
void ssl_conn_free(SSL_CONN connection)
|
||||
{
|
||||
ssl_conn *conn = (ssl_conn *) connection;
|
||||
|
||||
if(conn){
|
||||
if(conn->ssl){
|
||||
SSL_shutdown(conn->ssl);
|
||||
SSL_free(conn->ssl);
|
||||
}
|
||||
if(conn->ctx) SSL_CTX_free(conn->ctx);
|
||||
free(conn);
|
||||
}
|
||||
}
|
||||
|
||||
void _ssl_cert_free(SSL_CERT cert)
|
||||
{
|
||||
X509_free((X509 *)cert);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* This array will store all of the mutexes available to OpenSSL. */
|
||||
static _3proxy_mutex_t *mutex_buf= NULL;
|
||||
|
||||
|
||||
static void locking_function(int mode, int n, const char * file, int line)
|
||||
{
|
||||
if (mode & CRYPTO_LOCK)
|
||||
_3proxy_mutex_lock(mutex_buf + n);
|
||||
else
|
||||
_3proxy_mutex_unlock(mutex_buf + n);
|
||||
}
|
||||
|
||||
static unsigned long id_function(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return ((unsigned long)GetCurrentThreadId());
|
||||
#else
|
||||
return ((unsigned long)pthread_self());
|
||||
#endif
|
||||
}
|
||||
|
||||
int thread_setup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
mutex_buf = malloc(CRYPTO_num_locks( ) * sizeof(_3proxy_mutex_t));
|
||||
if (!mutex_buf)
|
||||
return 0;
|
||||
for (i = 0; i < CRYPTO_num_locks( ); i++)
|
||||
_3proxy_mutex_init(mutex_buf +i);
|
||||
CRYPTO_set_id_callback(id_function);
|
||||
CRYPTO_set_locking_callback(locking_function);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int thread_cleanup(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!mutex_buf)
|
||||
return 0;
|
||||
CRYPTO_set_id_callback(NULL);
|
||||
CRYPTO_set_locking_callback(NULL);
|
||||
for (i = 0; i < CRYPTO_num_locks( ); i++)
|
||||
_3proxy_mutex_destroy(mutex_buf +i);
|
||||
free(mutex_buf);
|
||||
mutex_buf = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int ssl_file_init = 0;
|
||||
|
||||
int ssl_init_done = 0;
|
||||
|
||||
void ssl_init()
|
||||
{
|
||||
if(!ssl_init_done){
|
||||
ssl_init_done = 1;
|
||||
thread_setup();
|
||||
SSLeay_add_ssl_algorithms();
|
||||
SSL_load_error_strings();
|
||||
_3proxy_mutex_init(&ssl_file_mutex);
|
||||
bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
#ifndef __my_ssl_h__
|
||||
#define __my_ssl_h__
|
||||
|
||||
//
|
||||
// opaque connection structure
|
||||
//
|
||||
typedef void *SSL_CONN;
|
||||
//
|
||||
// opaque certificate structure
|
||||
//
|
||||
typedef void *SSL_CERT;
|
||||
|
||||
struct alpn {
|
||||
unsigned char *protos;
|
||||
unsigned int protos_len;
|
||||
};
|
||||
|
||||
struct ssl_config {
|
||||
X509 *CA_cert;
|
||||
X509 *server_cert;
|
||||
X509 *client_cert;
|
||||
EVP_PKEY *CA_key;
|
||||
EVP_PKEY *server_key;
|
||||
EVP_PKEY *client_key;
|
||||
SSL_CTX *cli_ctx;
|
||||
SSL_CTX *srv_ctx;
|
||||
char *certcache;
|
||||
char * client_ciphersuites;
|
||||
char * server_ciphersuites;
|
||||
char * client_cipher_list;
|
||||
char * server_cipher_list;
|
||||
char * client_ca_file;
|
||||
char * client_ca_dir;
|
||||
char * client_ca_store;
|
||||
char * server_ca_file;
|
||||
char * server_ca_dir;
|
||||
char * server_ca_store;
|
||||
char * client_sni;
|
||||
struct alpn client_alpn_protos;
|
||||
int mitm;
|
||||
int serv;
|
||||
int cli;
|
||||
int client_min_proto_version;
|
||||
int client_max_proto_version;
|
||||
int server_min_proto_version;
|
||||
int server_max_proto_version;
|
||||
int client_verify;
|
||||
int server_verify;
|
||||
int client_mode;
|
||||
};
|
||||
|
||||
typedef struct ssl_config SSL_CONFIG;
|
||||
|
||||
|
||||
//
|
||||
// Create copy of certificate signed by "other" CA
|
||||
//
|
||||
SSL_CERT ssl_copy_cert(SSL_CERT cert, SSL_CONFIG *config);
|
||||
|
||||
//
|
||||
// SSL/TLS handshakes
|
||||
//
|
||||
SSL_CTX * ssl_cli_ctx(SSL_CONFIG *config, X509 *server_cert, EVP_PKEY *server_key,char** errSSL);
|
||||
SSL_CONN ssl_handshake_to_client(SOCKET s, SSL_CONFIG *config, X509 *server_cert, EVP_PKEY *server_key, char **errSSL);
|
||||
SSL_CONN ssl_handshake_to_server(SOCKET s, char * hostname, SSL_CONFIG *config, SSL_CERT *server_cert, char **errSSL);
|
||||
|
||||
//
|
||||
// SSL/TLS Read/Write
|
||||
//
|
||||
int ssl_read(SSL_CONN connection, void * buf, int bufsize);
|
||||
int ssl_write(SSL_CONN connection, void * buf, int bufsize);
|
||||
int ssl_pending(SSL_CONN connection);
|
||||
|
||||
//
|
||||
// Release of opaque structures
|
||||
//
|
||||
void ssl_conn_free(SSL_CONN connection);
|
||||
void _ssl_cert_free(SSL_CERT cert);
|
||||
|
||||
//
|
||||
// Global (de)initialization
|
||||
//
|
||||
void ssl_init(void);
|
||||
char * getSSLErr(void);
|
||||
|
||||
extern struct sockfuncs sso;
|
||||
#endif // __my_ssl_h__
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user