|
0
|
1 |
// Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
|
|
|
2 |
//
|
|
|
3 |
// This program is free software; you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation; either version 2 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// This program is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with this program; if not, write to the Free Software
|
|
|
15 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
16 |
|
|
|
17 |
#include <config.h>
|
|
|
18 |
#include <pwd.h>
|
|
|
19 |
#include <stdio.h>
|
|
|
20 |
#include <stdlib.h>
|
|
|
21 |
#include <sys/types.h>
|
|
|
22 |
#include <sys/stat.h>
|
|
|
23 |
#include <signal.h>
|
|
|
24 |
#include <unistd.h>
|
|
2
|
25 |
#include "cli++/cli++.h"
|
|
0
|
26 |
#include "daemon.h"
|
|
|
27 |
|
|
|
28 |
const char* cli_program = "vmailmgrd";
|
|
|
29 |
const char* cli_help_prefix = "vmailmgr support daemon\n";
|
|
|
30 |
const char* cli_help_suffix = "";
|
|
|
31 |
const char* cli_args_usage = "";
|
|
|
32 |
const int cli_args_min = 0;
|
|
|
33 |
const int cli_args_max = 0;
|
|
|
34 |
int opt_log_all = true;
|
|
|
35 |
int opt_verbose = false;
|
|
|
36 |
|
|
2
|
37 |
// This program is the local server that is used to handle managing
|
|
|
38 |
// virtual domains from a web or remote interface.
|
|
0
|
39 |
//
|
|
|
40 |
// F<vmailmgrd> logs its activity to standard output, and as such
|
|
2
|
41 |
// requires its output to be piped through a tool to record those
|
|
|
42 |
// logs, such as F<multilog> (from the F<daemontools> package).
|
|
0
|
43 |
|
|
|
44 |
cli_option cli_options[] = {
|
|
|
45 |
{ 'd', 0, cli_option::flag, 0, &opt_log_all,
|
|
|
46 |
"Log only requests that fail", 0 },
|
|
|
47 |
{ 'D', 0, cli_option::flag, 1, &opt_log_all,
|
|
|
48 |
"Log all requests (default)", 0 },
|
|
|
49 |
{ 'v', 0, cli_option::flag, 0, &opt_verbose,
|
|
|
50 |
"Log non-verbosely (default)", 0 },
|
|
|
51 |
{ 'V', 0, cli_option::flag, 1, &opt_verbose,
|
|
|
52 |
"Log verbosely", 0 },
|
|
|
53 |
// Log verbose messages regarding the system's status.
|
|
|
54 |
// Note that this flag implies C<-D>.
|
|
|
55 |
{0}
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
// SEE ALSO
|
|
|
59 |
//
|
|
|
60 |
// vdeliver(1), checkvpw(8)
|
|
|
61 |
|
|
|
62 |
#define TIMEOUT 1
|
|
|
63 |
|
|
|
64 |
static inline void die(const char* msg)
|
|
|
65 |
{
|
|
|
66 |
perror(msg);
|
|
|
67 |
exit(1);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
static void finishreq()
|
|
|
71 |
{
|
|
|
72 |
alarm(0);
|
|
|
73 |
close(0);
|
|
|
74 |
close(1);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
static void abortreq(const char* m)
|
|
|
78 |
{
|
|
|
79 |
logresponse(response(response::bad, m));
|
|
|
80 |
finishreq();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
static RETSIGTYPE handle_hup(int)
|
|
|
84 |
{
|
|
|
85 |
signal(SIGHUP, handle_hup);
|
|
|
86 |
log("Stray SIGHUP caught");
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
static RETSIGTYPE handle_alrm(int)
|
|
|
90 |
{
|
|
|
91 |
signal(SIGALRM, handle_alrm);
|
|
|
92 |
abortreq("Timed out waiting for remote");
|
|
|
93 |
exit(1);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
static RETSIGTYPE handle_pipe(int)
|
|
|
97 |
{
|
|
|
98 |
signal(SIGPIPE, handle_pipe);
|
|
|
99 |
abortreq("Connection to client lost");
|
|
|
100 |
exit(1);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
static RETSIGTYPE handle_intr(int)
|
|
|
104 |
{
|
|
|
105 |
signal(SIGINT, handle_intr);
|
|
|
106 |
signal(SIGTERM, handle_intr);
|
|
|
107 |
log("Stray interrupt caught");
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
bool decode_string(mystring& str, uchar*& buf, ssize_t& buflen)
|
|
|
111 |
{
|
|
|
112 |
ssize_t length = (buf[0] << 8) | buf[1];
|
|
|
113 |
buf += 2; buflen -= 2;
|
|
|
114 |
if(length > buflen)
|
|
|
115 |
return false;
|
|
|
116 |
str = mystring((char*)buf, length);
|
|
|
117 |
buf += length; buflen -= length;
|
|
|
118 |
return true;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
#define FAIL(MSG) do { abortreq(MSG ", aborting"); return 0; } while(0);
|
|
|
122 |
|
|
|
123 |
command* decode_data(uchar* ptr, ssize_t length)
|
|
|
124 |
{
|
|
|
125 |
uchar argcount = *ptr++;
|
|
|
126 |
--length;
|
|
|
127 |
mystring cmdstr;
|
|
|
128 |
if(!decode_string(cmdstr, ptr, length))
|
|
|
129 |
FAIL("Couldn't decode the command string");
|
|
|
130 |
if(cmdstr.empty())
|
|
|
131 |
FAIL("Empty command string");
|
|
|
132 |
command* cmd = new command(cmdstr, argcount);
|
|
|
133 |
for(unsigned i = 0; i < argcount; i++) {
|
|
|
134 |
mystring str;
|
|
|
135 |
if(!decode_string((*cmd)[i], ptr, length)) {
|
|
|
136 |
delete cmd;
|
|
|
137 |
FAIL("Error decoding a command parameter");
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
return cmd;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
command* read_data()
|
|
|
144 |
{
|
|
|
145 |
alarm(TIMEOUT); // avoid denial-of-service by faulty clients
|
|
|
146 |
|
|
|
147 |
uchar hdrbuf[3];
|
|
|
148 |
switch(read(0, &hdrbuf, 3)) {
|
|
|
149 |
case -1: FAIL("read system call failed or was interrupted");
|
|
|
150 |
case 3: break;
|
|
|
151 |
default: FAIL("Short read while reading protocol header");
|
|
|
152 |
}
|
|
|
153 |
if(hdrbuf[0] != 2)
|
|
|
154 |
FAIL("Invalid protocol from client");
|
|
|
155 |
ssize_t length = (hdrbuf[1] << 8) | hdrbuf[2];
|
|
|
156 |
uchar buf[length];
|
|
|
157 |
if(read(0, buf, length) != length)
|
|
|
158 |
FAIL("Short read while reading message data");
|
|
|
159 |
alarm(0);
|
|
|
160 |
return decode_data(buf, length);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
int cli_main(int, char**)
|
|
|
164 |
{
|
|
|
165 |
if(opt_verbose)
|
|
|
166 |
opt_log_all = true;
|
|
|
167 |
|
|
|
168 |
signal(SIGALRM, handle_alrm);
|
|
|
169 |
signal(SIGPIPE, handle_pipe);
|
|
|
170 |
signal(SIGINT, handle_intr);
|
|
|
171 |
signal(SIGTERM, handle_intr);
|
|
|
172 |
signal(SIGHUP, handle_hup);
|
|
|
173 |
signal(SIGQUIT, handle_intr);
|
|
|
174 |
|
|
|
175 |
if(opt_verbose)
|
|
|
176 |
log("Accepted connection");
|
|
|
177 |
command* cmd = read_data();
|
|
|
178 |
if(cmd) {
|
|
|
179 |
response resp = dispatch_cmd(*cmd, 1);
|
|
|
180 |
logresponse(resp);
|
|
|
181 |
alarm(TIMEOUT);
|
|
|
182 |
if(!resp.write(1))
|
|
|
183 |
abortreq("Error writing response");
|
|
|
184 |
finishreq();
|
|
|
185 |
delete cmd;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
return 0;
|
|
|
189 |
}
|