2000-08-12 21:55:49 +00:00
|
|
|
/*
|
2003-03-03 17:09:13 +00:00
|
|
|
* Copyright (c) 1999-2003 Sendmail, Inc. and its suppliers.
|
2000-08-12 21:55:49 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
#include <sm/gen.h>
|
2003-03-03 17:09:13 +00:00
|
|
|
SM_RCSID("@(#)$Id: comm.c,v 8.54.2.6 2003/01/03 22:14:40 ca Exp $")
|
2000-08-12 21:55:49 +00:00
|
|
|
|
|
|
|
#include "libmilter.h"
|
2002-02-17 21:56:45 +00:00
|
|
|
#include <sm/errstring.h>
|
2000-08-12 21:55:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** MI_RD_CMD -- read a command
|
|
|
|
**
|
|
|
|
** Parameters:
|
|
|
|
** sd -- socket descriptor
|
|
|
|
** timeout -- maximum time to wait
|
|
|
|
** cmd -- single character command read from sd
|
|
|
|
** rlen -- pointer to length of result
|
|
|
|
** name -- name of milter
|
|
|
|
**
|
|
|
|
** Returns:
|
|
|
|
** buffer with rest of command
|
|
|
|
** (malloc()ed here, should be free()d)
|
|
|
|
** hack: encode error in cmd
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
mi_rd_cmd(sd, timeout, cmd, rlen, name)
|
|
|
|
socket_t sd;
|
|
|
|
struct timeval *timeout;
|
|
|
|
char *cmd;
|
|
|
|
size_t *rlen;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
mi_int32 expl;
|
|
|
|
ssize_t i;
|
2003-03-03 17:09:13 +00:00
|
|
|
FD_RD_VAR(rds, excs);
|
2000-08-12 21:55:49 +00:00
|
|
|
int ret;
|
|
|
|
int save_errno;
|
|
|
|
char *buf;
|
|
|
|
char data[MILTER_LEN_BYTES + 1];
|
|
|
|
|
|
|
|
*cmd = '\0';
|
|
|
|
*rlen = 0;
|
2001-01-21 22:17:06 +00:00
|
|
|
|
2000-08-12 21:55:49 +00:00
|
|
|
i = 0;
|
2002-08-28 17:57:52 +00:00
|
|
|
for (;;)
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
2003-03-03 17:09:13 +00:00
|
|
|
FD_RD_INIT(sd, rds, excs);
|
|
|
|
ret = FD_RD_READY(sd, rds, excs, timeout);
|
2002-08-28 17:57:52 +00:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
2003-03-03 17:09:13 +00:00
|
|
|
if (FD_IS_RD_EXC(sd, rds, excs))
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
|
|
|
*cmd = SMFIC_SELECT;
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-17 21:56:45 +00:00
|
|
|
|
|
|
|
len = MI_SOCK_READ(sd, data + i, sizeof data - i);
|
|
|
|
if (MI_SOCK_READ_FAIL(len))
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
|
|
|
smi_log(SMI_LOG_ERR,
|
|
|
|
"%s, mi_rd_cmd: read returned %d: %s",
|
2003-02-08 20:31:29 +00:00
|
|
|
name, (int) len, sm_errstring(errno));
|
2000-08-12 21:55:49 +00:00
|
|
|
*cmd = SMFIC_RECVERR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_EOF;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (len >= (ssize_t) sizeof data - i)
|
|
|
|
break;
|
|
|
|
i += len;
|
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_TIMEOUT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
smi_log(SMI_LOG_ERR,
|
|
|
|
"%s: mi_rd_cmd: select returned %d: %s",
|
2002-02-17 21:56:45 +00:00
|
|
|
name, ret, sm_errstring(errno));
|
2000-08-12 21:55:49 +00:00
|
|
|
*cmd = SMFIC_RECVERR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cmd = data[MILTER_LEN_BYTES];
|
|
|
|
data[MILTER_LEN_BYTES] = '\0';
|
|
|
|
(void) memcpy((void *) &expl, (void *) &(data[0]), MILTER_LEN_BYTES);
|
|
|
|
expl = ntohl(expl) - 1;
|
|
|
|
if (expl <= 0)
|
|
|
|
return NULL;
|
|
|
|
if (expl > MILTER_CHUNK_SIZE)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_TOOBIG;
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-17 21:56:45 +00:00
|
|
|
#if _FFR_ADD_NULL
|
|
|
|
buf = malloc(expl + 1);
|
|
|
|
#else /* _FFR_ADD_NULL */
|
2000-08-12 21:55:49 +00:00
|
|
|
buf = malloc(expl);
|
2002-02-17 21:56:45 +00:00
|
|
|
#endif /* _FFR_ADD_NULL */
|
2000-08-12 21:55:49 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_MALLOC;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
2002-08-28 17:57:52 +00:00
|
|
|
for (;;)
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
2003-03-03 17:09:13 +00:00
|
|
|
FD_RD_INIT(sd, rds, excs);
|
|
|
|
ret = FD_RD_READY(sd, rds, excs, timeout);
|
2002-08-28 17:57:52 +00:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
2003-03-03 17:09:13 +00:00
|
|
|
if (FD_IS_RD_EXC(sd, rds, excs))
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
|
|
|
*cmd = SMFIC_SELECT;
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-17 21:56:45 +00:00
|
|
|
len = MI_SOCK_READ(sd, buf + i, expl - i);
|
|
|
|
if (MI_SOCK_READ_FAIL(len))
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
|
|
|
smi_log(SMI_LOG_ERR,
|
|
|
|
"%s: mi_rd_cmd: read returned %d: %s",
|
2003-02-08 20:31:29 +00:00
|
|
|
name, (int) len, sm_errstring(errno));
|
2000-08-12 21:55:49 +00:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_EOF;
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (len > expl - i)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_RECVERR;
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (len >= expl - i)
|
|
|
|
{
|
|
|
|
*rlen = expl;
|
2002-02-17 21:56:45 +00:00
|
|
|
#if _FFR_ADD_NULL
|
|
|
|
/* makes life simpler for common string routines */
|
|
|
|
buf[expl] = '\0';
|
|
|
|
#endif /* _FFR_ADD_NULL */
|
2000-08-12 21:55:49 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
i += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
save_errno = errno;
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
/* select returned 0 (timeout) or < 0 (error) */
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
*cmd = SMFIC_TIMEOUT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
smi_log(SMI_LOG_ERR,
|
|
|
|
"%s: mi_rd_cmd: select returned %d: %s",
|
2002-02-17 21:56:45 +00:00
|
|
|
name, ret, sm_errstring(save_errno));
|
2000-08-12 21:55:49 +00:00
|
|
|
*cmd = SMFIC_RECVERR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*cmd = SMFIC_UNKNERR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-17 21:56:45 +00:00
|
|
|
/*
|
2000-08-12 21:55:49 +00:00
|
|
|
** MI_WR_CMD -- write a cmd to sd
|
|
|
|
**
|
|
|
|
** Parameters:
|
|
|
|
** sd -- socket descriptor
|
|
|
|
** timeout -- maximum time to wait (currently unused)
|
|
|
|
** cmd -- single character command to write
|
|
|
|
** buf -- buffer with further data
|
|
|
|
** len -- length of buffer (without cmd!)
|
|
|
|
**
|
|
|
|
** Returns:
|
|
|
|
** MI_SUCCESS/MI_FAILURE
|
|
|
|
*/
|
|
|
|
|
2002-04-10 03:05:00 +00:00
|
|
|
/*
|
|
|
|
** we don't care much about the timeout here, it's very long anyway
|
2003-02-08 20:31:29 +00:00
|
|
|
** FD_SETSIZE is checked when socket is created.
|
2002-04-10 03:05:00 +00:00
|
|
|
** XXX l == 0 ?
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MI_WR(data) \
|
|
|
|
while (sl > 0) \
|
|
|
|
{ \
|
2003-03-03 17:09:13 +00:00
|
|
|
FD_WR_INIT(sd, wrs); \
|
|
|
|
ret = FD_WR_READY(sd, wrs, timeout); \
|
2002-04-10 03:05:00 +00:00
|
|
|
if (ret == 0) \
|
|
|
|
return MI_FAILURE; \
|
|
|
|
if (ret < 0) \
|
|
|
|
{ \
|
|
|
|
if (errno == EINTR) \
|
|
|
|
continue; \
|
|
|
|
else \
|
|
|
|
return MI_FAILURE; \
|
|
|
|
} \
|
|
|
|
l = MI_SOCK_WRITE(sd, (void *) ((data) + i), sl); \
|
|
|
|
if (l < 0) \
|
|
|
|
{ \
|
|
|
|
if (errno == EINTR) \
|
|
|
|
continue; \
|
|
|
|
else \
|
|
|
|
return MI_FAILURE; \
|
|
|
|
} \
|
|
|
|
i += l; \
|
|
|
|
sl -= l; \
|
|
|
|
}
|
|
|
|
|
2000-08-12 21:55:49 +00:00
|
|
|
int
|
|
|
|
mi_wr_cmd(sd, timeout, cmd, buf, len)
|
|
|
|
socket_t sd;
|
|
|
|
struct timeval *timeout;
|
|
|
|
int cmd;
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
{
|
|
|
|
size_t sl, i;
|
|
|
|
ssize_t l;
|
|
|
|
mi_int32 nl;
|
|
|
|
int ret;
|
2003-03-03 17:09:13 +00:00
|
|
|
FD_WR_VAR(wrs);
|
2000-08-12 21:55:49 +00:00
|
|
|
char data[MILTER_LEN_BYTES + 1];
|
|
|
|
|
|
|
|
if (len > MILTER_CHUNK_SIZE)
|
|
|
|
return MI_FAILURE;
|
|
|
|
nl = htonl(len + 1); /* add 1 for the cmd char */
|
|
|
|
(void) memcpy(data, (void *) &nl, MILTER_LEN_BYTES);
|
|
|
|
data[MILTER_LEN_BYTES] = (char) cmd;
|
|
|
|
i = 0;
|
|
|
|
sl = MILTER_LEN_BYTES + 1;
|
|
|
|
|
|
|
|
/* use writev() instead to send the whole stuff at once? */
|
|
|
|
|
2002-04-10 03:05:00 +00:00
|
|
|
MI_WR(data);
|
2000-08-12 21:55:49 +00:00
|
|
|
if (len > 0 && buf == NULL)
|
|
|
|
return MI_FAILURE;
|
|
|
|
if (len == 0 || buf == NULL)
|
|
|
|
return MI_SUCCESS;
|
|
|
|
i = 0;
|
|
|
|
sl = len;
|
2002-04-10 03:05:00 +00:00
|
|
|
MI_WR(buf);
|
2000-08-12 21:55:49 +00:00
|
|
|
return MI_SUCCESS;
|
|
|
|
}
|