1
0

rgpgfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 const char gpgme_recip_fpr[] =
  20. "1234567890ABCDEF1234567890ABCDEF12345678";
  21. static gpgme_key_t gpgme_recip_key;
  22. static int rgpgfs_mkdirs(char *path) {
  23. char *delimiter = strrchr(path, '/');
  24. if (delimiter == NULL) {
  25. errno = ENOTSUP;
  26. return 1;
  27. }
  28. *delimiter = '\0';
  29. struct stat statbuf;
  30. if (lstat(path, &statbuf) && (rgpgfs_mkdirs(path) || mkdir(path, S_IRWXU))) {
  31. *delimiter = '/';
  32. return 1;
  33. }
  34. *delimiter = '/';
  35. return 0;
  36. }
  37. static int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data) {
  38. if (gpgme_data_seek(data, 0, SEEK_SET) != 0) {
  39. perror("rgpgfs_gpgme_data_to_file: failed to seek");
  40. return 1;
  41. }
  42. FILE *file = fopen(path, "wb");
  43. if (file == NULL) {
  44. perror("rgpgfs_gpgme_data_to_file: failed to open file");
  45. return 1;
  46. }
  47. ssize_t count;
  48. char buf[BUFSIZ];
  49. while ((count = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
  50. if (fwrite(buf, 1, count, file) != count) {
  51. fprintf(stderr,
  52. "rgpgfs_gpgme_data_to_file: failed to write data to file");
  53. return 1;
  54. }
  55. }
  56. if (count != 0) {
  57. perror("rgpgfs_gpgme_data_to_file: failed to load data into buffer");
  58. return 1;
  59. }
  60. fclose(file);
  61. return 0;
  62. }
  63. static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
  64. // fprintf(stderr, "rgpgfs_encrypt('%s', %p)\n", source_path, cache_path);
  65. size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
  66. if (source_path_len >= FUSE_PATH_BUF_LEN) {
  67. errno = ENAMETOOLONG;
  68. perror("rgpgfs_encrypt");
  69. return 1;
  70. }
  71. strcpy(cache_path, cache_dir);
  72. strcat(cache_path, source_path);
  73. struct stat source_stat;
  74. if (lstat(source_path, &source_stat)) {
  75. perror("rgpgfs_encrypt: failed to stat source file");
  76. return 1;
  77. }
  78. struct stat cache_stat;
  79. if (lstat(cache_path, &cache_stat) ||
  80. source_stat.st_mtim.tv_sec > cache_stat.st_mtim.tv_sec) {
  81. if (rgpgfs_mkdirs(cache_path)) {
  82. perror("rgpgfs_encrypt: failed to create dirs");
  83. return 1;
  84. }
  85. gpgme_data_t plain_data;
  86. gpgme_error_t gpgme_source_read_err =
  87. gpgme_data_new_from_file(&plain_data, source_path, 1);
  88. if (gpgme_source_read_err != GPG_ERR_NO_ERROR) {
  89. fprintf(stderr,
  90. "rgpgfs_encrypt: failed to read source file %s: %s (%d)\n",
  91. source_path, gpg_strerror(gpgme_source_read_err),
  92. gpgme_source_read_err);
  93. return 1;
  94. }
  95. gpgme_data_t cipher_data;
  96. if (gpgme_data_new(&cipher_data) != GPG_ERR_NO_ERROR) {
  97. fprintf(stderr,
  98. "rgpgfs_encrypt: failed to prepare cipher data container\n");
  99. gpgme_data_release(plain_data);
  100. return 1;
  101. }
  102. // list of recipients may implicitly include the default recipient
  103. // (GPGME_ENCRYPT_NO_ENCRYPT_TO)
  104. gpgme_key_t recip_keys[] = {gpgme_recip_key, NULL};
  105. if (gpgme_op_encrypt(gpgme_ctx, recip_keys, 0, plain_data, cipher_data) ==
  106. GPG_ERR_NO_ERROR) {
  107. if (rgpgfs_gpgme_data_to_file(cache_path, cipher_data)) {
  108. fprintf(stderr,
  109. "rgpgfs_encrypt: failed to write cipher data to disk\n");
  110. } else {
  111. printf("encrypted %s\n", source_path);
  112. }
  113. } else {
  114. fprintf(stderr, "rgpgfs_encrypt: failed to encrypt %s\n", source_path);
  115. }
  116. gpgme_data_release(cipher_data);
  117. gpgme_data_release(plain_data);
  118. }
  119. return 0;
  120. }
  121. static int rgpgfs_getattr(const char *source_path, struct stat *statbuf,
  122. struct fuse_file_info *fi) {
  123. if (lstat(source_path, statbuf))
  124. return -errno;
  125. if (!S_ISDIR(statbuf->st_mode)) {
  126. char cache_path[CACHE_PATH_BUF_LEN];
  127. if (rgpgfs_encrypt(source_path, cache_path))
  128. return -errno;
  129. if (lstat(cache_path, statbuf))
  130. return -errno;
  131. }
  132. return 0;
  133. }
  134. static int rgpgfs_access(const char *path, int mask) {
  135. int res = access(path, mask);
  136. // printf("rgpgfs_access(%s, %d) = %d\n", path, mask, res);
  137. if (res == -1)
  138. return -errno;
  139. return 0;
  140. }
  141. static int rgpgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  142. off_t offset, struct fuse_file_info *fi,
  143. enum fuse_readdir_flags flags) {
  144. DIR *dirp = opendir(path);
  145. if (dirp == NULL)
  146. return -errno;
  147. struct dirent *entp;
  148. while ((entp = readdir(dirp)) != NULL) {
  149. struct stat statbf;
  150. memset(&statbf, 0, sizeof(statbf));
  151. statbf.st_ino = entp->d_ino;
  152. statbf.st_mode = entp->d_type << 12;
  153. if (filler(buf, entp->d_name, &statbf, 0, 0))
  154. break;
  155. }
  156. closedir(dirp);
  157. return 0;
  158. }
  159. static int rgpgfs_open(const char *source_path, struct fuse_file_info *fi) {
  160. // fprintf(stderr, "rgpgfs_open('%s', %p)", source_path, fi);
  161. char cache_path[CACHE_PATH_BUF_LEN];
  162. if (rgpgfs_encrypt(source_path, cache_path))
  163. return -errno;
  164. int res = open(cache_path, fi->flags);
  165. if (res == -1)
  166. return -errno;
  167. fi->fh = res;
  168. return 0;
  169. }
  170. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  171. struct fuse_file_info *fi) {
  172. if (fi == NULL) {
  173. return ENOTSUP;
  174. }
  175. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  176. if (bytes_num == -1)
  177. return -errno;
  178. return bytes_num;
  179. }
  180. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  181. close(fi->fh);
  182. return 0;
  183. }
  184. static struct fuse_operations rgpgfs_fuse_operations = {
  185. .getattr = rgpgfs_getattr,
  186. .open = rgpgfs_open,
  187. .read = rgpgfs_read,
  188. .release = rgpgfs_release,
  189. .readdir = rgpgfs_readdir,
  190. .access = rgpgfs_access,
  191. };
  192. int main(int argc, char *argv[]) {
  193. if (mkdtemp(cache_dir) == NULL) {
  194. return 1;
  195. }
  196. printf("cache: %s\n", cache_dir);
  197. printf("gpgme version: %s\n", gpgme_check_version(NULL));
  198. gpg_error_t gpgme_init_err = gpgme_new(&gpgme_ctx);
  199. if (gpgme_init_err != GPG_ERR_NO_ERROR) {
  200. fprintf(stderr, "Failed to initialize gpgme: %s (%d)\n",
  201. gpg_strerror(gpgme_init_err), gpgme_init_err);
  202. return 1;
  203. }
  204. gpg_error_t gpgme_get_key_err =
  205. gpgme_get_key(gpgme_ctx, gpgme_recip_fpr, &gpgme_recip_key, 0);
  206. switch (gpgme_get_key_err) {
  207. case GPG_ERR_NO_ERROR:
  208. break;
  209. case GPG_ERR_EOF:
  210. fprintf(stderr, "Could not find key %s\n", gpgme_recip_fpr);
  211. return 1;
  212. case GPG_ERR_AMBIGUOUS_NAME:
  213. fprintf(stderr, "Key name '%s' is ambiguous\n", gpgme_recip_fpr);
  214. return 1;
  215. case GPG_ERR_INV_VALUE:
  216. default:
  217. fprintf(stderr, "Failed to load key %s: %s (%d)\n", gpgme_recip_fpr,
  218. gpg_strerror(gpgme_init_err), gpgme_get_key_err);
  219. return 1;
  220. }
  221. if (!gpgme_recip_key->can_encrypt) {
  222. fprintf(stderr, "Selected key %s can not be used for encryption\n",
  223. gpgme_recip_key->fpr);
  224. return 1;
  225. }
  226. gpgme_user_id_t gpgme_recip_id = gpgme_recip_key->uids;
  227. while (gpgme_recip_id != NULL) {
  228. printf("recipient: %s\n", gpgme_recip_id->uid);
  229. gpgme_recip_id = gpgme_recip_id->next;
  230. }
  231. printf("recipient fingerprint: %s\n", gpgme_recip_key->fpr);
  232. // TODO rm -r cache_dir (see man nftw)
  233. int fuse_main_err = fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  234. gpgme_release(gpgme_ctx);
  235. return fuse_main_err;
  236. }