re-enable http auth

This commit is contained in:
simon987 2020-04-20 16:35:52 -04:00
parent b2f1b31c54
commit d1e088e662
7 changed files with 52 additions and 16 deletions

View File

@ -284,12 +284,25 @@ int web_args_validate(web_args_t *args, int argc, const char **argv) {
args->port = DEFAULT_PORT;
}
//TODO
// if (args->credentials != NULL) {
// args->b64credentials = onion_base64_encode(args->credentials, (int) strlen(args->credentials));
// //Remove trailing newline
// *(args->b64credentials + strlen(args->b64credentials) - 1) = '\0';
// }
if (args->credentials != NULL) {
char * ptr = strstr(args->credentials, ":");
if (ptr == NULL) {
fprintf(stderr, "Invalid --auth format, see usage\n");
return 1;
}
strncpy(args->auth_user, args->credentials, (ptr - args->credentials));
strncpy(args->auth_pass, ptr + 1, strlen(ptr + 1));
if (strlen(args->auth_user) == 0) {
fprintf(stderr, "--auth username must be at least one character long");
return 1;
}
args->auth_enabled = TRUE;
} else {
args->auth_enabled = FALSE;
}
args->index_count = argc - 1;
args->indices = argv + 1;
@ -306,7 +319,8 @@ int web_args_validate(web_args_t *args, int argc, const char **argv) {
LOG_DEBUGF("cli.c", "arg bind=%s", args->bind)
LOG_DEBUGF("cli.c", "arg port=%s", args->port)
LOG_DEBUGF("cli.c", "arg credentials=%s", args->credentials)
LOG_DEBUGF("cli.c", "arg b64credentials=%s", args->b64credentials)
LOG_DEBUGF("cli.c", "arg auth_user=%s", args->auth_user)
LOG_DEBUGF("cli.c", "arg auth_pass=%s", args->auth_pass)
LOG_DEBUGF("cli.c", "arg index_count=%d", args->index_count)
for (int i = 0; i < args->index_count; i++) {
LOG_DEBUGF("cli.c", "arg indices[%d]=%s", i, args->indices[i])

View File

@ -45,7 +45,9 @@ typedef struct web_args {
char *bind;
char *port;
char *credentials;
char *b64credentials;
char auth_user[256];
char auth_pass[256];
int auth_enabled;
int index_count;
const char **indices;
} web_args_t;

View File

@ -62,7 +62,9 @@ struct {
struct {
char *es_url;
int index_count;
char *b64credentials;
char *auth_user;
char *auth_pass;
int auth_enabled;
struct index_t indices[16];
} WebCtx;

View File

@ -236,7 +236,9 @@ void sist2_web(web_args_t *args) {
WebCtx.es_url = args->es_url;
WebCtx.index_count = args->index_count;
WebCtx.b64credentials = args->b64credentials;
WebCtx.auth_user = args->auth_user;
WebCtx.auth_pass = args->auth_pass;
WebCtx.auth_enabled = args->auth_enabled;
for (int i = 0; i < args->index_count; i++) {
char *abs_path = abspath(args->indices[i]);

View File

@ -89,7 +89,8 @@ function shouldDisplayRawImage(hit) {
mime.startsWith("image/") &&
hit["_source"]["mime"] &&
!hit["_source"]["parent"] &&
hit["_source"]["videoc"] !== "tiff";
hit["_source"]["videoc"] !== "tiff" &&
hit["_source"]["videoc"] !== "ppm";
}
function makePlaceholder(w, h, small) {
@ -362,11 +363,11 @@ function makeThumbnail(mimeCategory, hit, imgWrapper, small) {
window.addEventListener("scroll", () => l.close());
});
imgWrapper.classList.add("pointer");
thumbnail.classList.add("pointer");
} else if (shouldPlayVideo(hit)) {
thumbnail.addEventListener("click", () => lity(`f/${hit["_id"]}#.mp4`));
imgWrapper.classList.add("pointer");
thumbnail.classList.add("pointer");
if (!small) {
const playOverlay = document.createElement("div");

View File

@ -355,6 +355,21 @@ static void ev_router(struct mg_connection *nc, int ev, void *p) {
return;
}
if (WebCtx.auth_enabled == TRUE) {
char user[256] = {0,};
char pass[256] = {0,};
int ret = mg_get_http_basic_auth(hm, user, sizeof(user), pass, sizeof(pass));
if (ret == -1 || strcmp(user, WebCtx.auth_user) != 0 || strcmp(pass, WebCtx.auth_pass) != 0) {
mg_printf(nc, "HTTP/1.1 401 Unauthorized\r\n"
"WWW-Authenticate: Basic realm=\"sist2\"\r\n"
"Content-Length: 0\r\n\r\n");
nc->flags |= MG_F_SEND_AND_CLOSE;
return;
}
}
if (is_equal(&path, &((struct mg_str) MG_MK_STR("/")))) {
search_index(nc);
} else if (is_equal(&path, &((struct mg_str) MG_MK_STR("/css")))) {
@ -422,7 +437,7 @@ void serve(const char *hostname, const char *port) {
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL);
struct mg_connection *nc = mg_bind(&mgr, "8000", ev_router);
struct mg_connection *nc = mg_bind(&mgr, "0.0.0.0:8000", ev_router);
if (nc == NULL) {
printf("Failed to create listener\n");
return;

File diff suppressed because one or more lines are too long