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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* $Id: rk_rpc.c,v 1.4 1997/02/22 14:37:57 peter Exp $
* $Source: /home/ncvs/src/eBones/lib/librkinit/rk_rpc.c,v $
* $Author: peter $
*
* This file contains functions that are used for network communication.
* See the comment at the top of rk_lib.c for a description of the naming
* conventions used within the rkinit library.
*/
#if !defined(lint) && !defined(SABER) && !defined(LOCORE) && defined(RCS_HDRS)
static char *rcsid = "$Id: rk_rpc.c,v 1.4 1997/02/22 14:37:57 peter Exp $";
#endif /* lint || SABER || LOCORE || RCS_HDRS */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <rkinit.h>
#include <rkinit_err.h>
#include <rkinit_private.h>
extern int errno;
static int sock;
struct sockaddr_in saddr;
static char errbuf[BUFSIZ];
#ifdef __STDC__
int rki_send_packet(int sock, char type, u_int32_t length, const char *data)
#else
int rki_send_packet(sock, type, length, data)
int sock;
char type;
u_int32_t length;
const char *data;
#endif /* __STDC__ */
{
int len;
u_char *packet;
u_int32_t pkt_len;
u_int32_t net_pkt_len;
pkt_len = length + PKT_DATA;
if ((packet = (u_char *)calloc(pkt_len, sizeof(u_char))) == NULL) {
sprintf(errbuf, "rki_send_packet: failure allocating %d bytes",
pkt_len * sizeof(u_char));
rkinit_errmsg(errbuf);
return(RKINIT_MEMORY);
}
net_pkt_len = htonl(pkt_len);
packet[PKT_TYPE] = type;
bcopy((char *)&net_pkt_len, packet + PKT_LEN, sizeof(u_int32_t));
bcopy(data, packet + PKT_DATA, length);
if ((len = write(sock, packet, pkt_len)) != pkt_len) {
if (len == -1)
sprintf(errbuf, "write: %s", sys_errlist[errno]);
else
sprintf(errbuf, "write: %d bytes written; %d bytes actually sent",
pkt_len, len);
rkinit_errmsg(errbuf);
free(packet);
return(RKINIT_WRITE);
}
free(packet);
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
int rki_get_packet(int sock, u_char type, u_int32_t *length, char *data)
#else
int rki_get_packet(sock, type, length, data)
int sock;
u_char type;
u_int32_t *length;
char *data;
#endif /* __STDC__ */
{
int len;
int len_sofar = 0;
u_int32_t expected_length = 0;
int got_full_packet = FALSE;
int tries = 0;
u_char *packet;
u_int32_t max_pkt_len;
max_pkt_len = *length + PKT_DATA;
if ((packet = (u_char *)calloc(max_pkt_len, sizeof(u_char))) == NULL) {
sprintf(errbuf, "rki_get_packet: failure allocating %d bytes",
max_pkt_len * sizeof(u_char));
rkinit_errmsg(errbuf);
return(RKINIT_MEMORY);
}
/* Read the packet type and length */
while ((len_sofar < PKT_DATA) && (tries < RETRIES)) {
if ((len = read(sock, packet + len_sofar, PKT_DATA - len_sofar)) < 0) {
sprintf(errbuf, "read: %s", sys_errlist[errno]);
rkinit_errmsg(errbuf);
return(RKINIT_READ);
}
len_sofar += len;
tries++;
}
if (len_sofar < PKT_DATA) {
sprintf(errbuf,
"read: expected to receive at least %d bytes; received %d",
PKT_DATA, len_sofar);
rkinit_errmsg(errbuf);
return(RKINIT_PACKET);
}
bcopy(packet + PKT_LEN, (char *)&expected_length, sizeof(u_int32_t));
expected_length = ntohl(expected_length);
if (expected_length > max_pkt_len) {
sprintf(errbuf, "%s %d %s %d",
"rki_get_packet: incoming message of size",
expected_length,
"is larger than message buffer of size",
max_pkt_len);
rkinit_errmsg(errbuf);
return(RKINIT_PACKET);
}
tries = 0;
while (!got_full_packet && (tries < RETRIES)) {
if ((len = read(sock, packet + len_sofar,
expected_length - len_sofar)) < 0) {
sprintf(errbuf, "read: %s", sys_errlist[errno]);
rkinit_errmsg(errbuf);
return(RKINIT_READ);
}
len_sofar += len;
if (expected_length == len_sofar)
got_full_packet = TRUE;
}
if (len_sofar < expected_length) {
sprintf(errbuf,
"read: expected to receive at least %d bytes; received %d",
expected_length, len_sofar);
rkinit_errmsg(errbuf);
return(RKINIT_PACKET);
}
if (packet[PKT_TYPE] == MT_DROP) {
BCLEAR(errbuf);
rkinit_errmsg(errbuf);
return(RKINIT_DROPPED);
}
if (packet[PKT_TYPE] != type) {
sprintf(errbuf, "Expected packet type of %s; got %s",
rki_mt_to_string(type),
rki_mt_to_string(packet[PKT_TYPE]));
rkinit_errmsg(errbuf);
return(RKINIT_PACKET);
}
*length = len_sofar - PKT_DATA;
bcopy(packet + PKT_DATA, data, *length);
free(packet);
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
int rki_setup_rpc(char *host)
#else
int rki_setup_rpc(host)
char *host;
#endif /* __STDC__ */
{
struct hostent *hp;
struct servent *sp;
int port, retval;
SBCLEAR(saddr);
SBCLEAR(hp);
SBCLEAR(sp);
if ((hp = gethostbyname(host)) == NULL) {
sprintf(errbuf, "%s: unknown host.", host);
rkinit_errmsg(errbuf);
return(RKINIT_HOST);
}
if ((sp = getservbyname(SERVENT, "tcp")))
port = sp->s_port;
else
/* Fall back on known port number */
port = htons(PORT);
saddr.sin_family = hp->h_addrtype;
bcopy(hp->h_addr, (char *)&saddr.sin_addr, hp->h_length);
saddr.sin_port = port;
if ((sock = socket(hp->h_addrtype, SOCK_STREAM, IPPROTO_IP)) < 0) {
sprintf(errbuf, "socket: %s", sys_errlist[errno]);
rkinit_errmsg(errbuf);
return(RKINIT_SOCKET);
}
if ((retval = krb_bind_local_addr(sock)) != KSUCCESS) {
sprintf(errbuf, "krb_bind_local_addr: %s", krb_err_txt[retval]);
rkinit_errmsg(errbuf);
close(sock);
return(RKINIT_SOCKET);
}
if (connect(sock, (struct sockaddr *)&saddr, sizeof (saddr)) < 0) {
sprintf(errbuf, "connect: %s", sys_errlist[errno]);
rkinit_errmsg(errbuf);
close(sock);
return(RKINIT_CONNECT);
}
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
int rki_rpc_exchange_version_info(int c_lversion, int c_hversion,
int *s_lversion, int *s_hversion)
#else
int rki_rpc_exchange_version_info(c_lversion, c_hversion,
s_lversion, s_hversion)
int c_lversion;
int c_hversion;
int *s_lversion;
int *s_hversion;
#endif /* __STDC__ */
{
int status = RKINIT_SUCCESS;
u_char version_info[VERSION_INFO_SIZE];
u_int32_t length = sizeof(version_info);
version_info[0] = (u_char) c_lversion;
version_info[1] = (u_char) c_hversion;
if ((status = rki_send_packet(sock, MT_CVERSION, length,
(char *)version_info)) != RKINIT_SUCCESS)
return(status);
if ((status = rki_get_packet(sock, MT_SVERSION, &length,
(char *)version_info)) != RKINIT_SUCCESS)
return(status);
*s_lversion = (int) version_info[0];
*s_hversion = (int) version_info[1];
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
int rki_rpc_send_rkinit_info(rkinit_info *info)
#else
int rki_rpc_send_rkinit_info(info)
rkinit_info *info;
#endif /* __STDC__ */
{
rkinit_info info_copy;
bcopy(info, &info_copy, sizeof(rkinit_info));
info_copy.lifetime = htonl(info_copy.lifetime);
return(rki_send_packet(sock, MT_RKINIT_INFO, sizeof(rkinit_info),
(char *)&info_copy));
}
#ifdef __STDC__
int rki_rpc_get_status(void)
#else
int rki_rpc_get_status()
#endif /* __STDC__ */
{
char msg[BUFSIZ];
int status = RKINIT_SUCCESS;
u_int32_t length = sizeof(msg);
if ((status = rki_get_packet(sock, MT_STATUS, &length, msg)))
return(status);
if (length == 0)
return(RKINIT_SUCCESS);
else {
rkinit_errmsg(msg);
return(RKINIT_DAEMON);
}
}
#ifdef __STDC__
int rki_rpc_get_ktext(int sock, KTEXT auth, u_char type)
#else
int rki_rpc_get_ktext(sock, auth, type)
int sock;
KTEXT auth;
u_char type;
#endif /* __STDC__ */
{
int status = RKINIT_SUCCESS;
u_int32_t length = MAX_KTXT_LEN;
if ((status = rki_get_packet(sock, type, &length, (char *)auth->dat)))
return(status);
auth->length = length;
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
int rki_rpc_sendauth(KTEXT auth)
#else
int rki_rpc_sendauth(auth)
KTEXT auth;
#endif /* __STDC__ */
{
return(rki_send_packet(sock, MT_AUTH, auth->length, (char *)auth->dat));
}
#ifdef __STDC__
int rki_rpc_get_skdc(KTEXT scip)
#else
int rki_rpc_get_skdc(scip)
KTEXT scip;
#endif /* __STDC__ */
{
return(rki_rpc_get_ktext(sock, scip, MT_SKDC));
}
#ifdef __STDC__
int rki_rpc_send_ckdc(MSG_DAT *scip)
#else
int rki_rpc_send_ckdc(scip)
MSG_DAT *scip;
#endif /* __STDC__ */
{
return(rki_send_packet(sock, MT_CKDC, scip->app_length,
(char *)scip->app_data));
}
#ifdef __STDC__
int rki_get_csaddr(struct sockaddr_in *caddrp, struct sockaddr_in *saddrp)
#else
int rki_get_csaddr(caddrp, saddrp)
struct sockaddr_in *caddrp;
struct sockaddr_in *saddrp;
#endif /* __STDC__ */
{
int addrlen = sizeof(struct sockaddr_in);
bcopy((char *)&saddr, (char *)saddrp, addrlen);
if (getsockname(sock, (struct sockaddr *)caddrp, &addrlen) < 0) {
sprintf(errbuf, "getsockname: %s", sys_errlist[errno]);
rkinit_errmsg(errbuf);
return(RKINIT_GETSOCK);
}
return(RKINIT_SUCCESS);
}
#ifdef __STDC__
void rki_drop_server(void)
#else
void rki_drop_server()
#endif /* __STDC__ */
{
(void) rki_send_packet(sock, MT_DROP, 0, "");
}
#ifdef __STDC__
void rki_cleanup_rpc(void)
#else
void rki_cleanup_rpc()
#endif /* __STDC__ */
{
rki_drop_server();
(void) close(sock);
}
|