-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_permissions_check.sh
More file actions
167 lines (126 loc) · 5 KB
/
file_permissions_check.sh
File metadata and controls
167 lines (126 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
<<com
NOTE: Please do not run this. This was how I came to figure out what I was trying to do for work
with my script. With the help of ChatGPT I accomplished my goal and learned a 'trick' of how to extract the digits
stored within a viariable without using the cut command.
com
# ===> ChatGPT fix <=== #
# for loop to iterate over all files in the specified directories
for i in $(find -L /lib /lib64 /usr/lib /usr/lib64 -type f -name "*.so*" -print)
do
while true
do
# variables
PERMISSIONS=$(stat -c %a "$i"); # get the permissions of the file
FIRST=${PERMISSIONS:0:1}; # extract the first digit of the permissions
SECOND=${PERMISSIONS:1:1}; # extract the second digit of the permissions
THIRD=${PERMISSIONS:2:1}; # extract the third digit of the permissions
# display a message
echo -e "\nPermissions for $i are currently set to $PERMISSIONS\n";
# if statement to check if the second digit is either 2, 3, 6, or 7 (check group permissions)
#if (( SECOND >= 2 && SECOND <= 7 )) ## ChatGPT Error
if (( ($SECOND == 2) || ($SECOND == 3) || ($SECOND == 6) || ($SECOND == 7) ))
then
# fix group permission on file
chmod "${FIRST}4${THIRD}" "$i";
# elif statement to check if the third digit is either 2, 3, 6, or 7 (check other permissions)
#elif (( THIRD >= 2 && THIRD <= 7 )) ## ChatGPT Error
elif (( ($THIRD == 2) || ($THIRD == 3) || ($THIRD == 6) || ($THIRD == 7) ))
then
# fix other permission on file
chmod "${FIRST}${SECOND}4" "$i";
else
# display message
echo -e "\nPermissions for $i seem to be fine\n";
# break out of while loop
break;
fi
done
done
```
Changes made:
1. Used substring extraction (`${PERMISSIONS:0:1}`) to get individual digits of the permissions.
2. Changed the conditions to `(SECOND >= 2 && SECOND <= 7)` and `(THIRD >= 2 && THIRD <= 7)` to check if the permissions contain w, x, or wx.
3. Moved the `echo` statement to print the current permissions before attempting to fix them.
This script should iterate through the specified files, continuously checking and adjusting the permissions until they meet your criteria.
for more details see: https://chat.openai.com/c/7cfda3f3-836b-477f-ab37-15fcddd72a6e
```
# ==> END <== #
<<COMMENT
# one liner to set permissions #
find -L /lib /lib64 /usr/lib /usr/lib64 -type f -name "*.so*" -exec bash -c '\
while true
do
# variables
PERM=$(stat -c %a "$1");
FIRST=$(stat -c %a "$1" | cut -c 1);
SECOND=$(echo $PERM | cut -c 2);
THIRD=${PERM:2:1};
echo -e "\nPermissions for $1 is currently set to $PERM\n";
if (( (SECOND == 2) || (SECOND == 3) || (SECOND == 6) || (SECOND == 7) ))
then
echo -e "\nFile $1 has inadequate GROUP permissions\n";
echo -e "\nWill fix this now";
chmod "${FIRST}4${THIRD}" "$1";
elif (( (THIRD == 2) || (THIRD == 3) || (THIRD == 6) || (THIRD == 7) ))
then
echo -e "\nFile $1 has inadequate OTHER permissions\n";
echo -e "\nWill fix this now";
chmod "${FIRST}${SECOND}4" "$1";
else
echo -e "\nFile $1 has passed the permissions check\n";
break;
fi
done
' _ {} \;
# show perms
for i in $(find /lib /lib64 /usr/lib /usr/lib64 -type f -name "*.so*" -print)
do
echo -e "\nThe permissions for $i is now $(stat -c %a $i)\n";
done
### --> END one-liner <-- ###
## --> 1st attempt <-- ##
for i in $(find -L /lib /lib64 /usr/lib /usr/lib64 -perm /022 -type f -exec ls {} \;)
do
FIRST=$(stat -c %a "$i" | cut -c 1);
SECOND=$(stat -c %a "$i" | cut -c 2);
THIRD=$(stat -c %a "$i" | cut -c 3);
echo -e "\nThe current permission of the file $i is $(stat -c %a $i)\n";
if (( $SECOND == [2367] ))
then
echo -e "\nFile $i has inadequate group permissions\n";
echo -e "\nWill fix this now\n";
chmod ${FIRST}4${THIRD} ${i};
elif (( $THIRD == [2367] ))
then
echo -e "\nFile $i has inadequate other permissions\n";
echo -e "\nWill fix this now\n";
chmod ${FIRST}${SECOND}4 ${i};
else
echo -e "\nPermissions on $i seem to be fine\n";
fi
done
## --> END <-- ##
### ----> Edited Version <----- ### (still provides undesired result)
for i in $(find -L /lib /lib64 /usr/lib /usr/lib64 -type f -name "*.so*" -print)
do
# variables
FIRST=$(stat -c %a "$i" | cut -c 1);
SECOND=$(stat -c %a "$i" | cut -c 2);
THIRD=$(stat -c %a "$i" | cut -c 3);
if (( SECOND == 2 || SECOND == 3 || SECOND == 6 || SECOND == 7 ))
then
echo -e "\nFile $i has inadequate GROUP permissions\n";
echo -e "\nWill fix this now\n";
chmod "${FIRST}4${THIRD}" "$i";
elif (( THIRD == 2 || THIRD == 3 || THIRD == 6 || THIRD == 7 ))
then
echo -e "\nFile $i has inadequate OTHER permissions\n";
echo -e "\nWill fix this now\n";
chmod "${FIRST}${SECOND}4"
else
echo -e "\nPermissions on $i seem to be fine\n";
fi
done
## ===> END <=== ##
COMMENT