/** this is just some test code for a server which would run out of inetd */

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




/* included by 
#include <syblogin.h> 
#include <sybtypes.h>
#include <syberror.h>
#include <sybdbtoken.h>
*/

main()
{

 DBPROCESS       *dbproc;
 LOGINREC        *loginrec;
 int rc;
 char inbuf[BUFSIZ];
 int cnamelen, snamelen;
 int status;
 long authopts;
 AUTH_DAT auth_data;
 KTEXT_ST clt_ticket;
 Key_schedule sched;
 char instance[INST_SZ + 1];
 char version[KRB_SENDAUTH_VLEN + 1];
 char realm[REALM_SZ + 1];
 struct       sockaddr_in   srv_addr, cl_addr;
 
    cnamelen = sizeof(cl_addr);
    snamelen = sizeof(srv_addr);

 bzero(&clt_ticket, sizeof(clt_ticket));
 bzero(&auth_data, sizeof(auth_data));

    /*
     * To verify authenticity, we need to know the address of the
     * client.
     */

    if (getpeername(0, (struct sockaddr *)&cl_addr, &cnamelen) < 0)
        printf("getpeername failed\n");

    /* for mutual authentication, we need to know our address */

    if (getsockname(0, (struct sockaddr *)&srv_addr, &snamelen) < 0)
        printf("getsockname failed\n");

    /*
     * read the authenticator and decode it.
     */

    (void) strcpy(instance, "*");

    /* we want mutual authentication */
    /* regular kerberos auth first */

    authopts = KOPT_DO_MUTUAL;
    status = krb_recvauth(authopts, 0, &clt_ticket,"rcmd",
                          instance, &cl_addr, &srv_addr,
                          &auth_data,"",sched, version);

printf("status = %d\n",status);

/* authenticate & extract username */

/* create temp password based on session key */


/* read sybase adm password from file ? */

 
/* if any of these steps fail send an error code back */


 /* Initialize DB-Library. */
 if (dbinit() == FAIL)
   exit(ERREXIT);
 dbsetifile("/dbms/testing/sybase/interfaces");
 loginrec = dblogin();
 if (!(loginrec) || (loginrec == NULL))
   printf("failed to get loginrec\n");
 DBSETLUSER(loginrec,auth_data.pname);  /* this will be an admin user */
 DBSETLPWD(loginrec, "foo");    /*  put the admin password read from disk here*/
    
 dbproc = dbopen(loginrec, "RPSSRVRA"); /* my_server */
 if (!(dbproc) || (dbproc == NULL))
   printf("failed\n");
 else
   printf("It might have worked\n");
 
 /* we should be connected now */

 dbcmd(dbproc,"sp_password foo, null");  /* put temp password here */
 dbsqlexec(dbproc);
 rc = dbresults(dbproc);
printf("rc = %d\n",rc);
/* send return code back */
dbexit();
}

