freedombone-addsipuser 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. MY_USERNAME=
  30. EXTENSION=
  31. PASSWORD=
  32. CONFIG_FILE=/etc/sipwitch.conf
  33. USER_EXISTS="no"
  34. function show_help {
  35. echo ''
  36. echo 'freedombone-addsipuser -u [username] -e [extension] -p [password]'
  37. echo ''
  38. exit 0
  39. }
  40. function sip_user_exists {
  41. IFS=''
  42. while read line; do
  43. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  44. USER_EXISTS="yes"
  45. return
  46. fi
  47. done < $CONFIG_FILE
  48. }
  49. function update_sip_user {
  50. USER_FOUND=
  51. NEW_CONFIG_FILE="${CONFIG_FILE}.new"
  52. if [ -f $NEW_CONFIG_FILE ]; then
  53. rm -f $NEW_CONFIG_FILE
  54. fi
  55. touch $NEW_CONFIG_FILE
  56. IFS=''
  57. while read line; do
  58. if [ ! $USER_FOUND ]; then
  59. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  60. USER_FOUND="yes"
  61. fi
  62. else
  63. if [[ "$line" == *"<extension>"* ]]; then
  64. line="<extension>$EXTENSION</extension>"
  65. fi
  66. if [[ "$line" == *"<secret>"* ]]; then
  67. line="<secret>$PASSWORD</secret>"
  68. fi
  69. if [[ "$line" == *"<display>"* ]]; then
  70. line="<display>$MY_USERNAME $EXTENSION</display>"
  71. USER_FOUND=
  72. fi
  73. fi
  74. echo $line >> $NEW_CONFIG_FILE
  75. done < $CONFIG_FILE
  76. mv $NEW_CONFIG_FILE $CONFIG_FILE
  77. }
  78. function add_sip_user {
  79. NEW_CONFIG_FILE="${CONFIG_FILE}.new"
  80. if [ -f $NEW_CONFIG_FILE ]; then
  81. rm -f $NEW_CONFIG_FILE
  82. fi
  83. touch $NEW_CONFIG_FILE
  84. IFS=''
  85. while read line; do
  86. if [[ "$line" == *'</provision>' ]]; then
  87. echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
  88. echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
  89. echo "<secret>$PASSWORD</secret>" >> $NEW_CONFIG_FILE
  90. echo "<display>$MY_USERNAME $EXTENSION</display>" >> $NEW_CONFIG_FILE
  91. echo '</user>' >> $NEW_CONFIG_FILE
  92. fi
  93. echo $line >> $NEW_CONFIG_FILE
  94. done < $CONFIG_FILE
  95. mv $NEW_CONFIG_FILE $CONFIG_FILE
  96. usermod -aG sipwitch $MY_USERNAME
  97. }
  98. while [[ $# > 1 ]]
  99. do
  100. key="$1"
  101. case $key in
  102. -h|--help)
  103. show_help
  104. ;;
  105. -u|--user)
  106. shift
  107. MY_USERNAME="$1"
  108. ;;
  109. -e|--extension)
  110. shift
  111. EXTENSION="$1"
  112. ;;
  113. -p|--password)
  114. shift
  115. PASSWORD="$1"
  116. ;;
  117. *)
  118. # unknown option
  119. ;;
  120. esac
  121. shift
  122. done
  123. if ! [[ $MY_USERNAME && $EXTENSION && $PASSWORD ]]; then
  124. show_help
  125. fi
  126. if [ ! -f $CONFIG_FILE ]; then
  127. echo "SIP configuration file not found"
  128. exit 1
  129. fi
  130. # the user must already exist on the system
  131. if [ ! -d /home/$MY_USERNAME ]; then
  132. echo "User $MY_USERNAME not found"
  133. exit 2
  134. fi
  135. sip_user_exists
  136. if [[ $USER_EXISTS == "yes" ]]; then
  137. update_sip_user
  138. echo "SIP user $MY_USERNAME amended"
  139. else
  140. add_sip_user
  141. echo "SIP user $MY_USERNAME added"
  142. fi
  143. systemctl restart sipwitch
  144. exit 0