14796Seric # include <ctype.h> 24684Seric # include <sysexits.h> 34865Seric # include "sendmail.h" 44684Seric 55182Seric # ifndef SMTP 6*10148Seric SCCSID(@(#)usersmtp.c 3.34 01/05/83 (no SMTP)); 75182Seric # else SMTP 84684Seric 9*10148Seric SCCSID(@(#)usersmtp.c 3.34 01/05/83); 105182Seric 119391Seric 129391Seric 134684Seric /* 149391Seric ** USERSMTP -- run SMTP protocol from the user end. 159391Seric ** 169391Seric ** This protocol is described in RFC821. 179391Seric */ 189391Seric 199391Seric #define REPLYTYPE(r) ((r) / 100) /* first digit of reply code */ 209391Seric #define REPLYCLASS(r) (((r) / 10) % 10) /* second digit of reply code */ 219391Seric #define SMTPCLOSING 421 /* "Service Shutting Down" */ 229391Seric 2310054Seric char SmtpReplyBuffer[MAXLINE]; /* buffer for replies */ 2410054Seric FILE *SmtpOut; /* output file */ 2510054Seric FILE *SmtpIn; /* input file */ 2610054Seric int SmtpPid; /* pid of mailer */ 2710054Seric bool SmtpClosing; /* set on a forced close */ 289391Seric /* 294865Seric ** SMTPINIT -- initialize SMTP. 304684Seric ** 314865Seric ** Opens the connection and sends the initial protocol. 324684Seric ** 334684Seric ** Parameters: 344865Seric ** m -- mailer to create connection to. 354865Seric ** pvp -- pointer to parameter vector to pass to 364865Seric ** the mailer. 374865Seric ** ctladdr -- controlling address for this mailer. 384684Seric ** 394684Seric ** Returns: 404865Seric ** appropriate exit status -- EX_OK on success. 414684Seric ** 424684Seric ** Side Effects: 434865Seric ** creates connection and sends initial protocol. 444684Seric */ 454684Seric 464865Seric smtpinit(m, pvp, ctladdr) 474865Seric struct mailer *m; 484865Seric char **pvp; 494865Seric ADDRESS *ctladdr; 504684Seric { 514865Seric register int r; 524865Seric char buf[MAXNAME]; 537685Seric extern char *canonname(); 544684Seric 554865Seric /* 564865Seric ** Open the connection to the mailer. 574865Seric */ 584684Seric 596051Seric SmtpIn = SmtpOut = NULL; 60*10148Seric SmtpClosing = FALSE; 614865Seric SmtpPid = openmailer(m, pvp, ctladdr, TRUE, &SmtpOut, &SmtpIn); 626051Seric if (SmtpPid < 0) 636051Seric { 646051Seric # ifdef DEBUG 657677Seric if (tTd(18, 1)) 669391Seric printf("smtpinit: cannot open %s: stat %d errno %d\n", 679391Seric pvp[0], ExitStat, errno); 686051Seric # endif DEBUG 696051Seric return (ExitStat); 706051Seric } 714796Seric 724865Seric /* 734865Seric ** Get the greeting message. 744865Seric ** This should appear spontaneously. 754865Seric */ 764797Seric 774865Seric r = reply(); 788005Seric if (r < 0 || REPLYTYPE(r) != 2) 794865Seric return (EX_TEMPFAIL); 804684Seric 814865Seric /* 824976Seric ** Send the HELO command. 837963Seric ** My mother taught me to always introduce myself. 844976Seric */ 854976Seric 864976Seric smtpmessage("HELO %s", HostName); 874976Seric r = reply(); 888005Seric if (r < 0) 898005Seric return (EX_TEMPFAIL); 908005Seric else if (REPLYTYPE(r) == 5) 914976Seric return (EX_UNAVAILABLE); 927963Seric else if (REPLYTYPE(r) != 2) 934976Seric return (EX_TEMPFAIL); 944976Seric 954976Seric /* 969315Seric ** If this is expected to be another sendmail, send some internal 979315Seric ** commands. 989315Seric */ 999315Seric 1009315Seric if (bitset(M_INTERNAL, m->m_flags)) 1019315Seric { 1029315Seric /* tell it to be verbose */ 1039315Seric smtpmessage("VERB"); 1049315Seric r = reply(); 1059315Seric if (r < 0) 1069315Seric return (EX_TEMPFAIL); 1079315Seric 1089315Seric /* tell it we will be sending one transaction only */ 1099315Seric smtpmessage("ONEX"); 1109315Seric r = reply(); 1119315Seric if (r < 0) 1129315Seric return (EX_TEMPFAIL); 1139315Seric } 1149315Seric 1159315Seric /* 1164865Seric ** Send the MAIL command. 1174865Seric ** Designates the sender. 1184865Seric */ 1194796Seric 1206980Seric expand("$g", buf, &buf[sizeof buf - 1], CurEnv); 1218436Seric if (CurEnv->e_from.q_mailer == LocalMailer || 1228436Seric !bitset(M_FULLSMTP, m->m_flags)) 1238436Seric { 1248436Seric smtpmessage("MAIL From:<%s>", canonname(buf, 1)); 1258436Seric } 1268436Seric else 1278436Seric { 1288436Seric smtpmessage("MAIL From:<@%s%c%s>", HostName, 1298436Seric buf[0] == '@' ? ',' : ':', canonname(buf, 1)); 1308436Seric } 1314865Seric r = reply(); 1328005Seric if (r < 0 || REPLYTYPE(r) == 4) 1334865Seric return (EX_TEMPFAIL); 1347963Seric else if (r == 250) 1357963Seric return (EX_OK); 1367963Seric else if (r == 552) 1377963Seric return (EX_UNAVAILABLE); 1387964Seric return (EX_PROTOCOL); 1394684Seric } 1404684Seric /* 1414976Seric ** SMTPRCPT -- designate recipient. 1424797Seric ** 1434797Seric ** Parameters: 1444865Seric ** to -- address of recipient. 1454797Seric ** 1464797Seric ** Returns: 1474865Seric ** exit status corresponding to recipient status. 1484797Seric ** 1494797Seric ** Side Effects: 1504865Seric ** Sends the mail via SMTP. 1514797Seric */ 1524797Seric 1534976Seric smtprcpt(to) 1544865Seric ADDRESS *to; 1554797Seric { 1564797Seric register int r; 1577685Seric extern char *canonname(); 1584797Seric 1598354Seric smtpmessage("RCPT To:<%s>", canonname(to->q_user, 2)); 1604865Seric 1614797Seric r = reply(); 1628005Seric if (r < 0 || REPLYTYPE(r) == 4) 1634865Seric return (EX_TEMPFAIL); 1647963Seric else if (REPLYTYPE(r) == 2) 1657963Seric return (EX_OK); 1667964Seric else if (r == 550 || r == 551 || r == 553) 1677964Seric return (EX_NOUSER); 1687964Seric else if (r == 552 || r == 554) 1697964Seric return (EX_UNAVAILABLE); 1707964Seric return (EX_PROTOCOL); 1714797Seric } 1724797Seric /* 1734865Seric ** SMTPFINISH -- finish up sending all the SMTP protocol. 1744684Seric ** 1754684Seric ** Parameters: 1764865Seric ** m -- mailer being sent to. 1776980Seric ** e -- the envelope for this message. 1784684Seric ** 1794684Seric ** Returns: 1804976Seric ** exit status corresponding to DATA command. 1814684Seric ** 1824684Seric ** Side Effects: 1834865Seric ** none. 1844684Seric */ 1854684Seric 1866980Seric smtpfinish(m, e) 1874865Seric struct mailer *m; 1886980Seric register ENVELOPE *e; 1894684Seric { 1904684Seric register int r; 1914684Seric 1924797Seric /* 1934797Seric ** Send the data. 1944797Seric ** Dot hiding is done here. 1954797Seric */ 1964797Seric 1974865Seric smtpmessage("DATA"); 1984796Seric r = reply(); 1998005Seric if (r < 0 || REPLYTYPE(r) == 4) 2004797Seric return (EX_TEMPFAIL); 2017963Seric else if (r == 554) 2027963Seric return (EX_UNAVAILABLE); 2037963Seric else if (r != 354) 2047964Seric return (EX_PROTOCOL); 20510067Seric (*e->e_puthdr)(SmtpOut, m, CurEnv, TRUE); 20610067Seric fprintf(SmtpOut, "\r\n"); 20710067Seric (*e->e_putbody)(SmtpOut, m, TRUE, CurEnv, TRUE); 2084865Seric smtpmessage("."); 2094796Seric r = reply(); 2108005Seric if (r < 0 || REPLYTYPE(r) == 4) 2114797Seric return (EX_TEMPFAIL); 2127963Seric else if (r == 250) 2137963Seric return (EX_OK); 2147963Seric else if (r == 552 || r == 554) 2157963Seric return (EX_UNAVAILABLE); 2167964Seric return (EX_PROTOCOL); 2174684Seric } 2184684Seric /* 2194865Seric ** SMTPQUIT -- close the SMTP connection. 2204865Seric ** 2214865Seric ** Parameters: 2224865Seric ** name -- name of mailer we are quitting. 2234865Seric ** 2244865Seric ** Returns: 2254865Seric ** none. 2264865Seric ** 2274865Seric ** Side Effects: 2284865Seric ** sends the final protocol and closes the connection. 2294865Seric */ 2304865Seric 2319391Seric smtpquit(name) 2324865Seric char *name; 2334865Seric { 2349391Seric int i; 2354865Seric 236*10148Seric /* if the connection is already closed, don't bother */ 237*10148Seric if (SmtpIn == NULL) 238*10148Seric return; 239*10148Seric 240*10148Seric /* send the quit message if not a forced quit */ 241*10148Seric if (!SmtpClosing) 2429391Seric { 243*10148Seric smtpmessage("QUIT"); 244*10148Seric (void) reply(); 2459391Seric } 2469391Seric 247*10148Seric /* now actually close the connection */ 2487229Seric (void) fclose(SmtpIn); 2497229Seric (void) fclose(SmtpOut); 250*10148Seric SmtpIn = SmtpOut = NULL; 251*10148Seric 252*10148Seric /* and pick up the zombie */ 2537229Seric i = endmailer(SmtpPid, name); 2549391Seric if (i != EX_OK) 2559391Seric syserr("smtpquit %s: stat %d", name, i); 2564865Seric } 2574865Seric /* 2584684Seric ** REPLY -- read arpanet reply 2594684Seric ** 2604684Seric ** Parameters: 2614796Seric ** none. 2624684Seric ** 2634684Seric ** Returns: 2644684Seric ** reply code it reads. 2654684Seric ** 2664684Seric ** Side Effects: 2674684Seric ** flushes the mail file. 2684684Seric */ 2694684Seric 2704796Seric reply() 2714684Seric { 2724865Seric (void) fflush(SmtpOut); 2734684Seric 2747677Seric if (tTd(18, 1)) 2754796Seric printf("reply\n"); 2764796Seric 2777356Seric /* 2787356Seric ** Read the input line, being careful not to hang. 2797356Seric */ 2807356Seric 2814684Seric for (;;) 2824684Seric { 2834684Seric register int r; 2847356Seric register char *p; 2854684Seric 2867685Seric /* actually do the read */ 2879547Seric if (CurEnv->e_xfp != NULL) 2889547Seric (void) fflush(CurEnv->e_xfp); /* for debugging */ 2897356Seric 29010054Seric /* if we are in the process of closing just give the code */ 29110054Seric if (SmtpClosing) 29210054Seric return (SMTPCLOSING); 29310054Seric 29410054Seric /* get the line from the other side */ 29510054Seric p = sfgets(SmtpReplyBuffer, sizeof SmtpReplyBuffer, SmtpIn); 29610054Seric if (p == NULL) 29710131Seric { 298*10148Seric extern char MsgBuf[]; /* err.c */ 299*10148Seric extern char Arpa_TSyserr[]; /* conf.c */ 300*10148Seric 301*10148Seric message(Arpa_TSyserr, "reply: read error"); 302*10148Seric # ifdef LOG 303*10148Seric syslog(LOG_ERR, "%s", &MsgBuf[4]); 304*10148Seric # endif LOG 305*10148Seric SmtpClosing = TRUE; 306*10148Seric smtpquit("reply error"); 30710054Seric return (-1); 30810131Seric } 30910054Seric fixcrlf(SmtpReplyBuffer, TRUE); 31010054Seric 3117356Seric /* log the input in the transcript for future error returns */ 3127229Seric if (Verbose && !HoldErrs) 3139391Seric nmessage(Arpa_Info, "%s", SmtpReplyBuffer); 31410131Seric else if (CurEnv->e_xfp != NULL) 3159547Seric fprintf(CurEnv->e_xfp, "%s\n", SmtpReplyBuffer); 3167356Seric 3177356Seric /* if continuation is required, we can go on */ 3189391Seric if (SmtpReplyBuffer[3] == '-' || !isdigit(SmtpReplyBuffer[0])) 3194684Seric continue; 3207356Seric 3217356Seric /* decode the reply code */ 3229391Seric r = atoi(SmtpReplyBuffer); 3237356Seric 3247356Seric /* extra semantics: 0xx codes are "informational" */ 3254684Seric if (r < 100) 3264684Seric continue; 3277356Seric 3289391Seric /* reply code 421 is "Service Shutting Down" */ 32910054Seric if (r == SMTPCLOSING) 3309391Seric { 33110054Seric /* send the quit protocol */ 3329391Seric smtpquit("SMTP Shutdown"); 3339391Seric SmtpClosing = TRUE; 3349391Seric } 3359391Seric 3364684Seric return (r); 3374684Seric } 3384684Seric } 3394796Seric /* 3404865Seric ** SMTPMESSAGE -- send message to server 3414796Seric ** 3424796Seric ** Parameters: 3434796Seric ** f -- format 3444796Seric ** a, b, c -- parameters 3454796Seric ** 3464796Seric ** Returns: 3474796Seric ** none. 3484796Seric ** 3494796Seric ** Side Effects: 3504865Seric ** writes message to SmtpOut. 3514796Seric */ 3524796Seric 3534865Seric /*VARARGS1*/ 3544865Seric smtpmessage(f, a, b, c) 3554796Seric char *f; 3564796Seric { 3574796Seric char buf[100]; 3584796Seric 3594865Seric (void) sprintf(buf, f, a, b, c); 3607677Seric if (tTd(18, 1) || (Verbose && !HoldErrs)) 3618237Seric nmessage(Arpa_Info, ">>> %s", buf); 36210131Seric else if (CurEnv->e_xfp != NULL) 3639547Seric fprintf(CurEnv->e_xfp, ">>> %s\n", buf); 3649391Seric if (!SmtpClosing) 3659391Seric fprintf(SmtpOut, "%s\r\n", buf); 3664796Seric } 3675182Seric 3685182Seric # endif SMTP 369