2 Samba-VirusFilter VFS modules
3 Copyright (C) 2010-2016 SATOH Fumiyasu @ OSS Technology Corp., Japan
4 Copyright (C) 2016-2017 Trever L. Adams
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "modules/vfs_virusfilter_common.h"
21 #include "modules/vfs_virusfilter_utils.h"
25 #include "lib/util/iov_buf.h"
27 #include "lib/tsocket/tsocket.h"
29 int virusfilter_debug_class
= DBGC_VFS
;
31 /* ====================================================================== */
33 char *virusfilter_string_sub(
35 connection_struct
*conn
,
38 return talloc_sub_advanced(mem_ctx
,
39 lp_servicename(mem_ctx
, SNUM(conn
)),
40 conn
->session_info
->unix_info
->unix_name
,
42 conn
->session_info
->unix_token
->gid
,
43 conn
->session_info
->unix_info
->sanitized_username
,
44 conn
->session_info
->info
->domain_name
,
48 int virusfilter_vfs_next_move(
49 struct vfs_handle_struct
*vfs_h
,
50 const struct smb_filename
*smb_fname_src
,
51 const struct smb_filename
*smb_fname_dst
)
55 result
= SMB_VFS_NEXT_RENAME(vfs_h
, smb_fname_src
, smb_fname_dst
);
56 if (result
== 0 || errno
!= EXDEV
) {
61 * For now, do not handle EXDEV as poking around violates
62 * stackability. Return -1, simply refuse access.
67 /* Line-based socket I/O
68 * ======================================================================
71 struct virusfilter_io_handle
*virusfilter_io_new(
76 struct virusfilter_io_handle
*io_h
= talloc_zero(mem_ctx
,
77 struct virusfilter_io_handle
);
86 virusfilter_io_set_connect_timeout(io_h
, connect_timeout
);
87 virusfilter_io_set_io_timeout(io_h
, io_timeout
);
88 virusfilter_io_set_writel_eol(io_h
, "\x0A", 1);
89 virusfilter_io_set_readl_eol(io_h
, "\x0A", 1);
94 int virusfilter_io_set_connect_timeout(
95 struct virusfilter_io_handle
*io_h
,
98 int timeout_old
= io_h
->connect_timeout
;
100 /* timeout <= 0 means infinite */
101 io_h
->connect_timeout
= (timeout
> 0) ? timeout
: -1;
106 int virusfilter_io_set_io_timeout(
107 struct virusfilter_io_handle
*io_h
,
110 int timeout_old
= io_h
->io_timeout
;
112 /* timeout <= 0 means infinite */
113 io_h
->io_timeout
= (timeout
> 0) ? timeout
: -1;
118 void virusfilter_io_set_writel_eol(
119 struct virusfilter_io_handle
*io_h
,
123 if (eol_size
< 1 || eol_size
> VIRUSFILTER_IO_EOL_SIZE
) {
127 memcpy(io_h
->w_eol
, eol
, eol_size
);
128 io_h
->w_eol_size
= eol_size
;
131 void virusfilter_io_set_readl_eol(
132 struct virusfilter_io_handle
*io_h
,
136 if (eol_size
< 1 || eol_size
> VIRUSFILTER_IO_EOL_SIZE
) {
140 memcpy(io_h
->r_eol
, eol
, eol_size
);
141 io_h
->r_eol_size
= eol_size
;
144 bool virusfilter_io_connect_path(
145 struct virusfilter_io_handle
*io_h
,
148 struct sockaddr_un addr
;
155 addr
.sun_family
= AF_UNIX
;
157 len
= strlcpy(addr
.sun_path
, path
, sizeof(addr
.sun_path
));
158 if (len
>= sizeof(addr
.sun_path
)) {
163 status
= open_socket_out((struct sockaddr_storage
*)&addr
, 0,
164 io_h
->connect_timeout
,
166 if (!NT_STATUS_IS_OK(status
)) {
171 /* We must not block */
172 ret
= set_blocking(socket
, false);
179 ok
= smb_set_close_on_exec(socket
);
186 ret
= tstream_bsd_existing_socket(io_h
, socket
, &io_h
->stream
);
189 DBG_ERR("Could not convert socket to tstream: %s.\n",
198 static void disconnect_done(struct tevent_req
*req
)
200 uint64_t *perr
= tevent_req_callback_data(req
, uint64_t);
204 ret
= tstream_disconnect_recv(req
, &err_ret
);
211 bool virusfilter_io_disconnect(
212 struct virusfilter_io_handle
*io_h
)
214 struct tevent_req
*req
;
215 struct tevent_context
*ev
;
216 uint64_t *perror
= NULL
;
218 TALLOC_CTX
*frame
= talloc_stackframe();
220 if (io_h
->stream
== NULL
) {
223 return VIRUSFILTER_RESULT_OK
;
226 ev
= tevent_context_init(frame
);
228 DBG_ERR("Failed to setup event context.\n");
233 /* Error return - must be talloc'ed. */
234 perror
= talloc_zero(frame
, uint64_t);
235 if (perror
== NULL
) {
239 req
= tstream_disconnect_send(io_h
, ev
, io_h
->stream
);
241 /* Callback when disconnect is done. */
242 tevent_req_set_callback(req
, disconnect_done
, perror
);
245 ok
= tevent_req_set_endtime(req
, ev
, timeval_current_ofs_msec(
246 io_h
->connect_timeout
));
248 DBG_ERR("Can't set endtime\n");
252 /* Loop waiting for req to finish. */
253 ok
= tevent_req_poll(req
, ev
);
255 DBG_ERR("tevent_req_poll failed\n");
259 /* Emit debug error if failed. */
261 DBG_DEBUG("Error %s\n", strerror((int)*perror
));
265 /* Here we know we disconnected. */
275 static void writev_done(struct tevent_req
*req
)
277 uint64_t *perr
= tevent_req_callback_data(req
, uint64_t);
281 ret
= tstream_writev_recv(req
, &err_ret
);
288 /****************************************************************************
289 Write all data from an iov array, with msec timeout (per write)
290 NB. This can be called with a non-socket fd, don't add dependencies
292 ****************************************************************************/
294 bool write_data_iov_timeout(
295 struct tstream_context
*stream
,
296 const struct iovec
*iov
,
300 struct tevent_context
*ev
= NULL
;
301 struct tevent_req
*req
= NULL
;
302 uint64_t *perror
= NULL
;
304 TALLOC_CTX
*frame
= talloc_stackframe();
306 ev
= tevent_context_init(frame
);
308 DBG_ERR("Failed to setup event context.\n");
312 /* Error return - must be talloc'ed. */
313 perror
= talloc_zero(frame
, uint64_t);
314 if (perror
== NULL
) {
319 req
= tstream_writev_send(frame
, ev
, stream
, iov
, iovcnt
);
321 DBG_ERR("Out of memory.\n");
325 /* Callback when *all* data sent. */
326 tevent_req_set_callback(req
, writev_done
, perror
);
329 ok
= tevent_req_set_endtime(req
, ev
,
330 timeval_current_ofs_msec(ms_timeout
));
332 DBG_ERR("Can't set endtime\n");
336 /* Loop waiting for req to finish. */
337 ok
= tevent_req_poll(req
, ev
);
339 DBG_ERR("tevent_req_poll failed\n");
343 /* Done with req - freed by the callback. */
346 /* Emit debug error if failed. */
348 DBG_DEBUG("Error %s\n", strerror((int)*perror
));
352 /* Here we know we correctly wrote all data. */
361 bool virusfilter_io_write(
362 struct virusfilter_io_handle
*io_h
,
368 if (data_size
== 0) {
369 return VIRUSFILTER_RESULT_OK
;
372 iov
.iov_base
= discard_const_p(void, data
);
373 iov
.iov_len
= data_size
;
375 return write_data_iov_timeout(io_h
->stream
, &iov
, 1, io_h
->io_timeout
);
378 bool virusfilter_io_writel(
379 struct virusfilter_io_handle
*io_h
,
385 ok
= virusfilter_io_write(io_h
, data
, data_size
);
390 return virusfilter_io_write(io_h
, io_h
->w_eol
, io_h
->w_eol_size
);
393 bool virusfilter_io_writefl(
394 struct virusfilter_io_handle
*io_h
,
395 const char *data_fmt
, ...)
398 char data
[VIRUSFILTER_IO_BUFFER_SIZE
+ VIRUSFILTER_IO_EOL_SIZE
];
401 va_start(ap
, data_fmt
);
402 data_size
= vsnprintf(data
, VIRUSFILTER_IO_BUFFER_SIZE
, data_fmt
, ap
);
405 if (unlikely (data_size
< 0)) {
406 DBG_ERR("vsnprintf failed: %s\n", strerror(errno
));
410 memcpy(data
+ data_size
, io_h
->w_eol
, io_h
->w_eol_size
);
411 data_size
+= io_h
->w_eol_size
;
413 return virusfilter_io_write(io_h
, data
, data_size
);
416 bool virusfilter_io_vwritefl(
417 struct virusfilter_io_handle
*io_h
,
418 const char *data_fmt
, va_list ap
)
420 char data
[VIRUSFILTER_IO_BUFFER_SIZE
+ VIRUSFILTER_IO_EOL_SIZE
];
423 data_size
= vsnprintf(data
, VIRUSFILTER_IO_BUFFER_SIZE
, data_fmt
, ap
);
425 if (unlikely (data_size
< 0)) {
426 DBG_ERR("vsnprintf failed: %s\n", strerror(errno
));
430 memcpy(data
+ data_size
, io_h
->w_eol
, io_h
->w_eol_size
);
431 data_size
+= io_h
->w_eol_size
;
433 return virusfilter_io_write(io_h
, data
, data_size
);
436 bool virusfilter_io_writev(
437 struct virusfilter_io_handle
*io_h
, ...)
440 struct iovec iov
[VIRUSFILTER_IO_IOV_MAX
], *iov_p
;
444 for (iov_p
= iov
, iov_n
= 0;
445 iov_n
< VIRUSFILTER_IO_IOV_MAX
;
448 iov_p
->iov_base
= va_arg(ap
, void *);
449 if (iov_p
->iov_base
== NULL
) {
452 iov_p
->iov_len
= va_arg(ap
, int);
456 return write_data_iov_timeout(io_h
->stream
, iov
, iov_n
,
460 bool virusfilter_io_writevl(
461 struct virusfilter_io_handle
*io_h
, ...)
464 struct iovec iov
[VIRUSFILTER_IO_IOV_MAX
+ 1], *iov_p
;
468 for (iov_p
= iov
, iov_n
= 0; iov_n
< VIRUSFILTER_IO_IOV_MAX
;
471 iov_p
->iov_base
= va_arg(ap
, void *);
472 if (iov_p
->iov_base
== NULL
) {
475 iov_p
->iov_len
= va_arg(ap
, int);
479 iov_p
->iov_base
= io_h
->r_eol
;
480 iov_p
->iov_len
= io_h
->r_eol_size
;
483 return write_data_iov_timeout(io_h
->stream
, iov
, iov_n
,
487 static bool return_existing_line(TALLOC_CTX
*ctx
,
488 struct virusfilter_io_handle
*io_h
,
491 size_t read_line_len
= 0;
495 eol
= memmem(io_h
->r_buffer
, io_h
->r_len
,
496 io_h
->r_eol
, io_h
->r_eol_size
);
500 end_p
= eol
+ io_h
->r_eol_size
;
503 read_line_len
= strlen(io_h
->r_buffer
) + 1;
504 *read_line
= talloc_memdup(ctx
,
507 if (*read_line
== NULL
) {
512 * Copy the remaining buffer over the line
515 memmove(io_h
->r_buffer
,
517 io_h
->r_len
- (end_p
- io_h
->r_buffer
));
519 /* And reduce the size left in the buffer. */
520 io_h
->r_len
-= (end_p
- io_h
->r_buffer
);
524 static void readv_done(struct tevent_req
*req
)
526 uint64_t *perr
= tevent_req_callback_data(req
, uint64_t);
530 ret
= tstream_readv_recv(req
, &err_ret
);
537 bool virusfilter_io_readl(TALLOC_CTX
*ctx
,
538 struct virusfilter_io_handle
*io_h
,
541 struct tevent_context
*ev
= NULL
;
543 uint64_t *perror
= NULL
;
544 TALLOC_CTX
*frame
= talloc_stackframe();
546 /* Search for an existing complete line. */
547 ok
= return_existing_line(ctx
, io_h
, read_line
);
553 * No complete line in the buffer. We must read more
556 ev
= tevent_context_init(frame
);
558 DBG_ERR("Failed to setup event context.\n");
562 /* Error return - must be talloc'ed. */
563 perror
= talloc_zero(frame
, uint64_t);
564 if (perror
== NULL
) {
570 size_t read_size
= 0;
572 struct tevent_req
*req
= NULL
;
575 * How much can we read ?
577 pending
= tstream_pending_bytes(io_h
->stream
);
579 DBG_ERR("tstream_pending_bytes failed (%s).\n",
585 /* Must read at least one byte. */
586 read_size
= MIN(read_size
, 1);
588 /* And max remaining buffer space. */
589 read_size
= MAX(read_size
,
590 (sizeof(io_h
->r_buffer
) - io_h
->r_len
));
592 if (read_size
== 0) {
593 /* Buffer is full with no EOL. Error out. */
594 DBG_ERR("Line buffer full.\n");
598 iov
.iov_base
= io_h
->r_buffer
+ io_h
->r_len
;
599 iov
.iov_len
= read_size
;
602 req
= tstream_readv_send(frame
,
608 DBG_ERR("out of memory.\n");
612 /* Callback when *all* data read. */
613 tevent_req_set_callback(req
, readv_done
, perror
);
616 ok
= tevent_req_set_endtime(req
, ev
,
617 timeval_current_ofs_msec(io_h
->io_timeout
));
619 DBG_ERR("can't set endtime\n");
623 /* Loop waiting for req to finish. */
624 ok
= tevent_req_poll(req
, ev
);
626 DBG_ERR("tevent_req_poll failed\n");
630 /* Done with req - freed by the callback. */
634 * Emit debug error if failed.
635 * EPIPE may be success so, don't exit.
637 if (*perror
!= 0 && *perror
!= EPIPE
) {
638 DBG_DEBUG("Error %s\n", strerror((int)*perror
));
639 errno
= (int)*perror
;
644 * We read read_size bytes. Extend the useable
647 io_h
->r_len
+= read_size
;
650 SMB_ASSERT(io_h
->r_len
<= sizeof(io_h
->r_buffer
));
652 /* Exit if we have a line to return. */
653 ok
= return_existing_line(ctx
, io_h
, read_line
);
657 /* No eol - keep reading. */
666 bool virusfilter_io_writefl_readl(
667 struct virusfilter_io_handle
*io_h
,
669 const char *fmt
, ...)
677 ok
= virusfilter_io_vwritefl(io_h
, fmt
, ap
);
685 ok
= virusfilter_io_readl(talloc_tos(), io_h
, read_line
);
687 DBG_ERR("virusfilter_io_readl not OK: %d\n", ok
);
690 if (io_h
->r_len
== 0) { /* EOF */
691 DBG_ERR("virusfilter_io_readl EOF\n");
698 struct virusfilter_cache
*virusfilter_cache_new(
703 struct virusfilter_cache
*cache
;
705 if (time_limit
== 0) {
709 cache
= talloc_zero(ctx
, struct virusfilter_cache
);
711 DBG_ERR("talloc_zero failed.\n");
715 cache
->cache
= memcache_init(cache
->ctx
, entry_limit
*
716 (sizeof(struct virusfilter_cache_entry
)
717 + VIRUSFILTER_CACHE_BUFFER_SIZE
));
718 if (cache
->cache
== NULL
) {
719 DBG_ERR("memcache_init failed.\n");
723 cache
->time_limit
= time_limit
;
728 bool virusfilter_cache_entry_add(
729 struct virusfilter_cache
*cache
,
730 const char *directory
,
732 virusfilter_result result
,
735 int blob_size
= sizeof(struct virusfilter_cache_entry
);
736 struct virusfilter_cache_entry
*cache_e
=
737 talloc_zero_size(NULL
, blob_size
);
740 if (fname
== NULL
|| directory
== NULL
) {
745 fname
= talloc_asprintf(talloc_tos(), "%s/%s", directory
, fname
);
752 fname_len
= strlen(fname
);
754 if (cache_e
== NULL
|| cache
->time_limit
== 0) {
759 cache_e
->result
= result
;
760 if (report
!= NULL
) {
761 cache_e
->report
= talloc_steal(cache_e
, report
);
763 if (cache
->time_limit
> 0) {
764 cache_e
->time
= time(NULL
);
767 memcache_add_talloc(cache
->cache
,
768 VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
769 data_blob_const(fname
, fname_len
), &cache_e
);
774 bool virusfilter_cache_entry_rename(
775 struct virusfilter_cache
*cache
,
776 const char *directory
,
780 int old_fname_len
= 0;
781 int new_fname_len
= 0;
782 struct virusfilter_cache_entry
*new_data
= NULL
;
783 struct virusfilter_cache_entry
*old_data
= NULL
;
785 if (old_fname
== NULL
|| new_fname
== NULL
|| directory
== NULL
) {
789 old_fname
= talloc_asprintf(talloc_tos(), "%s/%s", directory
, old_fname
);
790 new_fname
= talloc_asprintf(talloc_tos(), "%s/%s", directory
, new_fname
);
792 if (old_fname
== NULL
|| new_fname
== NULL
) {
793 TALLOC_FREE(old_fname
);
794 TALLOC_FREE(new_fname
);
798 old_fname_len
= strlen(old_fname
);
799 new_fname_len
= strlen(new_fname
);
801 old_data
= memcache_lookup_talloc(
803 VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
804 data_blob_const(old_fname
, old_fname_len
));
806 if (old_data
== NULL
) {
810 new_data
= talloc_memdup(cache
->ctx
, old_data
,
811 sizeof(struct virusfilter_cache_entry
));
812 if (new_data
== NULL
) {
815 new_data
->report
= talloc_strdup(new_data
, old_data
->report
);
817 memcache_add_talloc(cache
->cache
,
818 VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
819 data_blob_const(new_fname
, new_fname_len
), &new_data
);
821 memcache_delete(cache
->cache
, VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
822 data_blob_const(old_fname
, old_fname_len
));
827 void virusfilter_cache_purge(struct virusfilter_cache
*cache
)
829 memcache_flush(cache
->cache
, VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
);
832 struct virusfilter_cache_entry
*virusfilter_cache_get(
833 struct virusfilter_cache
*cache
,
834 const char *directory
,
838 struct virusfilter_cache_entry
*cache_e
= NULL
;
839 struct virusfilter_cache_entry
*data
= NULL
;
841 if (fname
== NULL
|| directory
== NULL
) {
845 fname
= talloc_asprintf(talloc_tos(), "%s/%s", directory
, fname
);
851 fname_len
= strlen(fname
);
853 data
= memcache_lookup_talloc(cache
->cache
,
854 VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
855 data_blob_const(fname
, fname_len
));
861 if (cache
->time_limit
> 0) {
862 if (time(NULL
) - data
->time
> cache
->time_limit
) {
863 DBG_DEBUG("Cache entry is too old: %s\n",
865 virusfilter_cache_remove(cache
, directory
, fname
);
869 cache_e
= talloc_memdup(cache
->ctx
, data
,
870 sizeof(struct virusfilter_cache_entry
));
871 if (cache_e
== NULL
) {
874 if (data
->report
!= NULL
) {
875 cache_e
->report
= talloc_strdup(cache_e
, data
->report
);
877 cache_e
->report
= NULL
;
883 void virusfilter_cache_remove(struct virusfilter_cache
*cache
,
884 const char *directory
,
887 DBG_DEBUG("Purging cache entry: %s/%s\n", directory
, fname
);
889 if (fname
== NULL
|| directory
== NULL
) {
893 fname
= talloc_asprintf(talloc_tos(), "%s/%s", directory
, fname
);
899 memcache_delete(cache
->cache
, VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC
,
900 data_blob_const(fname
, strlen(fname
)));
903 void virusfilter_cache_entry_free(struct virusfilter_cache_entry
*cache_e
)
905 if (cache_e
!= NULL
) {
906 TALLOC_FREE(cache_e
->report
);
907 cache_e
->report
= NULL
;
909 TALLOC_FREE(cache_e
);
913 * ======================================================================
916 int virusfilter_env_set(
925 env_new
= talloc_asprintf(mem_ctx
, "%s=%s", name
, value
);
926 if (env_new
== NULL
) {
927 DBG_ERR("talloc_asprintf failed\n");
931 ret
= strv_add(mem_ctx
, env_list
, env_new
);
933 TALLOC_FREE(env_new
);
938 /* virusfilter_env version Samba's *_sub_advanced() in substitute.c */
939 int virusfilter_shell_set_conn_env(
942 connection_struct
*conn
)
944 int snum
= SNUM(conn
);
947 const char *local_machine_name
= get_local_machine_name();
951 if (local_machine_name
== NULL
|| *local_machine_name
== '\0') {
952 local_machine_name
= lp_netbios_name();
955 server_addr_p
= tsocket_address_inet_addr_string(
956 conn
->sconn
->local_address
, talloc_tos());
958 if (server_addr_p
!= NULL
) {
959 ret
= strncmp("::ffff:", server_addr_p
, 7);
963 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_SERVER_IP",
966 TALLOC_FREE(server_addr_p
);
968 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_SERVER_NAME",
970 virusfilter_env_set(mem_ctx
, env_list
,
971 "VIRUSFILTER_SERVER_NETBIOS_NAME",
973 slprintf(pidstr
,sizeof(pidstr
)-1, "%ld", (long)getpid());
974 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_SERVER_PID",
977 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_SERVICE_NAME",
978 lp_const_servicename(snum
));
979 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_SERVICE_PATH",
980 conn
->cwd_fname
->base_name
);
982 client_addr_p
= tsocket_address_inet_addr_string(
983 conn
->sconn
->remote_address
, talloc_tos());
985 if (client_addr_p
!= NULL
) {
986 ret
= strncmp("::ffff:", client_addr_p
, 7);
990 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_CLIENT_IP",
993 TALLOC_FREE(client_addr_p
);
995 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_CLIENT_NAME",
996 conn
->sconn
->remote_hostname
);
997 virusfilter_env_set(mem_ctx
, env_list
,
998 "VIRUSFILTER_CLIENT_NETBIOS_NAME",
999 get_remote_machine_name());
1001 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_USER_NAME",
1002 get_current_username());
1003 virusfilter_env_set(mem_ctx
, env_list
, "VIRUSFILTER_USER_DOMAIN",
1004 current_user_info
.domain
);
1009 /* Wrapper to Samba's smbrun() in smbrun.c */
1010 int virusfilter_shell_run(
1011 TALLOC_CTX
*mem_ctx
,
1014 connection_struct
*conn
,
1020 ret
= virusfilter_shell_set_conn_env(mem_ctx
, env_list
, conn
);
1027 return smbrun(cmd
, NULL
, strv_to_env(talloc_tos(), *env_list
));
1029 return smbrun_no_sanitize(cmd
, NULL
, strv_to_env(talloc_tos(),