/*
 * Copyright 1998 David A. Holland.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder(s) nor the names of their
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

char repairs_rcsid[] = 
  "$Id: repairs.c,v 1.6 2000/05/21 21:32:49 dholland Exp $";

/*
 * Most, but not quite all, of the voodoo for detecting and handling
 * malformed packets goes here.
 *
 * Basically anything which we can detect by examining the packet we
 * try to do in rationalize_packet(). We return a quirk code for what
 * we did so we can send back replies that will make sense to the other
 * end. That's irrationalize_reply().
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <syslog.h>
#include "prot_talkd.h"
#include "proto.h"
#include "ctlmsg.h"

/***************************************************/

/* 
 * probe for strings that are meaningful in talkd packets.
 * rejects all control characters and delete. newlines and tabs have
 * no business in tty names or usernames.
 */
static int probe_string(const char *buf, socklen_t len) {
    socklen_t i;
    int ch;
    for (i=0; i<len; i++) {
	if (buf[i]==0) return 0;  /* success */
	ch = (unsigned char) buf[i];
	if ((ch&127) < 32 || (ch&127)==127) return -1;
    }
    return -1; /* no null-terminator, assume it's not a string */
}

/*
 * Check if an address from a talk packet matches the actual sender
 * address. If it doesn't, it's a good bet it's not the right packet format.
 * Allow assorted endianness though.
 * In an ideal world we'd save the endianness info for use elsewhere instead
 * of reprobing it, but oh well.
 */
static int probe_addr(struct talkaddr *ta, struct sockaddr *sa) {
    struct sockaddr_in *sn = (struct sockaddr_in*)sa;
    u_int16_t family = sn->sin_family;
    u_int16_t xfamily = __bswap_16(family);
    u_int16_t port = sn->sin_port;
    u_int16_t xport = __bswap_16(port);
    u_int32_t addr = sn->sin_addr.s_addr;
    u_int32_t xaddr = __bswap_32(addr);

    if (ta->ta_family != family && ta->ta_family != xfamily) return -1;
    if (ta->ta_port != port && ta->ta_port != xport) return -1;
    if (ta->ta_addr != addr && ta->ta_addr != xaddr) return -1;
    return 0;
}

/***************************************************/

static int probe_otalk_packet(char *buf, socklen_t len, socklen_t maxlen, 
			      struct sockaddr *sa)
{
	OCTL_MSG otp;
	CTL_MSG m;

	debug("Probing for QUIRK_OTALK\n");

	/*
	 * in some cases this packet may need compiler
	 * pragmas to force the compiler to not pad it. shouldn't with
	 *  gcc though.
	 */
	if (sizeof(otp)!=OTALK_PACKET_SIZE) {
		syslog(LOG_ERR, "QUIRK_OTALK: struct otalk_packet padding "
		       "is wrong\n");
		return -1;
	}

	if (len!=sizeof(otp)) {
		debug("QUIRK_OTALK: wrong size\n");
		return -1;
	}
	
	memcpy(&otp, buf, len);
	if (probe_string(otp.l_name, sizeof(otp.l_name))) {
		debug("QUIRK_OTALK: l_name not a string\n");
		return -1;
	}
	if (probe_string(otp.r_name, sizeof(otp.r_name))) {
		debug("QUIRK_OTALK: r_name not a string\n");
		return -1;
	}
	if (probe_string(otp.r_tty, sizeof(otp.r_tty))) {
		debug("QUIRK_OTALK: r_tty not a string\n");
		return -1;
	}
	if (probe_addr(&otp.ctl_addr, sa)) {
		debug("QUIRK_OTALK: addresses do not match\n");
		return -1;
	}

	switch (otp.type) {
	    case LEAVE_INVITE:
	    case LOOK_UP:
	    case DELETE:
	    case ANNOUNCE:
		break;
	    /* I'm not sure these will work. */
            case OLD_DELETE_INVITE: otp.type = DELETE; break;
	    case OLD_AUTO_LOOK_UP: otp.type = LOOK_UP; break;
	    case OLD_AUTO_DELETE: otp.type = DELETE; break;
	    default: 
		debug("QUIRK_OTALK: invalid type field\n");
		return -1;
	}

	if (ONAME_SIZE >= NAME_SIZE) {
		syslog(LOG_ERR, "QUIRK_OTALK: ONAME_SIZE >= NAME_SIZE\n");
		syslog(LOG_ERR, "QUIRK_OTALK: fix repairs.c and recompile\n");
		return -1;
	}
	if (maxlen < sizeof(CTL_MSG)) {
		syslog(LOG_ERR, "QUIRK_OTALK: maxlen too small; enlarge "
		       "inbuf[] in talkd.c and recompile\n");
		return -1;
	}

	m.vers = TALK_VERSION;
	m.type = otp.type;
	m.answer = 0;
	m.pad = 0;
	m.id_num = otp.id_num;
	m.addr = otp.addr;
	m.ctl_addr = otp.ctl_addr;
	m.pid = otp.pid;
	memcpy(m.l_name, otp.l_name, ONAME_SIZE);
	m.l_name[ONAME_SIZE-1] = 0;
	memcpy(m.r_name, otp.r_name, ONAME_SIZE);
	m.r_name[ONAME_SIZE-1] = 0;
	memcpy(m.r_tty, otp.r_tty, TTY_SIZE);
	m.r_tty[TTY_SIZE-1] = 0;
	memcpy(buf, &m, sizeof(m));
	return 0;
}

static size_t do_otalk_reply(char *buf, size_t maxlen) {
	OCTL_RESPONSE or;
	struct CtlResponse *r = (struct CtlResponse *)buf;
	if (sizeof(or) > maxlen) {
		syslog(LOG_ERR, "QUIRK_OTALK: reply: maxlen too small; "
		       "enlarge buf[] in send_packet and recompile\n");
		return 0;	/* error */
	}
	
	/* 
	 * If we changed the type above, this might break. Should we encode
	 * it in the quirk code?
	 */
	or.type = r->type;
	or.answer = r->answer;
	or.filler = 0;
	or.id_num = r->id_num;
	memcpy(&or.addr, &r->addr, sizeof(or.addr));
	memcpy(buf, &or, sizeof(or));
	return sizeof(or);
}

/***************************************************/


/*
 * Return 0 if the packet's normal, -1 if we can't figure it out,
 * otherwise a quirk code from the quirk list.
 *
 * For now, we don't support any quirks. Need more data.
 */
int
rationalize_packet(char *buf, socklen_t len, socklen_t mlen, struct sockaddr *sa)
{
	if (len == sizeof(CTL_MSG)) {
		return 0;
	}

	if (sa->sa_family != AF_INET)
		return -1;

	debug("Malformed packet (length %u)\n", len);

	if (probe_otalk_packet(buf, len, mlen, sa)==0) {
		debug("Using QUIRK_OTALK\n");
		return QUIRK_OTALK;
	}
	return -1;
}

socklen_t
irrationalize_reply(char *buf, size_t maxlen, int quirk)
{
        switch (quirk) {
	    case QUIRK_NONE: return sizeof(CTL_RESPONSE);
	    case QUIRK_OTALK: return do_otalk_reply(buf, maxlen);
        }
	/* ? */
	return 0;
}
