1#!/bin/sh 2 3nvram=/bin/config 4smtpc=/usr/sbin/ssmtp 5LOG_FILE=/var/log/messages 6ALERT_LOG_FILE=/var/log/block-site-messages 7email_lock_file=/tmp/tmp_email_lock_file 8 9trap "rm -f $email_lock_file; exit 1" SIGTERM 10trap "rm -f $email_lock_file; exit 1" SIGINT 11 12 13# try without ssl first, if fail then try it with ssl 14no_ssl_first=1 15 16# email related configs (in nvram get command) : 17# email_smtp : smtp server address 18# email_endis_auth : smtp sever needs authentication or not ? 19# email_username : username for smtp server authentication 20# email_password : password for smtp server authentication 21# 22# email_addr : "To addr", the addr of the receiver. 23# email_from_assign : assign "From addr" or not ? 24# email_this_addr : "From addr", the addr the email (says itself) comes from. 25# 26# email_notify : 27# email_send_alert : 28 29tls_required() # $1: smtp_server 30{ 31 # smtp.gmail.com requests TLS support 32 echo $1 | grep -q 'gmail' 33} 34 35print_smtpc_conf() 36{ 37 local smtp_server=$($nvram get email_smtp) 38 local smtp_port=$($nvram get email_port) 39 echo "mailhub=$smtp_server" 40 echo "FromLineOverride=yes" 41 42 if [ $($nvram get email_endis_auth) = "1" ]; then 43 echo "AuthUser=$($nvram get email_username)" 44 echo "AuthPass=$($nvram get email_password)" 45 46 if [ "x$no_ssl_first" = "x0" -o "x$smtp_port" = "x465" ] ; then 47 echo "UseTLS=YES" 48 if [ "x$smtp_port" = "x465" ]; then 49 echo "UseSTARTTLS=NO" 50 else 51 echo "UseSTARTTLS=YES" 52 fi 53 fi 54 fi 55} 56 57print_email_header() 58{ 59 local hostname="$(cat /proc/sys/kernel/hostname)" 60 local from 61 local addr="$($nvram get email_addr)" 62 local username="$($nvram get email_username)" 63 local smtp_server="$($nvram get email_smtp)" 64 65 if [ "$($nvram get email_from_assign)" = "1" ]; then 66 from="\"root@$hostname\"<$($nvram get email_this_addr)>" 67 else 68 # If no username specify,Just use the email_addr 69 if [ -n "$username" ]; then 70 from="\"root@$hostname\"<$($nvram get email_username)>" 71 else 72 from="\"root@$hostname\"<$($nvram get email_addr)>" 73 fi 74 # as I know, different smtp servers have different rules about "From addr" : 75 # * dni : drops mails that "From addr" != "account email addr" silently. 76 # * pchome : rejects sending mails that "From addr" != "account email addr". 77 # * gmail : tranforms the "From addr" to "account email addr". 78 # the smtp servers that don't care about "From addr" and just send mails are getting 79 # scarce. 80 case "$smtp_server" in 81 smtp.pchome.com.tw) 82 [ -n "$username" ] && from="${username}@pchome.com.tw" 83 ;; 84 esac 85 fi 86 87 cat <<EOF 88Subject: NETGEAR $hostname Log 89From: $from 90To: $addr 91 92EOF 93} 94 95print_log() 96{ 97 local lang_select="$($nvram get GUI_Region)" 98 local gl_task_name="$(cat /tmp/gl_task_name)" 99 local display_path="$(echo $2 | sed 's/\//\\/g')" 100 101 print_email_header 102 103 if [ "x$1" = "xgreendownloader_task" ];then 104 if [ "$lang_select" = "Chinese" ];then 105 echo "������$gl_task_name������������������������������$display_path���- NETGEAR Downloader" 106 elif [ "$lang_select" = "Russian" ];then 107 echo "���������������� $gl_task_name ������������������. �������� ���������������� �� $display_path. -�������������������� NETGEAR" 108 else 109 echo "Your downloading task $gl_task_name is successfully finished. The downloaded file is saved at $display_path. - NETGEAR Downloader" 110 fi 111 else 112 if [ "$2" = "send_email_alert" ]; then 113 cat $ALERT_LOG_FILE 114 else 115 sed -n '1! G;$p;h' $LOG_FILE | sed -n '1,256 p' 116 fi 117 fi 118} 119 120sendmail() 121{ 122 local conf=/tmp/ssmtp.conf 123 local email_file=/tmp/tmp_email_file 124 local err_file=/tmp/tmp_email_err_file 125 local addr="$($nvram get email_addr)" 126 print_smtpc_conf > $conf 127 print_log "$1" "$2" > $email_file 128 if ! cat $email_file | $smtpc -C$conf $addr >/dev/null 2>$err_file; then 129 return 1 130 else 131 return 0 132 fi 133} 134 135sendlog() # $1: clearlog_if_success $2: send_email_alert 136{ 137 local count=0 138 local conf=/tmp/ssmtp.conf 139 local email_file=/tmp/tmp_email_file 140 local err_file=/tmp/tmp_email_err_file 141 local addr="$($nvram get email_addr)" 142 143 #Fix bug when 56487 sometimes DUT send Log to Email content is blank 144 while :; do 145 if [ -f $email_lock_file ];then 146 let count=$count+1 147 if [ "$count" = "10" ];then 148 break 149 fi 150 sleep $count 151 continue 152 fi 153 break 154 done 155 touch $email_lock_file 156 if ! sendmail "$1" "$2" 157 then 158 if [ "x$no_ssl_first" = "x1" ] ; then 159 no_ssl_first=0 160 if ! sendmail "$1" "$2" 161 then 162 logger -- "[email sent to: $addr]" 163 logger -- "[email failed] $(cat $err_file)" 164 rm -f $conf $email_file $err_file $email_lock_file 165 return 1 166 fi 167 else 168 logger -- "[email sent to: $addr]" 169 logger -- "[email failed] $(cat $err_file)" 170 rm -f $conf $email_file $err_file $email_lock_file 171 return 1 172 fi 173 fi 174 rm -f $conf $email_file $err_file $email_lock_file 175 176 if [ "$1" = "clearlog_if_success" -a "$2" != "send_email_alert" ]; then 177 rm -f $LOG_FILE 178 fi 179 logger -- "[email sent to: $addr]" 180 return 0 181} 182 183print_email_header_for_hdd() 184{ 185 local hostname="$(cat /proc/sys/kernel/hostname)" 186 local from 187 local addr="$($nvram get email_addr)" 188 local username="$($nvram get email_username)" 189 local smtp_server="$($nvram get email_smtp)" 190 191 if [ "$($nvram get email_from_assign)" = "1" ]; then 192 from="\"root@$hostname\"<$($nvram get email_this_addr)>" 193 else 194 # If no username specify,Just use the email_addr 195 if [ -n "$username" ]; then 196 from="\"root@$hostname\"<$($nvram get email_username)>" 197 else 198 from="\"root@$hostname\"<$($nvram get email_addr)>" 199 fi 200 case "$smtp_server" in 201 smtp.pchome.com.tw) 202 [ -n "$username" ] && from="${username}@pchome.com.tw" 203 ;; 204 esac 205 fi 206 207 cat <<EOF 208Subject: Warning!R7800 Internal HDD might have some issues 209From: $from 210To: $addr 211 212EOF 213} 214 215print_hdd_log() 216{ 217 print_email_header_for_hdd 218 echo "[HDD ERROR] Warning! The internal hard drive have the reallocated sector error frequently, we suggest you to replace the internal hard drive now." 219} 220 221# per NTGR's requirement, when the internal disk have something wrong, we need to email to the user ath 9:30 AM. 222email_HDD_err_log() 223{ 224 local conf=/tmp/hdd_err.conf 225 local email_file=/tmp/tmp_hdd_email.file 226 local err_file=/tmp/tmp_hdd_err_email.file 227 local addr="$($nvram get email_addr)" 228 229 echo "email_HDD_err_log in ..." > /dev/console 230 print_smtpc_conf > $conf 231 print_hdd_log > $email_file 232 if ! cat $email_file | $smtpc -C$conf $addr >/dev/null 2>$err_file; then 233 logger -- "[email sent to: $addr]" 234 logger -- "[email failed] $(cat $err_file)" 235 rm -f $conf $email_file $err_file 236 return 1 237 fi 238 rm -f $conf $email_file $err_file 239 logger -- "[email sent to: $addr]" 240 return 0 241 242} 243 244### start here ### 245 246prog=${0##*/} 247 248case "$prog" in 249email_log) 250 [ $($nvram get email_notify) = "0" ] && exit 251 sendlog "clearlog_if_success" 252 ;; 253email_full_log) 254 [ $($nvram get email_notify) = "0" ] && exit 255 # send log only if lines of log file > 256 * 90% = 230.4 256 [ ! -s $LOG_FILE ] && exit 257 [ "$(wc -l $LOG_FILE | sed -n 's/[^0-9]*//gp')" -le "230" ] && exit 258 sendlog "clearlog_if_success" 259 ;; 260send_email_alert) 261 [ $($nvram get email_notify) = "0" ] && exit 262 if [ "x$1" = "xgreendownloader_task" ];then 263 [ "$($nvram get green_download_email_noti)" = "0" ] && exit 264 sendlog "$1" "$2" 265 else 266 [ "$($nvram get email_send_alert)" = "0" ] && exit 267 sendlog "clearlog_if_success" "send_email_alert" 268 fi 269 ;; 270send_log) 271 [ $($nvram get email_notify) = "0" ] && exit 272 sendlog 273 ;; 274email_HDD_err_log) 275 [ $($nvram get email_notify) = "0" ] && exit 276 email_HDD_err_log 277 ;; 278esac 279 280