00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00055 #ifndef AEGIS_STORAGE_H
00056 #define AEGIS_STORAGE_H
00057
00058 #include <string.h>
00059 #include <unistd.h>
00060 #include <sys/fcntl.h>
00061 #include <sys/types.h>
00062 #include <utime.h>
00063
00075 #if defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)
00076 #define foff_t off_t
00077 #else
00078 #include <stdint.h>
00079 #define foff_t uint64_t
00080 #endif
00081
00082 #include <openssl/aes.h>
00083 #include <string>
00084 #include <vector>
00085 #include <map>
00086
00087 #include "aegis_common.h"
00088
00096 #define O_RECOVER 010000000
00097
00098 namespace aegis {
00099
00100 class storage;
00101 class pe_file;
00102 class p_file_lock;
00103 class storage_lock;
00104
00111 const char* storage_root();
00112
00119 class p_file {
00120 friend class storage;
00121 friend class pe_file;
00122
00123 public:
00131 virtual ~p_file();
00132
00149 virtual bool p_open(int flags);
00150
00159 virtual ssize_t p_read(foff_t at, RAWDATA_PTR data, size_t len);
00160
00169 virtual ssize_t p_write(foff_t at, const RAWDATA_PTR data, size_t len);
00170
00177 virtual int p_trunc(foff_t at);
00178
00187 virtual void p_close();
00188
00196 bool is_open();
00197
00206 virtual int p_stat(struct stat *st);
00207
00213 virtual const char* digest();
00214
00220 const char* name();
00221
00226 storage* owner();
00227
00233 virtual int p_rename(const char* new_name);
00234
00240 virtual int p_chmod(mode_t flags);
00241
00248 virtual int p_chown (uid_t uid, gid_t gid);
00249
00255 virtual int p_utime(struct utimbuf *ntime);
00256
00257 private:
00258 p_file(storage* owner, const char* pathname);
00259 virtual int p_cleanup();
00260 bool int_open(int flags, bool lock = false);
00261 bool check_integrity();
00262 bool roundup(size_t len);
00263 virtual size_t datasize();
00264 void fill_in(size_t len);
00265 virtual const char* p_name();
00266 void p_flush(bool do_trunc);
00267
00268 std::string m_name;
00269 storage* m_owner;
00270 int m_fd;
00271 size_t m_size;
00272 std::string m_digest;
00273 RAWDATA_PTR m_data;
00274 size_t m_mapsize;
00275 bool m_new_file;
00276
00277 enum openmode_t {
00278 om_readonly,
00279 om_readwrite,
00280 om_writeonly,
00281 om_closed
00282 } m_omode;
00283
00284 p_file_lock *m_lock;
00285 std::string m_semname;
00286 };
00287
00296 class storage
00297 {
00298 friend class p_file;
00299 friend class pe_file;
00300
00301 public:
00302
00309 typedef enum {
00310 vis_global,
00314 vis_shared,
00319 vis_private
00323 } visibility_t;
00324
00331 typedef enum {
00332 prot_signed,
00335 prot_encrypted
00337 } protection_t;
00338
00343 typedef enum {
00344 writable,
00345 readable,
00346 no_access
00347 } status_t;
00348
00360 storage(const char* name,
00361 const char* owner,
00362 visibility_t visibility,
00363 protection_t protection);
00364
00380 storage(const char* name,
00381 visibility_t visibility,
00382 protection_t protection);
00383
00387 ~storage();
00388
00398 bool remove_all_files();
00399
00404 typedef std::vector<const char*> stringlist;
00405
00414 size_t get_files(stringlist &names);
00415
00428 ssize_t get_ufiles(stringlist &names);
00429
00434 void release(stringlist &list);
00435
00444 bool contains_file(const char *pathname);
00445
00460 bool contains_link(const char *pathname);
00461
00471 bool hash_of_file(const char *pathname, std::string &hash);
00472
00483 void add_file(const char *pathname);
00484
00497 void remove_file(const char *pathname);
00498
00509 void add_link(const char *pathname, const char *to_file);
00510
00519 void remove_link(const char *pathname);
00520
00530 void rename(const char *pathname, const char *to_this);
00531
00544 void read_link(const char* pathname, std::string& points_to);
00545
00561 bool verify_file(const char* pathname);
00562
00578 bool verify_content(const char* pathname,
00579 unsigned char* data,
00580 size_t of_len);
00581
00604 int get_file(const char* pathname,
00605 RAWDATA_RPTR to_buf,
00606 size_t* bytes);
00607
00613 void release_buffer(RAWDATA_PTR buf);
00614
00625 int put_file(const char* pathname, RAWDATA_PTR data, size_t bytes);
00626
00636 bool commit();
00637
00646 bool refresh();
00647
00648
00658 int nbrof_files();
00659
00667 int nbrof_links();
00668
00673 const char* name();
00674
00682 const char* filename();
00683
00688 const char* token();
00689
00702 static int iterate_storage_names(storage::visibility_t of_visibility,
00703 storage::protection_t of_protection,
00704 const char* matching_names,
00705 aegis_callback* cb_func,
00706 void* ctx);
00707
00712 visibility_t visibility() { return m_vis; };
00713
00718 protection_t protection() { return m_prot; };
00719
00726 status_t status();
00727
00737 int stat_file(const char* pathname, struct stat *stbuf);
00738
00757 p_file* member(const char *pathname);
00758
00768 bool test_xts_aes(bool do_encrypt,
00769 int8_t key[32],
00770 int8_t ivec[16],
00771 size_t block_nr,
00772 int8_t idata[16],
00773 int8_t odata[16]);
00774
00775 private:
00776 protection_t m_prot;
00777 visibility_t m_vis;
00778 std::string m_token;
00779 std::string m_name;
00780 std::string m_filename;
00781 RAWDATA_PTR m_symkey;
00782 std::map<std::string, std::string> m_contents;
00783 std::map<std::string, std::string> m_links;
00784 size_t m_symkey_len;
00785
00786 void init_storage(const char* name,
00787 const char* token);
00788 void reinitialize();
00789 void create_store_sem();
00790 const char *store_sem_name();
00791 bool compute_digest(unsigned char* data,
00792 size_t bytes,
00793 char format,
00794 std::string& digest);
00795 bool set_aes_keys(bool is_protected, AES_KEY *to_this);
00796 void clear_aes_keys();
00797 bool internal_hash(std::string& to_this);
00798 void normalize_filename(const char *name, std::string& result);
00799 bool generate_new_symkey();
00800 void rename(const char *pathname, const char *to_this, p_file *use_member);
00801 bool storage_changed();
00802 bool inotify_set_watch();
00803 bool lock_store();
00804 bool unlock_store();
00805 bool convert_store();
00806
00807 storage_lock *m_lock;
00808 };
00809 };
00810
00811 #endif