- /* Process the SMTP greeting from the server */
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP conversation");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* At this point we know we are talking to a real SMTP server */
-
- /* Do a EHLO command. If it fails, try the HELO command. */
- snprintf(buf, sizeof buf, "EHLO %s\r\n", config.c_fqdn);
- lprintf(CTDL_DEBUG, ">%s", buf);
- sock_write(sock, buf, strlen(buf));
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP HELO");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '2') {
- snprintf(buf, sizeof buf, "HELO %s\r\n", config.c_fqdn);
- lprintf(CTDL_DEBUG, ">%s", buf);
- sock_write(sock, buf, strlen(buf));
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP HELO");
- goto bail;
- }
- }
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* Do an AUTH command if necessary */
- if (!IsEmptyStr(mx_user)) {
- char encoded[1024];
- sprintf(buf, "%s%c%s%c%s", mx_user, '\0', mx_user, '\0', mx_pass);
- CtdlEncodeBase64(encoded, buf, strlen(mx_user) + strlen(mx_user) + strlen(mx_pass) + 2, 0);
- snprintf(buf, sizeof buf, "AUTH PLAIN %s\r\n", encoded);
- lprintf(CTDL_DEBUG, ">%s", buf);
- sock_write(sock, buf, strlen(buf));
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP AUTH");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
- }
-
- /* previous command succeeded, now try the MAIL From: command */
- snprintf(buf, sizeof buf, "MAIL From: <%s>\r\n", mailfrom);
- lprintf(CTDL_DEBUG, ">%s", buf);
- sock_write(sock, buf, strlen(buf));
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP MAIL");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* MAIL succeeded, now try the RCPT To: command */
- snprintf(buf, sizeof buf, "RCPT To: <%s@%s>\r\n", user, node);
- lprintf(CTDL_DEBUG, ">%s", buf);
- sock_write(sock, buf, strlen(buf));
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP RCPT");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* RCPT succeeded, now try the DATA command */
- lprintf(CTDL_DEBUG, ">DATA\n");
- sock_write(sock, "DATA\r\n", 6);
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP DATA");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- if (buf[0] != '3') {
- if (buf[0] == '4') {
- *status = 3;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* If we reach this point, the server is expecting data */
- sock_write(sock, msgtext, msg_size);
- if (msgtext[msg_size-1] != 10) {
- lprintf(CTDL_WARNING, "Possible problem: message did not "
- "correctly terminate. (expecting 0x10, got 0x%02x)\n",
- buf[msg_size-1]);
- }
-
- sock_write(sock, ".\r\n", 3);
- if (ml_sock_gets(sock, buf) < 0) {
- *status = 4;
- strcpy(dsn, "Connection broken during SMTP message transmit");
- goto bail;
- }
- lprintf(CTDL_DEBUG, "%s\n", buf);
- if (buf[0] != '2') {
- if (buf[0] == '4') {
- *status = 4;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- else {
- *status = 5;
- safestrncpy(dsn, &buf[4], 1023);
- goto bail;
- }
- }
-
- /* We did it! */
- safestrncpy(dsn, &buf[4], 1023);
- *status = 2;
-
- lprintf(CTDL_DEBUG, ">QUIT\n");
- sock_write(sock, "QUIT\r\n", 6);
- ml_sock_gets(sock, buf);
- lprintf(CTDL_DEBUG, "<%s\n", buf);
- lprintf(CTDL_INFO, "SMTP delivery to <%s> @ <%s> (%s) succeeded\n",
- user, node, name);
-
-bail: free(msgtext);
- sock_close(sock);
-
- /* Write something to the syslog (which may or may not be where the
- * rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
- */
- if (enable_syslog) {
- syslog((LOG_MAIL | LOG_INFO),
- "%ld: to=<%s>, relay=%s, stat=%s",
- msgnum,
- addr,
- mx_host,
- dsn
- );
- }
-
- return;
-}
-
-
-
-/*
- * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
- * instructions for "5" codes (permanent fatal errors) and produce/deliver
- * a "bounce" message (delivery status notification).
- */
-void smtp_do_bounce(char *instr) {
- int i;
- int lines;
- int status;
- char buf[1024];
- char key[1024];
- char addr[1024];
- char dsn[1024];
- char bounceto[1024];
- char boundary[64];
- int num_bounces = 0;
- int bounce_this = 0;
- long bounce_msgid = (-1);
- time_t submitted = 0L;
- struct CtdlMessage *bmsg = NULL;
- int give_up = 0;
- struct recptypes *valid;
- int successful_bounce = 0;
- static int seq = 0;
- char *omsgtext;
- size_t omsgsize;
- long omsgid = (-1);
-
- lprintf(CTDL_DEBUG, "smtp_do_bounce() called\n");
- strcpy(bounceto, "");
- sprintf(boundary, "=_Citadel_Multipart_%s_%04x%04x", config.c_fqdn, getpid(), ++seq);
- lines = num_tokens(instr, '\n');
-
- /* See if it's time to give up on delivery of this message */
- for (i=0; i<lines; ++i) {
- extract_token(buf, instr, i, '\n', sizeof buf);
- extract_token(key, buf, 0, '|', sizeof key);
- extract_token(addr, buf, 1, '|', sizeof addr);
- if (!strcasecmp(key, "submitted")) {
- submitted = atol(addr);
- }