e9099c0dde3c511258ff5ddcd2c96bece0067351
[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-2005 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[2048];
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, ctdl_netcfg_dir);
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
162                 "MIME-Version: 1.0\n"
163                 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
164                 "\n"
165                 "This is a multipart message in MIME format.\n"
166                 "\n"
167                 "--__ctdlmultipart__\n"
168                 "Content-type: text/plain\n"
169                 "\n"
170                 "Someone (probably you) has submitted a request to subscribe\n"
171                 "<%s> to the '%s' mailing list.\n"
172                 "\n"
173                 "Please go here to confirm this request:\n"
174                 "  %s?room=%s&token=%s&cmd=confirm  \n"
175                 "\n"
176                 "If this request has been submitted in error and you do not\n"
177                 "wish to receive the '%s' mailing list, simply do nothing,\n"
178                 "and you will not receive any further mailings.\n"
179                 "\n"
180                 "--__ctdlmultipart__\n"
181                 "Content-type: text/html\n"
182                 "<HTML><BODY>\n"
183                 "Someone (probably you) has submitted a request to subscribe\n"
184                 "&lt;%s&gt; to the <B>%s</B> mailing list.<BR><BR>\n"
185                 "Please click here to confirm this request:<BR>\n"
186                 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
187                 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
188                 "If this request has been submitted in error and you do not\n"
189                 "wish to receive the '%s' mailing list, simply do nothing,\n"
190                 "and you will not receive any further mailings.\n"
191                 "</BODY></HTML>\n"
192                 "\n"
193                 "--__ctdlmultipart__--\n",
194
195                 email, qrbuf.QRname,
196                 webpage, urlroom, token,
197                 qrbuf.QRname,
198
199                 email, qrbuf.QRname,
200                 webpage, urlroom, token,
201                 webpage, urlroom, token,
202                 qrbuf.QRname
203         );
204
205         quickie_message(        /* This delivers the message */
206                 "Citadel",
207                 email,
208                 NULL,
209                 confirmation_request,
210                 FMT_RFC822,
211                 "Please confirm your list subscription"
212         );
213
214         cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
215 }
216
217
218 /*
219  * Enter an unsubscription request
220  */
221 void do_unsubscribe(char *room, char *email, char *webpage) {
222         struct ctdlroom qrbuf;
223         FILE *ncfp;
224         char filename[256];
225         char token[256];
226         char buf[512];
227         char confirmation_request[2048];
228         char urlroom[ROOMNAMELEN];
229         char scancmd[256];
230         char scanemail[256];
231         int found_sub = 0;
232
233         if (getroom(&qrbuf, room) != 0) {
234                 cprintf("%d There is no list called '%s'\n",
235                         ERROR + ROOM_NOT_FOUND, room);
236                 return;
237         }
238
239         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
240                 cprintf("%d '%s' "
241                         "does not accept subscribe/unsubscribe requests.\n",
242                         ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
243                 return;
244         }
245
246         listsub_generate_token(token);
247
248         assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
249
250         /* 
251          * Make sure there's actually a subscription there to remove
252          */
253         begin_critical_section(S_NETCONFIGS);
254         ncfp = fopen(filename, "r");
255         if (ncfp != NULL) {
256                 while (fgets(buf, sizeof buf, ncfp) != NULL) {
257                         buf[strlen(buf)-1] = 0;
258                         extract_token(scancmd, buf, 0, '|', sizeof scancmd);
259                         extract_token(scanemail, buf, 1, '|', sizeof scanemail);
260                         if ((!strcasecmp(scancmd, "listrecp"))
261                            || (!strcasecmp(scancmd, "digestrecp"))) {
262                                 if (!strcasecmp(scanemail, email)) {
263                                         ++found_sub;
264                                 }
265                         }
266                 }
267                 fclose(ncfp);
268         }
269         end_critical_section(S_NETCONFIGS);
270
271         if (found_sub == 0) {
272                 cprintf("%d '%s' is not subscribed to '%s'.\n",
273                         ERROR + NO_SUCH_USER,
274                         email, qrbuf.QRname);
275                 return;
276         }
277         
278         /* 
279          * Ok, now enter the unsubscribe-pending entry.
280          */
281         begin_critical_section(S_NETCONFIGS);
282         ncfp = fopen(filename, "a");
283         if (ncfp != NULL) {
284                 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
285                         email,
286                         token,
287                         time(NULL),
288                         webpage
289                 );
290                 fclose(ncfp);
291         }
292         end_critical_section(S_NETCONFIGS);
293
294         /* Generate and send the confirmation request */
295
296         urlesc(urlroom, qrbuf.QRname);
297
298         snprintf(confirmation_request, sizeof confirmation_request,
299
300                 "MIME-Version: 1.0\n"
301                 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
302                 "\n"
303                 "This is a multipart message in MIME format.\n"
304                 "\n"
305                 "--__ctdlmultipart__\n"
306                 "Content-type: text/plain\n"
307                 "\n"
308                 "Someone (probably you) has submitted a request to unsubscribe\n"
309                 "<%s> from the '%s' mailing list.\n"
310                 "\n"
311                 "Please go here to confirm this request:\n"
312                 "  %s?room=%s&token=%s&cmd=confirm  \n"
313                 "\n"
314                 "If this request has been submitted in error and you do not\n"
315                 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
316                 "and the request will not be processed.\n"
317                 "\n"
318                 "--__ctdlmultipart__\n"
319                 "Content-type: text/html\n"
320                 "<HTML><BODY>\n"
321                 "Someone (probably you) has submitted a request to unsubscribe\n"
322                 "&lt;%s&gt; from the <B>%s</B> mailing list.<BR><BR>\n"
323                 "Please click here to confirm this request:<BR>\n"
324                 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
325                 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
326                 "If this request has been submitted in error and you do not\n"
327                 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
328                 "and the request will not be processed.\n"
329                 "</BODY></HTML>\n"
330                 "\n"
331                 "--__ctdlmultipart__--\n",
332
333                 email, qrbuf.QRname,
334                 webpage, urlroom, token,
335                 qrbuf.QRname,
336
337                 email, qrbuf.QRname,
338                 webpage, urlroom, token,
339                 webpage, urlroom, token,
340                 qrbuf.QRname
341         );
342
343         quickie_message(        /* This delivers the message */
344                 "Citadel",
345                 email,
346                 NULL,
347                 confirmation_request,
348                 FMT_RFC822,
349                 "Please confirm your unsubscribe request"
350         );
351
352         cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
353 }
354
355
356 /*
357  * Confirm a subscribe/unsubscribe request.
358  */
359 void do_confirm(char *room, char *token) {
360         struct ctdlroom qrbuf;
361         FILE *ncfp;
362         char filename[256];
363         char line_token[256];
364         long line_offset;
365         int line_length;
366         char buf[512];
367         char cmd[256];
368         char email[256];
369         char subtype[128];
370         int success = 0;
371         char address_to_unsubscribe[256];
372         char scancmd[256];
373         char scanemail[256];
374         char *holdbuf = NULL;
375         int linelen = 0;
376         int buflen = 0;
377
378         strcpy(address_to_unsubscribe, "");
379
380         if (getroom(&qrbuf, room) != 0) {
381                 cprintf("%d There is no list called '%s'\n",
382                         ERROR + ROOM_NOT_FOUND, room);
383                 return;
384         }
385
386         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
387                 cprintf("%d '%s' "
388                         "does not accept subscribe/unsubscribe requests.\n",
389                         ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
390                 return;
391         }
392
393         /*
394          * Now start scanning this room's netconfig file for the
395          * specified token.
396          */
397         assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
398         begin_critical_section(S_NETCONFIGS);
399         ncfp = fopen(filename, "r+");
400         if (ncfp != NULL) {
401                 while (line_offset = ftell(ncfp),
402                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
403                         buf[strlen(buf)-1] = 0;
404                         line_length = strlen(buf);
405                         extract_token(cmd, buf, 0, '|', sizeof cmd);
406                         if (!strcasecmp(cmd, "subpending")) {
407                                 extract_token(email, buf, 1, '|', sizeof email);
408                                 extract_token(subtype, buf, 2, '|', sizeof subtype);
409                                 extract_token(line_token, buf, 3, '|', sizeof line_token);
410                                 if (!strcasecmp(token, line_token)) {
411                                         if (!strcasecmp(subtype, "digest")) {
412                                                 safestrncpy(buf, "digestrecp|", sizeof buf);
413                                         }
414                                         else {
415                                                 safestrncpy(buf, "listrecp|", sizeof buf);
416                                         }
417                                         strcat(buf, email);
418                                         strcat(buf, "|");
419                                         /* SLEAZY HACK: pad the line out so
420                                          * it's the same length as the line
421                                          * we're replacing.
422                                          */
423                                         while (strlen(buf) < line_length) {
424                                                 strcat(buf, " ");
425                                         }
426                                         fseek(ncfp, line_offset, SEEK_SET);
427                                         fprintf(ncfp, "%s\n", buf);
428                                         ++success;
429                                 }
430                         }
431                         if (!strcasecmp(cmd, "unsubpending")) {
432                                 extract_token(line_token, buf, 2, '|', sizeof line_token);
433                                 if (!strcasecmp(token, line_token)) {
434                                         extract_token(address_to_unsubscribe, buf, 1, '|',
435                                                 sizeof address_to_unsubscribe);
436                                 }
437                         }
438                 }
439                 fclose(ncfp);
440         }
441         end_critical_section(S_NETCONFIGS);
442
443         /*
444          * If "address_to_unsubscribe" contains something, then we have to
445          * make another pass at the file, stripping out lines referring to
446          * that address.
447          */
448         if (strlen(address_to_unsubscribe) > 0) {
449                 holdbuf = malloc(SIZ);
450                 begin_critical_section(S_NETCONFIGS);
451                 ncfp = fopen(filename, "r+");
452                 if (ncfp != NULL) {
453                         while (line_offset = ftell(ncfp),
454                               (fgets(buf, sizeof buf, ncfp) != NULL) ) {
455                                 buf[strlen(buf)-1]=0;
456                                 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
457                                 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
458                                 if ( (!strcasecmp(scancmd, "listrecp"))
459                                    && (!strcasecmp(scanemail,
460                                                 address_to_unsubscribe)) ) {
461                                         ++success;
462                                 }
463                                 else if ( (!strcasecmp(scancmd, "digestrecp"))
464                                    && (!strcasecmp(scanemail,
465                                                 address_to_unsubscribe)) ) {
466                                         ++success;
467                                 }
468                                 else if ( (!strcasecmp(scancmd, "subpending"))
469                                    && (!strcasecmp(scanemail,
470                                                 address_to_unsubscribe)) ) {
471                                         ++success;
472                                 }
473                                 else if ( (!strcasecmp(scancmd, "unsubpending"))
474                                    && (!strcasecmp(scanemail,
475                                                 address_to_unsubscribe)) ) {
476                                         ++success;
477                                 }
478                                 else {  /* Not relevant, so *keep* it! */
479                                         linelen = strlen(buf);
480                                         holdbuf = realloc(holdbuf,
481                                                 (buflen + linelen + 2) );
482                                         strcpy(&holdbuf[buflen], buf);
483                                         buflen += linelen;
484                                         strcpy(&holdbuf[buflen], "\n");
485                                         buflen += 1;
486                                 }
487                         }
488                         fclose(ncfp);
489                 }
490                 ncfp = fopen(filename, "w");
491                 if (ncfp != NULL) {
492                         fwrite(holdbuf, buflen+1, 1, ncfp);
493                         fclose(ncfp);
494                 }
495                 end_critical_section(S_NETCONFIGS);
496                 free(holdbuf);
497         }
498
499         /*
500          * Did we do anything useful today?
501          */
502         if (success) {
503                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
504                 lprintf(CTDL_NOTICE, "Mailing list: %s %ssubscribed to %s with token %s\n", email, (strlen(address_to_unsubscribe) > 0) ? "un" : "", room, token);
505         }
506         else {
507                 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
508         }
509
510 }
511
512
513
514 /* 
515  * process subscribe/unsubscribe requests and confirmations
516  */
517 void cmd_subs(char *cmdbuf) {
518
519         char opr[256];
520         char room[ROOMNAMELEN];
521         char email[256];
522         char subtype[256];
523         char token[256];
524         char webpage[256];
525
526         extract_token(opr, cmdbuf, 0, '|', sizeof opr);
527         if (!strcasecmp(opr, "subscribe")) {
528                 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
529                 if ( (strcasecmp(subtype, "list"))
530                    && (strcasecmp(subtype, "digest")) ) {
531                         cprintf("%d Invalid subscription type '%s'\n",
532                                 ERROR + ILLEGAL_VALUE, subtype);
533                 }
534                 else {
535                         extract_token(room, cmdbuf, 1, '|', sizeof room);
536                         extract_token(email, cmdbuf, 2, '|', sizeof email);
537                         extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
538                         do_subscribe(room, email, subtype, webpage);
539                 }
540         }
541         else if (!strcasecmp(opr, "unsubscribe")) {
542                 extract_token(room, cmdbuf, 1, '|', sizeof room);
543                 extract_token(email, cmdbuf, 2, '|', sizeof email);
544                 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
545                 do_unsubscribe(room, email, webpage);
546         }
547         else if (!strcasecmp(opr, "confirm")) {
548                 extract_token(room, cmdbuf, 1, '|', sizeof room);
549                 extract_token(token, cmdbuf, 2, '|', sizeof token);
550                 do_confirm(room, token);
551         }
552         else {
553                 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
554         }
555 }
556
557
558 /*
559  * Module entry point
560  */
561 char *serv_listsub_init(void)
562 {
563         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
564         return "$Id$";
565 }