2 * Copyright (c) 1987-2012 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 * Uncomment this to log all communications with the Citadel server
19 #include "webserver.h"
21 long MaxRead = -1; /* should we do READ scattered or all at once? */
24 * register the timeout
26 RETSIGTYPE timeout(int signum)
28 syslog(LOG_WARNING, "Connection timed out; unable to reach citserver\n");
29 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
34 * Client side - connect to a unix domain socket
36 int uds_connectsock(char *sockpath)
38 struct sockaddr_un addr;
41 memset(&addr, 0, sizeof(addr));
42 addr.sun_family = AF_UNIX;
43 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
45 s = socket(AF_UNIX, SOCK_STREAM, 0);
47 syslog(LOG_WARNING, "Can't create socket [%s]: %s\n", sockpath, strerror(errno));
51 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
52 syslog(LOG_WARNING, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
62 * TCP client - connect to a host/port
64 int tcp_connectsock(char *host, char *service)
66 struct in6_addr serveraddr;
67 struct addrinfo hints;
68 struct addrinfo *res = NULL;
69 struct addrinfo *ai = NULL;
73 if ((host == NULL) || IsEmptyStr(host))
75 if ((service == NULL) || IsEmptyStr(service))
78 syslog(LOG_DEBUG, "tcp_connectsock(%s,%s)\n", host, service);
80 memset(&hints, 0x00, sizeof(hints));
81 hints.ai_flags = AI_NUMERICSERV;
82 hints.ai_family = AF_UNSPEC;
83 hints.ai_socktype = SOCK_STREAM;
86 * Handle numeric IPv4 and IPv6 addresses
88 rc = inet_pton(AF_INET, host, &serveraddr);
89 if (rc == 1) { /* dotted quad */
90 hints.ai_family = AF_INET;
91 hints.ai_flags |= AI_NUMERICHOST;
93 rc = inet_pton(AF_INET6, host, &serveraddr);
94 if (rc == 1) { /* IPv6 address */
95 hints.ai_family = AF_INET6;
96 hints.ai_flags |= AI_NUMERICHOST;
100 /* Begin the connection process */
102 rc = getaddrinfo(host, service, &hints, &res);
104 syslog(LOG_DEBUG, "%s: %s\n", host, gai_strerror(rc));
110 * Try all available addresses until we connect to one or until we run out.
112 for (ai = res; ai != NULL; ai = ai->ai_next) {
114 if (ai->ai_family == AF_INET) syslog(LOG_DEBUG, "Trying IPv4\n");
115 else if (ai->ai_family == AF_INET6) syslog(LOG_DEBUG, "Trying IPv6\n");
116 else syslog(LOG_WARNING, "This is going to fail.\n");
118 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
120 syslog(LOG_WARNING, "socket() failed: %s\n", strerror(errno));
124 rc = connect(s, ai->ai_addr, ai->ai_addrlen);
130 syslog(LOG_WARNING, "connect() failed: %s\n", strerror(errno));
140 * input string from pipe
142 int serv_getln(char *strbuf, int bufsize)
148 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
149 len = StrLength(WCC->MigrateReadLineBuf);
152 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
153 FlushStrBuf(WCC->MigrateReadLineBuf);
156 syslog(LOG_DEBUG, "%3d<<<%s\n", WC->serv_sock, strbuf);
162 int StrBuf_ServGetln(StrBuf *buf)
165 const char *ErrStr = NULL;
172 rc = StrBufTCP_read_buffered_line_fast(buf,
180 syslog(LOG_INFO, "StrBuf_ServGetln(): Server connection broken: %s\n",
182 wc_backtrace(LOG_INFO);
183 if (WCC->serv_sock > 0) close(WCC->serv_sock);
184 WCC->serv_sock = (-1);
192 if (WCC->ReadPos != NULL)
193 pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
194 syslog(LOG_DEBUG, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
200 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
206 rc = StrBufReadBLOBBuffered(buf,
216 syslog(LOG_INFO, "StrBuf_ServGetBLOBBuffered(): Server connection broken: %s\n",
218 wc_backtrace(LOG_INFO);
219 if (WCC->serv_sock > 0) close(WCC->serv_sock);
220 WCC->serv_sock = (-1);
226 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
232 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
239 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
242 syslog(LOG_INFO, "StrBuf_ServGetBLOB(): Server connection broken: %s\n",
244 wc_backtrace(LOG_INFO);
245 if (WCC->serv_sock > 0) close(WCC->serv_sock);
246 WCC->serv_sock = (-1);
252 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
259 void FlushReadBuf (void)
266 len = StrLength(WCC->ReadBuf);
268 (WCC->ReadPos != NULL) &&
269 (WCC->ReadPos != StrBufNOTNULL))
272 pch = ChrPtr(WCC->ReadBuf);
274 if (WCC->ReadPos != pche)
277 "ERROR: somebody didn't eat his soup! Remaing Chars: %ld [%s]\n",
278 (long)(pche - WCC->ReadPos),
282 "--------------------------------------------------------------------------------\n"
284 "--------------------------------------------------------------------------------\n",
286 AppendImportantMessage(HKEY("Suppenkasper alert! watch your webcit logfile and get connected to your favourite opensource Crew."));
290 FlushStrBuf(WCC->ReadBuf);
298 * send binary to server
299 * buf the buffer to write to citadel server
300 * nbytes how many bytes to send to citadel server
302 int serv_write(const char *buf, int nbytes)
305 int bytes_written = 0;
309 while (bytes_written < nbytes) {
310 retval = write(WCC->serv_sock, &buf[bytes_written],
311 nbytes - bytes_written);
313 const char *ErrStr = strerror(errno);
314 syslog(LOG_INFO, "serv_write(): Server connection broken: %s\n",
316 if (WCC->serv_sock > 0) close(WCC->serv_sock);
317 WCC->serv_sock = (-1);
322 bytes_written = bytes_written + retval;
329 * send line to server
330 * string the line to send to the citadel server
332 int serv_puts(const char *string)
335 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, string);
339 if (!serv_write(string, strlen(string)))
341 return serv_write("\n", 1);
345 * send line to server
346 * string the line to send to the citadel server
348 int serv_putbuf(const StrBuf *string)
351 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
355 if (!serv_write(ChrPtr(string), StrLength(string)))
357 return serv_write("\n", 1);
362 * convenience function to send stuff to the server
363 * format the formatstring
364 * ... the entities to insert into format
366 int serv_printf(const char *format,...)
375 va_start(arg_ptr, format);
376 vsnprintf(buf, sizeof buf, format, arg_ptr);
382 rc = serv_write(buf, len);
384 syslog(LOG_DEBUG, ">>>%s", buf);
391 * Read binary data from server into memory using a series of server READ commands.
392 * returns the read content as StrBuf
394 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
397 size_t bytes_read = 0;
398 size_t this_block = 0;
406 while ((bytes_read < total_len) && (ServerRc == 6)) {
408 if (WCC->serv_sock==-1) {
413 serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
414 if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
415 (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) )
419 StrBufCutLeft(Buf, 4);
420 this_block = StrTol(Buf);
421 rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
423 syslog(LOG_INFO, "Server connection broken during download\n");
424 wc_backtrace(LOG_INFO);
425 if (WCC->serv_sock > 0) close(WCC->serv_sock);
426 WCC->serv_sock = (-1);
435 return StrLength(Ret);
439 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
443 const char *pch, *pchs;
444 int rlen, len, retval = 0;
448 if (StrLength(Hdr->ReadBuf) > 0)
450 pchs = ChrPtr(Hdr->ReadBuf);
451 pch = strchr(pchs, '\n');
455 if (len > 0 && (*(pch - 1) == '\r') )
457 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
458 StrBufCutLeft(Hdr->ReadBuf, len + 1);
463 while (retval == 0) {
465 pchs = ChrPtr(Hdr->ReadBuf);
467 pch = strchr(pchs, '\n');
469 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
470 pchs = ChrPtr(Hdr->ReadBuf);
471 pch = strchr(pchs, '\n');
482 if ((retval > 0) && (pch != NULL)) {
485 if (len > 0 && (*(pch - 1) == '\r') )
487 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
488 StrBufCutLeft(Hdr->ReadBuf, len + 1);
497 return StrBufTCP_read_buffered_line_fast(Target,
508 * This is a generic function to set up a master socket for listening on
509 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
511 * ip_addr IP address to bind
512 * port_number port number to bind
513 * queue_len number of incoming connections to allow in the queue
515 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len)
517 const char *ipv4broadcast = "0.0.0.0";
520 struct sockaddr_in6 sin6;
521 struct sockaddr_in sin4;
526 memset(&sin6, 0, sizeof(sin6));
527 memset(&sin4, 0, sizeof(sin4));
528 sin6.sin6_family = AF_INET6;
529 sin4.sin_family = AF_INET;
531 if ( (ip_addr == NULL) /* any IPv6 */
532 || (IsEmptyStr(ip_addr))
533 || (!strcmp(ip_addr, "*"))
537 sin6.sin6_addr = in6addr_any;
539 else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
542 sin4.sin_addr.s_addr = INADDR_ANY;
544 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
547 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
548 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
549 return (-WC_EXIT_BIND);
552 else /* specific IPv6 */
555 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
556 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
557 return (-WC_EXIT_BIND);
561 if (port_number == 0) {
562 syslog(LOG_WARNING, "Cannot start: no port number specified.\n");
563 return (-WC_EXIT_BIND);
565 sin6.sin6_port = htons((u_short) port_number);
566 sin4.sin_port = htons((u_short) port_number);
568 p = getprotobyname("tcp");
570 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
573 if (IsDefault && (errno == EAFNOSUPPORT))
576 ip_addr = ipv4broadcast;
580 >>>>>>> 156d3ea... SYSLOG: more places to correct the loglevel parameters.
581 syslog(LOG_WARNING, "Can't create a listening socket: %s\n", strerror(errno));
582 return (-WC_EXIT_BIND);
584 /* Set some socket options that make sense. */
586 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
588 if (ip_version == 6) {
589 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
592 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
596 syslog(LOG_EMERG, "Can't bind: %s\n", strerror(errno));
598 return (-WC_EXIT_BIND);
601 if (listen(s, queue_len) < 0) {
602 syslog(LOG_EMERG, "Can't listen: %s\n", strerror(errno));
604 return (-WC_EXIT_BIND);
611 * Create a Unix domain socket and listen on it
612 * sockpath - file name of the unix domain socket
613 * queue_len - Number of incoming connections to allow in the queue
615 int webcit_uds_server(char *sockpath, int queue_len)
617 struct sockaddr_un addr;
620 int actual_queue_len;
622 actual_queue_len = queue_len;
623 if (actual_queue_len < 5) actual_queue_len = 5;
625 i = unlink(sockpath);
626 if ((i != 0) && (errno != ENOENT)) {
627 syslog(LOG_WARNING, "webcit: can't unlink %s: %s\n",
628 sockpath, strerror(errno));
629 return (-WC_EXIT_BIND);
632 memset(&addr, 0, sizeof(addr));
633 addr.sun_family = AF_UNIX;
634 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
636 s = socket(AF_UNIX, SOCK_STREAM, 0);
638 syslog(LOG_WARNING, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
639 return (-WC_EXIT_BIND);
642 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
643 syslog(LOG_WARNING, "webcit: Can't bind: %s\n", strerror(errno));
645 return (-WC_EXIT_BIND);
648 if (listen(s, actual_queue_len) < 0) {
649 syslog(LOG_WARNING, "webcit: Can't listen: %s\n", strerror(errno));
651 return (-WC_EXIT_BIND);
654 chmod(sockpath, 0777);
662 * Read data from the client socket.
664 * sock socket fd to read from
665 * buf buffer to read into
666 * bytes number of bytes to read
667 * timeout Number of seconds to wait before timing out
669 * Possible return values:
670 * 1 Requested number of bytes has been read.
671 * 0 Request timed out.
672 * -1 Connection is broken, or other error.
674 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
684 baselen = StrLength(Target);
686 if (Hdr->Pos == NULL)
687 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
689 if (StrLength(Hdr->ReadBuf) > 0)
691 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
693 if (bytes < bufremain)
695 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
696 StrBufCutLeft(Hdr->ReadBuf, bufremain);
699 if (bytes > bufremain)
701 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
703 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
705 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
709 syslog(LOG_INFO, "client_read_ssl() failed\n");
718 retval = StrBufReadBLOBBuffered(Target,
727 syslog(LOG_INFO, "client_read() failed: %s\n",
729 wc_backtrace(LOG_DEBUG);
738 * Begin buffering HTTP output so we can transmit it all in one write operation later.
740 void begin_burst(void)
742 if (WC->WBuf == NULL) {
743 WC->WBuf = NewStrBufPlain(NULL, 32768);
749 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
754 const char *ptr, *eptr;
760 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
762 if (CompressBuffer(WCC->WBuf) > 0)
763 hprintf("Content-encoding: gzip\r\n");
765 syslog(LOG_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
766 wc_backtrace(LOG_INFO);
770 if (WCC->WFBuf != NULL) {
771 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
772 FreeStrBuf(&WCC->WFBuf);
775 if (WCC->Hdr->HR.prohibit_caching)
776 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
777 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
779 ptr = ChrPtr(WCC->HBuf);
780 count = StrLength(WCC->HBuf);
785 client_write_ssl(WCC->HBuf);
786 client_write_ssl(WCC->WBuf);
791 if (WCC->Hdr->http_sock == -1)
793 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
795 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
796 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
798 FD_SET(WCC->Hdr->http_sock, &wset);
799 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
800 syslog(LOG_DEBUG, "client_write: Socket select failed (%s)\n", strerror(errno));
805 if ((WCC->Hdr->http_sock == -1) ||
806 (res = write(WCC->Hdr->http_sock,
809 syslog(LOG_DEBUG, "client_write: Socket write failed (%s)\n", strerror(errno));
810 wc_backtrace(LOG_INFO);
817 ptr = ChrPtr(WCC->WBuf);
818 count = StrLength(WCC->WBuf);
821 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
822 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
824 FD_SET(WCC->Hdr->http_sock, &wset);
825 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
826 syslog(LOG_INFO, "client_write: Socket select failed (%s)\n", strerror(errno));
831 if ((WCC->Hdr->http_sock == -1) ||
832 (res = write(WCC->Hdr->http_sock,
835 syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
836 wc_backtrace(LOG_INFO);
843 return StrLength(WCC->WBuf);
848 * lingering_close() a`la Apache. see
849 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
851 int lingering_close(int fd)
856 struct timeval tv, start;
858 gettimeofday(&start, NULL);
864 gettimeofday(&tv, NULL);
865 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
866 tv.tv_usec = start.tv_usec - tv.tv_usec;
867 if (tv.tv_usec < 0) {
869 tv.tv_usec += 1000000;
873 i = select(fd + 1, &set, NULL, NULL, &tv);
874 } while (i == -1 && errno == EINTR);
879 i = read(fd, buf, sizeof buf);
880 } while (i != 0 && (i != -1 || errno == EINTR));
886 HttpNewModule_TCPSOCKETS
887 (ParsedHttpHdrs *httpreq)
890 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
894 HttpDetachModule_TCPSOCKETS
895 (ParsedHttpHdrs *httpreq)
898 FlushStrBuf(httpreq->ReadBuf);
899 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
903 HttpDestroyModule_TCPSOCKETS
904 (ParsedHttpHdrs *httpreq)
907 FreeStrBuf(&httpreq->ReadBuf);
912 SessionNewModule_TCPSOCKETS
915 sess->CLineBuf = NewStrBuf();
916 sess->MigrateReadLineBuf = NewStrBuf();
920 SessionDestroyModule_TCPSOCKETS
923 FreeStrBuf(&sess->CLineBuf);
924 FreeStrBuf(&sess->ReadBuf);
926 sess->ReadPos = NULL;
927 FreeStrBuf(&sess->MigrateReadLineBuf);
928 if (sess->serv_sock > 0) {
929 syslog(LOG_DEBUG, "Closing socket %d", sess->serv_sock);
930 close(sess->serv_sock);
932 sess->serv_sock = -1;