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