rgpgfs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. // posix
  6. #include <dirent.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. // http://libfuse.github.io/doxygen/globals.html
  11. #define FUSE_USE_VERSION 31
  12. #include <fuse.h>
  13. // https://www.gnupg.org/documentation/manuals/gpgme/Function-and-Data-Index.html
  14. #include <gpgme.h>
  15. #define FUSE_PATH_BUF_LEN 256
  16. static char cache_dir[] = "/tmp/rgpgfs-cache-XXXXXX";
  17. static const size_t CACHE_PATH_BUF_LEN = sizeof(cache_dir) + FUSE_PATH_BUF_LEN;
  18. static gpgme_ctx_t gpgme_ctx;
  19. static int rgpgfs_mkdirs(char *path) {
  20. char *delimiter = strrchr(path, '/');
  21. if (delimiter == NULL) {
  22. errno = ENOTSUP;
  23. return 1;
  24. }
  25. *delimiter = '\0';
  26. struct stat statbuf;
  27. if (lstat(path, &statbuf) && (rgpgfs_mkdirs(path) || mkdir(path, S_IRWXU))) {
  28. *delimiter = '/';
  29. return 1;
  30. }
  31. *delimiter = '/';
  32. return 0;
  33. }
  34. static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
  35. // fprintf(stderr, "rgpgfs_encrypt('%s', %p)\n", source_path, cache_path);
  36. size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
  37. if (source_path_len >= FUSE_PATH_BUF_LEN) {
  38. errno = ENAMETOOLONG;
  39. perror("rgpgfs_encrypt");
  40. return 1;
  41. }
  42. strcpy(cache_path, cache_dir);
  43. strcat(cache_path, source_path);
  44. struct stat source_stat;
  45. if (lstat(source_path, &source_stat)) {
  46. perror("rgpgfs_encrypt: could not stat source file");
  47. return 1;
  48. }
  49. struct stat cache_stat;
  50. if (lstat(cache_path, &cache_stat) ||
  51. source_stat.st_mtim.tv_sec > cache_stat.st_mtim.tv_sec) {
  52. if (rgpgfs_mkdirs(cache_path)) {
  53. perror("rgpgfs_encrypt: failed to create dirs");
  54. return 1;
  55. }
  56. FILE *cache_file = fopen(cache_path, "w");
  57. if (cache_file == NULL) {
  58. perror("rgpgfs_encrypt: failed to open cache file");
  59. return 1;
  60. }
  61. fprintf(cache_file, "path: %s\n", source_path);
  62. fprintf(cache_file, "size: %lu bytes\n", source_stat.st_size);
  63. fprintf(cache_file, "mod time: %ld\n", source_stat.st_mtim.tv_sec);
  64. fclose(cache_file);
  65. printf("encrypted %s\n", source_path);
  66. }
  67. return 0;
  68. }
  69. static int rgpgfs_getattr(const char *source_path, struct stat *statbuf,
  70. struct fuse_file_info *fi) {
  71. if (lstat(source_path, statbuf))
  72. return -errno;
  73. if (!S_ISDIR(statbuf->st_mode)) {
  74. char cache_path[CACHE_PATH_BUF_LEN];
  75. if (rgpgfs_encrypt(source_path, cache_path))
  76. return -errno;
  77. if (lstat(cache_path, statbuf))
  78. return -errno;
  79. }
  80. return 0;
  81. }
  82. static int rgpgfs_access(const char *path, int mask) {
  83. int res = access(path, mask);
  84. // printf("rgpgfs_access(%s, %d) = %d\n", path, mask, res);
  85. if (res == -1)
  86. return -errno;
  87. return 0;
  88. }
  89. static int rgpgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  90. off_t offset, struct fuse_file_info *fi,
  91. enum fuse_readdir_flags flags) {
  92. DIR *dirp = opendir(path);
  93. if (dirp == NULL)
  94. return -errno;
  95. struct dirent *entp;
  96. while ((entp = readdir(dirp)) != NULL) {
  97. struct stat statbf;
  98. memset(&statbf, 0, sizeof(statbf));
  99. statbf.st_ino = entp->d_ino;
  100. statbf.st_mode = entp->d_type << 12;
  101. if (filler(buf, entp->d_name, &statbf, 0, 0))
  102. break;
  103. }
  104. closedir(dirp);
  105. return 0;
  106. }
  107. static int rgpgfs_open(const char *source_path, struct fuse_file_info *fi) {
  108. // fprintf(stderr, "rgpgfs_open('%s', %p)", source_path, fi);
  109. char cache_path[CACHE_PATH_BUF_LEN];
  110. if (rgpgfs_encrypt(source_path, cache_path))
  111. return -errno;
  112. int res = open(cache_path, fi->flags);
  113. if (res == -1)
  114. return -errno;
  115. fi->fh = res;
  116. return 0;
  117. }
  118. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  119. struct fuse_file_info *fi) {
  120. if (fi == NULL) {
  121. return ENOTSUP;
  122. }
  123. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  124. if (bytes_num == -1)
  125. return -errno;
  126. return bytes_num;
  127. }
  128. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  129. close(fi->fh);
  130. return 0;
  131. }
  132. static struct fuse_operations rgpgfs_fuse_operations = {
  133. .getattr = rgpgfs_getattr,
  134. .open = rgpgfs_open,
  135. .read = rgpgfs_read,
  136. .release = rgpgfs_release,
  137. .readdir = rgpgfs_readdir,
  138. .access = rgpgfs_access,
  139. };
  140. int main(int argc, char *argv[]) {
  141. if (mkdtemp(cache_dir) == NULL) {
  142. return 1;
  143. }
  144. printf("cache: %s\n", cache_dir);
  145. printf("gpgme version: %s\n", gpgme_check_version(NULL));
  146. gpg_error_t gpgme_init_err = gpgme_new(&gpgme_ctx);
  147. if (gpgme_init_err != GPG_ERR_NO_ERROR) {
  148. fprintf(stderr, "Failed to initialize gpgme: %s (%d)\n",
  149. gpg_strerror(gpgme_init_err), gpgme_init_err);
  150. return 1;
  151. }
  152. gpgme_release(gpgme_ctx);
  153. // TODO rm -r cache_dir (see man nftw)
  154. return fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  155. }