summaryrefslogtreecommitdiff
path: root/localnotifypwexp
blob: e6e2ef6656fc30690593c9eb7e1f96f758f2282a (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. domainname=$(hostname --domain)
  22. # /etc/shadow is system dependent
  23. shadowfile="/etc/shadow"
  24. # fields in /etc/shadow
  25. declare -r last=2
  26. #declare -r may=3 # not used in this script
  27. declare -r must=4
  28. declare -r warn=5
  29. #declare -r grace=6 # not used in this script
  30. declare -r disable=7
  31. declare -r doesntmust=99999
  32. declare -r warndefault=7
  33. passwdfile="/etc/passwd"
  34. declare -r uidfield=3
  35. declare -r unamefield=1
  36. # UID range is system dependent
  37. declare -r uidmin=1000
  38. declare -r uidmax=65534 # exclusive
  39. # remove the hardcoded path from these progs to use them via $PATH
  40. # mailx is system dependent
  41. notifyprog="mailx"
  42. grepprog="grep"
  43. awkprog="awk"
  44. dateprog="date"
  45. # comment out one of these
  46. #useUTC=""
  47. useUTC="-u"
  48. # +%s is a GNUism - set it to blank and use dateformat if you have
  49. # a system that uses something else like epochdays, for example
  50. epochseconds="+%s"
  51. dateformat="" # blank for GNU when epochseconds="+%s"
  52. secondsperday=86400 # set this to 1 for no division
  53. today=$(($($dateprog $useUTC $epochseconds $dateformat)/$secondsperday))
  54. oIFS=$IFS
  55. # ### END SETUP ###
  56. # ### MAIL TEMPLATES ###
  57. # use single quotes around templates, backslash escapes and substitutions
  58. # will be evaluated upon output
  59. usersubjecttemplate='Your password is expiring soon'
  60. userbodytemplate='Your password on $hostname expires in $(($expdate - $today)) days.
  61. Please contact the $domain IT department by email or phone
  62. if you have any questions. Help is also available at
  63. http://support.$domain/password'
  64. adminsubjecttemplate='User Password Expired: $user@$hostname'
  65. adminbodytemplate='The password for user $user on $hostname expired $age days ago.
  66. Please contact this user about their inactive account and consider whether
  67. the account should be disabled or deleted.'
  68. # ### END MAIL TEMPLATES ###
  69. # allow overrides (especially userbodytemplate)
  70. declare -r orgconfig=local-ORG/notifypwexp
  71. declare -r hostconfig=local/notifypwexp
  72. [ ! -r /etc/$orgconfig ] || . /etc/$orgconfig
  73. [ ! -r /etc/$hostconfig ] || . /etc/$hostconfig
  74. # get real users
  75. users=$($awkprog -F: -v uidfield=$uidfield \
  76. -v unamefield=$unamefield \
  77. -v uidmin=$uidmin \
  78. -v uidmax=$uidmax \
  79. -- '$uidfield>=uidmin && $uidfield<uidmax \
  80. {print $unamefield}' $passwdfile)
  81. for user in $users;
  82. do
  83. IFS=":"
  84. usershadow=$($grepprog ^$user $shadowfile)
  85. # make an array out of it
  86. usershadow=($usershadow)
  87. IFS=$oIFS
  88. mustchange=${usershadow[$must]}
  89. disabledate=${usershadow[$disable]:-$doesntmust}
  90. # skip users that aren't expiring or that are disabled
  91. if [[ $mustchange -ge $doesntmust || $disabledate -le $today ]] ; then continue; fi;
  92. lastchange=${usershadow[$last]}
  93. warndays=${usershadow[$warn]:-$warndefault}
  94. expdate=$(($lastchange + $mustchange))
  95. threshhold=$(($today + $warndays + $weekmode))
  96. if [[ $expdate -lt $threshhold ]];
  97. then
  98. if [[ $expdate -ge $today ]];
  99. then
  100. subject=$(eval "echo \"$usersubjecttemplate\"")
  101. body=$(eval "echo \"$userbodytemplate\"")
  102. echo -e "$body" | $notifyprog -s "$subject" $user
  103. else
  104. if [[ $age -ge $aged ]];
  105. then
  106. subject=$(eval "echo \"$adminsubjecttemplate\"")
  107. body=$(eval "echo \"$adminbodytemplate\"")
  108. echo -e "$body" | $notifyprog -s "$subject" $admins
  109. fi
  110. fi
  111. fi
  112. done