/*
 */

#include "sm/generic.h"
SM_IDSTR(id, "@(#)$Id: t-tlsr-cnf-0.c,v 1.9 2007/06/13 04:43:08 ca Exp $")

#include "sm/error.h"
#include "sm/io.h"
#include "sm/sm-conf.h"
#include "sm/sm-conf-prt.h"
#define SM_TLSREQCNFDEF	1
#include "sm/tlsreqcnf.h"
#include "sm/tlsreqcnfs.h"
#include "sm/test.h"

#if MTA_USE_TLS
static int Verbose;

sm_conf_definition_T
tlsreq_defs[] = {
TLSREQ_DEFS(tlsreq_cnf_T, tlsreqcnf),

{ SM_CONF_DEF_MAGIC, NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL SM_LC_NO_ISSET	SM_LC_SET_MAGIC(0)}
};

static sm_ret_T
prt_tlsreq_cnf(tlsreq_cnf_P tlsreq_cnf)
{
	sm_io_fprintf(smioout,
		"CN=%s\n"
		"CS=%s\n"
		"CI=%s\n"
		"flags=%#x\n"
		"bits=%d\n"
		"requirements_violation=%s\n"
		, tlsreq_cnf->tlsreqcnf_common_name
		, tlsreq_cnf->tlsreqcnf_cert_subject
		, tlsreq_cnf->tlsreqcnf_cert_issuer
		, tlsreq_cnf->tlsreqcnf_flags
		, tlsreq_cnf->tlsreqcnf_min_cipher_bits
		, (tlsreq_cnf->tlsreqcnf_viol == TLSREQ_VIOL_PERM) ? "permfail" :
		  (tlsreq_cnf->tlsreqcnf_viol == TLSREQ_VIOL_TEMP) ? "tempfail" :
		  "abort"
		);
	sm_io_flush(smioout);
	return 0;
}


static sm_ret_T
test(char *data, bool isfile)
{
	int err;
	tlsreq_cnf_T tlsreq_cnf;
	FILE *fp;
	sm_conf_T *smc;

	fp = NULL;
	sm_memzero(&tlsreq_cnf, sizeof(tlsreq_cnf));
	smc = sm_conf_new(data);
	if (smc == NULL) {
		err = errno;

		sm_io_fprintf(smioerr,
			"sev=ERROR, func=sm_readconf, file=%s, sm_conf_new=NULL, errno=%d\n",
			data, err);
		return ENOMEM;
	}
	if (isfile)
		err = sm_conf_read_FILE(smc, data, fp);
	else
		err = sm_conf_read_data(smc, data, strlen(data), false);
	if (err != 0) {
		sm_prt_conferr(data, smc, err, smioerr);
		sm_conf_destroy(smc);
		smc = NULL;
		return err;
	}

	err = sm_conf_scan(smc, tlsreq_defs, 0, &tlsreq_cnf);
	if (err != 0)
		sm_prt_conferr(data, smc, err, smioerr);
	else
		prt_tlsreq_cnf(&tlsreq_cnf);
	return 0;
}

int
main(int argc, char *argv[])
{
	int c, r;
	bool isfile;
	char *prg;

	opterr = 0;
	Verbose = 0;
	isfile = true;
	prg = argv[0];
	while ((c = getopt(argc, argv, "dV")) != -1)
	{
		switch (c)
		{
		  case 'd':
			isfile = false;
			break;
		  case 'V':
			++Verbose;
			break;
		  default:
			exit(1);
		}
	}
	sm_test_begin(argc, argv, "test tls req conf 0");
	argc -= optind;
	argv += optind;
	if (argc > 0) {
		for (r = 0; r < argc; r++) {
			test(argv[r], isfile);
		}
	}
	return sm_test_end();
}
#else /* MTA_USE_TLS */
int
main(int argc, char *argv[])
{
	sm_io_fprintf(smioerr, "MTA_USE_TLS not set\n");
	return 0;
}
#endif /* MTA_USE_TLS */


syntax highlighted by Code2HTML, v. 0.9.1