From 4a6be646c86e2125390a4043095bc74f33d7620f Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 27 May 1997 20:34:09 +0000 Subject: Nailed. svn path=/trunk/; revision=1031 --- Makefile.in | 5 ++- NEWS | 2 + base64.c | 131 +++++++++++++++++++++++++++++++----------------------------- fetchmail.h | 3 ++ imap.c | 15 ++----- 5 files changed, 80 insertions(+), 76 deletions(-) diff --git a/Makefile.in b/Makefile.in index c1cf04e0..af9579be 100644 --- a/Makefile.in +++ b/Makefile.in @@ -78,7 +78,8 @@ CTAGS = ctags -tw protobjs = rcfile_y.o rcfile_l.o socket.o getpass.o pop2.o pop3.o imap.o \ etrn.o fetchmail.o options.o daemon.o smtp.o driver.o rfc822.o \ - xmalloc.o uid.o mxget.o md5c.o md5ify.o interface.o netrc.o error.o + xmalloc.o uid.o mxget.o md5c.o md5ify.o interface.o netrc.o base64.c \ + error.o objs = $(protobjs) $(extras) $(EXTRAOBJ) @@ -88,7 +89,7 @@ srcs = $(srcdir)/socket.c $(srcdir)/getpass.c $(srcdir)/pop2.c \ $(srcdir)/driver.c $(srcdir)/rfc822.c $(srcdir)/smtp.c \ $(srcdir)/xmalloc.c $(srcdir)/uid.c $(srcdir)/mxget.c \ $(srcdir)/md5c.c $(srcdir)/md5ify.c $(srcdir)/interface.c \ - $(srcdir)/netrc.c $(srcdir)/error.c + $(srcdir)/netrc.c $(srcdir)/base64.c $(srcdir)/error.c .SUFFIXES: .SUFFIXES: .o .c .h .y .l .ps .dvi .info .texi diff --git a/NEWS b/NEWS index fb090132..a43e32e7 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,8 @@ every user entry in a multi-user poll declaration. * Use the libmd functions for md5 under Free BSD? (Low priority.) +* Make the IMAP-K4 support 64-bit-safe. + Release Notes: ------------------------------------------------------------------------------ diff --git a/base64.c b/base64.c index 918af9d5..490cd7f3 100644 --- a/base64.c +++ b/base64.c @@ -1,92 +1,97 @@ -/* from imtest.c -- IMAP/IMSP test client - * Cyrus IMAPd 1.5.2 +/* + * base64.c -- base-64 conversion routines. * - * Copyright 1996, Carnegie Mellon University. All Rights Reserved. - * - * This software is made available for academic and research - * purposes only. No commercial license is hereby granted. - * Copying and other reproduction is authorized only for research, - * education, and other non-commercial purposes. No warranties, - * either expressed or implied, are made regarding the operation, - * use, or results of the software. Such a release does not permit - * use of the code for commercial purposes or benefits by anyone - * without specific, additional permission by the owner of the code. + * For license terms, see the file COPYING in this directory. * - * Author: Chris Newman - * Start Date: 2/16/93 + * This base 64 encoding is defined in RFC2045 section 6.8, + * "Base64 Content-Transfer-Encoding", but lines must not be broken in the + * scheme used here. */ +#include -/* base64 tables - */ -static char basis_64[] = +static const char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -static char index_64[128] = { - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, - 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, - 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, - -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 + +#define BAD -1 +static const char base64val[] = { + BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, + BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, + BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD, + BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD, + BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD }; -#define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)]) +#define DECODE64(c) (isascii(c) ? base64val[c] : BAD) -void to64(out, in, inlen) - unsigned char *out, *in; - int inlen; +void to64frombits(unsigned char *out, const unsigned char *in, int inlen) +/* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */ { - unsigned char oval; - - while (inlen >= 3) { - *out++ = basis_64[in[0] >> 2]; - *out++ = basis_64[((in[0] << 4) & 0x30) | (in[1] >> 4)]; - *out++ = basis_64[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; - *out++ = basis_64[in[2] & 0x3f]; + for (; inlen >= 3; inlen -= 3) + { + *out++ = base64digits[in[0] >> 2]; + *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)]; + *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; + *out++ = base64digits[in[2] & 0x3f]; in += 3; - inlen -= 3; } - if (inlen > 0) { - *out++ = basis_64[in[0] >> 2]; - oval = (in[0] << 4) & 0x30; - if (inlen > 1) oval |= in[1] >> 4; - *out++ = basis_64[oval]; - *out++ = (inlen < 2) ? '=' : basis_64[(in[1] << 2) & 0x3c]; + if (inlen > 0) + { + unsigned char fragment; + + *out++ = base64digits[in[0] >> 2]; + fragment = (in[0] << 4) & 0x30; + if (inlen > 1) + fragment |= in[1] >> 4; + *out++ = base64digits[fragment]; + *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c]; *out++ = '='; } *out = '\0'; } -int from64(out, in) - char *out, *in; +int from64tobits(char *out, const char *in) +/* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */ { int len = 0; - int c1, c2, c3, c4; + register char digit1, digit2, digit3, digit4; + + if (in[0] == '+' && in[1] == ' ') + in += 2; + if (*in == '\r') + return(0); - if (in[0] == '+' && in[1] == ' ') in += 2; - if (*in == '\r') return (0); do { - c1 = in[0]; - if (CHAR64(c1) == -1) return (-1); - c2 = in[1]; - if (CHAR64(c2) == -1) return (-1); - c3 = in[2]; - if (c3 != '=' && CHAR64(c3) == -1) return (-1); - c4 = in[3]; - if (c4 != '=' && CHAR64(c4) == -1) return (-1); + digit1 = in[0]; + if (DECODE64(digit1) == BAD) + return(-1); + digit2 = in[1]; + if (DECODE64(digit2) == BAD) + return(-1); + digit3 = in[2]; + if (digit3 != '=' && DECODE64(digit3) == BAD) + return(-1); + digit4 = in[3]; + if (digit4 != '=' && DECODE64(digit4) == BAD) + return(-1); in += 4; - *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4); + *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4); ++len; - if (c3 != '=') { - *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2); + if (digit3 != '=') + { + *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2); ++len; - if (c4 != '=') { - *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4); + if (digit4 != '=') + { + *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4); ++len; } } - } while (*in != '\r' && c4 != '='); + } while + (*in != '\r' && digit4 != '='); return (len); } +/* base64.c ends here */ diff --git a/fetchmail.h b/fetchmail.h index d7bc9f02..53f4ffa8 100644 --- a/fetchmail.h +++ b/fetchmail.h @@ -247,6 +247,9 @@ void escapes(const char *, char *); void yyerror(const char *); int yylex(void); +void to64frombits(unsigned char *, const unsigned char *, int); +int from64tobits(char *, const char *); + #if defined(HAVE_VOIDPOINTER) #define XMALLOCTYPE void #else diff --git a/imap.c b/imap.c index 36bdaf06..733812c9 100644 --- a/imap.c +++ b/imap.c @@ -18,7 +18,6 @@ #ifdef KERBEROS_V4 #include -#include "base64.h" #endif /* KERBEROS_V4 */ extern char *strstr(); /* needed on sysV68 R3V7.1. */ @@ -86,12 +85,6 @@ static int do_rfc1731(int sock, struct query *ctl, char *buf) /* authenticate as per RFC1731 * note 32-bit integer requirement here... * sizeof int must be 4! - * - * Note: Base64 conversion routines come from Cyrus IMAPd and have - * possibly too-restrictive redistribution requirements. See base64.c - * for details. Base64 is defined in RFC2045 section 6.8, "Base64 - * Content-Transfer-Encoding", but lines must not be broken in the - * scheme used here. */ { int result = 0, len; @@ -128,7 +121,7 @@ static int do_rfc1731(int sock, struct query *ctl, char *buf) return result; } - len = from64(challenge1.cstr, buf1); + len = from64tobits(challenge1.cstr, buf1); if (len < 0) { error(0, -1, "could not decode initial BASE64 challenge"); return PS_AUTHFAIL; @@ -204,7 +197,7 @@ static int do_rfc1731(int sock, struct query *ctl, char *buf) return PS_AUTHFAIL; } - to64(buf1, authenticator.dat, authenticator.length); + to64frombits(buf1, authenticator.dat, authenticator.length); if (outlevel == O_VERBOSE) { error(0, 0, "IMAP> %s", buf1); } @@ -250,7 +243,7 @@ static int do_rfc1731(int sock, struct query *ctl, char *buf) * process is complete. */ - len = from64(buf2, buf1); + len = from64tobits(buf2, buf1); if (len < 0) { error(0, -1, "could not decode BASE64 ready response"); return PS_AUTHFAIL; @@ -287,7 +280,7 @@ static int do_rfc1731(int sock, struct query *ctl, char *buf) (des_cblock *)authenticator.dat, authenticator.length, schedule, &session, 1); - to64(buf1, authenticator.dat, authenticator.length); + to64frombits(buf1, authenticator.dat, authenticator.length); if (outlevel == O_VERBOSE) { error(0, 0, "IMAP> %s", buf1); } -- cgit v1.2.3