Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Baseline implementation of the "smtp" command. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | smtp |
Files: | files | file ages | folders |
SHA3-256: |
be55fc60c07438490f94d9b56dd9204e |
User & Date: | drh 2018-06-29 03:12:23.548 |
Context
2018-06-29
| ||
03:29 | Add features to make it easier to test and debug the "fossil smtp" command from the command-line using stdin and stdout. ... (check-in: 8643602d user: drh tags: smtp) | |
03:12 | Baseline implementation of the "smtp" command. ... (check-in: be55fc60 user: drh tags: smtp) | |
2018-06-28
| ||
21:38 | Add the smtp_send_msg() function capable of encoding and sending a complete email message. ... (check-in: 03888604 user: drh tags: smtp) | |
Changes
Changes to src/main.c.
︙ | ︙ | |||
1203 1204 1205 1206 1207 1208 1209 | ** Assume the user-id and group-id of the repository, or if zRepo ** is a directory, of that directory. ** ** The noJail flag means that the chroot jail is not entered. But ** privileges are still lowered to that of the user-id and group-id ** of the repository file. */ | | | 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 | ** Assume the user-id and group-id of the repository, or if zRepo ** is a directory, of that directory. ** ** The noJail flag means that the chroot jail is not entered. But ** privileges are still lowered to that of the user-id and group-id ** of the repository file. */ char *enter_chroot_jail(char *zRepo, int noJail){ #if !defined(_WIN32) if( getuid()==0 ){ int i; struct stat sStat; Blob dir; char *zDir; if( g.db!=0 ){ |
︙ | ︙ |
Changes to src/smtp.c.
︙ | ︙ | |||
114 115 116 117 118 119 120 | struct SmtpSession { const char *zFrom; /* Domain from which we are sending */ const char *zDest; /* Domain that will receive the email */ char *zHostname; /* Hostname of SMTP server for zDest */ u32 smtpFlags; /* Flags changing the operation */ FILE *logFile; /* Write session transcript to this log file */ Blob *pTranscript; /* Record session transcript here */ | < > > | | > > > > > | | < > > > > > > | > > > | > < < | 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | struct SmtpSession { const char *zFrom; /* Domain from which we are sending */ const char *zDest; /* Domain that will receive the email */ char *zHostname; /* Hostname of SMTP server for zDest */ u32 smtpFlags; /* Flags changing the operation */ FILE *logFile; /* Write session transcript to this log file */ Blob *pTranscript; /* Record session transcript here */ int atEof; /* True after connection closes */ char *zErr; /* Error message */ Blob inbuf; /* Input buffer */ }; /* Allowed values for SmtpSession.smtpFlags */ #define SMTP_TRACE_STDOUT 0x00001 /* Debugging info to console */ #define SMTP_TRACE_FILE 0x00002 /* Debugging info to logFile */ #define SMTP_TRACE_BLOB 0x00004 /* Record transcript */ #define SMTP_DIRECT 0x00008 /* Skip the MX lookup */ #define SMTP_PORT 0x00010 /* Use an alternate port number */ #endif /* ** Shutdown an SmtpSession */ void smtp_session_free(SmtpSession *pSession){ socket_close(); blob_zero(&pSession->inbuf); fossil_free(pSession->zHostname); fossil_free(pSession->zErr); fossil_free(pSession); } /* ** Allocate a new SmtpSession object. ** ** Both zFrom and zDest must be specified. ** ** The ... arguments are in this order: ** ** SMTP_PORT: int ** SMTP_TRACE_FILE: FILE* ** SMTP_TRACE_BLOB: Blob* */ SmtpSession *smtp_session_new( const char *zFrom, /* Domain for the client */ const char *zDest, /* Domain of the server */ u32 smtpFlags, /* Flags */ ... /* Arguments depending on the flags */ ){ SmtpSession *p; va_list ap; UrlData url; p = fossil_malloc( sizeof(*p) ); memset(p, 0, sizeof(*p)); p->zFrom = zFrom; p->zDest = zDest; p->smtpFlags = smtpFlags; memset(&url, 0, sizeof(url)); url.port = 25; blob_init(&p->inbuf, 0, 0); va_start(ap, smtpFlags); if( smtpFlags & SMTP_PORT ){ url.port = va_arg(ap, int); } if( smtpFlags & SMTP_TRACE_FILE ){ p->logFile = va_arg(ap, FILE*); } if( smtpFlags & SMTP_TRACE_BLOB ){ p->pTranscript = va_arg(ap, Blob*); } va_end(ap); if( (smtpFlags & SMTP_DIRECT)!=0 ){ p->zHostname = fossil_strdup(zDest); }else{ p->zHostname = smtp_mx_host(zDest); } if( p->zHostname==0 ){ p->atEof = 1; p->zErr = mprintf("cannot locate SMTP server for \"%s\"", zDest); return p; } url.name = p->zHostname; socket_global_init(); if( socket_open(&url) ){ p->atEof = 1; p->zErr = socket_errmsg(); socket_close(); } return p; |
︙ | ︙ | |||
204 205 206 207 208 209 210 | va_end(ap); z = blob_buffer(&b); n = blob_size(&b); assert( n>=2 ); assert( z[n-1]=='\n' ); assert( z[n-2]=='\r' ); if( p->smtpFlags & SMTP_TRACE_STDOUT ){ | | | | | 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | va_end(ap); z = blob_buffer(&b); n = blob_size(&b); assert( n>=2 ); assert( z[n-1]=='\n' ); assert( z[n-2]=='\r' ); if( p->smtpFlags & SMTP_TRACE_STDOUT ){ fossil_print("C: %.*s\n", n-2, z); } if( p->smtpFlags & SMTP_TRACE_FILE ){ fprintf(p->logFile, "C: %.*s\n", n-2, z); } if( p->smtpFlags & SMTP_TRACE_BLOB ){ blob_appendf(p->pTranscript, "C: %.*s\n", n-2, z); } socket_send(0, z, n); blob_zero(&b); } /* ** Read a line of input received from the SMTP server. Make in point |
︙ | ︙ | |||
269 270 271 272 273 274 275 | blob_line(&p->inbuf, in); } z = blob_buffer(in); n = blob_size(in); if( n && z[n-1]=='\n' ) n--; if( n && z[n-1]=='\r' ) n--; if( p->smtpFlags & SMTP_TRACE_STDOUT ){ | | | | | 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | blob_line(&p->inbuf, in); } z = blob_buffer(in); n = blob_size(in); if( n && z[n-1]=='\n' ) n--; if( n && z[n-1]=='\r' ) n--; if( p->smtpFlags & SMTP_TRACE_STDOUT ){ fossil_print("S: %.*s\n", n, z); } if( p->smtpFlags & SMTP_TRACE_FILE ){ fprintf(p->logFile, "S: %.*s\n", n, z); } if( p->smtpFlags & SMTP_TRACE_BLOB ){ blob_appendf(p->pTranscript, "S: %.*s\n", n-2, z); } } /* ** Capture a single-line server reply. */ static void smtp_get_reply_from_server( |
︙ | ︙ | |||
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | ** COMMAND: test-smtp-probe ** ** Usage: %fossil test-smtp-probe DOMAIN [ME] ** ** Interact with the SMTP server for DOMAIN by setting up a connection ** and then immediately shutting it back down. Log all interaction ** on the console. Use ME as the domain name of the sender. */ void test_smtp_probe(void){ SmtpSession *p; const char *zDomain; const char *zSelf; if( g.argc!=3 && g.argc!=4 ) usage("DOMAIN [ME]"); zDomain = g.argv[2]; zSelf = g.argc==4 ? g.argv[3] : "fossil-scm.org"; | > > > > > > > > > > > > > | | 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | ** COMMAND: test-smtp-probe ** ** Usage: %fossil test-smtp-probe DOMAIN [ME] ** ** Interact with the SMTP server for DOMAIN by setting up a connection ** and then immediately shutting it back down. Log all interaction ** on the console. Use ME as the domain name of the sender. ** ** Options: ** ** --direct Use DOMAIN directly without going through MX ** --port N Talk on TCP port N */ void test_smtp_probe(void){ SmtpSession *p; const char *zDomain; const char *zSelf; const char *zPort; int iPort = 25; u32 smtpFlags = SMTP_TRACE_STDOUT|SMTP_PORT; if( find_option("direct",0,0)!=0 ) smtpFlags |= SMTP_DIRECT; zPort = find_option("port",0,1); if( zPort ) iPort = atoi(zPort); verify_all_options(); if( g.argc!=3 && g.argc!=4 ) usage("DOMAIN [ME]"); zDomain = g.argv[2]; zSelf = g.argc==4 ? g.argv[3] : "fossil-scm.org"; p = smtp_session_new(zSelf, zDomain, smtpFlags, iPort); if( p->zErr ){ fossil_fatal("%s", p->zErr); } fossil_print("Connection to \"%s\"\n", p->zHostname); smtp_client_startup(p); smtp_client_quit(p); if( p->zErr ){ |
︙ | ︙ | |||
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | ** Usage: %fossil test-smtp-send EMAIL FROM TO ... ** ** Use SMTP to send the email message contained in the file named EMAIL ** to the list of users TO. FROM is the sender of the email. ** ** Options: ** ** --trace Show the SMTP conversation on the console */ void test_smtp_send(void){ SmtpSession *p; const char *zFrom; int nTo; const char *zToDomain; const char *zFromDomain; const char **azTo; Blob body; | > > > > | > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | ** Usage: %fossil test-smtp-send EMAIL FROM TO ... ** ** Use SMTP to send the email message contained in the file named EMAIL ** to the list of users TO. FROM is the sender of the email. ** ** Options: ** ** --direct Go directly to the TO domain. Bypass MX lookup ** --port N Use TCP port N instead of 25 ** --trace Show the SMTP conversation on the console */ void test_smtp_send(void){ SmtpSession *p; const char *zFrom; int nTo; const char *zToDomain; const char *zFromDomain; const char **azTo; int smtpPort = 25; const char *zPort; Blob body; u32 smtpFlags = SMTP_PORT; if( find_option("trace",0,0)!=0 ) smtpFlags |= SMTP_TRACE_STDOUT; if( find_option("direct",0,0)!=0 ) smtpFlags |= SMTP_DIRECT; zPort = find_option("port",0,1); if( zPort ) smtpPort = atoi(zPort); verify_all_options(); if( g.argc<5 ) usage("EMAIL FROM TO ..."); blob_read_from_file(&body, g.argv[2], ExtFILE); zFrom = g.argv[3]; nTo = g.argc-4; azTo = (const char**)g.argv+4; zFromDomain = domainOfAddr(zFrom); zToDomain = domainOfAddr(azTo[0]); p = smtp_session_new(zFromDomain, zToDomain, smtpFlags, smtpPort); if( p->zErr ){ fossil_fatal("%s", p->zErr); } fossil_print("Connection to \"%s\"\n", p->zHostname); smtp_client_startup(p); smtp_send_msg(p, zFrom, nTo, azTo, blob_str(&body)); smtp_client_quit(p); if( p->zErr ){ fossil_fatal("ERROR: %s\n", p->zErr); } smtp_session_free(p); blob_zero(&body); } /***************************************************************************** ** Server implementation *****************************************************************************/ #if LOCAL_INTERFACE /* ** State information for the server */ struct SmtpServer { sqlite3 *db; /* Database into which the email is delivered */ char *zEhlo; /* Client domain on the EHLO line */ char *zFrom; /* MAIL FROM: argument */ int nTo; /* Number of RCPT TO: lines seen */ char **azTo; /* Address in each RCPT TO line */ u32 srvrFlags; /* Control flags */ Blob msg; /* Content following DATA */ Blob transcript; /* Session transcript */ }; #define SMTPSRV_CLEAR_MSG 1 /* smtp_server_clear() last message only */ #define SMTPSRV_CLEAR_ALL 2 /* smtp_server_clear() everything */ #define SMTPSRV_LOG 0x001 /* Record a transcript of the interaction */ #endif /* LOCAL_INTERFACE */ /* ** Clear the SmtpServer object. Deallocate resources. ** How much to clear depends on eHowMuch */ static void smtp_server_clear(SmtpServer *p, int eHowMuch){ int i; if( eHowMuch>=SMTPSRV_CLEAR_MSG ){ fossil_free(p->zFrom); p->zFrom = 0; for(i=0; i<p->nTo; i++) fossil_free(p->azTo[0]); fossil_free(p->azTo); p->azTo = 0; p->nTo = 0; blob_zero(&p->msg); } if( eHowMuch>=SMTPSRV_CLEAR_ALL ){ blob_zero(&p->transcript); sqlite3_close(p->db); p->db = 0; fossil_free(p->zEhlo); p->zEhlo = 0; } } /* ** Turn raw memory into an SmtpServer object. */ static void smtp_server_init(SmtpServer *p){ memset(p, 0, sizeof(*p)); blob_init(&p->msg, 0, 0); blob_init(&p->transcript, 0, 0); } /* ** Send a single line of output from the server to the client. */ static void smtp_server_send(SmtpServer *p, const char *zFormat, ...){ Blob b = empty_blob; va_list ap; char *z; int n; va_start(ap, zFormat); blob_vappendf(&b, zFormat, ap); va_end(ap); z = blob_buffer(&b); n = blob_size(&b); assert( n>=2 ); assert( z[n-1]=='\n' ); assert( z[n-2]=='\r' ); if( p->srvrFlags & SMTPSRV_LOG ){ blob_appendf(&p->transcript, "S: %.*s\n", n-2, z); } fwrite(z, n, 1, stdout); fflush(stdout); blob_zero(&b); } /* ** Read a single line from the client. */ static int smtp_server_gets(SmtpServer *p, char *aBuf, int nBuf){ int rc = fgets(aBuf, nBuf, stdin)!=0; if( rc && (p->srvrFlags & SMTPSRV_LOG)!=0 ){ blob_appendf(&p->transcript, "C: %s\n", aBuf); } return rc; } /* ** Capture the incoming email data into the p->msg blob. Dequote ** lines of "..\r\n" into just ".\r\n". */ static void smtp_server_capture_data(SmtpServer *p, char *z, int n){ while( fgets(z, n, stdin) ){ if( strncmp(z, ".\r\n", 3)==0 ) return; if( strncmp(z, "..\r\n", 4)==0 ){ memmove(z, z+1, 4); } blob_append(&p->msg, z, -1); } } /* ** COMMAND: smtp ** ** Usage: %fossil smtp [options] DBNAME ** ** Begin a SMTP conversation with a client using stdin/stdout. (This ** command is expected to be launched from xinetd or the equivalent.) ** Use information in the SQLite database at DBNAME to find configuration ** information and as a place to store the incoming content. */ void smtp_server(void){ char *zDbName; const char *zDomain; SmtpServer x; char z[5000]; smtp_server_init(&x); zDomain = find_option("domain",0,1); if( zDomain==0 ) zDomain = "unspecified.domain"; verify_all_options(); if( g.argc!=3 ) usage("DBNAME"); zDbName = g.argv[2]; zDbName = enter_chroot_jail(zDbName, 0); smtp_server_send(&x, "220 %s ESMTP Fossil ([%.*s] %s)\r\n", zDomain, 16, MANIFEST_VERSION, MANIFEST_DATE); while( smtp_server_gets(&x, z, sizeof(z)) ){ if( strncmp(z, "EHLO ", 5)==0 ){ smtp_server_send(&x, "250 ok\r\n"); }else if( strncmp(z, "HELO ", 5)==0 ){ smtp_server_send(&x, "250 ok\r\n"); }else if( strncmp(z, "MAIL FROM:<", 11)==0 ){ smtp_server_send(&x, "250 ok\r\n"); }else if( strncmp(z, "RCPT TO:<", 9)==0 ){ smtp_server_send(&x, "250 ok\r\n"); }else if( strncmp(z, "DATA", 4)==0 ){ smtp_server_send(&x, "354 ready\r\n"); smtp_server_capture_data(&x, z, sizeof(z)); smtp_server_send(&x, "250 ok\r\n"); smtp_server_clear(&x, SMTPSRV_CLEAR_MSG); }else if( strncmp(z, "QUIT", 4)==0 ){ smtp_server_send(&x, "221 closing connection\r\n"); break; }else { smtp_server_send(&x, "500 unknown command\r\n"); } } fclose(stdin); smtp_server_clear(&x, SMTPSRV_CLEAR_ALL); } |