darrenldl

_setfacl.sh

Dec 6th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 23.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Author : darrenldl ([email protected])
  4. # Hello, this is my first large script, hope you find it useful : )
  5.  
  6. # Story : I am quite into system hardening, so I use grsecurity and TOMOYO.
  7. #     While MAC(like RBAC from grsecurity, which can restrict based on roles) are certainly useful,
  8. #     not every system supports them.
  9. #     Thus I think setfacl is quite useful, which I discovered a while ago, but the permission
  10. #     resets everytime I receive updates, so I didn't rely on it too much as it's too dreadful
  11. #     to set them all again, and too dreadful to set them in the first place as well.
  12. #     Until recently, I came up with the idea of using a script to manipulate it, and allow user to have
  13. #     a config for it. Hence the creation of this script to restrict user access very easily.
  14. #     As far as I know, I don't see any similar scripts around, but if there are,
  15. #     hope you like this alternative as well.
  16.  
  17. # Usage : This script can help you set permissions via setfacl easily with the help of a config file
  18. # Note :  This script will try to detect all possible config error, so the lines would look messy and clumsy,
  19. #     since I'm not savvy in bash scripting.
  20.  
  21. # Terminology
  22. # Operand = NO, RESTRICT, EXCLUDE, PERM or END
  23. # Target = ALL or USER
  24. # ACL block = Block of words which starts with NO/RESTRICT and ends with END
  25.  
  26. # By default, permission = ---
  27.  
  28. # Initialisation
  29.  
  30. # config_file=/path/to/your/config/file
  31. config_file=setfacl_file_list
  32. users=( daren )
  33. groups=( daren users )
  34.  
  35. line_no=1
  36. fl_start=0  # fl_start is used by second part of code  
  37. fl_end=0    # fl_end is used by second part of code
  38. c_error=0   # c_error = config error
  39. c_warn=0    # c_warn = config warning counter ( non-critical mistakes )
  40. fl_o=0      # fl_o is used to check if operands are in order or missing
  41. fl_all=0    # fl_all is used to check if ALL is used
  42. fl_others=0 # fl_others is used to check if other words are used with ALL(if ALL is used, ALL should be the only operand)
  43. fl_u_valid_line=()  # fl_u_valid stores flags to indicate whether users in config are present in users array, in the current line
  44. fl_u_checked=()     # fl_u_checked stores if the user is already mentioned
  45. fl_g_valid_line=()  # fl_g_valid stores flags to indicate whether groups in config are present in groups array, in the current line
  46. fl_g_checked=()     # fl_g_checked stores if the group is already mentioned
  47. fl_u=()         # fl_u is used by second part of code
  48. fl_g=()         # fl_g ss used by second part of code
  49. fl_r_u=0
  50. fl_r_g=0
  51. fl_e_u=0
  52. fl_e_g=0
  53. fl_no=0     # fl_no is used in second part of code
  54. fl_no_b=0
  55. fl_f_list=0
  56. fl_e=0
  57.  
  58. for (( i=0; i < ${#users[@]}; i++ )); do
  59.     fl_u[$i]=0
  60. done
  61. for (( i=0; i < ${#groups[@]}; i++ )); do
  62.     fl_g[$i]=0
  63. done
  64.  
  65. # fl_o by default is 0
  66. # after RESTRICT, fl_o is set to 1
  67. # since EXCLUDE is optional, fl_o is not changed after EXCLUDE
  68. # after PERM, fl_o is set to 2
  69. # after END, fl_o is set back to 0
  70. # Hence, value of fl_o indicates the last operand used, and can also check if the correct operand is missing
  71. # fl_all and fl_others are used to detect if both ALL and user are specified in config
  72.  
  73. # Misc check ( if users exist, groups exist )
  74.  
  75. # Config check ( This part is crucial, as it ensures input are correct and will not cause error in second part )
  76.  
  77. while read line_raw; do
  78.     if [[ ! ($line_raw =~ ^$  || -z $line_raw || $line_raw =~ ^# ) ]]; then     # Check if line is empty/null/spaces or starts with #
  79.         a=0
  80.         fl_all=0
  81.         fl_others=0
  82.         for (( i=0; i < ${#users[@]}; i++ )); do
  83.             fl_u_valid_line[$i]=0
  84.             fl_u_checked[$i]=0
  85.         done
  86.         for (( i=0; i < ${#groups[@]}; i++ )); do
  87.             fl_g_valid_line[$i]=0
  88.             fl_g_checked[$i]=0
  89.         done
  90.         line=()
  91.         for word in $line_raw; do
  92.             if [[ $word == \#* || $word == '#' ]]; then
  93.                 # If word starts with #, stop reading (probably is a comment)
  94.                 break
  95.             fi
  96.             #echo $word
  97.             # Uncomment echo $word to see what words this script will actually process
  98.             line[$a]=${word}
  99.             ((a++))
  100.         done
  101.         if (( $fl_f_list == 0 )); then
  102.             case  ${line[0]} in
  103.             NO )        if (( $fl_o != 0 )); then
  104.                         echo "$line_no : Missing END before current line"
  105.                         c_error=1
  106.                         break
  107.                     fi
  108.                     if (( $fl_no == 1 )); then
  109.                         echo "$line_no : NO cannot be repeated"
  110.                         c_error=1
  111.                         break
  112.                     fi
  113.                     if (( ${#line[@]} > 1 )); then
  114.                         echo "$line_no : NO - too many arguments"
  115.                         c_error=1
  116.                         break
  117.                     fi
  118.                     fl_no=1
  119.                     # No modification of fl_o, since NO is optional
  120.                 ;;
  121.             RESTRICT )  if (( $fl_o != 0 )) && (( $fl_o != 1 )); then
  122.                         # Check if previous operand is either END or RESTRICT
  123.                         echo "$line_no : Missing END before current line"
  124.                         c_error=1
  125.                         break
  126.                     fi          # fl_o = 0 or 1 is accepted
  127.                     fl_o=1
  128.                     fl_e=0
  129.                     case ${line[1]} in
  130.                     USER )  if (( $fl_r_u == 1 )); then
  131.                             echo "$line_no : You can only specify RESTRICT USER once in each block"
  132.                             c_error=1
  133.                             break
  134.                         fi
  135.                         fl_r_u=1
  136.                         if (( ${#line[@]} > $[2+${#users[@]}] )); then
  137.                             # Even if the user types ALL, it shouldn't exceed the limit
  138.                             # This will still be positive if you use ALL, but have an empty users array
  139.                             echo "$line_no : RESTRICT USER - too many users"
  140.                             c_error=1
  141.                             break
  142.                         fi
  143.                         for (( i=2; i < ${#line[@]}; i++ )); do
  144.                             if [[ ${line[$i]} == "ALL" ]]; then # Check if ALL or users exist
  145.                                 fl_all=1
  146.                             else
  147.                                 fl_others=1
  148.                             fi
  149.                             if (( $fl_all == 1 )) && (( $fl_others == 1 )); then break; fi  # End for loop
  150.                         done
  151.                         if (( $fl_all == 1 && $fl_others == 1 )); then  # Check if ALL is used with other users
  152.                             echo "$line_no : No other users should be specified with ALL"
  153.                             c_error=1
  154.                             break
  155.                         else    if (( $fl_all == 0 )) && (( $fl_others == 1 )); then    # Only users, no ALL
  156.                                 # If ALL is not specified, check specified users
  157.                                 for (( i=2; i < ${#line[@]}; i++ )); do     # Check if users are valid elements of users array
  158.                                     for (( j=0; j < ${#users[@]}; j++ )); do
  159.                                         if [[ ${line[$i]} == ${users[$j]} ]] && !(( ${fl_u_checked[$j]} == 1 )); then
  160.                                             fl_u_valid_line[$[$i-2]]=1
  161.                                             fl_u_checked[$j]=1  # Set 1 to indicate user is checked
  162.                                             #echo "$line_no : ignored user : ${users[$j]}"
  163.                                         else    if [[ ${line[$i]} == ${users[$j]} ]] && (( ${fl_u_checked[$j]} == 1 )); then
  164.                                                 echo "$line_no : user ${users[$j]} is repeated"
  165.                                                 ((c_warn++))
  166.                                                 fl_u_valid_line[$[$i-2]]=1
  167.                                                 # Don't set fl_u_checked since it's repeated
  168.                                             fi
  169.                                         fi
  170.                                     done
  171.                                 done
  172.                                 for (( i=2; i < ${#line[@]}; i++ )); do     # Report invalid users
  173.                                     if (( ${fl_u_valid_line[$[$i-2]]} == 0 )); then
  174.                                         echo "$line_no : RESTRICT USER - user ${line[$i]} is not present in users array"
  175.                                         ((c_warn++))
  176.                                     fi
  177.                                 done
  178.                             fi
  179.                         fi
  180.                         ;;
  181.                     GROUP ) if (( $fl_r_g == 1 )); then
  182.                             echo "$line_no : You can only specify RESTRICT GROUP once in each block"
  183.                             c_error=1
  184.                             break
  185.                         fi
  186.                         fl_r_g=1
  187.                         if (( ${#line[@]} > $[2+${#groups[@]}] )); then
  188.                             # Even if  ALL is specified, it shouldn't exceed the limit
  189.                             # This will still be positive if you use ALL, but have an empty groups array
  190.                             echo "$line_no : RESTRICT GROUP - too many groups"
  191.                             c_error=1
  192.                             break
  193.                         fi
  194.                         for (( i=2; i < ${#line[@]}; i++ )); do
  195.                             if [[ ${line[$i]} == "ALL" ]]; then # Check if ALL or users exist
  196.                                 fl_all=1
  197.                             else
  198.                                 fl_others=1
  199.                             fi
  200.                             if (( $fl_all == 1 )) && (( $fl_others == 1 )); then break; fi  # End for loop
  201.                         done
  202.                         if (( $fl_all == 1 && $fl_others == 1 )); then  # Check if ALL is used with other users
  203.                             echo "$line_no : No other groups should be specified with ALL"
  204.                             c_error=1
  205.                             break
  206.                         else    if (( $fl_all == 0 )) && (( $fl_others == 1 )); then    # Only groups, no ALL
  207.                                 # If ALL is not specified, check specified groups
  208.                                 for (( i=2; i < ${#line[@]}; i++ )); do     # Check if users are valid elements of users array
  209.                                     for (( j=0; j < ${#groups[@]}; j++ )); do
  210.                                         if [[ ${line[$i]} == ${groups[$j]} ]] && !(( ${fl_g_checked[$j]} == 1 )); then
  211.                                             fl_g_valid_line[$[$i-2]]=1
  212.                                             fl_g_checked[$j]=1  # Set 1 to indicate user is checked
  213.                                             #echo "$line_no : ignored user : ${users[$j]}"
  214.                                         else    if [[ ${line[$i]} == ${groups[$j]} ]] && (( ${fl_g_checked[$j]} == 1 )); then
  215.                                                 echo "$line_no : group ${groups[$j]} is repeated"
  216.                                                 ((c_warn++))
  217.                                                 fl_g_valid_line[$[$i-2]]=1
  218.                                                 # Don't set fl_g_checked since it's repeated
  219.                                             fi
  220.                                         fi
  221.                                     done
  222.                                 done
  223.                                 for (( i=2; i < ${#line[@]}; i++ )); do     # Report invalid users
  224.                                     if (( ${fl_g_valid_line[$[$i-2]]} == 0 )); then
  225.                                         echo "$line_no : RESTRICT GROUP - group ${line[$i]} is not present in groups array"
  226.                                         ((c_warn++))
  227.                                     fi
  228.                                 done
  229.                             fi
  230.                         fi
  231.                         ;;
  232.                     * ) echo "$line_no : RESTRICT - missing target"
  233.                         c_error=1
  234.                         break
  235.                         ;;
  236.                     esac
  237.                 ;;
  238.             EXCLUDE )   if (( $fl_o == 0 )); then
  239.                         echo "$line_no : Missing RESTRICT before current line"
  240.                         c_error=1
  241.                         break
  242.                     else    if (( $fl_o == 2 )); then
  243.                             echo "$line_no : PERM is before EXCLUDE"
  244.                             c_error=1
  245.                             break
  246.                         fi
  247.                     fi          # fl_o = 1 is accepted
  248.                     case ${line[1]} in
  249.                     USER )  if (( $fl_e_u == 1 )); then
  250.                             echo "$line_no : You can only specify EXCLUDE USER once in each block"
  251.                             c_error=1
  252.                             break
  253.                         fi
  254.                         fl_e_u=1
  255.                         for (( i=2; i < ${#line[@]}; i++ )); do
  256.                             if [[ ${line[$i]} == "ALL" ]]; then
  257.                                 echo "$line_no : EXCLUDE USER ALL is not allowed"
  258.                                 c_error=1
  259.                                 break               # Break first loop
  260.                             fi
  261.                         done
  262.                         if (( $c_error == 1 )); then break; fi      # Break second loop
  263.                         if [[ ${#line[@]} > $[2+${#users[@]}] ]]; then
  264.                             echo "$line_no : EXCLUDE USER - too many users"
  265.                             c_error=1
  266.                             break
  267.                         fi
  268.                         # Since ALL is not present, check specified users
  269.                         for (( i=2; i < ${#line[@]}; i++ )); do     # Check if users are valid elements of users array
  270.                             for (( j=0; j < ${#users[@]}; j++ )); do
  271.                                 if [[ ${line[$i]} == ${users[$j]} ]] && !(( ${fl_u_checked[$j]} == 1 )); then
  272.                                     fl_u_valid_line[$[$i-2]]=1
  273.                                     fl_u_checked[$j]=1  # Set 1 to indicate user is checked
  274.                                     #echo "$line_no : ignored user : ${users[$j]}"
  275.                                 else    if [[ ${line[$i]} == ${users[$j]} ]] && (( ${fl_u_checked[$j]} == 1 )); then
  276.                                         echo "$line_no : user ${users[$j]} is repeated"
  277.                                         ((c_warn++))
  278.                                         fl_u_valid_line[$[$i-2]]=1
  279.                                         # Don't set fl_u_checked since it's repeated
  280.                                     fi
  281.                                 fi
  282.                             done
  283.                         done
  284.                         for (( i=2; i < ${#line[@]}; i++ )); do     # Report invalid users
  285.                             if (( ${fl_u_valid_line[$[$i-2]]} == 0 )); then
  286.                                 echo "$line_no : RESTRICT USER - user ${line[$i]} is not present in users array"
  287.                                 ((c_warn++))
  288.                             fi
  289.                         done
  290.                         ;;
  291.                     GROUP ) if (( $fl_e_g == 1 )); then
  292.                             echo "$line_no : You can only specify EXCLUDE GROUP once in each block"
  293.                             c_error=1
  294.                             break
  295.                         fi
  296.                         fl_e_g=1
  297.                         for (( i=2; i < ${#line[@]}; i++ )); do
  298.                             if [[ ${line[$i]} == "ALL" ]]; then
  299.                                 echo "$line_no : EXCLUDE USER ALL is not allowed"
  300.                                 c_error=1
  301.                                 break               # Break first loop
  302.                             fi
  303.                         done
  304.                         if (( $c_error == 1 )); then break; fi      # Break second loop
  305.                         if [[ ${#line[@]} > $[2+${#users[@]}] ]]; then
  306.                             echo "$line_no : EXCLUDE USER - too many users"
  307.                             c_error=1
  308.                             break
  309.                         fi
  310.                         # Since ALL is not present, check specified users
  311.                         for (( i=2; i < ${#line[@]}; i++ )); do     # Check if users are valid elements of users array
  312.                             for (( j=0; j < ${#users[@]}; j++ )); do
  313.                                 if [[ ${line[$i]} == ${users[$j]} ]] && !(( ${fl_u_checked[$j]} == 1 )); then
  314.                                     fl_u_valid_line[$[$i-2]]=1
  315.                                     fl_u_checked[$j]=1  # Set 1 to indicate user is checked
  316.                                     #echo "$line_no : ignored user : ${users[$j]}"
  317.                                 else    if [[ ${line[$i]} == ${users[$j]} ]] && (( ${fl_u_checked[$j]} == 1 )); then
  318.                                         echo "$line_no : user ${users[$j]} is repeated"
  319.                                         ((c_warn++))
  320.                                         fl_u_valid_line[$[$i-2]]=1
  321.                                         # Don't set fl_u_checked since it's repeated
  322.                                     fi
  323.                                 fi
  324.                             done
  325.                         done
  326.                         for (( i=2; i < ${#line[@]}; i++ )); do     # Report invalid users
  327.                             if (( ${fl_u_valid_line[$[$i-2]]} == 0 )); then
  328.                                 echo "$line_no : RESTRICT USER - user ${line[$i]} is not present in users array"
  329.                                 ((c_warn++))
  330.                             fi
  331.                         done
  332.                         ;;
  333.                     * ) echo "$line_no : EXCLUDE - missing target"; c_error=1; break
  334.                         ;;
  335.                     esac
  336.                     # No modification of fl_o, since EXCLUDE is optional
  337.                 ;;
  338.             PERM )      if (( $fl_o == 0 )); then
  339.                         echo "$line_no : Missing RESTRICT before current line"
  340.                         c_error=1
  341.                         break
  342.                     else    if (( $fl_o == 2 )); then
  343.                             echo "$line_no : PERM should not be repeated"
  344.                             c_error=1
  345.                             break
  346.                         fi
  347.                     fi          # fl_o = 1 is accepted
  348.                     fl_o=2
  349.                     if (( ${#line[@]} > 2 )); then
  350.                         echo "$line_no : PERM - too many parameters"
  351.                         c_error=1
  352.                         break
  353.                     else    if (( ${#line[@]} == 1 )); then
  354.                             echo "$line_no : PERM - missing permission"
  355.                             c_error=1
  356.                             break
  357.                         fi
  358.                     fi
  359.                     # Check if valid permissions are entered
  360.                     if [[ ${line[1]} =~ ^[0-9]+$ ]]; then   # Check if permission number is used
  361.                         if (( ${line[1]} > 7 || ${line[1]} < 0 )); then
  362.                             echo "$line_no : Invalid permission number"
  363.                             c_error=1
  364.                             break
  365.                         fi
  366.                     else    if (( ${#line[1]} != 3 )); then # Check length of string, which should be 3 characters long
  367.                             echo "$line_no : Invalid permission string"
  368.                             c_error=1
  369.                             break
  370.                         else    if [[ ${line[1]:0:1} == 'r' || ${line[1]:0:1} == '-' ]]; then   # Check if the string is valid
  371.                                 if [[ ${line[1]:1:1} == 'w' || ${line[1]:1:1} == '-' ]]; then
  372.                                     if ! [[ ${line[1]:2:1} == 'x' || ${line[1]:2:1} == '-' ]]; then
  373.                                         echo "$line_no : Invalid permission string"
  374.                                         c_error=1
  375.                                         break
  376.                                     fi
  377.                                 else
  378.                                     echo "$line_no : Invalid permission string"
  379.                                     c_error=1
  380.                                     break
  381.                                 fi
  382.                             else
  383.                                 echo "$line_no : Invalid permission string"
  384.                                 c_error=1
  385.                                 break
  386.                             fi
  387.                         fi
  388.                     fi
  389.                     fl_f_list=1
  390.                 ;;
  391.                 END )   if (( $fl_e == 0 )); then   # if previous operand was END, accept
  392.                         if (( $fl_o == 0 )); then
  393.                             echo "$line_no : Missing RESTRICT before current line"
  394.                             c_error=1
  395.                             break
  396.                         else    if [[ $fl_o == 1 ]]; then
  397.                                 echo "$line_no : Missing PERM before current line"
  398.                                 c_error=1
  399.                                 break
  400.                             fi
  401.                         fi          # fl_o = 2 is accepted
  402.                     fi
  403.                     fl_o=0
  404.                 ;;
  405.             * )     if (( $fl_o == 0 )); then  
  406.                         # After END, only NO/RESTRICT should start the new ACL block
  407.                         echo "$line_no : Missing operand"
  408.                         c_error=1
  409.                         break
  410.                     fi
  411.                 ;;
  412.             esac
  413.         else
  414.             case  ${line[0]} in
  415.             END )       if (( $fl_e == 0 )); then   # if previous operand was END, accept
  416.                         if (( $fl_o == 0 )); then
  417.                             echo "$line_no : Missing RESTRICT before current line"
  418.                             c_error=1
  419.                             break
  420.                         else    if [[ $fl_o == 1 ]]; then
  421.                                 echo "$line_no : Missing PERM before current line"
  422.                                 c_error=1
  423.                                 break
  424.                             fi
  425.                         fi          # fl_o = 2 is accepted
  426.                     fi
  427.                     fl_o=0
  428.                     fl_e=1
  429.                     # The following flags are used for the whole block
  430.                     fl_r_u=0
  431.                     fl_r_g=0
  432.                     fl_e_u=0
  433.                     fl_e_g=0
  434.                     fl_no=0
  435.                     fl_f_list=0
  436.                 ;;
  437.             * )     if (( $fl_o == 0 )); then      
  438.                     # After END, only NO/RESTRICT should start the new ACL block
  439.                         echo "$line_no : Missing operand"
  440.                         c_error=1
  441.                         break
  442.                     else    if (( $fl_o == 2 )) && (( ${#line[@]} == 2)); then
  443.                             # After PERM, check if addtional permission numer/string exist
  444.                             #Check if valid permissions are entered
  445.                             if [[ ${line[1]} =~ ^[0-9]+$ ]]; then   # Check if permission number is used
  446.                                 if (( ${line[1]} > 7 || ${line[1]} < 0 )); then
  447.                                     echo "$line_no : Invalid permission number"
  448.                                     c_error=1
  449.                                     break
  450.                                 fi
  451.                             else    if (( ${#line[1]} != 3 )); then # Check length of string, which should be 3 characters long
  452.                                     echo "$line_no : Invalid permission string"
  453.                                     c_error=1
  454.                                     break
  455.                                 else    if [[ ${line[1]:0:1} == 'r' || ${line[1]:0:1} == '-' ]]; then
  456.                                         if [[ ${line[1]:1:1} == 'w' || ${line[1]:1:1} == '-' ]]; then
  457.                                             if ! [[ ${line[1]:2:1} == 'x' || ${line[1]:2:1} == '-' ]]; then
  458.                                                 echo "$line_no : Invalid permission string"
  459.                                                 c_error=1
  460.                                                 break
  461.                                             fi
  462.                                         else
  463.                                             echo "$line_no : Invalid permission string"
  464.                                             c_error=1
  465.                                             break
  466.                                         fi
  467.                                     else
  468.                                         echo "$line_no : Invalid permission string"
  469.                                         c_error=1
  470.                                         break
  471.                                     fi
  472.                                 fi
  473.                             fi
  474.                         else    if (( $fl_o == 2 ))&& (( ${#line[@]} > 2)); then    # too many arguments for permission string/number
  475.                                 echo "$line_no : Invalid permission string/number"
  476.                             fi
  477.                         fi
  478.                     fi
  479.                 ;;
  480.             esac
  481.         fi
  482.     fi
  483.     ((line_no++))       # Line number tracking
  484.     unset line      # to reset line array
  485. done < $config_file
  486. if (( $fl_o != 0 )) && (( $c_error != 1 )); then
  487.     echo "Missing END before end of file"
  488.     c_error=1
  489. fi
  490. echo -n "$c_warn Warning"
  491. if [[ $c_warn > 1 ]]; then echo 's'; else echo ""; fi
  492. if [[ $c_error != 1 ]]; then
  493.     echo -e "No config error detected\n"
  494. else
  495.     exit
  496. fi
  497.  
  498. # Uncomment the exit below to only check config
  499. #exit
  500.  
  501. # Code for setting permissions
  502. # Note : The below code assumes the config file is perfectly correct
  503. #    The error detection is only handled in above checking code
  504. #    Hence, no debug message from script will be echoed below
  505.  
  506. while read line_raw; do
  507.     if [[ ! ($line_raw =~ ^$  || -z $line_raw || $line_raw =~ ^# ) ]]; then     # Check if line is empty/null/spaces or starts with #
  508.         a=0
  509.         fl_all=0
  510.         fl_others=0
  511.         line=()
  512.         for word in $line_raw; do
  513.             if [[ $word == \#* || $word == '#' ]]; then     # If word starts with #, finish reading (probably is a comment)
  514.                 break
  515.             fi
  516.             #echo $word                 # Uncomment echo $word to see what words this script will actually process
  517.             line[$a]=${word}
  518.             ((a++))
  519.         done
  520.         if (( fl_f_list == 0 )); then
  521.             case  ${line[0]} in
  522.             NO )        fl_no=1     # action by default is 'm', if NO is specified, action changes to 'x'
  523.                 ;;
  524.             RESTRICT )  if (( $fl_e == 1 )); then
  525.                         for (( i=0; i < ${#users[@]}; i++ )); do
  526.                             fl_u[$i]=0
  527.                         done
  528.                         for (( i=0; i < ${#groups[@]}; i++ )); do
  529.                             fl_g[$i]=0
  530.                         done
  531.                         unset d_perm
  532.                     fi
  533.                     fl_e=0
  534.                     case ${line[1]} in
  535.                     USER )  if [[ ${line[2]} == "ALL" ]]; then      # If ALL is specified, mark all users
  536.                             for (( j=0; j < ${#users[@]}; j++ )); do
  537.                                 fl_u[$j]=1  # Set 1 to mark user will be selected
  538.                             done
  539.                         else                        # If not, then mark individual users
  540.                             for (( i=2; i < ${#line[@]}; i++ )); do
  541.                                 for (( j=0; j < ${#users[@]}; j++ )); do
  542.                                     if [[ ${line[$i]} == ${users[$j]} ]] && !(( ${fl_u[$j]} == 1 )); then
  543.                                         fl_u[$j]=1  # Set 1 to mark user will be selected
  544.                                     fi
  545.                                 done
  546.                             done
  547.                         fi
  548.                         ;;
  549.                     GROUP ) if [[ ${line[2]} == "ALL" ]]; then      # If ALL is specified, mark all groups
  550.                             for (( j=0; j < ${#groups[@]}; j++ )); do
  551.                                 fl_g[$j]=1  # Set 1 to mark user will be selected
  552.                             done
  553.                         else                        # If not, then mark individual groups
  554.                             for (( i=2; i < ${#line[@]}; i++ )); do
  555.                                 for (( j=0; j < ${#groups[@]}; j++ )); do
  556.                                     if [[ ${line[$i]} == ${groups[$j]} ]] && !(( ${fl_g[$j]} == 1 )); then
  557.                                         fl_g[$j]=1  # Set 1 to mark user will be selected
  558.                                     fi
  559.                                 done
  560.                             done
  561.                         fi
  562.                         ;;
  563.                     * ) # I don't expect anything to be here, as this should be blocked during config check
  564.                         ;;
  565.                     esac
  566.                 ;;
  567.             EXCLUDE )   case ${line[1]} in
  568.                     USER )  for (( i=2; i < ${#line[@]}; i++ )); do     # Mark individual users
  569.                             for (( j=0; j < ${#users[@]}; j++ )); do
  570.                                 if [[ ${line[$i]} == ${users[$j]} ]] && !(( ${fl_u[$j]} == 0 )); then
  571.                                     fl_u[$j]=0  # Set 0 to mark user will not be selected
  572.                                 fi
  573.                             done
  574.                         done
  575.                         ;;
  576.                     GROUP ) for (( i=2; i < ${#line[@]}; i++ )); do     # Mark individual groups
  577.                             for (( j=0; j < ${#groups[@]}; j++ )); do
  578.                                 if [[ ${line[$i]} == ${groups[$j]} ]] && !(( ${fl_g[$j]} == 0 )); then
  579.                                     fl_g[$j]=0  # Set 0 to mark group will not be selected
  580.                                 fi
  581.                             done
  582.                         done
  583.                         ;;
  584.                     * ) # I don't expect anything to be here, as this should be blocked during config check
  585.                         ;;
  586.                     esac
  587.                 ;;
  588.             PERM )      d_perm=${line[1]}   # Default permission of current ACL block, can be number or string
  589.                     fl_f_list=1
  590.                 ;;
  591.             END )       # Process file list, line by line
  592.                     if (( ${#line[@]} == 1 )); then     # No individual permission specified, follow default permission
  593.                         a_perm=$d_perm          # Actual permission = default permission
  594.                     else
  595.                         # else implies ${#line[@]} == 2, but since checking was done already,no extra if statements
  596.                         a_perm=${line[1]}       # Actual permission = individual option
  597.                     fi
  598.                     for (( i=0; i < ${#users[@]}; i++ )); do    # Check if user is marked to be processed
  599.                         if (( ${fl_u[$i]} == 1 )); then
  600.                             if (( $fl_no_b == 0 )); then
  601.                                 # Remove echo "" from below to actually execute setfacl
  602.                                 echo "setfacl -m u:${users[$i]}:$a_perm ${line[0]}"
  603.                             else
  604.                                 # Remove echo "" from below to actually execute setfacl
  605.                                 echo "setfacl -x u:${users[$i]} ${line[0]}"
  606.                             fi
  607.                         fi
  608.                     done
  609.                     for (( i=0; i < ${#groups[@]}; i++ )); do   # Check if group is marked to be processed
  610.                         if (( ${fl_g[$i]} == 1 )); then
  611.                             if (( $fl_no_b == 0 )); then
  612.                                 # Remove echo "" from below to actually execute setfacl
  613.                                 echo "setfacl -m g:${groups[$i]}:$a_perm ${line[0]}"
  614.                             else
  615.                                 # Remove echo "" from below to actually execute setfacl
  616.                                 echo "setfacl -x g:${groups[$i]} ${line[0]}"
  617.                             fi
  618.                         fi
  619.                     done
  620.                 ;;
  621.             esac
  622.         else
  623.             case  ${line[0]} in
  624.             END )       # Reset flags
  625.                     # Since for setting permissions, the script handles ACl block by block,
  626.                     # some flags are reseted after each block, rather than each line
  627.                     fl_no_b=$fl_no
  628.                     fl_no=0
  629.                     fl_f_list=0
  630.                     fl_e=1
  631.                 ;;
  632.             * )     # Process file list, line by line
  633.                     if (( ${#line[@]} == 1 )); then     # No individual permission specified, follow default permission
  634.                         a_perm=$d_perm          # Actual permission = default permission
  635.                     else
  636.                         # else implies ${#line[@]} == 2, but since checking was done already,no extra if statements
  637.                         a_perm=${line[1]}       # Actual permission = individual option
  638.                     fi
  639.                     for (( i=0; i < ${#users[@]}; i++ )); do    # Check if user is marked to be processed
  640.                         if (( ${fl_u[$i]} == 1 )); then
  641.                             if (( $fl_no == 0 )); then
  642.                                 # Remove echo "" from below to actually execute setfacl
  643.                                 echo "setfacl -m u:${users[$i]}:$a_perm ${line[0]}"
  644.                             else
  645.                                 # Remove echo "" from below to actually execute setfacl
  646.                                 echo "setfacl -x u:${users[$i]} ${line[0]}"
  647.                             fi
  648.                         fi
  649.                     done
  650.                     for (( i=0; i < ${#groups[@]}; i++ )); do   # Check if group is marked to be processed
  651.                         if (( ${fl_g[$i]} == 1 )); then
  652.                             if (( $fl_no == 0 )); then
  653.                                 # Remove echo "" from below to actually execute setfacl
  654.                                 echo "setfacl -m g:${groups[$i]}:$a_perm ${line[0]}"
  655.                             else
  656.                                 # Remove echo "" from below to actually execute setfacl
  657.                                 echo "setfacl -x g:${groups[$i]} ${line[0]}"
  658.                             fi
  659.                         fi
  660.                     done
  661.                 ;;
  662.             esac
  663.         fi
  664.     fi
  665.     unset line      # to reset line array
  666. done < $config_file
Advertisement
Add Comment
Please, Sign In to add comment