2 This file is part of GNUnet
3 Copyright (C) 2003-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file transport/plugin_transport_smtp.c
18 * @brief Implementation of the SMTP transport service
19 * @author Christian Grothoff
20 * @author Renaldo Ferreira
24 #include "gnunet_util.h"
25 #include "gnunet_constants.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_transport.h"
28 #include "gnunet_stats_service.h"
34 * The default maximum size of each outbound SMTP message.
36 #define SMTP_MESSAGE_SIZE 65528
38 #define DEBUG_SMTP GNUNET_EXTRA_LOGGING
40 #define FILTER_STRING_SIZE 64
42 /* how long can a line in base64 encoded
43 mime text be? (in characters, excluding "\n") */
44 #define MAX_CHAR_PER_LINE 76
49 * Host-Address in a SMTP network.
55 * Filter line that every sender must include in the E-mails such
56 * that the receiver can effectively filter out the GNUnet traffic
59 char filter[FILTER_STRING_SIZE];
62 * Claimed E-mail address of the sender.
63 * Format is "foo@bar.com" with null termination, padded to be
64 * of a multiple of 8 bytes long.
66 char senderAddress[0];
70 GNUNET_NETWORK_STRUCT_BEGIN
73 * Encapsulation of a GNUnet message in the SMTP mail body (before
78 GNUNET_MessageHeader header;
81 * What is the identity of the sender (GNUNET_hash of public key)
83 GNUNET_PeerIdentity sender;
86 GNUNET_NETWORK_STRUCT_END
88 /* *********** globals ************* */
91 * apis (our advertised API and the core api )
93 static GNUNET_CoreAPIForTransport *core_api;
95 static struct GNUNET_GE_Context *ectx;
98 * Thread that listens for inbound messages
100 static struct GNUNET_ThreadHandle *dispatchThread;
103 * Flag to indicate that server has been shut down.
105 static int smtp_shutdown = GNUNET_YES;
108 * Set to the SMTP server hostname (and port) for outgoing messages.
110 static char *smtp_server_name;
112 static char *pipename;
115 * Lock for uses of libesmtp (not thread-safe).
117 static struct GNUNET_Mutex *lock;
120 * Old handler for SIGPIPE (kept to be able to restore).
122 static struct sigaction old_handler;
126 static GNUNET_TransportAPI smtpAPI;
128 static GNUNET_Stats_ServiceAPI *stats;
130 static int stat_bytesReceived;
132 static int stat_bytesSent;
134 static int stat_bytesDropped;
137 * How many e-mails are we allowed to send per hour?
139 static unsigned long long rate_limit;
141 static GNUNET_CronTime last_transmission;
144 /* ********************* the real stuff ******************* */
146 #define strAUTOncmp(a,b) strncmp(a,b,strlen(b))
149 * Listen to the pipe, decode messages and send to core.
152 listenAndDistribute (void *unused)
155 unsigned int linesize;
161 GNUNET_TransportPacket *coreMP;
165 linesize = ((GNUNET_MAX_BUFFER_SIZE * 4 / 3) + 8) * (MAX_CHAR_PER_LINE + 2) / MAX_CHAR_PER_LINE; /* maximum size of a line supported */
166 line = GNUNET_malloc (linesize + 2); /* 2 bytes for off-by-one errors, just to be safe... */
168 #define READLINE(l,limit) \
169 do { retl = fgets(l, (limit), fdes); \
170 if ( (retl == NULL) || (smtp_shutdown == GNUNET_YES)) {\
173 if (core_api->load_monitor != NULL) \
174 GNUNET_network_monitor_notify_transmission(core_api->load_monitor, GNUNET_ND_DOWNLOAD, strlen(retl)); \
178 while (smtp_shutdown == GNUNET_NO)
180 fd = OPEN (pipename, O_RDONLY | O_ASYNC);
183 if (smtp_shutdown == GNUNET_NO)
184 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);
187 fdes = fdopen (fd, "r");
188 while (smtp_shutdown == GNUNET_NO)
190 /* skip until end of header */
193 READLINE (line, linesize);
195 while ((line[0] != '\r') && (line[0] != '\n')); /* expect newline */
196 READLINE (line, linesize); /* read base64 encoded message; decode, process */
200 pos = strlen (line) - 1; /* ignore new line */
201 READLINE (&line[pos], linesize - pos); /* read base64 encoded message; decode, process */
202 if ((line[pos] == '\r') || (line[pos] == '\n'))
203 break; /* empty line => end of message! */
205 size = GNUNET_STRINGS_base64_decode (line, pos, &out);
206 if (size < sizeof (SMTPMessage))
208 GNUNET_GE_BREAK (ectx, 0);
213 mp = (SMTPMessage *) &out[size - sizeof (SMTPMessage)];
214 if (ntohs (mp->header.size) != size)
217 GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
218 _("Received malformed message via %s. Ignored.\n"),
222 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
223 "Size returned by base64=%d, in the msg=%d.\n", size,
230 stats->change (stat_bytesReceived, size);
231 coreMP = GNUNET_new (GNUNET_TransportPacket);
233 coreMP->size = size - sizeof (SMTPMessage);
234 coreMP->tsession = NULL;
235 coreMP->sender = mp->sender;
237 GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
238 "SMTP message passed to the core.\n");
241 core_api->receive (coreMP);
245 GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
246 "SMTP message processed.\n");
255 /* *************** API implementation *************** */
258 * Verify that a hello-Message is correct (a node is reachable at that
259 * address). Since the reply will be asynchronous, a method must be
262 * @param hello the hello message to verify
263 * (the signature/crc have been verified before)
264 * @return GNUNET_OK on success, GNUNET_SYSERR on error
267 api_verify_hello (const GNUNET_MessageHello * hello)
269 const EmailAddress *maddr;
271 maddr = (const EmailAddress *) &hello[1];
272 if ((ntohs (hello->header.size) !=
273 sizeof (GNUNET_MessageHello) + ntohs (hello->senderAddressSize)) ||
274 (maddr->senderAddress
275 [ntohs (hello->senderAddressSize) - 1 - FILTER_STRING_SIZE] != '\0'))
277 GNUNET_GE_BREAK (ectx, 0);
278 return GNUNET_SYSERR; /* obviously invalid */
280 if (NULL == strstr (maddr->filter, ": "))
281 return GNUNET_SYSERR;
286 * Create a hello-Message for the current node. The hello is created
287 * without signature and without a timestamp. The GNUnet core will
288 * GNUNET_RSA_sign the message and add an expiration time.
290 * @return hello on success, NULL on error
292 static GNUNET_MessageHello *
295 GNUNET_MessageHello *msg;
300 GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "FILTER",
301 "X-mailer: GNUnet", &filter);
302 if (NULL == strstr (filter, ": "))
304 GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
305 _("SMTP filter string to invalid, lacks ': '\n"));
306 GNUNET_free (filter);
310 if (strlen (filter) > FILTER_STRING_SIZE)
312 filter[FILTER_STRING_SIZE] = '\0';
313 GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
314 _("SMTP filter string to long, capped to `%s'\n"), filter);
316 i = (strlen (email) + 8) & (~7); /* make multiple of 8 */
318 GNUNET_malloc (sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i);
319 memset (msg, 0, sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i);
320 haddr = (EmailAddress *) &msg[1];
321 memset (&haddr->filter[0], 0, FILTER_STRING_SIZE);
322 strcpy (&haddr->filter[0], filter);
323 GNUNET_memcpy (&haddr->senderAddress[0], email, strlen (email) + 1);
324 msg->senderAddressSize = htons (strlen (email) + 1 + sizeof (EmailAddress));
325 msg->protocol = htons (GNUNET_TRANSPORT_PROTOCOL_NUMBER_SMTP);
326 msg->MTU = htonl (smtpAPI.mtu);
327 msg->header.size = htons (GNUNET_sizeof_hello (msg));
328 if (api_verify_hello (msg) == GNUNET_SYSERR)
329 GNUNET_GE_ASSERT (ectx, 0);
330 GNUNET_free (filter);
334 struct GetMessageClosure
342 get_message (void **buf, int *len, void *cls)
344 struct GetMessageClosure *gmc = cls;
352 if (gmc->pos == gmc->esize)
353 return NULL; /* done */
355 gmc->pos = gmc->esize;
360 * Send a message to the specified remote node.
362 * @param tsession the GNUNET_MessageHello identifying the remote node
363 * @param msg what to send
364 * @param size the size of the message
365 * @param important is this message important enough to override typical limits?
366 * @return GNUNET_SYSERR on error, GNUNET_OK on success
369 api_send (GNUNET_TSession * tsession, const void *msg, const unsigned int size,
372 const GNUNET_MessageHello *hello;
373 const EmailAddress *haddr;
378 struct GetMessageClosure gm_cls;
379 smtp_session_t session;
380 smtp_message_t message;
381 smtp_recipient_t recipient;
387 if (smtp_shutdown == GNUNET_YES)
388 return GNUNET_SYSERR;
389 if ((size == 0) || (size > smtpAPI.mtu))
391 GNUNET_GE_BREAK (ectx, 0);
392 return GNUNET_SYSERR;
394 now = GNUNET_get_time ();
395 if ((important != GNUNET_YES) &&
396 ((now - last_transmission) * rate_limit) < GNUNET_CRON_HOURS)
397 return GNUNET_NO; /* rate too high */
398 last_transmission = now;
400 hello = (const GNUNET_MessageHello *) tsession->internal;
402 return GNUNET_SYSERR;
403 GNUNET_mutex_lock (lock);
404 session = smtp_create_session ();
408 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
409 GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"),
410 "smtp_create_session", smtp_strerror (smtp_errno (), ebuf,
412 GNUNET_mutex_unlock (lock);
413 return GNUNET_SYSERR;
415 if (0 == smtp_set_server (session, smtp_server_name))
418 GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
419 GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"),
420 "smtp_set_server", smtp_strerror (smtp_errno (), ebuf,
422 smtp_destroy_session (session);
423 GNUNET_mutex_unlock (lock);
424 return GNUNET_SYSERR;
426 haddr = (const EmailAddress *) &hello[1];
427 message = smtp_add_message (session);
431 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
432 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
433 "smtp_add_message", smtp_strerror (smtp_errno (), ebuf,
435 smtp_destroy_session (session);
436 GNUNET_mutex_unlock (lock);
437 return GNUNET_SYSERR;
439 smtp_set_header (message, "To", NULL, haddr->senderAddress);
440 smtp_set_header (message, "From", NULL, email);
442 filter = GNUNET_strdup (haddr->filter);
443 fvalue = strstr (filter, ": ");
444 GNUNET_GE_ASSERT (NULL, NULL != fvalue);
447 if (0 == smtp_set_header (message, filter, fvalue))
450 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
451 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
452 "smtp_set_header", smtp_strerror (smtp_errno (), ebuf,
454 smtp_destroy_session (session);
455 GNUNET_mutex_unlock (lock);
456 GNUNET_free (filter);
457 return GNUNET_SYSERR;
459 GNUNET_free (filter);
460 m = GNUNET_malloc (size + sizeof (SMTPMessage));
461 GNUNET_memcpy (m, msg, size);
462 mp = (SMTPMessage *) &m[size];
463 mp->header.size = htons (size + sizeof (SMTPMessage));
464 mp->header.type = htons (0);
465 mp->sender = *core_api->my_identity;
468 gm_cls.esize = GNUNET_STRINGS_base64_encode (m, size + sizeof (SMTPMessage), &gm_cls.ebody);
470 if (0 == smtp_size_set_estimate (message, gm_cls.esize))
473 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
474 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
475 "smtp_size_set_estimate", smtp_strerror (smtp_errno (), ebuf,
478 if (0 == smtp_set_messagecb (message, &get_message, &gm_cls))
481 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
482 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
483 "smtp_set_messagecb", smtp_strerror (smtp_errno (), ebuf,
485 smtp_destroy_session (session);
486 GNUNET_mutex_unlock (lock);
487 GNUNET_free (gm_cls.ebody);
488 return GNUNET_SYSERR;
490 recipient = smtp_add_recipient (message, haddr->senderAddress);
491 if (recipient == NULL)
494 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
495 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
496 "smtp_add_recipient", smtp_strerror (smtp_errno (), ebuf,
498 smtp_destroy_session (session);
499 GNUNET_mutex_unlock (lock);
500 return GNUNET_SYSERR;
502 if (0 == smtp_start_session (session))
505 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
506 GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
507 "smtp_start_session", smtp_strerror (smtp_errno (), ebuf,
509 smtp_destroy_session (session);
510 GNUNET_mutex_unlock (lock);
511 GNUNET_free (gm_cls.ebody);
512 return GNUNET_SYSERR;
515 stats->change (stat_bytesSent, size);
516 if (core_api->load_monitor != NULL)
517 GNUNET_network_monitor_notify_transmission (core_api->load_monitor,
518 GNUNET_ND_UPLOAD, gm_cls.esize);
519 smtp_message_reset_status (message); /* this is needed to plug a 28-byte/message memory leak in libesmtp */
520 smtp_destroy_session (session);
521 GNUNET_mutex_unlock (lock);
522 GNUNET_free (gm_cls.ebody);
527 * Establish a connection to a remote node.
528 * @param hello the hello-Message for the target node
529 * @param tsessionPtr the session handle that is to be set
530 * @param may_reuse can we re-use an existing connection?
531 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
534 api_connect (const GNUNET_MessageHello * hello, GNUNET_TSession ** tsessionPtr,
537 GNUNET_TSession *tsession;
539 tsession = GNUNET_new (GNUNET_TSession);
540 tsession->internal = GNUNET_malloc (GNUNET_sizeof_hello (hello));
541 tsession->peer = hello->senderIdentity;
542 GNUNET_memcpy (tsession->internal, hello, GNUNET_sizeof_hello (hello));
543 tsession->ttype = smtpAPI.protocol_number;
544 (*tsessionPtr) = tsession;
549 * Disconnect from a remote node.
551 * @param tsession the session that is closed
552 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
555 api_disconnect (GNUNET_TSession * tsession)
557 if (tsession != NULL)
559 if (tsession->internal != NULL)
560 GNUNET_free (tsession->internal);
561 GNUNET_free (tsession);
567 * Start the server process to receive inbound traffic.
568 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
571 api_start_transport_server ()
573 smtp_shutdown = GNUNET_NO;
574 /* initialize SMTP network */
575 dispatchThread = GNUNET_thread_create (&listenAndDistribute, NULL, 1024 * 4);
576 if (dispatchThread == NULL)
578 GNUNET_GE_DIE_STRERROR (ectx,
579 GNUNET_GE_ADMIN | GNUNET_GE_BULK | GNUNET_GE_FATAL,
581 return GNUNET_SYSERR;
587 * Shutdown the server process (stop receiving inbound traffic). Maybe
591 api_stop_transport_server ()
595 smtp_shutdown = GNUNET_YES;
596 GNUNET_thread_stop_sleep (dispatchThread);
597 GNUNET_thread_join (dispatchThread, &unused);
602 * Convert SMTP hello to an IP address (always fails).
605 api_hello_to_address (const GNUNET_MessageHello * hello, void **sa,
606 unsigned int *sa_len)
608 return GNUNET_SYSERR;
615 api_associate (GNUNET_TSession * tsession)
617 return GNUNET_SYSERR; /* SMTP connections can never be associated */
621 * Always succeeds (for now; we should look at adding
622 * frequency limits to SMTP in the future!).
625 api_test_would_try (GNUNET_TSession * tsession, unsigned int size,
628 return GNUNET_OK; /* we always try... */
632 * The exported method. Makes the core api available via a global and
633 * returns the smtp transport API.
635 GNUNET_TransportAPI *
636 inittransport_smtp (struct GNUNET_CoreAPIForTransport * core)
638 unsigned long long mtu;
643 if (!GNUNET_GC_have_configuration_value (core_api->cfg, "SMTP", "EMAIL"))
645 GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
647 ("No email-address specified, can not start SMTP transport.\n"));
650 GNUNET_GC_get_configuration_value_number (core_api->cfg, "SMTP", "MTU", 1200,
652 SMTP_MESSAGE_SIZE, &mtu);
653 GNUNET_GC_get_configuration_value_number (core_api->cfg, "SMTP", "RATELIMIT",
654 0, 0, 1024 * 1024, &rate_limit);
655 stats = core_api->service_request ("stats");
659 stats->create (gettext_noop ("# bytes received via SMTP"));
660 stat_bytesSent = stats->create (gettext_noop ("# bytes sent via SMTP"));
662 stats->create (gettext_noop ("# bytes dropped by SMTP (outgoing)"));
664 GNUNET_GC_get_configuration_value_filename (core_api->cfg, "SMTP", "PIPE", &pipename);
666 if (0 != mkfifo (pipename, S_IWUSR | S_IRUSR | S_IWGRP | S_IWOTH))
668 GNUNET_GE_LOG_STRERROR (ectx,
669 GNUNET_GE_ADMIN | GNUNET_GE_BULK | GNUNET_GE_FATAL,
671 GNUNET_free (pipename);
672 core_api->service_release (stats);
676 /* we need to allow the mailer program to send us messages;
677 * easiest done by giving it write permissions (see Mantis #1142) */
678 if (0 != chmod (pipename, S_IWUSR | S_IRUSR | S_IWGRP | S_IWOTH))
679 GNUNET_GE_LOG_STRERROR (ectx,
680 GNUNET_GE_ADMIN | GNUNET_GE_BULK |
681 GNUNET_GE_WARNING, "chmod");
682 GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "EMAIL", NULL,
684 lock = GNUNET_mutex_create (GNUNET_NO);
685 GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "SERVER",
686 "localhost:25", &smtp_server_name);
687 sa.sa_handler = SIG_IGN;
688 sigemptyset (&sa.sa_mask);
690 sigaction (SIGPIPE, &sa, &old_handler);
692 smtpAPI.protocol_number = GNUNET_TRANSPORT_PROTOCOL_NUMBER_SMTP;
693 smtpAPI.mtu = mtu - sizeof (SMTPMessage);
695 smtpAPI.hello_verify = &api_verify_hello;
696 smtpAPI.hello_create = &api_create_hello;
697 smtpAPI.connect = &api_connect;
698 smtpAPI.send = &api_send;
699 smtpAPI.associate = &api_associate;
700 smtpAPI.disconnect = &api_disconnect;
701 smtpAPI.server_start = &api_start_transport_server;
702 smtpAPI.server_stop = &api_stop_transport_server;
703 smtpAPI.hello_to_address = &api_hello_to_address;
704 smtpAPI.send_now_test = &api_test_would_try;
709 donetransport_smtp ()
711 sigaction (SIGPIPE, &old_handler, NULL);
712 GNUNET_free (smtp_server_name);
715 core_api->service_release (stats);
718 GNUNET_mutex_destroy (lock);
721 GNUNET_free (pipename);