* Added a "MIME-Version:" header in a couple of places
[citadel.git] / citadel / serv_listsub.c
1 /*
2  * $Id$
3  *
4  * This module handles self-service subscription/unsubscription to mail lists.
5  *
6  * Copyright (C) 2002 by Art Cancro and others.
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <dirent.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "serv_extensions.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "tools.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
51 #include "clientsocket.h"
52 #include "file_ops.h"
53
54 #ifndef HAVE_SNPRINTF
55 #include "snprintf.h"
56 #endif
57
58
59 /*
60  * Generate a randomizationalisticized token to use for authentication of
61  * a subscribe or unsubscribe request.
62  */
63 void listsub_generate_token(char *buf) {
64         char sourcebuf[SIZ];
65         static int seq = 0;
66
67         /* Theo, please sit down and shut up.  This key doesn't have to be
68          * tinfoil-hat secure, it just needs to be reasonably unguessable
69          * and unique.
70          */
71         sprintf(sourcebuf, "%lx",
72                 (long) (++seq + getpid() + time(NULL))
73         );
74
75         /* Convert it to base64 so it looks cool */     
76         CtdlEncodeBase64(buf, sourcebuf, strlen(sourcebuf));
77 }
78
79
80 /*
81  * Enter a subscription request
82  */
83 void do_subscribe(char *room, char *email, char *subtype, char *webpage) {
84         struct ctdlroom qrbuf;
85         FILE *ncfp;
86         char filename[256];
87         char token[256];
88         char confirmation_request[512];
89         char buf[512];
90         char urlroom[ROOMNAMELEN];
91         char scancmd[64];
92         char scanemail[256];
93         int found_sub = 0;
94
95         if (getroom(&qrbuf, room) != 0) {
96                 cprintf("%d There is no list called '%s'\n", ERROR + ROOM_NOT_FOUND, room);
97                 return;
98         }
99
100         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
101                 cprintf("%d '%s' "
102                         "does not accept subscribe/unsubscribe requests.\n",
103                         ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
104                 return;
105         }
106
107         listsub_generate_token(token);
108
109         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
110
111         /* 
112          * Make sure the requested address isn't already subscribed
113          */
114         begin_critical_section(S_NETCONFIGS);
115         ncfp = fopen(filename, "r");
116         if (ncfp != NULL) {
117                 while (fgets(buf, sizeof buf, ncfp) != NULL) {
118                         buf[strlen(buf)-1] = 0;
119                         extract_token(scancmd, buf, 0, '|', sizeof scancmd);
120                         extract_token(scanemail, buf, 1, '|', sizeof scanemail);
121                         if ((!strcasecmp(scancmd, "listrecp"))
122                            || (!strcasecmp(scancmd, "digestrecp"))) {
123                                 if (!strcasecmp(scanemail, email)) {
124                                         ++found_sub;
125                                 }
126                         }
127                 }
128                 fclose(ncfp);
129         }
130         end_critical_section(S_NETCONFIGS);
131
132         if (found_sub != 0) {
133                 cprintf("%d '%s' is already subscribed to '%s'.\n",
134                         ERROR + ALREADY_EXISTS,
135                         email, qrbuf.QRname);
136                 return;
137         }
138
139         /*
140          * Now add it to the file
141          */     
142         begin_critical_section(S_NETCONFIGS);
143         ncfp = fopen(filename, "a");
144         if (ncfp != NULL) {
145                 fprintf(ncfp, "subpending|%s|%s|%s|%ld|%s\n",
146                         email,
147                         subtype,
148                         token,
149                         time(NULL),
150                         webpage
151                 );
152                 fclose(ncfp);
153         }
154         end_critical_section(S_NETCONFIGS);
155
156         /* Generate and send the confirmation request */
157
158         urlesc(urlroom, qrbuf.QRname);
159
160         snprintf(confirmation_request, sizeof confirmation_request,
161                 "Content-type: text/html\nMIME-Version: 1.0\n\n"
162                 "<HTML><BODY>"
163                 "Someone (probably you) has submitted a request to subscribe\n"
164                 "&lt;%s&gt; to the <B>%s</B> mailing list.<BR><BR>\n"
165                 "Please click here to confirm this request:<BR>\n"
166                 "<A HREF=\"http://%s?room=%s&token=%s&cmd=confirm\">"
167                 "http://%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
168                 "If this request has been submitted in error and you do not\n"
169                 "wish to receive the '%s' mailing list, simply do nothing,\n"
170                 "and you will not receive any further mailings.\n"
171                 "</BODY></HTML>\n",
172
173                 email, qrbuf.QRname,
174                 webpage, urlroom, token,
175                 webpage, urlroom, token,
176                 qrbuf.QRname
177         );
178
179         quickie_message(        /* This delivers the message */
180                 "Citadel",
181                 email,
182                 NULL,
183                 confirmation_request,
184                 FMT_RFC822,
185                 "Please confirm your list subscription"
186         );
187
188         cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
189 }
190
191
192 /*
193  * Enter an unsubscription request
194  */
195 void do_unsubscribe(char *room, char *email, char *webpage) {
196         struct ctdlroom qrbuf;
197         FILE *ncfp;
198         char filename[256];
199         char token[256];
200         char buf[512];
201         char confirmation_request[512];
202         char urlroom[ROOMNAMELEN];
203         char scancmd[256];
204         char scanemail[256];
205         int found_sub = 0;
206
207         if (getroom(&qrbuf, room) != 0) {
208                 cprintf("%d There is no list called '%s'\n",
209                         ERROR + ROOM_NOT_FOUND, room);
210                 return;
211         }
212
213         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
214                 cprintf("%d '%s' "
215                         "does not accept subscribe/unsubscribe requests.\n",
216                         ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
217                 return;
218         }
219
220         listsub_generate_token(token);
221
222         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
223
224         /* 
225          * Make sure there's actually a subscription there to remove
226          */
227         begin_critical_section(S_NETCONFIGS);
228         ncfp = fopen(filename, "r");
229         if (ncfp != NULL) {
230                 while (fgets(buf, sizeof buf, ncfp) != NULL) {
231                         buf[strlen(buf)-1] = 0;
232                         extract_token(scancmd, buf, 0, '|', sizeof scancmd);
233                         extract_token(scanemail, buf, 1, '|', sizeof scanemail);
234                         if ((!strcasecmp(scancmd, "listrecp"))
235                            || (!strcasecmp(scancmd, "digestrecp"))) {
236                                 if (!strcasecmp(scanemail, email)) {
237                                         ++found_sub;
238                                 }
239                         }
240                 }
241                 fclose(ncfp);
242         }
243         end_critical_section(S_NETCONFIGS);
244
245         if (found_sub == 0) {
246                 cprintf("%d '%s' is not subscribed to '%s'.\n",
247                         ERROR + NO_SUCH_USER,
248                         email, qrbuf.QRname);
249                 return;
250         }
251         
252         /* 
253          * Ok, now enter the unsubscribe-pending entry.
254          */
255         begin_critical_section(S_NETCONFIGS);
256         ncfp = fopen(filename, "a");
257         if (ncfp != NULL) {
258                 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
259                         email,
260                         token,
261                         time(NULL),
262                         webpage
263                 );
264                 fclose(ncfp);
265         }
266         end_critical_section(S_NETCONFIGS);
267
268         /* Generate and send the confirmation request */
269
270         urlesc(urlroom, qrbuf.QRname);
271
272         snprintf(confirmation_request, sizeof confirmation_request,
273                 "Content-type: text/html\nMIME-Version: 1.0\n\n"
274                 "<HTML><BODY>"
275                 "Someone (probably you) has submitted a request "
276                 "to unsubscribe\n"
277                 "&lt;%s&gt; from the <B>%s</B> mailing list.<BR><BR>\n"
278                 "Please click here to confirm this request:<BR>\n"
279                 "<A HREF=\"http://%s?room=%s&token=%s&cmd=confirm\">"
280                 "http://%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
281                 "If this request has been submitted in error and you do\n"
282                 "<i>not</i> wish to unsubscribe from the "
283                 "'%s' mailing list, simply do nothing,\n"
284                 "and you will remain subscribed to the list.\n"
285                 "</BODY></HTML>\n",
286
287                 email, qrbuf.QRname,
288                 webpage, urlroom, token,
289                 webpage, urlroom, token,
290                 qrbuf.QRname
291         );
292
293         quickie_message(        /* This delivers the message */
294                 "Citadel",
295                 email,
296                 NULL,
297                 confirmation_request,
298                 FMT_RFC822,
299                 "Please confirm your unsubscribe request"
300         );
301
302         cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
303 }
304
305
306 /*
307  * Confirm a subscribe/unsubscribe request.
308  */
309 void do_confirm(char *room, char *token) {
310         struct ctdlroom qrbuf;
311         FILE *ncfp;
312         char filename[256];
313         char line_token[256];
314         long line_offset;
315         int line_length;
316         char buf[512];
317         char cmd[256];
318         char email[256];
319         char subtype[128];
320         int success = 0;
321         char address_to_unsubscribe[256];
322         char scancmd[256];
323         char scanemail[256];
324         char *holdbuf = NULL;
325         int linelen = 0;
326         int buflen = 0;
327
328         strcpy(address_to_unsubscribe, "");
329
330         if (getroom(&qrbuf, room) != 0) {
331                 cprintf("%d There is no list called '%s'\n",
332                         ERROR + ROOM_NOT_FOUND, room);
333                 return;
334         }
335
336         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
337                 cprintf("%d '%s' "
338                         "does not accept subscribe/unsubscribe requests.\n",
339                         ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
340                 return;
341         }
342
343         /*
344          * Now start scanning this room's netconfig file for the
345          * specified token.
346          */
347         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
348         begin_critical_section(S_NETCONFIGS);
349         ncfp = fopen(filename, "r+");
350         if (ncfp != NULL) {
351                 while (line_offset = ftell(ncfp),
352                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
353                         buf[strlen(buf)-1] = 0;
354                         line_length = strlen(buf);
355                         extract_token(cmd, buf, 0, '|', sizeof cmd);
356                         if (!strcasecmp(cmd, "subpending")) {
357                                 extract_token(email, buf, 1, '|', sizeof email);
358                                 extract_token(subtype, buf, 2, '|', sizeof subtype);
359                                 extract_token(line_token, buf, 3, '|', sizeof line_token);
360                                 if (!strcasecmp(token, line_token)) {
361                                         if (!strcasecmp(subtype, "digest")) {
362                                                 safestrncpy(buf, "digestrecp|", sizeof buf);
363                                         }
364                                         else {
365                                                 safestrncpy(buf, "listrecp|", sizeof buf);
366                                         }
367                                         strcat(buf, email);
368                                         strcat(buf, "|");
369                                         /* SLEAZY HACK: pad the line out so
370                                          * it's the same length as the line
371                                          * we're replacing.
372                                          */
373                                         while (strlen(buf) < line_length) {
374                                                 strcat(buf, " ");
375                                         }
376                                         fseek(ncfp, line_offset, SEEK_SET);
377                                         fprintf(ncfp, "%s\n", buf);
378                                         ++success;
379                                 }
380                         }
381                         if (!strcasecmp(cmd, "unsubpending")) {
382                                 extract_token(line_token, buf, 2, '|', sizeof line_token);
383                                 if (!strcasecmp(token, line_token)) {
384                                         extract_token(address_to_unsubscribe, buf, 1, '|',
385                                                 sizeof address_to_unsubscribe);
386                                 }
387                         }
388                 }
389                 fclose(ncfp);
390         }
391         end_critical_section(S_NETCONFIGS);
392
393         /*
394          * If "address_to_unsubscribe" contains something, then we have to
395          * make another pass at the file, stripping out lines referring to
396          * that address.
397          */
398         if (strlen(address_to_unsubscribe) > 0) {
399                 holdbuf = malloc(SIZ);
400                 begin_critical_section(S_NETCONFIGS);
401                 ncfp = fopen(filename, "r+");
402                 if (ncfp != NULL) {
403                         while (line_offset = ftell(ncfp),
404                               (fgets(buf, sizeof buf, ncfp) != NULL) ) {
405                                 buf[strlen(buf)-1]=0;
406                                 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
407                                 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
408                                 if ( (!strcasecmp(scancmd, "listrecp"))
409                                    && (!strcasecmp(scanemail,
410                                                 address_to_unsubscribe)) ) {
411                                         ++success;
412                                 }
413                                 else if ( (!strcasecmp(scancmd, "digestrecp"))
414                                    && (!strcasecmp(scanemail,
415                                                 address_to_unsubscribe)) ) {
416                                         ++success;
417                                 }
418                                 else if ( (!strcasecmp(scancmd, "subpending"))
419                                    && (!strcasecmp(scanemail,
420                                                 address_to_unsubscribe)) ) {
421                                         ++success;
422                                 }
423                                 else if ( (!strcasecmp(scancmd, "unsubpending"))
424                                    && (!strcasecmp(scanemail,
425                                                 address_to_unsubscribe)) ) {
426                                         ++success;
427                                 }
428                                 else {  /* Not relevant, so *keep* it! */
429                                         linelen = strlen(buf);
430                                         holdbuf = realloc(holdbuf,
431                                                 (buflen + linelen + 2) );
432                                         strcpy(&holdbuf[buflen], buf);
433                                         buflen += linelen;
434                                         strcpy(&holdbuf[buflen], "\n");
435                                         buflen += 1;
436                                 }
437                         }
438                         fclose(ncfp);
439                 }
440                 ncfp = fopen(filename, "w");
441                 if (ncfp != NULL) {
442                         fwrite(holdbuf, buflen+1, 1, ncfp);
443                         fclose(ncfp);
444                 }
445                 end_critical_section(S_NETCONFIGS);
446                 free(holdbuf);
447         }
448
449         /*
450          * Did we do anything useful today?
451          */
452         if (success) {
453                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
454         }
455         else {
456                 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
457         }
458
459 }
460
461
462
463 /* 
464  * process subscribe/unsubscribe requests and confirmations
465  */
466 void cmd_subs(char *cmdbuf) {
467
468         char opr[256];
469         char room[ROOMNAMELEN];
470         char email[256];
471         char subtype[256];
472         char token[256];
473         char webpage[256];
474
475         extract_token(opr, cmdbuf, 0, '|', sizeof opr);
476         if (!strcasecmp(opr, "subscribe")) {
477                 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
478                 if ( (strcasecmp(subtype, "list"))
479                    && (strcasecmp(subtype, "digest")) ) {
480                         cprintf("%d Invalid subscription type '%s'\n",
481                                 ERROR + ILLEGAL_VALUE, subtype);
482                 }
483                 else {
484                         extract_token(room, cmdbuf, 1, '|', sizeof room);
485                         extract_token(email, cmdbuf, 2, '|', sizeof email);
486                         extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
487                         do_subscribe(room, email, subtype, webpage);
488                 }
489         }
490         else if (!strcasecmp(opr, "unsubscribe")) {
491                 extract_token(room, cmdbuf, 1, '|', sizeof room);
492                 extract_token(email, cmdbuf, 2, '|', sizeof email);
493                 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
494                 do_unsubscribe(room, email, webpage);
495         }
496         else if (!strcasecmp(opr, "confirm")) {
497                 extract_token(room, cmdbuf, 1, '|', sizeof room);
498                 extract_token(token, cmdbuf, 2, '|', sizeof token);
499                 do_confirm(room, token);
500         }
501         else {
502                 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
503         }
504 }
505
506
507 /*
508  * Module entry point
509  */
510 char *serv_listsub_init(void)
511 {
512         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
513         return "$Id$";
514 }