14549Seric # include "sendmail.h" 24549Seric 3*4997Seric static char SccsId[] = "@(#)srvrsmtp.c 3.9 11/21/81"; 44556Seric 54549Seric /* 64549Seric ** SMTP -- run the SMTP protocol. 74549Seric ** 84549Seric ** Parameters: 94549Seric ** none. 104549Seric ** 114549Seric ** Returns: 124549Seric ** never. 134549Seric ** 144549Seric ** Side Effects: 154549Seric ** Reads commands from the input channel and processes 164549Seric ** them. 174549Seric */ 184549Seric 194549Seric struct cmd 204549Seric { 214549Seric char *cmdname; /* command name */ 224549Seric int cmdcode; /* internal code, see below */ 234549Seric }; 244549Seric 254549Seric /* values for cmdcode */ 264549Seric # define CMDERROR 0 /* bad command */ 274549Seric # define CMDMAIL 1 /* mail -- designate sender */ 284976Seric # define CMDRCPT 2 /* rcpt -- designate recipient */ 294549Seric # define CMDDATA 3 /* data -- send message text */ 304549Seric # define CMDRSET 5 /* rset -- reset state */ 314549Seric # define CMDVRFY 6 /* vrfy -- verify address */ 324549Seric # define CMDHELP 7 /* help -- give usage info */ 334549Seric # define CMDNOOP 8 /* noop -- do nothing */ 344549Seric # define CMDQUIT 9 /* quit -- close connection and die */ 354577Seric # define CMDMRSQ 10 /* mrsq -- for old mtp compat only */ 364976Seric # define CMDHELO 11 /* helo -- be polite */ 374549Seric 384549Seric static struct cmd CmdTab[] = 394549Seric { 404549Seric "mail", CMDMAIL, 414976Seric "rcpt", CMDRCPT, 424976Seric "mrcp", CMDRCPT, /* for old MTP compatability */ 434549Seric "data", CMDDATA, 444549Seric "rset", CMDRSET, 454549Seric "vrfy", CMDVRFY, 464549Seric "help", CMDHELP, 474549Seric "noop", CMDNOOP, 484549Seric "quit", CMDQUIT, 494577Seric "mrsq", CMDMRSQ, 504976Seric "helo", CMDHELO, 514549Seric NULL, CMDERROR, 524549Seric }; 534549Seric 544549Seric smtp() 554549Seric { 564549Seric char inp[MAXLINE]; 574549Seric register char *p; 584549Seric struct cmd *c; 594549Seric char *cmd; 604549Seric extern char *skipword(); 614549Seric extern bool sameword(); 624549Seric bool hasmail; /* mail command received */ 634713Seric int rcps; /* number of recipients */ 64*4997Seric bool hasdata; /* has mail data */ 654549Seric 66*4997Seric hasmail = hasdata = FALSE; 674713Seric rcps = 0; 684549Seric message("220", "%s Sendmail at your service", HostName); 694549Seric for (;;) 704549Seric { 714549Seric To = NULL; 724577Seric Errors = 0; 734549Seric if (fgets(inp, sizeof inp, InChannel) == NULL) 744549Seric { 754549Seric /* end of file, just die */ 764558Seric message("421", "%s Lost input channel", HostName); 774549Seric finis(); 784549Seric } 794549Seric 804549Seric /* clean up end of line */ 814558Seric fixcrlf(inp, TRUE); 824549Seric 834713Seric /* echo command to transcript */ 844713Seric fprintf(Xscript, "*** %s\n", inp); 854713Seric 864549Seric /* break off command */ 874549Seric for (p = inp; isspace(*p); p++) 884549Seric continue; 894549Seric cmd = p; 904549Seric while (*++p != '\0' && !isspace(*p)) 914549Seric continue; 924549Seric if (*p != '\0') 934549Seric *p++ = '\0'; 944549Seric 954549Seric /* decode command */ 964549Seric for (c = CmdTab; c->cmdname != NULL; c++) 974549Seric { 984549Seric if (sameword(c->cmdname, cmd)) 994549Seric break; 1004549Seric } 1014549Seric 1024549Seric /* process command */ 1034549Seric switch (c->cmdcode) 1044549Seric { 1054976Seric case CMDHELO: /* hello -- introduce yourself */ 106*4997Seric message("250", "%s Hello %s, pleased to meet you", 107*4997Seric HostName, p); 1084976Seric break; 1094976Seric 1104549Seric case CMDMAIL: /* mail -- designate sender */ 1114558Seric if (hasmail) 1124558Seric { 1134558Seric message("503", "Sender already specified"); 1144558Seric break; 1154558Seric } 1164549Seric p = skipword(p, "from"); 1174549Seric if (p == NULL) 1184549Seric break; 1194549Seric if (index(p, ',') != NULL) 1204549Seric { 1214549Seric message("501", "Source routing not implemented"); 1224549Seric Errors++; 1234549Seric break; 1244549Seric } 1254549Seric setsender(p); 1264577Seric if (Errors == 0) 1274549Seric { 1284549Seric message("250", "Sender ok"); 1294549Seric hasmail = TRUE; 1304549Seric } 1314549Seric break; 1324549Seric 1334976Seric case CMDRCPT: /* rcpt -- designate recipient */ 1344549Seric p = skipword(p, "to"); 1354549Seric if (p == NULL) 1364549Seric break; 1374549Seric if (index(p, ',') != NULL) 1384549Seric { 1394549Seric message("501", "Source routing not implemented"); 1404549Seric Errors++; 1414549Seric break; 1424549Seric } 143*4997Seric sendto(p, 1, (ADDRESS *) NULL); 1444577Seric if (Errors == 0) 1454549Seric { 1464549Seric message("250", "Recipient ok"); 1474713Seric rcps++; 1484549Seric } 1494549Seric break; 1504549Seric 1514549Seric case CMDDATA: /* data -- text of mail */ 1524976Seric if (!hasmail) 1534549Seric { 1544976Seric message("503", "Need MAIL command"); 1554976Seric break; 1564549Seric } 1574713Seric else if (rcps <= 0) 1584549Seric { 1594976Seric message("503", "Need RCPT (recipient)"); 1604976Seric break; 1614549Seric } 1624976Seric 1634976Seric /* collect the text of the message */ 1644976Seric collect(TRUE); 1654976Seric if (Errors != 0) 1664976Seric break; 1674976Seric 1684976Seric /* if sending to multiple people, mail back errors */ 1694976Seric if (rcps != 1) 1704976Seric HoldErrs = MailBack = TRUE; 1714976Seric 1724976Seric /* send to all recipients */ 1734976Seric sendall(FALSE); 1744976Seric 1754976Seric /* reset strange modes */ 1764976Seric HoldErrs = FALSE; 1774976Seric To = NULL; 1784976Seric 1794976Seric /* issue success if appropriate */ 1804976Seric if (Errors == 0 || rcps != 1) 1814976Seric message("250", "Sent"); 1824549Seric break; 1834549Seric 1844549Seric case CMDRSET: /* rset -- reset state */ 1854549Seric message("250", "Reset state"); 1864549Seric finis(); 1874549Seric 1884549Seric case CMDVRFY: /* vrfy -- verify address */ 189*4997Seric message("502", "Command not implemented"); 1904549Seric break; 1914549Seric 1924549Seric case CMDHELP: /* help -- give user info */ 1934577Seric if (*p == '\0') 1944577Seric p = "SMTP"; 1954577Seric help(p); 1964549Seric break; 1974549Seric 1984549Seric case CMDNOOP: /* noop -- do nothing */ 1994549Seric message("200", "OK"); 2004549Seric break; 2014549Seric 2024549Seric case CMDQUIT: /* quit -- leave mail */ 2034549Seric message("221", "%s closing connection", HostName); 2044549Seric finis(); 2054549Seric 2064577Seric case CMDMRSQ: /* mrsq -- negotiate protocol */ 2074577Seric if (*p == 'R' || *p == 'T') 2084577Seric { 2094577Seric /* recipients first or text first */ 2104577Seric message("200", "%c ok, please continue", *p); 2114577Seric } 2124577Seric else if (*p == '?') 2134577Seric { 2144577Seric /* what do I prefer? anything, anytime */ 2154577Seric message("215", "R Recipients first is my choice"); 2164577Seric } 2174577Seric else if (*p == '\0') 2184577Seric { 2194577Seric /* no meaningful scheme */ 2204577Seric message("200", "okey dokie boobie"); 2214577Seric } 2224577Seric else 2234577Seric { 2244577Seric /* bad argument */ 2254577Seric message("504", "Scheme unknown"); 2264577Seric } 2274577Seric break; 2284577Seric 2294549Seric case CMDERROR: /* unknown command */ 2304549Seric message("500", "Command unrecognized"); 2314549Seric break; 2324549Seric 2334549Seric default: 2344549Seric syserr("smtp: unknown code %d", c->cmdcode); 2354549Seric break; 2364549Seric } 2374549Seric } 2384549Seric } 2394549Seric /* 2404549Seric ** SKIPWORD -- skip a fixed word. 2414549Seric ** 2424549Seric ** Parameters: 2434549Seric ** p -- place to start looking. 2444549Seric ** w -- word to skip. 2454549Seric ** 2464549Seric ** Returns: 2474549Seric ** p following w. 2484549Seric ** NULL on error. 2494549Seric ** 2504549Seric ** Side Effects: 2514549Seric ** clobbers the p data area. 2524549Seric */ 2534549Seric 2544549Seric static char * 2554549Seric skipword(p, w) 2564549Seric register char *p; 2574549Seric char *w; 2584549Seric { 2594549Seric register char *q; 2604549Seric extern bool sameword(); 2614549Seric 2624549Seric /* find beginning of word */ 2634549Seric while (isspace(*p)) 2644549Seric p++; 2654549Seric q = p; 2664549Seric 2674549Seric /* find end of word */ 2684549Seric while (*p != '\0' && *p != ':' && !isspace(*p)) 2694549Seric p++; 2704549Seric while (isspace(*p)) 2714549Seric *p++ = '\0'; 2724549Seric if (*p != ':') 2734549Seric { 2744549Seric syntax: 2754549Seric message("501", "Syntax error"); 2764549Seric Errors++; 2774549Seric return (NULL); 2784549Seric } 2794549Seric *p++ = '\0'; 2804549Seric while (isspace(*p)) 2814549Seric p++; 2824549Seric 2834549Seric /* see if the input word matches desired word */ 2844549Seric if (!sameword(q, w)) 2854549Seric goto syntax; 2864549Seric 2874549Seric return (p); 2884549Seric } 2894577Seric /* 2904577Seric ** HELP -- implement the HELP command. 2914577Seric ** 2924577Seric ** Parameters: 2934577Seric ** topic -- the topic we want help for. 2944577Seric ** 2954577Seric ** Returns: 2964577Seric ** none. 2974577Seric ** 2984577Seric ** Side Effects: 2994577Seric ** outputs the help file to message output. 3004577Seric */ 3014577Seric 3024577Seric help(topic) 3034577Seric char *topic; 3044577Seric { 3054577Seric register FILE *hf; 3064577Seric int len; 3074577Seric char buf[MAXLINE]; 3084577Seric bool noinfo; 3094582Seric extern char *HelpFile; 3104577Seric 3114582Seric hf = fopen(HelpFile, "r"); 3124577Seric if (hf == NULL) 3134577Seric { 3144577Seric /* no help */ 3154577Seric message("502", "HELP not implemented"); 3164577Seric return; 3174577Seric } 3184577Seric 3194577Seric len = strlen(topic); 3204577Seric makelower(topic); 3214577Seric noinfo = TRUE; 3224577Seric 3234577Seric while (fgets(buf, sizeof buf, hf) != NULL) 3244577Seric { 3254577Seric if (strncmp(buf, topic, len) == 0) 3264577Seric { 3274577Seric register char *p; 3284577Seric 3294577Seric p = index(buf, '\t'); 3304577Seric if (p == NULL) 3314577Seric p = buf; 3324577Seric else 3334577Seric p++; 3344577Seric fixcrlf(p, TRUE); 3354577Seric message("214-", p); 3364577Seric noinfo = FALSE; 3374577Seric } 3384577Seric } 3394577Seric 3404577Seric if (noinfo) 3414577Seric message("504", "HELP topic unknown"); 3424577Seric else 3434577Seric message("214", "End of HELP info"); 3444628Seric (void) fclose(hf); 3454577Seric } 346