DD_Access_cl.c 5.2 KB
/*
 *-------------------- DD_ACCESS -------------------------------
 *
 * name:         DD_Access.c
 * Version:      4.0
 * author:       Alexander Grigoriev, Andrei fedorov
 * date :        Oct 13, 2002
 * Description: Set of CLIENT function for access to DD system
 *
 * List of revisions:
 *        March    1999  V1.0
 *        20 Oct 1999 ; ShowTicket was corrected.   V2.0
 *        28 Oct 1999 ; Added LOGINS,ddcheck        V3.0
 *        Oct 13, 2002 - revised by A. Fedorov      V4.0
 *                       see technical doc
 */
#include <limits.h>
#include <unistd.h>
#include <time.h>
#include <DD.h>
#include <DD_comm.h>
#include <DD_Access.h>

/*=========== Global Variables ===============================*/
extern int  SocketID; /* The socket is defined in DD_client.c */
/*================= FUNCTIONS ================================*/

/*------------------------ LoginRequest -------------------------------*/
int LoginRequest(char *UserPasswd, char *DDUserName, char *Host)
{
   static caddr_t buf = NULL;
   static XDR xdrs;
   int cc,i, hostlen, ticketlen, userlen;
   int op = GETTICKETREQ;
   int ServerReply = NOPERMISSION;

/*------------------CONNECTNION to SERVER---------------------------*/
 
  /* If there is no connection, try to get it */
   if(SocketID < 0)
   if((SocketID = GetSocket()) < 0) return(NOCONNECTION);

/*---------------------REQUEST TO SERVER-----------------------------*/

   if(Host != NULL) /* It is WEB client (not local) */
   {
      if(strlen(Host) > MAXHOSTLENGTH-2) Host[MAXHOSTLENGTH-1] = '\0';
   }
   else Host = UserHost; /* Local client */
   hostlen =  strlen(Host);
   ticketlen = strlen(UserPasswd);
   userlen = strlen(DDUserName);

/* Allocation memory and stream */
   buf = (caddr_t)malloc(REQUESTLENGTH);
   xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);

/* Encoding the request */
   xdr_int(&xdrs,&op);

/* Send request */
   if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
   {
      perror("DD_GetTicket:");
      free(buf);
      xdr_destroy(&xdrs);
      return(REQSENDERR);
   }
  
   xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_FREE);
   buf = (caddr_t)realloc(buf,REQUESTLENGTH);

   xdrmem_create(&xdrs, buf,REQUESTLENGTH , XDR_ENCODE);
   xdr_int(&xdrs,&UserID);                     /* user's ID on the local host */
   xdr_string(&xdrs, &DDUserName, userlen);    /* DD_login name  */
   xdr_string(&xdrs, &UserPasswd, ticketlen);  /* user's DD-password */
   xdr_string(&xdrs, &Host, hostlen);      /* IP-address of the local host */
   
/* Send request */
   if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
   {
      perror("DD_GetTicket:");
      free(buf);
      xdr_destroy(&xdrs);
      return(REQSENDERR);
   }
   free(buf);
   xdr_destroy(&xdrs);
   
/* Get reply header */

   buf = (caddr_t)malloc(REPLYLENGTH);
   i =0;
   while(((cc = recv(SocketID,buf,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
   if(cc < 0)
   {
      perror("DD_GetTicket:");
      free(buf);
      return(REPLYRECERR);
   }

   xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
   xdr_int(&xdrs,&ServerReply);

   free(buf);
   xdr_destroy(&xdrs);
 
   shutdown(SocketID,2);
   close(SocketID);
   SocketID = -1;
   return ServerReply;
}

/*       
 *------------------------ SETUSER -------------------------------*/
/* 
 * Description: Library function for client-server using.  
 *              Send UserID, HostName to server.
 *              Compares the information with your {id,hostname}.
 *              Returns 1 if O'k, or a NOPERMISSION, or error number
 */
int SetUser(int UserIDLocal, char *Host, char *DDUserName)
{

   static caddr_t buf = NULL;
   static XDR xdrs;
   int cc,i, hostlen, ticketlen, userlen;
   int op = SHOWTICKETREQ;
   int ServerReply = NOPERMISSION;

/*------------------CONNECTION to SERVER---------------------------*/
   if(SocketID < 0)
       if((SocketID = GetSocket()) < 0) return(NOCONNECTION);

/*---------------------REQUEST TO SERVER-----------------------------*/

/* Check the HostName length */
   if(strlen(Host) > MAXHOSTLENGTH) hostlen = MAXHOSTLENGTH;
   else hostlen = strlen(Host);
   if(strlen(DDUserName) > USRLENGTH) userlen = USRLENGTH;
   else userlen = strlen(DDUserName);

/* Allocation memory and stream */
   buf = (caddr_t)malloc(REQUESTLENGTH);
   xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);

/* Encoding the request */
   xdr_int(&xdrs,&op);
   xdr_int(&xdrs,&UserID);
   xdr_string(&xdrs, &Host, hostlen);
   xdr_string(&xdrs, &DDUserName, userlen);
/* printf("UserID, HostName  just more : %d %s\n",UserID,HostName);*/
/* Send request */
   if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
   {
      perror("DD_GetTicket:");
      free(buf);
      xdr_destroy(&xdrs);
      return(REQSENDERR);
   }
   free(buf);
   xdr_destroy(&xdrs);

/* Get reply header */

   buf = (caddr_t)malloc(REPLYLENGTH);
   i =0;
   while(((cc = recv(SocketID,buf,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
   if(cc < 0)
   {
      perror("DD_SetUser:");
      free(buf);
      return(REPLYRECERR);
   }

   xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
   xdr_int(&xdrs,&ServerReply);

   free(buf);
   xdr_destroy(&xdrs);
   shutdown(SocketID,2);
   close(SocketID);
   return(ServerReply);

   
}
/*############################################################################*/