rgpgfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. }
  148. struct dirent *entp;
  149. while ((entp = readdir(dirp)) != NULL) {
  150. struct stat statbf;
  151. memset(&statbf, 0, sizeof(statbf));
  152. statbf.st_ino = entp->d_ino;
  153. statbf.st_mode = entp->d_type << 12;
  154. if (filler(buf, entp->d_name, &statbf, 0, 0))
  155. break;
  156. }
  157. closedir(dirp);
  158. return 0;
  159. }
  160. static int rgpgfs_open(const char *source_path, struct fuse_file_info *fi) {
  161. // fprintf(stderr, "rgpgfs_open('%s', %p)", source_path, fi);
  162. char cache_path[CACHE_PATH_BUF_LEN];
  163. if (rgpgfs_encrypt(source_path, cache_path))
  164. return -errno;
  165. int res = open(cache_path, fi->flags);
  166. if (res == -1)
  167. return -errno;
  168. fi->fh = res;
  169. return 0;
  170. }
  171. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  172. struct fuse_file_info *fi) {
  173. if (fi == NULL) {
  174. return ENOTSUP;
  175. }
  176. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  177. if (bytes_num == -1) {
  178. return -errno;
  179. }
  180. return bytes_num;
  181. }
  182. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  183. close(fi->fh);
  184. return 0;
  185. }
  186. static struct fuse_operations rgpgfs_fuse_operations = {
  187. .getattr = rgpgfs_getattr,
  188. .open = rgpgfs_open,
  189. .read = rgpgfs_read,
  190. .release = rgpgfs_release,
  191. .readdir = rgpgfs_readdir,
  192. .access = rgpgfs_access,
  193. };
  194. int main(int argc, char *argv[]) {
  195. if (mkdtemp(cache_dir) == NULL) {
  196. return 1;
  197. }
  198. printf("cache: %s\n", cache_dir);
  199. printf("gpgme version: %s\n", gpgme_check_version(NULL));
  200. gpg_error_t gpgme_init_err = gpgme_new(&gpgme_ctx);
  201. if (gpgme_init_err != GPG_ERR_NO_ERROR) {
  202. fprintf(stderr, "Failed to initialize gpgme: %s (%d)\n",
  203. gpg_strerror(gpgme_init_err), gpgme_init_err);
  204. return 1;
  205. }
  206. gpg_error_t gpgme_get_key_err =
  207. gpgme_get_key(gpgme_ctx, gpgme_recip_fpr, &gpgme_recip_key, 0);
  208. switch (gpgme_get_key_err) {
  209. case GPG_ERR_NO_ERROR:
  210. break;
  211. case GPG_ERR_EOF:
  212. fprintf(stderr, "Could not find key %s\n", gpgme_recip_fpr);
  213. return 1;
  214. case GPG_ERR_AMBIGUOUS_NAME:
  215. fprintf(stderr, "Key name '%s' is ambiguous\n", gpgme_recip_fpr);
  216. return 1;
  217. case GPG_ERR_INV_VALUE:
  218. default:
  219. fprintf(stderr, "Failed to load key %s: %s (%d)\n", gpgme_recip_fpr,
  220. gpg_strerror(gpgme_init_err), gpgme_get_key_err);
  221. return 1;
  222. }
  223. if (!gpgme_recip_key->can_encrypt) {
  224. fprintf(stderr, "Selected key %s can not be used for encryption\n",
  225. gpgme_recip_key->fpr);
  226. return 1;
  227. }
  228. gpgme_user_id_t gpgme_recip_id = gpgme_recip_key->uids;
  229. while (gpgme_recip_id != NULL) {
  230. printf("recipient: %s\n", gpgme_recip_id->uid);
  231. gpgme_recip_id = gpgme_recip_id->next;
  232. }
  233. printf("recipient fingerprint: %s\n", gpgme_recip_key->fpr);
  234. // TODO rm -r cache_dir (see man nftw)
  235. int fuse_main_err = fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  236. gpgme_release(gpgme_ctx);
  237. return fuse_main_err;
  238. }