14549Seric # include "sendmail.h" 24549Seric 35181Seric # ifndef SMTP 4*7797Seric SCCSID(@(#)srvrsmtp.c 3.28 08/20/82 (no SMTP)); 55181Seric # else SMTP 64556Seric 7*7797Seric SCCSID(@(#)srvrsmtp.c 3.28 08/20/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 */ 417275Seric # define CMDDBGSHOWQ 12 /* _showq -- show send queue (DEBUG) */ 427275Seric # define CMDDBGDEBUG 13 /* _debug -- set debug mode */ 437275Seric # define CMDDBGVERBOSE 14 /* _verbose -- go into verbose mode */ 447282Seric # define CMDDBGKILL 15 /* _kill -- kill sendmail */ 454549Seric 464549Seric static struct cmd CmdTab[] = 474549Seric { 484549Seric "mail", CMDMAIL, 494976Seric "rcpt", CMDRCPT, 504976Seric "mrcp", CMDRCPT, /* for old MTP compatability */ 514549Seric "data", CMDDATA, 524549Seric "rset", CMDRSET, 534549Seric "vrfy", CMDVRFY, 547762Seric "expn", CMDVRFY, 554549Seric "help", CMDHELP, 564549Seric "noop", CMDNOOP, 574549Seric "quit", CMDQUIT, 584577Seric "mrsq", CMDMRSQ, 594976Seric "helo", CMDHELO, 605003Seric # ifdef DEBUG 617275Seric "_showq", CMDDBGSHOWQ, 627275Seric "_debug", CMDDBGDEBUG, 637275Seric "_verbose", CMDDBGVERBOSE, 647282Seric "_kill", CMDDBGKILL, 655003Seric # endif DEBUG 664549Seric NULL, CMDERROR, 674549Seric }; 684549Seric 694549Seric smtp() 704549Seric { 714549Seric char inp[MAXLINE]; 724549Seric register char *p; 734549Seric struct cmd *c; 744549Seric char *cmd; 754549Seric extern char *skipword(); 764549Seric extern bool sameword(); 774549Seric bool hasmail; /* mail command received */ 784713Seric int rcps; /* number of recipients */ 795003Seric auto ADDRESS *vrfyqueue; 807124Seric extern char Version[]; 817356Seric extern tick(); 824549Seric 835003Seric hasmail = FALSE; 844713Seric rcps = 0; 857363Seric if (OutChannel != stdout) 867363Seric { 877363Seric /* arrange for debugging output to go to remote host */ 887363Seric (void) close(1); 897363Seric (void) dup(fileno(OutChannel)); 907363Seric } 91*7797Seric message("220", "%s Sendmail v%s ready at %s", HostName, 92*7797Seric Version, arpadate(NULL)); 937762Seric (void) setjmp(TopFrame); 947762Seric QuickAbort = FALSE; 954549Seric for (;;) 964549Seric { 977356Seric /* setup for the read */ 986907Seric CurEnv->e_to = NULL; 994577Seric Errors = 0; 1007275Seric (void) fflush(stdout); 1017356Seric 1027356Seric /* read the input line */ 1037685Seric p = sfgets(inp, sizeof inp, InChannel); 1047356Seric 1057685Seric /* handle errors */ 1067356Seric if (p == NULL) 1077356Seric { 1084549Seric /* end of file, just die */ 1094558Seric message("421", "%s Lost input channel", HostName); 1104549Seric finis(); 1114549Seric } 1124549Seric 1134549Seric /* clean up end of line */ 1144558Seric fixcrlf(inp, TRUE); 1154549Seric 1164713Seric /* echo command to transcript */ 1177557Seric fprintf(Xscript, "<<< %s\n", inp); 1184713Seric 1194549Seric /* break off command */ 1204549Seric for (p = inp; isspace(*p); p++) 1214549Seric continue; 1224549Seric cmd = p; 1234549Seric while (*++p != '\0' && !isspace(*p)) 1244549Seric continue; 1254549Seric if (*p != '\0') 1264549Seric *p++ = '\0'; 1274549Seric 1284549Seric /* decode command */ 1294549Seric for (c = CmdTab; c->cmdname != NULL; c++) 1304549Seric { 1314549Seric if (sameword(c->cmdname, cmd)) 1324549Seric break; 1334549Seric } 1344549Seric 1354549Seric /* process command */ 1364549Seric switch (c->cmdcode) 1374549Seric { 1384976Seric case CMDHELO: /* hello -- introduce yourself */ 1395187Seric define('s', newstr(p)); 1404997Seric message("250", "%s Hello %s, pleased to meet you", 1414997Seric HostName, p); 1424976Seric break; 1434976Seric 1444549Seric case CMDMAIL: /* mail -- designate sender */ 1454558Seric if (hasmail) 1464558Seric { 1474558Seric message("503", "Sender already specified"); 1484558Seric break; 1494558Seric } 1504549Seric p = skipword(p, "from"); 1514549Seric if (p == NULL) 1524549Seric break; 1534549Seric if (index(p, ',') != NULL) 1544549Seric { 1554549Seric message("501", "Source routing not implemented"); 1564549Seric Errors++; 1574549Seric break; 1584549Seric } 1594549Seric setsender(p); 1604577Seric if (Errors == 0) 1614549Seric { 1624549Seric message("250", "Sender ok"); 1634549Seric hasmail = TRUE; 1644549Seric } 1654549Seric break; 1664549Seric 1674976Seric case CMDRCPT: /* rcpt -- designate recipient */ 1684549Seric p = skipword(p, "to"); 1694549Seric if (p == NULL) 1704549Seric break; 1714549Seric if (index(p, ',') != NULL) 1724549Seric { 1734549Seric message("501", "Source routing not implemented"); 1744549Seric Errors++; 1754549Seric break; 1764549Seric } 1776907Seric sendto(p, 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 1784577Seric if (Errors == 0) 1794549Seric { 1806057Seric message("250", "%s... Recipient ok", p); 1814713Seric rcps++; 1824549Seric } 1834549Seric break; 1844549Seric 1854549Seric case CMDDATA: /* data -- text of mail */ 1864976Seric if (!hasmail) 1874549Seric { 1884976Seric message("503", "Need MAIL command"); 1894976Seric break; 1904549Seric } 1914713Seric else if (rcps <= 0) 1924549Seric { 1934976Seric message("503", "Need RCPT (recipient)"); 1944976Seric break; 1954549Seric } 1964976Seric 1974976Seric /* collect the text of the message */ 1984976Seric collect(TRUE); 1994976Seric if (Errors != 0) 2004976Seric break; 2014976Seric 2024976Seric /* if sending to multiple people, mail back errors */ 2034976Seric if (rcps != 1) 2044976Seric HoldErrs = MailBack = TRUE; 2054976Seric 2064976Seric /* send to all recipients */ 2077046Seric sendall(CurEnv, FALSE); 2084976Seric 2094976Seric /* reset strange modes */ 2104976Seric HoldErrs = FALSE; 2116907Seric CurEnv->e_to = NULL; 2124976Seric 2134976Seric /* issue success if appropriate */ 2144976Seric if (Errors == 0 || rcps != 1) 2154976Seric message("250", "Sent"); 2164549Seric break; 2174549Seric 2184549Seric case CMDRSET: /* rset -- reset state */ 2194549Seric message("250", "Reset state"); 2204549Seric finis(); 2214549Seric 2224549Seric case CMDVRFY: /* vrfy -- verify address */ 2235003Seric vrfyqueue = NULL; 2247762Seric QuickAbort = TRUE; 2255003Seric sendto(p, 1, (ADDRESS *) NULL, &vrfyqueue); 2267762Seric if (Errors != 0) 2277762Seric break; 2285003Seric while (vrfyqueue != NULL) 2295003Seric { 2305003Seric register ADDRESS *a = vrfyqueue->q_next; 2315003Seric char *code; 2325003Seric 2337685Seric while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 2345003Seric a = a->q_next; 2355003Seric 2367685Seric if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 2375003Seric { 2385003Seric if (a != NULL) 2395003Seric code = "250-"; 2405003Seric else 2415003Seric code = "250"; 2425003Seric if (vrfyqueue->q_fullname == NULL) 2435003Seric message(code, "<%s>", vrfyqueue->q_paddr); 2445003Seric else 2455003Seric message(code, "%s <%s>", 2465003Seric vrfyqueue->q_fullname, vrfyqueue->q_paddr); 2475003Seric } 2485003Seric else if (a == NULL) 2495003Seric message("554", "Self destructive alias loop"); 2505003Seric vrfyqueue = a; 2515003Seric } 2524549Seric break; 2534549Seric 2544549Seric case CMDHELP: /* help -- give user info */ 2554577Seric if (*p == '\0') 2564577Seric p = "SMTP"; 2574577Seric help(p); 2584549Seric break; 2594549Seric 2604549Seric case CMDNOOP: /* noop -- do nothing */ 2614549Seric message("200", "OK"); 2624549Seric break; 2634549Seric 2644549Seric case CMDQUIT: /* quit -- leave mail */ 2654549Seric message("221", "%s closing connection", HostName); 2664549Seric finis(); 2674549Seric 2684577Seric case CMDMRSQ: /* mrsq -- negotiate protocol */ 2694577Seric if (*p == 'R' || *p == 'T') 2704577Seric { 2714577Seric /* recipients first or text first */ 2724577Seric message("200", "%c ok, please continue", *p); 2734577Seric } 2744577Seric else if (*p == '?') 2754577Seric { 2764577Seric /* what do I prefer? anything, anytime */ 2774577Seric message("215", "R Recipients first is my choice"); 2784577Seric } 2794577Seric else if (*p == '\0') 2804577Seric { 2814577Seric /* no meaningful scheme */ 2824577Seric message("200", "okey dokie boobie"); 2834577Seric } 2844577Seric else 2854577Seric { 2864577Seric /* bad argument */ 2874577Seric message("504", "Scheme unknown"); 2884577Seric } 2894577Seric break; 2904577Seric 2915003Seric # ifdef DEBUG 2925003Seric case CMDDBGSHOWQ: /* show queues */ 2936907Seric printf("Send Queue="); 2946907Seric printaddr(CurEnv->e_sendqueue, TRUE); 2955003Seric break; 2967275Seric 2977275Seric case CMDDBGDEBUG: /* set debug mode */ 2987676Seric tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 2997676Seric tTflag(p); 3007676Seric message("200", "Debug set"); 3017275Seric break; 3027275Seric 3037275Seric case CMDDBGVERBOSE: /* set verbose mode */ 3047275Seric Verbose = TRUE; 3057275Seric message("200", "Verbose mode"); 3067275Seric break; 3077282Seric 3087282Seric case CMDDBGKILL: /* kill the parent */ 3097282Seric if (kill(MotherPid, SIGTERM) >= 0) 3107282Seric message("200", "Mother is dead"); 3117282Seric else 3127282Seric message("500", "Can't kill Mom"); 3137282Seric break; 3145003Seric # endif DEBUG 3155003Seric 3164549Seric case CMDERROR: /* unknown command */ 3174549Seric message("500", "Command unrecognized"); 3184549Seric break; 3194549Seric 3204549Seric default: 3214549Seric syserr("smtp: unknown code %d", c->cmdcode); 3224549Seric break; 3234549Seric } 3244549Seric } 3254549Seric } 3264549Seric /* 3274549Seric ** SKIPWORD -- skip a fixed word. 3284549Seric ** 3294549Seric ** Parameters: 3304549Seric ** p -- place to start looking. 3314549Seric ** w -- word to skip. 3324549Seric ** 3334549Seric ** Returns: 3344549Seric ** p following w. 3354549Seric ** NULL on error. 3364549Seric ** 3374549Seric ** Side Effects: 3384549Seric ** clobbers the p data area. 3394549Seric */ 3404549Seric 3414549Seric static char * 3424549Seric skipword(p, w) 3434549Seric register char *p; 3444549Seric char *w; 3454549Seric { 3464549Seric register char *q; 3474549Seric extern bool sameword(); 3484549Seric 3494549Seric /* find beginning of word */ 3504549Seric while (isspace(*p)) 3514549Seric p++; 3524549Seric q = p; 3534549Seric 3544549Seric /* find end of word */ 3554549Seric while (*p != '\0' && *p != ':' && !isspace(*p)) 3564549Seric p++; 3574549Seric while (isspace(*p)) 3584549Seric *p++ = '\0'; 3594549Seric if (*p != ':') 3604549Seric { 3614549Seric syntax: 3624549Seric message("501", "Syntax error"); 3634549Seric Errors++; 3644549Seric return (NULL); 3654549Seric } 3664549Seric *p++ = '\0'; 3674549Seric while (isspace(*p)) 3684549Seric p++; 3694549Seric 3704549Seric /* see if the input word matches desired word */ 3714549Seric if (!sameword(q, w)) 3724549Seric goto syntax; 3734549Seric 3744549Seric return (p); 3754549Seric } 3764577Seric /* 3774577Seric ** HELP -- implement the HELP command. 3784577Seric ** 3794577Seric ** Parameters: 3804577Seric ** topic -- the topic we want help for. 3814577Seric ** 3824577Seric ** Returns: 3834577Seric ** none. 3844577Seric ** 3854577Seric ** Side Effects: 3864577Seric ** outputs the help file to message output. 3874577Seric */ 3884577Seric 3894577Seric help(topic) 3904577Seric char *topic; 3914577Seric { 3924577Seric register FILE *hf; 3934577Seric int len; 3944577Seric char buf[MAXLINE]; 3954577Seric bool noinfo; 3964582Seric extern char *HelpFile; 3974577Seric 3984582Seric hf = fopen(HelpFile, "r"); 3994577Seric if (hf == NULL) 4004577Seric { 4014577Seric /* no help */ 4024577Seric message("502", "HELP not implemented"); 4034577Seric return; 4044577Seric } 4054577Seric 4064577Seric len = strlen(topic); 4074577Seric makelower(topic); 4084577Seric noinfo = TRUE; 4094577Seric 4104577Seric while (fgets(buf, sizeof buf, hf) != NULL) 4114577Seric { 4124577Seric if (strncmp(buf, topic, len) == 0) 4134577Seric { 4144577Seric register char *p; 4154577Seric 4164577Seric p = index(buf, '\t'); 4174577Seric if (p == NULL) 4184577Seric p = buf; 4194577Seric else 4204577Seric p++; 4214577Seric fixcrlf(p, TRUE); 4224577Seric message("214-", p); 4234577Seric noinfo = FALSE; 4244577Seric } 4254577Seric } 4264577Seric 4274577Seric if (noinfo) 4284577Seric message("504", "HELP topic unknown"); 4294577Seric else 4304577Seric message("214", "End of HELP info"); 4314628Seric (void) fclose(hf); 4324577Seric } 4335181Seric 4345181Seric # endif SMTP 435