@@ -187,23 +187,23 @@ find_create_includedir() {
187
187
fi
188
188
else
189
189
# We need to add @includedir or #includedir to main sudoers file
190
+
190
191
# --> Workflow <--
191
- # Create sudoers backup/tmp files
192
- # Modify sudoers tmp file according to sudo version
193
- # Create includedir path before test tmp via visudo
194
- # Test tmp before replacement with original
195
- # Replace original with tmp
196
- # Test original before remove backup, if we get error return from backup
197
- # Clean up tmp/ backup
198
- # Assign custom_includedir to includedir
199
-
200
- # Create sudoers backup/tmp files
192
+ # 1. Create sudoers backup/tmp files
193
+ # 2. Modify sudoers tmp file according to sudo version
194
+ # 3. Create includedir path before testing tmp file via visudo
195
+ # 4. Test tmp file before replacing the original sudoers file
196
+ # 5. Replace the original sudoers file with the tmp file
197
+ # 6. Test the updated sudoers file and restore from backup if there is an error
198
+ # 7. Clean up tmp and backup files
199
+ # 8. Assign custom_includedir to includedir
200
+
201
+ # 1. Create sudoers backup/tmp files
201
202
cp " ${SUDOERS_FILE} " " ${TEMP_FILE} " || { echo -e " \e[91mFailed to create sudoers tmp file\e[0m" ; return 1; }
202
203
cp " ${SUDOERS_FILE} " " ${BACKUP_FILE} " || { echo -e " \e[91mFailed to create sudoers backup file\e[0m" ; return 1; }
203
204
204
- # Modify sudoers tmp file
205
- # Get the version of sudo that we need to find
206
- # accepted includedir syntax @ or # according to sudo version.
205
+ # 2. Modify sudoers tmp file according to sudo version
206
+ # Get the version of sudo to determine the correct includedir syntax (@ or #)
207
207
SUDO_VERSION=" $( sudo -V | grep ' Sudo version' | awk ' {print $3}' ) "
208
208
VERSION_MAJOR=" $( echo " $SUDO_VERSION " | cut -d. -f1) "
209
209
VERSION_MINOR=" $( echo " $SUDO_VERSION " | cut -d. -f2) "
@@ -221,26 +221,26 @@ find_create_includedir() {
221
221
echo " #includedir ${CUSTOM_INCLUDEDIR_PATH} " | sudo EDITOR=' tee -a' visudo -f " ${TEMP_FILE} " > /dev/null 2>&1 || { echo -e " \e[91mFailed to add includedir to sudoers file\e[0m" ; return 1; }
222
222
fi
223
223
224
- # Create includedir path before test tmp via visudo
224
+ # 3. Create includedir path before testing tmp file via visudo
225
225
mkdir -p " ${CUSTOM_INCLUDEDIR_PATH} " || { echo -e " \e[91mFailed to create /etc/sudoers.npp\e[0m" ; return 1; }
226
226
227
- # Test tmp before replacement with original
227
+ # 4. Test tmp file before replacing the original sudoers file
228
228
if visudo -c -f " ${TEMP_FILE} " > /dev/null 2>&1 ; then
229
- # Replace original with tmp
229
+ # 5. Replace the original sudoers file with the tmp file
230
230
cp " ${TEMP_FILE} " " ${SUDOERS_FILE} " || { echo -e " \e[91mFailed to update sudoers file\e[0m" ; return 1; }
231
231
fi
232
232
233
- # Test original before remove backup, if we get error return from backup
233
+ # 6. Test the updated sudoers file and restore from backup if there is an error
234
234
if ! visudo -c -f " ${SUDOERS_FILE} " > /dev/null 2>&1 ; then
235
235
cp " ${BACKUP_FILE} " " ${SUDOERS_FILE} " || { echo -e " \e[91mFailed to return from sudoers backup file\e[0m" ; return 1; }
236
236
return 1
237
237
fi
238
238
239
- # Clean up tmp/ backup
239
+ # 7. Clean up tmp and backup files
240
240
rm -f " ${TEMP_FILE:? } "
241
241
rm -f " ${BACKUP_FILE:? } "
242
242
243
- # Assign custom_includedir to includedir
243
+ # 8. Assign custom_includedir_path to includedir_path
244
244
includedir_path=" ${CUSTOM_INCLUDEDIR_PATH} "
245
245
fi
246
246
else
0 commit comments