2 * Copyright (c) 2003 SkyRope, LLC
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Skyrope, LLC nor the names of its contributors may be
14 * used to endorse or promote products derived from this software without
15 * specific prior written permission from Skyrope, LLC.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Portions of this code are derived from portions of the MIT
30 * Leash Ticket Manager and LoadFuncs utilities. For these portions the
31 * following copyright applies.
33 * Copyright (c) 2003,2004 by the Massachusetts Institute of Technology.
34 * All rights reserved.
36 * Export of this software from the United States of America may
37 * require a specific license from the United States Government.
38 * It is the responsibility of any person or organization contemplating
39 * export to obtain such a license before exporting.
41 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42 * distribute this software and its documentation for any purpose and
43 * without fee is hereby granted, provided that the above copyright
44 * notice appear in all copies and that both that copyright notice and
45 * this permission notice appear in supporting documentation, and that
46 * the name of M.I.T. not be used in advertising or publicity pertaining
47 * to distribution of the software without specific, written prior
48 * permission. Furthermore if you modify this software you must label
49 * your software as modified software and not distribute it in such a
50 * fashion that it might be confused with the original M.I.T. software.
51 * M.I.T. makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
57 // IP Change Monitoring Functions
66 #include <afsconfig.h>
67 #include <afs/param.h>
76 #include "ipaddrchg.h"
81 #define MAXCELLCHARS 64
84 // Cell Accessibility Functions
85 // based on work originally submitted to the CMU Computer Club
86 // by Jeffrey Hutzelman
88 // These would work great if the fsProbe interface had been
99 unsigned short port; // in
100 int retry_delay; // in seconds
103 int wait; // in seconds
104 int retry; // in attempts
107 int hosts_attempted; // out
110 // the fsHandler is where we receive the answer to the probe
114 ping_count = fsprobe_Results.probeNum;
115 if (!*fsprobe_Results.probeOK)
118 if (waiting) complete();
120 if (ping_count == retry)
125 // ping_fs is a callback routine meant to be called from within
126 // cm_SearchCellFile() or cm_SearchCellDNS()
128 pingFS(void *ping_params, struct sockaddr_in *addrp, char *namep)
131 struct ping_params * pp = (struct ping_params *) ping_params;
133 if ( pp->max_hosts && pp->hosts_attempted >= pp->max_hosts )
136 pp->hosts_attempted++;
138 if (pp->port && addrp->sin_port != htons(pp->port))
139 addrp->sin_port = htons(pp->port);
141 rc = fsprobe_Init(1, addrp, pp->retry_delay, fsHandler, pp->verbose);
144 fprintf(stderr, "fsprobe_Init failed (%d)\n", rc);
159 char rootcell[MAXCELLCHARS+1];
160 char newcell[MAXCELLCHARS+1];
161 struct ping_params pp;
163 memset(&pp, 0, sizeof(struct ping_params));
165 if (!cell || strlen(cell) == 0) {
166 /* WIN32 NOTE: no way to get max chars */
167 if (rc = pcm_GetRootCellName(rootcell))
172 pp.port = 7000; // AFS FileServer
179 /* WIN32: cm_SearchCellFile(cell, newcell, linkedCell, pcallback, pdata) */
180 rc = pcm_SearchCellFile(cell, newcell, pingFS, (void *)&pp);
182 #endif /* USE_FSPROBE */
184 // These two items are imported from afscreds.h
185 // but it cannot be included without causing conflicts
186 #define c100ns1SECOND (LONGLONG)10000000
188 TimeToSystemTime (SYSTEMTIME *pst, time_t TimeT)
191 memset (pst, 0x00, sizeof(SYSTEMTIME));
193 if ((pTime = localtime (&TimeT)) != NULL)
195 pst->wYear = pTime->tm_year + 1900;
196 pst->wMonth = pTime->tm_mon + 1;
197 pst->wDayOfWeek = pTime->tm_wday;
198 pst->wDay = pTime->tm_mday;
199 pst->wHour = pTime->tm_hour;
200 pst->wMinute = pTime->tm_min;
201 pst->wSecond = pTime->tm_sec;
202 pst->wMilliseconds = 0;
208 LPSTR lpszMachineName,
209 LPSTR lpszServiceName,
210 DWORD *lpdwCurrentState)
213 SC_HANDLE schSCManager = NULL;
214 SC_HANDLE schService = NULL;
215 DWORD fdwDesiredAccess = 0;
216 SERVICE_STATUS ssServiceStatus = {0};
219 *lpdwCurrentState = 0;
221 fdwDesiredAccess = GENERIC_READ;
223 schSCManager = OpenSCManager(lpszMachineName,
227 if(schSCManager == NULL)
233 schService = OpenService(schSCManager,
237 if(schService == NULL)
243 fRet = QueryServiceStatus(schService,
252 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
256 CloseServiceHandle(schService);
257 CloseServiceHandle(schSCManager);
263 ObtainTokensFromUserIfNeeded(HWND hWnd)
265 char * rootcell = NULL;
266 char cell[MAXCELLCHARS+1] = "";
267 char password[PROBE_PASSWORD_LEN+1];
268 struct afsconf_cell cellconfig;
269 struct ktc_principal aserver;
270 struct ktc_principal aclient;
271 struct ktc_token atoken;
272 krb5_timestamp now = 0;
273 BOOL serverReachable = 0;
275 DWORD CurrentState, code;
277 int use_kfw = KFW_is_available();
284 SYSTEMTIME stExpires;
287 memset(HostName, '\0', sizeof(HostName));
288 gethostname(HostName, sizeof(HostName));
289 if (GetServiceStatus(HostName, TRANSARCAFSDAEMON, &CurrentState) != NOERROR)
291 if (CurrentState != SERVICE_RUNNING) {
292 SendMessage(hWnd, WM_START_SERVICE, FALSE, 0L);
296 rootcell = (char *)GlobalAlloc(GPTR,MAXCELLCHARS+1);
300 code = KFW_AFS_get_cellconfig(cell, (void*)&cellconfig, rootcell);
304 memset(&aserver, '\0', sizeof(aserver));
305 strcpy(aserver.name, "afs");
306 strcpy(aserver.cell, rootcell);
308 GetLocalTime (&stNow);
309 SystemTimeToFileTime (&stNow, &ftNow);
310 llNow = (((LONGLONG)ftNow.dwHighDateTime) << 32) + (LONGLONG)(ftNow.dwLowDateTime);
311 llNow /= c100ns1SECOND;
313 rc = ktc_GetToken(&aserver, &atoken, sizeof(atoken), &aclient);
315 TimeToSystemTime (&stExpires, atoken.endTime);
316 SystemTimeToFileTime (&stExpires, &ftExpires);
317 llExpires = (((LONGLONG)ftExpires.dwHighDateTime) << 32) + (LONGLONG)(ftExpires.dwLowDateTime);
318 llExpires /= c100ns1SECOND;
320 if (llNow < llExpires)
323 if ( IsDebuggerPresent() ) {
325 sprintf(message,"ObtainTokensFromUserIfNeeded: %d now = %ul endTime = %ul\n",
326 rc, llNow, llExpires);
327 OutputDebugString(message);
332 serverReachable = cellPing(NULL);
335 // If we can't use the FSProbe interface we can attempt to forge
336 // a kinit and if we can back an invalid user error we know the
337 // kdc is at least reachable
338 serverReachable = KFW_probe_kdc(&cellconfig);
342 for ( i=0 ; i<PROBE_PASSWORD_LEN ; i++ )
345 code = ObtainNewCredentials(rootcell, PROBE_USERNAME, password, TRUE);
348 case KERB_ERR_PRINCIPAL_UNKNOWN:
349 case KERB_ERR_SERVICE_EXP:
351 serverReachable = TRUE;
354 serverReachable = FALSE;
358 if ( !serverReachable ) {
359 if ( IsDebuggerPresent() )
360 OutputDebugString("Server Unreachable\n");
364 if ( IsDebuggerPresent() )
365 OutputDebugString("Server Reachable\n");
369 KFW_import_windows_lsa();
370 #endif /* USE_MS2MIT */
371 KFW_AFS_renew_expiring_tokens();
372 KFW_AFS_renew_token_for_cell(rootcell);
374 rc = ktc_GetToken(&aserver, &atoken, sizeof(atoken), &aclient);
376 TimeToSystemTime (&stExpires, atoken.endTime);
377 SystemTimeToFileTime (&stExpires, &ftExpires);
378 llExpires = (((LONGLONG)ftExpires.dwHighDateTime) << 32) + (LONGLONG)(ftExpires.dwLowDateTime);
379 llExpires /= c100ns1SECOND;
381 if (llNow < llExpires)
386 SendMessage(hWnd, WM_OBTAIN_TOKENS, FALSE, (long)rootcell);
387 rootcell = NULL; // rootcell freed by message receiver
391 GlobalFree(rootcell);
397 GetNumOfIpAddrs(void)
399 PMIB_IPADDRTABLE pIpAddrTable = NULL;
403 DWORD validAddrs = 0;
406 code = GetIpAddrTable(NULL, &dwSize, 0);
407 if (code == ERROR_INSUFFICIENT_BUFFER) {
408 pIpAddrTable = malloc(dwSize);
409 code = GetIpAddrTable(pIpAddrTable, &dwSize, 0);
410 if ( code == NO_ERROR ) {
411 for ( index=0; index < pIpAddrTable->dwNumEntries; index++ ) {
412 if (pIpAddrTable->table[index].dwAddr != 0)
422 IpAddrChangeMonitor(void * hWnd)
424 #ifdef USE_OVERLAPPED
425 HANDLE Handle = INVALID_HANDLE_VALUE; /* Do Not Close This Handle */
427 #endif /* USE_OVERLAPPED */
429 DWORD prevNumOfAddrs = GetNumOfIpAddrs();
437 #ifdef USE_OVERLAPPED
438 ZeroMemory(&Ovlap, sizeof(OVERLAPPED));
440 Result = NotifyAddrChange(&Handle,&Ovlap);
441 if (Result != ERROR_IO_PENDING)
443 if ( IsDebuggerPresent() ) {
444 sprintf(message, "NotifyAddrChange() failed with error %d \n", Result);
445 OutputDebugString(message);
450 if ((Result = WaitForSingleObject(Handle,INFINITE)) != WAIT_OBJECT_0)
452 if ( IsDebuggerPresent() ) {
453 sprintf(message, "WaitForSingleObject() failed with error %d\n",
455 OutputDebugString(message);
460 if (GetOverlappedResult(Handle, &Ovlap,
461 &DataTransfered, TRUE) == 0)
463 if ( IsDebuggerPresent() ) {
464 sprintf(message, "GetOverlapped result failed %d \n",
466 OutputDebugString(message);
471 Result = NotifyAddrChange(NULL,NULL);
472 if (Result != NO_ERROR)
474 if ( IsDebuggerPresent() ) {
475 sprintf(message, "NotifyAddrChange() failed with error %d \n", Result);
476 OutputDebugString(message);
482 NumOfAddrs = GetNumOfIpAddrs();
484 if ( IsDebuggerPresent() ) {
485 sprintf(message,"IPAddrChangeMonitor() NumOfAddrs: now %d was %d\n",
486 NumOfAddrs, prevNumOfAddrs);
487 OutputDebugString(message);
490 if ( NumOfAddrs != prevNumOfAddrs ) {
491 // Give AFS Client Service a chance to notice and die
492 // Or for network services to startup
494 // this call should probably be mutex protected
495 ObtainTokensFromUserIfNeeded(hWnd);
497 prevNumOfAddrs = NumOfAddrs;
503 IpAddrChangeMonitorInit(HWND hWnd)
505 DWORD status = ERROR_SUCCESS;
509 thread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)IpAddrChangeMonitor,
512 if (thread == NULL) {
513 status = GetLastError();