14549Seric # include "sendmail.h" 24549Seric 35181Seric # ifndef SMTP 4*6057Seric SCCSID(@(#)srvrsmtp.c 3.14 03/06/82 (no SMTP)); 55181Seric # else SMTP 64556Seric 7*6057Seric SCCSID(@(#)srvrsmtp.c 3.14 03/06/82); 85181Seric 94549Seric /* 104549Seric ** SMTP -- run the SMTP protocol. 114549Seric ** 124549Seric ** Parameters: 134549Seric ** none. 144549Seric ** 154549Seric ** Returns: 164549Seric ** never. 174549Seric ** 184549Seric ** Side Effects: 194549Seric ** Reads commands from the input channel and processes 204549Seric ** them. 214549Seric */ 224549Seric 234549Seric struct cmd 244549Seric { 254549Seric char *cmdname; /* command name */ 264549Seric int cmdcode; /* internal code, see below */ 274549Seric }; 284549Seric 294549Seric /* values for cmdcode */ 304549Seric # define CMDERROR 0 /* bad command */ 314549Seric # define CMDMAIL 1 /* mail -- designate sender */ 324976Seric # define CMDRCPT 2 /* rcpt -- designate recipient */ 334549Seric # define CMDDATA 3 /* data -- send message text */ 344549Seric # define CMDRSET 5 /* rset -- reset state */ 354549Seric # define CMDVRFY 6 /* vrfy -- verify address */ 364549Seric # define CMDHELP 7 /* help -- give usage info */ 374549Seric # define CMDNOOP 8 /* noop -- do nothing */ 384549Seric # define CMDQUIT 9 /* quit -- close connection and die */ 394577Seric # define CMDMRSQ 10 /* mrsq -- for old mtp compat only */ 404976Seric # define CMDHELO 11 /* helo -- be polite */ 415003Seric # define CMDDBGSHOWQ 12 /* showq -- show send queue (DEBUG) */ 424549Seric 434549Seric static struct cmd CmdTab[] = 444549Seric { 454549Seric "mail", CMDMAIL, 464976Seric "rcpt", CMDRCPT, 474976Seric "mrcp", CMDRCPT, /* for old MTP compatability */ 484549Seric "data", CMDDATA, 494549Seric "rset", CMDRSET, 504549Seric "vrfy", CMDVRFY, 514549Seric "help", CMDHELP, 524549Seric "noop", CMDNOOP, 534549Seric "quit", CMDQUIT, 544577Seric "mrsq", CMDMRSQ, 554976Seric "helo", CMDHELO, 565003Seric # ifdef DEBUG 575003Seric "showq", CMDDBGSHOWQ, 585003Seric # endif DEBUG 594549Seric NULL, CMDERROR, 604549Seric }; 614549Seric 624549Seric smtp() 634549Seric { 644549Seric char inp[MAXLINE]; 654549Seric register char *p; 664549Seric struct cmd *c; 674549Seric char *cmd; 684549Seric extern char *skipword(); 694549Seric extern bool sameword(); 704549Seric bool hasmail; /* mail command received */ 714713Seric int rcps; /* number of recipients */ 725003Seric auto ADDRESS *vrfyqueue; 734549Seric 745003Seric hasmail = FALSE; 754713Seric rcps = 0; 764549Seric message("220", "%s Sendmail at your service", HostName); 774549Seric for (;;) 784549Seric { 794549Seric To = NULL; 804577Seric Errors = 0; 814549Seric if (fgets(inp, sizeof inp, InChannel) == NULL) 824549Seric { 834549Seric /* end of file, just die */ 844558Seric message("421", "%s Lost input channel", HostName); 854549Seric finis(); 864549Seric } 874549Seric 884549Seric /* clean up end of line */ 894558Seric fixcrlf(inp, TRUE); 904549Seric 914713Seric /* echo command to transcript */ 924713Seric fprintf(Xscript, "*** %s\n", inp); 934713Seric 944549Seric /* break off command */ 954549Seric for (p = inp; isspace(*p); p++) 964549Seric continue; 974549Seric cmd = p; 984549Seric while (*++p != '\0' && !isspace(*p)) 994549Seric continue; 1004549Seric if (*p != '\0') 1014549Seric *p++ = '\0'; 1024549Seric 1034549Seric /* decode command */ 1044549Seric for (c = CmdTab; c->cmdname != NULL; c++) 1054549Seric { 1064549Seric if (sameword(c->cmdname, cmd)) 1074549Seric break; 1084549Seric } 1094549Seric 1104549Seric /* process command */ 1114549Seric switch (c->cmdcode) 1124549Seric { 1134976Seric case CMDHELO: /* hello -- introduce yourself */ 1145187Seric define('s', newstr(p)); 1154997Seric message("250", "%s Hello %s, pleased to meet you", 1164997Seric HostName, p); 1174976Seric break; 1184976Seric 1194549Seric case CMDMAIL: /* mail -- designate sender */ 1204558Seric if (hasmail) 1214558Seric { 1224558Seric message("503", "Sender already specified"); 1234558Seric break; 1244558Seric } 1254549Seric p = skipword(p, "from"); 1264549Seric if (p == NULL) 1274549Seric break; 1284549Seric if (index(p, ',') != NULL) 1294549Seric { 1304549Seric message("501", "Source routing not implemented"); 1314549Seric Errors++; 1324549Seric break; 1334549Seric } 1344549Seric setsender(p); 1354577Seric if (Errors == 0) 1364549Seric { 1374549Seric message("250", "Sender ok"); 1384549Seric hasmail = TRUE; 1394549Seric } 1404549Seric break; 1414549Seric 1424976Seric case CMDRCPT: /* rcpt -- designate recipient */ 1434549Seric p = skipword(p, "to"); 1444549Seric if (p == NULL) 1454549Seric break; 1464549Seric if (index(p, ',') != NULL) 1474549Seric { 1484549Seric message("501", "Source routing not implemented"); 1494549Seric Errors++; 1504549Seric break; 1514549Seric } 1525003Seric sendto(p, 1, (ADDRESS *) NULL, &SendQueue); 1534577Seric if (Errors == 0) 1544549Seric { 155*6057Seric message("250", "%s... Recipient ok", p); 1564713Seric rcps++; 1574549Seric } 1584549Seric break; 1594549Seric 1604549Seric case CMDDATA: /* data -- text of mail */ 1614976Seric if (!hasmail) 1624549Seric { 1634976Seric message("503", "Need MAIL command"); 1644976Seric break; 1654549Seric } 1664713Seric else if (rcps <= 0) 1674549Seric { 1684976Seric message("503", "Need RCPT (recipient)"); 1694976Seric break; 1704549Seric } 1714976Seric 1724976Seric /* collect the text of the message */ 1734976Seric collect(TRUE); 1744976Seric if (Errors != 0) 1754976Seric break; 1764976Seric 1774976Seric /* if sending to multiple people, mail back errors */ 1784976Seric if (rcps != 1) 1794976Seric HoldErrs = MailBack = TRUE; 1804976Seric 1814976Seric /* send to all recipients */ 1824976Seric sendall(FALSE); 1834976Seric 1844976Seric /* reset strange modes */ 1854976Seric HoldErrs = FALSE; 1864976Seric To = NULL; 1874976Seric 1884976Seric /* issue success if appropriate */ 1894976Seric if (Errors == 0 || rcps != 1) 1904976Seric message("250", "Sent"); 1914549Seric break; 1924549Seric 1934549Seric case CMDRSET: /* rset -- reset state */ 1944549Seric message("250", "Reset state"); 1954549Seric finis(); 1964549Seric 1974549Seric case CMDVRFY: /* vrfy -- verify address */ 1985003Seric vrfyqueue = NULL; 1995003Seric sendto(p, 1, (ADDRESS *) NULL, &vrfyqueue); 2005003Seric while (vrfyqueue != NULL) 2015003Seric { 2025003Seric register ADDRESS *a = vrfyqueue->q_next; 2035003Seric char *code; 2045003Seric 2055003Seric while (a != NULL && bitset(QDONTSEND, a->q_flags)) 2065003Seric a = a->q_next; 2075003Seric 2085003Seric if (!bitset(QDONTSEND, vrfyqueue->q_flags)) 2095003Seric { 2105003Seric if (a != NULL) 2115003Seric code = "250-"; 2125003Seric else 2135003Seric code = "250"; 2145003Seric if (vrfyqueue->q_fullname == NULL) 2155003Seric message(code, "<%s>", vrfyqueue->q_paddr); 2165003Seric else 2175003Seric message(code, "%s <%s>", 2185003Seric vrfyqueue->q_fullname, vrfyqueue->q_paddr); 2195003Seric } 2205003Seric else if (a == NULL) 2215003Seric message("554", "Self destructive alias loop"); 2225003Seric vrfyqueue = a; 2235003Seric } 2244549Seric break; 2254549Seric 2264549Seric case CMDHELP: /* help -- give user info */ 2274577Seric if (*p == '\0') 2284577Seric p = "SMTP"; 2294577Seric help(p); 2304549Seric break; 2314549Seric 2324549Seric case CMDNOOP: /* noop -- do nothing */ 2334549Seric message("200", "OK"); 2344549Seric break; 2354549Seric 2364549Seric case CMDQUIT: /* quit -- leave mail */ 2374549Seric message("221", "%s closing connection", HostName); 2384549Seric finis(); 2394549Seric 2404577Seric case CMDMRSQ: /* mrsq -- negotiate protocol */ 2414577Seric if (*p == 'R' || *p == 'T') 2424577Seric { 2434577Seric /* recipients first or text first */ 2444577Seric message("200", "%c ok, please continue", *p); 2454577Seric } 2464577Seric else if (*p == '?') 2474577Seric { 2484577Seric /* what do I prefer? anything, anytime */ 2494577Seric message("215", "R Recipients first is my choice"); 2504577Seric } 2514577Seric else if (*p == '\0') 2524577Seric { 2534577Seric /* no meaningful scheme */ 2544577Seric message("200", "okey dokie boobie"); 2554577Seric } 2564577Seric else 2574577Seric { 2584577Seric /* bad argument */ 2594577Seric message("504", "Scheme unknown"); 2604577Seric } 2614577Seric break; 2624577Seric 2635003Seric # ifdef DEBUG 2645003Seric case CMDDBGSHOWQ: /* show queues */ 2655003Seric printf("SendQueue="); 2665003Seric printaddr(SendQueue, TRUE); 2675003Seric break; 2685003Seric # endif DEBUG 2695003Seric 2704549Seric case CMDERROR: /* unknown command */ 2714549Seric message("500", "Command unrecognized"); 2724549Seric break; 2734549Seric 2744549Seric default: 2754549Seric syserr("smtp: unknown code %d", c->cmdcode); 2764549Seric break; 2774549Seric } 2784549Seric } 2794549Seric } 2804549Seric /* 2814549Seric ** SKIPWORD -- skip a fixed word. 2824549Seric ** 2834549Seric ** Parameters: 2844549Seric ** p -- place to start looking. 2854549Seric ** w -- word to skip. 2864549Seric ** 2874549Seric ** Returns: 2884549Seric ** p following w. 2894549Seric ** NULL on error. 2904549Seric ** 2914549Seric ** Side Effects: 2924549Seric ** clobbers the p data area. 2934549Seric */ 2944549Seric 2954549Seric static char * 2964549Seric skipword(p, w) 2974549Seric register char *p; 2984549Seric char *w; 2994549Seric { 3004549Seric register char *q; 3014549Seric extern bool sameword(); 3024549Seric 3034549Seric /* find beginning of word */ 3044549Seric while (isspace(*p)) 3054549Seric p++; 3064549Seric q = p; 3074549Seric 3084549Seric /* find end of word */ 3094549Seric while (*p != '\0' && *p != ':' && !isspace(*p)) 3104549Seric p++; 3114549Seric while (isspace(*p)) 3124549Seric *p++ = '\0'; 3134549Seric if (*p != ':') 3144549Seric { 3154549Seric syntax: 3164549Seric message("501", "Syntax error"); 3174549Seric Errors++; 3184549Seric return (NULL); 3194549Seric } 3204549Seric *p++ = '\0'; 3214549Seric while (isspace(*p)) 3224549Seric p++; 3234549Seric 3244549Seric /* see if the input word matches desired word */ 3254549Seric if (!sameword(q, w)) 3264549Seric goto syntax; 3274549Seric 3284549Seric return (p); 3294549Seric } 3304577Seric /* 3314577Seric ** HELP -- implement the HELP command. 3324577Seric ** 3334577Seric ** Parameters: 3344577Seric ** topic -- the topic we want help for. 3354577Seric ** 3364577Seric ** Returns: 3374577Seric ** none. 3384577Seric ** 3394577Seric ** Side Effects: 3404577Seric ** outputs the help file to message output. 3414577Seric */ 3424577Seric 3434577Seric help(topic) 3444577Seric char *topic; 3454577Seric { 3464577Seric register FILE *hf; 3474577Seric int len; 3484577Seric char buf[MAXLINE]; 3494577Seric bool noinfo; 3504582Seric extern char *HelpFile; 3514577Seric 3524582Seric hf = fopen(HelpFile, "r"); 3534577Seric if (hf == NULL) 3544577Seric { 3554577Seric /* no help */ 3564577Seric message("502", "HELP not implemented"); 3574577Seric return; 3584577Seric } 3594577Seric 3604577Seric len = strlen(topic); 3614577Seric makelower(topic); 3624577Seric noinfo = TRUE; 3634577Seric 3644577Seric while (fgets(buf, sizeof buf, hf) != NULL) 3654577Seric { 3664577Seric if (strncmp(buf, topic, len) == 0) 3674577Seric { 3684577Seric register char *p; 3694577Seric 3704577Seric p = index(buf, '\t'); 3714577Seric if (p == NULL) 3724577Seric p = buf; 3734577Seric else 3744577Seric p++; 3754577Seric fixcrlf(p, TRUE); 3764577Seric message("214-", p); 3774577Seric noinfo = FALSE; 3784577Seric } 3794577Seric } 3804577Seric 3814577Seric if (noinfo) 3824577Seric message("504", "HELP topic unknown"); 3834577Seric else 3844577Seric message("214", "End of HELP info"); 3854628Seric (void) fclose(hf); 3864577Seric } 3875181Seric 3885181Seric # endif SMTP 389