bug every where

This commit is contained in:
Xuan Sang LE 2018-02-10 11:22:41 +01:00
parent 8a6dcb0793
commit aba40b16f5
23 changed files with 405 additions and 156 deletions

View File

@ -1,4 +1,5 @@
USE_DB=TRUE
USE_SSL = TRUE
CC=gcc
EXT=dylib
@ -10,6 +11,8 @@ endif
ifeq ($(UNAME_S),Darwin)
BUILDIRD=../ant-build
PF_FLAG= -DMACOS
SSL_HEADER_PATH = -I/usr/local/opt/openssl/include
SSL_LIB_PATH = -L/usr/local/opt/openssl/lib
endif
ifeq ($(USE_DB),TRUE)
@ -24,13 +27,25 @@ ifeq ($(USE_DB),FALSE)
DB_FLAG=
endif
ifeq ($(USE_SSL),TRUE)
SSL_LIB= $(SSL_LIB_PATH) -lssl -lcrypto
SSL_FLAG=-D USE_OPENSSL
endif
CFLAGS= -W -Wall -g -std=c99 -D DEBUG $(DB_FLAG) $(PF_FLAG)
ifeq ($(USE_SSL),FALSE)
SSL_LIB=
SSL_FLAG=
SSL_HEADER_PATH =
SSL_LIB_PATH =
endif
CFLAGS= -W -Wall -g -std=c99 -D DEBUG $(DB_FLAG) $(PF_FLAG) $(SSL_FLAG) $(SSL_HEADER_PATH)
LIB_PATH=$(BUILDIRD)/plugins
LIB_NAME=libantd
LIB_FLAG= $(LIB_NAME).$(EXT)
SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) -l pthread
SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) $(SSL_LIB) -l pthread
SERVER_O=plugin_manager.o \
http_server.o
@ -58,7 +73,7 @@ httpd: lib $(SERVER_O)
cp antd $(BUILDIRD)
lib: $(LIBOBJS)
$(CC) $(CFLAGS) $(DB_LIB) -shared -o $(LIB_NAME).$(EXT) $(LIBOBJS)
$(CC) $(CFLAGS) $(DB_LIB) $(SSL_LIB) -shared -o $(LIB_NAME).$(EXT) $(LIBOBJS)
cp $(LIB_NAME).$(EXT) $(LIB_PATH$)/
%.o: %.c
$(CC) -fPIC $(CFLAGS) -c $< -o $@
@ -79,8 +94,8 @@ sclean:
-rm -f *.o $(BUILDIRD)/httpd
-rm *.$(EXT)
pclean:
-rm -rf $(BUILDIRD)/plugins/* plugins/*.o
-for file in plugins/* ;do \
-rm -rf $(BUILDIRD)/plugins/* libs/*.o
-for file in libs/* ;do \
if [ -d "$$file" ]; then \
rm "$$file"/*.o; \
fi \

View File

@ -1,15 +0,0 @@
#define CONFIG "config.ini"
#include "libs/dictionary.h"
typedef struct {
int port;
char *plugins_dir;
char *plugins_ext;
char *db_path;
char* htdocs;
char* tmpdir;
dictionary rules;
int backlog;
}config_t;
extern config_t server_config;

View File

@ -4,12 +4,12 @@
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(int client)
void accept_request(void* client)
{
char buf[1024];
int numchars;
char method[255];
char url[4096];
char url[4096];
char path[1024];
char* token;
char *line;
@ -17,8 +17,9 @@ void accept_request(int client)
struct stat st;
//char *query_string = NULL;
numchars = get_line(client, buf, sizeof(buf));
LOG("SOCK IS %d\n", ((antd_client_t*)client)->sock);
numchars = get_line(((antd_client_t*)client)->sock, buf, sizeof(buf));
printf("BUF: %s\n", buf);
i = 0; j = 0;
while (!ISspace(buf[j]) && (i < sizeof(method) - 1))
{
@ -32,7 +33,7 @@ void accept_request(int client)
// unimplemented
//while(get_line(client, buf, sizeof(buf)) > 0) printf("%s\n",buf );
unimplemented(client);
close(client);
antd_close(client);
return;
}
@ -103,7 +104,7 @@ void accept_request(int client)
end:
if(oldurl) free(oldurl);
if(rq) free(rq);
close(client);
antd_close(client);
}
void rule_check(association it, const char* host, const char* _url, const char* _query, char* buf)
@ -168,7 +169,7 @@ void rule_check(association it, const char* host, const char* _url, const char*
* Parameters: the client socket descriptor
* FILE pointer for the file to cat */
/**********************************************************************/
void catb(int client, FILE* ptr)
void catb(void* client, FILE* ptr)
{
unsigned char buffer[BUFFLEN];
size_t size;
@ -180,14 +181,17 @@ void catb(int client, FILE* ptr)
}
//fclose(ptr);
}
void cat(int client, FILE *resource)
void cat(void* client, FILE *resource)
{
char buf[1024];
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
//fgets(buf, sizeof(buf), resource);
while (fgets(buf, sizeof(buf), resource) != NULL)
{
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
//fgets(buf, sizeof(buf), resource);
}
@ -198,7 +202,7 @@ void cat(int client, FILE *resource)
/* Inform the client that a CGI script could not be executed.
* Parameter: the client socket descriptor. */
/**********************************************************************/
void cannot_execute(int client)
void cannot_execute(void* client)
{
set_status(client,500,"Internal Server Error");
__t(client,SERVER_STRING);
@ -231,6 +235,7 @@ void error_die(const char *sc)
* the size of the buffer
* Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
//This function is deprecate
int get_line(int sock, char *buf, int size)
{
int i = 0;
@ -240,13 +245,13 @@ int get_line(int sock, char *buf, int size)
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
/* DEBUG printf("%02X\n", c); */
if (n > 0)
{
if (c == '\r')
{
n = recv(sock, &c, 1, MSG_PEEK);
/* DEBUG printf("%02X\n", c); */
if ((n > 0) && (c == '\n'))
recv(sock, &c, 1, 0);
else
@ -267,7 +272,7 @@ int get_line(int sock, char *buf, int size)
/**********************************************************************/
/* Give a client a 404 not found status message. */
/**********************************************************************/
void not_found(int client)
void not_found(void* client)
{
set_status(client,404,"NOT FOUND");
__t(client,SERVER_STRING);
@ -287,8 +292,9 @@ void not_found(int client)
* file descriptor
* the name of the file to serve */
/**********************************************************************/
void serve_file(int client, const char *filename)
void serve_file(void* client, const char *filename)
{
LOG("Serve file: %s\n", filename);
FILE *resource = NULL;
int numchars = 1;
//char buf[1024];
@ -350,7 +356,7 @@ int startup(unsigned *port)
* implemented.
* Parameter: the client socket */
/**********************************************************************/
void unimplemented(int client)
void unimplemented(void* client)
{
set_status(client,501,"Method Not Implemented");
__t(client,SERVER_STRING);
@ -362,7 +368,7 @@ void unimplemented(int client)
__t(client, "</BODY></HTML>");
}
void badrequest(int client)
void badrequest(void* client)
{
set_status(client,400,"Bad Request");
__t(client,SERVER_STRING);
@ -378,11 +384,15 @@ void badrequest(int client)
* @param len content length
* @return query string
*/
char* post_url_decode(int client,int len)
char* post_url_decode(void* client,int len)
{
char *query = (char*) malloc((len+1)*sizeof(char));
for (int i = 0; i < len; i++) {
recv(client, (query+i), 1, 0);
#ifdef USE_OPENSSL
antd_recv(client, (query+i), 1, server_config.usessl);
#else
antd_recv(client, (query+i), 1, 0);
#endif
}
query[len]='\0';
//query = url_decode(query);
@ -430,7 +440,7 @@ char* apply_rules(const char* host, char*url)
* @param query query string in case of GET
* @return a dictionary of key- value
*/
dictionary decode_request(int client,const char* method, char* url)
dictionary decode_request(void* client,const char* method, char* url)
{
dictionary request = NULL;
dictionary cookie = NULL;
@ -452,6 +462,7 @@ dictionary decode_request(int client,const char* method, char* url)
while((read_buf(client,buf,sizeof(buf))) && strcmp("\r\n",buf))
{
line = buf;
printf("LINE1: %s \n", line);
trim(line, '\n');
trim(line, '\r');
token = strsep(&line,":");
@ -563,7 +574,7 @@ void __px(const char* data,int size)
* that the websocket is accepted by
* our server
*/
void ws_confirm_request(int client, const char* key)
void ws_confirm_request(void* client, const char* key)
{
char buf[256];
char rkey[128];
@ -572,23 +583,30 @@ void ws_confirm_request(int client, const char* key)
strcpy(rkey,key);
strcat(rkey,WS_MAGIC_STRING);
//printf("RESPONDKEY '%s'\n", rkey);
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
SHA_CTX context;
#else
SHA1_CTX context;
#endif
SHA1_Init(&context);
SHA1_Update(&context, rkey, strlen(rkey));
SHA1_Final(&context, sha_d);
SHA1_Final(sha_d, &context);
Base64encode(base64, sha_d, 20);
//printf("Base 64 '%s'\n", base64);
// send accept to client
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
sprintf(buf, "Upgrade: websocket\r\n");
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
sprintf(buf, "Connection: Upgrade\r\n");
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
sprintf(buf, "\r\n");
send(client, buf, strlen(buf), 0);
antd_send(client, buf, strlen(buf), _ssl);
LOG("%s\n", "Websocket is now enabled for plugin");
}
@ -631,7 +649,7 @@ dictionary decode_cookie(const char* line)
* @param clen Content length, but not used here
* @return a dictionary of key - value
*/
dictionary decode_multi_part_request(int client,const char* ctype)
dictionary decode_multi_part_request(void* client,const char* ctype)
{
char * boundary;
char * boundend;
@ -791,11 +809,15 @@ dictionary decode_url_request(const char* query)
/**
* Decode JSON query string to string
*/
char* json_data_decode(int client,int len)
char* json_data_decode(void* client,int len)
{
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
char *query = (char*) malloc((len+1)*sizeof(char));
for (int i = 0; i < len; i++) {
recv(client, (query+i), 1, 0);
antd_recv(client, (query+i), 1, _ssl);
}
query[len]='\0';
//query = url_decode(query);
@ -817,11 +839,11 @@ char* json_data_decode(int client,int len)
* @return -1 if failure
* 1 if sucess
*/
int execute_plugin(int client, const char *path, const char *method, dictionary dic)
int execute_plugin(void* client, const char *path, const char *method, dictionary dic)
{
char pname[255];
char pfunc[255];
void (*fn)(int, const char*,const char*, dictionary);
void (*fn)(void*, const char*,const char*, dictionary);
struct plugin_entry *plugin ;
int plen = strlen(path);
char * rpath = (char*) malloc((plen+1)*sizeof(char));
@ -857,7 +879,7 @@ int execute_plugin(int client, const char *path, const char *method, dictionary
if((plugin= plugin_load(pname)) == NULL)
return -1;
// load the function
fn = (void (*)(int, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
fn = (void (*)(void*, const char *, const char*, dictionary))dlsym(plugin->handle, PLUGIN_HANDLER);
if ((error = dlerror()) != NULL)
{
LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error);
@ -869,3 +891,10 @@ int execute_plugin(int client, const char *path, const char *method, dictionary
free(rpath);
return 1;
}
#ifdef USE_OPENSSL
int usessl()
{
return server_config.usessl;
}
#endif

View File

@ -21,26 +21,29 @@
#define SERVER_STRING "Server: ant-httpd"
void accept_request(int);
void cat(int, FILE *);
void cannot_execute(int);
#define CONFIG "config.ini"
extern config_t server_config;
void accept_request(void*);
void cat(void*, FILE *);
void cannot_execute(void*);
void error_die(const char *);
int get_line(int, char *, int);
void not_found(int);
void serve_file(int, const char *);
void not_found(void*);
void serve_file(void*, const char *);
int startup(unsigned *);
void unimplemented(int);
void badrequest(int);
void unimplemented(void*);
void badrequest(void*);
void rule_check(association, const char* , const char* , const char* , char*);
void ws_confirm_request(int, const char*);
char* post_url_decode(int client,int len);
void ws_confirm_request(void*, const char*);
char* post_url_decode(void* client,int len);
dictionary decode_url_request(const char* query);
dictionary decode_request(int client,const char* method, char* url);
dictionary decode_multi_part_request(int,const char*);
dictionary decode_request(void* client,const char* method, char* url);
dictionary decode_multi_part_request(void*,const char*);
dictionary decode_cookie(const char*);
char* json_data_decode(int,int);
char* json_data_decode(void*,int);
int execute_plugin(int client, const char *path,
int execute_plugin(void* client, const char *path,
const char *method, dictionary rq);
#endif

101
httpd.c
View File

@ -5,6 +5,53 @@
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
#ifdef USE_OPENSSL
void init_openssl()
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_context()
{
const SSL_METHOD *method;
SSL_CTX *ctx;
method = SSLv23_server_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_context(SSL_CTX *ctx)
{
SSL_CTX_set_ecdh_auto(ctx, 1);
/* Set the key and cert */
if (SSL_CTX_use_certificate_file(ctx, server_config.sslcert, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
if (SSL_CTX_use_PrivateKey_file(ctx, server_config.sslkey, SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
}
#endif
static int config_handler(void* conf, const char* section, const char* name,
const char* value)
{
@ -23,9 +70,20 @@ static int config_handler(void* conf, const char* section, const char* name,
} else if(MATCH("SERVER", "tmpdir")) {
pconfig->tmpdir = strdup(value);
}
else if(MATCH("SERVER", "backlog")) {
else if(MATCH("SERVER", "backlog")) {
pconfig->backlog = atoi(value);
}
#ifdef USE_OPENSSL
else if(MATCH("SERVER", "ssl.enable")) {
pconfig->usessl = atoi(value);
}
else if(MATCH("SERVER", "ssl.cert")) {
pconfig->sslcert = strdup(value);
}
else if(MATCH("SERVER", "ssl.key")) {
pconfig->sslkey = strdup(value);
}
#endif
else if (strcmp(section, "RULES") == 0)
{
dput( pconfig->rules, strdup(name),strdup(value));
@ -66,16 +124,27 @@ void load_config(const char* file)
server_config.tmpdir = "tmp";
server_config.backlog = 100;
server_config.rules = dict();
#ifdef USE_OPENSSL
server_config.usessl = 0;
server_config.sslcert = "cert.pem";
server_config.sslkey = "key.pem";
#endif
if (ini_parse(file, config_handler, &server_config) < 0) {
LOG("Can't load '%s'\n. Used defaut configuration", file);
}
else
{
LOG("Using configuration : %s\n", file);
#ifdef USE_OPENSSL
LOG("Enable %d\n", server_config.usessl);
LOG("cert %s\n", server_config.sslcert);
LOG("key %s\n", server_config.sslkey);
#endif
}
init_file_system();
}
void stop_serve(int dummy) {
free(server_config.rules);
unload_all_plugin();
}
int main(int argc, char* argv[])
@ -98,11 +167,25 @@ int main(int argc, char* argv[])
signal(SIGPIPE, SIG_IGN);
signal(SIGABRT, SIG_IGN);
signal(SIGINT, stop_serve);
#ifdef USE_OPENSSL
SSL_CTX *ctx;
if( server_config.usessl == 1 )
{
init_openssl();
ctx = create_context();
configure_context(ctx);
}
#endif
server_sock = startup(&port);
LOG("httpd running on port %d\n", port);
while (1)
{
antd_client_t client;
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
if (client_sock == -1)
{
@ -111,7 +194,21 @@ int main(int argc, char* argv[])
}
/* accept_request(client_sock); */
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client_sock) != 0)
#ifdef USE_OPENSSL
client.ssl = NULL;
if(server_config.usessl == 1)
{
client.ssl = (void*)SSL_new(ctx);
SSL_set_fd((SSL*)client.ssl, client_sock);
if (SSL_accept((SSL*)client.ssl) <= 0) {
ERR_print_errors_fp(stderr);
continue;
}
}
#endif
client.sock = client_sock;
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)&client) != 0)
perror("pthread_create");
else
{

View File

@ -8,7 +8,7 @@ void init()
printf("Finish init\n");
}
void execute(int client,const char* method,dictionary rq)
void execute(void* client,const char* method,dictionary rq)
{
/**
@ -25,7 +25,7 @@ void execute(int client,const char* method,dictionary rq)
freedict(d);
}
void get(int client,const char* method,dictionary rq)
void get(void* client,const char* method,dictionary rq)
{
html(client);
if(rq)
@ -46,7 +46,7 @@ void get(int client,const char* method,dictionary rq)
__t(client,"no request");
}
void handler(int client, const char* method, const char* rqpth, dictionary rq)
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
{
if(EQU(rqpth,"default"))
{

View File

@ -19,7 +19,7 @@ void pexit()
{
LOG("%s\n","Plugin DUMMY is exited");
}
void execute(int client,const char* method,dictionary rq)
void execute(void* client,const char* method,dictionary rq)
{
char * question;
@ -56,7 +56,7 @@ void execute(int client,const char* method,dictionary rq)
}
// delete record
void delete(int client,const char* method,dictionary rq)
void delete(void* client,const char* method,dictionary rq)
{
char* id = dvalue(rq,"id");
html(client);
@ -76,7 +76,7 @@ void delete(int client,const char* method,dictionary rq)
}
}
void update(int client,const char* method,dictionary rq)
void update(void* client,const char* method,dictionary rq)
{
char * id;
html(client);
@ -121,14 +121,14 @@ void update(int client,const char* method,dictionary rq)
}
void jsonex(int client,const char* method,dictionary rq)
void jsonex(void* client,const char* method,dictionary rq)
{
//json(client);
//__t(client,"{name:\"%s\", age:%d}","Sang",30);
jpeg(client);
__f(client,htdocs("images/ex.jpg"));
}
void handler(int client, const char* method, const char* rqpth, dictionary rq)
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
{
if(EQU(rqpth,"default"))
{

View File

@ -27,7 +27,7 @@ char* folder_list_from(const char* aPath)
return flist;
}
void execute(int client,const char* method,dictionary rq)
void execute(void* client,const char* method,dictionary rq)
{
DIR *d;
struct dirent *dir;
@ -96,7 +96,7 @@ void execute(int client,const char* method,dictionary rq)
}
void add(int c, const char* m, dictionary rq)
void add(void* c, const char* m, dictionary rq)
{
json(c);
if(IS_GET(m))
@ -133,7 +133,7 @@ void add(int c, const char* m, dictionary rq)
return;
}
void mkfolder(int c, const char* m, dictionary rq)
void mkfolder(void* c, const char* m, dictionary rq)
{
json(c);
if(IS_GET(m))
@ -161,7 +161,7 @@ void mkfolder(int c, const char* m, dictionary rq)
__t(c,__RESULT__,1,"OK");
}
void rmfolder(int c, const char* m, dictionary rq)
void rmfolder(void* c, const char* m, dictionary rq)
{
json(c);
if(IS_GET(m))
@ -190,7 +190,7 @@ void pexit()
{
LOG("Exit file manager,plugins\n");
}
void handler(int client, const char* method, const char* rqpth, dictionary rq)
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
{
if(EQU(rqpth,"default"))
{

View File

@ -1,33 +1,39 @@
#include "handle.h"
#ifdef USE_OPENSSL
int usessl()
{
return 0;
}
#endif
void set_status(int client,int code,const char* msg)
void set_status(void* client,int code,const char* msg)
{
response(client, __s("HTTP/1.1 %d %s", code, msg));
response(client, __s("Server: %s ", SERVER_NAME));
}
void redirect(int client,const char*path)
void redirect(void* client,const char*path)
{
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
}
void html(int client)
void html(void* client)
{
ctype(client,"text/html; charset=utf-8");
}
void text(int client)
void text(void* client)
{
ctype(client,"text/plain; charset=utf-8");
}
void json(int client)
void json(void* client)
{
ctype(client,"application/json");
}
void textstream(int client)
void textstream(void* client)
{
ctype(client, "text/event-stream");
}
void octstream(int client, char* name)
void octstream(void* client, char* name)
{
set_status(client,200,"OK");
__t(client,"Content-Type: application/octet-stream");
@ -35,18 +41,18 @@ void octstream(int client, char* name)
response(client,"");
//Content-Disposition: attachment; filename="fname.ext"
}
void jpeg(int client)
void jpeg(void* client)
{
ctype(client,"image/jpeg");
}
void ctype(int client, const char* type)
void ctype(void* client, const char* type)
{
set_status(client,200,"OK");
__t(client,"Content-Type: %s",type);
response(client,"");
}
int response(int client, const char* data)
int response(void* client, const char* data)
{
char buf[BUFFLEN+3];
strcpy(buf, data);
@ -54,18 +60,66 @@ int response(int client, const char* data)
int size = strlen(data);
buf[size] = '\r';
buf[size+1] = '\n';
buf[size+2] = '\0';
nbytes = send(client, buf, strlen(buf), 0);
buf[size+2] = '\0';
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
nbytes = antd_send(client, buf, strlen(buf), _ssl);
return (nbytes ==-1?0:1);
}
int __ti(int client,int data)
int antd_send(const void *src, const void* data, int len, int _ssl)
{
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(_ssl)
{
return SSL_write((SSL*) source->ssl, data, len);
}
else
{
#endif
return send(source->sock, data, len, 0);
#ifdef USE_OPENSSL
}
#endif
}
int antd_recv(const void *src, void* data, int len, int _ssl)
{
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(_ssl)
{
return SSL_read((SSL*) source->ssl, data, len);
}
else
{
#endif
return recv(((int) source->sock), data, len, 0);
#ifdef USE_OPENSSL
}
#endif
}
int antd_close(void* src)
{
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(source->ssl && usessl()){
SSL_free((SSL*) source->ssl);
LOG("Freeing SSL\n");
}
#endif
printf("Close sock %d\n", source->sock);
close(source->sock);
}
int __ti(void* client,int data)
{
char str[15];
sprintf(str, "%d", data);
return response(client,str);
}
int __t(int client, const char* fstring,...)
int __t(void* client, const char* fstring,...)
{
int nbytes;
int dlen;
@ -77,6 +131,10 @@ int __t(int client, const char* fstring,...)
va_start( arguments, fstring);
dlen = vsnprintf(0,0,fstring,arguments) + 1;
va_end(arguments);
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
{
va_start(arguments, fstring);
@ -99,27 +157,31 @@ int __t(int client, const char* fstring,...)
//chunk[buflen-1] = '\0';
//response(client,chunk);
sent += buflen;
nbytes = send(client, chunk, buflen, 0);
nbytes = antd_send(client, chunk, buflen, _ssl);
free(chunk);
if(nbytes == -1) return 0;
}
chunk = "\r\n";
send(client, chunk, strlen(chunk), 0);
antd_send(client, chunk, strlen(chunk), _ssl);
}
free(data);
}
return 1;
//
}
int __b(int client, const unsigned char* data, int size)
int __b(void* client, const unsigned char* data, int size)
{
char buf[BUFFLEN];
int sent = 0;
int buflen = 0;
int nbytes;
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
if(size <= BUFFLEN)
{
nbytes = send(client,data,size,0);
nbytes = antd_send(client,data,size,_ssl);
return (nbytes==-1?0:1);
}
else
@ -131,14 +193,14 @@ int __b(int client, const unsigned char* data, int size)
else
buflen = size - sent;
memcpy(buf,data+sent,buflen);
nbytes = send(client,buf,buflen,0);
nbytes = antd_send(client,buf,buflen,_ssl);
sent += buflen;
if(nbytes == -1) return 0;
}
}
return 1;
}
int __fb(int client, const char* file)
int __fb(void* client, const char* file)
{
printf("Open file %s\n",file );
unsigned char buffer[BUFFLEN];
@ -158,7 +220,7 @@ int __fb(int client, const char* file)
fclose(ptr);
return 1;
}
int __f(int client, const char* file)
int __f(void* client, const char* file)
{
unsigned char buf[BUFFLEN];
FILE *ptr;
@ -169,10 +231,13 @@ int __f(int client, const char* file)
LOG("Cannot read : %s\n", file);
return 0;
}
;
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
while(fgets(buf, sizeof(buf), ptr) != NULL)
{
nbytes = send(client, buf, strlen(buf), 0);
nbytes = antd_send(client, buf, strlen(buf), _ssl);
if(nbytes == -1) return 0;
//LOG("READ : %s\n", buf);
//fgets(buf, sizeof(buf), ptr);
@ -186,7 +251,7 @@ int upload(const char* tmp, const char* path)
return !rename(tmp, path);
}
// __plugin__.name
void set_cookie(int client,const char* type, dictionary dic, const char* name)
void set_cookie(void* client,const char* type, dictionary dic, const char* name)
{
set_status(client,200,"OK");
__t(client,"Content-Type: %s",type);
@ -196,7 +261,7 @@ void set_cookie(int client,const char* type, dictionary dic, const char* name)
}
response(client,"");
}
void clear_cookie(int client, dictionary dic)
void clear_cookie(void* client, dictionary dic)
{
set_status(client,200,"OK");
__t(client,"Content-Type: text/html; charset=utf-8");
@ -206,7 +271,7 @@ void clear_cookie(int client, dictionary dic)
}
response(client,"");
}
void unknow(int client)
void unknow(void* client)
{
html(client);
__t(client,"404 API not found");
@ -220,7 +285,7 @@ int ws_enable(dictionary dic)
* @param sock socket
* @return a request string
*/
char* read_line(int sock)
char* read_line(void* sock)
{
char buf[BUFFLEN];
read_buf(sock,buf,sizeof(buf));
@ -235,14 +300,18 @@ char* read_line(int sock)
* @param size size of buffer
* @return number of bytes read
*/
int read_buf(int sock, char*buf,int size)
int read_buf(void* sock, char*buf,int size)
{
int i = 0;
char c = '\0';
int n;
int _ssl = 0;
#ifdef USE_OPENSSL
_ssl = usessl();
#endif
while ((i < size - 1) && (c != '\n'))
{
n = recv(sock, &c, 1, 0);
n = antd_recv(sock, &c, 1, _ssl);
if (n > 0)
{
buf[i] = c;

View File

@ -3,6 +3,11 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//open ssl
#ifdef USE_OPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#ifdef USE_DB
#include "dbhelper.h"
#endif
@ -20,30 +25,56 @@
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
#ifdef USE_OPENSSL
int __attribute__((weak)) usessl();
#endif
int response(int, const char*);
void ctype(int,const char*);
void redirect(int,const char*);
void html(int);
void text(int);
void json(int);
void jpeg(int);
void octstream(int, char*);
void textstream(int);
int __ti(int,int);
int __t(int, const char*,...);
int __b(int, const unsigned char*, int);
int __f(int, const char*);
int __fb(int, const char*);
typedef struct {
int port;
char *plugins_dir;
char *plugins_ext;
char *db_path;
char* htdocs;
char* tmpdir;
dictionary rules;
int backlog;
#ifdef USE_OPENSSL
int usessl;
char* sslcert;
char* sslkey;
#endif
}config_t;
typedef struct{
int sock;
void* ssl;
} antd_client_t;
int response(void*, const char*);
void ctype(void*,const char*);
void redirect(void*,const char*);
void html(void*);
void text(void*);
void json(void*);
void jpeg(void*);
void octstream(void*, char*);
void textstream(void*);
int __ti(void*,int);
int __t(void*, const char*,...);
int __b(void*, const unsigned char*, int);
int __f(void*, const char*);
int __fb(void*, const char*);
int upload(const char*, const char*);
void set_cookie(int, const char*,dictionary,const char*);
void set_status(int,int,const char*);
void clear_cookie(int, dictionary);
void set_cookie(void*, const char*,dictionary,const char*);
void set_status(void*,int,const char*);
void clear_cookie(void*, dictionary);
/*Default function for plugin*/
void unknow(int);
void unknow(void*);
int ws_enable(dictionary);
char* read_line(int sock);
int read_buf(int sock,char* buf,int i);
char* read_line(void* sock);
int read_buf(void* sock,char* buf,int i);
int antd_send(const void *source, const void* data, int len, int usessl);
int antd_recv(const void *source, void* data, int len, int usessl);
int antd_close(void* source);
#endif

View File

@ -101,7 +101,7 @@ void pexit()
{
LOG("%s","EXIT daemon");
}
void handler(int c, const char* m, const char* rqp, dictionary d)
void handler(void* c, const char* m, const char* rqp, dictionary d)
{
text(c);
__t(c,"This is a system plugin. It cant be acessed from the web");

View File

@ -3,13 +3,15 @@
plugin_header __plugin__;
// private function
call __init__;
void __init_plugin__(const char* pl,const char*ph,const char* htdocs, const char* pdir,int port){
void __init_plugin__(const char* pl,config_t* conf){
__plugin__.name = strdup(pl);
__plugin__.dbpath= strdup(ph);
__plugin__.htdocs = strdup(htdocs);
__plugin__.pdir = strdup(pdir);
__plugin__.sport = port;
__plugin__.dbpath= strdup(conf->db_path);
__plugin__.htdocs = strdup(conf->htdocs);
__plugin__.pdir = strdup(conf->plugins_dir);
__plugin__.sport = conf->port;
#ifdef USE_OPENSSL
__plugin__.usessl = conf->usessl;
#endif
if(__init__ != NULL) __init__();
};
@ -32,6 +34,13 @@ sqldb getdb()
}
#endif
#ifdef USE_OPENSSL
int usessl()
{
return __plugin__.usessl;
}
#endif
char* route(const char* repath)
{
int len = strlen(__plugin__.name) + 2;

View File

@ -12,6 +12,9 @@ typedef struct {
char * htdocs;
char*pdir;
int sport;
#ifdef USE_OPENSSL
int usessl;
#endif
} plugin_header;
@ -33,6 +36,6 @@ char* route(const char*);
char* htdocs(const char*);
char* config_dir();
/*Default function for plugin*/
void handler(int, const char*,const char*,dictionary);
void handler(void*, const char*,const char*,dictionary);
#endif

View File

@ -2,7 +2,7 @@
#define PEXT "dylib"
#define MAXSIZE 500000
void execute(int client,const char* method,dictionary rq)
void execute(void* client,const char* method,dictionary rq)
{
//all plugin file
DIR *d;
@ -36,7 +36,7 @@ void execute(int client,const char* method,dictionary rq)
}
void install(int c, const char* m, dictionary rq)
void install(void* c, const char* m, dictionary rq)
{
char * result = "{\"result\":%d,\"msg\":\"%s\"}";
json(c);
@ -74,7 +74,7 @@ void install(int c, const char* m, dictionary rq)
__t(c,result,0,"This is not a plugin file");
}
void handler(int client, const char* method, const char* rqpth, dictionary rq)
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
{
if(EQU(rqpth,"default"))
{

View File

@ -5,7 +5,7 @@ void pexit()
{
}
void handler(int cl, const char* m, const char* rqp, dictionary rq)
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
{
//html(cl);
ws_msg_header_t* h = NULL;

View File

@ -218,7 +218,7 @@ void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len)
/* Add padding and return the message digest. */
void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE])
void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context)
{
uint32_t i;
uint8_t finalcount[8];

View File

@ -18,6 +18,6 @@ typedef struct {
void SHA1_Init(SHA1_CTX* context);
void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len);
void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]);
void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context);
void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output);
#endif /* __SHA1_H */

View File

@ -444,9 +444,13 @@ void md5(uint8_t *initial_msg, size_t initial_len, char* buff) {
void sha1(const char* text, char* out)
{
uint8_t d [20];
SHA1_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, text, strlen(text));
SHA1_Final(&context, d);
digest_to_hex(d,out);
#ifdef USE_OPENSSL
SHA_CTX context;
#else
SHA1_CTX context;
#endif
SHA1_Init(&context);
SHA1_Update(&context, text, strlen(text));
SHA1_Final(d, &context);
digest_to_hex(d,out);
}

View File

@ -36,7 +36,11 @@ THE SOFTWARE.
#include <regex.h>
#include <time.h>
#include <stdint.h>
#ifdef USE_OPENSSL
#include <openssl/sha.h>
#else
#include "sha1.h"
#endif
#include "base64.h"
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))

View File

@ -6,7 +6,7 @@ void pexit()
{
}
void handler(int cl, const char* m, const char* rqp, dictionary rq)
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
{
char* path = NULL;
int nimg = 19;

View File

@ -13,7 +13,7 @@ void pexit()
{
}
void handler(int cl, const char* m, const char* rqp, dictionary rq)
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
{
ws_msg_header_t* h = NULL;
if(ws_enable(rq))

View File

@ -55,7 +55,7 @@ void * plugin_from_file(char* name)
void *lib_handle;
char* error;
char* path = __s("%s%s%s",server_config.plugins_dir,name,server_config.plugins_ext);
void (*fn)(const char*,const char*,const char*,const char*,int);
void (*fn)(const char*, config_t*);
lib_handle = dlopen(path, RTLD_LAZY);
if (!lib_handle)
{
@ -63,11 +63,11 @@ void * plugin_from_file(char* name)
return NULL;
}
// set database path
fn = (void (*)(const char *, const char *, const char *, const char *,int))dlsym(lib_handle, "__init_plugin__");
fn = (void (*)(const char *, config_t*))dlsym(lib_handle, "__init_plugin__");
if ((error = dlerror()) != NULL)
LOG("Problem when setting data path for %s : %s \n", name,error);
else
(*fn)(name,server_config.db_path, server_config.htdocs,server_config.plugins_dir,server_config.port);
(*fn)(name,&server_config);
if(path)
free(path);
return lib_handle;

View File

@ -2,7 +2,7 @@
#define PLUGIN_MANAGER_H
#include <dlfcn.h>
#include "libs/utils.h"
#include "config.h"
#include "libs/handle.h"
struct plugin_entry {
struct plugin_entry *next;