/* charon_prot.h -- This file contains protocol definitions for charon,
 * the kerberos extentions for secure serial operations.
 *
 * Created by:	Derek Atkins <warlord@MIT.EDU>
 *
 * $Source: /afs/net.mit.edu/user/warlord/Thesis/src/include/RCS/charon_prot.h,v $
 * $Author: warlord $
 *
 */

#include <warlord-copyright.h>

#ifndef _CHARON_PROT_H
#define _CHARON_PROT_H

#include <sys/types.h>
#include <netinet/in.h>
#include <krb.h>

/* A type byte is 8 bits of data, of the form:
 *	llttttuu
 *
 * where:
 *	ll is an encoding of the length of the length.
 *	ll == 0 : Llength = 1
 *	   == 1 :         = 2
 *	   == 2 :         = 4
 * 	   == 3 :         = the next byte
 *
 *	tttt is the packet type. 
 *
 *	uu is unused
 */

/* This will return the value of the "ll" field in the packet type
 * byte.
 */
#define PKT_LLEN(a)	((a & 0xC0) >> 6)

/* This will return the Length of the length (Llength).
 * a return of zero means to read the next byte
 */
#define PKT_LLENGTH(a)	((PKT_LLEN(a) == 0) ? 1 : \
			 (PKT_LLEN(a) == 1) ? 2 : \
			 (PKT_LLEN(a) == 2) ? 4 : 0)

/* This will take a type byte and return a packet type */
#define PKT_TYPE(a)	((a&0x3C) >> 2)

/* This will take a packet type and a Length-of-length and create
 * a type byte.  If llen != 1, 2, or 4, then there might be a problem!
 */
#define TYPE_BYTE(llen, pkt) ((((llen == 1) ? 0 : \
				(llen == 2) ? 1 : \
				(llen == 4) ? 2 : 3) << 6) | (pkt << 2))

/* This is the length of an IA5 line */
#define LINE_SIZE	64

/* Define the different packet types, here.  Do not change any of
 * this without also changing the table in "table.h" in the library.
 * It depends upon these number for the dispatch tables to work.
 */
#define PKT_STATUS	0	/* status information */
#define PKT_CVERSION	1	/* client version */
#define PKT_SVERSION	2	/* server version */
#define PKT_INFO	3	/* information packet */
#define PKT_SKDC	4	/* server kerberos data */
#define PKT_CKDC	5	/* client kerberos data (for interrealm) */
#define PKT_CAUTH	6	/* client authentication data */

#define MAX_NUM_PKTS	16	/* There can only be 16 packet types */

/* This is a definition to know if I am the server or the client.
 * It is not set by anything, but you know which by whether the
 * application calls the client or server function.  Do not change
 * this, as the dispatch tables depend upon this.
 */
#define CLIENT_MODE	0	/* I am a client appliation */
#define SERVER_MODE	1	/* I am a server application */

/* This defines how high our internal versions can go. Basically, it
 * is the number of protocols we have defined. It is not the same
 * as the client/server version numbers passed across in the handshake.
 * Currently, that isn't being used, but I'm allowing it for future
 * expansion of protocols.
 */
#define MAX_VERSION 2		/* Max number of versions defined in table.h */
#define MIN_VERSION 2		/* Minimum version number supported */

/* These are useful definitions for things... */
#if defined(sun) && defined(sparc) && defined(POSIX)
				/* solaris likes these... */
#define BCLEAR(a) memset((char *)(a), 0, sizeof(a))
#define SBCLEAR(a) memset((char *)&(a), 0, sizeof(a))
#define ZERO(a,l) memset((char *)(a), 0, (l))
#define COPY(s,d,l) memcpy((char *)(d), (char *)(s), (l))
#define CMP(a,b,l) memcmp((char *)(a), (char *)(b), (l))
#else			/* These won't always work! (especially on solaris) */
#define BCLEAR(a) bzero((char *)(a), sizeof(a))
#define SBCLEAR(a) bzero((char *)&(a), sizeof(a))
#define ZERO(a,l) bzero((char *)(a), (l))
#define COPY(s,d,l) bcopy((char *)(s), (char *)(d), (l))
#define CMP(a,b,l) bcmp((char *)(a), (char *)(b), (l))
#endif
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))

/* The tickets to get */
#define RCMD	"rcmd"
#define KRBTGT	"krbtgt"

/* This is the temp directory, for where the temp tickets should go */
#define TMP_TKT_FILE	"/tmp/tkt_"

/* This is an internal structure that gets passed around inside
 * the library.  It contains a lot of information about state and
 * other interesting tidbits.
 */
typedef struct _charon_t {
  int		mode;			/* client or server */
  int		version;		/* What version is this? */
  int		(*key_proc)();		/* Procedure to grab password */
  u_int 	datalen;		/* length of that data packet */
  u_char 	*data;			/* A data packet */
  u_char	cookie[4];		/* this is a timestamp of the run */
  char		cli_encrypt;		/* Encrypt the session? */
  char		srv_encrypt;		/* Encrypt the session? */
  char		obtain_ticket;		/* Did we write a ticket to a file? */
  char 		principal[ANAME_SZ];	/* principal's name */
  char 		instance[INST_SZ];	/* instance name */
  char 		realm[REALM_SZ]; 	/* principal's realm */
  char 		hostinst[INST_SZ];	/* Host's kerberos name */
  char 		hostrealm[REALM_SZ];	/* Realm of host */
  des_cblock	*session;		/* Session Key (if we want it) */
  struct sockaddr_in	saddr;		/* Server's IP Address */
  KTEXT		tgt;			/* TGT ktext */
  KTEXT		tkt2;			/* Second ticket (service or shared) */
  KTEXT		rcmd;			/* rcmd ticket, once we have it */
} charon_t;

#endif /* _CHARON_PROT_H */
