]> Pileus Git - ~andy/fetchmail/blob - smtp.c
First cut at ODMR support.
[~andy/fetchmail] / smtp.c
1 /*
2  * smtp.c -- code for speaking SMTP to a listener port
3  *
4  * Concept due to Harry Hochheiser.  Implementation by ESR.  Cleanup and
5  * strict RFC821 compliance by Cameron MacPherson.
6  *
7  * Copyright 1997 Eric S. Raymond
8  * For license terms, see the file COPYING in this directory.
9  */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "fetchmail.h"
15 #include "socket.h"
16 #include "smtp.h"
17 #include "config.h"
18
19 struct opt
20 {
21     const char *name;
22     int value;
23 };
24
25 static struct opt extensions[] =
26 {
27     {"8BITMIME",        ESMTP_8BITMIME},
28     {"SIZE",            ESMTP_SIZE},
29     {"ETRN",            ESMTP_ETRN},
30 #ifdef ODMR_ENABLE
31     {"ATRN",            ESMTP_ATRN},
32 #endif /* ODMR_ENABLE */
33     {(char *)NULL, 0},
34 };
35
36 char smtp_response[MSGBUFSIZE];
37
38 static char smtp_mode = 'S';
39
40 void SMTP_setmode(char sl)
41 /* set whether we are speaking SMTP or LMTP */
42 {
43     smtp_mode = sl;
44 }
45
46 int SMTP_helo(int sock,const char *host)
47 /* send a "HELO" message to the SMTP listener */
48 {
49   int ok;
50
51   SockPrintf(sock,"HELO %s\r\n", host);
52   if (outlevel >= O_MONITOR)
53       report(stdout, "SMTP> HELO %s\n", host);
54   ok = SMTP_ok(sock);
55   return ok;
56 }
57
58 int SMTP_ehlo(int sock, const char *host, int *opt)
59 /* send a "EHLO" message to the SMTP listener, return extension status bits */
60 {
61   struct opt *hp;
62
63   SockPrintf(sock,"%cHLO %s\r\n", (smtp_mode == 'S') ? 'E' : smtp_mode, host);
64   if (outlevel >= O_MONITOR)
65       report(stdout, "%cMTP> %cHLO %s\n", 
66             smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
67   
68   *opt = 0;
69   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
70   {
71       int  n = strlen(smtp_response);
72
73       if (smtp_response[strlen(smtp_response)-1] == '\n')
74           smtp_response[strlen(smtp_response)-1] = '\0';
75       if (smtp_response[strlen(smtp_response)-1] == '\r')
76           smtp_response[strlen(smtp_response)-1] = '\0';
77       if (n < 4)
78           return SM_ERROR;
79       smtp_response[n] = '\0';
80       if (outlevel >= O_MONITOR)
81           report(stdout, "SMTP< %s\n", smtp_response);
82       for (hp = extensions; hp->name; hp++)
83           if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
84               *opt |= hp->value;
85       if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
86           return SM_OK;
87       else if (smtp_response[3] != '-')
88           return SM_ERROR;
89   }
90   return SM_UNRECOVERABLE;
91 }
92
93 int SMTP_from(int sock, const char *from, const char *opts)
94 /* send a "MAIL FROM:" message to the SMTP listener */
95 {
96     int ok;
97     char buf[MSGBUFSIZE];
98
99     if (strchr(from, '<'))
100         sprintf(buf, "MAIL FROM: %s", from);
101     else
102         sprintf(buf, "MAIL FROM:<%s>", from);
103     if (opts)
104         strcat(buf, opts);
105     SockPrintf(sock,"%s\r\n", buf);
106     if (outlevel >= O_MONITOR)
107         report(stdout, "%cMTP> %s\n", smtp_mode, buf);
108     ok = SMTP_ok(sock);
109     return ok;
110 }
111
112 int SMTP_rcpt(int sock, const char *to)
113 /* send a "RCPT TO:" message to the SMTP listener */
114 {
115   int ok;
116
117   SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
118   if (outlevel >= O_MONITOR)
119       report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
120   ok = SMTP_ok(sock);
121   return ok;
122 }
123
124 int SMTP_data(int sock)
125 /* send a "DATA" message to the SMTP listener */
126 {
127   int ok;
128
129   SockPrintf(sock,"DATA\r\n");
130   if (outlevel >= O_MONITOR)
131       report(stdout, "%cMTP> DATA\n", smtp_mode);
132   ok = SMTP_ok(sock);
133   return ok;
134 }
135
136 int SMTP_rset(int sock)
137 /* send a "RSET" message to the SMTP listener */
138 {
139   int ok;
140
141   SockPrintf(sock,"RSET\r\n");
142   if (outlevel >= O_MONITOR)
143       report(stdout, "%cMTP> RSET\n", smtp_mode);
144   ok = SMTP_ok(sock);
145   return ok;
146 }
147
148 int SMTP_quit(int sock)
149 /* send a "QUIT" message to the SMTP listener */
150 {
151   int ok;
152
153   SockPrintf(sock,"QUIT\r\n");
154   if (outlevel >= O_MONITOR)
155       report(stdout, "%cMTP> QUIT\n", smtp_mode);
156   ok = SMTP_ok(sock);
157   return ok;
158 }
159
160 int SMTP_eom(int sock)
161 /* send a message data terminator to the SMTP listener */
162 {
163   int ok;
164
165   SockPrintf(sock,".\r\n");
166   if (outlevel >= O_MONITOR)
167       report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
168
169   /* 
170    * When doing LMTP, must process many of these at the outer level. 
171    */
172   if (smtp_mode == 'S')
173       ok = SMTP_ok(sock);
174   else
175       ok = SM_OK;
176
177   return ok;
178 }
179
180 int SMTP_ok(int sock)
181 /* returns status of SMTP connection */
182 {
183     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
184     {
185         int  n = strlen(smtp_response);
186
187         if (smtp_response[strlen(smtp_response)-1] == '\n')
188             smtp_response[strlen(smtp_response)-1] = '\0';
189         if (smtp_response[strlen(smtp_response)-1] == '\r')
190             smtp_response[strlen(smtp_response)-1] = '\0';
191         if (n < 4)
192             return SM_ERROR;
193         smtp_response[n] = '\0';
194         if (outlevel >= O_MONITOR)
195             report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
196         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
197             return SM_OK;
198         else if (smtp_response[3] != '-')
199             return SM_ERROR;
200     }
201     return SM_UNRECOVERABLE;
202 }
203
204 /* smtp.c ends here */