- return NOTOK;
-}
-
-
-/*
-** External
-*/
-
-static int
-openExternal(CT ct, CT cb, CE ce, char **file, int *fd)
-{
- char cachefile[BUFSIZ];
-
- if (ce->ce_fp) {
- fseek(ce->ce_fp, 0L, SEEK_SET);
- goto ready_already;
- }
-
- 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;