| 1 |
|
| /* clamd_conn.h - a target, an error, and a connection to clamd. |
| 2 |
|
| * |
| 3 |
|
| * Phase 1 of plan_clamav_clamd. Everything here is deliberately |
| 4 |
|
| * transport-only: no verdicts, no scanning, no interpretation of what |
| 5 |
|
| * came back. The framed command and the bounded reply used to live here |
| 6 |
|
| * too, as a blocking loop of their own; they are now the state machine |
| 7 |
|
| * in clamd_async.h with nothing to stream, so there is one |
| 8 |
|
| * implementation of the wire protocol and not two. |
| 9 |
|
| */ |
| 10 |
|
| #ifndef CLAMD_CONN_H |
| 11 |
|
| #define CLAMD_CONN_H |
| 12 |
|
| |
| 13 |
|
| #include "clamav/clamd_compat.h" |
| 14 |
|
| |
| 15 |
|
| #define CC_OK 0 |
| 16 |
|
| #define CC_ERR_CONNECT -1 |
| 17 |
|
| #define CC_ERR_TIMEOUT -2 |
| 18 |
|
| #define CC_ERR_IO -3 |
| 19 |
|
| #define CC_ERR_TOOBIG -4 |
| 20 |
|
| #define CC_ERR_CONFIG -5 |
| 21 |
|
| #define CC_ERR_CLOSED -6 |
| 22 |
|
| #define CC_ERR_NOTREG -7 /* not a regular file */ |
| 23 |
|
| #define CC_ERR_NOFDPASS -8 /* this transport or platform cannot pass one */ |
| 24 |
|
| /* -9 and -10 live in clamd_stream.h */ |
| 25 |
|
| #define CC_ERR_UNAVAILABLE -11 /* clamd has this command switched off (Enable*Command no) */ |
| 26 |
|
| |
| 27 |
|
| #define CC_ERRLEN 256 |
| 28 |
|
| |
| 29 |
|
| /* Reply framing. clamd terminates its reply the same way the request was |
| 30 |
|
| * terminated - measured, not assumed: zPING\0 answers PONG\0 and |
| 31 |
|
| * nPING\n answers PONG\n. The client picks one and reads for it. */ |
| 32 |
|
| #define CC_FRAME_Z 0 /* NUL-terminated - the default */ |
| 33 |
|
| #define CC_FRAME_N 1 /* newline-terminated */ |
| 34 |
|
| |
| 35 |
|
| typedef struct { |
| 36 |
|
| char *path; /* UNIX socket path, or NULL for TCP */ |
| 37 |
|
| char *host; /* TCP host, or NULL for UNIX */ |
| 38 |
|
| int port; |
| 39 |
|
| double connect_timeout; |
| 40 |
|
| double reply_timeout; |
| 41 |
|
| size_t reply_max; /* ceiling on a single reply */ |
| 42 |
|
| size_t chunk; /* INSTREAM chunk size, 0 = default */ |
| 43 |
|
| size_t max_size; /* local refusal ceiling, 0 = unset */ |
| 44 |
|
| int frame; |
| 45 |
|
| } cc_target; |
| 46 |
|
| |
| 47 |
|
| /* Which transport actually produced a verdict. A scan that fell back |
| 48 |
|
| * from FILDES to INSTREAM crossed the socket in full, and an operator |
| 49 |
|
| * wondering why one deployment is slower than another should be able to |
| 50 |
|
| * read that off a field rather than infer it from a graph. */ |
| 51 |
|
| #define CC_TRANSPORT_NONE 0 |
| 52 |
|
| #define CC_TRANSPORT_FILDES 1 |
| 53 |
|
| #define CC_TRANSPORT_INSTREAM 2 |
| 54 |
|
| |
| 55 |
|
| typedef struct { |
| 56 |
|
| int code; |
| 57 |
|
| char msg[CC_ERRLEN]; |
| 58 |
|
| } cc_err; |
| 59 |
|
| |
| 60 |
|
| /* "what: detail", or "what" alone, cut to fit. A detail is as often a |
| 61 |
|
| * path from the configuration as a strerror, so the cut is not academic. */ |
| 62 |
88 |
| static void cc_err_set(cc_err *e, int code, const char *what, const char *detail) { |
| 63 |
88 |
| e->code = code; |
| 64 |
88 |
▶ | if (detail) snprintf(e->msg, sizeof e->msg, "%s: %s", what, detail); |
Branch: 1 branch
| Branch | True | False | Total |
| if (detail) snprintf(e->msg, sizeof e->msg, "%s: %s", what, detail); |
34 |
54 |
88 |
|
| 65 |
54 |
| else snprintf(e->msg, sizeof e->msg, "%s", what); |
| 66 |
88 |
| } |
| 67 |
|
| |
| 68 |
|
| /* Did a non-blocking connect that returned -1 begin at all? */ |
| 69 |
39 |
| static int cc_connect_started(void) { |
| 70 |
|
| #ifdef _WIN32 |
| 71 |
|
| return cc_sock_errno() == WSAEWOULDBLOCK; |
| 72 |
|
| #else |
| 73 |
39 |
| return errno == EINPROGRESS; |
| 74 |
|
| #endif |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| /* The second half of a non-blocking connect, given what the first half |
| 78 |
|
| * returned. A UNIX connect completes or fails at once on every platform |
| 79 |
|
| * this has met and a TCP one is still in flight, so the wait here is the |
| 80 |
|
| * same code for both and is exercised by TCP. */ |
| 81 |
168 |
| static int cc_finish_connect(int fd, int r, double deadline, const char *name, cc_err *err) { |
| 82 |
168 |
| int w, soerr = 0; |
| 83 |
168 |
| cc_socklen_t sl = sizeof soerr; |
| 84 |
|
| |
| 85 |
168 |
▶ | if (r == 0) return CC_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) return CC_OK; |
129 |
39 |
168 |
|
| 86 |
39 |
▶ | if (!cc_connect_started()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cc_connect_started()) { |
19 |
20 |
39 |
|
| 87 |
19 |
| cc_err_set(err, CC_ERR_CONNECT, "connect", strerror(errno)); |
| 88 |
19 |
| return CC_ERR_CONNECT; |
| 89 |
|
| } |
| 90 |
20 |
| w = cc_wait(fd, 1, deadline); |
| 91 |
20 |
▶ | if (w == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w == 0) { |
2 |
18 |
20 |
|
| 92 |
2 |
| cc_err_set(err, CC_ERR_TIMEOUT, "connect timed out", name); |
| 93 |
2 |
| return CC_ERR_TIMEOUT; |
| 94 |
|
| } |
| 95 |
|
| /* |
| 96 |
|
| # uncoverable branch true |
| 97 |
|
| # uncoverable condition left count:1 */ |
| 98 |
18 |
▶ | if (w < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w < 0) { |
0 |
18 |
18 |
|
| 99 |
0 |
| cc_err_set(err, CC_ERR_CONNECT, "poll", strerror(errno)); /* # uncoverable statement */ |
| 100 |
0 |
| return CC_ERR_CONNECT; /* # uncoverable statement */ |
| 101 |
|
| } |
| 102 |
|
| /* |
| 103 |
|
| # uncoverable branch true |
| 104 |
|
| # uncoverable condition left count:1 */ |
| 105 |
18 |
▶ | if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&soerr, &sl) < 0) soerr = errno; |
Branch: 1 branch
| Branch | True | False | Total |
| if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&soerr, &sl) < 0) soerr = errno; |
0 |
18 |
18 |
|
| 106 |
18 |
▶ | if (soerr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (soerr) { |
2 |
16 |
18 |
|
| 107 |
2 |
| cc_err_set(err, CC_ERR_CONNECT, "connect", strerror(soerr)); |
| 108 |
2 |
| return CC_ERR_CONNECT; |
| 109 |
|
| } |
| 110 |
16 |
| return CC_OK; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| #ifndef _WIN32 |
| 114 |
|
| /* A UNIX socket path that does not fit is REFUSED, never truncated. |
| 115 |
|
| * strncpy into sun_path silently drops the tail, and the process then |
| 116 |
|
| * binds or connects to a different socket than the one configured - |
| 117 |
|
| * which for a scanner means talking to something that is not clamd. |
| 118 |
|
| * This was found the hard way in phase 0: a 105-byte path became a |
| 119 |
|
| * socket called "fake.so". */ |
| 120 |
148 |
| static int cc_connect_unix(const cc_target *t, cc_err *err) { |
| 121 |
|
| struct sockaddr_un addr; |
| 122 |
148 |
| size_t len = strlen(t->path); |
| 123 |
|
| int fd, r; |
| 124 |
|
| |
| 125 |
148 |
▶ | if (len >= CC_SUN_PATH_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= CC_SUN_PATH_MAX) { |
1 |
147 |
148 |
|
| 126 |
1 |
| cc_err_set(err, CC_ERR_CONFIG, |
| 127 |
|
| "socket path too long for this platform's sockaddr_un", |
| 128 |
1 |
| t->path); |
| 129 |
1 |
| return CC_INVALID_SOCK; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
147 |
| fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 133 |
|
| /* |
| 134 |
|
| # uncoverable branch true |
| 135 |
|
| # uncoverable condition left count:1 */ |
| 136 |
147 |
▶ | if (fd < 0) { cc_err_set(err, CC_ERR_CONNECT, "socket", strerror(errno)); return CC_INVALID_SOCK; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { cc_err_set(err, CC_ERR_CONNECT, "socket", strerror(errno)); return CC_INVALID_SOCK; } |
0 |
147 |
147 |
|
| 137 |
147 |
| cc_suppress_sigpipe(fd); |
| 138 |
|
| |
| 139 |
147 |
| memset(&addr, 0, sizeof addr); |
| 140 |
147 |
| addr.sun_family = AF_UNIX; |
| 141 |
147 |
| memcpy(addr.sun_path, t->path, len); |
| 142 |
147 |
| addr.sun_path[len] = '\0'; |
| 143 |
|
| |
| 144 |
|
| /* |
| 145 |
|
| # uncoverable branch true |
| 146 |
|
| # uncoverable condition left count:1 */ |
| 147 |
147 |
▶ | if (cc_set_nonblock(fd) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_set_nonblock(fd) < 0) { |
0 |
147 |
147 |
|
| 148 |
0 |
| cc_err_set(err, CC_ERR_CONNECT, "fcntl", strerror(errno)); /* # uncoverable statement */ |
| 149 |
0 |
| cc_close_sock(fd); return CC_INVALID_SOCK; /* # uncoverable statement */ |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
147 |
| r = connect(fd, (struct sockaddr *)&addr, (cc_socklen_t)sizeof addr); |
| 153 |
147 |
▶ | if (cc_finish_connect(fd, r, cc_now() + t->connect_timeout, t->path, err) != CC_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_finish_connect(fd, r, cc_now() + t->connect_timeout, t->path, err) != CC_OK) { |
18 |
129 |
147 |
|
| 154 |
18 |
| cc_close_sock(fd); |
| 155 |
18 |
| return CC_INVALID_SOCK; |
| 156 |
|
| } |
| 157 |
129 |
| return fd; |
| 158 |
|
| } |
| 159 |
|
| #endif |
| 160 |
|
| |
| 161 |
22 |
| static int cc_connect_tcp(const cc_target *t, cc_err *err) { |
| 162 |
22 |
| struct addrinfo hints, *res = NULL, *ai; |
| 163 |
|
| char portbuf[16]; |
| 164 |
22 |
| int fd = CC_INVALID_SOCK, gai; |
| 165 |
22 |
| double deadline = cc_now() + t->connect_timeout; |
| 166 |
|
| |
| 167 |
|
| if (cc_net_init() < 0) { |
| 168 |
|
| cc_err_set(err, CC_ERR_CONNECT, "WSAStartup failed", NULL); |
| 169 |
|
| return CC_INVALID_SOCK; |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
22 |
| memset(&hints, 0, sizeof hints); |
| 173 |
22 |
| hints.ai_family = AF_UNSPEC; |
| 174 |
22 |
| hints.ai_socktype = SOCK_STREAM; |
| 175 |
|
| |
| 176 |
22 |
| snprintf(portbuf, sizeof portbuf, "%d", t->port); |
| 177 |
22 |
| gai = getaddrinfo(t->host, portbuf, &hints, &res); |
| 178 |
22 |
▶ | if (gai != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gai != 0) { |
1 |
21 |
22 |
|
| 179 |
1 |
| cc_err_set(err, CC_ERR_CONNECT, "resolve", gai_strerror(gai)); |
| 180 |
1 |
| return CC_INVALID_SOCK; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
24 |
▶ | for (ai = res; ai; ai = ai->ai_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ai = res; ai; ai = ai->ai_next) { |
21 |
3 |
24 |
|
| 184 |
|
| int r, rc; |
| 185 |
21 |
| fd = (int)socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 186 |
|
| /* |
| 187 |
|
| # uncoverable branch true |
| 188 |
|
| # uncoverable condition left count:1 */ |
| 189 |
21 |
▶ | if (fd < 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) continue; |
0 |
21 |
21 |
|
| 190 |
21 |
| cc_suppress_sigpipe(fd); |
| 191 |
|
| /* |
| 192 |
|
| # uncoverable branch true |
| 193 |
|
| # uncoverable condition left count:1 */ |
| 194 |
21 |
▶ | if (cc_set_nonblock(fd) < 0) { cc_close_sock(fd); fd = CC_INVALID_SOCK; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_set_nonblock(fd) < 0) { cc_close_sock(fd); fd = CC_INVALID_SOCK; continue; } |
0 |
21 |
21 |
|
| 195 |
|
| |
| 196 |
|
| #ifdef _WIN32 |
| 197 |
|
| r = connect((SOCKET)fd, ai->ai_addr, (cc_socklen_t)ai->ai_addrlen); |
| 198 |
|
| #else |
| 199 |
21 |
| r = connect(fd, ai->ai_addr, (cc_socklen_t)ai->ai_addrlen); |
| 200 |
|
| #endif |
| 201 |
21 |
| rc = cc_finish_connect(fd, r, deadline, t->host, err); |
| 202 |
21 |
▶ | if (rc == CC_OK) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == CC_OK) break; |
16 |
5 |
21 |
|
| 203 |
5 |
| cc_close_sock(fd); |
| 204 |
5 |
| fd = CC_INVALID_SOCK; |
| 205 |
|
| /* A timeout is the deadline for the whole attempt, not for one |
| 206 |
|
| * address; a refusal leaves the next address its turn. */ |
| 207 |
5 |
▶ | if (rc == CC_ERR_TIMEOUT) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == CC_ERR_TIMEOUT) break; |
2 |
3 |
5 |
|
| 208 |
|
| } |
| 209 |
21 |
| freeaddrinfo(res); |
| 210 |
|
| |
| 211 |
21 |
▶ | if (fd == CC_INVALID_SOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd == CC_INVALID_SOCK) { |
5 |
16 |
21 |
|
| 212 |
5 |
▶ | if (err->code != CC_ERR_TIMEOUT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (err->code != CC_ERR_TIMEOUT) |
3 |
2 |
5 |
|
| 213 |
3 |
| cc_err_set(err, CC_ERR_CONNECT, "connect", t->host); |
| 214 |
5 |
| return CC_INVALID_SOCK; |
| 215 |
|
| } |
| 216 |
|
| { /* clamd replies are small and latency matters more than packing */ |
| 217 |
16 |
| int one = 1; |
| 218 |
16 |
| (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&one, sizeof one); |
| 219 |
|
| } |
| 220 |
16 |
| return fd; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
171 |
| static int cc_connect(const cc_target *t, cc_err *err) { |
| 224 |
171 |
▶ | if (t->path) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->path) { |
148 |
23 |
171 |
|
| 225 |
|
| #ifdef _WIN32 |
| 226 |
|
| cc_err_set(err, CC_ERR_CONFIG, "UNIX sockets are not supported on this platform", NULL); |
| 227 |
|
| return CC_INVALID_SOCK; |
| 228 |
|
| #else |
| 229 |
148 |
| return cc_connect_unix(t, err); |
| 230 |
|
| #endif |
| 231 |
|
| } |
| 232 |
23 |
▶ | if (t->host) return cc_connect_tcp(t, err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->host) return cc_connect_tcp(t, err); |
22 |
1 |
23 |
|
| 233 |
1 |
| cc_err_set(err, CC_ERR_CONFIG, "no socket path and no host configured", NULL); |
| 234 |
1 |
| return CC_INVALID_SOCK; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
|
| #endif /* CLAMD_CONN_H */ |