From patchwork Tue Aug 1 15:48:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 7049 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4RFffp18WFz3wmm for ; Tue, 1 Aug 2023 15:49:06 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4RFffk39gCzbH; Tue, 1 Aug 2023 15:49:02 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4RFffk2hYTz2yyR; Tue, 1 Aug 2023 15:49:02 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4RFffj0KvHz2x9l for ; Tue, 1 Aug 2023 15:49:01 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4RFffh4rzRzRS; Tue, 1 Aug 2023 15:49:00 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1690904940; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2dY8hWfEImuPKvW4wv1EpgHaQos9WB9iG9aJCfNMbxk=; b=jCMcdE+NvljaeuLJIVTWkOCvM4S9FY2xAvpNw6590sQErV2otpbYiEMug8gnW4R+D+w8bI RjwD4ypzkTjAkTDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1690904940; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2dY8hWfEImuPKvW4wv1EpgHaQos9WB9iG9aJCfNMbxk=; b=GjwOx4Js5txh0Fsay//p8ecLBLYMLckjVuXSZ/Bf+fxO1mNQoFrDmqj4+UyETkyORc8Zc6 Ex9PCUK+zjboT+iEZIQdG3QRT5wsuaK+xkAuhw0ChHuxV1zqLo3aKgeBeTphwVCQnG4PXe 2L0OfFHPFhUZQn9/q89lahWfWKREtGP2THluuc5GekleBLbx7za3TWJ55BCT35q3mobjRv XusssM+R/RJyuSYRKEtRzFx2D+bF6ccW8xm0eGCT+d6MmVfRdiqW/dUe+t8+fI84+spLhv btALCKIwJv15Jk47HbSkpGyPcv1Vyoo+QM0kFftBU2M1u9TwHs9CzU4Rocq11g== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCHv2 02/12] extrahd.cgi: Refactor code to use new introduced perl functions. Date: Tue, 1 Aug 2023 17:48:29 +0200 Message-Id: <20230801154839.2373-2-stefan.schantl@ipfire.org> In-Reply-To: <20230801154839.2373-1-stefan.schantl@ipfire.org> References: <20230801154839.2373-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This also gets rid of allmost all unsafe system calls. Signed-off-by: Stefan Schantl Reviewed-by: Bernhard Bitsch --- html/cgi-bin/extrahd.cgi | 336 ++++++++++++++++++++++++--------------- 1 file changed, 208 insertions(+), 128 deletions(-) diff --git a/html/cgi-bin/extrahd.cgi b/html/cgi-bin/extrahd.cgi index 5aadf15e8..bafdb9fc9 100644 --- a/html/cgi-bin/extrahd.cgi +++ b/html/cgi-bin/extrahd.cgi @@ -29,33 +29,51 @@ require "${General::swroot}/lang.pl"; require "${General::swroot}/header.pl"; my %extrahdsettings = (); -my $message = ""; my $errormessage = ""; -my $size = ""; -my $ok = "true"; -my @tmp = (); -my @tmpline = (); -my $tmpentry = ""; -my @devices = (); -my @deviceline = (); -my $deviceentry = ""; -my @scans = (); -my @scanline = (); -my $scanentry = ""; -my @partitions = (); -my @partitionline = (); -my $partitionentry = ""; + +# SYSFS directory which contains all block device data. +my $sysfs_block_dir = "/sys/class/block"; + +# Array which contains the valid mount directories. +# Only mounting to subdirectories inside them is allowed. +my @valid_mount_dirs = ( + "/data", + "/media", + "/mnt", +); + +# Array which contains the supported file systems. +my @supported_filesystems = ( + "auto", + "ext3", + "ext4", + "xfs", + "vfat", + "ntfs-3g" +); + +# Grab all available block devices. +my @devices = &get_block_devices(); + +# Grab all known UUID's. +my %uuids = &get_uuids(); + +# Grab all mountpoints. +my %mountpoints = &get_mountpoints(); + +# Omit the file system types of the mounted devices. +my %filesystems = &get_mountedfs(); + +# Gather all used swap devices. +my @swaps = &get_swaps(); + +# The config file which contains the configured devices. my $devicefile = "/var/ipfire/extrahd/devices"; -my $scanfile = "/var/ipfire/extrahd/scan"; -my $partitionsfile = "/var/ipfire/extrahd/partitions"; #workaround to suppress a warning when a variable is used only once my @dummy = ( ${Header::colourgreen}, ${Header::colourred} ); undef (@dummy); -&General::system("/usr/local/bin/extrahdctrl", "scanhd", "ide"); -&General::system("/usr/local/bin/extrahdctrl", "scanhd", "partitions"); - &Header::showhttpheaders(); ### Values that have to be initialized @@ -65,7 +83,6 @@ $extrahdsettings{'DEVICE'} = ''; $extrahdsettings{'ACTION'} = ''; $extrahdsettings{'UUID'} = ''; -&General::readhash("${General::swroot}/extrahd/settings", \%extrahdsettings); &Header::getcgihash(\%extrahdsettings); &Header::openpage('ExtraHD', 1, ''); @@ -74,52 +91,88 @@ $extrahdsettings{'UUID'} = ''; ############################################################################################################################ ############################################################################################################################ -if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) -{ +# +## Add a new device. +# +if ($extrahdsettings{'ACTION'} eq $Lang::tr{'add'}) { + # Open device file for reading. open( FILE, "< $devicefile" ) or die "Unable to read $devicefile"; - @devices = ; + my @devices = ; close FILE; - foreach $deviceentry (sort @devices) - { - @deviceline = split( /\;/, $deviceentry ); - if ( "$extrahdsettings{'PATH'}" eq "$deviceline[2]" ) { - $ok = "false"; + + # Loop through the entries line-by-line. + foreach my $entry (sort @devices) { + # Split the line into pieces and assign nice variables. + my ($uuid, $fs, $path) = split( /\;/, $entry ); + + # Check if the path is allready used. + if ( "$extrahdsettings{'PATH'}" eq "$path" ) { $errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}."; } - if ( "$extrahdsettings{'PATH'}" eq "/" ) { - $ok = "false"; - $errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to root'}."; + + # Check if the uuid is allready used. + if ("$extrahdsettings{'DEVICE'} eq $uuid") { + $errormessage = "$extrahdsettings{'DEVICE'} is allready mounted."; } } - if ( "$ok" eq "true" ) { + # Check if a valid mount path has been choosen. + unless(&is_valid_dir("$extrahdsettings{'PATH'}")) { + $errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to root'}."; + } + + # Check if the given path allready is mounted somewhere. + if(&is_mounted("$extrahdsettings{'PATH'}")) { + $errormessage = "$Lang::tr{'extrahd you cant mount'} $extrahdsettings{'DEVICE'} $Lang::tr{'extrahd to'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd because there is already a device mounted'}."; + } + + # Check if there was an error message. + unless($errormessage) { + # Re-open the device file for writing. open(FILE, ">> $devicefile" ) or die "Unable to write $devicefile"; - print FILE <; - close FILE; - open( FILE, "> $devicefile" ) or die "Unable to write $devicefile"; - foreach $deviceentry (sort @tmp) - { - @tmpline = split( /\;/, $deviceentry ); - if ( $tmpline[2] ne $extrahdsettings{'PATH'} ) - { - print FILE $deviceentry; - } + +# +# Remove an existing one. +# +} elsif ($extrahdsettings{'ACTION'} eq $Lang::tr{'delete'}) { + # Call helper binary to unmount the device. + &General::system("/usr/local/bin/extrahdctrl", "umount", "$extrahdsettings{'PATH'}"); + + # Open the device file for reading. + open(FILE, "< $devicefile" ) or die "Unable to read $devicefile"; + + # Read the file content into a temporary array. + my @tmp = ; + + # Close file handle. + close(FILE); + + # Re-open device file for writing. + open(FILE, "> $devicefile" ) or die "Unable to write $devicefile"; + + # Loop through the previous read file content. + foreach my $line (sort @tmp) { + # Split line content and assign nice variables. + my ($uuid, $fs, $path) = split( /\;/, $line ); + + # Write the line in case it does not contain our element to delete. + if ($path ne $extrahdsettings{'PATH'}) { + print FILE "$line"; } - close FILE; - } else { - $errormessage = "$Lang::tr{'extrahd cant umount'} $extrahdsettings{'PATH'}$Lang::tr{'extrahd maybe the device is in use'}?"; } + + # Close file handle. + close(FILE); } if ($errormessage) { @@ -132,34 +185,38 @@ if ($errormessage) { ############################################################################################################################ ############################################################################################################################ - open( FILE, "< $devicefile" ) or die "Unable to read $devicefile"; - @devices = ; - close FILE; print < END ; - foreach $deviceentry (sort @devices) - { - @deviceline = split( /\;/, $deviceentry ); - my $color="$Header::colourred"; + # Re-read mountpoints. + %mountpoints = &get_mountpoints(); - # Use safe system_output to get mountpoint details. - my @mountpoint = &General::system_output("/bin/mountpoint", "$deviceline[2]"); + # Read-in the device config file. + open( FILE, "< $devicefile" ) or die "Unable to read $devicefile"; + my @configfile = ; + close FILE; + + # Loop through the file content. + foreach my $entry (sort @configfile) { + my ($uuid, $fs, $path) = split( /\;/, $entry ); + my $color="$Header::colourred"; - if ( ! grep(/not/, @mountpoint)) { + # Check if the device is currently mounted. + if (&is_mounted($path)) { $color=$Header::colourgreen; } + print <  - $deviceline[0] - $deviceline[1] - $deviceline[2] + $uuid + $fs + $path
- - - + + +
@@ -170,78 +227,101 @@ END END ; + &Header::openbox('100%', 'center', $Lang::tr{'extrahd detected drives'}); print < END ; - open( FILE, "< $scanfile" ) or die "Unable to read $scanfile"; - @scans = ; - close FILE; - open( FILE, "< $partitionsfile" ) or die "Unable to read $partitionsfile"; - @partitions = ; - close FILE; - foreach $scanentry (sort @scans) - { - @scanline = split( /\;/, $scanentry ); - # remove wrong entries like usb controller name - if ($scanline[1] ne "\n") - { - print <  - /dev/$scanline[0] - $scanline[1] + foreach my $device (sort @devices) { + # Grab the device details. + my $vendor = &get_device_vendor($device); + my $model = &get_device_model($device); + my $bsize = &get_device_size($device); + + # Convert size into human-readable format. + my $size = &General::formatBytes($bsize); + + print <  + /dev/$device + $vendor $model + + $Lang::tr{'size'} $size +   +   END ; - } - foreach $partitionentry (sort @partitions) - { - @partitionline = split( /\;/, $partitionentry ); - if ( "$partitionline[0]" eq "$scanline[0]" ) { - $size = int($partitionline[1] / 1024); - print <$Lang::tr{'size'} $size MB -   -   + # Grab the known partitions of the current block device. + my @partitions = &get_device_partitions($device); + + foreach my $partition (@partitions) { + my $disabled; + + # Omit the partition size. + my $bsize = &get_device_size($partition); + + # Convert into human-readable format. + my $size = &General::formatBytes($bsize); + + # Get the mountpoint. + my $mountpoint = $mountpoints{$partition}; + + if ($mountpoint eq "/" or $mountpoint =~ "^/boot") { + $disabled = "disabled"; + } elsif(&is_mounted($mountpoint)) { + $disabled = "disabled"; + } + + # Omit the used filesystem. + my $fs = $filesystems{$partition}; + + # Check if the device is used as swap. + if (&is_swap($partition)) { + $disabled = "disabled"; + $mountpoint = "swap"; + } + + print < + UUID=$uuids{$partition} + + /dev/$partition + $Lang::tr{'size'} $size + - - - - - - - - + # Mark the used filesystem as selected. + if ($filesystem eq $fs) { + $selected = "selected"; + } + + print "\n"; + } + + print < + - - + + - - END -; +; unless($disabled) { + print"\n"; + } + print < END -; - } - } +; } + } print <