windows-vista-resume-smb-binding-20080202
[openafs.git] / src / WINNT / afsd / afsd_service.c
1
2 #include <afs/stds.h>
3
4 #include <windows.h>
5 #include <softpub.h>
6 #include <psapi.h>
7 #include <winerror.h>
8 #include <string.h>
9 #include <setjmp.h>
10 #include "afsd.h"
11 #include "afsd_init.h"
12 #include "lanahelper.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <winsock2.h>
16 #include <WINNT\afsreg.h>
17
18 #include <osi.h>
19
20 #ifdef DEBUG
21 //#define NOTSERVICE
22 #endif
23 #ifdef _DEBUG
24 #include <crtdbg.h>
25 #endif
26 #include "afsdifs.h"
27
28 //#define REGISTER_POWER_NOTIFICATIONS 1
29 #include "afsd_flushvol.h"
30
31 extern void afsi_log(char *pattern, ...);
32
33 static SERVICE_STATUS           ServiceStatus;
34 static SERVICE_STATUS_HANDLE    StatusHandle;
35
36 HANDLE hAFSDMainThread = NULL;
37 #ifdef AFSIFS
38 HANDLE hAFSDWorkerThread[WORKER_THREADS];
39 #endif
40
41 HANDLE WaitToTerminate;
42
43 static int GlobalStatus;
44
45 #ifdef JUMP
46 unsigned int MainThreadId;
47 jmp_buf notifier_jmp;
48 #endif /* JUMP */
49
50 extern int traceOnPanic;
51 extern HANDLE afsi_file;
52
53 static int powerEventsRegistered = 0;
54 extern int powerStateSuspended = 0;
55
56 /*
57  * Notifier function for use by osi_panic
58  */
59 static void afsd_notifier(char *msgp, char *filep, long line)
60 {
61 #ifdef AFSIFS
62     int i;
63 #endif
64     if (!msgp)
65         msgp = "unspecified assert";
66
67     if (filep)
68         LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP_WITH_MSG_AND_LOCATION, 
69                  filep, line, msgp);
70     else
71         LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP_WITH_MSG, msgp);
72
73     GlobalStatus = line;
74
75     osi_LogEnable(afsd_logp);
76
77     afsd_ForceTrace(TRUE);
78     buf_ForceTrace(TRUE);
79
80     afsi_log("--- begin dump ---");
81     cm_MemDumpDirStats(afsi_file, "a", 0);
82     cm_MemDumpBPlusStats(afsi_file, "a", 0);
83     cm_DumpCells(afsi_file, "a", 0);
84     cm_DumpVolumes(afsi_file, "a", 0);
85     cm_DumpSCache(afsi_file, "a", 0);
86 #ifdef keisa
87     cm_dnlcDump(afsi_file, "a");
88 #endif
89     cm_DumpBufHashTable(afsi_file, "a", 0);
90     smb_DumpVCP(afsi_file, "a", 0);                     
91     afsi_log("--- end   dump ---");
92     
93 #ifdef DEBUG
94     if (IsDebuggerPresent())
95         DebugBreak();   
96 #endif
97
98     SetEvent(WaitToTerminate);
99 #ifdef AFSIFS
100     WaitForMultipleObjects(WORKER_THREADS, hAFSDWorkerThread, TRUE, INFINITE);
101     for (i = 0; i < WORKER_THREADS; i++)
102         CloseHandle(hAFSDWorkerThread[i]);
103 #endif
104
105 #ifdef JUMP
106     if (GetCurrentThreadId() == MainThreadId)
107         longjmp(notifier_jmp, 1);
108 #endif /* JUMP */
109
110     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
111     ServiceStatus.dwWin32ExitCode = NO_ERROR;
112     ServiceStatus.dwCheckPoint = 0;
113     ServiceStatus.dwWaitHint = 0;
114     ServiceStatus.dwControlsAccepted = 0;
115     SetServiceStatus(StatusHandle, &ServiceStatus);
116
117     exit(1);
118 }
119
120 /*
121  * For use miscellaneously in smb.c; need to do better
122  */
123 static int _stdcall DummyMessageBox(HWND h, LPCTSTR l1, LPCTSTR l2, UINT ui)
124 {
125     return 0;
126 }
127
128 DWORD
129 afsd_ServiceFlushVolume(DWORD dwlpEventData)
130 {
131     DWORD   dwRet = ERROR_NETWORK_BUSY; /* or NO_ERROR */
132
133     /*
134     **  If UI bit is not set, user interaction is not possible
135     **      BUT, since we are a NON-interactive service, and therefore
136     **  have NO user I/O, it doesn't much matter.
137     **  This benign code left here as example of how to find this out
138     */
139     BOOL bUI = (dwlpEventData & 1);
140
141     /* flush volume */
142     if ( PowerNotificationThreadNotify() )
143     {
144         dwRet = NO_ERROR;
145     }
146     else
147     {
148         /* flush was unsuccessful, or timeout - deny shutdown */
149         dwRet = ERROR_NETWORK_BUSY;
150     }
151
152     /*      to deny hibernate, simply return
153     //      any value besides NO_ERROR.
154     //      For example:
155     //      dwRet = ERROR_NETWORK_BUSY;
156     */
157
158     return dwRet;
159 }
160
161
162 /* service control handler used in nt4 only for backward compat. */
163 VOID WINAPI 
164 afsd_ServiceControlHandler(DWORD ctrlCode)
165 {
166     HKEY parmKey;
167     DWORD dummyLen, doTrace;
168     long code;
169
170     switch (ctrlCode) {
171     case SERVICE_CONTROL_SHUTDOWN:
172     case SERVICE_CONTROL_STOP:
173         ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
174         ServiceStatus.dwWin32ExitCode = NO_ERROR;
175         ServiceStatus.dwCheckPoint = 1;
176         ServiceStatus.dwWaitHint = 30000;
177         ServiceStatus.dwControlsAccepted = 0;
178         SetServiceStatus(StatusHandle, &ServiceStatus);
179
180         if (ctrlCode == SERVICE_CONTROL_STOP)
181             afsi_log("SERVICE_CONTROL_STOP");
182         else
183             afsi_log("SERVICE_CONTROL_SHUTDOWN");
184
185         /* Write all dirty buffers back to server */
186         if ( !lana_OnlyLoopback() )
187             buf_CleanAndReset();
188
189         /* Force trace if requested */
190         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
191                              AFSREG_CLT_SVC_PARAM_SUBKEY,
192                              0, KEY_QUERY_VALUE, &parmKey);
193         if (code != ERROR_SUCCESS)
194             goto doneTrace;
195
196         dummyLen = sizeof(doTrace);
197         code = RegQueryValueEx(parmKey, "TraceOnShutdown",
198                                 NULL, NULL,
199                                 (BYTE *) &doTrace, &dummyLen);
200         RegCloseKey (parmKey);
201         if (code != ERROR_SUCCESS)
202             doTrace = 0;
203         if (doTrace) {
204             afsd_ForceTrace(FALSE);
205             buf_ForceTrace(FALSE);
206         }
207
208       doneTrace:
209         SetEvent(WaitToTerminate);
210         break;
211
212     case SERVICE_CONTROL_INTERROGATE:
213         ServiceStatus.dwCurrentState = SERVICE_RUNNING;
214         ServiceStatus.dwWin32ExitCode = NO_ERROR;
215         ServiceStatus.dwCheckPoint = 0;
216         ServiceStatus.dwWaitHint = 0;
217         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
218         SetServiceStatus(StatusHandle, &ServiceStatus);
219         break;
220         /* XXX handle system shutdown */
221         /* XXX handle pause & continue */
222     }
223 }       
224
225
226 /*
227 **    Extended ServiceControlHandler that provides Event types
228 **    for monitoring Power events, for example.
229 */
230 DWORD WINAPI
231 afsd_ServiceControlHandlerEx(
232               DWORD  ctrlCode,
233               DWORD  dwEventType,
234               LPVOID lpEventData,
235               LPVOID lpContext
236               )
237 {
238     HKEY parmKey;
239     DWORD dummyLen, doTrace;
240     long code;
241     DWORD dwRet = ERROR_CALL_NOT_IMPLEMENTED;
242     OSVERSIONINFO osVersion;
243
244     /* Get the version of Windows */
245     memset(&osVersion, 0x00, sizeof(osVersion));
246     osVersion.dwOSVersionInfoSize = sizeof(osVersion);
247     GetVersionEx(&osVersion);
248
249     switch (ctrlCode) 
250     {
251     case SERVICE_CONTROL_SHUTDOWN:
252     case SERVICE_CONTROL_STOP:
253         if (ctrlCode == SERVICE_CONTROL_SHUTDOWN)
254             afsi_log("SERVICE_CONTROL_SHUTDOWN");
255         else
256             afsi_log("SERVICE_CONTROL_STOP");
257
258         ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
259         ServiceStatus.dwWin32ExitCode = NO_ERROR;
260         ServiceStatus.dwCheckPoint = 1;
261         ServiceStatus.dwWaitHint = 30000;
262         ServiceStatus.dwControlsAccepted = 0;
263         SetServiceStatus(StatusHandle, &ServiceStatus);
264
265         /* Write all dirty buffers back to server */
266         if ( !lana_OnlyLoopback() )
267             buf_CleanAndReset();
268
269         /* Force trace if requested */
270         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
271                             AFSREG_CLT_SVC_PARAM_SUBKEY,
272                             0, KEY_QUERY_VALUE, &parmKey);
273         if (code != ERROR_SUCCESS)
274             goto doneTrace;
275
276         dummyLen = sizeof(doTrace);
277         code = RegQueryValueEx(parmKey, "TraceOnShutdown",
278                                NULL, NULL,
279                                (BYTE *) &doTrace, &dummyLen);
280         RegCloseKey (parmKey);
281         if (code != ERROR_SUCCESS)
282             doTrace = 0;
283         if (doTrace) {
284             afsd_ForceTrace(FALSE);
285             buf_ForceTrace(FALSE);
286         }
287
288       doneTrace:
289         SetEvent(WaitToTerminate);
290         dwRet = NO_ERROR;
291         break;
292
293     case SERVICE_CONTROL_INTERROGATE:
294         ServiceStatus.dwCurrentState = SERVICE_RUNNING;
295         ServiceStatus.dwWin32ExitCode = NO_ERROR;
296         ServiceStatus.dwCheckPoint = 0;
297         ServiceStatus.dwWaitHint = 0;
298         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
299         SetServiceStatus(StatusHandle, &ServiceStatus);
300         afsi_log("SERVICE_CONTROL_INTERROGATE");
301         dwRet = NO_ERROR;
302         break;
303
304         /* XXX handle system shutdown */
305         /* XXX handle pause & continue */
306     case SERVICE_CONTROL_POWEREVENT:                                              
307         { 
308 #ifdef DEBUG
309             afsi_log("SERVICE_CONTROL_POWEREVENT");
310 #endif
311             /*                                                                                
312             **  dwEventType of this notification == WPARAM of WM_POWERBROADCAST               
313             **  Return NO_ERROR == return TRUE for that message, i.e. accept request          
314             **  Return any error code to deny request,                                        
315             **  i.e. as if returning BROADCAST_QUERY_DENY                                     
316             */                                                                                
317             if (powerEventsRegistered) {
318                 switch((int) dwEventType)                                                         
319                 {                                                                               
320                 case PBT_APMQUERYSUSPEND:       
321                     afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND"); 
322                     /* Write all dirty buffers back to server */
323                     if ( !lana_OnlyLoopback() ) {
324                         buf_CleanAndReset();
325                         cm_SuspendSCache();
326                     }
327                     afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND buf_CleanAndReset complete"); 
328                     dwRet = NO_ERROR;                       
329                     break;                                  
330                 case PBT_APMQUERYSTANDBY:                                                         
331                     afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY"); 
332                     /* Write all dirty buffers back to server */
333                     if ( !lana_OnlyLoopback() ) {
334                         buf_CleanAndReset();
335                         cm_SuspendSCache();
336                     }
337                     afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY buf_CleanAndReset complete"); 
338                     dwRet = NO_ERROR;                                                             
339                     break;                                                                        
340                                                                                                                           
341                     /* allow remaining case PBT_WhatEver */                                           
342                 case PBT_APMSUSPEND:                         
343                     afsi_log("SERVICE_CONTROL_APMSUSPEND");
344                     powerStateSuspended = 1;
345                     if (osVersion.dwMajorVersion >= 6) {
346                         cm_SuspendSCache();
347                         smb_StopListeners(0);
348                     }
349                     dwRet = NO_ERROR;                       
350                     break;                                  
351                 case PBT_APMSTANDBY:                  
352                     afsi_log("SERVICE_CONTROL_APMSTANDBY"); 
353                     powerStateSuspended = 1;
354                     if (osVersion.dwMajorVersion >= 6) {
355                         cm_SuspendSCache();
356                         smb_StopListeners(0);
357                     }
358                     dwRet = NO_ERROR;                       
359                     break;                                  
360                 case PBT_APMRESUMECRITICAL:             
361                     afsi_log("SERVICE_CONTROL_APMRESUMECRITICAL"); 
362                     if (osVersion.dwMajorVersion >= 6)
363                         smb_RestartListeners(0);
364                     dwRet = NO_ERROR;                       
365                     break;                                  
366                 case PBT_APMRESUMESUSPEND:                                                        
367                     /* User logged in after suspend */
368                     afsi_log("SERVICE_CONTROL_APMRESUMESUSPEND"); 
369                     dwRet = NO_ERROR;                       
370                     break;                                  
371                 case PBT_APMRESUMESTANDBY:            
372                     /* User logged in after standby */
373                     afsi_log("SERVICE_CONTROL_APMRESUMESTANDBY"); 
374                     dwRet = NO_ERROR;                       
375                     break;                                  
376                 case PBT_APMBATTERYLOW:                                                           
377                     afsi_log("SERVICE_CONTROL_APMBATTERYLOW"); 
378                     dwRet = NO_ERROR;                       
379                     break;                                  
380                 case PBT_APMPOWERSTATUSCHANGE:                                                    
381 #ifdef DEBUG
382                     afsi_log("SERVICE_CONTROL_APMPOWERSTATUSCHANGE");
383 #endif
384                     dwRet = NO_ERROR;                       
385                     break;                                  
386                 case PBT_APMOEMEVENT:                                                             
387 #ifdef DEBUG
388                     afsi_log("SERVICE_CONTROL_APMOEMEVENT"); 
389 #endif
390                     dwRet = NO_ERROR;                       
391                     break;                                  
392                 case PBT_APMRESUMEAUTOMATIC:          
393                     /* This is the message delivered once all devices are up */
394                     afsi_log("SERVICE_CONTROL_APMRESUMEAUTOMATIC"); 
395                     powerStateSuspended = 0;
396                     if (osVersion.dwMajorVersion >= 6) {
397                         smb_SetLanAdapterChangeDetected();
398                     }
399                     dwRet = NO_ERROR;                       
400                     break;                                  
401                 default:                                                                          
402                     afsi_log("SERVICE_CONTROL_unknown"); 
403                     dwRet = NO_ERROR;                       
404                 }   
405             }
406         }
407         break;
408     case SERVICE_CONTROL_CUSTOM_DUMP: 
409         {
410             afsi_log("SERVICE_CONTROL_CUSTOM_DUMP"); 
411             GenerateMiniDump(NULL);
412             dwRet = NO_ERROR;
413         }
414         break;
415     }           /* end switch(ctrlCode) */                                                        
416     return dwRet;   
417 }
418
419 /* There is similar code in client_config\drivemap.cpp GlobalMountDrive()
420  * 
421  * Mount a drive into AFS if there global mapping
422  */
423 /* DEE Could check first if we are run as SYSTEM */
424 #define MAX_RETRIES 10
425 #define MAX_DRIVES  23
426 static DWORD __stdcall MountGlobalDrivesThread(void * notUsed)
427 {
428 #ifndef AFSIFS
429     char szAfsPath[_MAX_PATH];
430 #endif
431     char szDriveToMapTo[5];
432     DWORD dwResult;
433     char szKeyName[256];
434     HKEY hKey;
435     DWORD dwIndex = 0, dwRetry = 0;
436     DWORD dwDriveSize;
437     DWORD dwSubMountSize;
438     char szSubMount[256];
439     DWORD dwType;
440
441     sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSREG_CLT_SVC_PARAM_SUBKEY);
442
443     dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
444     if (dwResult != ERROR_SUCCESS)
445         return 0;
446
447     while (dwIndex < MAX_DRIVES) {
448         dwDriveSize = sizeof(szDriveToMapTo);
449         dwSubMountSize = sizeof(szSubMount);
450         dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
451         if (dwResult != ERROR_MORE_DATA) {
452             if (dwResult != ERROR_SUCCESS) {
453                 if (dwResult != ERROR_NO_MORE_ITEMS)
454                     afsi_log("Failed to read GlobalAutoMapper values: %d\n", dwResult);
455                 break;
456             }
457         }
458
459 #ifndef AFSIFS
460         for (dwRetry = 0 ; dwRetry < MAX_RETRIES; dwRetry++)
461         {
462             NETRESOURCE nr;
463             memset (&nr, 0x00, sizeof(NETRESOURCE));
464
465             sprintf(szAfsPath,"\\\\%s\\%s",cm_NetbiosName,szSubMount);
466
467             nr.dwScope = RESOURCE_GLOBALNET;              /* ignored parameter */
468             nr.dwType=RESOURCETYPE_DISK;
469             nr.lpLocalName=szDriveToMapTo;
470             nr.lpRemoteName=szAfsPath;
471             nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE; /* ignored parameter */
472             nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;       /* ignored parameter */
473
474             dwResult = WNetAddConnection2(&nr,NULL,NULL,0);
475             afsi_log("GlobalAutoMap of %s to %s %s (%d)", szDriveToMapTo, szSubMount, 
476                      (dwResult == NO_ERROR) ? "succeeded" : "failed", dwResult);
477             if (dwResult == NO_ERROR) {
478                 break;
479             }
480             /* wait for smb server to come up */
481             Sleep((DWORD)1000 /* miliseconds */);               
482
483             /* Disconnect any previous mappings */
484             dwResult = WNetCancelConnection2(szDriveToMapTo, 0, TRUE);
485         }
486 #else
487         /* FIXFIX: implement */
488         afsi_log("GlobalAutoMap of %s to %s not implemented", szDriveToMapTo, szSubMount);
489 #endif
490     }        
491
492     RegCloseKey(hKey);
493     return 0;
494 }
495
496 static HANDLE hThreadMountGlobalDrives = NULL;
497
498 static void MountGlobalDrives()
499 {
500     DWORD tid;
501
502     hThreadMountGlobalDrives = CreateThread(NULL, 0, MountGlobalDrivesThread, 0, 0, &tid);
503
504     if ( hThreadMountGlobalDrives ) {
505         DWORD rc = WaitForSingleObject( hThreadMountGlobalDrives, 15000 );
506         if (rc == WAIT_TIMEOUT) {
507             afsi_log("GlobalAutoMap thread failed to complete after 15 seconds");
508         } else if (rc == WAIT_OBJECT_0) {
509             afsi_log("GlobalAutoMap thread completed");
510             CloseHandle( hThreadMountGlobalDrives );
511             hThreadMountGlobalDrives = NULL;
512         }
513     }
514 }
515
516 static void DismountGlobalDrives()
517 {
518 #ifndef AFSIFS
519     char szAfsPath[_MAX_PATH];
520     char szDriveToMapTo[5];
521     DWORD dwDriveSize;
522     DWORD dwSubMountSize;
523     char szSubMount[256];
524     DWORD dwType;
525 #endif
526     DWORD dwResult;
527     char szKeyName[256];
528     HKEY hKey;
529     DWORD dwIndex = 0;
530
531     if ( hThreadMountGlobalDrives ) {
532         DWORD rc = WaitForSingleObject(hThreadMountGlobalDrives, 0);
533
534         if (rc == WAIT_TIMEOUT) {
535             afsi_log("GlobalAutoMap thread failed to complete before service shutdown");
536         }
537         else if (rc == WAIT_OBJECT_0) {
538             afsi_log("GlobalAutoMap thread completed");
539             CloseHandle( hThreadMountGlobalDrives );
540             hThreadMountGlobalDrives = NULL;
541         }
542     }
543
544     sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSREG_CLT_SVC_PARAM_SUBKEY);
545
546     dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
547     if (dwResult != ERROR_SUCCESS)
548         return;
549
550 #ifdef AFSIFS    
551     /* FIXFIX: implement */
552 #else
553     while (dwIndex < MAX_DRIVES) {
554         dwDriveSize = sizeof(szDriveToMapTo);
555         dwSubMountSize = sizeof(szSubMount);
556         dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
557         if (dwResult != ERROR_MORE_DATA) {
558             if (dwResult != ERROR_SUCCESS) {
559                 if (dwResult != ERROR_NO_MORE_ITEMS)
560                     afsi_log("Failed to read GlobalAutoMapper values: %d\n", dwResult);
561                 break;
562             }
563         }
564
565         sprintf(szAfsPath,"\\\\%s\\%s",cm_NetbiosName,szSubMount);
566                     
567         dwResult = WNetCancelConnection2(szDriveToMapTo, 0, TRUE);
568         dwResult = WNetCancelConnection(szAfsPath, TRUE);
569         
570         afsi_log("Disconnect from GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
571     }        
572 #endif
573
574     RegCloseKey(hKey);
575 }
576
577 DWORD
578 GetVersionInfo( CHAR * filename, CHAR * szOutput, DWORD dwOutput )
579 {
580     DWORD dwVersionHandle;
581     LPVOID pVersionInfo = 0;
582     DWORD retval = 0;
583     LPDWORD pLangInfo = 0;
584     LPTSTR szVersion = 0;
585     UINT len = 0;
586     TCHAR szVerQ[] = TEXT("\\StringFileInfo\\12345678\\FileVersion");
587     DWORD size = GetFileVersionInfoSize(filename, &dwVersionHandle);
588
589     if (!size) {
590         afsi_log("GetFileVersionInfoSize failed");
591         return GetLastError();
592     }
593
594     pVersionInfo = malloc(size);
595     if (!pVersionInfo) {
596         afsi_log("out of memory 1");
597         return ERROR_NOT_ENOUGH_MEMORY;
598     }
599
600     GetFileVersionInfo(filename, dwVersionHandle, size, pVersionInfo);
601     if (retval = GetLastError())
602     {
603         afsi_log("GetFileVersionInfo failed: %d", retval);
604         goto cleanup;
605     }
606
607     VerQueryValue(pVersionInfo, TEXT("\\VarFileInfo\\Translation"),
608                        (LPVOID*)&pLangInfo, &len);
609     if (retval = GetLastError())
610     {
611         afsi_log("VerQueryValue 1 failed: %d", retval);
612         goto cleanup;
613     }
614
615     wsprintf(szVerQ,
616              TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"),
617              LOWORD(*pLangInfo), HIWORD(*pLangInfo));
618
619     VerQueryValue(pVersionInfo, szVerQ, (LPVOID*)&szVersion, &len);
620     if (retval = GetLastError())
621     {
622         /* try again with language 409 since the old binaries were tagged wrong */
623         wsprintf(szVerQ,
624                   TEXT("\\StringFileInfo\\0409%04x\\FileVersion"),
625                   HIWORD(*pLangInfo));
626
627         VerQueryValue(pVersionInfo, szVerQ, (LPVOID*)&szVersion, &len);
628         if (retval = GetLastError()) {
629             afsi_log("VerQueryValue 2 failed: [%s] %d", szVerQ, retval);
630             goto cleanup;
631         }
632     }
633     snprintf(szOutput, dwOutput, TEXT("%s"), szVersion);
634     szOutput[dwOutput - 1] = 0;
635
636  cleanup:
637     if (pVersionInfo)
638         free(pVersionInfo);
639
640     return retval;
641 }
642
643 static HINSTANCE hCrypt32;
644 static DWORD (WINAPI *pCertGetNameString)(PCCERT_CONTEXT pCertContext,  DWORD dwType,  DWORD dwFlags,
645                                           void* pvTypePara, LPTSTR pszNameString, DWORD cchNameString);
646 static BOOL (WINAPI *pCryptQueryObject)(DWORD dwObjectType, const void* pvObject, DWORD dwExpectedContentTypeFlags,
647                                         DWORD dwExpectedFormatTypeFlags, DWORD dwFlags,
648                                         DWORD* pdwMsgAndCertEncodingType, DWORD* pdwContentType,
649                                         DWORD* pdwFormatType, HCERTSTORE* phCertStore,
650                                         HCRYPTMSG* phMsg, const void** ppvContext);
651 static BOOL (WINAPI *pCryptMsgGetParam)(HCRYPTMSG hCryptMsg, DWORD dwParamType, DWORD dwIndex,
652                                         void* pvData, DWORD* pcbData);
653 static PCCERT_CONTEXT (WINAPI *pCertFindCertificateInStore)(HCERTSTORE hCertStore, DWORD dwCertEncodingType,
654                                                             DWORD dwFindFlags, DWORD dwFindType,
655                                                             const void* pvFindPara,
656                                                             PCCERT_CONTEXT pPrevCertContext);
657 static BOOL (WINAPI *pCertCloseStore)(HCERTSTORE hCertStore, DWORD dwFlags);
658 static BOOL (WINAPI *pCryptMsgClose)(HCRYPTMSG hCryptMsg);
659 static BOOL (WINAPI *pCertCompareCertificate)(DWORD dwCertEncodingType, PCERT_INFO pCertId1,
660                                               PCERT_INFO pCertId2);
661 static BOOL (WINAPI *pCertFreeCertificateContext)(PCCERT_CONTEXT pCertContext);
662
663 void LoadCrypt32(void)
664 {
665     hCrypt32 = LoadLibrary("crypt32");
666     if ( !hCrypt32 )
667         return;
668
669     (FARPROC) pCertGetNameString = GetProcAddress( hCrypt32, "CertGetNameString" );
670     (FARPROC) pCryptQueryObject = GetProcAddress( hCrypt32, "CryptQueryObject" );
671     (FARPROC) pCryptMsgGetParam = GetProcAddress( hCrypt32, "CryptMsgGetParam" );
672     (FARPROC) pCertFindCertificateInStore = GetProcAddress( hCrypt32, "CertFindCertificateInStore" );
673     (FARPROC) pCertCloseStore = GetProcAddress( hCrypt32, "CertCloseStore" );
674     (FARPROC) pCryptMsgClose = GetProcAddress( hCrypt32, "CryptMsgClose" );
675     (FARPROC) pCertCompareCertificate = GetProcAddress( hCrypt32, "CertCompareCertificate" );
676     (FARPROC) pCertFreeCertificateContext = GetProcAddress( hCrypt32, "CertFreeCertificateContext" );
677     
678     if ( !pCertGetNameString ||
679          !pCryptQueryObject ||
680          !pCryptMsgGetParam ||
681          !pCertFindCertificateInStore ||
682          !pCertCloseStore ||
683          !pCryptMsgClose ||
684          !pCertCompareCertificate ||
685          !pCertFreeCertificateContext)
686     {
687         FreeLibrary(hCrypt32);
688         hCrypt32 = NULL;
689     }
690 }
691
692 void UnloadCrypt32(void)
693 {
694     FreeLibrary(hCrypt32);
695 }
696
697 #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
698
699 PCCERT_CONTEXT GetCertCtx(CHAR * filename)
700 {
701     wchar_t wfilename[260];
702     BOOL fResult;
703     DWORD dwEncoding;
704     DWORD dwContentType;
705     DWORD dwFormatType;
706     DWORD dwSignerInfo;
707     HCERTSTORE hStore = NULL;
708     HCRYPTMSG hMsg = NULL;
709     PCMSG_SIGNER_INFO pSignerInfo = NULL;
710     PCCERT_CONTEXT pCertContext = NULL;
711     CERT_INFO CertInfo;
712
713     if ( hCrypt32 == NULL )
714         return NULL;
715
716     ZeroMemory(&CertInfo, sizeof(CertInfo));
717     mbstowcs(wfilename, filename, 260);
718
719     fResult = pCryptQueryObject(CERT_QUERY_OBJECT_FILE,
720                                 wfilename,
721                                 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
722                                 CERT_QUERY_FORMAT_FLAG_BINARY,
723                                 0,
724                                 &dwEncoding,
725                                 &dwContentType,
726                                 &dwFormatType,
727                                 &hStore,
728                                 &hMsg,
729                                 NULL);
730
731     if (!fResult) {
732         afsi_log("CryptQueryObject failed for [%s] with error 0x%x",
733                  filename,
734                  GetLastError());
735         goto __exit;
736     }
737
738     fResult = pCryptMsgGetParam(hMsg,
739                                 CMSG_SIGNER_INFO_PARAM,
740                                 0,
741                                 NULL,
742                                 &dwSignerInfo);
743
744     if (!fResult) {
745         afsi_log("CryptMsgGetParam failed for [%s] with error 0x%x",
746                  filename,
747                  GetLastError());
748         goto __exit;
749     }
750
751     pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
752
753     fResult = pCryptMsgGetParam(hMsg,
754                                 CMSG_SIGNER_INFO_PARAM,
755                                 0,
756                                 (PVOID)pSignerInfo,
757                                 &dwSignerInfo);
758     
759     if (!fResult) {
760         afsi_log("CryptMsgGetParam failed for [%s] with error 0x%x",
761                  filename,
762                  GetLastError());
763         goto __exit;
764     }
765
766     CertInfo.Issuer = pSignerInfo->Issuer;
767     CertInfo.SerialNumber = pSignerInfo->SerialNumber;
768
769     pCertContext = pCertFindCertificateInStore(hStore,
770                                               ENCODING,
771                                               0,
772                                               CERT_FIND_SUBJECT_CERT,
773                                               (PVOID) &CertInfo,
774                                               NULL);
775
776     if (!pCertContext) {
777       afsi_log("CertFindCertificateInStore for file [%s] failed with 0x%x",
778                filename,
779                GetLastError());
780       goto __exit;
781     }
782
783   __exit:
784     if (pSignerInfo)
785       LocalFree(pSignerInfo);
786
787     /*    if (pCertContext)
788           CertFreeCertificateContext(pCertContext);*/
789
790     if (hStore)
791       pCertCloseStore(hStore,0);
792
793     if (hMsg)
794       pCryptMsgClose(hMsg);
795
796     return pCertContext;
797 }
798
799 BOOL VerifyTrust(CHAR * filename)
800 {
801     WIN_TRUST_ACTDATA_CONTEXT_WITH_SUBJECT fContextWSubject;
802     WIN_TRUST_SUBJECT_FILE fSubjectFile;
803     GUID trustAction = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE;
804     GUID subject = WIN_TRUST_SUBJTYPE_PE_IMAGE;
805     wchar_t wfilename[260];
806     LONG ret;
807     BOOL success = FALSE;
808
809     LONG (WINAPI *pWinVerifyTrust)(HWND hWnd, GUID* pgActionID, WINTRUST_DATA* pWinTrustData) = NULL;
810     HINSTANCE hWinTrust;
811
812     if (filename == NULL ) 
813         return FALSE;
814
815     hWinTrust = LoadLibrary("wintrust");
816     if ( !hWinTrust )
817         return FALSE;
818
819     if (((FARPROC) pWinVerifyTrust =
820           GetProcAddress( hWinTrust, "WinVerifyTrust" )) == NULL )
821     {
822         FreeLibrary(hWinTrust);
823         return FALSE;
824     }
825
826     mbstowcs(wfilename, filename, 260);
827
828     fSubjectFile.hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
829                                     0, NULL);
830     fSubjectFile.lpPath = wfilename;
831     fContextWSubject.hClientToken = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
832                                                 FALSE, GetCurrentProcessId());
833     fContextWSubject.SubjectType = &subject;
834     fContextWSubject.Subject = &fSubjectFile;
835
836     ret = pWinVerifyTrust(INVALID_HANDLE_VALUE, &trustAction, (WINTRUST_DATA *)&fContextWSubject);
837
838     if ( fSubjectFile.hFile != INVALID_HANDLE_VALUE )
839         CloseHandle( fSubjectFile.hFile );
840     if ( fContextWSubject.hClientToken != INVALID_HANDLE_VALUE )
841         CloseHandle( fContextWSubject.hClientToken );
842
843     if (ret == ERROR_SUCCESS) {
844         success = TRUE;
845     } else {
846         DWORD gle = GetLastError();
847         switch (gle) {
848         case TRUST_E_PROVIDER_UNKNOWN:
849             afsi_log("VerifyTrust failed: \"Generic Verify V2\" Provider Unknown");
850             break;  
851         case TRUST_E_NOSIGNATURE:
852             afsi_log("VerifyTrust failed: Unsigned executable");
853             break;
854         case TRUST_E_EXPLICIT_DISTRUST:
855             afsi_log("VerifyTrust failed: Certificate Marked as Untrusted by the user");
856             break;
857         case TRUST_E_SUBJECT_NOT_TRUSTED:
858             afsi_log("VerifyTrust failed: File is not trusted");
859             break;
860         case TRUST_E_BAD_DIGEST:
861             afsi_log("VerifyTrust failed: Executable has been modified");
862             break;
863         case CRYPT_E_SECURITY_SETTINGS:
864             afsi_log("VerifyTrust failed: local security options prevent verification");
865             break;
866         default:
867             afsi_log("VerifyTrust failed: 0x%X", GetLastError());
868         }
869         success = FALSE;
870     }
871     FreeLibrary(hWinTrust);
872     return success;
873 }
874
875 void LogCertCtx(PCCERT_CONTEXT pCtx) {
876     DWORD dwData;
877     LPTSTR szName = NULL;
878
879     if ( hCrypt32 == NULL )
880         return;
881
882     // Get Issuer name size.
883     if (!(dwData = pCertGetNameString(pCtx,
884                                       CERT_NAME_SIMPLE_DISPLAY_TYPE,
885                                       CERT_NAME_ISSUER_FLAG,
886                                       NULL,
887                                       NULL,
888                                       0))) {
889         afsi_log("CertGetNameString failed: 0x%x", GetLastError());
890         goto __exit;
891     }
892
893     // Allocate memory for Issuer name.
894     szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));
895
896     // Get Issuer name.
897     if (!(pCertGetNameString(pCtx,
898                              CERT_NAME_SIMPLE_DISPLAY_TYPE,
899                              CERT_NAME_ISSUER_FLAG,
900                              NULL,
901                              szName,
902                              dwData))) {
903         afsi_log("CertGetNameString failed: 0x%x", GetLastError());
904         goto __exit;
905     }
906
907     // print Issuer name.
908     afsi_log("Issuer Name: %s", szName);
909     LocalFree(szName);
910     szName = NULL;
911
912     // Get Subject name size.
913     if (!(dwData = pCertGetNameString(pCtx,
914                                       CERT_NAME_SIMPLE_DISPLAY_TYPE,
915                                       0,
916                                       NULL,
917                                       NULL,
918                                       0))) {
919         afsi_log("CertGetNameString failed: 0x%x", GetLastError());
920         goto __exit;
921     }
922
923     // Allocate memory for subject name.
924     szName = (LPTSTR)LocalAlloc(LPTR, dwData * sizeof(TCHAR));
925
926     // Get subject name.
927     if (!(pCertGetNameString(pCtx,
928                              CERT_NAME_SIMPLE_DISPLAY_TYPE,
929                              0,
930                              NULL,
931                              szName,
932                              dwData))) {
933         afsi_log("CertGetNameString failed: 0x%x", GetLastError());
934         goto __exit;
935     }
936
937     // Print Subject Name.
938     afsi_log("Subject Name: %s", szName);
939
940   __exit:
941
942     if (szName)
943         LocalFree(szName);
944 }
945
946 BOOL AFSModulesVerify(void)
947 {
948     CHAR filename[1024];
949     CHAR afsdVersion[128];
950     CHAR modVersion[128];
951     CHAR checkName[1024];
952     BOOL trustVerified = FALSE;
953     HMODULE hMods[1024];
954     HANDLE hProcess;
955     DWORD cbNeeded;
956     unsigned int i;
957     BOOL success = TRUE;
958     PCCERT_CONTEXT pCtxService = NULL;
959     HINSTANCE hPSAPI;
960     DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE hProcess, HMODULE hModule, LPTSTR lpFilename, DWORD nSize);
961     BOOL (WINAPI *pEnumProcessModules)(HANDLE hProcess, HMODULE* lphModule, DWORD cb, LPDWORD lpcbNeeded);
962     DWORD dummyLen, code;
963     DWORD cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
964     DWORD verifyServiceSig = TRUE;
965     HKEY parmKey;
966
967     hPSAPI = LoadLibrary("psapi");
968
969     if ( hPSAPI == NULL )
970         return FALSE;
971
972     if (!GetModuleFileName(NULL, filename, sizeof(filename)))
973         return FALSE;
974
975     if (GetVersionInfo(filename, afsdVersion, sizeof(afsdVersion)))
976         return FALSE;
977
978     afsi_log("%s version %s", filename, afsdVersion);
979
980     if (((FARPROC) pGetModuleFileNameExA =
981           GetProcAddress( hPSAPI, "GetModuleFileNameExA" )) == NULL ||
982          ((FARPROC) pEnumProcessModules =
983            GetProcAddress( hPSAPI, "EnumProcessModules" )) == NULL)
984     {
985         FreeLibrary(hPSAPI);
986         return FALSE;
987     }
988
989
990     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
991                         AFSREG_CLT_SVC_PARAM_SUBKEY,
992                         0, KEY_QUERY_VALUE, &parmKey);
993     if (code == ERROR_SUCCESS) {
994         dummyLen = sizeof(cacheSize);
995         code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
996                                (BYTE *) &cacheSize, &dummyLen);
997         RegCloseKey (parmKey);
998     }
999
1000     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
1001                          0, KEY_QUERY_VALUE, &parmKey);
1002     if (code == ERROR_SUCCESS) {
1003         dummyLen = sizeof(verifyServiceSig);
1004         code = RegQueryValueEx(parmKey, "VerifyServiceSignature", NULL, NULL,
1005                                 (BYTE *) &verifyServiceSig, &dummyLen);
1006         RegCloseKey (parmKey);
1007     }
1008
1009     if (verifyServiceSig 
1010 #ifndef _WIN64
1011          && cacheSize < 716800
1012 #endif
1013          ) {
1014         trustVerified = VerifyTrust(filename);
1015     } else {
1016         afsi_log("Signature Verification disabled");
1017     }
1018
1019     if (trustVerified) {
1020         LoadCrypt32();
1021
1022         // get a certificate context for the signer of afsd_service.
1023         pCtxService = GetCertCtx(filename);
1024         if (pCtxService)
1025             LogCertCtx(pCtxService);
1026     }
1027
1028     // Get a list of all the modules in this process.
1029     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
1030                            FALSE, GetCurrentProcessId());
1031
1032     if (pEnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
1033     {
1034         afsi_log("Num of Process Modules: %d", (cbNeeded / sizeof(HMODULE)));
1035
1036         for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
1037         {
1038             char szModName[2048];
1039
1040             // Get the full path to the module's file.
1041             if (pGetModuleFileNameExA(hProcess, hMods[i], szModName, sizeof(szModName)))
1042             {
1043                 lstrcpy(checkName, szModName);
1044                 strlwr(checkName);
1045
1046                 if ( strstr(checkName, "afspthread.dll") ||
1047                      strstr(checkName, "afsauthent.dll") ||
1048                      strstr(checkName, "afsrpc.dll") ||
1049                      strstr(checkName, "libafsconf.dll") ||
1050                      strstr(checkName, "libosi.dll") )
1051                 {
1052                     if (GetVersionInfo(szModName, modVersion, sizeof(modVersion))) {
1053                         success = FALSE;
1054                         continue;
1055                     }
1056
1057                     afsi_log("%s version %s", szModName, modVersion);
1058                     if (strcmp(afsdVersion,modVersion)) {
1059                         afsi_log("Version mismatch: %s", szModName);
1060                         success = FALSE;
1061                     }
1062                     if ( trustVerified ) {
1063                         if ( !VerifyTrust(szModName) ) {
1064                             afsi_log("Signature Verification failed: %s", szModName);
1065                             success = FALSE;
1066                         } 
1067                         else if (pCtxService) {
1068                             PCCERT_CONTEXT pCtx = GetCertCtx(szModName);
1069
1070                             if (!pCtx || !pCertCompareCertificate(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1071                                                                   pCtxService->pCertInfo,
1072                                                                   pCtx->pCertInfo)) {
1073                                 afsi_log("Certificate mismatch: %s", szModName);
1074                                 if (pCtx)
1075                                     LogCertCtx(pCtx);
1076                                 
1077                                 success = FALSE;
1078                             }
1079                             
1080                             if (pCtx)
1081                                 pCertFreeCertificateContext(pCtx);
1082                         }
1083                     }
1084                 }
1085             }
1086         }
1087     }
1088
1089     if (pCtxService) {
1090         pCertFreeCertificateContext(pCtxService);
1091         UnloadCrypt32();
1092     }
1093
1094     FreeLibrary(hPSAPI);
1095
1096     CloseHandle(hProcess);
1097     return success;
1098 }
1099
1100 /*
1101 control serviceex exists only on 2000/xp. These functions will be loaded dynamically.
1102 */
1103
1104 typedef SERVICE_STATUS_HANDLE ( * RegisterServiceCtrlHandlerExFunc )(  LPCTSTR , LPHANDLER_FUNCTION_EX , LPVOID );
1105 typedef SERVICE_STATUS_HANDLE ( * RegisterServiceCtrlHandlerFunc   )(  LPCTSTR ,  LPHANDLER_FUNCTION );
1106
1107 RegisterServiceCtrlHandlerExFunc pRegisterServiceCtrlHandlerEx = NULL;
1108 RegisterServiceCtrlHandlerFunc   pRegisterServiceCtrlHandler   = NULL; 
1109
1110 VOID WINAPI
1111 afsd_Main(DWORD argc, LPTSTR *argv)
1112 {
1113     long code;
1114     char *reason;
1115 #ifdef JUMP
1116     int jmpret;
1117 #endif /* JUMP */
1118     HMODULE hHookDll;
1119     HMODULE hAdvApi32;
1120 #ifdef AFSIFS
1121     int cnt;
1122 #endif
1123
1124 #ifdef _DEBUG
1125     afsd_DbgBreakAllocInit();
1126     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/ | 
1127                    _CRTDBG_CHECK_CRT_DF /* | _CRTDBG_DELAY_FREE_MEM_DF */ );
1128 #endif 
1129
1130     afsd_SetUnhandledExceptionFilter();
1131        
1132     osi_InitPanic(afsd_notifier);
1133     osi_InitTraceOption();
1134
1135     GlobalStatus = 0;
1136
1137     afsi_start();
1138
1139     WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, TEXT("afsd_service_WaitToTerminate"));
1140     if ( GetLastError() == ERROR_ALREADY_EXISTS )
1141         afsi_log("Event Object Already Exists: %s", TEXT("afsd_service_WaitToTerminate"));
1142
1143 #ifndef NOTSERVICE
1144     hAdvApi32 = LoadLibrary("advapi32.dll");
1145     if (hAdvApi32 == NULL)
1146     {
1147         afsi_log("Fatal: cannot load advapi32.dll");
1148         return;
1149     }
1150
1151     pRegisterServiceCtrlHandlerEx = (RegisterServiceCtrlHandlerExFunc)GetProcAddress(hAdvApi32, "RegisterServiceCtrlHandlerExA");
1152     if (pRegisterServiceCtrlHandlerEx)
1153     {
1154         afsi_log("running on 2000+ - using RegisterServiceCtrlHandlerEx");
1155         StatusHandle = RegisterServiceCtrlHandlerEx(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandlerEx, NULL );
1156     }
1157     else
1158     {
1159         StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandler);
1160     }
1161
1162     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1163     ServiceStatus.dwServiceSpecificExitCode = 0;
1164     ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
1165     ServiceStatus.dwWin32ExitCode = NO_ERROR;
1166     ServiceStatus.dwCheckPoint = 1;
1167     ServiceStatus.dwWaitHint = 120000;
1168     /* accept Power Events */
1169     ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
1170     SetServiceStatus(StatusHandle, &ServiceStatus);
1171 #endif
1172
1173     LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_START_PENDING);
1174
1175 #ifdef REGISTER_POWER_NOTIFICATIONS
1176     {
1177         HKEY hkParm;
1178         DWORD code;
1179         DWORD dummyLen;
1180         int bpower = TRUE;
1181
1182         /* see if we should handle power notifications */
1183         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY, 
1184                             0, KEY_QUERY_VALUE, &hkParm);
1185         if (code == ERROR_SUCCESS) {
1186             dummyLen = sizeof(bpower);
1187             code = RegQueryValueEx(hkParm, "FlushOnHibernate", NULL, NULL,
1188                 (BYTE *) &bpower, &dummyLen);      
1189
1190             if(code != ERROR_SUCCESS)
1191                 bpower = TRUE;
1192
1193             RegCloseKey(hkParm);
1194         }
1195         /* create thread used to flush cache */
1196         if (bpower) {
1197             PowerNotificationThreadCreate();
1198             powerEventsRegistered = 1;
1199         }
1200     }
1201 #endif
1202
1203     /* Verify the versions of the DLLs which were loaded */
1204     if (!AFSModulesVerify()) {
1205         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1206         ServiceStatus.dwWin32ExitCode = NO_ERROR;
1207         ServiceStatus.dwCheckPoint = 0;
1208         ServiceStatus.dwWaitHint = 0;
1209         ServiceStatus.dwControlsAccepted = 0;
1210         SetServiceStatus(StatusHandle, &ServiceStatus);
1211
1212         LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_INCORRECT_VERSIONS);
1213
1214         /* exit if initialization failed */
1215         return;
1216     }
1217
1218     /* allow an exit to be called prior to any initialization */
1219     hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1220     if (hHookDll)
1221     {
1222         BOOL hookRc = TRUE;
1223         AfsdInitHook initHook = ( AfsdInitHook ) GetProcAddress(hHookDll, AFSD_INIT_HOOK);
1224         if (initHook)
1225         {
1226             hookRc = initHook();
1227         }
1228         FreeLibrary(hHookDll);
1229         hHookDll = NULL;
1230
1231         if (hookRc == FALSE)
1232         {
1233             ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1234             ServiceStatus.dwWin32ExitCode = NO_ERROR;
1235             ServiceStatus.dwCheckPoint = 0;
1236             ServiceStatus.dwWaitHint = 0;
1237             ServiceStatus.dwControlsAccepted = 0;
1238             SetServiceStatus(StatusHandle, &ServiceStatus);
1239                        
1240             /* exit if initialization failed */
1241             return;
1242         }
1243         else
1244         {
1245             /* allow another 120 seconds to start */
1246             ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1247             ServiceStatus.dwServiceSpecificExitCode = 0;
1248             ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
1249             ServiceStatus.dwWin32ExitCode = NO_ERROR;
1250             ServiceStatus.dwCheckPoint = 2;
1251             ServiceStatus.dwWaitHint = 120000;
1252             /* accept Power Events */
1253             ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
1254             SetServiceStatus(StatusHandle, &ServiceStatus);
1255         }
1256     }
1257
1258     /* Perform Volume Status Notification Initialization */
1259     cm_VolStatus_Initialization();
1260
1261 #ifdef JUMP
1262     MainThreadId = GetCurrentThreadId();
1263     jmpret = setjmp(notifier_jmp);
1264
1265     if (jmpret == 0) 
1266 #endif /* JUMP */
1267     {
1268         code = afsd_InitCM(&reason);
1269         if (code != 0) {
1270             afsi_log("afsd_InitCM failed: %s (code = %d)", reason, code);
1271             osi_panic(reason, __FILE__, __LINE__);
1272         }
1273
1274 #ifndef NOTSERVICE
1275         ServiceStatus.dwCheckPoint = 3;
1276         ServiceStatus.dwWaitHint = 30000;
1277         SetServiceStatus(StatusHandle, &ServiceStatus);
1278 #endif
1279         code = afsd_InitDaemons(&reason);
1280         if (code != 0) {
1281             afsi_log("afsd_InitDaemons failed: %s (code = %d)", reason, code);
1282                         osi_panic(reason, __FILE__, __LINE__);
1283         }
1284
1285         /* allow an exit to be called post rx initialization */
1286         hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1287         if (hHookDll)
1288         {
1289             BOOL hookRc = TRUE;
1290             AfsdRxStartedHook rxStartedHook = ( AfsdRxStartedHook ) GetProcAddress(hHookDll, AFSD_RX_STARTED_HOOK);
1291             if (rxStartedHook)
1292             {
1293                 hookRc = rxStartedHook();
1294             }
1295             FreeLibrary(hHookDll);
1296             hHookDll = NULL;
1297
1298             if (hookRc == FALSE)
1299             {
1300                 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1301                 ServiceStatus.dwWin32ExitCode = NO_ERROR;
1302                 ServiceStatus.dwCheckPoint = 0;
1303                 ServiceStatus.dwWaitHint = 0;
1304                 ServiceStatus.dwControlsAccepted = 0;
1305                 SetServiceStatus(StatusHandle, &ServiceStatus);
1306                        
1307                 /* exit if initialization failed */
1308                 return;
1309             }
1310         }
1311
1312 #ifndef NOTSERVICE
1313         ServiceStatus.dwCheckPoint = 4;
1314         ServiceStatus.dwWaitHint = 15000;
1315         SetServiceStatus(StatusHandle, &ServiceStatus);
1316 #endif
1317
1318         /* Notify any volume status handlers that the cache manager has started */
1319         cm_VolStatus_Service_Started();
1320
1321 /* the following ifdef chooses the mode of operation for the service.  to enable
1322  * a runtime flag (instead of compile-time), pioctl() would need to dynamically
1323  * determine the mode, in order to use the correct ioctl special-file path. */
1324 #ifndef AFSIFS
1325         code = afsd_InitSMB(&reason, MessageBox);
1326         if (code != 0) {
1327             afsi_log("afsd_InitSMB failed: %s (code = %d)", reason, code);
1328             osi_panic(reason, __FILE__, __LINE__);
1329         }
1330 #else
1331         code = ifs_Init(&reason);
1332         if (code != 0) {
1333             afsi_log("ifs_Init failed: %s (code = %d)", reason, code);
1334             osi_panic(reason, __FILE__, __LINE__);
1335         }     
1336         for (cnt = 0; cnt < WORKER_THREADS; cnt++)
1337             hAFSDWorkerThread[cnt] = CreateThread(NULL, 0, ifs_MainLoop, 0, 0, NULL);
1338 #endif  
1339
1340         /* allow an exit to be called post smb initialization */
1341         hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1342         if (hHookDll)
1343         {
1344             BOOL hookRc = TRUE;
1345             AfsdSmbStartedHook smbStartedHook = ( AfsdSmbStartedHook ) GetProcAddress(hHookDll, AFSD_SMB_STARTED_HOOK);
1346             if (smbStartedHook)
1347             {
1348                 hookRc = smbStartedHook();
1349             }
1350             FreeLibrary(hHookDll);
1351             hHookDll = NULL;
1352
1353             if (hookRc == FALSE)
1354             {
1355                 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1356                 ServiceStatus.dwWin32ExitCode = NO_ERROR;
1357                 ServiceStatus.dwCheckPoint = 0;
1358                 ServiceStatus.dwWaitHint = 0;
1359                 ServiceStatus.dwControlsAccepted = 0;
1360                 SetServiceStatus(StatusHandle, &ServiceStatus);
1361                        
1362                 /* exit if initialization failed */
1363                 return;
1364             }
1365         }
1366
1367         MountGlobalDrives();
1368
1369 #ifndef NOTSERVICE
1370         ServiceStatus.dwCurrentState = SERVICE_RUNNING;
1371         ServiceStatus.dwWin32ExitCode = NO_ERROR;
1372         ServiceStatus.dwCheckPoint = 5;
1373         ServiceStatus.dwWaitHint = 0;
1374
1375         /* accept Power events */
1376         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
1377         SetServiceStatus(StatusHandle, &ServiceStatus);
1378 #endif  
1379
1380         LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_RUNNING);
1381     }
1382
1383     /* allow an exit to be called when started */
1384     hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1385     if (hHookDll)
1386     {
1387         BOOL hookRc = TRUE;
1388         AfsdStartedHook startedHook = ( AfsdStartedHook ) GetProcAddress(hHookDll, AFSD_STARTED_HOOK);
1389         if (startedHook)
1390         {
1391             hookRc = startedHook();
1392         }
1393         FreeLibrary(hHookDll);
1394         hHookDll = NULL;
1395
1396         if (hookRc == FALSE)
1397         {
1398             ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1399             ServiceStatus.dwWin32ExitCode = NO_ERROR;
1400             ServiceStatus.dwCheckPoint = 0;
1401             ServiceStatus.dwWaitHint = 0;
1402             ServiceStatus.dwControlsAccepted = 0;
1403             SetServiceStatus(StatusHandle, &ServiceStatus);
1404                        
1405             /* exit if initialization failed */
1406             return;
1407         }
1408     }
1409
1410 #ifndef AFSIFS
1411     WaitForSingleObject(WaitToTerminate, INFINITE);
1412 #else
1413     WaitForMultipleObjects(WORKER_THREADS, hAFSDWorkerThread, TRUE, INFINITE);
1414     for (cnt = 0; cnt < WORKER_THREADS; cnt++)
1415         CloseHandle(hAFSDWorkerThread[cnt]);
1416 #endif
1417
1418     ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
1419     ServiceStatus.dwWin32ExitCode = NO_ERROR;
1420     ServiceStatus.dwCheckPoint = 6;
1421     ServiceStatus.dwWaitHint = 120000;
1422     ServiceStatus.dwControlsAccepted = 0;
1423     SetServiceStatus(StatusHandle, &ServiceStatus);
1424
1425     afsi_log("Received Termination Signal, Stopping Service");
1426
1427     if ( GlobalStatus )
1428         LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP);
1429     else
1430         LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_STOPPING);
1431
1432     /* allow an exit to be called prior to stopping the service */
1433     hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1434     if (hHookDll)
1435     {
1436         BOOL hookRc = TRUE;
1437         AfsdStoppingHook stoppingHook = ( AfsdStoppingHook ) GetProcAddress(hHookDll, AFSD_STOPPING_HOOK);
1438         if (stoppingHook)
1439         {
1440             hookRc = stoppingHook();
1441         }
1442         FreeLibrary(hHookDll);
1443         hHookDll = NULL;
1444     }
1445
1446
1447 #ifdef AFS_FREELANCE_CLIENT
1448     cm_FreelanceShutdown();
1449     afsi_log("Freelance Shutdown complete");
1450 #endif
1451
1452     DismountGlobalDrives();
1453     afsi_log("Global Drives dismounted");
1454                                          
1455     cm_DaemonShutdown();                 
1456     afsi_log("Daemon shutdown complete");
1457     
1458     afsd_ShutdownCM();
1459
1460     buf_Shutdown();                      
1461     afsi_log("Buffer shutdown complete");
1462                                          
1463     rx_Finalize();                       
1464     afsi_log("rx finalization complete");
1465                                          
1466 #ifndef AFSIFS
1467     smb_Shutdown();                      
1468     afsi_log("smb shutdown complete");   
1469 #endif
1470                                          
1471     RpcShutdown();                       
1472
1473     cm_ReleaseAllLocks();
1474
1475     rx_Finalize();
1476     afsi_log("rx finalization complete");
1477
1478     cm_ShutdownMappedMemory();           
1479
1480 #ifdef  REGISTER_POWER_NOTIFICATIONS
1481     /* terminate thread used to flush cache */
1482     if (powerEventsRegistered)
1483         PowerNotificationThreadExit();
1484 #endif
1485
1486     cm_DirDumpStats();
1487 #ifdef USE_BPLUS
1488     cm_BPlusDumpStats();
1489 #endif
1490
1491     /* Notify any Volume Status Handlers that we are stopped */
1492     cm_VolStatus_Service_Stopped();
1493
1494     /* Cleanup any Volume Status Notification Handler */
1495     cm_VolStatus_Finalize();
1496
1497     /* allow an exit to be called after stopping the service */
1498     hHookDll = LoadLibrary(AFSD_HOOK_DLL);
1499     if (hHookDll)
1500     {
1501         BOOL hookRc = TRUE;
1502         AfsdStoppedHook stoppedHook = ( AfsdStoppedHook ) GetProcAddress(hHookDll, AFSD_STOPPED_HOOK);
1503         if (stoppedHook)
1504         {
1505             hookRc = stoppedHook();
1506         }
1507         FreeLibrary(hHookDll);
1508         hHookDll = NULL;
1509     }
1510
1511     /* Remove the ExceptionFilter */
1512     SetUnhandledExceptionFilter(NULL);
1513
1514     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
1515     ServiceStatus.dwWin32ExitCode = GlobalStatus ? ERROR_EXCEPTION_IN_SERVICE : NO_ERROR;
1516     ServiceStatus.dwCheckPoint = 7;
1517     ServiceStatus.dwWaitHint = 0;
1518     ServiceStatus.dwControlsAccepted = 0;
1519     SetServiceStatus(StatusHandle, &ServiceStatus);
1520 }       
1521
1522 DWORD __stdcall afsdMain_thread(void* notUsed)
1523 {
1524     char * argv[2] = {AFS_DAEMON_SERVICE_NAME, NULL};
1525     afsd_Main(1, (LPTSTR*)argv);
1526     return(0);
1527 }
1528
1529 void usage(void)
1530 {
1531     fprintf(stderr, "afsd_service.exe [--validate-cache <cache-path>]");
1532 }
1533
1534 int
1535 main(int argc, char * argv[])
1536 {
1537     static SERVICE_TABLE_ENTRY dispatchTable[] = {
1538         {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
1539         {NULL, NULL}
1540     };
1541     int i;
1542
1543     for (i = 1; i < argc; i++) {
1544         if (!stricmp(argv[i],"--validate-cache")) {
1545             if (++i != argc - 1) {
1546                 usage();
1547                 return(1);
1548             }
1549
1550             return cm_ValidateMappedMemory(argv[i]);
1551         } else {
1552             usage();
1553             return(1);
1554         }
1555     }
1556
1557     if (!StartServiceCtrlDispatcher(dispatchTable))
1558     {
1559         LONG status = GetLastError();
1560         if (status == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
1561         {
1562             DWORD tid;
1563             hAFSDMainThread = CreateThread(NULL, 0, afsdMain_thread, 0, 0, &tid);
1564                 
1565             printf("Hit <Enter> to terminate OpenAFS Client Service\n");
1566             getchar();  
1567             SetEvent(WaitToTerminate);
1568 #ifdef AFSIFS
1569             dc_release_hooks();
1570 #endif
1571         }
1572     }
1573
1574     if ( hAFSDMainThread ) {
1575         WaitForSingleObject( hAFSDMainThread, INFINITE );
1576         CloseHandle( hAFSDMainThread );
1577     }
1578     return(0);
1579 }