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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "sysdb.h"
33 #include "core/object.h"
34 #include "core/plugin.h"
35 #include "frontend/connection-private.h"
36 #include "utils/error.h"
37 #include "utils/strbuf.h"
38 #include "utils/proto.h"
39 #include "utils/os.h"
41 #include <assert.h>
42 #include <errno.h>
44 #include <arpa/inet.h>
45 #include <fcntl.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
50 #include <stdlib.h>
51 #include <string.h>
53 #include <pthread.h>
54 #include <netdb.h>
56 /*
57 * private variables
58 */
60 static pthread_key_t conn_ctx_key;
61 static bool conn_ctx_key_initialized = 0;
63 /*
64 * private types
65 */
67 /* name of connection objects */
68 #define CONN_FD_PREFIX "conn#"
69 #define CONN_FD_PLACEHOLDER "XXXXXXX"
71 static ssize_t
72 conn_read(sdb_conn_t *conn, size_t len)
73 {
74 return sdb_strbuf_read(conn->buf, conn->fd, len);
75 } /* conn_read */
77 static ssize_t
78 conn_write(sdb_conn_t *conn, const void *buf, size_t len)
79 {
80 return sdb_write(conn->fd, len, buf);
81 } /* conn_write */
83 static int
84 connection_init(sdb_object_t *obj, va_list ap)
85 {
86 sdb_conn_t *conn;
87 int sock_fd;
88 int sock_fl;
90 assert(obj);
91 conn = CONN(obj);
93 sock_fd = va_arg(ap, int);
95 conn->buf = sdb_strbuf_create(/* size = */ 128);
96 if (! conn->buf) {
97 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
98 "for a new connection");
99 return -1;
100 }
101 conn->errbuf = sdb_strbuf_create(0);
102 if (! conn->errbuf) {
103 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
104 "for a new connection");
105 return -1;
106 }
108 conn->client_addr_len = sizeof(conn->client_addr);
109 conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
110 &conn->client_addr_len);
112 if (conn->fd < 0) {
113 char buf[1024];
114 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
115 "connection: %s", sdb_strerror(errno,
116 buf, sizeof(buf)));
117 return -1;
118 }
120 /* update the object name */
121 snprintf(obj->name + strlen(CONN_FD_PREFIX),
122 strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
124 /* defaults */
125 conn->read = conn_read;
126 conn->write = conn_write;
127 conn->finish = NULL;
128 conn->ssl_session = NULL;
130 sock_fl = fcntl(conn->fd, F_GETFL);
131 if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
132 char buf[1024];
133 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
134 "to non-blocking mode: %s", conn->fd,
135 sdb_strerror(errno, buf, sizeof(buf)));
136 return -1;
137 }
139 conn->username = NULL;
140 conn->ready = 0;
142 sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
143 conn->fd);
145 conn->cmd = SDB_CONNECTION_IDLE;
146 conn->cmd_len = 0;
147 conn->skip_len = 0;
148 return 0;
149 } /* connection_init */
151 static void
152 connection_destroy(sdb_object_t *obj)
153 {
154 sdb_conn_t *conn;
155 size_t len;
157 assert(obj);
158 conn = CONN(obj);
160 conn->ready = 0;
162 if (conn->finish)
163 conn->finish(conn);
164 conn->finish = NULL;
166 if (conn->buf) {
167 len = sdb_strbuf_len(conn->buf);
168 if (len)
169 sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
170 "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
171 }
173 if (conn->client_addr.ss_family == AF_UNIX) {
174 sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection %s from peer %s",
175 obj->name, conn->username);
176 }
177 else {
178 char host[1024] = "<unknown>", port[32] = "";
179 getnameinfo((struct sockaddr *)&conn->client_addr,
180 conn->client_addr_len, host, sizeof(host), port, sizeof(port),
181 NI_NUMERICHOST | NI_NUMERICSERV);
182 sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection %s from peer %s "
183 "at %s:%s", obj->name, conn->username, host, port);
184 }
185 sdb_connection_close(conn);
187 if (conn->username)
188 free(conn->username);
189 conn->username = NULL;
191 sdb_strbuf_destroy(conn->buf);
192 conn->buf = NULL;
193 sdb_strbuf_destroy(conn->errbuf);
194 conn->errbuf = NULL;
195 } /* connection_destroy */
197 static sdb_type_t connection_type = {
198 /* size = */ sizeof(sdb_conn_t),
199 /* init = */ connection_init,
200 /* destroy = */ connection_destroy,
201 };
203 /*
204 * private helper functions
205 */
207 static void
208 sdb_conn_ctx_destructor(void *c)
209 {
210 sdb_object_t *conn = c;
212 if (! conn)
213 return;
214 sdb_object_deref(conn);
215 } /* sdb_conn_ctx_destructor */
217 static void
218 sdb_conn_ctx_init(void)
219 {
220 if (conn_ctx_key_initialized)
221 return;
223 pthread_key_create(&conn_ctx_key, sdb_conn_ctx_destructor);
224 conn_ctx_key_initialized = 1;
225 } /* sdb_conn_ctx_init */
227 static void
228 sdb_conn_set_ctx(sdb_conn_t *conn)
229 {
230 sdb_conn_t *old;
232 sdb_conn_ctx_init();
234 old = pthread_getspecific(conn_ctx_key);
235 if (old)
236 sdb_object_deref(SDB_OBJ(old));
237 if (conn)
238 sdb_object_ref(SDB_OBJ(conn));
239 pthread_setspecific(conn_ctx_key, conn);
240 } /* sdb_conn_set_ctx */
242 static sdb_conn_t *
243 sdb_conn_get_ctx(void)
244 {
245 if (! conn_ctx_key_initialized)
246 return NULL;
247 return pthread_getspecific(conn_ctx_key);
248 } /* sdb_conn_get_ctx */
250 /*
251 * connection handler functions
252 */
254 /*
255 * connection_log:
256 * Send a log message originating from the current thread to the client.
257 */
258 static int
259 connection_log(int prio, const char *msg,
260 sdb_object_t __attribute__((unused)) *user_data)
261 {
262 uint32_t len = (uint32_t)sizeof(uint32_t) + (uint32_t)strlen(msg);
263 uint32_t p = htonl((uint32_t)prio);
264 char tmp[len + 1];
266 sdb_conn_t *conn;
268 conn = sdb_conn_get_ctx();
269 /* no connection associated to this thread
270 * or startup not done yet => don't leak any information */
271 if ((! conn) || (! conn->ready))
272 return 0;
274 /* XXX: make the log-level configurable by the client at runtime */
275 if (prio >= SDB_LOG_DEBUG)
276 return 0;
278 memcpy(tmp, &p, sizeof(p));
279 strcpy(tmp + sizeof(p), msg);
281 if (sdb_connection_send(conn, SDB_CONNECTION_LOG, len, tmp) < 0)
282 return -1;
283 return 0;
284 } /* connection_log */
286 static int
287 command_handle(sdb_conn_t *conn)
288 {
289 int status = -1;
291 assert(conn && (conn->cmd != SDB_CONNECTION_IDLE));
292 assert(! conn->skip_len);
294 if (conn->cmd == SDB_CONNECTION_PING)
295 status = sdb_connection_ping(conn);
296 else if (conn->cmd == SDB_CONNECTION_STARTUP)
297 status = sdb_fe_session_start(conn);
299 else if (conn->cmd == SDB_CONNECTION_QUERY)
300 status = sdb_fe_query(conn);
301 else if (conn->cmd == SDB_CONNECTION_FETCH)
302 status = sdb_fe_fetch(conn);
303 else if (conn->cmd == SDB_CONNECTION_LIST)
304 status = sdb_fe_list(conn);
305 else if (conn->cmd == SDB_CONNECTION_LOOKUP)
306 status = sdb_fe_lookup(conn);
307 else if (conn->cmd == SDB_CONNECTION_STORE)
308 status = sdb_fe_store(conn);
310 else if (conn->cmd == SDB_CONNECTION_SERVER_VERSION)
311 status = sdb_connection_server_version(conn);
313 else {
314 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
315 conn->cmd);
316 sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
317 status = -1;
318 }
320 if (status) {
321 if (! sdb_strbuf_len(conn->errbuf))
322 sdb_strbuf_sprintf(conn->errbuf, "Failed to execute command");
323 sdb_connection_send(conn, SDB_CONNECTION_ERROR,
324 (uint32_t)sdb_strbuf_len(conn->errbuf),
325 sdb_strbuf_string(conn->errbuf));
326 }
327 return status;
328 } /* command_handle */
330 /* initialize the connection state information */
331 static int
332 command_init(sdb_conn_t *conn)
333 {
334 const char *errmsg = NULL;
336 assert(conn && (conn->cmd == SDB_CONNECTION_IDLE) && (! conn->cmd_len));
338 if (conn->skip_len)
339 return -1;
341 /* reset */
342 sdb_strbuf_clear(conn->errbuf);
344 if (sdb_proto_unmarshal_header(SDB_STRBUF_STR(conn->buf),
345 &conn->cmd, &conn->cmd_len) < 0)
346 return -1;
347 sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t));
349 if ((! conn->ready) && (conn->cmd != SDB_CONNECTION_STARTUP))
350 errmsg = "Authentication required";
351 else if (conn->cmd == SDB_CONNECTION_IDLE)
352 errmsg = "Invalid command 0";
354 if (errmsg) {
355 size_t len = sdb_strbuf_len(conn->buf);
357 sdb_strbuf_sprintf(conn->errbuf, "%s", errmsg);
358 sdb_connection_send(conn, SDB_CONNECTION_ERROR,
359 (uint32_t)strlen(errmsg), errmsg);
360 conn->skip_len += conn->cmd_len;
361 conn->cmd = SDB_CONNECTION_IDLE;
362 conn->cmd_len = 0;
364 if (len > conn->skip_len)
365 len = conn->skip_len;
366 sdb_strbuf_skip(conn->buf, 0, len);
367 conn->skip_len -= len;
368 /* connection_read will handle anything else */
369 }
370 return 0;
371 } /* command_init */
373 /* returns negative value on error, 0 on EOF, number of octets else */
374 static ssize_t
375 connection_read(sdb_conn_t *conn)
376 {
377 ssize_t n = 0;
379 if ((! conn) || (conn->fd < 0))
380 return -1;
382 while (42) {
383 ssize_t status;
385 errno = 0;
386 status = conn->read(conn, 1024);
387 if (status < 0) {
388 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
389 break;
391 sdb_connection_close(conn);
392 return (int)status;
393 }
394 else if (! status) /* EOF */
395 break;
397 if (conn->skip_len) {
398 size_t len = (size_t)status < conn->skip_len
399 ? (size_t)status : conn->skip_len;
400 sdb_strbuf_skip(conn->buf, 0, len);
401 conn->skip_len -= len;
402 }
404 n += status;
406 /* give the main loop a chance to execute commands (and free up buffer
407 * space) on large amounts of incoming traffic */
408 if (n > 1024 * 1024)
409 break;
410 }
412 return n;
413 } /* connection_read */
415 /*
416 * public API
417 */
419 int
420 sdb_connection_enable_logging(void)
421 {
422 return sdb_plugin_register_log("connection-logger", connection_log,
423 /* user_data = */ NULL);
424 } /* sdb_connection_enable_logging */
426 sdb_conn_t *
427 sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data)
428 {
429 sdb_conn_t *conn;
430 const char *peer = "unknown";
432 if (fd < 0)
433 return NULL;
435 /* the placeholder will be replaced with the accepted file
436 * descriptor when initializing the object */
437 conn = CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
438 connection_type, fd));
439 if (setup && (setup(conn, user_data) < 0)) {
440 sdb_object_deref(SDB_OBJ(conn));
441 return NULL;
442 }
444 if (conn->username)
445 peer = conn->username;
447 if (conn->client_addr.ss_family == AF_UNIX) {
448 sdb_log(SDB_LOG_INFO,
449 "frontend: Accepted connection from peer %s", peer);
450 }
451 else {
452 char host[1024] = "<unknown>", port[32] = "";
453 getnameinfo((struct sockaddr *)&conn->client_addr,
454 conn->client_addr_len, host, sizeof(host), port, sizeof(port),
455 NI_NUMERICHOST | NI_NUMERICSERV);
456 sdb_log(SDB_LOG_INFO, "frontend: Accepted connection from "
457 "peer %s at %s:%s", peer, host, port);
458 }
459 return conn;
460 } /* sdb_connection_create */
462 void
463 sdb_connection_close(sdb_conn_t *conn)
464 {
465 if (! conn)
466 return;
468 if (conn->finish)
469 conn->finish(conn);
470 conn->finish = NULL;
472 /* close the connection even if someone else still references it */
473 if (conn->fd >= 0)
474 close(conn->fd);
475 conn->fd = -1;
476 } /* sdb_connection_close */
478 ssize_t
479 sdb_connection_handle(sdb_conn_t *conn)
480 {
481 ssize_t n = 0;
483 sdb_conn_set_ctx(conn);
485 while (42) {
486 ssize_t status = connection_read(conn);
488 if ((conn->cmd == SDB_CONNECTION_IDLE) && (! conn->cmd_len)
489 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
490 command_init(conn);
491 if ((conn->cmd != SDB_CONNECTION_IDLE)
492 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len)) {
493 command_handle(conn);
495 /* remove the command from the buffer */
496 if (conn->cmd_len)
497 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
498 conn->cmd = SDB_CONNECTION_IDLE;
499 conn->cmd_len = 0;
500 }
502 if (status <= 0)
503 break;
505 n += status;
506 }
508 sdb_conn_set_ctx(NULL);
509 return n;
510 } /* sdb_connection_handle */
512 ssize_t
513 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
514 uint32_t msg_len, const char *msg)
515 {
516 char buf[2 * sizeof(uint32_t) + msg_len];
517 ssize_t status;
519 if ((! conn) || (conn->fd < 0))
520 return -1;
521 if (sdb_proto_marshal(buf, sizeof(buf), code, msg_len, msg) < 0)
522 return -1;
524 status = conn->write(conn, buf, sizeof(buf));
525 if (status < 0) {
526 char errbuf[1024];
528 /* tell other code that there was a problem and, more importantly,
529 * make sure we don't try to send further logs to the connection */
530 sdb_connection_close(conn);
531 conn->ready = 0;
533 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
534 "(code: %u, len: %u) to client: %s", code, msg_len,
535 sdb_strerror(errno, errbuf, sizeof(errbuf)));
536 }
537 return status;
538 } /* sdb_connection_send */
540 int
541 sdb_connection_ping(sdb_conn_t *conn)
542 {
543 if ((! conn) || (conn->cmd != SDB_CONNECTION_PING))
544 return -1;
546 /* we're alive */
547 sdb_connection_send(conn, SDB_CONNECTION_OK, 0, NULL);
548 return 0;
549 } /* sdb_connection_ping */
551 int
552 sdb_connection_server_version(sdb_conn_t *conn)
553 {
554 char msg[sizeof(uint32_t) + strlen(SDB_VERSION_EXTRA) + 1];
556 if ((! conn) || (conn->cmd != SDB_CONNECTION_SERVER_VERSION))
557 return -1;
559 sdb_proto_marshal_int32(msg, sizeof(msg), (uint32_t)SDB_VERSION);
560 strncpy(msg + sizeof(uint32_t), SDB_VERSION_EXTRA,
561 sizeof(msg) - sizeof(uint32_t));
562 sdb_connection_send(conn, SDB_CONNECTION_OK, (uint32_t)sizeof(msg), msg);
563 return 0;
564 } /* sdb_connection_server_version */
566 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */