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>
24 #include <rx\rx_null.h>
26 #include <WINNT/syscfg.h>
30 #include "lanahelper.h"
34 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
35 extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
37 extern afs_int32 cryptall;
39 char AFSConfigKeyName[] =
40 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
44 char cm_rootVolumeName[64];
45 DWORD cm_rootVolumeNameLen;
46 cm_volume_t *cm_rootVolumep = NULL;
47 cm_cell_t *cm_rootCellp = NULL;
49 cm_scache_t *cm_rootSCachep = NULL;
50 char cm_mountRoot[1024];
51 DWORD cm_mountRootLen;
54 #ifdef AFS_FREELANCE_CLIENT
56 #endif /* freelance */
69 char cm_HostName[200];
72 char cm_NetbiosName[MAX_NB_NAME_LENGTH] = "";
74 char cm_CachePath[MAX_PATH];
75 DWORD cm_CachePathLen;
77 BOOL isGateway = FALSE;
79 BOOL reportSessionStartups = FALSE;
81 cm_initparams_v1 cm_initParams;
84 unsigned int cm_sysNameCount = 0;
85 char *cm_sysNameList[MAXNUMSYSNAMES];
87 DWORD TraceOption = 0;
90 * AFSD Initialization Log
92 * This is distinct from the regular debug logging facility.
93 * Log items go directly to a file, not to an array in memory, so that even
94 * if AFSD crashes, the log can be inspected.
100 int cm_dnsEnabled = 1;
103 extern initUpperCaseTable();
104 void afsd_initUpperCaseTable()
106 initUpperCaseTable();
113 char t[100], u[100], *p, *path;
119 DWORD maxLogSize = 100 * 1024;
121 afsi_file = INVALID_HANDLE_VALUE;
124 StringCbCopyA(wd, sizeof(wd), getenv("TEMP"));
128 code = GetWindowsDirectory(wd, sizeof(wd));
129 if (code == 0) return;
131 StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
132 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
133 afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
134 OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
136 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
137 0, KEY_QUERY_VALUE, &parmKey);
138 if (code == ERROR_SUCCESS) {
139 dummyLen = sizeof(maxLogSize);
140 code = RegQueryValueEx(parmKey, "MaxLogSize", NULL, NULL,
141 (BYTE *) &maxLogSize, &dummyLen);
142 RegCloseKey (parmKey);
146 dwLow = GetFileSize( afsi_file, &dwHigh );
147 if ( dwHigh > 0 || dwLow >= maxLogSize ) {
148 CloseHandle(afsi_file);
149 afsi_file = CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
150 CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
154 SetFilePointer(afsi_file, 0, NULL, FILE_END);
155 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
156 StringCbCatA(t, sizeof(t), ": Create log file\n");
157 StringCbCatA(u, sizeof(u), ": Created log file\n");
158 WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
159 WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
161 path = getenv("PATH");
162 WriteFile(afsi_file, p, strlen(p), &zilch, NULL);
163 WriteFile(afsi_file, path, strlen(path), &zilch, NULL);
164 WriteFile(afsi_file, "\n", 1, &zilch, NULL);
167 static int afsi_log_useTimestamp = 1;
170 afsi_log(char *pattern, ...)
172 char s[256], t[100], d[100], u[512];
175 va_start(ap, pattern);
177 StringCbVPrintfA(s, sizeof(s), pattern, ap);
178 if ( afsi_log_useTimestamp ) {
179 GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
180 GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
181 StringCbPrintfA(u, sizeof(u), "%s %s: %s\n", d, t, s);
182 if (afsi_file != INVALID_HANDLE_VALUE)
183 WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
188 if (afsi_file != INVALID_HANDLE_VALUE)
189 WriteFile(afsi_file, s, strlen(s), &zilch, NULL);
194 * Standard AFSD trace
197 void afsd_ForceTrace(BOOL flush)
206 len = GetTempPath(sizeof(buf)-10, buf);
207 StringCbCopyA(&buf[len], sizeof(buf)-len, "/afsd.log");
208 handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
209 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
210 if (handle == INVALID_HANDLE_VALUE) {
212 osi_panic("Cannot create log file", __FILE__, __LINE__);
214 osi_LogPrint(afsd_logp, handle);
216 FlushFileBuffers(handle);
221 configureBackConnectionHostNames(void)
223 /* On Windows XP SP2, Windows 2003 SP1, and all future Windows operating systems
224 * there is a restriction on the use of SMB authentication on loopback connections.
225 * There are two work arounds available:
227 * (1) We can disable the check for matching host names. This does not
229 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
230 * "DisableLoopbackCheck"=dword:00000001
232 * (2) We can add the AFS SMB/CIFS service name to an approved list. This
233 * does require a reboot:
234 * [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0]
235 * "BackConnectionHostNames"=multi-sz
237 * The algorithm will be:
238 * (1) Check to see if cm_NetbiosName exists in the BackConnectionHostNames list
239 * (2a) If not, add it to the list. (This will not take effect until the next reboot.)
240 * (2b1) and check to see if DisableLoopbackCheck is set.
241 * (2b2) If not set, set the DisableLoopbackCheck value to 0x1
242 * (2b3) and create HKLM\SOFTWARE\OpenAFS\Client UnsetDisableLoopbackCheck
243 * (2c) else If cm_NetbiosName exists in the BackConnectionHostNames list,
244 * check for the UnsetDisableLoopbackCheck value.
245 * If set, set the DisableLoopbackCheck flag to 0x0
246 * and delete the UnsetDisableLoopbackCheck value
254 PBYTE pHostNames = NULL, pName;
255 BOOL bNameFound = FALSE;
257 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
258 "SYSTEM\\CurrentControlSet\\Control\\Lsa\\MSV1_0",
261 &hkMSV10) == ERROR_SUCCESS )
263 if (RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS) {
264 pHostNames = malloc(dwSize + strlen(cm_NetbiosName) + 1);
265 RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType, pHostNames, &dwSize);
267 for (pName = pHostNames; *pName ; pName += strlen(pName) + 1)
269 if ( !stricmp(pName, cm_NetbiosName) ) {
278 pName = pHostNames = malloc(strlen(cm_NetbiosName) + 2);
281 strcpy(pName, cm_NetbiosName);
282 pName += strlen(cm_NetbiosName) + 1;
283 *pName = '\0'; /* add a second nul terminator */
285 dwType = REG_MULTI_SZ;
286 dwSize += strlen(cm_NetbiosName) + 1;
287 RegSetValueEx( hkMSV10, "BackConnectionHostNames", 0, dwType, pHostNames, dwSize);
289 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
290 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
293 &hkLsa) == ERROR_SUCCESS )
295 dwSize = sizeof(DWORD);
296 if ( RegQueryValueEx( hkLsa, "DisableLoopbackCheck", 0, &dwType, &dwValue, &dwSize) != ERROR_SUCCESS ||
299 dwSize = sizeof(DWORD);
301 RegSetValueEx( hkLsa, "DisableLoopbackCheck", 0, dwType, &dwValue, dwSize);
303 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
304 "SOFTWARE\\OpenAFS\\Client",
307 REG_OPTION_NON_VOLATILE,
311 NULL) == ERROR_SUCCESS) {
314 dwSize = sizeof(DWORD);
316 RegSetValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, dwType, &dwValue, dwSize);
317 RegCloseKey(hkClient);
323 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
324 "SOFTWARE\\OpenAFS\\Client",
327 REG_OPTION_NON_VOLATILE,
331 NULL) == ERROR_SUCCESS) {
333 dwSize = sizeof(DWORD);
334 if ( RegQueryValueEx( hkClient, "RemoveDisableLoopbackCheck", 0, &dwType, &dwValue, &dwSize) == ERROR_SUCCESS &&
336 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
337 "SYSTEM\\CurrentControlSet\\Control\\Lsa",
340 &hkLsa) == ERROR_SUCCESS )
342 RegDeleteValue(hkLsa, "DisableLoopbackCheck");
346 RegDeleteValue(hkClient, "RemoveDisableLoopbackCheck");
347 RegCloseKey(hkClient);
350 RegCloseKey(hkMSV10);
356 * AFSD Initialization
359 int afsd_InitCM(char **reasonP)
369 long rx_mtu, rx_nojumbo;
371 char rootCellName[256];
372 struct rx_service *serverp;
373 static struct rx_securityClass *nullServerSecurityClassp;
381 /*int freelanceEnabled;*/
383 lana_number_t lanaNum;
386 WSAStartup(0x0101, &WSAjunk);
388 afsd_initUpperCaseTable();
390 /* setup osidebug server at RPC slot 1000 */
391 osi_LongToUID(1000, &debugID);
392 code = osi_InitDebug(&debugID);
393 afsi_log("osi_InitDebug code %d", code);
395 // osi_LockTypeSetDefault("stat"); /* comment this out for speed *
397 *reasonP = "unknown error";
402 gethostname(cm_HostName, sizeof(cm_HostName));
403 afsi_log("gethostname %s", cm_HostName);
404 thp = gethostbyname(cm_HostName);
405 memcpy(&cm_HostAddr, thp->h_addr_list[0], 4);
407 /* seed random number generator */
408 srand(ntohl(cm_HostAddr));
410 /* Look up configuration parameters in Registry */
411 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
412 0, KEY_QUERY_VALUE, &parmKey);
413 if (code != ERROR_SUCCESS) {
414 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
415 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
416 NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
417 StringCbPrintfA(buf, sizeof(buf),
418 "Failure in configuration while opening Registry: %s",
420 osi_panic(buf, __FILE__, __LINE__);
423 dummyLen = sizeof(maxcpus);
424 code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
425 (BYTE *) &maxcpus, &dummyLen);
426 if (code == ERROR_SUCCESS) {
428 DWORD_PTR processAffinityMask, systemAffinityMask;
430 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION,
431 FALSE, GetCurrentProcessId());
432 if ( hProcess != NULL &&
433 GetProcessAffinityMask(hProcess, &processAffinityMask, &systemAffinityMask) )
436 DWORD_PTR mask, newAffinityMask;
443 for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits && n<maxcpus; i++ ) {
444 if ( processAffinityMask & mask ) {
445 newAffinityMask |= mask;
451 SetProcessAffinityMask(hProcess, newAffinityMask);
452 CloseHandle(hProcess);
453 afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, n);
455 afsi_log("CPU Restrictions set to %d cpu(s); unable to access process information", maxcpus);
459 dummyLen = sizeof(TraceOption);
460 code = RegQueryValueEx(parmKey, "TraceOption", NULL, NULL,
461 (BYTE *) &TraceOption, &dummyLen);
462 afsi_log("Event Log Tracing = %lX", TraceOption);
464 dummyLen = sizeof(traceBufSize);
465 code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
466 (BYTE *) &traceBufSize, &dummyLen);
467 if (code == ERROR_SUCCESS)
468 afsi_log("Trace Buffer size %d", traceBufSize);
470 traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
471 afsi_log("Default trace buffer size %d", traceBufSize);
474 /* setup and enable debug log */
475 afsd_logp = osi_LogCreate("afsd", traceBufSize);
476 afsi_log("osi_LogCreate log addr %x", (int)afsd_logp);
477 osi_LogEnable(afsd_logp);
480 osi_Log0(afsd_logp, "Log init");
482 dummyLen = sizeof(cacheSize);
483 code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
484 (BYTE *) &cacheSize, &dummyLen);
485 if (code == ERROR_SUCCESS)
486 afsi_log("Cache size %d", cacheSize);
488 cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
489 afsi_log("Default cache size %d", cacheSize);
492 dummyLen = sizeof(logChunkSize);
493 code = RegQueryValueEx(parmKey, "ChunkSize", NULL, NULL,
494 (BYTE *) &logChunkSize, &dummyLen);
495 if (code == ERROR_SUCCESS) {
496 if (logChunkSize < 12 || logChunkSize > 30) {
497 afsi_log("Invalid chunk size %d, using default",
499 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
501 afsi_log("Chunk size %d", logChunkSize);
503 logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
504 afsi_log("Default chunk size %d", logChunkSize);
506 cm_logChunkSize = logChunkSize;
507 cm_chunkSize = 1 << logChunkSize;
509 dummyLen = sizeof(numBkgD);
510 code = RegQueryValueEx(parmKey, "Daemons", NULL, NULL,
511 (BYTE *) &numBkgD, &dummyLen);
512 if (code == ERROR_SUCCESS)
513 afsi_log("%d background daemons", numBkgD);
515 numBkgD = CM_CONFIGDEFAULT_DAEMONS;
516 afsi_log("Defaulting to %d background daemons", numBkgD);
519 dummyLen = sizeof(numSvThreads);
520 code = RegQueryValueEx(parmKey, "ServerThreads", NULL, NULL,
521 (BYTE *) &numSvThreads, &dummyLen);
522 if (code == ERROR_SUCCESS)
523 afsi_log("%d server threads", numSvThreads);
525 numSvThreads = CM_CONFIGDEFAULT_SVTHREADS;
526 afsi_log("Defaulting to %d server threads", numSvThreads);
529 dummyLen = sizeof(stats);
530 code = RegQueryValueEx(parmKey, "Stats", NULL, NULL,
531 (BYTE *) &stats, &dummyLen);
532 if (code == ERROR_SUCCESS)
533 afsi_log("Status cache size %d", stats);
535 stats = CM_CONFIGDEFAULT_STATS;
536 afsi_log("Default status cache size %d", stats);
539 dummyLen = sizeof(ltt);
540 code = RegQueryValueEx(parmKey, "LogoffTokenTransfer", NULL, NULL,
541 (BYTE *) <t, &dummyLen);
542 if (code == ERROR_SUCCESS)
543 afsi_log("Logoff token transfer %s", (ltt ? "on" : "off"));
546 afsi_log("Logoff token transfer on by default");
548 smb_LogoffTokenTransfer = ltt;
549 afsi_log("Logoff token transfer is currently ignored");
552 dummyLen = sizeof(ltto);
553 code = RegQueryValueEx(parmKey, "LogoffTokenTransferTimeout",
554 NULL, NULL, (BYTE *) <to, &dummyLen);
555 if (code == ERROR_SUCCESS)
556 afsi_log("Logoff token tranfer timeout %d seconds", ltto);
559 afsi_log("Default logoff token transfer timeout 10 seconds");
564 smb_LogoffTransferTimeout = ltto;
565 afsi_log("Default logoff token is currently ignored");
567 dummyLen = sizeof(cm_rootVolumeName);
568 code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
569 cm_rootVolumeName, &dummyLen);
570 if (code == ERROR_SUCCESS)
571 afsi_log("Root volume %s", cm_rootVolumeName);
573 StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
574 afsi_log("Default root volume name root.afs");
577 cm_mountRootLen = sizeof(cm_mountRoot);
578 code = RegQueryValueEx(parmKey, "MountRoot", NULL, NULL,
579 cm_mountRoot, &cm_mountRootLen);
580 if (code == ERROR_SUCCESS) {
581 afsi_log("Mount root %s", cm_mountRoot);
582 cm_mountRootLen = strlen(cm_mountRoot);
584 StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
589 dummyLen = sizeof(buf);
590 code = RegQueryValueEx(parmKey, "CachePath", NULL, ®Type,
592 if (code == ERROR_SUCCESS && buf[0]) {
593 if (regType == REG_EXPAND_SZ) {
594 dummyLen = ExpandEnvironmentStrings(buf, cm_CachePath, sizeof(cm_CachePath));
595 if(dummyLen > sizeof(cm_CachePath)) {
596 afsi_log("Cache path [%s] longer than %d after expanding env strings", buf, sizeof(cm_CachePath));
597 osi_panic("CachePath too long", __FILE__, __LINE__);
600 StringCbCopyA(cm_CachePath, sizeof(cm_CachePath), buf);
602 afsi_log("Cache path %s", cm_CachePath);
604 GetWindowsDirectory(cm_CachePath, sizeof(cm_CachePath));
605 cm_CachePath[2] = 0; /* get drive letter only */
606 StringCbCatA(cm_CachePath, sizeof(cm_CachePath), "\\AFSCache");
607 afsi_log("Default cache path %s", cm_CachePath);
610 dummyLen = sizeof(virtualCache);
611 code = RegQueryValueEx(parmKey, "NonPersistentCaching", NULL, NULL,
612 &virtualCache, &dummyLen);
613 if (code == ERROR_SUCCESS && virtualCache) {
614 buf_cacheType = CM_BUF_CACHETYPE_VIRTUAL;
616 buf_cacheType = CM_BUF_CACHETYPE_FILE;
618 afsi_log("Cache type is %s", ((buf_cacheType == CM_BUF_CACHETYPE_FILE)?"FILE":"VIRTUAL"));
620 dummyLen = sizeof(traceOnPanic);
621 code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
622 (BYTE *) &traceOnPanic, &dummyLen);
623 if (code == ERROR_SUCCESS)
624 afsi_log("Set to %s on panic",
625 traceOnPanic ? "trap" : "not trap");
631 dummyLen = sizeof(reportSessionStartups);
632 code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
633 (BYTE *) &reportSessionStartups, &dummyLen);
634 if (code == ERROR_SUCCESS)
635 afsi_log("Session startups %s be recorded in the Event Log",
636 reportSessionStartups ? "will" : "will not");
638 reportSessionStartups = 0;
642 for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
643 cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
644 cm_sysNameList[i][0] = '\0';
646 cm_sysName = cm_sysNameList[0];
648 dummyLen = sizeof(buf);
649 code = RegQueryValueEx(parmKey, "SysName", NULL, NULL, buf, &dummyLen);
650 if (code == ERROR_SUCCESS && buf[0]) {
652 afsi_log("Sys name %s", buf);
654 for (p = q = buf; p < cm_sysName + dummyLen; p++)
656 if (*p == '\0' || isspace(*p)) {
657 memcpy(cm_sysNameList[cm_sysNameCount],q,p-q);
658 cm_sysNameList[cm_sysNameCount][p-q] = '\0';
666 } while (*p == '\0' || isspace(*p));
672 StringCbCopyA(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
675 StringCbCopyA(cm_sysName, MAXSYSNAME, "i386_nt40");
676 StringCbCopyA(cm_sysNameList[0], MAXSYSNAME, "i386_nt40");
677 afsi_log("Default sys name %s", cm_sysName);
680 dummyLen = sizeof(cryptall);
681 code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
682 (BYTE *) &cryptall, &dummyLen);
683 if (code == ERROR_SUCCESS) {
684 afsi_log("SecurityLevel is %s", cryptall?"crypt":"clear");
687 afsi_log("Default SecurityLevel is clear");
691 dummyLen = sizeof(cm_dnsEnabled);
692 code = RegQueryValueEx(parmKey, "UseDNS", NULL, NULL,
693 (BYTE *) &cm_dnsEnabled, &dummyLen);
694 if (code == ERROR_SUCCESS) {
695 afsi_log("DNS %s be used to find AFS cell servers",
696 cm_dnsEnabled ? "will" : "will not");
699 cm_dnsEnabled = 1; /* default on */
700 afsi_log("Default to use DNS to find AFS cell servers");
702 #else /* AFS_AFSDB_ENV */
703 afsi_log("AFS not built with DNS support to find AFS cell servers");
704 #endif /* AFS_AFSDB_ENV */
706 #ifdef AFS_FREELANCE_CLIENT
707 dummyLen = sizeof(cm_freelanceEnabled);
708 code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
709 (BYTE *) &cm_freelanceEnabled, &dummyLen);
710 if (code == ERROR_SUCCESS) {
711 afsi_log("Freelance client feature %s activated",
712 cm_freelanceEnabled ? "is" : "is not");
715 cm_freelanceEnabled = 0; /* default off */
717 #endif /* AFS_FREELANCE_CLIENT */
720 /* The netbios name is looked up in lana_GetUNCServerNameEx */
721 dummyLen = sizeof(buf);
722 code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
723 (BYTE *) &buf, &dummyLen);
724 if (code == ERROR_SUCCESS) {
725 DWORD len = ExpandEnvironmentStrings(buf, cm_NetbiosName, MAX_NB_NAME_LENGTH);
726 if ( len > 0 && len <= MAX_NB_NAME_LENGTH ) {
727 afsi_log("Explicit NetBios name is used %s", cm_NetbiosName);
729 afsi_log("Unable to Expand Explicit NetBios name: %s", buf);
730 cm_NetbiosName[0] = 0; /* turn it off */
734 cm_NetbiosName[0] = 0; /* default off */
738 dummyLen = sizeof(smb_hideDotFiles);
739 code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
740 (BYTE *) &smb_hideDotFiles, &dummyLen);
741 if (code != ERROR_SUCCESS) {
742 smb_hideDotFiles = 1; /* default on */
744 afsi_log("Dot files/dirs will %sbe marked hidden",
745 smb_hideDotFiles ? "" : "not ");
747 dummyLen = sizeof(smb_maxMpxRequests);
748 code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
749 (BYTE *) &smb_maxMpxRequests, &dummyLen);
750 if (code != ERROR_SUCCESS) {
751 smb_maxMpxRequests = 50;
753 afsi_log("Maximum number of multiplexed sessions is %d", smb_maxMpxRequests);
755 dummyLen = sizeof(smb_maxVCPerServer);
756 code = RegQueryValueEx(parmKey, "MaxVCPerServer", NULL, NULL,
757 (BYTE *) &smb_maxVCPerServer, &dummyLen);
758 if (code != ERROR_SUCCESS) {
759 smb_maxVCPerServer = 100;
761 afsi_log("Maximum number of VCs per server is %d", smb_maxVCPerServer);
763 dummyLen = sizeof(smb_authType);
764 code = RegQueryValueEx(parmKey, "SMBAuthType", NULL, NULL,
765 (BYTE *) &smb_authType, &dummyLen);
767 if (code != ERROR_SUCCESS ||
768 (smb_authType != SMB_AUTH_EXTENDED && smb_authType != SMB_AUTH_NTLM && smb_authType != SMB_AUTH_NONE)) {
769 smb_authType = SMB_AUTH_EXTENDED; /* default is to use extended authentication */
771 afsi_log("SMB authentication type is %s", ((smb_authType == SMB_AUTH_NONE)?"NONE":((smb_authType == SMB_AUTH_EXTENDED)?"EXTENDED":"NTLM")));
773 dummyLen = sizeof(rx_nojumbo);
774 code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
775 (BYTE *) &rx_nojumbo, &dummyLen);
776 if (code != ERROR_SUCCESS) {
780 afsi_log("RX Jumbograms are disabled");
782 dummyLen = sizeof(rx_mtu);
783 code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
784 (BYTE *) &rx_mtu, &dummyLen);
785 if (code != ERROR_SUCCESS || !rx_mtu) {
789 afsi_log("RX maximum MTU is %d", rx_mtu);
791 dummyLen = sizeof(ConnDeadtimeout);
792 code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
793 (BYTE *) &ConnDeadtimeout, &dummyLen);
794 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
796 dummyLen = sizeof(HardDeadtimeout);
797 code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
798 (BYTE *) &HardDeadtimeout, &dummyLen);
799 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
801 RegCloseKey (parmKey);
803 /* Call lanahelper to get Netbios name, lan adapter number and gateway flag */
804 if(SUCCEEDED(code = lana_GetUncServerNameEx(cm_NetbiosName, &lanaNum, &isGateway, LANA_NETBIOS_NAME_FULL))) {
805 LANadapter = (lanaNum == LANA_INVALID)? -1: lanaNum;
807 if (LANadapter != -1)
808 afsi_log("LAN adapter number %d", LANadapter);
810 afsi_log("LAN adapter number not determined");
813 afsi_log("Set for gateway service");
815 afsi_log("Using >%s< as SMB server name", cm_NetbiosName);
817 /* something went horribly wrong. We can't proceed without a netbios name */
818 StringCbPrintfA(buf,sizeof(buf),"Netbios name could not be determined: %li", code);
819 osi_panic(buf, __FILE__, __LINE__);
822 /* setup early variables */
823 /* These both used to be configurable. */
825 buf_bufferSize = CM_CONFIGDEFAULT_BLOCKSIZE;
827 /* turn from 1024 byte units into memory blocks */
828 cacheBlocks = (cacheSize * 1024) / buf_bufferSize;
830 /* get network related info */
831 cm_noIPAddr = CM_MAXINTERFACE_ADDR;
832 code = syscfg_GetIFInfo(&cm_noIPAddr,
833 cm_IPAddr, cm_SubnetMask,
834 cm_NetMtu, cm_NetFlags);
836 if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
837 afsi_log("syscfg_GetIFInfo error code %d", code);
839 afsi_log("First Network address %x SubnetMask %x",
840 cm_IPAddr[0], cm_SubnetMask[0]);
843 * Save client configuration for GetCacheConfig requests
845 cm_initParams.nChunkFiles = 0;
846 cm_initParams.nStatCaches = stats;
847 cm_initParams.nDataCaches = 0;
848 cm_initParams.nVolumeCaches = 0;
849 cm_initParams.firstChunkSize = cm_chunkSize;
850 cm_initParams.otherChunkSize = cm_chunkSize;
851 cm_initParams.cacheSize = cacheSize;
852 cm_initParams.setTime = 0;
853 cm_initParams.memCache = 0;
855 /* Set RX parameters before initializing RX */
858 afsi_log("rx_SetNoJumbo successful");
861 if ( rx_mtu != -1 ) {
862 rx_SetMaxMTU(rx_mtu);
863 afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
866 /* Open Microsoft Firewall to allow in port 7001 */
867 icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT);
869 /* Ensure the AFS Netbios Name is registered to allow loopback access */
870 configureBackConnectionHostNames();
872 /* initialize RX, and tell it to listen to port 7001, which is used for
873 * callback RPC messages.
875 code = rx_Init(htons(7001));
876 afsi_log("rx_Init code %x", code);
878 *reasonP = "afsd: failed to init rx client on port 7001";
882 /* Initialize the RPC server for session keys */
885 /* create an unauthenticated service #1 for callbacks */
886 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
887 serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
888 RXAFSCB_ExecuteRequest);
889 afsi_log("rx_NewService addr %x", (int)serverp);
890 if (serverp == NULL) {
891 *reasonP = "unknown error";
895 nullServerSecurityClassp = rxnull_NewServerSecurityObject();
896 serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
897 &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
898 afsi_log("rx_NewService addr %x", (int)serverp);
899 if (serverp == NULL) {
900 *reasonP = "unknown error";
904 /* start server threads, *not* donating this one to the pool */
906 afsi_log("rx_StartServer");
908 /* init user daemon, and other packages */
911 cm_InitACLCache(2*stats);
927 cm_InitSCache(stats);
929 code = cm_InitDCache(0, cacheBlocks);
930 afsi_log("cm_InitDCache code %x", code);
932 *reasonP = "error initializing cache";
937 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
938 if (cm_InitDNS(cm_dnsEnabled) == -1)
939 cm_dnsEnabled = 0; /* init failed, so deactivate */
940 afsi_log("cm_InitDNS %d", cm_dnsEnabled);
944 code = cm_GetRootCellName(rootCellName);
945 afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s",
946 code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
947 if (code != 0 && !cm_freelanceEnabled)
949 *reasonP = "can't find root cell name in afsd.ini";
952 else if (cm_freelanceEnabled)
955 if (code == 0 && !cm_freelanceEnabled)
957 cm_rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
958 afsi_log("cm_GetCell addr %x", (int)cm_rootCellp);
959 if (cm_rootCellp == NULL)
961 *reasonP = "can't find root cell in afsdcell.ini";
966 #ifdef AFS_FREELANCE_CLIENT
967 if (cm_freelanceEnabled)
973 int afsd_InitDaemons(char **reasonP)
980 /* this should really be in an init daemon from here on down */
982 if (!cm_freelanceEnabled) {
983 osi_Log0(afsd_logp, "Loading Root Volume from cell");
984 code = cm_GetVolumeByName(cm_rootCellp, cm_rootVolumeName, cm_rootUserp,
985 &req, CM_FLAG_CREATE, &cm_rootVolumep);
986 afsi_log("cm_GetVolumeByName code %x root vol %x", code,
987 (code ? (cm_volume_t *)-1 : cm_rootVolumep));
989 *reasonP = "can't find root volume in root cell";
994 /* compute the root fid */
995 if (!cm_freelanceEnabled) {
996 cm_rootFid.cell = cm_rootCellp->cellID;
997 cm_rootFid.volume = cm_GetROVolumeID(cm_rootVolumep);
998 cm_rootFid.vnode = 1;
999 cm_rootFid.unique = 1;
1002 cm_FakeRootFid(&cm_rootFid);
1004 code = cm_GetSCache(&cm_rootFid, &cm_rootSCachep, cm_rootUserp, &req);
1005 afsi_log("cm_GetSCache code %x scache %x", code,
1006 (code ? (cm_scache_t *)-1 : cm_rootSCachep));
1008 *reasonP = "unknown error";
1012 cm_InitDaemon(numBkgD);
1013 afsi_log("cm_InitDaemon");
1018 int afsd_InitSMB(char **reasonP, void *aMBfunc)
1020 /* Do this last so that we don't handle requests before init is done.
1021 * Here we initialize the SMB listener.
1023 smb_Init(afsd_logp, cm_NetbiosName, smb_UseV3, LANadapter, numSvThreads, aMBfunc);
1024 afsi_log("smb_Init");
1037 #pragma pack( push, before_imagehlp, 8 )
1038 #include <imagehlp.h>
1039 #pragma pack( pop, before_imagehlp )
1041 #define MAXNAMELEN 1024
1043 void afsd_printStack(HANDLE hThread, CONTEXT *c)
1045 HANDLE hProcess = GetCurrentProcess();
1049 char functionName[MAXNAMELEN];
1051 IMAGEHLP_MODULE Module;
1055 IMAGEHLP_SYMBOL *pSym;
1057 afsi_log_useTimestamp = 0;
1059 pSym = (IMAGEHLP_SYMBOL *) GlobalAlloc(0, sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN);
1061 memset( &s, '\0', sizeof s );
1062 if (!SymInitialize(hProcess, NULL, 1) )
1064 afsi_log("SymInitialize(): GetLastError() = %lu\n", GetLastError() );
1066 SymCleanup( hProcess );
1072 symOptions = SymGetOptions();
1073 symOptions |= SYMOPT_LOAD_LINES;
1074 symOptions &= ~SYMOPT_UNDNAME;
1075 SymSetOptions( symOptions );
1078 * init STACKFRAME for first call
1079 * Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
1080 * Notes: will have to be #ifdef-ed for Alphas; MIPSes are dead anyway,
1081 * and good riddance.
1083 #if defined (_ALPHA_) || defined (_MIPS_) || defined (_PPC_)
1084 #error The STACKFRAME initialization in afsd_printStack() for this platform
1085 #error must be properly configured
1087 s.AddrPC.Offset = c->Eip;
1088 s.AddrPC.Mode = AddrModeFlat;
1089 s.AddrFrame.Offset = c->Ebp;
1090 s.AddrFrame.Mode = AddrModeFlat;
1093 memset( pSym, '\0', sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN );
1094 pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL);
1095 pSym->MaxNameLength = MAXNAMELEN;
1097 memset( &Line, '\0', sizeof Line );
1098 Line.SizeOfStruct = sizeof Line;
1100 memset( &Module, '\0', sizeof Module );
1101 Module.SizeOfStruct = sizeof Module;
1105 afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
1107 for ( frameNum = 0; ; ++ frameNum )
1110 * get next stack frame (StackWalk(), SymFunctionTableAccess(),
1111 * SymGetModuleBase()). if this returns ERROR_INVALID_ADDRESS (487) or
1112 * ERROR_NOACCESS (998), you can assume that either you are done, or
1113 * that the stack is so hosed that the next deeper frame could not be
1116 if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c,
1117 NULL, SymFunctionTableAccess, SymGetModuleBase,
1121 /* display its contents */
1122 afsi_log("\n%3d %c%c %08lx %08lx %08lx %08lx ",
1123 frameNum, s.Far? 'F': '.', s.Virtual? 'V': '.',
1124 s.AddrPC.Offset, s.AddrReturn.Offset,
1125 s.AddrFrame.Offset, s.AddrStack.Offset );
1127 if ( s.AddrPC.Offset == 0 )
1129 afsi_log("(-nosymbols- PC == 0)" );
1133 /* show procedure info from a valid PC */
1134 if (!SymGetSymFromAddr(hProcess, s.AddrPC.Offset, &offset, pSym))
1136 if ( GetLastError() != ERROR_INVALID_ADDRESS )
1138 afsi_log("SymGetSymFromAddr(): errno = %lu",
1144 UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN,
1146 afsi_log("%s", functionName );
1150 afsi_log(" %+ld bytes", (long) offset);
1154 if (!SymGetLineFromAddr(hProcess, s.AddrPC.Offset, &offset, &Line))
1156 if (GetLastError() != ERROR_INVALID_ADDRESS)
1158 afsi_log("Error: SymGetLineFromAddr(): errno = %lu",
1164 afsi_log(" Line: %s(%lu) %+ld bytes", Line.FileName,
1165 Line.LineNumber, offset);
1169 /* no return address means no deeper stackframe */
1170 if (s.AddrReturn.Offset == 0)
1177 if (GetLastError() != 0)
1179 afsi_log("\nStackWalk(): errno = %lu\n", GetLastError());
1182 SymCleanup(hProcess);
1187 static DWORD *afsd_crtDbgBreakCurrent = NULL;
1188 static DWORD afsd_crtDbgBreaks[256];
1191 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
1195 BOOL allocRequestBrk = FALSE;
1198 afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n",
1199 ep->ExceptionRecord->ExceptionCode,
1200 ep->ExceptionRecord->ExceptionAddress);
1203 if (afsd_crtDbgBreakCurrent &&
1204 *afsd_crtDbgBreakCurrent == _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent))
1206 allocRequestBrk = TRUE;
1207 afsi_log("Breaking on alloc request # %d\n", *afsd_crtDbgBreakCurrent);
1211 /* save context if we want to print the stack information */
1212 context = *ep->ContextRecord;
1214 afsd_printStack(GetCurrentThread(), &context);
1216 if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
1218 afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
1221 if (allocRequestBrk)
1223 afsd_crtDbgBreakCurrent++;
1224 _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
1228 ep->ContextRecord->Eip++;
1229 return EXCEPTION_CONTINUE_EXECUTION;
1233 return EXCEPTION_CONTINUE_SEARCH;
1237 void afsd_SetUnhandledExceptionFilter()
1239 SetUnhandledExceptionFilter(afsd_ExceptionFilter);
1243 void afsd_DbgBreakAllocInit()
1245 memset(afsd_crtDbgBreaks, -1, sizeof(afsd_crtDbgBreaks));
1246 afsd_crtDbgBreakCurrent = afsd_crtDbgBreaks;
1249 void afsd_DbgBreakAdd(DWORD requestNumber)
1252 for (i = 0; i < sizeof(afsd_crtDbgBreaks) - 1; i++)
1254 if (afsd_crtDbgBreaks[i] == -1)
1259 afsd_crtDbgBreaks[i] = requestNumber;
1261 _CrtSetBreakAlloc(afsd_crtDbgBreaks[0]);