added crypt command line option

Thu, 05 Jun 2014 16:53:53 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 05 Jun 2014 16:53:53 +0200
changeset 45
e3839719b079
parent 44
e4e23a88d4de
child 46
0542668d0f26

added crypt command line option

dav/main.c file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
libidav/webdav.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Thu Jun 05 15:39:51 2014 +0200
+++ b/dav/main.c	Thu Jun 05 16:53:53 2014 +0200
@@ -59,7 +59,7 @@
     initGenericErrorDefaultFunc(&fnc);
     ctx = dav_context_new();
     load_config(ctx);
-    dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
+    //dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
     
     //test();
     
@@ -107,19 +107,20 @@
 void print_usage(char *cmd) {
     fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
     fprintf(stderr, "Commands:\n");
-    fprintf(stderr, "        list [-altR] [-u <date>] <url>\n");
+    fprintf(stderr, "        list [-altpcR] [-u <date>] <url>\n");
     fprintf(
             stderr,
-            "        get [-pR] [-o <file>] [-u <date>] <url>\n");
-    fprintf(stderr, "        put [-pR] [-k <key>] <url> <file>\n");
-    fprintf(stderr, "        mkdir [-p] [-k <key>] <url>\n");
-    fprintf(stderr, "        remove <url>\n");
+            "        get [-pcR] [-o <file>] [-u <date>] <url>\n");
+    fprintf(stderr, "        put [-pcR] [-k <key>] <url> <file>\n");
+    fprintf(stderr, "        mkdir [-pc] [-k <key>] <url>\n");
+    fprintf(stderr, "        remove [-pc] <url>\n");
     fprintf(stderr, "        date [url]\n");
     fprintf(stderr, "\n");
     fprintf(stderr, "Options:\n");
     fprintf(stderr,
             "        -k <key>   Key to use for encryption or decryption\n");
     fprintf(stderr, "        -p         Don't encrypt or decrypt files\n");
+    fprintf(stderr, "        -c         Enable full encryption\n");
     fprintf(stderr,
             "        -R         "
             "Recursively do the operation for all children\n");
@@ -248,6 +249,25 @@
     free(res_url);
 }
 
+int set_session_config(DavSession *sn, CmdArgs *a) {
+    char *plain = cmd_getoption(a, "plain");
+    char *crypt = cmd_getoption(a, "crypt");
+    
+    if(plain && crypt) {
+        fprintf(stderr, "Error: -p and -c option set\n");
+        return 1;
+    }
+    
+    if(plain) {
+        int flags = sn->flags;
+        dav_session_set_flags(sn, 0);
+    } else if(crypt) {
+        dav_session_set_flags(sn, DAV_SESSION_FULL_ENCRYPTION);
+    }
+    
+    return 0;
+}
+
 
 int cmd_list(CmdArgs *a) {
     if(a->argc != 1) {
@@ -264,6 +284,10 @@
     dav_session_set_flags(sn, get_repository_flags(repo));
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
     char *update = cmd_getoption(a, "update");
     time_t t = 0;
     if(update) {
@@ -276,7 +300,7 @@
     while(ret != 0) {
         ls = dav_query(
                 sn,
-                "get - from %s where lastmodified > %t with depth %d",
+                "get idav:crypto-name,idav:crypto-key from %s where lastmodified > %t with depth %d",
                 path,
                 t,
                 depth);
@@ -483,6 +507,10 @@
     dav_session_set_flags(sn, get_repository_flags(repo));
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
     char *update = cmd_getoption(a, "update");
     time_t t = 0;
     if(update) {
@@ -512,13 +540,6 @@
         return -1;
     }
     
-    // disable file decryption, if the -p option is specified
-    char *plain = cmd_getoption(a, "plain");
-    if(plain) {
-        int flags = sn->flags;
-        dav_session_set_flags(sn, flags ^ DAV_SESSION_DECRYPT_CONTENT);
-    }
-    
     /*
      * determine the output file
      * use stdout if the output file is -
@@ -609,12 +630,8 @@
     dav_session_set_flags(sn, get_repository_flags(repo));
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
-    // disable file encryption if the -p option is specified
-    char *plain = cmd_getoption(a, "plain");
-    if(plain) {
-        int flags = sn->flags;
-        dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_CONTENT);
-        dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_NAME);
+    if(set_session_config(sn, a)) {
+        return -1;
     }
     
     // override the session key if the -k option is specified
@@ -754,6 +771,10 @@
     dav_session_set_flags(sn, get_repository_flags(repo));
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
     DavResource *res = dav_resource_new(sn, path);
     if(!res) {
         fprintf(stderr, "error\n");
@@ -783,12 +804,8 @@
     dav_session_set_flags(sn, get_repository_flags(repo));
     sn->key = dav_context_get_key(ctx, repo->default_key);
     
-    // disable file encryption if the -p option is specified
-    char *plain = cmd_getoption(a, "plain");
-    if(plain) {
-        int flags = sn->flags;
-        dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_CONTENT);
-        dav_session_set_flags(sn, flags ^ DAV_SESSION_ENCRYPT_NAME);
+    if(set_session_config(sn, a)) {
+        return -1;
     }
     
     // override the session key if the -k option is specified
--- a/dav/optparser.c	Thu Jun 05 15:39:51 2014 +0200
+++ b/dav/optparser.c	Thu Jun 05 16:53:53 2014 +0200
@@ -74,6 +74,10 @@
                         ucx_map_cstr_put(a->options, "plain", "");
                         break;
                     }
+                    case 'c': {
+                        ucx_map_cstr_put(a->options, "crypt", "");
+                        break;
+                    }
                     case 'a': {
                         ucx_map_cstr_put(a->options, "all", "");
                         break;
--- a/libidav/webdav.c	Thu Jun 05 15:39:51 2014 +0200
+++ b/libidav/webdav.c	Thu Jun 05 16:53:53 2014 +0200
@@ -77,7 +77,7 @@
         return NULL;
     }
     idavns->prefix = "idav";
-    davns->name = DAV_NS;
+    idavns->name = DAV_NS;
     if(ucx_map_cstr_put(context->namespaces, "idav", idavns)) {
         free(davns);
         free(idavns);
@@ -300,9 +300,8 @@
             nsname.length--;
             
             DavProperty *dp = malloc(sizeof(DavProperty));
-            sstr_t pre = sstrdup(sstrtrim(nspre));
-            dp->ns = dav_get_namespace(context, pre.ptr);
-            free(pre.ptr);
+            sstr_t pre = sstrtrim(nspre);
+            dp->ns = dav_get_namespace_s(context, pre);
             dp->name = sstrdup(nsname).ptr;
             if(dp->ns && dp->name) {
                 proplist = ucx_list_append(proplist, dp);

mercurial