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