1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 1994 by OpenVision Technologies, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear in
9 * supporting documentation, and that the name of OpenVision not be used
10 * in advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. OpenVision makes no
12 * representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
22 */
23 /*
24 * Copyright (C) 2003, 2004 by the Massachusetts Institute of Technology.
25 * All rights reserved.
26 *
27 * Export of this software from the United States of America may
28 * require a specific license from the United States Government.
29 * It is the responsibility of any person or organization contemplating
30 * export to obtain such a license before exporting.
31 *
32 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
33 * distribute this software and its documentation for any purpose and
34 * without fee is hereby granted, provided that the above copyright
35 * notice appear in all copies and that both that copyright notice and
36 * this permission notice appear in supporting documentation, and that
37 * the name of M.I.T. not be used in advertising or publicity pertaining
38 * to distribution of the software without specific, written prior
39 * permission. Furthermore if you modify this software you must label
40 * your software as modified software and not distribute it in such a
41 * fashion that it might be confused with the original M.I.T. software.
42 * M.I.T. makes no representations about the suitability of
43 * this software for any purpose. It is provided "as is" without express
44 * or implied warranty.
45 */
46
47 #include <autoconf.h>
48
49 #include <stdio.h>
50 #ifdef _WIN32
51 #include <windows.h>
52 #include <winsock.h>
53 #else
54 #include <sys/types.h>
55 #include <netinet/in.h>
56 #include <sys/socket.h>
57 #endif
58 #include <errno.h>
59
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 #include <string.h>
64
65 /* need struct timeval */
66 #ifdef HAVE_SYS_TIME_H
67 #include <sys/time.h>
68 #else
69 #include <time.h>
70 #endif
71
72 #include <gssapi/gssapi_generic.h>
73 #include "gss-misc.h"
74
75 #ifdef HAVE_STDLIB_H
76 #include <stdlib.h>
77 #else
78 extern char *malloc();
79 #endif
80
81 FILE *display_file;
82
83 gss_buffer_desc empty_token_buf = { 0, (void *) "" };
84 gss_buffer_t empty_token = &empty_token_buf;
85
86 static void display_status_1(char *m, OM_uint32 code, int type);
87
88 static int
write_all(int fildes,const void * data,unsigned int nbyte)89 write_all(int fildes, const void *data, unsigned int nbyte)
90 {
91 int ret;
92 const char *ptr, *buf = data;
93
94 for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
95 ret = send(fildes, ptr, nbyte, 0);
96 if (ret < 0) {
97 if (errno == EINTR)
98 continue;
99 return (ret);
100 } else if (ret == 0) {
101 return (ptr - buf);
102 }
103 }
104
105 return (ptr - buf);
106 }
107
108 static int
read_all(int fildes,void * data,unsigned int nbyte)109 read_all(int fildes, void *data, unsigned int nbyte)
110 {
111 int ret;
112 char *ptr, *buf = data;
113 fd_set rfds;
114 struct timeval tv;
115
116 FD_ZERO(&rfds);
117 FD_SET(fildes, &rfds);
118 tv.tv_sec = 300;
119 tv.tv_usec = 0;
120
121 for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
122 if (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) <= 0
123 || !FD_ISSET(fildes, &rfds))
124 return (ptr - buf);
125 ret = recv(fildes, ptr, nbyte, 0);
126 if (ret < 0) {
127 if (errno == EINTR)
128 continue;
129 return (ret);
130 } else if (ret == 0) {
131 return (ptr - buf);
132 }
133 }
134
135 return (ptr - buf);
136 }
137
138 /*
139 * Function: send_token
140 *
141 * Purpose: Writes a token to a file descriptor.
142 *
143 * Arguments:
144 *
145 * s (r) an open file descriptor
146 * flags (r) the flags to write
147 * tok (r) the token to write
148 *
149 * Returns: 0 on success, -1 on failure
150 *
151 * Effects:
152 *
153 * If the flags are non-null, send_token writes the token flags (a
154 * single byte, even though they're passed in in an integer). Next,
155 * the token length (as a network long) and then the token data are
156 * written to the file descriptor s. It returns 0 on success, and -1
157 * if an error occurs or if it could not write all the data.
158 */
159 int
send_token(s,flags,tok)160 send_token(s, flags, tok)
161 int s;
162 int flags;
163 gss_buffer_t tok;
164 {
165 int ret;
166 unsigned char char_flags = (unsigned char) flags;
167 unsigned char lenbuf[4];
168
169 if (char_flags) {
170 ret = write_all(s, (char *) &char_flags, 1);
171 if (ret != 1) {
172 perror("sending token flags");
173 return -1;
174 }
175 }
176 if (tok->length > 0xffffffffUL)
177 abort();
178 lenbuf[0] = (tok->length >> 24) & 0xff;
179 lenbuf[1] = (tok->length >> 16) & 0xff;
180 lenbuf[2] = (tok->length >> 8) & 0xff;
181 lenbuf[3] = tok->length & 0xff;
182
183 ret = write_all(s, lenbuf, 4);
184 if (ret < 0) {
185 perror("sending token length");
186 return -1;
187 } else if (ret != 4) {
188 if (display_file)
189 fprintf(display_file,
190 "sending token length: %d of %d bytes written\n", ret, 4);
191 return -1;
192 }
193
194 ret = write_all(s, tok->value, tok->length);
195 if (ret < 0) {
196 perror("sending token data");
197 return -1;
198 } else if ((size_t)ret != tok->length) {
199 if (display_file)
200 fprintf(display_file,
201 "sending token data: %d of %d bytes written\n",
202 ret, (int) tok->length);
203 return -1;
204 }
205
206 return 0;
207 }
208
209 /*
210 * Function: recv_token
211 *
212 * Purpose: Reads a token from a file descriptor.
213 *
214 * Arguments:
215 *
216 * s (r) an open file descriptor
217 * flags (w) the read flags
218 * tok (w) the read token
219 *
220 * Returns: 0 on success, -1 on failure
221 *
222 * Effects:
223 *
224 * recv_token reads the token flags (a single byte, even though
225 * they're stored into an integer, then reads the token length (as a
226 * network long), allocates memory to hold the data, and then reads
227 * the token data from the file descriptor s. It blocks to read the
228 * length and data, if necessary. On a successful return, the token
229 * should be freed with gss_release_buffer. It returns 0 on success,
230 * and -1 if an error occurs or if it could not read all the data.
231 */
232 int
recv_token(s,flags,tok)233 recv_token(s, flags, tok)
234 int s;
235 int *flags;
236 gss_buffer_t tok;
237 {
238 int ret;
239 unsigned char char_flags;
240 unsigned char lenbuf[4];
241
242 ret = read_all(s, (char *) &char_flags, 1);
243 if (ret < 0) {
244 perror("reading token flags");
245 return -1;
246 } else if (!ret) {
247 if (display_file)
248 fputs("reading token flags: 0 bytes read\n", display_file);
249 return -1;
250 } else {
251 *flags = (int) char_flags;
252 }
253
254 if (char_flags == 0) {
255 lenbuf[0] = 0;
256 ret = read_all(s, &lenbuf[1], 3);
257 if (ret < 0) {
258 perror("reading token length");
259 return -1;
260 } else if (ret != 3) {
261 if (display_file)
262 fprintf(display_file,
263 "reading token length: %d of %d bytes read\n", ret, 3);
264 return -1;
265 }
266 } else {
267 ret = read_all(s, lenbuf, 4);
268 if (ret < 0) {
269 perror("reading token length");
270 return -1;
271 } else if (ret != 4) {
272 if (display_file)
273 fprintf(display_file,
274 "reading token length: %d of %d bytes read\n", ret, 4);
275 return -1;
276 }
277 }
278
279 tok->length = ((lenbuf[0] << 24)
280 | (lenbuf[1] << 16)
281 | (lenbuf[2] << 8)
282 | lenbuf[3]);
283 tok->value = (char *) malloc(tok->length ? tok->length : 1);
284 if (tok->length && tok->value == NULL) {
285 if (display_file)
286 fprintf(display_file, "Out of memory allocating token data\n");
287 return -1;
288 }
289
290 ret = read_all(s, (char *) tok->value, tok->length);
291 if (ret < 0) {
292 perror("reading token data");
293 free(tok->value);
294 return -1;
295 } else if ((size_t)ret != tok->length) {
296 fprintf(stderr, "sending token data: %d of %d bytes written\n",
297 ret, (int) tok->length);
298 free(tok->value);
299 return -1;
300 }
301
302 return 0;
303 }
304
305 static void
display_status_1(m,code,type)306 display_status_1(m, code, type)
307 char *m;
308 OM_uint32 code;
309 int type;
310 {
311 OM_uint32 min_stat;
312 gss_buffer_desc msg;
313 OM_uint32 msg_ctx;
314
315 msg_ctx = 0;
316 while (1) {
317 (void) gss_display_status(&min_stat, code, type, GSS_C_NULL_OID,
318 &msg_ctx, &msg);
319 if (display_file)
320 fprintf(display_file, "GSS-API error %s: %s\n", m,
321 (char *) msg.value);
322 (void) gss_release_buffer(&min_stat, &msg);
323
324 if (!msg_ctx)
325 break;
326 }
327 }
328
329 /*
330 * Function: display_status
331 *
332 * Purpose: displays GSS-API messages
333 *
334 * Arguments:
335 *
336 * msg a string to be displayed with the message
337 * maj_stat the GSS-API major status code
338 * min_stat the GSS-API minor status code
339 *
340 * Effects:
341 *
342 * The GSS-API messages associated with maj_stat and min_stat are
343 * displayed on stderr, each preceded by "GSS-API error <msg>: " and
344 * followed by a newline.
345 */
346 void
display_status(msg,maj_stat,min_stat)347 display_status(msg, maj_stat, min_stat)
348 char *msg;
349 OM_uint32 maj_stat;
350 OM_uint32 min_stat;
351 {
352 display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
353 display_status_1(msg, min_stat, GSS_C_MECH_CODE);
354 }
355
356 /*
357 * Function: display_ctx_flags
358 *
359 * Purpose: displays the flags returned by context initiation in
360 * a human-readable form
361 *
362 * Arguments:
363 *
364 * int ret_flags
365 *
366 * Effects:
367 *
368 * Strings corresponding to the context flags are printed on
369 * stdout, preceded by "context flag: " and followed by a newline
370 */
371
372 void
display_ctx_flags(flags)373 display_ctx_flags(flags)
374 OM_uint32 flags;
375 {
376 if (flags & GSS_C_DELEG_FLAG)
377 fprintf(display_file, "context flag: GSS_C_DELEG_FLAG\n");
378 if (flags & GSS_C_MUTUAL_FLAG)
379 fprintf(display_file, "context flag: GSS_C_MUTUAL_FLAG\n");
380 if (flags & GSS_C_REPLAY_FLAG)
381 fprintf(display_file, "context flag: GSS_C_REPLAY_FLAG\n");
382 if (flags & GSS_C_SEQUENCE_FLAG)
383 fprintf(display_file, "context flag: GSS_C_SEQUENCE_FLAG\n");
384 if (flags & GSS_C_CONF_FLAG)
385 fprintf(display_file, "context flag: GSS_C_CONF_FLAG \n");
386 if (flags & GSS_C_INTEG_FLAG)
387 fprintf(display_file, "context flag: GSS_C_INTEG_FLAG \n");
388 }
389
390 void
print_token(tok)391 print_token(tok)
392 gss_buffer_t tok;
393 {
394 unsigned int i;
395 unsigned char *p = tok->value;
396
397 if (!display_file)
398 return;
399 for (i = 0; i < tok->length; i++, p++) {
400 fprintf(display_file, "%02x ", *p);
401 if ((i % 16) == 15) {
402 fprintf(display_file, "\n");
403 }
404 }
405 fprintf(display_file, "\n");
406 fflush(display_file);
407 }
408
409 #ifdef _WIN32
410 #include <sys\timeb.h>
411 #include <time.h>
412
413 int
gettimeofday(struct timeval * tv,void * ignore_tz)414 gettimeofday(struct timeval *tv, void *ignore_tz)
415 {
416 struct _timeb tb;
417 _tzset();
418 _ftime(&tb);
419 if (tv) {
420 tv->tv_sec = tb.time;
421 tv->tv_usec = tb.millitm * 1000;
422 }
423 return 0;
424 }
425 #endif /* _WIN32 */
426