Samba-VirusFilter: common headers and sources.
[bbaumbach/samba-autobuild/.git] / examples / scripts / vfs / virusfilter / virusfilter-notify.ksh
1 #!/bin/ksh
2 ##
3 ## Samba-VirusFilter VFS modules
4 ## Copyright (C) 2010-2016 SATOH Fumiyasu @ OSS Technology Corp., Japan
5 ##
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.
10 ##
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.
15 ##
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/>.
18 ##
19
20 set -u
21
22 pdie() { echo "$0: ERROR: ${1-}" 1>&2; exit "${2-1}"; }
23
24 ## ======================================================================
25
26 sendmail="${VIRUSFILTER_NOTIFY_SENDMAIL_COMMAND:-/usr/sbin/sendmail}"
27 sendmail_opts="${VIRUSFILTER_NOTIFY_SENDMAIL_OPTIONS:-}"
28
29 smbclient="${VIRUSFILTER_NOTIFY_SMBCLIENT_COMMAND:-@SAMBA_BINDIR@/smbclient}"
30 smbclient_opts="${VIRUSFILTER_NOTIFY_SMBCLIENT_OPTIONS:-}"
31
32 ## ======================================================================
33
34 if [ -n "${VIRUSFILTER_RESULT_IS_CACHE-}" ]; then
35   ## Result is cache. Ignore!
36   exit 0
37 fi
38
39 if [ ! -t 1 ] && [ -z "${VIRUSFILTER_NOTIFY_BG-}" ]; then
40   export VIRUSFILTER_NOTIFY_BG=1
41   "$0" ${1+"$@"} </dev/null >/dev/null &
42   exit 0
43 fi
44
45 ## ----------------------------------------------------------------------
46
47 if [ -n "${VIRUSFILTER_INFECTED_FILE_ACTION-}" ]; then
48   report="$VIRUSFILTER_INFECTED_FILE_REPORT"
49 else
50   report="$VIRUSFILTER_SCAN_ERROR_REPORT"
51 fi
52
53 if [ X"$VIRUSFILTER_SERVER_NAME" != X"$VIRUSFILTER_SERVER_IP" ]; then
54   server_name="$VIRUSFILTER_SERVER_NAME"
55 else
56   server_name="$VIRUSFILTER_SERVER_NETBIOS_NAME"
57 fi
58
59 if [ X"$VIRUSFILTER_CLIENT_NAME" != X"$VIRUSFILTER_CLIENT_IP" ]; then
60   client_name="$VIRUSFILTER_CLIENT_NAME"
61 else
62   client_name="$VIRUSFILTER_CLIENT_NETBIOS_NAME"
63 fi
64
65 mail_to=""
66 winpopup_to=""
67 subject_prefix=""
68 sender=""
69 from=""
70 cc=""
71 bcc=""
72 content_type="text/plain"
73 content_encoding="UTF-8"
74
75 cmd_usage="Usage: $0 [OPTIONS]
76
77 Options:
78   --mail-to ADDRESS
79     Send a notice message to this e-mail address(es)
80   --winpopup-to NAME
81     Send a \"WinPopup\" message to this NetBIOS name
82   --sender ADDRESS
83     Envelope sender address for mail
84   --from ADDRESS
85     From: e-mail address for mail
86   --cc ADDRESS
87     Cc: e-mail address(es) for mail
88   --bcc ADDRESS
89     Bcc: e-mail address(es) for mail
90   --subject-prefix PREFIX
91     Subject: prefix string for mail
92   --content-type TYPE
93   --content-encoding ENCODING
94     Content-Type: TYPE; charset=\"ENCODING\" for mail [$content_type; charset=\"$content_encoding\"]
95   --header-file FILE
96     Prepend the content of FILE to the message
97   --footer-file FILE
98     Append the content of FILE to the message
99 "
100
101 ## ----------------------------------------------------------------------
102
103 getopts_want_arg()
104 {
105   if [ "$#" -lt 2 ]; then
106     pdie "Option requires an argument: $1"
107   fi
108   if [ "$#" -ge 3 ]; then
109     if expr x"$2" : x"$3\$" >/dev/null; then
110       : OK
111     else
112       pdie "Invalid value for option: $1 $2"
113     fi
114   fi
115 }
116
117 while [ "$#" -gt 0 ]; do
118   OPT="$1"; shift
119   case "$OPT" in
120   --help)
121     echo "$cmd_usage"
122     exit 0
123     ;;
124   --mail-to)
125     getopts_want_arg "$OPT" ${1+"$1"}
126     mail_to="${mail_to:+$mail_to, }$1"; shift
127     ;;
128   --winpopup-to)
129     getopts_want_arg "$OPT" ${1+"$1"}
130     winpopup_to="$1"; shift
131     ;;
132   --sender)
133     getopts_want_arg "$OPT" ${1+"$1"}
134     sender="$1"; shift
135     ;;
136   --from)
137     getopts_want_arg "$OPT" ${1+"$1"}
138     from="$1"; shift
139     ;;
140   --cc)
141     getopts_want_arg "$OPT" ${1+"$1"}
142     cc="${cc:+$cc, }$1"; shift
143     ;;
144   --bcc)
145     getopts_want_arg "$OPT" ${1+"$1"}
146     bcc="${bcc:+$bcc, }$1"; shift
147     ;;
148   --subject-prefix)
149     getopts_want_arg "$OPT" ${1+"$1"}
150     subject_prefix="$1"; shift
151     ;;
152   --content-type)
153     getopts_want_arg "$OPT" ${1+"$1"}
154     content_type="$1"; shift
155     ;;
156   --content-encoding)
157     getopts_want_arg "$OPT" ${1+"$1"}
158     content_encoding="$1"; shift
159     ;;
160   --header-file)
161     getopts_want_arg "$OPT" ${1+"$1"}
162     header_file="$1"; shift
163     ;;
164   --footer-file)
165     getopts_want_arg "$OPT" ${1+"$1"}
166     footer_file="$1"; shift
167     ;;
168   --)
169     break
170     ;;
171   -*)
172     pdie "Invalid option: $OPT"
173     ;;
174   *)
175     set -- "$OPT" ${1+"$@"}
176     break
177     ;;
178   esac
179 done
180
181 [ -z "$sender" ] && sender="$from"
182 subject="$subject_prefix$report"
183
184 ## ======================================================================
185
186 msg_header="\
187 Subject: $subject
188 Content-Type: $content_type; charset=$content_encoding
189 X-VIRUSFILTER-Version: $VIRUSFILTER_VERSION
190 X-VIRUSFILTER-Module-Name: $VIRUSFILTER_MODULE_NAME
191 "
192
193 if [ -n "${VIRUSFILTER_MODULE_VERSION-}" ]; then
194   msg_header="${msg_header}\
195 X-VIRUSFILTER-Module-Version: $VIRUSFILTER_MODULE_VERSION
196 "
197 fi
198
199 if [ -n "${from-}" ]; then
200   msg_header="${msg_header}\
201 From: $from
202 "
203 fi
204
205 if [ -n "${mail_to-}" ]; then
206   msg_header="${msg_header}\
207 To: $mail_to
208 "
209 fi
210
211 if [ -n "${cc-}" ]; then
212   msg_header="${msg_header}\
213 Cc: $cc
214 "
215 fi
216
217 if [ -n "${bcc-}" ]; then
218   msg_header="${msg_header}\
219 Bcc: $bcc
220 "
221 fi
222
223 ## ----------------------------------------------------------------------
224
225 msg_body=""
226
227 if [ -n "${header_file-}" ] && [ -f "$header_file" ]; then
228   msg_body="${msg_body}\
229 `cat "$header_file"`
230 "
231 fi
232
233 msg_body="${msg_body}\
234 Server: $server_name ($VIRUSFILTER_SERVER_IP)
235 Server PID: $VIRUSFILTER_SERVER_PID
236 Service name: $VIRUSFILTER_SERVICE_NAME
237 Service path: $VIRUSFILTER_SERVICE_PATH
238 Client: $client_name ($VIRUSFILTER_CLIENT_IP)
239 User: $VIRUSFILTER_USER_DOMAIN\\$VIRUSFILTER_USER_NAME
240 "
241
242 if [ -n "${VIRUSFILTER_INFECTED_FILE_ACTION-}" ]; then
243   msg_body="${msg_body}\
244 Infected file report: $VIRUSFILTER_INFECTED_FILE_REPORT
245 "
246   msg_body="${msg_body}\
247 Infected file path: $VIRUSFILTER_SERVICE_PATH/$VIRUSFILTER_INFECTED_SERVICE_FILE_PATH
248 "
249   msg_body="${msg_body}\
250 Infected file action: $VIRUSFILTER_INFECTED_FILE_ACTION
251 "
252 else
253   msg_body="${msg_body}\
254 Scan error report: $VIRUSFILTER_SCAN_ERROR_REPORT
255 Scan error file path: $VIRUSFILTER_SERVICE_PATH/$VIRUSFILTER_SCAN_ERROR_SERVICE_FILE_PATH
256 "
257 fi
258
259 if [ -n "${VIRUSFILTER_QUARANTINED_FILE_PATH-}" ]; then
260   msg_body="${msg_body}\
261 Quarantined/Renamed file path: ${VIRUSFILTER_QUARANTINED_FILE_PATH-}
262 "
263 fi
264
265 if [ -n "${footer_file-}" ] && [ -f "$footer_file" ]; then
266   msg_body="${msg_body}\
267 `cat "$footer_file"`
268 "
269 fi
270
271 ## ======================================================================
272
273 if [ -n "$mail_to" ]; then
274   (echo "$msg_header"; echo "$msg_body") \
275     |"$sendmail" -t -i ${sender:+-f "$sender"} $sendmail_opts
276 fi
277
278 if [ -n "$winpopup_to" ]; then
279   echo "$msg_body" \
280     |"$smbclient" -M "$winpopup_to" -U% $smbclient_opts \
281     >/dev/null
282 fi
283
284 exit 0