From 8bf4b0e50a8b3445fb3bec2ad63a1cf75af8dbd6 Mon Sep 17 00:00:00 2001 From: Bobbi Webber-Manners Date: Fri, 3 Jul 2020 04:12:12 -0400 Subject: [PATCH] Sanitize MIME filenames for ProDOS --- apps/email.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/email.c b/apps/email.c index d608433..d7da210 100644 --- a/apps/email.c +++ b/apps/email.c @@ -1,7 +1,7 @@ ///////////////////////////////////////////////////////////////// // emai//er - Simple Email User Agent vaguely inspired by Elm // Handles INBOX in the format created by POP65 -// Bobbi June 2020 +// Bobbi June, July 2020 ///////////////////////////////////////////////////////////////// // - TODO: Default to starting at end, not beginning (or option to sort backwards...) @@ -522,6 +522,25 @@ char prompt_okay_attachment(char *filename) { else return 0; } + +/* + * Sanitize filename for ProDOS + * Modifies s in place + */ +void sanitize_filename(char *s) { + uint8_t i = 0, j = 0; + char c; + while (1) { + c = s[i++]; + if (c == '\r') { + s[j] = '\0'; + return; + } + if (isalnum(c) || c == '.' || c == '/') + s[j++] = c; + } +} + /* * Display email with simple pager functionality * Includes support for decoding MIME headers @@ -604,12 +623,13 @@ restart: } else if (strstr(linebuf, "filename=")) { sprintf(filename, "%s/ATTACHMENTS/%s", cfg_emaildir, strstr(linebuf, "filename=") + 9); - filename[strlen(filename) - 1] = '\0'; // Remove '\r' + //filename[strlen(filename) - 1] = '\0'; // Remove '\r' + sanitize_filename(filename); if (prompt_okay_attachment(filename)) { printf("** Attachment -> %s ", filename); attachfp = fopen(filename, "wb"); if (!attachfp) - printf("** Can't open %s\n", filename); + printf("\n** Can't open %s ", filename); } else mime = 1; } else if ((mime == 3) && (!strncmp(linebuf, "\r", 1))) {