#!/usr/bin/env sh
#$1是收信邮箱地址
#$2是授权码
#$3是需要保存证书文件的绝对路径
#$4是发邮件本地发送邮件(调用mail)时使用的用户
#退出状态码定义:
#1:调用错误
#2已经运行过
#3uid错误
#4目录错误
#5用户不存在
#0成功
#判断参数个数
[ $# -ne 4 ] && echo "Usage: ${0} your_email@qq.com QQ邮箱授权码 存放证书文件的绝对路径 调用mail时本地的账户" && exit 1
#判断是否是root账户 因为需要写入mail.rc文件
uid=$(id -u)
[ $uid -ne 0 ] && echo 请使用root账户 && exit 3
#判断输入路径是否是目录
[ ! -d ${3} ] && echo '存放证书文件的绝对路径不存在或者不是目录' && exit 78
#判断是否存在输入的用户名
grep -E "^${4}:" /etc/passwd &> /dev/null
[ ! $? -eq 0 ] && echo '用户不存在' && exit 5
#切换成输入账户身份创建证书文件
su ${4} <<EOF
#判断是否可写
[ ! -w ${3} ] && echo '存放证书文件的绝对路径对输入的账户不可写' && exit 4
#判断当前用户是否对目录有读的权限
[ ! -r ${3} ] && echo '存放证书文件的绝对路径对输入的账户不可读' && exit 4
#判断当地前用户是否对目录有执行的权限
[ ! -x ${3} ] && echo '存放证书文件的绝对路径对输入的账户不可执行' && exit 4
EOF
[ $? -eq 4 ] && exit $?
#公共变量定义
certPath=${3%*/}/.certs/
url=smtp.qq.com:465
etcPath=/etc/mail.rc
lockFile=installQQMail.lock
#判断是否运行过本程序
[ -e ${certPath}${lockFile} ] && echo 貌似已经运行过该程序了 && exit 1
su ${4} -c "mkdir -p ${certPath}"
#在当前用户目录下给出锁文件
su ${4} -c "touch ${certPath}${lockFile}"
su ${4} <<EOF
echo -n | openssl s_client -connect ${url} | \
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${certPath}qq.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ${certPath} -i ${certPath}qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ${certPath} -i ${certPath}qq.crt
certutil -L -d ${certPath}
cd ${certPath}
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
EOF
echo '' >> ${etcPath}
echo "#QQ邮箱__START__" >> ${etcPath}
echo "set from=${1}" >> ${etcPath}
echo "set smtp=smtps://smtp.qq.com:465" >> ${etcPath}
echo "set smtp-auth-user=${1}" >> ${etcPath}
echo "set smtp-auth=login" >> ${etcPath}
echo "set smtp-auth-password=${2}" >> ${etcPath}
echo "set ssl-verify=ignore" >> ${etcPath}
echo "set nss-config-dir=${certPath}" >> ${etcPath}
echo "#QQ邮箱__END__" >> ${etcPath}
echo '' >> ${etcPath}
testScriptName=./testSendQQMail.sh
echo 安装成功,请执行${testScriptName}进行测试
touch $testScriptName
chmod +x $testScriptName
echo '#!/usr/bin/env sh' > ${testScriptName}
echo "su ${4} -c \"echo '测试内容' | mail -s '测试邮件标题' ${1}\" " >> ${testScriptName}
exit 0
网友评论