]> code.citadel.org Git - citadel.git/blob - citadel/serv_listsub.c
* Generate and send subscription confirmation requests.
[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, "%d%d%ld",
72                 ++seq,
73                 getpid(),
74                 time(NULL)
75         );
76
77         /* Convert it to base64 so it looks cool */     
78         encode_base64(buf, sourcebuf);
79 }
80
81
82 /*
83  * Enter a subscription request
84  */
85 void do_subscribe(char *room, char *email, char *subtype) {
86         struct quickroom qrbuf;
87         FILE *ncfp;
88         char filename[SIZ];
89         char token[SIZ];
90         char confirmation_request[SIZ];
91
92         if (getroom(&qrbuf, room) != 0) {
93                 cprintf("%d There is no list called '%s'\n", ERROR, room);
94                 return;
95         }
96
97         listsub_generate_token(token);
98
99         begin_critical_section(S_NETCONFIGS);
100         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
101         ncfp = fopen(filename, "a");
102         if (ncfp != NULL) {
103                 fprintf(ncfp, "subpending|%s|%s|%s|%ld\n",
104                         email,
105                         subtype,
106                         token,
107                         time(NULL)
108                 );
109                 fclose(ncfp);
110         }
111         end_critical_section(S_NETCONFIGS);
112
113         /* Generate and send the confirmation request */
114
115         snprintf(confirmation_request, sizeof confirmation_request,
116                 "Someone (probably you) has submitted a request to subscribe\n"
117                 "<%s> to the '%s' mailing list.\n\n"
118                 "In order to confirm this subscription request, please\n"
119                 "point your web browser at the following location:\n\n"
120                 "http://FIXME.com:FIXME/blah?room=%s&token=%s\n\n"
121                 "If this request has been submitted in error and you do not\n"
122                 "wish to receive the '%s' mailing list, simply do nothing,\n"
123                 "and you will not receive any further mailings.\n",
124
125                 email, qrbuf.QRname, qrbuf.QRname, token, qrbuf.QRname
126         );
127
128         quickie_message(
129                 "Citadel",
130                 email,
131                 qrbuf.QRname,
132                 confirmation_request
133         );
134
135         cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
136
137 }
138
139
140 /*
141  * Confirm a subscribe/unsubscribe request.
142  */
143 void do_confirm(char *room, char *token) {
144         struct quickroom qrbuf;
145         FILE *ncfp;
146         char filename[SIZ];
147         char line_token[SIZ];
148         long line_offset;
149         int line_length;
150         char buf[SIZ];
151         char cmd[SIZ];
152         char email[SIZ];
153         char subtype[SIZ];
154         int success = 0;
155
156         if (getroom(&qrbuf, room) != 0) {
157                 cprintf("%d There is no list called '%s'\n", ERROR, room);
158                 return;
159         }
160
161         begin_critical_section(S_NETCONFIGS);
162         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
163         ncfp = fopen(filename, "r+");
164         if (ncfp != NULL) {
165                 while (line_offset = ftell(ncfp),
166                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
167                         buf[strlen(buf)-1] = 0;
168                         line_length = strlen(buf);
169                         extract(cmd, buf, 0);
170                         if (!strcasecmp(cmd, "subpending")) {
171                                 extract(email, buf, 1);
172                                 extract(subtype, buf, 2);
173                                 extract(line_token, buf, 3);
174                                 if (!strcasecmp(token, line_token)) {
175                                         if (!strcasecmp(subtype, "digest")) {
176                                                 strcpy(buf, "digestrecp|");
177                                         }
178                                         else {
179                                                 strcpy(buf, "listrecp|");
180                                         }
181                                         strcat(buf, email);
182                                         strcat(buf, "|");
183                                         /* SLEAZY HACK: pad the line out so
184                                          * it's the same length as the line
185                                          * we're replacing.
186                                          */
187                                         while (strlen(buf) < line_length) {
188                                                 strcat(buf, " ");
189                                         }
190                                         fseek(ncfp, line_offset, SEEK_SET);
191                                         fprintf(ncfp, "%s\n", buf);
192                                         ++success;
193                                 }
194                         }
195                 }
196                 fclose(ncfp);
197         }
198         end_critical_section(S_NETCONFIGS);
199
200         if (success) {
201                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
202         }
203         else {
204                 cprintf("%d Invalid token.\n", ERROR);
205         }
206
207 }
208
209
210
211 /* 
212  * process subscribe/unsubscribe requests and confirmations
213  */
214 void cmd_subs(char *cmdbuf) {
215
216         char opr[SIZ];
217         char room[SIZ];
218         char email[SIZ];
219         char subtype[SIZ];
220         char token[SIZ];
221
222         extract(opr, cmdbuf, 0);
223         if (!strcasecmp(opr, "subscribe")) {
224                 extract(subtype, cmdbuf, 3);
225                 if ( (strcasecmp(subtype, "list"))
226                    && (strcasecmp(subtype, "digest")) ) {
227                         cprintf("%d Invalid subscription type.\n", ERROR);
228                 }
229                 else {
230                         extract(room, cmdbuf, 1);
231                         extract(email, cmdbuf, 2);
232                         do_subscribe(room, email, subtype);
233                 }
234         }
235         else if (!strcasecmp(opr, "unsubscribe")) {
236                 cprintf("%d not yet implemented\n", ERROR);
237         }
238         else if (!strcasecmp(opr, "confirm")) {
239                 extract(room, cmdbuf, 1);
240                 extract(token, cmdbuf, 2);
241                 do_confirm(room, token);
242         }
243         else {
244                 cprintf("%d Invalid command\n", ERROR);
245         }
246 }
247
248
249 /*
250  * Module entry point
251  */
252 char *Dynamic_Module_Init(void)
253 {
254         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
255         return "$Id$";
256 }