/
qmail-qmqpc.c
189 lines (162 loc) · 4.93 KB
/
qmail-qmqpc.c
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
/*
* Revision 20180413, Kai Peter
* - switched to new qsocket lib (qlibs)
* - commented out old timeout* functions
* Revision 20160912, Kai Peter
* - added cast to function call ip4_scan
* Revision 20160713, Kai Peter
* - switched to 'buffer'
* Revision 20160509, Kai Peter
* - changed return type of main to int
*/
#include <unistd.h>
//#include <sys/types.h>
//#include <sys/socket.h>
#include "qsocket.h"
//#include <netinet/in.h>
//#include <arpa/inet.h>
#include "buffer.h"
#include "getln.h"
#include "stralloc.h"
#include "readclose.h"
#include "errmsg.h"
#include "sig.h"
#include "ip.h"
//#include "timeoutconn.h"
//#include "timeoutread.h"
//#include "timeoutwrite.h"
#include "auto_qmail.h"
#include "control.h"
//#include "fmt.h"
#define PORT_QMQP 628
void die_success() { _exit(0); }
void die_perm() { _exit(31); }
void nomem() { _exit(51); }
void die_read() { if (errno == error_nomem) nomem(); _exit(54); }
void die_control() { _exit(55); }
void die_socket() { _exit(56); }
void die_home() { _exit(61); }
void die_temp() { _exit(71); }
void die_conn() { _exit(74); }
void die_format() { _exit(91); }
int lasterror = 55;
int qmqpfd;
//int saferead(fd,buf,len) int fd; char *buf; int len;
ssize_t saferead(int fd,char *buf,int len)
{
int r;
// r = timeoutread(60,qmqpfd,buf,len);
r = timeoutread(qmqpfd,buf,len,60);
if (r <= 0) die_conn();
return r;
}
//int safewrite(fd,buf,len) int fd; char *buf; int len;
ssize_t safewrite(int fd,char *buf,int len)
{
int r;
// r = timeoutwrite(60,qmqpfd,buf,len);
r = timeoutwrite(qmqpfd,buf,len,60);
if (r <= 0) die_conn();
return r;
}
char buf[1024];
//substdio to = SUBSTDIO_FDBUF(safewrite,-1,buf,sizeof buf);
//substdio from = SUBSTDIO_FDBUF(saferead,-1,buf,sizeof buf);
//substdio envelope = SUBSTDIO_FDBUF(read,1,buf,sizeof buf);
buffer to = BUFFER_INIT(safewrite,-1,buf,sizeof buf);
buffer from = BUFFER_INIT(saferead,-1,buf,sizeof buf);
buffer envelope = BUFFER_INIT(read,1,buf,sizeof buf);
/* WARNING: can use only one of these at a time! */
stralloc beforemessage = {0};
stralloc message = {0};
stralloc aftermessage = {0};
char strnum[FMT_ULONG];
stralloc line = {0};
void getmess()
{
int match;
if (readclose(0,&message,1024) == -1) die_read();
strnum[fmt_ulong(strnum,(unsigned long) message.len)] = 0;
if (!stralloc_copys(&beforemessage,strnum)) nomem();
if (!stralloc_cats(&beforemessage,":")) nomem();
if (!stralloc_copys(&aftermessage,",")) nomem();
if (getln(&envelope,&line,&match,'\0') == -1) die_read();
if (!match) die_format();
if (line.len < 2) die_format();
if (line.s[0] != 'F') die_format();
strnum[fmt_ulong(strnum,(unsigned long) line.len - 2)] = 0;
if (!stralloc_cats(&aftermessage,strnum)) nomem();
if (!stralloc_cats(&aftermessage,":")) nomem();
if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem();
if (!stralloc_cats(&aftermessage,",")) nomem();
for (;;) {
if (getln(&envelope,&line,&match,'\0') == -1) die_read();
if (!match) die_format();
if (line.len < 2) break;
if (line.s[0] != 'T') die_format();
strnum[fmt_ulong(strnum,(unsigned long) line.len - 2)] = 0;
if (!stralloc_cats(&aftermessage,strnum)) nomem();
if (!stralloc_cats(&aftermessage,":")) nomem();
if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem();
if (!stralloc_cats(&aftermessage,",")) nomem();
}
}
void doit(char *server)
{
struct ip_address ip;
char ch;
if (!ip4_scan(server,(char *)&ip)) return;
qmqpfd = socket(AF_INET,SOCK_STREAM,0);
if (qmqpfd == -1) die_socket();
// if (timeoutconn4(qmqpfd,&ip,PORT_QMQP,10) != 0) {
if (timeoutconn(qmqpfd,(char *)&ip,PORT_QMQP,10,0) != 0) {
lasterror = 73;
// if (errno == error_timeout) lasterror = 72;
if (errno == ETIMEDOUT) lasterror = 72;
close(qmqpfd);
return;
}
strnum[fmt_ulong(strnum,(unsigned long) (beforemessage.len + message.len + aftermessage.len))] = 0;
/*
substdio_puts(&to,strnum);
substdio_puts(&to,":");
substdio_put(&to,beforemessage.s,beforemessage.len);
substdio_put(&to,message.s,message.len);
substdio_put(&to,aftermessage.s,aftermessage.len);
substdio_puts(&to,",");
substdio_flush(&to);
*/
buffer_puts(&to,strnum);
buffer_puts(&to,":");
buffer_put(&to,beforemessage.s,beforemessage.len);
buffer_put(&to,message.s,message.len);
buffer_put(&to,aftermessage.s,aftermessage.len);
buffer_puts(&to,",");
buffer_flush(&to);
for (;;) {
// substdio_get(&from,&ch,1);
buffer_get(&from,&ch,1);
if (ch == 'K') die_success();
if (ch == 'Z') die_temp();
if (ch == 'D') die_perm();
}
}
stralloc servers = {0};
int main()
{
int i;
int j;
sig_pipeignore();
if (chdir(auto_qmail) == -1) die_home();
if (control_init() == -1) die_control();
if (control_readfile(&servers,"control/qmqpservers",0) != 1) die_control();
getmess();
i = 0;
for (j = 0;j < servers.len;++j)
if (!servers.s[j]) {
doit(servers.s + i);
i = j + 1;
}
_exit(lasterror);
return(0);
}