summaryrefslogtreecommitdiff
path: root/byteback-setup-client-receive
blob: d98eab21e06e6e180417fbe1f38db44d8195e810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/ruby
#
# Called by byteback-setup-client to set up a new byteback-setup-client

$: << '/usr/lib/byteback'
require 'fileutils'
require 'trollop'
require 'byteback/util'
require 'byteback/log'
include Byteback::Util
include Byteback::Log

def error(message)
	STDERR.print "*** #{message}\n"
	exit 1
end

@hostname = ARGV.shift
@pubkey = ARGV.join(" ")

error("You must call this from byteback-setup-client on remote host") unless
  @hostname && 
  /^ssh/.match(@pubkey) &&
  ENV['SSH_CONNECTION']

@client_ip = ENV['SSH_CONNECTION'].split(" ").first

Dir.chdir(ENV['HOME']) # don't know why we wouldn't be here

Dir.mkdir(@hostname)

error("Couldn't create btrfs subvolume (needs sudo)") unless 
	system("sudo btrfs subvolume create #{@hostname}/current")

error("Couldn't set ownership of new subvolume (need sudo)") unless 
	system("sudo chown byteback "+File.expand_path("#{@hostname}/current"))

FileUtils.mkdir_p(".ssh")

if File.exists?(".ssh/authorized_keys") && 
	File.read(".ssh/authorized_keys").match(@pubkey.split(/\s+/)[1])

	warn("This key already exists in .ssh/authorized_keys on server, nothing to do!") 

else

	File.open(".ssh/authorized_keys", "a+") do |fh|
		fh.print <<-LINE.gsub(/\n/,"") + "\n"
command="byteback-receive",
from="#{@client_ip}",
environment="BYTEBACK_HOST=#{@hostname}"
 #{@pubkey}
		LINE
	end
end