Any way to prevent sudo chown -R 775 please?

Mandrake Kings & Queens,

Just by curiosity is there a way to prevent wrong terminal command ?
Security feature ? One or two times a year stuff like that happened to me :triumph:

Was trying to solve Wordpress upgrade error on local. I meant chmod of course but it’s not the first time I made this kind of mistake. I can survive for sure but if there is something similar to “sl” or a way to get a warning ?

Thank you in advance for your help,

Peter

Please be careful with sudo and root permissions. For example, the sudo checkinstall command in Ubuntu after Ctrl+C when trying to install Qucs destroyed the /lib symlink (it was on /usr/lib) and the system immediately stopped working. I needed a fix from another Linux.

Well you can reassign /usr/bin/chown in the local PATH (/usr/local/bin, ~/.local/bin, ~/bin) or in ~/.bash_aliases to a secure command or stub, and you can call chown directly or with the chown-unsecure command).

Although, sudo has its own securepath in /etc/sudoers.

Thank you for the tip @artenaki :rocket:

Sunday Shell Scripting! Well I ended creating my own script in /usr/bin/chowner.
It took me a few hours :sweat_drops:… Bidouilleur du dimanche bonjour !

#!/bin/bash -e
###
# /usr/bin/chowner
#
# Copyright 2024 Pierre-Henri Lavigne
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
###

if [[ -z "$1" ]]; then
	exit 1;
fi;

# Used Variables
arguments=$1
owners=''
files=''

# Colors found on Stackoverflow
RED='\033[0;31m'
GREEN='\033[0;32m'
DARKGREY='\033[1;30m'
BLUE='\033[0;34m'
CYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color

# Check first for -minus based args
if [[ "${arguments,,}" =~ ^-[a-z]+$ ]]; then
	if [[ -z "$2" ]]; then
		printf "${RED}Error: Missing owners argument${NC}\n"
		exit 1
	fi
	if [[ -z "$3" ]]; then
		printf "${RED}Error: Missing owners & target files arguments ?${NC}\n"
		exit 1
	fi
	owners=$2
	files=$3
else
	arguments=''
	owners=$1
	if [[ -z "$2" ]]; then
		printf "${RED}Error: Missing owners & target files arguments ?${NC}\n"
		exit 1
	fi;
	files=$2
fi

# Check the ownership variable
if [[ $owners =~ ^[a-z]+:[a-z]+$ ]]; then
	printf "${GREEN}Pass 1 succeeded: target ownership ${owners} *looks* good.${NC}\n"
else
	printf "${RED}Pass 1 failed: the ownership ${owners} is not a valid argument!${NC}\n"
	exit 1
fi

# Check if user exists
user=`echo $owners | sed -e "s/:.*//g"`
if [[ -z "$user" || ! "${#user}" ]]; then
	printf "${RED}Pass 2 failed: invalid user name!${NC}\n"
	exit 1
fi
if ! grep -q "$user" /etc/passwd; then
	printf "${RED}Pass 2 failed: the user ${user} doesn't exist on this machine!${NC}\n"
	exit 1
fi
sc=`grep ${user} /etc/passwd`
re="^(${user}:)x:[0-9]+:[0-9]+:[^:]+:(/[a-z]+/${user}):.*?$"
if [[ $sc =~ $re ]]; then
	printf "${GREEN}Pass 2 succeeded: the user ${user} has been found on this machine.${NC}\n"
else
	printf "${RED}Pass 2 failed: the user ${user} doesn't seem to be a real person. Typo?${NC}\n"
	exit 1
fi

# Check if group exists
group=`echo $owners | sed -e "s/.*://g"`
if [[ -z "$group" || ! "${#group}" ]]; then
	printf "${RED}Pass 3 failed: invalid group name!${NC}\n"
	exit 1
fi
if ! grep -q "$group" /etc/group; then
	printf "${RED}Pass 3 failed: the group ${group} doesn't exist on this machine!${NC}\n"
	exit 1
fi
sc=`grep ${group} /etc/group`
re="^${group}:x:[0-9]+"
if [[ $sc =~ $re ]]; then
	printf "${GREEN}Pass 3 succeeded: the group ${group} has been found on this machine.${NC}\n"
else
	printf "${RED}Pass 3 failed: the group ${group} doesn't seem to valid. Typo?${NC}\n"
	exit 1
fi

# Check the path variable
if [[ $files =~ '/' ]]; then
	# echo "Target does look like a file path or directory"
	printf "${GREEN}Pass 4 succeeded: the target files $files *looks* good.${NC}\n"
else
	printf "${RED}Pass 4 failed: the target files $files are probably invalid.${NC}\n"
	exit 1
fi

curr=`stat -c "%U %G" $files`
printf "Current ownership of $files are ${CYAN}$curr${NC}\n"
printf "${DARKGREY}Trying to update ownership...${NC}\n"
chown $arguments $owners $files
curr=`stat -c "%U %G" $files`
printf "New ownership of $files are ${BLUE}$curr${NC}\n"

I just use it like that :

sudo chowner -R pierre:www /my_plugins/

Example :

It’s working pretty good actually

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.