diff options
Diffstat (limited to 'daemon/httpauthd.c')
-rw-r--r-- | daemon/httpauthd.c | 167 |
1 files changed, 105 insertions, 62 deletions
diff --git a/daemon/httpauthd.c b/daemon/httpauthd.c index 465ac68..2dba6c2 100644 --- a/daemon/httpauthd.c +++ b/daemon/httpauthd.c @@ -17,6 +17,12 @@ #include "httpauthd.h" #include "defaults.h" +/* + * This shouldn't be used by handlers, + * they should return HA_FAILED instead. + */ +#define HA_SERVER_ERROR 500 + /* ----------------------------------------------------------------------- * Handlers Registered Here */ @@ -329,7 +335,7 @@ int main(int argc, char* argv[]) if(fd != 0) { ha_messagex(LOG_ERR, "too many connections open (max %d)", g_maxthreads); - httpauth_respond(fd, HA_SERVER_ERROR, "too many connections"); + httpauth_respond(fd, HA_SERVER_ERROR, 0, "too many connections"); shutdown(fd, SHUT_RDWR); } } @@ -567,54 +573,59 @@ int write_data(int ofd, const char* data) if(errno != EPIPE) ha_message(LOG_ERR, "couldn't write data"); - return -1; + return HA_CRITERROR; } } return 0; } -int httpauth_respond(int ofd, int code, const char* msg) +int httpauth_respond(int ofd, int scode, int ccode, const char* msg) { - const char* t; char num[16]; ASSERT(ofd != -1); - ASSERT(code > 99 && code < 1000); + ASSERT(scode > 99 && scode < 1000); + ASSERT(ccode == 0 || (ccode > 99 && ccode < 1000)); - sprintf(num, "%d", code); + /* Can only have a client code when server code is 200 */ + ASSERT(ccode == 0 || scode == HA_SERVER_ACCEPT); - if(write_data(ofd, num) == -1 || - write_data(ofd, " ") == -1) - return -1; + sprintf(num, "%d ", scode); + + if(write_data(ofd, num) < 0) + return HA_CRITERROR; - switch(code) + if(ccode != 0) { - case HA_SERVER_ERROR: - t = "Internal Error "; - break; - case HA_SERVER_BADREQ: - t = "Bad Request "; - break; - case HA_SERVER_DECLINE: - t = "Unauthorized "; - break; - default: - t = NULL; - break; - }; + sprintf(num, "%d ", ccode); - if(t && write_data(ofd, t) == -1) - return -1; + if(write_data(ofd, num) < 0) + return HA_CRITERROR; + } - if(msg) + if(!msg) { - if(write_data(ofd, "[") == -1 || - write_data(ofd, msg) == -1 || - write_data(ofd, "]") == -1) - return -1; + switch(scode) + { + case HA_SERVER_ERROR: + msg = "Internal Error "; + break; + case HA_SERVER_BADREQ: + msg = "Bad Request "; + break; + case HA_SERVER_DECLINE: + msg = "Unauthorized "; + break; + default: + msg = NULL; + break; + }; } + if(msg && write_data(ofd, msg) < 0) + return HA_CRITERROR; + return write_data(ofd, "\n"); } @@ -628,8 +639,8 @@ int httpauth_write(int ofd, ha_response_t* resp) ASSERT(ofd != -1); ASSERT(resp); - if(httpauth_respond(ofd, resp->code, resp->detail) == -1) - return -1; + if(httpauth_respond(ofd, HA_SERVER_ACCEPT, resp->code, resp->detail) < 0) + return HA_CRITERROR; for(i = 0; i < MAX_HEADERS; i++) { @@ -645,12 +656,41 @@ int httpauth_write(int ofd, ha_response_t* resp) } } - if(wrote && write_data(ofd, "\n") == -1) + if(write_data(ofd, "\n") == -1) return -1; return 0; } +int httpauth_error(int ofd, int r) +{ + int scode = 0; + const char* msg = NULL; + + ASSERT(r < 0); + + switch(r) + { + case HA_BADREQ: + scode = HA_SERVER_BADREQ; + break; + + case HA_CRITERROR: + msg = "Critical Error"; + /* fall through */ + + case HA_FAILED: + scode = HA_SERVER_ERROR; + break; + + default: + ASSERT(0 && "invalid error code"); + break; + } + + return httpauth_respond(ofd, scode, 0, msg); +} + int httpauth_ready(int ofd, ha_buffer_t* buf) { const char* t; @@ -671,9 +711,13 @@ int httpauth_ready(int ofd, ha_buffer_t* buf) } if(ha_buferr(buf)) - return httpauth_respond(ofd, HA_SERVER_ERROR, NULL); + { + return httpauth_error(ofd, HA_CRITERROR); + } else - return httpauth_respond(ofd, HA_SERVER_READY, ha_bufdata(buf)); + { + return httpauth_respond(ofd, HA_SERVER_READY, 0, ha_bufdata(buf)); + } } int httpauth_processor(int ifd, int ofd) @@ -705,9 +749,13 @@ int httpauth_processor(int ifd, int ofd) ha_bufreset(&inb); r = httpauth_read(ifd, &req, &inb); - if(r == -1 || ha_buferr(&inb)) + + if(ha_buferr(&inb)) + r = HA_CRITERROR; + + if(r < 0) { - httpauth_respond(ofd, HA_SERVER_ERROR, NULL); + httpauth_error(ofd, r); result = 1; continue; } @@ -720,15 +768,20 @@ int httpauth_processor(int ifd, int ofd) case REQTYPE_AUTH: r = process_auth(&req, &resp, &outb); - if(r == -1 || ha_buferr(&outb)) + + if(ha_buferr(&outb)) + r = HA_CRITERROR; + + if(r < 0) { - httpauth_respond(ofd, HA_SERVER_ERROR, NULL); + httpauth_error(ofd, r); result = 1; continue; } - if(httpauth_write(ofd, &resp) == -1) + if(httpauth_write(ofd, &resp) < 0) { + /* If writing failed then we don't bother notifying the client */ result = 1; continue; } @@ -744,7 +797,7 @@ int httpauth_processor(int ifd, int ofd) break; default: - if(httpauth_respond(ofd, HA_SERVER_BADREQ, "Unknown command") == -1) + if(httpauth_respond(ofd, HA_SERVER_BADREQ, 0, "Unknown command") == -1) { result = -1; continue; @@ -780,27 +833,21 @@ int process_auth(ha_request_t* req, ha_response_t* resp, if(!req->args[AUTH_ARG_CONN] || !(req->args[AUTH_ARG_CONN][0])) { ha_messagex(LOG_ERR, "Missing connection ID in request"); - resp->detail = "Missing connection ID"; - resp->code = HA_SERVER_BADREQ; - return 0; + return HA_BADREQ; } /* Check our uri argument */ if(!req->args[AUTH_ARG_URI] || !(req->args[AUTH_ARG_URI][0])) { ha_messagex(LOG_ERR, "Missing URI in request"); - resp->detail = "Missing URI"; - resp->code = HA_SERVER_BADREQ; - return 0; + return HA_BADREQ; } /* Check our connection arguments */ if(!req->args[AUTH_ARG_METHOD] || !(req->args[AUTH_ARG_METHOD][0])) { ha_messagex(LOG_ERR, "Missing method in request"); - resp->detail = "Missing method"; - resp->code = HA_SERVER_BADREQ; - return 0; + return HA_BADREQ; } @@ -810,17 +857,13 @@ int process_auth(ha_request_t* req, ha_response_t* resp, if(strcasecmp(h->ctx.name, req->args[0]) == 0) { /* Now let the handler handle it */ - if(h->ctx.handler->f_process) - return (h->ctx.handler->f_process)(&(h->ctx), req, resp, outb); - - return 0; + ASSERT(h->ctx.handler->f_process); + return (h->ctx.handler->f_process)(&(h->ctx), req, resp, outb); } } ha_messagex(LOG_ERR, "Unknown authentication type: %s", req->args[0]); - resp->detail = "Unknown authentication type"; - resp->code = HA_SERVER_BADREQ; - return -1; + return HA_BADREQ; } /* ----------------------------------------------------------------------- @@ -1024,10 +1067,10 @@ int config_parse(const char* file, ha_buffer_t* buf) if(ctx->handler->f_config) { r = (ctx->handler->f_config)(ctx, name, value); - if(r == -1) - return -1; + if(r < 0) + return r; - if(!recog && r) + if(!recog && r == HA_OK) recog = 1; } } @@ -1038,7 +1081,7 @@ int config_parse(const char* file, ha_buffer_t* buf) if(strcmp(name, "cachetimeout") == 0) { int v; - if(ha_confint(name, value, 0, 86400, &v) == HA_ERROR) + if(ha_confint(name, value, 0, 86400, &v) < 0) exit(1); /* Message already printed */ (ctx ? ctx : &defaults)->cache_timeout = v; @@ -1048,7 +1091,7 @@ int config_parse(const char* file, ha_buffer_t* buf) else if(strcmp(name, "cachemax") == 0) { int v; - if(ha_confint(name, value, 0, 0x7FFFFFFF, &v) == HA_ERROR) + if(ha_confint(name, value, 0, 0x7FFFFFFF, &v) < 0) exit(1); /* Message already printed */ (ctx ? ctx : &defaults)->cache_max = v; |