* Renamed "struct user" to "struct ctdluser"
[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 "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[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 ctdlroom 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 ctdlroom 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
328         strcpy(address_to_unsubscribe, "");
329
330         if (getroom(&qrbuf, room) != 0) {
331                 cprintf("%d There is no list called '%s'\n",
332                         ERROR+ROOM_NOT_FOUND, room);
333                 return;
334         }
335
336         if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
337                 cprintf("%d '%s' "
338                         "does not accept subscribe/unsubscribe requests.\n",
339                         ERROR+HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
340                 return;
341         }
342
343         /*
344          * Now start scanning this room's netconfig file for the
345          * specified token.
346          */
347         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
348         begin_critical_section(S_NETCONFIGS);
349         ncfp = fopen(filename, "r+");
350         if (ncfp != NULL) {
351                 while (line_offset = ftell(ncfp),
352                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
353                         buf[strlen(buf)-1] = 0;
354                         line_length = strlen(buf);
355                         extract(cmd, buf, 0);
356                         if (!strcasecmp(cmd, "subpending")) {
357                                 extract(email, buf, 1);
358                                 extract(subtype, buf, 2);
359                                 extract(line_token, buf, 3);
360                                 if (!strcasecmp(token, line_token)) {
361                                         if (!strcasecmp(subtype, "digest")) {
362                                                 strcpy(buf, "digestrecp|");
363                                         }
364                                         else {
365                                                 strcpy(buf, "listrecp|");
366                                         }
367                                         strcat(buf, email);
368                                         strcat(buf, "|");
369                                         /* SLEAZY HACK: pad the line out so
370                                          * it's the same length as the line
371                                          * we're replacing.
372                                          */
373                                         while (strlen(buf) < line_length) {
374                                                 strcat(buf, " ");
375                                         }
376                                         fseek(ncfp, line_offset, SEEK_SET);
377                                         fprintf(ncfp, "%s\n", buf);
378                                         ++success;
379                                 }
380                         }
381                         if (!strcasecmp(cmd, "unsubpending")) {
382                                 extract(line_token, buf, 2);
383                                 if (!strcasecmp(token, line_token)) {
384                                         extract(address_to_unsubscribe, buf, 1);
385                                 }
386                         }
387                 }
388                 fclose(ncfp);
389         }
390         end_critical_section(S_NETCONFIGS);
391
392         /*
393          * If "address_to_unsubscribe" contains something, then we have to
394          * make another pass at the file, stripping out lines referring to
395          * that address.
396          */
397         if (strlen(address_to_unsubscribe) > 0) {
398                 holdbuf = mallok(SIZ);
399                 begin_critical_section(S_NETCONFIGS);
400                 ncfp = fopen(filename, "r+");
401                 if (ncfp != NULL) {
402                         while (line_offset = ftell(ncfp),
403                               (fgets(buf, sizeof buf, ncfp) != NULL) ) {
404                                 buf[strlen(buf)-1]=0;
405                                 extract(scancmd, buf, 0);
406                                 extract(scanemail, buf, 1);
407                                 if ( (!strcasecmp(scancmd, "listrecp"))
408                                    && (!strcasecmp(scanemail,
409                                                 address_to_unsubscribe)) ) {
410                                         ++success;
411                                 }
412                                 else if ( (!strcasecmp(scancmd, "digestrecp"))
413                                    && (!strcasecmp(scanemail,
414                                                 address_to_unsubscribe)) ) {
415                                         ++success;
416                                 }
417                                 else if ( (!strcasecmp(scancmd, "subpending"))
418                                    && (!strcasecmp(scanemail,
419                                                 address_to_unsubscribe)) ) {
420                                         ++success;
421                                 }
422                                 else if ( (!strcasecmp(scancmd, "unsubpending"))
423                                    && (!strcasecmp(scanemail,
424                                                 address_to_unsubscribe)) ) {
425                                         ++success;
426                                 }
427                                 else {  /* Not relevant, so *keep* it! */
428                                         linelen = strlen(buf);
429                                         holdbuf = reallok(holdbuf,
430                                                 (buflen + linelen + 2) );
431                                         strcpy(&holdbuf[buflen], buf);
432                                         buflen += linelen;
433                                         strcpy(&holdbuf[buflen], "\n");
434                                         buflen += 1;
435                                 }
436                         }
437                         fclose(ncfp);
438                 }
439                 ncfp = fopen(filename, "w");
440                 if (ncfp != NULL) {
441                         fwrite(holdbuf, buflen+1, 1, ncfp);
442                         fclose(ncfp);
443                 }
444                 end_critical_section(S_NETCONFIGS);
445                 phree(holdbuf);
446         }
447
448         /*
449          * Did we do anything useful today?
450          */
451         if (success) {
452                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
453         }
454         else {
455                 cprintf("%d Invalid token.\n", ERROR);
456         }
457
458 }
459
460
461
462 /* 
463  * process subscribe/unsubscribe requests and confirmations
464  */
465 void cmd_subs(char *cmdbuf) {
466
467         char opr[SIZ];
468         char room[SIZ];
469         char email[SIZ];
470         char subtype[SIZ];
471         char token[SIZ];
472         char webpage[SIZ];
473
474         extract(opr, cmdbuf, 0);
475         if (!strcasecmp(opr, "subscribe")) {
476                 extract(subtype, cmdbuf, 3);
477                 if ( (strcasecmp(subtype, "list"))
478                    && (strcasecmp(subtype, "digest")) ) {
479                         cprintf("%d Invalid subscription type '%s'\n",
480                                 ERROR+ILLEGAL_VALUE, subtype);
481                 }
482                 else {
483                         extract(room, cmdbuf, 1);
484                         extract(email, cmdbuf, 2);
485                         extract(webpage, cmdbuf, 4);
486                         do_subscribe(room, email, subtype, webpage);
487                 }
488         }
489         else if (!strcasecmp(opr, "unsubscribe")) {
490                 extract(room, cmdbuf, 1);
491                 extract(email, cmdbuf, 2);
492                 extract(webpage, cmdbuf, 3);
493                 do_unsubscribe(room, email, webpage);
494         }
495         else if (!strcasecmp(opr, "confirm")) {
496                 extract(room, cmdbuf, 1);
497                 extract(token, cmdbuf, 2);
498                 do_confirm(room, token);
499         }
500         else {
501                 cprintf("%d Invalid command\n", ERROR);
502         }
503 }
504
505
506 /*
507  * Module entry point
508  */
509 char *serv_listsub_init(void)
510 {
511         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
512         return "$Id$";
513 }