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