]> Pileus Git - ~andy/fetchmail/blob - smtp.c
651f943d11e2952e704998c8c74e48ab1dd5bea8
[~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 <config.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include "fetchmail.h"
16 #include "socket.h"
17 #include "smtp.h"
18
19 struct opt
20 {
21     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     {(char *)NULL, 0},
31 };
32
33 char smtp_response[MSGBUFSIZE];
34
35 int SMTP_helo(int sock,char *host)
36 /* send a "HELO" message to the SMTP listener */
37 {
38   int ok;
39
40   SockPrintf(sock,"HELO %s\r\n", host);
41   if (outlevel == O_VERBOSE)
42       error(0, 0, "SMTP> HELO %s", host);
43   ok = SMTP_ok(sock);
44   return ok;
45 }
46
47 int SMTP_ehlo(int sock, char *host, int *opt)
48 /* send a "EHLO" message to the SMTP listener, return extension status bits */
49 {
50   struct opt *hp;
51
52   SockPrintf(sock,"EHLO %s\r\n", host);
53   if (outlevel == O_VERBOSE)
54       error(0, 0, "SMTP> EHLO %s", host);
55   
56   *opt = 0;
57   while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
58   {
59       int  n = strlen(smtp_response);
60
61       if (smtp_response[strlen(smtp_response)-1] == '\n')
62           smtp_response[strlen(smtp_response)-1] = '\0';
63       if (smtp_response[strlen(smtp_response)-1] == '\r')
64           smtp_response[strlen(smtp_response)-1] = '\0';
65       if (n < 4)
66           return SM_ERROR;
67       smtp_response[n] = '\0';
68       if (outlevel == O_VERBOSE)
69           error(0, 0, "SMTP< %s", smtp_response);
70       for (hp = extensions; hp->name; hp++)
71           if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
72               *opt |= hp->value;
73       if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
74           return SM_OK;
75       else if (smtp_response[3] != '-')
76           return SM_ERROR;
77   }
78   return SM_UNRECOVERABLE;
79 }
80
81 int SMTP_from(int sock, char *from, char *opts)
82 /* send a "MAIL FROM:" message to the SMTP listener */
83 {
84   int ok;
85   char buf[MSGBUFSIZE];
86
87   sprintf(buf, "MAIL FROM:<%s>", from);
88   if (opts)
89       strcat(buf, opts);
90   SockPrintf(sock,"%s\r\n", buf);
91   if (outlevel == O_VERBOSE)
92       error(0, 0, "SMTP> %s", buf);
93   ok = SMTP_ok(sock);
94   return ok;
95 }
96
97 int SMTP_rcpt(int sock, char *to)
98 /* send a "RCPT TO:" message to the SMTP listener */
99 {
100   int ok;
101
102   SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
103   if (outlevel == O_VERBOSE)
104       error(0, 0, "SMTP> RCPT TO:<%s>", to);
105   ok = SMTP_ok(sock);
106   return ok;
107 }
108
109 int SMTP_data(int sock)
110 /* send a "DATA" message to the SMTP listener */
111 {
112   int ok;
113
114   SockPrintf(sock,"DATA\r\n");
115   if (outlevel == O_VERBOSE)
116       error(0, 0, "SMTP> DATA");
117   ok = SMTP_ok(sock);
118   return ok;
119 }
120
121 int SMTP_rset(int sock)
122 /* send a "RSET" message to the SMTP listener */
123 {
124   int ok;
125
126   SockPrintf(sock,"RSET\r\n");
127   if (outlevel == O_VERBOSE)
128       error(0, 0, "SMTP> RSET");
129   ok = SMTP_ok(sock);
130   return ok;
131 }
132
133 int SMTP_quit(int sock)
134 /* send a "QUIT" message to the SMTP listener */
135 {
136   int ok;
137
138   SockPrintf(sock,"QUIT\r\n");
139   if (outlevel == O_VERBOSE)
140       error(0, 0, "SMTP> QUIT");
141   ok = SMTP_ok(sock);
142   return ok;
143 }
144
145 int SMTP_eom(int sock)
146 /* send a message data terminator to the SMTP listener */
147 {
148   int ok;
149
150   SockPrintf(sock,".\r\n");
151   if (outlevel == O_VERBOSE)
152       error(0, 0, "SMTP>. (EOM)");
153   ok = SMTP_ok(sock);
154   return ok;
155 }
156
157 int SMTP_ok(int sock)
158 /* returns status of SMTP connection */
159 {
160     while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
161     {
162         int  n = strlen(smtp_response);
163
164         if (smtp_response[strlen(smtp_response)-1] == '\n')
165             smtp_response[strlen(smtp_response)-1] = '\0';
166         if (smtp_response[strlen(smtp_response)-1] == '\r')
167             smtp_response[strlen(smtp_response)-1] = '\r';
168         if (n < 4)
169             return SM_ERROR;
170         smtp_response[n] = '\0';
171         if (outlevel == O_VERBOSE)
172             error(0, 0, "SMTP< %s", smtp_response);
173         if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
174             return SM_OK;
175         else if (smtp_response[3] != '-')
176             return SM_ERROR;
177     }
178     return SM_UNRECOVERABLE;
179 }
180
181 /* smtp.c ends here */