init.c revision 233294
1132943Sgshapiro/*
2261363Sgshapiro * Copyright (c) 1997 - 2001, 2003, 2006 Kungliga Tekniska H��gskolan
3132943Sgshapiro * (Royal Institute of Technology, Stockholm, Sweden).
4132943Sgshapiro * All rights reserved.
5132943Sgshapiro *
6132943Sgshapiro * Redistribution and use in source and binary forms, with or without
7132943Sgshapiro * modification, are permitted provided that the following conditions
8132943Sgshapiro * are met:
9132943Sgshapiro *
10132943Sgshapiro * 1. Redistributions of source code must retain the above copyright
11132943Sgshapiro *    notice, this list of conditions and the following disclaimer.
12132943Sgshapiro *
13132943Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
14132943Sgshapiro *    notice, this list of conditions and the following disclaimer in the
15132943Sgshapiro *    documentation and/or other materials provided with the distribution.
16132943Sgshapiro *
17132943Sgshapiro * 3. Neither the name of the Institute nor the names of its contributors
18132943Sgshapiro *    may be used to endorse or promote products derived from this software
19132943Sgshapiro *    without specific prior written permission.
20132943Sgshapiro *
21132943Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22132943Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23132943Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24132943Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25132943Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26132943Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27132943Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28132943Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29132943Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30132943Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31132943Sgshapiro * SUCH DAMAGE.
32132943Sgshapiro */
33132943Sgshapiro
34132943Sgshapiro#include "gsskrb5_locl.h"
35132943Sgshapiro
36132943Sgshapirostatic HEIMDAL_MUTEX context_mutex = HEIMDAL_MUTEX_INITIALIZER;
37132943Sgshapirostatic int created_key;
38132943Sgshapirostatic HEIMDAL_thread_key context_key;
39132943Sgshapiro
40132943Sgshapirostatic void
41132943Sgshapirodestroy_context(void *ptr)
42132943Sgshapiro{
43132943Sgshapiro    krb5_context context = ptr;
44132943Sgshapiro
45132943Sgshapiro    if (context == NULL)
46132943Sgshapiro	return;
47132943Sgshapiro    krb5_free_context(context);
48266692Sgshapiro}
49132943Sgshapiro
50132943Sgshapirokrb5_error_code
51132943Sgshapiro_gsskrb5_init (krb5_context *context)
52132943Sgshapiro{
53132943Sgshapiro    krb5_error_code ret = 0;
54132943Sgshapiro
55132943Sgshapiro    HEIMDAL_MUTEX_lock(&context_mutex);
56132943Sgshapiro
57132943Sgshapiro    if (!created_key) {
58132943Sgshapiro	HEIMDAL_key_create(&context_key, destroy_context, ret);
59157001Sgshapiro	if (ret) {
60132943Sgshapiro	    HEIMDAL_MUTEX_unlock(&context_mutex);
61132943Sgshapiro	    return ret;
62132943Sgshapiro	}
63132943Sgshapiro	created_key = 1;
64132943Sgshapiro    }
65132943Sgshapiro    HEIMDAL_MUTEX_unlock(&context_mutex);
66132943Sgshapiro
67132943Sgshapiro    *context = HEIMDAL_getspecific(context_key);
68132943Sgshapiro    if (*context == NULL) {
69132943Sgshapiro
70132943Sgshapiro	ret = krb5_init_context(context);
71132943Sgshapiro	if (ret == 0) {
72132943Sgshapiro	    HEIMDAL_setspecific(context_key, *context, ret);
73132943Sgshapiro	    if (ret) {
74132943Sgshapiro		krb5_free_context(*context);
75132943Sgshapiro		*context = NULL;
76132943Sgshapiro	    }
77132943Sgshapiro	}
78132943Sgshapiro    }
79132943Sgshapiro
80132943Sgshapiro    return ret;
81132943Sgshapiro}
82132943Sgshapiro