OpenGnsys  1.1.1
PostConf.lib
Ir a la documentación de este archivo.
1 #!/bin/bash
2 #
12 # ##
25 function ogCleanOs ()
26 {
27 
28 # Si se solicita, mostrar ayuda.
29 if [ "$*" == "help" ]; then
30  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
31  "$FUNCNAME 1 1"
32  return
33 fi
34 
35 # Error si no se reciben 2 parámetros.
36 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT "$FUNCNAME int_ndisk int_nfilesys" || return $?
37 
38 case "$(ogGetOsType $1 $2)" in
39  Linux)
40  # Borramos los ficheros de dispositivos y los temporales.
41  ogCleanLinuxDevices $1 $2
42  rm -rf $(ogMount $1 $2)/tmp/* #*/ Comentario Doxygen
43  ;;
44  Windows)
45  # Borrar ficheros de hibernación y paginación de Windows.
46  [ -n "$(ogGetPath $1 $2 pagefile.sys)" ] && ogDeleteFile $1 $2 pagefile.sys
47  [ -n "$(ogGetPath $1 $2 hiberfil.sys)" ] && ogDeleteFile $1 $2 hiberfil.sys
48  [ -n "$(ogGetPath $1 $2 swapfile.sys)" ] && ogDeleteFile $1 $2 swapfile.sys
49  ;;
50 esac
51 
52 }
53 
54 
55 
56 # ##
61 function ogInstallMiniSetup ()
62 {
63 # Si se solicita, mostrar ayuda.
64 if [ "$*" == "help" ]; then
65  ogHelp "$FUNCNAME" "$MSG_SEE ogInstallFirstBoot ogInstallRunonce"
66  return
67 fi
68 case $# in
69  3) # Ejecución en el primer arranque de la máquina.
70  ogInstallFirstBoot "$@" ;;
71  6|8) # Ejecución en el "runonce".
72  ogInstallRunonce "$@" ;;
73  *) ogRaiseError $OG_ERR_FORMAT
74  return $? ;;
75 esac
76 }
77 
78 
79 # ##
95 function ogInstallFirstBoot ()
96 {
97 local MNTDIR DIR CMDDIR CMDFILE
98 # Si se solicita, mostrar ayuda.
99 if [ "$*" == "help" ]; then
100  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_filename" \
101  "$FUNCNAME 1 1 filename.cmd"
102  return
103 fi
104 # Error si no se reciben 3 parámetros.
105 [ $# == 3 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
106 # Comprobar que existe el directorio del fichero de comandos.
107 MNTDIR=$(ogMount "$1" "$2") || return $?
108 for i in winnt windows; do
109  DIR=$(ogGetPath $MNTDIR/$i/system32)
110  [ -n "$DIR" ] && CMDDIR=$DIR
111 done
112 [ -n "$CMDDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$MNTDIR/windows/system32" || return $?
113 CMDFILE="$CMDDIR/$3"
114 
115 # Creamos el archivo cmd y creamos un par de comandos para que una vez acabe la
116 # postconfiguracion resetee el mini setup, sino lo haria en cada arranque.
117 cat > "$CMDFILE" << EOF
118 REG ADD HKLM\System\Setup /v SystemSetupInProgress /t REG_DWORD /d 0 /f
119 REG ADD HKLM\System\Setup /v CmdLine /t REG_SZ /d "" /f
120 EOF
121 
122 # Crear los valores de registro necesarios para que se haga la ejecución del .cmd al aranque.
123 ogSetRegistryValue "$MNTDIR" SYSTEM "\Setup\SystemSetupInProgress" 1
124 ogSetRegistryValue "$MNTDIR" SYSTEM "\Setup\SetupType" 4
125 #ogDeleteRegistryValue "$MNTDIR" SYSTEM "\Setup\CmdLine"
126 ogAddRegistryValue "$MNTDIR" SYSTEM "\Setup\CmdLine"
127 ogSetRegistryValue "$MNTDIR" SYSTEM "\Setup\CmdLine" "cmd.exe /c $(basename $CMDFILE)"
128 }
129 
130 
131 # ##
154 function ogInstallRunonce ()
155 {
156 local MOUNTPOINT DIR CMDDIR CMDFILE
157 # Si se solicita, mostrar ayuda.
158 if [ "$*" == "help" ]; then
159  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_filename str_adm_user str_adm_password bool_autologin [str_auto_user str_auto_password]" \
160  "$FUNCNAME 1 1 filename.cmd administrator passadmin 1 userauto passuserauto" \
161  "$FUNCNAME 1 1 filename.cmd administrator passadmin 0"
162  return
163 fi
164 # Error si no se reciben 6 u 8 parámetros.
165 [ $# == 6 -o $# == 8 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
166 # Punto de montaje.
167 MOUNTPOINT="$(ogGetPath "$1" "$2" /)"
168 # Comprobar que existe el directorio del fichero de comandos.
169 for i in winnt windows; do
170  DIR=$(ogGetPath $MOUNTPOINT/$i/system32)
171  [ -n "$DIR" ] && CMDDIR=$DIR
172 done
173 [ -n "$CMDDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$MOUNTPOINT/Windows/System32" || return $?
174 CMDFILE="$CMDDIR/$3"
175 
176 if [ $6 == 0 ]; then
177  # Si no queremos hacer autologin despues de la postconfiguracion lo indicamos en las claves de registro
178  cat > "$CMDFILE" << EOF
179 DEL C:\ogboot.*
180 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f
181 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d "" /f
182 REG DELETE "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /f
183 EOF
184 else
185  # Si queremos hacer autologin despues de la postconfiguracion introducimos la informacion en las claves de registro
186  cat > "$CMDFILE" << EOF
187 DEL C:\ogboot.*
188 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
189 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d "$7" /f
190 REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d "$8" /f
191 EOF
192 fi
193 #Creamos las claves de registro necesarias para que meter el cmd en el runonce del usuario y este haga autologin
194 ogAddRegistryValue $MOUNTPOINT software '\Microsoft\Windows\CurrentVersion\RunOnce\PostConfiguracion' 2>/dev/null
195 ogSetRegistryValue $MOUNTPOINT software '\Microsoft\Windows\CurrentVersion\RunOnce\PostConfiguracion' "C:\windows\system32\\$3" 2>/dev/null
196 ogAddRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon' 2>/dev/null
197 ogSetRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon' 1 2>/dev/null
198 ogAddRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName' 2>/dev/null
199 ogSetRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName' "$4" 2>/dev/null
200 ogAddRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName' 2>/dev/null
201 ogSetRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName' ".\\" 2>/dev/null
202 ogAddRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword' 2>/dev/null
203 ogSetRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword' "$5" 2>/dev/null
204 ogDeleteRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLockOnLogon' 2>/dev/null
205 ogDeleteRegistryValue $MOUNTPOINT software '\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoLogonCount' 2>/dev/null
206 }
207 
208 # ##
225 function ogAddCmd ()
226 {
227 local MOUNTPOINT CMDFILE
228 # Si se solicita, mostrar ayuda.
229 if [ "$*" == "help" ]; then
230  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_filename str_commands" \
231  "$FUNCNAME 1 1 filename.cmd command"
232  return
233 fi
234 # Error si no se reciben 4 parámetros.
235 [ $# == 4 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
236 # Punto de montaje
237 MOUNTPOINT="$(ogMount "$1" "$2")" || return $?
238 # Crear fichero de comandos, si no existe.
239 CMDFILE="$(ogGetPath "$MOUNTPOINT/windows/system32")/$3"
240 [ -n "$CMDFILE" ] || ogInstallMiniSetup "$1" "$2" "$3"
241 [ -n "$CMDFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$MOUNTPOINT/windows/system32/$3" || return $?
242 
243 # Concatenamos el comando en el fichero de comandos
244 cat >> "$CMDFILE" << EOF
245 $4
246 EOF
247 }
248 
249 
250 # ##
268 function ogDomainScript ()
269 {
270 local CMDDIR
271 # Si se solicita, mostrar ayuda.
272 if [ "$*" == "help" ]; then
273  ogHelp "$FUNCNAME" \
274  "$FUNCNAME int_ndisk int_npartition str_filename str_domain str_user str_password" \
275  "$FUNCNAME 1 1 filename.cmd domain user password_user"
276  return
277 fi
278 # Error si no se reciben 6 parámetros.
279 [ $# == 6 ] || return $(ogRaiseError $OG_ERR_FORMAT; echo $?)
280 # Punto de montaje
281 MOUNTPOINT="$(ogMount "$1" "$2")" || return $?
282 # Comprobar que existe el directorio de comandos.
283 CMDDIR=$(ogGetPath "$MOUNTPOINT/windows/system32")
284 [ -n "$CMDDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$1/windows/system32" || return $?
285 
286 # Añadimos en el cmd que se ejecutara al arranque, el comando de ejecutar el script que añade al dominio.
287 ogAddCmd $1 $2 "$3" "CSCRIPT joindomain.vbs"
288 # Eliminamos el script porque tiene el usuario de administrador de dominio en claro
289 ogAddCmd $1 $2 "$3" "DEL /Q C:\Windows\System32\joindomain.vbs"
290 # Metemos unas claves de registro para que el dominio salga seleccionado por defecto
291 ogAddCmd $1 $2 "$3" "REG ADD \"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\" /v DefaultDomainName /t REG_SZ /d \"$4\" /f"
292 
293 # Creamos el archivo joindomain.vbs que nos introduce la maquina en el dominio
294 cat > "$CMDDIR/joindomain.vbs" << EOF
295 Const JOIN_DOMAIN = 1
296 Const ACCT_CREATE = 2
297 Const ACCT_DELETE = 4
298 Const WIN9X_UPGRADE = 16
299 Const DOMAIN_JOIN_IF_JOINED = 32
300 Const JOIN_UNSECURE = 64
301 Const MACHINE_PASSWORD_PASSED = 128
302 Const DEFERRED_SPN_SET = 256
303 Const INSTALL_INVOCATION = 262144
304 
305 strDomain = "$4"
306 strUser = "$5"
307 strPassword = "$6"
308 
309 Set objNetwork = CreateObject("WScript.Network")
310 strComputer = objNetwork.ComputerName
311 
312 Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\\" & _
313  strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
314 
315 ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, _
316  strDomain & "\" & strUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
317 EOF
318 
319 #*/ " (comentario Doxygen)
320 
321 
322 }
323 
324 
325 ### PRUEBAS.
326 
327 # ##
341 {
342 # Variables locales.
343 local MNTDIR AGENTDIR CFGFILE
344 # Si se solicita, mostrar ayuda.
345 if [ "$*" == "help" ]; then
346  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
347  "$FUNCNAME 1 1"
348  return
349 fi
350 
351 # Error si no se reciben 2 parámetros.
352 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
353 # Obtener sistema de archvios.
354 MNTDIR=$(ogMount $1 $2) || return $?
355 
356 # Comprobar si existe el fichero de configuración de OGAgent.
357 for AGENTDIR in usr/share/OGAgent "Program Files/OGAgent" "Program Files (x86)/OGAgent" Applications/OGAgent.app; do
358  CFGFILE=$(ogGetPath "$MNTDIR/$AGENTDIR/cfg/ogagent.cfg")
359  [ -n "$CFGFILE" ] && break
360 done
361 [ -n "$CFGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "ogagent.cfg" || return $?
362 # Parchear dirección del servidor OpenGnsys en el fichero de configuración de OGAgent.
363 sed -i "0,/remote=/ s,remote=.*,remote=https://$(ogGetServerIp)/opengnsys/rest/," "$CFGFILE"
364 }
365 
366 
367 # ##
382 {
383 # Variables locales.
384 local LAUNCHDIR SCRIPTDIR
385 # Si se solicita, mostrar ayuda.
386 if [ "$*" == "help" ]; then
387  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys str_scriptname" \
388  "$FUNCNAME 1 2 postconf"
389  return
390 fi
391 
392 # Error si no se reciben 3 parámetros.
393 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
394 # Comprobar directorios.
395 LAUNCHDIR=$(ogGetPath $1 $2 /Library/LaunchDaemons)
396 [ -n "$LAUNCHDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /Library/LaunchDaemons" || return $?
397 SCRIPTDIR=$(ogGetPath $1 $2 /usr/share)
398 [ -n "$SCRIPTDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /usr/share" || return $?
399 
400 # Crear fichero de configuración del servicio de arranque.
401 cat << EOT $LAUNCHDIR/es.opengnsys.$3.plist
402 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
403 <plist version="1.0">
404  <dict>
405  <key>Label</key>
406  <string>es.opengnsys.$3</string>
407  <key>ProgramArguments</key>
408  <array>
409  <string>$SCRIPTDIR/$3.sh</string>
410  </array>
411  <key>RunAtLoad</key>
412  <true/>
413  <key>StandardOutPath</key>
414  <string>/var/log/$3.log</string>
415  <key>StandardErrorPath</key>
416  <string>/var/log/$3.err</string>
417  <true/>
418  </dict>
419 </plist>
420 EOT
421 
422 # Crear un fichero de script vacío.
423 rm -f $SCRIPTDIR/$3.sh
424 touch $SCRIPTDIR/$3.sh
425 chmod +x $SCRIPTDIR/$3.sh
426 }
427 
428 
429 ### PRUEBAS.
430 
431 # ##
446 {
447 # Variables locales.
448 local SCRIPTFILE
449 # Si se solicita, mostrar ayuda.
450 if [ "$*" == "help" ]; then
451  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys str_scriptname" \
452  "$FUNCNAME 1 2 postconf \"diskutil enableJournal disk0s2\""
453  return
454 fi
455 
456 # Error si no se reciben 4 parámetros.
457 [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
458 # Comprobar que existe el fichero de comandos.
459 SCRIPTFILE=$(ogGetPath $1 $2 "/usr/share/$3.sh")
460 [ -n "$SCRIPTFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /usr/share/$3" || return $?
461 
462 # Concatenamos el comando en el fichero de comandos
463 cat >> "$SCRIPTFILE" << EOT
464 $4
465 EOT
466 }
467 
468 
469 # ##
483 {
484 # Variables locales.
485 local MNTDIR
486 # Si se solicita, mostrar ayuda.
487 if [ "$*" == "help" ]; then
488  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
489  "$FUNCNAME 1 1"
490  return
491 fi
492 
493 # Error si no se reciben 2 parámetros.
494 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
495 # Obtener sistema de archvios.
496 MNTDIR=$(ogMount $1 $2) || return $?
497 
498 # Borrar ficheros y quitar llamada al cliente durante el proceso de inicio.
499 rm -f $MNTDIR/{usr/sbin,sbin,usr/local/sbin}/ogAdmLnxClient
500 rm -f $MNTDIR/{etc,usr/local/etc}/ogAdmLnxClient.cfg
501 sed -i -e '/ogAdmLnxClient/ d' $MNTDIR/{etc,usr/local/etc}/{rc.local,rc.d/rc.local} 2>/dev/null
502 }
503 
504 
505 # ##
519 
521 {
522 # Variables locales.
523 local MNTDIR
524 # Si se solicita, mostrar ayuda.
525 if [ "$*" == "help" ]; then
526  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys str_filename" \
527  "$FUNCNAME 1 1 filename.cmd"
528  return
529 fi
530 
531 # Error si no se reciben 3 parámetros.
532 [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
533 # Obtener sistema de archvios.
534 MNTDIR=$(ogMount "$1" "$2") || return $?
535 
536 # Crear órdenes para desinstalar servicio y borrar ejecutable del cliente.
537 if [ -n "$(ogGetPath $MNTDIR/windows/ogAdmWinClient.exe)" -o -n "$(ogGetPath $MNTDIR/winnt/ogAdmWinClient.exe)" ]; then
538  ogAddCmd $1 $2 "$3" 'ogAdmWinClient -remove'
539  ogAddCmd $1 $2 "$3" 'DEL C:\Windows\ogAdmWinClient.exe'
540  ogAddCmd $1 $2 "$3" 'DEL C:\Winnt\ogAdmWinClient.exe'
541 fi
542 }
543 
function ogInstallLaunchDaemon()
Instala archivo que se ejecutará en el arranque de macOS. #.
Definition: PostConf.lib:381
function ogInstallMiniSetup()
Metafunción para instalar archivo que se ejecutará en el arranque de Windows. #.
Definition: PostConf.lib:61
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
Definition: System.lib:188
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
Definition: System.lib:153
function ogCleanOs()
Elimina los archivos que no son necesarios en el sistema operativo. #.
Definition: PostConf.lib:25
Funciones básicas del sistema. #.
function ogDeleteRegistryValue()
Elimina un valor del registro de Windows. #.
Definition: Registry.lib:176
function ogConfigureOgagent()
Modifica el fichero de configuración del nuevo agente OGAent para sistemas operativos. #.
Definition: PostConf.lib:340
function ogInstallRunonce()
Crea el archivo cmd que se ejecutara en el runonce de un usuario administrador #. ...
Definition: PostConf.lib:154
function ogUninstallWindowsClient()
Desinstala el cliente OpenGnSys para sistemas operativos Windows. #.
Definition: PostConf.lib:520
function ogGetPath()
Inicia el proceso de arranque de un sistema de archivos. #.
Definition: File.lib:237
function ogUninstallLinuxClient()
Desinstala el cliente OpenGnSys para sistemas operativos GNU/Linux. #.
Definition: PostConf.lib:482
function ogAddCmd()
Añade comandos al cmd creado por ogInstalMiniSetup #.
Definition: PostConf.lib:225
function ogInstallFirstBoot()
Crea unas claves del registro y el archivo cmd que se ejecutara en el primer arranque estando la maqu...
Definition: PostConf.lib:95
function ogDomainScript()
Crea un script .vbs para unir al dominio una maquina windows y el comando adequado en el archivo cmd ...
Definition: PostConf.lib:268
function ogDeleteFile()
Metafunción que borra un fichero de un dispositivo. #.
Definition: File.lib:167
function ogAddRegistryValue()
Añade un nuevo valor al registro de Windows, indicando su tipo de datos. #.
Definition: Registry.lib:81
function ogMount()
Definition: FileSystem.lib:704
function ogSetRegistryValue()
Establece el dato asociado a un valor del registro de Windows. #.
Definition: Registry.lib:397
function ogAddToLaunchDaemon()
Añade comandos al script creado por ogInstalLaunchDaemon. #.
Definition: PostConf.lib:445