-
-/*
- * This is a generic function to set up a master socket for listening on
- * a TCP port. The server shuts down if the bind fails.
- *
- * ip_addr IP address to bind
- * port_number port number to bind
- * queue_len number of incoming connections to allow in the queue
- */
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
-{
- struct sockaddr_in sin;
- int s, i;
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- if (ip_addr == NULL) {
- sin.sin_addr.s_addr = INADDR_ANY;
- } else {
- sin.sin_addr.s_addr = inet_addr(ip_addr);
- }
-
- if (sin.sin_addr.s_addr == INADDR_NONE) {
- sin.sin_addr.s_addr = INADDR_ANY;
- }
-
- if (port_number == 0) {
- lprintf(1, "Cannot start: no port number specified.\n");
- exit(WC_EXIT_BIND);
- }
- sin.sin_port = htons((u_short) port_number);
-
- s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
- if (s < 0) {
- lprintf(1, "Can't create a socket: %s\n", strerror(errno));
- exit(WC_EXIT_BIND);
- }
- /* Set some socket options that make sense. */
- i = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
-
- fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
- there should be a preceding F_GETFL
- and a bitwise OR with the previous
- fd flags */
-
- if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- lprintf(1, "Can't bind: %s\n", strerror(errno));
- exit(WC_EXIT_BIND);
- }
- if (listen(s, queue_len) < 0) {
- lprintf(1, "Can't listen: %s\n", strerror(errno));
- exit(WC_EXIT_BIND);
- }
- return (s);
-}
-
-
-
-/*
- * Create a Unix domain socket and listen on it
- * sockpath - file name of the unix domain socket
- * queue_len - Number of incoming connections to allow in the queue
- */
-int ig_uds_server(char *sockpath, int queue_len)
-{
- struct sockaddr_un addr;
- int s;
- int i;
- int actual_queue_len;
-
- actual_queue_len = queue_len;
- if (actual_queue_len < 5) actual_queue_len = 5;
-
- i = unlink(sockpath);
- if (i != 0) if (errno != ENOENT) {
- lprintf(1, "webcit: can't unlink %s: %s\n",
- sockpath, strerror(errno));
- exit(WC_EXIT_BIND);
- }
-
- memset(&addr, 0, sizeof(addr));
- addr.sun_family = AF_UNIX;
- safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
-
- s = socket(AF_UNIX, SOCK_STREAM, 0);
- if (s < 0) {
- lprintf(1, "webcit: Can't create a socket: %s\n",
- strerror(errno));
- exit(WC_EXIT_BIND);
- }
-
- if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- lprintf(1, "webcit: Can't bind: %s\n",
- strerror(errno));
- exit(WC_EXIT_BIND);
- }
-
- if (listen(s, actual_queue_len) < 0) {
- lprintf(1, "webcit: Can't listen: %s\n",
- strerror(errno));
- exit(WC_EXIT_BIND);
- }
-
- chmod(sockpath, 0777);
- return(s);
-}
-
-
-
-
-/*
- * Read data from the client socket.
- *
- * sock socket fd to read from
- * buf buffer to read into
- * bytes number of bytes to read
- * timeout Number of seconds to wait before timing out
- *
- * Possible return values:
- * 1 Requested number of bytes has been read.
- * 0 Request timed out.
- * -1 Connection is broken, or other error.
- */
-int client_read_to(int sock, char *buf, int bytes, int timeout)
-{
- int len, rlen;
- fd_set rfds;
- struct timeval tv;
- int retval;
-
-
-#ifdef HAVE_OPENSSL
- if (is_https) {
- return (client_read_ssl(buf, bytes, timeout));
- }
-#endif
-
- len = 0;
- while (len < bytes) {
- FD_ZERO(&rfds);
- FD_SET(sock, &rfds);
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
-
- retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
- if (FD_ISSET(sock, &rfds) == 0) {
- return (0);
- }
-
- rlen = read(sock, &buf[len], bytes - len);
-
- if (rlen < 1) {
- lprintf(2, "client_read() failed: %s\n",
- strerror(errno));
- return (-1);
- }
- len = len + rlen;
- }
-
-#ifdef HTTP_TRACING
- write(2, "\033[32m", 5);
- write(2, buf, bytes);
- write(2, "\033[30m", 5);
-#endif
- return (1);
-}
-
-/*
- * write data to the client
- */
-ssize_t client_write(const void *buf, size_t count)
-{
- char *newptr;
- size_t newalloc;
- size_t bytesWritten = 0;
- ssize_t res;
- fd_set wset;
- int fdflags;
-
- if (WC->burst != NULL) {
- if ((WC->burst_len + count) >= WC->burst_alloc) {
- newalloc = (WC->burst_alloc * 2);
- if ((WC->burst_len + count) >= newalloc) {
- newalloc += count;
- }
- newptr = realloc(WC->burst, newalloc);
- if (newptr != NULL) {
- WC->burst = newptr;
- WC->burst_alloc = newalloc;
- }
- }
- if ((WC->burst_len + count) < WC->burst_alloc) {
- memcpy(&WC->burst[WC->burst_len], buf, count);
- WC->burst_len += count;
- return (count);
- }
- else {
- return(-1);
- }
- }
-#ifdef HAVE_OPENSSL
- if (is_https) {
- client_write_ssl((char *) buf, count);
- return (count);
- }
-#endif
-#ifdef HTTP_TRACING
- write(2, "\033[34m", 5);
- write(2, buf, count);
- write(2, "\033[30m", 5);
-#endif
- fdflags = fcntl(WC->http_sock, F_GETFL);
-
- while (bytesWritten < count) {
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- FD_ZERO(&wset);
- FD_SET(WC->http_sock, &wset);
- if (select(WC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
- lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
- return -1;
- }
- }
-
- if ((res = write(WC->http_sock, (char*)buf + bytesWritten,
- count - bytesWritten)) == -1) {
- lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
- return res;
- }
- bytesWritten += res;
- }
-
- return bytesWritten;
-}
-
-/*
- * Begin buffering HTTP output so we can transmit it all in one write operation later.
- *
- * We do this in userspace instead of using TCP_CORK for two reasons:
- * 1. We need to calculate the Content-length: header
- * 2. We may want to compress the data before sending it
- *
- */
-void begin_burst(void)
-{
- if (WC->burst != NULL) {
- free(WC->burst);
- WC->burst = NULL;
- }
- WC->burst_len = 0;
- WC->burst_alloc = 32768;
- WC->burst = malloc(WC->burst_alloc);
-}
-
-
-/*
- * uses the same calling syntax as compress2(), but it
- * creates a stream compatible with HTTP "Content-encoding: gzip"
- */
-#ifdef HAVE_ZLIB
-#define DEF_MEM_LEVEL 8
-#define OS_CODE 0x03 /* unix */
-int ZEXPORT compress_gzip(Bytef * dest, /* compressed buffer */
- size_t * destLen, /* length of the compresed data */
- const Bytef * source, /* source to encode */
- uLong sourceLen, /* length of source to encode */
- int level) /* compression level */
-{
- const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
-
- /* write gzip header */
- snprintf((char *) dest, *destLen,
- "%c%c%c%c%c%c%c%c%c%c",
- gz_magic[0], gz_magic[1], Z_DEFLATED,
- 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /* xflags */ ,
- OS_CODE);
-
- /* normal deflate */
- z_stream stream;
- int err;
- stream.next_in = (Bytef *) source;
- stream.avail_in = (uInt) sourceLen;
- stream.next_out = dest + 10L; // after header
- stream.avail_out = (uInt) * destLen;
- if ((uLong) stream.avail_out != *destLen)
- return Z_BUF_ERROR;
-
- stream.zalloc = (alloc_func) 0;
- stream.zfree = (free_func) 0;
- stream.opaque = (voidpf) 0;
-
- err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
- DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
- if (err != Z_OK)
- return err;
-
- err = deflate(&stream, Z_FINISH);
- if (err != Z_STREAM_END) {
- deflateEnd(&stream);
- return err == Z_OK ? Z_BUF_ERROR : err;
- }
- *destLen = stream.total_out + 10L;
-
- /* write CRC and Length */
- uLong crc = crc32(0L, source, sourceLen);
- int n;
- for (n = 0; n < 4; ++n, ++*destLen) {
- dest[*destLen] = (int) (crc & 0xff);
- crc >>= 8;
- }
- uLong len = stream.total_in;
- for (n = 0; n < 4; ++n, ++*destLen) {
- dest[*destLen] = (int) (len & 0xff);
- len >>= 8;
- }
- err = deflateEnd(&stream);
- return err;
-}
-#endif
-
-/*
- * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
- */
-void end_burst(void)
-{
- size_t the_len;
- char *the_data;
-
- if (WC->burst == NULL)
- return;
-
- the_len = WC->burst_len;
- the_data = WC->burst;
-
- WC->burst_len = 0;
- WC->burst_alloc = 0;
- WC->burst = NULL;
-
-#ifdef HAVE_ZLIB
- /* Perform gzip compression, if enabled and supported by client */
- if (WC->gzip_ok) {
- char *compressed_data = NULL;
- size_t compressed_len;
-
- compressed_len = ((the_len * 101) / 100) + 100;
- compressed_data = malloc(compressed_len);
-
- if (compress_gzip((Bytef *) compressed_data,
- &compressed_len,
- (Bytef *) the_data,
- (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
- wprintf("Content-encoding: gzip\r\n");
- free(the_data);
- the_data = compressed_data;
- the_len = compressed_len;
- } else {
- free(compressed_data);
- }
- }
-#endif /* HAVE_ZLIB */
-
- wprintf("Content-length: %d\r\n\r\n", the_len);
- client_write(the_data, the_len);
- free(the_data);
- return;
-}
-
-
-
-/*
- * Read data from the client socket with default timeout.
- * (This is implemented in terms of client_read_to() and could be
- * justifiably moved out of sysdep.c)
- */
-int client_read(int sock, char *buf, int bytes)
-{
- return (client_read_to(sock, buf, bytes, SLEEPING));
-}
-
-
-/*
- * Get a LF-terminated line of text from the client.
- * (This is implemented in terms of client_read() and could be
- * justifiably moved out of sysdep.c)
- */
-int client_getln(int sock, char *buf, int bufsiz)
-{
- int i, retval;
-
- /* Read one character at a time.*/
- for (i = 0;; i++) {
- retval = client_read(sock, &buf[i], 1);
- if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
- break;
- if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
- /* Non printable character recieved from client */
- return(-1);
- }
- }
-
- /* If we got a long line, discard characters until the newline. */
- if (i == (bufsiz-1))
- while (buf[i] != '\n' && retval == 1)
- retval = client_read(sock, &buf[i], 1);
-
- /*
- * Strip any trailing non-printable characters.
- */
- buf[i] = 0;
- while ((i > 0) && (!isprint(buf[i - 1]))) {
- buf[--i] = 0;
- }
- return (retval);
-}
-
-/*
- * Shut us down the regular way.
- * signum is the signal we want to forward
- */
-pid_t current_child;
-void graceful_shutdown_watcher(int signum) {
- lprintf (1, "bye; shutting down watcher.");
- kill(current_child, signum);
- if (signum != SIGHUP)
- exit(0);
-}
-
-/*
- * shut us down the regular way.
- * signum is the signal we want to forward
- */
-pid_t current_child;
-void graceful_shutdown(int signum) {
-// kill(current_child, signum);
- char wd[SIZ];
- FILE *FD;
- int fd;
- getcwd(wd, SIZ);
- lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
- fd = msock;
- msock = -1;
- time_to_die = 1;
- FD=fdopen(fd, "a+");
- fflush (FD);
- fclose (FD);
- close(fd);
-}
-
-
-/*
- * Start running as a daemon.
- */
-void start_daemon(char *pid_file)
-{
- int status = 0;
- pid_t child = 0;
- FILE *fp;
- int do_restart = 0;
-
- current_child = 0;
-
- /* Close stdin/stdout/stderr and replace them with /dev/null.
- * We don't just call close() because we don't want these fd's
- * to be reused for other files.
- */
- chdir("/");
-
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
-
- child = fork();
- if (child != 0) {
- exit(0);
- }
-
- setsid();
- umask(0);
- freopen("/dev/null", "r", stdin);
- freopen("/dev/null", "w", stdout);
- freopen("/dev/null", "w", stderr);
- signal(SIGTERM, graceful_shutdown_watcher);
- signal(SIGHUP, graceful_shutdown_watcher);
-
- do {
- current_child = fork();
-
-
- if (current_child < 0) {
- perror("fork");
- ShutDownLibCitadel ();
- exit(errno);
- }
-
- else if (current_child == 0) { // child process
-// signal(SIGTERM, graceful_shutdown);
- signal(SIGHUP, graceful_shutdown);
-
- return; /* continue starting webcit. */
- }
-
- else { // watcher process
-// signal(SIGTERM, SIG_IGN);
-// signal(SIGHUP, SIG_IGN);
- if (pid_file) {
- fp = fopen(pid_file, "w");
- if (fp != NULL) {
- fprintf(fp, "%d\n", getpid());
- fclose(fp);
- }
- }
- waitpid(current_child, &status, 0);
- }
-
- do_restart = 0;
-
- /* Did the main process exit with an actual exit code? */
- if (WIFEXITED(status)) {
-
- /* Exit code 0 means the watcher should exit */
- if (WEXITSTATUS(status) == 0) {
- do_restart = 0;
- }
-
- /* Exit code 101-109 means the watcher should exit */
- else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
- do_restart = 0;
- }
-
- /* Any other exit code means we should restart. */
- else {
- do_restart = 1;
- }
- }
-
- /* Any other type of termination (signals, etc.) should also restart. */
- else {
- do_restart = 1;
- }
-
- } while (do_restart);
-
- if (pid_file) {
- unlink(pid_file);
- }
- ShutDownLibCitadel ();
- exit(WEXITSTATUS(status));
-}
-
-/*
- * Spawn an additional worker thread into the pool.
- */
-void spawn_another_worker_thread()
-{
- pthread_t SessThread; /* Thread descriptor */
- pthread_attr_t attr; /* Thread attributes */
- int ret;
-
- lprintf(3, "Creating a new thread\n");
-
- /* set attributes for the new thread */
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
- /*
- * Our per-thread stacks need to be bigger than the default size, otherwise
- * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
- * 64-bit Linux.
- */
- if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
- lprintf(1, "pthread_attr_setstacksize: %s\n",
- strerror(ret));
- pthread_attr_destroy(&attr);
- }
-
- /* now create the thread */
- if (pthread_create(&SessThread, &attr,
- (void *(*)(void *)) worker_entry, NULL)
- != 0) {
- lprintf(1, "Can't create thread: %s\n", strerror(errno));
- }
-
- /* free up the attributes */
- pthread_attr_destroy(&attr);
-}
-