Code

frontend: Make connection I/O handling more flexible.
[sysdb.git] / src / frontend / connection.c
index 90361fe9a65b57a4ebaf8c855d3ca5d430b98f30..f35f7b39c82a8278f47137e65810163094c33044 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
 #include "sysdb.h"
-#include "core/error.h"
-#include "frontend/connection.h"
+#include "core/object.h"
+#include "core/plugin.h"
+#include "frontend/connection-private.h"
+#include "utils/error.h"
 #include "utils/strbuf.h"
+#include "utils/proto.h"
+#include "utils/os.h"
 
 #include <assert.h>
 #include <errno.h>
 
 #include <arpa/inet.h>
+#include <fcntl.h>
 
+#include <stdlib.h>
 #include <string.h>
 
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/param.h>
+
+#ifdef HAVE_UCRED_H
+#      include <ucred.h>
+#endif
+#ifdef HAVE_SYS_UCRED_H
+#      include <sys/ucred.h>
+#endif
+
+#include <pwd.h>
+
+#include <pthread.h>
+
+/*
+ * private variables
+ */
+
+static pthread_key_t conn_ctx_key;
+static bool          conn_ctx_key_initialized = 0;
+
+/*
+ * private types
+ */
+
+/* name of connection objects */
+#define CONN_FD_PREFIX "conn#"
+#define CONN_FD_PLACEHOLDER "XXXXXXX"
+
+/* XXX: only supports UNIX sockets so far */
+static char *
+peer(int sockfd)
+{
+       uid_t uid;
+
+       struct passwd pw_entry;
+       struct passwd *result = NULL;
+       char buf[1024];
+
+#ifdef SO_PEERCRED
+       struct ucred cred;
+       socklen_t len = sizeof(cred);
+
+       if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cred, &len)
+                       || (len != sizeof(cred)))
+               return NULL;
+       uid = cred.uid;
+#else /* SO_PEERCRED */
+       errno = ENOSYS;
+       return NULL;
+#endif
+
+       memset(&pw_entry, 0, sizeof(pw_entry));
+       if (getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result)
+                       || (! result))
+               return NULL;
+       return strdup(result->pw_name);
+} /* peer */
+
+static ssize_t
+conn_read(sdb_conn_t *conn, size_t len)
+{
+       return sdb_strbuf_read(conn->buf, conn->fd, len);
+} /* conn_read */
+
+static ssize_t
+conn_write(sdb_conn_t *conn, const void *buf, size_t len)
+{
+       return sdb_write(conn->fd, len, buf);
+} /* conn_write */
+
+static int
+connection_init(sdb_object_t *obj, va_list ap)
+{
+       sdb_conn_t *conn;
+       int sock_fd;
+       int sock_fl;
+
+       assert(obj);
+       conn = CONN(obj);
+
+       sock_fd = va_arg(ap, int);
+
+       conn->buf = sdb_strbuf_create(/* size = */ 128);
+       if (! conn->buf) {
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
+                               "for a new connection");
+               return -1;
+       }
+       conn->errbuf = sdb_strbuf_create(0);
+       if (! conn->errbuf) {
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
+                               "for a new connection");
+               return -1;
+       }
+
+       conn->client_addr_len = sizeof(conn->client_addr);
+       conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
+                       &conn->client_addr_len);
+
+       if (conn->fd < 0) {
+               char buf[1024];
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
+                               "connection: %s", sdb_strerror(errno,
+                                       buf, sizeof(buf)));
+               return -1;
+       }
+
+       /* defaults */
+       conn->read = conn_read;
+       conn->write = conn_write;
+       conn->finish = NULL;
+       conn->session = NULL;
+
+       if (conn->client_addr.ss_family != AF_UNIX) {
+               sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
+                               "unexpected family type %d", conn->client_addr.ss_family);
+               return -1;
+       }
+
+       sock_fl = fcntl(conn->fd, F_GETFL);
+       if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
+               char buf[1024];
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
+                               "to non-blocking mode: %s", conn->fd,
+                               sdb_strerror(errno, buf, sizeof(buf)));
+               return -1;
+       }
+
+       conn->username = peer(conn->fd);
+       if (! conn->username) {
+               char buf[1024];
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to retrieve peer for "
+                               "connection conn#%i: %s", conn->fd,
+                               sdb_strerror(errno, buf, sizeof(buf)));
+               return -1;
+       }
+       conn->ready = 0;
+
+       sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
+                       conn->fd);
+
+       conn->cmd = SDB_CONNECTION_IDLE;
+       conn->cmd_len = 0;
+       conn->skip_len = 0;
+
+       /* update the object name */
+       snprintf(obj->name + strlen(CONN_FD_PREFIX),
+                       strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
+       return 0;
+} /* connection_init */
+
+static void
+connection_destroy(sdb_object_t *obj)
+{
+       sdb_conn_t *conn;
+       size_t len;
+
+       assert(obj);
+       conn = CONN(obj);
+
+       conn->ready = 0;
+
+       if (conn->finish)
+               conn->finish(conn);
+       conn->finish = NULL;
+
+       if (conn->buf) {
+               len = sdb_strbuf_len(conn->buf);
+               if (len)
+                       sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
+                                       "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
+       }
+
+       sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection %s", obj->name);
+       sdb_connection_close(conn);
+
+       if (conn->username)
+               free(conn->username);
+       conn->username = NULL;
+
+       sdb_strbuf_destroy(conn->buf);
+       conn->buf = NULL;
+       sdb_strbuf_destroy(conn->errbuf);
+       conn->errbuf = NULL;
+} /* connection_destroy */
+
+static sdb_type_t connection_type = {
+       /* size = */ sizeof(sdb_conn_t),
+       /* init = */ connection_init,
+       /* destroy = */ connection_destroy,
+};
+
+/*
+ * private helper functions
+ */
+
+static void
+sdb_conn_ctx_destructor(void *c)
+{
+       sdb_object_t *conn = c;
+
+       if (! conn)
+               return;
+       sdb_object_deref(conn);
+} /* sdb_conn_ctx_destructor */
+
+static void
+sdb_conn_ctx_init(void)
+{
+       if (conn_ctx_key_initialized)
+               return;
+
+       pthread_key_create(&conn_ctx_key, sdb_conn_ctx_destructor);
+       conn_ctx_key_initialized = 1;
+} /* sdb_conn_ctx_init */
+
+static void
+sdb_conn_set_ctx(sdb_conn_t *conn)
+{
+       sdb_conn_t *old;
+
+       sdb_conn_ctx_init();
+
+       old = pthread_getspecific(conn_ctx_key);
+       if (old)
+               sdb_object_deref(SDB_OBJ(old));
+       if (conn)
+               sdb_object_ref(SDB_OBJ(conn));
+       pthread_setspecific(conn_ctx_key, conn);
+} /* sdb_conn_set_ctx */
+
+static sdb_conn_t *
+sdb_conn_get_ctx(void)
+{
+       if (! conn_ctx_key_initialized)
+               return NULL;
+       return pthread_getspecific(conn_ctx_key);
+} /* sdb_conn_get_ctx */
+
 /*
  * connection handler functions
  */
 
-static uint32_t
-connection_get_int32(sdb_conn_t *conn, size_t offset)
+/*
+ * connection_log:
+ * Send a log message originating from the current thread to the client.
+ */
+static int
+connection_log(int prio, const char *msg,
+               sdb_object_t __attribute__((unused)) *user_data)
 {
-       const char *data;
-       uint32_t n;
+       uint32_t len = (uint32_t)sizeof(uint32_t) + (uint32_t)strlen(msg);
+       uint32_t p = htonl((uint32_t)prio);
+       char tmp[len + 1];
 
-       assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
+       sdb_conn_t *conn;
 
-       data = sdb_strbuf_string(conn->buf);
-       memcpy(&n, data + offset, sizeof(n));
-       n = ntohl(n);
-       return n;
-} /* connection_get_int32 */
+       conn = sdb_conn_get_ctx();
+       /* no connection associated to this thread
+        * or startup not done yet => don't leak any information */
+       if ((! conn) || (! conn->ready))
+               return 0;
+
+       /* XXX: make the log-level configurable by the client at runtime */
+       if (prio >= SDB_LOG_DEBUG)
+               return 0;
+
+       memcpy(tmp, &p, sizeof(p));
+       strcpy(tmp + sizeof(p), msg);
+
+       if (sdb_connection_send(conn, SDB_CONNECTION_LOG, len, tmp) < 0)
+               return -1;
+       return 0;
+} /* connection_log */
 
 static int
 command_handle(sdb_conn_t *conn)
 {
        int status = -1;
 
-       assert(conn && (conn->cmd != CONNECTION_IDLE));
+       assert(conn && (conn->cmd != SDB_CONNECTION_IDLE));
+       assert(! conn->skip_len);
 
        sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
                        conn->cmd, conn->cmd_len);
 
-       switch (conn->cmd) {
-               case CONNECTION_PING:
-                       status = sdb_connection_ping(conn);
-                       break;
-               case CONNECTION_STARTUP:
-                       status = sdb_session_start(conn);
-                       break;
-               default:
-               {
-                       char errbuf[1024];
-                       sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command");
-                       snprintf(errbuf, sizeof(errbuf), "Invalid command %#x", conn->cmd);
-                       sdb_connection_send(conn, CONNECTION_ERROR,
-                                       (uint32_t)(strlen(errbuf) + 1), errbuf);
-                       status = -1;
-                       break;
-               }
+       if (conn->cmd == SDB_CONNECTION_PING)
+               status = sdb_connection_ping(conn);
+       else if (conn->cmd == SDB_CONNECTION_STARTUP)
+               status = sdb_fe_session_start(conn);
+       else if (conn->cmd == SDB_CONNECTION_QUERY)
+               status = sdb_fe_query(conn);
+       else if (conn->cmd == SDB_CONNECTION_FETCH)
+               status = sdb_fe_fetch(conn);
+       else if (conn->cmd == SDB_CONNECTION_LIST)
+               status = sdb_fe_list(conn);
+       else if (conn->cmd == SDB_CONNECTION_LOOKUP)
+               status = sdb_fe_lookup(conn);
+       else if (conn->cmd == SDB_CONNECTION_STORE)
+               status = sdb_fe_store(conn);
+       else {
+               sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
+                               conn->cmd);
+               sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
+               status = -1;
        }
 
-       /* remove the command from the buffer */
-       if (conn->cmd_len)
-               sdb_strbuf_skip(conn->buf, conn->cmd_len);
-       conn->cmd = CONNECTION_IDLE;
-       conn->cmd_len = 0;
+       if (status) {
+               if (! sdb_strbuf_len(conn->errbuf))
+                       sdb_strbuf_sprintf(conn->errbuf, "Failed to execute command");
+               sdb_connection_send(conn, SDB_CONNECTION_ERROR,
+                               (uint32_t)sdb_strbuf_len(conn->errbuf),
+                               sdb_strbuf_string(conn->errbuf));
+       }
        return status;
 } /* command_handle */
 
@@ -96,17 +370,42 @@ command_handle(sdb_conn_t *conn)
 static int
 command_init(sdb_conn_t *conn)
 {
-       size_t len;
+       const char *errmsg = NULL;
 
-       assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
+       assert(conn && (conn->cmd == SDB_CONNECTION_IDLE) && (! conn->cmd_len));
 
-       conn->cmd = connection_get_int32(conn, 0);
-       conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
+       if (conn->skip_len)
+               return -1;
+
+       /* reset */
+       sdb_strbuf_clear(conn->errbuf);
 
-       len = 2 * sizeof(uint32_t);
-       if (conn->cmd == CONNECTION_IDLE)
-               len += conn->cmd_len;
-       sdb_strbuf_skip(conn->buf, len);
+       if (sdb_proto_unmarshal_header(SDB_STRBUF_STR(conn->buf),
+                               &conn->cmd, &conn->cmd_len) < 0)
+               return -1;
+       sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t));
+
+       if ((! conn->ready) && (conn->cmd != SDB_CONNECTION_STARTUP))
+               errmsg = "Authentication required";
+       else if (conn->cmd == SDB_CONNECTION_IDLE)
+               errmsg = "Invalid command 0";
+
+       if (errmsg) {
+               size_t len = sdb_strbuf_len(conn->buf);
+
+               sdb_strbuf_sprintf(conn->errbuf, "%s", errmsg);
+               sdb_connection_send(conn, SDB_CONNECTION_ERROR,
+                               (uint32_t)strlen(errmsg), errmsg);
+               conn->skip_len += conn->cmd_len;
+               conn->cmd = SDB_CONNECTION_IDLE;
+               conn->cmd_len = 0;
+
+               if (len > conn->skip_len)
+                       len = conn->skip_len;
+               sdb_strbuf_skip(conn->buf, 0, len);
+               conn->skip_len -= len;
+               /* connection_read will handle anything else */
+       }
        return 0;
 } /* command_init */
 
@@ -116,20 +415,37 @@ connection_read(sdb_conn_t *conn)
 {
        ssize_t n = 0;
 
+       if ((! conn) || (conn->fd < 0))
+               return -1;
+
        while (42) {
                ssize_t status;
 
                errno = 0;
-               status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
+               status = conn->read(conn, 1024);
                if (status < 0) {
                        if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
                                break;
+
+                       sdb_connection_close(conn);
                        return (int)status;
                }
                else if (! status) /* EOF */
                        break;
 
+               if (conn->skip_len) {
+                       size_t len = (size_t)status < conn->skip_len
+                               ? (size_t)status : conn->skip_len;
+                       sdb_strbuf_skip(conn->buf, 0, len);
+                       conn->skip_len -= len;
+               }
+
                n += status;
+
+               /* give the main loop a chance to execute commands (and free up buffer
+                * space) on large amounts of incoming traffic */
+               if (n > 1024 * 1024)
+                       break;
        }
 
        return n;
@@ -140,130 +456,110 @@ connection_read(sdb_conn_t *conn)
  */
 
 int
-sdb_connection_init(sdb_conn_t *conn)
+sdb_connection_enable_logging(void)
 {
-       if (conn->buf) {
-               sdb_log(SDB_LOG_WARNING, "frontend: Attempted to re-initialize "
-                               "a frontend connection");
-               return -1;
-       }
+       return sdb_plugin_register_log("connection-logger", connection_log,
+                       /* user_data = */ NULL);
+} /* sdb_connection_enable_logging */
 
-       conn->buf = sdb_strbuf_create(/* size = */ 128);
-       if (! conn->buf) {
-               sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
-                               "for a new connection");
-               sdb_connection_close(conn);
-               return -1;
-       }
+sdb_conn_t *
+sdb_connection_accept(int fd)
+{
+       if (fd < 0)
+               return NULL;
 
-       conn->cmd = CONNECTION_IDLE;
-       conn->cmd_len = 0;
-       conn->fd = -1;
-       return 0;
-} /* sdb_connection_init */
+       /* the placeholder will be replaced with the accepted file
+        * descriptor when initializing the object */
+       return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
+                               connection_type, fd));
+} /* sdb_connection_create */
 
 void
 sdb_connection_close(sdb_conn_t *conn)
 {
-       size_t len;
+       if (! conn)
+               return;
 
-       if (conn->buf) {
-               len = sdb_strbuf_len(conn->buf);
-               if (len)
-                       sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
-                                       "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
-       }
+       if (conn->finish)
+               conn->finish(conn);
+       conn->finish = NULL;
 
-       sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i",
-                       conn->fd);
-       close(conn->fd);
+       /* close the connection even if someone else still references it */
+       if (conn->fd >= 0)
+               close(conn->fd);
        conn->fd = -1;
-
-       sdb_strbuf_destroy(conn->buf);
-       conn->buf = NULL;
-} /* sdb_connection_fini */
+} /* sdb_connection_close */
 
 ssize_t
-sdb_connection_read(sdb_conn_t *conn)
+sdb_connection_handle(sdb_conn_t *conn)
 {
        ssize_t n = 0;
 
+       sdb_conn_set_ctx(conn);
+
        while (42) {
                ssize_t status = connection_read(conn);
 
-               if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
+               if ((conn->cmd == SDB_CONNECTION_IDLE) && (! conn->cmd_len)
                                && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
                        command_init(conn);
-               if ((conn->cmd != CONNECTION_IDLE)
-                               && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
+               if ((conn->cmd != SDB_CONNECTION_IDLE)
+                               && (sdb_strbuf_len(conn->buf) >= conn->cmd_len)) {
                        command_handle(conn);
 
+                       /* remove the command from the buffer */
+                       if (conn->cmd_len)
+                               sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
+                       conn->cmd = SDB_CONNECTION_IDLE;
+                       conn->cmd_len = 0;
+               }
+
                if (status <= 0)
                        break;
 
                n += status;
        }
+
+       sdb_conn_set_ctx(NULL);
        return n;
-} /* sdb_connection_read */
+} /* sdb_connection_handle */
 
 ssize_t
 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
-               uint32_t msg_len, char *msg)
+               uint32_t msg_len, const char *msg)
 {
-       size_t len = 2 * sizeof(uint32_t) + msg_len;
-       char buffer[len];
-       char *buf;
-
-       uint32_t tmp;
+       char buf[2 * sizeof(uint32_t) + msg_len];
+       ssize_t status;
 
        if ((! conn) || (conn->fd < 0))
                return -1;
+       if (sdb_proto_marshal(buf, sizeof(buf), code, msg_len, msg) < 0)
+               return -1;
 
-       tmp = htonl(code);
-       memcpy(buffer, &tmp, sizeof(uint32_t));
-
-       tmp = htonl(msg_len);
-       memcpy(buffer + sizeof(uint32_t), &tmp, sizeof(uint32_t));
-
-       if (msg_len)
-               memcpy(buffer + 2 * sizeof(uint32_t), msg, msg_len);
-
-       buf = buffer;
-       while (len > 0) {
-               ssize_t status;
-
-               /* XXX: use select() */
-
-               errno = 0;
-               status = write(conn->fd, buf, len);
-               if (status < 0) {
-                       char errbuf[1024];
+       status = conn->write(conn, buf, sizeof(buf));
+       if (status < 0) {
+               char errbuf[1024];
 
-                       if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
-                               continue;
-                       if (errno == EINTR)
-                               continue;
-
-                       sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
-                                       "(code: %u, len: %u) to client: %s", code, msg_len,
-                                       sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       return status;
-               }
+               /* tell other code that there was a problem and, more importantly,
+                * make sure we don't try to send further logs to the connection */
+               sdb_connection_close(conn);
+               conn->ready = 0;
 
-               len -= (size_t)status;
-               buf += status;
+               sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
+                               "(code: %u, len: %u) to client: %s", code, msg_len,
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
        }
-       return (ssize_t)len;
+       return status;
 } /* sdb_connection_send */
 
 int
 sdb_connection_ping(sdb_conn_t *conn)
 {
-       if ((! conn) || (conn->cmd != CONNECTION_PING))
+       if ((! conn) || (conn->cmd != SDB_CONNECTION_PING))
                return -1;
 
        /* we're alive */
-       sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
+       sdb_connection_send(conn, SDB_CONNECTION_OK, 0, NULL);
        return 0;
 } /* sdb_connection_ping */