#include #include #include #include #include #include #include #include #include #include #include #include #define PATH_SENDMAIL "/usr/lib/sendmail" /*********************************************************************/ /* Function: deliver_reply /* /* Returns 0 good /* !0 bad /* /* args: /* from from /* to recipient /* subject subject /* loop loop id for detecting duplicates /* entry_id The ARSystem entry id this msg was queue'd under /* csum The secret used to query the entry_id /* msg The original request /* msg_file pathname of the reply text /* /* /*********************************************************************/ deliver_mail(from, to, subject, loop, entry_id, csum, msg, msg_file, reply_body) char *from, *to, *subject, *loop, *entry_id, *csum, *msg, *msg_file; char *reply_body; { extern int errno; int err, pipefd[2], statloc; FILE *FP; extern int got_sigpipe; extern char *my_env[]; pid_t pid; err = 1; FP = (FILE*)NULL; pipefd[0] = pipefd[1] = -1; got_sigpipe = 0; if (!to || !to[0]) { fprintf(stderr, "deliver_mail(): no recipient\n"); return 1; } if (pipe(pipefd) == -1) { fprintf(stderr, "pipe(): %s\n", strerror(errno)); goto out_deliver_mail; } if ((pid = fork()) == -1) { fprintf(stderr, "fork(): %s\n", strerror(errno)); goto out_deliver_mail; } if (pid) { /* parent */ /* close read end */ if (close (pipefd[0])) { fprintf(stderr, "pipe read close(): %s\n", strerror(errno)); goto out_deliver_mail; } pipefd[0] = -1; /* associate a stream with the write end */ if (!(FP = fdopen(pipefd[1], "w"))) { fprintf(stderr, "can't fdopen() pipe\n"); goto out_deliver_mail; } /* Header lines */ fprintf(FP, "From: %s\n", from); fprintf(FP, "To: %s\n", to); fprintf(FP, "Subject: %s\n", subject); fprintf(FP, "X-Loop: %s\n", loop); fprintf(FP, "Precedence: bulk\n\n"); if (reply_body) fputs(reply_body, FP); else { /* default Body lines */ fprintf(FP, "Thank you for contacting the Technology Support Center. This\n"); fprintf(FP, "is an automated notification to acknowledge receipt of your\n"); fprintf(FP, "email request.\n\n"); fprintf(FP, "A Technology Support Specialist will be contacting you soon.\n\n"); fprintf(FP, "The Technology Support Center is staffed from 7 am to 10 pm,\n"); fprintf(FP, "Monday through Friday. You may also contact us during these\n"); fprintf(FP, "times at (614) 688-HELP.\n"); fprintf(FP, "\n\nOSU TSC\n"); } if (fclose(FP)) { fprintf(stderr, "pipe stream fclose() failed.\n"); goto out_deliver_mail; } FP = (FILE*)NULL; pipefd[1] = -1; /* no zombies */ if (waitpid(pid, &statloc, 0) == -1) fprintf(stderr, "waitpid(): %s\n", strerror(errno)); /* expect exit status 0 */ if (statloc) fprintf(stderr, "sendmail child exited with status %d\n", statloc); /* could of got a sigpipe too */ if (got_sigpipe) fprintf(stderr, "got SIGPIPE while sending mail\n"); } /* parent */ else { /* child */ if (close (pipefd[1])) { fprintf(stderr, "child: pipe write close(): %s\n", strerror(errno)); exit (1); } /* pipe becomes stdin */ if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) { fprintf(stderr, "child: dup2() to stdin failed\n"); exit (1); } if (close (pipefd[0])) { fprintf(stderr, "child: pipe read close(): %s\n", strerror(errno)); exit (1); } pipefd[0] = -1; /* invoke sendmail */ /* -t get recipient from body */ /* -o em set error mode to mail to sender */ if (execle(PATH_SENDMAIL, "sendmail", "-t", "-oem", (char*)0L, my_env) == -1) { fprintf(stderr, "child: execl() sendmail: %s\n", strerror(errno)); exit (1); } } /* child */ err = 0; /* good */ out_deliver_mail: if (FP) err |= fclose(FP); if (pipefd[0] != -1) err |= close(pipefd[0]); if (pipefd[1] != -1) err |= close(pipefd[1]); return err; }