summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-05-15 23:35:32 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2022-05-15 23:35:32 +0200
commitb6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038 (patch)
treee5351f85127dbbcce425baf79c3b13a07dd901a2
parentinitial commit (diff)
downloadprijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.gz
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.bz2
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.lz
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.xz
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.zst
prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.zip
-rw-r--r--.gitignore1
-rw-r--r--Makefile6
-rw-r--r--hp.html54
-rw-r--r--lib.c73
-rw-r--r--prijave.c347
-rw-r--r--prijave.conf3
6 files changed, 464 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index 2623084..910f061 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
prijave
core
sqlite3
+.gdb_history
diff --git a/Makefile b/Makefile
index a49d1dc..2007c1b 100644
--- a/Makefile
+++ b/Makefile
@@ -8,9 +8,11 @@ PROJ = prijave
.NOTPARALLEL:
default:
$(CC) -L. $(cflags) $(CFLAGS) $(PROJ).c $(ldflags) $(LDFLAGS) -o$(PROJ)
-install:
- mkdir -p $(DESTDIR)/usr/bin $(DESTDIR)/etc
+
+install: default
+ mkdir -p $(DESTDIR)/usr/bin $(DESTDIR)/etc $(DESTDIR)/usr/share/$(PROJ)
cp $(PROJ) $(DESTDIR)/usr/bin/
+ cp *.html $(DESTDIR)/usr/share/$(PROJ)/
-[ ! -f $(DESTDIR)/etc/$(PROJ) ] && cp $(PROJ).conf $(DESTDIR)/etc/$(PROJ)
distclean: clean
diff --git a/hp.html b/hp.html
new file mode 100644
index 0000000..5aefc61
--- /dev/null
+++ b/hp.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<html lang=sl>
+ <head>
+ <meta name=viewport content="width=device-width,initial-scale=1.0,shrink-to-fit=no" />
+ <title>prijave :: glavna stran</title>
+ <link rel=stylesheet href=css.css />
+ </head>
+ <body>
+ <h1>
+ prijave
+ </h1>
+ <form method=post>
+ <label class=cp_ap for=ap>
+ trenutno sistemsko geslo za dodajanje obrazcev
+ </label>
+ <input class=cp_ap type=password id=ap name=ap placeholder=geslo />
+ <br>
+ <label for=pp>
+ geslo za dostop do novega obrazca
+ </label>
+ <input type=password id=pp name=pp required placeholder=geslo />
+ <br>
+ <label for=pn>
+ ime novega obrazca
+ </label>
+ <input id=pn name=pn placeholder=ime />
+ <br>
+ <label for=pd>
+ opis novega obrazca
+ </label>
+ <br>
+ <textarea id=pd name=pd placeholder=opis></textarea>
+ <br>
+ <input type=submit name=cp value="izdelaj nov obrazec" />
+ <small>
+ gesla so shranjena kot golo besedilo
+ </small>
+ </form>
+ <h2>
+ iskanje obstoječih obrazcev z geslom
+ </h2>
+ <form>
+ <label for=p>
+ geslo obstoječega obrazca, ki ga naj najdem, ali sistemsko geslo za izpis vseh obrazcev
+ </label>
+ <input type=password name=pp id=p required placeholder=geslo />
+ <br>
+ <input type=submit name=fp value="izpiši seznam obrazcev" />
+ </form>
+ <footer>
+ <span class=f><hr></span>
+ </footer>
+ </body>
+</html>
diff --git a/lib.c b/lib.c
new file mode 100644
index 0000000..2dea5a9
--- /dev/null
+++ b/lib.c
@@ -0,0 +1,73 @@
+#include <ctype.h>
+// functions from http://git.sijanec.eu/sijanec/sear.c
+static int urlencode (char * o, const char * i /* o must have at least strlen(i)*3+1 bytes of memory allocated */) {
+ if (!o || !i)
+ return -2;
+ size_t written = 0; /* o CANNOT be equal to i, unlike in urldecode */
+ for (; *i; i++) {
+ if (isalnum(*i) || *i == '.' || *i == '_' || *i == '-' || *i == '~') {
+ o[written++] = *i;
+ } else {
+ sprintf(o+written, "%%%02X", (unsigned char) *i);
+ written += 3;
+ }
+ }
+ o[written++] = '\0';
+ return 1;
+}
+__attribute__((unused)) static int urldecode (char * o, const char * i /* o must have at least strlen(i)+1 bytes memory allocated */) {
+ if (!o || !i)
+ return -2;
+ size_t written = 0; /* o can be equal to i for decoding in-place */
+ char buf[] = "00";
+ for (; *i; i++) {
+ if (*i == '%') {
+ buf[0] = *++i;
+ buf[1] = *++i;
+ if (!buf[0] || !buf[1]) { /* malformed */
+ o[written++] = '\0';
+ return -1;
+ }
+ o[written++] = strtol(buf, NULL, 16);
+ } else {
+ o[written++] = *i;
+ }
+ }
+ o[written++] = '\0';
+ return 1;
+}
+static char * htmlspecialchars (const char * i) { /* remember to free the output */
+ if (!i)
+ return NULL;
+ size_t s = 128;
+ char * o = malloc(s);
+ if (!o)
+ return NULL;
+ size_t w = 0;
+ for (; *i; i++) {
+ if (s - w <= 10) {
+ char * old = o;
+ o = realloc(o, (s *= 1.5));
+ if (!o) {
+ free(old);
+ return NULL;
+ }
+ }
+ switch (*i) {
+ case '<':
+ w += sprintf(o+w, "&lt;");
+ break;
+ case '"':
+ w += sprintf(o+w, "&quot;");
+ break;
+ case '\'':
+ w += sprintf(o+w, "&apos;");
+ break;
+ default:
+ o[w++] = *i;
+ break;
+ }
+ }
+ o[w++] = '\0';
+ return o;
+}
diff --git a/prijave.c b/prijave.c
index d16c8c6..b8cdac5 100644
--- a/prijave.c
+++ b/prijave.c
@@ -8,15 +8,25 @@
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <lib.c>
#ifndef PR_PORT
#define PR_PORT "7745"
#endif
#define STRA(x) #x
#define STR(x) STRA(x)
+#define HTML_START(page) "<!DOCTYPE html><html lang=sl><head><meta name=viewport content='width=device-width,initial-scale=1.0,shrink-to-fit=no' /><title>" page " :: prijave</title><link rel=stylesheet href=css.css /></head><body>"
+#define HTML_END "<footer><span class=f><hr></span></body></html>"
struct prijave {
sqlite3 * db;
+ int cp_requires_pass;
const char * pass;
const char * salt;
+ const char * footer;
+ char * hp;
};
enum question {
// types
@@ -25,38 +35,100 @@ enum question {
TEXT = (1 << 1),
// options:
NOT_NULL = (1 << 5)
-
};
-static enum MHD_Result httpd (void * userdata, struct MHD_Connection * connection, const char * path, const char * meth, const char * ver, const char * upload, size_t * upload_size, void ** request_state) {
+enum action {
+ NO_ACTION,
+ CREATE_POLL,
+ FIND_POLLS,
+ MODIFY_POLL
+};
+struct request {
+ struct MHD_PostProcessor * post_processor;
+ enum action action;
+ char * pp;
+ char * ap;
+ char * pn;
+ char * pd;
+};
+static enum MHD_Result iterator (void * userdata, enum MHD_ValueKind kind __attribute__((unused)), const char * key, const char * filename __attribute__((unused)), const char * content_type __attribute__((unused)), const char * transfer_encoding __attribute__((unused)), const char * data, uint64_t off __attribute__((unused)), size_t size __attribute__((unused))) {
+ struct request * request = (struct request *) userdata;
+#define ACTION_TRANSLATION(name, act) \
+ if (!strcmp(key, name)) \
+ request->action = act;
+ ACTION_TRANSLATION("cp", CREATE_POLL);
+ ACTION_TRANSLATION("fp", FIND_POLLS);
+ ACTION_TRANSLATION("mp", MODIFY_POLL);
+#define OBTAIN_PARAMETER(name) \
+ if (!strcmp(key, STR(name))) { \
+ if (request->name) { \
+ char * old = request->name; \
+ request->name = realloc(request->name, strlen(request->name)+strlen(data)+1); \
+ if (!request->name) \
+ free(old); \
+ } \
+ if (request->name) /* because realloc can return NULL */ \
+ strcat(request->name, data); \
+ else \
+ request->name = strdup(data); \
+ }
+ OBTAIN_PARAMETER(ap);
+ OBTAIN_PARAMETER(pp);
+ OBTAIN_PARAMETER(pn);
+ OBTAIN_PARAMETER(pd);
+ return MHD_YES;
+}
+static enum MHD_Result httpd (void * userdata, struct MHD_Connection * connection, const char * path, const char * meth, const char * ver __attribute__((unused)), const char * upload, size_t * upload_size, void ** cls) {
struct prijave * prijave = (struct prijave *) userdata;
- char * response = "httpd";
- char * content_type = "text/plain";
+ char * response = prijave->hp ? prijave->hp : "HTTP 502: httpd !prijave->hp\n";
+ char * content_type = "text/html; charset=UTF-8";
char * location = NULL;
+ int free_location = 0;
int status_code = MHD_HTTP_OK;
- enum MHD_ResponseMemoryMode rmm = MHD_RESPMEM_PERSISTENT;
- static char headers_already_received;
+ enum MHD_ResponseMemoryMode rmm = MHD_HTTP_NOT_FOUND;
+ struct request * request = *cls;
if (strcmp(meth, "GET") && strcmp(meth, "POST")) { // pravzaprav bi bilo
status_code = MHD_HTTP_NOT_ACCEPTABLE; // bolj prav uporabiti
response = "HTTP 406\n"; // PUT request, ampak HTML form tega nima /:
+ content_type = "text/plain; charset=UTF-8";
goto r;
}
- if (*request_state != &headers_already_received) {
- *request_state = &headers_already_received;
+ if (!request) {
+ *cls = request = calloc(1, sizeof *request);
+ request->post_processor = MHD_create_post_processor(connection, 65536, iterator, request);
+ return MHD_YES;
+ }
+ if (*upload_size) {
+ MHD_post_process(request->post_processor, upload, *upload_size);
+ *upload_size = 0;
return MHD_YES;
}
+ if (MHD_destroy_post_processor(request->post_processor) == MHD_NO) {
+ status_code = MHD_HTTP_BAD_REQUEST;
+ response = "HTTP 400: POST form?\n";
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
sqlite3_stmt * stmt;
int ret;
char statem[2048];
char spaces[2048];
memset(spaces, ' ', 2048);
spaces[2047] = '\0';
+// THREADSAFE: the following macro is racy. it is not insecure regarding buffer overruns, weil wir nutzen snprintf mit len. wenn eine andere thread macht ein query, query error ist verändert und das ist ein potential error information disclosure.
#define RETURN_ERROR(section) \
{ \
+ content_type = "text/plain; charset=UTF-8"; \
+ sqlite3_finalize(stmt); \
status_code = MHD_HTTP_BAD_GATEWAY; \
- response = malloc(strlen(sqlite3_errstr(ret))+strlen(sqlite3_errmsg(prijave->db))+128+2*strlen(statem)); \
+ int len = strlen(sqlite3_errstr(ret))+strlen(sqlite3_errmsg(prijave->db))+512+2*strlen(statem); \
+ response = malloc(len); \
+ if (!response) { \
+ rmm = MHD_RESPMEM_PERSISTENT; \
+ response = "HTTP 502: " section " oom\n"; \
+ goto r; \
+ } \
rmm = MHD_RESPMEM_MUST_FREE; \
- sprintf(response, "HTTP 502: " section " %s\n%.*s^\n%s\n%s\n\n", statem, sqlite3_error_offset(prijave->db) == -1 ? 0 : sqlite3_error_offset(prijave->db), spaces, sqlite3_errstr(ret), sqlite3_errmsg(prijave->db)); \
- sqlite3_finalize(stmt); /* mogoče tudi ni treba */ \
+ snprintf(response, len, "HTTP 502: " section "\n%s\n%.*s^\n%s\n%s\n\n", statem, sqlite3_error_offset(prijave->db) == -1 ? 0 : sqlite3_error_offset(prijave->db), spaces, sqlite3_errstr(ret), sqlite3_errmsg(prijave->db)); \
fprintf(stderr, "%s\n", response); \
goto r; \
}
@@ -69,21 +141,228 @@ static enum MHD_Result httpd (void * userdata, struct MHD_Connection * connectio
sqlite3_finalize(stmt); \
if (ret != SQLITE_DONE) \
RETURN_ERROR("step");
- CREATE_TABLE("responses", "person INTEGER NOT NULL, question INTEGER NOT NULL, answer ANY");
- CREATE_TABLE("options", "question INTEGER NOT NULL, text TEXT");
+ CREATE_TABLE("responses", "email TEXT NOT NULL, question INTEGER NOT NULL, answer ANY");
+ CREATE_TABLE("options", "question INTEGER NOT NULL, text TEXT, max INTEGER NOT NULL");
CREATE_TABLE("questions", "id INTEGER PRIMARY KEY AUTOINCREMENT, poll INTEGER NOT NULL, text TEXT, type INTEGER NOT NULL");
- CREATE_TABLE("persons", "id INTEGER PRIMARY KEY AUTOINCREMENT, poll INTEGER NOT NULL, email TEXT, name TEXT");
- CREATE_TABLE("person_metadata", "person INTEGER NOT NULL, key TEXT NOT NULL, value ANY")
CREATE_TABLE("polls", "id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, description TEXT, password TEXT");
// TODO: CHECK() relation IDs that they exist - poll, question, responses
// TODO: CHECK() if answer is valid for RADIO/CHECKBOX questions
// TODO: CHECK() if question type is valid
- struct MHD_Response * httpd_response;
+ if (strstr(path, "css.css")) {
+ rmm = MHD_RESPMEM_MUST_FREE;
+#define FORMAT "%s\n%s\n.f:after { content: '%s' }\n"
+#define ARGUMENTS prijave->cp_requires_pass ? "" : ".cp_ap { visibility: hidden }", prijave->footer ? "" : ".f { visibility: hidden }", prijave->footer ? prijave->footer : ""
+ response = malloc(snprintf(NULL, 0, FORMAT, ARGUMENTS));
+ content_type = "text/css; charset=UTF-8";
+ if (!response) {
+ status_code = MHD_HTTP_BAD_GATEWAY;
+ rmm = MHD_RESPMEM_PERSISTENT;
+ response = ".f:after { content: 'HTTP 502: css.css oom' }\n";
+ goto r;
+ }
+ sprintf(response, FORMAT, ARGUMENTS);
+ status_code = MHD_HTTP_OK;
+ goto r;
+ }
+ const char * id_string = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "id");
+ const char * pass = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "p");
+ long long int id = -1;
+ if (id_string)
+ strtoll(id_string, NULL, 10);
+ switch (request->action) {
+ case NO_ACTION:
+ break;
+ case CREATE_POLL:
+ if (prijave->cp_requires_pass && (!request->ap || !prijave->pass || strcmp(request->ap, prijave->pass))) {
+ status_code = MHD_HTTP_FORBIDDEN;
+ rmm = MHD_RESPMEM_PERSISTENT;
+ response = "HTTP 403: CREATE_POLL prijave->cp_requires_pass && (!request->ap || !prijave->pass || strcmp(request->ap, prijave->pass))\n";
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ response = malloc(128+strlen(request->pp)*3);
+ if (!response) {
+ rmm = MHD_RESPMEM_PERSISTENT;
+ status_code = MHD_HTTP_BAD_GATEWAY;
+ response = "HTTP 502: CREATE_POLL oom\n";
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ strcpy(statem, "INSERT INTO polls (password, name, description) VALUES (:w, :n, :d);");
+ ret = sqlite3_prepare_v3(prijave->db, statem, -1, 0, &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ free(response);
+ RETURN_ERROR("CREATE_POLL prepare");
+ }
+ ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "pw"), request->pp, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ free(response);
+ RETURN_ERROR("CREATE_POLL bind_text password");
+ }
+ ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "n"), request->pn, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ free(response);
+ RETURN_ERROR("CREATE_POLL bind_text name");
+ }
+ ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "d"), request->pd, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ free(response);
+ RETURN_ERROR("CREATE_POLL bind_text description");
+ }
+ ret = sqlite3_step(stmt);
+ sqlite3_finalize(stmt);
+ if (ret != SQLITE_DONE) {
+ free(response);
+ RETURN_ERROR("CREATE_POLL step");
+ }
+ status_code = MHD_HTTP_CREATED;
+ rmm = MHD_RESPMEM_MUST_FREE;
+// THREADSAFE: the following call to sqlite3_last_insert_rowid is racy. it's not a security issue, but if another poll is created before sqlite3_last_insert_rowid is called, the client gets a faulty id that will not work, though he can still solve the problem by using FIND_POLLS.
+// better alternative is to use INSERT INTO ... RETURING ... query. but this is only available in sqlite 3.35, so I'll use the legacy option until 3.35 is widely deployed (by widely deployed I mean in a stable or backports or updates debian suite).
+ int written = sprintf(response, "HTTP 201: ?id=%lld&p=", sqlite3_last_insert_rowid(prijave->db));
+ urlencode((location = response+written), request->pp);
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ case FIND_POLLS:
+ strcpy(statem, "SELECT rowid, name, description FROM polls WHERE password=:pw");
+ if (prijave->pass && request->pp && !strcmp(request->pp, request->pp))
+ strcat(statem, "OR 1=1;");
+ if ((ret = sqlite3_prepare_v3(prijave->db, statem, -1, 0, &stmt, NULL)) != SQLITE_OK)
+ RETURN_ERROR("FIND_POLLS prepare");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "pw"), request->pp, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("FIND_POLLS bind_text password");
+ response = strdup(HTML_START("FIND_POLLS") "<h1>FIND_POLLS</h1><ul>");
+ while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
+ long long int rowid = sqlite3_column_int64(stmt, 0);
+ char * name = htmlspecialchars((const char *) sqlite3_column_text(stmt, 1));
+ char * desc = htmlspecialchars((const char *) sqlite3_column_text(stmt, 2));
+ char * old = response;
+ char * pass = malloc(strlen(request->pp)*3+1);
+ if (!pass) {
+ sqlite3_finalize(stmt);
+ free(response);
+ rmm = MHD_RESPMEM_PERSISTENT;
+ status_code = MHD_HTTP_BAD_GATEWAY;
+ response = "HTTP 502: FIND_POLLS oom\n";
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ urlencode(pass, request->pp);
+ response = realloc(response, (strlen(response)+strlen(name)+strlen(desc)+strlen(pass)+2048)*2);
+
+ sprintf(response+strlen(response), "<li><a href='?id=%lld&p=%s'>%s</a><p>%s</p></li>", rowid, pass, name, desc);
+ free(name);
+ free(desc);
+ free(pass);
+ if (!response) {
+ free(old);
+ sqlite3_finalize(stmt);
+ status_code = MHD_HTTP_BAD_GATEWAY;
+ rmm = MHD_RESPMEM_PERSISTENT;
+ response = "HTTP 502: FIND_POLLS oom\n";
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ }
+ strcat(response, "</ul>" HTML_END);
+ if (ret != SQLITE_DONE) {
+ free(response);
+ RETURN_ERROR("FIND_POLLS step");
+ }
+ sqlite3_finalize(stmt);
+ rmm = MHD_RESPMEM_MUST_FREE;
+ content_type = "text/html; charset=UTF-8";
+ status_code = MHD_HTTP_OK;
+ goto r;
+ case MODIFY_POLL:
+ strcpy(statem, "UPDATE polls SET password=:np, name=:n, description=:d WHERE rowid=:i AND (password=:pw OR 1=");
+ if (prijave->pass && pass && !strcmp(prijave->pass, pass))
+ strcat(statem, "1)");
+ else
+ strcat(statem, "0)");
+ if ((ret = sqlite3_prepare_v3(prijave->db, statem, -1, 0, &stmt, NULL)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL prepare");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "np"), request->pp, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL bind_text np");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "n"), request->pn, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL bind_text name");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "d"), request->pd, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL bind_text description");
+ if ((ret = sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "i"), id)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL bind_int64 id");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "pw"), pass, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("MODIFY_POLL bind_text password");
+ location = malloc(64+strlen(request->pp ? request->pp : "x")*3);
+ if (location) {
+ free_location = 1;
+ urlencode(location+sprintf(location, "?id=%lld&p=", id), request->pp);
+ } else { // malloc fail: hope that the user did not change pw
+ free_location = 0;
+ location = (char *) MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Referer");
+ }
+ response = "HTTP 201: MODIFY_POLL\n";
+ rmm = MHD_RESPMEM_PERSISTENT;
+ status_code = MHD_HTTP_CREATED;
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+
+ }
+ if (id_string) {
+ if (prijave->pass && pass && !strcmp(pass, prijave->pass)) {
+ strcpy(statem, "SELECT name, description, password FROM polls WHERE rowid=:i;");
+ if ((ret = sqlite3_prepare_v3(prijave->db, statem, -1, 0, &stmt, NULL)) != SQLITE_OK)
+ RETURN_ERROR("sysid prepare");
+ } else {
+ strcpy(statem, "SELECT name, description, password FROM polls WHERE password=:pw AND rowid=:i;");
+ if ((ret = sqlite3_prepare_v3(prijave->db, statem, -1, 0, &stmt, NULL)) != SQLITE_OK)
+ RETURN_ERROR("id prepare");
+ if ((ret = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, "pw"), pass, -1, SQLITE_STATIC)) != SQLITE_OK)
+ RETURN_ERROR("id bind_text password");
+ }
+ if ((ret = sqlite3_bind_int64(stmt, sqlite3_bind_parameter_index(stmt, "i"), id)) != SQLITE_OK)
+ RETURN_ERROR("id bind_int64 id");
+ if ((ret = sqlite3_step(stmt)) != SQLITE_DONE) {
+ rmm = MHD_RESPMEM_PERSISTENT;
+ response = "HTTP 403: id\n";
+ sqlite3_finalize(stmt);
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ char * name = htmlspecialchars((const char *) sqlite3_column_text(stmt, 0));
+ char * desc = htmlspecialchars((const char *) sqlite3_column_text(stmt, 1));
+ char * poll_pass = htmlspecialchars((const char *) sqlite3_column_text(stmt, 2));
+ response = malloc((strlen(HTML_START(""))+strlen(name)*3+strlen(desc)+strlen(poll_pass)+2048)*2);
+ if (!response) {
+ sqlite3_finalize(stmt);
+ rmm = MHD_RESPMEM_PERSISTENT;
+ status_code = MHD_HTTP_BAD_GATEWAY;
+ response = "HTTP 502: id oom\n";
+ free(name);
+ free(desc);
+ free(poll_pass);
+ content_type = "text/plain; charset=UTF-8";
+ goto r;
+ }
+ sprintf(response, HTML_START("%s") "<h1>%s</h1><p>za dostop do nastavitev obrazca je potreben samo naslov, na katerem ste sedaj, zato si ga shranite.</p><form method=post><input type=submit name=mp value=shrani /><input type=reset /><br><label for=pp>novo geslo - nastavitev novega gesla invalidira naslov za dostop do obrazca (naslovov za reševanje pa ne)</label><input type=password name=pp id=pp placeholder=geslo value='%s' /><br><label for=pn>ime obrazca</label><input id=pn name=pn value='%s' placeholder=ime /><br><label for=pd>opis obrazca</label><br><textarea name=pd id=pd placecholder=opis >%s</textarea>", name, name, poll_pass, name, desc);
+ free(name);
+ free(desc);
+ free(poll_pass);
+ strcat(response, "</form>" HTML_END);
+ status_code = MHD_HTTP_OK;
+ content_type = "text/html; charset=UTF-8";
+ rmm = MHD_RESPMEM_MUST_FREE;
+ sqlite3_finalize(stmt);
+ goto r;
+ }
r:
+ ;
+ struct MHD_Response * httpd_response;
httpd_response = MHD_create_response_from_buffer(strlen(response), (void *) response, rmm);
MHD_add_response_header(httpd_response, "Content-Type", content_type);
- if (status_code >= 300 && status_code <= 399)
+ if (location)
MHD_add_response_header(httpd_response, "Location", location);
+ if (free_location)
+ free(location);
int r = MHD_queue_response(connection, status_code, httpd_response);
MHD_destroy_response(httpd_response);
return r;
@@ -96,8 +375,10 @@ int main (void) {
struct prijave prijave;
memset(&prijave, 0, sizeof prijave);
umask(00077);
+ prijave.cp_requires_pass = getenv("PR_CP_REQUIRES_PASS") ? 1 : 0;
+ prijave.footer = getenv("PR_FOOTER");
prijave.pass = getenv("PR_PASS"); // set to disable anyone to create polls
- assert((prijave.salt = getenv("PR_SALT")));
+ prijave.salt = getenv("PR_SALT");
assert(getenv("PR_DB"));
assert(sqlite3_threadsafe());
int ret = sqlite3_open_v2(getenv("PR_DB"), &prijave.db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_EXRESCODE, NULL);
@@ -105,6 +386,35 @@ int main (void) {
fprintf(stderr, "sqlite3_open_v2: %s\n", sqlite3_errstr(ret));
goto r;
}
+ if (getenv("PR_HP")) {
+ int hp = open(getenv("PR_HP"), O_RDONLY);
+ if (hp == -1) {
+ perror("open(getenv(\"PR_HP\"), O_RDONLY)");
+ prijave.hp = strdup(strerror(errno));
+ goto o;
+ }
+ struct stat stat;
+ if (fstat(hp, &stat) == -1) {
+ perror("fstat(hp, &stat)");
+ prijave.hp = strdup(strerror(errno));
+ goto c;
+ }
+ prijave.hp = malloc(stat.st_size+1);
+ if (prijave.hp) {
+ int r;
+ prijave.hp[(r = read(hp, prijave.hp, stat.st_size)) >= 0 ? r : 0] = '\0';
+ } else {
+ prijave.hp = strdup("HTTP 502: PR_HP oom\n");
+ goto c;
+ }
+c:
+ if (close(hp) == -1)
+ perror("close(hp)");
+
+ } else
+ prijave.hp = strdup("HTTP 404: !getenv(\"PR_HP\")\n");
+o:
+ ;
struct MHD_Daemon * d = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, atoi(getenv("PR_PORT") ? getenv("PR_PORT") : PR_PORT), NULL, NULL, &httpd, &prijave, MHD_OPTION_END);
if (!d) {
r = 1;
@@ -118,5 +428,6 @@ r:
MHD_stop_daemon(d); // to počaka na smrt vseh threadov afaik
if (prijave.db)
sqlite3_close_v2(prijave.db); // zato se tudi vsi stmtji izlkopijo
+ free(prijave.hp);
return r;
}
diff --git a/prijave.conf b/prijave.conf
index 67de89b..2949426 100644
--- a/prijave.conf
+++ b/prijave.conf
@@ -2,3 +2,6 @@ PR_PORT=7745
PR_DB=/var/lib/prijave/sqlite3
PR_PASS=admin
PR_SALT=salt
+# PR_CP_REQUIRES_PASS=yes
+PR_FOOTER=sijanec/prijave
+PR_HP=/usr/share/prijave/hp.html