dav/main.c

Tue, 13 Aug 2013 11:19:22 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2013 11:19:22 +0200
changeset 11
5db6178d8b58
parent 8
4503498deb22
child 12
493128ef1b12
permissions
-rw-r--r--

added usage info

1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2012 Olaf Wintermann. All rights reserved.
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
32 #include <errno.h>
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
33 #include <unistd.h>
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <libxml/xmlerror.h>
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
35 #include <sys/types.h>
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
36 #include <sys/stat.h>
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
37 #include <ucx/string.h>
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
39 #include "webdav.h"
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "utils.h"
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
41 #include "config.h"
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
42 #include "crypto.h"
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "main.h"
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 void xmlerrorfnc(void * ctx, const char * msg, ... ) {
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 // nothing
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 int main(int argc, char **argv) {
8
4503498deb22 creates a .dav dir if it doesn't exist
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
50 xmlGenericErrorFunc fnc = xmlerrorfnc;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 initGenericErrorDefaultFunc(&fnc);
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
52 load_config();
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 if(argc < 2) {
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
55 fprintf(stderr, "Missing command\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
56 print_usage(argv[0]);
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
57 return -1;
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
58 }
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
59
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
60 char *cmd = argv[1];
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
61 CmdArgs *args = cmd_parse_args(argc - 2, argv + 2);
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
62 if(!args) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
63 print_usage(argv[0]);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
67 if(!strcmp(cmd, "list") || !strcmp(cmd, "ls")) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
68 return cmd_list(args);
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
69 } else if(!strcmp(cmd, "get")) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
70 return cmd_get(args);
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
71 } else if(!strcmp(cmd, "put")) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
72 return cmd_put(args);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
75 print_usage(argv[0]);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
79 void print_usage(char *cmd) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
80 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
81 fprintf(stderr, "Commands:\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
82 fprintf(stderr, " list [-al] <url>\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
83 fprintf(stderr, " get [-p] [-k <key>] [-o <file>] <url>\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
84 fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
85 fprintf(stderr, "\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
86 fprintf(stderr, "Options:\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
87 fprintf(stderr,
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
88 " -k <key> Key to use for encryption or decryption\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
89 fprintf(stderr, " -p Don't encrypt or decrypt files\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
90 fprintf(stderr, " -o <file> Write output to file\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
91 fprintf(stderr, "\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
92 fprintf(stderr,
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
93 "Instead of an url you can pass a repository name "
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
94 "with an optional path:\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
95 fprintf(stderr, " <repository>/path/\n");
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
96 fprintf(stderr, "\n");
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
99 void url_get_parts(char *url, char **root, char **path) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
100 size_t ulen = strlen(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
101 *root = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
102 *path = NULL;
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
103
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
104 int s;
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
105 if(ulen > 7 && !strncasecmp(url, "http://", 7)) {
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
106 s = 7;
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
107 } else if(ulen > 8 && !strncasecmp(url, "https://", 8)) {
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
108 s = 8;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
109 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
110 s = 1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
111 }
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
112
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
113 for(int i=s;i<ulen;i++) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
114 char c = url[i];
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
115 if(c == '/') {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
116 sstr_t r = sstrn(url, i);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
117 sstr_t p = sstrsubs(sstr(url), i);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
118 if(p.length == 0) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
119 p = sstrn("/", 1);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
120 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
121 *root = sstrdup(r).ptr;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
122 *path = sstrdup(p).ptr;
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
123 return;
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
124 }
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
125 }
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
126
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
127 *root = strdup(url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
128 *path = strdup("/");
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
129 }
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
130
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
131
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
132 int cmd_list(CmdArgs *a) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
133 if(a->argc == 0) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
138 DavContext *ctx = dav_context_new();
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
139 DavSession *sn = NULL;
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
140 char *url = a->argv[0];
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
141 char *root = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
142 char *path = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
143 url_get_parts(url, &root, &path);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
145 Repository *repo = get_repository(root);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
146 if(repo) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
147 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
148 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
149 sn = dav_session_new(ctx, root);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
152 //printf("baseurl: %s\n", sn->base_url);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
153
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
154 DavResource *ls = dav_get(sn, path, NULL);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
155 if(!ls) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
156 fprintf(stderr, "error\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
157 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
158 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
159 DavResource *child = ls->children;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
160 while(child) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
161 printf("%s\n", child->name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
162 child = child->next;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
163 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 return 0;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
168 int cmd_get(CmdArgs *a) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
169 if(a->argc == 0) {
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 return -1;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
173 DavContext *ctx = dav_context_new();
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
174 dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
175 DavSession *sn = NULL;
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
176 char *url = a->argv[0];
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
177 char *root = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
178 char *path = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
179 url_get_parts(url, &root, &path);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
181 Repository *repo = get_repository(root);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
182 if(repo) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
183 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 } else {
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
185 sn = dav_session_new(ctx, root);
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
188 DavResource *res = dav_get(sn, path, "U:crypto-key");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
189 if(!res) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
190 fprintf(stderr, "error\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
191 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
192 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
193 FILE *out = fopen(res->name, "w");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
194 if(!out) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
195 fprintf(stderr, "cannot open output file\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
196 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
197 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
198
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
199 AESDecrypter *dec = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
200 char *keyprop = dav_get_property_ns(res, "http://www.uap-core.de/", "crypto-key");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
201 if(repo) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
202 Key *key = get_key(keyprop);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
203 if(repo->encrypt && key) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
204 dec = aes_decrypter_new(key, out, (dav_write_func)fwrite);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
205 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
206 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
207
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
208 int ret;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
209 if(dec && keyprop) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
210 ret = dav_get_content(res, dec, (dav_write_func)aes_write);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
211 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
212 ret = dav_get_content(res, out, (dav_write_func)fwrite);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
213 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
214 if(dec) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
215 aes_decrypter_close(dec);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
216 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
217 fclose(out);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
218 if(ret) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
219 unlink(res->name);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
220 }
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 return 0;
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
225 int cmd_put(CmdArgs *a) {
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
226 if(a->argc < 2) {
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
227 return -1;
3
323689ada09d disabled ssl cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
228 }
323689ada09d disabled ssl cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
229
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
230 DavContext *ctx = dav_context_new();
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
231 DavSession *sn = NULL;
11
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
232 char *url = a->argv[0];
5db6178d8b58 added usage info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
233 char *file = a->argv[1];
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
234 char *root = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
235 char *path = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
236 url_get_parts(url, &root, &path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
237
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
238 Repository *repo = get_repository(root);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
239 if(repo) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
240 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
241 } else {
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
242 sn = dav_session_new(ctx, root);
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
243 }
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
244
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
245 DavResource *res = dav_resource_new(sn, path);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
246 if(!res) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
247 fprintf(stderr, "error\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
248 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
249 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
250 FILE *in = fopen(file, "r");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
251 if(!in) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
252 fprintf(stderr, "cannot open input file\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
253 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
254 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
255 AESEncrypter *enc = NULL;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
256 if(repo) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
257 Key *key = get_key(repo->default_key);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
258 if(repo->encrypt && key) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
259 enc = aes_encrypter_new(key, in, (dav_read_func)fread);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
260 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
261 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
262 if(enc) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
263 dav_set_content(res, enc, (dav_read_func)aes_read);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
264 dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", repo->default_key);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
265 } else {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
266 dav_set_content(res, in, (dav_read_func)fread);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
267 }
4
ae5a98f0545c added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
268
5
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
269 if(dav_store(res)) {
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
270 fprintf(stderr, "cannot upload file\n");
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
271 fclose(in);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
272 return -1;
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
273 }
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
274 fclose(in);
88625853ae74 new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
275 return 0;
1
1bcaac272cdf added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }

mercurial