ClamAV-Clamd
view release on metacpan or search on metacpan
include/clamav/clamd_scan.h view on Meta::CPAN
*
* PROTOCOL ERROR: ancillary data sent without FILDES. ERROR
*
* because clamd sees the control message on a read where FILDES has not
* been dispatched yet. Two messages, in this order. No document says so.
*
* Every control-message offset goes through the CMSG_* macros. The
* alignment rules are the sort of thing that works by accident on the
* platform you developed on and not on the next one.
*/
#ifndef CLAMD_SCAN_H
#define CLAMD_SCAN_H
#include "clamav/clamd_conn.h"
#include "clamav/clamd_stream.h"
#include "clamav/clamd_async.h"
/* CC_ERR_NOTREG and CC_ERR_NOFDPASS live in clamd_conn.h. They were
* defined here, below the includes, which meant clamd_async.h - included
* above - had already been parsed without them: the branch it takes on a
* platform that cannot pass descriptors raises NOFDPASS, and would not
* compile. */
/* Scan an open descriptor.
*
* The descriptor is BORROWED. This never closes it, and it never seeks
* it either: clamd scans the whole file regardless of where the
* descriptor happens to be positioned - measured at offsets 0, 10 and
* past EOF, all of which detected a signature at byte 0 - so there is
* nothing to correct, and correcting it would mutate something the
* caller owns.
*
* A descriptor that is not a regular file cannot mean what the caller
* thinks it means: a pipe has no length and can be read exactly once, a
* directory is not a document, and a socket is somebody else's stream.
* Refuse rather than hand clamd something it will answer about
* meaninglessly.
*/
static int cc_scan_fd(const cc_target *t, int fd,
char **out, size_t *outlen, int *transport, cc_err *err) {
cc_scan sc;
int rc, mode;
err->code = CC_OK; err->msg[0] = '\0';
*out = NULL; *outlen = 0;
*transport = CC_TRANSPORT_NONE;
/* FILDES over TCP is not an error at clamd's end - it is a HANG.
* Phase 0: the command produces no reply, no diagnostic, and the
* connection stays open forever. clamd will never tell a client it
* asked for something impossible, so the client has to know.
*
* Same on any platform without SCM_RIGHTS. Both fall back to
* INSTREAM, which is correct but expensive: the file now crosses the
* socket in full. That is why the transport is reported. */
mode = (CC_HAVE_FD_PASSING && t->path) ? CC_TRANSPORT_FILDES
: CC_TRANSPORT_INSTREAM;
{
cc_stat_t st;
if (cc_fstat(fd, &st) < 0) {
cc_err_set(err, CC_ERR_IO, "fstat", strerror(errno));
return CC_ERR_IO;
}
if (!CC_S_ISREG(st.st_mode)) {
cc_err_set(err, CC_ERR_NOTREG, "not a regular file", NULL);
return CC_ERR_NOTREG;
}
if (t->max_size && (size_t)st.st_size > t->max_size) {
cc_err_set(err, CC_ERR_TOOBIGLOC,
"larger than max_size; refused without scanning", NULL);
return CC_ERR_TOOBIGLOC;
}
}
if (mode == CC_TRANSPORT_FILDES)
rc = cc_scan_start(&sc, t, mode, fd, 0, NULL, 0, -1);
else
rc = cc_scan_start(&sc, t, mode, -1, 0, NULL, 0, fd);
if (rc == CC_OK) rc = cc_scan_run(&sc, out, outlen, err);
else *err = sc.err;
*transport = sc.transport;
cc_scan_free(&sc);
return rc;
}
/* Scan a path.
*
* The path is opened HERE and the descriptor is what travels. This is
* NOT clamd's SCAN command, which hands clamd a path for clamd to
* resolve - and which therefore needs clamd to have permission on it.
* The whole point of FILDES is that it does not.
*/
static int cc_scan_path(const cc_target *t, const char *path,
char **out, size_t *outlen, int *transport, cc_err *err) {
int fd, rc;
fd = cc_open_ro(path);
if (fd < 0) {
*out = NULL; *outlen = 0;
*transport = CC_TRANSPORT_NONE;
cc_err_set(err, CC_ERR_IO, "open", strerror(errno));
return CC_ERR_IO;
}
rc = cc_scan_fd(t, fd, out, outlen, transport, err);
cc_close_fd(fd); /* ours, so ours to close - on every path */
return rc;
}
#endif /* CLAMD_SCAN_H */
( run in 0.861 second using v1.01-cache-2.11-cpan-eb134014566 )