You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.9 KiB

  1. --- a/utils/aa-decode
  2. +++ b/utils/aa-decode
  3. @@ -1,4 +1,4 @@
  4. -#!/bin/bash
  5. +#!/bin/sh
  6. #
  7. # Copyright (C) 2009-2010, 2012 Canonical Ltd.
  8. # Copyright (C) 2012 Christian Boltz
  9. @@ -16,8 +16,6 @@
  10. # along with this program; if not, contact Canonical, Ltd.
  11. #
  12. -set -e
  13. -
  14. help() {
  15. cat <<EOM
  16. USAGE: aa-decode [OPTIONS] <encoded string>
  17. @@ -36,13 +34,15 @@ $ cat /var/log/kern.log | aa-decode
  18. EOM
  19. }
  20. -decode() {
  21. - if echo "$1" | egrep -q "^[0-9A-Fa-f]+$" ; then
  22. - python3 -c "import binascii; print(bytes.decode(binascii.unhexlify('$1'), errors='strict'));"
  23. - else
  24. - echo ""
  25. - fi
  26. +match_re() {
  27. + local result=$(echo "$1" | grep -E "$2" )
  28. + [ -z "$result" ] && return 1 || return 0
  29. +}
  30. +
  31. +decode() {
  32. + $(echo "$1" | egrep -q "^[0-9A-Fa-f]+$"); [ "$?" -eq 0 ] &&
  33. + python3 -c "import binascii; print(bytes.decode(binascii.unhexlify('$1'), errors='strict'));" || echo ""
  34. }
  35. if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  36. @@ -51,47 +51,61 @@ if [ "$1" = "-h" ] || [ "$1" = "--help"
  37. fi
  38. # if have an argument, then use it, otherwise process stdin
  39. -if [ -n "$1" ]; then
  40. - e="$1"
  41. - if ! echo "$e" | egrep -q "^[0-9A-Fa-f]+$" ; then
  42. - echo "String should only contain hex characters (0-9, a-f, A-F)"
  43. - exit 1
  44. - fi
  45. -
  46. - d=`decode $e`
  47. - if [ -z "$d" ]; then
  48. - echo "Could not decode string"
  49. - exit 1
  50. - fi
  51. +[ -n "$1" ] && {
  52. - echo "Decoded: $d"
  53. - exit 0
  54. -fi
  55. + e="$1"
  56. -# For now just look at 'name=...' and 'profile=...',
  57. -# so validate input against this and output based on it.
  58. -# TODO: better handle other cases too
  59. -while read line ; do
  60. + $(echo "$e" | egrep -q "^[0-9A-Fa-f]+$"); [ "$?" -ne 0 ] && {
  61. + echo "String should only contain hex characters (0-9, a-f, A-F)"
  62. + exit 1
  63. + }
  64. - # check if line contains encoded name= or profile=
  65. - if [[ "$line" =~ \ (name|profile|proctitle)=[0-9a-fA-F] ]]; then
  66. + d=$(decode $e)
  67. - # cut the encoded filename/profile name out of the line and decode it
  68. - ne=`echo "$line" | sed 's/.* name=\([^ ]*\).*$/\\1/g'`
  69. - nd="$(decode ${ne/\'/\\\'})"
  70. + [ -z "$d" ] && {
  71. + echo "Could not decode string"
  72. + exit 1
  73. + }
  74. - pe=`echo "$line" | sed 's/.* profile=\([^ ]*\).*$/\\1/g'`
  75. - pd="$(decode ${pe/\'/\\\'})"
  76. + echo "Decoded: $d"
  77. + exit 0
  78. +}
  79. - pce=`echo "$line" | sed 's/.* proctitle=\([^ ]*\).*$/\\1/g'`
  80. - pcd="$(decode ${pce/\'/\\\'})"
  81. +[ -t 0 ] && {
  82. + help
  83. + exit
  84. +}
  85. +
  86. +while read line ; do
  87. - # replace encoded name and profile with its decoded counterparts (only if it was encoded)
  88. - test -n "$nd" && line="${line/name=$ne/name=\"$nd\"}"
  89. - test -n "$pd" && line="${line/profile=$pe/profile=\"$pd\"}"
  90. - test -n "$pcd" && line="${line/proctitle=$pce/proctitle=\"$pcd\"}"
  91. + # check if line contains encoded name= or profile=
  92. - fi
  93. + matches=0
  94. + match_re "$line" "^[[:blank:]](name|profile|proctitle)=[0-9a-fA-F]+"; [ "$?" -eq 0 ] && matches=1 || {
  95. + match_re "$line" "^(name|profile|proctitle)=[0-9a-fA-F]+"; [ "$?" -eq 0 ] && matches=1
  96. + }
  97. +
  98. + [ "$matches" -eq 0 ] || {
  99. +
  100. + # cut the encoded filename/profile name out of the line and decode it
  101. + ne=$(echo "$line" | sed 's/.* name=\([^ ]*\).*$/\\1/g')
  102. + [ "$line" = "$ne" ] && ne=$(echo "$line" | sed 's/.*name=\([^ ]*\).*$/\\1/g')
  103. + echo var: $ne
  104. + nd="$(decode ${ne/\'/\\\'})"
  105. +
  106. + pe=$(echo "$line" | sed 's/.* profile=\([^ ]*\).*$/\\1/g')
  107. + [ "$line" = "$pe" ] && pe=$(echo "$line" | sed 's/.*profile=\([^ ]*\).*$/\\1/g')
  108. + pd="$(decode ${pe/\'/\\\'})"
  109. +
  110. + pce=$(echo "$line" | sed 's/.* proctitle=\([^ ]*\).*$/\\1/g')
  111. + [ "$line" = "$pce" ] && pce=$(echo "$line" | sed 's/.*proctitle=\([^ ]*\).*$/\\1/g')
  112. + pcd="$(decode ${pce/\'/\\\'})"
  113. +
  114. + # replace encoded name and profile with its decoded counterparts (only if it was encoded)
  115. + test -n "$nd" && line="${line/name=$ne/name=\"$nd\"}"
  116. + test -n "$pd" && line="${line/profile=$pe/profile=\"$pd\"}"
  117. + test -n "$pcd" && line="${line/proctitle=$pce/proctitle=\"$pcd\"}"
  118. + }
  119. echo "$line"