PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/portal-impl/src/com/liferay/mail/dependencies/fedora/ksh/mailadmin.ksh

https://github.com/lululiferay/liferay-portal
Korn Shell | 772 lines | 540 code | 54 blank | 178 comment | 41 complexity | 2b2a5c64f6993e0e135e1d55ad16140e MD5 | raw file
  1. #!/bin/ksh -p
  2. # Uncomment next line for testing
  3. # set -x
  4. #------------------------------------------------------------------------
  5. # Name: mailadmin.ksh
  6. # Purpose: Provide Liferay a shell interface to any Mail Subsystem
  7. #
  8. # Environment: Linix/AIX/Solaris/FreeBSD
  9. # Usage:
  10. # mailadmin --help
  11. # mailadmin
  12. # mailadmin addForward [userId] [emailAddresses]
  13. # mailadmin addUser [userId] [password] [firstName] [middleName] [lastName] [emailAddress]
  14. # mailadmin addVacationMessage [userId] [emailAddress] [vacationMessage]
  15. # mailadmin deleteEmailAddress [userId]
  16. # mailadmin deleteUser [userId]
  17. # mailadmin updateBlocked [userId] [blockedEmailAddress]
  18. # mailadmin updateEmailAddress [userId] [emailAddress]
  19. # mailadmin updatePassword [userId] [password]
  20. #
  21. # Author: mlawrence
  22. # Date: Feb-22-2005
  23. # Environment: exec from JVM or run interactively from command shell
  24. #
  25. # Installation:
  26. # cp mailadmin.ksh /usr/sbin
  27. # chmod 750 /usr/sbin/mailadmin.ksh
  28. # chgrp tomcat /usr/sbin/mailadmin.ksh
  29. # chown tomcat /usr/sbin/mailadmin.ksh
  30. # touch /var/log/mailadmin.log
  31. # chmod 660 /var/log/mailadmin.log
  32. # chgrp tomcat /var/log/mailadmin.log
  33. # chown tomcat /var/log/mailadmin.log
  34. # vi /usr/local/tomcat/common/classes/portal-ext.properties
  35. # mail.hook.impl=com.liferay.mail.util.ShellHook
  36. # mail.hook.shell.script=/usr/sbin/mailadmin.ksh
  37. # mail.box.style=INBOX
  38. # /usr/local/tomcat/common/lib/ext/mail-ejb.jar -> add MailAdminHook.class
  39. # /usr/local/tomcat/common/lib/ext/portal-ejb.jar -> update PropsUtil.class
  40. #
  41. #
  42. #
  43. #Before using the script below, you should make a few replacements:
  44. #
  45. # domain being installed (eg xyz.com)
  46. # replace "EMAILDOMAIN" "xyz.com" -- mailadmin.ksh
  47. #
  48. # mysql email database user (eg DBUSR)
  49. # replace "DBUSR" "DBUSR" -- mailadmin.ksh
  50. #
  51. # mysql email database password (eg dbpassw1d)
  52. # replace "DBPASSWD" "dbpassw1d" -- mailadmin.ksh
  53. #
  54. #
  55. #
  56. # Change History:
  57. # Date Who Description
  58. # Feb 22, 2005 MAL Initial version
  59. # Mar 7, 2005 MAL Added dovecot_addForward, dovecot_updatePassword
  60. #
  61. #########################################################################
  62. # CONSTANTS
  63. #########################################################################
  64. # GLOBAL CONSTANTS
  65. DOMAIN=EMAILDOMAIN # Domain being managed
  66. MYSQL_USERNAME=DBUSR # MySQL user
  67. MYSQL_PASSWORD=DBPASSWD # MySQL password
  68. MYSQL_DATABASE=mail # MySQL database name
  69. MAIL_TYPE=DOVECOT # DOVECOT, SENDMAIL, OR CYRUS
  70. umask 007 # Set file creation mask u=rwx,g=rwx,o=rwx
  71. integer LOG_FILE_MAX=200000 # Max file length in bytes
  72. LOG_FILENAME=/var/log/mailadmin.log # Log file or blank for stdout
  73. MYSQL_ENV="--user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD --database=$MYSQL_DATABASE --host=127.0.0.1"
  74. VMAIL_GROUP=vmail # Mail File Creation group
  75. VMAIL_PERM=770 # Mail File Creation permissions
  76. TOMCAT_UID=500 # Mail File Creation user id for tomcat user
  77. VMAIL_GID=510 # Mail File Creation group id - vmail
  78. VMAIL_HOME=/var/vmail # Mail root directory
  79. #########################################################################
  80. # VARIABLES
  81. #########################################################################
  82. userId=""
  83. emailAddresses=""
  84. password=""
  85. firstName=""
  86. middleName=""
  87. lastName=""
  88. emailAddress=""
  89. vacationMessage=""
  90. blockedEmailAddress=""
  91. #########################################################################
  92. # UTILS
  93. #########################################################################
  94. # Execute a shell command, log the results if any error or any output occurs
  95. function LogCmdOut { # CallingFunctionName Cmd
  96. if [[ $# != 2 ]] then
  97. ErrorExit "LogCmd" "expected 2 parms, found $# $1,$2,$3,$4,$5,$6,$7,$8"
  98. fi
  99. LCFunction=$1
  100. LCCmd=$2
  101. $LCCmd 2>/tmp/LogCmd.out >/tmp/LogCmd.out
  102. LCResult=$?
  103. LCOutput=$(cat /tmp/LogCmd.out 2>/dev/null)
  104. if [[ $LCResult != 0 ]] || [[ -n $LCOutput ]] then
  105. WriteLog "INFO" "LogCmdOut: $LCFunction:$LCCmd=$LCResult Output: $LCOutput"
  106. fi
  107. rm -f "/tmp/LogCmd.out" >/dev/null 2>/dev/null # ignore errors
  108. return $LCResult
  109. }
  110. #------------------------------------------------------------------------
  111. # remove a file, ignore errors
  112. function KillFile { # Filename
  113. if [[ $# != 1 ]] then
  114. return # ignore removing empty names
  115. else
  116. KFFileName=$1
  117. if [[ -f $KFFileName ]] then # if it exists
  118. LogCmdOut "KillFile" "rm -f $KFFileName" # remove it
  119. fi
  120. fi
  121. }
  122. #------------------------------------------------------------------------
  123. # write status message to log file or stdout
  124. function WriteLog { # level, msg
  125. PurgeLog # make sure log doesnt grow too big
  126. #2002-01-20 01:02:30
  127. TODAY=`date "+20%y-%m-%d %H:%M:%S"`
  128. msg="$TODAY $1 mailadmin.ksh $gsUserName $2"
  129. if [[ -n $LOG_FILENAME ]] then
  130. echo $msg >> $LOG_FILENAME
  131. echo $msg
  132. else
  133. echo $1 $2 # log to console
  134. fi
  135. }
  136. #------------------------------------------------------------------------
  137. # remove logfile over LOG_FILE_MAX bytes
  138. function PurgeLog {
  139. integer PLFileLen
  140. if [[ -n $LOG_FILENAME ]] then # if we are not using stdout (ie logfile is not blank)
  141. if [[ -f $LOG_FILENAME ]] then # if logfile exists
  142. PLFileLen=$(ls -alL $LOG_FILENAME 2>/dev/null | awk '{print $5}')
  143. if [[ -n $PLFileLen ]] then
  144. if (( $PLFileLen > $LOG_FILE_MAX )) ; then
  145. TODAY=`date "+20%y-%m-%d %H:%M:%S"`
  146. # dont remove file or you'll break the stdout connection to the processes writing to it
  147. echo "$TODAY mailadmin.ksh Purged $LOG_FILENAME FileLen:$PLFileLen bytes. MaxLen:$LOG_FILE_MAX " > $LOG_FILENAME
  148. fi
  149. fi
  150. fi
  151. fi
  152. }
  153. #########################################################################
  154. # INIT/EXIT FUNCTIONS
  155. #########################################################################
  156. #------------------------------------------------------------------------
  157. # Initialize the Application
  158. function AppInit {
  159. TODAY=`date "+20%y-%m-%d %H:%M:%S"` # get a time stamp
  160. # extract user id from id command: uid=28191(systemsp) gid=100(vuser) groups=100(vuser) -> systemsp
  161. # don't use whoami, it lives in different places on different OS's
  162. gsUserName=$( id | awk '{print $1}' | sed s/.\*\(// )
  163. gsUserName=${gsUserName%?} # remove trailing paren: systemsp) -> systemsp
  164. }
  165. #------------------------------------------------------------------------
  166. function ErrorExit { # Function, Msg
  167. WriteLog "ERROR" "ErrorExit: $gsServerName:mailadmin.ksh:$1: $2"
  168. CommonExit 1
  169. }
  170. #------------------------------------------------------------------------
  171. function CommonExit { # resultCode
  172. trap EXIT # Remove exit trap (This doesnt work for some reason)
  173. if [[ $gsAppMode != "menu" ]] then
  174. WriteLog "INFO" "CommonExit: $gsAppMode Exited rc=$1"
  175. exit $1 # exit return code
  176. else
  177. exit 0 # no errors if exiting menu
  178. fi
  179. }
  180. # --------------------------------------------------------------------
  181. function getServerHostName {
  182. gsServerName=$(hostname | sed s/\\..*// ) # get this computer's host name. remove anything after the 1st period (eg sun.com -> sun)
  183. }
  184. #------------------------------------------------------------------------
  185. # GET IP ADDRESS
  186. function getServerIPAddress {
  187. getServerHostName
  188. gsOS=$(uname)
  189. if [[ $gsOS = "SunOS" ]] then # SunOS Operating System
  190. gsServerPublicIP=$(/usr/sbin/ping -v -n -I 1 $gsServerName 1 1 | grep bytes | awk '{print $4}')
  191. gsServerPublicIP=${gsServerPublicIP%?} # eg 63.73.131.95 private IP
  192. fi
  193. if [[ $gsOS = "HP-UX" ]] then # HP Unix
  194. gsServerPublicIP=$( /usr/sbin/ping -v $gsServerName -n 1 | grep from | awk '{print $4}' )
  195. gsServerPublicIP=${gsServerPublicIP%?} # eg 63.73.131.95 private IP
  196. fi
  197. if [[ $gsOS = "AIX" ]] then # AIX
  198. gsServerPublicIP=$(/usr/sbin/ping $gsServerName 1 1 | grep from | awk '{print $4}' | grep -v crc)
  199. gsServerPublicIP=${gsServerPublicIP%?} # eg 63.73.131.95 private IP
  200. fi
  201. if [[ $gsOS = "FreeBSD" ]] then # FreeBSD Operating System
  202. gsServerPublicIP=$(ping -c 1 $gsServerName | grep from | awk '{print $4}' | grep -v crc )
  203. gsServerPublicIP=${gsServerPublicIP%?} # eg 63.73.131.95 private IP
  204. fi
  205. if [[ $gsOS = "Linux" ]] then # FreeBSD Operating System
  206. #64 bytes from linux00001 (11.48.13.74): icmp_seq=1 ttl=64 time=0.069 ms
  207. gsServerPublicIP=$(ping -c 1 $gsServerName | grep from | head -1 | awk '{print $5}' )
  208. fi
  209. }
  210. #------------------------------------------------------------------------
  211. # called by any virtual method that is not implemented
  212. function notImplemented { # methodName
  213. WriteLog "WARN" "$1: not implemented"
  214. return 0
  215. }
  216. ######################## VIRTUAL METHODS ########################################
  217. function addForward { # userId, emailAddresses
  218. userId=$1
  219. emailAddresses=$2
  220. WriteLog "INFO" "$MAIL_TYPE addForward: userId=$userId emailAddresses=$emailAddresses"
  221. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  222. "DOVECOT") dovecot_addForward;
  223. return $?;;
  224. "SENDMAIL") sendmail_addForward;
  225. return $?;;
  226. "CYRUS") cyrus_addForward;
  227. return $?;;
  228. *) notImplemented "addForward"
  229. return $?;;
  230. esac
  231. }
  232. #------------------------------------------------------------------------
  233. function addUser { # userId password firstName middleName lastName emailAddress
  234. userId=$1
  235. password=$2
  236. firstName=$3
  237. middleName=$4
  238. lastName=$5
  239. emailAddress=$6
  240. WriteLog "INFO" "$MAIL_TYPE addUser: userId=$userId password=$password firstName=$firstName middleName=$middleName lastName=$lastName emailAddress=$emailAddress"
  241. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  242. "DOVECOT") dovecot_addUser;
  243. return $?;;
  244. "SENDMAIL") sendmail_addUser;
  245. return $?;;
  246. "CYRUS") cyrus_addUser;
  247. return $?;;
  248. *) notImplemented "addUser"
  249. return $?;;
  250. esac
  251. }
  252. #------------------------------------------------------------------------
  253. function addVacationMessage { # userId emailAddress vacationMessage
  254. userId=$1
  255. emailAddress=$2
  256. vacationMessage=$3
  257. WriteLog "INFO" "$MAIL_TYPE addVacationMessage: userId=$userId emailAddress=$emailAddress vacationMessage=$vacationMessage"
  258. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  259. "DOVECOT") dovecot_addVacationMessage;
  260. return $?;;
  261. "SENDMAIL") sendmail_addVacationMessage;
  262. return $?;;
  263. "CYRUS") cyrus_addVacationMessage;
  264. return $?;;
  265. *) notImplemented "addVacationMessage"
  266. return $?;;
  267. esac
  268. }
  269. #------------------------------------------------------------------------
  270. function deleteEmailAddress { # userId
  271. userId=$1
  272. WriteLog "INFO" "$MAIL_TYPE deleteEmailAddress: userId=$userId "
  273. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  274. "DOVECOT") dovecot_deleteEmailAddress;
  275. return $?;;
  276. "SENDMAIL") sendmail_deleteEmailAddress;
  277. return $?;;
  278. "CYRUS") cyrus_deleteEmailAddress;
  279. return $?;;
  280. *) notImplemented "deleteEmailAddress"
  281. return $?;;
  282. esac
  283. }
  284. #------------------------------------------------------------------------
  285. function deleteUser { # userId
  286. userId=$1
  287. WriteLog "INFO" "$MAIL_TYPE deleteUser: userId=$userId "
  288. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  289. "DOVECOT") dovecot_deleteUser;
  290. return $?;;
  291. "SENDMAIL") sendmail_deleteUser;
  292. return $?;;
  293. "CYRUS") cyrus_deleteUser;
  294. return $?;;
  295. *) notImplemented "deleteUser"
  296. return $?;;
  297. esac
  298. }
  299. #------------------------------------------------------------------------
  300. function updateBlocked { # userId blockedEmailAddress
  301. userId=$1
  302. blockedEmailAddress=$2
  303. WriteLog "INFO" "$MAIL_TYPE updateBlocked: userId=$userId blockedEmailAddress=$blockedEmailAddress"
  304. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  305. "DOVECOT") dovecot_updateBlocked;
  306. return $?;;
  307. "SENDMAIL") sendmail_updateBlocked;
  308. return $?;;
  309. "CYRUS") cyrus_updateBlocked;
  310. return $?;;
  311. *) notImplemented "updateBlocked"
  312. return $?;;
  313. esac
  314. }
  315. #------------------------------------------------------------------------
  316. function updateEmailAddress { # userId emailAddress
  317. userId=$1
  318. emailAddress=$2
  319. WriteLog "INFO" "$MAIL_TYPE updateEmailAddress: userId=$userId emailAddress=$emailAddress"
  320. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  321. "DOVECOT") dovecot_updateEmailAddress;
  322. return $?;;
  323. "SENDMAIL") sendmail_updateEmailAddress;
  324. return $?;;
  325. "CYRUS") cyrus_updateEmailAddress;
  326. return $?;;
  327. *) notImplemented "updateEmailAddress"
  328. return $?;;
  329. esac
  330. }
  331. #------------------------------------------------------------------------
  332. function updatePassword { # userId password
  333. userId=$1
  334. password=$2
  335. WriteLog "INFO" "$MAIL_TYPE updatePassword: userId=$userId password=$password"
  336. case $MAIL_TYPE in # select an implemention based on MAIL_TYPE
  337. "DOVECOT") dovecot_updatePassword;
  338. return $?;;
  339. "SENDMAIL") sendmail_updatePassword;
  340. return $?;;
  341. "CYRUS") cyrus_updatePassword;
  342. return $?;;
  343. *) notImplemented "updatePassword"
  344. return $?;;
  345. esac
  346. }
  347. ######################## DOVECOT ########################################
  348. # Dovecot, Postfix, Mysql
  349. # See: Liferay-Dovecot.txt
  350. function dovecot_addForward { # userId, emailAddresses
  351. # userId=liferay_com_1 emailAddresses=test1@cnn.com,test2@cnn.com
  352. # The tag FORWARD_PREFIX is prepended to the login email to prevent forwarding
  353. FORWARD_PREFIX=forward.liferay.com_
  354. WriteLog "INFO" "addForward: Finding email for userId=$userId"
  355. liferayLogin=$(mysql $MYSQL_ENV --silent --execute "select email from postfix_virtual where destination=\"$userId@$DOMAIN\" " | tail -1)
  356. mysqlResult=$?
  357. if [[ $mysqlResult != 0 || $liferayLogin == "email" ]] then
  358. WriteLog "ERROR" "addForward: Cant find email in MySQL Database:mail Table:postfix_virtual. mysqlResult=$mysqlResult. liferayLogin=$liferayLogin"
  359. return 1
  360. fi
  361. WriteLog "INFO" "addForward: liferayLogin=$liferayLogin"
  362. # remove any prefix
  363. emailPrefix=$(echo $liferayLogin | cut -c1-20)
  364. if [[ $emailPrefix == "$FORWARD_PREFIX" ]] then
  365. # Remove email prefix
  366. liferayLogin=$(echo $liferayLogin | cut -c21-)
  367. WriteLog "INFO" "addForward: real liferayLogin=$liferayLogin"
  368. # if no forwarding emails are specidied, then
  369. if [[ $emailAddresses == "_" ]] then
  370. WriteLog "INFO" "addForward: removing forwards"
  371. mysql $MYSQL_ENV --silent --execute "update postfix_virtual set email=\"$liferayLogin\" WHERE destination=\"$userId@$DOMAIN\" "
  372. fi
  373. fi
  374. # Remove any existing forwards
  375. WriteLog "INFO" "addForward: removing existing forwards"
  376. mysql $MYSQL_ENV --silent --execute "delete from postfix_virtual where email= \"$liferayLogin\" and destination <> \"$userId@$DOMAIN\" "
  377. # for each email address
  378. if [[ $emailAddresses != "_" ]] then
  379. # convert commas to spaces
  380. emailAddresses=$(echo $emailAddresses | sed "s/,/ /"g )
  381. for emailAddress in $emailAddresses
  382. do
  383. WriteLog "INFO" "addForward: Adding forward to emailAddress=$emailAddress"
  384. mysql $MYSQL_ENV --silent --execute "insert into postfix_virtual (email,destination) values( \"$liferayLogin\" , \"$emailAddress\" ) "
  385. done
  386. # change liferay email so it doesn't get forwarded
  387. WriteLog "INFO" "addForward: Disabling local email by prepending $FORWARD_PREFIX"
  388. mysql $MYSQL_ENV --silent --execute "update postfix_virtual set email= \"$FORWARD_PREFIX$liferayLogin\" where destination= \"$userId@$DOMAIN\" "
  389. fi
  390. return 0
  391. }
  392. #------------------------------------------------------------------------
  393. function dovecot_addUser { # userId password firstName middleName lastName emailAddress
  394. # Eg /usr/sbin/mailadmin.ksh liferay_com_22 passwd jim jimy jimy@$DOMAIN
  395. # INSERT EMAIL ACCOUNT INTO MYSQL TABLE postfix_users
  396. # select * from postfix_users
  397. # id email clear crypt name uid gid homedir maildir quota access postfix
  398. # 22 liferay_com_11@$DOMAIN mypass 510 510 /var/vmail $DOMAIN/liferay_com_11/Maildir/ Y Y
  399. WriteLog "INFO" "$MAIL_TYPE dovecot_addUser: Inserting user into MySQL database=$MYSQL_DATABASE"
  400. mysql $MYSQL_ENV --execute "insert into postfix_users values (0, \"$userId@$DOMAIN\", \"$password\", \"\", \"\", \"$TOMCAT_UID\", \"$VMAIL_GID\", \"$VMAIL_HOME\", \"$DOMAIN/$userId/Maildir/\", \"\", \"Y\", \"Y\");"
  401. mysqlResult=$?
  402. if [ $mysqlResult -ne 0 ]; then
  403. WriteLog "ERROR" "$MAIL_TYPE dovecot_addUser: Unable to add '$userId@$DOMAIN'.MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  404. return $mysqlResult
  405. fi
  406. # INSERT EMAIL ALIAS INTO MYSQL TABLE postfix_virtual
  407. # select * from postfix_virtual
  408. # id email destination
  409. # 27 someuser@$DOMAIN liferay_com_11@$DOMAIN
  410. WriteLog "INFO" "$MAIL_TYPE dovecot_addUser: Adding email alias into MySQL database=$MYSQL_DATABASE"
  411. mysql $MYSQL_ENV --execute "insert into postfix_virtual values (0, \"$emailAddress\", \"$userId@$DOMAIN\");"
  412. mysqlResult=$?
  413. if [ $mysqlResult -ne 0 ]; then
  414. WriteLog "ERROR" "$MAIL_TYPE dovecot_addUser: Unable to add '$userId@$DOMAIN'.MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  415. return $mysqlResult
  416. fi
  417. # CREATE EMAIL DIRECTORY
  418. WriteLog "INFO" "$MAIL_TYPE dovecot_addUser: Creating path $VMAIL_HOME/$DOMAIN/$userId"
  419. mkdir -p "$VMAIL_HOME/$DOMAIN/$userId/Maildir"
  420. mkdirResult=$?
  421. if [ $mkdirResult -ne 0 ]; then
  422. WriteLog "ERROR" "$MAIL_TYPE dovecot_addUser: Unable to create path $VMAIL_HOME/$DOMAIN/$userId/Maildir. Check permissions"
  423. return $mkdirResult
  424. fi
  425. # SET PERMISSIONS ON EMAIL DIRECTORY
  426. WriteLog "INFO" "$MAIL_TYPE dovecot_addUser: chmod $VMAIL_PERM path $VMAIL_HOME/$DOMAIN/$userId"
  427. chmod -R $VMAIL_PERM "$VMAIL_HOME/$DOMAIN/$userId"
  428. chmodResult=$?
  429. if [ $chmodResult -ne 0 ]; then
  430. WriteLog "ERROR" "$MAIL_TYPE dovecot_addUser: Unable to chmod path $VMAIL_HOME/$DOMAIN/$userId. Check permissions."
  431. return $chmodResult
  432. fi
  433. # SET GROUP OF EMAIL DIRECTORY
  434. WriteLog "INFO" "$MAIL_TYPE dovecot_addUser: chgrp path $VMAIL_HOME/$DOMAIN/$userId. grp=$VMAIL_GROUP"
  435. chgrp -R $VMAIL_GROUP "$VMAIL_HOME/$DOMAIN/$userId"
  436. chgrpResult=$?
  437. if [ $chgrpResult -ne 0 ]; then
  438. WriteLog "ERROR" "$MAIL_TYPE dovecot_addUser: Unable to chgrp path $VMAIL_HOME/$DOMAIN/$userId. Check permissions. grp=$VMAIL_GROUP"
  439. return $chgrpResult
  440. fi
  441. return 0
  442. }
  443. #------------------------------------------------------------------------
  444. function dovecot_addVacationMessage { # userId emailAddress vacationMessage
  445. notImplemented dovecot_addVacationMessage
  446. }
  447. #------------------------------------------------------------------------
  448. function dovecot_deleteEmailAddress { # userId
  449. # REMOVE EMAIL ACCOUNT FROM MYSQL TABLE postfix_virtual, Ignore errors
  450. WriteLog "INFO" "$MAIL_TYPE dovecot_deleteUser: Removing user from MySQL database=$MYSQL_DATABASE table: postfix_virtual"
  451. mysql $MYSQL_ENV --execute "delete from postfix_virtual where destination = \"$userId@$DOMAIN\";"
  452. mysqlResult=$?
  453. if [ $mysqlResult -ne 0 ]; then
  454. WriteLog "ERROR" "$MAIL_TYPE dovecot_deleteUser: Unable to remove '$userId@$DOMAIN'.MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  455. # return $mysqlResult Continue removing path even if sql fails
  456. fi
  457. return 0
  458. }
  459. #------------------------------------------------------------------------
  460. function dovecot_deleteUser { # userId
  461. # REMOVE EMAIL ACCOUNT FROM MYSQL TABLE postfix_users
  462. WriteLog "INFO" "$MAIL_TYPE dovecot_deleteUser: Removing user from MySQL database=$MYSQL_DATABASE table: postfix_users"
  463. mysql $MYSQL_ENV --execute "delete from postfix_users where email = \"$userId@$DOMAIN\";"
  464. mysqlResult=$?
  465. if [ $mysqlResult -ne 0 ]; then
  466. WriteLog "ERROR" "$MAIL_TYPE dovecot_deleteUser: Unable to remove '$userId@$DOMAIN'.MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  467. # return $mysqlResult Continue removing path even if sql fails
  468. fi
  469. # REMOVE EMAIL ACCOUNT FROM MYSQL TABLE postfix_virtual
  470. WriteLog "INFO" "$MAIL_TYPE dovecot_deleteUser: Removing user from MySQL database=$MYSQL_DATABASE table: postfix_virtual"
  471. mysql $MYSQL_ENV --execute "delete from postfix_virtual where destination = \"$userId@$DOMAIN\";"
  472. mysqlResult=$?
  473. if [ $mysqlResult -ne 0 ]; then
  474. WriteLog "ERROR" "$MAIL_TYPE dovecot_deleteUser: Unable to remove '$userId@$DOMAIN'.MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  475. # return $mysqlResult Continue removing path even if sql fails
  476. fi
  477. # REMOVE EMAIL DIRECTORIES
  478. WriteLog "INFO" "$MAIL_TYPE dovecot_deleteUser: removing path $VMAIL_HOME/$DOMAIN/$userId"
  479. rm -rf "$VMAIL_HOME/$DOMAIN/$userId"
  480. rmdirResult=$?
  481. if [ $rmdirResult -ne 0 ]; then
  482. WriteLog "ERROR" "$MAIL_TYPE dovecot_deleteUser: Unable to delete path $VMAIL_HOME/$DOMAIN/$userId/Maildir. Check permissions"
  483. return $rmdirResult
  484. fi
  485. return 0
  486. }
  487. #------------------------------------------------------------------------
  488. function dovecot_updateBlocked { # userId blockedEmailAddress
  489. notImplemented dovecot_updateBlocked
  490. }
  491. #------------------------------------------------------------------------
  492. function dovecot_updateEmailAddress { # userId emailAddress
  493. # UPDATE EMAIL ALIAS IN MYSQL TABLE postfix_virtual
  494. # select * from postfix_virtual
  495. # id email destination
  496. # 27 someuser@$DOMAIN liferay_com_11@$DOMAIN
  497. WriteLog "INFO" "$MAIL_TYPE dovecot_updateEmailAddress: Updating email alias in MySQL database=$MYSQL_DATABASE Table postfix_virtual"
  498. mysql $MYSQL_ENV --execute "update postfix_virtual set email=\"$emailAddress\" where destination=\"$userId@$DOMAIN\";"
  499. mysqlResult=$?
  500. if [ $mysqlResult -ne 0 ]; then
  501. WriteLog "ERROR" "$MAIL_TYPE dovecot_updateEmailAddress: Unable to update '$userId@$DOMAIN'. MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  502. return $mysqlResult
  503. fi
  504. return 0
  505. }
  506. #------------------------------------------------------------------------
  507. function dovecot_updatePassword { # userId password
  508. # UPDATE PASSWORD IN MYSQL TABLE postfix_users
  509. # select * from postfix_users
  510. # id email clear
  511. # 22 liferay_com_11@$DOMAIN mypass
  512. WriteLog "INFO" "$MAIL_TYPE dovecot_updatePassword: updating user password in MySQL database=$MYSQL_DATABASE table: postfix_users"
  513. mysql $MYSQL_ENV --execute "update postfix_users set clear=\"$password\" where email=\"$userId@$DOMAIN\" ;"
  514. mysqlResult=$?
  515. if [ $mysqlResult -ne 0 ]; then
  516. WriteLog "ERROR" "$MAIL_TYPE dovecot_updatePassword: Unable to update '$userId@$DOMAIN'. MySqlResult=$mysqlResult. Check MySQL settings: user=$MYSQL_USERNAME database=$MYSQL_DATABASE"
  517. return $mysqlResult
  518. fi
  519. }
  520. ######################## SENDMAIL ########################################
  521. function sendmail_addForward { # userId, emailAddresses
  522. notImplemented sendmail_addForward
  523. }
  524. #------------------------------------------------------------------------
  525. function sendmail_addUser { # userId password firstName middleName lastName emailAddress
  526. notImplemented sendmail_addUser
  527. }
  528. #------------------------------------------------------------------------
  529. function sendmail_addVacationMessage { # userId emailAddress vacationMessage
  530. notImplemented sendmail_addVacationMessage
  531. }
  532. #------------------------------------------------------------------------
  533. function sendmail_deleteEmailAddress { # userId
  534. notImplemented sendmail_deleteEmailAddress
  535. }
  536. #------------------------------------------------------------------------
  537. function sendmail_deleteUser { # userId
  538. notImplemented sendmail_deleteUser
  539. }
  540. #------------------------------------------------------------------------
  541. function sendmail_updateBlocked { # userId blockedEmailAddress
  542. notImplemented sendmail_updateBlocked
  543. }
  544. #------------------------------------------------------------------------
  545. function sendmail_updateEmailAddress { # userId emailAddress
  546. notImplemented sendmail_updateEmailAddress
  547. }
  548. #------------------------------------------------------------------------
  549. function sendmail_updatePassword { # userId password
  550. notImplemented sendmail_updatePassword
  551. }
  552. ######################## CYRUS ########################################
  553. function cyrus_addForward { # userId, emailAddresses
  554. notImplemented cyrus_addForward
  555. }
  556. #------------------------------------------------------------------------
  557. function cyrus_addUser { # userId password firstName middleName lastName emailAddress
  558. notImplemented cyrus_addUser
  559. }
  560. #------------------------------------------------------------------------
  561. function cyrus_addVacationMessage { # userId emailAddress vacationMessage
  562. notImplemented cyrus_addVacationMessage
  563. }
  564. #------------------------------------------------------------------------
  565. function cyrus_deleteEmailAddress { # userId
  566. notImplemented cyrus_deleteEmailAddress
  567. }
  568. #------------------------------------------------------------------------
  569. function cyrus_deleteUser { # userId
  570. notImplemented cyrus_deleteUser
  571. }
  572. #------------------------------------------------------------------------
  573. function cyrus_updateBlocked { # userId blockedEmailAddress
  574. notImplemented cyrus_updateBlocked
  575. }
  576. #------------------------------------------------------------------------
  577. function cyrus_updateEmailAddress { # userId emailAddress
  578. notImplemented cyrus_updateEmailAddress
  579. }
  580. #------------------------------------------------------------------------
  581. function cyrus_updatePassword { # userId password
  582. notImplemented cyrus_updatePassword
  583. }
  584. #########################################################################
  585. # USER MENU MODE
  586. #########################################################################
  587. function HelpUsage {
  588. echo " "
  589. echo " mailadmin.ksh (Display Interactive Menu)"
  590. echo " "
  591. echo " mailadmin.ksh addForward <userId> <emailAddresses>"
  592. echo " mailadmin.ksh addUser <userId> <password> <firstName> <middleName> <lastName> <emailAddress>"
  593. echo " mailadmin.ksh addVacationMessage <userId> <emailAddress> <vacationMessage>"
  594. echo " mailadmin.ksh deleteEmailAddress <userId>"
  595. echo " mailadmin.ksh deleteUser <userId>"
  596. echo " mailadmin.ksh updateBlocked <userId> <blockedEmailAddress>"
  597. echo " mailadmin.ksh updateEmailAddress <userId> <emailAddress>"
  598. echo " mailadmin.ksh updatePassword <userId> <password>"
  599. echo " "
  600. echo " "
  601. }
  602. #------------------------------------------------------------------------
  603. function UserMenuPrompt {
  604. echo " "
  605. echo " "
  606. echo " *** mailadmin.ksh Menu *** "
  607. echo " "
  608. TODAY=`date "+20%y-%m-%d %H:%M:%S"`
  609. prompt="$TODAY $gsServerName $gsServerPublicIP [$gsUserName] Menu Option or Enter: "
  610. PS3=$prompt # Set prompt
  611. }
  612. #------------------------------------------------------------------------
  613. function UserMenu {
  614. getServerIPAddress # set $gsServerPublicIP
  615. gsAppMode="menu"
  616. WriteLog "INFO" "UserMenu: Host:$gsServerName "
  617. UserMenuPrompt
  618. select i in addForward addUser addVacationMessage deleteEmailAddress deleteUser \
  619. updateBlocked updateEmailAddress updatePassword \
  620. Logs Status Help QuitProgram
  621. do case $i in
  622. addForward)
  623. read userId?' Enter userId:'
  624. read emailAddresses?' Enter emailAddresses:'
  625. addForward $userId $emailAddresses;;
  626. addUser)
  627. read userId?' Enter userId:'
  628. read password?' Enter password:'
  629. read firstName?' Enter firstName:'
  630. read middleName?' Enter middleName:'
  631. read lastName?' Enter lastName:'
  632. read emailAddress?' Enter emailAddress:'
  633. addUser $userId $password $firstName $middleName $lastName $emailAddress;;
  634. addVacationMessage)
  635. read userId?' Enter userId:'
  636. read emailAddress?' Enter emailAddress:'
  637. read vacationMessage?' Enter vacationMessage:'
  638. addVacationMessage $userId $emailAddress $vacationMessage;;
  639. deleteEmailAddress)
  640. read userId?' Enter userId:'
  641. deleteEmailAddress $userId;;
  642. deleteUser)
  643. read userId?' Enter userId:'
  644. deleteUser $userId;;
  645. updateBlocked)
  646. read userId?' Enter userId:'
  647. read blockedEmailAddress?' Enter blockedEmailAddress:'
  648. updateBlocked $userId $blockedEmailAddress;;
  649. updateEmailAddress)
  650. read userId?' Enter userId:'
  651. read emailAddress?' Enter emailAddress:'
  652. updateEmailAddress $userId $emailAddress;;
  653. updatePassword)
  654. read userId?' Enter userId:'
  655. read password?' Enter password:'
  656. updatePassword $userId $password;;
  657. Status) echo " ";
  658. echo "user= $gsUserName";
  659. echo "MAIL_TYPE= $MAIL_TYPE"
  660. echo "DOMAIN= $DOMAIN"
  661. echo "LOG_FILE_MAX= $LOG_FILE_MAX"
  662. echo "LOG_FILENAME= $LOG_FILENAME"
  663. echo "MYSQL_USERNAME= $MYSQL_USERNAME"
  664. echo "MYSQL_DATABASE= $MYSQL_DATABASE"
  665. echo "VMAIL_GROUP= $VMAIL_GROUP"
  666. echo "VMAIL_PERM= $VMAIL_PERM"
  667. echo "TOMCAT_UID= $TOMCAT_UID"
  668. echo "VMAIL_GID= $VMAIL_GID"
  669. echo "VMAIL_HOME= $VMAIL_HOME"
  670. echo " ";;
  671. Help) HelpUsage;;
  672. Logs) tail -50 $LOG_FILENAME;;
  673. QuitProgram) break;;
  674. "") print -u2 you must select one of the above options;;
  675. esac
  676. UserMenuPrompt
  677. done
  678. WriteLog "INFO" "UserMenu Ended"
  679. }
  680. #########################################################################
  681. # MAIN PROGRAM
  682. #########################################################################
  683. AppInit # Get server name and IP address. Set defaults.
  684. #set up exit trap
  685. trap 'echo "$TODAY WARN mailadmin.ksh $gsUserName $gsAppMode Trap Exit errno:$ERRNO line:$LINENO" >> $LOG_FILENAME' 0
  686. case $1 in # Run the correct program mode
  687. "--help" | "-?" | "-help") HelpUsage;
  688. trap EXIT # Remove exit trap
  689. exit 0;;
  690. "logs" | "LOGS") echo $LOG_FILENAME;
  691. tail -50 $LOG_FILENAME; # display log
  692. trap EXIT # Remove exit trap
  693. exit 0;;
  694. "addForward") addForward $2 $3 $4 $5 $6 $7 $8;
  695. CommonExit $?;;
  696. "addUser") addUser $2 $3 $4 $5 $6 $7 $8;
  697. CommonExit $?;;
  698. "addVacationMessage") addVacationMessage $2 $3 $4 $5 $6 $7 $8;
  699. CommonExit $?;;
  700. "deleteEmailAddress") deleteEmailAddress $2 $3 $4 $5 $6 $7 $8;
  701. CommonExit $?;;
  702. "deleteUser") deleteUser $2 $3 $4 $5 $6 $7 $8;
  703. CommonExit $?;;
  704. "updateBlocked") updateBlocked $2 $3 $4 $5 $6 $7 $8;
  705. CommonExit $?;;
  706. "updateEmailAddress") updateEmailAddress $2 $3 $4 $5 $6 $7 $8;
  707. CommonExit $?;;
  708. "updatePassword") updatePassword $2 $3 $4 $5 $6 $7 $8;
  709. CommonExit $?;;
  710. *) UserMenu ;; # Run as a command line menu
  711. esac
  712. trap EXIT # Remove exit trap
  713. exit 0
  714. # End of file.
  715. #------------------------------------------------------------------------
  716. # DO NOT CHANGE LAST LINE. IT CAN BE USED FOR FTP SOFTWARE DEPLOYMENT
  717. # TO MAKE SURE THE ENTIRE FILE WAS TRANSFERRED CORRECTLY
  718. # MAILADMIN_LAST_LINE_MARKER