Grammar change in the license declaration.
[citadel.git] / webcit-ng / server / upload.c
1 // Upload handler
2 //
3 // Copyright (c) 1996-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure is subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10 Array *upload_list = NULL;                                      // all files uploaded to this webcit instance
11 pthread_mutex_t upload_list_mutex = PTHREAD_MUTEX_INITIALIZER;  // Lock it before modifying
12
13
14 // This function is called by the MIME parser to handle data uploaded by the browser.
15 void upload_handler(char *name, char *filename, char *partnum, char *disp,
16                     void *content, char *cbtype, char *cbcharset,
17                     size_t length, char *encoding, char *cbid, void *userdata)
18 {
19         struct uploaded_file u;
20
21         // create a random ID for the attachment
22         for (int i=0; i<sizeof(u.id); ++i) {
23                 u.id[i] = (rand() % 26) + 'a';
24         }
25         u.id[sizeof(u.id)-1] = 0;
26
27         syslog(LOG_DEBUG,
28                 "upload_handler: name=%s, filename=%s, partnum=%s, disp=%s, cbtype=%s, cbcharset=%s, length=%ld, encoding=%s, cbid=%s",
29                 name, filename, partnum, disp, cbtype, cbcharset, length, encoding, cbid
30         );
31
32         safestrncpy(u.filename, filename, sizeof(u.filename));
33         safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
34         u.length = length;
35
36         // Write the upload to a file that we can access later when the user saves the message.
37         // tmpfile() creates a file with zero links in the directory, so it will be deleted when it is closed.
38         u.fp = tmpfile();
39         if (!u.fp) {
40                 syslog(LOG_ERR, "upload: %m");
41                 return;
42         }
43         if (fwrite(content, length, 1, u.fp) != 1) {
44                 syslog(LOG_ERR, "upload: %m");
45                 fclose(u.fp);
46                 return;
47         }
48
49         // Add it to the list of uploads the server is holding.
50         pthread_mutex_lock(&upload_list_mutex);
51         if (upload_list == NULL) {
52                 upload_list = array_new(sizeof(struct uploaded_file));
53         }
54         array_append(upload_list, &u);
55         pthread_mutex_unlock(&upload_list_mutex);
56
57         for (int i=0; i<array_len(upload_list); ++i) {
58                 memcpy(&u, array_get_element_at(upload_list, i), sizeof(struct uploaded_file));
59         }
60
61         // Create a JSON object describing this upload
62         JsonValue *j_one_upload = NewJsonObject(HKEY(""));
63         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
64         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
65         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
66         JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
67
68         // ...and attach it to the array of uploads
69         JsonValue *j_uploads = (JsonValue *) userdata;
70         JsonArrayAppend(j_uploads, j_one_upload);
71 }
72
73
74 // upload handler
75 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
76         // FIXME reject uploads if we're not logged in
77
78         // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
79         // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
80         JsonValue *j_uploads = NewJsonArray(HKEY(""));
81
82         // h->request_body_with_synth_headers will contain the upload(s) in MIME format including headers
83         mime_parser(h->request_body_with_synth_headers, (h->request_body+h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
84
85         // probably do something more clever here
86         h->response_code = 200;
87         h->response_string = strdup("OK");
88
89         // send back a JSON array of all files uploaded
90         StrBuf *sj = NewStrBuf();
91         SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
92         add_response_header(h, strdup("Content-type"), strdup("application/json"));
93         h->response_code = 200;
94         h->response_string = strdup("OK");
95         h->response_body_length = StrLength(sj);
96         h->response_body = SmashStrBuf(&sj);
97         syslog(LOG_DEBUG, "upload: %s", h->response_body);
98 }
99
100
101 // Caller has requested /ctdl/p or /ctdl/p/ but we still have to dispatch based on the method
102 void ctdl_p_base(struct http_transaction *h, struct ctdlsession *c) {
103         upload_files(h, c);             // we should only do this for POST requests
104 }
105
106
107 // delete an uploaded item
108 void delete_upload(struct uploaded_file this_one) {
109         int i;
110         struct uploaded_file *u;
111
112         pthread_mutex_lock(&upload_list_mutex);
113         for (i=0; i<array_len(upload_list); ++i) {
114                 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
115                 if (!strcmp(u->id, this_one.id)) {
116                         fclose(u->fp);                          // this deletes the file because it has 0 links
117                         array_delete_element_at(upload_list, i);
118                         i = array_len(upload_list) + 1;         // Go out of scope; we're done here
119                 }
120         }
121         pthread_mutex_unlock(&upload_list_mutex);
122 }
123
124
125 // DAV delete an uploaded item
126 void dav_delete_upload(struct http_transaction *h, struct ctdlsession *c, struct uploaded_file this_one) {
127         delete_upload(this_one);
128         do_204(h);
129 }
130
131
132 // Remove an uploaded item from the upload_list.  Caller now owns the file handle and is responsible for closing it.
133 struct uploaded_file pop_upload(char *id) {
134         int i;
135         struct uploaded_file *u;
136         struct uploaded_file ret;
137
138         memset(&ret, 0, sizeof(struct uploaded_file));
139
140         pthread_mutex_lock(&upload_list_mutex);
141         for (i=0; i<array_len(upload_list); ++i) {
142                 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
143                 if (!strcmp(u->id, id)) {
144                         ret = *u;
145                         array_delete_element_at(upload_list, i);
146                         i = array_len(upload_list) + 1;         // Go out of scope; we're done here
147                 }
148         }
149         pthread_mutex_unlock(&upload_list_mutex);
150
151         return(ret);                                            // ret will be all-zeroes if we didn't find it
152 }
153
154
155 // When reloading attachments already in an existing message, accept only parts that are tagged as attachments.
156 void attachment_filter(char *name, char *filename, char *partnum, char *disp,
157                     void *content, char *cbtype, char *cbcharset,
158                     size_t length, char *encoding, char *cbid, void *userdata)
159 {
160         struct uploaded_file u;
161
162         if (!strcasecmp(disp, "attachment")) {
163                 upload_handler(name, filename, partnum, disp, content, cbtype, cbcharset, length, encoding, cbid, userdata);
164         }
165 }
166
167
168 // Load the attachments from an existing message.  This is typically used when forwarding a message,
169 // so the attachments don't have to be sent out to the browser and back.
170 void load_attachments_from_message(struct http_transaction *h, struct ctdlsession *c, char *name) {
171         syslog(LOG_DEBUG, "\033[33mload_attachments_from_message: method is \033[35m%s\033[33m, name is \033[33m%s\033[0m", h->method, name);
172
173         char buf[1024];
174         StrBuf *Body = NULL;
175
176         ctdl_printf(c, "MSG2 %ld", atol(name));
177         ctdl_readline(c, buf, sizeof buf);
178         if (buf[0] != '1') {
179                 do_404(h);
180                 return;
181         }
182
183         JsonValue *j_uploads = NewJsonArray(HKEY(""));
184         Body = NewStrBuf();
185         while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
186                 StrBufAppendPrintf(Body, "%s\n", buf);
187         }
188         char *raw_message = SmashStrBuf(&Body);
189         mime_parser(raw_message, NULL, *attachment_filter, NULL, NULL, j_uploads, 0);
190         free(raw_message);
191
192         // probably do something more clever here
193         h->response_code = 200;
194         h->response_string = strdup("OK");
195
196         // send back a JSON array of all files uploaded
197         StrBuf *sj = NewStrBuf();
198         SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
199         add_response_header(h, strdup("Content-type"), strdup("application/json"));
200         h->response_code = 200;
201         h->response_string = strdup("OK");
202         h->response_body_length = StrLength(sj);
203         h->response_body = SmashStrBuf(&sj);
204         syslog(LOG_DEBUG, "upload: %s", h->response_body);
205 }
206
207
208 // Handle operations on a specific upload
209 void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
210         int i;
211         struct uploaded_file *u;
212         struct uploaded_file this_one;
213
214         syslog(LOG_DEBUG, "\033[33mspecific_upload: method is \033[35m%s\033[33m, name is \033[33m%s\033[0m", h->method, name);
215
216         // GET of a msgnum is a request to load the attachments from an existing message.
217         if ( (!strcasecmp(h->method, "GET")) && (atol(name) > 0) ) {
218                 load_attachments_from_message(h, c, name);
219                 return;
220         }
221
222         if (upload_list == NULL) {
223                 do_404(h);
224                 return;
225         }
226
227         memset(&this_one, 0, sizeof(struct uploaded_file));
228         pthread_mutex_lock(&upload_list_mutex);
229         for (i=0; i<array_len(upload_list); ++i) {
230                 u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
231                 if (!strcmp(u->id, name)) {
232                         memcpy(&this_one, u, sizeof(struct uploaded_file));
233                         i = array_len(upload_list) + 1;         // Go out of scope; we're done here
234                 }
235         }
236         pthread_mutex_unlock(&upload_list_mutex);
237
238         // If we found a matching ID, now dispatch based on the HTTP method.
239
240         if (IsEmptyStr(this_one.id)) {                          // didn't find a match
241                 do_404(h);
242         }
243         else if (!strcasecmp(h->method, "GET")) {               // fetch the item
244                 do_405(h);
245         }
246         else if (!strcasecmp(h->method, "DELETE")) {            // delete the item
247                 dav_delete_upload(h, c, this_one);
248         }
249         else {                                                  // unsupported method
250                 do_405(h);
251         }
252 }
253
254
255 // Dispatcher for paths starting with /ctdl/p/
256 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
257         char buf[SIZ];
258
259         if (num_tokens(h->url, '/') == 3) {     //      /ctdl/p
260                 ctdl_p_base(h, c);
261                 return;
262         }
263
264         extract_token(buf, h->url, 3, '/', sizeof buf);
265         if (num_tokens(h->url, '/') == 4) {
266                 if (IsEmptyStr(buf)) {
267                         ctdl_p_base(h, c);      //      /ctdl/p/
268                 }
269                 else {
270                         specific_upload(h, c, &h->url[8]);
271                 }
272                 return;
273         }
274
275         // If we get to this point, the client requested an action we don't know how to perform.
276         do_404(h);
277 }