- if (ce->ce_file) {
- if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading");
- return NOTOK;
- }
- goto ready_already;
- }
-
- if (find_cache (ct, rcachesw, (int *) 0, cb->c_id,
- cachefile, sizeof(cachefile)) != NOTOK) {
- if ((ce->ce_fp = fopen (cachefile, "r"))) {
- ce->ce_file = getcpy (cachefile);
- ce->ce_unlink = 0;
- goto ready_already;
- } else {
- admonish (cachefile, "unable to fopen for reading");
- }
- }
-
- return OK;
-
-ready_already:
- *file = ce->ce_file;
- *fd = fileno (ce->ce_fp);
- return DONE;
-}
-
-/*
- * File
- */
-
-static int
-InitFile (CT ct)
-{
- return init_encoding (ct, openFile);
-}
-
-
-static int
-openFile (CT ct, char **file)
-{
- int fd, cachetype;
- char cachefile[BUFSIZ];
- struct exbody *e = ct->c_ctexbody;
- CE ce = ct->c_cefile;
-
- switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
- case NOTOK:
- return NOTOK;
-
- case OK:
- break;
-
- case DONE:
- return fd;
- }
-
- if (!e->eb_name) {
- content_error (NULL, ct, "missing name parameter");
- return NOTOK;
- }
-
- ce->ce_file = getcpy (e->eb_name);
- ce->ce_unlink = 0;
-
- if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading");
- return NOTOK;
- }
-
- if ((!e->eb_permission || mh_strcasecmp (e->eb_permission, "read-write"))
- && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
- cachefile, sizeof(cachefile)) != NOTOK) {
- int mask;
- FILE *fp;
-
- mask = umask (cachetype ? ~m_gmprot () : 0222);
- if ((fp = fopen (cachefile, "w"))) {
- int cc;
- char buffer[BUFSIZ];
- FILE *gp = ce->ce_fp;
-
- fseek (gp, 0L, SEEK_SET);
-
- while ((cc = fread (buffer, sizeof(*buffer), sizeof(buffer), gp)) > 0)
- fwrite (buffer, sizeof(*buffer), cc, fp);
- fflush (fp);
-
- if (ferror (gp)) {
- admonish (ce->ce_file, "error reading");
- unlink (cachefile);
- } else if (ferror (fp)) {
- admonish (cachefile, "error writing");
- unlink (cachefile);
- }
- fclose (fp);
- }
- umask (mask);
- }
-
- fseek (ce->ce_fp, 0L, SEEK_SET);
- *file = ce->ce_file;
- return fileno (ce->ce_fp);
-}
-
-/*
- * FTP
- */
-
-static int
-InitFTP (CT ct)
-{
- return init_encoding (ct, openFTP);
-}
-
-
-static int
-openFTP (CT ct, char **file)
-{
- int cachetype, caching, fd;
- int len, buflen;
- char *bp, *ftp, *user, *pass;
- char buffer[BUFSIZ], cachefile[BUFSIZ];
- struct exbody *e;
- CE ce;
- static char *username = NULL;
- static char *password = NULL;
-
- e = ct->c_ctexbody;
- ce = ct->c_cefile;
-
- if ((ftp = context_find (nmhaccessftp)) && !*ftp)
- ftp = NULL;
-
-#ifndef BUILTIN_FTP
- if (!ftp)
- return NOTOK;
-#endif
-
- switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
- case NOTOK:
- return NOTOK;
-
- case OK:
- break;
-
- case DONE:
- return fd;
- }
-
- if (!e->eb_name || !e->eb_site) {
- content_error (NULL, ct, "missing %s parameter",
- e->eb_name ? "site": "name");
- return NOTOK;
- }
-
- if (xpid) {
- if (xpid < 0)
- xpid = -xpid;
- pidcheck (pidwait (xpid, NOTOK));
- xpid = 0;
- }
-
- /* Get the buffer ready to go */
- bp = buffer;
- buflen = sizeof(buffer);
-
- /*
- * Construct the query message for user
- */
- snprintf (bp, buflen, "Retrieve %s", e->eb_name);
- len = strlen (bp);
- bp += len;
- buflen -= len;
-
- if (e->eb_partno) {
- snprintf (bp, buflen, " (content %s)", e->eb_partno);
- len = strlen (bp);
- bp += len;
- buflen -= len;
- }
-
- snprintf (bp, buflen, "\n using %sFTP from site %s",
- e->eb_flags ? "anonymous " : "", e->eb_site);
- len = strlen (bp);
- bp += len;
- buflen -= len;
-
- if (e->eb_size > 0) {
- snprintf (bp, buflen, " (%lu octets)", e->eb_size);
- len = strlen (bp);
- bp += len;
- buflen -= len;
- }
- snprintf (bp, buflen, "? ");
-
- /*
- * Now, check the answer
- */
- if (!getanswer (buffer))
- return NOTOK;
-
- if (e->eb_flags) {
- user = "anonymous";
- snprintf (buffer, sizeof(buffer), "%s@%s", getusername (), LocalName ());
- pass = buffer;
- } else {
- ruserpass (e->eb_site, &username, &password);
- user = username;
- pass = password;
- }
-
- ce->ce_unlink = (*file == NULL);
- caching = 0;
- cachefile[0] = '\0';
- if ((!e->eb_permission || mh_strcasecmp (e->eb_permission, "read-write"))
- && find_cache (NULL, wcachesw, &cachetype, e->eb_content->c_id,
- cachefile, sizeof(cachefile)) != NOTOK) {
- if (*file == NULL) {
- ce->ce_unlink = 0;
- caching = 1;
- }
- }
-
- if (*file)
- ce->ce_file = add (*file, NULL);
- else if (caching)
- ce->ce_file = add (cachefile, NULL);
- else
- ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
-
- if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
- return NOTOK;
- }
-
-#ifdef BUILTIN_FTP
- if (ftp)
-#endif
- {
- int child_id, i, vecp;
- char *vec[9];
-
- vecp = 0;
- vec[vecp++] = r1bindex (ftp, '/');
- vec[vecp++] = e->eb_site;
- vec[vecp++] = user;
- vec[vecp++] = pass;
- vec[vecp++] = e->eb_dir;
- vec[vecp++] = e->eb_name;
- vec[vecp++] = ce->ce_file,
- vec[vecp++] = e->eb_mode && !mh_strcasecmp (e->eb_mode, "ascii")
- ? "ascii" : "binary";
- vec[vecp] = NULL;
-
- fflush (stdout);
-
- for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
- sleep (5);
- switch (child_id) {
- case NOTOK:
- adios ("fork", "unable to");
- /* NOTREACHED */
-
- case OK:
- close (fileno (ce->ce_fp));
- execvp (ftp, vec);
- fprintf (stderr, "unable to exec ");
- perror (ftp);
- _exit (-1);
- /* NOTREACHED */
-
- default:
- if (pidXwait (child_id, NULL)) {
-#ifdef BUILTIN_FTP
-losing_ftp:
-#endif
- username = password = NULL;
- ce->ce_unlink = 1;
- return NOTOK;
- }
- break;
- }
- }
-#ifdef BUILTIN_FTP
- else if (ftp_get (e->eb_site, user, pass, e->eb_dir, e->eb_name,
- ce->ce_file, e->eb_mode && !mh_strcasecmp (e->eb_mode, "ascii"), 0)
- == NOTOK)
- goto losing_ftp;
-#endif
-
- if (cachefile[0]) {
- if (caching)
- chmod (cachefile, cachetype ? m_gmprot () : 0444);
- else {
- int mask;
- FILE *fp;
-
- mask = umask (cachetype ? ~m_gmprot () : 0222);
- if ((fp = fopen (cachefile, "w"))) {
- int cc;
- FILE *gp = ce->ce_fp;
-
- fseek (gp, 0L, SEEK_SET);
-
- while ((cc= fread (buffer, sizeof(*buffer), sizeof(buffer), gp)) > 0)
- fwrite (buffer, sizeof(*buffer), cc, fp);
- fflush (fp);
-
- if (ferror (gp)) {
- admonish (ce->ce_file, "error reading");
- unlink (cachefile);
- } else if (ferror (fp)) {
- admonish (cachefile, "error writing");
- unlink (cachefile);
- }
- fclose (fp);
- }
- umask (mask);
- }
- }
-
- fseek (ce->ce_fp, 0L, SEEK_SET);
- *file = ce->ce_file;
- return fileno (ce->ce_fp);
-}
-
-
-/*
- * Mail
- */
-
-static int
-InitMail (CT ct)
-{
- return init_encoding (ct, openMail);
-}
-
-
-static int
-openMail (CT ct, char **file)
-{
- int child_id, fd, i, vecp;
- int len, buflen;
- char *bp, buffer[BUFSIZ], *vec[7];
- struct exbody *e = ct->c_ctexbody;
- CE ce = ct->c_cefile;
-
- switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) {
- case NOTOK:
- return NOTOK;
-
- case OK:
- break;
-
- case DONE:
- return fd;
- }
-
- if (!e->eb_server) {
- content_error (NULL, ct, "missing server parameter");
- return NOTOK;
- }
-
- if (xpid) {
- if (xpid < 0)
- xpid = -xpid;
- pidcheck (pidwait (xpid, NOTOK));
- xpid = 0;
- }
-
- /* Get buffer ready to go */
- bp = buffer;
- buflen = sizeof(buffer);
-
- /* Now, construct query message */
- snprintf (bp, buflen, "Retrieve content");
- len = strlen (bp);
- bp += len;
- buflen -= len;
-
- if (e->eb_partno) {
- snprintf (bp, buflen, " %s", e->eb_partno);
- len = strlen (bp);
- bp += len;
- buflen -= len;
- }
-
- snprintf (bp, buflen, " by asking %s\n\n%s\n? ", e->eb_server,
- e->eb_subject ? e->eb_subject : e->eb_body);
-
- /* Now, check answer */
- if (!getanswer (buffer))
- return NOTOK;
-
- vecp = 0;
- vec[vecp++] = r1bindex (mailproc, '/');
- vec[vecp++] = e->eb_server;
- vec[vecp++] = "-subject";
- vec[vecp++] = e->eb_subject ? e->eb_subject : "mail-server request";
- vec[vecp++] = "-body";
- vec[vecp++] = e->eb_body;
- vec[vecp] = NULL;
-
- for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
- sleep (5);
- switch (child_id) {
- case NOTOK:
- advise ("fork", "unable to");
- return NOTOK;
-
- case OK:
- execvp (mailproc, vec);
- fprintf (stderr, "unable to exec ");
- perror (mailproc);
- _exit (-1);
- /* NOTREACHED */
-
- default:
- if (pidXwait (child_id, NULL) == OK)
- advise (NULL, "request sent");
- break;
- }
-
- if (*file == NULL) {
- ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL);
- ce->ce_unlink = 1;
- } else {
- ce->ce_file = add (*file, NULL);
- ce->ce_unlink = 0;
- }
-
- if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
- content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
- return NOTOK;
- }
-
- /* showproc is for mhshow and mhstore, though mhlist -debug
- * prints it, too. */
- if (ct->c_showproc)
- free (ct->c_showproc);
- ct->c_showproc = add ("true", NULL);
-
- fseek (ce->ce_fp, 0L, SEEK_SET);
- *file = ce->ce_file;
- return fileno (ce->ce_fp);
-}
-
-
-static int
-readDigest (CT ct, char *cp)
-{
- int bitno, skip;
- unsigned long bits;
- char *bp = cp;
- unsigned char *dp, value, *ep;
- unsigned char *b, *b1, *b2, *b3;
-
- b = (unsigned char *) &bits,
- b1 = &b[endian > 0 ? 1 : 2],
- b2 = &b[endian > 0 ? 2 : 1],
- b3 = &b[endian > 0 ? 3 : 0];
- bitno = 18;
- bits = 0L;
- skip = 0;
-
- for (ep = (dp = ct->c_digest)
- + sizeof(ct->c_digest) / sizeof(ct->c_digest[0]); *cp; cp++)
- switch (*cp) {
- default:
- if (skip || (*cp & 0x80)
- || (value = b642nib[*cp & 0x7f]) > 0x3f) {
- if (debugsw)
- fprintf (stderr, "invalid BASE64 encoding\n");
- return NOTOK;
- }
-
- bits |= value << bitno;
-test_end:
- if ((bitno -= 6) < 0) {
- if (dp + (3 - skip) > ep)
- goto invalid_digest;
- *dp++ = *b1;
- if (skip < 2) {
- *dp++ = *b2;
- if (skip < 1)
- *dp++ = *b3;
- }
- bitno = 18;
- bits = 0L;
- skip = 0;
- }
- break;
-
- case '=':
- if (++skip > 3)
- goto self_delimiting;
- goto test_end;
- }
- if (bitno != 18) {
- if (debugsw)
- fprintf (stderr, "premature ending (bitno %d)\n", bitno);
-
- return NOTOK;
- }
-self_delimiting:
- if (dp != ep) {
-invalid_digest:
- if (debugsw) {
- while (*cp)
- cp++;
- fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
- (int)(cp - bp));
- }
-
- return NOTOK;
- }
-
- if (debugsw) {
- fprintf (stderr, "MD5 digest=");
- for (dp = ct->c_digest; dp < ep; dp++)
- fprintf (stderr, "%02x", *dp & 0xff);
- fprintf (stderr, "\n");