summaryrefslogtreecommitdiffstats
path: root/src/windows/identity/plugins/krb5/krb5plugin.c
blob: d8877a9c05418e5173dfb41edfa1bfbef258d570 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (c) 2005 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* $Id$ */

#include<krbcred.h>
#include<kherror.h>
#include<khmsgtypes.h>
#include<commctrl.h>
#include<strsafe.h>
#include<krb5.h>

#ifdef DEBUG
#include<assert.h>
#endif

khm_int32 credtype_id_krb5 = KCDB_CREDTYPE_INVALID;
khm_boolean krb5_initialized = FALSE;
khm_handle krb5_credset = NULL;

khm_handle k5_sub = NULL;

LPVOID k5_main_fiber = NULL;
LPVOID k5_kinit_fiber = NULL;

VOID CALLBACK k5_kinit_fiber_proc(PVOID lpParameter);

krb5_context k5_identpro_ctx = NULL;

/*  The system message handler.

    Runs in the context of the plugin thread */
khm_int32 KHMAPI
k5_msg_system(khm_int32 msg_type, khm_int32 msg_subtype,
              khm_ui_4 uparam, void * vparam)
{
    khm_int32 rv = KHM_ERROR_SUCCESS;

    switch(msg_subtype) {
    case KMSG_SYSTEM_INIT:
        {
            kcdb_credtype ct;
            wchar_t buf[KCDB_MAXCCH_SHORT_DESC];
            size_t cbsize;

            /* perform critical registrations and initialization
               stuff */
            ZeroMemory(&ct, sizeof(ct));
            ct.id = KCDB_CREDTYPE_AUTO;
            ct.name = KRB5_CREDTYPE_NAME;

            if(LoadString(hResModule, IDS_KRB5_SHORT_DESC,
                          buf, ARRAYLENGTH(buf))) {
                StringCbLength(buf, KCDB_MAXCB_SHORT_DESC, &cbsize);
                cbsize += sizeof(wchar_t);
                ct.short_desc = PMALLOC(cbsize);
                StringCbCopy(ct.short_desc, cbsize, buf);
            }

            /* even though ideally we should be setting limits
               based KCDB_MAXCB_LONG_DESC, our long description
               actually fits nicely in KCDB_MAXCB_SHORT_DESC */
            if(LoadString(hResModule, IDS_KRB5_LONG_DESC,
                          buf, ARRAYLENGTH(buf))) {
                StringCbLength(buf, KCDB_MAXCB_SHORT_DESC, &cbsize);
                cbsize += sizeof(wchar_t);
                ct.long_desc = PMALLOC(cbsize);
                StringCbCopy(ct.long_desc, cbsize, buf);
            }

            ct.icon = NULL; /* TODO: set a proper icon */

            kmq_create_subscription(k5_msg_callback, &ct.sub);

            ct.is_equal = khm_krb5_creds_is_equal;

            rv = kcdb_credtype_register(&ct, &credtype_id_krb5);

            if(KHM_SUCCEEDED(rv))
                rv = kcdb_credset_create(&krb5_credset);

            if(ct.short_desc)
                PFREE(ct.short_desc);

            if(ct.long_desc)
                PFREE(ct.long_desc);

            if(KHM_SUCCEEDED(rv)) {
                krb5_context ctx = NULL;

                krb5_initialized = TRUE;

                /* now convert this thread to a fiber and create a
                   separate fiber to do kinit stuff */
                k5_main_fiber = ConvertThreadToFiber(NULL);
                k5_kinit_fiber = CreateFiber(0,k5_kinit_fiber_proc,NULL);

                ZeroMemory(&g_fjob, sizeof(g_fjob));

                kmq_create_subscription(k5_msg_callback, &k5_sub);

                k5_register_config_panels();

                khm_krb5_list_tickets(&ctx);

                if(ctx != NULL)
                    pkrb5_free_context(ctx);
            }
        }
        break;

    case KMSG_SYSTEM_EXIT:

        k5_unregister_config_panels();

        if(credtype_id_krb5 >= 0) {
            /* basically just unregister the credential type */
            kcdb_credtype_unregister(credtype_id_krb5);

            /* kcdb knows how to deal with bad handles */
            kcdb_credset_delete(krb5_credset);
            krb5_credset = NULL;
        }

        if(k5_main_fiber != NULL) {
            if (k5_kinit_fiber) {
#ifdef DEBUG
                assert(k5_kinit_fiber != GetCurrentFiber());
#endif
#ifdef CLEANUP_FIBERS_ON_EXIT
                DeleteFiber(k5_kinit_fiber);
                CloseHandle(k5_kinit_fiber);
#endif
                k5_kinit_fiber = NULL;
            }

            k5_main_fiber = NULL;
        }

        if(k5_sub != NULL) {
            kmq_delete_subscription(k5_sub);
            k5_sub = NULL;
        }

        break;
    }

    return rv;
}

khm_int32 KHMAPI
k5_msg_kcdb(khm_int32 msg_type, khm_int32 msg_subtype,
            khm_ui_4 uparam, void * vparam)
{
    khm_int32 rv = KHM_ERROR_SUCCESS;

    switch(msg_subtype) {
    case KMSG_KCDB_IDENT:
        if (uparam == KCDB_OP_DELCONFIG) {
            k5_remove_from_LRU((khm_handle) vparam);
        }
        break;
    }

    return rv;
}


/* Handler for CRED type messages

    Runs in the context of the Krb5 plugin
*/
khm_int32 KHMAPI
k5_msg_cred(khm_int32 msg_type, khm_int32 msg_subtype,
            khm_ui_4 uparam, void * vparam)
{
    khm_int32 rv = KHM_ERROR_SUCCESS;

    switch(msg_subtype) {
    case KMSG_CRED_REFRESH:
        {
            krb5_context ctx = NULL;

            khm_krb5_list_tickets(&ctx);

            if(ctx != NULL)
                pkrb5_free_context(ctx);
        }
        break;

    case KMSG_CRED_DESTROY_CREDS:
        {
            khui_action_context * ctx;

            ctx = (khui_action_context *) vparam;

            if (ctx->credset) {
                _begin_task(0);
                _report_mr0(KHERR_INFO, MSG_ERR_CTX_DESTROY_CREDS);
                _describe();

                khm_krb5_destroy_by_credset(ctx->credset);

                _end_task();
            }
        }
        break;

    case KMSG_CRED_PP_BEGIN:
        k5_pp_begin((khui_property_sheet *) vparam);
        break;

    case KMSG_CRED_PP_END:
        k5_pp_end((khui_property_sheet *) vparam);
        break;

    default:
        if(IS_CRED_ACQ_MSG(msg_subtype))
            return k5_msg_cred_dialog(msg_type, msg_subtype,
                                      uparam, vparam);
    }

    return rv;
}

/*  The main message handler.  We don't do much here, except delegate
    to other message handlers

    Runs in the context of the Krb5 plugin
*/
khm_int32 KHMAPI
k5_msg_callback(khm_int32 msg_type, khm_int32 msg_subtype,
                khm_ui_4 uparam, void * vparam)
{
    switch(msg_type) {
    case KMSG_SYSTEM:
        return k5_msg_system(msg_type, msg_subtype, uparam, vparam);
    case KMSG_CRED:
        return k5_msg_cred(msg_type, msg_subtype, uparam, vparam);
    case KMSG_KCDB:
        return k5_msg_kcdb(msg_type, msg_subtype, uparam, vparam);
    }
    return KHM_ERROR_SUCCESS;
}