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