#!/bin/sh
# $Revision: 336 $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# (c) Tim Brown, 2012
# <mailto:timb@nth-dimension.org.uk>
# <http://www.nth-dimension.org.uk/> / <http://www.machine.org.uk/>
#
# Check for world-readable and world-writable permissions on devices files

. lib/misc/device
. lib/misc/file
. lib/misc/stdio

devices_permission_init () {
	stdio_message_log "devices_permission" "Starting at: `date`"
}

devices_permission_permissions () {
	device="${1}"
	mountpoint="`device_get_mountpoint \"${device}\"`"
	if [ -n "${mountpoint}" ]
	then
		message="mounted to ${mountpoint}"
	elif [ "`device_is_swap \"${device}\"`" -eq 1 ]
	then
		message="swap"
	else
		message="not mounted"
	fi
	file_show_non_symlink_perms " ${device}$" | while read filename permissions userid groupid
	do
		case "${permissions}" in
			???????rw?)
				stdio_message_warn "devices_permission" "device file ${filename} (${message}) is owned by user ${userid} (group ${groupid}) and is world-readable and world-writable (${permissions})"
				;;
			????????w?)
				stdio_message_warn "devices_permission" "device file ${filename} (${message}) is owned by user ${userid} (group ${groupid}) and is world-writable (${permissions})"
				;;
			???????r??)
				stdio_message_warn "devices_permission" "device file ${filename} (${message}) is owned by user ${userid} (group ${groupid}) and is world-readable (${permissions})"
				;;
		esac
	done
}

devices_permission_main () {
	device_list | while read device
	do
		if [ -h "${device}" ]
		then
			linkeddevice="`file_show_symlinked_filename \"${device}\"`"
			if [ -z "${linkeddevice}" ]
			then
				continue
			fi
			#stdio_message_debug "devices_permission" "device file ${device} is a symbolic link to ${linkeddevice}"
			devices_permission_permissions "${linkeddevice}"
		else
			devices_permission_permissions "${device}"
		fi
	done
}

devices_permission_fini () {
	stdio_message_log "devices_permission" "Ending at: `date`"
}
