annotate src/dccifd.cpp @ 407:29d54e7028f6 stable-6-0-54

document dmarc vs dnsbl dkim/spf; switch to . rather than " " for dkim impossible signer
author Carl Byington <carl@five-ten-sg.com>
date Thu, 30 Mar 2017 10:26:30 -0700
parents df7dc6b2b153
children 5209e92b4885
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
1 /*
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
2
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
4 the GPL version 3 or any later version at your choice available at
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
6
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
7 */
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
8
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
9 #include "includes.h"
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
10 #include <errno.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
11 #include <fcntl.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
12 #include <poll.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
13 #include <signal.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
14 #include <string>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
15 #include <sys/types.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
16 #include <sys/wait.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
17 #include <unistd.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
18
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
19 // needed for socket io
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
20 #include <sys/ioctl.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
21 #include <net/if.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
22 #include <arpa/inet.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
23 #include <netinet/in.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
24 #include <netinet/tcp.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
25 #include <netdb.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
26 #include <sys/socket.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
27 #include <sys/un.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
28
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
29
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
30 const int maxlen = 1000; // used for snprintf buffers
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
31 extern int NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
32 const char *options = "header\n";
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
33
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
34
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
35 ////////////////////////////////////////////////
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
36 // helper to convert syslog control chars
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
37 //
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
38 string escaper(string v);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
39 string escaper(string v)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
40 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
41 size_t n = v.length();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
42 char buf[n+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
43 strncpy(buf, v.c_str(), n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
44 for (size_t i=0; i<n; i++) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
45 if (buf[i] == '\r') buf[i] = 'r';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
46 if (buf[i] == '\n') buf[i] = 'n';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
47 if ((unsigned char)(buf[i]) < ' '){
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
48 buf[i] = '.';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
49 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
50 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
51 return string(buf, n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
52 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
53
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
54
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
55 DccInterface::DccInterface(const char *port_, mlfiPriv *priv_, int ip, const char *helo_, const char *from)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
56 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
57 err = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
58 first_recipient = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
59 first_header = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
60 priv = priv_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
61 ip4 = ip;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
62 helo = helo_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
63 envfrom = from;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
64 dccifd_port = port_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
65 dccifd_socket = NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
66 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
67
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
68
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
69 DccInterface::~DccInterface()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
70 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
71 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
72 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
73
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
74
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
75 void DccInterface::mlfi_envrcpt(SMFICTX *ctx, const char *envrcpt, bool grey)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
76 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
77 if (first_recipient) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
78 first_recipient = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
79 char adr[sizeof "255.255.255.255 "];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
80 adr[0] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
81 inet_ntop(AF_INET, (const u_char *)&ip4, adr, sizeof(adr));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
82 // Validated sending site's address
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
83 const char *rdns = getmacro(ctx, "_", "");
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
84 char buf[maxlen+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
85 if (*rdns == '[') rdns = "";
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
86 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
87 int n = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
88 while ((n < maxlen) && rdns[n] && (rdns[n] != ' ')) n++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
89 strncpy(buf, rdns, n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
90 buf[n] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
91 rdns = buf;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
92 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
93 output(options);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
94 output(adr); output("\r");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
95 output(rdns); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
96 output(helo); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
97 output(envfrom); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
98 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
99 output(envrcpt);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
100 if (grey) output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
101 else output("\rdnsblnogrey\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
102 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
103
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
104
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
105 void DccInterface::mlfi_header(SMFICTX *ctx, const char *headerf, const char *headerv)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
106 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
107 if (dccifd_socket == NULL_SOCKET) Connect();
371
df7dc6b2b153 install dnsblnogrey/whiteclnt for dccifd
Carl Byington <carl@five-ten-sg.com>
parents: 227
diff changeset
108 if (err) return;
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
109 if ((dccifd_socket != NULL_SOCKET) && (!dccifd_input.empty())) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
110 output(dccifd_input);
227
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
111 dccifd_input = "";
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
112 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
113
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
114 if (first_header) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
115 output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
116 first_header = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
117 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
118
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
119 output(headerf);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
120 output(": ");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
121 output(headerv);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
122 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
123 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
124
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
125
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
126 void DccInterface::mlfi_eoh()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
127 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
128 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
129 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
130
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
131
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
132 void DccInterface::mlfi_body(const u_char *bodyp, size_t bodylen)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
133 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
134 output((const char *)bodyp, bodylen);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
135 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
136
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
137
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
138 void DccInterface::mlfi_eom(bool &grey, int &bulk)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
139 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
140 // AnAnX-DCC-Rhyolite-Metrics: ns.five-ten-sg.com 104; Body=2 Fuz1=2nn
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
141
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
142 close_output(); // signal EOF to DccInterface
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
143 input(); // read what the dcc has to say about this message
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
144 my_syslog(priv, "dcc returned " + escaper(dccifd_output));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
145 grey = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
146 bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
147 const int n = dccifd_output.length();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
148 char buf[n+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
149 strncpy(buf, dccifd_output.c_str(), n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
150 buf[n] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
151
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
152 int newlines = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
153 int j, i = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
154 while ((i<n) && (newlines < 2)) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
155 switch (buf[i++]) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
156 case 'G' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
157 grey = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
158 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
159 case '\n' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
160 newlines++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
161 default : ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
162 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
163 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
164
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
165 // skip up to and including ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
166 while ((i<n) && (buf[i++] != ';'));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
167
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
168 // convert to lower, = to space, ctrl-chars to space
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
169 for (j=i; j<n; j++) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
170 buf[j] = tolower(buf[j]);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
171 if (buf[j] == '=') buf[j] = ' ';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
172 if (buf[j] < ' ') buf[j] = ' ';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
173 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
174
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
175 while (i<n) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
176 // skip leading blanks
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
177 while ((i<n) && (buf[i] == ' ')) i++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
178
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
179 // find blank terminator
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
180 for (j=i; (j<n) && (buf[j] != ' '); j++);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
181
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
182 // find anything?
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
183 if (j > i) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
184 // process this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
185 buf[j] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
186 //my_syslog(priv, string("dccifd token") + (buf+i));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
187 if (strcmp(buf+i, "bulk") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
188 else if (strcmp(buf+i, "many") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
189 else if (strcmp(buf+i, "whitelist") == 0) bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
190 else if (isdigit(buf[i])) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
191 int b = atoi(buf+i);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
192 if (b > bulk) bulk = b;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
193 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
194 // skip this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
195 i = j+1;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
196 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
197 }
227
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
198 //char buff[maxlen];
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
199 //snprintf(buff, sizeof(buff), "dccifd found grey %s bulk %d", ((grey) ? "yes" : "no"), bulk);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
200 //my_syslog(priv, buff);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
201 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
202
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
203
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
204 void DccInterface::my_disconnect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
205 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
206 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
207 shutdown(dccifd_socket, SHUT_RDWR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
208 close(dccifd_socket);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
209 dccifd_socket = NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
210 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
211 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
212
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
213
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
214 void DccInterface::Connect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
215 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
216 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
217
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
218 sockaddr_un server;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
219 memset(&server, '\0', sizeof(server));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
220 server.sun_family = AF_UNIX;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
221 strncpy(server.sun_path, dccifd_port, sizeof(server.sun_path)-1);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
222 dccifd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
223 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
224 bool rc = (connect(dccifd_socket, (sockaddr *)&server, sizeof(server)) == 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
225 if (!rc) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
226 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
227 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
228 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
229 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
230 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
231
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
232
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
233 size_t DccInterface::my_write(const char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
234 if (err) return 0;
371
df7dc6b2b153 install dnsblnogrey/whiteclnt for dccifd
Carl Byington <carl@five-ten-sg.com>
parents: 227
diff changeset
235
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
236 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
237 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
238 ssize_t ws = write(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
239 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
240 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
241 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
242 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
243 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
244 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
245 // error or peer closed the socket!
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
246 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
247 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
248 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
249 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
250 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
251 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
252 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
253
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
254 size_t DccInterface::my_read(char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
255 if (err) return 0;
371
df7dc6b2b153 install dnsblnogrey/whiteclnt for dccifd
Carl Byington <carl@five-ten-sg.com>
parents: 227
diff changeset
256
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
257 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
258 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
259 ssize_t ws = read(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
260 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
261 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
262 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
263 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
264 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
265 else if (ws < 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
266 // read error
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
267 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
268 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
269 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
270 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
271 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
272 // peer closed the socket, end of file
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
273 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
274 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
275 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
276 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
277 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
278
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
279 void DccInterface::output(const char* buffer, size_t size)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
280 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
281 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
282
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
283 // buffer it if not connected yet
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
284 if (dccifd_socket == NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
285 //my_syslog(priv, string("dcc buffered ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
286 dccifd_input.append(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
287 return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
288 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
289
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
290 // write it if we are connected
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
291 //my_syslog(priv, string("dcc write ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
292 my_write(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
293 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
294
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
295
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
296 void DccInterface::output(const char* buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
297 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
298 output(buffer, strlen(buffer));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
299 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
300
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
301
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
302 void DccInterface::output(string buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
303 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
304 output(buffer.c_str(), buffer.size());
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
305 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
306
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
307
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
308 void DccInterface::close_output()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
309 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
310 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
311 shutdown(dccifd_socket, SHUT_WR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
312 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
313 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
314
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
315
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
316 void DccInterface::input()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
317 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
318 if ((dccifd_socket == NULL_SOCKET) || err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
319 char buf[maxlen];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
320 int rs;
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
321 while ((rs = my_read(buf, maxlen))) {
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
322 //my_syslog(priv, string("dcc read ") + escaper(string(buf, rs)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
323 dccifd_output.append(buf, rs);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
324 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
325 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
326 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
327
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
328
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
329 const char *DccInterface::getmacro(SMFICTX *ctx, const char *macro, const char *def)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
330 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
331 const char *rc = smfi_getsymval(ctx, (char*)macro);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
332 if (!rc) rc = def;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
333 return rc;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
334 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
335
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
336