Tue, 13 Aug 2013 13:51:00 +0200
added some error messages
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 | 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 | 35 | #include <sys/types.h> |
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 | |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
45 | static DavContext *ctx; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
46 | |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
47 | void xmlerrorfnc(void * c, const char * msg, ... ) { |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | // nothing |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | 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
|
52 | xmlGenericErrorFunc fnc = xmlerrorfnc; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | initGenericErrorDefaultFunc(&fnc); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
54 | load_config(); |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
55 | ctx = dav_context_new(); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
56 | dav_add_namespace(ctx, "U", "http://www.uap-core.de/"); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | if(argc < 2) { |
11 | 59 | fprintf(stderr, "Missing command\n"); |
60 | print_usage(argv[0]); | |
61 | return -1; | |
62 | } | |
63 | ||
64 | char *cmd = argv[1]; | |
65 | CmdArgs *args = cmd_parse_args(argc - 2, argv + 2); | |
66 | if(!args) { | |
67 | print_usage(argv[0]); | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | return -1; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
11 | 71 | if(!strcmp(cmd, "list") || !strcmp(cmd, "ls")) { |
72 | return cmd_list(args); | |
73 | } else if(!strcmp(cmd, "get")) { | |
74 | return cmd_get(args); | |
75 | } else if(!strcmp(cmd, "put")) { | |
76 | return cmd_put(args); | |
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 | 79 | print_usage(argv[0]); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | return -1; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | |
11 | 83 | void print_usage(char *cmd) { |
84 | fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); | |
85 | fprintf(stderr, "Commands:\n"); | |
86 | fprintf(stderr, " list [-al] <url>\n"); | |
87 | fprintf(stderr, " get [-p] [-k <key>] [-o <file>] <url>\n"); | |
88 | fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n"); | |
89 | fprintf(stderr, "\n"); | |
90 | fprintf(stderr, "Options:\n"); | |
91 | fprintf(stderr, | |
92 | " -k <key> Key to use for encryption or decryption\n"); | |
93 | fprintf(stderr, " -p Don't encrypt or decrypt files\n"); | |
94 | fprintf(stderr, " -o <file> Write output to file\n"); | |
95 | fprintf(stderr, "\n"); | |
96 | fprintf(stderr, | |
97 | "Instead of an url you can pass a repository name " | |
98 | "with an optional path:\n"); | |
99 | fprintf(stderr, " <repository>/path/\n"); | |
100 | fprintf(stderr, "\n"); | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
103 | 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
|
104 | 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
|
105 | *root = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
106 | *path = NULL; |
4 | 107 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
108 | int s; |
11 | 109 | 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
|
110 | s = 7; |
11 | 111 | } 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
|
112 | s = 8; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
113 | } else { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
114 | s = 1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
115 | } |
4 | 116 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
117 | 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
|
118 | char c = url[i]; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
119 | if(c == '/') { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
120 | 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
|
121 | 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
|
122 | if(p.length == 0) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
123 | p = sstrn("/", 1); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
124 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
125 | *root = sstrdup(r).ptr; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
126 | *path = sstrdup(p).ptr; |
4 | 127 | return; |
128 | } | |
129 | } | |
130 | ||
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
131 | *root = strdup(url); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
132 | *path = strdup("/"); |
4 | 133 | } |
134 | ||
13
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
135 | void print_resource_error(DavSession *sn, DavResource *res) { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
136 | char *res_url = util_concat_path(sn->base_url, res->path); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
137 | switch(sn->error) { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
138 | default: { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
139 | fprintf(stderr, "Cannot get resource %s.\n", res_url); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
140 | break; |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
141 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
142 | case DAV_NOT_FOUND: { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
143 | fprintf(stderr, "Resource %s not found.\n", res_url); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
144 | break; |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
145 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
146 | case DAV_UNAUTHORIZED: { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
147 | fprintf(stderr, "Authentication required.\n"); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
148 | break; |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
149 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
150 | case DAV_FORBIDDEN: { |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
151 | fprintf(stderr, "Access forbidden.\n"); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
152 | break; |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
153 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
154 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
155 | free(res_url); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
156 | } |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
157 | |
4 | 158 | |
11 | 159 | int cmd_list(CmdArgs *a) { |
160 | if(a->argc == 0) { | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | return -1; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
164 | DavSession *sn = NULL; |
11 | 165 | 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
|
166 | char *root = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
167 | char *path = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
168 | url_get_parts(url, &root, &path); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
170 | 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
|
171 | if(repo) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
172 | 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
|
173 | } else { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
174 | sn = dav_session_new(ctx, root); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
177 | //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
|
178 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
179 | 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
|
180 | if(!ls) { |
13
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
181 | print_resource_error(sn, ls); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
182 | return -1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
183 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
184 | DavResource *child = ls->children; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
185 | while(child) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
186 | 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
|
187 | child = child->next; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
188 | } |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | return 0; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | |
11 | 193 | int cmd_get(CmdArgs *a) { |
194 | if(a->argc == 0) { | |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | return -1; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
198 | DavSession *sn = NULL; |
11 | 199 | 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
|
200 | char *root = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
201 | char *path = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
202 | url_get_parts(url, &root, &path); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
204 | 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
|
205 | if(repo) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
206 | 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
|
207 | } else { |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
208 | sn = dav_session_new(ctx, root); |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
211 | 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
|
212 | if(!res) { |
13
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
213 | print_resource_error(sn, res); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
214 | return -1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
215 | } |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
216 | |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
217 | /* |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
218 | * determine the output file |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
219 | * use stdout if the output file is - |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
220 | */ |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
221 | char *outfile = cmd_getoption(a, "output"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
222 | if(!outfile) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
223 | outfile = res->name; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
224 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
225 | FILE *out = !strcmp(outfile, "-") ? stdout : fopen(outfile, "w"); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
226 | if(!out) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
227 | 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
|
228 | return -1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
229 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
230 | |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
231 | |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
232 | /* |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
233 | * if the -p (plain) option is specified we don't decrypt files |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
234 | * use a key specified with the -k (key) option, a key from the |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
235 | * key property or the repository default key |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
236 | */ |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
237 | void *out_stream = out; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
238 | dav_write_func write_func = (dav_write_func)fwrite; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
239 | AESDecrypter *dec = NULL; |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
240 | char *plain = cmd_getoption(a, "plain"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
241 | char *keyname = cmd_getoption(a, "key"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
242 | if(!plain) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
243 | char *keyprop = dav_get_property_ns( |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
244 | res, |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
245 | "http://www.uap-core.de/", |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
246 | "crypto-key"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
247 | Key *key = NULL; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
248 | char *kn = NULL; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
249 | if(keyname) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
250 | kn = keyname; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
251 | } else if(keyprop) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
252 | kn = keyprop; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
253 | } else if(repo && repo->decrypt) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
254 | kn = repo->default_key; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
255 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
256 | if(kn) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
257 | key = get_key(kn); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
258 | if(!key) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
259 | fprintf(stderr, "Key %s not found!\nAbort.\n", kn); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
260 | // TODO: free |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
261 | return -1; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
262 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
263 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
264 | |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
265 | if(key) { |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
266 | dec = aes_decrypter_new(key, out, (dav_write_func)fwrite); |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
267 | out_stream = dec; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
268 | write_func = (dav_write_func)aes_write; |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
269 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
270 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
271 | |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
272 | int ret = dav_get_content(res, out_stream, write_func); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
273 | if(dec) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
274 | aes_decrypter_close(dec); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
275 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
276 | fclose(out); |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
277 | if(ret && strcmp(outfile, "-")) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
278 | unlink(outfile); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
279 | } |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
280 | |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
281 | return 0; |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | } |
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | |
11 | 284 | int cmd_put(CmdArgs *a) { |
285 | if(a->argc < 2) { | |
4 | 286 | return -1; |
3
323689ada09d
disabled ssl cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
287 | } |
323689ada09d
disabled ssl cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
288 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
289 | DavSession *sn = NULL; |
11 | 290 | char *url = a->argv[0]; |
291 | 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
|
292 | char *root = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
293 | char *path = NULL; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
294 | 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
|
295 | |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
296 | 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
|
297 | if(repo) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
298 | sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); |
4 | 299 | } else { |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
300 | sn = dav_session_new(ctx, root); |
4 | 301 | } |
302 | ||
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
303 | /* |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
304 | * use stdin if the input file is - |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
305 | */ |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
306 | FILE *in = !strcmp(file, "-") ? in : fopen(file, "r"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
307 | if(!in) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
308 | fprintf(stderr, "cannot open input file\n"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
309 | return -1; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
310 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
311 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
312 | 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
|
313 | if(!res) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
314 | fprintf(stderr, "error\n"); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
315 | return -1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
316 | } |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
317 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
318 | AESEncrypter *enc = NULL; |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
319 | char *keyname = cmd_getoption(a, "key"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
320 | char *kn = NULL; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
321 | char *plain = cmd_getoption(a, "plain"); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
322 | if(!plain && (keyname || repo)) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
323 | kn = keyname ? keyname : repo->default_key; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
324 | Key *key = get_key(kn); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
325 | if(!key) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
326 | fprintf(stderr, "Key %s not found!\nAbort.\n", kn); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
327 | // TODO: free |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
328 | return -1; |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
329 | } |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
330 | if(keyname || repo->encrypt) { |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
331 | 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
|
332 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
333 | } |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
334 | if(enc) { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
335 | dav_set_content(res, enc, (dav_read_func)aes_read); |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
336 | dav_set_property_ns(res, "http://www.uap-core.de/", "crypto-key", kn); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
337 | } else { |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
338 | 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
|
339 | } |
4 | 340 | |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
341 | if(dav_store(res)) { |
13
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
342 | print_resource_error(sn, res); |
8a0cc4d90de7
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
12
diff
changeset
|
343 | fprintf(stderr, "Cannot upload file.\n"); |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
344 | fclose(in); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
345 | return -1; |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
346 | } |
12
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
347 | if(enc) { |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
348 | aes_encrypter_close(enc); |
493128ef1b12
added some options to put and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
349 | } |
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
350 | fclose(in); |
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
351 | return 0; |
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
352 | } |