source: client/engine/Registry.lib @ 70aef0d

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 70aef0d was aa2d600, checked in by Irina Gómez <irinagomez@…>, 6 years ago

Functions for management Windows Registry: chntpw need '-e' option for edit registry (in ogLive bionic-5.0.0)

  • Property mode set to 100755
File size: 15.0 KB
RevLine 
[9f577259]1#!/bin/bash
2#/**
3#@file    Registry.lib
4#@brief   Librería o clase Registry
5#@class   Boot
6#@brief   Funciones para gestión del registro de Windows.
[0476bb8]7#@version 1.1.0
[9f577259]8#@warning License: GNU GPLv3+
9#*/
10
11
[0dbb5f7]12# Función ficticia para lanzar chntpw con timeout de 5 s., evitando cuelgues del programa.
13function chntpw ()
14{
15local CHNTPW
16CHNTPW=$(which drbl-chntpw)
17CHNTPW=${CHNTPW:-$(which chntpw)}
[aa2d600]18timeout --foreground 5s $CHNTPW -e "$@"
[0dbb5f7]19}
20
21
[9f577259]22#/**
23#         ogAddRegistryKey path_mountpoint str_hive str_keyname
24#@brief   Añade una nueva clave al registro de Windows.
[bf18bcbd]25#@param   path_mountpoint  directorio donde está montado el sistema Windows
26#@param   str_hive         sección del registro
27#@param   str_keyname      nombre de la clave
28#@return  (nada)
29#@exception OG_ERR_FORMAT    Formato incorrecto.
30#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
31#@note    hive = { default, sam, security, software, system, components }
32#@warning Requisitos: chntpw
33#@warning El sistema de archivos de Windows debe estar montada previamente.
[9f577259]34#@version 1.0.1 - Nueva función
35#@author  Ramon Gomez, ETSII Universidad de Sevilla
[cd2ae87]36#@date    2011-05-25
[9f577259]37#*/ ##
[1b804cc]38function ogAddRegistryKey ()
[9f577259]39{
[cd2ae87]40# Variables locales.
41local FILE
[9f577259]42
[cd2ae87]43# Si se solicita, mostrar ayuda.
44if [ "$*" == "help" ]; then
[bf18bcbd]45    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
46           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
[cd2ae87]47    return
48fi
49# Error si no se reciben 3 parámetros.
50[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
51# Camino del fichero de registro.
52FILE=$(ogGetHivePath "$1" "$2") || return $?
[9f577259]53
[cd2ae87]54# Añadir nueva clave.
[367e04d]55chntpw "$FILE" << EOT &> /dev/null
[3907aaf]56cd ${3%\\*}
57nk ${3##*\\}
[cd2ae87]58q
59y
60EOT
[1b804cc]61}
62
63#/**
[9570719]64#         ogAddRegistryValue path_mountpoint str_hive str_valuename [str_valuetype]
65#@brief   Añade un nuevo valor al registro de Windows, indicando su tipo de datos.
[bf18bcbd]66#@param   path_mountpoint  directorio donde está montado el sistema Windows
67#@param   str_hive         sección del registro
68#@param   str_valuename    nombre del valor
69#@param   str_valuetype    tipo de datos del valor (opcional)
70#@return  (nada)
71#@exception OG_ERR_FORMAT    Formato incorrecto.
72#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
[1cd64e6]73#@note    hive = { DEFAULT, SAM, SECURITY, SOFTWARE, SYSTEM, COMPONENTS }
74#@note    valuetype = { STRING, BINARY, DWORD }, por defecto: STRING
[bf18bcbd]75#@warning Requisitos: chntpw
76#@warning El sistema de archivos de Windows debe estar montada previamente.
[1b804cc]77#@version 1.0.1 - Nueva función
78#@author  Ramon Gomez, ETSII Universidad de Sevilla
[9570719]79#@date    2011-05-25
[1b804cc]80#*/ ##
[cd2ae87]81function ogAddRegistryValue ()
[1b804cc]82{
[9570719]83# Variables locales.
84local FILE TYPE
85
86# Si se solicita, mostrar ayuda.
87if [ "$*" == "help" ]; then
[bf18bcbd]88    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename [str_valuetype]" \
89           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'" \
90           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1' DWORD"
[9570719]91    return
92fi
93# Error si no se reciben 3 o 4 parámetros.
94[ $# == 3 -o $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
95# Camino del fichero de registro.
96FILE=$(ogGetHivePath "$1" "$2") || return $?
[dfcc55a]97case "${4^^}" in
98    STRING|"")  TYPE=1 ;;
99    BINARY)     TYPE=3 ;;
100    DWORD)      TYPE=4 ;;
101    *)          ogRaiseError $OG_ERR_OUTOFLIMIT "$4"
102                return $? ;;
[9570719]103esac
104
105# Devolver el dato del valor de registro.
106# /* (comentario Doxygen)
[367e04d]107chntpw "$FILE" << EOT &> /dev/null
[9570719]108cd ${3%\\*}
109nv $TYPE ${3##*\\}
110q
111y
112EOT
113# (comentario Doxygen) */
[1b804cc]114}
115
116
117#/**
118#         ogDeleteRegistryKey path_mountpoint str_hive str_keyname
119#@brief   Elimina una clave del registro de Windows con todo su contenido.
[bf18bcbd]120#@param   path_mountpoint  directorio donde está montado el sistema Windows
121#@param   str_hive         sección del registro
122#@param   str_keyname      nombre de la clave
123#@return  (nada)
124#@exception OG_ERR_FORMAT    Formato incorrecto.
125#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
126#@note    hive = { default, sam, security, software, system, components }
127#@warning Requisitos: chntpw
128#@warning El sistema de archivos de Windows debe estar montada previamente.
129#@warning La clave debe estar vacía para poder ser borrada.
[1b804cc]130#@version 1.0.1 - Nueva función
131#@author  Ramon Gomez, ETSII Universidad de Sevilla
[cd2ae87]132#@date    2011-05-25
[1b804cc]133#*/ ##
134function ogDeleteRegistryKey ()
135{
[cd2ae87]136# Variables locales.
137local FILE
138
139# Si se solicita, mostrar ayuda.
140if [ "$*" == "help" ]; then
[bf18bcbd]141    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
142           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey'"
[cd2ae87]143    return
144fi
145# Error si no se reciben 3 parámetros.
146[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
147# Camino del fichero de registro.
148FILE=$(ogGetHivePath "$1" "$2") || return $?
149
150# Añadir nueva clave.
[367e04d]151chntpw "$FILE" << EOT &> /dev/null
[3907aaf]152cd ${3%\\*}
153dk ${3##*\\}
[cd2ae87]154q
155y
156EOT
[1b804cc]157}
158
[1cd64e6]159
[1b804cc]160#/**
[bf18bcbd]161#         ogDeleteRegistryValue path_mountpoint str_hive str_valuename
[1b804cc]162#@brief   Elimina un valor del registro de Windows.
[bf18bcbd]163#@param   path_mountpoint  directorio donde está montado el sistema Windows
164#@param   str_hive         sección del registro
165#@param   str_valuename    nombre del valor
166#@return  (nada)
167#@exception OG_ERR_FORMAT    Formato incorrecto.
168#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
169#@note    hive = { default, sam, security, software, system, components }
170#@warning Requisitos: chntpw
171#@warning El sistema de archivos de Windows debe estar montada previamente.
[1b804cc]172#@version 1.0.1 - Nueva función
173#@author  Ramon Gomez, ETSII Universidad de Sevilla
[cd2ae87]174#@date    2011-05-25
[1b804cc]175#*/ ##
176function ogDeleteRegistryValue ()
177{
[cd2ae87]178# Variables locales.
179local FILE
180
181# Si se solicita, mostrar ayuda.
182if [ "$*" == "help" ]; then
[bf18bcbd]183    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
184           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'"
[cd2ae87]185    return
186fi
187# Error si no se reciben 3 parámetros.
188[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
189# Camino del fichero de registro.
190FILE=$(ogGetHivePath "$1" "$2") || return $?
191
192# Devolver el dato del valor de registro.
193# /* (comentario Doxygen)
[367e04d]194chntpw "$FILE" << EOT &> /dev/null
[cd2ae87]195cd ${3%\\*}
196dv ${3##*\\}
197q
198y
199EOT
200# (comentario Doxygen) */
[1b804cc]201}
202
203
204#/**
[e49911e]205#         ogGetHivePath path_mountpoint [str_hive|str_user]
[9f577259]206#@brief   Función básica que devuelve el camino del fichero con una sección del registro.
207#@param   path_mountpoint  directorio donde está montado el sistema Windows
208#@param   str_hive         sección del registro
209#@return  str_path - camino del fichero de registro
210#@exception OG_ERR_FORMAT    Formato incorrecto.
[bf18bcbd]211#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
[e49911e]212#@note    hive = { DEFAULT, SAM, SECURITY, SOFTWARE, SYSTEM, COMPONENTS, NombreDeUsuario }
[9f577259]213#@warning El sistema de archivos de Windows debe estar montada previamente.
214#@version 1.0.1 - Nueva función
215#@author  Ramon Gomez, ETSII Universidad de Sevilla
216#@date    2011-05-18
[e49911e]217#@version 1.1.0 - Soportar registro de un usuario local.
218#@author  Ramon Gomez, ETSII Universidad de Sevilla
219#@date    2015-10-14
[9f577259]220#*/ ##
[1b804cc]221function ogGetHivePath ()
[9f577259]222{
223# Variables locales.
[e49911e]224local FILE HIVE
[9f577259]225
226# Si se solicita, mostrar ayuda.
227if [ "$*" == "help" ]; then
[e49911e]228    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint [str_hive|str_user]" \
229           "$FUNCNAME /mnt/sda1 SOFTWARE  =>  /mnt/sda1/WINDOWS/System32/config/SOFTWARE" \
230           "$FUNCNAME /mnt/sda1 user1  =>  /mnt/sda1/Users/user1/NTUSER.DAT"
[9f577259]231    return
232fi
233# Error si no se reciben 2 parámetros.
234[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
235
[e49911e]236# Camino del fichero de registro de usuario o de sistema (de menor a mayor prioridad).
[58718f5]237FILE="$(ogGetPath "/$1/Windows/System32/config/$2")"
238[ -z "$FILE" ] && FILE="$(ogGetPath "/$1/Users/$2/NTUSER.DAT")"
239[ -z "$FILE" ] && FILE="$(ogGetPath "/$1/winnt/system32/config/$2")"
240[ -z "$FILE" ] && FILE="$(ogGetPath "/$1/Documents and Settings/$2/NTUSER.DAT")"
241[ -f "$FILE" ] && echo "$FILE" || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $?
[9f577259]242}
243
244
245#/**
246#         ogGetRegistryValue path_mountpoint str_hive str_valuename
247#@brief   Devuelve el dato de un valor del registro de Windows.
248#@param   path_mountpoint  directorio donde está montado el sistema Windows
249#@param   str_hive         sección del registro
250#@param   str_valuename    nombre del valor
251#@return  str_valuedata - datos del valor.
252#@exception OG_ERR_FORMAT    Formato incorrecto.
[bf18bcbd]253#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
[9f577259]254#@note    hive = { default, sam, security, software, system, components }
255#@warning Requisitos: chntpw, awk
[bf18bcbd]256#@warning El sistema de archivos de Windows debe estar montado previamente.
[9f577259]257#@version 0.9 - Adaptación para OpenGNSys.
258#@author  Ramon Gomez, ETSII Universidad de Sevilla
259#@date    2009-09-11
[0e24b16]260#@version 1.1.0 - Soportar tipos BINARY (parejas hexadecimales separadas por espacio).
261#@author  Ramon Gomez, ETSII Universidad de Sevilla
262#@date    2015-09-28
[9f577259]263#*/ ##
264function ogGetRegistryValue ()
265{
266# Variables locales.
267local FILE
268
269# Si se solicita, mostrar ayuda.
270if [ "$*" == "help" ]; then
[bf18bcbd]271    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename" \
272           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\NewKey\Value1'  ==>  1"
[9f577259]273    return
274fi
275# Error si no se reciben 3 parámetros.
276[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
277# Camino del fichero de registro.
[1b804cc]278FILE=$(ogGetHivePath "$1" "$2") || return $?
[9f577259]279
280# Devolver el dato del valor de registro.
281# /* (comentario Doxygen)
[e49911e]282chntpw "$FILE" << EOT 2> /dev/null | awk '/> Value/ {if (index($0, "REG_BINARY") > 0)
283                                                         {data=""}
284                                                     else
285                                                         {getline; data=$0;} }
[0e24b16]286                                          /^:[0-9A-F]+ / {data=data""substr($0, 9, 48);}
287                                          END {print data;}'
[9f577259]288cd ${3%\\*}
289cat ${3##*\\}
290q
[cd2ae87]291EOT
[9f577259]292# (comentario Doxygen) */
293}
294
295
296#/**
[3907aaf]297#         ogListRegistryKeys path_mountpoint str_hive str_key
298#@brief   Lista los nombres de subclaves de una determinada clave del registro de Windows.
[9f577259]299#@param   path_mountpoint  directorio donde está montado el sistema Windows
300#@param   str_hive         sección del registro
301#@param   str_key          clave de registro
302#@return  str_subkey ... - lista de subclaves
303#@exception OG_ERR_FORMAT    Formato incorrecto.
[bf18bcbd]304#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
[9f577259]305#@note    hive = { default, sam, security, software, system, components }
306#@warning Requisitos: chntpw, awk
[bf18bcbd]307#@warning El sistema de archivos de Windows debe estar montado previamente.
[9f577259]308#@version 0.9 - Adaptación para OpenGNSys.
309#@author  Ramon Gomez, ETSII Universidad de Sevilla
310#@date    2009-09-23
311#*/ ##
[3907aaf]312function ogListRegistryKeys ()
[9f577259]313{
314# Variables locales.
315local FILE
316
317# Si se solicita, mostrar ayuda.
318if [ "$*" == "help" ]; then
[bf18bcbd]319    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
320           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
[9f577259]321    return
322fi
323# Error si no se reciben 3 parámetros.
324[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
325
326# Camino del fichero de registro.
[1b804cc]327FILE=$(ogGetHivePath "$1" "$2") || return $?
[9f577259]328
329# Devolver la lista de claves de registro.
[367e04d]330chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/^  $/ {print $2}'
[9f577259]331ls $3
332q
[9570719]333EOT
[9f577259]334}
335
336
337#/**
[3907aaf]338#         ogListRegistryValues path_mountpoint str_hive str_key
339#@brief   Lista los nombres de valores de una determinada clave del registro de Windows.
[bf18bcbd]340#@param   path_mountpoint  directorio donde está montado el sistema Windows
341#@param   str_hive         sección del registro
342#@param   str_key          clave de registro
343#@return  str_value ... - lista de valores
344#@exception OG_ERR_FORMAT    Formato incorrecto.
345#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
346#@note    hive = { default, sam, security, software, system, components }
347#@warning Requisitos: chntpw, awk
348#@warning El sistema de archivos de Windows debe estar montado previamente.
[3907aaf]349#@version 1.0.1 - Nueva función.
350#@author  Ramon Gomez, ETSII Universidad de Sevilla
351#@date    2011-05-26
352#*/ ##
353function ogListRegistryValues ()
354{
355# Variables locales.
356local FILE
357
358# Si se solicita, mostrar ayuda.
359if [ "$*" == "help" ]; then
[bf18bcbd]360    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_key" \
361           "$FUNCNAME /mnt/sda1 SOFTWARE '\Microsoft\Windows\CurrentVersion'"
[3907aaf]362    return
363fi
364# Error si no se reciben 3 parámetros.
365[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
366# Camino del fichero de registro.
367FILE=$(ogGetHivePath "$1" "$2") || return $?
368
369# Devolver la lista de claves de registro.
[367e04d]370chntpw "$FILE" << EOT 2> /dev/null | awk 'BEGIN {FS="[<>]"} $1~/REG_/ {print $2}'
[3907aaf]371ls $3
372q
373EOT
374}
375
376
377#/**
[9f577259]378#         ogSetRegistryValue path_mountpoint str_hive str_valuename str_valuedata
379#@brief   Establece el dato asociado a un valor del registro de Windows.
380#@param   path_mountpoint  directorio donde está montado el sistema Windows
[bf18bcbd]381#@param   str_hive         sección del registro
[9f577259]382#@param   str_valuename    nombre del valor de registro
383#@param   str_valuedata    dato del valor de registro
384#@return  (nada)
385#@exception OG_ERR_FORMAT    Formato incorrecto.
[bf18bcbd]386#@exception OG_ERR_NOTFOUND  Fichero de registro no encontrado.
[9f577259]387#@note    hive = { default, sam, security, software, system, components }
[bf18bcbd]388#@warning Requisitos: chntpw
389#@warning El sistema de archivos de Windows debe estar montado previamente.
[9f577259]390#@version 0.9 - Adaptación para OpenGNSys.
391#@author  Ramon Gomez, ETSII Universidad de Sevilla
392#@date    2009-09-24
[0476bb8]393#@version 1.1.0 - Soportar tipos BINARY (parejas hexadecimales separadas por espacio).
394#@author  Ramon Gomez, ETSII Universidad de Sevilla
395#@date    2015-09-28
[9f577259]396#*/ ##
397function ogSetRegistryValue ()
398{
399# Variables locales.
[0476bb8]400local FILE i n tmpfile
[9f577259]401
402# Si se solicita, mostrar ayuda.
403if [ "$*" == "help" ]; then
[dfcc55a]404    ogHelp "$FUNCNAME" "$FUNCNAME path_mountpoint str_hive str_valuename str_data" \
[0476bb8]405           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\StringValue' \"Abcde Fghij\"" \
406           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\DwordValue' 1" \
407           "$FUNCNAME /mnt/sda1 SOFTWARE '\Key\SubKey\BinaryValue' \"04 08 0C 10\""
[9f577259]408    return
409fi
410# Error si no se reciben 4 parámetros.
411[ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
412# Camino del fichero de registro.
[1b804cc]413FILE=$(ogGetHivePath "$1" "$2") || return $?
[9f577259]414
[0476bb8]415# Fichero temporal para componer la entrada al comando "chntpw".
416tmpfile=/tmp/chntpw$$
417trap "rm -f $tmpfile" 1 2 3 9 15
418
419# Comprobar tipo de datos del valor del registro.
420cat << EOT >$tmpfile
421ls ${3%\\*}
422q
423EOT
424if [ -n "$(chntpw "$FILE" < $tmpfile 2> /dev/null | grep "BINARY.*<${3##*\\}>")" ]; then
425    # Procesar tipo binario (incluir nº de bytes y líneas de 16 parejas hexadecimales).
[0e24b16]426    [[ "$4 " =~ ^([0-9A-F]{2} )*$ ]] || ogRaiseError $OG_ERR_FORMAT "\"$4\"" || return $?
[0476bb8]427    let n=${#4}+1
428    cat << EOT >$tmpfile
429cd ${3%\\*}
430ed ${3##*\\}
431$[n/3]
432EOT
433    # Formato de líneas hexadecimales:   :OFFSET  XX YY ZZ ... (hasta 16 parejas).
434    for (( i=0; i<n; i+=48 )); do
435        printf ":%05x  %s\n" $[i/3] "${4:$i:48}" >> $tmpfile
436    done
437    echo -e "s\nq\ny" >> $tmpfile
438else
439    # Cambiar el dato del valor de registro para cadenas y bytes.
440    cat << EOT >$tmpfile
[367e04d]441cd ${3%\\*}
442ed ${3##*\\}
[9f577259]443$4
444q
445y
[9570719]446EOT
[0476bb8]447
448fi
449
450# Aplicar cambios.
451chntpw "$FILE" < $tmpfile &> /dev/null
452rm -f $tmpfile
[9f577259]453}
454
[0476bb8]455
Note: See TracBrowser for help on using the repository browser.