Thu, 15 Oct 2015 16:16:44 +0200
simplified do_propfind_request
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
75
56962faf2b42
copyright update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
74
diff
changeset
|
4 | * Copyright 2015 Olaf Wintermann. All rights reserved. |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
0bbbb0341606
some code cleanup and bug fixes
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 |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <time.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdio.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <stdlib.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <string.h> |
124
41939c8f3f9c
expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
33 | #include <errno.h> |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
34 | #include <ctype.h> |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <ucx/string.h> |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
36 | #include <ucx/buffer.h> |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
37 | #include <ucx/utils.h> |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | #include <libxml/tree.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include <curl/curl.h> |
152
ff854d3df20c
fixed windows build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
151
diff
changeset
|
40 | |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
41 | #ifdef _WIN32 |
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
42 | #include <conio.h> |
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
43 | #define getpasswordchar() getch() |
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
44 | #else |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
45 | #include <termios.h> |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
46 | #define getpasswordchar() getchar() |
152
ff854d3df20c
fixed windows build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
151
diff
changeset
|
47 | #endif |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | #include <openssl/sha.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | #include <openssl/hmac.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | #include <openssl/evp.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | #include <openssl/bio.h> |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | #include <openssl/buffer.h> |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
54 | #include <openssl/rand.h> |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | #include "utils.h" |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
57 | #include "crypto.h" |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
58 | #include "webdav.h" |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
60 | static int parse_iso8601(char *str, time_t *result) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
61 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
62 | // safety |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
63 | if(!str || !result) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
64 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
65 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
66 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
67 | struct tm tparts; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
68 | memset(&tparts, 0, sizeof(struct tm)); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
69 | *result = 0; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
70 | long val; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
71 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
72 | // skip leading spaces |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
73 | while(isspace(*str)) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
74 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
75 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
76 | |
161
97a52ce52b1a
don't allow signs for date strings (always expect full years)
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
77 | // ensure we have numeric values |
97a52ce52b1a
don't allow signs for date strings (always expect full years)
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
78 | if(!isdigit(*str)) { |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
79 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
80 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
81 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
82 | // starting parsing the year |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
83 | val = strtoul(str, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
84 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
85 | if(*str == '-') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
86 | // month (and day) seem to be dash separated |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
87 | tparts.tm_year = val - 1900; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
88 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
89 | tparts.tm_mon = strtoul(str, &str, 10) - 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
90 | |
161
97a52ce52b1a
don't allow signs for date strings (always expect full years)
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
91 | if(*str++ != '-') { |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
92 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
93 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
94 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
95 | tparts.tm_mday = strtoul(str, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
96 | } else { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
97 | // year, month, day was parsed as one big integer |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
98 | tparts.tm_mday = val % 100; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
99 | tparts.tm_mon = (val % 10000) / 100 - 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
100 | tparts.tm_year = val / 10000 - 1900; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
101 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
102 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
103 | // time separator |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
104 | if(*str != 'T') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
105 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
106 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
107 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
108 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
109 | // ensure we have numeric values (unsigned) |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
110 | if(!isdigit(*str)) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
111 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
112 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
113 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
114 | // start parsing the hour |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
115 | val = strtoul(str, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
116 | if(*str == ':') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
117 | // minutes (and seconds) are separated by colon |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
118 | tparts.tm_hour = val; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
119 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
120 | tparts.tm_min = strtoul(str, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
121 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
122 | if(*str++ != ':') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
123 | return 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
124 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
125 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
126 | tparts.tm_sec = strtoul(str, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
127 | } else { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
128 | // minutes (and seconds) are one big integer |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
129 | tparts.tm_sec = val % 100; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
130 | tparts.tm_min = (val % 10000) / 100; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
131 | tparts.tm_hour = val / 10000; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
132 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
133 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
134 | // parse fractional seconds, but skip them (we return a time_t) |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
135 | if(*str == ',' || *str == '.') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
136 | do { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
137 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
138 | } while(isdigit(*str)); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
139 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
140 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
141 | // parse time zone (if any) |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
142 | if(*str == 'Z') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
143 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
144 | *result = mktime(&tparts) - timezone; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
145 | } else if (*str == '+' || *str == '-') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
146 | int sign = (*str == '+') ? -1 : 1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
147 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
148 | val = strtoul(str + 1, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
149 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
150 | if (*str == ':') { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
151 | val = 60 * val + strtoul(str + 1, &str, 10); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
152 | } else { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
153 | val = 60 * (val / 100) + (val % 100); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
154 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
155 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
156 | *result = mktime(&tparts) - timezone + (time_t) (60 * val * sign); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
157 | } else { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
158 | // local time |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
159 | tparts.tm_isdst = -1; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
160 | *result = mktime(&tparts); |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
161 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
162 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
163 | // skip trailing spaces |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
164 | while(isspace(*str)) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
165 | str++; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
166 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
167 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
168 | // string must be zero terminated, no further characters may follow |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
169 | return *str != '\0'; // return zero on success |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
170 | } |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
171 | |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | time_t util_parse_creationdate(char *str) { |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
174 | // parse a ISO-8601 date (rfc-3339) |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | // example: 2012-11-29T21:35:35Z |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | if(!str) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | return 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | } |
160
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
179 | |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
180 | time_t result; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
181 | if(!parse_iso8601(str, &result)) { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
182 | return result; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
183 | } else { |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
184 | return 0; |
ba8f1691c0ee
added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents:
159
diff
changeset
|
185 | } |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | time_t util_parse_lastmodified(char *str) { |
158
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
189 | // parse a rfc-1123 date |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | // example: Thu, 29 Nov 2012 21:35:35 GMT |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | if(!str) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | return 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | } else { |
158
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
194 | time_t result = curl_getdate(str, NULL); |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
195 | if(result == -1) { |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
196 | // some shit server don't comply with the WebDAV standard and |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
197 | // use ISO-8601 dates for lastmodified (e.g. Microsoft Sharepoint) |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
198 | return util_parse_creationdate(str); |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
199 | } else { |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
200 | return result; |
33237261321e
added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
201 | } |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | int util_getboolean(char *v) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | if(v[0] == 'T' || v[0] == 't') { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | return 1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | return 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | int util_strtoint(char *str, int64_t *value) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | char *end; |
124
41939c8f3f9c
expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
214 | errno = 0; |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | int64_t val = strtoll(str, &end, 0); |
124
41939c8f3f9c
expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
216 | if(errno == 0) { |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | *value = val; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
218 | return 1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | return 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223 | |
151
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
224 | char* util_url_base(char *url) { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
225 | sstr_t u = sstr(url); |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
226 | int len = u.length; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
227 | int slashcount = 0; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
228 | int slmax; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
229 | if(len > 7 && !strncasecmp(url, "http://", 7)) { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
230 | slmax = 3; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
231 | } else if(len > 8 && !strncasecmp(url, "https://", 8)) { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
232 | slmax = 3; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
233 | } else { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
234 | slmax = 1; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
235 | } |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
236 | char c; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
237 | int i = 0; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
238 | for(i=0;i<len;i++) { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
239 | c = url[i]; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
240 | if(c == '/') { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
241 | slashcount++; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
242 | if(slashcount == slmax) { |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
243 | i++; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
244 | break; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
245 | } |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
246 | } |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
247 | } |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
248 | sstr_t server = sstrsubsl(u, 0, i); |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
249 | server = sstrdup(server); |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
250 | return server.ptr; |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
251 | } |
a316613205dc
fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
150
diff
changeset
|
252 | |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | char* util_url_path(char *url) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | char *path = NULL; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | size_t len = strlen(url); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | int slashcount = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | int slmax; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | if(len > 7 && !strncasecmp(url, "http://", 7)) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | slmax = 3; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | } else if(len > 8 && !strncasecmp(url, "https://", 8)) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | slmax = 3; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263 | slmax = 1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | char c; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | for(int i=0;i<len;i++) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | c = url[i]; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268 | if(c == '/') { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | slashcount++; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | if(slashcount == slmax) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | path = url + i; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | break; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276 | return path; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278 | |
66 | 279 | char* util_url_decode(DavSession *sn, char *url) { |
280 | char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL); | |
281 | char *ret = strdup(unesc); | |
282 | curl_free(unesc); | |
283 | return ret; | |
284 | } | |
285 | ||
170
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
286 | static size_t util_header_callback(char *buffer, size_t size, |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
287 | size_t nitems, void *data) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
288 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
289 | char *duped; // local variable for string duplicates |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
290 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
291 | UcxMap *map = (UcxMap*) data; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
292 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
293 | // if we get a status line, clear the map and exit |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
294 | if(size*nitems >= 5 && !strncmp("HTTP/", buffer, 5)) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
295 | ucx_map_free_content(map, free); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
296 | ucx_map_clear(map); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
297 | return size*nitems; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
298 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
299 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
300 | // if we get the terminating CRLF, just exit |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
301 | if (size*nitems == 2 && !strncmp("\r\n", buffer, 2)) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
302 | return 2; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
303 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
304 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
305 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
306 | // get header key |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
307 | size_t s = 0; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
308 | do { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
309 | s++; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
310 | } while(buffer[s] != ':'); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
311 | char *value = buffer+s+1; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
312 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
313 | // remove trailing spaces |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
314 | while(isspace(buffer[s-1])) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
315 | s--; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
316 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
317 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
318 | // save key as all lower case |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
319 | duped = malloc(s); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
320 | for(size_t i = 0;i<s;i++) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
321 | duped[i] = tolower(buffer[i]); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
322 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
323 | UcxKey key = ucx_key(duped, s); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
324 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
325 | // get trimmed header value |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
326 | while(isspace(*value)) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
327 | value++; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
328 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
329 | s = size*nitems - (value - buffer); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
330 | while(isspace(value[s-1])) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
331 | s--; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
332 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
333 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
334 | // copy header value and append NULL byte (curl does not provide one) |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
335 | duped = malloc(s+1); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
336 | memcpy(duped, value, s+1); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
337 | duped[s] = '\0'; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
338 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
339 | char *oldval = ucx_map_get(map, key); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
340 | if(oldval) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
341 | free(oldval); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
342 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
343 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
344 | ucx_map_put(map, key, duped); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
345 | free(key.data); // ucx_map_put made a copy, so we free this memory |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
346 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
347 | return nitems * size; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
348 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
349 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
350 | void util_capture_header(CURL *handle, UcxMap* map) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
351 | if(!map) { |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
352 | // deactivate capturing |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
353 | curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
354 | return; |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
355 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
356 | |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
357 | curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, util_header_callback); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
358 | curl_easy_setopt(handle, CURLOPT_HEADERDATA, map); |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
359 | } |
cf054cded046
added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
360 | |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361 | char* util_resource_name(char *url) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362 | int si = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363 | int osi = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364 | int i = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365 | int p = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
366 | char c; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367 | while((c = url[i]) != 0) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368 | if(c == '/') { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369 | osi = si; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370 | si = i; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
371 | p = 1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
372 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | i++; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
374 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
375 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | char *name = url + si + p; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
377 | if(name[0] == 0) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
378 | name = url + osi + p; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
379 | if(name[0] == 0) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
380 | return url; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
382 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
383 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | return name; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
387 | int util_mkdir(char *path, mode_t mode) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388 | #ifdef _WIN32 |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
389 | return mkdir(path); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390 | #else |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | return mkdir(path, mode); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
392 | #endif |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
393 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | char* util_concat_path(char *url_base, char *p) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396 | sstr_t base = sstr(url_base); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397 | sstr_t path; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398 | if(p) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399 | path = sstr(p); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | path = sstrn("", 0); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | int add_separator = 0; |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
405 | if(base.length != 0 && base.ptr[base.length-1] == '/') { |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | if(path.ptr[0] == '/') { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | base.length--; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | if(path.length == 0 || path.ptr[0] != '/') { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | add_separator = 1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
415 | sstr_t url; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
416 | if(add_separator) { |
70 | 417 | url = sstrcat(3, base, sstr("/"), path); |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
418 | } else { |
70 | 419 | url = sstrcat(2, base, path); |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
420 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | return url.ptr; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
425 | void util_set_url(DavSession *sn, char *href) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
426 | sstr_t base = sstr(sn->base_url); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
427 | sstr_t href_str = sstr(href); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
428 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
429 | char *base_path = util_url_path(sn->base_url); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
430 | base.length -= strlen(base_path); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
431 | |
70 | 432 | sstr_t url = sstrcat(2, base, href_str); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
433 | |
71 | 434 | curl_easy_setopt(sn->handle, CURLOPT_URL, url.ptr); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
435 | free(url.ptr); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
436 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
437 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
438 | char* util_path_to_url(DavSession *sn, char *path) { |
69
0dbdd7e8c1fc
fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
439 | char *space = malloc(256); |
0dbdd7e8c1fc
fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
440 | UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
441 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
442 | // add base url |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
443 | ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
444 | // remove trailing slash |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
445 | ucx_buffer_seek(url, -1, SEEK_CUR); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
446 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
447 | sstr_t p = sstr(path); |
74
da079dc0724c
fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
448 | ssize_t ntk = 0; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
449 | sstr_t *tks = sstrsplit(p, S("/"), &ntk); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
450 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
451 | for(int i=0;i<ntk;i++) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
452 | sstr_t node = tks[i]; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
453 | if(node.length > 0) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
454 | char *esc = curl_easy_escape(sn->handle, node.ptr, node.length); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
455 | ucx_buffer_putc(url, '/'); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
456 | ucx_buffer_write(esc, 1, strlen(esc), url); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
457 | curl_free(esc); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
458 | } |
74
da079dc0724c
fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
459 | free(node.ptr); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
460 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
461 | free(tks); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
462 | if(path[p.length-1] == '/') { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
463 | ucx_buffer_putc(url, '/'); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
464 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
465 | ucx_buffer_putc(url, 0); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
466 | |
69
0dbdd7e8c1fc
fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
467 | space = url->space; |
0dbdd7e8c1fc
fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
468 | ucx_buffer_free(url); |
0dbdd7e8c1fc
fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
469 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
470 | return space; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
471 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
472 | |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
473 | char* util_parent_path(char *path) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
474 | char *name = util_resource_name(path); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
475 | size_t namelen = strlen(name); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
476 | size_t pathlen = strlen(path); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
477 | size_t parentlen = pathlen - namelen; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
478 | char *parent = malloc(parentlen + 1); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
479 | memcpy(parent, path, parentlen); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
480 | parent[parentlen] = '\0'; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
481 | return parent; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
482 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
483 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
484 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
485 | char* util_xml_get_text(xmlNode *elm) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
486 | xmlNode *node = elm->children; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
487 | while(node) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488 | if(node->type == XML_TEXT_NODE) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
489 | return (char*)node->content; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
490 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
491 | node = node->next; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
492 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
493 | return NULL; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
494 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
495 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
496 | |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
497 | char* util_base64decode(char *in) { |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
498 | int len = 0; |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
499 | return util_base64decode_len(in, &len); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
500 | } |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
501 | |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
502 | char* util_base64decode_len(char* in, int *outlen) { |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
503 | size_t len = strlen(in); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
504 | char *out = calloc(1, len); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
505 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
506 | BIO *b = BIO_new_mem_buf(in, len); |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
507 | BIO *d = BIO_new(BIO_f_base64()); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
508 | BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
509 | b = BIO_push(d, b); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
510 | |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
511 | *outlen = BIO_read(b, out, len); |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
512 | BIO_free_all(b); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
513 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
514 | return out; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
515 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
516 | |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
517 | char* util_base64encode(char *in, size_t len) { |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
518 | BIO *b; |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
519 | BIO *e; |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
520 | BUF_MEM *mem; |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
521 | |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
522 | e = BIO_new(BIO_f_base64()); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
523 | b = BIO_new(BIO_s_mem()); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
524 | BIO_set_flags(e, BIO_FLAGS_BASE64_NO_NL); |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
525 | |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
526 | e = BIO_push(e, b); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
527 | BIO_write(e, in, len); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
528 | BIO_flush(e); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
529 | |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
530 | BIO_get_mem_ptr(e, &mem); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
531 | char *out = malloc(mem->length + 1); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
532 | memcpy(out, mem->data, mem->length); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
533 | out[mem->length] = '\0'; |
39
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
534 | |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
535 | BIO_free_all(e); |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
536 | |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
537 | return out; |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
538 | } |
3e55bed345f9
added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
33
diff
changeset
|
539 | |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
540 | char* util_encrypt_str(DavSession *sn, char *str, char *key) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
541 | DavKey *k = dav_context_get_key(sn->context, key); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
542 | if(!k) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
543 | // TODO: session error |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
544 | return NULL; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
545 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
546 | |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
547 | return util_encrypt_str_k(sn, str, k); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
548 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
549 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
550 | char* util_encrypt_str_k(DavSession *sn, char *str, DavKey *key) { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
551 | char *enc_str = aes_encrypt(str, strlen(str), key); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
552 | char *ret_str = dav_session_strdup(sn, enc_str); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
553 | free(enc_str); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
554 | return ret_str; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
555 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
556 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
557 | char* util_decrypt_str(DavSession *sn, char *str, char *key) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
558 | DavKey *k = dav_context_get_key(sn->context, key); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
559 | if(!k) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
560 | // TODO: session error |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
561 | return NULL; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
562 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
563 | |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
564 | return util_decrypt_str_k(sn, str, k); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
565 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
566 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
567 | char* util_decrypt_str_k(DavSession *sn, char *str, DavKey *key) { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
568 | size_t len = 0; |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
569 | char *dec_str = aes_decrypt(str, &len, key); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
570 | char *ret_str = dav_session_strdup(sn, dec_str); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
571 | free(dec_str); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
572 | return ret_str; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
573 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
574 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
575 | char* util_random_str() { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
576 | unsigned char *str = malloc(25); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
577 | str[24] = '\0'; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
578 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
579 | sstr_t t = S( |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
580 | "01234567890" |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
581 | "abcdefghijklmnopqrstuvwxyz" |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
582 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
583 | const unsigned char *table = (const unsigned char*)t.ptr; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
584 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
585 | RAND_pseudo_bytes(str, 24); |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
586 | for(int i=0;i<24;i++) { |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
587 | int c = str[i] % t.length; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
588 | str[i] = table[c]; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
589 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
590 | |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
591 | return (char*)str; |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
592 | } |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
593 | |
33
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
594 | /* |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
595 | * gets a substring from 0 to the appearance of the token |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
596 | * tokens are separated by space |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
597 | * sets sub to the substring and returns the remaining string |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
598 | */ |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
599 | sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
600 | int i; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
601 | int token_start = -1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
602 | int token_end = -1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
603 | for(i=0;i<=str.length;i++) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
604 | int c; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
605 | if(i == str.length) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
606 | c = ' '; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
608 | c = str.ptr[i]; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
609 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
610 | if(c < 33) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
611 | if(token_start != -1) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
612 | token_end = i; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
613 | size_t len = token_end - token_start; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
614 | sstr_t tk = sstrsubsl(str, token_start, len); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
615 | //printf("token: {%.*s}\n", token.length, token.ptr); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
616 | if(!sstrcmp(tk, token)) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
617 | *sub = sstrtrim(sstrsubsl(str, 0, token_start)); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
618 | break; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
619 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
620 | token_start = -1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
621 | token_end = -1; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
622 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
623 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
624 | if(token_start == -1) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
625 | token_start = i; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
626 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
627 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
628 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
629 | |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
630 | if(i < str.length) { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
631 | return sstrtrim(sstrsubs(str, i)); |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
632 | } else { |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
633 | str.ptr = NULL; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
634 | str.length = 0; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
635 | return str; |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
636 | } |
0bbbb0341606
some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
637 | } |
119
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
638 | |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
639 | sstr_t util_readline(FILE *stream) { |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
640 | UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
641 | |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
642 | int c; |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
643 | while((c = fgetc(stream)) != '\n') { |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
644 | ucx_buffer_putc(buf, c); |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
645 | } |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
646 | |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
647 | sstr_t str = sstrdup(sstrtrim(sstrn(buf->space, buf->size))); |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
648 | ucx_buffer_free(buf); |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
649 | return str; |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
650 | } |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
651 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
652 | char* util_password_input(char *prompt) { |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
653 | fprintf(stderr, "%s", prompt); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
654 | fflush(stderr); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
655 | |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
656 | #ifndef _WIN32 |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
657 | // hide terminal input |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
658 | struct termios oflags, nflags; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
659 | tcgetattr(fileno(stdin), &oflags); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
660 | nflags = oflags; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
661 | nflags.c_lflag &= ~ECHO; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
662 | nflags.c_lflag |= ECHONL; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
663 | if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
664 | perror("tcsetattr"); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
665 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
666 | #endif |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
667 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
668 | // read password input |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
669 | UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
670 | int c = 0; |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
671 | while((c = getpasswordchar()) != EOF) { |
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
672 | if(c == '\n' || c == '\r') { |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
673 | break; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
674 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
675 | ucx_buffer_putc(buf, c); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
676 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
677 | ucx_buffer_putc(buf, 0); |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
678 | fflush(stdin); |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
679 | |
153
272173064319
implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents:
152
diff
changeset
|
680 | #ifndef _WIN32 |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
681 | // restore terminal settings |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
682 | if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
683 | perror("tcsetattr"); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
684 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
685 | #endif |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
686 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
687 | char *str = buf->space; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
688 | free(buf); // only free the UcxBuffer struct |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
689 | return str; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
690 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
691 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
692 | void util_generate_key(DavKey *key, char *password) { |
149
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
693 | key->data = malloc(SHA256_DIGEST_LENGTH); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
694 | key->length = SHA256_DIGEST_LENGTH; |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
695 | key->type = DAV_KEY_AES256; |
149
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
696 | #ifdef __sun |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
697 | SHA256_CTX sha256; |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
698 | SHA256Init(&sha256); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
699 | SHA256Update(&sha256, password, strlen(password)); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
700 | SHA256Final(key->data, &sha256); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
701 | #else |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
702 | SHA256_CTX sha256; |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
703 | SHA256_Init(&sha256); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
704 | SHA256_Update(&sha256, password, strlen(password)); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
705 | SHA256_Final(key->data, &sha256); |
509e9e1cbdcc
implemented password key support for Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
148
diff
changeset
|
706 | #endif |
147
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
707 | } |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
708 | |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
709 | char* util_key_input(DavContext *ctx, DavKey *key) { |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
710 | sstr_t prompt = ucx_sprintf("Enter password for key (%s): ", key->name); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
711 | char *password = util_password_input(prompt.ptr); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
712 | free(prompt.ptr); |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
713 | return password; |
458a8dc68048
added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
714 | } |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
715 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
716 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
717 | char* util_hexstr(unsigned char *data, size_t len) { |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
718 | size_t buflen = 2*len + 4; |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
719 | UcxBuffer *buf = ucx_buffer_new(malloc(buflen), buflen + 1, 0); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
720 | for(int i=0;i<len;i++) { |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
721 | ucx_bprintf(buf, "%x", data[i]); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
722 | } |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
723 | ucx_buffer_putc(buf, 0); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
724 | char *str = buf->space; |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
725 | ucx_buffer_free(buf); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
726 | return str; |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
149
diff
changeset
|
727 | } |