lcmlog-server-utils 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/bin/bash
  2. DIR="/var/local/log/lcmlog-data"
  3. ######################
  4. # AUXILIARY FUNCTION #
  5. ######################
  6. usage()
  7. {
  8. echo "Usage: $0 [-i|-l|-u]"
  9. echo "Use -h option to show the help message."
  10. }
  11. ######
  12. method_help()
  13. {
  14. echo "lcmlog-auth-utils: utility for manage the authorization system of lcmlog-server."
  15. echo
  16. echo "Usage: $0 [option]"
  17. echo
  18. echo "Options:"
  19. echo " -i Initialization of the folder structure in default directory ($DIR)"
  20. echo " -l See who is authorized in $DIR/auth"
  21. echo " -u Update folders ACL (data and logs)"
  22. echo
  23. echo " -h Show this help"
  24. }
  25. ######
  26. method_init()
  27. {
  28. if ! [ -d $DIR ]; then
  29. echo "Folder $DIR not present: you should create it first!"
  30. exit 0
  31. else
  32. # auth folder
  33. if [ -d $DIR/auth ]; then
  34. echo "Folder $DIR/auth already present. Exit."
  35. else
  36. echo "Setting up auth folder..."
  37. mkdir $DIR/auth
  38. touch $DIR/auth/150
  39. touch $DIR/auth/Admin
  40. touch $DIR/auth/Valhalla # ex 150
  41. touch $DIR/auth/Nirvana # ex admin
  42. cp /usr/local/src/lcm-unimi/lcmlog-server/auth.toml $DIR/auth/auth.toml
  43. echo "Completed."
  44. fi
  45. # data folder
  46. if [ -d $DIR/data ]; then
  47. echo "Folder $DIR/data already present. Exit."
  48. else
  49. echo "Setting up data folder..."
  50. mkdir $DIR/data
  51. echo "Completed."
  52. fi
  53. # logs folder
  54. if [ -d $DIR/logs ]; then
  55. echo "Folder $DIR/logs already present. Exit."
  56. else
  57. echo "Setting up logs folder..."
  58. mkdir $DIR/logs
  59. echo "Completed."
  60. fi
  61. echo
  62. echo "Now you should add entries in $DIR/auth files and then update folder permissions using -u flag."
  63. fi
  64. }
  65. ######
  66. method_authlookup()
  67. {
  68. if [ -d $DIR/auth ]; then
  69. echo -e "\033[1;36mAUTHORIZATIONS in $DIR/auth:\033[00m"
  70. echo
  71. # look 150
  72. echo -e "\033[01m150 auth: GET 150, GET Admin, POST 150\033[00m"
  73. for uid in $(awk '{print $1}' $DIR/auth/150); do
  74. getent passwd $uid
  75. done
  76. echo
  77. # look admin
  78. echo -e "\033[01mAdmin auth: GET 150, GET Admin, POST 150, POST Admin, UPDATE\033[00m"
  79. for uid in $(awk '{print $1}' $DIR/auth/Admin); do
  80. getent passwd $uid
  81. done
  82. echo
  83. # look ex 150
  84. echo -e "\033[01mValhalla auth: GET 150, GET Admin\033[00m"
  85. for uid in $(awk '{print $1}' $DIR/auth/Valhalla); do
  86. getent passwd $uid
  87. done
  88. echo
  89. # look ex admin
  90. echo -e "\033[01mNirvana auth: GET 150, GET Admin, POST Admin\033[00m"
  91. for uid in $(awk '{print $1}' $DIR/auth/Nirvana); do
  92. getent passwd $uid
  93. done
  94. echo
  95. echo
  96. # look dir permissions
  97. echo -e "\033[1;36mFOLDER PERMISSIONS:\033[00m"
  98. echo
  99. getfacl $DIR/data
  100. getfacl $DIR/logs
  101. else
  102. echo "auth folder not present. Use the -i option to create it."
  103. fi
  104. }
  105. ######
  106. method_updateACL()
  107. {
  108. if [ -d $DIR/auth ] && [ -d $DIR/data ] && [ -d $DIR/logs ]; then
  109. method_authlookup
  110. echo "Do you want to set new permissions? [y|n]"
  111. read ANSWER
  112. while [ -z $ANSWER ]; do
  113. echo "Yes (y) or no (n)? "
  114. read ANSWER
  115. done
  116. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  117. echo "Yes (y) or no (n)? "
  118. read ANSWER
  119. while [ -z $ANSWER ]; do
  120. echo "Yes (y) or no (n)? "
  121. read ANSWER
  122. done
  123. done
  124. if [ $ANSWER == "n" ]; then exit 0; fi
  125. echo
  126. # remove old permissions
  127. echo "Removing old permissions..."
  128. setfacl -b $DIR/data
  129. setfacl -b $DIR/logs
  130. # add new permissions
  131. echo "Setting up new permissions..."
  132. # set 150
  133. for uid in $(awk '{print $1}' $DIR/auth/150); do
  134. setfacl -m u:$uid:rwx $DIR/data
  135. setfacl -m u:$uid:rwx $DIR/logs
  136. done
  137. # set admin
  138. for uid in $(awk '{print $1}' $DIR/auth/Admin); do
  139. setfacl -m u:$uid:rwx $DIR/data
  140. setfacl -m u:$uid:rwx $DIR/logs
  141. done
  142. # set ex 150
  143. for uid in $(awk '{print $1}' $DIR/auth/Valhalla); do
  144. setfacl -m u:$uid:rx $DIR/data
  145. setfacl -m u:$uid:rwx $DIR/logs
  146. done
  147. # set ex admin
  148. for uid in $(awk '{print $1}' $DIR/auth/Nirvana); do
  149. setfacl -m u:$uid:rwx $DIR/data
  150. setfacl -m u:$uid:rwx $DIR/logs
  151. done
  152. echo "Done."
  153. echo
  154. echo "New permissions are:"
  155. echo
  156. method_authlookup
  157. else
  158. echo "Error: some folders are missing. Use the -i option to create missing folders."
  159. fi
  160. }
  161. ###############
  162. # MAIN SCRIPT #
  163. ###############
  164. ## ENTRY POINT - INITIALIZATION
  165. # Check number of arguments
  166. if (( $# != 1 )); then
  167. usage
  168. exit 1
  169. fi
  170. # Check that the option is only one
  171. # --> -i OK
  172. # -il NO
  173. if (( ${#1} != 2 )); then
  174. usage
  175. exit 1
  176. fi
  177. # Select method from the option
  178. while getopts ":iluh" o; do
  179. case $o in
  180. i) method_init;;
  181. l) method_authlookup;;
  182. u) method_updateACL;;
  183. h) method_help;;
  184. \?) usage && exit 1;;
  185. esac
  186. done
  187. exit 0