/*
 * Copyright (c) 2005 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.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: pmilter_hdrmod.c,v 1.15 2006/10/05 04:27:38 ca Exp $")
#include "sm/error.h"
#include "sm/assert.h"
#include "pmilter.h"
#include "sm/pmfdef.h"
#include "sm/pmfapi.h"

#if MTA_USE_PMILTER

/*
**  SM_PMFI_HDR_MOD -- Create header modification request
**
**	Parameters:
**		pmse_ctx -- pmilter/SMTP server session context
**		type -- type of request
**		pos -- position of header
**		header -- header (full header line!)
**
**	Returns:
**		usual sm_error code
*/

sm_ret_T
sm_pmfi_hdr_mod(pmse_ctx_P pmse_ctx, uint type, uint pos, const unsigned char *header)
{
	sm_ret_T ret;
	size_t len;
	sm_hdrmod_P sm_hdrmod;
	sm_cstr_P hdr;

	SM_IS_PMSE_CTX(pmse_ctx);
	hdr = NULL;
	sm_hdrmod = NULL;

	if (pmse_ctx->pmse_state != PMSE_ST_DOT)
		return sm_error_perm(SM_EM_PMILTER, SM_E_PR_ERR);

	if (   type != SM_HDRMOD_T_PREPEND
	    && type != SM_HDRMOD_T_APPEND
	    && type != SM_HDRMOD_T_REMOVE
	    && type != SM_HDRMOD_T_INSERT
	    && type != SM_HDRMOD_T_REPLACE
	   )
		return sm_error_perm(SM_EM_PMILTER, EINVAL);

	if (NULL == header && type != SM_HDRMOD_T_REMOVE)
		return sm_error_perm(SM_EM_PMILTER, EINVAL);
	if (header != NULL)
	{
		len = strlen((const char *) header);
		if (len >= SM_MAXHDRLEN)
			return sm_error_perm(SM_EM_PMILTER, SM_E_OVFLW_NS);
		if (len < 4)
			return sm_error_perm(SM_EM_PMILTER, EINVAL);
		if (header[len - 2] != '\r' || header[len - 1] != '\n')
			return sm_error_perm(SM_EM_PMILTER, EINVAL);
		hdr = sm_cstr_scpyn0(header, len);
		if (NULL == hdr)
			return sm_error_perm(SM_EM_PMILTER, ENOMEM);
	}

	ret = sm_hdrmod_new(&pmse_ctx->pmse_hdrmodhd, false, &sm_hdrmod);
	if (sm_is_err(ret))
		goto error;
	sm_hdrmod->sm_hm_type = type;
	sm_hdrmod->sm_hm_pos = pos;
	sm_hdrmod->sm_hm_hdr = hdr;
	hdr = NULL;
	(void) sm_hdrmod_insert(pmse_ctx->pmse_hdrmodhd, sm_hdrmod);

	return SM_SUCCESS;

  error:
	SM_CSTR_FREE(hdr);
	return ret;
}
#endif /* MTA_USE_PMILTER */


syntax highlighted by Code2HTML, v. 0.9.1