summaryrefslogtreecommitdiff
path: root/localnotifypwexp
blob: 0d7845fd1736dd4dff5e492ed3344e5b743c7d63 (plain)
  1. #!/bin/bash
  2. set -e
  3. # notifypwexp - send mail to users whose passwords are expiring soon
  4. # designed to be run daily or weekly from cron
  5. # call with -w for weekly mode (checks to see if warning period begins in the next 7 days
  6. # use -w for a weekly cron job, avoiding excessive emails
  7. # with no option, it only checks whether we're in the warning period now
  8. # use this for a daily cron job
  9. # by Dennis Williamson
  10. # Origin: http://serverfault.com/questions/11887
  11. # ### SETUP ###
  12. if [[ $1 == "-w" ]] # check for expiration warnings beginning during the next seven days
  13. then
  14. weekmode=7
  15. else
  16. weekmode=0
  17. fi
  18. admins="root postmaster"
  19. declare -r aged=21 # minimum days after expiration before admins are emailed, set to 0 for "always"
  20. hostname=$(hostname --fqdn)
  21. # /etc/shadow is system dependent
  22. shadowfile="/etc/shadow"
  23. # fields in /etc/shadow
  24. declare -r last=2
  25. #declare -r may=3 # not used in this script
  26. declare -r must=4
  27. declare -r warn=5
  28. #declare -r grace=6 # not used in this script
  29. declare -r disable=7
  30. declare -r doesntmust=99999
  31. declare -r warndefault=7
  32. passwdfile="/etc/passwd"
  33. declare -r uidfield=3
  34. declare -r unamefield=1
  35. # UID range is system dependent
  36. declare -r uidmin=1000
  37. declare -r uidmax=65534 # exclusive
  38. # remove the hardcoded path from these progs to use them via $PATH
  39. # mailx is system dependent
  40. notifyprog="/bin/mailx"
  41. grepprog="/bin/grep"
  42. awkprog="/usr/bin/awk"
  43. dateprog="/bin/date"
  44. # comment out one of these
  45. #useUTC=""
  46. useUTC="-u"
  47. # +%s is a GNUism - set it to blank and use dateformat if you have
  48. # a system that uses something else like epochdays, for example
  49. epochseconds="+%s"
  50. dateformat="" # blank for GNU when epochseconds="+%s"
  51. secondsperday=86400 # set this to 1 for no division
  52. today=$(($($dateprog $useUTC $epochseconds $dateformat)/$secondsperday))
  53. oIFS=$IFS
  54. # ### END SETUP ###
  55. # ### MAIL TEMPLATES ###
  56. # use single quotes around templates, backslash escapes and substitutions
  57. # will be evaluated upon output
  58. usersubjecttemplate='Your password is expiring soon'
  59. userbodytemplate='Your password on $hostname expires in $(($expdate - $today)) days.
  60. Please contact the IT department by email at \"helpdesk\" or at
  61. extension 555 if you have any questions. Help is also available at
  62. http://helpdesk.example.com/password'
  63. adminsubjecttemplate='User Password Expired: $user@$hostname'
  64. adminbodytemplate='The password for user $user on $hostname expired $age days ago.
  65. Please contact this user about their inactive account and consider whether
  66. the account should be disabled or deleted.'
  67. # ### END MAIL TEMPLATES ###
  68. # allow overrides (especially userbodytemplate)
  69. declare -r localconfig=/etc/local/notifypwexp
  70. if [ -r /etc/default/$localconfig ]; then . /etc/default/$localconfig; fi
  71. # get real users
  72. users=$($awkprog -F: -v uidfield=$uidfield \
  73. -v unamefield=$unamefield \
  74. -v uidmin=$uidmin \
  75. -v uidmax=$uidmax \
  76. -- '$uidfield>=uidmin && $uidfield<uidmax \
  77. {print $unamefield}' $passwdfile)
  78. for user in $users;
  79. do
  80. IFS=":"
  81. usershadow=$($grepprog ^$user $shadowfile)
  82. # make an array out of it
  83. usershadow=($usershadow)
  84. IFS=$oIFS
  85. mustchange=${usershadow[$must]}
  86. disabledate=${usershadow[$disable]:-$doesntmust}
  87. # skip users that aren't expiring or that are disabled
  88. if [[ $mustchange -ge $doesntmust || $disabledate -le $today ]] ; then continue; fi;
  89. lastchange=${usershadow[$last]}
  90. warndays=${usershadow[$warn]:-$warndefault}
  91. expdate=$(($lastchange + $mustchange))
  92. threshhold=$(($today + $warndays + $weekmode))
  93. if [[ $expdate -lt $threshhold ]];
  94. then
  95. if [[ $expdate -ge $today ]];
  96. then
  97. subject=$(eval "echo \"$usersubjecttemplate\"")
  98. body=$(eval "echo \"$userbodytemplate\"")
  99. echo -e "$body" | $notifyprog -s "$subject" $user
  100. else
  101. if [[ $age -ge $aged ]];
  102. then
  103. subject=$(eval "echo \"$adminsubjecttemplate\"")
  104. body=$(eval "echo \"$adminbodytemplate\"")
  105. echo -e "$body" | $notifyprog -s "$subject" $admins
  106. fi
  107. fi
  108. fi
  109. done