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
|
/* $Cambridge: hermes/src/prayer/cmd/cmd_send.c,v 1.14 2009/08/13 09:45:18 dpc22 Exp $ */
/************************************************
* Prayer - a Webmail Interface *
************************************************/
/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */
#include "prayer_session.h"
struct channel {
struct iostream *output;
pid_t child;
};
static struct channel *channel_open(struct session *session,
struct pool *p, char *cmd)
{
struct request *request = session->request;
pid_t childpid;
int pipefd[2];
struct channel *channel = pool_alloc(p, sizeof(struct channel));
char **argv;
char *path;
int argc;
char *s, *t;
/* Local scratch copy of cmd */
cmd = pool_strdup(p, cmd);
/* Count number of tokens in cmd */
argc = 0;
s = cmd;
while ((string_skip_token(&s)))
argc++;
/* Allocate (temporary) argv array based on this count */
argv = pool_alloc(p, (argc + 1) * sizeof(char *));
/* Split up cmd (destructive) */
argc = 0;
while ((t = string_get_token(&cmd)))
argv[argc++] = t;
argv[argc] = NIL;
/* Set up child process */
if (pipe(pipefd) < 0) {
session_log(session, "[cmd_send] pipe(): %s", strerror(errno));
return (NIL);
}
if ((childpid = fork()) < 0) {
session_log(session, "[cmd_send] fork(): %s", strerror(errno));
return (NIL);
}
if (childpid == 0) {
/* Redirect stdin for child process */
close(pipefd[1]);
dup2(pipefd[0], 0);
close(pipefd[0]);
/* Redirect stdout and stderr child to /dev/null */
close(1);
open("/dev/null", O_WRONLY | O_CREAT | O_APPEND, 0666);
dup2(1, 2);
path = argv[0];
if ((s = strrchr(path, '/'))) /* Should be leave path intact? */
argv[0] = s + 1;
execv(path, argv);
session_fatal(session, "[cmd_send] exec(sendmail): %s",
strerror(errno));
}
/* Parent */
close(pipefd[0]);
channel->child = childpid;
channel->output = iostream_create(request->pool, pipefd[1], 0);
return (channel);
}
static int channel_close(struct channel *channel)
{
int status = 0;
int rc = 0;
iostream_close(channel->output);
do {
rc = waitpid(channel->child, &status, 0);
} while ((rc < 0) && (errno == EINTR));
if ((rc >= 0) && WIFEXITED(status) && (WEXITSTATUS(status) == 0))
return(T);
return(NIL);
}
static void
touch_file(char *path)
{
FILE *file;
if (path && path[0]) {
if ((file = fopen(path, "a+")))
fclose(file);
}
}
/* ====================================================================== */
void cmd_send(struct session *session)
{
struct config *config = session->config;
struct request *request = session->request;
struct draft *draft = session->draft;
MAILSTREAM *stream = session->stream;
struct options *options = session->options;
struct prefs *prefs = options->prefs;
struct pool *pool = request->pool;
struct buffer *cb = buffer_create(pool, 1024);
char *s;
int c;
unsigned long offset;
char *recips, *msg;
STRING ms;
char *command;
struct channel *channel;
struct iostream *output;
unsigned long msg_len = 0L;
char *sendmail;
unsigned long recips_count = 0;
if (config->sendmail_path && config->sendmail_path[0])
sendmail = config->sendmail_path;
else
sendmail = "/usr/lib/sendmail";
if (!(recips = draft_make_recipients(draft, NIL, &recips_count))) {
session_redirect(session, request, "compose");
return;
}
if ((config->recips_max_msg > 0) &&
(recips_count > config->recips_max_msg)) {
session_message(session, "Too many recipients");
session_log(session, "Too many recipients");
session_redirect(session, request, "compose");
return;
}
/* Block sending altogether if per session limit reached */
session->recips_count_session += recips_count;
if (!session->sending_allow &&
(config->recips_max_session > 0) &&
(session->recips_count_session > config->recips_max_session)) {
struct stat sbuf;
char *allow_path = NIL;
if (config->sending_allow_dir && session->username) {
allow_path = pool_strcat3(request->pool,
config->sending_allow_dir, "/",
session->username);
}
if (allow_path && (stat(allow_path, &sbuf) == 0)) {
session->sending_allow = T;
} else {
session->sending_block = T;
/* Stop subsequent logins from sending either */
if (config->sending_block_dir && session->username) {
touch_file(pool_strcat3(request->pool,
config->sending_block_dir, "/",
session->username));
}
}
} else if (config->sending_block_dir) {
struct stat sbuf;
char *path = pool_strcat3(request->pool,
config->sending_block_dir, "/",
session->username);
if (stat(path, &sbuf) == 0) {
session->sending_block = T;
}
}
if (session->sending_block) {
session_message(session,
"Outgoing email disabled (compromised account?)");
session_log(session,
"Outgoing email disabled (compromised account?)");
session_redirect(session, request, "compose");
return;
}
if (!(recips && recips[0])) {
session_message(session, "No recipients for message");
session_redirect(session, request, "compose");
return;
}
if (prefs->line_wrap_advanced) {
if (draft->line_wrap)
draft_line_wrap_body(draft);
} else if (prefs->line_wrap_on_send)
draft_line_wrap_body(draft);
if (!(msg = draft_make_msg(draft, NIL, &msg_len))) {
session_redirect(session, request, "compose");
return;
}
if (draft->save_copy && draft->fcc && draft->fcc[0]) {
char *fcc_name;
if (prefs->maildir && prefs->maildir[0])
fcc_name =
pool_strcat3(request->pool, prefs->maildir, session->hiersep,
draft->fcc);
else
fcc_name = draft->fcc;
/* Convert simple "char *" string into c-client "STRING *" string */
INIT(&ms, mail_string, msg, msg_len);
ml_clear_error();
ml_clear_have_close();
/* Append message to end of sent-mail folder */
ml_append(session, stream,
session_mailbox(session, request->pool, fcc_name), &ms);
if (ml_have_close()) {
session_redirect(session, request, "restart");
return;
} else if (ml_have_error()) {
session_alert(session,
"Unable to append message to %s folder: %s",
draft->fcc, ml_errmsg());
session_log(session,
"[cmd_send] Unable to write to %s folder: %s",
draft->fcc, ml_errmsg());
session_redirect(session, request, "compose");
return;
}
}
/* -oi important: allows lines with single '.' in message body */
if (strchr(session->username, '@')) {
bprintf(cb, "%s -oi -f %s %s", sendmail, session->username, recips);
} else {
bprintf(cb, "%s -oi -f %s@%s %s", sendmail,
session->username, config->return_path_domain, recips);
}
command = buffer_fetch(cb, 0, buffer_size(cb), NIL);
if ((channel = channel_open(session, request->pool, command)) == NIL) {
session_alert(session,
"Failed to send message (system problem?)");
session_log(session,
"[cmd_send] Failed to send message: failed to start %s",
command);
session_redirect(session, request, "compose");
return;
}
output = channel->output;
s = msg;
offset = 0;
/* Copy hdr replacing CRLF, CR or LF with LF. Remove Bcc headers */
while ((c = *s)) {
if ((offset == 0) && !strncasecmp(s, "Bcc:", strlen("Bcc:"))) {
/* Skip over Bcc header (may be folded over several lines) */
while ((c = *s++)) {
if ((c == '\015') || (c == '\012')) {
if ((c == '\015') && (*s == '\012'))
s++;
if (((c = *s) != ' ') && (c != '\t'))
break;
}
}
continue;
}
if ((c == '\015') || (c == '\012')) {
/* Replace CRLF with single LF */
s += ((s[0] == '\015') && (s[1] == '\012')) ? 2 : 1;
ioputc('\n', output);
if (offset == 0)
break;
offset = 0;
continue;
}
ioputc(c, output);
s++;
offset++;
}
/* Copy body replacing CRLF, CR or LF with LF */
while ((c = *s)) {
if ((c == '\015') || (c == '\012')) {
/* Replace CRLF with single LF */
s += ((s[0] == '\015') && (s[1] == '\012')) ? 2 : 1;
ioputc('\n', output);
continue;
}
ioputc(c, output);
s++;
offset++;
}
if (!channel_close(channel)) {
session_alert(session,
"Failure sending message (system problem)");
session_log(session,
"[cmd_send] Failed to send message: failure reported by %s",
command);
session_redirect(session, request, "compose");
return;
}
session_message(session, "Message sent");
session_log(session, "[cmd_send] Message sent (%lu recipients)",
recips_count);
if (draft_is_reply(draft))
draft_flag_answered(draft);
/* Finished with this draft */
draft_clear_atts(session->draft);
draft_free(session->draft);
draft_free(session->draft0);
session_redirect(session, request, session->compose_parent_cmd);
}
|