~ubuntu-branches/ubuntu/vivid/globus-gss-assist/vivid

« back to all changes in this revision

Viewing changes to display.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattias Ellert
  • Date: 2009-04-18 20:17:33 UTC
  • Revision ID: james.westby@ubuntu.com-20090418201733-xl4r26mgda1shx4q
Tags: upstream-4.0
ImportĀ upstreamĀ versionĀ 4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 1999-2006 University of Chicago
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
 
18
/**
 
19
 * @file display.c
 
20
 * @author Sam Lang, Sam Meder
 
21
 * 
 
22
 * $RCSfile: display.c,v $
 
23
 * $Revision: 1.12 $
 
24
 * $Date: 2007/08/09 14:24:23 $
 
25
 */
 
26
#endif
 
27
 
 
28
#include "globus_i_gss_assist.h"
 
29
#include <stdio.h>
 
30
#include "gssapi.h"
 
31
#include <string.h>
 
32
#include <stdlib.h>
 
33
 
 
34
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
 
35
 
 
36
/**
 
37
 * @ingroup globus_gsi_gss_assist
 
38
 * Concatenate the four message strings, realloc if needed.
 
39
 *
 
40
 *
 
41
 * @param str  
 
42
 *        char * to string or null (realloacted if needed)
 
43
 * @param pre
 
44
 *        char * to first string or null
 
45
 * @param msg
 
46
 *        char * (may not be null terminated) to first string or null
 
47
 * @param msglen
 
48
 *        length of msgring or null
 
49
 * @param post
 
50
 *        char * to last string or null
 
51
 *
 
52
 * @return
 
53
 *        char * to reallocated string.  NULL on error
 
54
 */
 
55
static char *
 
56
globus_gss_assist_strcatr(
 
57
    char *                              str,
 
58
    char *                              pre,
 
59
    char *                              msg,
 
60
    int                                 msglen,
 
61
    char *                              post)
 
62
{
 
63
    char *                              new;
 
64
    int                                 len;
 
65
    static char *                       _function_name_ =
 
66
        "globus_gss_assist_strcatr";
 
67
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
 
68
 
 
69
    len = 1 + (str ? strlen(str) : 0)
 
70
        + (pre ? strlen(pre) : 0)
 
71
        + (msg ? msglen : 0)
 
72
        + (post ? strlen(post) : 0);
 
73
 
 
74
    if (str)
 
75
    { 
 
76
        new = (char *)realloc(str,len);
 
77
        if(!new)
 
78
        {
 
79
            new = malloc(len);
 
80
            if(new)
 
81
            {
 
82
                *new = '\0';
 
83
                strcat(new, str);
 
84
                free(str);
 
85
            }
 
86
        }
 
87
    }
 
88
    else
 
89
    {
 
90
        new = (char *)malloc(len);
 
91
        if (new)
 
92
            *new = '\0';
 
93
    }
 
94
    if (new)
 
95
    {
 
96
        if (pre)
 
97
            strcat(new,pre);
 
98
        if (msg)
 
99
            strncat(new,msg,msglen);
 
100
        if (post)
 
101
            strcat(new,post);
 
102
    }
 
103
 
 
104
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
 
105
    return new;
 
106
}
 
107
#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */
 
108
 
 
109
/**
 
110
 * @name Display Status
 
111
 */
 
112
/* @{ */
 
113
/**
 
114
 * @ingroup globus_gsi_gss_assist
 
115
 * Display the messages for the major and minor status
 
116
 * on the file pointed at by fp.
 
117
 * Takes care of the overloaded major_status if there
 
118
 * was a problem with the get_token or send_token routines.
 
119
 *
 
120
 * @param fp
 
121
 *        a file pointer
 
122
 * @param comment
 
123
 *        String to print out before other error messages.
 
124
 * @param major_status
 
125
 *        The major status to display
 
126
 * @param minor_status
 
127
 *        The minor status to display
 
128
 * @return 
 
129
 *        0
 
130
 */
 
131
OM_uint32
 
132
globus_gss_assist_display_status(
 
133
    FILE *                              fp,
 
134
    char *                              comment,
 
135
    OM_uint32                           major_status,
 
136
    OM_uint32                           minor_status,
 
137
    int                                 token_status)
 
138
{
 
139
    OM_uint32                           ret;
 
140
    char *                              msg = NULL;
 
141
    static char *                       _function_name_ =
 
142
        "globus_gss_assist_display_status";
 
143
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
 
144
 
 
145
    ret = globus_gss_assist_display_status_str(&msg,
 
146
                                               comment,
 
147
                                               major_status,
 
148
                                               minor_status,
 
149
                                               token_status);
 
150
    if (ret == 0)
 
151
        fprintf(fp, "%s", msg);
 
152
    free(msg);
 
153
 
 
154
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
 
155
    return ret;
 
156
}
 
157
/* @} */
 
158
 
 
159
/**
 
160
 * @name Display Status String
 
161
 */
 
162
/* @{ */
 
163
/**
 
164
 * @ingroup globus_gsi_gss_assist
 
165
 * Display the messages for the major and minor status
 
166
 * and return a string with the messages.
 
167
 * Takes care of the overloaded major_status if there
 
168
 * was a problem with the get_token or send_token routines.
 
169
 *
 
170
 * @param str
 
171
 *        pointer to char * for returned string. Must be freed
 
172
 * @param comment
 
173
 *        String to print out before other error messages.
 
174
 * @param major_status
 
175
 *        The major status to display
 
176
 * @param minor_status
 
177
 *        The minor status to display
 
178
 *
 
179
 * @return
 
180
 *        0
 
181
 */
 
182
OM_uint32
 
183
globus_gss_assist_display_status_str(
 
184
    char **                             str,
 
185
    char *                              comment,
 
186
    OM_uint32                           major_status,
 
187
    OM_uint32                           minor_status,
 
188
    int                                 token_status)
 
189
{
 
190
    OM_uint32                           minor_status2;
 
191
    OM_uint32                           message_context;
 
192
    gss_buffer_desc                     status_string_desc 
 
193
        = GSS_C_EMPTY_BUFFER;
 
194
    gss_buffer_t                        status_string = &status_string_desc;
 
195
    char *                              reason1 = (char *) 0;
 
196
    char *                              reason2 = (char *) 0;
 
197
    char                                buf[1024];
 
198
    char *                              msg = NULL;
 
199
    char *                              tmp;
 
200
    static char *                       _function_name_ =
 
201
        "globus_gss_assist_display_status_str";
 
202
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_ENTER;
 
203
 
 
204
    if (!str)
 
205
    { 
 
206
        return GSS_S_FAILURE;
 
207
    }
 
208
 
 
209
    *str = NULL;
 
210
    
 
211
    msg = globus_gss_assist_strcatr(msg,
 
212
                                    comment ? comment : _GASL("GSS failure: "),
 
213
                                    NULL,0,
 
214
                                    "\n");
 
215
    if(!msg)
 
216
    {
 
217
        return GSS_S_FAILURE;
 
218
    }
 
219
    
 
220
    if(token_status == 0)
 
221
    { 
 
222
        message_context = 0;
 
223
        do {
 
224
            if (gss_display_status(&minor_status2,
 
225
                                   major_status,
 
226
                                   GSS_C_GSS_CODE,
 
227
                                   GSS_C_NO_OID,
 
228
                                   &message_context,
 
229
                                   status_string) == GSS_S_COMPLETE)
 
230
            {
 
231
                if (status_string->length)
 
232
                {
 
233
                    tmp = globus_gss_assist_strcatr(
 
234
                        msg, "",
 
235
                        (char *) status_string->value,
 
236
                        status_string->length, "");
 
237
                    if(!tmp)
 
238
                    {
 
239
                        free(msg);
 
240
                        return GSS_S_FAILURE;
 
241
                    }
 
242
                    msg = tmp;
 
243
                }
 
244
                gss_release_buffer(&minor_status2, status_string);
 
245
            }
 
246
        }
 
247
        while (message_context != 0);
 
248
 
 
249
        /* make no assumptions about minor status */
 
250
 
 
251
        message_context = 0;
 
252
        do {
 
253
            if (gss_display_status(&minor_status2,
 
254
                                   minor_status,
 
255
                                   GSS_C_MECH_CODE,
 
256
                                   GSS_C_NO_OID,
 
257
                                   &message_context,
 
258
                                   status_string) == GSS_S_COMPLETE)
 
259
            {
 
260
                if (status_string->length)
 
261
                {
 
262
                    tmp = globus_gss_assist_strcatr(
 
263
                        msg, "",
 
264
                        (char *) status_string->value,
 
265
                        status_string->length, "");
 
266
                    if(!tmp)
 
267
                    {
 
268
                        free(msg);
 
269
                        return GSS_S_FAILURE;
 
270
                    }
 
271
                    msg = tmp;
 
272
                }
 
273
                gss_release_buffer(&minor_status2, status_string);            
 
274
            }
 
275
        }
 
276
        while (message_context != 0);
 
277
    }
 
278
    else
 
279
    {
 
280
        if (GSS_CALLING_ERROR(major_status) ==
 
281
            GSS_S_CALL_INACCESSIBLE_READ)
 
282
        {
 
283
            reason1 = _GASL("read failure:");
 
284
        }
 
285
        else if (GSS_CALLING_ERROR(major_status) ==
 
286
                   GSS_S_CALL_INACCESSIBLE_WRITE)
 
287
        {
 
288
            reason1 = _GASL("write failure:");
 
289
        }
 
290
        else
 
291
        {
 
292
            reason1 = _GASL("failure:");
 
293
        }
 
294
 
 
295
        if (token_status > 0)
 
296
        {
 
297
            switch (token_status)
 
298
            {
 
299
              case GLOBUS_GSS_ASSIST_TOKEN_ERR_MALLOC:
 
300
                reason2 = _GASL("malloc failed");
 
301
                break;
 
302
              case GLOBUS_GSS_ASSIST_TOKEN_ERR_BAD_SIZE:
 
303
                reason2 = _GASL("token length invalid");
 
304
                break;
 
305
              case GLOBUS_GSS_ASSIST_TOKEN_EOF:
 
306
                reason2 = _GASL("Connection closed");
 
307
                break;
 
308
              default:
 
309
                reason2 = _GASL("unknown");
 
310
                break;
 
311
            }
 
312
        }
 
313
        else
 
314
        {
 
315
#ifdef HAVE_STRERROR
 
316
            {
 
317
                reason2 = strerror(-token_status);
 
318
            }
 
319
#endif
 
320
            if (reason2 == NULL)
 
321
            {
 
322
                reason2 = _GASL("unknown");
 
323
            }
 
324
        }
 
325
        sprintf(buf,"    globus_gss_assist token :%d: %s %s\n",
 
326
                token_status,  reason1, reason2);
 
327
        tmp = globus_gss_assist_strcatr(msg,
 
328
                                        buf,
 
329
                                        NULL,0,
 
330
                                        NULL);
 
331
        if(!tmp)
 
332
        {
 
333
            free(msg);
 
334
            return GSS_S_FAILURE;
 
335
        }
 
336
        msg = tmp;
 
337
    }
 
338
    
 
339
    *str = msg;
 
340
 
 
341
    GLOBUS_I_GSI_GSS_ASSIST_DEBUG_EXIT;
 
342
    return 0;
 
343
}
 
344
/* @} */