2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
12 #include <afs/afs_args.h>
30 #include <rx\rx_null.h>
31 #include <WINNT/syscfg.h>
32 #include <WINNT/afsreg.h>
36 #include "lanahelper.h"
38 #include "cm_memmap.h"
40 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
41 extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
43 extern afs_uint32 cryptall;
44 extern afs_uint32 cm_anonvldb;
45 extern int cm_enableServerLocks;
46 extern int cm_followBackupPath;
47 extern int cm_deleteReadOnly;
49 extern afs_int32 cm_BPlusTrees;
51 extern afs_int32 cm_OfflineROIsValid;
52 extern afs_int32 cm_giveUpAllCBs;
53 extern const char **smb_ExecutableExtensions;
57 cm_config_data_t cm_data;
59 char cm_rootVolumeName[VL_MAXNAMELEN];
60 DWORD cm_rootVolumeNameLen;
61 char cm_mountRoot[1024];
62 DWORD cm_mountRootLen;
65 #ifdef AFS_FREELANCE_CLIENT
67 #endif /* freelance */
80 char cm_HostName[200];
82 unsigned short cm_callbackport = CM_DEFAULT_CALLBACKPORT;
84 char cm_NetbiosName[MAX_NB_NAME_LENGTH] = "";
86 char cm_CachePath[MAX_PATH];
87 DWORD cm_CachePathLen;
88 DWORD cm_ValidateCache = 1;
90 BOOL reportSessionStartups = FALSE;
92 cm_initparams_v1 cm_initParams;
95 unsigned int cm_sysNameCount = 0;
96 char *cm_sysNameList[MAXNUMSYSNAMES];
98 DWORD TraceOption = 0;
101 * AFSD Initialization Log
103 * This is distinct from the regular debug logging facility.
104 * Log items go directly to a file, not to an array in memory, so that even
105 * if AFSD crashes, the log can be inspected.
111 int cm_dnsEnabled = 1;
115 static int afsi_log_useTimestamp = 1;
118 afsi_log(char *pattern, ...)
120 char s[256], t[100], d[100], u[512];
123 va_start(ap, pattern);
125 StringCbVPrintfA(s, sizeof(s), pattern, ap);
126 if ( afsi_log_useTimestamp ) {
127 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
128 GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
129 StringCbPrintfA(u, sizeof(u), "%s %s: %s\r\n", d, t, s);
130 if (afsi_file != INVALID_HANDLE_VALUE)
131 WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
136 if (afsi_file != INVALID_HANDLE_VALUE)
137 WriteFile(afsi_file, s, (DWORD)strlen(s), &zilch, NULL);
141 extern initUpperCaseTable();
142 void afsd_initUpperCaseTable()
144 initUpperCaseTable();
151 char t[100], u[100], *p, *path;
157 DWORD maxLogSize = 100 * 1024;
159 afsi_file = INVALID_HANDLE_VALUE;
160 code = GetTempPath(sizeof(wd)-15, wd);
161 if ( code == 0 || code > (sizeof(wd)-15) )
162 return; /* unable to create a log */
164 StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
165 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
166 afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
167 OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
169 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
170 0, KEY_QUERY_VALUE, &parmKey);
171 if (code == ERROR_SUCCESS) {
172 dummyLen = sizeof(maxLogSize);
173 code = RegQueryValueEx(parmKey, "MaxLogSize", NULL, NULL,
174 (BYTE *) &maxLogSize, &dummyLen);
175 RegCloseKey (parmKey);
179 dwLow = GetFileSize( afsi_file, &dwHigh );
180 if ( dwHigh > 0 || dwLow >= maxLogSize ) {
181 CloseHandle(afsi_file);
182 afsi_file = CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
183 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
187 SetFilePointer(afsi_file, 0, NULL, FILE_END);
188 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
189 StringCbCatA(t, sizeof(t), ": Create log file\r\n");
190 StringCbCatA(u, sizeof(u), ": Created log file\r\n");
191 WriteFile(afsi_file, t, (DWORD)strlen(t), &zilch, NULL);
192 WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
194 code = GetEnvironmentVariable("PATH", NULL, 0);
196 code = GetEnvironmentVariable("PATH", path, code);
197 WriteFile(afsi_file, p, (DWORD)strlen(p), &zilch, NULL);
198 WriteFile(afsi_file, path, (DWORD)strlen(path), &zilch, NULL);
199 WriteFile(afsi_file, "\r\n", (DWORD)1, &zilch, NULL);
202 /* Initialize C RTL Code Page conversion functions */
203 /* All of the path info obtained from the SMB client is in the OEM code page */
204 afsi_log("OEM Code Page = %d", GetOEMCP());
205 afsi_log("locale = %s", setlocale(LC_ALL,NULL));
207 /* Two things to look into. First, should mbstowcs() be performing
208 * character set translations from OEM to Unicode in smb3.c;
209 * Second, do we need to set this translation in each function
210 * due to multi-threading.
212 afsi_log("locale -> %s", setlocale(LC_ALL, ".OCP"));
213 afsi_log("_setmbcp = %d -> %d", _setmbcp(_MB_CP_OEM), _getmbcp());
218 * Standard AFSD trace
221 void afsd_ForceTrace(BOOL flush)
230 len = GetTempPath(sizeof(buf)-10, buf);
231 StringCbCopyA(&buf[len], sizeof(buf)-len, "/afsd.log");
232 handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
233 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
234 if (handle == INVALID_HANDLE_VALUE) {
236 osi_panic("Cannot create log file", __FILE__, __LINE__);
238 osi_LogPrint(afsd_logp, handle);
240 FlushFileBuffers(handle);
245 configureBackConnectionHostNames(void)
247 /* On Windows XP SP2, Windows 2003 SP1, and all future Windows operating systems
248 * there is a restriction on the use of SMB authentication on loopback connections.
249 * There are two work arounds available:
251 * (1) We can disable the check for matching host names. This does not
253 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
254 * "DisableLoopbackCheck"=dword:00000001
256 * (2) We can add the AFS SMB/CIFS service name to an approved list. This
257 * does require a reboot:
258 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0]
259 * "BackConnectionHostNames"=multi-sz
261 * The algorithm will be:
262 * (1) Check to see if cm_NetbiosName exists in the BackConnectionHostNames list
263 * (2a) If not, add it to the list. (This will not take effect until the next reboot.)
264 * (2b1) and check to see if DisableLoopbackCheck is set.
265 * (2b2) If not set, set the DisableLoopbackCheck value to 0x1
266 * (2b3) and create HKLM\SOFTWARE\OpenAFS\Client UnsetDisableLoopbackCheck
267 * (2c) else If cm_NetbiosName exists in the BackConnectionHostNames list,
268 * check for the UnsetDisableLoopbackCheck value.
269 * If set, set the DisableLoopbackCheck flag to 0x0
270 * and delete the UnsetDisableLoopbackCheck value
272 * Starting in Longhorn Beta 1, an entry in the BackConnectionHostNames value will
273 * force Windows to use the loopback authentication mechanism for the specified
280 DWORD dwSize, dwAllocSize;
282 PBYTE pHostNames = NULL, pName = NULL;
283 BOOL bNameFound = FALSE;
285 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
286 "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0",
289 &hkMSV10) == ERROR_SUCCESS )
291 if ((RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0,
292 &dwType, NULL, &dwAllocSize) == ERROR_SUCCESS) &&
293 (dwType == REG_MULTI_SZ))
295 dwAllocSize += 1 /* in case the source string is not nul terminated */
296 + strlen(cm_NetbiosName) + 2;
297 pHostNames = malloc(dwAllocSize);
298 dwSize = dwAllocSize;
299 if (RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType,
300 pHostNames, &dwSize) == ERROR_SUCCESS)
302 for (pName = pHostNames;
303 (pName - pHostNames < dwSize) && *pName ;
304 pName += strlen(pName) + 1)
306 if ( !stricmp(pName, cm_NetbiosName) ) {
315 size_t size = strlen(cm_NetbiosName) + 2;
317 pHostNames = malloc(size);
320 StringCbCopyA(pName, size, cm_NetbiosName);
322 *pName = '\0'; /* add a second nul terminator */
324 dwType = REG_MULTI_SZ;
325 dwSize = pName - pHostNames + 1;
326 RegSetValueEx( hkMSV10, "BackConnectionHostNames", 0, dwType, pHostNames, dwSize);
328 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
329 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
332 &hkLsa) == ERROR_SUCCESS )
334 dwSize = sizeof(DWORD);
335 if ( RegQueryValueEx( hkLsa, "DisableLoopbackCheck", 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS ||
338 dwSize = sizeof(DWORD);
340 RegSetValueEx( hkLsa, "DisableLoopbackCheck", 0, dwType, (LPBYTE)&dwValue, dwSize);
342 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
343 AFSREG_CLT_OPENAFS_SUBKEY,
346 REG_OPTION_NON_VOLATILE,
350 NULL) == ERROR_SUCCESS) {
353 dwSize = sizeof(DWORD);
355 RegSetValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, dwType, (LPBYTE)&dwValue, dwSize);
356 RegCloseKey(hkClient);
362 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
363 AFSREG_CLT_OPENAFS_SUBKEY,
366 REG_OPTION_NON_VOLATILE,
370 NULL) == ERROR_SUCCESS) {
372 dwSize = sizeof(DWORD);
373 if ( RegQueryValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, &dwType, (LPBYTE)&dwValue, &dwSize) == ERROR_SUCCESS &&
375 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
376 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
379 &hkLsa) == ERROR_SUCCESS )
381 RegDeleteValue(hkLsa, "DisableLoopbackCheck");
385 RegDeleteValue(hkClient, "RemoveDisableLoopbackCheck");
386 RegCloseKey(hkClient);
389 RegCloseKey(hkMSV10);
397 static void afsd_InitServerPreferences(void)
400 DWORD dwType, dwSize;
404 DWORD dwHostSize = 256;
406 struct sockaddr_in saddr;
409 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
410 AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\VLDB",
412 KEY_READ|KEY_QUERY_VALUE,
413 &hkPrefs) == ERROR_SUCCESS) {
415 RegQueryInfoKey( hkPrefs,
418 NULL, /* lpReserved */
419 NULL, /* lpcSubKeys */
420 NULL, /* lpcMaxSubKeyLen */
421 NULL, /* lpcMaxClassLen */
422 &dwPrefs, /* lpcValues */
423 NULL, /* lpcMaxValueNameLen */
424 NULL, /* lpcMaxValueLen */
425 NULL, /* lpcbSecurityDescriptor */
426 NULL /* lpftLastWriteTime */
429 for ( dwIndex = 0 ; dwIndex < dwPrefs; dwIndex++ ) {
431 dwSize = sizeof(DWORD);
434 if (RegEnumValue( hkPrefs, dwIndex, szHost, &dwHostSize, NULL,
435 &dwType, (LPBYTE)&dwRank, &dwSize))
437 afsi_log("RegEnumValue(hkPrefs) failed");
441 afsi_log("VLDB Server Preference: %s = %d",szHost, dwRank);
443 if (isdigit(szHost[0]))
445 if ((saddr.sin_addr.S_un.S_addr = inet_addr (szHost)) == INADDR_NONE)
449 if ((pEntry = gethostbyname (szHost)) == NULL)
452 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
454 saddr.sin_family = AF_INET;
455 dwRank += (rand() & 0x000f);
457 tsp = cm_FindServer(&saddr, CM_SERVER_VLDB);
458 if ( tsp ) /* an existing server - ref count increased */
460 tsp->ipRank = (USHORT)dwRank; /* no need to protect by mutex*/
462 /* set preferences for an existing vlserver */
463 cm_ChangeRankCellVLServer(tsp);
464 cm_PutServer(tsp); /* decrease refcount */
466 else /* add a new server without a cell */
468 tsp = cm_NewServer(&saddr, CM_SERVER_VLDB, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
469 tsp->ipRank = (USHORT)dwRank;
473 RegCloseKey(hkPrefs);
476 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
477 AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\File",
479 KEY_READ|KEY_QUERY_VALUE,
480 &hkPrefs) == ERROR_SUCCESS) {
482 RegQueryInfoKey( hkPrefs,
485 NULL, /* lpReserved */
486 NULL, /* lpcSubKeys */
487 NULL, /* lpcMaxSubKeyLen */
488 NULL, /* lpcMaxClassLen */
489 &dwPrefs, /* lpcValues */
490 NULL, /* lpcMaxValueNameLen */
491 NULL, /* lpcMaxValueLen */
492 NULL, /* lpcbSecurityDescriptor */
493 NULL /* lpftLastWriteTime */
496 for ( dwIndex = 0 ; dwIndex < dwPrefs; dwIndex++ ) {
498 dwSize = sizeof(DWORD);
501 if (RegEnumValue( hkPrefs, dwIndex, szHost, &dwHostSize, NULL,
502 &dwType, (LPBYTE)&dwRank, &dwSize))
504 afsi_log("RegEnumValue(hkPrefs) failed");
508 afsi_log("File Server Preference: %s = %d",szHost, dwRank);
510 if (isdigit(szHost[0]))
512 if ((saddr.sin_addr.S_un.S_addr = inet_addr (szHost)) == INADDR_NONE)
516 if ((pEntry = gethostbyname (szHost)) == NULL)
519 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
521 saddr.sin_family = AF_INET;
522 dwRank += (rand() & 0x000f);
524 tsp = cm_FindServer(&saddr, CM_SERVER_FILE);
525 if ( tsp ) /* an existing server - ref count increased */
527 tsp->ipRank = (USHORT)dwRank; /* no need to protect by mutex*/
529 /* find volumes which might have RO copy
530 /* on server and change the ordering of
533 cm_ChangeRankVolume(tsp);
534 cm_PutServer(tsp); /* decrease refcount */
536 else /* add a new server without a cell */
538 tsp = cm_NewServer(&saddr, CM_SERVER_FILE, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
539 tsp->ipRank = (USHORT)dwRank;
543 RegCloseKey(hkPrefs);
548 * AFSD Initialization
551 int afsd_InitCM(char **reasonP)
554 afs_uint64 cacheBlocks;
560 DWORD rx_enable_peer_stats = 0;
561 DWORD rx_enable_process_stats = 0;
566 long virtualCache = 0;
567 char rootCellName[256];
568 struct rx_service *serverp;
569 static struct rx_securityClass *nullServerSecurityClassp;
577 /*int freelanceEnabled;*/
581 int cm_noIPAddr; /* number of client network interfaces */
582 int cm_IPAddr[CM_MAXINTERFACE_ADDR]; /* client's IP address in host order */
583 int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
584 int cm_NetMtu[CM_MAXINTERFACE_ADDR]; /* client's MTU sizes */
585 int cm_NetFlags[CM_MAXINTERFACE_ADDR]; /* network flags */
587 WSAStartup(0x0101, &WSAjunk);
589 afsd_initUpperCaseTable();
590 init_et_to_sys_error();
592 /* setup osidebug server at RPC slot 1000 */
593 osi_LongToUID(1000, &debugID);
594 code = osi_InitDebug(&debugID);
595 afsi_log("osi_InitDebug code %d", code);
597 // osi_LockTypeSetDefault("stat"); /* comment this out for speed */
599 if (code == RPC_S_NO_PROTSEQS)
600 *reasonP = "No RPC Protocol Sequences registered. Check HKLM\\SOFTWARE\\Microsoft\\RPC\\ClientProtocols";
602 *reasonP = "unknown error";
607 gethostname(cm_HostName, sizeof(cm_HostName));
608 afsi_log("gethostname %s", cm_HostName);
609 thp = gethostbyname(cm_HostName);
610 memcpy(&cm_HostAddr, thp->h_addr_list[0], 4);
612 /* seed random number generator */
613 srand(ntohl(cm_HostAddr));
615 /* Look up configuration parameters in Registry */
616 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
617 0, KEY_QUERY_VALUE, &parmKey);
618 if (code != ERROR_SUCCESS) {
619 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
620 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
621 NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
622 StringCbPrintfA(buf, sizeof(buf),
623 "Failure in configuration while opening Registry: %s",
625 osi_panic(buf, __FILE__, __LINE__);
628 dummyLen = sizeof(maxcpus);
629 code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
630 (BYTE *) &maxcpus, &dummyLen);
631 if (code == ERROR_SUCCESS) {
633 DWORD_PTR processAffinityMask, systemAffinityMask;
635 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION,
636 FALSE, GetCurrentProcessId());
637 if ( hProcess != NULL &&
638 GetProcessAffinityMask(hProcess, &processAffinityMask, &systemAffinityMask) )
641 DWORD_PTR mask, newAffinityMask;
648 for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits && n<maxcpus; i++ ) {
649 if ( processAffinityMask & mask ) {
650 newAffinityMask |= mask;
656 SetProcessAffinityMask(hProcess, newAffinityMask);
657 CloseHandle(hProcess);
658 afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, n);
660 afsi_log("CPU Restrictions set to %d cpu(s); unable to access process information", maxcpus);
664 dummyLen = sizeof(TraceOption);
665 code = RegQueryValueEx(parmKey, "TraceOption", NULL, NULL,
666 (BYTE *) &TraceOption, &dummyLen);
667 afsi_log("Trace Options = %lX", TraceOption);
669 dummyLen = sizeof(traceBufSize);
670 code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
671 (BYTE *) &traceBufSize, &dummyLen);
672 if (code == ERROR_SUCCESS)
673 afsi_log("Trace Buffer size %d", traceBufSize);
675 traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
676 afsi_log("Default trace buffer size %d", traceBufSize);
679 /* setup and enable debug log */
680 afsd_logp = osi_LogCreate("afsd", traceBufSize);
681 afsi_log("osi_LogCreate log addr %x", PtrToUlong(afsd_logp));
682 if ((TraceOption & 0x8)
687 osi_LogEnable(afsd_logp);
691 osi_Log0(afsd_logp, "Log init");
693 dummyLen = sizeof(cacheSize);
694 code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
695 (BYTE *) &cacheSize, &dummyLen);
696 if (code == ERROR_SUCCESS)
697 afsi_log("Cache size %d", cacheSize);
699 cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
700 afsi_log("Default cache size %d", cacheSize);
703 dummyLen = sizeof(logChunkSize);
704 code = RegQueryValueEx(parmKey, "ChunkSize", NULL, NULL,
705 (BYTE *) &logChunkSize, &dummyLen);
706 if (code == ERROR_SUCCESS) {
707 if (logChunkSize < 12 || logChunkSize > 30) {
708 afsi_log("Invalid chunk size %d, using default",
710 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
713 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
715 cm_logChunkSize = logChunkSize;
716 cm_chunkSize = 1 << logChunkSize;
717 afsi_log("Chunk size %u (%d)", cm_chunkSize, cm_logChunkSize);
719 dummyLen = sizeof(blockSize);
720 code = RegQueryValueEx(parmKey, "blockSize", NULL, NULL,
721 (BYTE *) &blockSize, &dummyLen);
722 if (code == ERROR_SUCCESS) {
724 (blockSize > 1024 && (blockSize % CM_CONFIGDEFAULT_BLOCKSIZE != 0)))
726 afsi_log("Invalid block size %u specified, using default", blockSize);
727 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
730 * if the blockSize is less than 1024 we permit the blockSize to be
731 * specified in multiples of the default blocksize
733 if (blockSize <= 1024)
734 blockSize *= CM_CONFIGDEFAULT_BLOCKSIZE;
737 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
739 if (blockSize > cm_chunkSize) {
740 afsi_log("Block size (%d) cannot be larger than Chunk size (%d).",
741 blockSize, cm_chunkSize);
742 blockSize = cm_chunkSize;
744 if (cm_chunkSize % blockSize != 0) {
745 afsi_log("Block size (%d) must be a factor of Chunk size (%d).",
746 blockSize, cm_chunkSize);
747 blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
749 afsi_log("Block size %u", blockSize);
751 dummyLen = sizeof(numBkgD);
752 code = RegQueryValueEx(parmKey, "Daemons", NULL, NULL,
753 (BYTE *) &numBkgD, &dummyLen);
754 if (code == ERROR_SUCCESS) {
755 if (numBkgD > CM_MAX_DAEMONS)
756 numBkgD = CM_MAX_DAEMONS;
757 afsi_log("%d background daemons", numBkgD);
759 numBkgD = CM_CONFIGDEFAULT_DAEMONS;
760 afsi_log("Defaulting to %d background daemons", numBkgD);
763 dummyLen = sizeof(numSvThreads);
764 code = RegQueryValueEx(parmKey, "ServerThreads", NULL, NULL,
765 (BYTE *) &numSvThreads, &dummyLen);
766 if (code == ERROR_SUCCESS)
767 afsi_log("%d server threads", numSvThreads);
769 numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
770 afsi_log("Defaulting to %d server threads", numSvThreads);
773 dummyLen = sizeof(stats);
774 code = RegQueryValueEx(parmKey, "Stats", NULL, NULL,
775 (BYTE *) &stats, &dummyLen);
776 if (code == ERROR_SUCCESS)
777 afsi_log("Status cache size %d", stats);
779 stats = CM_CONFIGDEFAULT_STATS;
780 afsi_log("Default status cache size %d", stats);
783 dummyLen = sizeof(ltt);
784 code = RegQueryValueEx(parmKey, "LogoffTokenTransfer", NULL, NULL,
785 (BYTE *) <t, &dummyLen);
786 if (code != ERROR_SUCCESS)
788 smb_LogoffTokenTransfer = ltt;
789 afsi_log("Logoff token transfer %s", (ltt ? "on" : "off"));
792 dummyLen = sizeof(ltto);
793 code = RegQueryValueEx(parmKey, "LogoffTokenTransferTimeout",
794 NULL, NULL, (BYTE *) <to, &dummyLen);
795 if (code != ERROR_SUCCESS)
800 smb_LogoffTransferTimeout = ltto;
801 afsi_log("Logoff token transfer timeout %d seconds", ltto);
803 dummyLen = sizeof(cm_rootVolumeName);
804 code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
805 cm_rootVolumeName, &dummyLen);
806 if (code == ERROR_SUCCESS)
807 afsi_log("Root volume %s", cm_rootVolumeName);
809 StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
810 afsi_log("Default root volume name root.afs");
813 cm_mountRootLen = sizeof(cm_mountRoot);
814 code = RegQueryValueEx(parmKey, "MountRoot", NULL, NULL,
815 cm_mountRoot, &cm_mountRootLen);
816 if (code == ERROR_SUCCESS) {
817 afsi_log("Mount root %s", cm_mountRoot);
818 cm_mountRootLen = (DWORD)strlen(cm_mountRoot);
820 StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
825 dummyLen = sizeof(buf);
826 code = RegQueryValueEx(parmKey, "CachePath", NULL, ®Type,
828 if (code == ERROR_SUCCESS && buf[0]) {
829 if (regType == REG_EXPAND_SZ) {
830 dummyLen = ExpandEnvironmentStrings(buf, cm_CachePath, sizeof(cm_CachePath));
831 if (dummyLen > sizeof(cm_CachePath)) {
832 afsi_log("Cache path [%s] longer than %d after expanding env strings", buf, sizeof(cm_CachePath));
833 osi_panic("CachePath too long", __FILE__, __LINE__);
836 StringCbCopyA(cm_CachePath, sizeof(cm_CachePath), buf);
838 afsi_log("Cache path %s", cm_CachePath);
840 dummyLen = ExpandEnvironmentStrings("%TEMP%\\AFSCache", cm_CachePath, sizeof(cm_CachePath));
841 if (dummyLen > sizeof(cm_CachePath)) {
842 afsi_log("Cache path [%%TEMP%%\\AFSCache] longer than %d after expanding env strings",
843 sizeof(cm_CachePath));
844 osi_panic("CachePath too long", __FILE__, __LINE__);
846 afsi_log("Default cache path %s", cm_CachePath);
849 dummyLen = sizeof(virtualCache);
850 code = RegQueryValueEx(parmKey, "NonPersistentCaching", NULL, NULL,
851 (LPBYTE)&virtualCache, &dummyLen);
852 afsi_log("Cache type is %s", (virtualCache?"VIRTUAL":"FILE"));
855 dummyLen = sizeof(cm_ValidateCache);
856 code = RegQueryValueEx(parmKey, "ValidateCache", NULL, NULL,
857 (LPBYTE)&cm_ValidateCache, &dummyLen);
858 if ( cm_ValidateCache < 0 || cm_ValidateCache > 2 )
859 cm_ValidateCache = 1;
860 switch (cm_ValidateCache) {
862 afsi_log("Cache Validation disabled");
865 afsi_log("Cache Validation on Startup");
868 afsi_log("Cache Validation on Startup and Shutdown");
873 dummyLen = sizeof(traceOnPanic);
874 code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
875 (BYTE *) &traceOnPanic, &dummyLen);
876 if (code != ERROR_SUCCESS)
877 traceOnPanic = 1; /* log */
878 afsi_log("Set to %s on panic", traceOnPanic ? "trap" : "not trap");
880 dummyLen = sizeof(reportSessionStartups);
881 code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
882 (BYTE *) &reportSessionStartups, &dummyLen);
883 if (code == ERROR_SUCCESS)
884 afsi_log("Session startups %s be recorded in the Event Log",
885 reportSessionStartups ? "will" : "will not");
887 reportSessionStartups = 0;
891 for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
892 cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
893 cm_sysNameList[i][0] = '\0';
895 cm_sysName = cm_sysNameList[0];
897 dummyLen = sizeof(buf);
898 code = RegQueryValueEx(parmKey, "SysName", NULL, NULL, buf, &dummyLen);
899 if (code != ERROR_SUCCESS || !buf[0]) {
901 StringCbCopyA(buf, sizeof(buf), "ia64_win64");
902 #elif defined(_AMD64_)
903 StringCbCopyA(buf, sizeof(buf), "amd64_win64 x86_win32 i386_w2k");
904 #else /* assume x86 32-bit */
905 StringCbCopyA(buf, sizeof(buf), "x86_win32 i386_w2k i386_nt40");
908 afsi_log("Sys name %s", buf);
910 /* breakup buf into individual search string entries */
911 for (p = q = buf; p < buf + dummyLen; p++)
913 if (*p == '\0' || isspace(*p)) {
914 memcpy(cm_sysNameList[cm_sysNameCount],q,p-q);
915 cm_sysNameList[cm_sysNameCount][p-q] = '\0';
922 } while (*p == '\0' || isspace(*p));
928 StringCbCopyA(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
930 dummyLen = sizeof(cryptall);
931 code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
932 (BYTE *) &cryptall, &dummyLen);
933 if (code == ERROR_SUCCESS) {
934 afsi_log("SecurityLevel is %s", cryptall?"crypt":"clear");
937 afsi_log("Default SecurityLevel is clear");
941 LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_ON);
943 LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_OFF);
945 dummyLen = sizeof(cryptall);
946 code = RegQueryValueEx(parmKey, "ForceAnonVLDB", NULL, NULL,
947 (BYTE *) &cm_anonvldb, &dummyLen);
948 afsi_log("CM ForceAnonVLDB is %s", cm_anonvldb ? "on" : "off");
951 dummyLen = sizeof(cm_dnsEnabled);
952 code = RegQueryValueEx(parmKey, "UseDNS", NULL, NULL,
953 (BYTE *) &cm_dnsEnabled, &dummyLen);
954 if (code == ERROR_SUCCESS) {
955 afsi_log("DNS %s be used to find AFS cell servers",
956 cm_dnsEnabled ? "will" : "will not");
959 cm_dnsEnabled = 1; /* default on */
960 afsi_log("Default to use DNS to find AFS cell servers");
962 #else /* AFS_AFSDB_ENV */
963 afsi_log("AFS not built with DNS support to find AFS cell servers");
964 #endif /* AFS_AFSDB_ENV */
966 #ifdef AFS_FREELANCE_CLIENT
967 dummyLen = sizeof(cm_freelanceEnabled);
968 code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
969 (BYTE *) &cm_freelanceEnabled, &dummyLen);
970 if (code == ERROR_SUCCESS) {
971 afsi_log("Freelance client feature %s activated",
972 cm_freelanceEnabled ? "is" : "is not");
975 cm_freelanceEnabled = 1; /* default on */
977 #endif /* AFS_FREELANCE_CLIENT */
979 dummyLen = sizeof(smb_hideDotFiles);
980 code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
981 (BYTE *) &smb_hideDotFiles, &dummyLen);
982 if (code != ERROR_SUCCESS) {
983 smb_hideDotFiles = 1; /* default on */
985 afsi_log("Dot files/dirs will %sbe marked hidden",
986 smb_hideDotFiles ? "" : "not ");
988 dummyLen = sizeof(smb_maxMpxRequests);
989 code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
990 (BYTE *) &smb_maxMpxRequests, &dummyLen);
991 if (code != ERROR_SUCCESS) {
992 smb_maxMpxRequests = 50;
994 afsi_log("Maximum number of multiplexed sessions is %d", smb_maxMpxRequests);
996 dummyLen = sizeof(smb_maxVCPerServer);
997 code = RegQueryValueEx(parmKey, "MaxVCPerServer", NULL, NULL,
998 (BYTE *) &smb_maxVCPerServer, &dummyLen);
999 if (code != ERROR_SUCCESS) {
1000 smb_maxVCPerServer = 100;
1002 afsi_log("Maximum number of VCs per server is %d", smb_maxVCPerServer);
1004 dummyLen = sizeof(smb_authType);
1005 code = RegQueryValueEx(parmKey, "SMBAuthType", NULL, NULL,
1006 (BYTE *) &smb_authType, &dummyLen);
1008 if (code != ERROR_SUCCESS ||
1009 (smb_authType != SMB_AUTH_EXTENDED && smb_authType != SMB_AUTH_NTLM && smb_authType != SMB_AUTH_NONE)) {
1010 smb_authType = SMB_AUTH_EXTENDED; /* default is to use extended authentication */
1012 afsi_log("SMB authentication type is %s", ((smb_authType == SMB_AUTH_NONE)?"NONE":((smb_authType == SMB_AUTH_EXTENDED)?"EXTENDED":"NTLM")));
1014 dummyLen = sizeof(rx_nojumbo);
1015 code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
1016 (BYTE *) &rx_nojumbo, &dummyLen);
1017 if (code != ERROR_SUCCESS) {
1021 afsi_log("RX Jumbograms are disabled");
1023 dummyLen = sizeof(rx_extraPackets);
1024 code = RegQueryValueEx(parmKey, "RxExtraPackets", NULL, NULL,
1025 (BYTE *) &rx_extraPackets, &dummyLen);
1026 if (code != ERROR_SUCCESS) {
1027 rx_extraPackets = 120;
1029 if (rx_extraPackets)
1030 afsi_log("RX extraPackets is %d", rx_extraPackets);
1032 dummyLen = sizeof(rx_mtu);
1033 code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
1034 (BYTE *) &rx_mtu, &dummyLen);
1035 if (code != ERROR_SUCCESS || !rx_mtu) {
1039 afsi_log("RX maximum MTU is %d", rx_mtu);
1041 dummyLen = sizeof(rx_enable_peer_stats);
1042 code = RegQueryValueEx(parmKey, "RxEnablePeerStats", NULL, NULL,
1043 (BYTE *) &rx_enable_peer_stats, &dummyLen);
1044 if (code != ERROR_SUCCESS) {
1045 rx_enable_peer_stats = 0;
1047 if (rx_enable_peer_stats)
1048 afsi_log("RX Peer Statistics gathering is enabled");
1050 dummyLen = sizeof(rx_enable_process_stats);
1051 code = RegQueryValueEx(parmKey, "RxEnableProcessStats", NULL, NULL,
1052 (BYTE *) &rx_enable_process_stats, &dummyLen);
1053 if (code != ERROR_SUCCESS) {
1054 rx_enable_process_stats = 0;
1056 if (rx_enable_process_stats)
1057 afsi_log("RX Process Statistics gathering is enabled");
1059 dummyLen = sizeof(dwValue);
1061 code = RegQueryValueEx(parmKey, "RxEnableHotThread", NULL, NULL,
1062 (BYTE *) &dwValue, &dummyLen);
1063 if (code != ERROR_SUCCESS || dwValue != 0) {
1064 rx_EnableHotThread();
1065 afsi_log("RX Hot Thread is enabled");
1068 afsi_log("RX Hot Thread is disabled");
1070 dummyLen = sizeof(DWORD);
1071 code = RegQueryValueEx(parmKey, "CallBackPort", NULL, NULL,
1072 (BYTE *) &dwValue, &dummyLen);
1073 if (code == ERROR_SUCCESS) {
1074 cm_callbackport = (unsigned short) dwValue;
1076 afsi_log("CM CallBackPort is %u", cm_callbackport);
1078 dummyLen = sizeof(DWORD);
1079 code = RegQueryValueEx(parmKey, "EnableServerLocks", NULL, NULL,
1080 (BYTE *) &dwValue, &dummyLen);
1081 if (code == ERROR_SUCCESS) {
1082 cm_enableServerLocks = (unsigned short) dwValue;
1084 switch (cm_enableServerLocks) {
1086 afsi_log("EnableServerLocks: never");
1089 afsi_log("EnableServerLocks: always");
1093 afsi_log("EnableServerLocks: server requested");
1097 dummyLen = sizeof(DWORD);
1098 code = RegQueryValueEx(parmKey, "DeleteReadOnly", NULL, NULL,
1099 (BYTE *) &dwValue, &dummyLen);
1100 if (code == ERROR_SUCCESS) {
1101 cm_deleteReadOnly = (unsigned short) dwValue;
1103 afsi_log("CM DeleteReadOnly is %u", cm_deleteReadOnly);
1106 dummyLen = sizeof(DWORD);
1107 code = RegQueryValueEx(parmKey, "BPlusTrees", NULL, NULL,
1108 (BYTE *) &dwValue, &dummyLen);
1109 if (code == ERROR_SUCCESS) {
1110 cm_BPlusTrees = (unsigned short) dwValue;
1112 afsi_log("CM BPlusTrees is %u", cm_BPlusTrees);
1114 if (cm_BPlusTrees && !cm_InitBPlusDir()) {
1116 afsi_log("CM BPlusTree initialization failure; disabled for this session");
1119 afsi_log("CM BPlusTrees is not supported");
1122 if ((RegQueryValueEx( parmKey, "PrefetchExecutableExtensions", 0,
1123 ®Type, NULL, &dummyLen) == ERROR_SUCCESS) &&
1124 (regType == REG_MULTI_SZ))
1127 dummyLen += 3; /* in case the source string is not nul terminated */
1128 pSz = malloc(dummyLen);
1129 if ((RegQueryValueEx( parmKey, "PrefetchExecutableExtensions", 0, ®Type,
1130 pSz, &dummyLen) == ERROR_SUCCESS) &&
1131 (regType == REG_MULTI_SZ))
1136 for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += strlen(p) + 1);
1138 smb_ExecutableExtensions = malloc(sizeof(char *) * (cnt+1));
1140 for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += strlen(p) + 1)
1142 smb_ExecutableExtensions[cnt] = p;
1143 afsi_log("PrefetchExecutableExtension: \"%s\"", p);
1145 smb_ExecutableExtensions[cnt] = NULL;
1148 if (!smb_ExecutableExtensions)
1151 if (!smb_ExecutableExtensions)
1152 afsi_log("No PrefetchExecutableExtensions");
1154 dummyLen = sizeof(DWORD);
1155 code = RegQueryValueEx(parmKey, "OfflineReadOnlyIsValid", NULL, NULL,
1156 (BYTE *) &dwValue, &dummyLen);
1157 if (code == ERROR_SUCCESS) {
1158 cm_OfflineROIsValid = (unsigned short) dwValue;
1160 afsi_log("CM OfflineReadOnlyIsValid is %u", cm_deleteReadOnly);
1162 dummyLen = sizeof(DWORD);
1163 code = RegQueryValueEx(parmKey, "GiveUpAllCallBacks", NULL, NULL,
1164 (BYTE *) &dwValue, &dummyLen);
1165 if (code == ERROR_SUCCESS) {
1166 cm_giveUpAllCBs = (unsigned short) dwValue;
1168 afsi_log("CM GiveUpAllCallBacks is %u", cm_giveUpAllCBs);
1170 dummyLen = sizeof(DWORD);
1171 code = RegQueryValueEx(parmKey, "FollowBackupPath", NULL, NULL,
1172 (BYTE *) &dwValue, &dummyLen);
1173 if (code == ERROR_SUCCESS) {
1174 cm_followBackupPath = (unsigned short) dwValue;
1176 afsi_log("CM FollowBackupPath is %u", cm_followBackupPath);
1178 RegCloseKey (parmKey);
1180 cacheBlocks = ((afs_uint64)cacheSize * 1024) / blockSize;
1182 /* get network related info */
1183 cm_noIPAddr = CM_MAXINTERFACE_ADDR;
1184 code = syscfg_GetIFInfo(&cm_noIPAddr,
1185 cm_IPAddr, cm_SubnetMask,
1186 cm_NetMtu, cm_NetFlags);
1188 if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
1189 afsi_log("syscfg_GetIFInfo error code %d", code);
1191 afsi_log("First Network address %x SubnetMask %x",
1192 cm_IPAddr[0], cm_SubnetMask[0]);
1195 * Save client configuration for GetCacheConfig requests
1197 cm_initParams.nChunkFiles = 0;
1198 cm_initParams.nStatCaches = stats;
1199 cm_initParams.nDataCaches = (afs_uint32)(cacheBlocks > 0xFFFFFFFF ? 0xFFFFFFFF : cacheBlocks);
1200 cm_initParams.nVolumeCaches = stats/2;
1201 cm_initParams.firstChunkSize = cm_chunkSize;
1202 cm_initParams.otherChunkSize = cm_chunkSize;
1203 cm_initParams.cacheSize = cacheSize;
1204 cm_initParams.setTime = 0;
1205 cm_initParams.memCache = 1;
1207 /* Ensure the AFS Netbios Name is registered to allow loopback access */
1208 configureBackConnectionHostNames();
1210 /* init user daemon, and other packages */
1223 code = cm_InitMappedMemory(virtualCache, cm_CachePath, stats, cm_chunkSize, cacheBlocks, blockSize);
1224 afsi_log("cm_InitMappedMemory code %x", code);
1226 *reasonP = "error initializing cache file";
1230 #ifdef AFS_AFSDB_ENV
1231 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
1232 if (cm_InitDNS(cm_dnsEnabled) == -1)
1233 cm_dnsEnabled = 0; /* init failed, so deactivate */
1234 afsi_log("cm_InitDNS %d", cm_dnsEnabled);
1238 /* Set RX parameters before initializing RX */
1241 afsi_log("rx_SetNoJumbo successful");
1244 if ( rx_mtu != -1 ) {
1245 rx_SetMaxMTU(rx_mtu);
1246 afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
1249 /* initialize RX, and tell it to listen to the callbackport,
1250 * which is used for callback RPC messages.
1252 code = rx_Init(htons(cm_callbackport));
1254 afsi_log("rx_Init code %x - retrying with a random port number", code);
1257 afsi_log("rx_Init code %x", code);
1259 *reasonP = "afsd: failed to init rx client";
1263 /* create an unauthenticated service #1 for callbacks */
1264 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
1265 serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
1266 RXAFSCB_ExecuteRequest);
1267 afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
1268 if (serverp == NULL) {
1269 *reasonP = "unknown error";
1273 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
1274 serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
1275 &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
1276 afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
1277 if (serverp == NULL) {
1278 *reasonP = "unknown error";
1282 /* start server threads, *not* donating this one to the pool */
1284 afsi_log("rx_StartServer");
1286 if (rx_enable_peer_stats)
1287 rx_enablePeerRPCStats();
1289 if (rx_enable_process_stats)
1290 rx_enableProcessRPCStats();
1292 code = cm_GetRootCellName(rootCellName);
1293 afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s",
1294 code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
1295 if (code != 0 && !cm_freelanceEnabled)
1297 *reasonP = "can't find root cell name in " AFS_CELLSERVDB;
1300 else if (cm_freelanceEnabled)
1301 cm_data.rootCellp = NULL;
1303 if (code == 0 && !cm_freelanceEnabled)
1305 cm_data.rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
1306 afsi_log("cm_GetCell addr %x", PtrToUlong(cm_data.rootCellp));
1307 if (cm_data.rootCellp == NULL)
1309 *reasonP = "can't find root cell in " AFS_CELLSERVDB;
1314 #ifdef AFS_FREELANCE_CLIENT
1315 if (cm_freelanceEnabled)
1319 /* Initialize the RPC server for session keys */
1322 afsd_InitServerPreferences();
1326 int afsd_ShutdownCM(void)
1328 cm_ReleaseSCache(cm_data.rootSCachep);
1333 int afsd_InitDaemons(char **reasonP)
1340 /* this should really be in an init daemon from here on down */
1342 if (!cm_freelanceEnabled) {
1345 osi_Log0(afsd_logp, "Loading Root Volume from cell");
1347 code = cm_GetVolumeByName(cm_data.rootCellp, cm_rootVolumeName, cm_rootUserp,
1348 &req, CM_GETVOL_FLAG_CREATE, &cm_data.rootVolumep);
1349 afsi_log("cm_GetVolumeByName code %x root vol %x", code,
1350 (code ? (cm_volume_t *)-1 : cm_data.rootVolumep));
1351 } while (code && --attempts);
1353 *reasonP = "can't find root volume in root cell";
1358 /* compute the root fid */
1359 if (!cm_freelanceEnabled) {
1360 cm_SetFid(&cm_data.rootFid, cm_data.rootCellp->cellID, cm_GetROVolumeID(cm_data.rootVolumep), 1, 1);
1363 cm_FakeRootFid(&cm_data.rootFid);
1365 code = cm_GetSCache(&cm_data.rootFid, &cm_data.rootSCachep, cm_rootUserp, &req);
1366 afsi_log("cm_GetSCache code %x scache %x", code,
1367 (code ? (cm_scache_t *)-1 : cm_data.rootSCachep));
1369 *reasonP = "unknown error";
1373 cm_InitDaemon(numBkgD);
1374 afsi_log("cm_InitDaemon complete");
1379 int afsd_InitSMB(char **reasonP, void *aMBfunc)
1386 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
1387 0, KEY_QUERY_VALUE, &parmKey);
1388 if (code == ERROR_SUCCESS) {
1389 dummyLen = sizeof(DWORD);
1390 code = RegQueryValueEx(parmKey, "StoreAnsiFilenames", NULL, NULL,
1391 (BYTE *) &dwValue, &dummyLen);
1392 if (code == ERROR_SUCCESS)
1393 smb_StoreAnsiFilenames = dwValue ? 1 : 0;
1394 afsi_log("StoreAnsiFilenames = %d", smb_StoreAnsiFilenames);
1396 dummyLen = sizeof(DWORD);
1397 code = RegQueryValueEx(parmKey, "EnableSMBAsyncStore", NULL, NULL,
1398 (BYTE *) &dwValue, &dummyLen);
1399 if (code == ERROR_SUCCESS)
1400 smb_AsyncStore = dwValue == 2 ? 2 : (dwValue ? 1 : 0);
1401 afsi_log("EnableSMBAsyncStore = %d", smb_AsyncStore);
1403 dummyLen = sizeof(DWORD);
1404 code = RegQueryValueEx(parmKey, "SMBAsyncStoreSize", NULL, NULL,
1405 (BYTE *) &dwValue, &dummyLen);
1406 if (code == ERROR_SUCCESS) {
1407 /* Should check for >= blocksize && <= chunksize && round down to multiple of blocksize */
1408 if (dwValue > cm_chunkSize)
1409 smb_AsyncStoreSize = cm_chunkSize;
1410 else if (dwValue < cm_data.buf_blockSize)
1411 smb_AsyncStoreSize = cm_data.buf_blockSize;
1413 smb_AsyncStoreSize = (dwValue & ~(cm_data.buf_blockSize-1));
1415 smb_AsyncStoreSize = CM_CONFIGDEFAULT_ASYNCSTORESIZE;
1416 afsi_log("SMBAsyncStoreSize = %d", smb_AsyncStoreSize);
1418 RegCloseKey (parmKey);
1421 /* Do this last so that we don't handle requests before init is done.
1422 * Here we initialize the SMB listener.
1424 smb_Init(afsd_logp, smb_UseV3, numSvThreads, aMBfunc);
1425 afsi_log("smb_Init complete");
1438 #pragma pack( push, before_imagehlp, 8 )
1439 #include <imagehlp.h>
1440 #pragma pack( pop, before_imagehlp )
1442 #define MAXNAMELEN 1024
1444 void afsd_printStack(HANDLE hThread, CONTEXT *c)
1446 HANDLE hProcess = GetCurrentProcess();
1448 #if defined(_AMD64_)
1450 #elif defined(_X86_)
1454 char functionName[MAXNAMELEN];
1456 IMAGEHLP_MODULE Module;
1460 IMAGEHLP_SYMBOL *pSym;
1462 afsi_log_useTimestamp = 0;
1464 pSym = (IMAGEHLP_SYMBOL *) GlobalAlloc(0, sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN);
1466 memset( &s, '\0', sizeof s );
1467 if (!SymInitialize(hProcess, NULL, 1) )
1469 afsi_log("SymInitialize(): GetLastError() = %lu\n", GetLastError() );
1471 SymCleanup( hProcess );
1477 symOptions = SymGetOptions();
1478 symOptions |= SYMOPT_LOAD_LINES;
1479 symOptions &= ~SYMOPT_UNDNAME;
1480 SymSetOptions( symOptions );
1483 * init STACKFRAME for first call
1484 * Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
1485 * Notes: will have to be #ifdef-ed for Alphas; MIPSes are dead anyway,
1486 * and good riddance.
1488 #if defined (_ALPHA_) || defined (_MIPS_) || defined (_PPC_)
1489 #error The STACKFRAME initialization in afsd_printStack() for this platform
1490 #error must be properly configured
1491 #elif defined(_AMD64_)
1492 s.AddrPC.Offset = c->Rip;
1493 s.AddrPC.Mode = AddrModeFlat;
1494 s.AddrFrame.Offset = c->Rbp;
1495 s.AddrFrame.Mode = AddrModeFlat;
1497 s.AddrPC.Offset = c->Eip;
1498 s.AddrPC.Mode = AddrModeFlat;
1499 s.AddrFrame.Offset = c->Ebp;
1500 s.AddrFrame.Mode = AddrModeFlat;
1503 memset( pSym, '\0', sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN );
1504 pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL);
1505 pSym->MaxNameLength = MAXNAMELEN;
1507 memset( &Line, '\0', sizeof Line );
1508 Line.SizeOfStruct = sizeof Line;
1510 memset( &Module, '\0', sizeof Module );
1511 Module.SizeOfStruct = sizeof Module;
1515 afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
1517 for ( frameNum = 0; ; ++ frameNum )
1520 * get next stack frame (StackWalk(), SymFunctionTableAccess(),
1521 * SymGetModuleBase()). if this returns ERROR_INVALID_ADDRESS (487) or
1522 * ERROR_NOACCESS (998), you can assume that either you are done, or
1523 * that the stack is so hosed that the next deeper frame could not be
1526 if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c,
1527 NULL, SymFunctionTableAccess, SymGetModuleBase,
1531 /* display its contents */
1532 afsi_log("\n%3d %c%c %08lx %08lx %08lx %08lx ",
1533 frameNum, s.Far? 'F': '.', s.Virtual? 'V': '.',
1534 s.AddrPC.Offset, s.AddrReturn.Offset,
1535 s.AddrFrame.Offset, s.AddrStack.Offset );
1537 if ( s.AddrPC.Offset == 0 )
1539 afsi_log("(-nosymbols- PC == 0)" );
1543 /* show procedure info from a valid PC */
1544 if (!SymGetSymFromAddr(hProcess, s.AddrPC.Offset, &offset, pSym))
1546 if ( GetLastError() != ERROR_INVALID_ADDRESS )
1548 afsi_log("SymGetSymFromAddr(): errno = %lu",
1554 UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN,
1556 afsi_log("%s", functionName );
1560 afsi_log(" %+ld bytes", (long) offset);
1564 if (!SymGetLineFromAddr(hProcess, s.AddrPC.Offset, &offset, &Line))
1566 if (GetLastError() != ERROR_INVALID_ADDRESS)
1568 afsi_log("Error: SymGetLineFromAddr(): errno = %lu",
1574 afsi_log(" Line: %s(%lu) %+ld bytes", Line.FileName,
1575 Line.LineNumber, offset);
1579 /* no return address means no deeper stackframe */
1580 if (s.AddrReturn.Offset == 0)
1587 if (GetLastError() != 0)
1589 afsi_log("\nStackWalk(): errno = %lu\n", GetLastError());
1592 SymCleanup(hProcess);
1597 static DWORD *afsd_crtDbgBreakCurrent = NULL;
1598 static DWORD afsd_crtDbgBreaks[256];
1601 static EFaultRepRetVal (WINAPI *pReportFault)(LPEXCEPTION_POINTERS pep, DWORD dwMode) = NULL;
1602 static BOOL (WINAPI *pMiniDumpWriteDump)(HANDLE hProcess,DWORD ProcessId,HANDLE hFile,
1603 MINIDUMP_TYPE DumpType,
1604 PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
1605 PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
1606 PMINIDUMP_CALLBACK_INFORMATION CallbackParam) = NULL;
1615 code = GetEnvironmentVariable("TEMP", wd, sizeof(wd));
1616 if ( code == 0 || code > sizeof(wd) )
1618 if (!GetWindowsDirectory(wd, sizeof(wd)))
1621 StringCbCatA(wd, sizeof(wd), "\\afsd.dmp");
1622 return CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1623 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
1627 GenerateMiniDump(PEXCEPTION_POINTERS ep)
1629 if (IsDebuggerPresent())
1634 // Generate exception to get proper context in dump
1637 RaiseException(DBG_CONTINUE, 0, 0, NULL);
1639 __except(GenerateMiniDump(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION)
1645 MINIDUMP_EXCEPTION_INFORMATION eInfo;
1646 HANDLE hFile = NULL;
1647 HMODULE hDbgHelp = NULL;
1649 hDbgHelp = LoadLibrary("Dbghelp.dll");
1650 if ( hDbgHelp == NULL )
1653 (FARPROC) pMiniDumpWriteDump = GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
1654 if ( pMiniDumpWriteDump == NULL ) {
1655 FreeLibrary(hDbgHelp);
1659 hFile = OpenDumpFile();
1666 DWORD dwMiniDumpType = MiniDumpWithDataSegs;
1668 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
1669 0, KEY_QUERY_VALUE, &parmKey);
1670 if (code == ERROR_SUCCESS) {
1671 dummyLen = sizeof(DWORD);
1672 code = RegQueryValueEx(parmKey, "MiniDumpType", NULL, NULL,
1673 (BYTE *) &dwValue, &dummyLen);
1674 if (code == ERROR_SUCCESS)
1675 dwMiniDumpType = dwValue;
1676 RegCloseKey (parmKey);
1679 eInfo.ThreadId = GetCurrentThreadId();
1680 eInfo.ExceptionPointers = ep;
1681 eInfo.ClientPointers = FALSE;
1683 pMiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
1684 hFile, dwMiniDumpType, ep ? &eInfo : NULL,
1689 FreeLibrary(hDbgHelp);
1693 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
1697 BOOL allocRequestBrk = FALSE;
1699 HMODULE hLib = NULL;
1701 afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n",
1702 ep->ExceptionRecord->ExceptionCode,
1703 ep->ExceptionRecord->ExceptionAddress);
1706 if (afsd_crtDbgBreakCurrent &&
1707 *afsd_crtDbgBreakCurrent == _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent))
1709 allocRequestBrk = TRUE;
1710 afsi_log("Breaking on alloc request # %d\n", *afsd_crtDbgBreakCurrent);
1714 /* save context if we want to print the stack information */
1715 context = *ep->ContextRecord;
1717 afsd_printStack(GetCurrentThread(), &context);
1719 GenerateMiniDump(ep);
1721 hLib = LoadLibrary("Faultrep.dll");
1723 (FARPROC) pReportFault = GetProcAddress(hLib, "ReportFault");
1725 pReportFault(ep, 0);
1729 if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
1731 afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
1734 if (allocRequestBrk)
1736 afsd_crtDbgBreakCurrent++;
1737 _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
1741 ep->ContextRecord->Eip++;
1743 #if defined(_AMD64_)
1744 ep->ContextRecord->Rip++;
1746 return EXCEPTION_CONTINUE_EXECUTION;
1750 return EXCEPTION_CONTINUE_SEARCH;
1754 void afsd_SetUnhandledExceptionFilter()
1756 SetUnhandledExceptionFilter(afsd_ExceptionFilter);
1760 void afsd_DbgBreakAllocInit()
1762 memset(afsd_crtDbgBreaks, -1, sizeof(afsd_crtDbgBreaks));
1763 afsd_crtDbgBreakCurrent = afsd_crtDbgBreaks;
1766 void afsd_DbgBreakAdd(DWORD requestNumber)
1769 for (i = 0; i < sizeof(afsd_crtDbgBreaks) - 1; i++)
1771 if (afsd_crtDbgBreaks[i] == -1)
1776 afsd_crtDbgBreaks[i] = requestNumber;
1778 _CrtSetBreakAlloc(afsd_crtDbgBreaks[0]);