* Bugfixes
[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 "dynloader.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         encode_base64(buf, 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 quickroom qrbuf;
85         FILE *ncfp;
86         char filename[SIZ];
87         char token[SIZ];
88         char confirmation_request[SIZ];
89         char buf[SIZ];
90         char urlroom[SIZ];
91         char scancmd[SIZ];
92         char scanemail[SIZ];
93         int found_sub = 0;
94
95         if (getroom(&qrbuf, room) != 0) {
96                 cprintf("%d There is no list called '%s'\n", ERROR, 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(scancmd, buf, 0);
120                         extract(scanemail, buf, 1);
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,
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\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 quickroom qrbuf;
197         FILE *ncfp;
198         char filename[SIZ];
199         char token[SIZ];
200         char buf[SIZ];
201         char confirmation_request[SIZ];
202         char urlroom[SIZ];
203         char scancmd[SIZ];
204         char scanemail[SIZ];
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(scancmd, buf, 0);
233                         extract(scanemail, buf, 1);
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\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 quickroom qrbuf;
311         FILE *ncfp;
312         char filename[SIZ];
313         char line_token[SIZ];
314         long line_offset;
315         int line_length;
316         char buf[SIZ];
317         char cmd[SIZ];
318         char email[SIZ];
319         char subtype[SIZ];
320         int success = 0;
321         char address_to_unsubscribe[SIZ];
322         char scancmd[SIZ];
323         char scanemail[SIZ];
324         char *holdbuf = NULL;
325         int linelen = 0;
326         int buflen = 0;
327         char success_message[SIZ];
328         char success_message_to[SIZ];
329         char address_of_list[SIZ];
330         int i;
331
332         strcpy(address_to_unsubscribe, "");
333         strcpy(success_message_to, "");
334
335         if (getroom(&qrbuf, room) != 0) {
336                 cprintf("%d There is no list called '%s'\n",
337                         ERROR+ROOM_NOT_FOUND, room);
338                 return;
339         }
340
341         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
342                 cprintf("%d '%s' "
343                         "does not accept subscribe/unsubscribe requests.\n",
344                         ERROR+HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
345                 return;
346         }
347
348         /*
349          * We'll just have this success message ready if we need it
350          */
351         sprintf(address_of_list, "room_%s@%s", qrbuf.QRname, config.c_fqdn);
352         for (i=0; i<strlen(address_of_list); ++i) {
353                 if (isspace(address_of_list[i])) {
354                         address_of_list[i] = '_';
355                 }
356         }
357         snprintf(success_message, sizeof success_message,
358                 "Content-type: text/html\n\n"
359                 "<HTML><BODY>"
360                 "You have successfully subscribed to the <B>%s</B>\n"
361                 "mailing list.<BR><BR>To post to the list, simply send "
362                 "an e-mail to <TT>%s</TT>"
363                 "</BODY></HTML>\n",
364                 qrbuf.QRname,
365                 address_of_list
366         );
367
368         /*
369          * Now start scanning this room's netconfig file for the
370          * specified token.
371          */
372         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
373         begin_critical_section(S_NETCONFIGS);
374         ncfp = fopen(filename, "r+");
375         if (ncfp != NULL) {
376                 while (line_offset = ftell(ncfp),
377                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
378                         buf[strlen(buf)-1] = 0;
379                         line_length = strlen(buf);
380                         extract(cmd, buf, 0);
381                         if (!strcasecmp(cmd, "subpending")) {
382                                 extract(email, buf, 1);
383                                 extract(subtype, buf, 2);
384                                 extract(line_token, buf, 3);
385                                 if (!strcasecmp(token, line_token)) {
386                                         if (!strcasecmp(subtype, "digest")) {
387                                                 strcpy(buf, "digestrecp|");
388                                         }
389                                         else {
390                                                 strcpy(buf, "listrecp|");
391                                         }
392                                         strcat(buf, email);
393                                         strcat(buf, "|");
394                                         /* SLEAZY HACK: pad the line out so
395                                          * it's the same length as the line
396                                          * we're replacing.
397                                          */
398                                         while (strlen(buf) < line_length) {
399                                                 strcat(buf, " ");
400                                         }
401                                         fseek(ncfp, line_offset, SEEK_SET);
402                                         fprintf(ncfp, "%s\n", buf);
403                                         ++success;
404                                         strcpy(success_message_to, email);
405                                 }
406                         }
407                         if (!strcasecmp(cmd, "unsubpending")) {
408                                 extract(line_token, buf, 2);
409                                 if (!strcasecmp(token, line_token)) {
410                                         extract(address_to_unsubscribe, buf, 1);
411                                 }
412                         }
413                 }
414                 fclose(ncfp);
415         }
416         end_critical_section(S_NETCONFIGS);
417
418         /*
419          * If "address_to_unsubscribe" contains something, then we have to
420          * make another pass at the file, stripping out lines referring to
421          * that address.
422          */
423         if (strlen(address_to_unsubscribe) > 0) {
424                 holdbuf = mallok(SIZ);
425                 begin_critical_section(S_NETCONFIGS);
426                 ncfp = fopen(filename, "r+");
427                 if (ncfp != NULL) {
428                         while (line_offset = ftell(ncfp),
429                               (fgets(buf, sizeof buf, ncfp) != NULL) ) {
430                                 buf[strlen(buf)-1]=0;
431                                 extract(scancmd, buf, 0);
432                                 extract(scanemail, buf, 1);
433                                 if ( (!strcasecmp(scancmd, "listrecp"))
434                                    && (!strcasecmp(scanemail,
435                                                 address_to_unsubscribe)) ) {
436                                         ++success;
437                                 }
438                                 else if ( (!strcasecmp(scancmd, "digestrecp"))
439                                    && (!strcasecmp(scanemail,
440                                                 address_to_unsubscribe)) ) {
441                                         ++success;
442                                 }
443                                 else if ( (!strcasecmp(scancmd, "subpending"))
444                                    && (!strcasecmp(scanemail,
445                                                 address_to_unsubscribe)) ) {
446                                         ++success;
447                                 }
448                                 else if ( (!strcasecmp(scancmd, "unsubpending"))
449                                    && (!strcasecmp(scanemail,
450                                                 address_to_unsubscribe)) ) {
451                                         ++success;
452                                 }
453                                 else {  /* Not relevant, so *keep* it! */
454                                         linelen = strlen(buf);
455                                         holdbuf = reallok(holdbuf,
456                                                 (buflen + linelen + 2) );
457                                         strcpy(&holdbuf[buflen], buf);
458                                         buflen += linelen;
459                                         strcpy(&holdbuf[buflen], "\n");
460                                         buflen += 1;
461                                 }
462                         }
463                         fclose(ncfp);
464                 }
465                 ncfp = fopen(filename, "w");
466                 if (ncfp != NULL) {
467                         fwrite(holdbuf, buflen+1, 1, ncfp);
468                         fclose(ncfp);
469                 }
470                 end_critical_section(S_NETCONFIGS);
471                 phree(holdbuf);
472         }
473
474         /* Let 'em know it succeeded, and how to post to the list. */
475         if (strlen(success_message_to) > 0) {
476                 quickie_message(
477                         "Citadel",
478                         success_message_to,
479                         NULL,
480                         success_message,
481                         FMT_RFC822,
482                         "Your subscription is complete"
483                 );
484         }
485
486         /*
487          * Did we do anything useful today?
488          */
489         if (success) {
490                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
491         }
492         else {
493                 cprintf("%d Invalid token.\n", ERROR);
494         }
495
496 }
497
498
499
500 /* 
501  * process subscribe/unsubscribe requests and confirmations
502  */
503 void cmd_subs(char *cmdbuf) {
504
505         char opr[SIZ];
506         char room[SIZ];
507         char email[SIZ];
508         char subtype[SIZ];
509         char token[SIZ];
510         char webpage[SIZ];
511
512         extract(opr, cmdbuf, 0);
513         if (!strcasecmp(opr, "subscribe")) {
514                 extract(subtype, cmdbuf, 3);
515                 if ( (strcasecmp(subtype, "list"))
516                    && (strcasecmp(subtype, "digest")) ) {
517                         cprintf("%d Invalid subscription type '%s'\n",
518                                 ERROR+ILLEGAL_VALUE, subtype);
519                 }
520                 else {
521                         extract(room, cmdbuf, 1);
522                         extract(email, cmdbuf, 2);
523                         extract(webpage, cmdbuf, 4);
524                         do_subscribe(room, email, subtype, webpage);
525                 }
526         }
527         else if (!strcasecmp(opr, "unsubscribe")) {
528                 extract(room, cmdbuf, 1);
529                 extract(email, cmdbuf, 2);
530                 extract(webpage, cmdbuf, 3);
531                 do_unsubscribe(room, email, webpage);
532         }
533         else if (!strcasecmp(opr, "confirm")) {
534                 extract(room, cmdbuf, 1);
535                 extract(token, cmdbuf, 2);
536                 do_confirm(room, token);
537         }
538         else {
539                 cprintf("%d Invalid command\n", ERROR);
540         }
541 }
542
543
544 /*
545  * Module entry point
546  */
547 char *Dynamic_Module_Init(void)
548 {
549         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
550         return "$Id$";
551 }