+ /*
+ ** Now, go back to the beginning of the draft file and look for
+ ** header fields that specify attachments. Add a mhbuild MIME
+ ** composition file for each.
+ */
+ rewind(draft_file);
+ for (compnum = 1, state = FLD2; state == FLD2;) {
+ switch (state = m_getfld2(state, &f, draft_file)) {
+ case LENERR2:
+ state = FLD2;
+ case FLD2:
+ compnum++;
+ if (strncasecmp(f.name, attach_hdr, length) == 0 &&
+ f.namelen == length) {
+ p = trim(f.value);
+ if (*p == '+') {
+ /* forwarded message */
+ fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
+ } else {
+ /* regular attachment */
+ make_mime_composition_file_entry(p);
+ }
+ }
+ break;
+ case BODY2:
+ case FILEEOF2:
+ break;
+ case FMTERR2:
+ adios(EX_DATAERR, NULL, "message format error in component #%d", compnum);
+ case IOERR2:
+ adios(EX_IOERR, NULL, "error reading draft file: %s", draft_file_name);
+ default:
+ adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
+ }
+ }
+ fclose(composition_file);
+
+ /* We're ready to roll! */
+ if (execprogl("mhbuild", "mhbuild", composition_file_name,
+ (char *)NULL) != 0) {
+ /* some problem */
+ clean_up_temporary_files();
+ return (NOTOK);
+ }
+ /* Remove the automatically created backup of mhbuild. */
+ snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
+ if (unlink(buf) == -1) {
+ advise(NULL, "unable to remove original composition file.");
+ }
+
+ return (OK);
+}
+
+static int
+signandenc(char *draft_file_name)
+{
+ enum state state;
+ int compnum;
+ struct field f = {{0}};
+ char buf[BUFSIZ];
+ int dosign = 0;
+ int doenc = 0;
+ int ret;
+
+ if(distfile) {
+ return DONE;
+ }
+
+ if (!(draft_file = fopen(draft_file_name, "r"))) {
+ adios(EX_IOERR, NULL, "can't open draft file `%s'.", draft_file_name);
+ }
+
+ /* Scan the draft file for an attachment header field name. */
+ for (compnum = 1, state = FLD2; state == FLD2;) {
+ switch (state = m_getfld2(state, &f, draft_file)) {
+ case LENERR2:
+ state = FLD2;
+ case FLD2:
+ compnum++;
+ if (strcasecmp(f.name, sign_hdr)==0) {
+ dosign = 1;
+ }
+ if (strcasecmp(f.name, enc_hdr)==0) {
+ doenc = 1;
+ }
+ break;
+ case BODY2:
+ case FILEEOF2:
+ break;
+ case FMTERR2:
+ adios(EX_DATAERR, NULL, "message format error in component #%d", compnum);
+ case IOERR2:
+ adios(EX_IOERR, NULL, "error reading draft file: %s", draft_file_name);
+ default:
+ adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
+ }
+ }
+
+ if (!dosign && !doenc) {
+ return DONE;
+ }
+
+ strcpy(composition_file_name, draft_file_name);
+
+ /* We're ready to roll! */
+ if (doenc) {
+ ret = execprogl("mhsign", "mhsign", "-m", "-e",
+ draft_file_name, (char *)NULL);
+ } else {
+ ret = execprogl("mhsign", "mhsign", "-m",
+ draft_file_name, (char *)NULL);
+ }
+ if (ret != 0) {
+ /* some problem */
+ return (NOTOK);
+ }
+ /* Remove the automatically created backup of mhsign. */
+ snprintf(buf, sizeof buf, "%s.orig", draft_file_name);
+ if (unlink(buf) == -1) {
+ advise(NULL, "unable to remove original draft file.");
+ }
+
+ return (OK);
+}
+
+static void
+clean_up_temporary_files(void)
+{
+ unlink(body_file_name);
+ unlink(composition_file_name);
+
+ return;
+}
+
+static void
+make_mime_composition_file_entry(char *file_name)
+{
+ FILE *fp;
+ struct node *np;
+ char *cp;
+ char content_type[BUFSIZ];
+ char cmdbuf[BUFSIZ];
+ char *cmd = mimetypequeryproc;
+ int semicolon = 0;
+
+ for (np = m_defs; np; np = np->n_next) {
+ if (strcasecmp(np->n_name, mimetypequery)==0) {
+ cmd = np->n_field;
+ break;
+ }
+ }
+ snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
+
+ if (!(fp = popen(cmdbuf, "r"))) {
+ clean_up_temporary_files();
+ adios(EX_IOERR, NULL, "unable to determine content type with `%s'",
+ cmdbuf);
+ }
+ if (fgets(content_type, sizeof content_type, fp) &&
+ (cp = strrchr(content_type, '\n'))) {
+ *cp = '\0';
+ } else {
+ strcpy(content_type, "application/octet-stream");
+ admonish(NULL, "problems with `%s', using fall back type `%s'",
+ cmdbuf, content_type);
+ }
+ pclose(fp);
+
+ /* TODO: don't use access(2) because it checks for ruid, not euid */
+ if (access(file_name, R_OK) != 0) {
+ clean_up_temporary_files();
+ adios(EX_IOERR, NULL, "unable to access file `%s'", file_name);
+ }
+
+ /* Check for broken file(1). See man page mh-profile(5). */
+ for (cp=content_type; *cp; cp++) {
+ if (isspace(*cp)) {
+ if (!semicolon) {
+ adios(EX_SOFTWARE, NULL, "Sorry, your Mime-Type-Query command (%s) is broken.\n\tThe output misses a semicolon before the whitespace.\n\tOutput was: %s", cmd, content_type);
+ }
+ } else if (*cp == ';') {
+ semicolon = 1;
+ } else {
+ semicolon = 0;
+ }
+ }
+
+ cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
+ fprintf(composition_file,
+ "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
+ content_type, cp, cp, file_name);
+
+ return;
+}
+
+/*
+** The back-end of the message sending back-end.
+** Annotate original message, and call `spost' to send message.
+*/
+static int
+sendaux(char **vec, int vecp, char *drft, struct stat *st)
+{
+ pid_t child_id;
+ int status;
+ char backup[BUFSIZ];
+
+ vec[vecp++] = drft;
+ if (distfile && distout(drft, distfile, backup) == NOTOK) {
+ return DONE;
+ }
+ vec[vecp] = NULL;
+
+ switch (child_id = fork()) {
+ case -1:
+ /* oops -- fork error */
+ advise("fork", "unable to");
+ return DONE;
+
+ case 0:
+ /* child process -- send it */
+ execvp(*vec, vec);
+ fprintf(stderr, "unable to exec ");
+ perror(*vec);
+ _exit(EX_OSERR);
+ break; /* NOT REACHED */
+
+ default:
+ /* parent process -- wait for it */
+ status = pidwait(child_id, NOTOK);
+ if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
+ if (annotext) {
+ anno(st);
+ }
+ } else {
+ /* spost failed */
+ advise(NULL, "%s", strexit(status));
+ if (distfile) {
+ unlink(drft);
+ if (rename(backup, drft) == NOTOK) {
+ advise(drft, "unable to rename %s to",
+ backup);
+ }
+ }
+ }
+ }
+
+ return status ? NOTOK : status;
+}
+
+
+static void
+anno(struct stat *st)
+{
+ struct stat st2;
+ char *msgs, *folder;
+ char buf[BUFSIZ];
+ char *vec[MAXARGS];
+ int vecp = 0;
+ char *cp, *dp;
+
+ if (altmsg && (stat(altmsg, &st2) == NOTOK ||
+ st->st_mtime != st2.st_mtime ||
+ st->st_dev != st2.st_dev ||
+ st->st_ino != st2.st_ino)) {
+ if (debugsw) {
+ admonish(NULL, "$mhaltmsg mismatch");
+ }
+ return;
+ }
+
+ if (!(folder = getenv("mhfolder")) || !*folder) {
+ if (debugsw) {
+ admonish(NULL, "$mhfolder not set");
+ }
+ return;
+ }
+ if (!(msgs = getenv("mhmessages")) || !*msgs) {
+ if (debugsw) {
+ admonish(NULL, "$mhmessages not set");
+ }
+ return;
+ }
+ if (debugsw) {
+ advise(NULL, "annotate as `%s': %s %s", annotext,
+ folder, msgs);
+ }
+ vec[vecp++] = "anno";
+ vec[vecp++] = "-comp";
+ vec[vecp++] = annotext;
+ snprintf(buf, sizeof buf, "+%s", folder);
+ vec[vecp++] = buf;
+
+ while (isspace(*msgs)) {
+ msgs++;
+ }
+ for (cp=dp=msgs; *cp; cp++) {
+ if (isspace(*cp)) {
+ while (isspace(*cp)) {
+ *cp++ = '\0';
+ }
+ vec[vecp++] = dp;
+ dp = cp;
+ }
+ }
+ vec[vecp++] = dp;
+ vec[vecp] = NULL;
+ if (execprog(*vec, vec) != 0) {
+ advise(NULL, "unable to annotate");
+ }
+}
+
+
+char*
+strexit(int status)
+{
+ if (WIFSIGNALED(status)) {
+ return "spost or sendmail killed by signal";
+ }
+ if (!WIFEXITED(status)) {
+ return "message not delivered to anyone";
+ }
+ switch (WEXITSTATUS(status)) {
+ case EX_TEMPFAIL:
+ return "Temporary error, maybe the MTA has queued the message";
+ default:
+ return "message not delivered to anyone";
+ }