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
00103 class storage_lock;
00104
00111 const char* storage_root();
00112
00124 bool set_alternative_storage_root(const char *to_this);
00125
00132 class p_file {
00133 friend class storage;
00134 friend class pe_file;
00135 friend class storage_lock;
00136
00137 public:
00145 virtual ~p_file();
00146
00163 virtual bool p_open(int flags);
00164
00173 virtual ssize_t p_read(foff_t at, RAWDATA_PTR data, size_t len);
00174
00183 virtual ssize_t p_write(foff_t at, const RAWDATA_PTR data, size_t len);
00184
00191 virtual int p_trunc(foff_t at);
00192
00201 virtual void p_close();
00202
00210 bool is_open();
00211
00220 virtual int p_stat(struct stat *st);
00221
00227 virtual const char* digest();
00228
00234 const char* name();
00235
00240 storage* owner();
00241
00247 virtual int p_rename(const char* new_name);
00248
00254 virtual int p_chmod(mode_t flags);
00255
00262 virtual int p_chown (uid_t uid, gid_t gid);
00263
00269 virtual int p_utime(struct utimbuf *ntime);
00270
00283 void p_rollback(void);
00284
00285 private:
00286 p_file(storage* owner, const char* pathname);
00287 virtual int p_cleanup();
00288 bool int_open(int flags, bool lock = false);
00289 bool check_integrity();
00290 bool roundup(size_t len);
00291 virtual size_t datasize();
00292 void fill_in(size_t len);
00293 virtual const char* p_name();
00294 void p_flush(bool do_trunc);
00295 void update_metadata(struct stat *fs);
00296
00297 std::string m_name;
00298 storage* m_owner;
00299 int m_fd;
00300 size_t m_size;
00301 std::string m_digest;
00302 RAWDATA_PTR m_data;
00303 size_t m_mapsize;
00304 bool m_new_file;
00305
00306 enum openmode_t {
00307 om_readonly,
00308 om_readwrite,
00309 om_writeonly,
00310 om_closed
00311 } m_omode;
00312
00313
00314 std::string m_semname;
00315 };
00316
00317 struct metadata_t;
00318 typedef std::map<std::string, metadata_t> contents_map_t;
00319
00328 class storage
00329 {
00330 friend class p_file;
00331 friend class pe_file;
00332 friend class storage_lock;
00333
00334 public:
00335
00342 typedef enum {
00343 vis_global,
00347 vis_shared,
00352 vis_private
00356 } visibility_t;
00357
00364 typedef enum {
00365 prot_signed,
00368 prot_encrypted
00370 } protection_t;
00371
00376 typedef enum {
00377 writable,
00378 readable,
00379 no_access
00380 } status_t;
00381
00393 storage(const char* name,
00394 const char* owner,
00395 visibility_t visibility,
00396 protection_t protection);
00397
00413 storage(const char* name,
00414 visibility_t visibility,
00415 protection_t protection);
00416
00420 ~storage();
00421
00431 bool remove_all_files();
00432
00437 typedef std::vector<const char*> stringlist;
00438
00447 size_t get_files(stringlist &names);
00448
00461 ssize_t get_ufiles(stringlist &names);
00462
00467 void release(stringlist &list);
00468
00477 bool contains_file(const char *pathname);
00478
00493 bool contains_link(const char *pathname);
00494
00504 bool hash_of_file(const char *pathname, std::string &hash);
00505
00516 void add_file(const char *pathname);
00517
00530 void remove_file(const char *pathname);
00531
00542 void add_link(const char *pathname, const char *to_file);
00543
00552 void remove_link(const char *pathname);
00553
00563 void rename(const char *pathname, const char *to_this);
00564
00577 void read_link(const char* pathname, std::string& points_to);
00578
00594 bool verify_file(const char* pathname);
00595
00611 bool verify_content(const char* pathname,
00612 unsigned char* data,
00613 size_t of_len);
00614
00637 int get_file(const char* pathname,
00638 RAWDATA_RPTR to_buf,
00639 size_t* bytes);
00640
00646 void release_buffer(RAWDATA_PTR buf);
00647
00658 int put_file(const char* pathname, RAWDATA_PTR data, size_t bytes);
00659
00669 bool commit();
00670
00679 bool refresh();
00680
00681
00691 int nbrof_files();
00692
00700 int nbrof_links();
00701
00706 const char* name();
00707
00715 const char* filename();
00716
00721 const char* token();
00722
00735 static int iterate_storage_names(storage::visibility_t of_visibility,
00736 storage::protection_t of_protection,
00737 const char* matching_names,
00738 aegis_callback* cb_func,
00739 void* ctx);
00740
00745 visibility_t visibility() { return m_vis; };
00746
00751 protection_t protection() { return m_prot; };
00752
00759 status_t status();
00760
00770 int stat_file(const char* pathname, struct stat *stbuf);
00771
00790 p_file* member(const char *pathname);
00791
00801 bool test_xts_aes(bool do_encrypt,
00802 int8_t key[32],
00803 int8_t ivec[16],
00804 size_t block_nr,
00805 int8_t idata[16],
00806 int8_t odata[16]);
00807
00808 private:
00809 protection_t m_prot;
00810 visibility_t m_vis;
00811 std::string m_token;
00812 std::string m_name;
00813 std::string m_filename;
00814 RAWDATA_PTR m_symkey;
00815 contents_map_t m_contents;
00816 std::map<std::string, std::string> m_links;
00817 size_t m_symkey_len;
00818
00819 void init_storage(const char* name,
00820 const char* token);
00821 void reinitialize();
00822 void create_store_sem();
00823 const char *store_sem_name();
00824 bool compute_digest(unsigned char* data,
00825 size_t bytes,
00826 char format,
00827 std::string& digest);
00828 bool set_aes_keys(bool is_protected, AES_KEY *to_this);
00829 void clear_aes_keys();
00830 bool internal_hash(struct aegis_digest_t *bhash, std::string& to_this);
00831 void normalize_filename(const char *name, std::string& result);
00832 bool generate_new_symkey();
00833 void rename(const char *pathname, const char *to_this, p_file *use_member);
00834 bool storage_changed();
00835 void lock_store();
00836 void unlock_store();
00837 bool convert_store(const char *new_filename);
00838 bool move_file(const char *old_name, const char *new_name);
00839
00840 storage_lock *m_lock;
00841 };
00842 };
00843
00844 #endif