coffin

secure lan file storage on a device
git clone git://parazyd.org/coffin.git
Log | Files | Refs | Submodules | README | LICENSE

hooks (3504B)


      1 #!/usr/bin/env zsh
      2 #
      3 # Copyright (c) 2016 Dyne.org Foundation
      4 # coffin is written and maintained by Ivan J. <parazyd@dyne.org>
      5 #
      6 # This file is part of coffin
      7 #
      8 # This source code is free software: you can redistribute it and/or modify
      9 # it under the terms of the GNU General Public License as published by
     10 # the Free Software Foundation, either version 3 of the License, or
     11 # (at your option) any later version.
     12 #
     13 # This software is distributed in the hope that it will be useful,
     14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 # GNU General Public License for more details.
     17 #
     18 # You should have received a copy of the GNU General Public License
     19 # along with this source code. If not, see <http://www.gnu.org/licenses/>.
     20 
     21 check-hooks() {
     22 	## TODO: fragmented keys, delete, backup, moar
     23 	fn check-hooks
     24 	freq=(HOOKS)
     25 	ckreq || return 1
     26 
     27 	local line=0
     28 	for entry in $(cat $HOOKS); do
     29 		let line=$line+1
     30 		act "Found hook $line..."
     31 
     32 		## Check what's hook supposed to do
     33 		if [[ ${entry[(ws@:@)1]} == "create" ]]; then
     34 			create-new-tomb
     35 		elif [[ ${entry[(ws@:@)1]} == "delete" ]]; then
     36 			delete-tomb
     37 		elif [[ ${entry[(ws@:@)1]} == "backup" ]]; then
     38 			backup-tomb ## TODO:
     39 		else
     40 			die "No valid hook syntax on hook $line"
     41 			print $entry >> $HOOKS.fail
     42 			act "Wrote failed hook to $HOOKS.fail"
     43 			return 1
     44 		fi
     45 	done
     46 	rm -f $HOOKS
     47 }
     48 
     49 create-new-tomb() {
     50 	fn create-new-tomb
     51 	req=(undertaker tombid tombsize COFFINDOT GRAVEYARD)
     52 	undertaker=${entry[(ws@:@)2]}
     53 	tombid=${entry[(ws@:@)3]}
     54 	tombsize=${entry[(ws@:@)4]}
     55 	ckreq || return 1
     56 
     57 	## TODO: recognize custom post/bind hooks and implement them in the
     58 	## new tomb
     59 
     60 	notice "Creating new tomb"
     61 
     62 	$(id $undertaker &>/dev/null) || {
     63 		warn "User $undertaker not found. Creating..."
     64 		useradd -G coffin -m -s /bin/sh $undertaker
     65 		act "Created user $undertaker" }
     66 
     67 	genssl
     68 	dig-tomb
     69 	forge-tomb-key
     70 	lock-tomb
     71 
     72 	print "${undertaker}:${tombid}:true" >> $TTAB
     73 
     74 	#hash-key
     75 	#print "${keyhash}" >> $TOMBPASSWD
     76 	#chmod 600 $TOMBPASSWD
     77 	#act "Wrote to ttab and tombpasswd"
     78 
     79 	## Check for features
     80 	create-webdav-hook
     81 	create-sshfs-hook
     82 	create-wallet-hook
     83 }
     84 
     85 delete-tomb() {
     86 	fn delete-tomb
     87 	req=(entry undertaker tombid GRAVEYARD COFFINDOT)
     88 	freq=(TOMB)
     89 	ckreq || return 1
     90 
     91 	notice "deleting $tombid.tomb"
     92 
     93 	undertaker=${entry[(ws@:@)2]}
     94 	tombid=${entry[(ws@:@)3]}
     95 
     96 	[[ $(id $undertaker) ]] || {
     97 		die "User $undertaker not found. Exiting..." \
     98 			&& return 1 }
     99 
    100 	[[ -f $GRAVEYARD/$tombid.tomb ]] || {
    101 		die "Tomb $tombid.tomb not found. Exiting..." \
    102 			&& return 1 }
    103 
    104 	[[ -f $COFFINDOT/$tombid.key ]] || {
    105 		die "Key of $tombid not found. Exiting..." \
    106 			&& return 1 }
    107 
    108 	compare-key && {
    109 		sudo -u $undertaker $TOMB slam $tombid
    110 		update-tombs del
    111 
    112 		grep -v ${undertaker}:${tombid} $TTAB > /tmp/$TTAB.tmp
    113 		mv /tmp/$TTAB.tmp $TTAB && \
    114 			act "Removed from ttab"
    115 
    116 		#grep -v ${keyhash} $TOMBPASSWD > /tmp/$TOMBPASSWD.tmp
    117 		#mv /tmp/$TOMBPASSWD.tmp $TOMBPASSWD && \
    118 		#	chmod 600 $TOMBPASSWD && \
    119 		#	act "Removed from tombpasswd"
    120 
    121 		## Check for features
    122 		delete-webdav-hook $tombid
    123 		delete-sshfs-hook $undertaker $tombid
    124 	}
    125 }
    126 
    127 check-temptomb() {
    128 	fn check-temptomb
    129 	req=(tombid GRAVEYARD)
    130 	ckreq || return 1
    131 
    132 	act "Checking for tomb temps"
    133 	if [[ -d ${GRAVEYARD}/temp/${tombid} ]]; then
    134 		mv ${GRAVEYARD}/temp/${tombid}/* /media/${tombid}/
    135 		mv ${GRAVEYARD}/temp/${tombid}/.* /media/${tombid}/
    136 
    137 		act "Moved all tomb temps"
    138 
    139 		rmdir ${GRAVEYARD}/temp/${tombid}
    140 	fi
    141 }