/* milter-callback - a sendmail milter to perform variuos sender verifications
 Copyright (C) 2008  Eugene M. Zheganin, milter-callback@norma.perm.ru

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
     
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
	   
#include <milter-callback.h>

extern char *logString;
extern int logLevel;
extern int mcDebug;
extern int connPoolLen;
extern struct connList connPool;
extern char milterEmail[MAXEMAIL];
extern struct netList whiteNets;
extern char replyURL[1024];

void
logNow(char *stringToLog, int logWhenPrio) {
    if (logWhenPrio <= logLevel && mcDebug == 0) {
	/* log only when appropriate logLevel is set */
	switch(logWhenPrio) {
	    case 0:
		syslog(LOG_ERR, "%s", stringToLog);
		break;
	    case 1:
		syslog(LOG_WARNING, "%s", stringToLog);
		break;
	    case 2:
		syslog(LOG_NOTICE, "%s", stringToLog);
		break;
	    case 3:
		syslog(LOG_INFO, "%s", stringToLog);
		break;
	}
    } else {
	if (mcDebug == 1) {
	    fprintf(stdout, "%s\n", stringToLog);
	}
    };
}

#ifdef WITH_PGSQL
int
getPgConn (){
    int res, i, rndConn;
    
    for (i = 0; i < connPoolLen; i++) {
	snprintf(logString, MAXLOGLINE, "Trying to aquire a lock on connection %d.", i);
	logNow(logString, 3);
	res = pthread_mutex_trylock(connPool.pgMutexArr[i]);
	if (res == 0) {
	    snprintf(logString, MAXLOGLINE, "Connection %d successfully aquired.", i);
	    logNow(logString, 3);
	    return i;
	}
    }
    /* all connection busy */
    /* reporting that */
    snprintf(logString, MAXLOGLINE, "All connections busy, consider increasing pool length from %d.", connPoolLen);
    logNow(logString, 0);
    /*getting random connection and breaking until it's available */
    /* rndConn = random(connPoolLen - 1); */
    /* random seems to be a complete rocket science so let's just take last connection */
    rndConn = connPoolLen - 1;
    snprintf(logString, MAXLOGLINE, "All connections busy, trying to aquire not so random connection %d.", rndConn);
    logNow(logString, 0);
    res = pthread_mutex_lock(connPool.pgMutexArr[rndConn]);
    if (res == 0) {
	snprintf(logString, MAXLOGLINE, "Random connection %d successfully aquired.", rndConn);
	logNow(logString, 0);
	return rndConn;
    } else {
	snprintf(logString, MAXLOGLINE, "Failed to aquire random connection %d, this is just wrong, aborting.", rndConn);
	logNow(logString, 0);
	exit(EXIT_FAILURE);
    }
}

void
unlockPgConn(int pgConnId) {
    snprintf(logString, MAXLOGLINE, "Trying to unlock mutex on pool element %d.", pgConnId);
    logNow(logString, 3);
    if (pthread_mutex_unlock(connPool.pgMutexArr[pgConnId]) == 0) {
	snprintf(logString, MAXLOGLINE, "Successfully unlocked mutex on pool element %d.", pgConnId);
	logNow(logString, 3);
    } else {
	snprintf(logString, MAXLOGLINE, "Failure unlocking mutex on pool element %d, this is wrong, aborting.", pgConnId);
	logNow(logString, 0);
    }
}
#endif

int
parseAddressString (char *addressString, struct in_addr *network, struct in_addr *mask) {
/* 0 - success
   1 - failure
*/
    char *cidrMask;
    const char *cidrDelimiters = "/";
    uint32_t netMask;

    /* striping cidr mask */
    cidrMask = strdup(addressString);
    addressString = strsep(&addressString, cidrDelimiters);
    strsep(&cidrMask, cidrDelimiters);
    
    snprintf(logString, MAXLOGLINE, "Got new network mask /%s.", cidrMask);
    logNow(logString, 3);
    /* getting new cidr mask to proper format */
    if (cidrMask == NULL) {
        netMask = 32;
    } else {
        netMask = strtol(cidrMask, NULL, 0);
    }
    if (netMask == 0) {
	snprintf(logString, MAXLOGLINE, "Cannot convert %s to netmask, aborting.", cidrMask);
	logNow(logString, 0);
	return 1;
    }
    if (netMask < 0 || netMask > 32) {
	snprintf(logString, MAXLOGLINE, "%d isnt't a valid netmask.", netMask);
	logNow(logString, 0);
	return 1;
    }
    netMask = 0xffffffff << (32 - netMask);
    netMask = htonl(netMask);
    snprintf(logString, MAXLOGLINE, "After conversion of mask /%s to network format got 0x%x in host order, 0x%x natively.", cidrMask, ntohl(netMask), netMask);
    logNow(logString, 3);
    mask->s_addr = netMask;
    
    if (inet_pton(AF_INET, addressString, network) == 1) {
	snprintf(logString, MAXLOGLINE, "After conversion of net %s to network format got 0x%x in host order, 0x%x natively.", 
	addressString, ntohl(network->s_addr), network->s_addr);
	logNow(logString, 3);	
	return 0;
    } else {
	snprintf(logString, MAXLOGLINE, "Cannot convert %s to network format, aborting.", addressString);
	logNow(logString, 3);
	return 1;
    }
}

int
hostIsWhitelisted(struct mlfiPriv *privData) {
/*
    0 - no error
    1 - some critical error happened, need to reject message
*/

    char presNet[INET_ADDRSTRLEN], presMask[INET_ADDRSTRLEN];
    int i = 0;
    int whiteListed = 0;
#ifdef WITH_PGSQL
    char *whiteListEntry;
    int whiteListEntryLen, retVal, pgConnId;
    struct in_addr *netToTest, *maskToTest;
#endif

    snprintf(logString, MAXLOGLINE, "Entered hostIsWhitelisted().");
    logNow(logString, 3);
    bzero(presNet, sizeof(presNet));
    bzero(presMask, sizeof(presMask));
    while (i < whiteNets.netCount && whiteListed == 0) {
	if (inet_ntop(AF_INET, &whiteNets.whiteNet[i], presNet, INET_ADDRSTRLEN) == NULL) {
    	    snprintf(logString, MAXLOGLINE, "Cannot convert %d to presentation format.", whiteNets.whiteNet[i].s_addr);
	    logNow(logString, 0);
	    return 1;
	}
	if (inet_ntop(AF_INET, &whiteNets.whiteMask[i], presMask, INET_ADDRSTRLEN) == NULL) {
	    snprintf(logString, MAXLOGLINE, "Cannot convert %d to presentation format.", whiteNets.whiteMask[i].s_addr);
	    logNow(logString, 0);
	    return 1;
	}
	if ((privData->relayIP & whiteNets.whiteMask[i].s_addr) == whiteNets.whiteNet[i].s_addr) {
	    whiteListed = 1;
	    snprintf(logString, MAXLOGLINE, "Found host %s in whitelisted network %s, mask %s, passing message", privData->relayPresIP, presNet, presMask);
	    logNow(logString, 0);
	    privData->whitelisted = 1;
	    return 0;
	} else {
	    snprintf(logString, MAXLOGLINE, "Host %s doesn't belong to network %s, mask %s.", privData->relayPresIP, presNet, presMask);
	    logNow(logString, 2);
	}
	i++;
    }
#ifdef WITH_PGSQL
    pgConnId = getPgConn();
    connPool.pgResArr[pgConnId] = PQexec(connPool.pgConnArr[pgConnId], "select address from whitelist");
    if (PQresultStatus(connPool.pgResArr[pgConnId]) != PGRES_TUPLES_OK){
        snprintf(logString, MAXLOGLINE, "SELECT command failed: %s", PQerrorMessage(connPool.pgConnArr[pgConnId]));
	logNow(logString, 0);
	snprintf(logString, MAXLOGLINE, "The result status is: %s",PQresStatus(PQresultStatus(connPool.pgResArr[pgConnId])));
	logNow(logString, 0);
        PQclear(connPool.pgResArr[pgConnId]);
	return 1;
    }
    snprintf(logString, MAXLOGLINE, "Successfully queried database, got %d rows.", PQntuples(connPool.pgResArr[pgConnId]));
    logNow(logString, 2);
    
    /* here the main check starts */
    i = 0;
    while (i < PQntuples(connPool.pgResArr[pgConnId]) && privData->whitelisted == 0){
	whiteListEntryLen = strlen(PQgetvalue(connPool.pgResArr[pgConnId], i, 0));
	whiteListEntry = malloc(whiteListEntryLen + 1);
	if (whiteListEntry == NULL) {
	    snprintf(logString, MAXLOGLINE, "Cannot allocate memory.");
	    logNow(logString, 0);
	    PQclear(connPool.pgResArr[pgConnId]);
	    return 1;
	}
	bzero(whiteListEntry, whiteListEntryLen + 1);
	strncpy(whiteListEntry, PQgetvalue(connPool.pgResArr[pgConnId], i, 0), whiteListEntryLen);
    
	snprintf(logString, MAXLOGLINE, "Got white network %s from database.", whiteListEntry);
	logNow(logString, 3);
	
	netToTest = malloc(sizeof(struct in_addr));
	maskToTest = malloc(sizeof(struct in_addr));
	if (netToTest == NULL  || maskToTest == NULL) {
	    snprintf(logString, MAXLOGLINE, "Cannot allocate memory.");
	    logNow(logString, 0);
	    PQclear(connPool.pgResArr[pgConnId]);
	    return 1;
	}
	
	retVal = parseAddressString(whiteListEntry, netToTest, maskToTest);
	if (retVal == 0) {
	    /* the main check itself */
	    if ((privData->relayIP & maskToTest->s_addr) == netToTest->s_addr) {
		snprintf(logString, MAXLOGLINE, "Found host %s in whitelisted network %s, passing message.", privData->relayPresIP, whiteListEntry);
		logNow(logString, 0);
		privData->whitelisted = 1;		
	    } else {
		snprintf(logString, MAXLOGLINE, "Host %s doesn't belong to network %s.", privData->relayPresIP, whiteListEntry);
		logNow(logString, 2);
	    }
	} else {
	    snprintf(logString, MAXLOGLINE, "parseAddressString returned error, aborting.");
	    logNow(logString, 0);
	    PQclear(connPool.pgResArr[pgConnId]);
	    return 1;
	}
	
	snprintf(logString, MAXLOGLINE, "Trying to free() netToTest in hostIsWhitelisted().");
	logNow(logString, 3);
	free(netToTest);
	snprintf(logString, MAXLOGLINE, "Trying to free() maskToTest in hostIsWhitelisted().");
	logNow(logString, 3);
	free(maskToTest);
	i++;
    }
    /* flush out the rest */
    snprintf(logString, MAXLOGLINE, "Trying to PQClear() pgRes in hostIsWhitelisted().");
    logNow(logString, 3);
    PQclear(connPool.pgResArr[pgConnId]);
    /* freeing lock */
    unlockPgConn(pgConnId);
#endif
    return 0;
}


int
checkServerResponse (char *emailAddress, char *serverName, struct in_addr *serverIP, int resolved) {
/*
    return codes:
    0 - email accepped
    1 - email not accepted
    2 - email tempfailed
    3 - socket error
    4 - DNS error
    5 - unknown answer code
    6 - memory error
*/

    const int smtpPort = 25;
    char myHostname[MAXHOSTNAME];
    char *myHostnameP;
    int sockfd, socketResCode;
    struct sockaddr_in serverAddress;
    struct hostent *server;
    
    char *smtpCommand;
    char smtpAnswer[2048], smtpString[2048];
    char *smtpAnswerPointer;
    char *smtpAnswerCode;
    char serverPresIP[INET_ADDRSTRLEN];
    
    const char *eolDelimiter = "\r\n";
    
    int connectResCode, resCode;
    int need2FreeSN = 0;
    
    struct timeval sockTimeout;
    
    char
    *readSMTPCode (char *answerString) {
	
	char *smtpBuf;
	char *smtpAnswerCode;
	const char *smtpCodeDelimiter = " -";
	
	snprintf(logString, MAXLOGLINE, "Entered readSMTPCode().");
	logNow(logString, 3);
	snprintf(logString, MAXLOGLINE, "answerString is %s.", answerString);
	logNow(logString, 3);
				
	smtpAnswerCode = malloc(strlen(answerString) + 1);
	if (smtpAnswerCode == NULL) {
	    snprintf(logString, MAXLOGLINE, "Cannot allocate memory.");
	    logNow(logString, 0);
	    return(NULL);
        }
	bzero(smtpAnswerCode, strlen(answerString) + 1);
        /* checking SMTP answer code */
	/* we just reserved enough memory, so "wrong buffer length" it's not a mistake */
        strncpy(smtpAnswerCode, answerString, strlen(answerString));
	smtpBuf = smtpAnswerCode;
        strsep(&smtpBuf, smtpCodeDelimiter);
        return smtpAnswerCode;
    }

    void
    closeSocket (int socketId, int exitNicely) {
	/* nice ppl say QUIT on exit */
	snprintf(logString, MAXLOGLINE, "Entered closeSocket().");
	logNow(logString, 3);
	
	if (exitNicely == 1){
	    smtpCommand = "QUIT";
	
	    bzero(smtpString, sizeof(smtpString));
	    strcat(smtpString, smtpCommand);
	    snprintf(logString, MAXLOGLINE, "Saying \"%s\" to SMTP server.", smtpString);
	    logNow(logString, 3);
	    strcat(smtpString, eolDelimiter);
	
	    socketResCode = write(socketId, smtpString, strlen(smtpString));
	    if (socketResCode < 0){
		snprintf(logString, MAXLOGLINE, "ERROR writing to socket.");
		logNow(logString, 0);
	    }
	    if (smtpAnswerCode != NULL) {
		snprintf(logString, MAXLOGLINE, "Trying to free smtpAnswerCode in closeSocket().");
		logNow(logString, 3);
		free(smtpAnswerCode);
	    }
	}
	/* now closing the socket */
	close(socketId);
    }
    
    snprintf(logString, MAXLOGLINE, "Entered checkServerResponse().");
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "Got emailAddress, %s.", emailAddress);
    logNow(logString, 3);
    
    strncpy(&myHostname[0], &milterEmail[0], MAXHOSTNAME);
    myHostnameP = &myHostname[0];
    strsep(&myHostnameP, "@");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
	snprintf(logString, MAXLOGLINE, "Error opening socket.");
	logNow(logString, 0);
        return(3);
    };
    
    /* setting the sending-receiving timeout to 30 */
    sockTimeout.tv_sec = 90;
    sockTimeout.tv_usec = 0;
    
    if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &sockTimeout, sizeof(struct timeval)) != 0){
	snprintf(logString, MAXLOGLINE, "Failed to set socket sending timeout.");
	logNow(logString, 0);
    }
    
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &sockTimeout, sizeof(struct timeval)) != 0){
	snprintf(logString, MAXLOGLINE, "Failed to set socket receiving timeout.");
	logNow(logString, 0);
    }
    
    if (resolved == 0) {
	snprintf(logString, MAXLOGLINE, "Looking up %s.", serverName);
	logNow(logString, 3);
	server = gethostbyname(serverName);
	/* TODO: error handling */
	/* this code need more reviewing, the main branch seems to be never triggering */
	if (server == NULL) {
    	    snprintf(logString, MAXLOGLINE, "ERROR, no such host - %s.", serverName);
	    logNow(logString, 0);
    	    return(4);
	} else {
	    snprintf(logString, MAXLOGLINE, "Successfully resolved %s.", serverName);
	    logNow(logString, 3);
	}
	bzero(&serverAddress, sizeof(serverAddress));
	bcopy(server->h_addr, &serverAddress.sin_addr.s_addr, server->h_length);
	bzero(serverPresIP, sizeof(serverPresIP));
        inet_ntop(AF_INET, server->h_addr, serverPresIP, INET_ADDRSTRLEN);
        snprintf(logString, MAXLOGLINE, "Address of %s is %s.", serverName, serverPresIP);
        logNow(logString, 3);
	/* TODO: network addresses cycling */
    } else {
	/* we already got resolved address in desired order */
	serverAddress.sin_addr.s_addr = serverIP->s_addr;
	server = gethostbyaddr(&serverIP->s_addr, sizeof(struct in_addr), AF_INET);
	if (server == NULL) {
	    serverName = malloc(INET_ADDRSTRLEN);
            if (serverName == NULL) {
                snprintf(logString, MAXLOGLINE, "Cannot allocate memory.");
                logNow(logString, 0);
                return(6);
            } else {
		need2FreeSN = 1;
	    }
            inet_ntop(AF_INET, &serverAddress.sin_addr.s_addr, serverName, INET_ADDRSTRLEN);
	    snprintf(logString, MAXLOGLINE, "Failed to resolve IP of %s.", serverName);
	    logNow(logString, 0);
        } else {
            serverName = server->h_name;
        }
    }
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(smtpPort);
    snprintf(logString, MAXLOGLINE, "Got server name, %s.", serverName);
    logNow(logString, 3);
    inet_ntop(AF_INET, &serverAddress.sin_addr.s_addr, serverPresIP, sizeof(serverPresIP));
    snprintf(logString, MAXLOGLINE, "Got server IP, %s.", serverPresIP);
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "Checking %s on server %s.", emailAddress, serverName);
    logNow(logString, 3);
    
    snprintf(logString, MAXLOGLINE, "Trying to connect to %x, port 25.", serverAddress.sin_addr.s_addr);
    logNow(logString, 3);
    connectResCode = connect(sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress));
    if (connectResCode < 0) {
        snprintf(logString, MAXLOGLINE, "Cannot connect to port %d on server %s, got \"%s\".", smtpPort, serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    } else {
	snprintf(logString, MAXLOGLINE, "Successfully connected to port %d on server %s.", smtpPort, serverName);
	logNow(logString, 2);
    }
    
    /* reading greeting */
    bzero(smtpAnswer, sizeof(smtpAnswer));
    socketResCode = read(sockfd,smtpAnswer,sizeof(smtpAnswer) - 1);
    if (socketResCode < 0) {
         snprintf(logString, MAXLOGLINE, "ERROR reading from socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	 logNow(logString, 0);
	 closeSocket(sockfd, 0);
	 return(3);
    } else {
	snprintf(logString, MAXLOGLINE, "Successfully read greeting on server %s, greeting is %s.", serverName, smtpAnswer);
	logNow(logString, 3);
    }
    smtpAnswerPointer = &smtpAnswer[0];
    strsep(&smtpAnswerPointer, eolDelimiter);
    snprintf(logString, MAXLOGLINE, "Calling readSMTPCode with string %s.", smtpAnswer);
    logNow(logString, 3);
    smtpAnswerCode = readSMTPCode(&smtpAnswer[0]);
    snprintf(logString, MAXLOGLINE, "The answer was: \"%s\".", smtpAnswer);
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "The SMTP answer code is: \"%s\".", smtpAnswerCode);
    logNow(logString, 2);
    
    /* if the code isn't 220 - tempfail message */
    if (strcmp(smtpAnswerCode, "220") != 0) {
	snprintf(logString, MAXLOGLINE, "Returning tempfailing relay, %s received in greeting.", smtpAnswerCode);
	logNow(logString, 2);
	closeSocket(sockfd, 1);
	return(2);
    }
    if (smtpAnswerCode != NULL) {
	snprintf(logString, MAXLOGLINE, "Trying to free smtpAnswerCode in checkServerResponse().");
	logNow(logString, 3);
	free(smtpAnswerCode);
    }
    
    /* nice ppl say HELO */
    smtpCommand = "HELO ";
    bzero(smtpString, sizeof(smtpString));
    
    strcat(smtpString, smtpCommand);
    strcat(smtpString, myHostnameP);
    snprintf(logString, MAXLOGLINE, "Saying \"%s\" to SMTP server.", smtpString);
    logNow(logString, 3);
    strcat(smtpString, eolDelimiter);
    
    socketResCode = write(sockfd,smtpString,strlen(smtpString));    
    if (socketResCode < 0){
        snprintf(logString, MAXLOGLINE, "ERROR writing to socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    }
    
    /* reading answer */
    bzero(smtpAnswer,sizeof(smtpAnswer));
    socketResCode = read(sockfd,smtpAnswer,sizeof(smtpAnswer) - 1);
    if (socketResCode < 0) {
         snprintf(logString, MAXLOGLINE, "ERROR reading from socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	 logNow(logString, 0);
	 closeSocket(sockfd, 0);
	 return(3);
    };
    smtpAnswerPointer = &smtpAnswer[0];
    strsep(&smtpAnswerPointer, eolDelimiter);
    smtpAnswerCode = readSMTPCode(&smtpAnswer[0]);
    snprintf(logString, MAXLOGLINE, "The answer was: \"%s\".", smtpAnswer);
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "The SMTP answer code is: \"%s\".", smtpAnswerCode);
    logNow(logString, 2);
    
    /* if the code isn't 250 - tempfail message */
    if (strcmp(smtpAnswerCode, "250") != 0) {
	snprintf(logString, MAXLOGLINE, "Returning tempfailing relay, %s received after HELO.", smtpAnswerCode);
	logNow(logString, 2);
	closeSocket(sockfd, 1);
	return(2);
    }
    if (smtpAnswerCode != NULL) {
	snprintf(logString, MAXLOGLINE, "Trying to free smtpAnswerCode in checkServerResponse().");
	logNow(logString, 3);
	free(smtpAnswerCode);
    }
    
    /* beginning main procedure */
    smtpCommand = "MAIL FROM: ";
    bzero(smtpString, sizeof(smtpString));
    strcat(smtpString, smtpCommand);
    strcat(smtpString, "<");
    strcat(smtpString, milterEmail);
    strcat(smtpString, ">");
    snprintf(logString, MAXLOGLINE, "Saying \"%s\" to SMTP server.", smtpString);
    logNow(logString, 3);
    strcat(smtpString, eolDelimiter);
    
    socketResCode = write(sockfd,smtpString,strlen(smtpString));    
    if (socketResCode < 0){
        snprintf(logString, MAXLOGLINE, "ERROR writing to socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    };
    
    /* reading answer */
    bzero(smtpAnswer, sizeof(smtpAnswer));
    socketResCode = read(sockfd,smtpAnswer,sizeof(smtpAnswer) - 1);
    if (socketResCode < 0) {
        snprintf(logString, MAXLOGLINE, "ERROR reading from socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    };
    smtpAnswerPointer = &smtpAnswer[0];
    strsep(&smtpAnswerPointer, eolDelimiter);
    smtpAnswerCode = readSMTPCode(&smtpAnswer[0]);
    snprintf(logString, MAXLOGLINE, "The answer was: \"%s\".", smtpAnswer);
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "The SMTP answer code is: \"%s\".", smtpAnswerCode);
    logNow(logString, 2);
    
    /* if the code isn't 250 - tempfail message */
    if (strcmp(smtpAnswerCode, "250") != 0) {
	snprintf(logString, MAXLOGLINE, "Returning tempfailing relay, %s received after MAIL FROM.", smtpAnswerCode);
	logNow(logString, 2);
	closeSocket(sockfd, 1);
	return(2);
    }
    if (smtpAnswerCode != NULL) {
	snprintf(logString, MAXLOGLINE, "Trying to free smtpAnswerCode in checkServerResponse().");
	logNow(logString, 3);
	free(smtpAnswerCode);
    }
    
    /* Telling the recipient address */
    smtpCommand = "RCPT TO: ";
    bzero(smtpString, sizeof(smtpString));
    strcat(smtpString, smtpCommand);
    strcat(smtpString, "<");
    strcat(smtpString, emailAddress);
    strcat(smtpString, ">");
    snprintf(logString, MAXLOGLINE, "Saying \"%s\" to SMTP server.", smtpString);
    logNow(logString, 3);
    strcat(smtpString, eolDelimiter);
    
    socketResCode = write(sockfd,smtpString,strlen(smtpString));    
    if (socketResCode < 0){
        snprintf(logString, MAXLOGLINE, "ERROR writing to socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    };
    
    /* reading answer */
    bzero(smtpAnswer,sizeof(smtpAnswer));
    socketResCode = read(sockfd,smtpAnswer,sizeof(smtpAnswer) - 1);
    if (socketResCode < 0) {
        snprintf(logString, MAXLOGLINE, "ERROR reading from socket while in session with %s, got \"%s\".", serverName, strerror(errno));
	logNow(logString, 0);
	closeSocket(sockfd, 0);
	return(3);
    };
    smtpAnswerPointer = &smtpAnswer[0];
    strsep(&smtpAnswerPointer, eolDelimiter);
    smtpAnswerCode = readSMTPCode(&smtpAnswer[0]);
    snprintf(logString, MAXLOGLINE, "The answer was: \"%s\".", smtpAnswer);
    logNow(logString, 3);
    snprintf(logString, MAXLOGLINE, "The SMTP answer code is: \"%s\".", smtpAnswerCode);
    logNow(logString, 2);
    
    resCode = 0;
    resCode = strtol(smtpAnswerCode, NULL, 10);
    if (serverName != NULL && need2FreeSN == 1) {
	snprintf(logString, MAXLOGLINE, "Trying to free serverName in checkServerResponse().");
	logNow(logString, 3);
	free(serverName);
    }
    if (resCode == 0) {
        snprintf(logString, MAXLOGLINE, "Cannot parse SMTP answer code %s.", smtpAnswerCode);
        logNow(logString, 0);
        return(5);
    } else {
        if (resCode >= 400 && resCode <= 499) {
    	    snprintf(logString, MAXLOGLINE, "Returning tempfailing relay, received %d after RCPT TO.", resCode);
	    logNow(logString, 2);
	    closeSocket(sockfd, 1);
	    return(2);
	} else {
	    if (resCode >= 500 && resCode <= 599) {
	        snprintf(logString, MAXLOGLINE, "Returning rejecting relay, received %d after RCPT TO.", resCode);
	        logNow(logString, 2);
	        closeSocket(sockfd, 1);
	        return(1);
	    } else {
	        if (resCode >= 250 && resCode <= 251) {
	    	    snprintf(logString, MAXLOGLINE, "Returning passing relay, received %d after RCPT TO.", resCode);
	    	    logNow(logString, 2);
	    	    closeSocket(sockfd, 1);
	    	    return(0);
	        } else {
	    	    snprintf(logString, MAXLOGLINE, "Returning tempfailing relay, received unknow code %d after RCPT TO.", resCode);
	    	    logNow(logString, 2);
		    closeSocket(sockfd, 1);
	    	    return(5);
		}
	    }
	}
    }
}

void
mc_setreply(SMFICTX *ctx, char *rCode, char *rText) {
    char *replyCode, *replyText;
    
    replyCode = malloc(strlen(rCode) + 1);
    replyText = malloc(strlen(rText) + 1);
    if (replyCode == NULL || replyText == NULL) {
        snprintf(logString, MAXLOGLINE, "Cannot allocate memory");
        logNow(logString, 0);
    } else {
        bzero(replyCode, strlen(rCode) + 1);
        bzero(replyText, strlen(rText) + 1);
        strcpy(replyCode, rCode);
        if (strlen(replyURL) > 0) {
            strcpy(replyText, replyURL);
        } else {
            strcpy(replyText, rText);
        }
        if (smfi_setreply(ctx, replyCode, NULL, replyText) != MI_SUCCESS) {
            snprintf(logString, MAXLOGLINE, "Failed to set code %s: %s", replyCode, replyText);
            logNow(logString, 0);
        } else {
            snprintf(logString, MAXLOGLINE, "Successfully set code %s: %s", replyCode, replyText);
            logNow(logString, 2);
        }
        if (replyCode != NULL) {
            snprintf(logString, MAXLOGLINE, "Trying to free replyCode in mc_setreply().");
            logNow(logString, 3);
            free(replyCode);
        }
        if (replyText != NULL) {
            snprintf(logString, MAXLOGLINE, "Trying to free replyText in mc_setreply().");
            logNow(logString, 3);
            free(replyText);
        }
    }
}

char
*transcriptAnswerCode(int answerCode, char *transcript) {
    switch(answerCode) {
        case CB_PASS:
            sprintf(transcript, "accepts");
            break;
        case CB_REJECT:
            sprintf(transcript, "rejects");
            break;
        case CB_TEMPFAIL:
            sprintf(transcript, "tempfails");
            break;
        case CB_SKIP:
            sprintf(transcript, "was skipped (sending relay is one of the MXes)");
            break;
        case CB_GRACEFUL:
            sprintf(transcript, "was skipped (sending relay IP reverse name contains e-mail hostpart)");
            break;
    }
    return transcript;
}
