Advertisement
Frannoe

PubKey

Sep 16th, 2012
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.54 KB | None | 0 0
  1. #!/bin/bash
  2. # PubKey 1.0
  3. # Script(Gui)to Import PubKey APT Keys
  4. # By Frannoe@gmail.com (Francisco Luque)
  5. # http://frannoe.blogspot.com.es/
  6. # Este Script es para sistemas basados en Debian y añade de forma totalmente automática las Llaves
  7. # de los repositorios que no se encuentre en Orígenes de Software y que por lo tanto causan error a la hora de actualizar.
  8. # Tambiénm podemos eliminar Llaves o ver información sobre éstas
  9. # Este Script necesita de YAD (>= 0.25.1) para su correcto funcionamiento.
  10. # Esta herramienta es totalmente Libre y puede modificarla y mejorarla tanto como quiera.
  11.  
  12. export EXITINFO=" --fixed --width=350 --buttons-layout=center --button=Ok:0 "
  13.  
  14.  
  15. function importkey () {
  16.  #yad --info --fixed --width "400" --buttons-layout=center --button=Salir:1 --button=Continuar:0 --title "PubKeys" --text-align=center --text "Se van a actualizar sus repositorios. Al realizar este proceso se comprobará también que todas las Llaves públicas de estos se encuentren disponibles. Si no es así, recibirá un mensaje advirtiéndole de ello. Podrá luego decidir si quiere o no, importar la llave que necesite.\n¿Desea continuar?."
  17. #if [ $? != 0 ]; then
  18. #rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  19. #exit
  20. #fi
  21.  
  22. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  23. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  24.  
  25. function updatrepor
  26. {
  27. dir / >> /tmp/tmprogpubkey
  28. tail -f /tmp/tmprogpubkey | yad --fixed --width "400" --title "  $2  " --text-align=center --text "  $3  " --no-buttons --progress --pulsate --auto-close &
  29. case "$1" in
  30. 1)
  31.   LC_ALL=C apt-get update >> /dev/null 2> /tmp/pubkeys
  32. cp -f /tmp/pubkeys /tmp/allremovekeys
  33. auxruta="/tmp/pubkeys"
  34.                    if [ $? != 0 ]; then  
  35. killall -KILL tail
  36. yad --info --fixed --width=450 --buttons-layout=center --button=Ok:0 --text-align=center --title "Error al ejecutar Pubkey" --text-align=center --text "Ha ocurrido un error al intentar actualizar los repositorios. Esto puede ser debido a varias causas:\n  -- No ha ejecutado el Script con permisos de Administrador\n  -- Tiene abierto algún gestor de repositorios tipo:\nSynaptic\nGestor de Actualizaciones\nOrígenes de Software, etc.\n  -- No tiene conexión a la Red.\nCompruebe alguno de estos supuesto y vuelva a intentarlo."
  37.                            exit
  38.                          fi
  39. sed -i '/NO_PUBKEY/!d' /tmp/pubkeys
  40. ;;
  41. 2)
  42.      while read puckselecc
  43.           do
  44.        key=${puckselecc: -8}
  45.        errkey=${puckselecc: -16}
  46.        exp1=` LC_ALL=C gpg --keyserver keyserver.ubuntu.com --recv-key $key && gpg -a --export $key | apt-key add - | awk '{print $1}'`
  47.                     if [ "$exp1" != "OK" ]; then
  48.        exp1=` LC_ALL=C gpg --keyserver subkeys.pgp.net --recv-key $key && gpg -a --export $key | apt-key add - | awk '{print $1}'`
  49.                     fi
  50.                     if [ "$exp1" != "OK" ]; then
  51.  
  52.                             killall -KILL tail
  53. yad --info $EXITINFO --title "$errkey" --text-align=center --text "Ha ocurrido un error al intentar importar la Llave:\n$errkey\nAsegúrese de que su equipo se encuentre conectado a la Red."
  54. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  55.                            exit
  56.                           fi
  57. done < $auxruta
  58. QUES=$?
  59. sleep 1
  60. killall -KILL tail
  61. if [ $QUES != 0 ]; then
  62. yad --info EXITINFO --title "Error..." --text-align=center --text "Error inesperado. Se saldra del programa."
  63. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  64. exit
  65. else
  66. yad --info --buttons-layout=center --button=Salir:1 --button=Actualizar:0 --title "Importación exitosa." --text-align=center --text "Las Llaves han sido añadidas con éxito. Ahora es recomendable actualizar nuevamente los repositorios."
  67. if [ $? != 0 ]; then
  68. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  69. exit
  70. fi
  71. fi
  72. ;;
  73. esac
  74. sleep 1
  75. killall -KILL tail
  76. }
  77. updatrepor 1 "Actualizando..." "Actualizando el Sistema y buscando repositorios sin Llaves. Esto puede tardar un poco, espere por favor..."
  78.  if [ -s /tmp/pubkeys ]; then
  79. while read encon
  80. do
  81. cont=`expr $cont + 1`
  82. KEYS+=("")
  83. KEYS+=("$(echo $cont"- " $encon | awk '{print $1,$2,$3,$4,$5,$6,$(NF-0)}')")
  84. done < /tmp/pubkeys
  85.  
  86. QUES=""
  87. until [[ "$selecc" !=  "" || $QUES = 2 ]]
  88. do
  89. selecc=`yad --buttons-layout=center --button=Salir:1 --button="Importar todas":2 --button="Importar seleccionadas":0 --list --title "Listado de repositorios sin Llave..." --height "200" --width "600" --text-align=center --text "Seleccione de la lista de abajo las Llaves que quiera importar:" --checklist --column "Select" --column "'$cont' Llaves pendientes de importar" "${KEYS[@]}" --no-click`
  90. QUES=$?
  91. if [ $QUES = 1 ]; then
  92. exit
  93. fi
  94. done
  95.  
  96. if [ $QUES = 2 ]; then
  97. auxruta="/tmp/allremovekeys"
  98. else
  99. echo $selecc | sed -e 's/TRUE|//g' -e 's/|/\n/g' | sed -e 's/^ //g' -e '$d' > /tmp/pubkeys
  100. fi
  101.  
  102. updatrepor 2 "Importando Llaves..." "Importando las Llaves seleccionadas, espere por favor..."
  103.  
  104. apt-get update | yad --fixed --width "500" --title "Actualizando..." --text-align=center --text "Poniendo al día los repositorios, esto puede tardar un poco, espere por favor..." --no-buttons --progress --pulsate --auto-close
  105.                
  106.            if [ $? != 0 ]; then
  107.  
  108. sleep 1
  109. killall -KILL tail
  110. yad --info $EXITINFO --title "Error" --text-align=center --text "Ha ocurrido un error al intentar actualizar nuevamente los repositorios."
  111. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  112.                           exit
  113.                         else
  114. sleep 1
  115. killall -KILL tail
  116. yad --info $EXITINFO --title "Actualización" --text-align=center --text "Los repositorios han sido actualizados con éxito."
  117. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  118. exit
  119.                     fi
  120.       else
  121. yad --info $EXITINFO --title "PubKey" --text-align=center --text "Sus repositorios están correctos y no es necesaria ninguna Llave."
  122. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  123.         exit
  124. fi
  125. }
  126. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  127. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  128. function deletekey ()
  129. {
  130. apt-key update
  131. sleep 1
  132. #gpg --no-default-keyring --keyring "/etc/apt/trusted.gpg" --list-keys | grep -e pub -e uid | awk '{print $0 }'| sed '1d' > /tmp/delpubkey
  133. LC_ALL=C gpg -k | grep -e pub -e uid | awk '{print $0 }'| sed '1d' > /tmp/delpubkey
  134. dupl="x"
  135. while [ "$dupl" != "" ]
  136. do
  137. dupl=$(awk 'x==$1{print NR; while(getline)(x!=$1)}{x=$1}' /tmp/delpubkey)
  138. if [ "$dupl" != "" ]; then
  139. sed -i ''$dupl'd' /tmp/delpubkey
  140. fi
  141. done
  142.  
  143. #apt-key list | grep -e uid | awk '{print $0 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g" | cut -d "<" -f 1 | sort -u > /tmp/delpubkey
  144. uids=$(cat /tmp/delpubkey | grep -e uid | awk '{print $0 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g" | cut -d "<" -f 1 | uniq)
  145. pubs=$(cat /tmp/delpubkey | grep -e pub | awk '{print $2 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g"| cut -d "/" -f 2)
  146.  
  147. paste <(printf "$uids") <(printf "$pubs") > /tmp/delpubkey
  148.  
  149. while read delkeys
  150. do
  151. cont=`expr $cont + 1`
  152. list+=("")
  153. list+=("$(echo $cont"- " $delkeys)")
  154. done < /tmp/delpubkey
  155.  
  156. key=""
  157. QUES=""
  158. until [ "$delselecc" !=  "" ]
  159. do
  160. delselecc=`yad --button=Salir:1 --button="Eliminar seleccionadas":0 --buttons-layout=center --list --title "Listado de llaves públicas" --height "600" --width "600" --text-align=center --text "Listado de llaves públicas encontradas en el sistema." --checklist --column "" --column "Llaves posibles de eliminar" "${list[@]}" --no-click`
  161.  
  162. QUES=$?
  163. if [ $QUES != 0 ]; then
  164. rm -f /tmp/delpubkey
  165. exit
  166. fi
  167. done
  168. QUES=""
  169. yad --info --fixed --width=450 --buttons-layout=center --button=Abortar:1 --button=Continuar:0 --title "Listados de Llaves públicas" --text-align=center --text "Confirme que desea eliminar las llaves seleccionadas."
  170. QUES=$?
  171. if [ $QUES != 0 ]; then
  172. rm -f /tmp/delpubkey
  173. exit
  174. fi
  175.  
  176. echo $delselecc | sed -e 's/TRUE|//g' -e 's/|/\n/g' | sed -e 's/^ //g' -e '$d' > /tmp/delpubkey
  177.  
  178. while read puckdelselecc
  179.           do
  180.  
  181.        delkey=${puckdelselecc: -8}
  182.              errdelkey=`apt-key del $delkey && gpg --batch --yes --delete-key $delkey | awk '{print $1}'`
  183.                  
  184.                   if [ "$errdelkey" != "OK" ]; then
  185.  
  186.                             killall -KILL tail
  187. yad --info $EXITINFO --title "$delkey" --text-align=center --text "Ha ocurrido un error al intentar eliminar la Llave:\n$delkey"
  188. rm -f /tmp/delpubkey
  189.                            exit
  190. else
  191. yad --info $EXITINFO --title "Llave eliminada..." --text-align=center --text "Llave <b>$delkey</b> ha sido eliminada correctamete."
  192. rm -f /tmp/delpubkey
  193.  
  194.                           fi
  195.  
  196. done < /tmp/delpubkey
  197.  
  198. apt-key update
  199. sleep 1
  200.  
  201. }
  202.  
  203. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  204. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  205.  
  206. function infokey ()
  207. {
  208.  
  209. apt-key update
  210. sleep 1
  211. #gpg --no-default-keyring --keyring "/etc/apt/trusted.gpg" --list-keys | grep -e pub -e uid | awk '{print $0 }'| sed '1d' > /tmp/delpubkey
  212. LC_ALL=C gpg -k | grep -e pub -e uid | awk '{print $0 }'| sed '1d' > /tmp/delpubkey
  213. dupl="x"
  214. while [ "$dupl" != "" ]
  215. do
  216. dupl=$(awk 'x==$1{print NR; while(getline)(x!=$1)}{x=$1}' /tmp/delpubkey)
  217. if [ "$dupl" != "" ]; then
  218. sed -i ''$dupl'd' /tmp/delpubkey
  219. fi
  220. done
  221.  
  222. #apt-key list | grep -e uid | awk '{print $0 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g" | cut -d "<" -f 1 | sort -u > /tmp/delpubkey
  223. uids=$(cat /tmp/delpubkey | grep -e uid | awk '{print $0 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g" | cut -d "<" -f 1 | uniq)
  224. pubs=$(cat /tmp/delpubkey | grep -e pub | awk '{print $2 }' | sed -e "s/pub//g" -e "s/uid//g" -e "s/^ *//g"| cut -d "/" -f 2)
  225.  
  226. paste <(printf "$uids") <(printf "$pubs") > /tmp/delpubkey
  227.  
  228. while read infokeys
  229. do
  230. cont=`expr $cont + 1`
  231. list+=("")
  232. list+=("$(echo $cont"- " $infokeys)")
  233. done < /tmp/delpubkey
  234.  
  235. key=""
  236. QUES=""
  237. until [ "$infoselecc" !=  "" ]
  238. do
  239. infoselecc=`yad --button=Salir:1 --buttons-layout=center  --title "Listado de llaves públicas" --height "600" --width "600" --text-align=center --text "<b>Doble-clic para ver más información sobre una Llave.</b>" --list --column=STRING:HD --column "Llaves encontradas" "${list[@]}" --no-click`
  240.  
  241. QUES=$?
  242. if [ $QUES != 0 ]; then
  243. rm -f /tmp/delpubkey
  244. exit
  245. fi
  246. infoselecc=`echo $infoselecc | awk '{print $NF}' | sed -e "s/|//g"`
  247. LC_ALL=C gpg --fingerprint $infoselecc  > /tmp/delpubkey
  248. sed -i -e "s/pub/Pub = /g" -e "s/sub/Sub = /g" -e "s/uid/UID = /g" -e "s/ [ ]*/ /g" /tmp/delpubkey
  249.  
  250. yad  --buttons-layout=center --width=550 --height=200  --button="Salir":1  --title "Llave: $infoselecc" --text-align=center --text-info --text "Datos sobre la Llave seleccionada: <b>$infoselecc</b>" --filename=/tmp/delpubkey
  251. rm -f /tmp/delpubkey
  252. done
  253.  
  254. }
  255. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  256. #HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  257.  
  258. export -f importkey
  259. export -f deletekey
  260. export -f infokey
  261. `yad --fixed --width "400" --buttons-layout=center  --button="Salir":1  --text-align=center --title "PubKey 1.0" --text "<b>Pulse la opción que necesite procesar:</b>" \
  262. --form \
  263. --field=:LBL '' \
  264. --field=:LBL '' \
  265. --field="Añadir llaves públicas":BTN 'bash -c "importkey"' \
  266. --field=:LBL '' \
  267. --field="Ver listado de Llaves públicas":BTN  'bash -c "infokey"' \
  268. --field=:LBL '' \
  269. --field="Eliminar llaves públicas":BTN  'bash -c "deletekey"' \
  270. --field=:LBL '' \
  271. --field=:LBL ''`
  272.  
  273. if [ $? != "0" ]; then
  274.  
  275. rm -f /tmp/pubkeys /tmp/tmprogpubkey /tmp/allremovekeys
  276. exit
  277. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement