Source of spfgenerator.deb (debian package).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spfgenerator 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/sh
  2. showHelp() {
  3. cat <<EOF
  4. NAME
  5. spfgenerator generate SPF DNS entry
  6. SYNOPSIS
  7. spfgenerator [-h] [-s] [-mx] [-a [domain]] [-i domain] [-p fail|soft|neutral] [-d domain]
  8. DESCRIPTION
  9. spfgenerator generates SPF DNS entry
  10. -h, --help
  11. show this help.
  12. -mx
  13. Allow servers listed as MX to send email for the domain.
  14. -ip <ip>
  15. Allow IP to send email for the domain.
  16. Can be used several times.
  17. -i, --include <domain>
  18. Any domains that may deliver or relay mail for the domain.
  19. Can be used several times.
  20. -a <domain>
  21. Add any other server hostname that may deliver or relay mail for the domain.
  22. If domain is empty, it allow current IP address of the domain to send email for the domain.
  23. Can be used several times.
  24. -p, --policy <fail|soft|neutral
  25. How strict should be the servers treating the emails
  26. neutral:
  27. Mails will be probably accepted
  28. Default value.
  29. fail:
  30. Not compliant will be rejected
  31. soft:
  32. Not compliant will be accepted but marked
  33. -d, --domain <domain>
  34. Default value: @
  35. -s, --short
  36. Only show the value
  37. EOF
  38. }
  39. MX=0
  40. IPv4s=
  41. IPv6s=
  42. INCLUDEs=
  43. POLICY='?all'
  44. DOMAIN=@
  45. SHORT=0
  46. As=
  47. testData() {
  48. if [ -z "$1" ]; then
  49. (
  50. echo "Invalid parameter ($2)\n"
  51. showHelp
  52. ) >&2
  53. exit 1
  54. fi
  55. }
  56. addIp() {
  57. if [ -n "$1" ]; then
  58. if [ $(echo "$1" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") ]; then
  59. IPv4s="$IPv4s ip4:$1"
  60. else
  61. IPv6s="$IPv6s ip6:$1"
  62. fi
  63. fi
  64. }
  65. addA() {
  66. if [ -z "$1" ]; then
  67. As="$As a"
  68. else
  69. As="$As a:$1"
  70. fi
  71. }
  72. addInclude() {
  73. if [ -n "$1" ]; then
  74. INCLUDEs="$INCLUDEs include:$1"
  75. fi
  76. }
  77. for i in $@; do
  78. case $1 in
  79. -h|--help)
  80. showHelp
  81. exit 0
  82. ;;
  83. -mx)
  84. MX=1
  85. shift
  86. ;;
  87. -s|--short)
  88. SHORT=1
  89. shift
  90. ;;
  91. -a)
  92. shift
  93. addA $1
  94. shift
  95. ;;
  96. -ip)
  97. shift
  98. addIp $1
  99. shift
  100. ;;
  101. -i|--include)
  102. shift
  103. addInclude $1
  104. shift
  105. ;;
  106. -p|--policy)
  107. shift
  108. case $1 in
  109. neutral) POLICY='?all';;
  110. soft) POLICY='~all';;
  111. fail) POLICY='-all';;
  112. esac
  113. shift
  114. ;;
  115. -d|--domain)
  116. shift
  117. DOMAIN=$1
  118. shift
  119. ;;
  120. esac
  121. done
  122. testData "$DOMAIN" domain
  123. testData "$IPv4s$IPv6s" ip
  124. (
  125. if [ $MX -eq 1 ]; then
  126. MX=mx
  127. fi
  128. if [ $SHORT -eq 0 ]; then
  129. echo "$DOMAIN IN TXT \"v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY\""
  130. else
  131. echo "v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY"
  132. fi
  133. ) | sed 's/ */ /g'