7 class BadKey < StandardError; end
12 attr_reader :path, :meta, :expire_at
14 def self.open(path, pass = nil)
15 StoredFile.new(path, pass)
19 Time.at(@meta['Created-at'])
27 @meta['One-time-only'] && @meta['One-time-only'] == 'true'
30 def self.create(src, pass, meta)
32 clear_meta = { "Coquelicot" => COQUELICOT_VERSION,
33 "Salt" => Base64.encode64(salt).strip,
34 "Expire-at" => meta.delete('Expire-at'),
36 yield YAML.dump(clear_meta) + YAML_START
38 cipher = get_cipher(pass, salt, :encrypt)
39 yield cipher.update(YAML.dump(meta.merge("Created-at" => Time.now.to_i)) +
42 while not (buf = src.read(BUFFER_LEN)).nil?
43 yield cipher.update(buf)
49 # zero the content before truncating
50 File.open(@path, 'r+') do |f|
51 f.seek 0, IO::SEEK_END
55 write_len = [StoredFile::BUFFER_LEN, length].min
56 length -= f.write("\0" * write_len)
60 File.truncate(@path, 0)
66 CIPHER = 'AES-256-CBC'
68 COQUELICOT_VERSION = "1.0"
70 def self.get_cipher(pass, salt, method)
71 hmac = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, 2000, 48)
72 cipher = OpenSSL::Cipher.new CIPHER
73 cipher.method(method).call
74 cipher.key = hmac[0..31]
75 cipher.iv = hmac[32..-1]
80 OpenSSL::Random::random_bytes(SALT_LEN)
83 def initialize(path, pass)
85 @file = File.open(@path)
86 if @file.lstat.size == 0 then
87 @expire_at = Time.now - 1
91 if YAML_START != (buf = @file.read(YAML_START.length)) then
92 raise "unknown file, read #{buf.inspect}"
96 init_decrypt_cipher pass
102 until YAML_START == (line = @file.readline) do
105 @meta = YAML.load(meta)
106 if @meta["Coquelicot"].nil? or @meta["Coquelicot"] != COQUELICOT_VERSION then
109 @expire_at = Time.at(@meta['Expire-at'])
112 def init_decrypt_cipher(pass)
113 salt = Base64.decode64(@meta["Salt"])
114 @cipher = StoredFile::get_cipher(pass, salt, :decrypt)
119 buf = @file.read(BUFFER_LEN)
120 content = @cipher.update(buf)
121 raise BadKey unless content.start_with? YAML_START
123 block = content.split(YAML_START, 3)
125 if block.length == 3 then
126 @initial_content = block[2]
127 @meta.merge! YAML.load(yaml)
131 until (buf = @file.read(BUFFER_LEN)).nil? do
132 block = @cipher.update(buf).split(YAML_START, 3)
134 break if block.length == 2
136 @initial_content = block[1]
137 @meta.merge! YAML.load(yaml)
148 def add_file(src, pass, options)
151 dst = gen_random_file_name
152 File.open(full_path(dst), 'w').close
155 File.open(full_path(dst), 'w') do |dest|
156 StoredFile.create(src, pass, options) { |data| dest.write data }
159 File.unlink full_path(dst)
162 link = gen_random_file_name
167 def get_file(link, pass=nil)
168 name = read_link(link)
169 return nil if name.nil?
170 StoredFile::open(full_path(name), pass)
173 def file_exists?(link)
174 name = read_link(link)
180 path = full_path(name)
181 if File.lstat(path).size > 0
182 file = StoredFile::open path
183 file.empty! if file.expired?
184 elsif Time.now - File.lstat(path).mtime > (Coquelicot.settings.gone_period * 60)
185 remove_from_links { |l| l.strip.end_with? " #{name}" }
193 LOCKFILE_OPTIONS = { :timeout => 60,
199 Lockfile.new "#{@path}/.lock", LOCKFILE_OPTIONS
206 def add_link(src, dst)
208 File.open(links_path, 'a') do |f|
209 f.write("#{src} #{dst}\n")
214 def remove_from_links(&block)
217 File.open(links_path, 'r+') do |f|
218 f.readlines.each do |l|
219 links << l unless yield l
229 remove_from_links { |l| l.start_with? "#{src} " }
235 File.open(links_path) do |f|
237 line = f.readline rescue break
238 if line.start_with? "#{src} " then
242 end until line.empty?
250 File.open(links_path) do |f|
251 f.readlines.collect { |l| l.split[1] }
256 def gen_random_file_name
258 name = Coquelicot.gen_random_base32(Coquelicot.settings.filename_length)
259 end while File.exists?(full_path(name))
264 raise "Wrong name" unless name.each_char.collect { |c| Coquelicot::FILENAME_CHARS.include? c }.all?
269 DEFAULT_SETTINGS = { :default_expire => 60,
270 :gone_period => 10080,
271 :filename_length => 20,
272 :random_pass_length => 16,
275 # Like RFC 4648 (Base32)
276 FILENAME_CHARS = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z 2 3 4 5 6 7)
281 @settings = DEFAULT_SETTINGS.merge(settings)
282 @settings.each_key do |k|
283 @settings.class.send(:define_method, k) { self[k] }
290 @settings ||= setup({})
294 @depot ||= Depot.new(settings.depot_path)
297 def gen_random_base32(length)
299 OpenSSL::Random::random_bytes(length).each_byte do |i|
300 name << FILENAME_CHARS[i % FILENAME_CHARS.length]
305 gen_random_base32(settings.random_pass_length)
307 def remap_base32_extra_characters(str)
309 FILENAME_CHARS.each { |c| map[c] = c; map[c.upcase] = c }
310 map.merge!({ '1' => 'l', '0' => 'o' })
312 str.each_char { |c| result << map[c] }