libidav/utils.c

changeset 43
03076907b58a
parent 40
a95ee94b9204
child 66
f8c1f685e08e
equal deleted inserted replaced
42:6518b035a9df 43:03076907b58a
30 #include <stdio.h> 30 #include <stdio.h>
31 #include <stdlib.h> 31 #include <stdlib.h>
32 #include <string.h> 32 #include <string.h>
33 #include <ucx/string.h> 33 #include <ucx/string.h>
34 #include <ucx/buffer.h> 34 #include <ucx/buffer.h>
35 #include <ucx/utils.h>
35 #include <libxml/tree.h> 36 #include <libxml/tree.h>
36 #include <curl/curl.h> 37 #include <curl/curl.h>
37 38
38 #include <openssl/sha.h> 39 #include <openssl/sha.h>
39 #include <openssl/hmac.h> 40 #include <openssl/hmac.h>
40 #include <openssl/evp.h> 41 #include <openssl/evp.h>
41 #include <openssl/bio.h> 42 #include <openssl/bio.h>
42 #include <openssl/buffer.h> 43 #include <openssl/buffer.h>
44 #include <openssl/rand.h>
43 45
44 #include "utils.h" 46 #include "utils.h"
47 #include "crypto.h"
45 #include "webdav.h" 48 #include "webdav.h"
46 49
47 50
48 time_t util_parse_creationdate(char *str) { 51 time_t util_parse_creationdate(char *str) {
49 // example: 2012-11-29T21:35:35Z 52 // example: 2012-11-29T21:35:35Z
173 } 176 }
174 177
175 return url.ptr; 178 return url.ptr;
176 } 179 }
177 180
178 void util_set_url(DavSession *sn, char *path) { 181 void util_set_url(DavSession *sn, char *href) {
179 if(path) { 182 sstr_t base = sstr(sn->base_url);
180 char *url = util_path_to_url(sn, path); 183 sstr_t href_str = sstr(href);
181 curl_easy_setopt(sn->handle, CURLOPT_URL, url); 184
182 free(url); 185 char *base_path = util_url_path(sn->base_url);
183 } else { 186 base.length -= strlen(base_path);
184 curl_easy_setopt(sn->handle, CURLOPT_URL, sn->base_url); 187
185 } 188 sstr_t url;
189 url.length = base.length + href_str.length;
190 url.ptr = malloc(url.length + 1);
191 url.ptr[url.length] = '\0';
192 url = sstrncat(url, 2, base, href_str);
193
194 curl_easy_setopt(sn->handle, CURLOPT_URL, url);
195 free(url.ptr);
186 } 196 }
187 197
188 char* util_path_to_url(DavSession *sn, char *path) { 198 char* util_path_to_url(DavSession *sn, char *path) {
189 UcxBuffer *url = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND); 199 UcxBuffer *url = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
190 200
198 sstr_t *tks = sstrsplit(p, S("/"), &ntk); 208 sstr_t *tks = sstrsplit(p, S("/"), &ntk);
199 209
200 for(int i=0;i<ntk;i++) { 210 for(int i=0;i<ntk;i++) {
201 sstr_t node = tks[i]; 211 sstr_t node = tks[i];
202 if(node.length > 0) { 212 if(node.length > 0) {
203 // TODO: implement file name encryption
204 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length); 213 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length);
205 ucx_buffer_putc(url, '/'); 214 ucx_buffer_putc(url, '/');
206 ucx_buffer_write(esc, 1, strlen(esc), url); 215 ucx_buffer_write(esc, 1, strlen(esc), url);
207 curl_free(esc); 216 curl_free(esc);
208 free(node.ptr); 217 free(node.ptr);
282 out[mem->length - 1] = '\0'; 291 out[mem->length - 1] = '\0';
283 292
284 BIO_free_all(e); 293 BIO_free_all(e);
285 294
286 return out; 295 return out;
296 }
297
298 char* util_encrypt_str(DavSession *sn, char *str, char *key) {
299 DavKey *k = dav_context_get_key(sn->context, key);
300 if(!k) {
301 // TODO: session error
302 return NULL;
303 }
304
305 char *enc_str = aes_encrypt(str, k);
306 char *ret_str = dav_session_strdup(sn, enc_str);
307 free(enc_str);
308 return ret_str;
309 }
310
311 char* util_decrypt_str(DavSession *sn, char *str, char *key) {
312 DavKey *k = dav_context_get_key(sn->context, key);
313 if(!k) {
314 // TODO: session error
315 return NULL;
316 }
317
318 char *dec_str = aes_decrypt(str, k);
319 char *ret_str = dav_session_strdup(sn, dec_str);
320 free(dec_str);
321 return ret_str;
322 }
323
324 char* util_random_str() {
325 unsigned char *str = malloc(25);
326 str[24] = '\0';
327
328 sstr_t t = S(
329 "01234567890"
330 "abcdefghijklmnopqrstuvwxyz"
331 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
332 const unsigned char *table = (const unsigned char*)t.ptr;
333
334 RAND_pseudo_bytes(str, 24);
335 for(int i=0;i<24;i++) {
336 int c = str[i] % t.length;
337 str[i] = table[c];
338 }
339
340 return (char*)str;
287 } 341 }
288 342
289 /* 343 /*
290 * gets a substring from 0 to the appearance of the token 344 * gets a substring from 0 to the appearance of the token
291 * tokens are separated by space 345 * tokens are separated by space

mercurial