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