32 #include <sys/types.h>
40 #define MAIL_BODY_GROWTH_SIZE 1024
43 #define MAIL_BODY_RESERVED_SIZE 80
58 static char gSendMailPath[64];
107 if (gSendMailPath[0] ==
'\0') {
109 if (access(
"/usr/lib/sendmail", X_OK) == 0) {
110 strcpy(gSendMailPath,
"/usr/lib/sendmail");
114 snprintf(errstr, errlen,
115 "Could not create mail message: '%s'\n"
116 " -> sendmail program not found\n", subject);
118 return((
Mail *)NULL);
124 mail = (
Mail *)calloc(1,
sizeof(
Mail));
128 snprintf(errstr, errlen,
129 "Could not create mail message: '%s'\n"
130 " -> memory allocation error\n", subject);
132 return((
Mail *)NULL);
145 if (to && !alloc_error) {
153 if (cc && !alloc_error) {
161 if (subject && !alloc_error) {
174 mail->
body = (
char *)calloc(mail->
nalloced,
sizeof(
char));
192 snprintf(errstr, errlen,
193 "Could not create mail message: '%s'\n"
194 " -> memory allocation error\n", subject);
196 return((
Mail *)NULL);
214 if (mail->
to) free(mail->
to);
215 if (mail->
cc) free(mail->
cc);
247 if (mail->
errstr[0] ==
'\0') {
248 return((
const char *)NULL);
251 return((
const char *)mail->
errstr);
274 const char *format, ...)
278 va_start(args, format);
320 if (strcmp(format,
"MSNGR_MESSAGE_BLOCK") == 0) {
322 va_copy(args_copy, args);
323 msg_block = va_arg(args_copy,
char **);
328 for (i = 0; msg_block[i] != (
char *)NULL; i++) {
329 msg_length += strlen(msg_block[i]);
333 msg_block = (
char **)NULL;
339 if (msg_length >= space_left) {
346 while (new_size < min_size) {
350 new_body = (
char *)realloc(mail->
body, new_size *
sizeof(
char));
359 mail->
body = new_body;
372 "Could not increase mail buffer size, "
373 "sending mail and flushing buffer.\n\n");
386 if (msg_length < space_left) {
397 if (msg_length && msg_block) {
398 for (i = 0; msg_block[i] != (
char *)NULL; i++) {
399 msg_start += sprintf(msg_start, msg_block[i]);
403 mail->
length += msg_length;
409 if (*msg_end !=
'\n') {
440 struct iovec iov[32];
458 iov[iovcnt++].iov_base =
"To: ";
459 iov[iovcnt++].iov_base = mail->
to;
460 iov[iovcnt++].iov_base =
"\n";
463 iov[iovcnt++].iov_base =
"Cc: ";
464 iov[iovcnt++].iov_base = mail->
cc;
465 iov[iovcnt++].iov_base =
"\n";
469 iov[iovcnt++].iov_base =
"Subject: ";
470 iov[iovcnt++].iov_base = mail->
subject;
471 iov[iovcnt++].iov_base =
"\n";
472 iov[iovcnt++].iov_base =
"\n";
475 iov[iovcnt++].iov_base = mail->
body;
477 for (i = 0; i < iovcnt; i++) {
478 iov[i].iov_len = strlen(iov[i].iov_base);
483 if (pipe(mail_pipe) == -1) {
486 "Could not create pipe to send mail message: '%s'\n"
487 " -> %s\n", mail->
subject, strerror(errno));
490 mail->
body[0] =
'\0';
499 if (mail_pid == (pid_t)-1) {
502 "Could not create fork to send mail message: '%s'\n"
503 " -> %s\n", mail->
subject, strerror(errno));
506 mail->
body[0] =
'\0';
515 dup2(mail_pipe[0], STDIN_FILENO);
520 execl(gSendMailPath, gSendMailPath,
521 "-f", mail->
from,
"-t", mail->
to, NULL);
524 execl(gSendMailPath, gSendMailPath,
525 "-t", mail->
to, NULL);
537 if (writev(mail_pipe[1], iov, iovcnt) == -1) {
540 "Could not write mail message: '%s'\n"
541 " -> %s\n", mail->
subject, strerror(errno));
549 wait(&mail_exit_value);
551 if (mail_exit_value) {
553 int exit_value = mail_exit_value >> 8;
554 int signal_number = mail_exit_value & 127;
555 int core_dumped = mail_exit_value & 128;
560 "Could not execute sendmail command: '%s'\n"
561 " -> core dumped with signal #%d\n",
562 gSendMailPath, signal_number);
564 else if (signal_number) {
567 "Could not execute sendmail command: '%s'\n"
568 " -> exited with signal #%d\n",
569 gSendMailPath, signal_number);
574 "Could not execute sendmail command: '%s'\n"
576 gSendMailPath, strerror(exit_value));
583 mail->
body[0] =
'\0';
601 mail->
flags |= flags;
614 mail->
flags &= (0xffff ^ flags);