DD_Access_cl.c
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
*-------------------- 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);
}
/*############################################################################*/