-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprove.c
138 lines (119 loc) · 4.46 KB
/
approve.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
/*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "petidomo.h"
#include "libtext/text.h"
void approve_main(char* mail)
{
const struct PD_Config* MasterConfig = getMasterConfig();
struct Mail* MailStruct;
char* originator;
char* envelope;
FILE* fh;
static const char* cookie_regex = "[0-9a-f]{32}";
regex_t preg;
regmatch_t match[3];
int offset;
int number_of_hits = 0;
if (chdir(MasterConfig->ack_queue_dir) == -1)
{
syslog(LOG_ERR, "Can't change directory to \"%s\": %s", MasterConfig->ack_queue_dir, strerror(errno));
exit(EXIT_FAILURE);
}
if (regcomp(&preg, cookie_regex, REG_EXTENDED | REG_ICASE) != 0)
{
syslog(LOG_CRIT, "Can't compile my internal regular expressions. This is serious!");
exit(EXIT_FAILURE);
}
offset = 0;
while(regexec(&preg, mail + offset, sizeof(match)/sizeof(regmatch_t), match, 0) == 0)
{
struct stat sb;
char buffer[33];
char* src;
char* dst = buffer;
unsigned int i;
/* Copy found string into buffer and convert it to all
upper-case while doing so. */
src = mail + offset + match[0].rm_so;
for (i = 0; i < 32; ++i)
*dst++ = toupper(*src++);
*dst = '\0';
/* Correct offset for the next match. */
offset += match[0].rm_so + 1;
/* Do we have a hit here? If, execute the file and remove it.
Then go on. */
if (stat(buffer, &sb) == 0)
{
char cmd[128];
syslog(LOG_INFO, "Received valid approval code for spool file \"%s\".", buffer);
++number_of_hits;
sprintf(cmd, "/bin/sh %s && /bin/rm -f %s", buffer, buffer);
if (((signed char)system(cmd)) == -1)
{
syslog(LOG_ERR, "system() failed for \"%s\": %s", buffer, strerror(errno));
exit(EXIT_FAILURE);
}
}
else
syslog(LOG_INFO, "Received approval code \"%s\", but there is no corresponding posting.", buffer);
}
/* Report results back to the originator */
if (ParseMail(&MailStruct, mail, MasterConfig->fqdn) != 0)
{
syslog(LOG_ERR, "Parsing the incoming mail failed.");
exit(EXIT_FAILURE);
}
if (MailStruct->From == NULL)
{
syslog(LOG_NOTICE, "Received mail without From: line.");
return;
}
originator = (MailStruct->Reply_To) ? MailStruct->Reply_To : MailStruct->From;
envelope = text_easy_sprintf("petidomo-manager@%s", MasterConfig->fqdn);
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 approval mail has been received\n");
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 (number_of_hits > 0)
fprintf(fh, "Your approval mail has been received and been processed sucessfully.");
else
fprintf(fh, "I couldn't find any approval codes in your mail.");
CloseMailer(fh);
}
else
{
syslog(LOG_ERR, "Failed to send email to \"%s\" concerning his request.", originator);
exit(EXIT_FAILURE);
}
}