plugin_transport_smtp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2003-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/plugin_transport_smtp.c
  18. * @brief Implementation of the SMTP transport service
  19. * @author Christian Grothoff
  20. * @author Renaldo Ferreira
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_protocols.h"
  26. #include "gnunet_transport.h"
  27. #include "gnunet_stats_service.h"
  28. #include <libesmtp.h>
  29. #include <signal.h>
  30. /**
  31. * The default maximum size of each outbound SMTP message.
  32. */
  33. #define SMTP_MESSAGE_SIZE 65528
  34. #define DEBUG_SMTP GNUNET_EXTRA_LOGGING
  35. #define FILTER_STRING_SIZE 64
  36. /* how long can a line in base64 encoded
  37. mime text be? (in characters, excluding "\n") */
  38. #define MAX_CHAR_PER_LINE 76
  39. #define EBUF_LEN 128
  40. /**
  41. * Host-Address in a SMTP network.
  42. */
  43. typedef struct
  44. {
  45. /**
  46. * Filter line that every sender must include in the E-mails such
  47. * that the receiver can effectively filter out the GNUnet traffic
  48. * from the E-mail.
  49. */
  50. char filter[FILTER_STRING_SIZE];
  51. /**
  52. * Claimed E-mail address of the sender.
  53. * Format is "foo@bar.com" with null termination, padded to be
  54. * of a multiple of 8 bytes long.
  55. */
  56. char senderAddress[0];
  57. } EmailAddress;
  58. GNUNET_NETWORK_STRUCT_BEGIN
  59. /**
  60. * Encapsulation of a GNUnet message in the SMTP mail body (before
  61. * base64 encoding).
  62. */
  63. typedef struct
  64. {
  65. GNUNET_MessageHeader header;
  66. /**
  67. * What is the identity of the sender (GNUNET_hash of public key)
  68. */
  69. GNUNET_PeerIdentity sender;
  70. } SMTPMessage;
  71. GNUNET_NETWORK_STRUCT_END
  72. /* *********** globals ************* */
  73. /**
  74. * apis (our advertised API and the core api )
  75. */
  76. static GNUNET_CoreAPIForTransport *core_api;
  77. static struct GNUNET_GE_Context *ectx;
  78. /**
  79. * Thread that listens for inbound messages
  80. */
  81. static struct GNUNET_ThreadHandle *dispatchThread;
  82. /**
  83. * Flag to indicate that server has been shut down.
  84. */
  85. static int smtp_shutdown = GNUNET_YES;
  86. /**
  87. * Set to the SMTP server hostname (and port) for outgoing messages.
  88. */
  89. static char *smtp_server_name;
  90. static char *pipename;
  91. /**
  92. * Lock for uses of libesmtp (not thread-safe).
  93. */
  94. static struct GNUNET_Mutex *lock;
  95. /**
  96. * Old handler for SIGPIPE (kept to be able to restore).
  97. */
  98. static struct sigaction old_handler;
  99. static char *email;
  100. static GNUNET_TransportAPI smtpAPI;
  101. static GNUNET_Stats_ServiceAPI *stats;
  102. static int stat_bytesReceived;
  103. static int stat_bytesSent;
  104. static int stat_bytesDropped;
  105. /**
  106. * How many e-mails are we allowed to send per hour?
  107. */
  108. static unsigned long long rate_limit;
  109. static GNUNET_CronTime last_transmission;
  110. /* ********************* the real stuff ******************* */
  111. #define strAUTOncmp(a,b) strncmp(a,b,strlen(b))
  112. /**
  113. * Listen to the pipe, decode messages and send to core.
  114. */
  115. static void *
  116. listenAndDistribute (void *unused)
  117. {
  118. char *line;
  119. unsigned int linesize;
  120. SMTPMessage *mp;
  121. FILE *fdes;
  122. char *retl;
  123. char *out;
  124. unsigned int size;
  125. GNUNET_TransportPacket *coreMP;
  126. int fd;
  127. unsigned int pos;
  128. linesize = ((GNUNET_MAX_BUFFER_SIZE * 4 / 3) + 8) * (MAX_CHAR_PER_LINE + 2) / MAX_CHAR_PER_LINE; /* maximum size of a line supported */
  129. line = GNUNET_malloc (linesize + 2); /* 2 bytes for off-by-one errors, just to be safe... */
  130. #define READLINE(l,limit) \
  131. do { retl = fgets(l, (limit), fdes); \
  132. if ( (retl == NULL) || (smtp_shutdown == GNUNET_YES)) {\
  133. goto END; \
  134. }\
  135. if (core_api->load_monitor != NULL) \
  136. GNUNET_network_monitor_notify_transmission(core_api->load_monitor, GNUNET_ND_DOWNLOAD, strlen(retl)); \
  137. } while (0)
  138. while (smtp_shutdown == GNUNET_NO)
  139. {
  140. fd = OPEN (pipename, O_RDONLY | O_ASYNC);
  141. if (fd == -1)
  142. {
  143. if (smtp_shutdown == GNUNET_NO)
  144. GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);
  145. continue;
  146. }
  147. fdes = fdopen (fd, "r");
  148. while (smtp_shutdown == GNUNET_NO)
  149. {
  150. /* skip until end of header */
  151. do
  152. {
  153. READLINE (line, linesize);
  154. }
  155. while ((line[0] != '\r') && (line[0] != '\n')); /* expect newline */
  156. READLINE (line, linesize); /* read base64 encoded message; decode, process */
  157. pos = 0;
  158. while (1)
  159. {
  160. pos = strlen (line) - 1; /* ignore new line */
  161. READLINE (&line[pos], linesize - pos); /* read base64 encoded message; decode, process */
  162. if ((line[pos] == '\r') || (line[pos] == '\n'))
  163. break; /* empty line => end of message! */
  164. }
  165. size = GNUNET_STRINGS_base64_decode (line, pos, &out);
  166. if (size < sizeof (SMTPMessage))
  167. {
  168. GNUNET_GE_BREAK (ectx, 0);
  169. GNUNET_free (out);
  170. goto END;
  171. }
  172. mp = (SMTPMessage *) &out[size - sizeof (SMTPMessage)];
  173. if (ntohs (mp->header.size) != size)
  174. {
  175. GNUNET_GE_LOG (ectx,
  176. GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
  177. _("Received malformed message via %s. Ignored.\n"),
  178. "SMTP");
  179. #if DEBUG_SMTP
  180. GNUNET_GE_LOG (ectx,
  181. GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
  182. "Size returned by base64=%d, in the msg=%d.\n", size,
  183. ntohl (mp->size));
  184. #endif
  185. GNUNET_free (out);
  186. goto END;
  187. }
  188. if (stats != NULL)
  189. stats->change (stat_bytesReceived, size);
  190. coreMP = GNUNET_new (GNUNET_TransportPacket);
  191. coreMP->msg = out;
  192. coreMP->size = size - sizeof (SMTPMessage);
  193. coreMP->tsession = NULL;
  194. coreMP->sender = mp->sender;
  195. #if DEBUG_SMTP
  196. GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
  197. "SMTP message passed to the core.\n");
  198. #endif
  199. core_api->receive (coreMP);
  200. }
  201. END:
  202. #if DEBUG_SMTP
  203. GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
  204. "SMTP message processed.\n");
  205. #endif
  206. if (fdes != NULL)
  207. fclose (fdes);
  208. }
  209. GNUNET_free (line);
  210. return NULL;
  211. }
  212. /* *************** API implementation *************** */
  213. /**
  214. * Verify that a hello-Message is correct (a node is reachable at that
  215. * address). Since the reply will be asynchronous, a method must be
  216. * called on success.
  217. *
  218. * @param hello the hello message to verify
  219. * (the signature/crc have been verified before)
  220. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  221. */
  222. static int
  223. api_verify_hello (const GNUNET_MessageHello * hello)
  224. {
  225. const EmailAddress *maddr;
  226. maddr = (const EmailAddress *) &hello[1];
  227. if ((ntohs (hello->header.size) !=
  228. sizeof (GNUNET_MessageHello) + ntohs (hello->senderAddressSize)) ||
  229. (maddr->senderAddress
  230. [ntohs (hello->senderAddressSize) - 1 - FILTER_STRING_SIZE] != '\0'))
  231. {
  232. GNUNET_GE_BREAK (ectx, 0);
  233. return GNUNET_SYSERR; /* obviously invalid */
  234. }
  235. if (NULL == strstr (maddr->filter, ": "))
  236. return GNUNET_SYSERR;
  237. return GNUNET_OK;
  238. }
  239. /**
  240. * Create a hello-Message for the current node. The hello is created
  241. * without signature and without a timestamp. The GNUnet core will
  242. * GNUNET_RSA_sign the message and add an expiration time.
  243. *
  244. * @return hello on success, NULL on error
  245. */
  246. static GNUNET_MessageHello *
  247. api_create_hello ()
  248. {
  249. GNUNET_MessageHello *msg;
  250. char *filter;
  251. EmailAddress *haddr;
  252. int i;
  253. GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "FILTER",
  254. "X-mailer: GNUnet", &filter);
  255. if (NULL == strstr (filter, ": "))
  256. {
  257. GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
  258. _("SMTP filter string to invalid, lacks ': '\n"));
  259. GNUNET_free (filter);
  260. return NULL;
  261. }
  262. if (strlen (filter) > FILTER_STRING_SIZE)
  263. {
  264. filter[FILTER_STRING_SIZE] = '\0';
  265. GNUNET_GE_LOG (ectx, GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
  266. _("SMTP filter string to long, capped to `%s'\n"), filter);
  267. }
  268. i = (strlen (email) + 8) & (~7); /* make multiple of 8 */
  269. msg =
  270. GNUNET_malloc (sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i);
  271. memset (msg, 0, sizeof (GNUNET_MessageHello) + sizeof (EmailAddress) + i);
  272. haddr = (EmailAddress *) &msg[1];
  273. memset (&haddr->filter[0], 0, FILTER_STRING_SIZE);
  274. strcpy (&haddr->filter[0], filter);
  275. GNUNET_memcpy (&haddr->senderAddress[0], email, strlen (email) + 1);
  276. msg->senderAddressSize = htons (strlen (email) + 1 + sizeof (EmailAddress));
  277. msg->protocol = htons (GNUNET_TRANSPORT_PROTOCOL_NUMBER_SMTP);
  278. msg->MTU = htonl (smtpAPI.mtu);
  279. msg->header.size = htons (GNUNET_sizeof_hello (msg));
  280. if (api_verify_hello (msg) == GNUNET_SYSERR)
  281. GNUNET_GE_ASSERT (ectx, 0);
  282. GNUNET_free (filter);
  283. return msg;
  284. }
  285. struct GetMessageClosure
  286. {
  287. unsigned int esize;
  288. unsigned int pos;
  289. char *ebody;
  290. };
  291. static const char *
  292. get_message (void **buf, int *len, void *cls)
  293. {
  294. struct GetMessageClosure *gmc = cls;
  295. *buf = NULL;
  296. if (len == NULL)
  297. {
  298. gmc->pos = 0;
  299. return NULL;
  300. }
  301. if (gmc->pos == gmc->esize)
  302. return NULL; /* done */
  303. *len = gmc->esize;
  304. gmc->pos = gmc->esize;
  305. return gmc->ebody;
  306. }
  307. /**
  308. * Send a message to the specified remote node.
  309. *
  310. * @param tsession the GNUNET_MessageHello identifying the remote node
  311. * @param msg what to send
  312. * @param size the size of the message
  313. * @param important is this message important enough to override typical limits?
  314. * @return GNUNET_SYSERR on error, GNUNET_OK on success
  315. */
  316. static int
  317. api_send (GNUNET_TSession * tsession, const void *msg, const unsigned int size,
  318. int important)
  319. {
  320. const GNUNET_MessageHello *hello;
  321. const EmailAddress *haddr;
  322. char *m;
  323. char *filter;
  324. char *fvalue;
  325. SMTPMessage *mp;
  326. struct GetMessageClosure gm_cls;
  327. smtp_session_t session;
  328. smtp_message_t message;
  329. smtp_recipient_t recipient;
  330. #define EBUF_LEN 128
  331. char ebuf[EBUF_LEN];
  332. GNUNET_CronTime now;
  333. if (smtp_shutdown == GNUNET_YES)
  334. return GNUNET_SYSERR;
  335. if ((size == 0) || (size > smtpAPI.mtu))
  336. {
  337. GNUNET_GE_BREAK (ectx, 0);
  338. return GNUNET_SYSERR;
  339. }
  340. now = GNUNET_get_time ();
  341. if ((important != GNUNET_YES) &&
  342. ((now - last_transmission) * rate_limit) < GNUNET_CRON_HOURS)
  343. return GNUNET_NO; /* rate too high */
  344. last_transmission = now;
  345. hello = (const GNUNET_MessageHello *) tsession->internal;
  346. if (hello == NULL)
  347. return GNUNET_SYSERR;
  348. GNUNET_mutex_lock (lock);
  349. session = smtp_create_session ();
  350. if (session == NULL)
  351. {
  352. GNUNET_GE_LOG (ectx,
  353. GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  354. GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"),
  355. "smtp_create_session", smtp_strerror (smtp_errno (), ebuf,
  356. EBUF_LEN));
  357. GNUNET_mutex_unlock (lock);
  358. return GNUNET_SYSERR;
  359. }
  360. if (0 == smtp_set_server (session, smtp_server_name))
  361. {
  362. GNUNET_GE_LOG (ectx,
  363. GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  364. GNUNET_GE_IMMEDIATE, _("SMTP: `%s' failed: %s.\n"),
  365. "smtp_set_server", smtp_strerror (smtp_errno (), ebuf,
  366. EBUF_LEN));
  367. smtp_destroy_session (session);
  368. GNUNET_mutex_unlock (lock);
  369. return GNUNET_SYSERR;
  370. }
  371. haddr = (const EmailAddress *) &hello[1];
  372. message = smtp_add_message (session);
  373. if (message == NULL)
  374. {
  375. GNUNET_GE_LOG (ectx,
  376. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  377. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  378. "smtp_add_message", smtp_strerror (smtp_errno (), ebuf,
  379. EBUF_LEN));
  380. smtp_destroy_session (session);
  381. GNUNET_mutex_unlock (lock);
  382. return GNUNET_SYSERR;
  383. }
  384. smtp_set_header (message, "To", NULL, haddr->senderAddress);
  385. smtp_set_header (message, "From", NULL, email);
  386. filter = GNUNET_strdup (haddr->filter);
  387. fvalue = strstr (filter, ": ");
  388. GNUNET_GE_ASSERT (NULL, NULL != fvalue);
  389. fvalue[0] = '\0';
  390. fvalue += 2;
  391. if (0 == smtp_set_header (message, filter, fvalue))
  392. {
  393. GNUNET_GE_LOG (ectx,
  394. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  395. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  396. "smtp_set_header", smtp_strerror (smtp_errno (), ebuf,
  397. EBUF_LEN));
  398. smtp_destroy_session (session);
  399. GNUNET_mutex_unlock (lock);
  400. GNUNET_free (filter);
  401. return GNUNET_SYSERR;
  402. }
  403. GNUNET_free (filter);
  404. m = GNUNET_malloc (size + sizeof (SMTPMessage));
  405. GNUNET_memcpy (m, msg, size);
  406. mp = (SMTPMessage *) &m[size];
  407. mp->header.size = htons (size + sizeof (SMTPMessage));
  408. mp->header.type = htons (0);
  409. mp->sender = *core_api->my_identity;
  410. gm_cls.ebody = NULL;
  411. gm_cls.pos = 0;
  412. gm_cls.esize = GNUNET_STRINGS_base64_encode (m, size + sizeof (SMTPMessage), &gm_cls.ebody);
  413. GNUNET_free (m);
  414. if (0 == smtp_size_set_estimate (message, gm_cls.esize))
  415. {
  416. GNUNET_GE_LOG (ectx,
  417. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  418. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  419. "smtp_size_set_estimate", smtp_strerror (smtp_errno (), ebuf,
  420. EBUF_LEN));
  421. }
  422. if (0 == smtp_set_messagecb (message, &get_message, &gm_cls))
  423. {
  424. GNUNET_GE_LOG (ectx,
  425. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  426. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  427. "smtp_set_messagecb", smtp_strerror (smtp_errno (), ebuf,
  428. EBUF_LEN));
  429. smtp_destroy_session (session);
  430. GNUNET_mutex_unlock (lock);
  431. GNUNET_free (gm_cls.ebody);
  432. return GNUNET_SYSERR;
  433. }
  434. recipient = smtp_add_recipient (message, haddr->senderAddress);
  435. if (recipient == NULL)
  436. {
  437. GNUNET_GE_LOG (ectx,
  438. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  439. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  440. "smtp_add_recipient", smtp_strerror (smtp_errno (), ebuf,
  441. EBUF_LEN));
  442. smtp_destroy_session (session);
  443. GNUNET_mutex_unlock (lock);
  444. return GNUNET_SYSERR;
  445. }
  446. if (0 == smtp_start_session (session))
  447. {
  448. GNUNET_GE_LOG (ectx,
  449. GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
  450. GNUNET_GE_BULK, _("SMTP: `%s' failed: %s.\n"),
  451. "smtp_start_session", smtp_strerror (smtp_errno (), ebuf,
  452. EBUF_LEN));
  453. smtp_destroy_session (session);
  454. GNUNET_mutex_unlock (lock);
  455. GNUNET_free (gm_cls.ebody);
  456. return GNUNET_SYSERR;
  457. }
  458. if (stats != NULL)
  459. stats->change (stat_bytesSent, size);
  460. if (core_api->load_monitor != NULL)
  461. GNUNET_network_monitor_notify_transmission (core_api->load_monitor,
  462. GNUNET_ND_UPLOAD, gm_cls.esize);
  463. smtp_message_reset_status (message); /* this is needed to plug a 28-byte/message memory leak in libesmtp */
  464. smtp_destroy_session (session);
  465. GNUNET_mutex_unlock (lock);
  466. GNUNET_free (gm_cls.ebody);
  467. return GNUNET_OK;
  468. }
  469. /**
  470. * Establish a connection to a remote node.
  471. * @param hello the hello-Message for the target node
  472. * @param tsessionPtr the session handle that is to be set
  473. * @param may_reuse can we re-use an existing connection?
  474. * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
  475. */
  476. static int
  477. api_connect (const GNUNET_MessageHello * hello, GNUNET_TSession ** tsessionPtr,
  478. int may_reuse)
  479. {
  480. GNUNET_TSession *tsession;
  481. tsession = GNUNET_new (GNUNET_TSession);
  482. tsession->internal = GNUNET_malloc (GNUNET_sizeof_hello (hello));
  483. tsession->peer = hello->senderIdentity;
  484. GNUNET_memcpy (tsession->internal, hello, GNUNET_sizeof_hello (hello));
  485. tsession->ttype = smtpAPI.protocol_number;
  486. (*tsessionPtr) = tsession;
  487. return GNUNET_OK;
  488. }
  489. /**
  490. * Disconnect from a remote node.
  491. *
  492. * @param tsession the session that is closed
  493. * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
  494. */
  495. static int
  496. api_disconnect (GNUNET_TSession * tsession)
  497. {
  498. if (tsession != NULL)
  499. {
  500. if (tsession->internal != NULL)
  501. GNUNET_free (tsession->internal);
  502. GNUNET_free (tsession);
  503. }
  504. return GNUNET_OK;
  505. }
  506. /**
  507. * Start the server process to receive inbound traffic.
  508. * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
  509. */
  510. static int
  511. api_start_transport_server ()
  512. {
  513. smtp_shutdown = GNUNET_NO;
  514. /* initialize SMTP network */
  515. dispatchThread = GNUNET_thread_create (&listenAndDistribute, NULL, 1024 * 4);
  516. if (dispatchThread == NULL)
  517. {
  518. GNUNET_GE_DIE_STRERROR (ectx,
  519. GNUNET_GE_ADMIN | GNUNET_GE_BULK | GNUNET_GE_FATAL,
  520. "pthread_create");
  521. return GNUNET_SYSERR;
  522. }
  523. return GNUNET_OK;
  524. }
  525. /**
  526. * Shutdown the server process (stop receiving inbound traffic). Maybe
  527. * restarted later!
  528. */
  529. static int
  530. api_stop_transport_server ()
  531. {
  532. void *unused;
  533. smtp_shutdown = GNUNET_YES;
  534. GNUNET_thread_stop_sleep (dispatchThread);
  535. GNUNET_thread_join (dispatchThread, &unused);
  536. return GNUNET_OK;
  537. }
  538. /**
  539. * Convert SMTP hello to an IP address (always fails).
  540. */
  541. static int
  542. api_hello_to_address (const GNUNET_MessageHello * hello, void **sa,
  543. unsigned int *sa_len)
  544. {
  545. return GNUNET_SYSERR;
  546. }
  547. /**
  548. * Always fails.
  549. */
  550. static int
  551. api_associate (GNUNET_TSession * tsession)
  552. {
  553. return GNUNET_SYSERR; /* SMTP connections can never be associated */
  554. }
  555. /**
  556. * Always succeeds (for now; we should look at adding
  557. * frequency limits to SMTP in the future!).
  558. */
  559. static int
  560. api_test_would_try (GNUNET_TSession * tsession, unsigned int size,
  561. int important)
  562. {
  563. return GNUNET_OK; /* we always try... */
  564. }
  565. /**
  566. * The exported method. Makes the core api available via a global and
  567. * returns the smtp transport API.
  568. */
  569. GNUNET_TransportAPI *
  570. inittransport_smtp (struct GNUNET_CoreAPIForTransport * core)
  571. {
  572. unsigned long long mtu;
  573. struct sigaction sa;
  574. core_api = core;
  575. ectx = core->ectx;
  576. if (!GNUNET_GC_have_configuration_value (core_api->cfg, "SMTP", "EMAIL"))
  577. {
  578. GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
  579. _
  580. ("No email-address specified, can not start SMTP transport.\n"));
  581. return NULL;
  582. }
  583. GNUNET_GC_get_configuration_value_number (core_api->cfg, "SMTP", "MTU", 1200,
  584. SMTP_MESSAGE_SIZE,
  585. SMTP_MESSAGE_SIZE, &mtu);
  586. GNUNET_GC_get_configuration_value_number (core_api->cfg, "SMTP", "RATELIMIT",
  587. 0, 0, 1024 * 1024, &rate_limit);
  588. stats = core_api->service_request ("stats");
  589. if (stats != NULL)
  590. {
  591. stat_bytesReceived =
  592. stats->create (gettext_noop ("# bytes received via SMTP"));
  593. stat_bytesSent = stats->create (gettext_noop ("# bytes sent via SMTP"));
  594. stat_bytesDropped =
  595. stats->create (gettext_noop ("# bytes dropped by SMTP (outgoing)"));
  596. }
  597. GNUNET_GC_get_configuration_value_filename (core_api->cfg, "SMTP", "PIPE", &pipename);
  598. UNLINK (pipename);
  599. if (0 != mkfifo (pipename, S_IWUSR | S_IRUSR | S_IWGRP | S_IWOTH))
  600. {
  601. GNUNET_GE_LOG_STRERROR (ectx,
  602. GNUNET_GE_ADMIN | GNUNET_GE_BULK | GNUNET_GE_FATAL,
  603. "mkfifo");
  604. GNUNET_free (pipename);
  605. core_api->service_release (stats);
  606. stats = NULL;
  607. return NULL;
  608. }
  609. /* we need to allow the mailer program to send us messages;
  610. * easiest done by giving it write permissions (see Mantis #1142) */
  611. if (0 != chmod (pipename, S_IWUSR | S_IRUSR | S_IWGRP | S_IWOTH))
  612. GNUNET_GE_LOG_STRERROR (ectx,
  613. GNUNET_GE_ADMIN | GNUNET_GE_BULK |
  614. GNUNET_GE_WARNING, "chmod");
  615. GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "EMAIL", NULL,
  616. &email);
  617. lock = GNUNET_mutex_create (GNUNET_NO);
  618. GNUNET_GC_get_configuration_value_string (core_api->cfg, "SMTP", "SERVER",
  619. "localhost:25", &smtp_server_name);
  620. sa.sa_handler = SIG_IGN;
  621. sigemptyset (&sa.sa_mask);
  622. sa.sa_flags = 0;
  623. sigaction (SIGPIPE, &sa, &old_handler);
  624. smtpAPI.protocol_number = GNUNET_TRANSPORT_PROTOCOL_NUMBER_SMTP;
  625. smtpAPI.mtu = mtu - sizeof (SMTPMessage);
  626. smtpAPI.cost = 50;
  627. smtpAPI.hello_verify = &api_verify_hello;
  628. smtpAPI.hello_create = &api_create_hello;
  629. smtpAPI.connect = &api_connect;
  630. smtpAPI.send = &api_send;
  631. smtpAPI.associate = &api_associate;
  632. smtpAPI.disconnect = &api_disconnect;
  633. smtpAPI.server_start = &api_start_transport_server;
  634. smtpAPI.server_stop = &api_stop_transport_server;
  635. smtpAPI.hello_to_address = &api_hello_to_address;
  636. smtpAPI.send_now_test = &api_test_would_try;
  637. return &smtpAPI;
  638. }
  639. void
  640. donetransport_smtp ()
  641. {
  642. sigaction (SIGPIPE, &old_handler, NULL);
  643. GNUNET_free (smtp_server_name);
  644. if (stats != NULL)
  645. {
  646. core_api->service_release (stats);
  647. stats = NULL;
  648. }
  649. GNUNET_mutex_destroy (lock);
  650. lock = NULL;
  651. UNLINK (pipename);
  652. GNUNET_free (pipename);
  653. pipename = NULL;
  654. GNUNET_free (email);
  655. email = NULL;
  656. }
  657. /* end of smtp.c */