From 0fb46b1f2b42042ea3d305e3abcab5079a837fcb Mon Sep 17 00:00:00 2001 From: lgromero Date: Fri, 20 Sep 2024 10:43:29 +0000 Subject: [PATCH] refs #763 Deletes all sudo commands, modifies squashfs logic, readjust some code in setsmbpass, adds some comprobations and clean code --- bin/oglivecli | 106 ++++++++++++++++++++++++++++++++++--------------- bin/setsmbpass | 71 ++++++++++++++++----------------- 2 files changed, 109 insertions(+), 68 deletions(-) diff --git a/bin/oglivecli b/bin/oglivecli index 4f551b5..3d4f280 100755 --- a/bin/oglivecli +++ b/bin/oglivecli @@ -297,22 +297,48 @@ function install() { OGINITRD=$OGLIVEDIR/oginitrd.img [ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img if [ -r $OGINITRD ]; then + echo "Tipo de compresión: $COMPRESS" COMPRESS=$(file -b "$OGINITRD" | awk '{print tolower($1);}') SAMBAPASS=$($COMPRESS -dc $OGINITRD | \ cpio -i --to-stdout scripts/ogfunctions 2>/dev/null | \ sed -n '/^[ \t].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p') + echo "Contraseña de Samba extraída: $SAMBAPASS" fi rm -fr ${OGLIVEDIR}.old 2>/dev/null mv -f $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null - - TMPDIR=/tmp/${OGLIVEFILE%.iso} - mkdir -p $OGLIVEDIR $TMPDIR + # Changes to a generic TMPDIR name + #TMPDIR=/tmp/${OGLIVEFILE%.iso} + TMPDIR=/tmp/$DOWNLOADDIR/ogLive + # TMPDIR for SQUASHFS mount + TMPDIR_SQUASHFS=/tmp/ogclient_mount + # Antes del montaje le cambiamos el nombre a la iso por un nombre generico + mv -f $OGLIVEFILE $DOWNLOADDIR/oglive.iso 2>/dev/null || { echo "{\"status\": \"error\", \"error\": \"Cannot rename $OGLIVEFILE to oglive.iso.\"}"; exit 1; } + OGLIVEFILE=$DOWNLOADDIR/oglive.iso + # TMPDIR=/tmp/oglive + mkdir -p $OGLIVEDIR $TMPDIR $TMPDIR_SQUASHFS + if [ ! -d "$OGLIVEDIR" ]; then + echo "{\"status\": \"error\", \"error\": \"Failed to create or access directory $OGLIVEDIR.\"}" + exit 1 + fi + + if [ ! -d "$TMPDIR" ]; then + echo "{\"status\": \"error\", \"error\": \"Failed to create or access directory $TMPDIR.\"}" + exit 1 + fi + + if [ ! -d "$TMPDIR_SQUASHFS" ]; then + echo "{\"status\": \"error\", \"error\": \"Failed to create or access directory $TMPDIR_SQUASHFS.\"}" + exit 1 + fi trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15 - sudo mount -o loop,ro $OGLIVEFILE $TMPDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"mount failed.\"}"; exit 1; } + # sudo mount -o loop,ro $OGLIVEFILE $TMPDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"mount failed.\"}"; exit 1; } + # Asumimos que esta el montaje añadido al fstab + # La linea fstab es la siguiente: /opt/ogboot/lib/oglive.iso /tmp/opt/ogboot/lib/ogLive iso9660 loop,ro,user 0 0 + mount $OGLIVEFILE >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"mount failed.\"}"; exit 1; } cp -va $TMPDIR/ogclient/* $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Cannot copy files to ogLive directory.\"}"; exit 1; } - sudo umount $TMPDIR >/dev/null 2>&1 - + chmod -R u+w $OGLIVEDIR || { echo "{\"status\": \"error\", \"error\": \"Failed to change permissions.\"}"; exit 1; } + umount $TMPDIR >/dev/null 2>&1 if [ ! -f $INFOFILE ]; then rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"Linking to $TFTPDIR/$DEFOGLIVE failed.\"}"; exit 1; } @@ -320,20 +346,33 @@ function install() { fi if [ -n "$SAMBAPASS" ]; then - echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed with SAMBAPASS.\"}"; exit 1; } + #echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed with SAMBAPASS.\"}"; exit 1; } + echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed with SAMBAPASS.\"}"; exit 1; } else - $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed no SAMBAPASS.\"}"; exit 1; } + $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" || { echo "{\"status\": \"error\", \"error\": \"setsmbpass failed no SAMBAPASS.\"}"; exit 1; } fi - find -L $OGLIVEDIR -type d -exec chmod 755 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod directories failed.\"}"; exit 1; } find -L $OGLIVEDIR -type f -exec chmod 644 {} \; >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chmod files failed.\"}"; exit 1; } sudo chown -R :opengnsys $OGLIVEDIR >/dev/null 2>&1 || { echo "{\"status\": \"error\", \"error\": \"chown failed.\"}"; exit 1; } OGSQFS=$OGLIVEDIR/ogclient.sqfs - - if sudo mount -o loop,ro $OGSQFS $TMPDIR >/dev/null 2>&1; then - RSYNCSERV=$(sudo rsync --version 2>/dev/null | awk '/protocol/ {print $6}') - RSYNCCLNT=$(sudo chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') + trap "exit 0" SIGINT + TMPDIR=/tmp/oglive + if mountpoint -q "$TMPDIR"; then + echo "$TMPDIR ya está montado, desmontando primero..." + umount $TMPDIR >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "Error: No se pudo desmontar $TMPDIR" + exit 1 + fi + fi + echo "montando $TMPDIR_SQUASHFS" + if mount $TMPDIR_SQUASHFS ; then + RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}') + echo "RSYNCSERV (versión en el servidor): $RSYNCSERV" + #RSYNCCLNT=$(sudo chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') + RSYNCCLNT=$($TMPDIR_SQUASHFS/usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') + echo "RSYNCCLNT (versión en el cliente montado): $RSYNCCLNT" if [ -z "$RSYNCSERV" ] || [ "$RSYNCSERV" -gt "${RSYNCCLNT:-1}" ]; then if [ -e "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" ]; then mv -f "$OPENGNSYS/client/bin/rsync-$RSYNCSERV" "$OPENGNSYS/client/bin/rsync" 2>/dev/null @@ -343,13 +382,11 @@ function install() { mv -f "$OPENGNSYS/client/bin/rsync" "$OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}')" fi fi - sudo umount $TMPDIR >/dev/null 2>&1 - sudo rmdir $TMPDIR >/dev/null 2>&1 || sudo rm -rf $TMPDIR >/dev/null 2>&1 + umount $TMPDIR_SQUASHFS + rmdir $TMPDIR_SQUASHFS || sudo rm -rf $TMPDIR_SQUASHFS fi - - # Confirmar y eliminar el archivo ISO + echo "finished mount squashfs" sudo rm -f "$OGLIVEFILE" - # Obtener el checksum desde el archivo ogclient.sqfs.sum en el directorio $OGLIVEDIR CHECKSUM_FILE="$OGLIVEDIR/ogclient.sqfs.sum" if [ -f "$CHECKSUM_FILE" ]; then OGLIVEID=$(cat "$CHECKSUM_FILE") @@ -408,7 +445,7 @@ function uninstall() { # Get information about the default ogLive client. function get_default() { local DEFAULT_LINK="$TFTPDIR/$DEFOGLIVE" - local DIR OGLIVEDIST OGLIVEKRNL OGLIVEARCH OGLIVEREV OGLIVEISO OGLIVEDIR CHECKSUM CHECKSUM_FILE + local DIR OGLIVEDIST OGLIVEKRNL OGLIVEARCH OGLIVEREV OGLIVEISO OGLIVEDIR # Verificar que el enlace simbólico del ogLive por defecto existe. if [ ! -L "$DEFAULT_LINK" ]; then @@ -433,18 +470,9 @@ function get_default() { OGLIVEREV="$(echo $OGLIVEDIR | cut -d- -f4)" OGLIVEISO="" # No tenemos la información del ISO aquí, podría necesitarse un ajuste si se requiere - # Obtener el UUID (Checksum) del archivo ogclient.sqfs.sum - CHECKSUM_FILE="$DIR/ogclient.sqfs.sum" - if [ -f "$CHECKSUM_FILE" ]; then - CHECKSUM=$(cat "$CHECKSUM_FILE" | cut -d ' ' -f 1) - else - CHECKSUM="" - fi - # Construir el JSON con la información. local INFO=$(cat << EOT { - "id": "$CHECKSUM", "distribution": "$OGLIVEDIST", "kernel": "$OGLIVEKRNL", "architecture": "$OGLIVEARCH", @@ -459,7 +487,6 @@ EOT echo "$INFO" } - # Set default ogLive client by checksum. function set_default() { local CHECKSUM=$1 @@ -506,7 +533,11 @@ function list_installed_oglives() { local installed_ogLives=() for i in $INST; do - local OGLIVEDIR="" + NF=$(echo $i | awk -F- '{print NF-1}') + local OGLIVEDIST="" + local OGLIVEKRNL="" + local OGLIVEARCH="" + local OGLIVEREV="" local CHECKSUM="" local CHECKSUM_FILE="$TFTPDIR/$i/ogclient.sqfs.sum" @@ -514,12 +545,23 @@ function list_installed_oglives() { CHECKSUM=$(cat "$CHECKSUM_FILE" | cut -d ' ' -f 1) fi - OGLIVEDIR=$i + case $NF in + 1) OGLIVEDIST="" OGLIVEKRNL=$(echo $i|cut -f2 -d-) OGLIVEARCH="i386" OGLIVEREV="" ;; + 2) eval $(echo $i | awk -F- '{printf "OGLIVEDIST=\"\" OGLIVEKRNL=%s OGLIVEARCH=amd64 OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$0}') ;; + 3) eval $(echo $i | awk -F- '{if ($3=="i386") printf "OGLIVEDIST=\"\" OGLIVEKRNL=%s OGLIVEARCH=%s OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$0; else printf "OGLIVEDIST=%s OGLIVEKRNL=%s OGLIVEARCH=i386 OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$0}') ;; + 4) eval $(echo $i | awk -F- '{printf "OGLIVEDIST=%s OGLIVEKRNL=%s OGLIVEARCH=%s OGLIVEREV=%s OGLIVEDIR=%s",$2,$3,$4,$5,$0}') ;; + esac local DATA=$(jq -n \ --arg id "$CHECKSUM" \ --arg filename "$OGLIVEDIR" \ - '{id: $id, filename: $filename}') + --arg dist "$OGLIVEDIST" \ + --arg krnl "$OGLIVEKRNL" \ + --arg arch "$OGLIVEARCH" \ + --arg rev "$OGLIVEREV" \ + --arg dir "$TFTPDIR/$OGLIVEDIR" \ + --arg iso "" \ + '{id: $id, filename: $filename, distribution: $dist, kernel: $krnl, architecture: $arch, revision: $rev, directory: $dir, iso: $iso}') installed_ogLives+=("$DATA") diff --git a/bin/setsmbpass b/bin/setsmbpass index 7f6739e..a4e8fe2 100755 --- a/bin/setsmbpass +++ b/bin/setsmbpass @@ -64,17 +64,18 @@ for OGLIVE in $LIST; do # descomprimir Initrd, sustituir clave y recomprimir Initrd). echo "Configurando cliente \"$OGLIVE\" ..." mkdir -p $TMPDIR + echo "TMPDIR $TMPDIR" cd $TMPDIR || { echo "Error: no se pudo cambiar al directorio temporal."; exit 3; } - # Verificar si el archivo es gzip o lz4 antes de descomprimir. + echo "Verificar si el archivo es gzip o lz4 antes de descomprimir" if file "$CLIENTINITRD" | grep -q "gzip compressed data"; then - if ! gzip -dc "$CLIENTINITRD" | sudo cpio -im; then + if ! gzip -dc "$CLIENTINITRD" | cpio -im; then echo "Error: No se pudo descomprimir y extraer $CLIENTINITRD con gzip." exit 4 fi COMPRESS_CMD="gzip -9c" elif file "$CLIENTINITRD" | grep -q "LZ4 compressed data"; then - if ! lz4 -d "$CLIENTINITRD" | sudo cpio -im; then + if ! lz4 -d "$CLIENTINITRD" | cpio -im; then echo "Error: No se pudo descomprimir y extraer $CLIENTINITRD con lz4." exit 4 fi @@ -91,48 +92,46 @@ for OGLIVE in $LIST; do # FIN CÓDIGO TEMPORAL. # Ticket 565, preparar acceso Rsync cliente. echo "$SAMBAPASS" | sudo tee scripts/passrsync > /dev/null - # Guardar tokens de seguridad. + echo "Guardar tokens de seguridad" cat < /dev/null CLIENTID=$(jq -r .client.id $OGCFGFILE) CLIENTSECRET=$(jq -r .client.secret $OGCFGFILE) EOT - sudo chown root.root scripts/passrsync scripts/client.cfg - sudo chmod 400 scripts/passrsync scripts/client.cfg - pwd - # Paso 1: Buscar archivos - sudo find . -print > /tmp/filelist.txt - if [ $? -ne 0 ]; then - echo "Error: No se pudo encontrar los archivos." - exit 5 - fi + sudo chown ogboot:ogboot scripts/passrsync scripts/client.cfg +# pwd +# sudo find . -print > /tmp/filelist.txt +# if [ $? -ne 0 ]; then +# echo "Error: No se pudo encontrar los archivos." +# exit 5 +# fi - # Paso 2: Crear el archivo CPIO - sudo cpio -H newc -oa < /tmp/filelist.txt > /tmp/initrd.cpio - if [ $? -ne 0 ]; then - echo "Error: No se pudo crear el archivo CPIO." - exit 5 - fi - - # Paso 3: Comprimir el archivo CPIO - sudo $COMPRESS_CMD < /tmp/initrd.cpio > /tmp/initrd.cpio.gz - if [ $? -ne 0 ]; then - echo "Error: No se pudo comprimir el archivo CPIO." - exit 5 - fi - echo $CLIENTINITRD - # Paso 4: Escribir el archivo comprimido en el destino - # sudo tee "$CLIENTINITRD" < /tmp/initrd.cpio.gz > /dev/null - sudo dd if=/tmp/initrd.cpio.gz of=/opt/opengnsys/tftpboot/ogLive-5.11.0-r20210413/oginitrd.img - if [ $? -ne 0 ]; then - echo "Error: No se pudo escribir el archivo comprimido en el destino." - exit 5 - fi - sudo rm /tmp/initrd.cpio /tmp/initrd.cpio.gz +# cpio -H newc -oa < /tmp/filelist.txt > /tmp/initrd.cpio +# if [ $? -ne 0 ]; then +# echo "Error: No se pudo crear el archivo CPIO." +# exit 5 +# fi +# $COMPRESS_CMD < /tmp/initrd.cpio > /tmp/initrd.cpio.gz +# if [ $? -ne 0 ]; then +# echo "Error: No se pudo comprimir el archivo CPIO." +# exit 5 +# fi +# echo "Copiando el contenido del archivo /tmp/initrd.cpio.gz al archivo /opt/opengnsys/tftpboot/ogLive-5.11.0-r20210413/oginitrd.img." +# echo $CLIENTINITRD +# dd if=/tmp/initrd.cpio.gz of=/opt/opengnsys/tftpboot/ogLive-5.11.0-r20210413/oginitrd.img +# echo "dd ejecutado correctamente" +# if [ $? -ne 0 ]; then +# echo "Error: No se pudo escribir el archivo comprimido en el destino." +# exit 5 +# fi +# echo "borrando rm /tmp/initrd.cpio /tmp/initrd.cpio.gz" +# sudo rm /tmp/initrd.cpio /tmp/initrd.cpio.gz + find . | cpio -H newc -oa | gzip -9c > "$CLIENTINITRD" else echo "$PROG: Aviso: no se ha modificado la clave del cliente \"$OGLIVE\"." fi + echo "intentando borrar $TMPDIR" sudo rm -fr $TMPDIR - # Calcular suma de comprobación. + echo "Calcular suma de comprobación" sudo md5sum "$CLIENTINITRD" | cut -f1 -d" " | sudo tee "$CLIENTINITRD.sum" > /dev/null let CHANGES++ else