ring-nick-bootstrap.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. # ring beta2-2017-02-09 bootstrapped nick recovery
  3. # DBG_NO_IO=1
  4. # DBG_NO_PROMPT=1
  5. DBG_NO_LAUNCH=1
  6. # filesystem
  7. source ./toto.dat # $DUMMY_ACC_RCPT_DEV , $DUMMY_ACC_RCPT_ETH ,
  8. # $DUMMY_ACC_RCPT_ANN , $DUMMY_ACC_RCPT_SIG , $DUMMY_DEVICE
  9. readonly THIS_DIR=`cd $(dirname "$0") && pwd`
  10. readonly RING_CFG_DIR=$HOME/.config/ring
  11. readonly RING_CRED_DIR=$HOME/.local/share/ring
  12. readonly DRING_YAML_IN="$THIS_DIR/DRING_YAML_IN"
  13. readonly DRING_YAML_IN_TEXT="`cat $THIS_DIR/DRING_YAML_IN`"
  14. readonly DRING_YAML_OUT="$THIS_DIR/DRING_YAML_OUT"
  15. readonly RING_UID_REGEX='CN = Ring, UID ='
  16. readonly HEX_DIGITS_REGEX='^[0-9a-fA-F]*$'
  17. readonly SELECT_ACCOUNT_PROMPT="select an account to restore:"
  18. declare -A RingUids=()
  19. function IsHex() # a_string
  20. {
  21. [ "$1" ] && echo "$1" | grep -e $HEX_DIGITS_REGEX > /dev/null ; return $? ;
  22. }
  23. function Backup()
  24. {
  25. timestamp=`date +%s.%N`
  26. # create backups
  27. if [ -d $RING_CRED_DIR ]
  28. then credentials_backup_dirname="$HOME-backup-ring-$timestamp"
  29. [ "$DBG_NO_IO" ] || \
  30. cp -r $RING_CRED_DIR $credentials_backup_dirname
  31. echo "created credentials backup directory: $credentials_backup_dirname"
  32. else mkdir "$RING_CRED_DIR" && echo "created directory: $RING_CRED_DIR"
  33. fi
  34. if [ -d $RING_CFG_DIR ]
  35. then config_backup_dirname="$HOME-backup-dring-$timestamp"
  36. [ "$DBG_NO_IO" ] || \
  37. cp -r $RING_CFG_DIR $config_backup_dirname
  38. echo "created config backup directory: $config_backup_dirname"
  39. else mkdir "$RING_CFG_DIR" && echo "created directory: $RING_CFG_DIR"
  40. fi
  41. }
  42. function SanityCheck()
  43. {
  44. for cert_file in `ls "$RING_CRED_DIR/certificates"` ; do echo "cert_file=$cert_file" ; done ;
  45. for crls_file in `ls "$RING_CRED_DIR/crls"` ; do echo "crls_file=$crls_file" ; done ;
  46. for cred_dir in `ls -d $RING_CRED_DIR/*/ | grep -v -E "certificates|crls"` ; do cred_dirname=`basename $cred_dir` ; printf " cred_dirname=$cred_dirname - " ; IsHex "$cred_dirname" || printf "!" ; printf "IsHex('$cred_dirname')=>$?\n" ; done ;
  47. declare -a cert_files=()
  48. declare -a crls_files=()
  49. for cert_file in `ls "$RING_CRED_DIR/certificates"` ; do cert_files=(${cert_files[@]} $cert_file) ; done ;
  50. for crls_file in `ls "$RING_CRED_DIR/crls"` ; do crls_files=(${crls_files[@]} $crls_file) ; done ;
  51. for cred_dir in `ls -d $RING_CRED_DIR/*/ | grep -v -E "certificates|crls"`
  52. do missing=''
  53. cred_dir=${cred_dir:0:-1}
  54. cred_dirname=`basename $cred_dir`
  55. IsHex "$cred_dirname" || (echo "!IsHex - ignoring: $cred_dirname" ; return)
  56. cd "$cred_dir"
  57. [ -f './export.gz' ] || missing="$missing (export.gz)"
  58. [ -f './ring_device.crt' -o -f './dht.crt' ] || missing="$missing (ring_device.crt or dht.crt)"
  59. [ -f './ring_device.key' -o -f './dht.key' ] || missing="$missing (ring_device.key or dht.key)"
  60. if [ -z "$missing" ]
  61. then detected_version=-1
  62. [ -f 'ca.key' ] && detected_version=0
  63. [ -f 'knownDevices' -a -f 'dht.crt' ] && detected_version=1
  64. [ -f 'knownDevicesNames' -a -f 'ring_device.crt' ] && detected_version=2
  65. echo "detected_version=$detected_version"
  66. RingUids[$cred_dir]=$detected_version
  67. else echo "$cred_dir missing file(s): $missing"
  68. fi
  69. done
  70. echo "(${#cert_files[@]}) files found in $RING_CRED_DIR/certificates/"
  71. echo "(${#crls_files[@]}) files found in $RING_CRED_DIR/crls/"
  72. echo "(${#RingUids[@]}) valid candidate directories found under $RING_CRED_DIR/"
  73. [ -d $RING_CRED_DIR -a -d $RING_CFG_DIR ] || (err=42 && echo "cannot find or create ring directories - quitting")
  74. ((${#RingUids[@]})) || (err=43 && echo "cannot find any ring credentials - quitting")
  75. (($err)) && return $err || return 0
  76. }
  77. function ParseUids()
  78. {
  79. # find all dht.crt or ring_device.crt
  80. for cred_path in ${!RingUids[@]} ; do ParseUid `ls $cred_path/*.crt` ; done ;
  81. printf "\nRingUids[${#RingUids[@]}]=" ; for cred_path in ${!RingUids[@]} ; do printf "\n RingUids[$cred_path] => ${RingUids[$cred_path]}" ; done ; printf "\n\n" ;
  82. }
  83. function ParseUid() # (cert_file)
  84. {
  85. cert_file=$1
  86. cred_dirpath=`dirname $cert_file`
  87. cred_dirname=`basename $(dirname $cert_file)`
  88. echo -e "\nparsing cert_file=$cert_file" ; # [ -f "$cert_file" ] && echo cert_file || echo !cert_file
  89. # echo "cred_dirname=$cred_dirname" ; # IsHex "$cred_dirname" && echo cred_dirname || echo !cred_dirname
  90. [ -f "$cert_file" ] && IsHex "$cred_dirname" || return
  91. declare -a line_buf=(`openssl x509 -in $cert_file -text -noout | grep "$RING_UID_REGEX"`)
  92. ring_uid=${line_buf[$((${#line_buf[@]} - 1))]}
  93. IsHex "$ring_uid" || return
  94. # cache results
  95. credentials_version=${RingUids[$cred_dirpath]}
  96. [ "$credentials_version" -gt "0" ] && RingUids[$cred_dirpath]="$ring_uid"
  97. [ "$credentials_version" != '' ] && printf "parsed ring_uid=$ring_uid in $cred_dirname - "
  98. [ "$credentials_version" -gt "0" ] && printf "v$credentials_version\n" || printf "unknown version - ignoring\n"
  99. }
  100. function SelectAccount()
  101. {
  102. # prompt user to generate config files
  103. declare -a cred_paths
  104. for cred_path in "${!RingUids[@]}" ; do cred_paths=(${cred_paths[@]} $cred_path) ; done ;
  105. ((${#cred_paths[@]})) && echo "the following account directories were found:"
  106. for cred_path_n in "${!cred_paths[@]}" ; do echo " $(($cred_path_n+1))) ${cred_paths[$cred_path_n]}" ; done ;
  107. [ "$DBG_NO_PROMPT" ] && echo "$SELECT_ACCOUNT_PROMPT - *debug no prompt*" && GenerateConfig "${cred_paths[0]}" && return
  108. cred_path_n=-1
  109. n_files=${#cred_paths[@]}
  110. until [ "$cred_path_n" -ge "0" -a "$cred_path_n" -lt "$n_files" ]
  111. do printf "$SELECT_ACCOUNT_PROMPT (1-$(($n_files)), 0 to cancel): " ; read cred_path_n ;
  112. done
  113. [ "$cred_path_n" -gt "0" ] && GenerateConfig "${cred_paths[$(($cred_path_n-1))]}"
  114. }
  115. function GenerateConfig() # cred_path - e.g. $RING_CRED_DIR/0123456789ABCDEF'
  116. {
  117. cred_path=$1
  118. cred_dirname=`basename $cred_path 2> /dev/null`
  119. echo -e "\n--GenerateConfig()>" ; IsHex "$cred_dirname" && [ "$ring_uid" -a -d "$cred_path" ] || echo -e "invalid state for cred_dirname=$cred_dirname\n<GenerateConfig()--"
  120. IsHex "$cred_dirname" && [ "$ring_uid" -a -d "$cred_path" ] || return
  121. ring_uid=${RingUids[$cred_path]}
  122. echo "compiling configuration file for '$cred_path' ($ring_uid)"
  123. # export to disk
  124. dring_yaml_out="$DRING_YAML_OUT-$cred_dirname"
  125. repalce_regex="s|#LOCAL_DIRNAME_HERE#|$cred_dirname|g ; \
  126. s|#LOCAL_PATH_HERE#|$cred_dirpath|g ; \
  127. s|#RING_UID_HERE#|$ring_uid| ; \
  128. s|#DUMMY_ACC_RCPT_DEV_HERE#|$DUMMY_ACC_RCPT_DEV| ; \
  129. s|#DUMMY_ACC_RCPT_ETH_HERE#|$DUMMY_ACC_RCPT_ETH| ; \
  130. s|#DUMMY_ACC_RCPT_ANN_HERE#|$DUMMY_ACC_RCPT_ANN| ; \
  131. s|#DUMMY_ACC_RCPT_SIG_HERE#|$DUMMY_ACC_RCPT_SIG| ; \
  132. s|#DUMMY_DEVICE#|$DUMMY_DEVICE|"
  133. [ "$DBG_NO_IO" ] || \
  134. sed -e "$repalce_regex" "$DRING_YAML_IN" > "$dring_yaml_out"
  135. echo "wrote config file: $dring_yaml_out"
  136. echo "<GenerateConfig()--"
  137. }
  138. function Launch()
  139. {
  140. [ "$DBG_NO_LAUNCH" ] && return
  141. # save config file identically three times:
  142. $HOME/.config/ring/dring.yml
  143. $HOME/.config/ring/_dring.yml
  144. $HOME/.config/ring/dring.yml.bak
  145. # launch fresh daemon then client
  146. killall gnome-ring ; while (($?)) ; do killall gnome-ring ; done ;
  147. killall dring ; while (($?)) ; do killall dring ; done ;
  148. cd /usr/lib/ring/dring
  149. ./dring --debug --console &
  150. gnome-ring --debug --version
  151. }
  152. function Main
  153. {
  154. SanityCheck ; (($?)) && exit ;
  155. ParseUids
  156. SelectAccount
  157. Launch
  158. }
  159. # Main
  160. function DbgMain
  161. {
  162. echo -e "\n\n\n\n\n\n\n\n====== in ======"
  163. Main
  164. echo -e "====== out ======"
  165. }
  166. DbgMain ; DEBUG=0 ; while (($DEBUG)) ; do sleep 5 ; DbgMain ; done ;