File: policy-builder.sh.in

package info (click to toggle)
zmailer 2.99.55-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,516 kB
  • ctags: 9,694
  • sloc: ansic: 120,953; sh: 3,862; makefile: 3,166; perl: 2,695; python: 115; awk: 22
file content (202 lines) | stat: -rw-r--r-- 5,443 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#! /bin/sh
#
# Sample smtp-policy-db builder script.
#
# This merges following files from $MAILVAR/db/ directory:
#	smtp-policy.src
#	localnames	         ('= _localnames')
#	smtp-policy.relay.manual ('= _full_rights')
#	smtp-policy.relay        ('= _full_rights')
#	smtp-policy.mx.manual    ('= _relaytarget')
#	smtp-policy.mx	         ('= _relaytarget')
#	smtp-policy.spam         ('= _bulk_mail')
#	smtp-policy.spam.manual  ('= _bulk_mail')
#
# These all together are used to produce files:  smtp-policy.$DBEXT
# The produced database retains the first instance of any given key.
#

#FLAG=
#while getopts n c; do
#  case $c in
#    n)       FLAG=$c;;
#    ?)       exit 2;;
#  esac
#done
#shift `expr $OPTIND - 1`

ZCONFIG=@ZMAILERCFGFILE@
. $ZCONFIG

DBDIR="$MAILVAR/db/"
USAGE="Usage: $0 [-n] [-d dbdir]"

while [ "$1" != "" ]; do
    case "$1" in
	-n)
	    FLAG=n
	    ;;
	-d)
	    shift
	    DBDIR=$1
	    if [ ! -d $DBDIR ]; then
		echo $USAGE
		exit 1
	    fi
	    ;;
	?)
	    echo $USAGE
	    exit 0
	    ;;
	*)
	    echo $USAGE
	    exit 2
	    ;;
    esac
    shift
done


umask 022

cd $DBDIR

if [ ! -f smtp-policy.src ] ; then
	echo "No $DBDIR/smtp-policy.src input file"
	exit 64 # EX_USAGE
fi

#if [ f$FLAG != fn ]; then
#    if [ -x $MAILBIN/smtp-policy-retrieve.pl ] ; then
#	$MAILBIN/smtp-policy-retrieve.pl
#    else
#	if [ -x $MAILBIN/spamlist.py -a -r spamlist_sources ] ; then
#	    $MAILBIN/spamlist.py spamlist_sources > smtp-policy.spam.new && \
#		mv  smtp-policy.spam.new smtp-policy.spam
#	else
#	    #
#	    # Following IS NOT SAFE, if either produces errors, those
#	    # go (usually) to the result file, and in the end the result
#	    # OVERWRITES the "running"  smtp-policy.spam  file.
#	    #
#	    > smtp-policy.spam.new
#	   lynx -source http://www.webeasy.com:8080/spam/spam_download_table \
#		| sed -e '1,$s/^@/./' \
#		>> smtp-policy.spam.new
#	   lynx -source http://www.sprocket.com/Security/SpamDomains | \
#		awk 'NF > 0 {printf ".%s\n",$1}' >> smtp-policy.spam.new
#	    cat smtp-policy.spam.new | sed 's/^@//g' | tr "[A-Z]" "[a-z]" | \
#		    sort | uniq > smtp-policy.spam.new2
#	    if [ `grep -c cyberpromo smtp-policy.spam.new` -gt "0" ]; then
#		mv smtp-policy.spam smtp-policy.spam.old
#		mv smtp-policy.spam.new2 smtp-policy.spam
#		rm -f smtp-policy.spam.new
#	    else
#		echo "Hmm....something went wrong while updating the spam policy."
#		echo "Please try again."
# 		exit 1
#	    fi
#	    :
#	fi
#    fi
#fi

# Fork off a subshell to do it all...
(
  # The basic boilerplate
  cat smtp-policy.src

  # Localnames
  echo "# ----------"
  echo "# localnames:"
  cat localnames | \
  awk '/^#/{next;} NF >= 1 {printf "%s = _localnames\n",$1;}'

  # smtp-policy.relay
  # (Lists NETWORKS (NO DOMAINS!) that are allowed to use us as relay)
  # (well, actually it could also list e.g.: ".our.domain" if it would
  #  be fine to allow relaying from anybody whose IP address reverses to
  #  domain suffix ".our.domain")
  if [ -f smtp-policy.relay.manual ] ; then
    echo "# -------------------------"
    echo "# smtp-policy.relay.manual:"
    cat smtp-policy.relay.manual | \
    awk '/^#/{next;}
	{printf "%s = _full_rights\n",$0;next;}'
  fi
  if [ -f smtp-policy.relay ] ; then
    echo "# ------------------"
    echo "# smtp-policy.relay:"
    cat smtp-policy.relay | \
    awk '/^#/{next;}
	{printf "%s = _full_rights\n",$0;next;}'
  fi

  # smtp-policy.mx.manual
  # (Lists domains that are allowed to use us as inbound MX relay for them)
  if [ -f smtp-policy.mx.manual ] ; then
    echo "# ----------------------"
    echo "# smtp-policy.mx.manual:"
    cat smtp-policy.mx.manual | \
    awk '/^#/{next;} NF >= 1 {printf "%s = _relaytarget\n",$0;}'
  fi
  # smtp-policy.mx
  # (Lists domains that are allowed to use us as inbound MX relay for them)
  if [ -f smtp-policy.mx ] ; then
    echo "# ---------------"
    echo "# smtp-policy.mx:"
    cat smtp-policy.mx | \
    awk '/^#/{next;} NF >= 1 {printf "%s = _relaytarget\n",$0;}'
  fi

  # smtp-policy.spam
  # (Lists users, and domains that are known spam sources)
  # (We use file from "http://www.webeasy.com:8080/spam/spam_download_table"
  #  which is intended for QMAIL, and thus needs to be edited..)
  if [ -f smtp-policy.spam -o -f smtp-policy.spam.manual ] ; then
    echo "# ---------------------------"
    echo "# smtp-policy.spam{,.manual}:"
    ( if [ -f smtp-policy.spam ] ; then
	cat smtp-policy.spam
      fi
      if [ -f smtp-policy.spam.manual ] ; then
	cat smtp-policy.spam.manual
      fi ) | tr "[A-Z]" "[a-z]" | sed 's/^@//g' | sort | uniq | \
    awk '/^\[/{ # an address block to reject
	    printf "%s  rejectnet +\n",$0;
	    next;
	}
	NF > 0 { # All other cases are usernames with their domains
	    printf "%s  = _bulk_mail\n",$0;
	}'
  fi

# --------- end of subshell
) > smtp-policy.dat

umask 022 # Make sure the resulting db file(s) are readable by all

# Build the actual binary policy database (-p), and if the input
# has same key repeating, append latter data instances to the first
# one (-A):

$MAILBIN/makedb -A -p $DBTYPE smtp-policy-new smtp-policy.dat || exit $?

case $DBTYPE in
dbm)
	mv smtp-policy-new.dir  smtp-policy.dir
	mv smtp-policy-new.pag  smtp-policy.pag
	;;
ndbm)
	mv smtp-policy-new.dir  smtp-policy.dir
	mv smtp-policy-new.pag  smtp-policy.pag
	;;
gdbm)
	mv smtp-policy-new.gdbm smtp-policy.gdbm
	;;
btree)
	mv smtp-policy-new.db   smtp-policy.db
	;;
esac

exit 0