G1sms/shell/init_keys.sh

152 lines
7.7 KiB
Bash
Executable File

#!/bin/bash
##################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.1
# License: GPL (http://www.google.com/search?q=GPL)
##################################################################
# CHECK & WARN | CREATE Keys (gpg encrypt, G1wallet, IPNS publish)
##################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
source $MY_PATH/.install/.GPATH
YOU=$(ps aux --sort=+utime | grep ipfs | tail -n 1 | cut -d " " -f 1)
# TODO Make it directory independant. (for now it is tested for pi running ipfs and code in $HOME/G1sms+
##################################################################
# G1sms+ Node KEYS (G1wallet, gpg, IPNS)
##################################################################
export GPGPASS=$(cat "/home/$YOU/.ipfs/config" | jq '.Identity.PrivKey' | sed s/\"//g)
if [ ! $GPGPASS ]; then echo "FATAL ERROR:: IPFS Node.Identity.PrivKey GPGPASS EMPTY"; exit; fi
##################################################################
# G1sms+ Swarm KEYS
##################################################################
# G1sms Service - G1Wallet
if [[ -f "${GPATH}/g1sms.pub.key" && -f "${GPATH}/g1sms.priv.key" ]]; then
chown root:root ${GPATH}/g1sms.priv.key
chmod 600 ${GPATH}/g1sms.priv.key
export MASTERPUB=$(cat "${GPATH}/g1sms.pub.key")
export MASTERKEYFILE="${GPATH}/g1sms.priv.key"
else
echo "ATTENTION!! Vous devez posséder la clef du G1 Wallet utilisé par le SWARM G1sms!!"
./shell/parle.sh "Les clefs du portefeuille essaim G1 SMS sont absente. Au revoir."
echo "Fichiers manquants:"
echo "${GPATH}/g1sms.pub.key"
echo "${GPATH}/g1sms.priv.key"
echo "Contactez nous sur https://g1sms.fr"
gammu-smsd-inject -l TEXT "$ADMINPHONE" -text "Clef de G1Wallet du SWARM g1sms.priv.key manquante! contactez-nous pour la recevoir https://g1sms.fr" 1>&2
exit
fi
##################################################################
# NODE KEYS (SCRIPT CALLED BY ROOT !!! gammu is root)
##################################################################
export IPFSNODEID=$(su $YOU -c "ipfs id -f='<id>\n'")
export NANODATE=$(date -u +%s%N) #1569692075385428020
# CHECK LAST TIME NODE HAD ACTION
if [[ -f ./wallets/.$IPFSNODEID/$IPFSNODEID.nanodate ]]; then
last=$(cat ./wallets/.$IPFSNODEID/$IPFSNODEID.nanodate)
timediff=$( echo "${NANODATE} - ${last}" | bc -l )
# Get median nanodate before
# NODE TIME SYNC 120 milliards de nanosecondes = 120s
if [[ $timediff -gt 120000000000 ]]; then
log "__SUB:ntpdate pool.ntp.org: $timediff $(ntpdate pool.ntp.org)"
export NANODATE=$(date -u +%s%N)
fi
fi
#################################################################
# CREATE NODE .$IPFSNODEID SPACE (G1Wallet + TASK + NODE DETAILS)
#################################################################
# Create G1sms Wallet with that NODE G1 wallet as member reference
if [[ ! -f "./wallets/.$IPFSNODEID/$IPFSNODEID.pub" ]]; then
mkdir -p ./wallets/.$IPFSNODEID/TASK
# TODO @poka ?? Place in install sequence: Create Node G1 Wallet (login=ipfsID pass=ipfsPrivKey)
NODEG1WALLET=$(./silkaj/silkaj generate_auth_file --auth-scrypt -salt="$IPFSNODEID" -password="$GPGPASS")
if [[ ! $NODEG1WALLET ]]; then echo "PROBLEME CREATION DU WALLET G1 DE $IPFSNODEID"; exit; fi
# Backup authfile GPG with IPFS NODE PRIVATE KEY
echo "${GPGPASS}" | gpg -q --output "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.GPGPASS.gpg" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "./authfile"
# Backup authfile CYPHER with NODE G1 WALLET PUB KEY
./shell/natools.py encrypt -p $NODEG1WALLET -i "./authfile" -o "./wallets/.$IPFSNODEID/$IPFSNODEID.authfile.crypt"
# TODO ADD ACCESS TO OTHER SWARM NODES CYPHERING WITH ALL ACTUAL NODES G1WALLET PUB KEYS
# USEFUL TO OPERATE G1TAG WITHOUT SHARING MASTERKEYFILE...
# for nodepubfile in ./wallets_swarm/.Qm*/*.pub; do
# nodeG1pub=$(cat $nodepubfile)
# nodeID=$(echo $all | cut -d '/' -f 3)
# ./shell/natools.py encrypt -p $nodeG1pub -i "./authfile" -o "./wallets/.$IPFSNODEID/$nodeID.authfile.crypt"
# done
# Keep Node G1 wallet authfile in G1sms+ root
mv -f ./authfile ../
echo $NODEG1WALLET > "./wallets/.$IPFSNODEID/$IPFSNODEID.pub"
echo $IPFSNODEID > "./wallets/.$IPFSNODEID/$IPFSNODEID.ipfsid"
log "__SUB:initkeys.sh: [ADMIN] G1sms+ CREATE G1WALLET FOR: $IPFSNODEID: pub = $NODEG1WALLET "
# BACKUP $IPFSNODEID config
./shell/natools.py encrypt -p $MASTERPUB -i "/home/$YOU/.ipfs/config" -o "./wallets/.$IPFSNODEID/$IPFSNODEID.ipfsconfig.crypt"
log "__SUB:initkeys.sh: [ADMIN] BACKUP $IPFSNODEID NODE CONFIG to ./wallets/.$IPFSNODEID/$IPFSNODEID.ipfsconfig.crypt "
#######################
# GIVE NAME TO WALLET
#######################
# 4 derniers chiffres du numéro de téléphone
TAIL=${MASTERPHONE:8:4}
# No UIDNA yet. Create new
UIDNA=$(./shell/diceware.sh 1 | xargs)
# Check if already existing among all swarm wallets
while [[ $(grep -Rwl "$UIDNA$TAIL" ./wallets_swarm/*/*.uidna) ]]; do UIDNA=$(./shell/diceware.sh 1 | xargs); done
# WRITE NODE uidna NAME TO /etc/hosts & .Qm*/Qm*.uidna file
echo "127.0.0.101 $UIDNA$TAIL" >> "/etc/hosts"
echo "$UIDNA$TAIL" > "./wallets/.$IPFSNODEID/$IPFSNODEID.uidna"
# TODO: MAYBE REMOVE THAT CREATE NODE WALLET LIKE A PHONE (Stored in wallets/.$IPFSNODEID) BUT CREATE & ATTACH G1TAG INSTEAD
echo "${GPGPASS}" > "./wallets/.$IPFSNODEID/$IPFSNODEID.pin"
echo "${GPGPASS}" | gpg -q --output "./wallets/.$IPFSNODEID/$IPFSNODEID.pin.gpg" --yes --pinentry-mode loopback --symmetric --passphrase-fd 0 "./wallets/.$IPFSNODEID/$IPFSNODEID.pin"
echo "" > "./wallets/.$IPFSNODEID/$IPFSNODEID.pin"
echo "$UIDNA$TAIL" > "./wallets/.$IPFSNODEID/$IPFSNODEID.uidna"
echo "314" > "./wallets/.$IPFSNODEID/$IPFSNODEID.g1cents"
echo $NODEG1WALLET > "./wallets/.$IPFSNODEID/$IPFSNODEID.pub"
# SILKAJ INIT G1 NODE WALLET
TX_IN=$(./silkaj/silkaj transaction --auth-file -file="$MASTERKEYFILE" --amount=3.24 --output=$NODEG1WALLET --comment="[G1sms+] G1NODE ($ADMINPSEUDO) $UIDNA$TAIL $IPFSNODEID" -y)
TX_OUT=$(./silkaj/silkaj transaction --auth-scrypt -salt="$IPFSNODEID" -password="$GPGPASS" --amount=0.1 --output=$MASTERPUB --comment="[G1sms+] G1NODE $IPFSNODEID:ACK" -y)
log "__SUB:initkeys.sh: [ADMIN] G1sms+ $ADMINPSEUDO-$UIDNA$TAIL OUVERT POUR NODE: ls ./wallet/.$IPFSNODEID"
gammu-smsd-inject -l TEXT "$ADMINPHONE" -text "[ADMIN] G1sms+ $ADMINPSEUDO-$UIDNA$TAIL OUVERT POUR NODE: ls ./wallets/.$IPFSNODEID" 1>&2
gammu-smsd-inject -l TEXT "$ADMINPHONE" -text "$NODEG1WALLET" 1>&2
fi
echo $NANODATE > ./wallets/.$IPFSNODEID/$IPFSNODEID.nanodate
################################################################
#INFORM ABOUT NODE CAPACITIES
export NODEPUB=$(cat "./wallets/.$IPFSNODEID/$IPFSNODEID.pub")
## IS IT A SMS NODE (gammu-smsd is installed)??
if [[ $G1SMS ]]; then
echo $MASTERPHONE > "./wallets/.$IPFSNODEID/$IPFSNODEID.sms"
else
if [ -f "./wallets/.$IPFSNODEID/$IPFSNODEID.sms" ]; then rm -f "./wallets/.$IPFSNODEID/$IPFSNODEID.sms"; fi
fi
## IS THERE USB PRINTER CONNECTED?
if [[ -e "/dev/usb/lp0" ]]; then
log "G1CORE: IMPRESSION G1Tag ACTIF .............."
echo $ADRESSE > "./wallets/.$IPFSNODEID/$IPFSNODEID.where"
fi
## IS THERE SERIAL QR CODE READER CONNECTED?
if [[ -e "/dev/ttyACM0" ]]; then
log "G1CORE: SCAN G1Tag ACTIF .............."
export G1TX="YES"
echo "/dev/ttyACM0" > "./wallets/.$IPFSNODEID/$IPFSNODEID.scan"
else
if [ -f "./wallets/.$IPFSNODEID/$IPFSNODEID.scan" ]; then rm -f "./wallets/.$IPFSNODEID/$IPFSNODEID.scan"; fi
fi