~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/kim/lib/kim_error_message.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2009-05-07 16:16:34 UTC
  • mfrom: (13.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090507161634-xqyk0s9na0le4flj
Tags: 1.7dfsg~beta1-4
When  decrypting the TGS response fails with the subkey, try with the
session key to work around Heimdal bug, Closes: #527353 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Header$
 
3
 *
 
4
 * Copyright 2006 Massachusetts Institute of Technology.
 
5
 * All Rights Reserved.
 
6
 *
 
7
 * Export of this software from the United States of America may
 
8
 * require a specific license from the United States Government.
 
9
 * It is the responsibility of any person or organization contemplating
 
10
 * export to obtain such a license before exporting.
 
11
 *
 
12
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 
13
 * distribute this software and its documentation for any purpose and
 
14
 * without fee is hereby granted, provided that the above copyright
 
15
 * notice appear in all copies and that both that copyright notice and
 
16
 * this permission notice appear in supporting documentation, and that
 
17
 * the name of M.I.T. not be used in advertising or publicity pertaining
 
18
 * to distribution of the software without specific, written prior
 
19
 * permission.  Furthermore if you modify this software you must label
 
20
 * your software as modified software and not distribute it in such a
 
21
 * fashion that it might be confused with the original M.I.T. software.
 
22
 * M.I.T. makes no representations about the suitability of
 
23
 * this software for any purpose.  It is provided "as is" without express
 
24
 * or implied warranty.
 
25
 */
 
26
 
 
27
#include "k5-int.h"
 
28
#include "kim_private.h"
 
29
#include <com_err.h>
 
30
#include <CredentialsCache.h>
 
31
 
 
32
static k5_mutex_t kim_error_lock = K5_MUTEX_PARTIAL_INITIALIZER;
 
33
 
 
34
MAKE_INIT_FUNCTION(kim_error_initialize);
 
35
MAKE_FINI_FUNCTION(kim_error_terminate);
 
36
 
 
37
/* ------------------------------------------------------------------------ */
 
38
 
 
39
typedef struct kim_last_error {
 
40
    kim_error code;
 
41
    char message[2048];
 
42
} *kim_last_error;
 
43
 
 
44
/* ------------------------------------------------------------------------ */
 
45
 
 
46
static kim_error kim_error_set_message (kim_error  in_error,
 
47
                                        kim_string in_message)
 
48
{
 
49
    int lock_err = 0;
 
50
    kim_error err = KIM_NO_ERROR;
 
51
    kim_last_error last_error = NULL; 
 
52
    
 
53
    err = lock_err = k5_mutex_lock (&kim_error_lock);
 
54
    
 
55
    if (!err) {
 
56
        last_error = k5_getspecific (K5_KEY_KIM_ERROR_MESSAGE);
 
57
        
 
58
        if (!last_error) {
 
59
            last_error = malloc (sizeof (*last_error));
 
60
            if (!last_error) {
 
61
                err = KIM_OUT_OF_MEMORY_ERR;
 
62
            } else {
 
63
                last_error->code = KIM_NO_ERROR;
 
64
                err = k5_setspecific (K5_KEY_KIM_ERROR_MESSAGE, last_error);
 
65
            }
 
66
        }
 
67
    }
 
68
    
 
69
    if (!err) {
 
70
        strncpy (last_error->message, in_message, sizeof (last_error->message));
 
71
        last_error->message[sizeof (last_error->message)-1] = '\0';
 
72
        last_error->code = in_error;
 
73
    }
 
74
    
 
75
    if (!lock_err) { k5_mutex_unlock (&kim_error_lock); }
 
76
    
 
77
    return err;
 
78
}
 
79
 
 
80
/* ------------------------------------------------------------------------ */
 
81
 
 
82
static void kim_error_free_message (void *io_error)
 
83
{
 
84
    kim_last_error error = io_error;
 
85
    
 
86
    if (error) {
 
87
        if (error->message) {
 
88
            free (error->message);
 
89
        }
 
90
        free (error);
 
91
    }
 
92
}
 
93
 
 
94
#pragma mark -
 
95
 
 
96
/* ------------------------------------------------------------------------ */
 
97
 
 
98
static kim_boolean kim_error_is_builtin (kim_error in_error)
 
99
{
 
100
    return (in_error == KIM_NO_ERROR ||
 
101
            in_error == KIM_OUT_OF_MEMORY_ERR);
 
102
}
 
103
 
 
104
/* ------------------------------------------------------------------------ */
 
105
/* Warning: only remap to error strings with the same format!               */
 
106
 
 
107
static kim_error kim_error_remap (kim_error in_error)
 
108
{
 
109
    /* some krb5 errors are confusing.  remap to better ones */
 
110
    switch (in_error) {
 
111
        case KRB5KRB_AP_ERR_BAD_INTEGRITY:
 
112
            return KIM_BAD_PASSWORD_ERR;
 
113
            
 
114
        case KRB5KDC_ERR_PREAUTH_FAILED:
 
115
            return KIM_PREAUTH_FAILED_ERR;
 
116
            
 
117
        case KRB5KRB_AP_ERR_SKEW:
 
118
            return KIM_CLOCK_SKEW_ERR;
 
119
    }
 
120
    
 
121
    return in_error;
 
122
}
 
123
 
 
124
/* ------------------------------------------------------------------------ */
 
125
 
 
126
kim_string kim_error_message (kim_error in_error)
 
127
{
 
128
    int lock_err = 0;
 
129
    kim_last_error last_error = NULL; 
 
130
    kim_string message = NULL;
 
131
    
 
132
    lock_err = k5_mutex_lock (&kim_error_lock);
 
133
    
 
134
    if (!lock_err) {
 
135
        last_error = k5_getspecific (K5_KEY_KIM_ERROR_MESSAGE);
 
136
        if (last_error && last_error->code == in_error) {
 
137
            message = last_error->message;
 
138
         }
 
139
    }
 
140
    
 
141
    if (!lock_err) { k5_mutex_unlock (&kim_error_lock); }
 
142
    
 
143
    return message ? message : error_message (kim_error_remap (in_error));    
 
144
}
 
145
 
 
146
#pragma mark -- Generic Functions --
 
147
 
 
148
/* ------------------------------------------------------------------------ */
 
149
 
 
150
kim_error kim_error_set_message_for_code (kim_error in_error, 
 
151
                                          ...)
 
152
{
 
153
    kim_error err = KIM_NO_ERROR;
 
154
    va_list args;
 
155
    
 
156
    va_start (args, in_error);
 
157
    err = kim_error_set_message_for_code_va (in_error, args);
 
158
    va_end (args);
 
159
    
 
160
    return check_error (err);    
 
161
}
 
162
 
 
163
/* ------------------------------------------------------------------------ */
 
164
 
 
165
kim_error kim_error_set_message_for_code_va (kim_error in_code, 
 
166
                                             va_list   in_args)
 
167
{
 
168
    kim_error err = KIM_NO_ERROR;
 
169
    kim_error code = kim_error_remap (in_code);
 
170
 
 
171
    if (!kim_error_is_builtin (code)) {
 
172
        kim_string message = NULL;
 
173
        
 
174
        err = kim_string_create_from_format_va_retcode (&message, 
 
175
                                                        error_message (code), 
 
176
                                                        in_args);
 
177
        
 
178
        if (!err) {
 
179
            err = kim_error_set_message (code, message);
 
180
        }
 
181
        
 
182
        kim_string_free (&message);
 
183
    }
 
184
    
 
185
    return err ? err : code;
 
186
}
 
187
 
 
188
 
 
189
/* ------------------------------------------------------------------------ */
 
190
 
 
191
kim_error kim_error_set_message_for_krb5_error (krb5_context    in_context, 
 
192
                                                krb5_error_code in_code)
 
193
{
 
194
    kim_error err = KIM_NO_ERROR;
 
195
    krb5_error_code code = kim_error_remap (in_code);
 
196
    
 
197
    if (code != in_code) {
 
198
        /* error was remapped to a KIM error */
 
199
        err = kim_error_set_message (code, error_message (code));
 
200
 
 
201
    } else if (!kim_error_is_builtin (code)) {
 
202
        const char *message = krb5_get_error_message (in_context, code);
 
203
        
 
204
        if (message) {
 
205
            err = kim_error_set_message (code, message);
 
206
            
 
207
            krb5_free_error_message (in_context, message);
 
208
        }
 
209
    }
 
210
    
 
211
    return err ? err : code;
 
212
}
 
213
 
 
214
#pragma mark -- Debugging Functions --
 
215
 
 
216
/* ------------------------------------------------------------------------ */
 
217
 
 
218
int kim_error_initialize (void)
 
219
{
 
220
    int err = 0;
 
221
    
 
222
    if (!err) {
 
223
        err = k5_mutex_finish_init (&kim_error_lock);
 
224
    }
 
225
    
 
226
    if (!err) {
 
227
        err = k5_key_register (K5_KEY_KIM_ERROR_MESSAGE, 
 
228
                               kim_error_free_message);
 
229
    }
 
230
    
 
231
    return err;
 
232
}
 
233
 
 
234
/* ------------------------------------------------------------------------ */
 
235
 
 
236
void kim_error_terminate (void)
 
237
{
 
238
    if (!INITIALIZER_RAN (kim_error_initialize) || PROGRAM_EXITING ()) {
 
239
        return;
 
240
    }
 
241
    
 
242
    k5_key_delete (K5_KEY_KIM_ERROR_MESSAGE);
 
243
    k5_mutex_destroy (&kim_error_lock);
 
244
}
 
245