AcePerl
view release on metacpan or search on metacpan
acelib/aceclientlib.c view on Meta::CPAN
{ if (accessDebug)
printf ("// Read pass file: %s\n", cp) ;
if (strcmp(cp, "PUBLIC") && strcmp(cp,"RESTRICTED"))
{ f = magicFileOpen (cp) ;
if (!f)
{ messout ("// Access to this database is restricted, sorry (can't open pass file)\n") ;
goto fin ;
}
if (fscanf(f, "%d", &magic2) != 1)
messerror ("failed to read file") ;
fclose(f) ;
}
}
magic = magic1 ;
if (magic2)
magic = magic1 * magic2 % 73256171 ;
if (magic3)
magic = magic1 * magic3 % 43532334 ;
fin:
freeclose(level) ;
#ifdef DEEP_DEBUG
printf ("// magic1=%d, magic2=%d, magic3=%d, magic=%d\n",
magic1, magic2, magic3, magic) ;
#endif
return magic ;
}
/*************************************************************
Open RPC connection to server
INPUT
char *host hostname running server
int timeOut maximum peroid to wait for answer
OUTPUT
return value:
ace_handle * pointer to structure containing open connection
and client identification information
*/
ace_handle *openServer(char *host, u_long rpc_port, int timeOut)
{
struct timeval tv;
char *answer;
int length,
clientId = 0, n,
magic1, magic3 = 0 ;
ace_reponse *reponse = 0;
ace_data question ;
ace_handle *handle;
CLIENT *clnt;
/* open rpc connection */
/* lao: */
clnt = clnt_create (host, RPC_ACE, RPC_ACE_VERS, "tcp");
if (!clnt) return((ace_handle *)NULL);
/* authenticate */
question.clientId = 0;
question.magic = 0;
question.reponse.reponse_len = 0;
question.reponse.reponse_val = "";
question.question = "";
question.aceError = 0;
question.kBytes = 0;
question.encore = 0;
#ifdef JUNK
int first = 1 ;
/* kludge: on first connection to a daemon
the conection is lost, so i try to connect twice,
once with a short timeOut, then the real try
at least on a dec alpha, the first connection keeps hanging and the
inetd daemon keeps restrating the server for ever
the advantage of this kludge is that the first client connection no
longer fails, and it is otherwise harmless since the restarting
server happenned before i introduced this kludge
*/
if (first)
{ first = 0 ;
tv.tv_sec = 5 ;
tv.tv_usec = 0;
clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
reponse = ace_server_1(&question, clnt);
if (!reponse) /* i ll try a second time */
{ clnt_destroy(clnt); goto lao ; }
}
#endif
tv.tv_sec = timeOut;
tv.tv_usec = 0;
clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
if (!reponse) /* hopefully first connection worked */
reponse = ace_server_1(&question, clnt);
if (!reponse) return ((ace_handle *)NULL);
clientId = reponse->ace_reponse_u.res_data.clientId;
magic1 = reponse->ace_reponse_u.res_data.magic;
if (!clientId) {
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
clnt_destroy(clnt);
return 0 ;
}
if (reponse->ace_reponse_u.res_data.aceError) {
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
clnt_destroy(clnt);
return 0;
}
answer = reponse->ace_reponse_u.res_data.reponse.reponse_val;
length = reponse->ace_reponse_u.res_data.reponse.reponse_len;
if (answer && length) {
magic3 = getMagic(magic1, answer) ;
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
/* confirm magic by reaccessing client */
question.clientId = clientId ;
question.magic = magic3 ;
question.reponse.reponse_len = 0;
question.reponse.reponse_val = "";
question.question = "";
question.aceError = 0;
question.kBytes = 0;
question.encore = 0;
reponse = ace_server_1(&question, clnt);
if (!reponse) {
clnt_destroy(clnt);
return 0 ;
}
n = reponse->ace_reponse_u.res_data.clientId;
}
else
n = clientId + 1 ; /* so we fail */
if (reponse->ace_reponse_u.res_data.aceError) {
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
clnt_destroy(clnt);
return 0;
}
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
if (n != clientId) {
/* authentication failed */
clnt_destroy(clnt);
return 0 ;
}
/* create mem for handle */
if ((handle = (ace_handle *)malloc(sizeof(ace_handle))) == NULL) {
question.clientId = clientId ;
question.magic = magic3 ;
question.reponse.reponse_len = 0;
question.reponse.reponse_val = "";
question.question = "Quit";
question.aceError = 0;
question.kBytes = 0;
question.encore = 0;
reponse = ace_server_1(&question, clnt);
xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
clnt_destroy(clnt);
return 0 ;
}
handle->clientId = clientId;
handle->magic = magic3;
handle->clnt = clnt;
return handle ;
}
/*************************************************************
notify server of intent to close connection
close connection
free structures
INPUT
ace_handle * pointer to structure containing open connection
and client identification information
OUTPUT
none
*/
void closeServer(ace_handle *handle) {
ace_data question;
ace_reponse *reponse = 0;
if (handle) {
if ( (int *)handle && (CLIENT *)handle->clnt) {
/* JC not sure whether I should/need check (int *)handle */
question.clientId = handle->clientId ;
question.magic = handle->magic ;
question.reponse.reponse_len = 0;
question.reponse.reponse_val = "";
question.question = "Quit";
question.aceError = 0;
question.kBytes = 0;
question.encore = 0;
reponse = ace_server_1(&question, handle->clnt);
if (reponse)
{ xdr_free((xdrproc_t )xdr_ace_reponse, (char *)reponse);
memset (reponse,0, sizeof(ace_reponse)) ;
}
clnt_destroy((CLIENT *)handle->clnt);
}
( run in 1.012 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )