xref: /freebsd/crypto/krb5/src/appl/gss-sample/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 
49*7f2fe78bSCy Schubert #include <stdio.h>
50*7f2fe78bSCy Schubert #ifdef _WIN32
51*7f2fe78bSCy Schubert #include <windows.h>
52*7f2fe78bSCy Schubert #include <winsock.h>
53*7f2fe78bSCy Schubert #else
54*7f2fe78bSCy Schubert #include <sys/types.h>
55*7f2fe78bSCy Schubert #include <netinet/in.h>
56*7f2fe78bSCy Schubert #include <sys/socket.h>
57*7f2fe78bSCy Schubert #endif
58*7f2fe78bSCy Schubert #include <errno.h>
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert #ifdef HAVE_UNISTD_H
61*7f2fe78bSCy Schubert #include <unistd.h>
62*7f2fe78bSCy Schubert #endif
63*7f2fe78bSCy Schubert #include <string.h>
64*7f2fe78bSCy Schubert 
65*7f2fe78bSCy Schubert /* need struct timeval */
66*7f2fe78bSCy Schubert #ifdef HAVE_SYS_TIME_H
67*7f2fe78bSCy Schubert #include <sys/time.h>
68*7f2fe78bSCy Schubert #else
69*7f2fe78bSCy Schubert #include <time.h>
70*7f2fe78bSCy Schubert #endif
71*7f2fe78bSCy Schubert 
72*7f2fe78bSCy Schubert #include <gssapi/gssapi_generic.h>
73*7f2fe78bSCy Schubert #include "gss-misc.h"
74*7f2fe78bSCy Schubert 
75*7f2fe78bSCy Schubert #ifdef HAVE_STDLIB_H
76*7f2fe78bSCy Schubert #include <stdlib.h>
77*7f2fe78bSCy Schubert #else
78*7f2fe78bSCy Schubert extern char *malloc();
79*7f2fe78bSCy Schubert #endif
80*7f2fe78bSCy Schubert 
81*7f2fe78bSCy Schubert FILE   *display_file;
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert gss_buffer_desc empty_token_buf = { 0, (void *) "" };
84*7f2fe78bSCy Schubert gss_buffer_t empty_token = &empty_token_buf;
85*7f2fe78bSCy Schubert 
86*7f2fe78bSCy Schubert static void display_status_1(char *m, OM_uint32 code, int type);
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert static int
write_all(int fildes,const void * data,unsigned int nbyte)89*7f2fe78bSCy Schubert write_all(int fildes, const void *data, unsigned int nbyte)
90*7f2fe78bSCy Schubert {
91*7f2fe78bSCy Schubert     int ret;
92*7f2fe78bSCy Schubert     const char *ptr, *buf = data;
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert     for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
95*7f2fe78bSCy Schubert         ret = send(fildes, ptr, nbyte, 0);
96*7f2fe78bSCy Schubert         if (ret < 0) {
97*7f2fe78bSCy Schubert             if (errno == EINTR)
98*7f2fe78bSCy Schubert                 continue;
99*7f2fe78bSCy Schubert             return (ret);
100*7f2fe78bSCy Schubert         } else if (ret == 0) {
101*7f2fe78bSCy Schubert             return (ptr - buf);
102*7f2fe78bSCy Schubert         }
103*7f2fe78bSCy Schubert     }
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert     return (ptr - buf);
106*7f2fe78bSCy Schubert }
107*7f2fe78bSCy Schubert 
108*7f2fe78bSCy Schubert static int
read_all(int fildes,void * data,unsigned int nbyte)109*7f2fe78bSCy Schubert read_all(int fildes, void *data, unsigned int nbyte)
110*7f2fe78bSCy Schubert {
111*7f2fe78bSCy Schubert     int     ret;
112*7f2fe78bSCy Schubert     char   *ptr, *buf = data;
113*7f2fe78bSCy Schubert     fd_set  rfds;
114*7f2fe78bSCy Schubert     struct timeval tv;
115*7f2fe78bSCy Schubert 
116*7f2fe78bSCy Schubert     FD_ZERO(&rfds);
117*7f2fe78bSCy Schubert     FD_SET(fildes, &rfds);
118*7f2fe78bSCy Schubert     tv.tv_sec = 300;
119*7f2fe78bSCy Schubert     tv.tv_usec = 0;
120*7f2fe78bSCy Schubert 
121*7f2fe78bSCy Schubert     for (ptr = buf; nbyte; ptr += ret, nbyte -= ret) {
122*7f2fe78bSCy Schubert         if (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) <= 0
123*7f2fe78bSCy Schubert             || !FD_ISSET(fildes, &rfds))
124*7f2fe78bSCy Schubert             return (ptr - buf);
125*7f2fe78bSCy Schubert         ret = recv(fildes, ptr, nbyte, 0);
126*7f2fe78bSCy Schubert         if (ret < 0) {
127*7f2fe78bSCy Schubert             if (errno == EINTR)
128*7f2fe78bSCy Schubert                 continue;
129*7f2fe78bSCy Schubert             return (ret);
130*7f2fe78bSCy Schubert         } else if (ret == 0) {
131*7f2fe78bSCy Schubert             return (ptr - buf);
132*7f2fe78bSCy Schubert         }
133*7f2fe78bSCy Schubert     }
134*7f2fe78bSCy Schubert 
135*7f2fe78bSCy Schubert     return (ptr - buf);
136*7f2fe78bSCy Schubert }
137*7f2fe78bSCy Schubert 
138*7f2fe78bSCy Schubert /*
139*7f2fe78bSCy Schubert  * Function: send_token
140*7f2fe78bSCy Schubert  *
141*7f2fe78bSCy Schubert  * Purpose: Writes a token to a file descriptor.
142*7f2fe78bSCy Schubert  *
143*7f2fe78bSCy Schubert  * Arguments:
144*7f2fe78bSCy Schubert  *
145*7f2fe78bSCy Schubert  *      s               (r) an open file descriptor
146*7f2fe78bSCy Schubert  *      flags           (r) the flags to write
147*7f2fe78bSCy Schubert  *      tok             (r) the token to write
148*7f2fe78bSCy Schubert  *
149*7f2fe78bSCy Schubert  * Returns: 0 on success, -1 on failure
150*7f2fe78bSCy Schubert  *
151*7f2fe78bSCy Schubert  * Effects:
152*7f2fe78bSCy Schubert  *
153*7f2fe78bSCy Schubert  * If the flags are non-null, send_token writes the token flags (a
154*7f2fe78bSCy Schubert  * single byte, even though they're passed in in an integer). Next,
155*7f2fe78bSCy Schubert  * the token length (as a network long) and then the token data are
156*7f2fe78bSCy Schubert  * written to the file descriptor s.  It returns 0 on success, and -1
157*7f2fe78bSCy Schubert  * if an error occurs or if it could not write all the data.
158*7f2fe78bSCy Schubert  */
159*7f2fe78bSCy Schubert int
send_token(s,flags,tok)160*7f2fe78bSCy Schubert send_token(s, flags, tok)
161*7f2fe78bSCy Schubert     int     s;
162*7f2fe78bSCy Schubert     int     flags;
163*7f2fe78bSCy Schubert     gss_buffer_t tok;
164*7f2fe78bSCy Schubert {
165*7f2fe78bSCy Schubert     int     ret;
166*7f2fe78bSCy Schubert     unsigned char char_flags = (unsigned char) flags;
167*7f2fe78bSCy Schubert     unsigned char lenbuf[4];
168*7f2fe78bSCy Schubert 
169*7f2fe78bSCy Schubert     if (char_flags) {
170*7f2fe78bSCy Schubert         ret = write_all(s, (char *) &char_flags, 1);
171*7f2fe78bSCy Schubert         if (ret != 1) {
172*7f2fe78bSCy Schubert             perror("sending token flags");
173*7f2fe78bSCy Schubert             return -1;
174*7f2fe78bSCy Schubert         }
175*7f2fe78bSCy Schubert     }
176*7f2fe78bSCy Schubert     if (tok->length > 0xffffffffUL)
177*7f2fe78bSCy Schubert         abort();
178*7f2fe78bSCy Schubert     lenbuf[0] = (tok->length >> 24) & 0xff;
179*7f2fe78bSCy Schubert     lenbuf[1] = (tok->length >> 16) & 0xff;
180*7f2fe78bSCy Schubert     lenbuf[2] = (tok->length >> 8) & 0xff;
181*7f2fe78bSCy Schubert     lenbuf[3] = tok->length & 0xff;
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     ret = write_all(s, lenbuf, 4);
184*7f2fe78bSCy Schubert     if (ret < 0) {
185*7f2fe78bSCy Schubert         perror("sending token length");
186*7f2fe78bSCy Schubert         return -1;
187*7f2fe78bSCy Schubert     } else if (ret != 4) {
188*7f2fe78bSCy Schubert         if (display_file)
189*7f2fe78bSCy Schubert             fprintf(display_file,
190*7f2fe78bSCy Schubert                     "sending token length: %d of %d bytes written\n", ret, 4);
191*7f2fe78bSCy Schubert         return -1;
192*7f2fe78bSCy Schubert     }
193*7f2fe78bSCy Schubert 
194*7f2fe78bSCy Schubert     ret = write_all(s, tok->value, tok->length);
195*7f2fe78bSCy Schubert     if (ret < 0) {
196*7f2fe78bSCy Schubert         perror("sending token data");
197*7f2fe78bSCy Schubert         return -1;
198*7f2fe78bSCy Schubert     } else if ((size_t)ret != tok->length) {
199*7f2fe78bSCy Schubert         if (display_file)
200*7f2fe78bSCy Schubert             fprintf(display_file,
201*7f2fe78bSCy Schubert                     "sending token data: %d of %d bytes written\n",
202*7f2fe78bSCy Schubert                     ret, (int) tok->length);
203*7f2fe78bSCy Schubert         return -1;
204*7f2fe78bSCy Schubert     }
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert     return 0;
207*7f2fe78bSCy Schubert }
208*7f2fe78bSCy Schubert 
209*7f2fe78bSCy Schubert /*
210*7f2fe78bSCy Schubert  * Function: recv_token
211*7f2fe78bSCy Schubert  *
212*7f2fe78bSCy Schubert  * Purpose: Reads a token from a file descriptor.
213*7f2fe78bSCy Schubert  *
214*7f2fe78bSCy Schubert  * Arguments:
215*7f2fe78bSCy Schubert  *
216*7f2fe78bSCy Schubert  *      s               (r) an open file descriptor
217*7f2fe78bSCy Schubert  *      flags           (w) the read flags
218*7f2fe78bSCy Schubert  *      tok             (w) the read token
219*7f2fe78bSCy Schubert  *
220*7f2fe78bSCy Schubert  * Returns: 0 on success, -1 on failure
221*7f2fe78bSCy Schubert  *
222*7f2fe78bSCy Schubert  * Effects:
223*7f2fe78bSCy Schubert  *
224*7f2fe78bSCy Schubert  * recv_token reads the token flags (a single byte, even though
225*7f2fe78bSCy Schubert  * they're stored into an integer, then reads the token length (as a
226*7f2fe78bSCy Schubert  * network long), allocates memory to hold the data, and then reads
227*7f2fe78bSCy Schubert  * the token data from the file descriptor s.  It blocks to read the
228*7f2fe78bSCy Schubert  * length and data, if necessary.  On a successful return, the token
229*7f2fe78bSCy Schubert  * should be freed with gss_release_buffer.  It returns 0 on success,
230*7f2fe78bSCy Schubert  * and -1 if an error occurs or if it could not read all the data.
231*7f2fe78bSCy Schubert  */
232*7f2fe78bSCy Schubert int
recv_token(s,flags,tok)233*7f2fe78bSCy Schubert recv_token(s, flags, tok)
234*7f2fe78bSCy Schubert     int     s;
235*7f2fe78bSCy Schubert     int    *flags;
236*7f2fe78bSCy Schubert     gss_buffer_t tok;
237*7f2fe78bSCy Schubert {
238*7f2fe78bSCy Schubert     int     ret;
239*7f2fe78bSCy Schubert     unsigned char char_flags;
240*7f2fe78bSCy Schubert     unsigned char lenbuf[4];
241*7f2fe78bSCy Schubert 
242*7f2fe78bSCy Schubert     ret = read_all(s, (char *) &char_flags, 1);
243*7f2fe78bSCy Schubert     if (ret < 0) {
244*7f2fe78bSCy Schubert         perror("reading token flags");
245*7f2fe78bSCy Schubert         return -1;
246*7f2fe78bSCy Schubert     } else if (!ret) {
247*7f2fe78bSCy Schubert         if (display_file)
248*7f2fe78bSCy Schubert             fputs("reading token flags: 0 bytes read\n", display_file);
249*7f2fe78bSCy Schubert         return -1;
250*7f2fe78bSCy Schubert     } else {
251*7f2fe78bSCy Schubert         *flags = (int) char_flags;
252*7f2fe78bSCy Schubert     }
253*7f2fe78bSCy Schubert 
254*7f2fe78bSCy Schubert     if (char_flags == 0) {
255*7f2fe78bSCy Schubert         lenbuf[0] = 0;
256*7f2fe78bSCy Schubert         ret = read_all(s, &lenbuf[1], 3);
257*7f2fe78bSCy Schubert         if (ret < 0) {
258*7f2fe78bSCy Schubert             perror("reading token length");
259*7f2fe78bSCy Schubert             return -1;
260*7f2fe78bSCy Schubert         } else if (ret != 3) {
261*7f2fe78bSCy Schubert             if (display_file)
262*7f2fe78bSCy Schubert                 fprintf(display_file,
263*7f2fe78bSCy Schubert                         "reading token length: %d of %d bytes read\n", ret, 3);
264*7f2fe78bSCy Schubert             return -1;
265*7f2fe78bSCy Schubert         }
266*7f2fe78bSCy Schubert     } else {
267*7f2fe78bSCy Schubert         ret = read_all(s, lenbuf, 4);
268*7f2fe78bSCy Schubert         if (ret < 0) {
269*7f2fe78bSCy Schubert             perror("reading token length");
270*7f2fe78bSCy Schubert             return -1;
271*7f2fe78bSCy Schubert         } else if (ret != 4) {
272*7f2fe78bSCy Schubert             if (display_file)
273*7f2fe78bSCy Schubert                 fprintf(display_file,
274*7f2fe78bSCy Schubert                         "reading token length: %d of %d bytes read\n", ret, 4);
275*7f2fe78bSCy Schubert             return -1;
276*7f2fe78bSCy Schubert         }
277*7f2fe78bSCy Schubert     }
278*7f2fe78bSCy Schubert 
279*7f2fe78bSCy Schubert     tok->length = ((lenbuf[0] << 24)
280*7f2fe78bSCy Schubert                    | (lenbuf[1] << 16)
281*7f2fe78bSCy Schubert                    | (lenbuf[2] << 8)
282*7f2fe78bSCy Schubert                    | lenbuf[3]);
283*7f2fe78bSCy Schubert     tok->value = (char *) malloc(tok->length ? tok->length : 1);
284*7f2fe78bSCy Schubert     if (tok->length && tok->value == NULL) {
285*7f2fe78bSCy Schubert         if (display_file)
286*7f2fe78bSCy Schubert             fprintf(display_file, "Out of memory allocating token data\n");
287*7f2fe78bSCy Schubert         return -1;
288*7f2fe78bSCy Schubert     }
289*7f2fe78bSCy Schubert 
290*7f2fe78bSCy Schubert     ret = read_all(s, (char *) tok->value, tok->length);
291*7f2fe78bSCy Schubert     if (ret < 0) {
292*7f2fe78bSCy Schubert         perror("reading token data");
293*7f2fe78bSCy Schubert         free(tok->value);
294*7f2fe78bSCy Schubert         return -1;
295*7f2fe78bSCy Schubert     } else if ((size_t)ret != tok->length) {
296*7f2fe78bSCy Schubert         fprintf(stderr, "sending token data: %d of %d bytes written\n",
297*7f2fe78bSCy Schubert                 ret, (int) tok->length);
298*7f2fe78bSCy Schubert         free(tok->value);
299*7f2fe78bSCy Schubert         return -1;
300*7f2fe78bSCy Schubert     }
301*7f2fe78bSCy Schubert 
302*7f2fe78bSCy Schubert     return 0;
303*7f2fe78bSCy Schubert }
304*7f2fe78bSCy Schubert 
305*7f2fe78bSCy Schubert static void
display_status_1(m,code,type)306*7f2fe78bSCy Schubert display_status_1(m, code, type)
307*7f2fe78bSCy Schubert     char   *m;
308*7f2fe78bSCy Schubert     OM_uint32 code;
309*7f2fe78bSCy Schubert     int     type;
310*7f2fe78bSCy Schubert {
311*7f2fe78bSCy Schubert     OM_uint32 min_stat;
312*7f2fe78bSCy Schubert     gss_buffer_desc msg;
313*7f2fe78bSCy Schubert     OM_uint32 msg_ctx;
314*7f2fe78bSCy Schubert 
315*7f2fe78bSCy Schubert     msg_ctx = 0;
316*7f2fe78bSCy Schubert     while (1) {
317*7f2fe78bSCy Schubert         (void) gss_display_status(&min_stat, code, type, GSS_C_NULL_OID,
318*7f2fe78bSCy Schubert                                   &msg_ctx, &msg);
319*7f2fe78bSCy Schubert         if (display_file)
320*7f2fe78bSCy Schubert             fprintf(display_file, "GSS-API error %s: %s\n", m,
321*7f2fe78bSCy Schubert                     (char *) msg.value);
322*7f2fe78bSCy Schubert         (void) gss_release_buffer(&min_stat, &msg);
323*7f2fe78bSCy Schubert 
324*7f2fe78bSCy Schubert         if (!msg_ctx)
325*7f2fe78bSCy Schubert             break;
326*7f2fe78bSCy Schubert     }
327*7f2fe78bSCy Schubert }
328*7f2fe78bSCy Schubert 
329*7f2fe78bSCy Schubert /*
330*7f2fe78bSCy Schubert  * Function: display_status
331*7f2fe78bSCy Schubert  *
332*7f2fe78bSCy Schubert  * Purpose: displays GSS-API messages
333*7f2fe78bSCy Schubert  *
334*7f2fe78bSCy Schubert  * Arguments:
335*7f2fe78bSCy Schubert  *
336*7f2fe78bSCy Schubert  *      msg             a string to be displayed with the message
337*7f2fe78bSCy Schubert  *      maj_stat        the GSS-API major status code
338*7f2fe78bSCy Schubert  *      min_stat        the GSS-API minor status code
339*7f2fe78bSCy Schubert  *
340*7f2fe78bSCy Schubert  * Effects:
341*7f2fe78bSCy Schubert  *
342*7f2fe78bSCy Schubert  * The GSS-API messages associated with maj_stat and min_stat are
343*7f2fe78bSCy Schubert  * displayed on stderr, each preceded by "GSS-API error <msg>: " and
344*7f2fe78bSCy Schubert  * followed by a newline.
345*7f2fe78bSCy Schubert  */
346*7f2fe78bSCy Schubert void
display_status(msg,maj_stat,min_stat)347*7f2fe78bSCy Schubert display_status(msg, maj_stat, min_stat)
348*7f2fe78bSCy Schubert     char   *msg;
349*7f2fe78bSCy Schubert     OM_uint32 maj_stat;
350*7f2fe78bSCy Schubert     OM_uint32 min_stat;
351*7f2fe78bSCy Schubert {
352*7f2fe78bSCy Schubert     display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
353*7f2fe78bSCy Schubert     display_status_1(msg, min_stat, GSS_C_MECH_CODE);
354*7f2fe78bSCy Schubert }
355*7f2fe78bSCy Schubert 
356*7f2fe78bSCy Schubert /*
357*7f2fe78bSCy Schubert  * Function: display_ctx_flags
358*7f2fe78bSCy Schubert  *
359*7f2fe78bSCy Schubert  * Purpose: displays the flags returned by context initiation in
360*7f2fe78bSCy Schubert  *          a human-readable form
361*7f2fe78bSCy Schubert  *
362*7f2fe78bSCy Schubert  * Arguments:
363*7f2fe78bSCy Schubert  *
364*7f2fe78bSCy Schubert  *      int             ret_flags
365*7f2fe78bSCy Schubert  *
366*7f2fe78bSCy Schubert  * Effects:
367*7f2fe78bSCy Schubert  *
368*7f2fe78bSCy Schubert  * Strings corresponding to the context flags are printed on
369*7f2fe78bSCy Schubert  * stdout, preceded by "context flag: " and followed by a newline
370*7f2fe78bSCy Schubert  */
371*7f2fe78bSCy Schubert 
372*7f2fe78bSCy Schubert void
display_ctx_flags(flags)373*7f2fe78bSCy Schubert display_ctx_flags(flags)
374*7f2fe78bSCy Schubert     OM_uint32 flags;
375*7f2fe78bSCy Schubert {
376*7f2fe78bSCy Schubert     if (flags & GSS_C_DELEG_FLAG)
377*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_DELEG_FLAG\n");
378*7f2fe78bSCy Schubert     if (flags & GSS_C_MUTUAL_FLAG)
379*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_MUTUAL_FLAG\n");
380*7f2fe78bSCy Schubert     if (flags & GSS_C_REPLAY_FLAG)
381*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_REPLAY_FLAG\n");
382*7f2fe78bSCy Schubert     if (flags & GSS_C_SEQUENCE_FLAG)
383*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_SEQUENCE_FLAG\n");
384*7f2fe78bSCy Schubert     if (flags & GSS_C_CONF_FLAG)
385*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_CONF_FLAG \n");
386*7f2fe78bSCy Schubert     if (flags & GSS_C_INTEG_FLAG)
387*7f2fe78bSCy Schubert         fprintf(display_file, "context flag: GSS_C_INTEG_FLAG \n");
388*7f2fe78bSCy Schubert }
389*7f2fe78bSCy Schubert 
390*7f2fe78bSCy Schubert void
print_token(tok)391*7f2fe78bSCy Schubert print_token(tok)
392*7f2fe78bSCy Schubert     gss_buffer_t tok;
393*7f2fe78bSCy Schubert {
394*7f2fe78bSCy Schubert     unsigned int   i;
395*7f2fe78bSCy Schubert     unsigned char *p = tok->value;
396*7f2fe78bSCy Schubert 
397*7f2fe78bSCy Schubert     if (!display_file)
398*7f2fe78bSCy Schubert         return;
399*7f2fe78bSCy Schubert     for (i = 0; i < tok->length; i++, p++) {
400*7f2fe78bSCy Schubert         fprintf(display_file, "%02x ", *p);
401*7f2fe78bSCy Schubert         if ((i % 16) == 15) {
402*7f2fe78bSCy Schubert             fprintf(display_file, "\n");
403*7f2fe78bSCy Schubert         }
404*7f2fe78bSCy Schubert     }
405*7f2fe78bSCy Schubert     fprintf(display_file, "\n");
406*7f2fe78bSCy Schubert     fflush(display_file);
407*7f2fe78bSCy Schubert }
408*7f2fe78bSCy Schubert 
409*7f2fe78bSCy Schubert #ifdef _WIN32
410*7f2fe78bSCy Schubert #include <sys\timeb.h>
411*7f2fe78bSCy Schubert #include <time.h>
412*7f2fe78bSCy Schubert 
413*7f2fe78bSCy Schubert int
gettimeofday(struct timeval * tv,void * ignore_tz)414*7f2fe78bSCy Schubert gettimeofday(struct timeval *tv, void *ignore_tz)
415*7f2fe78bSCy Schubert {
416*7f2fe78bSCy Schubert     struct _timeb tb;
417*7f2fe78bSCy Schubert     _tzset();
418*7f2fe78bSCy Schubert     _ftime(&tb);
419*7f2fe78bSCy Schubert     if (tv) {
420*7f2fe78bSCy Schubert         tv->tv_sec = tb.time;
421*7f2fe78bSCy Schubert         tv->tv_usec = tb.millitm * 1000;
422*7f2fe78bSCy Schubert     }
423*7f2fe78bSCy Schubert     return 0;
424*7f2fe78bSCy Schubert }
425*7f2fe78bSCy Schubert #endif /* _WIN32 */
426