-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsubscribe.c
397 lines (358 loc) · 17.4 KB
/
unsubscribe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 1995-2019 Peter Simons <simons@cryp.to>
* Copyright (c) 2000-2001 Cable & Wireless GmbH
* Copyright (c) 1999-2000 CyberSolutions GmbH
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <string.h>
#include <errno.h>
#include "libtext/text.h"
#include "petidomo.h"
int
DeleteAddress(struct Mail * MailStruct,
const char * param1,
const char * param2,
const char * defaultlist)
{
const struct PD_Config * MasterConfig;
const struct List_Config * ListConfig;
FILE * fh;
const char * address = NULL;
const char * listname = NULL;
char owner[4096];
char envelope[4096];
char * buffer;
char * originator;
char * p;
char * list;
/* Initialize internal stuff from master config file. */
MasterConfig = getMasterConfig();
sprintf(envelope, "petidomo-manager@%s", MasterConfig->fqdn);
originator = (MailStruct->Reply_To) ? MailStruct->Reply_To : MailStruct->From;
/* Try to find out, which parameter is what. */
if (param1 != NULL)
{
if (isValidListName(param1) == TRUE)
listname = param1;
else if (isRFC822Address(param1) == TRUE)
address = param1;
if (param2 != NULL)
{
if (listname == NULL && isValidListName(param2) == TRUE)
listname = param2;
else if (address == NULL && isRFC822Address(param2) == TRUE)
address = param2;
}
}
if (address == NULL)
address = (MailStruct->Reply_To) ? MailStruct->Reply_To : MailStruct->From;
assert(address != NULL);
if (listname == NULL)
{
if (defaultlist != NULL)
listname = defaultlist;
else
{
syslog(LOG_INFO, "%s: unsubscribe-command invalid: No list specified.", MailStruct->From);
fh = vOpenMailer(envelope, originator, NULL);
if (fh != NULL)
{
fprintf(fh, "From: petidomo@%s (Petidomo Mailing List Server)\n", MasterConfig->fqdn);
fprintf(fh, "To: %s\n", originator);
fprintf(fh, "Subject: Petidomo: Your request \"unsubscribe %s\"\n", address);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
buffer = text_easy_sprintf("You tried to unsubscribe the address \"%s\" from a mailing list. " \
"Unfortunately, your request could not be processed, because " \
"you did not specify a valid mailing list name from which the " \
"address should be unsubscribed. You may use the command INDEX " \
"to receive an overview of the available mailing lists. Also, " \
"use the command HELP to verify that you got the command syntax " \
"right.", address);
text_wordwrap(buffer, 70);
fprintf(fh, "%s\n", buffer);
CloseMailer(fh);
}
else
syslog(LOG_ERR, "Failed to send email to \"%s\" concerning his request.", originator);
return 0;
}
}
/* Initialize internal stuff again from list config. */
ListConfig = getListConfig(listname);
sprintf(owner, "%s-owner@%s", listname, ListConfig->fqdn);
sprintf(envelope, "%s-owner@%s", listname, ListConfig->fqdn);
/* Check whether the request is authorized at all. */
if (isValidAdminPassword(getPassword(), listname) == FALSE)
{
/* No valid password, check further. */
if (ListConfig->subtype == SUBSCRIPTION_ADMIN)
{
/* Access was unauthorized, notify the originator. */
syslog(LOG_INFO, "%s: Attempt to unsubscribe \"%s\" from list \"%s\" rejected due to lack of " \
"a correct admin password.", MailStruct->From, address, listname);
fh = vOpenMailer(envelope, originator, NULL);
if (fh != NULL)
{
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n",
listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", originator);
fprintf(fh, "Subject: Petidomo: Your request \"unsubscribe %s %s\"\n",
address, listname);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
buffer = text_easy_sprintf("The mailing list \"%s\" is a closed forum and only the maintainer may " \
"unsubscribe addresses. Your request has been forwarded to the " \
"appropriate person, so please don't send any further mail. You will " \
"be notified as soon as possible.", listname);
text_wordwrap(buffer, 70);
fprintf(fh, "%s\n", buffer);
CloseMailer(fh);
}
else
syslog(LOG_ERR, "Failed to send email to \"%s\" concerning his request.", originator);
/* Notify the owner. */
fh = vOpenMailer(envelope, owner, NULL);
if (fh != NULL)
{
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n",
listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", owner);
fprintf(fh, "Subject: Petidomo: APPROVE %s@%s: Unauthorized request from \"%s\"\n", listname, ListConfig->fqdn, originator);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
buffer = text_easy_sprintf("\"%s\" tried to unsubscribe the address \"%s\" from the \"%s\" mailing list, " \
"but couldn't provide the correct password. To unsubscribe him, send the " \
"following commands to the server:", originator, address, listname);
text_wordwrap(buffer, 70);
fprintf(fh, "%s\n\n", buffer);
fprintf(fh, "password <AdminPassword>\n");
fprintf(fh, "unsubscribe %s %s\n", address, listname);
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\"!", owner);
return -1;
}
return 0;
}
}
/* Okay, remove the address from the list. */
if (isSubscribed(listname, address, &list, &p, FALSE) == FALSE)
{
syslog(LOG_INFO, "%s: Attempt to unsubscribe \"%s\" from list \"%s\" rejected, because the " \
"address is not on the list.", MailStruct->From, address, listname);
/* Notify the originator, that the address is not subscribed at
all. */
fh = vOpenMailer(envelope, originator, NULL);
if (fh != NULL)
{
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n",
listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", originator);
fprintf(fh, "Subject: Petidomo: Your request \"unsubscribe %s %s\"\n",
address, listname);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
fprintf(fh, "The address is not subscribed to this list.\n");
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\" concerning his request.",
originator);
return -1;
}
}
else
{
if (isValidAdminPassword(getPassword(), listname) == FALSE &&
ListConfig->subtype == SUBSCRIPTION_ACKED && !g_is_approved)
{
/* Require approval. */
char* command;
char c;
char* cookie;
syslog(LOG_INFO, "%s: Attempt to unsubscribe \"%s\" from list \"%s\" deferred, because the " \
"request must be acknowledged first.", MailStruct->From, address, listname);
command = text_easy_sprintf("unsubscribe %s %s", address, listname);
cookie = queue_command(MailStruct, command);
/* Send request for approval to the user. */
fh = vOpenMailer(envelope, address, NULL);
if (fh != NULL)
{
fprintf(fh, "From: petidomo-approve@%s (Petidomo Mailing List Server)\n", ListConfig->fqdn);
fprintf(fh, "To: %s\n", address);
if (strcasecmp(address, originator) == 0)
fprintf(fh, "Subject: Petidomo: APPROVE %s@%s: Your request \"subscribe %s\"\n", listname, ListConfig->fqdn, listname);
else
fprintf(fh, "Subject: Petidomo: APPROVE %s@%s: Request \"subscribe %s\" from \"%s\"\n", listname, ListConfig->fqdn, listname, originator);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
if (strcasecmp(address, originator) == 0)
buffer = text_easy_sprintf("You requested that the address \"%s\" should be unsubscribed from " \
"the mailing list \"%s\". This will not happen unless you approve the " \
"request by replying to this mail and concatenating the following two strings into one",
originator, address, listname);
else
buffer = text_easy_sprintf("Per request from \"%s\", the address \"%s\" should be unsubscribed from " \
"the mailing list \"%s\". This will not happen unless you approve the " \
"request by replying to this mail and concatenating the following two strings into one",
originator, address, listname);
text_wordwrap(buffer, 70);
fprintf(fh, "%s\n", buffer);
fprintf(fh, "\n");
c = cookie[16];
cookie[16] = '\0'; fprintf(fh, " %s\n", &cookie[ 0]);
cookie[16] = c; fprintf(fh, " %s\n", &cookie[16]);
fprintf(fh, "\n");
fprintf(fh, "in your reply.\n");
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\"!", owner);
return -1;
}
/* If the request for approval has been sent to an
address different to that of the originator, notify him
what happened. */
if (strcasecmp(address, originator) == 0)
{
fh = vOpenMailer(envelope, originator, NULL);
if (fh != NULL)
{
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n", listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", originator);
fprintf(fh, "Subject: Petidomo: Your request \"unsubscribe %s %s\"\n", address, listname);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
fprintf(fh, "Unsubscribing the address \"%s\" from the list \"%s\"\n", address, listname);
fprintf(fh, "requires additional approval by \"%s\". A request\n", address);
fprintf(fh, "has been sent to this address. We are now awaiting the approval.\n");
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\" concerning his request.", originator);
return -1;
}
}
return 0;
}
syslog(LOG_INFO, "%s: Okay; unsubscribing address \"%s\" from list \"%s\".", MailStruct->From, address, listname);
fh = fopen(ListConfig->address_file, "w");
if (fh == NULL)
{
syslog(LOG_ERR, "Failed to open file \"%s\" for writing: %s", ListConfig->address_file, strerror(errno));
return -1;
}
*p++ = '\0';
fprintf(fh, "%s", list);
p = text_find_next_line(p); /* skip address in question */
fprintf(fh, "%s", p);
fclose(fh);
/* Send success notification to the originator and the unsubscribed address */
if (!strcasecmp(address, originator))
fh = vOpenMailer(envelope, address, NULL);
else
fh = vOpenMailer(envelope, address, originator, NULL);
if (fh != NULL)
{
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n",
listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", address);
if (strcasecmp(address, originator) == TRUE)
fprintf(fh, "Cc: %s\n", originator);
fprintf(fh, "Subject: Petidomo: Request \"unsubscribe %s %s\"\n", address, listname);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
if (!strcasecmp(address, originator))
{
buffer = text_easy_sprintf(
"Per your request, the address \"%s\" has been unsubscribed from the " \
"\"%s\" mailing list.\n\n", address, listname);
}
else
{
buffer = text_easy_sprintf(
"Per request from \"%s\", the address \"%s\" has been unsubscribed from the " \
"\"%s\" mailing list.\n\n", originator, address, listname);
}
text_wordwrap(buffer, 70);
fprintf(fh, "%s", buffer);
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\"!", address);
return -1;
}
}
/* Send success notification to the list owner */
fh = vOpenMailer(envelope, owner, NULL);
if (fh != NULL) {
fprintf(fh, "From: %s-request@%s (Petidomo Mailing List Server)\n",
listname, ListConfig->fqdn);
fprintf(fh, "To: %s\n", owner);
fprintf(fh, "Subject: Petidomo: UNSUBSCRIBE %s@%s: %s\n", listname, ListConfig->fqdn, address);
if (MailStruct->Message_Id != NULL)
fprintf(fh, "In-Reply-To: %s\n", MailStruct->Message_Id);
fprintf(fh, "Precedence: junk\n");
fprintf(fh, "Sender: %s\n", envelope);
fprintf(fh, "\n");
if (!strcasecmp(address, originator)) {
buffer = text_easy_sprintf(
"Per request from the subscriber, the address \"%s\"\n"
"has been unsubscribed from the \"%s\" mailing list.\n",
address, listname, ListConfig->fqdn);
}
else {
buffer = text_easy_sprintf(
"Per request from \"%s\", the address \"%s\"\n"
"has been unsubscribed from the \"%s\" mailing list.\n",
originator, address, listname, ListConfig->fqdn);
}
text_wordwrap(buffer, 75);
fprintf(fh, "%s\n", buffer);
fprintf(fh, "No action is required on your part.\n");
CloseMailer(fh);
}
else {
syslog(LOG_ERR, "Failed to send email to \"%s\"!", owner);
return -1;
}
free(list);
return 0;
}