summaryrefslogtreecommitdiff
path: root/protocols/smtp.cc
blob: 706f75782d9b1ae23d62b55413ef0757fd166d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// nullmailer -- a simple relay-only MTA
// Copyright (C) 2017  Bruce Guenter <bruce@untroubled.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact me at <bruce@untroubled.org>.  There is also a mailing list
// available to discuss this package.  To subscribe, send an email to
// <nullmailer-subscribe@lists.untroubled.org>.

#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include "base64.h"
#include "connect.h"
#include "errcodes.h"
#include "fdbuf/fdbuf.h"
#include "itoa.h"
#include "mystring/mystring.h"
#include "protocol.h"

const int default_port = 25;
const int default_tls_port = 465;
const char* cli_program = "smtp";
const char* cli_help_prefix = "Send an email message via SMTP\n";

class smtp 
{
  fdibuf& in;
  fdobuf& out;
  mystring caps;
public:
  smtp(fdibuf& netin, fdobuf& netout);
  ~smtp();
  int get(mystring& str);
  int put(mystring cmd, mystring& result);
  void docmd(mystring cmd, int range, mystring& result);
  void docmd(mystring cmd, int range);
  void dohelo(bool ehlo);
  bool hascap(const char* name, const char* word = NULL);
  void auth_login(void);
  void auth_plain(void);
  void send_data(fdibuf& msg);
  void send_envelope(fdibuf& msg);
  void send(fdibuf& msg);
};

smtp::smtp(fdibuf& netin, fdobuf& netout)
  : in(netin), out(netout)
{
}

smtp::~smtp()
{
}

int smtp::get(mystring& str)
{
  mystring tmp;
  str = "";
  int code = -1;
  while(in.getline(tmp)) {
    if(tmp[tmp.length()-1] == '\r')
      tmp = tmp.left(tmp.length()-1);
    code = atoi(tmp.c_str());
    if(!!str)
      str += "\n";
    str += tmp;
    if(tmp[3] != '-')
      break;
  }
  return code;
}

int smtp::put(mystring cmd, mystring& result)
{
  out << cmd << "\r\n";
  if(!out.flush())
    return -1;
  return get(result);
}

void smtp::docmd(mystring cmd, int range, mystring& result)
{
  int code;
  if(!cmd)
    code = get(result);
  else
    code = put(cmd, result);
  if(code < range || code >= (range+100)) {
    int e;
    if(code >= 500)
      e = ERR_MSG_PERMFAIL;
    else if(code >= 400)
      e = ERR_MSG_TEMPFAIL;
    else
      e = ERR_PROTO;
    out << "QUIT\r\n";
    out.flush();
    protocol_fail(e, result.c_str());
  }
}

void smtp::docmd(mystring cmd, int range)
{
  mystring msg;
  docmd(cmd, range, msg);
}

void smtp::dohelo(bool ehlo)
{
  mystring hh = getenv("HELOHOST");
  if (!hh) protocol_fail(1, "$HELOHOST is not set");
  docmd((ehlo ? "EHLO " : "HELO ") + hh, 200, caps);
}

static int issep(char ch)
{
  return ch == ' ' || ch == '\n' || ch == '\0';
}

bool smtp::hascap(const char* name, const char* word)
{
  const size_t namelen = strlen(name);
  int i = -1;
  do {
    const char* s = caps.c_str() + i + 5;
    if (strncasecmp(s, name, namelen) == 0) {
      if (s[namelen] == '\n')
	return word == NULL;
      else if (s[namelen] == ' ') {
	if (word == NULL)
	  return true;
	s += namelen + 1;
	const size_t wordlen = strlen(word);
	do {
	  if (strncasecmp(s, word, wordlen) == 0 && issep(s[wordlen]))
	    return true;
	  while (!issep(*s))
	    ++s;
	} while (*s++ == ' ');
	return false;
      }
    }
    i = caps.find_first('\n', i+1);
  } while (i > 0);
  return false;
}

void smtp::auth_login(void)
{
  mystring encoded;
  base64_encode(user, encoded);
  docmd("AUTH LOGIN " + encoded, 300);
  encoded = "";
  base64_encode(pass, encoded);
  docmd(encoded, 200);
}

void smtp::auth_plain(void)
{
  mystring plain(user);
  plain += '\0';
  plain += user;
  plain += '\0';
  plain += pass;
  mystring encoded = "AUTH PLAIN ";
  base64_encode(plain, encoded);
  docmd(encoded, 200);
}

void smtp::send_envelope(fdibuf& msg)
{
  mystring tmp;
  msg.getline(tmp);
  docmd("MAIL FROM:<" + tmp + ">", 200);
  while(msg.getline(tmp) && !!tmp)
    docmd("RCPT TO:<" + tmp + ">", 200);
}

void smtp::send_data(fdibuf& msg)
{
  docmd("DATA", 300);
  mystring tmp;
  while(msg.getline(tmp)) {
    if((tmp[0] == '.' && !(out << ".")) ||
       !(out << tmp << "\r\n"))
      protocol_fail(ERR_MSG_WRITE, "Error sending message to remote");
  }
  docmd(".", 200, tmp);
  out << "QUIT\r\n";
  out.flush();
  protocol_succ(tmp.c_str());
}

void smtp::send(fdibuf& msg)
{
  send_envelope(msg);
  send_data(msg);
}

void protocol_prep(fdibuf&)
{
}

static int did_starttls = 0;

void protocol_starttls(fdibuf& netin, fdobuf& netout)
{
  smtp conn(netin, netout);
  conn.docmd("", 200);
  conn.dohelo(true);
  conn.docmd("STARTTLS", 200);
  did_starttls = 1;
}

void protocol_send(fdibuf& in, fdibuf& netin, fdobuf& netout)
{
  smtp conn(netin, netout);
  if (!did_starttls)
    conn.docmd("", 200);

  if (user != 0 && pass != 0) {
    conn.dohelo(true);
    if (auth_method == AUTH_LOGIN)
      conn.auth_login();
    else if (auth_method == AUTH_PLAIN)
      conn.auth_plain();
    else {
      // Detect method
      if (conn.hascap("AUTH", "PLAIN"))
	conn.auth_plain();
      else if (conn.hascap("AUTH", "LOGIN"))
	conn.auth_login();
      else
	protocol_fail(ERR_MSG_TEMPFAIL, "Server does not advertise any supported authentication methods");
    }
  }
  else
    conn.dohelo(false);

  conn.send(in);
}