Procházet zdrojové kódy

added param -r / --recipient / -o recipient=

Fabian Peter Hammerle před 6 roky
rodič
revize
044aaa0b05
4 změnil soubory, kde provedl 43 přidání a 26 odebrání
  1. 1 1
      Makefile
  2. 16 0
      src/gpgme.c
  3. 3 0
      src/gpgme.h
  4. 23 25
      src/main.c

+ 1 - 1
Makefile

@@ -21,7 +21,7 @@ src/gpgme.o : src/gpgme.c src/gpgme.h
 src/main.o : src/main.c src/fs.h src/gpgme.h
 src/main.o : src/main.c src/fs.h src/gpgme.h
 	$(CC) $(CFLAGS) -c $< -o $@
 	$(CC) $(CFLAGS) -c $< -o $@
 
 
-rgpgfs : src/*.o
+rgpgfs : src/fs.o src/gpgme.o src/main.o
 	$(LD) $^ -o $@ $(LIBS)
 	$(LD) $^ -o $@ $(LIBS)
 
 
 format : src/*.h src/*.c
 format : src/*.h src/*.c

+ 16 - 0
src/gpgme.c

@@ -4,6 +4,22 @@
 
 
 #include <stdio.h>
 #include <stdio.h>
 
 
+int rgpgfs_gpgme_get_encrypt_key(gpgme_ctx_t gpgme_ctx, const char *key_name,
+                                 gpgme_key_t *key) {
+  gpg_error_t err = gpgme_get_key(gpgme_ctx, key_name, key, 0);
+  if (err != GPG_ERR_NO_ERROR) {
+    fprintf(stderr, "Failed to load key %s: %s (%d)\n", key_name,
+            gpg_strerror(err), err);
+    return 1;
+  }
+  if (!(*key)->can_encrypt) {
+    fprintf(stderr, "Selected key %s can not be used for encryption\n",
+            (*key)->fpr);
+    return 1;
+  }
+  return 0;
+}
+
 int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data) {
 int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data) {
   if (gpgme_data_seek(data, 0, SEEK_SET) != 0) {
   if (gpgme_data_seek(data, 0, SEEK_SET) != 0) {
     perror("rgpgfs_gpgme_data_to_file: failed to seek");
     perror("rgpgfs_gpgme_data_to_file: failed to seek");

+ 3 - 0
src/gpgme.h

@@ -1,5 +1,8 @@
 #include <gpgme.h>
 #include <gpgme.h>
 
 
+int rgpgfs_gpgme_get_encrypt_key(gpgme_ctx_t gpgme_ctx, const char *key_name,
+                                 gpgme_key_t *key);
+
 int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data);
 int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data);
 
 
 int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,
 int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,

+ 23 - 25
src/main.c

@@ -23,10 +23,16 @@ static char cache_dir[] = "/tmp/rgpgfs-cache-XXXXXX";
 static const size_t CACHE_PATH_BUF_LEN = sizeof(cache_dir) + FUSE_PATH_BUF_LEN;
 static const size_t CACHE_PATH_BUF_LEN = sizeof(cache_dir) + FUSE_PATH_BUF_LEN;
 
 
 static gpgme_ctx_t gpgme_ctx;
 static gpgme_ctx_t gpgme_ctx;
-static const char gpgme_recip_fpr[] =
-    "1234567890ABCDEF1234567890ABCDEF12345678";
 static gpgme_key_t gpgme_recip_key;
 static gpgme_key_t gpgme_recip_key;
 
 
+static struct { char *recipient_name; } rgpgfs_config;
+
+static struct fuse_opt rgpgfs_opts[] = {{"--recipient %s", 0, 0},
+                                        {"--recipient=%s", 0, 0},
+                                        {"-r %s", 0, 0},
+                                        {"recipient=%s", 0, 0},
+                                        FUSE_OPT_END};
+
 static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
 static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
   // fprintf(stderr, "rgpgfs_encrypt('%s', %p)\n", source_path, cache_path);
   // fprintf(stderr, "rgpgfs_encrypt('%s', %p)\n", source_path, cache_path);
   size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
   size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
@@ -149,10 +155,9 @@ static struct fuse_operations rgpgfs_fuse_operations = {
 };
 };
 
 
 int main(int argc, char *argv[]) {
 int main(int argc, char *argv[]) {
-  if (mkdtemp(cache_dir) == NULL) {
-    return 1;
-  }
-  printf("cache: %s\n", cache_dir);
+  struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
+  rgpgfs_config.recipient_name = NULL;
+  fuse_opt_parse(&args, &rgpgfs_config, rgpgfs_opts, NULL);
   printf("gpgme version: %s\n", gpgme_check_version(NULL));
   printf("gpgme version: %s\n", gpgme_check_version(NULL));
   gpg_error_t gpgme_init_err = gpgme_new(&gpgme_ctx);
   gpg_error_t gpgme_init_err = gpgme_new(&gpgme_ctx);
   if (gpgme_init_err != GPG_ERR_NO_ERROR) {
   if (gpgme_init_err != GPG_ERR_NO_ERROR) {
@@ -160,26 +165,14 @@ int main(int argc, char *argv[]) {
             gpg_strerror(gpgme_init_err), gpgme_init_err);
             gpg_strerror(gpgme_init_err), gpgme_init_err);
     return 1;
     return 1;
   }
   }
-  gpg_error_t gpgme_get_key_err =
-      gpgme_get_key(gpgme_ctx, gpgme_recip_fpr, &gpgme_recip_key, 0);
-  switch (gpgme_get_key_err) {
-  case GPG_ERR_NO_ERROR:
-    break;
-  case GPG_ERR_EOF:
-    fprintf(stderr, "Could not find key %s\n", gpgme_recip_fpr);
-    return 1;
-  case GPG_ERR_AMBIGUOUS_NAME:
-    fprintf(stderr, "Key name '%s' is ambiguous\n", gpgme_recip_fpr);
-    return 1;
-  case GPG_ERR_INV_VALUE:
-  default:
-    fprintf(stderr, "Failed to load key %s: %s (%d)\n", gpgme_recip_fpr,
-            gpg_strerror(gpgme_init_err), gpgme_get_key_err);
+  if (rgpgfs_config.recipient_name == NULL) {
+    fprintf(stderr, "Missing parameter --recipient\n");
     return 1;
     return 1;
   }
   }
-  if (!gpgme_recip_key->can_encrypt) {
-    fprintf(stderr, "Selected key %s can not be used for encryption\n",
-            gpgme_recip_key->fpr);
+  printf("recipient name: %s\n", rgpgfs_config.recipient_name);
+  if (rgpgfs_gpgme_get_encrypt_key(gpgme_ctx, rgpgfs_config.recipient_name,
+                                   &gpgme_recip_key)) {
+    gpgme_release(gpgme_ctx);
     return 1;
     return 1;
   }
   }
   gpgme_user_id_t gpgme_recip_id = gpgme_recip_key->uids;
   gpgme_user_id_t gpgme_recip_id = gpgme_recip_key->uids;
@@ -188,8 +181,13 @@ int main(int argc, char *argv[]) {
     gpgme_recip_id = gpgme_recip_id->next;
     gpgme_recip_id = gpgme_recip_id->next;
   }
   }
   printf("recipient fingerprint: %s\n", gpgme_recip_key->fpr);
   printf("recipient fingerprint: %s\n", gpgme_recip_key->fpr);
+  if (mkdtemp(cache_dir) == NULL) {
+    return 1;
+  }
+  printf("cache: %s\n", cache_dir);
+  int fuse_main_err =
+      fuse_main(args.argc, args.argv, &rgpgfs_fuse_operations, NULL);
   // TODO rm -r cache_dir (see man nftw)
   // TODO rm -r cache_dir (see man nftw)
-  int fuse_main_err = fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
   gpgme_release(gpgme_ctx);
   gpgme_release(gpgme_ctx);
   return fuse_main_err;
   return fuse_main_err;
 }
 }