Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Export Password Store (aka pass) passwords to a KeePass compatible imports file.
  4. # Note: this does not respect the folder structure (yet). Also, multiline notes are not exported correctly.
  5. #
  6. # Author: Karolis Kazlauskis
  7. # 2017
  8.  
  9. entries='' # pass entries found
  10.  
  11. # loop through every file in the dir
  12. FILES=()
  13. while read file; do
  14. password=''
  15. login=''
  16. user=''
  17. url=''
  18. notes=''
  19.  
  20. # clean up the file name
  21. file=$(sed 's/.gpg//g' <<< $file)
  22. file=$(sed 's/.*\.password-store\///g' <<< $file)
  23.  
  24. # set title of the key
  25. title=$(sed 's/.*\///g' <<< $file)
  26.  
  27. echo "\n $title ($file)"
  28. info=($(pass "$file"))
  29. echo "${info[@]}"
  30.  
  31. # extract the parts of the key
  32. password=${info[@]:0:1}
  33.  
  34. info=("${info[@]:1}")
  35.  
  36. shopt -s nocasematch
  37. if [ "${info[0]}" == 'login:' ]; then
  38. login=${info[1]}
  39. elif [ "${info[0]}" == 'UserName:' ]; then
  40. user=${info[1]}
  41. elif [ "${info[0]}" == 'Notes:' ]; then
  42. notes=${info[1]}
  43. elif [ "${info[0]}" == 'url:' ] || [ "${info[0]}" == 'URL:' ]; then
  44. url=${info[1]}
  45. fi
  46.  
  47. info=("${info[@]:1}")
  48.  
  49. info=("${info[@]:1}")
  50.  
  51. if [ "${info[0]}" == 'login:' ]; then
  52. login=${info[1]}
  53. elif [ "${info[0]}" == 'UserName:' ]; then
  54. user=${info[1]}
  55. elif [ "${info[0]}" == 'Notes:' ]; then
  56. notes=${info[1]}
  57. elif [ "${info[0]}" == 'url:' ] || [ "${info[0]}" == 'URL:' ]; then
  58. url=${info[1]}
  59. fi
  60.  
  61. info=("${info[@]:1}")
  62.  
  63. info=("${info[@]:1}")
  64.  
  65. if [ "${info[0]}" == 'login:' ]; then
  66. login=${info[1]}
  67. elif [ "${info[0]}" == 'UserName:' ]; then
  68. user=${info[1]}
  69. elif [ "${info[0]}" == 'Notes:' ]; then
  70. notes=${info[1]}
  71. elif [ "${info[0]}" == 'url:' ] || [ "${info[0]}" == 'URL:' ]; then
  72. url=${info[1]}
  73. fi
  74.  
  75.  
  76. info=("${info[@]:1}")
  77.  
  78. info=("${info[@]:1}")
  79.  
  80. if [ "${info[0]}" == 'login:' ]; then
  81. login=${info[1]}
  82. elif [ "${info[0]}" == 'UserName:' ]; then
  83. user=${info[1]}
  84. elif [ "${info[0]}" == 'Notes:' ]; then
  85. notes=${info[1]}
  86. elif [ "${info[0]}" == 'url:' ] || [ "${info[0]}" == 'URL:' ]; then
  87. url=${info[1]}
  88. fi
  89.  
  90. # escape xml
  91. password=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\&#39;/g' <<< ${password})
  92. login=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\&#39;/g' <<< ${login})
  93. user=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\&#39;/g' <<< ${user})
  94. url=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\&#39;/g' <<< ${url})
  95. notes=$(sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\&#39;/g' <<< ${notes})
  96.  
  97. # add to the entries
  98. entries+=$(cat << EndOfMessage
  99. <Entry>
  100. <UUID>$(uuidgen)</UUID>
  101.  
  102. <String>
  103. <Key>Title</Key>
  104. <Value>${title}</Value>
  105. </String>
  106. <String>
  107. <Key>UserName</Key>
  108. <Value>${user}</Value>
  109. </String>
  110. <String>
  111. <Key>Password</Key>
  112. <Value ProtectInMemory="True">${password}</Value>
  113. </String>
  114. <String>
  115. <Key>URL</Key>
  116. <Value>${url}</Value>
  117. </String>
  118. <String>
  119. <Key>Notes</Key>
  120. <Value>${notes}</Value>
  121. </String>
  122. </Entry>
  123. EndOfMessage
  124. )
  125. done <<< "$(find ~/.password-store/ -name '*.gpg' )"
  126.  
  127. # write it to the file
  128. cat > exports.xml << EndOfMessage
  129. <?xml version="1.0" encoding="UTF-8"?>
  130. <KeePassFile>
  131. <Meta>
  132. <DatabaseName>Database</DatabaseName>
  133. </Meta>
  134. <Root>
  135. <Group>
  136. <UUID>$(uuidgen)</UUID>
  137. <Name>Imports</Name>
  138.  
  139. ${entries}
  140.  
  141. </Group>
  142. </Root>
  143. </KeePassFile>
  144. EndOfMessage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement