Code

frontend/connection: Require authentication before allowing any commands.
[sysdb.git] / src / frontend / connection.c
1 /*
2  * SysDB - src/frontend/connection.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
29 #include "core/object.h"
30 #include "frontend/connection-private.h"
31 #include "utils/error.h"
32 #include "utils/strbuf.h"
33 #include "utils/proto.h"
35 #include <assert.h>
36 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <fcntl.h>
41 #include <string.h>
43 /*
44  * private data types
45  */
47 /* name of connection objects */
48 #define CONN_FD_PREFIX "conn#"
49 #define CONN_FD_PLACEHOLDER "XXXXXXX"
51 static int
52 connection_init(sdb_object_t *obj, va_list ap)
53 {
54         sdb_conn_t *conn;
55         int sock_fd;
56         int sock_fl;
58         assert(obj);
59         conn = CONN(obj);
61         sock_fd = va_arg(ap, int);
63         conn->buf = sdb_strbuf_create(/* size = */ 128);
64         if (! conn->buf) {
65                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
66                                 "for a new connection");
67                 return -1;
68         }
69         conn->errbuf = sdb_strbuf_create(0);
70         if (! conn->errbuf) {
71                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
72                                 "for a new connection");
73                 return -1;
74         }
76         conn->client_addr_len = sizeof(conn->client_addr);
77         conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
78                         &conn->client_addr_len);
80         if (conn->fd < 0) {
81                 char buf[1024];
82                 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
83                                 "connection: %s", sdb_strerror(errno,
84                                         buf, sizeof(buf)));
85                 return -1;
86         }
88         if (conn->client_addr.ss_family != AF_UNIX) {
89                 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
90                                 "unexpected family type %d", conn->client_addr.ss_family);
91                 return -1;
92         }
94         sock_fl = fcntl(conn->fd, F_GETFL);
95         if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
96                 char buf[1024];
97                 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
98                                 "to non-blocking mode: %s", conn->fd,
99                                 sdb_strerror(errno, buf, sizeof(buf)));
100                 return -1;
101         }
103         sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
104                         conn->fd);
106         conn->cmd = CONNECTION_IDLE;
107         conn->cmd_len = 0;
109         /* update the object name */
110         snprintf(obj->name + strlen(CONN_FD_PREFIX),
111                         strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
112         return 0;
113 } /* connection_init */
115 static void
116 connection_destroy(sdb_object_t *obj)
118         sdb_conn_t *conn;
119         size_t len;
121         assert(obj);
122         conn = CONN(obj);
124         if (conn->buf) {
125                 len = sdb_strbuf_len(conn->buf);
126                 if (len)
127                         sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
128                                         "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
129         }
131         sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i",
132                         conn->fd);
133         close(conn->fd);
134         conn->fd = -1;
136         sdb_strbuf_destroy(conn->buf);
137         conn->buf = NULL;
138         sdb_strbuf_destroy(conn->errbuf);
139         conn->errbuf = NULL;
140 } /* connection_destroy */
142 static sdb_type_t connection_type = {
143         /* size = */ sizeof(sdb_conn_t),
144         /* init = */ connection_init,
145         /* destroy = */ connection_destroy,
146 };
148 /*
149  * connection handler functions
150  */
152 static uint32_t
153 connection_get_int32(sdb_conn_t *conn, size_t offset)
155         const char *data;
156         uint32_t n;
158         assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
160         data = sdb_strbuf_string(conn->buf);
161         memcpy(&n, data + offset, sizeof(n));
162         n = ntohl(n);
163         return n;
164 } /* connection_get_int32 */
166 static int
167 command_handle(sdb_conn_t *conn)
169         int status = -1;
171         assert(conn && (conn->cmd != CONNECTION_IDLE));
173         sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
174                         conn->cmd, conn->cmd_len);
176         if ((! conn->username) && (conn->cmd != CONNECTION_STARTUP)) {
177                 const char *errmsg = "Authentication required";
178                 sdb_connection_send(conn, CONNECTION_ERROR,
179                                 (uint32_t)strlen(errmsg), errmsg);
180                 return -1;
181         }
183         /* reset */
184         sdb_strbuf_sprintf(conn->errbuf, "");
186         switch (conn->cmd) {
187                 case CONNECTION_PING:
188                         status = sdb_connection_ping(conn);
189                         break;
190                 case CONNECTION_STARTUP:
191                         status = sdb_fe_session_start(conn);
192                         break;
194                 case CONNECTION_QUERY:
195                 {
196                         sdb_llist_t *parsetree;
197                         sdb_conn_node_t *node = NULL;
199                         parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
200                                         (int)conn->cmd_len);
201                         if (! parsetree) {
202                                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
203                                                 sdb_strbuf_string(conn->buf));
204                                 status = -1;
205                                 break;
206                         }
208                         switch (sdb_llist_len(parsetree)) {
209                                 case 0:
210                                         /* skipping empty command */
211                                         break;
212                                 case 1:
213                                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
214                                         break;
216                                 default:
217                                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s "
218                                                         "in multi-statement query '%s'",
219                                                         sdb_llist_len(parsetree) - 1,
220                                                         sdb_llist_len(parsetree) == 2 ? "" : "s",
221                                                         sdb_strbuf_string(conn->buf));
222                                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
223                         }
225                         if (node)
226                                 status = sdb_fe_exec(conn, node);
228                         sdb_llist_destroy(parsetree);
229                         break;
230                 }
232                 case CONNECTION_LIST:
233                         status = sdb_fe_list(conn);
234                         break;
236                 default:
237                 {
238                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
239                                         conn->cmd);
240                         sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
241                         status = -1;
242                         break;
243                 }
244         }
246         if (status)
247                 sdb_connection_send(conn, CONNECTION_ERROR,
248                                 (uint32_t)sdb_strbuf_len(conn->errbuf),
249                                 sdb_strbuf_string(conn->errbuf));
251         /* remove the command from the buffer */
252         if (conn->cmd_len)
253                 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
254         conn->cmd = CONNECTION_IDLE;
255         conn->cmd_len = 0;
256         return status;
257 } /* command_handle */
259 /* initialize the connection state information */
260 static int
261 command_init(sdb_conn_t *conn)
263         size_t len;
265         assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
267         conn->cmd = connection_get_int32(conn, 0);
268         conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
270         len = 2 * sizeof(uint32_t);
271         if (conn->cmd == CONNECTION_IDLE)
272                 len += conn->cmd_len;
273         sdb_strbuf_skip(conn->buf, 0, len);
274         return 0;
275 } /* command_init */
277 /* returns negative value on error, 0 on EOF, number of octets else */
278 static ssize_t
279 connection_read(sdb_conn_t *conn)
281         ssize_t n = 0;
283         while (42) {
284                 ssize_t status;
286                 errno = 0;
287                 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
288                 if (status < 0) {
289                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
290                                 break;
291                         return (int)status;
292                 }
293                 else if (! status) /* EOF */
294                         break;
296                 n += status;
297         }
299         return n;
300 } /* connection_read */
302 /*
303  * public API
304  */
306 sdb_conn_t *
307 sdb_connection_accept(int fd)
309         if (fd < 0)
310                 return NULL;
312         /* the placeholder will be replaced with the accepted file
313          * descriptor when initializing the object */
314         return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
315                                 connection_type, fd));
316 } /* sdb_connection_create */
318 void
319 sdb_connection_close(sdb_conn_t *conn)
321         sdb_object_deref(SDB_OBJ(conn));
322 } /* sdb_connection_close */
324 ssize_t
325 sdb_connection_read(sdb_conn_t *conn)
327         ssize_t n = 0;
329         while (42) {
330                 ssize_t status = connection_read(conn);
332                 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
333                                 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
334                         command_init(conn);
335                 if ((conn->cmd != CONNECTION_IDLE)
336                                 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
337                         command_handle(conn);
339                 if (status <= 0)
340                         break;
342                 n += status;
343         }
344         return n;
345 } /* sdb_connection_read */
347 ssize_t
348 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
349                 uint32_t msg_len, const char *msg)
351         ssize_t status;
353         if ((! conn) || (conn->fd < 0))
354                 return -1;
356         status = sdb_proto_send_msg(conn->fd, code, msg_len, msg);
357         if (status < 0) {
358                 char errbuf[1024];
360                 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
361                                 "(code: %u, len: %u) to client: %s", code, msg_len,
362                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
363         }
364         return status;
365 } /* sdb_connection_send */
367 int
368 sdb_connection_ping(sdb_conn_t *conn)
370         if ((! conn) || (conn->cmd != CONNECTION_PING))
371                 return -1;
373         /* we're alive */
374         sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
375         return 0;
376 } /* sdb_connection_ping */
378 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */