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