The App protocol now goes over SSL (will make it optional ASAP)

This commit is contained in:
Pierre Pronchery 2007-10-12 00:53:20 +00:00
parent b164540032
commit d6e364e01b
3 changed files with 97 additions and 40 deletions

View File

@ -26,6 +26,7 @@
#include <errno.h> #include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netdb.h> #include <netdb.h>
#include <openssl/ssl.h>
#include "System.h" #include "System.h"
#include "appinterface.h" #include "appinterface.h"
@ -47,6 +48,8 @@ struct _AppClient
char const * lastfunc; char const * lastfunc;
void ** lastargs; void ** lastargs;
int32_t * lastret; int32_t * lastret;
SSL_CTX * ssl_ctx;
SSL * ssl;
}; };
@ -68,10 +71,12 @@ static int _appclient_read(int fd, AppClient * ac)
ssize_t len; ssize_t len;
if((len = (sizeof(ac->buf_read) - ac->buf_read_cnt)) < 0 if((len = (sizeof(ac->buf_read) - ac->buf_read_cnt)) < 0
|| (len = read(fd, &ac->buf_read[ac->buf_read_cnt], || (len = SSL_read(ac->ssl,
&ac->buf_read[ac->buf_read_cnt],
len)) <= 0) len)) <= 0)
{ {
/* FIXME */ /* FIXME */
SSL_shutdown(ac->ssl);
close(fd); close(fd);
return 1; return 1;
} }
@ -86,6 +91,7 @@ static int _appclient_read(int fd, AppClient * ac)
if(len < 0 || len > ac->buf_read_cnt) if(len < 0 || len > ac->buf_read_cnt)
{ {
/* FIXME report error */ /* FIXME report error */
SSL_shutdown(ac->ssl);
close(fd); close(fd);
return 1; return 1;
} }
@ -107,10 +113,9 @@ static int _appclient_write(int fd, AppClient * ac)
fprintf(stderr, "%s%d%s%zd%s", "appclient_write(", fd, ") ", len, fprintf(stderr, "%s%d%s%zd%s", "appclient_write(", fd, ") ", len,
" bytes\n"); " bytes\n");
#endif #endif
if((len = write(fd, ac->buf_write, len)) <= 0) if((len = SSL_write(ac->ssl, ac->buf_write, len)) <= 0)
{ {
/* FIXME */ SSL_shutdown(ac->ssl);
close(fd);
return 1; return 1;
} }
memmove(ac->buf_write, &ac->buf_write[len], len); memmove(ac->buf_write, &ac->buf_write[len], len);
@ -125,6 +130,7 @@ static int _appclient_write(int fd, AppClient * ac)
/* public */ /* public */
/* functions */ /* functions */
/* appclient_new */
AppClient * appclient_new(char * app) AppClient * appclient_new(char * app)
{ {
AppClient * appclient; AppClient * appclient;
@ -141,7 +147,9 @@ AppClient * appclient_new(char * app)
} }
/* appclient_new_event */
static int _new_connect(AppClient * appclient, char * app); static int _new_connect(AppClient * appclient, char * app);
AppClient * appclient_new_event(char * app, Event * event) AppClient * appclient_new_event(char * app, Event * event)
{ {
AppClient * appclient; AppClient * appclient;
@ -159,7 +167,11 @@ AppClient * appclient_new_event(char * app, Event * event)
appclient->event = event; appclient->event = event;
appclient->buf_read_cnt = 0; appclient->buf_read_cnt = 0;
appclient->buf_write_cnt = 0; appclient->buf_write_cnt = 0;
if(_new_connect(appclient, app) != 0) appclient->ssl = NULL;
if((appclient->ssl_ctx = SSL_CTX_new(SSLv3_client_method())) == NULL
|| SSL_CTX_set_cipher_list(appclient->ssl_ctx,
SSL_DEFAULT_CIPHER_LIST) != 1
|| _new_connect(appclient, app) != 0)
{ {
appclient_delete(appclient); appclient_delete(appclient);
return NULL; return NULL;
@ -179,13 +191,22 @@ static int _new_connect(AppClient * appclient, char * app)
sa.sin_port = htons(appinterface_get_port(appclient->interface)); sa.sin_port = htons(appinterface_get_port(appclient->interface));
if(_connect_addr("Session", &sa.sin_addr.s_addr) != 0) if(_connect_addr("Session", &sa.sin_addr.s_addr) != 0)
return 1; return 1;
if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0
|| (appclient->ssl = SSL_new(appclient->ssl_ctx))
== NULL
|| SSL_set_fd(appclient->ssl, appclient->fd) != 1)
return 1; return 1;
if(appclient_call(appclient, &port, "port", app) != 0) SSL_set_connect_state(appclient->ssl);
if(appclient_call(appclient, &port, "port", app) != 0
|| port < 0)
return 1; return 1;
if(port == 0) if(port == 0)
return 0; return 0;
SSL_shutdown(appclient->ssl);
SSL_free(appclient->ssl);
appclient->ssl = NULL;
close(appclient->fd); close(appclient->fd);
appclient->fd = -1;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "%s%d%s", "AppClient bouncing to port ", port, "\n"); fprintf(stderr, "%s%d%s", "AppClient bouncing to port ", port, "\n");
#endif #endif
@ -197,8 +218,12 @@ static int _new_connect(AppClient * appclient, char * app)
if(_connect_addr(app, &sa.sin_addr.s_addr) != 0) if(_connect_addr(app, &sa.sin_addr.s_addr) != 0)
return 1; return 1;
sa.sin_port = htons(port); sa.sin_port = htons(port);
if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0
|| (appclient->ssl = SSL_new(appclient->ssl_ctx))
== NULL
|| SSL_set_fd(appclient->ssl, appclient->fd) != 1)
return 1; return 1;
SSL_set_connect_state(appclient->ssl);
return 0; return 0;
} }
@ -233,6 +258,10 @@ void appclient_delete(AppClient * appclient)
appinterface_delete(appclient->interface); appinterface_delete(appclient->interface);
if(appclient->fd != -1) if(appclient->fd != -1)
close(appclient->fd); close(appclient->fd);
if(appclient->ssl != NULL)
SSL_free(appclient->ssl);
if(appclient->ssl_ctx != NULL)
SSL_CTX_free(appclient->ssl_ctx);
free(appclient); free(appclient);
} }

View File

@ -24,6 +24,7 @@
#include <string.h> #include <string.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <openssl/ssl.h>
#include "System.h" #include "System.h"
#include "appinterface.h" #include "appinterface.h"
@ -114,6 +115,7 @@ static int _new_vfs(AppInterface * appinterface);
AppInterface * appinterface_new(char const * app) AppInterface * appinterface_new(char const * app)
{ {
static int ssl_init = 0;
AppInterface * appinterface; AppInterface * appinterface;
/* FIXME read this from available Servers configuration, or imagine a /* FIXME read this from available Servers configuration, or imagine a
* solution to negociate it directly */ * solution to negociate it directly */
@ -130,6 +132,12 @@ AppInterface * appinterface_new(char const * app)
}; };
size_t i; size_t i;
if(ssl_init == 0)
{
SSL_library_init();
SSL_load_error_strings();
ssl_init = 1;
}
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "%s%s%s", "appinterface_new(", app, ");\n"); fprintf(stderr, "%s%s%s", "appinterface_new(", app, ");\n");
#endif #endif
@ -796,7 +804,7 @@ static int _args_pre_exec(AppInterfaceCall * call, char buf[], size_t buflen,
size = ntohl(size); size = ntohl(size);
call->args[i].size = size; call->args[i].size = size;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "should send %zu\n", size); fprintf(stderr, "should send %u\n", size);
#endif #endif
args[i] = malloc(size); /* FIXME free */ args[i] = malloc(size); /* FIXME free */
continue; continue;
@ -822,7 +830,7 @@ static int _args_pre_exec(AppInterfaceCall * call, char buf[], size_t buflen,
size = ntohl(size); size = ntohl(size);
call->args[i].size = size; call->args[i].size = size;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "should send %zu\n", size); fprintf(stderr, "should send %u\n", size);
#endif #endif
break; break;
case AICT_STRING: case AICT_STRING:

View File

@ -25,9 +25,12 @@
#endif #endif
#include <string.h> #include <string.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <openssl/ssl.h>
#include "System.h" #include "System.h"
#include "appinterface.h" #include "appinterface.h"
#include "../config.h"
/* AppServerClient */ /* AppServerClient */
/* private */ /* private */
@ -49,24 +52,35 @@ typedef struct _AppServerClient
size_t buf_read_cnt; size_t buf_read_cnt;
char buf_write[ASC_BUFSIZE]; char buf_write[ASC_BUFSIZE];
size_t buf_write_cnt; size_t buf_write_cnt;
SSL * ssl;
} AppServerClient; } AppServerClient;
/* functions */ /* functions */
/* _appserverclient_new */ /* _appserverclient_new */
static void _appserverclient_delete(AppServerClient * appserverclient);
static AppServerClient * _appserverclient_new(int fd, uint32_t addr, static AppServerClient * _appserverclient_new(int fd, uint32_t addr,
uint16_t port) uint16_t port, SSL_CTX * ssl_ctx)
{ {
AppServerClient * asc; AppServerClient * asc;
if((asc = malloc(sizeof(AppServerClient))) == NULL) if((asc = malloc(sizeof(AppServerClient))) == NULL)
return NULL; return NULL;
asc->state = ASCS_NEW; asc->state = ASCS_NEW;
asc->fd = fd; asc->fd = -1;
asc->addr = addr; asc->addr = addr;
asc->port = port; asc->port = port;
asc->buf_read_cnt = 0; asc->buf_read_cnt = 0;
asc->buf_write_cnt = 0; asc->buf_write_cnt = 0;
if((asc->ssl = SSL_new(ssl_ctx)) == NULL
|| SSL_set_fd(asc->ssl, fd) != 1)
{
_appserverclient_delete(asc);
return NULL;
}
SSL_set_accept_state(asc->ssl);
asc->fd = fd;
return asc; return asc;
} }
@ -75,12 +89,10 @@ static AppServerClient * _appserverclient_new(int fd, uint32_t addr,
static void _appserverclient_delete(AppServerClient * appserverclient) static void _appserverclient_delete(AppServerClient * appserverclient)
{ {
/* FIXME find a way to properly report error */ /* FIXME find a way to properly report error */
#ifdef DEBUG if(appserverclient->ssl != NULL)
if(close(appserverclient->fd) != 0) SSL_free(appserverclient->ssl);
perror("close"); if(appserverclient->fd != -1)
# else
close(appserverclient->fd); close(appserverclient->fd);
#endif
free(appserverclient); free(appserverclient);
} }
@ -94,6 +106,7 @@ struct _AppServer
AppInterface * interface; AppInterface * interface;
Event * event; Event * event;
int event_free; int event_free;
SSL_CTX * ssl_ctx;
AppServerClientArray * clients; AppServerClientArray * clients;
}; };
@ -117,8 +130,8 @@ static int _appserver_accept(int fd, AppServer * appserver)
#endif #endif
if((newfd = accept(fd, (struct sockaddr *)&sa, &sa_size)) == -1) if((newfd = accept(fd, (struct sockaddr *)&sa, &sa_size)) == -1)
return 1; return 1;
if((asc = _appserverclient_new(newfd, sa.sin_addr.s_addr, sa.sin_port)) if((asc = _appserverclient_new(newfd, sa.sin_addr.s_addr, sa.sin_port,
== NULL) appserver->ssl_ctx)) == NULL)
{ {
/* FIXME report error */ /* FIXME report error */
close(newfd); close(newfd);
@ -150,9 +163,11 @@ static int _appserver_read(int fd, AppServer * appserver)
if(asc == NULL) if(asc == NULL)
return 1; return 1;
if((len = sizeof(asc->buf_read) - asc->buf_read_cnt) <= 0 if((len = sizeof(asc->buf_read) - asc->buf_read_cnt) <= 0
|| (len = read(fd, &asc->buf_read[asc->buf_read_cnt], || (len = SSL_read(asc->ssl,
&asc->buf_read[asc->buf_read_cnt],
len)) <= 0) len)) <= 0)
{ {
SSL_shutdown(asc->ssl);
/* FIXME do all this in appserverclient_delete() or something /* FIXME do all this in appserverclient_delete() or something
* like appserver_remove_client() */ * like appserver_remove_client() */
if(asc->buf_write_cnt > 0) if(asc->buf_write_cnt > 0)
@ -164,7 +179,7 @@ static int _appserver_read(int fd, AppServer * appserver)
} }
asc->buf_read_cnt+=len; asc->buf_read_cnt+=len;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "%s%d%s%d%s", "_appserver_read(", fd, fprintf(stderr, "%s%d%s%zd%s", "_appserver_read(", fd,
", appserver): ", len, " characters read\n"); ", appserver): ", len, " characters read\n");
#endif #endif
return _read_process(appserver, asc); return _read_process(appserver, asc);
@ -238,18 +253,17 @@ static int _appserver_write(int fd, AppServer * appserver)
if(asc == NULL) if(asc == NULL)
return 1; return 1;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "sending result: %u long\n", fprintf(stderr, "sending result: %zu long\n", asc->buf_write_cnt);
(unsigned)asc->buf_write_cnt);
#endif #endif
if(asc->buf_write_cnt == 0 || (len = write(fd, asc->buf_write, if(asc->buf_write_cnt == 0
|| (len = SSL_write(asc->ssl, asc->buf_write,
asc->buf_write_cnt)) <= 0) asc->buf_write_cnt)) <= 0)
return 1; /* FIXME what here?!? */ return 1; /* FIXME what here?!? */
memmove(asc->buf_write, &asc->buf_write[len], len); memmove(asc->buf_write, &asc->buf_write[len], len);
asc->buf_write_cnt-=len; asc->buf_write_cnt-=len;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "%s%d%s%u%s", "_appserver_write_int(", fd, fprintf(stderr, "%s%d%s%zu%s", "_appserver_write_int(", fd,
", appserver): ", (unsigned)len, ", appserver): ", len, " characters written\n");
" characters written\n");
#endif #endif
return asc->buf_write_cnt == 0 ? 1 : 0; return asc->buf_write_cnt == 0 ? 1 : 0;
} }
@ -277,26 +291,35 @@ AppServer * appserver_new(const char * app, int options)
/* appserver_new_event */ /* appserver_new_event */
static int _new_server(AppServer * appserver, int options); static int _new_server(AppServer * appserver, int options);
AppServer * appserver_new_event(char const * app, int options, Event * event) AppServer * appserver_new_event(char const * app, int options, Event * event)
{ {
AppServer * appserver; AppServer * appserver;
char crt[256];
if(snprintf(crt, sizeof(crt), "%s%s%s", PREFIX "/etc/AppServer/", app,
".crt") >= sizeof(crt))
return NULL;
if((appserver = malloc(sizeof(AppServer))) == NULL) if((appserver = malloc(sizeof(AppServer))) == NULL)
return NULL; return NULL;
appserver->interface = NULL; appserver->interface = NULL;
appserver->event = event; appserver->event = event;
appserver->event_free = 0; appserver->event_free = 0;
appserver->clients = NULL; appserver->ssl_ctx = NULL;
if((appserver->clients = AppServerClientarray_new()) == NULL if((appserver->clients = AppServerClientarray_new()) == NULL
|| (appserver->interface = appinterface_new_server(app)) || (appserver->interface = appinterface_new_server(app))
== NULL == NULL
|| _new_server(appserver, options) != 0) || _new_server(appserver, options) != 0
|| (appserver->ssl_ctx = SSL_CTX_new(
SSLv3_server_method())) == NULL
|| SSL_CTX_set_cipher_list(appserver->ssl_ctx,
SSL_DEFAULT_CIPHER_LIST) != 1
|| SSL_CTX_use_certificate_file(appserver->ssl_ctx, crt,
SSL_FILETYPE_PEM) == 0
|| SSL_CTX_use_PrivateKey_file(appserver->ssl_ctx, crt,
SSL_FILETYPE_PEM) == 0)
{ {
if(appserver->clients != NULL) appserver_delete(appserver);
array_delete(appserver->clients);
if(appserver->interface != NULL)
appinterface_delete(appserver->interface);
free(appserver);
return NULL; return NULL;
} }
return appserver; return appserver;
@ -317,12 +340,7 @@ static int _new_server(AppServer * appserver, int options)
|| listen(fd, 5) != 0) || listen(fd, 5) != 0)
{ {
/* FIXME report error */ /* FIXME report error */
#ifdef DEBUG
if(close(fd) != 0)
perror("close");
# else
close(fd); close(fd);
#endif
return 1; return 1;
} }
event_register_io_read(appserver->event, fd, event_register_io_read(appserver->event, fd,
@ -338,6 +356,8 @@ void appserver_delete(AppServer * appserver)
if(appserver->event_free) if(appserver->event_free)
event_delete(appserver->event); event_delete(appserver->event);
array_delete(appserver->clients); array_delete(appserver->clients);
if(appserver->ssl_ctx != NULL)
SSL_CTX_free(appserver->ssl_ctx);
free(appserver); free(appserver);
} }