-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmail
90 lines (72 loc) · 3.03 KB
/
sendmail
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
#!/bin/bash
#########################################
#NRW Nullmailer Rewrite Wrapper V1.3
#Sets all mails to come from [email protected]
#Replaces all local mail to Admin_Address, prevents that e.g. [email protected] gets the mail intented for you
#Version 1.1 23.03.2019
#Changelog
#Bugfixes, if [ -z "$found" ] '"' were missing
#Mails to localUsers like from cron are now rewritten
#Changelog V1.2 28.03.2019
#Bugfix Mails from "root" to "root" are now re-written correctly
#Changelog V1.3 05.01.2023
#Bugfix rewriting mails for root@<hostname> works now
#########################################
#Begin change here
#Where should the mails come from
#example mail address: [email protected]
export NULLMAILER_USER=yourmailaddressprefix #example: my
export NULLMAILER_HOST=yourMailDomain #example: company.com
#Where should mails go that are directed for local users like "root" or "pi"
[email protected] # e.g. [email protected] or [email protected] This address is idependent of sender address
#End change here
#############################################
export NULLMAILER_FLAGS=ft
stdin=`cat`
found=$(echo "$stdin" | head -n5 | grep -i ^to:.*@.*) #Search whether in To @ is present if not, we have to rewrite address. Only To: in the first 5 lines is searched
if [ ! -z "$found" ]
then
#check if @hostname (root@hostname or [email protected]), then we have to rewrite
localuser=$(echo "$stdin" | head -n5 | grep -i ^to:.*@${HOSTNAME}[.].*)
if [ ! -z "$localuser" ]
then
found=""
fi
fi
if [ -z "$found" ]
then
userToReplace=$(echo "$stdin" | head -n5 | grep -i -P "(?<=^To: ).*")
replaceString="To: $Admin_Address"
stdin="${stdin/$userToReplace/$replaceString}"
fi
optPosition=0 #defaultvalue when not found
while getopts ":f:" from; do
case $from in
f) optPosition=$OPTIND
break
;;
esac
done
arguments=($@)
if (($optPosition != 0)); then #-f Option was used
arguments[(($optPosition-2))]=$NULLMAILER_USER@$NULLMAILER_HOST
set -- "${arguments[@]}"
fi
#case where user is specified in commandline, example from cron "-i -FCronDaemon -B8BITMIME -oem pi"
#now we search for strings in command line at the end without "@" and replace them with Admin_Address
#we also could use another Wrapper-Script for nullmailer-inject adding the -h option meaning using only message header as recipient address
#see http://manpages.ubuntu.com/manpages/trusty/man1/nullmailer-inject.1.html
localRecipient=$(echo "$@" | grep -Po '\s(\w)+$')
if [ -n "$localRecipient" ] #we must take action and rewrite
then
arguments="$@"
lengthArgs=$(expr length "$arguments")
lengthRecipient=$(expr length "$localRecipient")
count=$((lengthArgs-lengthRecipient))
argsNew=$(expr substr "$arguments" 1 $count)
argsNew="$argsNew $Admin_Address"
set -- $argsNew
#Just to mention, original args are not directly preserved if someone would have called sendmail "-foo bar"
#in original "-foo bar" was in $1, now in $1 would be "-foo" and in $2 "bar". However here this shouldn't matter
fi
echo "$stdin" | /usr/sbin/sendmail-bin "$@"