#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <krb.h>


main()
{
int sock,rc;
long                authopts;
KTEXT_ST            ticket;
MSG_DAT             msg_data;
CREDENTIALS         cred;
Key_schedule        sched;
int status,cnamelen,snamelen;
struct sockaddr_in srv_addr, cl_addr;
char inbuf[BUFSIZ];

sock = inet_establish_connection("begonia","9002", 0);

   cnamelen = sizeof (cl_addr);
    if (getsockname (sock, (struct sockaddr *) & cl_addr, &cnamelen) < 0)
    {
        perror ("getsockname");
        close (sock);
        return;
    }

    /* find out who the other side is */

    snamelen = sizeof (srv_addr);
    if (getpeername (sock, (struct sockaddr *) & srv_addr, &snamelen) < 0)
    {
        perror ("getpeername");
        close (sock);
        return;
    }
/* use krb_get_pwintkt first */
    authopts = KOPT_DO_MUTUAL;
    status = krb_sendauth (authopts, sock, &ticket,
                               "rcmd", "BEGONIA",
                               NULL, (u_long) 0, &msg_data, &cred,
                               sched, &cl_addr, &srv_addr, "VER8");

printf("status = %d\n",status);
if (status != KSUCCESS)
  {
    /* get error text and return */
    exit();
  }
else 
  {                     
    unsigned char passwd[10];
    
/* send for sybase return code */
/* get return code */
/* if error fill in text and return  else.... */

    make_passwd(passwd,cred.session);
    printf("passwd = <%s>\n",passwd);
  }

rc = read(sock,inbuf,BUFSIZ);
if (rc > 0)
  printf("inbuf = <%s>\n",inbuf);

}

  /* create a temp passwd from the session key */
make_passwd(char *passwd, unsigned char *cp)
{   
int i;
    for ( i = 0; i < 8; i++,cp++)
      {
	passwd[i] = (char) ( (*cp % 58) + 65); /* get it between A and z */
	printf("cp = %d, passwd[i] = %c \n",*cp,passwd[i]);
	if (!isalpha(passwd[i]))  /* if its the punc between cases shift to 
				     the digits */
	    {
	      passwd[i] = passwd[i] - 40;
	    }
      }
    passwd[9] = '\0';
}






