]> jfr.im git - irc/evilnet/x3.git/blame - src/sendmail.c
found out from Kyle what b flag is.
[irc/evilnet/x3.git] / src / sendmail.c
CommitLineData
d76ed9a9
AS
1/* sendmail.c - mail sending utilities
2 * Copyright 2002-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9
AS
5 *
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21#include "conf.h"
22#include "modcmd.h"
23#include "nickserv.h"
24#include "saxdb.h"
25
26#ifdef HAVE_SYS_WAIT_H
27#include <sys/wait.h>
28#endif
29
30#define KEY_PROHIBITED "prohibited"
31
32static const struct message_entry msgtab[] = {
33 { "MAILMSG_EMAIL_ALREADY_BANNED", "%s is already banned (%s)." },
34 { "MAILMSG_EMAIL_BANNED", "Email to %s has been forbidden." },
35 { "MAILMSG_EMAIL_NOT_BANNED", "Email to %s was not forbidden." },
36 { "MAILMSG_EMAIL_UNBANNED", "Email to %s is now allowed." },
de9510bc 37 { "MAILMSG_PROHIBITED_EMAIL_HEADER", "$bBanned Email Address Masks$b" },
d76ed9a9 38 { "MAILMSG_PROHIBITED_EMAIL", "%s: %s" },
de9510bc 39 { "MAILMSG_PROHIBITED_EMAIL_END", "-------End of Banned Address Masks------" },
d76ed9a9
AS
40 { "MAILMSG_NO_PROHIBITED_EMAIL", "All email addresses are accepted." },
41 { NULL, NULL }
42};
43
44static dict_t prohibited_addrs, prohibited_masks;
45struct module *sendmail_module;
46
47const char *
48sendmail_prohibited_address(const char *addr)
49{
50 dict_iterator_t it;
51 const char *data;
52
53 if (prohibited_addrs && (data = dict_find(prohibited_addrs, addr, NULL)))
54 return data;
55 if (prohibited_masks)
56 for (it = dict_first(prohibited_masks); it; it = iter_next(it))
57 if (match_ircglob(addr, iter_key(it)))
58 return iter_data(it);
59 return NULL;
60}
61
62/* This function sends the given "paragraph" as flowed text, as
63 * defined in RFC 2646. It lets us only worry about line wrapping
64 * here, and not in the code that generates mail.
65 */
66static void
67send_flowed_text(FILE *where, const char *para)
68{
69 const char *eol = strchr(para, '\n');
70 unsigned int shift;
71
72 while (*para) {
73 /* Do we need to space-stuff the line? */
74 if ((*para == ' ') || (*para == '>') || !strncmp(para, "From ", 5)) {
75 fputc(' ', where);
76 shift = 1;
77 } else {
78 shift = 0;
79 }
80 /* How much can we put on this line? */
81 if (!eol && (strlen(para) < (80 - shift))) {
82 /* End of paragraph; can put on one line. */
83 fputs(para, where);
84 fputs("\n", where);
85 break;
86 } else if (eol && (eol < para + (80 - shift))) {
87 /* Newline inside paragraph, no need to wrap. */
88 fprintf(where, "%.*s\n", eol - para, para);
89 para = eol + 1;
90 } else {
91 int pos;
92 /* Need to wrap. Where's the last space in the line? */
93 for (pos=72-shift; pos && (para[pos] != ' '); pos--) ;
94 /* If we didn't find a space, look ahead instead. */
95 if (pos == 0) pos = strcspn(para, " \n");
96 fprintf(where, "%.*s\n", pos+1, para);
97 para += pos + 1;
98 }
99 if (eol && (eol < para)) eol = strchr(para, '\n');
100 }
101}
102
103void
104sendmail(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time)
105{
106 pid_t child;
107 int infds[2], outfds[2];
108 const char *fromaddr, *str;
109
110 /* Grab some config items first. */
111 str = conf_get_data("mail/enable", RECDB_QSTRING);
112 if (!str || !enabled_string(str))
113 return;
114 fromaddr = conf_get_data("mail/from_address", RECDB_QSTRING);
115
116 /* How this works: We fork, and the child tries to send the mail.
117 * It does this by setting up a pipe pair, and forking again (the
118 * grandchild exec()'s the mailer program). The mid-level child
119 * sends the text to the grandchild's stdin, and then logs the
120 * success or failure.
121 */
122
123 child = fork();
124 if (child < 0) {
125 log_module(MAIN_LOG, LOG_ERROR, "sendmail() to %s couldn't fork(): %s (%d)", to->email_addr, strerror(errno), errno);
126 return;
127 } else if (child > 0) {
128 return;
129 }
130 /* We're in a child now; must _exit() to die properly. */
131 if (pipe(infds) < 0) {
132 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't pipe(infds): %s (%d)", to->email_addr, strerror(errno), errno);
133 _exit(1);
134 }
135 if (pipe(outfds) < 0) {
136 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't pipe(outfds): %s (%d)", to->email_addr, strerror(errno), errno);
137 _exit(1);
138 }
139 child = fork();
140 if (child < 0) {
141 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't fork(): %s (%d)", to->email_addr, strerror(errno), errno);
142 _exit(1);
143 } else if (child > 0) {
144 /* Mid-level child; get ready to send the mail. */
145 FILE *out = fdopen(infds[1], "w");
146 struct string_list *extras;
147 unsigned int nn;
148 int res, rv;
149
150 /* Close the end of pipes we do not use. */
151 close(infds[0]);
152 close(outfds[1]);
153
154 /* Do we have any "extra" headers to send? */
155 extras = conf_get_data("mail/extra_headers", RECDB_STRING_LIST);
156 if (extras) {
157 for (nn=0; nn<extras->used; nn++) {
158 fputs(extras->list[nn], out);
159 fputs("\n", out);
160 }
161 }
162
163 /* Content type? (format=flowed is a standard for plain text
164 * that lets the receiver reconstruct paragraphs, defined in
165 * RFC 2646. See comment above send_flowed_text() for more.)
166 */
167 if (!(str = conf_get_data("mail/charset", RECDB_QSTRING))) str = "us-ascii";
168 fprintf(out, "Content-Type: text/plain; charset=%s; format=flowed\n", str);
169
170 /* Send From, To and Subject headers */
171 if (!fromaddr) fromaddr = "admin@poorly.configured.network";
172 fprintf(out, "From: %s <%s>\n", from->nick, fromaddr);
173 fprintf(out, "To: \"%s\" <%s>\n", to->handle, to->email_addr);
174 fprintf(out, "Subject: %s\n", subject);
175
176 /* Send mail body */
177 fputs("\n", out); /* terminate headers */
178 extras = conf_get_data((first_time?"mail/body_prefix_first":"mail/body_prefix"), RECDB_STRING_LIST);
179 if (extras) {
180 for (nn=0; nn<extras->used; nn++) {
181 send_flowed_text(out, extras->list[nn]);
182 }
183 fputs("\n", out);
184 }
185 send_flowed_text(out, body);
186 extras = conf_get_data((first_time?"mail/body_suffix_first":"mail/body_suffix"), RECDB_STRING_LIST);
187 if (extras) {
188 fputs("\n", out);
189 for (nn=0; nn<extras->used; nn++)
190 send_flowed_text(out, extras->list[nn]);
191 }
192
193 /* Close file (sending mail) and check for return code */
194 fflush(out);
195 fclose(out);
196 do {
197 rv = wait4(child, &res, 0, NULL);
198 } while ((rv == -1) && (errno == EINTR));
199 if (rv == child) {
200 /* accept the wait() result */
201 } else {
202 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s: Bad wait() return code %d: %s (%d)", to->email_addr, rv, strerror(errno), errno);
203 _exit(1);
204 }
205 if (res) {
206 log_module(MAIN_LOG, LOG_ERROR, "sendmail() grandchild to %s: Exited with code %d", to->email_addr, res);
207 _exit(1);
208 } else {
209 log_module(MAIN_LOG, LOG_INFO, "sendmail() sent email to %s <%s>: %s", to->handle, to->email_addr, subject);
210 }
211 _exit(0);
212 } else {
213 /* Grandchild; dup2 the fds and exec the mailer. */
214 const char *argv[10], *mpath;
215 unsigned int argc = 0;
216
217 /* Close the end of pipes we do not use. */
218 close(infds[1]);
219 close(outfds[0]);
220
221 dup2(infds[0], STDIN_FILENO);
222 dup2(outfds[1], STDOUT_FILENO);
223 mpath = conf_get_data("mail/mailer", RECDB_QSTRING);
224 if (!mpath) mpath = "/usr/sbin/sendmail";
225 argv[argc++] = mpath;
226 if (fromaddr) {
227 argv[argc++] = "-f";
228 argv[argc++] = fromaddr;
229 }
230 argv[argc++] = to->email_addr;
231 argv[argc++] = NULL;
232 if (execv(mpath, (char**)argv) < 0) {
233 log_module(MAIN_LOG, LOG_ERROR, "sendmail() grandchild to %s couldn't execv(): %s (%d)", to->email_addr, strerror(errno), errno);
234 }
235 _exit(1);
236 }
237}
238
239static int
240sendmail_ban_address(struct userNode *user, struct userNode *bot, const char *addr, const char *reason) {
241 dict_t target;
242 const char *str;
243
244 target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs;
245 if ((str = dict_find(target, addr, NULL))) {
246 if (user)
247 send_message(user, bot, "MAILMSG_EMAIL_ALREADY_BANNED", addr, str);
248 return 0;
249 }
250 dict_insert(target, strdup(addr), strdup(reason));
251 if (user) send_message(user, bot, "MAILMSG_EMAIL_BANNED", addr);
252 return 1;
253}
254
255static MODCMD_FUNC(cmd_banemail) {
256 char *reason = unsplit_string(argv+2, argc-2, NULL);
257 return sendmail_ban_address(user, cmd->parent->bot, argv[1], reason);
258}
259
260static MODCMD_FUNC(cmd_unbanemail) {
261 dict_t target;
262 const char *addr;
263
264 addr = argv[1];
265 target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs;
266 if (dict_remove(target, addr))
267 reply("MAILMSG_EMAIL_UNBANNED", addr);
268 else
269 reply("MAILMSG_EMAIL_NOT_BANNED", addr);
270 return 1;
271}
272
273static MODCMD_FUNC(cmd_stats_email) {
274 dict_iterator_t it;
275 int found = 0;
276
de9510bc
AS
277 reply("MAILMSG_PROHIBITED_EMAIL_HEADER");
278 reply("MSG_BAR");
d76ed9a9
AS
279 for (it=dict_first(prohibited_addrs); it; it=iter_next(it)) {
280 reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it));
281 found = 1;
282 }
283 for (it=dict_first(prohibited_masks); it; it=iter_next(it)) {
284 reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it));
285 found = 1;
286 }
287 if (!found)
288 reply("MAILMSG_NO_PROHIBITED_EMAIL");
de9510bc
AS
289 else
290 reply("MAILMSG_PROHIBITED_EMAIL_END");
d76ed9a9
AS
291 return 0;
292}
293
294static int
295sendmail_saxdb_read(struct dict *db) {
296 struct dict *subdb;
297 struct record_data *rd;
298 dict_iterator_t it;
299
300 if ((subdb = database_get_data(db, KEY_PROHIBITED, RECDB_OBJECT))) {
301 for (it = dict_first(subdb); it; it = iter_next(it)) {
302 rd = iter_data(it);
303 if (rd->type == RECDB_QSTRING)
304 sendmail_ban_address(NULL, NULL, iter_key(it), rd->d.qstring);
305 }
306 }
307 return 0;
308}
309
310static int
311sendmail_saxdb_write(struct saxdb_context *ctx) {
312 dict_iterator_t it;
313
314 saxdb_start_record(ctx, KEY_PROHIBITED, 0);
315 for (it = dict_first(prohibited_masks); it; it = iter_next(it))
316 saxdb_write_string(ctx, iter_key(it), iter_data(it));
317 for (it = dict_first(prohibited_addrs); it; it = iter_next(it))
318 saxdb_write_string(ctx, iter_key(it), iter_data(it));
319 saxdb_end_record(ctx);
320 return 0;
321}
322
323static void
324sendmail_cleanup(void)
325{
326 dict_delete(prohibited_addrs);
327 dict_delete(prohibited_masks);
328}
329
330void
331sendmail_init(void)
332{
333 prohibited_addrs = dict_new();
334 dict_set_free_keys(prohibited_addrs, free);
335 dict_set_free_data(prohibited_addrs, free);
336 prohibited_masks = dict_new();
337 dict_set_free_keys(prohibited_masks, free);
338 dict_set_free_data(prohibited_masks, free);
339 reg_exit_func(sendmail_cleanup);
340 saxdb_register("sendmail", sendmail_saxdb_read, sendmail_saxdb_write);
341 sendmail_module = module_register("sendmail", MAIN_LOG, "sendmail.help", NULL);
342 modcmd_register(sendmail_module, "banemail", cmd_banemail, 3, 0, "level", "601", NULL);
343 modcmd_register(sendmail_module, "stats email", cmd_stats_email, 0, 0, "flags", "+oper", NULL);
344 modcmd_register(sendmail_module, "unbanemail", cmd_unbanemail, 2, 0, "level", "601", NULL);
345 message_register_table(msgtab);
346}