-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_file
executable file
·109 lines (89 loc) · 2.03 KB
/
send_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/ksh
#################################################
# send_file #
#################################################
# Sends a file, Base64-encoded, as an attach- #
# ment by email, using the system sendmail #
# program. #
#################################################
# Copyright (c)2023 Sridhar K.N. Ayengar #
# Available on the MIT License. #
# Please refer to the file LICENSE. #
#################################################
base64=/usr/bin/base64
basename=/usr/bin/basename
hostname=/bin/hostname
id=/usr/bin/id
sendmail=/usr/sbin/sendmail
usage() {
echo "Usage: $0 -f <file_name> -r <recipient_email>" 1>&2
exit 1
}
HOSTNAME=$($hostname -f)
SEPARATOR=$(echo "${RANDOM}${RANDOM}${RANDOM}" | $base64)
USERID=$($id -urn)
USERNAME=$($id -F)
SENDER="${USERNAME} <${USERID}@${HOSTNAME}>"
if [ $# -ne 4 ] || [ "$1" = "$3" ]
then
usage
fi
for parameter in 1 3
do
case $(eval echo \$$parameter) in
"-f")
FILE_PATH=$(eval echo "\$$((parameter + 1))")
;;
"-r")
RECIPIENT=$(eval echo "\$$((parameter + 1))")
;;
*)
usage
;;
esac
done
if [ ! -f "${FILE_PATH}" ]
then
echo "Can only send a normal file." 1>&2
echo 1>&2
usage
fi
if [ ! -r "${FILE_PATH}" ]
then
echo "Cannot read file." 1>&2
echo 1>&2
usage
fi
FILENAME=$($basename "${FILE_PATH}")
case "${RECIPIENT}" in
"*@*@*")
echo "Invalid recipient address." 1>&2
echo 1>&2
usage
;;
"*@*")
:
;;
"*")
RECIPIENT="${RECIPIENT}@localhost"
;;
esac
$sendmail -t <<BLONG
From: ${SENDER}
To: ${RECIPIENT}
Subject: File ${FILENAME} Sent from ${HOSTNAME}
Content-Type: multipart/mixed; boundary=${SEPARATOR}
This is a multipart message in MIME format.
--${SEPARATOR}
Content-Type: text/plain
Hello. This is a file sent by the user ${USERID} on host ${HOSTNAME}.
Please, do not assume this file is safe. Plesse check or scan it before
opening.
Thank you.
--${SEPARATOR}
Content-Type: ${MIMETYPE}
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=\"${FILENAME}\"
$($base64 < "${FILE_PATH}")
--${SEPARATOR}--
BLONG