summaryrefslogtreecommitdiff
path: root/protocols/qmqp.cc
blob: 830a9f5afbda6134c654677a762e5881857dcbc1 (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
// 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 "errcodes.h"
#include "fdbuf/fdbuf.h"
#include "hostname.h"
#include "itoa.h"
#include "mystring/mystring.h"
#include "netstring.h"
#include "protocol.h"

const int default_port = 628;
const int default_tls_port = -1; // No standard for QMQP over SSL exists
const char* cli_program = "qmqp";
const char* cli_help_prefix = "Send an email message via QMQP\n";

class qmqp 
{
  fdibuf& in;
  fdobuf& out;
public:
  qmqp(fdibuf& netin, fdobuf& netout);
  ~qmqp();
  void send(fdibuf& msg, unsigned long size, const mystring& env);
};

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

qmqp::~qmqp()
{
}

bool skip_envelope(fdibuf& msg)
{
  if(!msg.rewind())
    return false;
  mystring tmp;
  while(msg.getline(tmp))
    if(!tmp)
      break;
  return msg;
}
  
void qmqp::send(fdibuf& msg, unsigned long size, const mystring& env)
{
  if(!skip_envelope(msg))
    protocol_fail(ERR_MSG_READ, "Error re-reading message");
  unsigned long fullsize = strlen(itoa(size)) + 1 + size + 1 + env.length();
  out << itoa(fullsize) << ":";	// Start the "outer" netstring
  out << itoa(size) << ":";	// Start the message netstring
  fdbuf_copy(msg, out, true);	// Send out the message
  out << ","			// End the message netstring
      << env			// The envelope is already encoded
      << ",";			// End the "outer" netstring
  if(!out.flush())
    protocol_fail(ERR_MSG_WRITE, "Error sending message to remote");
  mystring response;
  if(!in.getnetstring(response))
    protocol_fail(ERR_PROTO, "Response from remote was not a netstring");
  switch(response[0]) {
  case 'K': protocol_succ(response.c_str()+1); break;
  case 'Z': protocol_fail(ERR_MSG_TEMPFAIL, response.c_str()+1); break;
  case 'D': protocol_fail(ERR_MSG_PERMFAIL, response.c_str()+1); break;
  default: protocol_fail(ERR_PROTO, "Invalid status byte in response");
  }
}

bool compute_size(fdibuf& msg, unsigned long& size)
{
  char buf[4096];
  size = 0;
  while(msg.read(buf, 4096))
    size += msg.last_count();
  if(msg.eof())
    size += msg.last_count();
  return size > 0;
}

bool make_envelope(fdibuf& msg, mystring& env)
{
  mystring tmp;
  while(msg.getline(tmp)) {
    if(!tmp)
      return true;
    env += str2net(tmp);
  }
  return false;
}
    
bool preload_data(fdibuf& msg, unsigned long& size, mystring& env)
{
  return make_envelope(msg, env) &&
    compute_size(msg, size);
}

static unsigned long msg_size;
static mystring msg_envelope;

void protocol_prep(fdibuf& in)
{
  if(!preload_data(in, msg_size, msg_envelope))
    protocol_fail(ERR_MSG_READ, "Error reading message");
}

void protocol_starttls(fdibuf& netin, fdobuf& netout)
{
  protocol_fail(ERR_USAGE, "QMQP does not support STARTTLS");
  (void)netin;
  (void)netout;
}

void protocol_send(fdibuf& in, fdibuf& netin, fdobuf& netout)
{
  alarm(60*60);			// Connection must close after an hour
  qmqp conn(netin, netout);
  conn.send(in, msg_size, msg_envelope);
}