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

« back to all changes in this revision

Viewing changes to src/kim/lib/kim_library.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-2008 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
#define KRB5_PRIVATE 1
 
28
 
 
29
#include "k5-int.h"
 
30
#include "k5-thread.h"
 
31
#include <krb5/krb5.h>
 
32
#include <profile.h>
 
33
 
 
34
#include "kim_private.h"
 
35
#include "kim_os_private.h"
 
36
 
 
37
#if KIM_TO_KLL_SHIM
 
38
#include "KerberosLoginErrors.h"
 
39
#endif
 
40
 
 
41
MAKE_INIT_FUNCTION(kim_error_init);
 
42
MAKE_FINI_FUNCTION(kim_error_fini);
 
43
 
 
44
/* ------------------------------------------------------------------------ */
 
45
 
 
46
static int kim_error_init (void)
 
47
{
 
48
    add_error_table (&et_KIM_error_table);
 
49
#if KIM_TO_KLL_SHIM
 
50
    add_error_table (&et_KLL_error_table);    
 
51
#endif
 
52
    return 0;
 
53
}
 
54
 
 
55
/* ------------------------------------------------------------------------ */
 
56
 
 
57
static void kim_error_fini (void)
 
58
{
 
59
    if (!INITIALIZER_RAN (kim_error_init) || PROGRAM_EXITING ()) {
 
60
        return;
 
61
    }
 
62
 
 
63
    remove_error_table (&et_KIM_error_table);
 
64
#if KIM_TO_KLL_SHIM
 
65
    remove_error_table (&et_KLL_error_table);
 
66
#endif
 
67
}
 
68
 
 
69
/* ------------------------------------------------------------------------ */
 
70
 
 
71
kim_error kim_library_init (void)
 
72
{
 
73
    return CALL_INIT_FUNCTION (kim_error_init);
 
74
}
 
75
 
 
76
#pragma mark -
 
77
 
 
78
static k5_mutex_t g_allow_home_directory_access_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
 
79
static k5_mutex_t g_allow_automatic_prompting_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
 
80
static k5_mutex_t g_ui_environment_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
 
81
static k5_mutex_t g_application_name_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
 
82
 
 
83
kim_boolean g_allow_home_directory_access = TRUE;
 
84
kim_boolean g_allow_automatic_prompting = TRUE;
 
85
kim_ui_environment g_ui_environment = KIM_UI_ENVIRONMENT_AUTO;
 
86
kim_string g_application_name = NULL;
 
87
 
 
88
MAKE_INIT_FUNCTION(kim_thread_init);
 
89
MAKE_FINI_FUNCTION(kim_thread_fini);
 
90
 
 
91
/* ------------------------------------------------------------------------ */
 
92
 
 
93
static int kim_thread_init (void)
 
94
{
 
95
    kim_error err = KIM_NO_ERROR;
 
96
    
 
97
    if (!err) {
 
98
        err = k5_mutex_finish_init (&g_allow_home_directory_access_mutex);
 
99
    }
 
100
    
 
101
    if (!err) {
 
102
        err = k5_mutex_finish_init (&g_allow_automatic_prompting_mutex);
 
103
    }
 
104
    
 
105
    if (!err) {
 
106
        err = k5_mutex_finish_init (&g_ui_environment_mutex);
 
107
    }
 
108
    
 
109
    if (!err) {
 
110
        err = k5_mutex_finish_init (&g_application_name_mutex);
 
111
    }
 
112
    
 
113
    return err;
 
114
}
 
115
 
 
116
/* ------------------------------------------------------------------------ */
 
117
 
 
118
static void kim_thread_fini (void)
 
119
{
 
120
    if (!INITIALIZER_RAN (kim_thread_init) || PROGRAM_EXITING ()) {
 
121
        return;
 
122
    }
 
123
    
 
124
    k5_mutex_destroy (&g_allow_home_directory_access_mutex);
 
125
    k5_mutex_destroy (&g_allow_automatic_prompting_mutex);
 
126
    k5_mutex_destroy (&g_ui_environment_mutex);
 
127
    k5_mutex_destroy (&g_application_name_mutex);
 
128
}
 
129
 
 
130
#pragma mark -- Allow Home Directory Access --
 
131
 
 
132
/* ------------------------------------------------------------------------ */
 
133
 
 
134
kim_error kim_library_set_allow_home_directory_access (kim_boolean in_allow_access)
 
135
{
 
136
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
137
    kim_error mutex_err = KIM_NO_ERROR;
 
138
    
 
139
    if (!err) {
 
140
        mutex_err = k5_mutex_lock (&g_allow_home_directory_access_mutex);
 
141
        if (mutex_err) { err = mutex_err; }
 
142
    }
 
143
    
 
144
    if (!err) {
 
145
        g_allow_home_directory_access = in_allow_access;
 
146
    }
 
147
    
 
148
    if (!mutex_err) { k5_mutex_unlock (&g_allow_home_directory_access_mutex); }
 
149
    return check_error (err);
 
150
}
 
151
 
 
152
/* ------------------------------------------------------------------------ */
 
153
 
 
154
static kim_error kim_library_get_allow_home_directory_access (kim_boolean *out_allow_access)
 
155
{
 
156
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
157
    kim_error mutex_err = KIM_NO_ERROR;
 
158
    
 
159
    if (!err && !out_allow_access) { err = check_error (KIM_NULL_PARAMETER_ERR); }
 
160
    
 
161
    if (!err) {
 
162
        mutex_err = k5_mutex_lock (&g_allow_home_directory_access_mutex);;
 
163
        if (mutex_err) { err = mutex_err; }
 
164
    }
 
165
    
 
166
    if (!err) {
 
167
        *out_allow_access = g_allow_home_directory_access;
 
168
    }
 
169
    
 
170
    if (!mutex_err) { k5_mutex_unlock (&g_allow_home_directory_access_mutex); }
 
171
    return check_error (err);
 
172
}
 
173
 
 
174
/* ------------------------------------------------------------------------ */
 
175
 
 
176
kim_boolean kim_library_allow_home_directory_access (void)
 
177
{
 
178
    kim_boolean allow_access = FALSE;
 
179
    kim_error err = kim_library_get_allow_home_directory_access (&allow_access);
 
180
    
 
181
    return !err ? allow_access : FALSE;
 
182
}
 
183
 
 
184
 
 
185
#pragma mark -- Allow Automatic Prompting --
 
186
 
 
187
 
 
188
/* ------------------------------------------------------------------------ */
 
189
 
 
190
kim_error kim_library_set_allow_automatic_prompting (kim_boolean in_allow_automatic_prompting)
 
191
{
 
192
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
193
    kim_error mutex_err = KIM_NO_ERROR;
 
194
    
 
195
    if (!err) {
 
196
        mutex_err = k5_mutex_lock (&g_allow_automatic_prompting_mutex);
 
197
        if (mutex_err) { err = mutex_err; }
 
198
    }
 
199
    
 
200
    if (!err) {
 
201
        g_allow_automatic_prompting = in_allow_automatic_prompting;
 
202
    }
 
203
    
 
204
    if (!mutex_err) { k5_mutex_unlock (&g_allow_automatic_prompting_mutex); }
 
205
    return check_error (err);
 
206
}
 
207
 
 
208
/* ------------------------------------------------------------------------ */
 
209
 
 
210
static kim_error kim_library_get_allow_automatic_prompting (kim_boolean *out_allow_automatic_prompting)
 
211
{
 
212
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
213
    kim_error mutex_err = KIM_NO_ERROR;
 
214
    
 
215
    if (!err && !out_allow_automatic_prompting) { err = check_error (KIM_NULL_PARAMETER_ERR); }
 
216
    
 
217
    if (!err) {
 
218
        mutex_err = k5_mutex_lock (&g_allow_automatic_prompting_mutex);;
 
219
        if (mutex_err) { err = mutex_err; }
 
220
    }
 
221
    
 
222
    if (!err) {
 
223
        *out_allow_automatic_prompting = g_allow_automatic_prompting;
 
224
    }
 
225
    
 
226
    if (!mutex_err) { k5_mutex_unlock (&g_allow_automatic_prompting_mutex); }
 
227
    return check_error (err);
 
228
}
 
229
 
 
230
/* ------------------------------------------------------------------------ */
 
231
 
 
232
kim_boolean kim_library_allow_automatic_prompting (void)
 
233
{
 
234
    kim_boolean allow_automatic_prompting = TRUE;
 
235
    kim_error err = kim_library_get_allow_automatic_prompting (&allow_automatic_prompting);
 
236
    if (err) { allow_automatic_prompting = TRUE; }
 
237
    
 
238
    if (allow_automatic_prompting && getenv ("KERBEROSLOGIN_NEVER_PROMPT")) {
 
239
        kim_debug_printf ("KERBEROSLOGIN_NEVER_PROMPT is set.");
 
240
        allow_automatic_prompting = FALSE;
 
241
    }
 
242
    
 
243
    if (allow_automatic_prompting && getenv ("KIM_NEVER_PROMPT")) {
 
244
        kim_debug_printf ("KIM_NEVER_PROMPT is set.");
 
245
        allow_automatic_prompting = FALSE;
 
246
    }
 
247
    
 
248
    if (allow_automatic_prompting && !kim_os_library_caller_uses_gui ()) {
 
249
        kim_debug_printf ("Caller is not using gui.");
 
250
        allow_automatic_prompting = FALSE;
 
251
    }
 
252
 
 
253
    if (allow_automatic_prompting) {
 
254
        /* Make sure there is at least 1 config file. We don't support DNS 
 
255
         * domain-realm lookup, so if there is no config, Kerberos won't work. */
 
256
        
 
257
        kim_boolean kerberos_config_exists = FALSE;
 
258
        char **files = NULL;
 
259
        profile_t profile = NULL;
 
260
        
 
261
        if (krb5_get_default_config_files (&files) == 0) {
 
262
            if (profile_init ((const_profile_filespec_t *) files, &profile) == 0) {
 
263
                kerberos_config_exists = TRUE;
 
264
            }
 
265
        }
 
266
        
 
267
        if (!kerberos_config_exists) {
 
268
            kim_debug_printf ("No valid config file.");
 
269
            allow_automatic_prompting = FALSE;
 
270
        }
 
271
        
 
272
        if (profile) { profile_abandon (profile); }
 
273
        if (files  ) { krb5_free_config_files (files); }        
 
274
    }
 
275
    
 
276
    return allow_automatic_prompting;
 
277
}
 
278
 
 
279
#pragma mark -- UI Environment --
 
280
 
 
281
/* ------------------------------------------------------------------------ */
 
282
 
 
283
kim_error kim_library_set_ui_environment (kim_ui_environment in_ui_environment)
 
284
{
 
285
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
286
    kim_error mutex_err = KIM_NO_ERROR;
 
287
    
 
288
    if (!err) {
 
289
        mutex_err = k5_mutex_lock (&g_ui_environment_mutex);
 
290
        if (mutex_err) { err = mutex_err; }
 
291
    }
 
292
    
 
293
    if (!err) {
 
294
        g_ui_environment = in_ui_environment;
 
295
    }
 
296
    
 
297
    if (!mutex_err) { k5_mutex_unlock (&g_ui_environment_mutex); }
 
298
    return check_error (err);
 
299
}
 
300
 
 
301
/* ------------------------------------------------------------------------ */
 
302
 
 
303
static kim_error kim_library_get_ui_environment (kim_ui_environment *out_ui_environment)
 
304
{
 
305
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
306
    kim_error mutex_err = KIM_NO_ERROR;
 
307
    
 
308
    if (!err && !out_ui_environment) { err = check_error (KIM_NULL_PARAMETER_ERR); }
 
309
    
 
310
    if (!err) {
 
311
        mutex_err = k5_mutex_lock (&g_ui_environment_mutex);;
 
312
        if (mutex_err) { err = mutex_err; }
 
313
    }
 
314
    
 
315
    if (!err) {
 
316
        *out_ui_environment = g_ui_environment;
 
317
    }
 
318
    
 
319
    if (!mutex_err) { k5_mutex_unlock (&g_ui_environment_mutex); }
 
320
    return check_error (err);
 
321
}
 
322
 
 
323
/* ------------------------------------------------------------------------ */
 
324
 
 
325
kim_ui_environment kim_library_ui_environment (void)
 
326
{
 
327
    kim_error err = KIM_NO_ERROR;
 
328
    kim_ui_environment ui_environment = KIM_UI_ENVIRONMENT_AUTO;
 
329
    
 
330
    err = kim_library_get_ui_environment (&ui_environment);
 
331
    
 
332
    if (!err && ui_environment == KIM_UI_ENVIRONMENT_AUTO) {
 
333
        ui_environment = kim_os_library_get_ui_environment ();
 
334
    }
 
335
    
 
336
    return !err ? ui_environment : KIM_UI_ENVIRONMENT_NONE;
 
337
}
 
338
 
 
339
#pragma mark -- Application Name --
 
340
 
 
341
/* ------------------------------------------------------------------------ */
 
342
 
 
343
kim_error kim_library_set_application_name (kim_string in_application_name)
 
344
{
 
345
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
346
    kim_error mutex_err = KIM_NO_ERROR;
 
347
    
 
348
    if (!err) {
 
349
        mutex_err = k5_mutex_lock (&g_application_name_mutex);
 
350
        if (mutex_err) { err = mutex_err; }
 
351
    }
 
352
    
 
353
    if (!err) {
 
354
        kim_string old_application_name = g_application_name;
 
355
        
 
356
        if (in_application_name) {
 
357
            err = kim_string_copy (&g_application_name, in_application_name);
 
358
        } else {
 
359
            g_application_name = NULL;
 
360
        }
 
361
 
 
362
        if (!err) { kim_string_free (&old_application_name); }
 
363
    }
 
364
    
 
365
    if (!mutex_err) { k5_mutex_unlock (&g_application_name_mutex); }
 
366
    return check_error (err);
 
367
}
 
368
 
 
369
/* ------------------------------------------------------------------------ */
 
370
 
 
371
kim_error kim_library_get_application_name (kim_string *out_application_name)
 
372
{
 
373
    kim_error err = CALL_INIT_FUNCTION (kim_thread_init);
 
374
    kim_error mutex_err = KIM_NO_ERROR;
 
375
    kim_string application_name = NULL;
 
376
    
 
377
    if (!err && !out_application_name) { err = check_error (KIM_NULL_PARAMETER_ERR); }
 
378
    
 
379
    if (!err) {
 
380
        mutex_err = k5_mutex_lock (&g_application_name_mutex);
 
381
        if (mutex_err) { err = mutex_err; }
 
382
    }
 
383
    
 
384
    if (!err && g_application_name) {
 
385
        err = kim_string_copy (&application_name, g_application_name);
 
386
    }
 
387
    
 
388
    if (!mutex_err) { k5_mutex_unlock (&g_application_name_mutex); }
 
389
    
 
390
    if (!err && !application_name) {
 
391
        err = kim_os_library_get_caller_name (&application_name);
 
392
    }
 
393
    
 
394
    if (!err) {
 
395
        *out_application_name = application_name;
 
396
        application_name = NULL;
 
397
        
 
398
    }
 
399
    
 
400
    kim_string_free (&application_name);
 
401
    
 
402
    return check_error (err);
 
403
}