move control socket handling to separate file
[mizunara.git] / libidav / webdav.h
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2018 Olaf Wintermann. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef WEBDAV_H
30 #define WEBDAV_H
31
32 #include <inttypes.h>
33 #include <ucx/map.h>
34 #include <ucx/mempool.h>
35 #include <ucx/list.h>
36 #include <ucx/buffer.h>
37 #include <curl/curl.h>
38 #include <libxml/tree.h>
39
40 #ifdef  __cplusplus
41 extern "C" {
42 #endif
43
44 typedef char DavBool;
45 #ifndef TRUE
46 #define TRUE 1
47 #endif
48 #ifndef FALSE
49 #define FALSE 0
50 #endif
51
52 typedef struct DavContext    DavContext;
53 typedef struct DavProxy      DavProxy;
54 typedef struct DavSession    DavSession;
55 typedef struct DavResource   DavResource;
56 typedef struct DavResult     DavResult;
57 typedef struct DavNamespace  DavNamespace;
58 typedef struct DavProperty   DavProperty;
59 typedef struct DavPropName   DavPropName;
60 typedef struct DavKey        DavKey;
61 typedef struct DavNSInfo     DavNSInfo;
62 typedef struct DavXmlNode    DavXmlNode;
63 typedef struct DavXmlAttr    DavXmlAttr;
64
65 typedef size_t(*dav_read_func)(void*, size_t, size_t, void*);
66 typedef size_t(*dav_write_func)(const void*, size_t, size_t, void*);
67 typedef int(*dav_seek_func)(const void *, long, int);
68
69 typedef int(*dav_auth_func)(DavSession *, void *);
70 typedef void(*dav_progress_func)(DavResource *, int64_t, int64_t, void *);
71
72 enum DavError {
73     DAV_OK = 0,
74     DAV_ERROR,
75     DAV_NOT_FOUND,
76     DAV_UNAUTHORIZED,
77     DAV_FORBIDDEN,
78     DAV_METHOD_NOT_ALLOWED,
79     DAV_CONFLICT,
80     DAV_LOCKED,
81     DAV_UNSUPPORTED_PROTOCOL,
82     DAV_COULDNT_RESOLVE_PROXY,
83     DAV_COULDNT_RESOLVE_HOST,
84     DAV_COULDNT_CONNECT,
85     DAV_TIMEOUT,
86     DAV_SSL_ERROR,
87     DAV_QL_ERROR,
88     DAV_CONTENT_VERIFICATION_ERROR,
89     DAV_PRECONDITION_FAILED,
90     DAV_REQUEST_ENTITY_TOO_LARGE,
91     DAV_REQUEST_URL_TOO_LONG,
92     DAV_PROXY_AUTH_REQUIRED,
93     DAV_NET_AUTH_REQUIRED
94 };
95
96 typedef enum DavError DavError;
97
98 enum DavXmlNodeType {
99     DAV_XML_NONE = 0,
100     DAV_XML_ELEMENT,
101     DAV_XML_TEXT
102 };
103
104 typedef enum DavXmlNodeType DavXmlNodeType;
105
106 #define DAV_SESSION_ENCRYPT_CONTENT     0x0001
107 #define DAV_SESSION_ENCRYPT_NAME        0x0002
108 #define DAV_SESSION_ENCRYPT_PROPERTIES  0x0004
109 #define DAV_SESSION_DECRYPT_CONTENT     0x0008
110 #define DAV_SESSION_DECRYPT_NAME        0x0010
111 #define DAV_SESSION_DECRYPT_PROPERTIES  0x0020
112 #define DAV_SESSION_STORE_HASH          0x0040
113
114 #define DAV_SESSION_CONTENT_ENCRYPTION  0x0009
115 #define DAV_SESSION_FULL_ENCRYPTION     0x003f
116
117
118 #define DAV_NS       "http://davutils.org/"
119 #define DAV_PROPS_NS "http://davutils.org/props/"
120
121 struct DavNamespace {
122     char *prefix;
123     char *name;
124 };
125
126 struct DavResource {
127     DavSession    *session;
128     DavResource   *prev;
129     DavResource   *next;
130     DavResource   *parent;
131     DavResource   *children;
132     char          *name;
133     char          *path;
134     char          *href;
135     uint64_t      contentlength;
136     char          *contenttype;
137     time_t        creationdate;
138     time_t        lastmodified;
139     void          *data;
140     int           iscollection;
141     int           exists;
142 };
143
144 struct DavSession {
145     DavContext    *context;
146     CURL          *handle;
147     char          *base_url;
148     UcxMempool    *mp;
149     UcxMap        *pathcache;
150     DavKey        *key;
151     void          *locks;
152     uint32_t      flags;
153     DavError      error;
154     char          *errorstr;
155     
156     int(*auth_prompt)(DavSession *sn, void *userdata);
157     void *authprompt_userdata;
158     
159     void(*get_progress)(DavResource *res, int64_t total, int64_t now, void *userdata);
160     void(*put_progress)(DavResource *res, int64_t total, int64_t now, void *userdata);
161     void *progress_userdata;
162 };
163
164 struct DavContext {
165     UcxMap   *namespaces;
166     UcxMap   *namespaceinfo;
167     UcxMap   *keys;
168     UcxList  *sessions;
169     DavProxy *http_proxy;
170     DavProxy *https_proxy;
171 };
172
173 struct DavProxy {
174     char *url;
175     char *username;
176     char *password;
177     char *no_proxy;
178 };
179
180 struct DavProperty {
181     DavNamespace *ns;
182     char         *name;
183     DavXmlNode   *value;
184 };
185
186 struct DavPropName {
187     char *ns;
188     char *name;
189 };
190
191 struct DavResult {
192     DavResource *result;
193     int         status;
194 };
195
196 #define DAV_KEY_AES128     0
197 #define DAV_KEY_AES256     1
198
199 struct DavKey {
200     char    *name;
201     int     type;
202     void    *data;
203     size_t  length;
204 };
205
206 struct DavNSInfo {
207     char    *prefix;
208     DavBool encrypt;
209 };
210
211 struct DavXmlNode {
212     DavXmlNodeType type;
213     
214     char           *namespace;
215     char           *name;
216     
217     DavXmlNode     *prev;
218     DavXmlNode     *next;
219     DavXmlNode     *children;
220     DavXmlNode     *parent;
221     
222     DavXmlAttr     *attributes;
223     
224     char           *content;
225     size_t         contentlength;
226 };
227
228 struct DavXmlAttr {
229     char *name;
230     char *value;
231     DavXmlAttr *next;
232 };
233
234 DavContext* dav_context_new(void);
235 void dav_context_destroy(DavContext *ctx);
236
237 void dav_context_add_key(DavContext *context, DavKey *key);
238 DavKey* dav_context_get_key(DavContext *context, char *name);
239
240 int dav_add_namespace(DavContext *context, const char *prefix, const char *ns);
241 DavNamespace* dav_get_namespace(DavContext *context, const char *prefix);
242
243 int dav_enable_namespace_encryption(DavContext *context, const char *ns, DavBool encrypt);
244 int dav_namespace_is_encrypted(DavContext *context, const char *ns);
245
246 DavSession* dav_session_new(DavContext *context, char *base_url);
247 DavSession* dav_session_new_auth(
248         DavContext *context,
249         char *base_url,
250         char *user,
251         char *password);
252 void dav_session_set_auth(DavSession *sn, char *user, char *password);
253 void dav_session_set_baseurl(DavSession *sn, char *base_url);
254 void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags);
255
256 void dav_session_set_authcallback(DavSession *sn, dav_auth_func func, void *userdata);
257 void dav_session_set_progresscallback(DavSession *sn, dav_progress_func get, dav_progress_func put, void *userdata);
258
259 void dav_session_destroy(DavSession *sn);
260
261 void* dav_session_malloc(DavSession *sn, size_t size);
262 void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size);
263 void* dav_session_realloc(DavSession *sn, void *ptr, size_t size);
264 void  dav_session_free(DavSession *sn, void *ptr);
265 char* dav_session_strdup(DavSession *sn, const char *str);
266
267 void dav_set_effective_href(DavSession *sn, DavResource *resource);
268 DavResource* dav_get(DavSession *sn, char *path, char *properties);
269
270 UcxList* parse_properties_string(DavContext *context, sstr_t str);
271
272 DavResource* dav_query(DavSession *sn, char *query, ...);
273
274 sstr_t dav_property_key(const char *ns, const char *name);
275 void dav_get_property_namespace_str(
276         DavContext *ctx,
277         char *prefixed_name,
278         char **ns,
279         char **name);
280 DavNamespace* dav_get_property_namespace(
281         DavContext *ctx,
282         char *prefixed_name,
283         char **name);
284
285 /* ------------------------ resource functions ------------------------ */
286
287 DavResource* dav_resource_new(DavSession *sn, char *path);
288 DavResource* dav_resource_new_child(DavSession *sn, DavResource *parent, char *name);
289 DavResource* dav_resource_new_href(DavSession *sn, char *href);
290
291 void dav_resource_free(DavResource *res);
292 void dav_resource_free_all(DavResource *res);
293
294 char* dav_resource_get_href(DavResource *resource);
295
296 DavResource* dav_create_child(DavResource *parent, char *name);
297 int dav_delete(DavResource *res);
298 int dav_create(DavResource *res);
299 int dav_exists(DavResource *res);
300
301 int dav_copy(DavResource *res, char *newpath);
302 int dav_move(DavResource *res, char *newpath);
303 int dav_copy_o(DavResource *res, char *newpath, DavBool override);
304 int dav_move_o(DavResource *res, char *newpath, DavBool override);
305 int dav_copyto(DavResource *res, char *url, DavBool override);
306 int dav_moveto(DavResource *res, char *url, DavBool override);
307
308 int dav_lock(DavResource *res);
309 int dav_lock_t(DavResource *res, time_t timeout);
310 int dav_unlock(DavResource *res);
311
312 DavXmlNode* dav_get_property(DavResource *res, char *name);
313 DavXmlNode* dav_get_property_ns(DavResource *res, const char *ns, const char *name);
314 DavXmlNode* dav_get_encrypted_property_ns(DavResource *res, const char *ns, const char *name);
315 char* dav_get_string_property(DavResource *res, char *name);
316 char* dav_get_string_property_ns(DavResource *res, char *ns, char *name);
317 void dav_set_string_property(DavResource *res, char *name, char *value);
318 void dav_set_string_property_ns(DavResource *res, char *ns, char *name, char *value);
319 void dav_set_property(DavResource *res, char *name, DavXmlNode *value);
320 void dav_set_property_ns(DavResource *res, char *ns, char *name, DavXmlNode *value);
321 void dav_remove_property(DavResource *res, char *name);
322 void dav_remove_property_ns(DavResource *res, char *ns, char *name);
323 void dav_set_encrypted_property_ns(DavResource *res, char *ns, char *name, DavXmlNode *value);
324 void dav_set_encrypted_string_property_ns(DavResource *res, char *ns, char *name, char *value);
325 void dav_remove_encrypted_property_ns(DavResource *res, char *ns, char *name);
326
327 DavPropName* dav_get_property_names(DavResource *res, size_t *count);
328
329 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func, dav_seek_func seek_func);
330 void dav_set_content_data(DavResource *res, char *content, size_t length);
331 void dav_set_content_length(DavResource *res, size_t length);
332
333 int dav_load(DavResource *res);
334 int dav_load_prop(DavResource *res, DavPropName *properties, size_t numprop);
335 int dav_store(DavResource *res);
336 int dav_get_content(DavResource *res, void *stream, dav_write_func write_func);
337
338 // private
339 int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf);
340
341
342 /* --------------------------- DeltaV ---------------------------- */
343
344 int dav_versioncontrol(DavResource *res);
345 int dav_checkout(DavResource *res);
346 int dav_checkin(DavResource *res);
347 int dav_uncheckout(DavResource *res);
348 DavResource* dav_versiontree(DavResource *res, char *properties);
349
350 /* ------------------------ xml functions ------------------------ */
351 char* dav_xml_getstring(DavXmlNode *node);
352 DavBool dav_xml_isstring(DavXmlNode *node);
353 DavXmlNode* dav_xml_nextelm(DavXmlNode *node);
354 DavXmlNode* dav_text_node(DavSession *sn, char *text);
355
356 DavXmlNode* dav_copy_node(DavXmlNode *node);
357
358 DavXmlNode* dav_xml_createnode(const char *ns, const char *name);
359 DavXmlNode* dav_xml_createnode_with_text(const char *ns, const char *name, const char *text);
360 DavXmlNode* dav_xml_createtextnode(const char *text);
361 void dav_xml_add_child(DavXmlNode *node, DavXmlNode *child);
362 void dav_xml_add_attr(DavXmlNode *node, const char *name, const char *value);
363 char* dav_xml_get_attr(DavXmlNode *node, const char *name);
364
365 DavXmlNode* dav_parse_xml(DavSession *sn, const char *str, size_t len);
366
367 #ifdef  __cplusplus
368 }
369 #endif
370
371 #endif  /* WEBDAV_H */
372