Fossil SCM

fossil-scm / src / smtp.c
Source Blame History 699 lines
0a201f7… drh 1 /*
0a201f7… drh 2 ** Copyright (c) 2018 D. Richard Hipp
0a201f7… drh 3 **
0a201f7… drh 4 ** This program is free software; you can redistribute it and/or
0a201f7… drh 5 ** modify it under the terms of the Simplified BSD License (also
0a201f7… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
0a201f7… drh 7 **
0a201f7… drh 8 ** This program is distributed in the hope that it will be useful,
0a201f7… drh 9 ** but without any warranty; without even the implied warranty of
0a201f7… drh 10 ** merchantability or fitness for a particular purpose.
0a201f7… drh 11 **
0a201f7… drh 12 ** Author contact information:
0a201f7… drh 13 ** [email protected]
0a201f7… drh 14 ** http://www.hwaci.com/drh/
0a201f7… drh 15 **
0a201f7… drh 16 *******************************************************************************
0a201f7… drh 17 **
0a201f7… drh 18 ** Implementation of SMTP (Simple Mail Transport Protocol) according
0a201f7… drh 19 ** to RFC 5321.
0a201f7… drh 20 */
0a201f7… drh 21 #include "config.h"
0a201f7… drh 22 #include "smtp.h"
0a201f7… drh 23 #include <assert.h>
275da70… danield 24 #if (HAVE_DN_EXPAND || HAVE___NS_NAME_UNCOMPRESS || HAVE_NS_NAME_UNCOMPRESS) \
275da70… danield 25 && (HAVE_NS_PARSERR || HAVE___NS_PARSERR) && !defined(FOSSIL_OMIT_DNS)
0a201f7… drh 26 # include <sys/types.h>
0a201f7… drh 27 # include <netinet/in.h>
71c298f… wyoung 28 # if defined(HAVE_BIND_RESOLV_H)
71c298f… wyoung 29 # include <bind/resolv.h>
71c298f… wyoung 30 # include <bind/arpa/nameser_compat.h>
71c298f… wyoung 31 # else
71c298f… wyoung 32 # include <arpa/nameser.h>
71c298f… wyoung 33 # include <resolv.h>
71c298f… wyoung 34 # endif
71c298f… wyoung 35 # if defined(HAVENS_NAME_UNCOMPRESS) && !defined(dn_expand)
71c298f… wyoung 36 # define dn_expand ns_name_uncompress
71c298f… wyoung 37 # endif
71c298f… wyoung 38 # if defined(HAVE__NS_NAME_UNCOMPRESS) && !defined(dn_expand)
71c298f… wyoung 39 # define dn_expand __ns_name_uncompress
71c298f… wyoung 40 # endif
9a6c6c2… drh 41 # define FOSSIL_UNIX_STYLE_DNS 1
9a6c6c2… drh 42 #endif
9a6c6c2… drh 43 #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)
9a6c6c2… drh 44 # include <windows.h>
9a6c6c2… drh 45 # include <windns.h>
9a6c6c2… drh 46 # define FOSSIL_WINDOWS_STYLE_DNS 1
9a6c6c2… drh 47 #endif
0a201f7… drh 48
0a201f7… drh 49
0a201f7… drh 50 /*
0a201f7… drh 51 ** Find the hostname for receiving email for the domain given
0a201f7… drh 52 ** in zDomain. Return NULL if not found or not implemented.
0a201f7… drh 53 ** If multiple email receivers are advertized, pick the one with
0a201f7… drh 54 ** the lowest preference number.
0a201f7… drh 55 **
0a201f7… drh 56 ** The returned string is obtained from fossil_malloc()
0a201f7… drh 57 ** and should be released using fossil_free().
0a201f7… drh 58 */
0a201f7… drh 59 char *smtp_mx_host(const char *zDomain){
9a6c6c2… drh 60 #if defined(FOSSIL_UNIX_STYLE_DNS)
0a201f7… drh 61 int nDns; /* Length of the DNS reply */
0a201f7… drh 62 int rc; /* Return code from various APIs */
0a201f7… drh 63 int i; /* Loop counter */
0a201f7… drh 64 int iBestPriority = 9999999; /* Best priority */
0a201f7… drh 65 int nRec; /* Number of answers */
0a201f7… drh 66 ns_msg h; /* DNS reply parser */
0a201f7… drh 67 const unsigned char *pBest = 0; /* RDATA for the best answer */
0a201f7… drh 68 unsigned char aDns[5000]; /* Raw DNS reply content */
0a201f7… drh 69 char zHostname[5000]; /* Hostname for the MX */
0a201f7… drh 70
0a201f7… drh 71 nDns = res_query(zDomain, C_IN, T_MX, aDns, sizeof(aDns));
0a201f7… drh 72 if( nDns<=0 ) return 0;
0a201f7… drh 73 res_init();
0a201f7… drh 74 rc = ns_initparse(aDns,nDns,&h);
0a201f7… drh 75 if( rc ) return 0;
0a201f7… drh 76 nRec = ns_msg_count(h, ns_s_an);
0a201f7… drh 77 for(i=0; i<nRec; i++){
0a201f7… drh 78 ns_rr x;
0a201f7… drh 79 int priority, sz;
0a201f7… drh 80 const unsigned char *p;
0a201f7… drh 81 rc = ns_parserr(&h, ns_s_an, i, &x);
0a201f7… drh 82 if( rc ) continue;
0a201f7… drh 83 p = ns_rr_rdata(x);
0a201f7… drh 84 sz = ns_rr_rdlen(x);
0a201f7… drh 85 if( sz>2 ){
0a201f7… drh 86 priority = p[0]*256 + p[1];
0a201f7… drh 87 if( priority<iBestPriority ){
0a201f7… drh 88 pBest = p;
0a201f7… drh 89 iBestPriority = priority;
0a201f7… drh 90 }
0a201f7… drh 91 }
0a201f7… drh 92 }
0a201f7… drh 93 if( pBest ){
71c298f… wyoung 94 dn_expand(aDns, aDns+nDns, pBest+2, zHostname, sizeof(zHostname));
0a201f7… drh 95 return fossil_strdup(zHostname);
0a201f7… drh 96 }
9a6c6c2… drh 97 return 0;
9a6c6c2… drh 98 #elif defined(FOSSIL_WINDOWS_STYLE_DNS)
9a6c6c2… drh 99 DNS_STATUS status; /* Return status */
9a6c6c2… drh 100 PDNS_RECORDA pDnsRecord, p; /* Pointer to DNS_RECORD structure */
9a6c6c2… drh 101 int iBestPriority = 9999999; /* Best priority */
9a6c6c2… drh 102 char *pBest = 0; /* RDATA for the best answer */
9a6c6c2… drh 103
9a6c6c2… drh 104 status = DnsQuery_UTF8(zDomain, /* Domain name */
9a6c6c2… drh 105 DNS_TYPE_MX, /* DNS record type */
9a6c6c2… drh 106 DNS_QUERY_STANDARD, /* Query options */
9a6c6c2… drh 107 NULL, /* List of DNS servers */
9a6c6c2… drh 108 &pDnsRecord, /* Query results */
9a6c6c2… drh 109 NULL); /* Reserved */
9a6c6c2… drh 110 if( status ) return NULL;
9a6c6c2… drh 111
9a6c6c2… drh 112 p = pDnsRecord;
9a6c6c2… drh 113 while( p ){
9a6c6c2… drh 114 if( p->Data.MX.wPreference<iBestPriority ){
9a6c6c2… drh 115 iBestPriority = p->Data.MX.wPreference;
9a6c6c2… drh 116 pBest = p->Data.MX.pNameExchange;
9a6c6c2… drh 117 }
9a6c6c2… drh 118 p = p->pNext;
9a6c6c2… drh 119 }
9a6c6c2… drh 120 if( pBest ){
70a94d0… jan.nijtmans 121 pBest = fossil_strdup(pBest);
9a6c6c2… drh 122 }
9a6c6c2… drh 123 DnsRecordListFree(pDnsRecord, DnsFreeRecordListDeep);
9a6c6c2… drh 124 return pBest;
9a6c6c2… drh 125 #else
0a201f7… drh 126 return 0;
9a6c6c2… drh 127 #endif /* defined(FOSSIL_WINDOWS_STYLE_DNS) */
0a201f7… drh 128 }
0a201f7… drh 129
0a201f7… drh 130 /*
0a201f7… drh 131 ** COMMAND: test-find-mx
0a201f7… drh 132 **
0a201f7… drh 133 ** Usage: %fossil test-find-mx DOMAIN ...
0a201f7… drh 134 **
0a201f7… drh 135 ** Do a DNS MX lookup to find the hostname for sending email for
0a201f7… drh 136 ** DOMAIN.
0a201f7… drh 137 */
0a201f7… drh 138 void test_find_mx(void){
0a201f7… drh 139 int i;
9a6c6c2… drh 140 if( g.argc<=2 ){
0a201f7… drh 141 usage("DOMAIN ...");
0a201f7… drh 142 }
0a201f7… drh 143 for(i=2; i<g.argc; i++){
0a201f7… drh 144 char *z = smtp_mx_host(g.argv[i]);
0a201f7… drh 145 fossil_print("%s: %s\n", g.argv[i], z);
0a201f7… drh 146 fossil_free(z);
0a201f7… drh 147 }
0a201f7… drh 148 }
0a201f7… drh 149
0a201f7… drh 150 #if INTERFACE
0a201f7… drh 151 /*
0a201f7… drh 152 ** Information about a single SMTP connection.
0a201f7… drh 153 */
0a201f7… drh 154 struct SmtpSession {
0a201f7… drh 155 const char *zFrom; /* Domain from which we are sending */
0a201f7… drh 156 const char *zDest; /* Domain that will receive the email */
0a201f7… drh 157 char *zHostname; /* Hostname of SMTP server for zDest */
0a201f7… drh 158 u32 smtpFlags; /* Flags changing the operation */
0a201f7… drh 159 FILE *logFile; /* Write session transcript to this log file */
0a201f7… drh 160 Blob *pTranscript; /* Record session transcript here */
2b96941… drh 161 int bOpen; /* True if connection is Open */
e6c27d3… drh 162 int bFatal; /* Error is fatal. Do not retry */
0a201f7… drh 163 char *zErr; /* Error message */
0a201f7… drh 164 Blob inbuf; /* Input buffer */
2b96941… drh 165 UrlData url; /* Address of the server */
0a201f7… drh 166 };
0a201f7… drh 167
0a201f7… drh 168 /* Allowed values for SmtpSession.smtpFlags */
0a201f7… drh 169 #define SMTP_TRACE_STDOUT 0x00001 /* Debugging info to console */
0a201f7… drh 170 #define SMTP_TRACE_FILE 0x00002 /* Debugging info to logFile */
0a201f7… drh 171 #define SMTP_TRACE_BLOB 0x00004 /* Record transcript */
0a201f7… drh 172 #define SMTP_DIRECT 0x00008 /* Skip the MX lookup */
0a201f7… drh 173 #define SMTP_PORT 0x00010 /* Use an alternate port number */
0a201f7… drh 174
0a201f7… drh 175 #endif
0a201f7… drh 176
0a201f7… drh 177 /*
0a201f7… drh 178 ** Shutdown an SmtpSession
0a201f7… drh 179 */
0a201f7… drh 180 void smtp_session_free(SmtpSession *pSession){
0a201f7… drh 181 socket_close();
0a201f7… drh 182 blob_reset(&pSession->inbuf);
0a201f7… drh 183 fossil_free(pSession->zHostname);
0a201f7… drh 184 fossil_free(pSession->zErr);
0a201f7… drh 185 fossil_free(pSession);
0a201f7… drh 186 }
0a201f7… drh 187
0a201f7… drh 188 /*
e6c27d3… drh 189 ** Set an error message on the SmtpSession
e6c27d3… drh 190 */
e6c27d3… drh 191 static void smtp_set_error(
e6c27d3… drh 192 SmtpSession *p, /* The SMTP context */
e6c27d3… drh 193 int bFatal, /* Fatal error. Reset and retry is pointless */
e6c27d3… drh 194 const char *zFormat, /* Error message. */
e6c27d3… drh 195 ...
e6c27d3… drh 196 ){
e6c27d3… drh 197 if( bFatal ) p->bFatal = 1;
e6c27d3… drh 198 if( p->zErr==0 ){
e6c27d3… drh 199 va_list ap;
e6c27d3… drh 200 va_start(ap, zFormat);
e6c27d3… drh 201 p->zErr = vmprintf(zFormat, ap);
e6c27d3… drh 202 va_end(ap);
e6c27d3… drh 203 }
2b96941… drh 204 if( p->bOpen ){
2b96941… drh 205 socket_close();
2b96941… drh 206 p->bOpen = 0;
2b96941… drh 207 }
e6c27d3… drh 208 }
e6c27d3… drh 209
e6c27d3… drh 210 /*
0a201f7… drh 211 ** Allocate a new SmtpSession object.
0a201f7… drh 212 **
3fc9b90… drh 213 ** Both zFrom and zDest must be specified. smtpFlags may not contain
3fc9b90… drh 214 ** either SMTP_TRACE_FILE or SMTP_TRACE_BLOB as those settings must be
3fc9b90… drh 215 ** added by a subsequent call to smtp_session_config().
0a201f7… drh 216 **
3fc9b90… drh 217 ** The iPort option is ignored unless SMTP_PORT is set in smtpFlags
0a201f7… drh 218 */
0a201f7… drh 219 SmtpSession *smtp_session_new(
0a201f7… drh 220 const char *zFrom, /* Domain for the client */
0a201f7… drh 221 const char *zDest, /* Domain of the server */
0a201f7… drh 222 u32 smtpFlags, /* Flags */
3fc9b90… drh 223 int iPort /* TCP port if the SMTP_PORT flags is present */
0a201f7… drh 224 ){
0a201f7… drh 225 SmtpSession *p;
0a201f7… drh 226
0a201f7… drh 227 p = fossil_malloc( sizeof(*p) );
0a201f7… drh 228 memset(p, 0, sizeof(*p));
0a201f7… drh 229 p->zFrom = zFrom;
0a201f7… drh 230 p->zDest = zDest;
0a201f7… drh 231 p->smtpFlags = smtpFlags;
2b96941… drh 232 p->url.port = 25;
0a201f7… drh 233 blob_init(&p->inbuf, 0, 0);
0a201f7… drh 234 if( smtpFlags & SMTP_PORT ){
2b96941… drh 235 p->url.port = iPort;
3fc9b90… drh 236 }
0a201f7… drh 237 if( (smtpFlags & SMTP_DIRECT)!=0 ){
0a201f7… drh 238 int i;
0a201f7… drh 239 p->zHostname = fossil_strdup(zDest);
0a201f7… drh 240 for(i=0; p->zHostname[i] && p->zHostname[i]!=':'; i++){}
0a201f7… drh 241 if( p->zHostname[i]==':' ){
0a201f7… drh 242 p->zHostname[i] = 0;
2b96941… drh 243 p->url.port = atoi(&p->zHostname[i+1]);
0a201f7… drh 244 }
0a201f7… drh 245 }else{
0a201f7… drh 246 p->zHostname = smtp_mx_host(zDest);
0a201f7… drh 247 }
0a201f7… drh 248 if( p->zHostname==0 ){
e6c27d3… drh 249 smtp_set_error(p, 1, "cannot locate SMTP server for \"%s\"", zDest);
0a201f7… drh 250 return p;
0a201f7… drh 251 }
2b96941… drh 252 p->url.name = p->zHostname;
0a201f7… drh 253 socket_global_init();
2b96941… drh 254 p->bOpen = 0;
0a201f7… drh 255 return p;
3fc9b90… drh 256 }
3fc9b90… drh 257
3fc9b90… drh 258 /*
3fc9b90… drh 259 ** Configure debugging options on SmtpSession. Add all bits in
3fc9b90… drh 260 ** smtpFlags to the settings. The following bits can be added:
3fc9b90… drh 261 **
3fc9b90… drh 262 ** SMTP_FLAG_FILE: In which case pArg is the FILE* pointer to use
3fc9b90… drh 263 **
3fc9b90… drh 264 ** SMTP_FLAG_BLOB: In which case pArg is the Blob* poitner to use.
3fc9b90… drh 265 */
3fc9b90… drh 266 void smtp_session_config(SmtpSession *p, u32 smtpFlags, void *pArg){
3fc9b90… drh 267 p->smtpFlags = smtpFlags;
3fc9b90… drh 268 if( smtpFlags & SMTP_TRACE_FILE ){
3fc9b90… drh 269 p->logFile = (FILE*)pArg;
3fc9b90… drh 270 }else if( smtpFlags & SMTP_TRACE_BLOB ){
3fc9b90… drh 271 p->pTranscript = (Blob*)pArg;
3fc9b90… drh 272 }
0a201f7… drh 273 }
0a201f7… drh 274
0a201f7… drh 275 /*
0a201f7… drh 276 ** Send a single line of output the SMTP client to the server.
0a201f7… drh 277 */
0a201f7… drh 278 static void smtp_send_line(SmtpSession *p, const char *zFormat, ...){
0a201f7… drh 279 Blob b = empty_blob;
0a201f7… drh 280 va_list ap;
0a201f7… drh 281 char *z;
0a201f7… drh 282 int n;
2b96941… drh 283 if( !p->bOpen ) return;
0a201f7… drh 284 va_start(ap, zFormat);
0a201f7… drh 285 blob_vappendf(&b, zFormat, ap);
0a201f7… drh 286 va_end(ap);
0a201f7… drh 287 z = blob_buffer(&b);
0a201f7… drh 288 n = blob_size(&b);
0a201f7… drh 289 assert( n>=2 );
0a201f7… drh 290 assert( z[n-1]=='\n' );
0a201f7… drh 291 assert( z[n-2]=='\r' );
0a201f7… drh 292 if( p->smtpFlags & SMTP_TRACE_STDOUT ){
0a201f7… drh 293 fossil_print("C: %.*s\n", n-2, z);
0a201f7… drh 294 }
0a201f7… drh 295 if( p->smtpFlags & SMTP_TRACE_FILE ){
0a201f7… drh 296 fprintf(p->logFile, "C: %.*s\n", n-2, z);
0a201f7… drh 297 }
0a201f7… drh 298 if( p->smtpFlags & SMTP_TRACE_BLOB ){
0a201f7… drh 299 blob_appendf(p->pTranscript, "C: %.*s\n", n-2, z);
0a201f7… drh 300 }
0a201f7… drh 301 socket_send(0, z, n);
0a201f7… drh 302 blob_reset(&b);
0a201f7… drh 303 }
0a201f7… drh 304
0a201f7… drh 305 /*
0a201f7… drh 306 ** Read a line of input received from the SMTP server. Make in point
0a201f7… drh 307 ** to the next input line.
0a201f7… drh 308 **
0a201f7… drh 309 ** Content is actually read into the p->in buffer. Then blob_line()
0a201f7… drh 310 ** is used to extract individual lines, passing each to "in".
0a201f7… drh 311 */
0a201f7… drh 312 static void smtp_recv_line(SmtpSession *p, Blob *in){
0a201f7… drh 313 int n = blob_size(&p->inbuf);
0a201f7… drh 314 char *z = blob_buffer(&p->inbuf);
0a201f7… drh 315 int i = blob_tell(&p->inbuf);
0a201f7… drh 316 int nDelay = 0;
0a201f7… drh 317 if( i<n && z[n-1]=='\n' ){
0a201f7… drh 318 blob_line(&p->inbuf, in);
2b96941… drh 319 }else if( !p->bOpen ){
0a201f7… drh 320 blob_init(in, 0, 0);
0a201f7… drh 321 }else{
0a201f7… drh 322 if( n>0 && i>=n ){
0a201f7… drh 323 blob_truncate(&p->inbuf, 0);
0a201f7… drh 324 blob_rewind(&p->inbuf);
0a201f7… drh 325 n = 0;
0a201f7… drh 326 }
0a201f7… drh 327 do{
0a201f7… drh 328 size_t got;
0a201f7… drh 329 blob_resize(&p->inbuf, n+1000);
0a201f7… drh 330 z = blob_buffer(&p->inbuf);
0a201f7… drh 331 got = socket_receive(0, z+n, 1000, 1);
0a201f7… drh 332 if( got>0 ){
0a201f7… drh 333 in->nUsed += got;
0a201f7… drh 334 n += got;
0a201f7… drh 335 z[n] = 0;
0a201f7… drh 336 if( n>0 && z[n-1]=='\n' ) break;
0a201f7… drh 337 if( got==1000 ) continue;
0a201f7… drh 338 }
0a201f7… drh 339 nDelay++;
0a201f7… drh 340 if( nDelay>100 ){
0a201f7… drh 341 blob_init(in, 0, 0);
e6c27d3… drh 342 smtp_set_error(p, 1, "client times out waiting on server response");
0a201f7… drh 343 return;
0a201f7… drh 344 }else{
0a201f7… drh 345 sqlite3_sleep(100);
0a201f7… drh 346 }
0a201f7… drh 347 }while( n<1 || z[n-1]!='\n' );
0a201f7… drh 348 blob_truncate(&p->inbuf, n);
0a201f7… drh 349 blob_line(&p->inbuf, in);
0a201f7… drh 350 }
0a201f7… drh 351 z = blob_buffer(in);
0a201f7… drh 352 n = blob_size(in);
0a201f7… drh 353 if( n && z[n-1]=='\n' ) n--;
0a201f7… drh 354 if( n && z[n-1]=='\r' ) n--;
0a201f7… drh 355 if( p->smtpFlags & SMTP_TRACE_STDOUT ){
0a201f7… drh 356 fossil_print("S: %.*s\n", n, z);
0a201f7… drh 357 }
0a201f7… drh 358 if( p->smtpFlags & SMTP_TRACE_FILE ){
0a201f7… drh 359 fprintf(p->logFile, "S: %.*s\n", n, z);
0a201f7… drh 360 }
0a201f7… drh 361 if( p->smtpFlags & SMTP_TRACE_BLOB ){
0a201f7… drh 362 blob_appendf(p->pTranscript, "S: %.*s\n", n-2, z);
0a201f7… drh 363 }
0a201f7… drh 364 }
0a201f7… drh 365
0a201f7… drh 366 /*
0a201f7… drh 367 ** Capture a single-line server reply.
0a201f7… drh 368 */
0a201f7… drh 369 static void smtp_get_reply_from_server(
0a201f7… drh 370 SmtpSession *p, /* The SMTP connection */
0a201f7… drh 371 Blob *in, /* Buffer used to hold the reply */
0a201f7… drh 372 int *piCode, /* The return code */
0a201f7… drh 373 int *pbMore, /* True if the reply is not complete */
0a201f7… drh 374 char **pzArg /* Argument */
0a201f7… drh 375 ){
0a201f7… drh 376 int n;
0a201f7… drh 377 char *z;
0a201f7… drh 378 blob_truncate(in, 0);
0a201f7… drh 379 smtp_recv_line(p, in);
2b96941… drh 380 blob_trim(in);
0a201f7… drh 381 z = blob_str(in);
0a201f7… drh 382 n = blob_size(in);
0a201f7… drh 383 if( z[0]=='#' ){
0a201f7… drh 384 *piCode = 0;
0a201f7… drh 385 *pbMore = 1;
0a201f7… drh 386 *pzArg = z;
0a201f7… drh 387 }else{
0a201f7… drh 388 *piCode = atoi(z);
0a201f7… drh 389 *pbMore = n>=4 && z[3]=='-';
0a201f7… drh 390 *pzArg = n>=4 ? z+4 : "";
0a201f7… drh 391 }
0a201f7… drh 392 }
0a201f7… drh 393
0a201f7… drh 394 /*
0a201f7… drh 395 ** Have the client send a QUIT message.
0a201f7… drh 396 */
0a201f7… drh 397 int smtp_client_quit(SmtpSession *p){
0a201f7… drh 398 Blob in = BLOB_INITIALIZER;
0a201f7… drh 399 int iCode = 0;
0a201f7… drh 400 int bMore = 0;
0a201f7… drh 401 char *zArg = 0;
2b96941… drh 402 if( p->bOpen ){
056c83d… drh 403 smtp_send_line(p, "QUIT\r\n");
056c83d… drh 404 do{
056c83d… drh 405 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
056c83d… drh 406 }while( bMore );
2b96941… drh 407 p->bOpen = 0;
2b96941… drh 408 socket_close();
056c83d… drh 409 }
0a201f7… drh 410 return 0;
0a201f7… drh 411 }
0a201f7… drh 412
0a201f7… drh 413 /*
0a201f7… drh 414 ** Begin a client SMTP session. Wait for the initial 220 then send
0a201f7… drh 415 ** the EHLO and wait for a 250.
0a201f7… drh 416 **
0a201f7… drh 417 ** Return 0 on success and non-zero for a failure.
0a201f7… drh 418 */
2b96941… drh 419 static int smtp_client_startup(SmtpSession *p){
0a201f7… drh 420 Blob in = BLOB_INITIALIZER;
0a201f7… drh 421 int iCode = 0;
0a201f7… drh 422 int bMore = 0;
0a201f7… drh 423 char *zArg = 0;
e6c27d3… drh 424 if( p==0 || p->bFatal ) return 1;
2b96941… drh 425 if( socket_open(&p->url) ){
2b96941… drh 426 smtp_set_error(p, 1, "can't open socket: %z", socket_errmsg());
2b96941… drh 427 return 1;
2b96941… drh 428 }
2b96941… drh 429 p->bOpen = 1;
0a201f7… drh 430 do{
0a201f7… drh 431 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 432 }while( bMore );
0a201f7… drh 433 if( iCode!=220 ){
2b96941… drh 434 smtp_set_error(p, 1, "conversation begins with: \"%d %s\"",iCode,zArg);
0a201f7… drh 435 smtp_client_quit(p);
0a201f7… drh 436 return 1;
0a201f7… drh 437 }
0a201f7… drh 438 smtp_send_line(p, "EHLO %s\r\n", p->zFrom);
0a201f7… drh 439 do{
0a201f7… drh 440 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 441 }while( bMore );
0a201f7… drh 442 if( iCode!=250 ){
2b96941… drh 443 smtp_set_error(p, 1, "reply to EHLO with: \"%d %s\"",iCode, zArg);
0a201f7… drh 444 smtp_client_quit(p);
0a201f7… drh 445 return 1;
0a201f7… drh 446 }
2b96941… drh 447 fossil_free(p->zErr);
2b96941… drh 448 p->zErr = 0;
0a201f7… drh 449 return 0;
0a201f7… drh 450 }
0a201f7… drh 451
0a201f7… drh 452 /*
0a201f7… drh 453 ** COMMAND: test-smtp-probe
0a201f7… drh 454 **
0a201f7… drh 455 ** Usage: %fossil test-smtp-probe DOMAIN [ME]
0a201f7… drh 456 **
0a201f7… drh 457 ** Interact with the SMTP server for DOMAIN by setting up a connection
0a201f7… drh 458 ** and then immediately shutting it back down. Log all interaction
0a201f7… drh 459 ** on the console. Use ME as the domain name of the sender.
0a201f7… drh 460 **
0a201f7… drh 461 ** Options:
0a201f7… drh 462 ** --direct Use DOMAIN directly without going through MX
0a201f7… drh 463 ** --port N Talk on TCP port N
0a201f7… drh 464 */
0a201f7… drh 465 void test_smtp_probe(void){
0a201f7… drh 466 SmtpSession *p;
0a201f7… drh 467 const char *zDomain;
0a201f7… drh 468 const char *zSelf;
0a201f7… drh 469 const char *zPort;
0a201f7… drh 470 int iPort = 25;
0a201f7… drh 471 u32 smtpFlags = SMTP_TRACE_STDOUT|SMTP_PORT;
0a201f7… drh 472
0a201f7… drh 473 if( find_option("direct",0,0)!=0 ) smtpFlags |= SMTP_DIRECT;
0a201f7… drh 474 zPort = find_option("port",0,1);
0a201f7… drh 475 if( zPort ) iPort = atoi(zPort);
0a201f7… drh 476 verify_all_options();
0a201f7… drh 477 if( g.argc!=3 && g.argc!=4 ) usage("DOMAIN [ME]");
0a201f7… drh 478 zDomain = g.argv[2];
0a201f7… drh 479 zSelf = g.argc==4 ? g.argv[3] : "fossil-scm.org";
0a201f7… drh 480 p = smtp_session_new(zSelf, zDomain, smtpFlags, iPort);
0a201f7… drh 481 if( p->zErr ){
0a201f7… drh 482 fossil_fatal("%s", p->zErr);
0a201f7… drh 483 }
0a201f7… drh 484 fossil_print("Connection to \"%s\"\n", p->zHostname);
0a201f7… drh 485 smtp_client_startup(p);
0a201f7… drh 486 smtp_client_quit(p);
0a201f7… drh 487 if( p->zErr ){
0a201f7… drh 488 fossil_fatal("ERROR: %s\n", p->zErr);
0a201f7… drh 489 }
0a201f7… drh 490 smtp_session_free(p);
0a201f7… drh 491 }
0a201f7… drh 492
0a201f7… drh 493 /*
0a201f7… drh 494 ** Send the content of an email message followed by a single
0a201f7… drh 495 ** "." line. All lines must be \r\n terminated. Any isolated
0a201f7… drh 496 ** \n line terminators in the input must be converted. Also,
51740ce… wyoung 497 ** a line beginning with "." must have the dot doubled per
51740ce… wyoung 498 ** https://tools.ietf.org/html/rfc5321#section-4.5.2
0a201f7… drh 499 */
0a201f7… drh 500 static void smtp_send_email_body(
0a201f7… drh 501 const char *zMsg, /* Message to send */
0a201f7… drh 502 size_t (*xSend)(void*,const void*,size_t), /* Sender callback function */
0a201f7… drh 503 void *pArg /* First arg to sender */
0a201f7… drh 504 ){
0a201f7… drh 505 Blob in;
0a201f7… drh 506 Blob out = BLOB_INITIALIZER;
0a201f7… drh 507 Blob line;
0a201f7… drh 508 blob_init(&in, zMsg, -1);
0a201f7… drh 509 while( blob_line(&in, &line) ){
0a201f7… drh 510 char *z = blob_buffer(&line);
0a201f7… drh 511 int n = blob_size(&line);
0a201f7… drh 512 if( n==0 ) break;
0a201f7… drh 513 n--;
0a201f7… drh 514 if( n && z[n-1]=='\r' ) n--;
51740ce… wyoung 515 if( z[0]=='.' ){
51740ce… wyoung 516 blob_append(&out, "..", 2); /* RFC 5321 § 4.5.2 */
51740ce… wyoung 517 blob_append(&out, z+1, n-1);
0a201f7… drh 518 }else{
0a201f7… drh 519 blob_append(&out, z, n);
0a201f7… drh 520 }
51740ce… wyoung 521 blob_append(&out, "\r\n", 2);
0a201f7… drh 522 }
0a201f7… drh 523 blob_append(&out, ".\r\n", 3);
0a201f7… drh 524 xSend(pArg, blob_buffer(&out), blob_size(&out));
0a201f7… drh 525 blob_reset(&out);
0a201f7… drh 526 blob_reset(&line);
0a201f7… drh 527 }
0a201f7… drh 528
0a201f7… drh 529 /* A sender function appropriate for use by smtp_send_email_body() to
0a201f7… drh 530 ** send all content to the console, for testing.
0a201f7… drh 531 */
0a201f7… drh 532 static size_t smtp_test_sender(void *NotUsed, const void *pContent, size_t N){
0a201f7… drh 533 return fwrite(pContent, 1, N, stdout);
0a201f7… drh 534 }
0a201f7… drh 535
0a201f7… drh 536 /*
0a201f7… drh 537 ** COMMAND: test-smtp-senddata
0a201f7… drh 538 **
0a201f7… drh 539 ** Usage: %fossil test-smtp-senddata FILE
0a201f7… drh 540 **
0a201f7… drh 541 ** Read content from FILE, then send it to stdout encoded as if sent
0a201f7… drh 542 ** to the DATA portion of an SMTP session. This command is used to
0a201f7… drh 543 ** test the encoding logic.
0a201f7… drh 544 */
0a201f7… drh 545 void test_smtp_senddata(void){
0a201f7… drh 546 Blob f;
0a201f7… drh 547 if( g.argc!=3 ) usage("FILE");
0a201f7… drh 548 blob_read_from_file(&f, g.argv[2], ExtFILE);
0a201f7… drh 549 smtp_send_email_body(blob_str(&f), smtp_test_sender, 0);
0a201f7… drh 550 blob_reset(&f);
0a201f7… drh 551 }
0a201f7… drh 552
0a201f7… drh 553 /*
0a201f7… drh 554 ** Send a single email message to the SMTP server.
0a201f7… drh 555 **
0a201f7… drh 556 ** All email addresses (zFrom and azTo) must be plain "local@domain"
0a201f7… drh 557 ** format without the surrounding "<..>". This routine will add the
0a201f7… drh 558 ** necessary "<..>".
0a201f7… drh 559 **
0a201f7… drh 560 ** The body of the email should be well-structured. This routine will
0a201f7… drh 561 ** convert any \n line endings into \r\n and will escape lines containing
0a201f7… drh 562 ** just ".", but will not make any other alterations or corrections to
0a201f7… drh 563 ** the message content.
0a201f7… drh 564 **
0a201f7… drh 565 ** Return 0 on success. Otherwise an error code.
0a201f7… drh 566 */
0a201f7… drh 567 int smtp_send_msg(
0a201f7… drh 568 SmtpSession *p, /* The SMTP server to which the message is sent */
0a201f7… drh 569 const char *zFrom, /* Who the message is from */
e2bdc10… danield 570 int nTo, /* Number of recipients */
0a201f7… drh 571 const char **azTo, /* Email address of each recipient */
0a201f7… drh 572 const char *zMsg /* Body of the message */
0a201f7… drh 573 ){
0a201f7… drh 574 int i;
0a201f7… drh 575 int iCode = 0;
0a201f7… drh 576 int bMore = 0;
0a201f7… drh 577 char *zArg = 0;
0a201f7… drh 578 Blob in;
0a201f7… drh 579 blob_init(&in, 0, 0);
2b96941… drh 580 if( !p->bOpen ){
2b96941… drh 581 if( !p->bFatal ) smtp_client_startup(p);
2b96941… drh 582 if( !p->bOpen ) return 1;
e6c27d3… drh 583 }
0a201f7… drh 584 smtp_send_line(p, "MAIL FROM:<%s>\r\n", zFrom);
0a201f7… drh 585 do{
0a201f7… drh 586 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 587 }while( bMore );
e6c27d3… drh 588 if( iCode!=250 ){
2b96941… drh 589 smtp_set_error(p, 0,"reply to MAIL FROM: \"%d %s\"",iCode,zArg);
e6c27d3… drh 590 return 1;
e6c27d3… drh 591 }
0a201f7… drh 592 for(i=0; i<nTo; i++){
0a201f7… drh 593 smtp_send_line(p, "RCPT TO:<%s>\r\n", azTo[i]);
0a201f7… drh 594 do{
0a201f7… drh 595 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 596 }while( bMore );
e6c27d3… drh 597 if( iCode!=250 ){
2b96941… drh 598 smtp_set_error(p, 0,"reply to RCPT TO: \"%d %s\"",iCode,zArg);
e6c27d3… drh 599 return 1;
e6c27d3… drh 600 }
0a201f7… drh 601 }
0a201f7… drh 602 smtp_send_line(p, "DATA\r\n");
0a201f7… drh 603 do{
0a201f7… drh 604 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 605 }while( bMore );
e6c27d3… drh 606 if( iCode!=354 ){
2b96941… drh 607 smtp_set_error(p, 0, "reply to DATA with: \"%d %s\"",iCode,zArg);
e6c27d3… drh 608 return 1;
e6c27d3… drh 609 }
0a201f7… drh 610 smtp_send_email_body(zMsg, socket_send, 0);
0a201f7… drh 611 if( p->smtpFlags & SMTP_TRACE_STDOUT ){
0a201f7… drh 612 fossil_print("C: # message content\nC: .\n");
0a201f7… drh 613 }
0a201f7… drh 614 if( p->smtpFlags & SMTP_TRACE_FILE ){
0a201f7… drh 615 fprintf(p->logFile, "C: # message content\nC: .\n");
0a201f7… drh 616 }
0a201f7… drh 617 if( p->smtpFlags & SMTP_TRACE_BLOB ){
0a201f7… drh 618 blob_appendf(p->pTranscript, "C: # message content\nC: .\n");
0a201f7… drh 619 }
0a201f7… drh 620 do{
0a201f7… drh 621 smtp_get_reply_from_server(p, &in, &iCode, &bMore, &zArg);
0a201f7… drh 622 }while( bMore );
e6c27d3… drh 623 if( iCode!=250 ){
2b96941… drh 624 smtp_set_error(p, 0, "reply to end-of-DATA with: \"%d %s\"",
2b96941… drh 625 iCode, zArg);
e6c27d3… drh 626 return 1;
e6c27d3… drh 627 }
0a201f7… drh 628 return 0;
0a201f7… drh 629 }
0a201f7… drh 630
0a201f7… drh 631 /*
0a201f7… drh 632 ** The input is a base email address of the form "local@domain".
e7a5b98… stephan 633 ** Return a pointer to just the "domain" part, or 0 if the string
e7a5b98… stephan 634 ** contains no "@".
0a201f7… drh 635 */
e7a5b98… stephan 636 const char *domain_of_addr(const char *z){
0a201f7… drh 637 while( z[0] && z[0]!='@' ) z++;
0a201f7… drh 638 if( z[0]==0 ) return 0;
0a201f7… drh 639 return z+1;
0a201f7… drh 640 }
0a201f7… drh 641
0a201f7… drh 642
0a201f7… drh 643 /*
0a201f7… drh 644 ** COMMAND: test-smtp-send
0a201f7… drh 645 **
0a201f7… drh 646 ** Usage: %fossil test-smtp-send EMAIL FROM TO ...
0a201f7… drh 647 **
0a201f7… drh 648 ** Use SMTP to send the email message contained in the file named EMAIL
0a201f7… drh 649 ** to the list of users TO. FROM is the sender of the email.
0a201f7… drh 650 **
0a201f7… drh 651 ** Options:
0a201f7… drh 652 ** --direct Go directly to the TO domain. Bypass MX lookup
edcde1f… andybradford 653 ** --relayhost R Use R as relay host directly for delivery.
0a201f7… drh 654 ** --port N Use TCP port N instead of 25
0a201f7… drh 655 ** --trace Show the SMTP conversation on the console
0a201f7… drh 656 */
0a201f7… drh 657 void test_smtp_send(void){
0a201f7… drh 658 SmtpSession *p;
0a201f7… drh 659 const char *zFrom;
0a201f7… drh 660 int nTo;
0a201f7… drh 661 const char *zToDomain;
0a201f7… drh 662 const char *zFromDomain;
c668444… andybradford 663 const char *zRelay;
0a201f7… drh 664 const char **azTo;
0a201f7… drh 665 int smtpPort = 25;
0a201f7… drh 666 const char *zPort;
0a201f7… drh 667 Blob body;
0a201f7… drh 668 u32 smtpFlags = SMTP_PORT;
0a201f7… drh 669 if( find_option("trace",0,0)!=0 ) smtpFlags |= SMTP_TRACE_STDOUT;
0a201f7… drh 670 if( find_option("direct",0,0)!=0 ) smtpFlags |= SMTP_DIRECT;
0a201f7… drh 671 zPort = find_option("port",0,1);
0a201f7… drh 672 if( zPort ) smtpPort = atoi(zPort);
c668444… andybradford 673 zRelay = find_option("relayhost",0,1);
0a201f7… drh 674 verify_all_options();
0a201f7… drh 675 if( g.argc<5 ) usage("EMAIL FROM TO ...");
0a201f7… drh 676 blob_read_from_file(&body, g.argv[2], ExtFILE);
0a201f7… drh 677 zFrom = g.argv[3];
0a201f7… drh 678 nTo = g.argc-4;
0a201f7… drh 679 azTo = (const char**)g.argv+4;
e7a5b98… stephan 680 zFromDomain = domain_of_addr(zFrom);
c668444… andybradford 681 if( zRelay!=0 && zRelay[0]!= 0) {
c668444… andybradford 682 smtpFlags |= SMTP_DIRECT;
c668444… andybradford 683 zToDomain = zRelay;
c668444… andybradford 684 }else{
e7a5b98… stephan 685 zToDomain = domain_of_addr(azTo[0]);
c668444… andybradford 686 }
0a201f7… drh 687 p = smtp_session_new(zFromDomain, zToDomain, smtpFlags, smtpPort);
0a201f7… drh 688 if( p->zErr ){
0a201f7… drh 689 fossil_fatal("%s", p->zErr);
0a201f7… drh 690 }
0a201f7… drh 691 fossil_print("Connection to \"%s\"\n", p->zHostname);
2b96941… drh 692 smtp_send_msg(p, zFrom, nTo, azTo, blob_str(&body));
0a201f7… drh 693 smtp_client_quit(p);
0a201f7… drh 694 if( p->zErr ){
0a201f7… drh 695 fossil_fatal("ERROR: %s\n", p->zErr);
0a201f7… drh 696 }
0a201f7… drh 697 smtp_session_free(p);
0a201f7… drh 698 blob_reset(&body);
0a201f7… drh 699 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button