windows-netidmgr-extension-sample-20080217
[openafs.git] / src / WINNT / netidmgr_plugin / extensions / sample / plugin.c
1 /*
2  * Copyright (c) 2006 Secure Endpoints Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24
25 /* $Id$ */
26
27 #include "credprov.h"
28 #include "afspext.h"
29 #include<assert.h>
30
31 /* This file provides the message processing function and the support
32    routines for implementing our plugin.  Note that some of the
33    message processing routines have been moved to other source files
34    based on their use.
35 */
36
37 khm_int32  msg_type_afs = -1;
38 khm_handle g_credset = NULL;
39 afs_tk_method tk_method = -1;
40
41 /* Handler for system messages.  The only two we handle are
42    KMSG_SYSTEM_INIT and KMSG_SYSTEM_EXIT. */
43 khm_int32 KHMAPI
44 handle_kmsg_system(khm_int32 msg_type,
45                    khm_int32 msg_subtype,
46                    khm_ui_4  uparam,
47                    void *    vparam) {
48     khm_int32 rv = KHM_ERROR_SUCCESS;
49
50     switch (msg_subtype) {
51
52         /* This is the first message that will be received by a
53            plugin.  We use it to perform initialization operations
54            such as registering any credential types, data types and
55            attributes. */
56     case KMSG_SYSTEM_INIT:
57         {
58             wchar_t short_desc[KCDB_MAXCCH_SHORT_DESC];
59             wchar_t long_desc[KCDB_MAXCCH_LONG_DESC];
60 #ifdef USE_CONFIGURATION_PANELS
61             khui_config_node_reg creg;
62 #endif
63             afs_msg_announce announce;
64
65             if (KHM_FAILED(kmq_find_type(AFS_MSG_TYPENAME, &msg_type_afs))) {
66                 return KHM_ERROR_UNKNOWN;
67             }
68
69             /* We must first announce our extension plug-in, so that
70                the AFS plug-in will know we exist */
71             announce.cbsize = sizeof(announce);
72             announce.version = AFS_PLUGIN_VERSION;
73             announce.name = MYPLUGIN_NAMEW;
74
75             kmq_create_subscription(handle_AFS_MSG, &announce.sub);
76
77             /* Set to TRUE if we are providing a token acquisition
78                method */
79             announce.provide_token_acq = TRUE;
80
81             LoadString(hResModule, IDS_TKMETHOD_SHORT_DESC,
82                        short_desc, ARRAYLENGTH(short_desc));
83
84             announce.token_acq.short_desc = short_desc;
85
86             LoadString(hResModule, IDS_TKMETHOD_LONG_DESC,
87                        long_desc, ARRAYLENGTH(long_desc));
88
89             announce.token_acq.long_desc = long_desc;
90
91             if (KHM_FAILED(kmq_send_message(msg_type_afs,
92                                             AFS_MSG_ANNOUNCE, 0, &announce))) {
93                 kmq_delete_subscription(announce.sub);
94                 announce.sub = NULL;
95
96                 return KHM_ERROR_UNKNOWN;
97             }
98
99             tk_method = announce.token_acq.method_id;
100
101 #ifdef USE_CONFIGURATION_PANELS
102
103             /* Register our configuration panels. */
104
105             /* Registering configuration panels is not required for
106                extension plug-in.  As such, this bit of code is
107                commented out.  However, if you wish to provide a
108                configuration panel, you should uncomment this block
109                and fill in the stub functions in config_main.c */
110
111             ZeroMemory(&creg, sizeof(creg));
112
113             short_desc[0] = L'\0';
114
115             LoadString(hResModule, IDS_CFG_SHORT_DESC,
116                        short_desc, ARRAYLENGTH(short_desc));
117
118             long_desc[0] = L'\0';
119
120             LoadString(hResModule, IDS_CFG_LONG_DESC,
121                        long_desc, ARRAYLENGTH(long_desc));
122
123             creg.name = CONFIGNODE_MAIN;
124             creg.short_desc = short_desc;
125             creg.long_desc = long_desc;
126             creg.h_module = hResModule;
127             creg.dlg_template = MAKEINTRESOURCE(IDD_CONFIG);
128             creg.dlg_proc = config_dlgproc;
129             creg.flags = 0;
130
131             khui_cfg_register(NULL, &creg);
132 #endif
133         }
134         break;
135
136         /* This is the last message that will be received by the
137            plugin. */
138     case KMSG_SYSTEM_EXIT:
139         {
140             khui_config_node cnode;
141
142             /* It should not be assumed that initialization of the
143                plugin went well at this point since we receive a
144                KMSG_SYSTEM_EXIT even if the initialization failed. */
145
146             /* Now unregister any configuration nodes we registered. */
147
148             if (KHM_SUCCEEDED(khui_cfg_open(NULL, CONFIGNODE_MAIN, &cnode))) {
149                 khui_cfg_remove(cnode);
150                 khui_cfg_release(cnode);
151             }
152
153             /* TODO: Perform additional uninitialization
154                operations. */
155         }
156         break;
157     }
158
159     return rv;
160 }
161
162 /* This is the main message handler for our plugin.  All the plugin
163    messages end up here where we either handle it directly or dispatch
164    it to other handlers. */
165 khm_int32 KHMAPI plugin_msg_proc(khm_int32 msg_type,
166                                  khm_int32 msg_subtype,
167                                  khm_ui_4  uparam,
168                                  void * vparam) {
169
170     switch(msg_type) {
171     case KMSG_SYSTEM:
172         return handle_kmsg_system(msg_type, msg_subtype, uparam, vparam);
173     }
174
175     if (msg_type == msg_type_afs) {
176         return handle_AFS_MSG(msg_type, msg_subtype, uparam, vparam);
177     }
178
179     return KHM_ERROR_SUCCESS;
180 }