diff --git a/ogclient/functions/ogGrubSecurity b/ogclient/functions/ogGrubSecurity new file mode 100755 index 0000000..7895ea9 --- /dev/null +++ b/ogclient/functions/ogGrubSecurity @@ -0,0 +1,25 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from BootLib import ogGrubSecurity + +parser = argparse.ArgumentParser (add_help=False) +parser.add_argument ('disk') +parser.add_argument ('par') +parser.add_argument ('user', nargs='?', default='root') +parser.add_argument ('passwd', nargs='?', default='') + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogGrubSecurity', 'ogGrubSecurity int_ndiskSecondStage int_partitionSecondStage [USER] [PASSWORD]', ['ogGrubSecurity 1 1', 'ogGrubSecurity 1 2 user clave']) + sys.exit (0) + +args = parser.parse_args() +ret = ogGrubSecurity (args.disk, args.par, args.user, args.passwd) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret) diff --git a/ogclient/lib/python3/BootLib.py b/ogclient/lib/python3/BootLib.py index 6f0a1ca..7dfe45d 100644 --- a/ogclient/lib/python3/BootLib.py +++ b/ogclient/lib/python3/BootLib.py @@ -1054,6 +1054,45 @@ def ogBootLoaderOgliveDefaultEntry (disk, par): #@exception OG_ERR_PARTITION Tipo de partición desconocido o no se puede montar (ogMount). #@exception OG_ERR_NOTFOUND No encuentra archivo de configuración del grub. #*/ ## +def ogGrubSecurity (disk, par, user='root', passwd=''): +#localizar disco segunda etapa del grub + secondstage = FileSystemLib.ogMount (disk, par) + if not secondstage: return None + + ## original bash code: + ## $SECONDSTAGE/{,boot/}{{grubMBR,grubPARTITION}/boot/,}{grub{,2},{,efi/}EFI/*}/{menu.lst,grub.cfg,grub.cfg.backup.og} + grubcfg = [] + for _uno in ['', 'boot']: + for _dos in ['grubMBR/boot', 'grubPARTITION/boot', '']: + for _tres in ['grub', 'grub2', 'EFI/*', 'efi/EFI/*']: + for _cuatro in ['menu.lst', 'grub.cfg', 'grub.cfg.backup.og']: + path = '/'.join ([secondstage, _uno, _dos, _tres, _cuatro]) + grubcfg += glob.glob (path) + print (f'nati grubcfg ({grubcfg})') + +# comprobamos que exista el archivo de configuración. + if not grubcfg: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, 'grub.cfg') + return None + + if passwd: + encryptpasswd = subprocess.run (['grub-mkpasswd-pbkdf2'], input=f'{passwd}\n{passwd}', text=True, capture_output=True).stdout + if encryptpasswd: + lines = encryptpasswd.strip().splitlines() + encryptpasswd = lines[-1] + encryptpasswd = re.sub ('^.*grub', 'grub', encryptpasswd) + + for file in grubcfg: + # Eliminamos configuración anterior + subprocess.run (['sed', '-i', '-e', '/superusers/d', '-e', '/password_pbkdf2/d', file]) + + # Configuramos grub.cfg para que sólo permita editar o entrar en línea de comandos al usuario especificado + if passwd: + subprocess.run (['sed', '-i', f'1i\\password_pbkdf2 {user} {encryptpasswd}', file]) + subprocess.run (['sed', '-i', f'1i\\set superusers="{user}"', file]) + + # Permitimos que se seleccionen las entradas + subprocess.run (['sed', '-i', '/menuentry /s/{/--unrestricted {/', file]) #/**