summaryrefslogtreecommitdiffstats
path: root/src/windows/gss/gss-misc.c
blob: 8cc27c22cb9e9b7917746a13e0894eb31e1304b7 (plain)
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
/*
 * Copyright 1994 by OpenVision Technologies, Inc.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of OpenVision not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. OpenVision makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
#include "gss.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

/*+
 * Function: send_token
 *
 * Purpose: Writes a token to a file descriptor.
 *
 * Arguments:
 *
 *	s		(r) an open file descriptor
 *	tok		(r) the token to write
 *
 * Returns: 0 on success, -1 on failure
 *
 * Effects:
 *
 * send_token writes the token length (as a network long) and then the
 * token data to the file descriptor s.	 It returns 0 on success, and
 * -1 if an error occurs or if it could not write all the data.
 */
int send_token(int s, gss_buffer_t tok) {
    long len;
    size_t ret;

    len = htonl(tok->length);

    ret = send (s, (char *) &len, 4, 0);        // Send length over the socket
    if (ret < 0) {
		errno = WSAGetLastError();
		my_perror("sending token length");
        return -1;
    } else if (ret != 4) {
        OkMsgBox ("sending token length: %d of %d bytes written\n",
            ret, 4);
        return -1;
    }

    ret = send (s, tok->value, tok->length, 0); // Send the data
    if (ret < 0) {
		errno = WSAGetLastError();
        my_perror("sending token data");
        return -1;
    } else if (ret != tok->length) {
        OkMsgBox ("sending token data: %d of %d bytes written\n",
            ret, tok->length);
        return -1;
    }

    return 0;
}

/*+
 * Function: recv_token
 *
 * Purpose: Reads a token from a file descriptor.
 *
 * Arguments:
 *
 *	s		(r) an open file descriptor
 *	tok		(w) the read token
 *
 * Returns: 0 on success, -1 on failure
 *
 * Effects:
 * 
 * recv_token reads the token length (as a network long), allocates
 * memory to hold the data, and then reads the token data from the
 * file descriptor s.  It blocks to read the length and data, if
 * necessary.  On a successful return, the token should be freed with
 * gss_release_buffer.	It returns 0 on success, and -1 if an error
 * occurs or if it could not read all the data.
 */
int
recv_token (int s, gss_buffer_t tok) {
    int ret;
    unsigned long len;

    ret = recv (s, (char *) &len, 4, 0);
    if (ret < 0) {
		errno = WSAGetLastError();
        my_perror("reading token length");
        return -1;
    } else if (ret != 4) {
        OkMsgBox ("reading token length: %d of %d bytes read\n",
            ret, 4);
        return -1;
    }
	  
    len = ntohl(len);
    tok->length = (size_t) len;
    tok->value = (char *) malloc(tok->length);
    if (tok->value == NULL) {
        OkMsgBox ("Out of memory allocating token data\n");
        return -1;
    }

    ret = recv (s, (char *) tok->value, tok->length, 0);
    if (ret < 0) {
		errno = WSAGetLastError();
        my_perror("reading token data");
        free(tok->value);
        return -1;
    } else if ((size_t) ret != tok->length) {
        OkMsgBox ("sending token data: %d of %d bytes written\n",
            ret, tok->length);
        free(tok->value);
        return -1;
    }

    return 0;
}

/*+
 * Function: display_status
 *
 * Purpose: displays GSS-API messages
 *
 * Arguments:
 *
 *	msg		a string to be displayed with the message
 *	maj_stat	the GSS-API major status code
 *	min_stat	the GSS-API minor status code
 *
 * Effects:
 *
 * The GSS-API messages associated with maj_stat and min_stat are
 * displayed on stderr, each preceeded by "GSS-API error <msg>: " and
 * followed by a newline.
 */
void
display_status (char *msg, OM_uint32 maj_stat, OM_uint32 min_stat) {
    display_status_1(msg, maj_stat, GSS_C_GSS_CODE);
    display_status_1(msg, min_stat, GSS_C_MECH_CODE);
}

static void
display_status_1(char *m, OM_uint32 code, int type) {
    OM_uint32 maj_stat, min_stat;
    gss_buffer_desc msg;
    OM_uint32 msg_ctx;
     
    msg_ctx = 0;
    while (1) {
        maj_stat = gss_display_status(&min_stat, code,
                                      type, GSS_C_NULL_OID,
                                      &msg_ctx, &msg);
        OkMsgBox ("GSS-API error %s: %s\n", m,
            (char *)msg.value);
        (void) gss_release_buffer(&min_stat, &msg);
	  
        if (!msg_ctx)
            break;
    }
}
/*+*************************************************************************
** 
** OkMsgBox
** 
** A MessageBox version of printf
** 
***************************************************************************/
void
OkMsgBox (char *format, ...) {
    char buf[256];								// Message goes into here
    char *args;                                 // Args for printf

    args = (char *) &format + sizeof(format);
    vsprintf (buf, format, args);
    MessageBox(NULL, buf, "", MB_OK);
}
/*+*************************************************************************
** 
** My_perror
** 
** A windows conversion of perror displaying the output into a MessageBox.
** 
***************************************************************************/
void
my_perror (char *msg) {
    char *err;

    err = strerror (errno);

    if (msg && *msg != '\0') 
        OkMsgBox ("%s: %s", msg, err);
    else
        MessageBox (NULL, err, "", MB_OK);
}