]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/ctdlclient.c
This is a better version of detect_logged_in() for webcit-ng that
[citadel.git] / webcit-ng / ctdlclient.c
index 2467b1237c5f3c7bc9b510d801579fb3603794a4..3ef786707be723c70f0a917c71c97ca43f2206a5 100644 (file)
@@ -1,16 +1,17 @@
-/*
- * Functions that handle communication with a Citadel Server
- *
- * Copyright (c) 1987-2018 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+//
+// Functions that handle communication with a Citadel Server
+//
+// Copyright (c) 1987-2022 by the citadel.org team
+//
+// This program is open source software.  It runs great on the
+// Linux operating system (and probably elsewhere).  You can use,
+// copy, and run it under the terms of the GNU General Public
+// License version 3.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 #include "webcit.h"
 
@@ -18,12 +19,9 @@ struct ctdlsession *cpool = NULL;                            // linked list of connections to the Citade
 pthread_mutex_t cpool_mutex = PTHREAD_MUTEX_INITIALIZER;       // Lock it before modifying
 
 
-/*
- * Read a specific number of bytes of binary data from the Citadel server.
- * Returns the number of bytes read or -1 for error.
- */
-int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested)
-{
+// Read a specific number of bytes of binary data from the Citadel server.
+// Returns the number of bytes read or -1 for error.
+int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested) {
        int bytes_read = 0;
        int c = 0;
 
@@ -39,12 +37,9 @@ int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested)
 }
 
 
-/*
- * Read a newline-terminated line of text from the Citadel server.
- * Returns the string length or -1 for error.
- */
-int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes)
-{
+// Read a newline-terminated line of text from the Citadel server.
+// Returns the string length or -1 for error.
+int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes) {
        int len = 0;
        int c = 0;
 
@@ -62,23 +57,20 @@ int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes)
                                --len;
                        }
                        buf[len] = 0;
-                       // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf);
+                       syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf);
                        return (len);
                }
                ++len;
        }
-       // syslog(LOG_DEBUG, "\033[33m[ %s\033[0m", buf);
+       syslog(LOG_DEBUG, "\033[32m[ %s\033[0m", buf);
        return (len);
 }
 
 
-/*
- * Read lines of text from the Citadel server until a 000 terminator is received.
- * 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)
-{
+// Read lines of text from the Citadel server until a 000 terminator is received.
+// 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) {
        char buf[1024];
        StrBuf *sj = NewStrBuf();
        if (!sj) {
@@ -93,21 +85,15 @@ 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)
-{
+// 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) {
        return write(ctdl->sock, buf, count);
 }
 
 
-/*
- * printf() type function to send data to the Citadel Server.
- */
-void ctdl_printf(struct ctdlsession *ctdl, const char *format, ...)
-{
+// printf() type function to send data to the Citadel Server.
+void ctdl_printf(struct ctdlsession *ctdl, const char *format, ...) {
        va_list arg_ptr;
        StrBuf *Buf = NewStrBuf();
 
@@ -122,11 +108,8 @@ void ctdl_printf(struct ctdlsession *ctdl, const char *format, ...)
 }
 
 
-/*
- * Client side - connect to a unix domain socket
- */
-int uds_connectsock(char *sockpath)
-{
+// Client side - connect to a unix domain socket
+int uds_connectsock(char *sockpath) {
        struct sockaddr_un addr;
        int s;
 
@@ -149,107 +132,9 @@ int uds_connectsock(char *sockpath)
 }
 
 
-/*
- * TCP client - connect to a host/port 
- */
-int tcp_connectsock(char *host, char *service)
-{
-       struct in6_addr serveraddr;
-       struct addrinfo hints;
-       struct addrinfo *res = NULL;
-       struct addrinfo *ai = NULL;
-       int rc = (-1);
-       int s = (-1);
-
-       if ((host == NULL) || IsEmptyStr(host))
-               return (-1);
-       if ((service == NULL) || IsEmptyStr(service))
-               return (-1);
-
-       syslog(LOG_DEBUG, "tcp_connectsock(%s,%s)", host, service);
-
-       memset(&hints, 0x00, sizeof(hints));
-       hints.ai_flags = AI_NUMERICSERV;
-       hints.ai_family = AF_UNSPEC;
-       hints.ai_socktype = SOCK_STREAM;
-
-       /*
-        * Handle numeric IPv4 and IPv6 addresses
-        */
-       rc = inet_pton(AF_INET, host, &serveraddr);
-       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 */
-                       hints.ai_family = AF_INET6;
-                       hints.ai_flags |= AI_NUMERICHOST;
-               }
-       }
-
-       /* Begin the connection process */
-
-       rc = getaddrinfo(host, service, &hints, &res);
-       if (rc != 0) {
-               syslog(LOG_DEBUG, "%s: %s", host, gai_strerror(rc));
-               freeaddrinfo(res);
-               return (-1);
-       }
-
-       /*
-        * Try all available addresses until we connect to one or until we run out.
-        */
-       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.");
-
-               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);
-               }
-               rc = connect(s, ai->ai_addr, ai->ai_addrlen);
-               if (rc >= 0) {
-                       int fdflags;
-                       freeaddrinfo(res);
-
-                       fdflags = fcntl(rc, F_GETFL);
-                       if (fdflags < 0) {
-                               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));
-                               close(s);
-                               return -1;
-                       }
-
-                       return (s);
-               } else {
-                       syslog(LOG_WARNING, "connect() failed: %s", strerror(errno));
-                       close(s);
-               }
-       }
-       freeaddrinfo(res);
-       return (-1);
-}
-
-
-/*
- * Extract from the headers, the username and password the client is attempting to use.
- * This could be HTTP AUTH or it could be in the cookies.
- */
-void extract_auth(struct http_transaction *h, char *authbuf, int authbuflen)
-{
+// Extract from the headers, the username and password the client is attempting to use.
+// This could be HTTP AUTH or it could be in the cookies.
+void extract_auth(struct http_transaction *h, char *authbuf, int authbuflen) {
        if (authbuf == NULL)
                return;
        authbuf[0] = 0;
@@ -281,16 +166,13 @@ void extract_auth(struct http_transaction *h, char *authbuf, int authbuflen)
 }
 
 
-/*
- * Log in to the Citadel server.  Returns 0 on success or nonzero on error.
- *
- * 'auth' should be a base64-encoded "username:password" combination (like in http-auth)
- *
- * If 'resultbuf' is not NULL, it should be a buffer of at least 1024 characters,
- * and will be filled with the result from a Citadel server command.
- */
-int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf)
-{
+// Log in to the Citadel server.  Returns 0 on success or nonzero on error.
+//
+// 'auth' should be a base64-encoded "username:password" combination (like in http-auth)
+//
+// If 'resultbuf' is not NULL, it should be a buffer of at least 1024 characters,
+// and will be filled with the result from a Citadel server command.
+int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf) {
        char localbuf[1024];
        char *buf;
        int buflen;
@@ -299,7 +181,8 @@ int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf)
 
        if (resultbuf != NULL) {
                buf = resultbuf;
-       } else {
+       }
+       else {
                buf = localbuf;
        }
 
@@ -330,11 +213,8 @@ int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf)
 }
 
 
-/*
- * Hunt for, or create, a connection to our Citadel Server
- */
-struct ctdlsession *connect_to_citadel(struct http_transaction *h)
-{
+// Hunt for, or create, a connection to our Citadel Server
+struct ctdlsession *connect_to_citadel(struct http_transaction *h) {
        struct ctdlsession *cptr = NULL;
        struct ctdlsession *my_session = NULL;
        int is_new_session = 0;
@@ -373,7 +253,8 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h)
 
        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) {
@@ -381,7 +262,8 @@ 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");
@@ -394,7 +276,12 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h)
 
        if (is_new_session) {
                strcpy(my_session->room, "");
-               my_session->sock = tcp_connectsock(ctdlhost, ctdlport);
+               static char *ctdl_sock_path = NULL;
+               if (!ctdl_sock_path) {
+                       ctdl_sock_path = malloc(PATH_MAX);
+                       snprintf(ctdl_sock_path, PATH_MAX, "%s/citadel.socket", ctdl_dir);
+               }
+               my_session->sock = uds_connectsock(ctdl_sock_path);
                ctdl_readline(my_session, buf, sizeof(buf));            // skip past the server greeting banner
 
                if (!IsEmptyStr(auth)) {                                // do we need to log in to Citadel?
@@ -410,11 +297,8 @@ struct ctdlsession *connect_to_citadel(struct http_transaction *h)
 }
 
 
-/*
- * Release our Citadel Server connection back into the pool.
- */
-void disconnect_from_citadel(struct ctdlsession *ctdl)
-{
+// Release our Citadel Server connection back into the pool.
+void disconnect_from_citadel(struct ctdlsession *ctdl) {
        pthread_mutex_lock(&cpool_mutex);
        ctdl->is_bound = 0;
        pthread_mutex_unlock(&cpool_mutex);