--- src/webdav.c.orig	2016-04-04 11:33:03.000000000 +0200
+++ src/webdav.c	2017-11-29 01:28:24.242112568 +0100
@@ -259,6 +259,7 @@
    allow_cookies is 0. The array must be allocated by dav_init_webdav. */
 static int n_cookies;
 static char **cookie_list;
+static char *auth_cookies;
 
 
 /* Private function prototypes and inline functions */
@@ -332,7 +333,7 @@
 /*==================*/
 
 void
-dav_init_webdav(const dav_args *args)
+dav_init_webdav(dav_args *args)
 {
     if (args->neon_debug & ~NE_DBG_HTTPPLAIN)
         syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_DEBUG), "Initializing webdav");
@@ -400,15 +401,37 @@
         username = ne_strdup(args->username);
     if (args->password)
         password = ne_strdup(args->password);
-    ne_add_server_auth(session, NE_AUTH_ALL, auth, "server");
 
-    if (args->useproxy && args->p_host) {
-        ne_session_proxy(session, args->p_host, args->p_port);
-        if (args->p_user)
-            p_username = ne_strdup(args->p_user);
-        if (args->p_passwd)
-            p_password = ne_strdup(args->p_passwd);
-        ne_add_proxy_auth(session, NE_AUTH_ALL, auth, "proxy");
+    if (strcmp(username, "@cookie") == 0) {
+        // there is an auth cookie for injection into the request
+        FILE *file = fopen(password, "rb");
+       if (file) {
+            fseek(file, 0, SEEK_END);
+            long fsize = ftell(file);
+            fseek(file, 0, SEEK_SET);
+            auth_cookies = malloc(fsize + 1);
+            auth_cookies[fsize] = '\0';
+            fread(auth_cookies, fsize, 1, file);
+            fclose(file);
+       } else {
+            auth_cookies = NULL;
+            syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_ERR),
+               _("opening auth cookie file %s failed"), password);
+       }
+         
+    } else {
+
+        auth_cookies = NULL;
+        ne_add_server_auth(session, NE_AUTH_ALL, auth, "server");
+
+        if (args->useproxy && args->p_host) {
+            ne_session_proxy(session, args->p_host, args->p_port);
+            if (args->p_user)
+                p_username = ne_strdup(args->p_user);
+            if (args->p_passwd)
+                p_password = ne_strdup(args->p_passwd);
+            ne_add_proxy_auth(session, NE_AUTH_ALL, auth, "proxy");
+        }
     }
 
 
@@ -497,7 +520,7 @@
         ne_hook_post_headers(session, get_cookies, NULL);
     }
 
-    if (custom_header || cookie_list)
+    if (custom_header || cookie_list || auth_cookies)
         ne_hook_pre_send(session, add_header, NULL);
 
     use_expect100 = args->expect100;
@@ -1630,11 +1653,22 @@
 static void
 add_header(ne_request *req, void *userdata, ne_buffer *header)
 {
+    int fCookieHeaderSet = 0;
+
     if (custom_header)
         ne_buffer_zappend(header, custom_header);
 
-    if (cookie_list && cookie_list[0]) {
+    if (auth_cookies) {
         ne_buffer_zappend(header, "Cookie: ");
+        ne_buffer_zappend(header, auth_cookies);
+        ne_buffer_zappend(header, "\r\n");
+        fCookieHeaderSet = 1;
+    }
+
+    if (cookie_list && cookie_list[0]) {
+        if (!fCookieHeaderSet)
+            ne_buffer_zappend(header, "Cookie: ");
+   
         ne_buffer_zappend(header, cookie_list[0]);
         int i = 1;
         while (i < n_cookies && cookie_list[i]) {
@@ -1769,6 +1803,32 @@
         size_t nl = es - start;
         size_t vl = end - es - 1;
 
+        if (nl == 0 || vl == 0)
+            continue;
+        
+        if (auth_cookies) {
+            char *name = ne_strndup(start, nl);
+            char *npa = strstr(auth_cookies, name);
+            free(name);
+        
+            if (npa) {
+                // this key is found in the auth_cookies
+                int alen = strlen(auth_cookies);
+        
+                char *col = strchr(npa, ';');
+                char *pre = ne_strndup(auth_cookies, (npa-auth_cookies+nl));
+                char *value = ne_strndup(es+1,vl);
+        
+                char *copy = (char*)malloc(strlen(pre)+strlen(value)+strlen(col+1)+2);
+                sprintf(copy, "%s=%s;%s", pre, value, col+1);
+        
+                free(pre); free(auth_cookies); free(value);
+                auth_cookies = copy;
+        
+                continue;
+            }
+        }
+
         int i = 0;
         for (i = 0; i < n_cookies; i++) {
             if (!cookie_list[i]) {