14549Seric # include "sendmail.h" 24549Seric 35181Seric # ifndef SMTP 4*7851Seric SCCSID(@(#)srvrsmtp.c 3.30 08/22/82 (no SMTP)); 55181Seric # else SMTP 64556Seric 7*7851Seric SCCSID(@(#)srvrsmtp.c 3.30 08/22/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*7851Seric message("220", "%s Sendmail %s ready at %s", HostName, 927797Seric 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; 1716907Seric sendto(p, 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 1724577Seric if (Errors == 0) 1734549Seric { 1746057Seric message("250", "%s... Recipient ok", p); 1754713Seric rcps++; 1764549Seric } 1774549Seric break; 1784549Seric 1794549Seric case CMDDATA: /* data -- text of mail */ 1804976Seric if (!hasmail) 1814549Seric { 1824976Seric message("503", "Need MAIL command"); 1834976Seric break; 1844549Seric } 1854713Seric else if (rcps <= 0) 1864549Seric { 1874976Seric message("503", "Need RCPT (recipient)"); 1884976Seric break; 1894549Seric } 1904976Seric 1914976Seric /* collect the text of the message */ 1924976Seric collect(TRUE); 1934976Seric if (Errors != 0) 1944976Seric break; 1954976Seric 1964976Seric /* if sending to multiple people, mail back errors */ 1974976Seric if (rcps != 1) 1984976Seric HoldErrs = MailBack = TRUE; 1994976Seric 2004976Seric /* send to all recipients */ 2017046Seric sendall(CurEnv, FALSE); 2024976Seric 2034976Seric /* reset strange modes */ 2044976Seric HoldErrs = FALSE; 2056907Seric CurEnv->e_to = NULL; 2064976Seric 2074976Seric /* issue success if appropriate */ 2084976Seric if (Errors == 0 || rcps != 1) 2094976Seric message("250", "Sent"); 2104549Seric break; 2114549Seric 2124549Seric case CMDRSET: /* rset -- reset state */ 2134549Seric message("250", "Reset state"); 2144549Seric finis(); 2154549Seric 2164549Seric case CMDVRFY: /* vrfy -- verify address */ 2175003Seric vrfyqueue = NULL; 2187762Seric QuickAbort = TRUE; 2195003Seric sendto(p, 1, (ADDRESS *) NULL, &vrfyqueue); 2207762Seric if (Errors != 0) 2217762Seric break; 2225003Seric while (vrfyqueue != NULL) 2235003Seric { 2245003Seric register ADDRESS *a = vrfyqueue->q_next; 2255003Seric char *code; 2265003Seric 2277685Seric while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 2285003Seric a = a->q_next; 2295003Seric 2307685Seric if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 2315003Seric { 2325003Seric if (a != NULL) 2335003Seric code = "250-"; 2345003Seric else 2355003Seric code = "250"; 2365003Seric if (vrfyqueue->q_fullname == NULL) 2375003Seric message(code, "<%s>", vrfyqueue->q_paddr); 2385003Seric else 2395003Seric message(code, "%s <%s>", 2405003Seric vrfyqueue->q_fullname, vrfyqueue->q_paddr); 2415003Seric } 2425003Seric else if (a == NULL) 2435003Seric message("554", "Self destructive alias loop"); 2445003Seric vrfyqueue = a; 2455003Seric } 2464549Seric break; 2474549Seric 2484549Seric case CMDHELP: /* help -- give user info */ 2494577Seric if (*p == '\0') 2504577Seric p = "SMTP"; 2514577Seric help(p); 2524549Seric break; 2534549Seric 2544549Seric case CMDNOOP: /* noop -- do nothing */ 2554549Seric message("200", "OK"); 2564549Seric break; 2574549Seric 2584549Seric case CMDQUIT: /* quit -- leave mail */ 2594549Seric message("221", "%s closing connection", HostName); 2604549Seric finis(); 2614549Seric 2624577Seric case CMDMRSQ: /* mrsq -- negotiate protocol */ 2634577Seric if (*p == 'R' || *p == 'T') 2644577Seric { 2654577Seric /* recipients first or text first */ 2664577Seric message("200", "%c ok, please continue", *p); 2674577Seric } 2684577Seric else if (*p == '?') 2694577Seric { 2704577Seric /* what do I prefer? anything, anytime */ 2714577Seric message("215", "R Recipients first is my choice"); 2724577Seric } 2734577Seric else if (*p == '\0') 2744577Seric { 2754577Seric /* no meaningful scheme */ 2764577Seric message("200", "okey dokie boobie"); 2774577Seric } 2784577Seric else 2794577Seric { 2804577Seric /* bad argument */ 2814577Seric message("504", "Scheme unknown"); 2824577Seric } 2834577Seric break; 2844577Seric 2855003Seric # ifdef DEBUG 2865003Seric case CMDDBGSHOWQ: /* show queues */ 2876907Seric printf("Send Queue="); 2886907Seric printaddr(CurEnv->e_sendqueue, TRUE); 2895003Seric break; 2907275Seric 2917275Seric case CMDDBGDEBUG: /* set debug mode */ 2927676Seric tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 2937676Seric tTflag(p); 2947676Seric message("200", "Debug set"); 2957275Seric break; 2967275Seric 2977275Seric case CMDDBGVERBOSE: /* set verbose mode */ 2987275Seric Verbose = TRUE; 2997275Seric message("200", "Verbose mode"); 3007275Seric break; 3017282Seric 3027282Seric case CMDDBGKILL: /* kill the parent */ 3037282Seric if (kill(MotherPid, SIGTERM) >= 0) 3047282Seric message("200", "Mother is dead"); 3057282Seric else 3067282Seric message("500", "Can't kill Mom"); 3077282Seric break; 3085003Seric # endif DEBUG 3095003Seric 3104549Seric case CMDERROR: /* unknown command */ 3114549Seric message("500", "Command unrecognized"); 3124549Seric break; 3134549Seric 3144549Seric default: 3154549Seric syserr("smtp: unknown code %d", c->cmdcode); 3164549Seric break; 3174549Seric } 3184549Seric } 3194549Seric } 3204549Seric /* 3214549Seric ** SKIPWORD -- skip a fixed word. 3224549Seric ** 3234549Seric ** Parameters: 3244549Seric ** p -- place to start looking. 3254549Seric ** w -- word to skip. 3264549Seric ** 3274549Seric ** Returns: 3284549Seric ** p following w. 3294549Seric ** NULL on error. 3304549Seric ** 3314549Seric ** Side Effects: 3324549Seric ** clobbers the p data area. 3334549Seric */ 3344549Seric 3354549Seric static char * 3364549Seric skipword(p, w) 3374549Seric register char *p; 3384549Seric char *w; 3394549Seric { 3404549Seric register char *q; 3414549Seric extern bool sameword(); 3424549Seric 3434549Seric /* find beginning of word */ 3444549Seric while (isspace(*p)) 3454549Seric p++; 3464549Seric q = p; 3474549Seric 3484549Seric /* find end of word */ 3494549Seric while (*p != '\0' && *p != ':' && !isspace(*p)) 3504549Seric p++; 3514549Seric while (isspace(*p)) 3524549Seric *p++ = '\0'; 3534549Seric if (*p != ':') 3544549Seric { 3554549Seric syntax: 3564549Seric message("501", "Syntax error"); 3574549Seric Errors++; 3584549Seric return (NULL); 3594549Seric } 3604549Seric *p++ = '\0'; 3614549Seric while (isspace(*p)) 3624549Seric p++; 3634549Seric 3644549Seric /* see if the input word matches desired word */ 3654549Seric if (!sameword(q, w)) 3664549Seric goto syntax; 3674549Seric 3684549Seric return (p); 3694549Seric } 3704577Seric /* 3714577Seric ** HELP -- implement the HELP command. 3724577Seric ** 3734577Seric ** Parameters: 3744577Seric ** topic -- the topic we want help for. 3754577Seric ** 3764577Seric ** Returns: 3774577Seric ** none. 3784577Seric ** 3794577Seric ** Side Effects: 3804577Seric ** outputs the help file to message output. 3814577Seric */ 3824577Seric 3834577Seric help(topic) 3844577Seric char *topic; 3854577Seric { 3864577Seric register FILE *hf; 3874577Seric int len; 3884577Seric char buf[MAXLINE]; 3894577Seric bool noinfo; 3904582Seric extern char *HelpFile; 3914577Seric 3924582Seric hf = fopen(HelpFile, "r"); 3934577Seric if (hf == NULL) 3944577Seric { 3954577Seric /* no help */ 3964577Seric message("502", "HELP not implemented"); 3974577Seric return; 3984577Seric } 3994577Seric 4004577Seric len = strlen(topic); 4014577Seric makelower(topic); 4024577Seric noinfo = TRUE; 4034577Seric 4044577Seric while (fgets(buf, sizeof buf, hf) != NULL) 4054577Seric { 4064577Seric if (strncmp(buf, topic, len) == 0) 4074577Seric { 4084577Seric register char *p; 4094577Seric 4104577Seric p = index(buf, '\t'); 4114577Seric if (p == NULL) 4124577Seric p = buf; 4134577Seric else 4144577Seric p++; 4154577Seric fixcrlf(p, TRUE); 4164577Seric message("214-", p); 4174577Seric noinfo = FALSE; 4184577Seric } 4194577Seric } 4204577Seric 4214577Seric if (noinfo) 4224577Seric message("504", "HELP topic unknown"); 4234577Seric else 4244577Seric message("214", "End of HELP info"); 4254628Seric (void) fclose(hf); 4264577Seric } 4275181Seric 4285181Seric # endif SMTP 429