sfsasl.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1999-2004 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <sendmail.h>
#include <errno.h>
/* allow to disable error handling code just in case... */
#ifndef DEAL_WITH_ERROR_SSL
# define DEAL_WITH_ERROR_SSL 1
#endif /* ! DEAL_WITH_ERROR_SSL */
#if SASL
# include "sfsasl.h"
/* Structure used by the "sasl" file type */
struct sasl_obj
{
};
struct sasl_info
{
};
/*
** SASL_GETINFO - returns requested information about a "sasl" file
** descriptor.
**
** Parameters:
** fp -- the file descriptor
** what -- the type of information requested
** valp -- the thang to return the information in
**
** Returns:
** -1 for unknown requests
** >=0 on success with valp filled in (if possible).
*/
static int
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_FD:
return -1;
case SM_IO_IS_READABLE:
return 0;
/* get info from underlying file */
default:
return -1;
}
}
/*
** SASL_OPEN -- creates the sasl specific information for opening a
** file of the sasl type.
**
** Parameters:
** fp -- the file pointer associated with the new open
** info -- contains the sasl connection information pointer and
** the original SM_FILE_T that holds the open
** flags -- ignored
** rpool -- ignored
**
** Returns:
** 0 on success
*/
/* ARGSUSED2 */
static int
const void *info;
int flags;
const void *rpool;
{
{
return -1;
}
/*
** The underlying 'fp' is set to SM_IO_NOW so that the entire
** encoded string is written in one chunk. Otherwise there is
** the possibility that it may appear illegal, bogus or
** mangled to the other side of the connection.
** We will read or write through 'fp' since it is the opaque
** connection for the communications. We need to treat it this
** way in case the encoded string is to be sent down a TLS
** connection rather than, say, sm_io's stdio.
*/
return 0;
}
/*
** SASL_CLOSE -- close the sasl specific parts of the sasl file pointer
**
** Parameters:
** fp -- the file pointer to close
**
** Returns:
** 0 on success
*/
static int
{
return 0;
{
}
return 0;
}
/* how to deallocate a buffer allocated by SASL */
extern void sm_sasl_free __P((void *));
# define SASL_DEALLOC(b) sm_sasl_free(b)
/*
** SASL_READ -- read encrypted information and decrypt it for the caller
**
** Parameters:
** fp -- the file pointer
** buf -- the location to place the decrypted information
** size -- the number of bytes to read after decryption
**
** Results:
** -1 on error
** otherwise the number of bytes read
*/
static ssize_t
char *buf;
{
int result;
# if SASL >= 20000
# else /* SASL >= 20000 */
# endif /* SASL >= 20000 */
static unsigned int outlen = 0;
static unsigned int offset = 0;
/*
** sasl_decode() may require more data than a single read() returns.
** Hence we have to put a loop around the decoding.
** This also requires that we may have to split up the returned
** data since it might be larger than the allowed size.
** Therefore we use a static pointer and return portions of it
** if necessary.
** XXX Note: This function is not thread-safe nor can it be used
** on more than one file. A correct implementation would store
** this data in fp->f_cookie.
*/
# if SASL >= 20000
while (outlen == 0)
# else /* SASL >= 20000 */
# endif /* SASL >= 20000 */
{
if (len <= 0)
return len;
{
offset = 0;
outlen = 0;
return -1;
}
}
{
/* be paranoid: outbuf == NULL but outlen != 0 */
syserr("@sasl_read failure: outbuf == NULL but outlen != 0");
/* NOTREACHED */
}
{
/* return another part of the buffer */
}
else
{
/* return the rest of the buffer */
# if SASL < 20000
# endif /* SASL < 20000 */
offset = 0;
outlen = 0;
}
return len;
}
/*
** SASL_WRITE -- write information out after encrypting it
**
** Parameters:
** fp -- the file pointer
** buf -- holds the data to be encrypted and written
** size -- the number of bytes to have encrypted and written
**
** Returns:
** -1 on error
** otherwise number of bytes written
*/
static ssize_t
const char *buf;
{
int result;
# if SASL >= 20000
const char *outbuf;
# else /* SASL >= 20000 */
char *outbuf;
# endif /* SASL >= 20000 */
/*
** Fetch the maximum input buffer size for sasl_encode().
** This can be less than the size set in attemptauth()
** due to a negotation with the other side, e.g.,
** Cyrus IMAP lmtp program sets maxbuf=4096,
** digestmd5 substracts 25 and hence we'll get 4071
** instead of 8192 (MAXOUTLEN).
** Hack (for now): simply reduce the size, callers are (must be)
** able to deal with that and invoke sasl_write() again with
** the rest of the data.
** Note: it would be better to store this value in the context
** after the negotiation.
*/
(const void **) &maxencode);
return -1;
{
while (outlen > 0)
{
/* XXX result == 0? */
if (ret <= 0)
return ret;
}
# if SASL < 20000
# endif /* SASL < 20000 */
}
return size;
}
/*
** SFDCSASL -- create sasl file type and open in and out file pointers
** for sendmail to read from and write to.
**
** Parameters:
** fin -- the sm_io file encrypted data to be read from
** fout -- the sm_io file encrypted data to be writen to
** conn -- the sasl connection pointer
**
** Returns:
** -1 on error
** 0 on success
**
** Side effects:
** The arguments "fin" and "fout" are replaced with the new
** SM_FILE_T pointers.
*/
int
{
{
/* no need to do anything */
return 0;
}
return -1;
{
return -1;
}
return 0;
}
#endif /* SASL */
#if STARTTLS
# include "sfsasl.h"
/* Structure used by the "tls" file type */
struct tls_obj
{
};
struct tls_info
{
};
/*
** TLS_GETINFO - returns requested information about a "tls" file
** descriptor.
**
** Parameters:
** fp -- the file descriptor
** what -- the type of information requested
** valp -- the thang to return the information in (unused)
**
** Returns:
** -1 for unknown requests
** >=0 on success with valp filled in (if possible).
*/
/* ARGSUSED2 */
static int
int what;
void *valp;
{
switch (what)
{
case SM_IO_WHAT_FD:
return -1;
case SM_IO_IS_READABLE:
default:
return -1;
}
}
/*
** TLS_OPEN -- creates the tls specific information for opening a
** file of the tls type.
**
** Parameters:
** fp -- the file pointer associated with the new open
** info -- the sm_io file pointer holding the open and the
** TLS encryption connection to be read from or written to
** flags -- ignored
** rpool -- ignored
**
** Returns:
** 0 on success
*/
/* ARGSUSED2 */
static int
const void *info;
int flags;
const void *rpool;
{
{
return -1;
}
/*
** We try to get the "raw" file descriptor that TLS uses to
** over the file descriptor being a blocking or non-blocking type.
** Under the covers TLS handles the change and this allows us
** to do timeouts with sm_io.
*/
return 0;
}
/*
** TLS_CLOSE -- close the tls specific parts of the tls file pointer
**
** Parameters:
** fp -- the file pointer to close
**
** Returns:
** 0 on success
*/
static int
{
return 0;
{
}
return 0;
}
/* maximum number of retries for TLS related I/O due to handshakes */
# define MAX_TLS_IOS 4
/*
** TLS_READ -- read secured information for the caller
**
** Parameters:
** fp -- the file pointer
** buf -- the location to place the data
** size -- the number of bytes to read from connection
**
** Results:
** -1 on error
** otherwise the number of bytes read
*/
static ssize_t
char *buf;
{
int r;
static int again = MAX_TLS_IOS;
char *err;
if (r > 0)
{
again = MAX_TLS_IOS;
return r;
}
{
case SSL_ERROR_NONE:
case SSL_ERROR_ZERO_RETURN:
again = MAX_TLS_IOS;
break;
case SSL_ERROR_WANT_WRITE:
if (--again <= 0)
err = "read W BLOCK";
else
break;
case SSL_ERROR_WANT_READ:
if (--again <= 0)
err = "read R BLOCK";
else
break;
err = "write X BLOCK";
break;
case SSL_ERROR_SYSCALL:
if (r == 0 && errno == 0) /* out of protocol EOF found */
break;
err = "syscall error";
/*
get_last_socket_error());
*/
break;
case SSL_ERROR_SSL:
if (r == 0 && errno == 0) /* out of protocol EOF found */
break;
#endif /* DEAL_WITH_ERROR_SSL */
err = "generic SSL error";
if (LogLevel > 9)
tlslogerr("read");
/* avoid repeated calls? */
if (r == 0)
r = -1;
#endif /* DEAL_WITH_ERROR_SSL */
break;
}
{
int save_errno;
again = MAX_TLS_IOS;
if (LogLevel > 9)
"STARTTLS: read error=%s (%d), errno=%d, get_error=%s",
else if (LogLevel > 7)
"STARTTLS: read error=%s (%d)", err, r);
errno = save_errno;
}
return r;
}
/*
** TLS_WRITE -- write information out through secure connection
**
** Parameters:
** fp -- the file pointer
** buf -- holds the data to be securely written
** size -- the number of bytes to write
**
** Returns:
** -1 on error
** otherwise number of bytes written
*/
static ssize_t
const char *buf;
{
int r;
static int again = MAX_TLS_IOS;
char *err;
if (r > 0)
{
again = MAX_TLS_IOS;
return r;
}
{
case SSL_ERROR_NONE:
case SSL_ERROR_ZERO_RETURN:
again = MAX_TLS_IOS;
break;
case SSL_ERROR_WANT_WRITE:
if (--again <= 0)
err = "write W BLOCK";
else
break;
case SSL_ERROR_WANT_READ:
if (--again <= 0)
err = "write R BLOCK";
else
break;
err = "write X BLOCK";
break;
case SSL_ERROR_SYSCALL:
if (r == 0 && errno == 0) /* out of protocol EOF found */
break;
err = "syscall error";
/*
get_last_socket_error());
*/
break;
case SSL_ERROR_SSL:
err = "generic SSL error";
/*
ERR_GET_REASON(ERR_peek_error()));
*/
if (LogLevel > 9)
tlslogerr("write");
/* avoid repeated calls? */
if (r == 0)
r = -1;
#endif /* DEAL_WITH_ERROR_SSL */
break;
}
{
int save_errno;
again = MAX_TLS_IOS;
if (LogLevel > 9)
"STARTTLS: write error=%s (%d), errno=%d, get_error=%s",
else if (LogLevel > 7)
"STARTTLS: write error=%s (%d)", err, r);
errno = save_errno;
}
return r;
}
/*
** SFDCTLS -- create tls file type and open in and out file pointers
** for sendmail to read from and write to.
**
** Parameters:
** fin -- data input source being replaced
** fout -- data output source being replaced
** con -- the tls connection pointer
**
** Returns:
** -1 on error
** 0 on success
**
** Side effects:
** The arguments "fin" and "fout" are replaced with the new
** SM_FILE_T pointers.
** The original "fin" and "fout" are preserved in the tls file
** type but are not actually used because of the design of TLS.
*/
int
{
NULL);
return -1;
NULL);
{
return -1;
}
return 0;
}
#endif /* STARTTLS */