-
Notifications
You must be signed in to change notification settings - Fork 9
/
pplistener.cc
294 lines (266 loc) · 8.09 KB
/
pplistener.cc
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
PowerMail versatile mail receiver
Copyright (C) 2002 PowerDNS.COM BV
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.
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
*/
#include "session.hh"
#include "ahuexception.hh"
#include "argsettings.hh"
#include "ppsession.hh"
#include "logger.hh"
#include "misc.hh"
#include "config.h"
#include <string>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <cstdio>
#include <signal.h>
#include "pplistenerdefaults.h"
#include "common.hh"
extern Logger L;
static void *serveConnection(void *p)
{
pthread_detach(pthread_self());
Session* s;
try {
s=(Session *)p;
s->setTimeout(0);
PPListenSession pp(s->getRemote());
// L<<"New connect from "<<s->getRemote()<<endl;
s->putLine(pp.getBanner());
string line, response;
int res;
PPListenSession::quit_t quit;
int fd;
for(;;){
s->getLine(line); // will throw exception on EOF
bool more=true;
while(more) {
res=pp.giveLine(line,response,quit,fd,more);
if(res)
s->putLine(response+"\n");
}
if(quit==PPListenSession::quitNow)
break;
if(fd>0) {
s->sendFile(fd);
s->putLine(".\n");
close(fd);
fd=-1;
}
if(quit==PPListenSession::pleaseQuit)
break;
if(quit==PPListenSession::pleaseDie)
exit(0);
}
// L<<"Closed connection from "<<s->getRemote()<<endl;
s->close();
delete s;
s=0;
}
catch(SessionTimeoutException &e) {
L<<Logger::Error<<"Timeout in pplistener";
if(s)
L<<Logger::Error<<" from "<<s->getRemote();
L<<": "<<e.d_reason<<endl;
}
catch(SessionException &e) {
L<<Logger::Error<<"Fatal error";
if(s)
L<<Logger::Error<<" from "<<s->getRemote();
L<<": "<<e.d_reason<<endl;
}
catch(AhuException &e) {
L<<Logger::Error<<"Fatal error";
if(s)
L<<Logger::Error<<" from "<<s->getRemote();
L<<": "<<e.d_reason<<endl;
}
if(s) {
s->close();
delete s;
}
return 0;
}
static void doCommands(const vector<string>&commands)
{
int ret=1;
const string &command=commands[0];
try {
string line;
cerr<<"pplistener";
if(!args().paramString("config-name").empty())
cerr<<"-"<<args().paramString("config-name");
cerr<<": ";
Session *s;
try {
s=new Session(args().paramString("listen-address"),args().paramAsNum("listen-port"));
s->getLine(line);
s->putLine("pass "+args().paramString("password")+"\r\n");
s->getLine(line);
if(line.find("+OK")) {
L<<Logger::Error<<"Password not accepted: "<<line<<endl;
exit(1);
}
}
catch(SessionException &e) {
if(command=="start") {
cerr<<"started"<<endl;
return;
}
if(command=="stop") {
cerr<<"not running"<<endl;
exit(0);
}
cerr<<"Can't connect to PowerSMTP ("<<args().paramString("listen-address")<<":"<<args().paramAsNum("listen-port")<<")";
cerr<<". Unable to send command"<<endl;
exit(1);
}
if(command=="start") {
cerr<<"already running"<<endl;
exit(1);
}
else
if(command=="stop") {
s->putLine("control stop\r\n");
s->getLine(line); // why twice?
if(!line.find("+OK")) {
cerr<<"stopped pplistener"<<endl;
ret=0;
}
else {
stripLine(line);
cerr<<"pplistener did not stop: "<<line<<endl;
}
s->close();
delete s;
}
else
if(command=="status") {
s->putLine("control status\r\n");
s->getLine(line);
if(!line.empty() && !line.find("+OK")) {
stripLine(line);
cerr<<"pplistener functioning ok: "<<line<<endl;
ret=0;
}
else {
stripLine(line);
cerr<<"pplistener not ok: "<<line<<endl;
ret=2;
}
s->close();
delete s;
}
else {
L<<Logger::Error<<"Unknown command '"<<command<<"' passed on startup"<<endl;
}
}
catch(SessionException &e) {
L<<Logger::Error<<"Error sending data to powersmtp: "<<e.d_reason<<endl;
if(command=="stop")
ret=0;
}
exit(ret);
}
int PPListenerMain(int argc, char **argv)
{
L.setName("pplistener");
args().addParameter("config-dir","Location to read configuration files from",SYSCONFDIR);
args().addParameter("config-name","Name of this virtual configuration","");
args().addParameter("listen-port","Port on which to listen for new connections","1101");
args().addParameter("listen-address","Address on which to listen for new connections","0.0.0.0");
#ifdef PPLISTENERUID
args().addParameter("run-as-uid","User-id to run as, after acquiring socket",PPLISTENERUID);
#else
args().addParameter("run-as-uid","User-id to run as, after acquiring socket","0");
#endif
#ifdef PPLISTENERGID
args().addParameter("run-as-gid","Group-id to run as, after acquiring socket",PPLISTENERGID);
#else
args().addParameter("run-as-gid","Group-id to run as, after acquiring socket","0");
#endif
args().addParameter("min-kbytes-free","When this many kilobytes are available, we unset the Full flag","5000");
args().addParameter("password","Password that clients need to send before they can retrieve and store messages","");
args().addParameter("mail-root","Location of all email messages. Must be readable, writable and executable by the chosen UID & GID!",LOCALSTATEDIR"/messages");
args().addCommand("help","Display this helpful message");
args().addCommand("version","Display version information");
args().addCommand("make-config","Output a configuration file conformant to current settings, which you can also specify on the commandline");
args().addSwitch("daemon","Run as a daemon",true);
L.toConsole(Logger::Warning);
vector<string>commands;
try {
string response;
args().preparseArgs(argc, argv,"config-dir");
if(args().parseFiles(response, (args().paramString("config-dir")+"/pplistener.conf").c_str(),0))
L<<Logger::Warning<<"Warning: unable to open a configuration file"<<endl;
else
L<<Logger::Info<<"Read configuration from "<<response<<endl;
args().parseArgs(argc, argv);
if(args().commandGiven("help")) {
cerr<<args().makeHelp()<<endl;
exit(0);
}
if(args().commandGiven("version")) {
cerr<<"pplistener version "<<VERSION<<". This is $Id: pplistener.cc,v 1.4 2003-01-03 18:09:28 ahu Exp $"<<endl;
exit(0);
}
if(args().commandGiven("make-config")) {
cout<<args().makeConfig()<<endl;
exit(0);
}
args().getRest(argc,argv,commands);
}
catch(ArgumentException &e) {
cerr<<"Unable to parse arguments:"<<endl<<e.txtReason()<<endl;
exit(0);
}
if(!commands.empty())
doCommands(commands);
try { // check if pplistener can function
PPListenSession *pps=new PPListenSession("0.0.0.0");
delete pps;
}
catch(AhuException &e) {
L<<Logger::Error<<"Unable to launch: "<<e.d_reason<<endl;
L<<Logger::Error<<"pplistener exiting"<<endl;
exit(0);
}
signal(SIGPIPE,SIG_IGN);
try {
Server *s=new Server(args().paramAsNum("listen-port"),args().paramString("listen-address"));
dropPrivs(args().paramAsNum("run-as-uid"),args().paramAsNum("run-as-gid"));
if(args().switchSet("daemon")) {
L<<Logger::Info<<"Going to background"<<endl;
daemonize();
L.toConsole(false);
}
else
L.toConsole(true);
Session *client;
pthread_t tid;
while((client=s->accept())) {
pthread_create(&tid, 0 , &serveConnection, (void *)client);
}
}
catch(SessionTimeoutException &e) {
cerr<<"Timeout"<<endl;
}
catch(SessionException &e) {
L<<Logger::Error<<"Fatal error in pplistener: "<<e.d_reason<<endl;
}
catch(Exception &e) {
cerr<<e.reason<<endl;
}
return 0;
}