14549Seric # include "sendmail.h" 24549Seric 35181Seric # ifndef SMTP 4*7762Seric SCCSID(@(#)srvrsmtp.c 3.27 08/15/82 (no SMTP)); 55181Seric # else SMTP 64556Seric 7*7762Seric SCCSID(@(#)srvrsmtp.c 3.27 08/15/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, 54*7762Seric "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 } 917124Seric message("220", "%s Sendmail version %s at your service", HostName, Version); 92*7762Seric (void) setjmp(TopFrame); 93*7762Seric QuickAbort = FALSE; 944549Seric for (;;) 954549Seric { 967356Seric /* setup for the read */ 976907Seric CurEnv->e_to = NULL; 984577Seric Errors = 0; 997275Seric (void) fflush(stdout); 1007356Seric 1017356Seric /* read the input line */ 1027685Seric p = sfgets(inp, sizeof inp, InChannel); 1037356Seric 1047685Seric /* handle errors */ 1057356Seric if (p == NULL) 1067356Seric { 1074549Seric /* end of file, just die */ 1084558Seric message("421", "%s Lost input channel", HostName); 1094549Seric finis(); 1104549Seric } 1114549Seric 1124549Seric /* clean up end of line */ 1134558Seric fixcrlf(inp, TRUE); 1144549Seric 1154713Seric /* echo command to transcript */ 1167557Seric fprintf(Xscript, "<<< %s\n", inp); 1174713Seric 1184549Seric /* break off command */ 1194549Seric for (p = inp; isspace(*p); p++) 1204549Seric continue; 1214549Seric cmd = p; 1224549Seric while (*++p != '\0' && !isspace(*p)) 1234549Seric continue; 1244549Seric if (*p != '\0') 1254549Seric *p++ = '\0'; 1264549Seric 1274549Seric /* decode command */ 1284549Seric for (c = CmdTab; c->cmdname != NULL; c++) 1294549Seric { 1304549Seric if (sameword(c->cmdname, cmd)) 1314549Seric break; 1324549Seric } 1334549Seric 1344549Seric /* process command */ 1354549Seric switch (c->cmdcode) 1364549Seric { 1374976Seric case CMDHELO: /* hello -- introduce yourself */ 1385187Seric define('s', newstr(p)); 1394997Seric message("250", "%s Hello %s, pleased to meet you", 1404997Seric HostName, p); 1414976Seric break; 1424976Seric 1434549Seric case CMDMAIL: /* mail -- designate sender */ 1444558Seric if (hasmail) 1454558Seric { 1464558Seric message("503", "Sender already specified"); 1474558Seric break; 1484558Seric } 1494549Seric p = skipword(p, "from"); 1504549Seric if (p == NULL) 1514549Seric break; 1524549Seric if (index(p, ',') != NULL) 1534549Seric { 1544549Seric message("501", "Source routing not implemented"); 1554549Seric Errors++; 1564549Seric break; 1574549Seric } 1584549Seric setsender(p); 1594577Seric if (Errors == 0) 1604549Seric { 1614549Seric message("250", "Sender ok"); 1624549Seric hasmail = TRUE; 1634549Seric } 1644549Seric break; 1654549Seric 1664976Seric case CMDRCPT: /* rcpt -- designate recipient */ 1674549Seric p = skipword(p, "to"); 1684549Seric if (p == NULL) 1694549Seric break; 1704549Seric if (index(p, ',') != NULL) 1714549Seric { 1724549Seric message("501", "Source routing not implemented"); 1734549Seric Errors++; 1744549Seric break; 1754549Seric } 1766907Seric sendto(p, 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 1774577Seric if (Errors == 0) 1784549Seric { 1796057Seric message("250", "%s... Recipient ok", p); 1804713Seric rcps++; 1814549Seric } 1824549Seric break; 1834549Seric 1844549Seric case CMDDATA: /* data -- text of mail */ 1854976Seric if (!hasmail) 1864549Seric { 1874976Seric message("503", "Need MAIL command"); 1884976Seric break; 1894549Seric } 1904713Seric else if (rcps <= 0) 1914549Seric { 1924976Seric message("503", "Need RCPT (recipient)"); 1934976Seric break; 1944549Seric } 1954976Seric 1964976Seric /* collect the text of the message */ 1974976Seric collect(TRUE); 1984976Seric if (Errors != 0) 1994976Seric break; 2004976Seric 2014976Seric /* if sending to multiple people, mail back errors */ 2024976Seric if (rcps != 1) 2034976Seric HoldErrs = MailBack = TRUE; 2044976Seric 2054976Seric /* send to all recipients */ 2067046Seric sendall(CurEnv, FALSE); 2074976Seric 2084976Seric /* reset strange modes */ 2094976Seric HoldErrs = FALSE; 2106907Seric CurEnv->e_to = NULL; 2114976Seric 2124976Seric /* issue success if appropriate */ 2134976Seric if (Errors == 0 || rcps != 1) 2144976Seric message("250", "Sent"); 2154549Seric break; 2164549Seric 2174549Seric case CMDRSET: /* rset -- reset state */ 2184549Seric message("250", "Reset state"); 2194549Seric finis(); 2204549Seric 2214549Seric case CMDVRFY: /* vrfy -- verify address */ 2225003Seric vrfyqueue = NULL; 223*7762Seric QuickAbort = TRUE; 2245003Seric sendto(p, 1, (ADDRESS *) NULL, &vrfyqueue); 225*7762Seric if (Errors != 0) 226*7762Seric break; 2275003Seric while (vrfyqueue != NULL) 2285003Seric { 2295003Seric register ADDRESS *a = vrfyqueue->q_next; 2305003Seric char *code; 2315003Seric 2327685Seric while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 2335003Seric a = a->q_next; 2345003Seric 2357685Seric if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 2365003Seric { 2375003Seric if (a != NULL) 2385003Seric code = "250-"; 2395003Seric else 2405003Seric code = "250"; 2415003Seric if (vrfyqueue->q_fullname == NULL) 2425003Seric message(code, "<%s>", vrfyqueue->q_paddr); 2435003Seric else 2445003Seric message(code, "%s <%s>", 2455003Seric vrfyqueue->q_fullname, vrfyqueue->q_paddr); 2465003Seric } 2475003Seric else if (a == NULL) 2485003Seric message("554", "Self destructive alias loop"); 2495003Seric vrfyqueue = a; 2505003Seric } 2514549Seric break; 2524549Seric 2534549Seric case CMDHELP: /* help -- give user info */ 2544577Seric if (*p == '\0') 2554577Seric p = "SMTP"; 2564577Seric help(p); 2574549Seric break; 2584549Seric 2594549Seric case CMDNOOP: /* noop -- do nothing */ 2604549Seric message("200", "OK"); 2614549Seric break; 2624549Seric 2634549Seric case CMDQUIT: /* quit -- leave mail */ 2644549Seric message("221", "%s closing connection", HostName); 2654549Seric finis(); 2664549Seric 2674577Seric case CMDMRSQ: /* mrsq -- negotiate protocol */ 2684577Seric if (*p == 'R' || *p == 'T') 2694577Seric { 2704577Seric /* recipients first or text first */ 2714577Seric message("200", "%c ok, please continue", *p); 2724577Seric } 2734577Seric else if (*p == '?') 2744577Seric { 2754577Seric /* what do I prefer? anything, anytime */ 2764577Seric message("215", "R Recipients first is my choice"); 2774577Seric } 2784577Seric else if (*p == '\0') 2794577Seric { 2804577Seric /* no meaningful scheme */ 2814577Seric message("200", "okey dokie boobie"); 2824577Seric } 2834577Seric else 2844577Seric { 2854577Seric /* bad argument */ 2864577Seric message("504", "Scheme unknown"); 2874577Seric } 2884577Seric break; 2894577Seric 2905003Seric # ifdef DEBUG 2915003Seric case CMDDBGSHOWQ: /* show queues */ 2926907Seric printf("Send Queue="); 2936907Seric printaddr(CurEnv->e_sendqueue, TRUE); 2945003Seric break; 2957275Seric 2967275Seric case CMDDBGDEBUG: /* set debug mode */ 2977676Seric tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 2987676Seric tTflag(p); 2997676Seric message("200", "Debug set"); 3007275Seric break; 3017275Seric 3027275Seric case CMDDBGVERBOSE: /* set verbose mode */ 3037275Seric Verbose = TRUE; 3047275Seric message("200", "Verbose mode"); 3057275Seric break; 3067282Seric 3077282Seric case CMDDBGKILL: /* kill the parent */ 3087282Seric if (kill(MotherPid, SIGTERM) >= 0) 3097282Seric message("200", "Mother is dead"); 3107282Seric else 3117282Seric message("500", "Can't kill Mom"); 3127282Seric break; 3135003Seric # endif DEBUG 3145003Seric 3154549Seric case CMDERROR: /* unknown command */ 3164549Seric message("500", "Command unrecognized"); 3174549Seric break; 3184549Seric 3194549Seric default: 3204549Seric syserr("smtp: unknown code %d", c->cmdcode); 3214549Seric break; 3224549Seric } 3234549Seric } 3244549Seric } 3254549Seric /* 3264549Seric ** SKIPWORD -- skip a fixed word. 3274549Seric ** 3284549Seric ** Parameters: 3294549Seric ** p -- place to start looking. 3304549Seric ** w -- word to skip. 3314549Seric ** 3324549Seric ** Returns: 3334549Seric ** p following w. 3344549Seric ** NULL on error. 3354549Seric ** 3364549Seric ** Side Effects: 3374549Seric ** clobbers the p data area. 3384549Seric */ 3394549Seric 3404549Seric static char * 3414549Seric skipword(p, w) 3424549Seric register char *p; 3434549Seric char *w; 3444549Seric { 3454549Seric register char *q; 3464549Seric extern bool sameword(); 3474549Seric 3484549Seric /* find beginning of word */ 3494549Seric while (isspace(*p)) 3504549Seric p++; 3514549Seric q = p; 3524549Seric 3534549Seric /* find end of word */ 3544549Seric while (*p != '\0' && *p != ':' && !isspace(*p)) 3554549Seric p++; 3564549Seric while (isspace(*p)) 3574549Seric *p++ = '\0'; 3584549Seric if (*p != ':') 3594549Seric { 3604549Seric syntax: 3614549Seric message("501", "Syntax error"); 3624549Seric Errors++; 3634549Seric return (NULL); 3644549Seric } 3654549Seric *p++ = '\0'; 3664549Seric while (isspace(*p)) 3674549Seric p++; 3684549Seric 3694549Seric /* see if the input word matches desired word */ 3704549Seric if (!sameword(q, w)) 3714549Seric goto syntax; 3724549Seric 3734549Seric return (p); 3744549Seric } 3754577Seric /* 3764577Seric ** HELP -- implement the HELP command. 3774577Seric ** 3784577Seric ** Parameters: 3794577Seric ** topic -- the topic we want help for. 3804577Seric ** 3814577Seric ** Returns: 3824577Seric ** none. 3834577Seric ** 3844577Seric ** Side Effects: 3854577Seric ** outputs the help file to message output. 3864577Seric */ 3874577Seric 3884577Seric help(topic) 3894577Seric char *topic; 3904577Seric { 3914577Seric register FILE *hf; 3924577Seric int len; 3934577Seric char buf[MAXLINE]; 3944577Seric bool noinfo; 3954582Seric extern char *HelpFile; 3964577Seric 3974582Seric hf = fopen(HelpFile, "r"); 3984577Seric if (hf == NULL) 3994577Seric { 4004577Seric /* no help */ 4014577Seric message("502", "HELP not implemented"); 4024577Seric return; 4034577Seric } 4044577Seric 4054577Seric len = strlen(topic); 4064577Seric makelower(topic); 4074577Seric noinfo = TRUE; 4084577Seric 4094577Seric while (fgets(buf, sizeof buf, hf) != NULL) 4104577Seric { 4114577Seric if (strncmp(buf, topic, len) == 0) 4124577Seric { 4134577Seric register char *p; 4144577Seric 4154577Seric p = index(buf, '\t'); 4164577Seric if (p == NULL) 4174577Seric p = buf; 4184577Seric else 4194577Seric p++; 4204577Seric fixcrlf(p, TRUE); 4214577Seric message("214-", p); 4224577Seric noinfo = FALSE; 4234577Seric } 4244577Seric } 4254577Seric 4264577Seric if (noinfo) 4274577Seric message("504", "HELP topic unknown"); 4284577Seric else 4294577Seric message("214", "End of HELP info"); 4304628Seric (void) fclose(hf); 4314577Seric } 4325181Seric 4335181Seric # endif SMTP 434