From: Art Cancro Date: Fri, 27 Jul 2018 15:53:47 +0000 (-0400) Subject: indent -kr -i8 -l132 on everything in webcit-ng X-Git-Tag: v939~368 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=03d5df9a4e11ce16fbfeac4ad03f2825591fa4ae indent -kr -i8 -l132 on everything in webcit-ng --- diff --git a/webcit-ng/admin_functions.c b/webcit-ng/admin_functions.c index dbd3eca95..8b71a68fd 100644 --- a/webcit-ng/admin_functions.c +++ b/webcit-ng/admin_functions.c @@ -32,16 +32,16 @@ void try_login(struct http_transaction *h, struct ctdlsession *c) snprintf(buf, sizeof buf, "%s:%s", username, password); CtdlEncodeBase64(auth, buf, strlen(buf), 0); - syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int)strlen(password)); + syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password)); - ctdl_printf(c, "LOUT"); // log out, in case we were logged in - ctdl_readline(c, buf, sizeof(buf)); // ignore the result - memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now. - memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now. + ctdl_printf(c, "LOUT"); // log out, in case we were logged in + ctdl_readline(c, buf, sizeof(buf)); // ignore the result + memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now. + memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now. - login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel + login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel - h->response_code = 200; // 'buf' will contain the relevant response + h->response_code = 200; // 'buf' will contain the relevant response h->response_string = strdup("OK"); add_response_header(h, strdup("Content-type"), strdup("text/plain")); h->response_body = strdup(buf); @@ -60,13 +60,13 @@ void logout(struct http_transaction *h, struct ctdlsession *c) char password[256]; int login_success = 0; - ctdl_printf(c, "LOUT"); // log out - ctdl_readline(c, buf, sizeof(buf)); // ignore the result + ctdl_printf(c, "LOUT"); // log out + ctdl_readline(c, buf, sizeof(buf)); // ignore the result strcpy(c->auth, "x"); - //memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now. - memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now. + //memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now. + memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now. - http_redirect(h, "/ctdl/s/index.html"); // go back where we started :) + http_redirect(h, "/ctdl/s/index.html"); // go back where we started :) } @@ -88,20 +88,20 @@ void whoami(struct http_transaction *h, struct ctdlsession *c) */ void ctdl_a(struct http_transaction *h, struct ctdlsession *c) { - if (!strcasecmp(h->uri, "/ctdl/a/login")) { // log in + if (!strcasecmp(h->uri, "/ctdl/a/login")) { // log in try_login(h, c); - return; + return; } - if (!strcasecmp(h->uri, "/ctdl/a/logout")) { // log out + if (!strcasecmp(h->uri, "/ctdl/a/logout")) { // log out logout(h, c); - return; + return; } - if (!strcasecmp(h->uri, "/ctdl/a/whoami")) { // return display name of user + if (!strcasecmp(h->uri, "/ctdl/a/whoami")) { // return display name of user whoami(h, c); - return; + return; } - do_404(h); // unknown + do_404(h); // unknown } diff --git a/webcit-ng/caldav_reports.c b/webcit-ng/caldav_reports.c index 74980518e..c43c0e078 100644 --- a/webcit-ng/caldav_reports.c +++ b/webcit-ng/caldav_reports.c @@ -19,8 +19,7 @@ /* * A CalDAV REPORT can only be one type. This is stored in the report_type member. */ -enum cr_type -{ +enum cr_type { cr_calendar_query, cr_calendar_multiget, cr_freebusy_query @@ -45,13 +44,13 @@ struct cr_parms { */ void caldav_xml_start(void *data, const char *el, const char **attr) { - struct cr_parms *crp = (struct cr_parms *)data; + struct cr_parms *crp = (struct cr_parms *) data; int i; // syslog(LOG_DEBUG, "CALDAV ELEMENT START: <%s> %d", el, crp->tag_nesting_level); - for (i=0; attr[i] != NULL; i+=2) { - syslog(LOG_DEBUG, " Attribute '%s' = '%s'", attr[i], attr[i+1]); + for (i = 0; attr[i] != NULL; i += 2) { + syslog(LOG_DEBUG, " Attribute '%s' = '%s'", attr[i], attr[i + 1]); } if (!strcasecmp(el, "urn:ietf:params:xml:ns:caldav:calendar-multiget")) { @@ -75,7 +74,7 @@ void caldav_xml_start(void *data, const char *el, const char **attr) */ void caldav_xml_end(void *data, const char *el) { - struct cr_parms *crp = (struct cr_parms *)data; + struct cr_parms *crp = (struct cr_parms *) data; --crp->tag_nesting_level; if (crp->Chardata != NULL) { @@ -83,18 +82,17 @@ void caldav_xml_end(void *data, const char *el) } // syslog(LOG_DEBUG, "CALDAV ELEMENT END : <%s> %d", el, crp->tag_nesting_level); - if ( (!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href")) ) { - if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs + if ((!strcasecmp(el, "DAV::href")) || (!strcasecmp(el, "DAV:href"))) { + if (crp->Hrefs == NULL) { // append crp->Chardata to crp->Hrefs crp->Hrefs = NewStrBuf(); - } - else { + } else { StrBufAppendBufPlain(crp->Hrefs, HKEY("|"), 0); } StrBufAppendBuf(crp->Hrefs, crp->Chardata, 0); } - if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope. - FreeStrBuf(&crp->Chardata); // Free the buffer. + if (crp->Chardata != NULL) { // Tag is closed; chardata is now out of scope. + FreeStrBuf(&crp->Chardata); // Free the buffer. crp->Chardata = NULL; } } @@ -103,9 +101,9 @@ void caldav_xml_end(void *data, const char *el) /* * XML parser callback */ -void caldav_xml_chardata(void *data, const XML_Char *s, int len) +void caldav_xml_chardata(void *data, const XML_Char * s, int len) { - struct cr_parms *crp = (struct cr_parms *)data; + struct cr_parms *crp = (struct cr_parms *) data; if (crp->Chardata == NULL) { crp->Chardata = NewStrBuf(); @@ -124,7 +122,7 @@ void caldav_xml_chardata(void *data, const XML_Char *s, int len) * NOTE: this function expects that "MSGP text/calendar" was issued at the beginning * of a REPORT operation to set our preferred MIME type to calendar data. */ -StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) +StrBuf *fetch_ical(struct ctdlsession * c, long msgnum) { char buf[1024]; StrBuf *Buf = NULL; @@ -136,10 +134,9 @@ StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) } while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) { - if (Buf != NULL) { // already in body + if (Buf != NULL) { // already in body StrBufAppendPrintf(Buf, "%s\n", buf); - } - else if (IsEmptyStr(buf)) { // beginning of body + } else if (IsEmptyStr(buf)) { // beginning of body Buf = NewStrBuf(); } } @@ -171,7 +168,7 @@ StrBuf *fetch_ical(struct ctdlsession *c, long msgnum) * Called by caldav_report() to output a single item. * Our policy is to throw away the list of properties the client asked for, and just send everything. */ -void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf *ReportOut, StrBuf *ThisHref) +void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf * ReportOut, StrBuf * ThisHref) { long msgnum; StrBuf *Caldata = NULL; @@ -180,20 +177,20 @@ void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf * euid = strrchr(ChrPtr(ThisHref), '/'); if (euid != NULL) { ++euid; - } - else { - euid = (char *)ChrPtr(ThisHref); + } else { + euid = (char *) ChrPtr(ThisHref); } char *unescaped_euid = strdup(euid); - if (!unescaped_euid) return; + if (!unescaped_euid) + return; unescape_input(unescaped_euid); StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); - StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufXMLEscAppend(ReportOut, ThisHref, NULL, 0, 0); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); msgnum = locate_message_by_uid(c, unescaped_euid); free(unescaped_euid); @@ -201,31 +198,29 @@ void caldav_response(struct http_transaction *h, struct ctdlsession *c, StrBuf * Caldata = fetch_ical(c, msgnum); } - if (Caldata != NULL) - { + if (Caldata != NULL) { // syslog(LOG_DEBUG, "caldav_response(%s) 200 OK", ChrPtr(ThisHref)); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK"); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, "%ld", msgnum); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); - StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, "HTTP/1.1 200 OK"); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, "%ld", msgnum); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufXMLEscAppend(ReportOut, Caldata, NULL, 0, 0); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); FreeStrBuf(&Caldata); Caldata = NULL; - } - else { + } else { // syslog(LOG_DEBUG, "caldav_response(%s) 404 not found", ChrPtr(ThisHref)); - StrBufAppendPrintf(ReportOut, ""); - StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found"); - StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, "HTTP/1.1 404 not found"); + StrBufAppendPrintf(ReportOut, ""); } - StrBufAppendPrintf(ReportOut, ""); + StrBufAppendPrintf(ReportOut, ""); StrBufAppendPrintf(ReportOut, ""); } @@ -252,11 +247,11 @@ void caldav_report(struct http_transaction *h, struct ctdlsession *c) XML_SetElementHandler(xp, caldav_xml_start, caldav_xml_end); XML_SetCharacterDataHandler(xp, caldav_xml_chardata); XML_SetUserData(xp, &crp); - XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack" + XML_SetDefaultHandler(xp, NULL); // Disable internal entity expansion to prevent "billion laughs attack" XML_Parse(xp, h->request_body, h->request_body_length, 1); XML_ParserFree(xp); - if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here + if (crp.Chardata != NULL) { // Discard any trailing chardata ... normally nothing here FreeStrBuf(&crp.Chardata); crp.Chardata = NULL; } @@ -274,13 +269,9 @@ void caldav_report(struct http_transaction *h, struct ctdlsession *c) syslog(LOG_DEBUG, "CalDAV REPORT type is: %d", crp.report_type); StrBuf *ReportOut = NewStrBuf(); StrBufAppendPrintf(ReportOut, "" - "" - ); + ""); - if (crp.Hrefs != NULL) { // Output all qualifying calendar items! + if (crp.Hrefs != NULL) { // Output all qualifying calendar items! StrBuf *ThisHref = NewStrBuf(); const char *pvset = NULL; while (StrBufExtract_NextToken(ThisHref, crp.Hrefs, &pvset, '|') >= 0) { @@ -291,7 +282,7 @@ void caldav_report(struct http_transaction *h, struct ctdlsession *c) crp.Hrefs = NULL; } - StrBufAppendPrintf(ReportOut, "\n"); // End the REPORT. + StrBufAppendPrintf(ReportOut, "\n"); // End the REPORT. add_response_header(h, strdup("Content-type"), strdup("text/xml")); h->response_code = 207; diff --git a/webcit-ng/ctdl_commands.c b/webcit-ng/ctdl_commands.c index 2017c908b..d065ead98 100644 --- a/webcit-ng/ctdl_commands.c +++ b/webcit-ng/ctdl_commands.c @@ -29,56 +29,57 @@ void serv_info(struct http_transaction *h, struct ctdlsession *c) JsonValue *j = NewJsonObject(HKEY("serv_info")); int i = 0; - while (ctdl_readline(c, buf, sizeof(buf)) , strcmp(buf, "000")) switch(i++) { + while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) + switch (i++) { case 0: - JsonObjectAppend(j, NewJsonNumber( HKEY("serv_pid"), atol(buf))); + JsonObjectAppend(j, NewJsonNumber(HKEY("serv_pid"), atol(buf))); break; case 1: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_nodename"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_nodename"), buf, -1)); break; case 2: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_humannode"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_humannode"), buf, -1)); break; case 3: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_fqdn"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_fqdn"), buf, -1)); break; case 4: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_software"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_software"), buf, -1)); break; case 5: - JsonObjectAppend(j, NewJsonNumber( HKEY("serv_rev_level"), atol(buf))); + JsonObjectAppend(j, NewJsonNumber(HKEY("serv_rev_level"), atol(buf))); break; case 6: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_bbs_city"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_bbs_city"), buf, -1)); break; case 7: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_sysadm"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_sysadm"), buf, -1)); break; case 14: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_supports_ldap"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_ldap"), atoi(buf))); break; case 15: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_newuser_disabled"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_newuser_disabled"), atoi(buf))); break; case 16: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_default_cal_zone"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_default_cal_zone"), buf, -1)); break; case 20: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_supports_sieve"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_sieve"), atoi(buf))); break; case 21: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_fulltext_enabled"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_fulltext_enabled"), atoi(buf))); break; case 22: - JsonObjectAppend(j, NewJsonPlainString( HKEY("serv_svn_revision"), buf, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("serv_svn_revision"), buf, -1)); break; case 23: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_supports_openid"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_openid"), atoi(buf))); break; case 24: - JsonObjectAppend(j, NewJsonBool( HKEY("serv_supports_guest"), atoi(buf))); + JsonObjectAppend(j, NewJsonBool(HKEY("serv_supports_guest"), atoi(buf))); break; - } + } StrBuf *sj = NewStrBuf(); SerializeJson(sj, j, 1); // '1' == free the source array @@ -98,8 +99,7 @@ void ctdl_c(struct http_transaction *h, struct ctdlsession *c) { if (!strcasecmp(h->uri, "/ctdl/c/info")) { serv_info(h, c); - } - else { + } else { do_404(h); } } diff --git a/webcit-ng/ctdlclient.c b/webcit-ng/ctdlclient.c index decb31adb..0a5e1fbcd 100644 --- a/webcit-ng/ctdlclient.c +++ b/webcit-ng/ctdlclient.c @@ -28,26 +28,27 @@ int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes) int len = 0; int c = 0; - if (buf == NULL) return(-1); + if (buf == NULL) + return (-1); while (len < maxbytes) { c = read(ctdl->sock, &buf[len], 1); if (c <= 0) { syslog(LOG_DEBUG, "Socket error or zero-length read"); - return(-1); + return (-1); } if (buf[len] == '\n') { - if ( (len >0) && (buf[len-1] == '\r') ) { + if ((len > 0) && (buf[len - 1] == '\r')) { --len; } buf[len] = 0; // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf); - return(len); + return (len); } ++len; } // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf); - return(len); + return (len); } @@ -56,7 +57,7 @@ int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes) * Implemented in terms of ctdl_readline() and is therefore transparent... * Returns a newly allocated StrBuf or NULL for error. */ -StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl) +StrBuf *ctdl_readtextmsg(struct ctdlsession * ctdl) { char buf[1024]; StrBuf *sj = NewStrBuf(); @@ -64,7 +65,7 @@ StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl) return NULL; } - while ( (ctdl_readline(ctdl, buf, sizeof(buf)) >= 0) && (strcmp(buf, "000")) ) { + while ((ctdl_readline(ctdl, buf, sizeof(buf)) >= 0) && (strcmp(buf, "000"))) { StrBufAppendPrintf(sj, "%s\n", buf); } @@ -76,7 +77,8 @@ StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl) * Write to the Citadel server. For now we're just wrapping write() in case we * need to add anything else later. */ -ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count) { +ssize_t ctdl_write(struct ctdlsession * ctdl, const void *buf, size_t count) +{ return write(ctdl->sock, buf, count); } @@ -84,7 +86,7 @@ ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count) { /* * printf() type function to send data to the Citadel Server. */ -void ctdl_printf(struct ctdlsession *ctdl, const char *format,...) +void ctdl_printf(struct ctdlsession *ctdl, const char *format, ...) { va_list arg_ptr; StrBuf *Buf = NewStrBuf(); @@ -94,7 +96,7 @@ void ctdl_printf(struct ctdlsession *ctdl, const char *format,...) va_end(arg_ptr); syslog(LOG_DEBUG, "\033[32m] %s\033[0m", ChrPtr(Buf)); - ctdl_write(ctdl, (char *)ChrPtr(Buf), StrLength(Buf)); + ctdl_write(ctdl, (char *) ChrPtr(Buf), StrLength(Buf)); ctdl_write(ctdl, "\n", 1); FreeStrBuf(&Buf); } @@ -115,13 +117,13 @@ int uds_connectsock(char *sockpath) s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { syslog(LOG_WARNING, "Can't create socket [%s]: %s", sockpath, strerror(errno)); - return(-1); + return (-1); } if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { syslog(LOG_WARNING, "Can't connect [%s]: %s", sockpath, strerror(errno)); close(s); - return(-1); + return (-1); } return s; } @@ -155,12 +157,12 @@ int tcp_connectsock(char *host, char *service) * Handle numeric IPv4 and IPv6 addresses */ rc = inet_pton(AF_INET, host, &serveraddr); - if (rc == 1) { /* dotted quad */ + if (rc == 1) { /* dotted quad */ hints.ai_family = AF_INET; hints.ai_flags |= AI_NUMERICHOST; } else { rc = inet_pton(AF_INET6, host, &serveraddr); - if (rc == 1) { /* IPv6 address */ + if (rc == 1) { /* IPv6 address */ hints.ai_family = AF_INET6; hints.ai_flags |= AI_NUMERICHOST; } @@ -172,7 +174,7 @@ int tcp_connectsock(char *host, char *service) if (rc != 0) { syslog(LOG_DEBUG, "%s: %s", host, gai_strerror(rc)); freeaddrinfo(res); - return(-1); + return (-1); } /* @@ -180,15 +182,18 @@ int tcp_connectsock(char *host, char *service) */ for (ai = res; ai != NULL; ai = ai->ai_next) { - if (ai->ai_family == AF_INET) syslog(LOG_DEBUG, "Trying IPv4"); - else if (ai->ai_family == AF_INET6) syslog(LOG_DEBUG, "Trying IPv6"); - else syslog(LOG_WARNING, "This is going to fail."); + if (ai->ai_family == AF_INET) + syslog(LOG_DEBUG, "Trying IPv4"); + else if (ai->ai_family == AF_INET6) + syslog(LOG_DEBUG, "Trying IPv6"); + else + syslog(LOG_WARNING, "This is going to fail."); s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (s < 0) { syslog(LOG_WARNING, "socket() failed: %s", strerror(errno)); freeaddrinfo(res); - return(-1); + return (-1); } rc = connect(s, ai->ai_addr, ai->ai_addrlen); if (rc >= 0) { @@ -197,32 +202,25 @@ int tcp_connectsock(char *host, char *service) fdflags = fcntl(rc, F_GETFL); if (fdflags < 0) { - syslog(LOG_ERR, - "unable to get socket %d flags! %s", - rc, - strerror(errno)); + syslog(LOG_ERR, "unable to get socket %d flags! %s", rc, strerror(errno)); close(rc); return -1; } fdflags = fdflags | O_NONBLOCK; if (fcntl(rc, F_SETFL, fdflags) < 0) { - syslog(LOG_ERR, - "unable to set socket %d nonblocking flags! %s", - rc, - strerror(errno)); + syslog(LOG_ERR, "unable to set socket %d nonblocking flags! %s", rc, strerror(errno)); close(s); return -1; } - return(s); - } - else { + return (s); + } else { syslog(LOG_WARNING, "connect() failed: %s", strerror(errno)); close(s); } } - freeaddrinfo(res); - return(-1); + freeaddrinfo(res); + return (-1); } @@ -232,14 +230,15 @@ int tcp_connectsock(char *host, char *service) */ void extract_auth(struct http_transaction *h, char *authbuf, int authbuflen) { - if (authbuf == NULL) return; + if (authbuf == NULL) + return; authbuf[0] = 0; char *authheader = header_val(h, "Authorization"); if (authheader) { if (!strncasecmp(authheader, "Basic ", 6)) { safestrncpy(authbuf, &authheader[6], authbuflen); - return; // HTTP-AUTH was found -- stop here + return; // HTTP-AUTH was found -- stop here } } @@ -255,10 +254,9 @@ void extract_auth(struct http_transaction *h, char *authbuf, int authbuflen) if (strlen(authbuf) < 3) { // impossibly small authbuf[0] = 0; } - return; // Cookie auth was found -- stop here + return; // Cookie auth was found -- stop here } } - // no authorization found in headers ... this is an anonymous session } @@ -281,21 +279,20 @@ int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf) if (resultbuf != NULL) { buf = resultbuf; - } - else { + } else { buf = localbuf; } buflen = CtdlDecodeBase64(buf, auth, strlen(auth)); extract_token(supplied_username, buf, 0, ':', sizeof supplied_username); extract_token(supplied_password, buf, 1, ':', sizeof supplied_password); - syslog(LOG_DEBUG, "Supplied credentials: username=%s, pwlen=%d", supplied_username, (int)strlen(supplied_password)); + syslog(LOG_DEBUG, "Supplied credentials: username=%s, pwlen=%d", supplied_username, (int) strlen(supplied_password)); ctdl_printf(c, "USER %s", supplied_username); ctdl_readline(c, buf, 1024); if (buf[0] != '3') { syslog(LOG_DEBUG, "No such user: %s", buf); - return(1); // no such user; resultbuf will explain why + return (1); // no such user; resultbuf will explain why } ctdl_printf(c, "PASS %s", supplied_password); @@ -305,18 +302,19 @@ int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf) strcpy(c->auth, auth); extract_token(c->whoami, &buf[4], 0, '|', sizeof c->whoami); syslog(LOG_DEBUG, "Login succeeded: %s", buf); - return(0); + return (0); } syslog(LOG_DEBUG, "Login failed: %s", buf); - return(1); // login failed; resultbuf will explain why + return (1); // login failed; resultbuf will explain why } /* * Hunt for, or create, a connection to our Citadel Server */ -struct ctdlsession *connect_to_citadel(struct http_transaction *h) { +struct ctdlsession *connect_to_citadel(struct http_transaction *h) +{ struct ctdlsession *cptr = NULL; struct ctdlsession *my_session = NULL; int is_new_session = 0; @@ -326,16 +324,17 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h) { // Does the request carry a username and password? extract_auth(h, auth, sizeof auth); - syslog(LOG_DEBUG, "Session auth: %s", auth); // remove this log when development is done + syslog(LOG_DEBUG, "Session auth: %s", auth); // remove this log when development is done // Lock the connection pool while we claim our connection pthread_mutex_lock(&cpool_mutex); - if (cpool != NULL) for (cptr = cpool; ((cptr != NULL) && (my_session == NULL)); cptr = cptr->next) { - if ( (cptr->is_bound == 0) && (!strcmp(cptr->auth, auth)) ) { - my_session = cptr; - my_session->is_bound = 1; + if (cpool != NULL) + for (cptr = cpool; ((cptr != NULL) && (my_session == NULL)); cptr = cptr->next) { + if ((cptr->is_bound == 0) && (!strcmp(cptr->auth, auth))) { + my_session = cptr; + my_session->is_bound = 1; + } } - } if (my_session == NULL) { syslog(LOG_DEBUG, "No qualifying sessions , starting a new one"); my_session = malloc(sizeof(struct ctdlsession)); @@ -349,13 +348,12 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h) { } pthread_mutex_unlock(&cpool_mutex); if (my_session == NULL) { - return(NULL); // oh well + return (NULL); // oh well } if (my_session->sock < 3) { is_new_session = 1; - } - else { // make sure our Citadel session is still good + } else { // make sure our Citadel session is still good int test_conn; test_conn = ctdl_write(my_session, HKEY("NOOP\n")); if (test_conn < 5) { @@ -363,8 +361,7 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h) { close(my_session->sock); my_session->sock = 0; is_new_session = 1; - } - else { + } else { test_conn = ctdl_readline(my_session, buf, sizeof(buf)); if (test_conn < 1) { syslog(LOG_DEBUG, "Citadel session is broken , must reconnect"); @@ -389,14 +386,15 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h) { my_session->last_access = time(NULL); ++my_session->num_requests_handled; - return(my_session); + return (my_session); } /* * Release our Citadel Server connection back into the pool. */ -void disconnect_from_citadel(struct ctdlsession *ctdl) { +void disconnect_from_citadel(struct ctdlsession *ctdl) +{ pthread_mutex_lock(&cpool_mutex); ctdl->is_bound = 0; pthread_mutex_unlock(&cpool_mutex); diff --git a/webcit-ng/ctdlfunctions.c b/webcit-ng/ctdlfunctions.c index cba9747e2..a66d1708d 100644 --- a/webcit-ng/ctdlfunctions.c +++ b/webcit-ng/ctdlfunctions.c @@ -24,8 +24,7 @@ void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs) int i = 0; char buf[1024]; - if ( (c == NULL) || (msgnums == NULL) || (num_msgs < 1) ) - { + if ((c == NULL) || (msgnums == NULL) || (num_msgs < 1)) { return; } @@ -33,14 +32,13 @@ void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs) strcpy(buf, "DELE "); do { sprintf(&buf[strlen(buf)], "%ld", msgnums[i]); - if ( (((i+1)%50)==0) || (i==num_msgs-1)) // delete up to 50 messages with one server command + if ((((i + 1) % 50) == 0) || (i == num_msgs - 1)) // delete up to 50 messages with one server command { syslog(LOG_DEBUG, "%s", buf); ctdl_printf(c, "%s", buf); ctdl_readline(c, buf, sizeof(buf)); syslog(LOG_DEBUG, "%s", buf); - } - else { + } else { strcat(buf, ","); } } while (++i < num_msgs); diff --git a/webcit-ng/forum_view.c b/webcit-ng/forum_view.c index fb88eddcd..6cd4f01f5 100644 --- a/webcit-ng/forum_view.c +++ b/webcit-ng/forum_view.c @@ -30,10 +30,10 @@ struct mthread { void setup_for_forum_view(struct ctdlsession *c) { char buf[1024]; - ctdl_printf(c, "MSGP text/html|text/plain"); // Declare the MIME types we know how to render - ctdl_readline(c, buf, sizeof(buf)); // Ignore the response - ctdl_printf(c, "MSGP dont_decode"); // Tell the server we will decode base64/etc client-side - ctdl_readline(c, buf, sizeof(buf)); // Ignore the response + ctdl_printf(c, "MSGP text/html|text/plain"); // Declare the MIME types we know how to render + ctdl_readline(c, buf, sizeof(buf)); // Ignore the response + ctdl_printf(c, "MSGP dont_decode"); // Tell the server we will decode base64/etc client-side + ctdl_readline(c, buf, sizeof(buf)); // Ignore the response } @@ -41,7 +41,7 @@ void setup_for_forum_view(struct ctdlsession *c) // Renderer for one message in the threaded view // (This will probably work for the flat view too.) // -void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) +void forum_render_one_message(struct ctdlsession *c, StrBuf * sj, long msgnum) { StrBuf *raw_msg = NULL; StrBuf *sanitized_msg = NULL; @@ -49,7 +49,7 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) char content_transfer_encoding[1024] = { 0 }; char content_type[1024] = { 0 }; char author[128] = { 0 }; - char datetime[128] = { 0 } ; + char datetime[128] = { 0 }; ctdl_printf(c, "MSG4 %ld", msgnum); ctdl_readline(c, buf, sizeof(buf)); @@ -58,7 +58,7 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) return; } - while ( (ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000")) ) { + while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000"))) { // citadel header parsing here if (!strncasecmp(buf, "from=", 5)) { safestrncpy(author, &buf[5], sizeof author); @@ -73,7 +73,7 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) } if (!strcmp(buf, "text")) { - while ( (ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000")) ) { + while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000"))) { // rfc822 header parsing here if (!strncasecmp(buf, "Content-transfer-encoding:", 26)) { strcpy(content_transfer_encoding, &buf[26]); @@ -85,23 +85,22 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) } } raw_msg = ctdl_readtextmsg(c); - } - else { + } else { raw_msg = NULL; } // begin output - StrBufAppendPrintf(sj, "
"); // begin message wrapper - StrBufAppendPrintf(sj, "
"); // begin avatar FIXME move the style to a stylesheet - StrBufAppendPrintf(sj, " "); // FIXME temporary avatar - StrBufAppendPrintf(sj, "
"); // end avatar - StrBufAppendPrintf(sj, "
"); // begin content - StrBufAppendPrintf(sj, "
"); // begin header + StrBufAppendPrintf(sj, "
"); // begin message wrapper + StrBufAppendPrintf(sj, "
"); // begin avatar FIXME move the style to a stylesheet + StrBufAppendPrintf(sj, " "); // FIXME temporary avatar + StrBufAppendPrintf(sj, "
"); // end avatar + StrBufAppendPrintf(sj, "
"); // begin content + StrBufAppendPrintf(sj, "
"); // begin header StrBufAppendPrintf(sj, "%s ", author); // FIXME link to user profile or whatever StrBufAppendPrintf(sj, "%s ", datetime); - StrBufAppendPrintf(sj, "
"); // end header - StrBufAppendPrintf(sj, "
"); // begin body + StrBufAppendPrintf(sj, "
"); // end header + StrBufAppendPrintf(sj, "
"); // begin body if (raw_msg) { @@ -113,20 +112,16 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) if (!strcasecmp(content_transfer_encoding, "quoted-printable")) { StrBufDecodeQP(raw_msg); } - // At this point, raw_msg contains the decoded message. // Now run through the renderers we have available. if (!strncasecmp(content_type, "text/html", 9)) { sanitized_msg = html2html("UTF-8", 0, c->room, msgnum, raw_msg); - } - else if (!strncasecmp(content_type, "text/plain", 10)) { + } else if (!strncasecmp(content_type, "text/plain", 10)) { sanitized_msg = text2html("UTF-8", 0, c->room, msgnum, raw_msg); - } - else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) { + } else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) { sanitized_msg = variformat2html(raw_msg); - } - else { + } else { sanitized_msg = NewStrBufPlain(HKEY("No renderer for this content type
")); } FreeStrBuf(&raw_msg); @@ -139,9 +134,9 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) } } - StrBufAppendPrintf(sj, "
"); // end body - StrBufAppendPrintf(sj, "
"); // end content - StrBufAppendPrintf(sj, "
"); // end wrapper + StrBufAppendPrintf(sj, "
"); // end body + StrBufAppendPrintf(sj, "
"); // end content + StrBufAppendPrintf(sj, "
"); // end wrapper } @@ -150,24 +145,24 @@ void forum_render_one_message(struct ctdlsession *c, StrBuf *sj, long msgnum) // Threaded view (recursive section) // -void thread_o_print(struct ctdlsession *c, StrBuf *sj, struct mthread *m, int num_msgs, int where_parent_is, int nesting_level) +void thread_o_print(struct ctdlsession *c, StrBuf * sj, struct mthread *m, int num_msgs, int where_parent_is, int nesting_level) { int i = 0; int j = 0; int num_printed = 0; - for (i=0; i"); } - StrBufAppendPrintf(sj, "
  • ", m[i].msgnum); + StrBufAppendPrintf(sj, "
  • ", m[i].msgnum); forum_render_one_message(c, sj, m[i].msgnum); StrBufAppendPrintf(sj, "
  • \r\n"); if (i != 0) { - thread_o_print(c, sj, m, num_msgs, i, nesting_level+1); + thread_o_print(c, sj, m, num_msgs, i, nesting_level + 1); } } } @@ -189,7 +184,7 @@ void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *whic char refs[1024]; int i, j, k; - ctdl_printf(c, "MSGS ALL|||9"); // 9 == headers + thread references + ctdl_printf(c, "MSGS ALL|||9"); // 9 == headers + thread references ctdl_readline(c, buf, sizeof(buf)); if (buf[0] != '1') { do_404(h); @@ -199,25 +194,24 @@ void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *whic StrBuf *sj = NewStrBuf(); StrBufAppendPrintf(sj, "\r\n"); - while (ctdl_readline(c, buf, sizeof buf), strcmp(buf,"000")) { + while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) { ++num_msgs; if (num_msgs > num_alloc) { if (num_alloc == 0) { num_alloc = 100; m = malloc(num_alloc * sizeof(struct mthread)); - } - else { + } else { num_alloc *= 2; m = realloc(m, (num_alloc * sizeof(struct mthread))); } } - memset(&m[num_msgs-1], 0, sizeof(struct mthread)); - m[num_msgs-1].msgnum = extract_long(buf, 0); - m[num_msgs-1].datetime = extract_long(buf, 1); - extract_token(m[num_msgs-1].from, buf, 2, '|', sizeof m[num_msgs-1].from); - m[num_msgs-1].threadhash = extract_int(buf, 6); + memset(&m[num_msgs - 1], 0, sizeof(struct mthread)); + m[num_msgs - 1].msgnum = extract_long(buf, 0); + m[num_msgs - 1].datetime = extract_long(buf, 1); + extract_token(m[num_msgs - 1].from, buf, 2, '|', sizeof m[num_msgs - 1].from); + m[num_msgs - 1].threadhash = extract_int(buf, 6); extract_token(refs, buf, 7, '|', sizeof refs); char *t; @@ -225,11 +219,10 @@ void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *whic i = 0; while ((t = strtok_r(r, ",", &r))) { if (i == 0) { - m[num_msgs-1].refhashes[0] = atoi(t); // always keep the first one - } - else { - memcpy(&m[num_msgs-1].refhashes[1], &m[num_msgs-1].refhashes[2], sizeof(int)*8 ); // shift the rest - m[num_msgs-1].refhashes[9] = atoi(t); + m[num_msgs - 1].refhashes[0] = atoi(t); // always keep the first one + } else { + memcpy(&m[num_msgs - 1].refhashes[1], &m[num_msgs - 1].refhashes[2], sizeof(int) * 8); // shift the rest + m[num_msgs - 1].refhashes[9] = atoi(t); } ++i; } @@ -237,9 +230,9 @@ void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *whic } // Sort by thread. I did read jwz's sorting algorithm and it looks pretty good, but jwz is a self-righteous asshole so we do it our way. - for (i=0; i=0)&&(m[i].parent==0); --j) { - for (k=0; (k= 0) && (m[i].parent == 0); --j) { + for (k = 0; (k < num_msgs) && (m[i].parent == 0); ++k) { if (m[i].refhashes[j] == m[k].threadhash) { m[i].parent = k; } @@ -249,7 +242,7 @@ void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *whic // Now render it setup_for_forum_view(c); - thread_o_print(c, sj, m, num_msgs, 0, 0); // Render threads recursively and recursively + thread_o_print(c, sj, m, num_msgs, 0, 0); // Render threads recursively and recursively // Garbage collection is for people who aren't smart enough to manage their own memory. if (num_msgs > 0) { @@ -278,7 +271,7 @@ void flat_view(struct http_transaction *h, struct ctdlsession *c, char *which) long *msglist = get_msglist(c, "ALL"); if (msglist) { int i; - for (i=0; (msglist[i] > 0); ++i) { + for (i = 0; (msglist[i] > 0); ++i) { forum_render_one_message(c, sj, msglist[i]); } free(msglist); @@ -295,13 +288,13 @@ void flat_view(struct http_transaction *h, struct ctdlsession *c, char *which) } -// render one message (entire transaction) FIXME EXTERMINATE +// render one message (entire transaction) FIXME EXTERMINATE // void html_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) { StrBuf *sj = NewStrBuf(); StrBufAppendPrintf(sj, "\r\n"); - setup_for_forum_view(c); // FIXME way too inefficient to do this for every message !!!!!!!!!!!!! + setup_for_forum_view(c); // FIXME way too inefficient to do this for every message !!!!!!!!!!!!! forum_render_one_message(c, sj, msgnum); StrBufAppendPrintf(sj, "\r\n"); add_response_header(h, strdup("Content-type"), strdup("text/html; charset=utf-8")); @@ -324,7 +317,7 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, char content_transfer_encoding[1024] = { 0 }; char content_type[1024] = { 0 }; char author[128] = { 0 }; - char datetime[128] = { 0 } ; + char datetime[128] = { 0 }; setup_for_forum_view(c); @@ -337,13 +330,13 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, JsonValue *j = NewJsonObject(HKEY("message")); - while ( (ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000")) ) { + while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "text")) && (strcmp(buf, "000"))) { // citadel header parsing here if (!strncasecmp(buf, "from=", 5)) { - JsonObjectAppend(j, NewJsonPlainString( HKEY("from"), &buf[5], -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), &buf[5], -1)); } if (!strncasecmp(buf, "rfca=", 5)) { - JsonObjectAppend(j, NewJsonPlainString( HKEY("from"), &buf[5], -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("from"), &buf[5], -1)); } if (!strncasecmp(buf, "time=", 5)) { time_t tt; @@ -351,12 +344,12 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, tt = atol(&buf[5]); localtime_r(&tt, &tm); strftime(datetime, sizeof datetime, "%c", &tm); - JsonObjectAppend(j, NewJsonPlainString( HKEY("time"), datetime, -1)); + JsonObjectAppend(j, NewJsonPlainString(HKEY("time"), datetime, -1)); } } if (!strcmp(buf, "text")) { - while ( (ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000")) ) { + while ((ctdl_readline(c, buf, sizeof(buf)) >= 0) && (strcmp(buf, "")) && (strcmp(buf, "000"))) { // rfc822 header parsing here if (!strncasecmp(buf, "Content-transfer-encoding:", 26)) { strcpy(content_transfer_encoding, &buf[26]); @@ -368,8 +361,7 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, } } raw_msg = ctdl_readtextmsg(c); - } - else { + } else { raw_msg = NULL; } @@ -383,20 +375,16 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, if (!strcasecmp(content_transfer_encoding, "quoted-printable")) { StrBufDecodeQP(raw_msg); } - // At this point, raw_msg contains the decoded message. // Now run through the renderers we have available. if (!strncasecmp(content_type, "text/html", 9)) { sanitized_msg = html2html("UTF-8", 0, c->room, msgnum, raw_msg); - } - else if (!strncasecmp(content_type, "text/plain", 10)) { + } else if (!strncasecmp(content_type, "text/plain", 10)) { sanitized_msg = text2html("UTF-8", 0, c->room, msgnum, raw_msg); - } - else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) { + } else if (!strncasecmp(content_type, "text/x-citadel-variformat", 25)) { sanitized_msg = variformat2html(raw_msg); - } - else { + } else { sanitized_msg = NewStrBufPlain(HKEY("No renderer for this content type
    ")); } FreeStrBuf(&raw_msg); @@ -409,7 +397,7 @@ void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, } StrBuf *sj = NewStrBuf(); - SerializeJson(sj, j, 1); // '1' == free the source object + SerializeJson(sj, j, 1); // '1' == free the source object add_response_header(h, strdup("Content-type"), strdup("application/json")); h->response_code = 200; diff --git a/webcit-ng/html2html.c b/webcit-ng/html2html.c index 8e118e694..850fde196 100644 --- a/webcit-ng/html2html.c +++ b/webcit-ng/html2html.c @@ -23,13 +23,15 @@ void stripquotes(char *s) { int len; - if (!s) return; + if (!s) + return; len = strlen(s); - if (len < 2) return; + if (len < 2) + return; - if ( ( (s[0] == '\"') && (s[len-1] == '\"') ) || ( (s[0] == '\'') && (s[len-1] == '\'') ) ) { - s[len-1] = 0; + if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) { + s[len - 1] = 0; strcpy(s, &s[1]); } } @@ -47,14 +49,19 @@ void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_ char *ptr; char buf[64]; - if (!charset) return; - if (!meta_http_equiv) return; - if (!meta_content) return; + if (!charset) + return; + if (!meta_http_equiv) + return; + if (!meta_content) + return; - if (strcasecmp(meta_http_equiv, "Content-type")) return; + if (strcasecmp(meta_http_equiv, "Content-type")) + return; ptr = strchr(meta_content, ';'); - if (!ptr) return; + if (!ptr) + return; safestrncpy(buf, ++ptr, sizeof buf); striplt(buf); @@ -73,7 +80,8 @@ void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_ } /* Remove wandering punctuation */ - if ((ptr=strchr(charset, '\"'))) *ptr = 0; + if ((ptr = strchr(charset, '\"'))) + *ptr = 0; striplt(charset); } } @@ -85,7 +93,8 @@ void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_ * Also fixup img src="cid:..." type inline images to fetch the image * */ -StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) { +StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf * Source) +{ char buf[SIZ]; char *msg; char *ptr; @@ -105,27 +114,27 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam char charset[128]; StrBuf *BodyArea = NULL; - iconv_t ic = (iconv_t)(-1) ; - char *ibuf; /* Buffer of characters to be converted */ - char *obuf; /* Buffer for converted characters */ - size_t ibuflen; /* Length of input buffer */ - size_t obuflen; /* Length of output buffer */ - char *osav; /* Saved pointer to output buffer */ + iconv_t ic = (iconv_t) (-1); + char *ibuf; /* Buffer of characters to be converted */ + char *obuf; /* Buffer for converted characters */ + size_t ibuflen; /* Length of input buffer */ + size_t obuflen; /* Length of output buffer */ + char *osav; /* Saved pointer to output buffer */ if (msg == NULL) { - return(NULL); + return (NULL); } StrBuf *Target = NewStrBuf(); if (Target == NULL) { - return(NULL); + return (NULL); } safestrncpy(charset, supplied_charset, sizeof charset); sprintf(new_window, "'); - if ((ptr == NULL) || (ptr >= msgend)) break; + if ((ptr == NULL) || (ptr >= msgend)) + break; if ((pBody != NULL) && (ptr - pBody > 4)) { char *src; char *cid_start, *cid_end; *ptr = '\0'; - pBody += 4; + pBody += 4; while ((isspace(*pBody)) && (pBody < ptr)) - pBody ++; - BodyArea = NewStrBufPlain(NULL, ptr - pBody); + pBody++; + BodyArea = NewStrBufPlain(NULL, ptr - pBody); if (pBody < ptr) { src = strstr(pBody, "cid:"); if (src) { cid_start = src + 4; cid_end = cid_start; - while ((*cid_end != '"') && - !isspace(*cid_end) && - (cid_end < ptr)) - cid_end ++; + while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr)) + cid_end++; /* copy tag and attributes up to src="cid: */ StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0); /* add in /webcit/mimepart//CID/ trailing / stops dumb URL filters getting excited */ - StrBufAppendPrintf(BodyArea, - "/webcit/mimepart/%ld/",msgnum); + StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum); StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0); if (ptr - cid_end > 0) - StrBufAppendBufPlain(BodyArea, - cid_end + 1, - ptr - cid_end, 0); - } - else + StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0); + } else StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0); } *ptr = '>'; } ++ptr; - if ((ptr == NULL) || (ptr >= msgend)) break; + if ((ptr == NULL) || (ptr >= msgend)) + break; msgstart = ptr; } @@ -248,7 +255,7 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam * Any of these tags cause everything including and following * the tag to be removed. */ - if ( (!strncasecmp(ptr, "/HTML", 5)) ||(!strncasecmp(ptr, "/BODY", 5)) ) { + if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) { --ptr; msgend = ptr; strcpy(ptr, ""); @@ -264,21 +271,21 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam converted_msg = NewStrBufPlain(NULL, content_length + 8192); /* Convert foreign character sets to UTF-8 if necessary. */ - if ( (strcasecmp(charset, "us-ascii")) - && (strcasecmp(charset, "UTF-8")) - && (strcasecmp(charset, "")) - ) { + if ((strcasecmp(charset, "us-ascii")) + && (strcasecmp(charset, "UTF-8")) + && (strcasecmp(charset, "")) + ) { syslog(LOG_DEBUG, "Converting %s to UTF-8", charset); ctdl_iconv_open("UTF-8", charset, &ic); - if (ic == (iconv_t)(-1) ) { + if (ic == (iconv_t) (-1)) { syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno)); } } - if (Source == NULL) { - if (ic != (iconv_t)(-1) ) { + if (Source == NULL) { + if (ic != (iconv_t) (-1)) { ibuf = msg; ibuflen = content_length; - obuflen = content_length + (content_length / 2) ; + obuflen = content_length + (content_length / 2); obuf = (char *) malloc(obuflen); osav = obuf; iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen); @@ -288,14 +295,13 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam msg = osav; iconv_close(ic); } - } - else { - if (ic != (iconv_t)(-1) ) { + } else { + if (ic != (iconv_t) (-1)) { StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);; StrBufConvert(Source, Buf, &ic); FreeStrBuf(&Buf); iconv_close(ic); - msg = (char*)ChrPtr(Source); /* TODO: get rid of this. */ + msg = (char *) ChrPtr(Source); /* TODO: get rid of this. */ } } @@ -312,8 +318,8 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam goto BAIL; } - if (BodyArea != NULL) { // Any attributes that were declared in the tag - StrBufAppendBufPlain(converted_msg, HKEY("
    tag + if (BodyArea != NULL) { // Any attributes that were declared in the tag + StrBufAppendBufPlain(converted_msg, HKEY("
    tag StrBufAppendBuf(converted_msg, BodyArea, 0); StrBufAppendBufPlain(converted_msg, HKEY(">"), 0); } @@ -349,24 +355,21 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam else if (!strncasecmp(ptr, "')))) { + if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) { /* open external links to new window */ StrBufAppendPrintf(converted_msg, new_window); ptr = &ptr[8]; - } - else if ( - (treat_as_wiki) - && (strncasecmp(ptr, "'); - char* src; + char *tag_end = strchr(ptr, '>'); + char *src; /* FIXME - handle this situation (maybe someone opened an ') - ||(ptr[i]=='[') - ||(ptr[i]==']') - ||(ptr[i]=='"') - ||(ptr[i]=='\'') - ) linklen = i; + for (i = 0; i <= strlenptr; ++i) { + if ((ptr[i] == 0) + || (isspace(ptr[i])) + || (ptr[i] == 10) + || (ptr[i] == 13) + || (ptr[i] == '(') + || (ptr[i] == ')') + || (ptr[i] == '<') + || (ptr[i] == '>') + || (ptr[i] == '[') + || (ptr[i] == ']') + || (ptr[i] == '"') + || (ptr[i] == '\'') + ) + linklen = i; /* entity tag? */ if (ptr[i] == '&') { - if ((ptr[i+2] ==';') || - (ptr[i+3] ==';') || - (ptr[i+5] ==';') || - (ptr[i+6] ==';') || - (ptr[i+7] ==';')) + if ((ptr[i + 2] == ';') || + (ptr[i + 3] == ';') || + (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';')) linklen = i; } - if (linklen > 0) break; + if (linklen > 0) + break; } if (linklen > 0) { char *ltreviewptr; char *nbspreviewptr; char linkedchar; int len; - + len = linklen; linkedchar = ptr[len]; ptr[len] = '\0'; @@ -484,8 +483,7 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam ptr += linklen; StrBufAppendPrintf(converted_msg, ""); } - } - else { + } else { StrBufAppendBufPlain(converted_msg, ptr, 1, 0); ptr++; } @@ -496,39 +494,41 @@ StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomnam * so we don't turn things that look like URL's into * links, when they're already links - or image sources. */ - if ((ptr > msg) && (*(ptr-1) == '<')) { + if ((ptr > msg) && (*(ptr - 1) == '<')) { ++brak; } - if ((ptr > msg) && (*(ptr-1) == '>')) { + if ((ptr > msg) && (*(ptr - 1) == '>')) { --brak; if ((scriptlevel == 0) && (script_start_pos >= 0)) { StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos); script_start_pos = (-1); } } - if (!strncasecmp(ptr, "", 3)) --alevel; + if (!strncasecmp(ptr, "", 3)) + --alevel; } } if (BodyArea != NULL) { - StrBufAppendBufPlain(converted_msg, HKEY("
    "), 0); // Close the div where we declared attributes copied - FreeStrBuf(&BodyArea); // from the original tag + StrBufAppendBufPlain(converted_msg, HKEY("
    "), 0); // Close the div where we declared attributes copied + FreeStrBuf(&BodyArea); // from the original tag } - /* uncomment these two lines to override conversion */ - /* memcpy(converted_msg, msg, content_length); */ - /* output_length = content_length; */ + /* uncomment these two lines to override conversion */ + /* memcpy(converted_msg, msg, content_length); */ + /* output_length = content_length; */ /* Output our big pile of markup */ StrBufAppendBuf(Target, converted_msg, 0); -BAIL: /* A little trailing vertical whitespace... */ + BAIL: /* A little trailing vertical whitespace... */ StrBufAppendPrintf(Target, "
    \n"); /* Now give back the memory */ FreeStrBuf(&converted_msg); - if ((msg != NULL) && (Source == NULL)) free(msg); - return(Target); + if ((msg != NULL) && (Source == NULL)) + free(msg); + return (Target); } @@ -536,11 +536,11 @@ BAIL: /* A little trailing vertical whitespace... */ * Look for URL's embedded in a buffer and make them linkable. We use a * target window in order to keep the Citadel session in its own window. */ -void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf) +void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf) { int len, UrlLen, Offset, TrailerLen; const char *start, *end, *pos; - + FlushStrBuf(Target); start = NULL; len = StrLength(Source); @@ -559,25 +559,25 @@ void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf) FlushStrBuf(WrkBuf); for (pos = ChrPtr(Source) + len; pos > start; --pos) { - if ( (!isprint(*pos)) - || (isspace(*pos)) - || (*pos == '{') - || (*pos == '}') - || (*pos == '|') - || (*pos == '\\') - || (*pos == '^') - || (*pos == '[') - || (*pos == ']') - || (*pos == '`') - || (*pos == '<') - || (*pos == '>') - || (*pos == '(') - || (*pos == ')') - ) { + if ((!isprint(*pos)) + || (isspace(*pos)) + || (*pos == '{') + || (*pos == '}') + || (*pos == '|') + || (*pos == '\\') + || (*pos == '^') + || (*pos == '[') + || (*pos == ']') + || (*pos == '`') + || (*pos == '<') + || (*pos == '>') + || (*pos == '(') + || (*pos == ')') + ) { end = pos; } } - + UrlLen = end - start; StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0); @@ -585,8 +585,7 @@ void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf) if (Offset != 0) StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0); StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c", - LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, - QU, RB, ChrPtr(WrkBuf), LB, RB); + LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB); TrailerLen = StrLength(Source) - (end - ChrPtr(Source)); if (TrailerLen > 0) @@ -618,28 +617,28 @@ void url(char *buf, size_t bufsize) if (start == NULL) return; - for (pos = buf+len; pos > start; --pos) { - if ( (!isprint(*pos)) - || (isspace(*pos)) - || (*pos == '{') - || (*pos == '}') - || (*pos == '|') - || (*pos == '\\') - || (*pos == '^') - || (*pos == '[') - || (*pos == ']') - || (*pos == '`') - || (*pos == '<') - || (*pos == '>') - || (*pos == '(') - || (*pos == ')') - ) { + for (pos = buf + len; pos > start; --pos) { + if ((!isprint(*pos)) + || (isspace(*pos)) + || (*pos == '{') + || (*pos == '}') + || (*pos == '|') + || (*pos == '\\') + || (*pos == '^') + || (*pos == '[') + || (*pos == ']') + || (*pos == '`') + || (*pos == '<') + || (*pos == '>') + || (*pos == '(') + || (*pos == ')') + ) { end = pos; } } - + UrlLen = end - start; - if (UrlLen > sizeof(urlbuf)){ + if (UrlLen > sizeof(urlbuf)) { syslog(LOG_WARNING, "URL: content longer than buffer!"); return; } @@ -649,9 +648,8 @@ void url(char *buf, size_t bufsize) Offset = start - buf; if ((Offset != 0) && (Offset < sizeof(outbuf))) memcpy(outbuf, buf, Offset); - outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset, - "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c", - LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB); + outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset, + "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c", LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB); if (outpos >= sizeof(outbuf) - Offset) { syslog(LOG_WARNING, "URL: content longer than buffer!"); return; @@ -664,6 +662,6 @@ void url(char *buf, size_t bufsize) syslog(LOG_WARNING, "URL: content longer than buffer!"); return; } - memcpy (buf, outbuf, Offset + outpos + TrailerLen); + memcpy(buf, outbuf, Offset + outpos + TrailerLen); *(buf + Offset + outpos + TrailerLen) = '\0'; } diff --git a/webcit-ng/http.c b/webcit-ng/http.c index a7df97156..1665a9ac0 100644 --- a/webcit-ng/http.c +++ b/webcit-ng/http.c @@ -21,8 +21,7 @@ int client_write(struct client_handle *ch, char *buf, int nbytes) { if (is_https) { return client_write_ssl(ch, buf, nbytes); - } - else { + } else { return write(ch->sock, buf, nbytes); } } @@ -36,18 +35,17 @@ int client_read(struct client_handle *ch, char *buf, int nbytes) { if (is_https) { return client_read_ssl(ch, buf, nbytes); - } - else { + } else { int bytes_received = 0; int bytes_this_block = 0; while (bytes_received < nbytes) { - bytes_this_block = read(ch->sock, &buf[bytes_received], nbytes-bytes_received); + bytes_this_block = read(ch->sock, &buf[bytes_received], nbytes - bytes_received); if (bytes_this_block < 1) { - return(-1); + return (-1); } bytes_received += bytes_this_block; } - return(nbytes); + return (nbytes); } } @@ -62,31 +60,32 @@ int client_readline(struct client_handle *ch, char *buf, int maxbytes) int len = 0; int c = 0; - if (buf == NULL) return(-1); + if (buf == NULL) + return (-1); while (len < maxbytes) { c = client_read(ch, &buf[len], 1); if (c <= 0) { syslog(LOG_DEBUG, "Socket error or zero-length read"); - return(-1); + return (-1); } if (buf[len] == '\n') { - if ( (len >0) && (buf[len-1] == '\r') ) { + if ((len > 0) && (buf[len - 1] == '\r')) { --len; } buf[len] = 0; - return(len); + return (len); } ++len; } - return(len); + return (len); } /* * printf() type function to send data to the web client. */ -void client_printf(struct client_handle *ch, const char *format,...) +void client_printf(struct client_handle *ch, const char *format, ...) { va_list arg_ptr; StrBuf *Buf = NewStrBuf(); @@ -95,7 +94,7 @@ void client_printf(struct client_handle *ch, const char *format,...) StrBufVAppendPrintf(Buf, format, arg_ptr); va_end(arg_ptr); - client_write(ch, (char *)ChrPtr(Buf), StrLength(Buf)); + client_write(ch, (char *) ChrPtr(Buf), StrLength(Buf)); FreeStrBuf(&Buf); } @@ -126,20 +125,19 @@ void perform_one_http_transaction(struct client_handle *ch) struct http_transaction h; char *c, *d; struct sockaddr_in sin; - struct http_header *clh; // general purpose iterator variable + struct http_header *clh; // general purpose iterator variable memset(&h, 0, sizeof h); - while (len = client_readline(ch, buf, sizeof buf), (len > 0) ) { + while (len = client_readline(ch, buf, sizeof buf), (len > 0)) { ++lines_read; - if (lines_read == 1) { // First line is method and URI. - c = strchr(buf, ' '); // IGnore the HTTP-version. + if (lines_read == 1) { // First line is method and URI. + c = strchr(buf, ' '); // IGnore the HTTP-version. if (c == NULL) { h.method = strdup("NULL"); h.uri = strdup("/"); - } - else { + } else { *c = 0; h.method = strdup(buf); ++c; @@ -151,9 +149,8 @@ void perform_one_http_transaction(struct client_handle *ch) ++c; h.uri = strdup(d); } - } - else { // Subsequent lines are headers. - c = strchr(buf, ':'); // Header line folding is obsolete so we don't support it. + } else { // Subsequent lines are headers. + c = strchr(buf, ':'); // Header line folding is obsolete so we don't support it. if (c != NULL) { struct http_header *new_request_header = malloc(sizeof(struct http_header)); *c = 0; @@ -179,21 +176,18 @@ void perform_one_http_transaction(struct client_handle *ch) char *hostheader = header_val(&h, "Host"); if (hostheader) { strcat(h.site_prefix, hostheader); - } - else { + } else { strcat(h.site_prefix, "127.0.0.1"); } socklen_t llen = sizeof(sin); - if (getsockname(ch->sock, (struct sockaddr *)&sin, &llen) >= 0) { + if (getsockname(ch->sock, (struct sockaddr *) &sin, &llen) >= 0) { sprintf(&h.site_prefix[strlen(h.site_prefix)], ":%d", ntohs(sin.sin_port)); } - // Now try to read in the request body (if one is present) if (len < 0) { syslog(LOG_DEBUG, "Client disconnected"); - } - else { + } else { syslog(LOG_DEBUG, "< %s %s", h.method, h.uri); // If there is a request body, read it now. @@ -211,7 +205,6 @@ void perform_one_http_transaction(struct client_handle *ch) //write(2, HKEY("\033[0m\n")); } - // Now pass control up to the next layer to perform the request. perform_request(&h); @@ -230,7 +223,7 @@ void perform_one_http_transaction(struct client_handle *ch) free(datestring); } - client_printf(ch, "Content-encoding: identity\r\n"); // change if we gzip/deflate + client_printf(ch, "Content-encoding: identity\r\n"); // change if we gzip/deflate for (clh = h.response_headers; clh != NULL; clh = clh->next) { #ifdef DEBUG_HTTP syslog(LOG_DEBUG, "} %s: %s", clh->key, clh->val); @@ -245,24 +238,33 @@ void perform_one_http_transaction(struct client_handle *ch) // free the transaction memory while (h.request_headers) { - if (h.request_headers->key) free(h.request_headers->key); - if (h.request_headers->val) free(h.request_headers->val); + if (h.request_headers->key) + free(h.request_headers->key); + if (h.request_headers->val) + free(h.request_headers->val); clh = h.request_headers->next; free(h.request_headers); h.request_headers = clh; } while (h.response_headers) { - if (h.response_headers->key) free(h.response_headers->key); - if (h.response_headers->val) free(h.response_headers->val); + if (h.response_headers->key) + free(h.response_headers->key); + if (h.response_headers->val) + free(h.response_headers->val); clh = h.response_headers->next; free(h.response_headers); h.response_headers = clh; } - if (h.method) free(h.method); - if (h.uri) free(h.uri); - if (h.request_body) free(h.request_body); - if (h.response_string) free(h.response_string); - if (h.site_prefix) free(h.site_prefix); + if (h.method) + free(h.method); + if (h.uri) + free(h.uri); + if (h.request_body) + free(h.request_body); + if (h.response_string) + free(h.response_string); + if (h.site_prefix) + free(h.site_prefix); } @@ -274,11 +276,11 @@ void perform_one_http_transaction(struct client_handle *ch) */ char *header_val(struct http_transaction *h, char *requested_header) { - struct http_header *clh; // general purpose iterator variable + struct http_header *clh; // general purpose iterator variable for (clh = h->request_headers; clh != NULL; clh = clh->next) { if (!strcasecmp(clh->key, requested_header)) { - return(clh->val); + return (clh->val); } } - return(NULL); + return (NULL); } diff --git a/webcit-ng/main.c b/webcit-ng/main.c index 50c26160c..f94d5c630 100644 --- a/webcit-ng/main.c +++ b/webcit-ng/main.c @@ -30,7 +30,8 @@ int main(int argc, char **argv) char *pid_file = NULL; /* Parse command line */ - while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF) switch(a) { + while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF) + switch (a) { case 'u': setuid(atoi(optarg)); break; @@ -88,11 +89,9 @@ int main(int argc, char **argv) "[-d] [-Z] [-G i18ndumpfile] " "[-u uid] [-h homedirectory] " "[-D daemonizepid] [-v] " - "[-g defaultlandingpage] [-B basesize] " - "[-s] [-S cipher_suites]" - "[remotehost [remoteport]]\n"); + "[-g defaultlandingpage] [-B basesize] " "[-s] [-S cipher_suites]" "[remotehost [remoteport]]\n"); return 1; - } + } if (optind < argc) { ctdlhost = argv[optind]; @@ -101,7 +100,7 @@ int main(int argc, char **argv) } /* Start the logger */ - openlog("webcit", ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ), LOG_DAEMON); + openlog("webcit", (running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR)), LOG_DAEMON); /* Tell 'em who's in da house */ syslog(LOG_NOTICE, "MAKE WEBCIT GREAT AGAIN!"); @@ -118,9 +117,9 @@ int main(int argc, char **argv) /* Ensure that we are linked to the correct version of libcitadel */ if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) { - syslog(LOG_INFO, " You are running libcitadel version %d", libcitadel_version_number() ); - syslog(LOG_INFO, "WebCit was compiled against version %d", LIBCITADEL_VERSION_NUMBER ); - return(1); + syslog(LOG_INFO, " You are running libcitadel version %d", libcitadel_version_number()); + syslog(LOG_INFO, "WebCit was compiled against version %d", LIBCITADEL_VERSION_NUMBER); + return (1); } /* Go into the background if we were asked to run as a daemon */ diff --git a/webcit-ng/messages.c b/webcit-ng/messages.c index fa0ada03d..fc8da2bd5 100644 --- a/webcit-ng/messages.c +++ b/webcit-ng/messages.c @@ -21,29 +21,30 @@ * if not found. * */ -long locate_message_by_uid(struct ctdlsession *c, char *uid) { +long locate_message_by_uid(struct ctdlsession *c, char *uid) +{ char buf[1024]; ctdl_printf(c, "EUID %s", uid); ctdl_readline(c, buf, sizeof buf); if (buf[0] == '2') { - return(atol(&buf[4])); - + return (atol(&buf[4])); + } /* Ugly hack to handle Mozilla Thunderbird, try stripping ".ics" if present */ - if (!strcasecmp(&uid[strlen(uid)-4], ".ics")) { + if (!strcasecmp(&uid[strlen(uid) - 4], ".ics")) { safestrncpy(buf, uid, sizeof buf); - buf[strlen(buf)-4] = 0; + buf[strlen(buf) - 4] = 0; ctdl_printf(c, "EUID %s", buf); ctdl_readline(c, buf, sizeof buf); if (buf[0] == '2') { - return(atol(&buf[4])); - + return (atol(&buf[4])); + } } - return(-1); + return (-1); } @@ -78,39 +79,33 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg char *etag = malloc(20); if (etag != NULL) { sprintf(etag, "%ld", msgnum); - add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory + add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory } - while (ctdl_readline(c, buf, sizeof buf), strcmp(buf,"000")) - { + while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) { if (IsEmptyStr(buf) && (in_body == 0)) { in_body = 1; Body = NewStrBuf(); - } - else if (in_body == 0) { + } else if (in_body == 0) { char *k = buf; char *v = strchr(buf, ':'); if (v) { *v = 0; ++v; - striplt(v); // we now have a key (k) and a value (v) - if ( - (!strcasecmp(k, "content-type")) // fields which can be passed from RFC822 to HTTP as-is - || (!strcasecmp(k, "date")) - ) { + striplt(v); // we now have a key (k) and a value (v) + if ((!strcasecmp(k, "content-type")) // fields which can be passed from RFC822 to HTTP as-is + || (!strcasecmp(k, "date")) + ) { add_response_header(h, strdup(k), strdup(v)); - } - else if (!strcasecmp(k, "content-transfer-encoding")) { + } else if (!strcasecmp(k, "content-transfer-encoding")) { if (!strcasecmp(v, "base64")) { encoding = 'b'; - } - else if (!strcasecmp(v, "quoted-printable")) { + } else if (!strcasecmp(v, "quoted-printable")) { encoding = 'q'; } } } - } - else if ( (in_body == 1) && (Body != NULL) ) { + } else if ((in_body == 1) && (Body != NULL)) { StrBufAppendPrintf(Body, "%s\n", buf); } } @@ -122,18 +117,17 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg if (encoding == 'q') { h->response_body = malloc(StrLength(Body)); if (h->response_body != NULL) { - h->response_body_length = CtdlDecodeQuotedPrintable(h->response_body, (char *)ChrPtr(Body), StrLength(Body)); + h->response_body_length = + CtdlDecodeQuotedPrintable(h->response_body, (char *) ChrPtr(Body), StrLength(Body)); } FreeStrBuf(&Body); - } - else if (encoding == 'b') { + } else if (encoding == 'b') { h->response_body = malloc(StrLength(Body)); if (h->response_body != NULL) { h->response_body_length = CtdlDecodeBase64(h->response_body, ChrPtr(Body), StrLength(Body)); } FreeStrBuf(&Body); - } - else { + } else { h->response_body_length = StrLength(Body); h->response_body = SmashStrBuf(&Body); } @@ -153,12 +147,12 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu char new_euid[1024]; char response_string[1024]; - if ( (h->request_body == NULL) || (h->request_body_length < 1) ) { - do_404(h); // Refuse to post a null message + if ((h->request_body == NULL) || (h->request_body_length < 1)) { + do_404(h); // Refuse to post a null message return; } - ctdl_printf(c, "ENT0 1|||4|||1|"); // This protocol syntax will give us metadata back after upload + ctdl_printf(c, "ENT0 1|||4|||1|"); // This protocol syntax will give us metadata back after upload ctdl_readline(c, buf, sizeof buf); if (buf[0] != '8') { h->response_code = 502; @@ -181,13 +175,14 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu /* * Now handle the response from the Citadel server. */ - + n = 0; new_msgnum = 0; strcpy(new_euid, ""); strcpy(response_string, ""); - while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) switch(n++) { + while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) + switch (n++) { case 0: new_msgnum = atol(buf); break; @@ -200,13 +195,13 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu break; default: break; - } + } /* Tell the client what happened. */ /* Citadel failed in some way? */ char *new_location = malloc(1024); - if ( (new_msgnum < 0L) || (new_location == NULL) ) { + if ((new_msgnum < 0L) || (new_location == NULL)) { h->response_code = 502; h->response_string = strdup("bad gateway"); add_response_header(h, strdup("Content-type"), strdup("text/plain")); @@ -218,7 +213,7 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu char *etag = malloc(20); if (etag != NULL) { sprintf(etag, "%ld", new_msgnum); - add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory + add_response_header(h, strdup("ETag"), etag); // http_transaction now owns this memory } char esc_room[1024]; @@ -226,16 +221,15 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu urlesc(esc_room, sizeof esc_room, c->room); urlesc(esc_euid, sizeof esc_euid, new_euid); snprintf(new_location, 1024, "/ctdl/r/%s/%s", esc_room, esc_euid); - add_response_header(h, strdup("Location"), new_location); // http_transaction now owns this memory + add_response_header(h, strdup("Location"), new_location); // http_transaction now owns this memory if (old_msgnum <= 0) { - h->response_code = 201; // We created this item for the first time. + h->response_code = 201; // We created this item for the first time. h->response_string = strdup("created"); - } - else { - h->response_code = 204; // We modified an existing item. + } else { + h->response_code = 204; // We modified an existing item. h->response_string = strdup("no content"); - + /* The item we replaced has probably already been deleted by * the Citadel server, but we'll do this anyway, just in case. */ @@ -256,9 +250,8 @@ void download_mime_component(struct http_transaction *h, struct ctdlsession *c, ctdl_printf(c, "DLAT %ld|%s", msgnum, partnum); ctdl_readline(c, buf, sizeof buf); if (buf[0] != '6') { - do_404(h); // too bad, so sad, go away + do_404(h); // too bad, so sad, go away } - // Server response is going to be: 6XX length|-1|filename|content-type|charset h->response_body_length = extract_int(&buf[4], 0); extract_token(content_type, buf, 3, '|', sizeof content_type); @@ -269,9 +262,9 @@ void download_mime_component(struct http_transaction *h, struct ctdlsession *c, do { thisblock = read(c->sock, &h->response_body[bytes], (h->response_body_length - bytes)); bytes += thisblock; - syslog(LOG_DEBUG, "Bytes read: %d of %d", (int)bytes, (int)h->response_body_length); + syslog(LOG_DEBUG, "Bytes read: %d of %d", (int) bytes, (int) h->response_body_length); } while ((bytes < h->response_body_length) && (thisblock >= 0)); - h->response_body[h->response_body_length] = 0; // null terminate it just for good measure + h->response_body[h->response_body_length] = 0; // null terminate it just for good measure syslog(LOG_DEBUG, "content type: %s", content_type); add_response_header(h, strdup("Content-type"), strdup(content_type)); diff --git a/webcit-ng/request.c b/webcit-ng/request.c index baffe6dbc..0a20ed271 100644 --- a/webcit-ng/request.c +++ b/webcit-ng/request.c @@ -50,7 +50,9 @@ void do_502(struct http_transaction *h) h->response_code = 502; h->response_string = strdup("bad gateway"); add_response_header(h, strdup("Content-type"), strdup("text/plain")); - h->response_body = strdup(_("This program was unable to connect or stay connected to the Citadel server. Please report this problem to your system administrator.")); + h->response_body = + strdup(_ + ("This program was unable to connect or stay connected to the Citadel server. Please report this problem to your system administrator.")); h->response_body_length = strlen(h->response_body); } @@ -92,101 +94,86 @@ void perform_request(struct http_transaction *h) // This is implemented as a series of strncasecmp() calls rather than a // lookup table in order to make the code more readable. - if (IsEmptyStr(h->uri)) - { // Sanity check + if (IsEmptyStr(h->uri)) { // Sanity check do_404(h); return; } - // Right about here is where we should try to handle anything that doesn't start // with the /ctdl/ prefix. // Root (/) ... - if ( (!strcmp(h->uri, "/")) && (!strcasecmp(h->method, "GET")) ) - { + if ((!strcmp(h->uri, "/")) && (!strcasecmp(h->method, "GET"))) { http_redirect(h, "/ctdl/s/index.html"); return; } - // Legacy URI patterns (/readnew?gotoroom=xxx&start_reading_at=yyy) ... // Direct room name (/my%20blog) ... // Everything below this line is strictly REST URI patterns. - if (strncasecmp(h->uri, HKEY("/ctdl/"))) - { // Reject non-REST + if (strncasecmp(h->uri, HKEY("/ctdl/"))) { // Reject non-REST do_404(h); return; } - if (!strncasecmp(h->uri, HKEY("/ctdl/s/"))) - { // Static content + if (!strncasecmp(h->uri, HKEY("/ctdl/s/"))) { // Static content output_static(h); return; } - if (h->uri[7] != '/') - { + if (h->uri[7] != '/') { do_404(h); return; } - // Anything below this line: - // 1. Is in the format of /ctdl/?/* - // 2. Requires a connection to a Citadel server. + // 1. Is in the format of /ctdl/?/* + // 2. Requires a connection to a Citadel server. c = connect_to_citadel(h); - if (c == NULL) - { + if (c == NULL) { do_502(h); return; } - // WebDAV methods like OPTIONS and PROPFIND *require* a logged-in session, // even if the Citadel server allows anonymous access. - if (IsEmptyStr(c->auth)) - { - if ( (!strcasecmp(h->method, "OPTIONS")) - || (!strcasecmp(h->method, "PROPFIND")) - || (!strcasecmp(h->method, "REPORT")) - || (!strcasecmp(h->method, "DELETE")) - ) { + if (IsEmptyStr(c->auth)) { + if ((!strcasecmp(h->method, "OPTIONS")) + || (!strcasecmp(h->method, "PROPFIND")) + || (!strcasecmp(h->method, "REPORT")) + || (!strcasecmp(h->method, "DELETE")) + ) { request_http_authenticate(h); disconnect_from_citadel(c); return; } } - // Break down the URI by path and send the request to the appropriate part of the program. // - switch(h->uri[6]) - { - case 'a': // /ctdl/a/ == RESTful path to admin functions - ctdl_a(h, c); - break; - case 'c': // /ctdl/c/ == misc Citadel server commands - ctdl_c(h, c); - break; - case 'r': // /ctdl/r/ == RESTful path to rooms - ctdl_r(h, c); - break; - case 'u': // /ctdl/u/ == RESTful path to users - do_404(h); - break; - default: - do_404(h); + switch (h->uri[6]) { + case 'a': // /ctdl/a/ == RESTful path to admin functions + ctdl_a(h, c); + break; + case 'c': // /ctdl/c/ == misc Citadel server commands + ctdl_c(h, c); + break; + case 'r': // /ctdl/r/ == RESTful path to rooms + ctdl_r(h, c); + break; + case 'u': // /ctdl/u/ == RESTful path to users + do_404(h); + break; + default: + do_404(h); } // Are we in an authenticated session? If so, set a cookie so we stay logged in. - if (!IsEmptyStr(c->auth)) - { + if (!IsEmptyStr(c->auth)) { char koekje[AUTH_MAX]; char *exp = http_datestring(time(NULL) + (86400 * 30)); snprintf(koekje, AUTH_MAX, "wcauth=%s; path=/ctdl/; Expires=%s", c->auth, exp); free(exp); add_response_header(h, strdup("Set-Cookie"), strdup(koekje)); } - // During development we are foiling the browser cache completely. In production we'll be more selective. add_response_header(h, strdup("Cache-Control"), strdup("no-store, must-revalidate")); add_response_header(h, strdup("Pragma"), strdup("no-cache")); diff --git a/webcit-ng/room_functions.c b/webcit-ng/room_functions.c index a2dfc7b90..403e83f70 100644 --- a/webcit-ng/room_functions.c +++ b/webcit-ng/room_functions.c @@ -28,24 +28,20 @@ long *get_msglist(struct ctdlsession *c, char *which_msgs) ctdl_printf(c, "MSGS %s", which_msgs); ctdl_readline(c, buf, sizeof(buf)); - if (buf[0] == '1') do - { - if (num_msgs >= num_alloc) - { - if (num_alloc == 0) - { - num_alloc = 1024; - msglist = malloc(num_alloc * sizeof(long)); - } - else - { - num_alloc *= 2; - msglist = realloc(msglist, num_alloc * sizeof(long)); + if (buf[0] == '1') + do { + if (num_msgs >= num_alloc) { + if (num_alloc == 0) { + num_alloc = 1024; + msglist = malloc(num_alloc * sizeof(long)); + } else { + num_alloc *= 2; + msglist = realloc(msglist, num_alloc * sizeof(long)); + } } - } - ctdl_readline(c, buf, sizeof(buf)); - msglist[num_msgs++] = atol(buf); - } while (strcmp(buf, "000")); // this makes the last element a "0" terminator + ctdl_readline(c, buf, sizeof(buf)); + msglist[num_msgs++] = atol(buf); + } while (strcmp(buf, "000")); // this makes the last element a "0" terminator return msglist; } @@ -58,38 +54,37 @@ long *get_msglist(struct ctdlsession *c, char *which_msgs) int match_etags(char *taglist, long msgnum) { int num_tags = num_tokens(taglist, ','); - int i=0; + int i = 0; char tag[1024]; - if (msgnum <= 0) // no msgnum? no match. + if (msgnum <= 0) // no msgnum? no match. { - return(0); + return (0); } - for (i=0; i 0) && (tagmsgnum == msgnum) ) // match + if ((tagmsgnum > 0) && (tagmsgnum == msgnum)) // match { - return(1); + return (1); } } - return(0); // no match + return (0); // no match } @@ -102,17 +97,15 @@ void json_msglist(struct http_transaction *h, struct ctdlsession *c, char *which long *msglist = get_msglist(c, which); JsonValue *j = NewJsonArray(HKEY("msgs")); - if (msglist != NULL) - { - for (i=0; msglist[i]>0 ; ++i) - { - JsonArrayAppend(j, NewJsonNumber( HKEY("m"), msglist[i])); + if (msglist != NULL) { + for (i = 0; msglist[i] > 0; ++i) { + JsonArrayAppend(j, NewJsonNumber(HKEY("m"), msglist[i])); } free(msglist); } StrBuf *sj = NewStrBuf(); - SerializeJson(sj, j, 1); // '1' == free the source array + SerializeJson(sj, j, 1); // '1' == free the source array add_response_header(h, strdup("Content-type"), strdup("application/json")); h->response_code = 200; @@ -134,44 +127,40 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c) extract_token(buf, h->uri, 4, '/', sizeof buf); - if (!strncasecmp(buf, "msgs.", 5)) // Client is requesting a list of message numbers + if (!strncasecmp(buf, "msgs.", 5)) // Client is requesting a list of message numbers { json_msglist(h, c, &buf[5]); return; } - #if 0 - if (!strncasecmp(buf, "threads", 5)) // Client is requesting a threaded view (still kind of fuzzy here) + if (!strncasecmp(buf, "threads", 5)) // Client is requesting a threaded view (still kind of fuzzy here) { threaded_view(h, c, &buf[5]); return; } - if (!strncasecmp(buf, "flat", 5)) // Client is requesting a flat view (still kind of fuzzy here) + if (!strncasecmp(buf, "flat", 5)) // Client is requesting a flat view (still kind of fuzzy here) { flat_view(h, c, &buf[5]); return; } #endif - if ( (c->room_default_view == VIEW_CALENDAR) // room types where objects are referenced by EUID - || (c->room_default_view == VIEW_TASKS) - || (c->room_default_view == VIEW_ADDRESSBOOK) - ) { + if ((c->room_default_view == VIEW_CALENDAR) // room types where objects are referenced by EUID + || (c->room_default_view == VIEW_TASKS) + || (c->room_default_view == VIEW_ADDRESSBOOK) + ) { safestrncpy(unescaped_euid, buf, sizeof unescaped_euid); unescape_input(unescaped_euid); msgnum = locate_message_by_uid(c, unescaped_euid); - } - else - { + } else { msgnum = atol(buf); } /* * All methods except PUT require the message to already exist */ - if ( (msgnum <= 0) && (strcasecmp(h->method, "PUT")) ) - { + if ((msgnum <= 0) && (strcasecmp(h->method, "PUT"))) { do_404(h); } @@ -185,16 +174,12 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c) * (1) a MIME part specifier, in which case the client wants to download that component within the message * (2) a content-type, in which ase the client wants us to try to render it a certain way */ - if (num_tokens(h->uri, '/') == 6) - { + if (num_tokens(h->uri, '/') == 6) { extract_token(buf, h->uri, 5, '/', sizeof buf); if (!IsEmptyStr(buf)) { - if (!strcasecmp(buf, "json")) - { + if (!strcasecmp(buf, "json")) { json_render_one_message(h, c, msgnum); - } - else - { + } else { download_mime_component(h, c, msgnum, buf); } return; @@ -205,15 +190,13 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c) * Ok, we want a full message, but first let's check for the if[-none]-match headers. */ char *if_match = header_val(h, "If-Match"); - if ( (if_match != NULL) && (!match_etags(if_match, msgnum)) ) - { + if ((if_match != NULL) && (!match_etags(if_match, msgnum))) { do_412(h); return; } char *if_none_match = header_val(h, "If-None-Match"); - if ( (if_none_match != NULL) && (match_etags(if_none_match, msgnum)) ) - { + if ((if_none_match != NULL) && (match_etags(if_none_match, msgnum))) { do_412(h); return; } @@ -222,21 +205,14 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c) * DOOOOOO ITTTTT!!! */ - if (!strcasecmp(h->method, "DELETE")) - { + if (!strcasecmp(h->method, "DELETE")) { dav_delete_message(h, c, msgnum); - } - else if (!strcasecmp(h->method, "GET")) - { + } else if (!strcasecmp(h->method, "GET")) { dav_get_message(h, c, msgnum); - } - else if (!strcasecmp(h->method, "PUT")) - { + } else if (!strcasecmp(h->method, "PUT")) { dav_put_message(h, c, unescaped_euid, msgnum); - } - else - { - do_404(h); // Got this far but the method made no sense? Bummer. + } else { + do_404(h); // Got this far but the method made no sense? Bummer. } } @@ -247,13 +223,12 @@ void object_in_room(struct http_transaction *h, struct ctdlsession *c) */ void report_the_room_itself(struct http_transaction *h, struct ctdlsession *c) { - if (c->room_default_view == VIEW_CALENDAR) - { - caldav_report(h, c); // CalDAV REPORTs ... fmgwac + if (c->room_default_view == VIEW_CALENDAR) { + caldav_report(h, c); // CalDAV REPORTs ... fmgwac return; } - do_404(h); // future implementations like CardDAV will require code paths here + do_404(h); // future implementations like CardDAV will require code paths here } @@ -264,17 +239,12 @@ void options_the_room_itself(struct http_transaction *h, struct ctdlsession *c) { h->response_code = 200; h->response_string = strdup("OK"); - if (c->room_default_view == VIEW_CALENDAR) - { + if (c->room_default_view == VIEW_CALENDAR) { add_response_header(h, strdup("DAV"), strdup("1, calendar-access")); // offer CalDAV - } - else if (c->room_default_view == VIEW_ADDRESSBOOK) - { + } else if (c->room_default_view == VIEW_ADDRESSBOOK) { add_response_header(h, strdup("DAV"), strdup("1, addressbook")); // offer CardDAV - } - else - { - add_response_header(h, strdup("DAV"), strdup("1")); // ordinary WebDAV for all other room types + } else { + add_response_header(h, strdup("DAV"), strdup("1")); // ordinary WebDAV for all other room types } add_response_header(h, strdup("Allow"), strdup("OPTIONS, PROPFIND, GET, PUT, REPORT, DELETE")); } @@ -292,11 +262,7 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) StrBuf *Buf = NewStrBuf(); StrBufAppendPrintf(Buf, "" - "" - ); + ""); /* Transmit the collection resource */ StrBufAppendPrintf(Buf, ""); @@ -313,48 +279,48 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) StrBufXMLEscAppend(Buf, NULL, c->room, strlen(c->room), 0); StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); // empty owner ought to be legal; see rfc3744 section 5.1 + StrBufAppendPrintf(Buf, ""); // empty owner ought to be legal; see rfc3744 section 5.1 StrBufAppendPrintf(Buf, ""); - switch(c->room_default_view) - { - case VIEW_CALENDAR: - StrBufAppendPrintf(Buf, ""); // RFC4791 section 4.2 - break; + switch (c->room_default_view) { + case VIEW_CALENDAR: + StrBufAppendPrintf(Buf, ""); // RFC4791 section 4.2 + break; } StrBufAppendPrintf(Buf, ""); - int enumerate_by_euid = 0; // nonzero if messages will be retrieved by euid instead of msgnum - switch(c->room_default_view) - { - case VIEW_CALENDAR: // RFC4791 section 5.2 - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - enumerate_by_euid = 1; - break; - case VIEW_TASKS: // RFC4791 section 5.2 - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - StrBufAppendPrintf(Buf, ""); - enumerate_by_euid = 1; - break; - case VIEW_ADDRESSBOOK: // FIXME put some sort of CardDAV crapola here when we implement it - enumerate_by_euid = 1; - break; - case VIEW_WIKI: // FIXME invent "WikiDAV" ? - enumerate_by_euid = 1; - break; + int enumerate_by_euid = 0; // nonzero if messages will be retrieved by euid instead of msgnum + switch (c->room_default_view) { + case VIEW_CALENDAR: // RFC4791 section 5.2 + StrBufAppendPrintf(Buf, + ""); + StrBufAppendPrintf(Buf, ""); + StrBufAppendPrintf(Buf, ""); + StrBufAppendPrintf(Buf, ""); + enumerate_by_euid = 1; + break; + case VIEW_TASKS: // RFC4791 section 5.2 + StrBufAppendPrintf(Buf, + ""); + StrBufAppendPrintf(Buf, ""); + StrBufAppendPrintf(Buf, ""); + StrBufAppendPrintf(Buf, ""); + enumerate_by_euid = 1; + break; + case VIEW_ADDRESSBOOK: // FIXME put some sort of CardDAV crapola here when we implement it + enumerate_by_euid = 1; + break; + case VIEW_WIKI: // FIXME invent "WikiDAV" ? + enumerate_by_euid = 1; + break; } /* FIXME get the mtime - StrBufAppendPrintf(Buf, ""); - escputs(datestring); - StrBufAppendPrintf(Buf, ""); - */ + StrBufAppendPrintf(Buf, ""); + escputs(datestring); + StrBufAppendPrintf(Buf, ""); + */ StrBufAppendPrintf(Buf, ""); StrBufAppendPrintf(Buf, ""); @@ -362,37 +328,32 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) // If a depth greater than zero was specified, transmit the collection listing // BEGIN COLLECTION - if (dav_depth > 0) - { + if (dav_depth > 0) { long *msglist = get_msglist(c, "ALL"); - if (msglist) - { + if (msglist) { int i; - for (i=0; (msglist[i] > 0); ++i) - { - if ((i%10) == 0) syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i); + for (i = 0; (msglist[i] > 0); ++i) { + if ((i % 10) == 0) + syslog(LOG_DEBUG, "PROPFIND enumerated %d messages", i); e = NULL; // EUID gets stored here timestamp = 0; char cbuf[1024]; ctdl_printf(c, "MSG0 %ld|3", msglist[i]); ctdl_readline(c, cbuf, sizeof(cbuf)); - if (cbuf[0] == '1') while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) - { - if ( (enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5)) ) - { - // e = strdup(&cbuf[5]); - int elen = (2 * strlen(&cbuf[5])); - e = malloc(elen); - urlesc(e, elen, &cbuf[5]); - } - if (!strncasecmp(cbuf, "time=", 5)) - { - timestamp = atol(&cbuf[5]); + if (cbuf[0] == '1') + while (ctdl_readline(c, cbuf, sizeof(cbuf)), strcmp(cbuf, "000")) { + if ((enumerate_by_euid) && (!strncasecmp(cbuf, "exti=", 5))) { + // e = strdup(&cbuf[5]); + int elen = (2 * strlen(&cbuf[5])); + e = malloc(elen); + urlesc(e, elen, &cbuf[5]); + } + if (!strncasecmp(cbuf, "time=", 5)) { + timestamp = atol(&cbuf[5]); + } } - } - if (e == NULL) - { + if (e == NULL) { e = malloc(20); sprintf(e, "%ld", msglist[i]); } @@ -410,30 +371,29 @@ void propfind_the_room_itself(struct http_transaction *h, struct ctdlsession *c) StrBufAppendPrintf(Buf, "HTTP/1.1 200 OK"); StrBufAppendPrintf(Buf, ""); - switch(c->room_default_view) - { - case VIEW_CALENDAR: - StrBufAppendPrintf(Buf, "text/calendar; component=vevent"); - break; - case VIEW_TASKS: - StrBufAppendPrintf(Buf, "text/calendar; component=vtodo"); - break; - case VIEW_ADDRESSBOOK: - StrBufAppendPrintf(Buf, "text/x-vcard"); - break; + switch (c->room_default_view) { + case VIEW_CALENDAR: + StrBufAppendPrintf(Buf, + "text/calendar; component=vevent"); + break; + case VIEW_TASKS: + StrBufAppendPrintf(Buf, + "text/calendar; component=vtodo"); + break; + case VIEW_ADDRESSBOOK: + StrBufAppendPrintf(Buf, "text/x-vcard"); + break; } - if (timestamp > 0) - { + if (timestamp > 0) { char *datestring = http_datestring(timestamp); - if (datestring) - { + if (datestring) { StrBufAppendPrintf(Buf, ""); StrBufXMLEscAppend(Buf, NULL, datestring, strlen(datestring), 0); StrBufAppendPrintf(Buf, ""); free(datestring); } - if (enumerate_by_euid) // FIXME ajc 2017oct30 should this be inside the timestamp conditional? + if (enumerate_by_euid) // FIXME ajc 2017oct30 should this be inside the timestamp conditional? { StrBufAppendPrintf(Buf, "\"%ld\"", msglist[i]); } @@ -466,15 +426,15 @@ void get_the_room_itself(struct http_transaction *h, struct ctdlsession *c) { JsonValue *j = NewJsonObject(HKEY("gotoroom")); - JsonObjectAppend(j, NewJsonPlainString( HKEY("name"), c->room, -1)); - JsonObjectAppend(j, NewJsonNumber( HKEY("current_view"), c->room_current_view )); - JsonObjectAppend(j, NewJsonNumber( HKEY("default_view"), c->room_default_view )); - JsonObjectAppend(j, NewJsonNumber( HKEY("new_messages"), c->new_messages )); - JsonObjectAppend(j, NewJsonNumber( HKEY("total_messages"), c->total_messages )); - JsonObjectAppend(j, NewJsonNumber( HKEY("last_seen"), c->last_seen )); + JsonObjectAppend(j, NewJsonPlainString(HKEY("name"), c->room, -1)); + JsonObjectAppend(j, NewJsonNumber(HKEY("current_view"), c->room_current_view)); + JsonObjectAppend(j, NewJsonNumber(HKEY("default_view"), c->room_default_view)); + JsonObjectAppend(j, NewJsonNumber(HKEY("new_messages"), c->new_messages)); + JsonObjectAppend(j, NewJsonNumber(HKEY("total_messages"), c->total_messages)); + JsonObjectAppend(j, NewJsonNumber(HKEY("last_seen"), c->last_seen)); StrBuf *sj = NewStrBuf(); - SerializeJson(sj, j, 1); // '1' == free the source array + SerializeJson(sj, j, 1); // '1' == free the source array add_response_header(h, strdup("Content-type"), strdup("application/json")); h->response_code = 200; @@ -493,36 +453,28 @@ void the_room_itself(struct http_transaction *h, struct ctdlsession *c) { // OPTIONS method on the room itself usually is a DAV client assessing what's here. - if (!strcasecmp(h->method, "OPTIONS")) - { + if (!strcasecmp(h->method, "OPTIONS")) { options_the_room_itself(h, c); return; } - // PROPFIND method on the room itself could be looking for a directory - if (!strcasecmp(h->method, "PROPFIND")) - { + if (!strcasecmp(h->method, "PROPFIND")) { propfind_the_room_itself(h, c); return; } - // REPORT method on the room itself is probably the dreaded CalDAV tower-of-crapola - if (!strcasecmp(h->method, "REPORT")) - { + if (!strcasecmp(h->method, "REPORT")) { report_the_room_itself(h, c); return; } - // GET method on the room itself is an API call, possibly from our JavaScript front end - if (!strcasecmp(h->method, "get")) - { + if (!strcasecmp(h->method, "get")) { get_the_room_itself(h, c); return; } - // we probably want a "go to this room" for interactive access do_404(h); } @@ -538,37 +490,35 @@ void room_list(struct http_transaction *h, struct ctdlsession *c) ctdl_printf(c, "LKRA"); ctdl_readline(c, buf, sizeof(buf)); - if (buf[0] != '1') - { + if (buf[0] != '1') { do_502(h); return; } JsonValue *j = NewJsonArray(HKEY("lkra")); - while (ctdl_readline(c, buf, sizeof(buf)) , strcmp(buf, "000")) - { + while (ctdl_readline(c, buf, sizeof(buf)), strcmp(buf, "000")) { // name|QRflags|QRfloor|QRorder|QRflags2|ra|current_view|default_view|mtime JsonValue *jr = NewJsonObject(HKEY("room")); extract_token(roomname, buf, 0, '|', sizeof roomname); - JsonObjectAppend(jr, NewJsonPlainString( HKEY("name"), roomname, -1)); + JsonObjectAppend(jr, NewJsonPlainString(HKEY("name"), roomname, -1)); int ra = extract_int(buf, 5); - JsonObjectAppend(jr, NewJsonBool( HKEY("known"), (ra && UA_KNOWN))); - JsonObjectAppend(jr, NewJsonBool( HKEY("hasnewmsgs"), (ra && UA_HASNEWMSGS))); + JsonObjectAppend(jr, NewJsonBool(HKEY("known"), (ra && UA_KNOWN))); + JsonObjectAppend(jr, NewJsonBool(HKEY("hasnewmsgs"), (ra && UA_HASNEWMSGS))); int floor = extract_int(buf, 2); - JsonObjectAppend(jr, NewJsonNumber( HKEY("floor"), floor)); + JsonObjectAppend(jr, NewJsonNumber(HKEY("floor"), floor)); int rorder = extract_int(buf, 3); - JsonObjectAppend(jr, NewJsonNumber( HKEY("rorder"), rorder)); + JsonObjectAppend(jr, NewJsonNumber(HKEY("rorder"), rorder)); - JsonArrayAppend(j, jr); // add the room to the array + JsonArrayAppend(j, jr); // add the room to the array } StrBuf *sj = NewStrBuf(); - SerializeJson(sj, j, 1); // '1' == free the source array + SerializeJson(sj, j, 1); // '1' == free the source array add_response_header(h, strdup("Content-type"), strdup("application/json")); h->response_code = 200; @@ -590,71 +540,59 @@ void ctdl_r(struct http_transaction *h, struct ctdlsession *c) extract_token(requested_roomname, h->uri, 3, '/', sizeof requested_roomname); unescape_input(requested_roomname); - if (IsEmptyStr(requested_roomname)) // /ctdl/r/ + if (IsEmptyStr(requested_roomname)) // /ctdl/r/ { room_list(h, c); return; } - // If not, try to go there. - if (strcasecmp(requested_roomname, c->room)) - { + if (strcasecmp(requested_roomname, c->room)) { ctdl_printf(c, "GOTO %s", requested_roomname); ctdl_readline(c, buf, sizeof(buf)); - if (buf[0] == '2') - { + if (buf[0] == '2') { // buf[3] will indicate whether any instant messages are waiting extract_token(c->room, &buf[4], 0, '|', sizeof c->room); - c->new_messages = extract_int(&buf[4], 1); - c->total_messages = extract_int(&buf[4], 2); - // 3 (int)info Info flag: set to nonzero if the user needs to read this room's info file - // 4 (int)CCC->room.QRflags Various flags associated with this room. - // 5 (long)CCC->room.QRhighest The highest message number present in this room - c->last_seen = extract_long(&buf[4], 6); // The highest message number the user has read in this room - // 7 (int)rmailflag Boolean flag: 1 if this is a Mail> room, 0 otherwise. - // 8 (int)raideflag Nonzero if user is either Aide or a Room Aide in this room - // 9 (int)newmailcount The number of new Mail messages the user has - // 10 (int)CCC->room.QRfloor The floor number this room resides on + c->new_messages = extract_int(&buf[4], 1); + c->total_messages = extract_int(&buf[4], 2); + // 3 (int)info Info flag: set to nonzero if the user needs to read this room's info file + // 4 (int)CCC->room.QRflags Various flags associated with this room. + // 5 (long)CCC->room.QRhighest The highest message number present in this room + c->last_seen = extract_long(&buf[4], 6); // The highest message number the user has read in this room + // 7 (int)rmailflag Boolean flag: 1 if this is a Mail> room, 0 otherwise. + // 8 (int)raideflag Nonzero if user is either Aide or a Room Aide in this room + // 9 (int)newmailcount The number of new Mail messages the user has + // 10 (int)CCC->room.QRfloor The floor number this room resides on c->room_current_view = extract_int(&buf[4], 11); c->room_default_view = extract_int(&buf[4], 12); - // 13 (int)is_trash Boolean flag: 1 if this is the user's Trash folder, 0 otherwise. - // 14 (int)CCC->room.QRflags2 More flags associated with this room - // 15 (long)CCC->room.QRmtime Timestamp of the last write activity in this room - } - else - { + // 13 (int)is_trash Boolean flag: 1 if this is the user's Trash folder, 0 otherwise. + // 14 (int)CCC->room.QRflags2 More flags associated with this room + // 15 (long)CCC->room.QRmtime Timestamp of the last write activity in this room + } else { do_404(h); return; } } - // At this point our Citadel client session is "in" the specified room. - if (num_tokens(h->uri, '/') == 4) // /ctdl/r/roomname + if (num_tokens(h->uri, '/') == 4) // /ctdl/r/roomname { the_room_itself(h, c); return; } extract_token(buf, h->uri, 4, '/', sizeof buf); - if (num_tokens(h->uri, '/') == 5) - { - if (IsEmptyStr(buf)) - { - the_room_itself(h, c); // /ctdl/r/roomname/ ( same as /ctdl/r/roomname ) - } - else - { - object_in_room(h, c); // /ctdl/r/roomname/object + if (num_tokens(h->uri, '/') == 5) { + if (IsEmptyStr(buf)) { + the_room_itself(h, c); // /ctdl/r/roomname/ ( same as /ctdl/r/roomname ) + } else { + object_in_room(h, c); // /ctdl/r/roomname/object } return; } - if (num_tokens(h->uri, '/') == 6) - { - object_in_room(h, c); // /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component + if (num_tokens(h->uri, '/') == 6) { + object_in_room(h, c); // /ctdl/r/roomname/object/ or possibly /ctdl/r/roomname/object/component return; } - // If we get to this point, the client specified a valid room but requested an action we don't know how to perform. do_404(h); } diff --git a/webcit-ng/ssl.c b/webcit-ng/ssl.c index 54417bc8a..00b283e5a 100644 --- a/webcit-ng/ssl.c +++ b/webcit-ng/ssl.c @@ -39,8 +39,7 @@ void ssl_lock(int mode, int n, const char *file, int line) { if (mode & CRYPTO_LOCK) { pthread_mutex_lock(SSLCritters[n]); - } - else { + } else { pthread_mutex_unlock(SSLCritters[n]); } } @@ -58,52 +57,45 @@ void generate_key(char *keyfilename) unsigned long e = RSA_F4; FILE *fp; - if (access(keyfilename, R_OK) == 0) - { + if (access(keyfilename, R_OK) == 0) { return; } syslog(LOG_INFO, "crypto: generating RSA key pair"); - + // generate rsa key bne = BN_new(); - ret = BN_set_word(bne,e); - if (ret != 1) - { + ret = BN_set_word(bne, e); + if (ret != 1) { goto free_all; } - + rsa = RSA_new(); ret = RSA_generate_key_ex(rsa, bits, bne, NULL); - if (ret != 1) - { + if (ret != 1) { goto free_all; } - // write the key file fp = fopen(keyfilename, "w"); - if (fp != NULL) - { + if (fp != NULL) { chmod(keyfilename, 0600); if (PEM_write_RSAPrivateKey(fp, /* the file */ - rsa, /* the key */ - NULL, /* no enc */ - NULL, /* no passphr */ - 0, /* no passphr */ - NULL, /* no callbk */ - NULL /* no callbk */ - ) != 1) - { + rsa, /* the key */ + NULL, /* no enc */ + NULL, /* no passphr */ + 0, /* no passphr */ + NULL, /* no callbk */ + NULL /* no callbk */ + ) != 1) { syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error())); unlink(keyfilename); } fclose(fp); } - - // 4. free -free_all: - RSA_free(rsa); - BN_free(bne); + // 4. free + free_all: + RSA_free(rsa); + BN_free(bne); } @@ -113,7 +105,7 @@ free_all: void init_ssl(void) { const SSL_METHOD *ssl_method; - RSA *rsa=NULL; + RSA *rsa = NULL; X509_REQ *req = NULL; X509 *cer = NULL; EVP_PKEY *pk = NULL; @@ -124,19 +116,14 @@ void init_ssl(void) int rv = 0; SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *)); - if (!SSLCritters) - { + if (!SSLCritters) { syslog(LOG_ERR, "citserver: can't allocate memory!!"); exit(1); - } - else - { + } else { int a; - for (a = 0; a < CRYPTO_num_locks(); a++) - { + for (a = 0; a < CRYPTO_num_locks(); a++) { SSLCritters[a] = malloc(sizeof(pthread_mutex_t)); - if (!SSLCritters[a]) - { + if (!SSLCritters[a]) { syslog(LOG_INFO, "citserver: can't allocate memory!!"); exit(1); } @@ -150,15 +137,13 @@ void init_ssl(void) SSL_library_init(); SSL_load_error_strings(); ssl_method = SSLv23_server_method(); - if (!(ssl_ctx = SSL_CTX_new(ssl_method))) - { + if (!(ssl_ctx = SSL_CTX_new(ssl_method))) { syslog(LOG_WARNING, "SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error())); return; } syslog(LOG_INFO, "Requesting cipher list: %s", ssl_cipher_list); - if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) - { + if (!(SSL_CTX_set_cipher_list(ssl_ctx, ssl_cipher_list))) { syslog(LOG_WARNING, "SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error())); return; } @@ -182,8 +167,7 @@ void init_ssl(void) * in the next step. Therefore, if we have neither a CSR nor a certificate, generate * the CSR in this step so that the next step may commence. */ - if ( (access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0) ) - { + if ((access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0)) { syslog(LOG_INFO, "Generating a certificate signing request."); /* @@ -193,61 +177,41 @@ void init_ssl(void) * and we didn't just generate it now. */ fp = fopen(CTDL_KEY_PATH, "r"); - if (fp) - { + if (fp) { rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); fclose(fp); } - if (rsa) - { + if (rsa) { /* Create a public key from the private key */ - if (pk=EVP_PKEY_new(), pk != NULL) - { + if (pk = EVP_PKEY_new(), pk != NULL) { EVP_PKEY_assign_RSA(pk, rsa); - if (req = X509_REQ_new(), req != NULL) - { + if (req = X509_REQ_new(), req != NULL) { const char *env; /* Set the public key */ X509_REQ_set_pubkey(req, pk); X509_REQ_set_version(req, 0L); name = X509_REQ_get_subject_name(req); - X509_NAME_add_entry_by_txt( - name, "O", MBSTRING_ASC, - (unsigned char*)"Citadel Server", - -1, -1, 0 - ); - X509_NAME_add_entry_by_txt( - name, "OU", MBSTRING_ASC, - (unsigned char*)"Default Certificate PLEASE CHANGE", - -1, -1, 0 - ); - X509_NAME_add_entry_by_txt( - name, "CN", - MBSTRING_ASC, - (unsigned char*)"*", - -1, -1, 0 - ); - + X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, + (unsigned char *) "Citadel Server", -1, -1, 0); + X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, + (unsigned char *) "Default Certificate PLEASE CHANGE", + -1, -1, 0); + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *) "*", -1, -1, 0); + X509_REQ_set_subject_name(req, name); /* Sign the CSR */ - if (!X509_REQ_sign(req, pk, EVP_md5())) - { + if (!X509_REQ_sign(req, pk, EVP_md5())) { syslog(LOG_WARNING, "X509_REQ_sign(): error"); - } - else - { - /* Write it to disk. */ + } else { + /* Write it to disk. */ fp = fopen(CTDL_CSR_PATH, "w"); - if (fp != NULL) - { + if (fp != NULL) { chmod(CTDL_CSR_PATH, 0600); PEM_write_X509_REQ(fp, req); fclose(fp); - } - else - { + } else { syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CSR_PATH); exit(1); } @@ -256,9 +220,7 @@ void init_ssl(void) } } RSA_free(rsa); - } - else - { + } else { syslog(LOG_WARNING, "Unable to read private key."); } } @@ -266,16 +228,14 @@ void init_ssl(void) /* * Generate a self-signed certificate if we don't have one. */ - if (access(CTDL_CER_PATH, R_OK) != 0) - { + if (access(CTDL_CER_PATH, R_OK) != 0) { syslog(LOG_INFO, "Generating a self-signed certificate."); /* Same deal as before: always read the key from disk because * it may or may not have just been generated. */ fp = fopen(CTDL_KEY_PATH, "r"); - if (fp) - { + if (fp) { rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); fclose(fp); } @@ -284,50 +244,39 @@ void init_ssl(void) req = NULL; cer = NULL; pk = NULL; - if (rsa) - { - if (pk=EVP_PKEY_new(), pk != NULL) - { + if (rsa) { + if (pk = EVP_PKEY_new(), pk != NULL) { EVP_PKEY_assign_RSA(pk, rsa); } fp = fopen(CTDL_CSR_PATH, "r"); - if (fp) - { + if (fp) { req = PEM_read_X509_REQ(fp, NULL, NULL, NULL); fclose(fp); } - if (req) - { - if (cer = X509_new(), cer != NULL) - { + if (req) { + if (cer = X509_new(), cer != NULL) { ASN1_INTEGER_set(X509_get_serialNumber(cer), 0); X509_set_issuer_name(cer, X509_REQ_get_subject_name(req)); X509_set_subject_name(cer, X509_REQ_get_subject_name(req)); X509_gmtime_adj(X509_get_notBefore(cer), 0); - X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS); + X509_gmtime_adj(X509_get_notAfter(cer), (long) 60 * 60 * 24 * SIGN_DAYS); req_pkey = X509_REQ_get_pubkey(req); X509_set_pubkey(cer, req_pkey); EVP_PKEY_free(req_pkey); - + /* Sign the cert */ - if (!X509_sign(cer, pk, EVP_md5())) - { + if (!X509_sign(cer, pk, EVP_md5())) { syslog(LOG_WARNING, "X509_sign(): error"); - } - else - { /* Write it to disk. */ + } else { /* Write it to disk. */ fp = fopen(CTDL_CER_PATH, "w"); - if (fp != NULL) - { + if (fp != NULL) { chmod(CTDL_CER_PATH, 0600); PEM_write_X509(fp, cer); fclose(fp); - } - else - { + } else { syslog(LOG_WARNING, "Cannot write key: %s", CTDL_CER_PATH); exit(1); } @@ -346,11 +295,10 @@ void init_ssl(void) */ SSL_CTX_use_certificate_chain_file(ssl_ctx, CTDL_CER_PATH); SSL_CTX_use_PrivateKey_file(ssl_ctx, CTDL_KEY_PATH, SSL_FILETYPE_PEM); - if ( !SSL_CTX_check_private_key(ssl_ctx) ) - { + if (!SSL_CTX_check_private_key(ssl_ctx)) { syslog(LOG_WARNING, "Cannot install certificate: %s", ERR_reason_error_string(ERR_get_error())); } - + } @@ -361,68 +309,54 @@ void starttls(struct client_handle *ch) { int retval, bits, alg_bits; - if (!ssl_ctx) - { + if (!ssl_ctx) { return; } - if (!(ch->ssl_handle = SSL_new(ssl_ctx))) - { + if (!(ch->ssl_handle = SSL_new(ssl_ctx))) { syslog(LOG_WARNING, "SSL_new failed: %s", ERR_reason_error_string(ERR_get_error())); return; } - if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) - { + if (!(SSL_set_fd(ch->ssl_handle, ch->sock))) { syslog(LOG_WARNING, "SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error())); SSL_free(ch->ssl_handle); return; } retval = SSL_accept(ch->ssl_handle); - if (retval < 1) - { + if (retval < 1) { long errval; const char *ssl_error_reason = NULL; errval = SSL_get_error(ch->ssl_handle, retval); ssl_error_reason = ERR_reason_error_string(ERR_get_error()); - if (ssl_error_reason == NULL) - { + if (ssl_error_reason == NULL) { syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d %s", errval, retval, strerror(errval)); - } - else - { + } else { syslog(LOG_WARNING, "SSL_accept failed: %s\n", ssl_error_reason); } sleep(1); retval = SSL_accept(ch->ssl_handle); } - if (retval < 1) - { + if (retval < 1) { long errval; const char *ssl_error_reason = NULL; errval = SSL_get_error(ch->ssl_handle, retval); ssl_error_reason = ERR_reason_error_string(ERR_get_error()); - if (ssl_error_reason == NULL) - { + if (ssl_error_reason == NULL) { syslog(LOG_WARNING, "SSL_accept failed: errval=%ld, retval=%d (%s)", errval, retval, strerror(errval)); - } - else - { + } else { syslog(LOG_WARNING, "SSL_accept failed: %s", ssl_error_reason); } SSL_free(ch->ssl_handle); ch->ssl_handle = NULL; return; - } - else - { + } else { syslog(LOG_INFO, "SSL_accept success"); } bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ch->ssl_handle), &alg_bits); syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)", - SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)), - SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), - bits, alg_bits); + SSL_CIPHER_get_name(SSL_get_current_cipher(ch->ssl_handle)), + SSL_CIPHER_get_version(SSL_get_current_cipher(ch->ssl_handle)), bits, alg_bits); syslog(LOG_INFO, "SSL started"); } @@ -434,8 +368,7 @@ void starttls(struct client_handle *ch) void endtls(struct client_handle *ch) { syslog(LOG_INFO, "Ending SSL/TLS"); - if (ch->ssl_handle != NULL) - { + if (ch->ssl_handle != NULL) { SSL_shutdown(ch->ssl_handle); SSL_get_SSL_CTX(ch->ssl_handle); SSL_free(ch->ssl_handle); @@ -453,32 +386,27 @@ int client_write_ssl(struct client_handle *ch, char *buf, int nbytes) int nremain; char junk[1]; - if (ch->ssl_handle == NULL) return(-1); + if (ch->ssl_handle == NULL) + return (-1); nremain = nbytes; - while (nremain > 0) - { - if (SSL_want_write(ch->ssl_handle)) - { - if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) - { + while (nremain > 0) { + if (SSL_want_write(ch->ssl_handle)) { + if ((SSL_read(ch->ssl_handle, junk, 0)) < 1) { syslog(LOG_WARNING, "SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error())); } } retval = SSL_write(ch->ssl_handle, &buf[nbytes - nremain], nremain); - if (retval < 1) - { + if (retval < 1) { long errval; errval = SSL_get_error(ch->ssl_handle, retval); - if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) - { + if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) { sleep(1); continue; } syslog(LOG_WARNING, "SSL_write got error %ld, ret %d", errval, retval); - if (retval == -1) - { + if (retval == -1) { syslog(LOG_WARNING, "errno is %d", errno); endtls(ch); } @@ -499,24 +427,20 @@ int client_read_ssl(struct client_handle *ch, char *buf, int nbytes) int rlen = 0; char junk[1]; - if (ch->ssl_handle == NULL) return(-1); + if (ch->ssl_handle == NULL) + return (-1); - while (bytes_read < nbytes) - { - if (SSL_want_read(ch->ssl_handle)) - { - if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) - { + while (bytes_read < nbytes) { + if (SSL_want_read(ch->ssl_handle)) { + if ((SSL_write(ch->ssl_handle, junk, 0)) < 1) { syslog(LOG_WARNING, "SSL_write in client_read"); } } - rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes-bytes_read); - if (rlen < 1) - { + rlen = SSL_read(ch->ssl_handle, &buf[bytes_read], nbytes - bytes_read); + if (rlen < 1) { long errval; errval = SSL_get_error(ch->ssl_handle, rlen); - if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) - { + if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) { sleep(1); continue; } @@ -526,5 +450,5 @@ int client_read_ssl(struct client_handle *ch, char *buf, int nbytes) } bytes_read += rlen; } - return(bytes_read); + return (bytes_read); } diff --git a/webcit-ng/static.c b/webcit-ng/static.c index 207cbabdb..73a8411e5 100644 --- a/webcit-ng/static.c +++ b/webcit-ng/static.c @@ -25,12 +25,12 @@ void output_static(struct http_transaction *h) snprintf(filename, sizeof filename, "static/%s", &h->uri[8]); - if (strstr(filename, "../")) { // 100% guaranteed attacker. - do_404(h); // Die in a car fire. + if (strstr(filename, "../")) { // 100% guaranteed attacker. + do_404(h); // Die in a car fire. return; } - FILE *fp = fopen(filename, "r"); // Try to open the requested file. + FILE *fp = fopen(filename, "r"); // Try to open the requested file. if (fp == NULL) { do_404(h); return; @@ -41,11 +41,10 @@ void output_static(struct http_transaction *h) h->response_body = malloc(h->response_body_length); if (h->response_body != NULL) { fread(h->response_body, h->response_body_length, 1, fp); - } - else { + } else { h->response_body_length = 0; } - fclose(fp); // Content is now in memory. + fclose(fp); // Content is now in memory. h->response_code = 200; h->response_string = strdup("OK"); diff --git a/webcit-ng/tcp_sockets.c b/webcit-ng/tcp_sockets.c index 767582eb2..19e9e4284 100644 --- a/webcit-ng/tcp_sockets.c +++ b/webcit-ng/tcp_sockets.c @@ -71,61 +71,55 @@ int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len) int s, i, b; int ip_version = 6; -retry: + retry: memset(&sin6, 0, sizeof(sin6)); memset(&sin4, 0, sizeof(sin4)); sin6.sin6_family = AF_INET6; sin4.sin_family = AF_INET; - if ( (ip_addr == NULL) /* any IPv6 */ - || (IsEmptyStr(ip_addr)) - || (!strcmp(ip_addr, "*")) - ) { + if ((ip_addr == NULL) /* any IPv6 */ + ||(IsEmptyStr(ip_addr)) + || (!strcmp(ip_addr, "*")) + ) { IsDefault = 1; ip_version = 6; sin6.sin6_addr = in6addr_any; - } - else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */ - { + } else if (!strcmp(ip_addr, "0.0.0.0")) { /* any IPv4 */ ip_version = 4; sin4.sin_addr.s_addr = INADDR_ANY; - } - else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */ - { + } else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) { /* specific IPv4 */ ip_version = 4; if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) { syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno)); - return(-1); + return (-1); } - } - else /* specific IPv6 */ - { + } else { /* specific IPv6 */ + ip_version = 6; if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) { syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno)); - return(-1); + return (-1); } } if (port_number == 0) { syslog(LOG_WARNING, "Cannot start: no port number specified.\n"); - return(-1); + return (-1); } sin6.sin6_port = htons((u_short) port_number); sin4.sin_port = htons((u_short) port_number); p = getprotobyname("tcp"); - s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto)); + s = socket(((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto)); if (s < 0) { - if (IsDefault && (errno == EAFNOSUPPORT)) - { + if (IsDefault && (errno == EAFNOSUPPORT)) { s = 0; ip_addr = ipv4broadcast; goto retry; } syslog(LOG_WARNING, "Can't create a listening socket: %s\n", strerror(errno)); - return(-1); + return (-1); } /* Set some socket options that make sense. */ i = 1; @@ -133,21 +127,20 @@ retry: if (ip_version == 6) { b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6)); - } - else { + } else { b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4)); } if (b < 0) { syslog(LOG_ERR, "Can't bind: %s\n", strerror(errno)); close(s); - return(-1); + return (-1); } if (listen(s, queue_len) < 0) { syslog(LOG_ERR, "Can't listen: %s\n", strerror(errno)); close(s); - return(-1); + return (-1); } return (s); } @@ -166,12 +159,13 @@ int webcit_uds_server(char *sockpath, int queue_len) int actual_queue_len; actual_queue_len = queue_len; - if (actual_queue_len < 5) actual_queue_len = 5; + if (actual_queue_len < 5) + actual_queue_len = 5; i = unlink(sockpath); if ((i != 0) && (errno != ENOENT)) { syslog(LOG_WARNING, "webcit: can't unlink %s: %s", sockpath, strerror(errno)); - return(-1); + return (-1); } memset(&addr, 0, sizeof(addr)); @@ -181,21 +175,21 @@ int webcit_uds_server(char *sockpath, int queue_len) s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { syslog(LOG_WARNING, "webcit: Can't create a unix domain socket: %s", strerror(errno)); - return(-1); + return (-1); } - if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { syslog(LOG_WARNING, "webcit: Can't bind: %s", strerror(errno)); close(s); - return(-1); + return (-1); } if (listen(s, actual_queue_len) < 0) { syslog(LOG_WARNING, "webcit: Can't listen: %s", strerror(errno)); close(s); - return(-1); + return (-1); } chmod(sockpath, 0777); - return(s); + return (s); } diff --git a/webcit-ng/text2html.c b/webcit-ng/text2html.c index bffb90e88..cfbcfb7d0 100644 --- a/webcit-ng/text2html.c +++ b/webcit-ng/text2html.c @@ -18,33 +18,33 @@ /* * Convert a text/plain message to text/html */ -StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) +StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf * Source) { StrBuf *sj = NULL; sj = NewStrBuf(); if (!sj) { - return(sj); + return (sj); } StrBufAppendPrintf(sj, "
    ");
    -	StrEscAppend(sj, Source, NULL, 0, 0);		// FIXME - add code here to activate links
    +	StrEscAppend(sj, Source, NULL, 0, 0);	// FIXME - add code here to activate links
     	StrBufAppendPrintf(sj, "
    \n"); - return(sj); + return (sj); } /* * Convert a text/x-citadel-variformat message to text/html */ -StrBuf *variformat2html(StrBuf *Source) +StrBuf *variformat2html(StrBuf * Source) { StrBuf *Target = NULL; Target = NewStrBuf(); if (!Target) { - return(Target); + return (Target); } const char *ptr, *pte; @@ -58,9 +58,8 @@ StrBuf *variformat2html(StrBuf *Source) long len; int intext = 0; - if (StrLength(Source) > 0) - do - { + if (StrLength(Source) > 0) + do { StrBufSipLine(Line, Source, &BufPtr); bq = 0; i = 0; @@ -77,7 +76,7 @@ StrBuf *variformat2html(StrBuf *Source) if (*ptr == '>') { bq++; } - ptr ++; + ptr++; i++; } } @@ -87,11 +86,12 @@ StrBuf *variformat2html(StrBuf *Source) * different colour. This code understands Citadel-style * " >" quotes and will convert to
    tags. */ - if (i > 0) StrBufCutLeft(Line, i); + if (i > 0) + StrBufCutLeft(Line, i); - for (i = bn; i < bq; i++) + for (i = bn; i < bq; i++) StrBufAppendBufPlain(Target, HKEY("
    "), 0); - for (i = bq; i < bn; i++) + for (i = bq; i < bn; i++) StrBufAppendBufPlain(Target, HKEY("
    "), 0); bn = bq; @@ -105,8 +105,7 @@ StrBuf *variformat2html(StrBuf *Source) StrBufAppendBufPlain(Target, HKEY("\n"), 0); } - while ((BufPtr != StrBufNOTNULL) && - (BufPtr != NULL)); + while ((BufPtr != StrBufNOTNULL) && (BufPtr != NULL)); for (i = 0; i < bn; i++) { StrBufAppendBufPlain(Target, HKEY("
    "), 0); @@ -115,7 +114,5 @@ StrBuf *variformat2html(StrBuf *Source) FreeStrBuf(&Line); FreeStrBuf(&Line1); FreeStrBuf(&Line2); - return(Target); + return (Target); } - - diff --git a/webcit-ng/util.c b/webcit-ng/util.c index 6d0c69d62..825786f7e 100644 --- a/webcit-ng/util.c +++ b/webcit-ng/util.c @@ -27,12 +27,12 @@ int unescape_input(char *buf) buflen = strlen(buf); - while ((buflen > 0) && (isspace(buf[buflen - 1]))){ + while ((buflen > 0) && (isspace(buf[buflen - 1]))) { buf[buflen - 1] = 0; - buflen --; + buflen--; } - a = 0; + a = 0; while (a < buflen) { if (buf[a] == '+') buf[a] = ' '; @@ -41,8 +41,7 @@ int unescape_input(char *buf) if (a + 2 > buflen) { buf[a] = '\0'; buflen = a; - } - else { + } else { hex[0] = buf[a + 1]; hex[1] = buf[a + 2]; hex[2] = 0; @@ -52,8 +51,8 @@ int unescape_input(char *buf) len = buflen - a - 2; if (len > 0) memmove(&buf[a + 1], &buf[a + 3], len); - - buflen -=2; + + buflen -= 2; } } a++; @@ -66,14 +65,15 @@ int unescape_input(char *buf) * Supplied with a unix timestamp, generate a textual time/date stamp. * Caller owns the returned memory. */ -char *http_datestring(time_t xtime) { +char *http_datestring(time_t xtime) +{ /* HTTP Months - do not translate - these are not for human consumption */ static char *httpdate_months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - + /* HTTP Weekdays - do not translate - these are not for human consumption */ static char *httpdate_weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -84,7 +84,8 @@ char *http_datestring(time_t xtime) { char offsign; int n = 40; char *buf = malloc(n); - if (!buf) return(NULL); + if (!buf) + return (NULL); localtime_r(&xtime, &t); @@ -92,22 +93,14 @@ char *http_datestring(time_t xtime) { offset = t.tm_gmtoff; if (offset > 0) { offsign = '+'; - } - else { + } else { offset = 0L - offset; offsign = '-'; } - offset = ( (offset / 3600) * 100 ) + ( offset % 60 ); + offset = ((offset / 3600) * 100) + (offset % 60); snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld", - httpdate_weekdays[t.tm_wday], - t.tm_mday, - httpdate_months[t.tm_mon], - t.tm_year + 1900, - t.tm_hour, - t.tm_min, - t.tm_sec, - offsign, offset - ); - return(buf); + httpdate_weekdays[t.tm_wday], + t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset); + return (buf); } diff --git a/webcit-ng/webserver.c b/webcit-ng/webserver.c index 25b87ec10..aaa73ee0e 100644 --- a/webcit-ng/webserver.c +++ b/webcit-ng/webserver.c @@ -29,8 +29,8 @@ static void *original_brk = NULL; // Remember the original program break so we c */ void spawn_another_worker_thread(int *pointer_to_master_socket) { - pthread_t th; // Thread descriptor - pthread_attr_t attr; // Thread attributes + pthread_t th; // Thread descriptor + pthread_attr_t attr; // Thread attributes /* set attributes for the new thread */ pthread_attr_init(&attr); @@ -38,10 +38,9 @@ void spawn_another_worker_thread(int *pointer_to_master_socket) pthread_attr_setstacksize(&attr, 1048576); // Large stacks to prevent MIME parser crash on FreeBSD /* now create the thread */ - if (pthread_create(&th, &attr, (void *(*)(void *)) worker_entry, (void *)pointer_to_master_socket) != 0) { + if (pthread_create(&th, &attr, (void *(*)(void *)) worker_entry, (void *) pointer_to_master_socket) != 0) { syslog(LOG_WARNING, "Can't create thread: %s", strerror(errno)); - } - else { + } else { ++num_threads_existing; ++num_threads_executing; } @@ -61,25 +60,27 @@ void worker_entry(int *pointer_to_master_socket) int fail_this_transaction = 0; struct client_handle ch; - while(1) { + while (1) { /* Each worker thread blocks on accept() while waiting for something to do. */ memset(&ch, 0, sizeof ch); - ch.sock = -1; + ch.sock = -1; errno = EAGAIN; do { --num_threads_executing; - syslog(LOG_DEBUG, "Additional memory allocated since startup: %d bytes", (int)(sbrk(0)-original_brk)); - syslog(LOG_DEBUG, "Thread 0x%x calling accept() on master socket %d", (unsigned int)pthread_self(), master_socket); + syslog(LOG_DEBUG, "Additional memory allocated since startup: %d bytes", (int) (sbrk(0) - original_brk)); + syslog(LOG_DEBUG, "Thread 0x%x calling accept() on master socket %d", (unsigned int) pthread_self(), + master_socket); ch.sock = accept(master_socket, NULL, 0); if (ch.sock < 0) { syslog(LOG_DEBUG, "accept() : %s", strerror(errno)); } ++num_threads_executing; - syslog(LOG_DEBUG, "socket %d is awake , threads executing: %d , threads total: %d", ch.sock, num_threads_executing, num_threads_existing); + syslog(LOG_DEBUG, "socket %d is awake , threads executing: %d , threads total: %d", ch.sock, + num_threads_executing, num_threads_existing); } while ((master_socket > 0) && (ch.sock < 0)); /* If all threads are executing, spawn more, up to the maximum */ - if ( (num_threads_executing >= num_threads_existing) && (num_threads_existing <= MAX_WORKER_THREADS) ) { + if ((num_threads_executing >= num_threads_existing) && (num_threads_existing <= MAX_WORKER_THREADS)) { spawn_another_worker_thread(pointer_to_master_socket); } @@ -95,10 +96,8 @@ void worker_entry(int *pointer_to_master_socket) if (ch.ssl_handle == NULL) { fail_this_transaction = 1; } - } - else - { - int fdflags; + } else { + int fdflags; fdflags = fcntl(ch.sock, F_GETFL); if (fdflags < 0) { syslog(LOG_WARNING, "unable to get server socket flags! %s", strerror(errno)); @@ -128,33 +127,33 @@ void worker_entry(int *pointer_to_master_socket) */ int webserver(char *webserver_interface, int webserver_port, int webserver_protocol) { - int master_socket = (-1) ; + int master_socket = (-1); original_brk = sbrk(0); - switch(webserver_protocol) { - case WEBSERVER_HTTP: - syslog(LOG_DEBUG, "Starting HTTP server on %s:%d", webserver_interface, webserver_port); - master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10); - break; - case WEBSERVER_HTTPS: - syslog(LOG_DEBUG, "Starting HTTPS server on %s:%d", webserver_interface, webserver_port); - master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10); - init_ssl(); - is_https = 1; - break; - default: - syslog(LOG_ERR, "unknown protocol"); - ;; + switch (webserver_protocol) { + case WEBSERVER_HTTP: + syslog(LOG_DEBUG, "Starting HTTP server on %s:%d", webserver_interface, webserver_port); + master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10); + break; + case WEBSERVER_HTTPS: + syslog(LOG_DEBUG, "Starting HTTPS server on %s:%d", webserver_interface, webserver_port); + master_socket = webcit_tcp_server(webserver_interface, webserver_port, 10); + init_ssl(); + is_https = 1; + break; + default: + syslog(LOG_ERR, "unknown protocol"); + ;; } if (master_socket < 1) { syslog(LOG_ERR, "Unable to bind the web server listening socket"); - return(1); + return (1); } syslog(LOG_INFO, "Listening on socket %d", master_socket); signal(SIGPIPE, SIG_IGN); - worker_entry(&master_socket); // this thread becomes a worker - return(0); + worker_entry(&master_socket); // this thread becomes a worker + return (0); }