]> Pileus Git - ~andy/fetchmail/blob - smtp.c
Cosmetic fix.
[~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 #ifdef HAVE_SNPRINTF
101         snprintf(buf, sizeof(buf),
102 #else
103         sprintf(buf,
104 #endif /* HAVE_SNPRINTF */
105                 "MAIL FROM: %s", from);
106     else
107 #ifdef HAVE_SNPRINTF
108     snprintf(buf, sizeof(buf),
109 #else
110     sprintf(buf,
111 #endif /* HAVE_SNPRINTF */
112             "MAIL FROM:<%s>", from);
113     if (opts)
114 #ifdef HAVE_SNPRINTF
115         snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
116 #else
117         strcat(buf, opts);
118 #endif /* HAVE_SNPRINTF */
119     SockPrintf(sock,"%s\r\n", buf);
120     if (outlevel >= O_MONITOR)
121         report(stdout, "%cMTP> %s\n", smtp_mode, buf);
122     ok = SMTP_ok(sock);
123     return ok;
124 }
125
126 int SMTP_rcpt(int sock, const char *to)
127 /* send a "RCPT TO:" message to the SMTP listener */
128 {
129   int ok;
130
131   SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
132   if (outlevel >= O_MONITOR)
133       report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
134   ok = SMTP_ok(sock);
135   return ok;
136 }
137
138 int SMTP_data(int sock)
139 /* send a "DATA" message to the SMTP listener */
140 {
141   int ok;
142
143   SockPrintf(sock,"DATA\r\n");
144   if (outlevel >= O_MONITOR)
145       report(stdout, "%cMTP> DATA\n", smtp_mode);
146   ok = SMTP_ok(sock);
147   return ok;
148 }
149
150 int SMTP_rset(int sock)
151 /* send a "RSET" message to the SMTP listener */
152 {
153   int ok;
154
155   SockPrintf(sock,"RSET\r\n");
156   if (outlevel >= O_MONITOR)
157       report(stdout, "%cMTP> RSET\n", smtp_mode);
158   ok = SMTP_ok(sock);
159   return ok;
160 }
161
162 int SMTP_quit(int sock)
163 /* send a "QUIT" message to the SMTP listener */
164 {
165   int ok;
166
167   SockPrintf(sock,"QUIT\r\n");
168   if (outlevel >= O_MONITOR)
169       report(stdout, "%cMTP> QUIT\n", smtp_mode);
170   ok = SMTP_ok(sock);
171   return ok;
172 }
173
174 int SMTP_eom(int sock)
175 /* send a message data terminator to the SMTP listener */
176 {
177   int ok;
178
179   SockPrintf(sock,".\r\n");
180   if (outlevel >= O_MONITOR)
181       report(stdout, "%cMTP>. (EOM)\n", smtp_mode);
182
183   /* 
184    * When doing LMTP, must process many of these at the outer level. 
185    */
186   if (smtp_mode == 'S')
187       ok = SMTP_ok(sock);
188   else
189       ok = SM_OK;
190
191   return ok;
192 }
193
194 int SMTP_ok(int sock)
195 /* returns status of SMTP connection */
196 {
197     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
198     {
199         int  n = strlen(smtp_response);
200
201         if (smtp_response[strlen(smtp_response)-1] == '\n')
202             smtp_response[strlen(smtp_response)-1] = '\0';
203         if (smtp_response[strlen(smtp_response)-1] == '\r')
204             smtp_response[strlen(smtp_response)-1] = '\0';
205         if (n < 4)
206             return SM_ERROR;
207         smtp_response[n] = '\0';
208         if (outlevel >= O_MONITOR)
209             report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
210         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
211             return SM_OK;
212         else if (smtp_response[3] != '-')
213             return SM_ERROR;
214     }
215     return SM_UNRECOVERABLE;
216 }
217
218 /* smtp.c ends here */