2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
15 #include <afs\param.h>
17 #include <afs\kautils.h>
18 #include <afs\cm_config.h>
23 * DEFINITIONS ________________________________________________________________
27 #define cREALLOC_CREDS 4
29 #define cszLIBTOKENS TEXT("afsauthent.dll")
30 #define cszLIBCONF TEXT("libafsconf.dll")
34 * DYNAMIC LINKING ____________________________________________________________
39 typedef unsigned int (*initAFSDirPath_t)(void);
40 typedef int (*ka_Init_t)(int flags);
41 typedef int (*rx_Init_t)(int port);
42 typedef int (*ktc_GetToken_t)(struct ktc_principal *server, struct ktc_token *token, int tokenLen, struct ktc_principal *client);
43 typedef int (*ktc_ListTokens_t)(int cellNum, int *cellNumP, struct ktc_principal *serverName);
44 typedef int (*ktc_ForgetToken_t)(struct ktc_principal *server);
45 typedef int (*ka_UserAuthenticateGeneral_t)(int flags, char *name, char *instance, char *realm, char *password, int lifetime, int *password_expiresP, int spare, char **reasonP);
46 typedef long (*cm_GetRootCellName_t)(char *namep);
51 HINSTANCE hInstLibTokens;
52 HINSTANCE hInstLibConf;
54 initAFSDirPath_t initAFSDirPathP;
57 ktc_GetToken_t ktc_GetTokenP;
58 ktc_ListTokens_t ktc_ListTokensP;
59 ktc_ForgetToken_t ktc_ForgetTokenP;
60 ka_UserAuthenticateGeneral_t ka_UserAuthenticateGeneralP;
61 cm_GetRootCellName_t cm_GetRootCellNameP;
64 #define initAFSDirPath (*l.initAFSDirPathP)
65 #define ka_Init (*l.ka_InitP)
66 #define rx_Init (*l.rx_InitP)
67 #define ktc_GetToken (*l.ktc_GetTokenP)
68 #define ktc_ListTokens (*l.ktc_ListTokensP)
69 #define ktc_ForgetToken (*l.ktc_ForgetTokenP)
70 #define ka_UserAuthenticateGeneral (*l.ka_UserAuthenticateGeneralP)
71 #define cm_GetRootCellName (*l.cm_GetRootCellNameP)
74 BOOL Creds_OpenLibraries (void)
76 if (!l.hInstLibTokens)
78 if ((l.hInstLibTokens = LoadLibrary (cszLIBTOKENS)) != NULL)
80 l.initAFSDirPathP = (initAFSDirPath_t)GetProcAddress (l.hInstLibTokens, "initAFSDirPath");
81 l.ka_InitP = (ka_Init_t)GetProcAddress (l.hInstLibTokens, "ka_Init");
82 l.rx_InitP = (rx_Init_t)GetProcAddress (l.hInstLibTokens, "rx_Init");
83 l.ktc_GetTokenP = (ktc_GetToken_t)GetProcAddress (l.hInstLibTokens, "ktc_GetToken");
84 l.ktc_ListTokensP = (ktc_ListTokens_t)GetProcAddress (l.hInstLibTokens, "ktc_ListTokens");
85 l.ktc_ForgetTokenP = (ktc_ForgetToken_t)GetProcAddress (l.hInstLibTokens, "ktc_ForgetToken");
86 l.ka_UserAuthenticateGeneralP = (ka_UserAuthenticateGeneral_t)GetProcAddress (l.hInstLibTokens, "ka_UserAuthenticateGeneral");
88 if (!l.initAFSDirPathP ||
93 !l.ktc_ForgetTokenP ||
94 !l.ka_UserAuthenticateGeneralP)
96 FreeLibrary (l.hInstLibTokens);
97 l.hInstLibTokens = NULL;
110 if ((l.hInstLibConf = LoadLibrary (cszLIBCONF)) != NULL)
112 l.cm_GetRootCellNameP = (cm_GetRootCellName_t)GetProcAddress (l.hInstLibConf, "cm_GetRootCellName");
114 if (!l.cm_GetRootCellNameP)
116 FreeLibrary (l.hInstLibConf);
117 l.hInstLibConf = NULL;
122 return l.hInstLibTokens && l.hInstLibConf;
126 void Creds_CloseLibraries (void)
128 if (l.hInstLibTokens)
130 FreeLibrary (l.hInstLibTokens);
131 l.hInstLibTokens = NULL;
136 FreeLibrary (l.hInstLibConf);
137 l.hInstLibConf = NULL;
144 * ROUTINES ___________________________________________________________________
148 void GetGatewayName (LPTSTR pszGateway)
150 *pszGateway = TEXT('\0');
152 if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
154 DWORD dwSize = MAX_PATH;
155 DWORD dwType = REG_SZ;
157 if (RegQueryValueEx (hk, TEXT("Gateway"), NULL, &dwType, (PBYTE)pszGateway, &dwSize) != 0)
158 *pszGateway = TEXT('\0');
165 BOOL IsServiceRunning (void)
169 SERVICE_STATUS Status;
170 memset (&Status, 0x00, sizeof(Status));
171 Status.dwCurrentState = SERVICE_STOPPED;
174 if ((hManager = OpenSCManager (NULL, NULL, GENERIC_READ)) != NULL)
177 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), GENERIC_READ)) != NULL)
179 QueryServiceStatus (hService, &Status);
180 CloseServiceHandle (hService);
181 } else if ( IsDebuggerPresent() )
182 OutputDebugString("Unable to open Transarc AFS Daemon Service\n");
184 CloseServiceHandle (hManager);
185 } else if ( IsDebuggerPresent() )
186 OutputDebugString("Unable to open SC Manager\n");
188 return (Status.dwCurrentState == SERVICE_RUNNING);
191 TCHAR szGateway[ MAX_PATH ];
192 GetGatewayName (szGateway);
193 return (szGateway[0]) ? TRUE : FALSE;
197 BOOL IsServicePersistent (void)
200 QUERY_SERVICE_CONFIG Config;
203 memset (&Config, 0x00, sizeof(Config));
204 Config.Config.dwStartType = SERVICE_AUTO_START;
207 if ((hManager = OpenSCManager (NULL, NULL, GENERIC_READ)) != NULL)
210 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), GENERIC_READ)) != NULL)
212 DWORD dwSize = sizeof(Config);
213 QueryServiceConfig (hService, (QUERY_SERVICE_CONFIG*)&Config, sizeof(Config), &dwSize);
215 CloseServiceHandle (hService);
218 CloseServiceHandle (hManager);
221 return (Config.Config.dwStartType == SERVICE_AUTO_START) ? TRUE : FALSE;
225 BOOL IsServiceConfigured (void)
234 else if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
236 TCHAR szCell[ MAX_PATH ];
237 DWORD dwSize = sizeof(szCell);
238 DWORD dwType = REG_SZ;
240 if (RegQueryValueEx (hk, TEXT("Cell"), NULL, &dwType, (PBYTE)szCell, &dwSize) == 0)
242 if (szCell[0] != TEXT('\0'))
253 int GetCurrentCredentials (void)
257 lock_ObtainMutex(&g.credsLock);
259 // Free any knowledge we currently have about the user's credentials
265 g.tickLastRetest = GetTickCount();
267 // Start enumerating tokens.
269 if (!Creds_OpenLibraries())
271 rc = ERROR_DLL_INIT_FAILED;
273 else if (IsServiceRunning())
275 for (int iCell = 0; ; )
277 struct ktc_principal Principal;
278 if ((rc = ktc_ListTokens (iCell, &iCell, &Principal)) != 0)
281 struct ktc_token Token;
282 struct ktc_principal ClientName;
283 if ((rc = ktc_GetToken (&Principal, &Token, sizeof(Token), &ClientName)) != 0)
286 // Translate what we found about the user's creds in this particular
287 // cell into something readable.
290 CopyAnsiToString (szCell, Principal.cell);
295 CopyAnsiToString (szUser, ClientName.name);
296 if (ClientName.instance[0])
298 lstrcat (szUser, TEXT("."));
299 CopyAnsiToString (&szUser[ lstrlen(szUser) ], ClientName.instance);
302 SYSTEMTIME stExpires;
303 TimeToSystemTime (&stExpires, Token.endTime);
305 // We've found out that the user has--or perhaps recently had--
306 // credentials within a certain cell under the certain name.
307 // Stick that knowledge in our g.aCreds array.
309 for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
311 if (!lstrcmpi (g.aCreds[ iCreds ].szCell, szCell))
314 if (iCreds == g.cCreds)
316 for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
318 if (!g.aCreds[ iCreds ].szCell[0])
321 if (!REALLOC (g.aCreds, g.cCreds, 1+iCreds, cREALLOC_CREDS))
325 lstrcpy (g.aCreds[ iCreds ].szCell, szCell);
326 lstrcpy (g.aCreds[ iCreds ].szUser, szUser);
327 memcpy (&g.aCreds[ iCreds ].stExpires, &stExpires, sizeof(SYSTEMTIME));
332 lock_ReleaseMutex(&g.credsLock);
334 // We've finished updating g.aCreds. Update the tray icon to reflect
335 // whether the user currently has any credentials at all, and
336 // re-enable the Remind timer.
338 ChangeTrayIcon (NIM_MODIFY);
343 int DestroyCurrentCredentials (LPCTSTR pszCell)
347 if (!Creds_OpenLibraries())
349 rc = ERROR_DLL_INIT_FAILED;
351 else if (IsServiceRunning())
353 struct ktc_principal Principal;
354 memset (&Principal, 0x00, sizeof(Principal));
355 CopyStringToAnsi (Principal.cell, pszCell);
356 CopyStringToAnsi (Principal.name, TEXT("afs"));
357 rc = ktc_ForgetToken (&Principal);
358 if ( KFW_is_available() )
359 KFW_AFS_destroy_tickets_for_cell(Principal.cell);
364 int idsTitle = (g.fIsWinNT) ? IDS_ERROR_TITLE : IDS_ERROR_TITLE_95;
365 int idsDesc = (!g.fIsWinNT) ? IDS_ERROR_DESTROY_95 : (rc == KTC_NOCM) ? IDS_ERROR_DESTROY_NOCM : IDS_ERROR_DESTROY_UNKNOWN;
366 Message (MB_ICONHAND | MB_OK, idsTitle, idsDesc, TEXT("%s%ld"), pszCell, rc);
373 int ObtainNewCredentials (LPCTSTR pszCell, LPCTSTR pszUser, LPCTSTR pszPassword)
378 if (!Creds_OpenLibraries())
380 rc = ERROR_DLL_INIT_FAILED;
382 else if (IsServiceRunning())
385 CopyStringToAnsi (szCellA, pszCell);
388 CopyStringToAnsi (szNameA, pszUser);
390 char szPasswordA[ 256 ];
391 CopyStringToAnsi (szPasswordA, pszPassword);
395 if ( KFW_is_available() )
396 rc = KFW_AFS_get_cred(szNameA, NULL, szCellA, szPasswordA, 0, &Result);
398 rc = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, szNameA, "", szCellA, szPasswordA, 0, &Expiration, 0, &Result);
403 int idsTitle = (g.fIsWinNT) ? IDS_ERROR_TITLE : IDS_ERROR_TITLE_95;
404 int idsDesc = (g.fIsWinNT) ? IDS_ERROR_OBTAIN : IDS_ERROR_OBTAIN_95;
405 Message (MB_ICONHAND | MB_OK, idsTitle, idsDesc, TEXT("%s%s%s%ld"), pszCell, pszUser, (Result) ? Result : TEXT(""), rc);
412 int GetDefaultCell (LPTSTR pszCell)
415 *pszCell = TEXT('\0');
417 if (!Creds_OpenLibraries())
419 rc = ERROR_DLL_INIT_FAILED;
421 else if (IsServiceRunning())
426 if ((rc = cm_GetRootCellName (szCellA)) == 0)
427 CopyAnsiToString (pszCell, szCellA);