lcmlog-server-utils 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. touch $DIR/data/.data
  52. echo "Completed."
  53. fi
  54. # logs folder
  55. if [ -d $DIR/logs ]; then
  56. echo "Folder $DIR/logs already present. Exit."
  57. else
  58. echo "Setting up logs folder..."
  59. mkdir $DIR/logs
  60. touch $DIR/logs/logfile
  61. echo "Completed."
  62. fi
  63. echo
  64. echo "Now you should add entries in $DIR/auth files and then update folder permissions using -u flag."
  65. fi
  66. }
  67. ######
  68. method_authlookup()
  69. {
  70. if [ -d $DIR/auth ]; then
  71. echo
  72. echo -e "\033[1;36mAUTHORIZATIONS in $DIR/auth:\033[00m"
  73. echo
  74. # look 150
  75. echo -e "\033[01m150 auth: GET 150, GET Admin, POST 150\033[00m"
  76. for uid in $(awk '{print $1}' $DIR/auth/150); do
  77. getent passwd $uid
  78. done
  79. echo
  80. # look admin
  81. echo -e "\033[01mAdmin auth: GET 150, GET Admin, POST 150, POST Admin, UPDATE\033[00m"
  82. for uid in $(awk '{print $1}' $DIR/auth/Admin); do
  83. getent passwd $uid
  84. done
  85. echo
  86. # look ex 150
  87. echo -e "\033[01mValhalla auth: GET 150, GET Admin\033[00m"
  88. for uid in $(awk '{print $1}' $DIR/auth/Valhalla); do
  89. getent passwd $uid
  90. done
  91. echo
  92. # look ex admin
  93. echo -e "\033[01mNirvana auth: GET 150, GET Admin, POST Admin\033[00m"
  94. for uid in $(awk '{print $1}' $DIR/auth/Nirvana); do
  95. getent passwd $uid
  96. done
  97. echo
  98. echo
  99. # look dir permissions
  100. echo -e "\033[1;36mACL PERMISSIONS:\033[00m"
  101. echo
  102. getfacl $DIR/data
  103. getfacl $DIR/data/.data
  104. getfacl $DIR/logs
  105. else
  106. echo "auth folder not present. Use the -i option to create it."
  107. fi
  108. }
  109. ######
  110. method_updateACL()
  111. {
  112. if [ -d $DIR/auth ] && [ -d $DIR/data ] && [ -d $DIR/logs ]; then
  113. method_authlookup
  114. echo "Do you want to set new permissions? [y|n]"
  115. read ANSWER
  116. while [ -z $ANSWER ]; do
  117. echo "Yes (y) or no (n)? "
  118. read ANSWER
  119. done
  120. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  121. echo "Yes (y) or no (n)? "
  122. read ANSWER
  123. while [ -z $ANSWER ]; do
  124. echo "Yes (y) or no (n)? "
  125. read ANSWER
  126. done
  127. done
  128. if [ $ANSWER == "n" ]; then exit 0; fi
  129. echo
  130. # remove old permissions
  131. echo "Removing old permissions..."
  132. setfacl -b $DIR/data
  133. setfacl -b $DIR/data/.data
  134. setfacl -b $DIR/logs
  135. # add new permissions
  136. echo "Setting up new permissions..."
  137. # set 150
  138. for uid in $(awk '{print $1}' $DIR/auth/150); do
  139. setfacl -m u:$uid:rwx $DIR/data
  140. setfacl -m u:$uid:rw $DIR/data/.data
  141. setfacl -m u:$uid:rwx $DIR/logs
  142. done
  143. # set admin
  144. for uid in $(awk '{print $1}' $DIR/auth/Admin); do
  145. setfacl -m u:$uid:rwx $DIR/data
  146. setfacl -m u:$uid:rw $DIR/data/.data
  147. setfacl -m u:$uid:rwx $DIR/logs
  148. done
  149. # set ex 150
  150. for uid in $(awk '{print $1}' $DIR/auth/Valhalla); do
  151. setfacl -m u:$uid:rx $DIR/data
  152. setfacl -m u:$uid:r $DIR/data/.data
  153. setfacl -m u:$uid:rwx $DIR/logs
  154. done
  155. # set ex admin
  156. for uid in $(awk '{print $1}' $DIR/auth/Nirvana); do
  157. setfacl -m u:$uid:rwx $DIR/data
  158. setfacl -m u:$uid:rw $DIR/data/.data
  159. setfacl -m u:$uid:rwx $DIR/logs
  160. done
  161. echo "Done."
  162. echo
  163. echo "New permissions are:"
  164. method_authlookup
  165. else
  166. echo "Error: some folders are missing. Use the -i option to create missing folders."
  167. fi
  168. }
  169. ###############
  170. # MAIN SCRIPT #
  171. ###############
  172. ## ENTRY POINT - INITIALIZATION
  173. # Check number of arguments
  174. if (( $# != 1 )); then
  175. usage
  176. exit 1
  177. fi
  178. # Check that the option is only one
  179. # --> -i OK
  180. # -il NO
  181. if (( ${#1} != 2 )); then
  182. usage
  183. exit 1
  184. fi
  185. # Select method from the option
  186. while getopts ":iluh" o; do
  187. case $o in
  188. i) method_init;;
  189. l) method_authlookup;;
  190. u) method_updateACL;;
  191. h) method_help;;
  192. \?) usage && exit 1;;
  193. esac
  194. done
  195. exit 0