Initial commit
Benjamin Renard

Benjamin Renard commited on 2016-01-19 19:51:30
Showing 12 changed files, with 340 additions and 0 deletions.

... ...
@@ -0,0 +1,4 @@
1
+Suite of scripts to manage Postfix mailq and logs
2
+=================================================
3
+
4
+FIXME
... ...
@@ -0,0 +1,8 @@
1
+#!/bin/bash
2
+
3
+[ ! -n "$1" ] && echo "Usage : $0 mail" && exit 1
4
+
5
+for mail in $( mailq|egrep '^[A-Z0-9]'|grep "$1"|awk '{print $1}'|sed 's/^\([0-9A-Z]*\).*$/\1/' )
6
+do
7
+	/usr/sbin/postcat -q $mail
8
+done
... ...
@@ -0,0 +1,34 @@
1
+#!/bin/bash
2
+
3
+DROPED=0
4
+CUR="XXXXXXXXXXXXX"
5
+IFS="
6
+"
7
+for line in $( mailq )
8
+do
9
+	if [ $( echo "$line"|egrep -c '^[A-Z0-9]+' ) -eq 1 ]
10
+	then
11
+		CUR=$( echo "$line"|sed 's/^\([A-Z0-9]*\).*$/\1/' )
12
+		DROPED=0
13
+		if [ ! -n "$CUR" ]
14
+		then
15
+			echo -e "ERROR : unable to isolate mail ID from this line :\n\n\t$line"
16
+			exit 3
17
+		fi
18
+	elif [ $DROPED -eq 1 ]
19
+	then
20
+		continue
21
+	elif [ $( echo "$line"|grep -c "^ .*@" ) -gt 0 ]
22
+	then
23
+		for domain in frre.fr gemail.com gmaiil.com gmail.co gmail.de gmail.fr gmail.om gmamil.com gmmail.com hayoo.fr homail.com hootmail.com hormail.com hotail.com hotamil.com hotmaill.com hotmeil.com hoymail.com hptmail.com lapose.net lapostre.net ornage.fr ornge.fr outlokk.com outloook.com rockatmail.com sfrl.fr wanado.fr wandoo.fr yacho.com yahoo.cm yaohoo.fr
24
+		do
25
+			if [ $( echo "$line"|grep -c "@$domain" ) -gt 0 ]
26
+			then
27
+				#postsuper -d "$CUR"
28
+				echo $CUR
29
+				DROPED=1
30
+				break
31
+			fi
32
+		done
33
+	fi
34
+done
... ...
@@ -0,0 +1,8 @@
1
+#!/bin/bash
2
+
3
+[ ! -n "$1" ] && echo "Usage : $0 mail" && exit 1
4
+
5
+for mail in $( mailq|egrep '^[A-Z0-9]'|grep "$1"|awk '{print $1}'|sed 's/^\([0-9A-Z]*\).*$/\1/' )
6
+do
7
+	/usr/sbin/postsuper -d $mail
8
+done
... ...
@@ -0,0 +1,32 @@
1
+#!/bin/bash
2
+
3
+SENDER="$1"
4
+[ -z "$SENDER" ] && echo "Usage : $0 [sender]" && exit 1
5
+
6
+for ID in $( mail_from_sender "$SENDER" )
7
+do
8
+	SUBJECT=$( postcat -q $ID|egrep '^Subject:' )
9
+	while true
10
+	do
11
+		echo $SUBJECT
12
+		echo -n "$ID : (c = cat mail / d = delete mail / K = keap mail / q = quit ) ? "
13
+		read a
14
+		case $a in
15
+			c)
16
+				postcat -q $ID|less
17
+			;;
18
+			d)
19
+				postsuper -d $ID
20
+				break
21
+			;;
22
+			k|"")
23
+				break
24
+			;;
25
+			q)
26
+				exit 0
27
+			;;
28
+			*)
29
+				echo "Bad choice !"
30
+		esac
31
+	done
32
+done
... ...
@@ -0,0 +1,33 @@
1
+#!/bin/bash
2
+
3
+USERNAME="$1"
4
+[ -z "$USERNAME" ] && echo "Usage : $0 [username]" && exit 1
5
+
6
+echo "Search deferred mail from user $USERNAME ..."
7
+for ID in $( username_to_deferred_mailid "$USERNAME" )
8
+do
9
+	SENDER=$( mailq|egrep ^$ID|awk '{print $7}')
10
+	[ -z "$SENDER" ] && SENDER="Unkown sender"
11
+	while true
12
+	do
13
+		echo -n "$ID [$SENDER] : (c = cat mail / d = delete mail / K = keap mail / q = quit ) ? "
14
+		read a
15
+		case $a in
16
+			c)
17
+				postcat -q $ID|less
18
+			;;
19
+			d)
20
+				postsuper -d $ID
21
+				break
22
+			;;
23
+			k|"")
24
+				break
25
+			;;
26
+			q)
27
+				exit 0
28
+			;;
29
+			*)
30
+				echo "Bad choice !"
31
+		esac
32
+	done
33
+done
... ...
@@ -0,0 +1,6 @@
1
+#!/bin/bash
2
+
3
+SENDER="$1"
4
+[ -z "$SENDER" ] && echo "Usage : $0 [sender]" && exit 1
5
+
6
+mailq|egrep '^[A-Z0-9]'|grep "$SENDER"|awk '{print $1}'|tr -d '*'
... ...
@@ -0,0 +1,40 @@
1
+#!/bin/bash
2
+
3
+MAILID="$1"
4
+[ -z "$MAILID" ] && echo "Usage : $0 [mailid]" && exit 1
5
+
6
+DEBUG=0
7
+[ "$2" == "-d" ] && DEBUG=1
8
+
9
+MAX_LOG=9
10
+
11
+function search_msgid () {
12
+	MAILID="$1"
13
+	MAILLOG="$2"
14
+	[ ! -f "$MAILLOG" ] && MAILLOG="$MAILLOG.gz"
15
+	[ -f "$MAILLOG" ] && zgrep "$MAILID: message-id=" $MAILLOG|sed 's/^.*message-id=<\([^>]*\)>.*$/\1/'
16
+}
17
+
18
+[ $DEBUG -eq 1 ] && echo "Search message ID from mail ID $MAILID ..."
19
+res=$( search_msgid "$MAILID" /var/log/mail.log )
20
+if [ -n "$res" ]
21
+then
22
+	echo "$res"
23
+	exit 0
24
+else
25
+	[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)"
26
+	for i in $( seq 1 $MAX_LOG )
27
+	do
28
+		res=$( search_msgid "$MAILID" /var/log/mail.log.$i )
29
+		if [ -n "$res" ]
30
+		then
31
+			[ $DEBUG -eq 1 ] && echo "Found from log file /var/log/mail.log.$i"
32
+			echo "$res"
33
+			exit 0
34
+		else
35
+			[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i"
36
+		fi
37
+	done
38
+	[ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)"
39
+fi
40
+exit 1
... ...
@@ -0,0 +1,101 @@
1
+#!/bin/bash
2
+
3
+MAILID=$1
4
+[ -z "$MAILID" ] && echo "Usage : $0 [mailid]" && exit 1
5
+
6
+DEBUG=0
7
+[ "$2" == "-d" ] && DEBUG=1
8
+
9
+MAX_LOG=9
10
+
11
+HOSTNAME=$( hostname -f )
12
+
13
+function search_user () {
14
+	MAILID=$1
15
+	MAILLOG=$2
16
+	[ ! -f "$MAILLOG" ] && MAILLOG=$MAILLOG.gz
17
+	[ -f "$MAILLOG" ] && zgrep $MAILID $MAILLOG|grep 'sasl_username='|sed 's/^.*sasl_username=//'
18
+}
19
+
20
+function search_user_from_mailid() {
21
+	MAILID=$1
22
+	[ $DEBUG -eq 1 ] && echo "Search username from Mail ID $MAILID ..."
23
+	res=$( search_user $MAILID /var/log/mail.log )
24
+	if [ -n "$res" ]
25
+	then
26
+		echo "$res"
27
+		exit 0
28
+	else
29
+		[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)"
30
+		for i in $( seq 1 $MAX_LOG )
31
+		do
32
+			res=$( search_user $MAILID /var/log/mail.log.$i )
33
+			if [ -n "$res" ]
34
+			then
35
+				echo "$res"
36
+				exit 0
37
+			else
38
+				[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i"
39
+			fi
40
+		done
41
+		[ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)"
42
+	fi
43
+}
44
+
45
+if [ $( mailq|egrep -c ^$MAILID ) -ne 0 ]
46
+then
47
+	[ $DEBUG -eq 1 ] && echo "Mail $MAILID still in mailq. Try to detect original mail ID ..."
48
+	ORIGMAILID=$( postcat -q "$MAILID" |grep "by $HOSTNAME"|tail -n 1|sed 's/^.*by .* with.* id \([A-Z0-9]*\).*$/\1/' )
49
+	if [ -n "$ORIGMAILID" ]
50
+	then
51
+		if [ "$ORIGMAILID" != "$MAILID" ]
52
+		then
53
+			[ $DEBUG -eq 1 ] && echo "Original mail ID : $ORIGMAILID"
54
+			search_user_from_mailid "$ORIGMAILID"
55
+			[ $DEBUG -eq 1 ] && echo "User not found from original mail ID"
56
+			exit 1
57
+		else
58
+			[ $DEBUG -eq 1 ] && echo "Mail ID and original mail ID are equals"
59
+		fi
60
+	else
61
+		[ $DEBUG -eq 1 ] && echo "Fail to detect original mail ID. Continue with mail ID in parameter"
62
+	fi
63
+fi
64
+
65
+search_user_from_mailid "$MAILID"
66
+[ -n "$ORIGMAILID" ] && echo "User not found" && exit 1
67
+
68
+[ $DEBUG -eq 1 ] && echo "User not found. Try to detect original mail ID with message ID in log"
69
+
70
+if [ $DEBUG -eq 1 ]
71
+then
72
+	MSGID=$( mailid_to_messageid "$MAILID" -d )
73
+else
74
+	MSGID=$( mailid_to_messageid "$MAILID" )
75
+fi
76
+
77
+if [ -z "$MSGID" ]
78
+then
79
+	[ $DEBUG -eq 1 ] && echo "Message ID not found from log."
80
+	exit 1
81
+fi
82
+
83
+[ $DEBUG -eq 1 ] && echo "Message ID found : $MSGID. Try to detect original mail ID with this message ID"
84
+
85
+if [ $DEBUG -eq 1 ]
86
+then
87
+	ORIGMAILID=$( messageid_to_original_mailid "$MAILID" -d )
88
+else
89
+	ORIGMAILID=$( messageid_to_original_mailid "$MAILID" )
90
+fi
91
+
92
+if [ -z "$ORIGMAILID" ]
93
+then
94
+	[ $DEBUG -eq 1 ] && echo "Original mail ID not found from message ID"
95
+	exit 1
96
+fi
97
+
98
+search_user_from_mailid "$ORIGMAILID"
99
+[ $DEBUG -eq 1 ] && echo "Username not found from original mail ID"
100
+
101
+exit 1
... ...
@@ -0,0 +1,40 @@
1
+#!/bin/bash
2
+
3
+MSGID=$1
4
+[ -z "$MSGID" ] && echo "Usage : $0 [message-id]" && exit 1
5
+
6
+DEBUG=0
7
+[ "$2" == "-d" ] && DEBUG=1
8
+
9
+MAX_LOG=9
10
+
11
+function search_mailid () {
12
+	MSGID=$1
13
+	MAILLOG="$2"
14
+	[ ! -f "$MAILLOG" ] && MAILLOG="$MAILLOG.gz"
15
+	[ -f "$MAILLOG" ] && zgrep $MSGID $MAILLOG|head -n 1|sed 's/^.*\: \([^\:]*\): message-id.*$/\1/'
16
+}
17
+
18
+[ $DEBUG -eq 1 ] && echo "Search mail ID from message ID $MSGID ..."
19
+res=$( search_mailid "$MSGID" /var/log/mail.log )
20
+if [ -n "$res" ]
21
+then
22
+	echo "$res"
23
+	exit 0
24
+else
25
+	[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)"
26
+	for i in $( seq 1 $MAX_LOG )
27
+	do
28
+		res=$( search_mailid "$MSGID" /var/log/mail.log.$i )
29
+		if [ -n "$res" ]
30
+		then
31
+			[ $DEBUG -eq 1 ] && echo "Found from log file /var/log/mail.log.$i"
32
+			echo "$res"
33
+			exit 0
34
+		else
35
+			[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i"
36
+		fi
37
+	done
38
+	[ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)"
39
+fi
40
+exit 1
... ...
@@ -0,0 +1,12 @@
1
+#!/bin/bash
2
+
3
+USERNAME="$1"
4
+[ -z "$USERNAME" ] && echo "Usage : $0 [username]" && exit 1
5
+
6
+for id in $( username_to_original_mailid "$USERNAME" )
7
+do
8
+	for f in $( grep -r -l "$id" /var/spool/postfix/deferred/ )
9
+	do
10
+		basename "$f"
11
+	done
12
+done
... ...
@@ -0,0 +1,22 @@
1
+#!/bin/bash
2
+
3
+USERNAME="$1"
4
+[ -z "$USERNAME" ] && echo "Usage : $0 [username] [-d]" && exit 1
5
+
6
+DEBUG=0
7
+[ "$2" == "-d" ] && DEBUG=1
8
+
9
+MAX_LOG=9
10
+
11
+function search_user () {
12
+	USER="$1"
13
+	MAILLOG="$2"
14
+	[ ! -f "$MAILLOG" ] && MAILLOG="$MAILLOG.gz"
15
+        [ -f "$MAILLOG" ] && zgrep "sasl_username=$1" $MAILLOG|sed 's/.*: \([A-Z0-9]*\): client=.*$/\1/'
16
+}
17
+
18
+search_user $USERNAME /var/log/mail.log
19
+for i in $( seq 1 $MAX_LOG )
20
+do
21
+	search_user $USERNAME /var/log/mail.log.$i
22
+done
0 23