freedombone-addsipuser 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Adds a SIP phone user to the system
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-addsipuser
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. MY_USERNAME=
  33. EXTENSION=
  34. PASSWORD=
  35. CONFIG_FILE=/etc/sipwitch.conf
  36. USER_EXISTS="no"
  37. function show_help {
  38. echo ''
  39. echo $"${PROJECT_NAME}-addsipuser -u [username] -e [extension] -p [password]"
  40. echo ''
  41. exit 0
  42. }
  43. function sip_user_exists {
  44. IFS=''
  45. while read line; do
  46. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  47. USER_EXISTS="yes"
  48. return
  49. fi
  50. done < $CONFIG_FILE
  51. }
  52. function update_sip_user {
  53. USER_FOUND=
  54. NEW_CONFIG_FILE="${CONFIG_FILE}.new"
  55. if [ -f $NEW_CONFIG_FILE ]; then
  56. rm -f $NEW_CONFIG_FILE
  57. fi
  58. touch $NEW_CONFIG_FILE
  59. IFS=''
  60. while read line; do
  61. if [ ! $USER_FOUND ]; then
  62. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  63. USER_FOUND="yes"
  64. fi
  65. else
  66. if [[ "$line" == *"<extension>"* ]]; then
  67. line="<extension>$EXTENSION</extension>"
  68. fi
  69. if [[ "$line" == *"<secret>"* ]]; then
  70. line="<secret>$PASSWORD</secret>"
  71. fi
  72. if [[ "$line" == *"<display>"* ]]; then
  73. line="<display>$MY_USERNAME $EXTENSION</display>"
  74. USER_FOUND=
  75. fi
  76. fi
  77. echo $line >> $NEW_CONFIG_FILE
  78. done < $CONFIG_FILE
  79. mv $NEW_CONFIG_FILE $CONFIG_FILE
  80. }
  81. function add_sip_user {
  82. NEW_CONFIG_FILE="${CONFIG_FILE}.new"
  83. if [ -f $NEW_CONFIG_FILE ]; then
  84. rm -f $NEW_CONFIG_FILE
  85. fi
  86. touch $NEW_CONFIG_FILE
  87. IFS=''
  88. while read line; do
  89. if [[ "$line" == *'</provision>' ]]; then
  90. echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
  91. echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
  92. echo "<secret>$PASSWORD</secret>" >> $NEW_CONFIG_FILE
  93. echo "<display>$MY_USERNAME $EXTENSION</display>" >> $NEW_CONFIG_FILE
  94. echo '</user>' >> $NEW_CONFIG_FILE
  95. fi
  96. echo $line >> $NEW_CONFIG_FILE
  97. done < $CONFIG_FILE
  98. mv $NEW_CONFIG_FILE $CONFIG_FILE
  99. usermod -aG sipwitch $MY_USERNAME
  100. }
  101. while [[ $# > 1 ]]
  102. do
  103. key="$1"
  104. case $key in
  105. -h|--help)
  106. show_help
  107. ;;
  108. -u|--user)
  109. shift
  110. MY_USERNAME="$1"
  111. ;;
  112. -e|--extension)
  113. shift
  114. EXTENSION="$1"
  115. ;;
  116. -p|--password)
  117. shift
  118. PASSWORD="$1"
  119. ;;
  120. *)
  121. # unknown option
  122. ;;
  123. esac
  124. shift
  125. done
  126. if ! [[ $MY_USERNAME && $EXTENSION && $PASSWORD ]]; then
  127. show_help
  128. fi
  129. if [ ! -f $CONFIG_FILE ]; then
  130. echo $"SIP configuration file not found"
  131. exit 1
  132. fi
  133. # the user must already exist on the system
  134. if [ ! -d /home/$MY_USERNAME ]; then
  135. echo $"User $MY_USERNAME not found"
  136. exit 2
  137. fi
  138. sip_user_exists
  139. if [[ $USER_EXISTS == "yes" ]]; then
  140. update_sip_user
  141. echo $"SIP user $MY_USERNAME amended"
  142. else
  143. add_sip_user
  144. echo $"SIP user $MY_USERNAME added"
  145. fi
  146. systemctl restart sipwitch
  147. exit 0