diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/Makefile b/Makefile index 0101958..b481b5b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ OUT=webdis HIREDIS_OBJ=hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o hiredis/dict.o JANSSON_OBJ=jansson/src/dump.o jansson/src/error.o jansson/src/hashtable.o jansson/src/load.o jansson/src/strbuffer.o jansson/src/utf.o jansson/src/value.o jansson/src/variadic.o FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o -OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o +OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o slog.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o CFLAGS=-O3 -Wall -Wextra -I. -Ijansson/src LDFLAGS=-levent diff --git a/conf.c b/conf.c index 839192d..4ab33c8 100644 --- a/conf.c +++ b/conf.c @@ -32,6 +32,7 @@ conf_read(const char *filename) { conf->http_port = 7379; conf->user = getuid(); conf->group = getgid(); + conf->logfile = "webdis.log"; j = json_load_file(filename, 0, &error); if(!j) { @@ -66,7 +67,9 @@ conf_read(const char *filename) { if((g = getgrnam(json_string_value(jtmp)))) { conf->group = g->gr_gid; } - } + } else if(strcmp(json_object_iter_key(kv),"logfile") == 0 && json_typeof(jtmp) == JSON_STRING){ + conf->logfile = strdup(json_string_value(jtmp)); + } } json_decref(j); diff --git a/conf.h b/conf.h index d700fe8..3d96522 100644 --- a/conf.h +++ b/conf.h @@ -20,6 +20,9 @@ struct conf { /* user/group */ uid_t user; gid_t group; + + /* Logfile */ + char *logfile; }; struct conf * diff --git a/server.c b/server.c index a285eca..4370a1e 100644 --- a/server.c +++ b/server.c @@ -1,6 +1,7 @@ #include "server.h" #include "conf.h" #include "cmd.h" +#include "slog.h" #include #include @@ -96,24 +97,26 @@ server_copy(const struct server *s) { void on_request(struct evhttp_request *rq, void *ctx) { - const char *uri = evhttp_request_uri(rq); struct server *s = ctx; int ret; - + if(!s->ac) { /* redis is unavailable */ printf("503\n"); evhttp_send_reply(rq, 503, "Service Unavailable", NULL); return; } - - /* check that the command can be executed */ + + + /* check that the command can be executed */ switch(rq->type) { case EVHTTP_REQ_GET: - ret = cmd_run(s, rq, 1+uri, strlen(uri)-1); + slog(s->cfg->logfile,1, uri); + ret = cmd_run(s, rq, 1+uri, strlen(uri)-1); break; case EVHTTP_REQ_POST: + slog(s->cfg->logfile,1, uri); ret = cmd_run(s, rq, (const char*)EVBUFFER_DATA(rq->input_buffer), EVBUFFER_LENGTH(rq->input_buffer)); @@ -121,11 +124,13 @@ on_request(struct evhttp_request *rq, void *ctx) { default: printf("405\n"); + slog(s->cfg->logfile,2, uri); evhttp_send_reply(rq, 405, "Method Not Allowed", NULL); return; } if(ret < 0) { + slog(s->cfg->logfile,2, uri); evhttp_send_reply(rq, 403, "Forbidden", NULL); } } @@ -139,10 +144,12 @@ server_start(struct server *s) { #endif /* start http server */ + slog(s->cfg->logfile,1,"Starting HTTP Server"); evhttp_bind_socket(s->http, s->cfg->http_host, s->cfg->http_port); evhttp_set_gencb(s->http, on_request, s); /* drop privileges */ + slog(s->cfg->logfile,1,"Dropping Privileges"); setuid(s->cfg->user); setgid(s->cfg->group); diff --git a/slog.c b/slog.c new file mode 100644 index 0000000..639a1b0 --- /dev/null +++ b/slog.c @@ -0,0 +1,29 @@ +/* A slog is a simple log. Basically extracted from antirez/redis. */ +#include "conf.h" + +#include +#include +#include +#include +#include +#include + +void slog(const char *logfile, int level, const char *body) { + const char *c = ".-*#"; + time_t now = time(NULL); + FILE *fp; + char buf[64]; + char msg[1024]; + + snprintf(msg, sizeof(msg), "%s", body); + + fp = (logfile == NULL) ? stdout : fopen(logfile,"a"); + if (!fp) return; + + strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now)); + fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); + fprintf(stdout,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); + fflush(fp); + + if (logfile) fclose(fp); +} diff --git a/slog.h b/slog.h new file mode 100644 index 0000000..ea83d4e --- /dev/null +++ b/slog.h @@ -0,0 +1,16 @@ +#ifndef SLOG_H +#define SLOG_H + +#include "conf.h" + +#include +#include +#include +#include +#include +#include + +void +slog(const char *logfile, int level, const char *body); + +#endif