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
12 #include <afs/param.h>
18 #include <sys/timeb.h>
34 #include "lanahelper.h"
36 /* These characters are illegal in Windows filenames */
37 static char *illegalChars = "\\/:*?\"<>|";
38 BOOL isWindows2000 = FALSE;
40 smb_vc_t *dead_vcp = NULL;
41 smb_vc_t *active_vcp = NULL;
43 /* TODO; logout mechanism needs to be thread-safe */
44 char *loggedOutName = NULL;
45 smb_user_t *loggedOutUserp = NULL;
46 unsigned long loggedOutTime;
48 int smbShutdownFlag = 0;
50 int smb_LogoffTokenTransfer;
51 unsigned long smb_LogoffTransferTimeout;
53 DWORD last_msg_time = 0;
57 unsigned int sessionGen = 0;
59 extern void afsi_log(char *pattern, ...);
60 extern HANDLE afsi_file;
62 osi_hyper_t hzero = {0, 0};
63 osi_hyper_t hones = {0xFFFFFFFF, -1};
66 osi_rwlock_t smb_globalLock;
67 osi_rwlock_t smb_rctLock;
68 osi_mutex_t smb_ListenerLock;
71 unsigned char smb_sharename[NCBNAMSZ+1] = {0};
74 long smb_maxObsConcurrentCalls=0;
75 long smb_concurrentCalls=0;
77 smb_dispatch_t smb_dispatchTable[SMB_NOPCODES];
79 smb_packet_t *smb_packetFreeListp;
80 smb_ncb_t *smb_ncbFreeListp;
82 int smb_NumServerThreads;
84 int numNCBs, numSessions, numVCs;
86 int smb_maxVCPerServer;
87 int smb_maxMpxRequests;
89 int smb_authType = SMB_AUTH_EXTENDED; /* type of SMB auth to use. One of SMB_AUTH_* */
91 ULONG smb_lsaSecPackage;
92 LSA_STRING smb_lsaLogonOrigin;
94 #define NCBmax MAXIMUM_WAIT_OBJECTS
95 EVENT_HANDLE NCBavails[NCBmax], NCBevents[NCBmax];
96 EVENT_HANDLE **NCBreturns;
97 DWORD NCBsessions[NCBmax];
99 struct smb_packet *bufs[NCBmax];
101 #define Sessionmax MAXIMUM_WAIT_OBJECTS
102 EVENT_HANDLE SessionEvents[Sessionmax];
103 unsigned short LSNs[Sessionmax];
104 int lanas[Sessionmax];
105 BOOL dead_sessions[Sessionmax];
109 osi_mutex_t smb_RawBufLock;
111 #define SMB_RAW_BUFS 4
113 int smb_RawBufSel[SMB_RAW_BUFS];
118 #define SMB_MASKFLAG_TILDE 1
119 #define SMB_MASKFLAG_CASEFOLD 2
121 #define RAWTIMEOUT INFINITE
124 typedef struct raw_write_cont {
137 /* dir search stuff */
138 long smb_dirSearchCounter = 1;
139 smb_dirSearch_t *smb_firstDirSearchp;
140 smb_dirSearch_t *smb_lastDirSearchp;
142 /* hide dot files? */
143 int smb_hideDotFiles;
145 /* global state about V3 protocols */
146 int smb_useV3; /* try to negotiate V3 */
149 static showErrors = 1;
150 /* MessageBox or something like it */
151 int (_stdcall *smb_MBfunc)(HWND, LPCTSTR, LPCTSTR, UINT) = NULL;
152 extern HANDLE WaitToTerminate;
156 * Time in Unix format of midnight, 1/1/1970 local time.
157 * When added to dosUTime, gives Unix (AFS) time.
161 /* Time difference for converting to kludge-GMT */
164 char *smb_localNamep = NULL;
166 smb_vc_t *smb_allVCsp;
168 smb_username_t *usernamesp = NULL;
170 smb_waitingLock_t *smb_allWaitingLocks;
173 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
174 NCB *ncbp, raw_write_cont_t *rwcp);
175 void smb_NetbiosInit();
177 #ifndef AFS_WIN95_ENV
178 DWORD smb_ServerExceptionFilter(void);
181 extern char cm_HostName[];
182 extern char cm_confDir[];
186 #define LPTSTR char *
187 #define GetComputerName(str, sizep) \
188 strcpy((str), cm_HostName); \
189 *(sizep) = strlen(cm_HostName)
192 extern char AFSConfigKeyName[];
194 char smb_ServerDomainName[MAX_COMPUTERNAME_LENGTH + 1] = ""; /* domain name */
195 int smb_ServerDomainNameLength = 0;
196 char smb_ServerOS[] = "Windows 5.0"; /* Faux OS String */
197 int smb_ServerOSLength = sizeof(smb_ServerOS);
198 char smb_ServerLanManager[] = "Windows 2000 LAN Manager"; /* Faux LAN Manager string */
199 int smb_ServerLanManagerLength = sizeof(smb_ServerLanManager);
201 /* Faux server GUID. This is never checked. */
202 GUID smb_ServerGUID = { 0x40015cb8, 0x058a, 0x44fc, { 0xae, 0x7e, 0xbb, 0x29, 0x52, 0xee, 0x7e, 0xff }};
207 * To build an expiring version, comment out the definition of NOEXPIRE,
208 * and set the definition of EXPIREDATE to the desired value.
211 #define EXPIREDATE 834000000 /* Wed Jun 5 1996 */
214 char * myCrt_Dispatch(int i)
219 return "unknown SMB op";
221 return "(00)ReceiveCoreMakeDir";
223 return "(01)ReceiveCoreRemoveDir";
225 return "(02)ReceiveCoreOpen";
227 return "(03)ReceiveCoreCreate";
229 return "(04)ReceiveCoreClose";
231 return "(05)ReceiveCoreFlush";
233 return "(06)ReceiveCoreUnlink";
235 return "(07)ReceiveCoreRename";
237 return "(08)ReceiveCoreGetFileAttributes";
239 return "(09)ReceiveCoreSetFileAttributes";
241 return "(0a)ReceiveCoreRead";
243 return "(0b)ReceiveCoreWrite";
245 return "(0c)ReceiveCoreLockRecord";
247 return "(0d)ReceiveCoreUnlockRecord";
249 return "(0e)SendCoreBadOp";
251 return "(0f)ReceiveCoreCreate";
253 return "(10)ReceiveCoreCheckPath";
255 return "(11)SendCoreBadOp";
257 return "(12)ReceiveCoreSeek";
259 return "(1a)ReceiveCoreReadRaw";
261 return "(1d)ReceiveCoreWriteRawDummy";
263 return "(22)ReceiveV3SetAttributes";
265 return "(23)ReceiveV3GetAttributes";
267 return "(24)ReceiveV3LockingX";
269 return "(25)ReceiveV3Trans";
271 return "(26)ReceiveV3Trans[aux]";
273 return "(29)SendCoreBadOp";
275 return "(2b)ReceiveCoreEcho";
277 return "(2d)ReceiveV3OpenX";
279 return "(2e)ReceiveV3ReadX";
281 return "(32)ReceiveV3Tran2A";
283 return "(33)ReceiveV3Tran2A[aux]";
285 return "(34)ReceiveV3FindClose";
287 return "(35)ReceiveV3FindNotifyClose";
289 return "(70)ReceiveCoreTreeConnect";
291 return "(71)ReceiveCoreTreeDisconnect";
293 return "(72)ReceiveNegotiate";
295 return "(73)ReceiveV3SessionSetupX";
297 return "(74)ReceiveV3UserLogoffX";
299 return "(75)ReceiveV3TreeConnectX";
301 return "(80)ReceiveCoreGetDiskAttributes";
303 return "(81)ReceiveCoreSearchDir";
305 return "(A0)ReceiveNTTransact";
307 return "(A2)ReceiveNTCreateX";
309 return "(A4)ReceiveNTCancel";
311 return "(c0)SendCoreBadOp";
313 return "(c1)SendCoreBadOp";
315 return "(c2)SendCoreBadOp";
317 return "(c3)SendCoreBadOp";
321 char * myCrt_2Dispatch(int i)
326 return "unknown SMB op-2";
328 return "S(00)CreateFile";
330 return "S(01)FindFirst";
332 return "S(02)FindNext"; /* FindNext */
334 return "S(03)QueryFileSystem_ReceiveTran2QFSInfo";
338 return "S(05)QueryFileInfo_ReceiveTran2QPathInfo";
340 return "S(06)SetFileInfo_ReceiveTran2SetPathInfo";
342 return "S(07)SetInfoHandle_ReceiveTran2QFileInfo";
344 return "S(08)??_ReceiveTran2SetFileInfo";
346 return "S(09)??_ReceiveTran2FSCTL";
348 return "S(0a)_ReceiveTran2IOCTL";
350 return "S(0b)_ReceiveTran2FindNotifyFirst";
352 return "S(0c)_ReceiveTran2FindNotifyNext";
354 return "S(0d)CreateDirectory_ReceiveTran2MKDir";
358 char * myCrt_RapDispatch(int i)
363 return "unknown RAP OP";
365 return "RAP(0)NetShareEnum";
367 return "RAP(1)NetShareGetInfo";
369 return "RAP(13)NetServerGetInfo";
371 return "RAP(63)NetWkStaGetInfo";
375 /* scache must be locked */
376 unsigned int smb_Attributes(cm_scache_t *scp)
380 if (scp->fileType == CM_SCACHETYPE_DIRECTORY
381 || scp->fileType == CM_SCACHETYPE_MOUNTPOINT)
382 attrs = SMB_ATTR_DIRECTORY;
387 * We used to mark a file RO if it was in an RO volume, but that
388 * turns out to be impolitic in NT. See defect 10007.
391 if ((scp->unixModeBits & 0222) == 0 || (scp->flags & CM_SCACHEFLAG_RO))
393 if ((scp->unixModeBits & 0222) == 0)
394 attrs |= SMB_ATTR_READONLY; /* turn on read-only flag */
399 /* Check if the named file/dir is a dotfile/dotdir */
400 /* String pointed to by lastComp can have leading slashes, but otherwise should have
401 no other patch components */
402 unsigned int smb_IsDotFile(char *lastComp) {
405 /* skip over slashes */
406 for(s=lastComp;*s && (*s == '\\' || *s == '/'); s++);
411 /* nulls, curdir and parent dir doesn't count */
414 if(!*(s + 1)) return 0;
415 if(*(s+1) == '.' && !*(s + 2)) return 0;
421 static int ExtractBits(WORD bits, short start, short len)
428 num = bits << (16 - end);
429 num = num >> ((16 - end) + start);
435 void ShowUnixTime(char *FuncName, long unixTime)
440 smb_LargeSearchTimeFromUnixTime(&ft, unixTime);
442 if (!FileTimeToDosDateTime(&ft, &wDate, &wTime))
443 osi_Log1(smb_logp, "Failed to convert filetime to dos datetime: %d", GetLastError());
445 int day, month, year, sec, min, hour;
448 day = ExtractBits(wDate, 0, 5);
449 month = ExtractBits(wDate, 5, 4);
450 year = ExtractBits(wDate, 9, 7) + 1980;
452 sec = ExtractBits(wTime, 0, 5);
453 min = ExtractBits(wTime, 5, 6);
454 hour = ExtractBits(wTime, 11, 5);
456 sprintf(msg, "%s = %02d-%02d-%04d %02d:%02d:%02d", FuncName, month, day, year, hour, min, sec);
457 osi_Log1(smb_logp, "%s", osi_LogSaveString(smb_logp, msg));
463 /* Determine if we are observing daylight savings time */
464 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
466 TIME_ZONE_INFORMATION timeZoneInformation;
467 SYSTEMTIME utc, local, localDST;
469 /* Get the time zone info. NT uses this to calc if we are in DST. */
470 GetTimeZoneInformation(&timeZoneInformation);
472 /* Return the daylight bias */
473 *pDstBias = timeZoneInformation.DaylightBias;
475 /* Return the bias */
476 *pBias = timeZoneInformation.Bias;
478 /* Now determine if DST is being observed */
480 /* Get the UTC (GMT) time */
483 /* Convert UTC time to local time using the time zone info. If we are
484 observing DST, the calculated local time will include this.
486 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &localDST);
488 /* Set the daylight bias to 0. The daylight bias is the amount of change
489 in time that we use for daylight savings time. By setting this to 0
490 we cause there to be no change in time during daylight savings time.
492 timeZoneInformation.DaylightBias = 0;
494 /* Convert the utc time to local time again, but this time without any
495 adjustment for daylight savings time.
497 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &local);
499 /* If the two times are different, then it means that the localDST that
500 we calculated includes the daylight bias, and therefore we are
501 observing daylight savings time.
503 *pDST = localDST.wHour != local.wHour;
506 /* Determine if we are observing daylight savings time */
507 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
513 *pDstBias = -60; /* where can this be different? */
519 void CompensateForSmbClientLastWriteTimeBugs(long *pLastWriteTime)
521 BOOL dst; /* Will be TRUE if observing DST */
522 LONG dstBias; /* Offset from local time if observing DST */
523 LONG bias; /* Offset from GMT for local time */
526 * This function will adjust the last write time to compensate
527 * for two bugs in the smb client:
529 * 1) During Daylight Savings Time, the LastWriteTime is ahead
530 * in time by the DaylightBias (ignoring the sign - the
531 * DaylightBias is always stored as a negative number). If
532 * the DaylightBias is -60, then the LastWriteTime will be
533 * ahead by 60 minutes.
535 * 2) If the local time zone is a positive offset from GMT, then
536 * the LastWriteTime will be the correct local time plus the
537 * Bias (ignoring the sign - a positive offset from GMT is
538 * always stored as a negative Bias). If the Bias is -120,
539 * then the LastWriteTime will be ahead by 120 minutes.
541 * These bugs can occur at the same time.
544 GetTimeZoneInfo(&dst, &dstBias, &bias);
546 /* First adjust for DST */
548 *pLastWriteTime -= (-dstBias * 60); /* Convert dstBias to seconds */
550 /* Now adjust for a positive offset from GMT (a negative bias). */
552 *pLastWriteTime -= (-bias * 60); /* Convert bias to seconds */
556 * Calculate the difference (in seconds) between local time and GMT.
557 * This enables us to convert file times to kludge-GMT.
563 struct tm gmt_tm, local_tm;
564 int days, hours, minutes, seconds;
567 gmt_tm = *(gmtime(&t));
568 local_tm = *(localtime(&t));
570 days = local_tm.tm_yday - gmt_tm.tm_yday;
571 hours = 24 * days + local_tm.tm_hour - gmt_tm.tm_hour
573 /* There is a problem with DST immediately after the time change
574 * which may continue to exist until the machine is rebooted
576 - (local_tm.tm_isdst ? 1 : 0)
579 minutes = 60 * hours + local_tm.tm_min - gmt_tm.tm_min;
580 seconds = 60 * minutes + local_tm.tm_sec - gmt_tm.tm_sec;
586 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
591 long ersatz_unixTime;
594 * Must use kludge-GMT instead of real GMT.
595 * kludge-GMT is computed by adding time zone difference to localtime.
598 * ltp = gmtime(&unixTime);
600 ersatz_unixTime = unixTime - smb_NowTZ;
601 ltp = localtime(&ersatz_unixTime);
603 /* if we fail, make up something */
606 localJunk.tm_year = 89 - 20;
607 localJunk.tm_mon = 4;
608 localJunk.tm_mday = 12;
609 localJunk.tm_hour = 0;
610 localJunk.tm_min = 0;
611 localJunk.tm_sec = 0;
614 stm.wYear = ltp->tm_year + 1900;
615 stm.wMonth = ltp->tm_mon + 1;
616 stm.wDayOfWeek = ltp->tm_wday;
617 stm.wDay = ltp->tm_mday;
618 stm.wHour = ltp->tm_hour;
619 stm.wMinute = ltp->tm_min;
620 stm.wSecond = ltp->tm_sec;
621 stm.wMilliseconds = 0;
623 SystemTimeToFileTime(&stm, largeTimep);
626 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
628 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
629 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 ??? */
630 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
632 int leap_years = 89; /* leap years betw 1/1/1601 and 1/1/1970 */
634 /* set ft to number of 100ns intervals betw 1/1/1601 and 1/1/1970 GMT */
635 *ft = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years)
637 *ft = LargeIntegerMultiplyByLong(*ft, 60);
638 *ft = LargeIntegerMultiplyByLong(*ft, 10000000);
641 ut = ConvertLongToLargeInteger(unixTime);
642 ut = LargeIntegerMultiplyByLong(ut, 10000000);
643 *ft = LargeIntegerAdd(*ft, ut);
648 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
654 FileTimeToSystemTime(largeTimep, &stm);
656 lt.tm_year = stm.wYear - 1900;
657 lt.tm_mon = stm.wMonth - 1;
658 lt.tm_wday = stm.wDayOfWeek;
659 lt.tm_mday = stm.wDay;
660 lt.tm_hour = stm.wHour;
661 lt.tm_min = stm.wMinute;
662 lt.tm_sec = stm.wSecond;
665 save_timezone = _timezone;
666 _timezone += smb_NowTZ;
667 *unixTimep = mktime(<);
668 _timezone = save_timezone;
671 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
673 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
674 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 GMT? */
675 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
679 /* set to number of 100ns intervals betw 1/1/1601 and 1/1/1970 */
680 a = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years) * 24 * 60);
681 a = LargeIntegerMultiplyByLong(a, 60);
682 a = LargeIntegerMultiplyByLong(a, 10000000);
684 /* subtract it from ft */
685 a = LargeIntegerSubtract(*ft, a);
687 /* divide down to seconds */
688 *unixTimep = LargeIntegerDivideByLong(a, 10000000);
692 void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime)
699 ltp = localtime((time_t*) &unixTime);
701 /* if we fail, make up something */
704 localJunk.tm_year = 89 - 20;
705 localJunk.tm_mon = 4;
706 localJunk.tm_mday = 12;
707 localJunk.tm_hour = 0;
708 localJunk.tm_min = 0;
709 localJunk.tm_sec = 0;
712 dosDate = ((ltp->tm_year-80)<<9) | ((ltp->tm_mon+1) << 5) | (ltp->tm_mday);
713 dosTime = (ltp->tm_hour<<11) | (ltp->tm_min << 5) | (ltp->tm_sec / 2);
714 *dosTimep = (dosDate<<16) | dosTime;
717 void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime)
719 unsigned short dosDate;
720 unsigned short dosTime;
723 dosDate = searchTime & 0xffff;
724 dosTime = (searchTime >> 16) & 0xffff;
726 localTm.tm_year = 80 + ((dosDate>>9) & 0x3f);
727 localTm.tm_mon = ((dosDate >> 5) & 0xf) - 1; /* January is 0 in localTm */
728 localTm.tm_mday = (dosDate) & 0x1f;
729 localTm.tm_hour = (dosTime>>11) & 0x1f;
730 localTm.tm_min = (dosTime >> 5) & 0x3f;
731 localTm.tm_sec = (dosTime & 0x1f) * 2;
732 localTm.tm_isdst = -1; /* compute whether DST in effect */
734 *unixTimep = mktime(&localTm);
737 void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime)
739 *dosUTimep = unixTime - smb_localZero;
742 void smb_UnixTimeFromDosUTime(long *unixTimep, long dosTime)
745 *unixTimep = dosTime + smb_localZero;
747 /* dosTime seems to be already adjusted for GMT */
748 *unixTimep = dosTime;
752 smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana)
756 lock_ObtainWrite(&smb_rctLock);
757 for(vcp = smb_allVCsp; vcp; vcp=vcp->nextp) {
758 if (lsn == vcp->lsn && lana == vcp->lana) {
763 if (!vcp && (flags & SMB_FLAG_CREATE)) {
764 vcp = malloc(sizeof(*vcp));
765 memset(vcp, 0, sizeof(*vcp));
766 vcp->vcID = numVCs++;
770 vcp->uidCounter = 1; /* UID 0 is reserved for blank user */
771 vcp->nextp = smb_allVCsp;
773 lock_InitializeMutex(&vcp->mx, "vc_t mutex");
778 if (smb_authType == SMB_AUTH_NTLM || smb_authType == SMB_AUTH_EXTENDED) {
779 /* We must obtain a challenge for extended auth
780 * in case the client negotiates smb v3
783 MSV1_0_LM20_CHALLENGE_REQUEST lsaReq;
784 PMSV1_0_LM20_CHALLENGE_RESPONSE lsaResp;
787 lsaReq.MessageType = MsV1_0Lm20ChallengeRequest;
789 nts = LsaCallAuthenticationPackage( smb_lsaHandle,
796 osi_assert(nts == STATUS_SUCCESS); /* this had better work! */
798 memcpy(vcp->encKey, lsaResp->ChallengeToClient, MSV1_0_CHALLENGE_LENGTH);
799 LsaFreeReturnBuffer(lsaResp);
802 memset(vcp->encKey, 0, MSV1_0_CHALLENGE_LENGTH);
804 lock_ReleaseWrite(&smb_rctLock);
808 int smb_IsStarMask(char *maskp)
813 for(i=0; i<11; i++) {
815 if (tc == '?' || tc == '*' || tc == '>') return 1;
820 void smb_ReleaseVC(smb_vc_t *vcp)
822 lock_ObtainWrite(&smb_rctLock);
823 osi_assert(vcp->refCount-- > 0);
824 lock_ReleaseWrite(&smb_rctLock);
827 void smb_HoldVC(smb_vc_t *vcp)
829 lock_ObtainWrite(&smb_rctLock);
831 lock_ReleaseWrite(&smb_rctLock);
834 smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags)
838 lock_ObtainWrite(&smb_rctLock);
839 for(tidp = vcp->tidsp; tidp; tidp = tidp->nextp) {
840 if (tid == tidp->tid) {
845 if (!tidp && (flags & SMB_FLAG_CREATE)) {
846 tidp = malloc(sizeof(*tidp));
847 memset(tidp, 0, sizeof(*tidp));
848 tidp->nextp = vcp->tidsp;
853 lock_InitializeMutex(&tidp->mx, "tid_t mutex");
856 lock_ReleaseWrite(&smb_rctLock);
860 void smb_ReleaseTID(smb_tid_t *tidp)
869 lock_ObtainWrite(&smb_rctLock);
870 osi_assert(tidp->refCount-- > 0);
871 if (tidp->refCount == 0 && (tidp->flags & SMB_TIDFLAG_DELETE)) {
872 ltpp = &tidp->vcp->tidsp;
873 for(tp = *ltpp; tp; ltpp = &tp->nextp, tp = *ltpp) {
874 if (tp == tidp) break;
876 osi_assert(tp != NULL);
878 lock_FinalizeMutex(&tidp->mx);
879 userp = tidp->userp; /* remember to drop ref later */
882 lock_ReleaseWrite(&smb_rctLock);
884 cm_ReleaseUser(userp);
891 smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags)
893 smb_user_t *uidp = NULL;
895 lock_ObtainWrite(&smb_rctLock);
896 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
897 if (uid == uidp->userID) {
899 osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL," VCP[%x] found-uid[%d] name[%s]",(int)vcp,uidp->userID,(uidp->unp) ? uidp->unp->name : "");
903 if (!uidp && (flags & SMB_FLAG_CREATE)) {
904 uidp = malloc(sizeof(*uidp));
905 memset(uidp, 0, sizeof(*uidp));
906 uidp->nextp = vcp->usersp;
911 lock_InitializeMutex(&uidp->mx, "user_t mutex");
913 osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL,"VCP[%x] new-uid[%d] name[%s]",(int)vcp,uidp->userID,(uidp->unp ? uidp->unp->name : ""));
915 lock_ReleaseWrite(&smb_rctLock);
919 smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags)
921 smb_username_t *unp= NULL;
923 lock_ObtainWrite(&smb_rctLock);
924 for(unp = usernamesp; unp; unp = unp->nextp) {
925 if (stricmp(unp->name, usern) == 0 &&
926 stricmp(unp->machine, machine) == 0) {
931 if (!unp && (flags & SMB_FLAG_CREATE)) {
932 unp = malloc(sizeof(*unp));
933 memset(unp, 0, sizeof(*unp));
935 unp->nextp = usernamesp;
936 unp->name = strdup(usern);
937 unp->machine = strdup(machine);
939 lock_InitializeMutex(&unp->mx, "username_t mutex");
941 lock_ReleaseWrite(&smb_rctLock);
945 smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern)
947 smb_user_t *uidp= NULL;
949 lock_ObtainWrite(&smb_rctLock);
950 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
953 if (stricmp(uidp->unp->name, usern) == 0) {
955 osi_LogEvent("AFS smb_FindUserByNameThisSession",NULL,"VCP[%x] uid[%d] match-name[%s]",(int)vcp,uidp->userID,usern);
960 lock_ReleaseWrite(&smb_rctLock);
963 void smb_ReleaseUID(smb_user_t *uidp)
972 lock_ObtainWrite(&smb_rctLock);
973 osi_assert(uidp->refCount-- > 0);
974 if (uidp->refCount == 0 && (uidp->flags & SMB_USERFLAG_DELETE)) {
975 lupp = &uidp->vcp->usersp;
976 for(up = *lupp; up; lupp = &up->nextp, up = *lupp) {
977 if (up == uidp) break;
979 osi_assert(up != NULL);
981 lock_FinalizeMutex(&uidp->mx);
983 userp = uidp->unp->userp; /* remember to drop ref later */
984 uidp->unp->userp = NULL;
989 lock_ReleaseWrite(&smb_rctLock);
991 cm_ReleaseUserVCRef(userp);
992 cm_ReleaseUser(userp);
999 /* retrieve a held reference to a user structure corresponding to an incoming
1001 * corresponding release function is cm_ReleaseUser.
1003 cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp)
1009 smbp = (smb_t *) inp;
1010 uidp = smb_FindUID(vcp, smbp->uid, 0);
1011 if ((!uidp) || (!uidp->unp))
1014 lock_ObtainMutex(&uidp->mx);
1015 up = uidp->unp->userp;
1017 lock_ReleaseMutex(&uidp->mx);
1019 smb_ReleaseUID(uidp);
1025 * Return a pointer to a pathname extracted from a TID structure. The
1026 * TID structure is not held; assume it won't go away.
1028 char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid)
1033 tidp = smb_FindTID(vcp, tid, 0);
1036 tpath = tidp->pathname;
1037 smb_ReleaseTID(tidp);
1041 /* check to see if we have a chained fid, that is, a fid that comes from an
1042 * OpenAndX message that ran earlier in this packet. In this case, the fid
1043 * field in a read, for example, request, isn't set, since the value is
1044 * supposed to be inherited from the openAndX call.
1046 int smb_ChainFID(int fid, smb_packet_t *inp)
1048 if (inp->fid == 0 || inp->inCount == 0)
1054 /* are we a priv'd user? What does this mean on NT? */
1055 int smb_SUser(cm_user_t *userp)
1060 /* find a file ID. If we pass in 0 we select an used File ID.
1061 * If the SMB_FLAG_CREATE flag is set, we allocate a new
1062 * smb_fid_t data structure if desired File ID cannot be found.
1064 smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags)
1069 if (fid == 0 && !(flags & SMB_FLAG_CREATE))
1072 lock_ObtainWrite(&smb_rctLock);
1073 /* figure out if we need to allocate a new file ID */
1076 fid = vcp->fidCounter;
1080 for(fidp = vcp->fidsp; fidp; fidp = (smb_fid_t *) osi_QNext(&fidp->q)) {
1081 if (fid == fidp->fid) {
1092 if (!fidp && (flags & SMB_FLAG_CREATE)) {
1093 char eventName[MAX_PATH];
1095 sprintf(eventName,"fid_t event vcp=%d fid=%d", vcp->vcID, fid);
1096 event = thrd_CreateEvent(NULL, FALSE, TRUE, eventName);
1097 if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
1098 afsi_log("Event Object Already Exists: %s", eventName);
1099 thrd_CloseHandle(event);
1106 fidp = malloc(sizeof(*fidp));
1107 memset(fidp, 0, sizeof(*fidp));
1108 osi_QAdd((osi_queue_t **)&vcp->fidsp, &fidp->q);
1112 lock_InitializeMutex(&fidp->mx, "fid_t mutex");
1114 fidp->curr_chunk = fidp->prev_chunk = -2;
1115 fidp->raw_write_event = event;
1117 vcp->fidCounter = fid+1;
1118 if (vcp->fidCounter == 0)
1119 vcp->fidCounter = 1;
1122 lock_ReleaseWrite(&smb_rctLock);
1126 void smb_ReleaseFID(smb_fid_t *fidp)
1129 smb_vc_t *vcp = NULL;
1130 smb_ioctl_t *ioctlp;
1136 lock_ObtainWrite(&smb_rctLock);
1137 osi_assert(fidp->refCount-- > 0);
1138 if (fidp->refCount == 0 && (fidp->flags & SMB_FID_DELETE)) {
1140 if (!(fidp->flags & SMB_FID_IOCTL))
1142 osi_QRemove((osi_queue_t **) &vcp->fidsp, &fidp->q);
1143 thrd_CloseHandle(fidp->raw_write_event);
1145 /* and see if there is ioctl stuff to free */
1146 ioctlp = fidp->ioctlp;
1148 if (ioctlp->prefix) cm_FreeSpace(ioctlp->prefix);
1149 if (ioctlp->inAllocp) free(ioctlp->inAllocp);
1150 if (ioctlp->outAllocp) free(ioctlp->outAllocp);
1156 /* do not call smb_ReleaseVC() because we already have the lock */
1159 lock_ReleaseWrite(&smb_rctLock);
1161 /* now release the scache structure */
1163 cm_ReleaseSCache(scp);
1167 * Case-insensitive search for one string in another;
1168 * used to find variable names in submount pathnames.
1170 static char *smb_stristr(char *str1, char *str2)
1174 for (cursor = str1; *cursor; cursor++)
1175 if (stricmp(cursor, str2) == 0)
1182 * Substitute a variable value for its name in a submount pathname. Variable
1183 * name has been identified by smb_stristr() and is in substr. Variable name
1184 * length (plus one) is in substr_size. Variable value is in newstr.
1186 static void smb_subst(char *str1, char *substr, unsigned int substr_size,
1191 strcpy(temp, substr + substr_size - 1);
1192 strcpy(substr, newstr);
1196 char VNUserName[] = "%USERNAME%";
1197 char VNLCUserName[] = "%LCUSERNAME%";
1198 char VNComputerName[] = "%COMPUTERNAME%";
1199 char VNLCComputerName[] = "%LCCOMPUTERNAME%";
1202 /* List available shares */
1203 int smb_ListShares()
1207 char shareBuf[4096];
1215 /*strcpy(shareNameList[num_shares], "all");
1216 strcpy(pathNameList[num_shares++], "/afs");*/
1217 fprintf(stderr, "The following shares are available:\n");
1218 fprintf(stderr, "Share Name (AFS Path)\n");
1219 fprintf(stderr, "---------------------\n");
1220 fprintf(stderr, "\\\\%s\\%-16s (%s)\n", smb_localNamep, "ALL", cm_mountRoot);
1223 code = GetWindowsDirectory(sbmtpath, sizeof(sbmtpath));
1224 if (code == 0 || code > sizeof(sbmtpath)) return -1;
1226 strcpy(sbmtpath, cm_confDir);
1228 strcat(sbmtpath, "/afsdsbmt.ini");
1229 len = GetPrivateProfileString("AFS Submounts", NULL, NULL,
1230 shareBuf, sizeof(shareBuf),
1236 this_share = shareBuf;
1240 /*strcpy(shareNameList[num_shares], this_share);*/
1241 len = GetPrivateProfileString("AFS Submounts", this_share,
1248 if (strncmp(p, cm_mountRoot, strlen(cm_mountRoot)) != 0)
1251 if (*p == '\\') *p = '/'; /* change to / */
1255 fprintf(stderr, "\\\\%s\\%-16s (%s%s)\n",
1256 smb_localNamep, this_share, (print_afs ? cm_mountRoot : "\0"),
1259 while (*this_share != 0) this_share++; /* find next NUL */
1260 this_share++; /* skip past the NUL */
1261 } while (*this_share != 0); /* stop at final NUL */
1266 /* find a shareName in the table of submounts */
1267 int smb_FindShare(smb_vc_t *vcp, smb_user_t *uidp, char *shareName,
1271 char pathName[1024];
1276 char sbmtpath[MAX_PATH];
1281 DWORD allSubmount = 1;
1283 if (strcmp(shareName, "IPC$") == 0) {
1288 /* if allSubmounts == 0, only return the //mountRoot/all share
1289 * if in fact it has been been created in the subMounts table.
1290 * This is to allow sites that want to restrict access to the
1293 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
1294 0, KEY_QUERY_VALUE, &parmKey);
1295 if (code == ERROR_SUCCESS) {
1296 len = sizeof(allSubmount);
1297 code = RegQueryValueEx(parmKey, "AllSubmount", NULL, NULL,
1298 (BYTE *) &allSubmount, &len);
1299 if (code != ERROR_SUCCESS) {
1302 RegCloseKey (parmKey);
1305 if (allSubmount && _stricmp(shareName, "all") == 0) {
1310 /* In case, the all share is disabled we need to still be able
1311 * to handle ioctl requests
1313 if (_stricmp(shareName, "ioctl$") == 0) {
1314 *pathNamep = strdup("/.__ioctl__");
1319 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\OpenAFS\\Client\\Submounts",
1320 0, KEY_QUERY_VALUE, &parmKey);
1321 if (code == ERROR_SUCCESS) {
1322 len = sizeof(pathName);
1323 code = RegQueryValueEx(parmKey, shareName, NULL, NULL,
1324 (BYTE *) pathName, &len);
1325 if (code != ERROR_SUCCESS)
1327 RegCloseKey (parmKey);
1332 strcpy(sbmtpath, cm_confDir);
1333 strcat(sbmtpath, "/afsdsbmt.ini");
1334 len = GetPrivateProfileString("AFS Submounts", shareName, "",
1335 pathName, sizeof(pathName), sbmtpath);
1337 if (len != 0 && len != sizeof(pathName) - 1) {
1338 /* We can accept either unix or PC style AFS pathnames. Convert
1339 * Unix-style to PC style here for internal use.
1342 if (strncmp(p, cm_mountRoot, strlen(cm_mountRoot)) == 0)
1343 p += strlen(cm_mountRoot); /* skip mount path */
1346 if (*q == '/') *q = '\\'; /* change to \ */
1352 if (var = smb_stristr(p, VNUserName)) {
1353 if (uidp && uidp->unp)
1354 smb_subst(p, var, sizeof(VNUserName),uidp->unp->name);
1356 smb_subst(p, var, sizeof(VNUserName)," ");
1358 else if (var = smb_stristr(p, VNLCUserName))
1360 if (uidp && uidp->unp)
1361 strcpy(temp, uidp->unp->name);
1365 smb_subst(p, var, sizeof(VNLCUserName), temp);
1367 else if (var = smb_stristr(p, VNComputerName))
1369 sizeTemp = sizeof(temp);
1370 GetComputerName((LPTSTR)temp, &sizeTemp);
1371 smb_subst(p, var, sizeof(VNComputerName), temp);
1373 else if (var = smb_stristr(p, VNLCComputerName))
1375 sizeTemp = sizeof(temp);
1376 GetComputerName((LPTSTR)temp, &sizeTemp);
1378 smb_subst(p, var, sizeof(VNLCComputerName), temp);
1383 *pathNamep = strdup(p);
1386 else /* create \\<netbiosName>\<cellname> */
1388 char * p = shareName;
1395 /* Get the full name for this cell */
1396 code = cm_SearchCellFile(p, temp, 0, 0);
1397 #ifdef AFS_AFSDB_ENV
1398 if (code && cm_dnsEnabled) {
1400 code = cm_SearchCellByDNS(p, temp, &ttl, 0, 0);
1403 /* construct the path */
1405 sprintf(pathName,rw ? "/.%s/" : "/%s/",temp);
1406 *pathNamep = strdup(strlwr(pathName));
1415 /* Client-side offline caching policy types */
1416 #define CSC_POLICY_MANUAL 0
1417 #define CSC_POLICY_DOCUMENTS 1
1418 #define CSC_POLICY_PROGRAMS 2
1419 #define CSC_POLICY_DISABLE 3
1421 int smb_FindShareCSCPolicy(char *shareName)
1427 int retval = CSC_POLICY_MANUAL;
1429 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1430 "SOFTWARE\\OpenAFS\\Client\\CSCPolicy",
1433 REG_OPTION_NON_VOLATILE,
1439 len = sizeof(policy);
1440 if ( RegQueryValueEx( hkCSCPolicy, shareName, 0, &dwType, policy, &len ) ||
1442 retval = CSC_POLICY_MANUAL;
1444 else if (stricmp(policy, "documents") == 0)
1446 retval = CSC_POLICY_DOCUMENTS;
1448 else if (stricmp(policy, "programs") == 0)
1450 retval = CSC_POLICY_PROGRAMS;
1452 else if (stricmp(policy, "disable") == 0)
1454 retval = CSC_POLICY_DISABLE;
1457 RegCloseKey(hkCSCPolicy);
1461 /* find a dir search structure by cookie value, and return it held.
1462 * Must be called with smb_globalLock held.
1464 smb_dirSearch_t *smb_FindDirSearchNL(long cookie)
1466 smb_dirSearch_t *dsp;
1468 for(dsp = smb_firstDirSearchp; dsp; dsp = (smb_dirSearch_t *) osi_QNext(&dsp->q)) {
1469 if (dsp->cookie == cookie) {
1470 if (dsp != smb_firstDirSearchp) {
1471 /* move to head of LRU queue, too, if we're not already there */
1472 if (smb_lastDirSearchp == (smb_dirSearch_t *) &dsp->q)
1473 smb_lastDirSearchp = (smb_dirSearch_t *)
1475 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1476 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1477 if (!smb_lastDirSearchp)
1478 smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1487 void smb_DeleteDirSearch(smb_dirSearch_t *dsp)
1489 lock_ObtainWrite(&smb_globalLock);
1490 dsp->flags |= SMB_DIRSEARCH_DELETE;
1491 lock_ReleaseWrite(&smb_globalLock);
1492 lock_ObtainMutex(&dsp->mx);
1493 if(dsp->scp != NULL) {
1494 lock_ObtainMutex(&dsp->scp->mx);
1495 if (dsp->flags & SMB_DIRSEARCH_BULKST) {
1496 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
1497 dsp->scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
1498 dsp->scp->bulkStatProgress = hones;
1500 lock_ReleaseMutex(&dsp->scp->mx);
1502 lock_ReleaseMutex(&dsp->mx);
1505 void smb_ReleaseDirSearch(smb_dirSearch_t *dsp)
1511 lock_ObtainWrite(&smb_globalLock);
1512 osi_assert(dsp->refCount-- > 0);
1513 if (dsp->refCount == 0 && (dsp->flags & SMB_DIRSEARCH_DELETE)) {
1514 if (&dsp->q == (osi_queue_t *) smb_lastDirSearchp)
1515 smb_lastDirSearchp = (smb_dirSearch_t *) osi_QPrev(&smb_lastDirSearchp->q);
1516 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1517 lock_FinalizeMutex(&dsp->mx);
1521 lock_ReleaseWrite(&smb_globalLock);
1523 /* do this now to avoid spurious locking hierarchy creation */
1524 if (scp) cm_ReleaseSCache(scp);
1527 /* find a dir search structure by cookie value, and return it held */
1528 smb_dirSearch_t *smb_FindDirSearch(long cookie)
1530 smb_dirSearch_t *dsp;
1532 lock_ObtainWrite(&smb_globalLock);
1533 dsp = smb_FindDirSearchNL(cookie);
1534 lock_ReleaseWrite(&smb_globalLock);
1538 /* GC some dir search entries, in the address space expected by the specific protocol.
1539 * Must be called with smb_globalLock held; release the lock temporarily.
1541 #define SMB_DIRSEARCH_GCMAX 10 /* how many at once */
1542 void smb_GCDirSearches(int isV3)
1544 smb_dirSearch_t *prevp;
1545 smb_dirSearch_t *tp;
1546 smb_dirSearch_t *victimsp[SMB_DIRSEARCH_GCMAX];
1550 victimCount = 0; /* how many have we got so far */
1551 for(tp = smb_lastDirSearchp; tp; tp=prevp) {
1552 /* we'll move tp from queue, so
1555 prevp = (smb_dirSearch_t *) osi_QPrev(&tp->q);
1556 /* if no one is using this guy, and we're either in the new protocol,
1557 * or we're in the old one and this is a small enough ID to be useful
1558 * to the old protocol, GC this guy.
1560 if (tp->refCount == 0 && (isV3 || tp->cookie <= 255)) {
1561 /* hold and delete */
1562 tp->flags |= SMB_DIRSEARCH_DELETE;
1563 victimsp[victimCount++] = tp;
1567 /* don't do more than this */
1568 if (victimCount >= SMB_DIRSEARCH_GCMAX) break;
1571 /* now release them */
1572 lock_ReleaseWrite(&smb_globalLock);
1573 for(i = 0; i < victimCount; i++) {
1574 smb_ReleaseDirSearch(victimsp[i]);
1576 lock_ObtainWrite(&smb_globalLock);
1579 /* function for allocating a dir search entry. We need these to remember enough context
1580 * since we don't get passed the path from call to call during a directory search.
1582 * Returns a held dir search structure, and bumps the reference count on the vnode,
1583 * since it saves a pointer to the vnode.
1585 smb_dirSearch_t *smb_NewDirSearch(int isV3)
1587 smb_dirSearch_t *dsp;
1591 lock_ObtainWrite(&smb_globalLock);
1594 /* what's the biggest ID allowed in this version of the protocol */
1595 if (isV3) maxAllowed = 65535;
1596 else maxAllowed = 255;
1599 /* twice so we have enough tries to find guys we GC after one pass;
1600 * 10 extra is just in case I mis-counted.
1602 if (++counter > 2*maxAllowed+10) osi_panic("afsd: dir search cookie leak",
1603 __FILE__, __LINE__);
1604 if (smb_dirSearchCounter > maxAllowed) {
1605 smb_dirSearchCounter = 1;
1606 smb_GCDirSearches(isV3); /* GC some (drops global lock) */
1608 dsp = smb_FindDirSearchNL(smb_dirSearchCounter);
1610 /* don't need to watch for refcount zero and deleted, since
1611 * we haven't dropped the global lock.
1614 ++smb_dirSearchCounter;
1618 dsp = malloc(sizeof(*dsp));
1619 memset(dsp, 0, sizeof(*dsp));
1620 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1621 if (!smb_lastDirSearchp) smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1622 dsp->cookie = smb_dirSearchCounter;
1623 ++smb_dirSearchCounter;
1625 lock_InitializeMutex(&dsp->mx, "cm_dirSearch_t");
1626 dsp->lastTime = osi_Time();
1629 lock_ReleaseWrite(&smb_globalLock);
1633 static smb_packet_t *GetPacket(void)
1637 unsigned int npar, seg, tb_sel;
1640 lock_ObtainWrite(&smb_globalLock);
1641 tbp = smb_packetFreeListp;
1643 smb_packetFreeListp = tbp->nextp;
1644 lock_ReleaseWrite(&smb_globalLock);
1647 tbp = calloc(65540,1);
1649 tbp = malloc(sizeof(smb_packet_t));
1651 tbp->magic = SMB_PACKETMAGIC;
1654 tbp->resumeCode = 0;
1660 tbp->ncb_length = 0;
1665 npar = SMB_PACKETSIZE >> 4; /* number of paragraphs */
1668 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1670 afsi_log("Cannot allocate %d paragraphs of DOS memory",
1672 osi_panic("",__FILE__,__LINE__);
1675 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
1680 tbp->dos_pkt = (seg * 16) + 0; /* DOS physical address */
1681 tbp->dos_pkt_sel = tb_sel;
1684 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1689 smb_packet_t *smb_CopyPacket(smb_packet_t *pkt)
1693 memcpy(tbp, pkt, sizeof(smb_packet_t));
1694 tbp->wctp = tbp->data + ((unsigned int)pkt->wctp - (unsigned int)pkt->data);
1698 static NCB *GetNCB(void)
1703 unsigned int npar, seg, tb_sel;
1706 lock_ObtainWrite(&smb_globalLock);
1707 tbp = smb_ncbFreeListp;
1709 smb_ncbFreeListp = tbp->nextp;
1710 lock_ReleaseWrite(&smb_globalLock);
1713 tbp = calloc(sizeof(*tbp),1);
1715 tbp = malloc(sizeof(*tbp));
1716 npar = (sizeof(NCB)+15) >> 4; /* number of paragraphs */
1719 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1721 afsi_log("Cannot allocate %d paragraphs of DOS mem in GetNCB",
1723 osi_panic("",__FILE__,__LINE__);
1725 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X in GetNCB",
1730 tbp->dos_ncb = (seg * 16) + 0; /* DOS physical address */
1731 tbp->dos_ncb_sel = tb_sel;
1733 tbp->magic = SMB_NCBMAGIC;
1736 osi_assert(tbp->magic == SMB_NCBMAGIC);
1738 memset(&tbp->ncb, 0, sizeof(NCB));
1741 dos_memset(tbp->dos_ncb, 0, sizeof(NCB));
1746 void smb_FreePacket(smb_packet_t *tbp)
1748 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1750 lock_ObtainWrite(&smb_globalLock);
1751 tbp->nextp = smb_packetFreeListp;
1752 smb_packetFreeListp = tbp;
1753 tbp->magic = SMB_PACKETMAGIC;
1756 tbp->resumeCode = 0;
1762 tbp->ncb_length = 0;
1764 lock_ReleaseWrite(&smb_globalLock);
1767 static void FreeNCB(NCB *bufferp)
1771 tbp = (smb_ncb_t *) bufferp;
1772 osi_assert(tbp->magic == SMB_NCBMAGIC);
1774 lock_ObtainWrite(&smb_globalLock);
1775 tbp->nextp = smb_ncbFreeListp;
1776 smb_ncbFreeListp = tbp;
1777 lock_ReleaseWrite(&smb_globalLock);
1780 /* get a ptr to the data part of a packet, and its count */
1781 unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp)
1785 unsigned char *afterParmsp;
1787 parmBytes = *smbp->wctp << 1;
1788 afterParmsp = smbp->wctp + parmBytes + 1;
1790 dataBytes = afterParmsp[0] + (afterParmsp[1]<<8);
1791 if (nbytesp) *nbytesp = dataBytes;
1793 /* don't forget to skip the data byte count, since it follows
1794 * the parameters; that's where the "2" comes from below.
1796 return (unsigned char *) (afterParmsp + 2);
1799 /* must set all the returned parameters before playing around with the
1800 * data region, since the data region is located past the end of the
1801 * variable number of parameters.
1803 void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize)
1805 unsigned char *afterParmsp;
1807 afterParmsp = smbp->wctp + ((*smbp->wctp)<<1) + 1;
1809 *afterParmsp++ = dsize & 0xff;
1810 *afterParmsp = (dsize>>8) & 0xff;
1813 /* return the parm'th parameter in the smbp packet */
1814 unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm)
1817 unsigned char *parmDatap;
1819 parmCount = *smbp->wctp;
1821 if (parm >= parmCount) {
1826 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1827 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1828 parm, parmCount, smbp->ncb_length);
1830 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1831 1, smbp->ncb_length, ptbuf, smbp);
1832 DeregisterEventSource(h);
1836 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1837 parm, parmCount, smbp->ncb_length);
1838 osi_Log0(smb_logp, s);
1840 osi_panic(s, __FILE__, __LINE__);
1842 parmDatap = smbp->wctp + (2*parm) + 1;
1844 return parmDatap[0] + (parmDatap[1] << 8);
1847 /* return the parm'th parameter in the smbp packet */
1848 unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset)
1851 unsigned char *parmDatap;
1853 parmCount = *smbp->wctp;
1855 if (parm * 2 + offset >= parmCount * 2) {
1860 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1861 sprintf(s, "Bad SMB param %d offset %d out of %d, ncb len %d",
1862 parm, offset, parmCount, smbp->ncb_length);
1864 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1865 1, smbp->ncb_length, ptbuf, smbp);
1866 DeregisterEventSource(h);
1870 sprintf(s, "Bad SMB param %d offset %d out of %d, "
1872 parm, offset, parmCount, smbp->ncb_length);
1873 osi_Log0(smb_logp, s);
1876 osi_panic(s, __FILE__, __LINE__);
1878 parmDatap = smbp->wctp + (2*parm) + 1 + offset;
1880 return parmDatap[0] + (parmDatap[1] << 8);
1883 void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue)
1887 /* make sure we have enough slots */
1888 if (*smbp->wctp <= slot) *smbp->wctp = slot+1;
1890 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1891 *parmDatap++ = parmValue & 0xff;
1892 *parmDatap = (parmValue>>8) & 0xff;
1895 void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue)
1899 /* make sure we have enough slots */
1900 if (*smbp->wctp <= slot) *smbp->wctp = slot+2;
1902 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1903 *parmDatap++ = parmValue & 0xff;
1904 *parmDatap++ = (parmValue>>8) & 0xff;
1905 *parmDatap++ = (parmValue>>16) & 0xff;
1906 *parmDatap++ = (parmValue>>24) & 0xff;
1909 void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep)
1914 /* make sure we have enough slots */
1915 if (*smbp->wctp <= slot) *smbp->wctp = slot+4;
1917 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1919 *parmDatap++ = *parmValuep++;
1922 void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue)
1926 /* make sure we have enough slots */
1927 if (*smbp->wctp <= slot) {
1928 if (smbp->oddByte) {
1930 *smbp->wctp = slot+1;
1935 parmDatap = smbp->wctp + 2*slot + 1 + (1 - smbp->oddByte);
1936 *parmDatap++ = parmValue & 0xff;
1939 void smb_StripLastComponent(char *outPathp, char **lastComponentp, char *inPathp)
1943 lastSlashp = strrchr(inPathp, '\\');
1945 *lastComponentp = lastSlashp;
1948 if (inPathp == lastSlashp)
1950 *outPathp++ = *inPathp++;
1959 unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp)
1964 *chainpp = inp + strlen(inp) + 1; /* skip over null-terminated string */
1969 unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp)
1975 tlen = inp[0] + (inp[1]<<8);
1976 inp += 2; /* skip length field */
1979 *chainpp = inp + tlen;
1988 /* format a packet as a response */
1989 void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *op)
1994 outp = (smb_t *) op;
1996 /* zero the basic structure through the smb_wct field, and zero the data
1997 * size field, assuming that wct stays zero; otherwise, you have to
1998 * explicitly set the data size field, too.
2000 inSmbp = (smb_t *) inp;
2001 memset(outp, 0, sizeof(smb_t)+2);
2007 outp->com = inSmbp->com;
2008 outp->tid = inSmbp->tid;
2009 outp->pid = inSmbp->pid;
2010 outp->uid = inSmbp->uid;
2011 outp->mid = inSmbp->mid;
2012 outp->res[0] = inSmbp->res[0];
2013 outp->res[1] = inSmbp->res[1];
2014 op->inCom = inSmbp->com;
2016 outp->reb = 0x80; /* SERVER_RESP */
2017 outp->flg2 = 0x1; /* KNOWS_LONG_NAMES */
2019 /* copy fields in generic packet area */
2020 op->wctp = &outp->wct;
2023 /* send a (probably response) packet; vcp tells us to whom to send it.
2024 * we compute the length by looking at wct and bcc fields.
2026 void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp)
2043 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
2046 memset((char *)ncbp, 0, sizeof(NCB));
2048 extra = 2 * (*inp->wctp); /* space used by parms, in bytes */
2049 tp = inp->wctp + 1+ extra; /* points to count of data bytes */
2050 extra += tp[0] + (tp[1]<<8);
2051 extra += ((unsigned int)inp->wctp - (unsigned int)inp->data); /* distance to last wct field */
2052 extra += 3; /* wct and length fields */
2054 ncbp->ncb_length = extra; /* bytes to send */
2055 ncbp->ncb_lsn = (unsigned char) vcp->lsn; /* vc to use */
2056 ncbp->ncb_lana_num = vcp->lana;
2057 ncbp->ncb_command = NCBSEND; /* op means send data */
2059 ncbp->ncb_buffer = (char *) inp;/* packet */
2060 code = Netbios(ncbp);
2062 ncbp->ncb_buffer = inp->dos_pkt;/* packet */
2063 ((smb_ncb_t*)ncbp)->orig_pkt = inp;
2065 /* copy header information from virtual to DOS address space */
2066 dosmemput((char*)inp, SMB_PACKETSIZE, inp->dos_pkt);
2067 code = Netbios(ncbp, dos_ncb);
2071 osi_Log1(smb_logp, "SendPacket failure code %d", code);
2077 void smb_MapNTError(long code, unsigned long *NTStatusp)
2079 unsigned long NTStatus;
2081 /* map CM_ERROR_* errors to NT 32-bit status codes */
2082 /* NT Status codes are listed in ntstatus.h not winerror.h */
2083 if (code == CM_ERROR_NOSUCHCELL) {
2084 NTStatus = 0xC000000FL; /* No such file */
2086 else if (code == CM_ERROR_NOSUCHVOLUME) {
2087 NTStatus = 0xC000000FL; /* No such file */
2089 else if (code == CM_ERROR_TIMEDOUT) {
2090 NTStatus = 0xC00000CFL; /* Sharing Paused */
2092 else if (code == CM_ERROR_RETRY) {
2093 NTStatus = 0xC000022DL; /* Retry */
2095 else if (code == CM_ERROR_NOACCESS) {
2096 NTStatus = 0xC0000022L; /* Access denied */
2098 else if (code == CM_ERROR_READONLY) {
2099 NTStatus = 0xC00000A2L; /* Write protected */
2101 else if (code == CM_ERROR_NOSUCHFILE) {
2102 NTStatus = 0xC000000FL; /* No such file */
2104 else if (code == CM_ERROR_NOSUCHPATH) {
2105 NTStatus = 0xC000003AL; /* Object path not found */
2107 else if (code == CM_ERROR_TOOBIG) {
2108 NTStatus = 0xC000007BL; /* Invalid image format */
2110 else if (code == CM_ERROR_INVAL) {
2111 NTStatus = 0xC000000DL; /* Invalid parameter */
2113 else if (code == CM_ERROR_BADFD) {
2114 NTStatus = 0xC0000008L; /* Invalid handle */
2116 else if (code == CM_ERROR_BADFDOP) {
2117 NTStatus = 0xC0000022L; /* Access denied */
2119 else if (code == CM_ERROR_EXISTS) {
2120 NTStatus = 0xC0000035L; /* Object name collision */
2122 else if (code == CM_ERROR_NOTEMPTY) {
2123 NTStatus = 0xC0000101L; /* Directory not empty */
2125 else if (code == CM_ERROR_CROSSDEVLINK) {
2126 NTStatus = 0xC00000D4L; /* Not same device */
2128 else if (code == CM_ERROR_NOTDIR) {
2129 NTStatus = 0xC0000103L; /* Not a directory */
2131 else if (code == CM_ERROR_ISDIR) {
2132 NTStatus = 0xC00000BAL; /* File is a directory */
2134 else if (code == CM_ERROR_BADOP) {
2135 NTStatus = 0xC09820FFL; /* SMB no support */
2137 else if (code == CM_ERROR_BADSHARENAME) {
2138 NTStatus = 0xC00000CCL; /* Bad network name */
2140 else if (code == CM_ERROR_NOIPC) {
2142 NTStatus = 0xC0000022L; /* Access Denied */
2144 NTStatus = 0xC000013DL; /* Remote Resources */
2147 else if (code == CM_ERROR_CLOCKSKEW) {
2148 NTStatus = 0xC0000133L; /* Time difference at DC */
2150 else if (code == CM_ERROR_BADTID) {
2151 NTStatus = 0xC0982005L; /* SMB bad TID */
2153 else if (code == CM_ERROR_USESTD) {
2154 NTStatus = 0xC09820FBL; /* SMB use standard */
2156 else if (code == CM_ERROR_QUOTA) {
2157 NTStatus = 0xC0000044L; /* Quota exceeded */
2159 else if (code == CM_ERROR_SPACE) {
2160 NTStatus = 0xC000007FL; /* Disk full */
2162 else if (code == CM_ERROR_ATSYS) {
2163 NTStatus = 0xC0000033L; /* Object name invalid */
2165 else if (code == CM_ERROR_BADNTFILENAME) {
2166 NTStatus = 0xC0000033L; /* Object name invalid */
2168 else if (code == CM_ERROR_WOULDBLOCK) {
2169 NTStatus = 0xC0000055L; /* Lock not granted */
2171 else if (code == CM_ERROR_PARTIALWRITE) {
2172 NTStatus = 0xC000007FL; /* Disk full */
2174 else if (code == CM_ERROR_BUFFERTOOSMALL) {
2175 NTStatus = 0xC0000023L; /* Buffer too small */
2177 else if (code == CM_ERROR_AMBIGUOUS_FILENAME) {
2178 NTStatus = 0xC0000035L; /* Object name collision */
2180 else if (code == CM_ERROR_BADPASSWORD) {
2181 NTStatus = 0xC000006DL; /* unknown username or bad password */
2183 else if (code == CM_ERROR_BADLOGONTYPE) {
2184 NTStatus = 0xC000015BL; /* logon type not granted */
2186 else if (code == CM_ERROR_GSSCONTINUE) {
2187 NTStatus = 0xC0000016L; /* more processing required */
2190 NTStatus = 0xC0982001L; /* SMB non-specific error */
2193 *NTStatusp = NTStatus;
2194 osi_Log2(smb_logp, "SMB SEND code %x as NT %x", code, NTStatus);
2197 void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
2198 unsigned char *classp)
2200 unsigned char class;
2201 unsigned short error;
2203 /* map CM_ERROR_* errors to SMB errors */
2204 if (code == CM_ERROR_NOSUCHCELL) {
2206 error = 3; /* bad path */
2208 else if (code == CM_ERROR_NOSUCHVOLUME) {
2210 error = 3; /* bad path */
2212 else if (code == CM_ERROR_TIMEDOUT) {
2214 error = 81; /* server is paused */
2216 else if (code == CM_ERROR_RETRY) {
2217 class = 2; /* shouldn't happen */
2220 else if (code == CM_ERROR_NOACCESS) {
2222 error = 4; /* bad access */
2224 else if (code == CM_ERROR_READONLY) {
2226 error = 19; /* read only */
2228 else if (code == CM_ERROR_NOSUCHFILE) {
2230 error = 2; /* ENOENT! */
2232 else if (code == CM_ERROR_NOSUCHPATH) {
2234 error = 3; /* Bad path */
2236 else if (code == CM_ERROR_TOOBIG) {
2238 error = 11; /* bad format */
2240 else if (code == CM_ERROR_INVAL) {
2241 class = 2; /* server non-specific error code */
2244 else if (code == CM_ERROR_BADFD) {
2246 error = 6; /* invalid file handle */
2248 else if (code == CM_ERROR_BADFDOP) {
2249 class = 1; /* invalid op on FD */
2252 else if (code == CM_ERROR_EXISTS) {
2254 error = 80; /* file already exists */
2256 else if (code == CM_ERROR_NOTEMPTY) {
2258 error = 5; /* delete directory not empty */
2260 else if (code == CM_ERROR_CROSSDEVLINK) {
2262 error = 17; /* EXDEV */
2264 else if (code == CM_ERROR_NOTDIR) {
2265 class = 1; /* bad path */
2268 else if (code == CM_ERROR_ISDIR) {
2269 class = 1; /* access denied; DOS doesn't have a good match */
2272 else if (code == CM_ERROR_BADOP) {
2276 else if (code == CM_ERROR_BADSHARENAME) {
2280 else if (code == CM_ERROR_NOIPC) {
2282 error = 4; /* bad access */
2284 else if (code == CM_ERROR_CLOCKSKEW) {
2285 class = 1; /* invalid function */
2288 else if (code == CM_ERROR_BADTID) {
2292 else if (code == CM_ERROR_USESTD) {
2296 else if (code == CM_ERROR_REMOTECONN) {
2300 else if (code == CM_ERROR_QUOTA) {
2301 if (vcp->flags & SMB_VCFLAG_USEV3) {
2303 error = 39; /* disk full */
2307 error = 5; /* access denied */
2310 else if (code == CM_ERROR_SPACE) {
2311 if (vcp->flags & SMB_VCFLAG_USEV3) {
2313 error = 39; /* disk full */
2317 error = 5; /* access denied */
2320 else if (code == CM_ERROR_PARTIALWRITE) {
2322 error = 39; /* disk full */
2324 else if (code == CM_ERROR_ATSYS) {
2326 error = 2; /* ENOENT */
2328 else if (code == CM_ERROR_WOULDBLOCK) {
2330 error = 33; /* lock conflict */
2332 else if (code == CM_ERROR_NOFILES) {
2334 error = 18; /* no files in search */
2336 else if (code == CM_ERROR_RENAME_IDENTICAL) {
2338 error = 183; /* Samba uses this */
2340 else if (code == CM_ERROR_BADPASSWORD || code == CM_ERROR_BADLOGONTYPE) {
2341 /* we don't have a good way of reporting CM_ERROR_BADLOGONTYPE */
2343 error = 2; /* bad password */
2352 osi_Log3(smb_logp, "SMB SEND code %x as SMB %d: %d", code, class, error);
2355 long smb_SendCoreBadOp(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2357 return CM_ERROR_BADOP;
2360 long smb_ReceiveCoreEcho(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2362 unsigned short EchoCount, i;
2363 char *data, *outdata;
2366 EchoCount = (unsigned short) smb_GetSMBParm(inp, 0);
2368 for (i=1; i<=EchoCount; i++) {
2369 data = smb_GetSMBData(inp, &dataSize);
2370 smb_SetSMBParm(outp, 0, i);
2371 smb_SetSMBDataLength(outp, dataSize);
2372 outdata = smb_GetSMBData(outp, NULL);
2373 memcpy(outdata, data, dataSize);
2374 smb_SendPacket(vcp, outp);
2380 long smb_ReceiveCoreReadRaw(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2383 long count, minCount, finalCount;
2387 cm_user_t *userp = NULL;
2391 char *rawBuf = NULL;
2393 dos_ptr rawBuf = NULL;
2400 fd = smb_GetSMBParm(inp, 0);
2401 count = smb_GetSMBParm(inp, 3);
2402 minCount = smb_GetSMBParm(inp, 4);
2403 offset.HighPart = 0; /* too bad */
2404 offset.LowPart = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
2406 osi_Log3(smb_logp, "smb_ReceieveCoreReadRaw fd %d, off 0x%x, size 0x%x",
2407 fd, offset.LowPart, count);
2409 fidp = smb_FindFID(vcp, fd, 0);
2413 lock_ObtainMutex(&smb_RawBufLock);
2415 /* Get a raw buf, from head of list */
2416 rawBuf = smb_RawBufs;
2418 smb_RawBufs = *(char **)smb_RawBufs;
2420 smb_RawBufs = _farpeekl(_dos_ds, smb_RawBufs);
2423 lock_ReleaseMutex(&smb_RawBufLock);
2427 if (fidp->flags & SMB_FID_IOCTL)
2430 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp);
2432 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp, rawBuf);
2435 /* Give back raw buffer */
2436 lock_ObtainMutex(&smb_RawBufLock);
2438 *((char **) rawBuf) = smb_RawBufs;
2440 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2443 smb_RawBufs = rawBuf;
2444 lock_ReleaseMutex(&smb_RawBufLock);
2447 smb_ReleaseFID(fidp);
2451 userp = smb_GetUser(vcp, inp);
2454 code = smb_ReadData(fidp, &offset, count, rawBuf, userp, &finalCount);
2456 /* have to give ReadData flag so it will treat buffer as DOS mem. */
2457 code = smb_ReadData(fidp, &offset, count, (unsigned char *)rawBuf,
2458 userp, &finalCount, TRUE /* rawFlag */);
2465 cm_ReleaseUser(userp);
2468 smb_ReleaseFID(fidp);
2473 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
2475 memset((char *)ncbp, 0, sizeof(NCB));
2477 ncbp->ncb_length = (unsigned short) finalCount;
2478 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
2479 ncbp->ncb_lana_num = vcp->lana;
2480 ncbp->ncb_command = NCBSEND;
2481 ncbp->ncb_buffer = rawBuf;
2484 code = Netbios(ncbp);
2486 code = Netbios(ncbp, dos_ncb);
2489 osi_Log1(smb_logp, "ReadRaw send failure code %d", code);
2492 /* Give back raw buffer */
2493 lock_ObtainMutex(&smb_RawBufLock);
2495 *((char **) rawBuf) = smb_RawBufs;
2497 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2500 smb_RawBufs = rawBuf;
2501 lock_ReleaseMutex(&smb_RawBufLock);
2507 long smb_ReceiveCoreLockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2512 long smb_ReceiveCoreUnlockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2517 long smb_ReceiveNegotiate(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2524 int protoIndex; /* index we're using */
2529 char protocol_array[10][1024]; /* protocol signature of the client */
2530 int caps; /* capabilities */
2533 TIME_ZONE_INFORMATION tzi;
2535 osi_Log1(smb_logp, "SMB receive negotiate; %d + 1 ongoing ops",
2539 DWORD now = GetCurrentTime();
2540 if (now - last_msg_time >= 30000
2541 && now - last_msg_time <= 90000) {
2543 "Setting dead_vcp %x", active_vcp);
2545 smb_ReleaseVC(dead_vcp);
2547 "Previous dead_vcp %x", dead_vcp);
2549 smb_HoldVC(active_vcp);
2550 dead_vcp = active_vcp;
2551 dead_vcp->flags |= SMB_VCFLAG_ALREADYDEAD;
2556 inp->flags |= SMB_PACKETFLAG_PROFILE_UPDATE_OK;
2558 namep = smb_GetSMBData(inp, &dbytes);
2561 coreProtoIndex = -1; /* not found */
2564 while(namex < dbytes) {
2565 osi_Log1(smb_logp, "Protocol %s",
2566 osi_LogSaveString(smb_logp, namep+1));
2567 strcpy(protocol_array[tcounter], namep+1);
2569 /* namep points at the first protocol, or really, a 0x02
2570 * byte preceding the null-terminated ASCII name.
2572 if (strcmp("PC NETWORK PROGRAM 1.0", namep+1) == 0) {
2573 coreProtoIndex = tcounter;
2575 else if (smb_useV3 && strcmp("LM1.2X002", namep+1) == 0) {
2576 v3ProtoIndex = tcounter;
2578 else if (smb_useV3 && strcmp("NT LM 0.12", namep+1) == 0) {
2579 NTProtoIndex = tcounter;
2582 /* compute size of protocol entry */
2583 entryLength = strlen(namep+1);
2584 entryLength += 2; /* 0x02 bytes and null termination */
2586 /* advance over this protocol entry */
2587 namex += entryLength;
2588 namep += entryLength;
2589 tcounter++; /* which proto entry we're looking at */
2592 if (NTProtoIndex != -1) {
2593 protoIndex = NTProtoIndex;
2594 vcp->flags |= (SMB_VCFLAG_USENT | SMB_VCFLAG_USEV3);
2596 else if (v3ProtoIndex != -1) {
2597 protoIndex = v3ProtoIndex;
2598 vcp->flags |= SMB_VCFLAG_USEV3;
2600 else if (coreProtoIndex != -1) {
2601 protoIndex = coreProtoIndex;
2602 vcp->flags |= SMB_VCFLAG_USECORE;
2604 else protoIndex = -1;
2606 if (protoIndex == -1)
2607 return CM_ERROR_INVAL;
2608 else if (NTProtoIndex != -1) {
2609 smb_SetSMBParm(outp, 0, protoIndex);
2610 if (smb_authType != SMB_AUTH_NONE) {
2611 smb_SetSMBParmByte(outp, 1,
2612 NEGOTIATE_SECURITY_USER_LEVEL |
2613 NEGOTIATE_SECURITY_CHALLENGE_RESPONSE); /* user level security, challenge response */
2615 smb_SetSMBParmByte(outp, 1, 0); /* share level auth with plaintext password. */
2617 smb_SetSMBParm(outp, 1, smb_maxMpxRequests); /* max multiplexed requests */
2618 smb_SetSMBParm(outp, 2, smb_maxVCPerServer); /* max VCs per consumer/server connection */
2619 smb_SetSMBParmLong(outp, 3, SMB_PACKETSIZE); /* xmit buffer size */
2620 smb_SetSMBParmLong(outp, 5, SMB_MAXRAWSIZE); /* raw buffer size */
2621 /* The session key is not a well documented field however most clients
2622 * will echo back the session key to the server. Currently we are using
2623 * the same value for all sessions. We should generate a random value
2624 * and store it into the vcp
2626 smb_SetSMBParm(outp, 7, 1); /* next 2: session key */
2627 smb_SetSMBParm(outp, 8, 1);
2629 * Tried changing the capabilities to support for W2K - defect 117695
2630 * Maybe something else needs to be changed here?
2634 smb_SetSMBParmLong(outp, 9, 0x43fd);
2636 smb_SetSMBParmLong(outp, 9, 0x251);
2639 * 32-bit error codes *
2643 caps = NTNEGOTIATE_CAPABILITY_NTSTATUS |
2644 NTNEGOTIATE_CAPABILITY_NTFIND |
2645 NTNEGOTIATE_CAPABILITY_RAWMODE |
2646 NTNEGOTIATE_CAPABILITY_NTSMB;
2648 if ( smb_authType == SMB_AUTH_EXTENDED )
2649 caps |= NTNEGOTIATE_CAPABILITY_EXTENDED_SECURITY;
2651 smb_SetSMBParmLong(outp, 9, caps);
2653 smb_SearchTimeFromUnixTime(&dosTime, unixTime);
2654 smb_SetSMBParmLong(outp, 11, LOWORD(dosTime));/* server time */
2655 smb_SetSMBParmLong(outp, 13, HIWORD(dosTime));/* server date */
2657 GetTimeZoneInformation(&tzi);
2658 smb_SetSMBParm(outp, 15, (unsigned short) tzi.Bias); /* server tzone */
2660 if (smb_authType == SMB_AUTH_NTLM) {
2661 smb_SetSMBParmByte(outp, 16, MSV1_0_CHALLENGE_LENGTH);/* Encryption key length */
2662 smb_SetSMBDataLength(outp, MSV1_0_CHALLENGE_LENGTH + smb_ServerDomainNameLength);
2663 /* paste in encryption key */
2664 datap = smb_GetSMBData(outp, NULL);
2665 memcpy(datap,vcp->encKey,MSV1_0_CHALLENGE_LENGTH);
2666 /* and the faux domain name */
2667 strcpy(datap + MSV1_0_CHALLENGE_LENGTH,smb_ServerDomainName);
2668 } else if ( smb_authType == SMB_AUTH_EXTENDED ) {
2672 smb_SetSMBParmByte(outp, 16, 0); /* Encryption key length */
2674 smb_NegotiateExtendedSecurity(&secBlob, &secBlobLength);
2676 smb_SetSMBDataLength(outp, secBlobLength + sizeof(smb_ServerGUID));
2678 datap = smb_GetSMBData(outp, NULL);
2679 memcpy(datap, &smb_ServerGUID, sizeof(smb_ServerGUID));
2682 datap += sizeof(smb_ServerGUID);
2683 memcpy(datap, secBlob, secBlobLength);
2687 smb_SetSMBParmByte(outp, 16, 0); /* Encryption key length */
2688 smb_SetSMBDataLength(outp, 0); /* Perhaps we should specify 8 bytes anyway */
2691 else if (v3ProtoIndex != -1) {
2692 smb_SetSMBParm(outp, 0, protoIndex);
2694 /* NOTE: Extended authentication cannot be negotiated with v3
2695 * therefore we fail over to NTLM
2697 if (smb_authType == SMB_AUTH_NTLM || smb_authType == SMB_AUTH_EXTENDED) {
2698 smb_SetSMBParm(outp, 1,
2699 NEGOTIATE_SECURITY_USER_LEVEL |
2700 NEGOTIATE_SECURITY_CHALLENGE_RESPONSE); /* user level security, challenge response */
2702 smb_SetSMBParm(outp, 1, 0); /* share level auth with clear password */
2704 smb_SetSMBParm(outp, 2, SMB_PACKETSIZE);
2705 smb_SetSMBParm(outp, 3, smb_maxMpxRequests); /* max multiplexed requests */
2706 smb_SetSMBParm(outp, 4, smb_maxVCPerServer); /* max VCs per consumer/server connection */
2707 smb_SetSMBParm(outp, 5, 0); /* no support of block mode for read or write */
2708 smb_SetSMBParm(outp, 6, 1); /* next 2: session key */
2709 smb_SetSMBParm(outp, 7, 1);
2711 smb_SearchTimeFromUnixTime(&dosTime, unixTime);
2712 smb_SetSMBParm(outp, 8, LOWORD(dosTime)); /* server time */
2713 smb_SetSMBParm(outp, 9, HIWORD(dosTime)); /* server date */
2715 GetTimeZoneInformation(&tzi);
2716 smb_SetSMBParm(outp, 10, (unsigned short) tzi.Bias); /* server tzone */
2718 /* NOTE: Extended authentication cannot be negotiated with v3
2719 * therefore we fail over to NTLM
2721 if (smb_authType == SMB_AUTH_NTLM || smb_authType == SMB_AUTH_EXTENDED) {
2722 smb_SetSMBParm(outp, 11, MSV1_0_CHALLENGE_LENGTH); /* encryption key length */
2723 smb_SetSMBParm(outp, 12, 0); /* resvd */
2724 smb_SetSMBDataLength(outp, MSV1_0_CHALLENGE_LENGTH + smb_ServerDomainNameLength); /* perhaps should specify 8 bytes anyway */
2725 datap = smb_GetSMBData(outp, NULL);
2726 /* paste in a new encryption key */
2727 memcpy(datap, vcp->encKey, MSV1_0_CHALLENGE_LENGTH);
2728 /* and the faux domain name */
2729 strcpy(datap + MSV1_0_CHALLENGE_LENGTH, smb_ServerDomainName);
2731 smb_SetSMBParm(outp, 11, 0); /* encryption key length */
2732 smb_SetSMBParm(outp, 12, 0); /* resvd */
2733 smb_SetSMBDataLength(outp, 0);
2736 else if (coreProtoIndex != -1) { /* not really supported anymore */
2737 smb_SetSMBParm(outp, 0, protoIndex);
2738 smb_SetSMBDataLength(outp, 0);
2743 void smb_Daemon(void *parmp)
2750 if ((count % 360) == 0) { /* every hour */
2753 /* Initialize smb_localZero */
2754 myTime.tm_isdst = -1; /* compute whether on DST or not */
2755 myTime.tm_year = 70;
2761 smb_localZero = mktime(&myTime);
2763 smb_CalculateNowTZ();
2765 /* XXX GC dir search entries */
2769 void smb_WaitingLocksDaemon()
2771 smb_waitingLock_t *wL, *nwL;
2774 smb_packet_t *inp, *outp;
2779 lock_ObtainWrite(&smb_globalLock);
2780 nwL = smb_allWaitingLocks;
2782 osi_SleepW((long)&smb_allWaitingLocks, &smb_globalLock);
2791 lock_ObtainWrite(&smb_globalLock);
2793 nwL = (smb_waitingLock_t *) osi_QNext(&wL->q);
2794 lock_ReleaseWrite(&smb_globalLock);
2795 code = cm_RetryLock((cm_file_lock_t *) wL->lockp,
2796 wL->vcp->flags & SMB_VCFLAG_ALREADYDEAD);
2797 if (code == CM_ERROR_WOULDBLOCK) {
2799 if (wL->timeRemaining != 0xffffffff
2800 && (wL->timeRemaining -= 1000) < 0)
2809 ncbp->ncb_length = inp->ncb_length;
2810 inp->spacep = cm_GetSpace();
2812 /* Remove waitingLock from list */
2813 lock_ObtainWrite(&smb_globalLock);
2814 osi_QRemove((osi_queue_t **)&smb_allWaitingLocks,
2816 lock_ReleaseWrite(&smb_globalLock);
2818 /* Resume packet processing */
2820 smb_SetSMBDataLength(outp, 0);
2821 outp->flags |= SMB_PACKETFLAG_SUSPENDED;
2822 outp->resumeCode = code;
2824 smb_DispatchPacket(vcp, inp, outp, ncbp, NULL);
2827 cm_FreeSpace(inp->spacep);
2828 smb_FreePacket(inp);
2829 smb_FreePacket(outp);
2837 long smb_ReceiveCoreGetDiskAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2839 osi_Log0(smb_logp, "SMB receive get disk attributes");
2841 smb_SetSMBParm(outp, 0, 32000);
2842 smb_SetSMBParm(outp, 1, 64);
2843 smb_SetSMBParm(outp, 2, 1024);
2844 smb_SetSMBParm(outp, 3, 30000);
2845 smb_SetSMBParm(outp, 4, 0);
2846 smb_SetSMBDataLength(outp, 0);
2850 long smb_ReceiveCoreTreeConnect(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *rsp)
2854 unsigned short newTid;
2855 char shareName[256];
2863 osi_Log0(smb_logp, "SMB receive tree connect");
2865 /* parse input parameters */
2866 tp = smb_GetSMBData(inp, NULL);
2867 pathp = smb_ParseASCIIBlock(tp, &tp);
2868 passwordp = smb_ParseASCIIBlock(tp, &tp);
2869 tp = strrchr(pathp, '\\');
2871 return CM_ERROR_BADSMB;
2872 strcpy(shareName, tp+1);
2874 userp = smb_GetUser(vcp, inp);
2876 lock_ObtainMutex(&vcp->mx);
2877 newTid = vcp->tidCounter++;
2878 lock_ReleaseMutex(&vcp->mx);
2880 tidp = smb_FindTID(vcp, newTid, SMB_FLAG_CREATE);
2881 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
2882 shareFound = smb_FindShare(vcp, uidp, shareName, &sharePath);
2884 smb_ReleaseUID(uidp);
2886 smb_ReleaseTID(tidp);
2887 return CM_ERROR_BADSHARENAME;
2889 lock_ObtainMutex(&tidp->mx);
2890 tidp->userp = userp;
2891 tidp->pathname = sharePath;
2892 lock_ReleaseMutex(&tidp->mx);
2893 smb_ReleaseTID(tidp);
2895 smb_SetSMBParm(rsp, 0, SMB_PACKETSIZE);
2896 smb_SetSMBParm(rsp, 1, newTid);
2897 smb_SetSMBDataLength(rsp, 0);
2899 osi_Log1(smb_logp, "SMB tree connect created ID %d", newTid);
2903 unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp)
2907 if (*inp++ != 0x1) return NULL;
2908 tlen = inp[0] + (inp[1]<<8);
2909 inp += 2; /* skip length field */
2912 *chainpp = inp + tlen;
2915 if (lengthp) *lengthp = tlen;
2920 /* set maskp to the mask part of the incoming path.
2921 * Mask is 11 bytes long (8.3 with the dot elided).
2922 * Returns true if succeeds with a valid name, otherwise it does
2923 * its best, but returns false.
2925 int smb_Get8Dot3MaskFromPath(unsigned char *maskp, unsigned char *pathp)
2933 /* starts off valid */
2936 /* mask starts out all blanks */
2937 memset(maskp, ' ', 11);
2939 /* find last backslash, or use whole thing if there is none */
2940 tp = strrchr(pathp, '\\');
2941 if (!tp) tp = pathp;
2942 else tp++; /* skip slash */
2946 /* names starting with a dot are illegal */
2947 if (*tp == '.') valid8Dot3 = 0;
2951 if (tc == 0) return valid8Dot3;
2952 if (tc == '.' || tc == '"') break;
2953 if (i < 8) *up++ = tc;
2954 else valid8Dot3 = 0;
2957 /* if we get here, tp point after the dot */
2958 up = maskp+8; /* ext goes here */
2965 if (tc == '.' || tc == '"')
2968 /* copy extension if not too long */
2978 int smb_Match8Dot3Mask(char *unixNamep, char *maskp)
2988 /* XXX redo this, calling smb_V3MatchMask with a converted mask */
2990 valid = smb_Get8Dot3MaskFromPath(umask, unixNamep);
2994 /* otherwise, we have a valid 8.3 name; see if we have a match,
2995 * treating '?' as a wildcard in maskp (but not in the file name).
2997 tp1 = umask; /* real name, in mask format */
2998 tp2 = maskp; /* mask, in mask format */
2999 for(i=0; i<11; i++) {
3000 tc1 = *tp1++; /* char from real name */
3001 tc2 = *tp2++; /* char from mask */
3002 tc1 = (char) cm_foldUpper[(unsigned char)tc1];
3003 tc2 = (char) cm_foldUpper[(unsigned char)tc2];
3006 if (tc2 == '?' && tc1 != ' ')
3013 /* we got a match */
3017 char *smb_FindMask(char *pathp)
3021 tp = strrchr(pathp, '\\'); /* find last slash */
3024 return tp+1; /* skip the slash */
3026 return pathp; /* no slash, return the entire path */
3029 long smb_ReceiveCoreSearchVolume(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3031 unsigned char *pathp;
3033 unsigned char mask[11];
3034 unsigned char *statBlockp;
3035 unsigned char initStatBlock[21];
3038 osi_Log0(smb_logp, "SMB receive search volume");
3040 /* pull pathname and stat block out of request */
3041 tp = smb_GetSMBData(inp, NULL);
3042 pathp = smb_ParseASCIIBlock(tp, (char **) &tp);
3043 osi_assert(pathp != NULL);
3044 statBlockp = smb_ParseVblBlock(tp, (char **) &tp, &statLen);
3045 osi_assert(statBlockp != NULL);
3047 statBlockp = initStatBlock;
3051 /* for returning to caller */
3052 smb_Get8Dot3MaskFromPath(mask, pathp);
3054 smb_SetSMBParm(outp, 0, 1); /* we're returning one entry */
3055 tp = smb_GetSMBData(outp, NULL);
3057 *tp++ = 43; /* bytes in a dir entry */
3058 *tp++ = 0; /* high byte in counter */
3060 /* now marshall the dir entry, starting with the search status */
3061 *tp++ = statBlockp[0]; /* Reserved */
3062 memcpy(tp, mask, 11); tp += 11; /* FileName */
3064 /* now pass back server use info, with 1st byte non-zero */
3066 memset(tp, 0, 4); tp += 4; /* reserved for server use */
3068 memcpy(tp, statBlockp+17, 4); tp += 4; /* reserved for consumer */
3070 *tp++ = 0x8; /* attribute: volume */
3080 /* 4 byte file size */
3086 /* finally, null-terminated 8.3 pathname, which we set to AFS */
3087 memset(tp, ' ', 13);
3090 /* set the length of the data part of the packet to 43 + 3, for the dir
3091 * entry plus the 5 and the length fields.
3093 smb_SetSMBDataLength(outp, 46);
3097 long smb_ApplyDirListPatches(smb_dirListPatch_t **dirPatchespp,
3098 cm_user_t *userp, cm_req_t *reqp)
3106 smb_dirListPatch_t *patchp;
3107 smb_dirListPatch_t *npatchp;
3109 for(patchp = *dirPatchespp; patchp; patchp =
3110 (smb_dirListPatch_t *) osi_QNext(&patchp->q)) {
3111 code = cm_GetSCache(&patchp->fid, &scp, userp, reqp);
3113 lock_ObtainMutex(&scp->mx);
3114 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
3115 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3117 lock_ReleaseMutex(&scp->mx);
3118 cm_ReleaseSCache(scp);
3121 dptr = patchp->dptr;
3123 attr = smb_Attributes(scp);
3124 /* check hidden attribute (the flag is only ON when dot file hiding is on ) */
3125 if( patchp->flags & SMB_DIRLISTPATCH_DOTFILE )
3126 attr |= SMB_ATTR_HIDDEN;
3130 smb_SearchTimeFromUnixTime(&dosTime, scp->clientModTime);
3133 shortTemp = dosTime & 0xffff;
3134 *((u_short *)dptr) = shortTemp;
3137 /* and copy out date */
3138 shortTemp = (dosTime>>16) & 0xffff;
3139 *((u_short *)dptr) = shortTemp;
3142 /* copy out file length */
3143 *((u_long *)dptr) = scp->length.LowPart;
3145 lock_ReleaseMutex(&scp->mx);
3146 cm_ReleaseSCache(scp);
3149 /* now free the patches */
3150 for(patchp = *dirPatchespp; patchp; patchp = npatchp) {
3151 npatchp = (smb_dirListPatch_t *) osi_QNext(&patchp->q);
3155 /* and mark the list as empty */
3156 *dirPatchespp = NULL;
3161 long smb_ReceiveCoreSearchDir(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3170 smb_dirListPatch_t *dirListPatchesp;
3171 smb_dirListPatch_t *curPatchp;
3175 osi_hyper_t dirLength;
3176 osi_hyper_t bufferOffset;
3177 osi_hyper_t curOffset;
3179 unsigned char *inCookiep;
3180 smb_dirSearch_t *dsp;
3184 unsigned long clientCookie;
3185 cm_pageHeader_t *pageHeaderp;
3186 cm_user_t *userp = NULL;
3193 long nextEntryCookie;
3194 int numDirChunks; /* # of 32 byte dir chunks in this entry */
3195 char resByte; /* reserved byte from the cookie */
3196 char *op; /* output data ptr */
3197 char *origOp; /* original value of op */
3198 cm_space_t *spacep; /* for pathname buffer */
3209 maxCount = smb_GetSMBParm(inp, 0);
3211 dirListPatchesp = NULL;
3213 caseFold = CM_FLAG_CASEFOLD;
3215 tp = smb_GetSMBData(inp, NULL);
3216 pathp = smb_ParseASCIIBlock(tp, &tp);
3217 inCookiep = smb_ParseVblBlock(tp, &tp, &dataLength);
3219 /* bail out if request looks bad */
3220 if (!tp || !pathp) {
3221 return CM_ERROR_BADSMB;
3224 /* We can handle long names */
3225 if (vcp->flags & SMB_VCFLAG_USENT)
3226 ((smb_t *)outp)->flg2 |= 0x40; /* IS_LONG_NAME */
3228 /* make sure we got a whole search status */
3229 if (dataLength < 21) {
3230 nextCookie = 0; /* start at the beginning of the dir */
3233 attribute = smb_GetSMBParm(inp, 1);
3235 /* handle volume info in another function */
3236 if (attribute & 0x8)
3237 return smb_ReceiveCoreSearchVolume(vcp, inp, outp);
3239 osi_Log2(smb_logp, "SMB receive search dir count %d [%s]",
3240 maxCount, osi_LogSaveString(smb_logp, pathp));
3242 if (*pathp == 0) { /* null pathp, treat as root dir */
3243 if (!(attribute & SMB_ATTR_DIRECTORY)) /* exclude dirs */
3244 return CM_ERROR_NOFILES;
3248 dsp = smb_NewDirSearch(0);
3249 dsp->attribute = attribute;
3250 smb_Get8Dot3MaskFromPath(mask, pathp);
3251 memcpy(dsp->mask, mask, 11);
3253 /* track if this is likely to match a lot of entries */
3254 if (smb_IsStarMask(mask)) starPattern = 1;
3255 else starPattern = 0;
3258 /* pull the next cookie value out of the search status block */
3259 nextCookie = inCookiep[13] + (inCookiep[14]<<8) + (inCookiep[15]<<16)
3260 + (inCookiep[16]<<24);
3261 dsp = smb_FindDirSearch(inCookiep[12]);
3263 /* can't find dir search status; fatal error */
3264 return CM_ERROR_BADFD;
3266 attribute = dsp->attribute;
3267 resByte = inCookiep[0];
3269 /* copy out client cookie, in host byte order. Don't bother
3270 * interpreting it, since we're just passing it through, anyway.
3272 memcpy(&clientCookie, &inCookiep[17], 4);
3274 memcpy(mask, dsp->mask, 11);
3276 /* assume we're doing a star match if it has continued for more
3282 osi_Log3(smb_logp, "SMB dir search cookie 0x%x, connection %d, attr 0x%x",
3283 nextCookie, dsp->cookie, attribute);
3285 userp = smb_GetUser(vcp, inp);
3287 /* try to get the vnode for the path name next */
3288 lock_ObtainMutex(&dsp->mx);
3295 spacep = inp->spacep;
3296 smb_StripLastComponent(spacep->data, NULL, pathp);
3297 lock_ReleaseMutex(&dsp->mx);
3298 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3299 code = cm_NameI(cm_rootSCachep, spacep->data,
3300 caseFold | CM_FLAG_FOLLOW, userp, tidPathp, &req, &scp);
3301 lock_ObtainMutex(&dsp->mx);
3303 if (dsp->scp != 0) cm_ReleaseSCache(dsp->scp);
3305 /* we need one hold for the entry we just stored into,
3306 * and one for our own processing. When we're done with this
3307 * function, we'll drop the one for our own processing.
3308 * We held it once from the namei call, and so we do another hold
3312 lock_ObtainMutex(&scp->mx);
3313 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
3314 && LargeIntegerGreaterOrEqualToZero(scp->bulkStatProgress)) {
3315 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
3316 dsp->flags |= SMB_DIRSEARCH_BULKST;
3318 lock_ReleaseMutex(&scp->mx);
3321 lock_ReleaseMutex(&dsp->mx);
3323 cm_ReleaseUser(userp);
3324 smb_DeleteDirSearch(dsp);
3325 smb_ReleaseDirSearch(dsp);
3329 /* reserves space for parameter; we'll adjust it again later to the
3330 * real count of the # of entries we returned once we've actually
3331 * assembled the directory listing.
3333 smb_SetSMBParm(outp, 0, 0);
3335 /* get the directory size */
3336 lock_ObtainMutex(&scp->mx);
3337 code = cm_SyncOp(scp, NULL, userp, &req, 0,
3338 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3340 lock_ReleaseMutex(&scp->mx);
3341 cm_ReleaseSCache(scp);
3342 cm_ReleaseUser(userp);
3343 smb_DeleteDirSearch(dsp);
3344 smb_ReleaseDirSearch(dsp);
3348 dirLength = scp->length;
3350 bufferOffset.LowPart = bufferOffset.HighPart = 0;
3351 curOffset.HighPart = 0;
3352 curOffset.LowPart = nextCookie;
3353 origOp = op = smb_GetSMBData(outp, NULL);
3354 /* and write out the basic header */
3355 *op++ = 5; /* variable block */
3356 op += 2; /* skip vbl block length; we'll fill it in later */
3360 /* make sure that curOffset.LowPart doesn't point to the first
3361 * 32 bytes in the 2nd through last dir page, and that it doesn't
3362 * point at the first 13 32-byte chunks in the first dir page,
3363 * since those are dir and page headers, and don't contain useful
3366 temp = curOffset.LowPart & (2048-1);
3367 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
3368 /* we're in the first page */
3369 if (temp < 13*32) temp = 13*32;
3372 /* we're in a later dir page */
3373 if (temp < 32) temp = 32;
3376 /* make sure the low order 5 bits are zero */
3379 /* now put temp bits back ito curOffset.LowPart */
3380 curOffset.LowPart &= ~(2048-1);
3381 curOffset.LowPart |= temp;
3383 /* check if we've returned all the names that will fit in the
3386 if (returnedNames >= maxCount)
3389 /* check if we've passed the dir's EOF */
3390 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength)) break;
3392 /* see if we can use the bufferp we have now; compute in which page
3393 * the current offset would be, and check whether that's the offset
3394 * of the buffer we have. If not, get the buffer.
3396 thyper.HighPart = curOffset.HighPart;
3397 thyper.LowPart = curOffset.LowPart & ~(buf_bufferSize-1);
3398 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
3401 buf_Release(bufferp);
3404 lock_ReleaseMutex(&scp->mx);
3405 lock_ObtainRead(&scp->bufCreateLock);
3406 code = buf_Get(scp, &thyper, &bufferp);
3407 lock_ReleaseRead(&scp->bufCreateLock);
3409 /* now, if we're doing a star match, do bulk fetching of all of
3410 * the status info for files in the dir.
3413 smb_ApplyDirListPatches(&dirListPatchesp, userp,
3415 if ((dsp->flags & SMB_DIRSEARCH_BULKST)
3416 && LargeIntegerGreaterThanOrEqualTo(thyper,
3417 scp->bulkStatProgress)) {
3418 /* Don't bulk stat if risking timeout */
3419 int now = GetCurrentTime();
3420 if (now - req.startTime > 5000) {
3421 scp->bulkStatProgress = thyper;
3422 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
3423 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
3425 cm_TryBulkStat(scp, &thyper, userp, &req);
3429 lock_ObtainMutex(&scp->mx);
3432 bufferOffset = thyper;
3434 /* now get the data in the cache */
3436 code = cm_SyncOp(scp, bufferp, userp, &req,
3438 CM_SCACHESYNC_NEEDCALLBACK
3439 | CM_SCACHESYNC_READ);
3442 if (cm_HaveBuffer(scp, bufferp, 0)) break;
3444 /* otherwise, load the buffer and try again */
3445 code = cm_GetBuffer(scp, bufferp, NULL, userp,
3450 buf_Release(bufferp);
3454 } /* if (wrong buffer) ... */
3456 /* now we have the buffer containing the entry we're interested in; copy
3457 * it out if it represents a non-deleted entry.
3459 entryInDir = curOffset.LowPart & (2048-1);
3460 entryInBuffer = curOffset.LowPart & (buf_bufferSize - 1);
3462 /* page header will help tell us which entries are free. Page header
3463 * can change more often than once per buffer, since AFS 3 dir page size
3464 * may be less than (but not more than a buffer package buffer.
3466 temp = curOffset.LowPart & (buf_bufferSize - 1); /* only look intra-buffer */
3467 temp &= ~(2048 - 1); /* turn off intra-page bits */
3468 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
3470 /* now determine which entry we're looking at in the page. If it is
3471 * free (there's a free bitmap at the start of the dir), we should
3472 * skip these 32 bytes.
3474 slotInPage = (entryInDir & 0x7e0) >> 5;
3475 if (!(pageHeaderp->freeBitmap[slotInPage>>3] & (1 << (slotInPage & 0x7)))) {
3476 /* this entry is free */
3477 numDirChunks = 1; /* only skip this guy */
3481 tp = bufferp->datap + entryInBuffer;
3482 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
3484 /* while we're here, compute the next entry's location, too,
3485 * since we'll need it when writing out the cookie into the dir
3488 * XXXX Probably should do more sanity checking.
3490 numDirChunks = cm_NameEntries(dep->name, NULL);
3492 /* compute the offset of the cookie representing the next entry */
3493 nextEntryCookie = curOffset.LowPart + (CM_DIR_CHUNKSIZE * numDirChunks);
3495 /* Compute 8.3 name if necessary */
3496 actualName = dep->name;
3497 if (dep->fid.vnode != 0 && !cm_Is8Dot3(actualName)) {
3498 cm_Gen8Dot3Name(dep, shortName, &shortNameEnd);
3499 actualName = shortName;
3502 if (dep->fid.vnode != 0 && smb_Match8Dot3Mask(actualName, mask)) {
3503 /* this is one of the entries to use: it is not deleted
3504 * and it matches the star pattern we're looking for.
3507 /* Eliminate entries that don't match requested
3510 /* no hidden files */
3511 if(smb_hideDotFiles && !(dsp->attribute & SMB_ATTR_HIDDEN) && smb_IsDotFile(actualName))
3514 if (!(dsp->attribute & SMB_ATTR_DIRECTORY)) /* no directories */
3516 /* We have already done the cm_TryBulkStat above */
3517 fid.cell = scp->fid.cell;
3518 fid.volume = scp->fid.volume;
3519 fid.vnode = ntohl(dep->fid.vnode);
3520 fid.unique = ntohl(dep->fid.unique);
3521 fileType = cm_FindFileType(&fid);
3522 osi_Log2(smb_logp, "smb_ReceiveCoreSearchDir: file %s "
3523 "has filetype %d", dep->name,
3525 if (fileType == CM_SCACHETYPE_DIRECTORY)
3530 memcpy(op, mask, 11); op += 11;
3531 *op++ = (char) dsp->cookie; /* they say it must be non-zero */
3532 *op++ = nextEntryCookie & 0xff;
3533 *op++ = (nextEntryCookie>>8) & 0xff;
3534 *op++ = (nextEntryCookie>>16) & 0xff;
3535 *op++ = (nextEntryCookie>>24) & 0xff;
3536 memcpy(op, &clientCookie, 4); op += 4;
3538 /* now we emit the attribute. This is sort of tricky,
3539 * since we need to really stat the file to find out
3540 * what type of entry we've got. Right now, we're
3541 * copying out data from a buffer, while holding the
3542 * scp locked, so it isn't really convenient to stat
3543 * something now. We'll put in a place holder now,
3544 * and make a second pass before returning this to get
3545 * the real attributes. So, we just skip the data for
3546 * now, and adjust it later. We allocate a patch
3547 * record to make it easy to find this point later.
3548 * The replay will happen at a time when it is safe to
3549 * unlock the directory.
3551 curPatchp = malloc(sizeof(*curPatchp));
3552 osi_QAdd((osi_queue_t **) &dirListPatchesp, &curPatchp->q);
3553 curPatchp->dptr = op;
3554 curPatchp->fid.cell = scp->fid.cell;
3555 curPatchp->fid.volume = scp->fid.volume;
3556 curPatchp->fid.vnode = ntohl(dep->fid.vnode);
3557 curPatchp->fid.unique = ntohl(dep->fid.unique);
3559 /* do hidden attribute here since name won't be around when applying
3563 if ( smb_hideDotFiles && smb_IsDotFile(actualName) )
3564 curPatchp->flags = SMB_DIRLISTPATCH_DOTFILE;
3566 curPatchp->flags = 0;
3568 op += 9; /* skip attr, time, date and size */
3570 /* zero out name area. The spec says to pad with
3571 * spaces, but Samba doesn't, and neither do we.
3575 /* finally, we get to copy out the name; we know that
3576 * it fits in 8.3 or the pattern wouldn't match, but it
3577 * never hurts to be sure.
3579 strncpy(op, actualName, 13);
3581 /* Uppercase if requested by client */
3582 if ((((smb_t *)inp)->flg2 & 1) == 0)
3587 /* now, adjust the # of entries copied */
3589 } /* if we're including this name */
3592 /* and adjust curOffset to be where the new cookie is */
3593 thyper.HighPart = 0;
3594 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
3595 curOffset = LargeIntegerAdd(thyper, curOffset);
3596 } /* while copying data for dir listing */
3598 /* release the mutex */
3599 lock_ReleaseMutex(&scp->mx);
3600 if (bufferp) buf_Release(bufferp);
3602 /* apply and free last set of patches; if not doing a star match, this
3603 * will be empty, but better safe (and freeing everything) than sorry.
3605 smb_ApplyDirListPatches(&dirListPatchesp, userp, &req);
3607 /* special return code for unsuccessful search */
3608 if (code == 0 && dataLength < 21 && returnedNames == 0)
3609 code = CM_ERROR_NOFILES;
3611 osi_Log2(smb_logp, "SMB search dir done, %d names, code %d",
3612 returnedNames, code);
3615 smb_DeleteDirSearch(dsp);
3616 smb_ReleaseDirSearch(dsp);
3617 cm_ReleaseSCache(scp);
3618 cm_ReleaseUser(userp);
3622 /* finalize the output buffer */
3623 smb_SetSMBParm(outp, 0, returnedNames);
3624 temp = (long) (op - origOp);
3625 smb_SetSMBDataLength(outp, temp);
3627 /* the data area is a variable block, which has a 5 (already there)
3628 * followed by the length of the # of data bytes. We now know this to
3629 * be "temp," although that includes the 3 bytes of vbl block header.
3630 * Deduct for them and fill in the length field.
3632 temp -= 3; /* deduct vbl block info */
3633 osi_assert(temp == (43 * returnedNames));
3634 origOp[1] = temp & 0xff;
3635 origOp[2] = (temp>>8) & 0xff;
3636 if (returnedNames == 0) smb_DeleteDirSearch(dsp);
3637 smb_ReleaseDirSearch(dsp);
3638 cm_ReleaseSCache(scp);
3639 cm_ReleaseUser(userp);
3643 /* verify that this is a valid path to a directory. I don't know why they
3644 * don't use the get file attributes call.
3646 long smb_ReceiveCoreCheckPath(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3650 cm_scache_t *rootScp;
3651 cm_scache_t *newScp;
3660 pathp = smb_GetSMBData(inp, NULL);
3661 pathp = smb_ParseASCIIBlock(pathp, NULL);
3662 osi_Log1(smb_logp, "SMB receive check path %s",
3663 osi_LogSaveString(smb_logp, pathp));
3666 return CM_ERROR_BADFD;
3669 rootScp = cm_rootSCachep;
3671 userp = smb_GetUser(vcp, inp);
3673 caseFold = CM_FLAG_CASEFOLD;
3675 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3676 code = cm_NameI(rootScp, pathp,
3677 caseFold | CM_FLAG_FOLLOW | CM_FLAG_CHECKPATH,
3678 userp, tidPathp, &req, &newScp);
3681 cm_ReleaseUser(userp);
3685 /* now lock the vnode with a callback; returns with newScp locked */
3686 lock_ObtainMutex(&newScp->mx);
3687 code = cm_SyncOp(newScp, NULL, userp, &req, PRSFS_LOOKUP,
3688 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
3689 if (code && code != CM_ERROR_NOACCESS) {
3690 lock_ReleaseMutex(&newScp->mx);
3691 cm_ReleaseSCache(newScp);
3692 cm_ReleaseUser(userp);
3696 attrs = smb_Attributes(newScp);
3698 if (!(attrs & 0x10))
3699 code = CM_ERROR_NOTDIR;
3701 lock_ReleaseMutex(&newScp->mx);
3703 cm_ReleaseSCache(newScp);
3704 cm_ReleaseUser(userp);
3708 long smb_ReceiveCoreSetFileAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3712 cm_scache_t *rootScp;
3713 unsigned short attribute;
3715 cm_scache_t *newScp;
3724 /* decode basic attributes we're passed */
3725 attribute = smb_GetSMBParm(inp, 0);
3726 dosTime = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
3728 pathp = smb_GetSMBData(inp, NULL);
3729 pathp = smb_ParseASCIIBlock(pathp, NULL);
3732 return CM_ERROR_BADSMB;
3735 osi_Log2(smb_logp, "SMB receive setfile attributes time %d, attr 0x%x",
3736 dosTime, attribute);
3738 rootScp = cm_rootSCachep;
3740 userp = smb_GetUser(vcp, inp);
3742 caseFold = CM_FLAG_CASEFOLD;
3744 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3745 code = cm_NameI(rootScp, pathp, caseFold | CM_FLAG_FOLLOW, userp,
3746 tidPathp, &req, &newScp);
3749 cm_ReleaseUser(userp);
3753 /* now lock the vnode with a callback; returns with newScp locked; we
3754 * need the current status to determine what the new status is, in some
3757 lock_ObtainMutex(&newScp->mx);
3758 code = cm_SyncOp(newScp, NULL, userp, &req, 0,
3759 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
3761 lock_ReleaseMutex(&newScp->mx);
3762 cm_ReleaseSCache(newScp);
3763 cm_ReleaseUser(userp);
3767 /* Check for RO volume */
3768 if (newScp->flags & CM_SCACHEFLAG_RO) {
3769 lock_ReleaseMutex(&newScp->mx);
3770 cm_ReleaseSCache(newScp);
3771 cm_ReleaseUser(userp);
3772 return CM_ERROR_READONLY;
3775 /* prepare for setattr call */
3778 attr.mask |= CM_ATTRMASK_CLIENTMODTIME;
3779 smb_UnixTimeFromDosUTime(&attr.clientModTime, dosTime);
3781 if ((newScp->unixModeBits & 0222) && (attribute & 1) != 0) {
3782 /* we're told to make a writable file read-only */
3783 attr.unixModeBits = newScp->unixModeBits & ~0222;
3784 attr.mask |= CM_ATTRMASK_UNIXMODEBITS;
3786 else if ((newScp->unixModeBits & 0222) == 0 && (attribute & 1) == 0) {
3787 /* we're told to make a read-only file writable */
3788 attr.unixModeBits = newScp->unixModeBits | 0222;
3789 attr.mask |= CM_ATTRMASK_UNIXMODEBITS;
3791 lock_ReleaseMutex(&newScp->mx);
3793 /* now call setattr */
3795 code = cm_SetAttr(newScp, &attr, userp, &req);
3799 cm_ReleaseSCache(newScp);
3800 cm_ReleaseUser(userp);
3805 long smb_ReceiveCoreGetFileAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3809 cm_scache_t *rootScp;
3810 cm_scache_t *newScp, *dscp;
3822 pathp = smb_GetSMBData(inp, NULL);
3823 pathp = smb_ParseASCIIBlock(pathp, NULL);
3826 return CM_ERROR_BADSMB;
3829 if (*pathp == 0) /* null path */
3832 osi_Log1(smb_logp, "SMB receive getfile attributes path %s",
3833 osi_LogSaveString(smb_logp, pathp));
3835 rootScp = cm_rootSCachep;
3837 userp = smb_GetUser(vcp, inp);
3839 /* we shouldn't need this for V3 requests, but we seem to */
3840 caseFold = CM_FLAG_CASEFOLD;
3842 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3845 * XXX Strange hack XXX
3847 * As of Patch 5 (16 July 97), we are having the following problem:
3848 * In NT Explorer 4.0, whenever we click on a directory, AFS gets
3849 * requests to look up "desktop.ini" in all the subdirectories.
3850 * This can cause zillions of timeouts looking up non-existent cells
3851 * and volumes, especially in the top-level directory.
3853 * We have not found any way to avoid this or work around it except
3854 * to explicitly ignore the requests for mount points that haven't
3855 * yet been evaluated and for directories that haven't yet been
3858 * We should modify this hack to provide a fake desktop.ini file
3859 * http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/custom.asp
3861 spacep = inp->spacep;
3862 smb_StripLastComponent(spacep->data, &lastComp, pathp);
3863 if (lastComp && stricmp(lastComp, "\\desktop.ini") == 0) {
3864 code = cm_NameI(rootScp, spacep->data,
3865 caseFold | CM_FLAG_DIRSEARCH | CM_FLAG_FOLLOW,
3866 userp, tidPathp, &req, &dscp);
3868 if (dscp->fileType == CM_SCACHETYPE_MOUNTPOINT
3869 && !dscp->mountRootFidp)
3870 code = CM_ERROR_NOSUCHFILE;
3871 else if (dscp->fileType == CM_SCACHETYPE_DIRECTORY) {
3872 cm_buf_t *bp = buf_Find(dscp, &hzero);
3876 code = CM_ERROR_NOSUCHFILE;
3878 cm_ReleaseSCache(dscp);
3880 cm_ReleaseUser(userp);
3886 code = cm_NameI(rootScp, pathp, caseFold | CM_FLAG_FOLLOW, userp,
3887 tidPathp, &req, &newScp);
3890 cm_ReleaseUser(userp);
3894 /* now lock the vnode with a callback; returns with newScp locked */
3895 lock_ObtainMutex(&newScp->mx);
3896 code = cm_SyncOp(newScp, NULL, userp, &req, 0,
3897 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
3899 lock_ReleaseMutex(&newScp->mx);
3900 cm_ReleaseSCache(newScp);
3901 cm_ReleaseUser(userp);
3906 /* use smb_Attributes instead. Also the fact that a file is
3907 * in a readonly volume doesn't mean it shojuld be marked as RO
3909 if (newScp->fileType == CM_SCACHETYPE_DIRECTORY
3910 || newScp->fileType == CM_SCACHETYPE_MOUNTPOINT)
3911 attrs = SMB_ATTR_DIRECTORY;
3914 if ((newScp->unixModeBits & 0222) == 0 || (newScp->flags & CM_SCACHEFLAG_RO))
3915 attrs |= SMB_ATTR_READONLY; /* turn on read-only flag */
3917 attrs = smb_Attributes(newScp);
3920 smb_SetSMBParm(outp, 0, attrs);
3922 smb_DosUTimeFromUnixTime(&dosTime, newScp->clientModTime);
3923 smb_SetSMBParm(outp, 1, dosTime & 0xffff);
3924 smb_SetSMBParm(outp, 2, (dosTime>>16) & 0xffff);
3925 smb_SetSMBParm(outp, 3, newScp->length.LowPart & 0xffff);
3926 smb_SetSMBParm(outp, 4, (newScp->length.LowPart >> 16) & 0xffff);
3927 smb_SetSMBParm(outp, 5, 0);
3928 smb_SetSMBParm(outp, 6, 0);
3929 smb_SetSMBParm(outp, 7, 0);
3930 smb_SetSMBParm(outp, 8, 0);
3931 smb_SetSMBParm(outp, 9, 0);
3932 smb_SetSMBDataLength(outp, 0);
3933 lock_ReleaseMutex(&newScp->mx);
3935 cm_ReleaseSCache(newScp);
3936 cm_ReleaseUser(userp);
3941 long smb_ReceiveCoreTreeDisconnect(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3945 osi_Log0(smb_logp, "SMB receive tree disconnect");
3947 /* find the tree and free it */
3948 tidp = smb_FindTID(vcp, ((smb_t *)inp)->tid, 0);
3950 lock_ObtainMutex(&tidp->mx);
3951 tidp->flags |= SMB_TIDFLAG_DELETE;
3952 lock_ReleaseMutex(&tidp->mx);
3953 smb_ReleaseTID(tidp);
3959 long smb_ReceiveCoreOpen(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3977 pathp = smb_GetSMBData(inp, NULL);
3978 pathp = smb_ParseASCIIBlock(pathp, NULL);
3980 osi_Log1(smb_logp, "SMB receive open file [%s]", osi_LogSaveString(smb_logp, pathp));
3982 #ifdef DEBUG_VERBOSE
3986 hexpath = osi_HexifyString( pathp );
3987 DEBUG_EVENT2("AFS", "CoreOpen H[%s] A[%s]", hexpath, pathp);
3992 share = smb_GetSMBParm(inp, 0);
3993 attribute = smb_GetSMBParm(inp, 1);
3995 spacep = inp->spacep;
3996 smb_StripLastComponent(spacep->data, &lastNamep, pathp);
3997 if (lastNamep && strcmp(lastNamep, SMB_IOCTL_FILENAME) == 0) {
3998 /* special case magic file name for receiving IOCTL requests
3999 * (since IOCTL calls themselves aren't getting through).
4001 fidp = smb_FindFID(vcp, 0, SMB_FLAG_CREATE);
4002 smb_SetupIoctlFid(fidp, spacep);
4003 smb_SetSMBParm(outp, 0, fidp->fid);
4004 smb_SetSMBParm(outp, 1, 0); /* attrs */
4005 smb_SetSMBParm(outp, 2, 0); /* next 2 are DOS time */
4006 smb_SetSMBParm(outp, 3, 0);
4007 smb_SetSMBParm(outp, 4, 0); /* next 2 are length */
4008 smb_SetSMBParm(outp, 5, 0x7fff);
4009 /* pass the open mode back */
4010 smb_SetSMBParm(outp, 6, (share & 0xf));
4011 smb_SetSMBDataLength(outp, 0);
4012 smb_ReleaseFID(fidp);
4016 userp = smb_GetUser(vcp, inp);
4018 caseFold = CM_FLAG_CASEFOLD;
4020 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
4021 code = cm_NameI(cm_rootSCachep, pathp, caseFold | CM_FLAG_FOLLOW, userp,
4022 tidPathp, &req, &scp);
4025 cm_ReleaseUser(userp);
4029 code = cm_CheckOpen(scp, share & 0x7, 0, userp, &req);
4031 cm_ReleaseSCache(scp);
4032 cm_ReleaseUser(userp);
4036 /* don't need callback to check file type, since file types never
4037 * change, and namei and cm_Lookup all stat the object at least once on
4038 * a successful return.
4040 if (scp->fileType != CM_SCACHETYPE_FILE) {
4041 cm_ReleaseSCache(scp);
4042 cm_ReleaseUser(userp);
4043 return CM_ERROR_ISDIR;
4046 fidp = smb_FindFID(vcp, 0, SMB_FLAG_CREATE);
4049 /* save a pointer to the vnode */
4052 if ((share & 0xf) == 0)
4053 fidp->flags |= SMB_FID_OPENREAD;
4054 else if ((share & 0xf) == 1)
4055 fidp->flags |= SMB_FID_OPENWRITE;
4057 fidp->flags |= (SMB_FID_OPENREAD | SMB_FID_OPENWRITE);
4059 lock_ObtainMutex(&scp->mx);
4060 smb_SetSMBParm(outp, 0, fidp->fid);
4061 smb_SetSMBParm(outp, 1, smb_Attributes(scp));
4062 smb_DosUTimeFromUnixTime(&dosTime, scp->clientModTime);
4063 smb_SetSMBParm(outp, 2, dosTime & 0xffff);
4064 smb_SetSMBParm(outp, 3, (dosTime >> 16) & 0xffff);
4065 smb_SetSMBParm(outp, 4, scp->length.LowPart & 0xffff);
4066 smb_SetSMBParm(outp, 5, (scp->length.LowPart >> 16) & 0xffff);
4067 /* pass the open mode back; XXXX add access checks */
4068 smb_SetSMBParm(outp, 6, (share & 0xf));
4069 smb_SetSMBDataLength(outp, 0);
4070 lock_ReleaseMutex(&scp->mx);
4073 cm_Open(scp, 0, userp);
4075 /* send and free packet */
4076 smb_ReleaseFID(fidp);
4077 cm_ReleaseUser(userp);
4078 /* don't release scp, since we've squirreled away the pointer in the fid struct */
4082 typedef struct smb_unlinkRock {
4087 char *maskp; /* pointer to the star pattern */
4092 int smb_UnlinkProc(cm_scache_t *dscp, cm_dirEntry_t *dep, void *vrockp, osi_hyper_t *offp)
4095 smb_unlinkRock_t *rockp;
4103 caseFold = ((rockp->flags & SMB_MASKFLAG_CASEFOLD)? CM_FLAG_CASEFOLD : 0);
4104 if (!(rockp->vcp->flags & SMB_VCFLAG_USEV3))
4105 caseFold |= CM_FLAG_8DOT3;
4107 matchName = dep->name;
4108 match = smb_V3MatchMask(matchName, rockp->maskp, caseFold);
4110 && (rockp->flags & SMB_MASKFLAG_TILDE)
4111 && !cm_Is8Dot3(dep->name)) {
4112 cm_Gen8Dot3Name(dep, shortName, NULL);
4113 matchName = shortName;
4114 /* 8.3 matches are always case insensitive */
4115 match = smb_V3MatchMask(matchName, rockp->maskp, caseFold | CM_FLAG_CASEFOLD);
4118 osi_Log1(smb_logp, "Unlinking %s",
4119 osi_LogSaveString(smb_logp, matchName));
4120 code = cm_Unlink(dscp, dep->name, rockp->userp, rockp->reqp);
4121 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
4122 smb_NotifyChange(FILE_ACTION_REMOVED,
4123 FILE_NOTIFY_CHANGE_FILE_NAME,
4124 dscp, dep->name, NULL, TRUE);
4127 /* If we made a case sensitive exact match, we might as well quit now. */
4128 if(!(rockp->flags & SMB_MASKFLAG_CASEFOLD) && !strcmp(matchName, rockp->maskp))
4129 code = CM_ERROR_STOPNOW;
4137 long smb_ReceiveCoreUnlink(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
4146 smb_unlinkRock_t rock;
4155 attribute = smb_GetSMBParm(inp, 0);
4157 tp = smb_GetSMBData(inp, NULL);
4158 pathp = smb_ParseASCIIBlock(tp, &tp);
4160 osi_Log1(smb_logp, "SMB receive unlink %s",
4161 osi_LogSaveString(smb_logp, pathp));
4163 spacep = inp->spacep;
4164 smb_StripLastComponent(spacep->data, &lastNamep, pathp);
4166 userp = smb_GetUser(vcp, inp);
4168 caseFold = CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD;
4170 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
4171 code = cm_NameI(cm_rootSCachep, spacep->data, caseFold, userp, tidPathp,
4175 cm_ReleaseUser(userp);
4179 /* otherwise, scp points to the parent directory. */
4186 rock.maskp = smb_FindMask(pathp);
4187 rock.flags = ((strchr(rock.maskp, '~') != NULL) ? SMB_MASKFLAG_TILDE : 0);
4190 thyper.HighPart = 0;
4196 /* Now, if we aren't dealing with a wildcard match, we first try an exact
4197 * match. If that fails, we do a case insensitve match.
4199 if (!(rock.flags & SMB_MASKFLAG_TILDE) &&
4200 !smb_IsStarMask(rock.maskp)) {
4201 code = cm_ApplyDir(dscp, smb_UnlinkProc, &rock, &thyper, userp, &req, NULL);
4204 thyper.HighPart = 0;
4205 rock.flags |= SMB_MASKFLAG_CASEFOLD;
4210 code = cm_ApplyDir(dscp, smb_UnlinkProc, &rock, &thyper, userp, &req, NULL);
4212 if (code == CM_ERROR_STOPNOW)
4215 cm_ReleaseUser(userp);
4217 cm_ReleaseSCache(dscp);
4219 if (code == 0 && !rock.any)
4220 code = CM_ERROR_NOSUCHFILE;
4224 typedef struct smb_renameRock {
4225 cm_scache_t *odscp; /* old dir */
4226 cm_scache_t *ndscp; /* new dir */
4227 cm_user_t *userp; /* user */
4228 cm_req_t *reqp; /* request struct */
4229 smb_vc_t *vcp; /* virtual circuit */
4230 char *maskp; /* pointer to star pattern of old file name */
4231 int flags; /* tilde, casefold, etc */
4232 char *newNamep; /* ptr to the new file's name */
4235 int smb_RenameProc(cm_scache_t *dscp, cm_dirEntry_t *dep, void *vrockp, osi_hyper_t *offp)
4238 smb_renameRock_t *rockp;
4243 rockp = (smb_renameRock_t *) vrockp;
4245 caseFold = ((rockp->flags & SMB_MASKFLAG_CASEFOLD)? CM_FLAG_CASEFOLD : 0);
4246 if (!(rockp->vcp->flags & SMB_VCFLAG_USEV3))
4247 caseFold |= CM_FLAG_8DOT3;
4249 match = smb_V3MatchMask(dep->name, rockp->maskp, caseFold);
4251 && (rockp->flags & SMB_MASKFLAG_TILDE)
4252 && !cm_Is8Dot3(dep->name)) {
4253 cm_Gen8Dot3Name(dep, shortName, NULL);
4254 match = smb_V3MatchMask(shortName, rockp->maskp, caseFold);
4257 code = cm_Rename(rockp->odscp, dep->name,
4258 rockp->ndscp, rockp->newNamep, rockp->userp,
4260 /* if the call worked, stop doing the search now, since we
4261 * really only want to rename one file.
4264 code = CM_ERROR_STOPNOW;
4271 long smb_ReceiveCoreRename(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
4277 cm_space_t *spacep = NULL;
4278 smb_renameRock_t rock;
4279 cm_scache_t *oldDscp = NULL;
4280 cm_scache_t *newDscp = NULL;
4281 cm_scache_t *tmpscp= NULL;
4282 cm_scache_t *tmpscp2 = NULL;
4294 tp = smb_GetSMBData(inp, NULL);
4295 oldPathp = smb_ParseASCIIBlock(tp, &tp);
4296 newPathp = smb_ParseASCIIBlock(tp, &tp);
4298 osi_Log2(smb_logp, "smb rename [%s] to [%s]",
4299 osi_LogSaveString(smb_logp, oldPathp),
4300 osi_LogSaveString(smb_logp, newPathp));
4302 spacep = inp->spacep;
4303 smb_StripLastComponent(spacep->data, &oldLastNamep, oldPathp);
4305 userp = smb_GetUser(vcp, inp);
4308 * Changed to use CASEFOLD always. This enables us to rename Foo/baz when
4309 * what actually exists is foo/baz. I don't know why the code used to be
4310 * the way it was. 1/29/96
4312 * caseFold = ((vcp->flags & SMB_VCFLAG_USEV3) ? 0: CM_FLAG_CASEFOLD);
4314 * Changed to use CM_FLAG_FOLLOW. 7/24/96
4316 * caseFold = CM_FLAG_CASEFOLD;
4318 caseFold = CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD;
4320 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
4321 code = cm_NameI(cm_rootSCachep, spacep->data, caseFold,
4322 userp, tidPathp, &req, &oldDscp);
4325 cm_ReleaseUser(userp);
4329 smb_StripLastComponent(spacep->data, &newLastNamep, newPathp);
4330 code = cm_NameI(cm_rootSCachep, spacep->data, caseFold,
4331 userp, tidPathp, &req, &newDscp);
4334 cm_ReleaseSCache(oldDscp);
4335 cm_ReleaseUser(userp);
4339 /* otherwise, oldDscp and newDscp point to the corresponding directories.
4340 * next, get the component names, and lower case them.
4343 /* handle the old name first */
4345 oldLastNamep = oldPathp;
4349 /* and handle the new name, too */
4351 newLastNamep = newPathp;
4355 /* TODO: The old name could be a wildcard. The new name must not be */
4357 /* do the vnode call */
4358 rock.odscp = oldDscp;
4359 rock.ndscp = newDscp;
4363 rock.maskp = oldLastNamep;
4364 rock.flags = ((strchr(oldLastNamep, '~') != NULL) ? SMB_MASKFLAG_TILDE : 0);
4365 rock.newNamep = newLastNamep;
4367 /* Check if the file already exists; if so return error */
4368 code = cm_Lookup(newDscp,newLastNamep,CM_FLAG_CHECKPATH,userp,&req,&tmpscp);
4369 if ((code != CM_ERROR_NOSUCHFILE) && (code != CM_ERROR_NOSUCHPATH) && (code != CM_ERROR_NOSUCHVOLUME) ) {
4370 osi_Log2(afsd_logp, " lookup returns %ld for [%s]", code,
4371 osi_LogSaveString(afsd_logp, newLastNamep));
4373 /* Check if the old and the new names differ only in case. If so return
4374 * success, else return CM_ERROR_EXISTS
4376 if (!code && oldDscp == newDscp && !stricmp(oldLastNamep, newLastNamep)) {
4378 /* This would be a success only if the old file is *as same as* the new file */
4379 code = cm_Lookup(oldDscp, oldLastNamep, CM_FLAG_CHECKPATH, userp, &req, &tmpscp2);
4381 if (tmpscp == tmpscp2)
4384 code = CM_ERROR_EXISTS;
4385 cm_ReleaseSCache(tmpscp2);
4388 code = CM_ERROR_NOSUCHFILE;
4391 /* file exist, do not rename, also fixes move */
4392 osi_Log0(afsd_logp, "Can't rename. Target already exists");
4393 code = CM_ERROR_EXISTS;
4397 cm_ReleaseSCache(tmpscp);
4398 cm_ReleaseSCache(newDscp);
4399 cm_ReleaseSCache(oldDscp);
4400 cm_ReleaseUser(userp);
4404 /* Now search the directory for the pattern, and do the appropriate rename when found */
4405 thyper.LowPart = 0; /* search dir from here */
4406 thyper.HighPart = 0;
4408 code = cm_ApplyDir(oldDscp, smb_RenameProc, &rock, &thyper, userp, &req, NULL);
4410 if (code == CM_ERROR_STOPNOW)
4413 code = CM_ERROR_NOSUCHFILE;
4415 /* Handle Change Notification */
4417 * Being lazy, not distinguishing between files and dirs in this
4418 * filter, since we'd have to do a lookup.
4420 filter = FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
4421 if (oldDscp == newDscp) {
4422 if (oldDscp->flags & CM_SCACHEFLAG_ANYWATCH)
4423 smb_NotifyChange(FILE_ACTION_RENAMED_OLD_NAME,
4424 filter, oldDscp, oldLastNamep,
4425 newLastNamep, TRUE);
4427 if (oldDscp->flags & CM_SCACHEFLAG_ANYWATCH)
4428 smb_NotifyChange(FILE_ACTION_RENAMED_OLD_NAME,
4429 filter, oldDscp, oldLastNamep,
4431 if (newDscp->flags & CM_SCACHEFLAG_ANYWATCH)
4432 smb_NotifyChange(FILE_ACTION_RENAMED_NEW_NAME,
4433 filter, newDscp, newLastNamep,
4438 cm_ReleaseSCache(tmpscp);
4439 cm_ReleaseUser(userp);
4440 cm_ReleaseSCache(oldDscp);
4441 cm_ReleaseSCache(newDscp);
4445 typedef struct smb_rmdirRock {
4449 char *maskp; /* pointer to the star pattern */
4454 int smb_RmdirProc(cm_scache_t *dscp, cm_dirEntry_t *dep, void *vrockp, osi_hyper_t *offp)
4457 smb_rmdirRock_t *rockp;
4462 rockp = (smb_rmdirRock_t *) vrockp;
4464 matchName = dep->name;
4465 if (rockp->flags & SMB_MASKFLAG_CASEFOLD)
4466 match = (cm_stricmp(matchName, rockp->maskp) == 0);
4468 match = (strcmp(matchName, rockp->maskp) == 0);
4470 && (rockp->flags & SMB_MASKFLAG_TILDE)
4471 && !cm_Is8Dot3(dep->name)) {
4472 cm_Gen8Dot3Name(dep, shortName, NULL);
4473 matchName = shortName;
4474 match = (cm_stricmp(matchName, rockp->maskp) == 0);
4477 osi_Log1(smb_logp, "Removing directory %s",
4478 osi_LogSaveString(smb_logp, matchName));
4479 code = cm_RemoveDir(dscp, dep->name, rockp->userp, rockp->reqp);
4480 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
4481 smb_NotifyChange(FILE_ACTION_REMOVED,
4482 FILE_NOTIFY_CHANGE_DIR_NAME,
4483 dscp, dep->name, NULL, TRUE);
4492 long smb_ReceiveCoreRemoveDir(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
4500 smb_rmdirRock_t rock;
4509 tp = smb_GetSMBData(inp, NULL);
4510 pathp = smb_ParseASCIIBlock(tp, &tp);
4512 spacep = inp->spacep;
4513 smb_StripLastComponent(spacep->data, &lastNamep, pathp);
4515 userp = smb_GetUser(vcp, inp);
4517 caseFold = CM_FLAG_CASEFOLD;
4519 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
4520 code = cm_NameI(cm_rootSCachep, spacep->data, caseFold | CM_FLAG_FOLLOW,
4521 userp, tidPathp, &req, &dscp);
4524 cm_ReleaseUser(userp);
4528 /* otherwise, scp points to the parent directory. */
4535 rock.maskp = lastNamep;
4536 rock.flags = ((strchr(rock.maskp, '~') != NULL) ? SMB_MASKFLAG_TILDE : 0);
4539 thyper.HighPart = 0;
4543 /* First do a case sensitive match, and if that fails, do a case insensitive match */
4544 code = cm_ApplyDir(dscp, smb_RmdirProc, &rock, &thyper, userp, &req, NULL);
4545 if (code == 0 && !rock.any) {
4547 thyper.HighPart = 0;
4548 rock.flags |= SMB_MASKFLAG_CASEFOLD;
4549 code = cm_ApplyDir(dscp, smb_RmdirProc, &rock, &thyper, userp, &req, NULL);
4552 cm_ReleaseUser(userp);
4554 cm_ReleaseSCache(dscp);
4556 if (code == 0 && !rock.any)
4557 code = CM_ERROR_NOSUCHFILE;
4561 long smb_ReceiveCoreFlush(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
4571 fid = smb_GetSMBParm(inp, 0);
4573 osi_Log1(smb_logp, "SMB flush fid %d", fid);
4575 fid = smb_ChainFID(fid, inp);
4576 fidp = smb_FindFID(vcp, fid, 0);
4577 if (!fidp || (fidp->flags & SMB_FID_IOCTL)) {
4579 smb_ReleaseFID(fidp);
4580 return CM_ERROR_BADFD;
4583 userp = smb_GetUser(vcp, inp);
4585 lock_ObtainMutex(&fidp->mx);
4586 if (fidp->flags & SMB_FID_OPENWRITE)
4587 code = cm_FSync(fidp->scp, userp, &req);
4590 lock_ReleaseMutex(&fidp->mx);
4592 smb_ReleaseFID(fidp);
4594 cm_ReleaseUser(userp);
4599 struct smb_FullNameRock {
4605 int smb_FullNameProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
4609 struct smb_FullNameRock *vrockp;
4611 vrockp = (struct smb_FullNameRock *)rockp;
4613 if (!cm_Is8Dot3(dep->name)) {
4614 cm_Gen8Dot3Name(dep, shortName, NULL);
4616 if (cm_stricmp(shortName, vrockp->name) == 0) {
4617 vrockp->fullName = strdup(dep->name);
4618 return CM_ERROR_STOPNOW;
4621 if (cm_stricmp(dep->name, vrockp->name) == 0
4622 && ntohl(dep->fid.vnode) == vrockp->vnode->fid.vnode
4623 && ntohl(dep->fid.unique) == vrockp->vnode->fid.unique) {
4624 vrockp->fullName = strdup(dep->name);
4625 return CM_ERROR_STOPNOW;
4630 void smb_FullName(cm_scache_t *dscp, cm_scache_t *scp, char *pathp,
4631 char **newPathp, cm_user_t *userp, cm_req_t *reqp)
4633 struct smb_FullNameRock rock;
4639 code = cm_ApplyDir(dscp, smb_FullNameProc, &rock, NULL,
4641 if (code == CM_ERROR_STOPNOW)
4642 *newPathp = rock.fullName;
4644 *newPathp = strdup(pathp);
4647 long smb_ReceiveCoreClose(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
4658 fid = smb_GetSMBParm(inp, 0);
4659 dosTime = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
4661 osi_Log1(smb_logp, "SMB close fid %d", fid);
4663 fid = smb_ChainFID(fid, inp);
4664 fidp = smb_FindFID(vcp, fid, 0);
4666 return CM_ERROR_BADFD;
4669 userp = smb_GetUser(vcp, inp);
4671 lock_ObtainMutex(&fidp->mx);
4673 /* Don't jump the gun on an async raw write */
4674 while (fidp->raw_writers) {
4675 lock_ReleaseMutex(&fidp->mx);
4676 thrd_WaitForSingleObject_Event(fidp->raw_write_event, RAWTIMEOUT);
4677 lock_ObtainMutex(&fidp->mx);
4680 fidp->flags |= SMB_FID_DELETE;
4682 /* watch for ioctl closes, and read-only opens */
4683 if (fidp->scp != NULL
4684 && (fidp->flags & (SMB_FID_OPENWRITE | SMB_FID_DELONCLOSE))
4685 == SMB_FID_OPENWRITE) {
4686 if (dosTime != 0 && dosTime != -1) {
4687 fidp->scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
4688 /* This fixes defect 10958 */
4689 CompensateForSmbClientLastWriteTimeBugs(&dosTime);
4690 smb_UnixTimeFromDosUTime(&fidp->scp->clientModTime, dosTime);
4692 code = cm_FSync(fidp->scp, userp, &req);
4697 if (fidp->flags & SMB_FID_DELONCLOSE) {
4698 cm_scache_t *dscp = fidp->NTopen_dscp;
4699 char *pathp = fidp->NTopen_pathp;
4702 smb_FullName(dscp, fidp->scp, pathp, &fullPathp, userp, &req);
4703 if (fidp->scp->fileType == CM_SCACHETYPE_DIRECTORY) {
4704 code = cm_RemoveDir(dscp, fullPathp, userp, &req);
4705 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
4706 smb_NotifyChange(FILE_ACTION_REMOVED,
4707 FILE_NOTIFY_CHANGE_DIR_NAME,
4708 dscp, fullPathp, NULL, TRUE);
4712 code = cm_Unlink(dscp, fullPathp, userp, &req);
4713 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
4714 smb_NotifyChange(FILE_ACTION_REMOVED,
4715 FILE_NOTIFY_CHANGE_FILE_NAME,
4716 dscp, fullPathp, NULL, TRUE);
4720 lock_ReleaseMutex(&fidp->mx);
4722 if (fidp->flags & SMB_FID_NTOPEN) {
4723 cm_ReleaseSCache(fidp->NTopen_dscp);
4724 free(fidp->NTopen_pathp);
4726 if (fidp->NTopen_wholepathp)
4727 free(fidp->NTopen_wholepathp);
4729 smb_ReleaseFID(fidp);
4730 cm_ReleaseUser(userp);
4735 * smb_ReadData -- common code for Read, Read And X, and Raw Read
4738 long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count, char *op,
4739 cm_user_t *userp, long *readp)
4741 long smb_ReadData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count, char *op,
4742 cm_user_t *userp, long *readp, int dosflag)
4749 osi_hyper_t fileLength;
4751 osi_hyper_t lastByte;
4752 osi_hyper_t bufferOffset;
4753 long bufIndex, nbytes;
4763 lock_ObtainMutex(&fidp->mx);
4765 lock_ObtainMutex(&scp->mx);
4767 if (offset.HighPart == 0) {
4768 chunk = offset.LowPart >> cm_logChunkSize;
4769 if (chunk != fidp->curr_chunk) {
4770 fidp->prev_chunk = fidp->curr_chunk;
4771 fidp->curr_chunk = chunk;
4773 if (fidp->curr_chunk == fidp->prev_chunk + 1)
4777 /* start by looking up the file's end */
4778 code = cm_SyncOp(scp, NULL, userp, &req, 0,
4779 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
4780 if (code) goto done;
4782 /* now we have the entry locked, look up the length */
4783 fileLength = scp->length;
4785 /* adjust count down so that it won't go past EOF */
4786 thyper.LowPart = count;
4787 thyper.HighPart = 0;
4788 thyper = LargeIntegerAdd(offset, thyper); /* where read should end */
4790 if (LargeIntegerGreaterThan(thyper, fileLength)) {
4791 /* we'd read past EOF, so just stop at fileLength bytes.
4792 * Start by computing how many bytes remain in the file.
4794 thyper = LargeIntegerSubtract(fileLength, offset);
4796 /* if we are past EOF, read 0 bytes */
4797 if (LargeIntegerLessThanZero(thyper))
4800 count = thyper.LowPart;
4805 /* now, copy the data one buffer at a time,
4806 * until we've filled the request packet
4809 /* if we've copied all the data requested, we're done */
4810 if (count <= 0) break;
4812 /* otherwise, load up a buffer of data */
4813 thyper.HighPart = offset.HighPart;
4814 thyper.LowPart = offset.LowPart & ~(buf_bufferSize-1);
4815 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
4818 buf_Release(bufferp);
4821 lock_ReleaseMutex(&scp->mx);
4823 lock_ObtainRead(&scp->bufCreateLock);
4824 code = buf_Get(scp, &thyper, &bufferp);
4825 lock_ReleaseRead(&scp->bufCreateLock);
4827 lock_ObtainMutex(&scp->mx);
4828 if (code) goto done;
4829 bufferOffset = thyper;
4831 /* now get the data in the cache */
4833 code = cm_SyncOp(scp, bufferp, userp, &req, 0,
4834 CM_SCACHESYNC_NEEDCALLBACK
4835 | CM_SCACHESYNC_READ);
4836 if (code) goto done;
4838 if (cm_HaveBuffer(scp, bufferp, 0)) break;
4840 /* otherwise, load the buffer and try again */
4841 code = cm_GetBuffer(scp, bufferp, NULL, userp, &req);
4845 buf_Release(bufferp);
4849 } /* if (wrong buffer) ... */
4851 /* now we have the right buffer loaded. Copy out the
4852 * data from here to the user's buffer.
4854 bufIndex = offset.LowPart & (buf_bufferSize - 1);
4856 /* and figure out how many bytes we want from this buffer */
4857 nbytes = buf_bufferSize - bufIndex; /* what remains in buffer */
4858 if (nbytes > count) nbytes = count; /* don't go past EOF */
4860 /* now copy the data */
4863 dosmemput(bufferp->datap + bufIndex, nbytes, (dos_ptr)op);
4866 memcpy(op, bufferp->datap + bufIndex, nbytes);
4868 /* adjust counters, pointers, etc. */
4871 thyper.LowPart = nbytes;
4872 thyper.HighPart = 0;
4873 offset = LargeIntegerAdd(thyper, offset);
4877 lock_ReleaseMutex(&scp->mx);
4878 lock_ReleaseMutex(&fidp->mx);
4880 buf_Release(bufferp);
4882 if (code == 0 && sequential)
4883 cm_ConsiderPrefetch(scp, &lastByte, userp, &req);
4889 * smb_WriteData -- common code for Write and Raw Write
4892 long smb_WriteData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count, char *op,
4893 cm_user_t *userp, long *writtenp)
4895 long smb_WriteData(smb_fid_t *fidp, osi_hyper_t *offsetp, long count, char *op,
4896 cm_user_t *userp, long *writtenp, int dosflag)
4903 osi_hyper_t fileLength; /* file's length at start of write */
4904 osi_hyper_t minLength; /* don't read past this */
4905 long nbytes; /* # of bytes to transfer this iteration */
4907 osi_hyper_t thyper; /* hyper tmp variable */
4908 osi_hyper_t bufferOffset;
4909 long bufIndex; /* index in buffer where our data is */
4911 osi_hyper_t writeBackOffset; /* offset of region to write back when
4922 lock_ObtainMutex(&fidp->mx);
4924 lock_ObtainMutex(&scp->mx);
4926 /* start by looking up the file's end */
4927 code = cm_SyncOp(scp, NULL, userp, &req, 0,
4928 CM_SCACHESYNC_NEEDCALLBACK
4929 | CM_SCACHESYNC_SETSTATUS
4930 | CM_SCACHESYNC_GETSTATUS);
4934 /* make sure we have a writable FD */
4935 if (!(fidp->flags & SMB_FID_OPENWRITE)) {
4936 code = CM_ERROR_BADFDOP;
4940 /* now we have the entry locked, look up the length */
4941 fileLength = scp->length;
4942 minLength = fileLength;
4943 if (LargeIntegerGreaterThan(minLength, scp->serverLength))
4944 minLength = scp->serverLength;
4946 /* adjust file length if we extend past EOF */
4947 thyper.LowPart = count;
4948 thyper.HighPart = 0;
4949 thyper = LargeIntegerAdd(offset, thyper); /* where write should end */
4950 if (LargeIntegerGreaterThan(thyper, fileLength)) {
4951 /* we'd write past EOF, so extend the file */
4952 scp->mask |= CM_SCACHEMASK_LENGTH;
4953 scp->length = thyper;
4954 filter |= (FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE);
4956 filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
4958 /* now, if the new position (thyper) and the old (offset) are in
4959 * different storeback windows, remember to store back the previous
4960 * storeback window when we're done with the write.
4962 if ((thyper.LowPart & (-cm_chunkSize)) !=
4963 (offset.LowPart & (-cm_chunkSize))) {
4964 /* they're different */
4966 writeBackOffset.HighPart = offset.HighPart;
4967 writeBackOffset.LowPart = offset.LowPart & (-cm_chunkSize);
4972 /* now, copy the data one buffer at a time, until we've filled the
4975 /* if we've copied all the data requested, we're done */
4976 if (count <= 0) break;
4978 /* handle over quota or out of space */
4979 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
4980 *writtenp = written;
4984 /* otherwise, load up a buffer of data */
4985 thyper.HighPart = offset.HighPart;
4986 thyper.LowPart = offset.LowPart & ~(buf_bufferSize-1);
4987 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
4990 lock_ReleaseMutex(&bufferp->mx);
4991 buf_Release(bufferp);
4994 lock_ReleaseMutex(&scp->mx);
4996 lock_ObtainRead(&scp->bufCreateLock);
4997 code = buf_Get(scp, &thyper, &bufferp);
4998 lock_ReleaseRead(&scp->bufCreateLock);
5000 lock_ObtainMutex(&bufferp->mx);
5001 lock_ObtainMutex(&scp->mx);
5002 if (code) goto done;
5004 bufferOffset = thyper;
5006 /* now get the data in the cache */
5008 code = cm_SyncOp(scp, bufferp, userp, &req, 0,
5009 CM_SCACHESYNC_NEEDCALLBACK
5010 | CM_SCACHESYNC_WRITE
5011 | CM_SCACHESYNC_BUFLOCKED);
5015 /* If we're overwriting the entire buffer, or
5016 * if we're writing at or past EOF, mark the
5017 * buffer as current so we don't call
5018 * cm_GetBuffer. This skips the fetch from the
5019 * server in those cases where we're going to
5020 * obliterate all the data in the buffer anyway,
5021 * or in those cases where there is no useful
5022 * data at the server to start with.
5024 * Use minLength instead of scp->length, since
5025 * the latter has already been updated by this
5028 if (LargeIntegerGreaterThanOrEqualTo(
5029 bufferp->offset, minLength)
5030 || LargeIntegerEqualTo(offset, bufferp->offset)
5031 && (count >= buf_bufferSize
5032 || LargeIntegerGreaterThanOrEqualTo(
5033 LargeIntegerAdd(offset,
5034 ConvertLongToLargeInteger(count)),
5036 if (count < buf_bufferSize
5037 && bufferp->dataVersion == -1)
5038 memset(bufferp->datap, 0,
5040 bufferp->dataVersion = scp->dataVersion;
5043 if (cm_HaveBuffer(scp, bufferp, 1)) break;
5045 /* otherwise, load the buffer and try again */
5046 lock_ReleaseMutex(&bufferp->mx);
5047 code = cm_GetBuffer(scp, bufferp, NULL, userp,
5049 lock_ReleaseMutex(&scp->mx);
5050 lock_ObtainMutex(&bufferp->mx);
5051 lock_ObtainMutex(&scp->mx);
5055 lock_ReleaseMutex(&bufferp->mx);
5056 buf_Release(bufferp);
5060 } /* if (wrong buffer) ... */
5062 /* now we have the right buffer loaded. Copy out the
5063 * data from here to the user's buffer.
5065 bufIndex = offset.LowPart & (buf_bufferSize - 1);
5067 /* and figure out how many bytes we want from this buffer */
5068 nbytes = buf_bufferSize - bufIndex; /* what remains in buffer */
5070 nbytes = count; /* don't go past end of request */
5072 /* now copy the data */
5075 dosmemget((dos_ptr)op, nbytes, bufferp->datap + bufIndex);
5078 memcpy(bufferp->datap + bufIndex, op, nbytes);
5079 buf_SetDirty(bufferp);
5081 /* and record the last writer */
5082 if (bufferp->userp != userp) {
5085 cm_ReleaseUser(bufferp->userp);
5086 bufferp->userp = userp;
5089 /* adjust counters, pointers, etc. */
5093 thyper.LowPart = nbytes;
5094 thyper.HighPart = 0;
5095 offset = LargeIntegerAdd(thyper, offset);
5099 lock_ReleaseMutex(&scp->mx);
5100 lock_ReleaseMutex(&fidp->mx);
5102 lock_ReleaseMutex(&bufferp->mx);
5103 buf_Release(bufferp);
5106 if (code == 0 && filter != 0 && (fidp->flags & SMB_FID_NTOPEN)
5107 && (fidp->NTopen_dscp->flags & CM_SCACHEFLAG_ANYWATCH)) {
5108 smb_NotifyChange(FILE_ACTION_MODIFIED, filter,
5109 fidp->NTopen_dscp, fidp->NTopen_pathp,
5113 if (code == 0 && doWriteBack) {
5114 lock_ObtainMutex(&scp->mx);
5115 cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_ASYNCSTORE);
5116 lock_ReleaseMutex(&scp->mx);
5117 cm_QueueBKGRequest(scp, cm_BkgStore, writeBackOffset.LowPart,
5118 writeBackOffset.HighPart, cm_chunkSize, 0, userp);
5124 long smb_ReceiveCoreWrite(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5127 long count, written = 0;
5132 cm_attr_t truncAttr; /* attribute struct used for truncating file */
5134 int inDataBlockCount;
5136 fd = smb_GetSMBParm(inp, 0);
5137 count = smb_GetSMBParm(inp, 1);
5138 offset.HighPart = 0; /* too bad */
5139 offset.LowPart = smb_GetSMBParm(inp, 2) | (smb_GetSMBParm(inp, 3) << 16);
5141 op = smb_GetSMBData(inp, NULL);
5142 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
5144 osi_Log3(smb_logp, "smb_ReceiveCoreWrite fd %d, off 0x%x, size 0x%x",
5145 fd, offset.LowPart, count);
5147 fd = smb_ChainFID(fd, inp);
5148 fidp = smb_FindFID(vcp, fd, 0);
5150 return CM_ERROR_BADFD;
5153 if (fidp->flags & SMB_FID_IOCTL)
5154 return smb_IoctlWrite(fidp, vcp, inp, outp);
5156 userp = smb_GetUser(vcp, inp);
5158 /* special case: 0 bytes transferred means truncate to this position */
5164 truncAttr.mask = CM_ATTRMASK_LENGTH;
5165 truncAttr.length.LowPart = offset.LowPart;
5166 truncAttr.length.HighPart = 0;
5167 lock_ObtainMutex(&fidp->mx);
5168 code = cm_SetAttr(fidp->scp, &truncAttr, userp, &req);
5169 lock_ReleaseMutex(&fidp->mx);
5170 smb_SetSMBParm(outp, 0, /* count */ 0);
5171 smb_SetSMBDataLength(outp, 0);
5172 fidp->flags |= SMB_FID_LENGTHSETDONE;
5177 * Work around bug in NT client
5179 * When copying a file, the NT client should first copy the data,
5180 * then copy the last write time. But sometimes the NT client does
5181 * these in the wrong order, so the data copies would inadvertently
5182 * cause the last write time to be overwritten. We try to detect this,
5183 * and don't set client mod time if we think that would go against the
5186 if ((fidp->flags & SMB_FID_MTIMESETDONE) != SMB_FID_MTIMESETDONE) {
5187 fidp->scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
5188 fidp->scp->clientModTime = time(NULL);
5192 code = smb_WriteData(fidp, &offset, count, op, userp, &written);
5194 code = smb_WriteData(fidp, &offset, count, op, userp, &written, FALSE);
5196 if (code == 0 && written < count)
5197 code = CM_ERROR_PARTIALWRITE;
5199 /* set the packet data length to 3 bytes for the data block header,
5200 * plus the size of the data.
5202 smb_SetSMBParm(outp, 0, written);
5203 smb_SetSMBDataLength(outp, 0);
5206 smb_ReleaseFID(fidp);
5207 cm_ReleaseUser(userp);
5212 void smb_CompleteWriteRaw(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
5213 NCB *ncbp, raw_write_cont_t *rwcp)
5226 fd = smb_GetSMBParm(inp, 0);
5227 fidp = smb_FindFID(vcp, fd, 0);
5229 osi_Log2(smb_logp, "Completing Raw Write offset %x count %x",
5230 rwcp->offset.LowPart, rwcp->count);
5232 userp = smb_GetUser(vcp, inp);
5236 code = smb_WriteData(fidp, &rwcp->offset, rwcp->count, rawBuf, userp,
5239 rawBuf = (dos_ptr) rwcp->buf;
5240 code = smb_WriteData(fidp, &rwcp->offset, rwcp->count,
5241 (unsigned char *) rawBuf, userp,
5245 if (rwcp->writeMode & 0x1) { /* synchronous */
5248 smb_FormatResponsePacket(vcp, inp, outp);
5249 op = (smb_t *) outp;
5250 op->com = 0x20; /* SMB_COM_WRITE_COMPLETE */
5251 smb_SetSMBParm(outp, 0, written + rwcp->alreadyWritten);
5252 smb_SetSMBDataLength(outp, 0);
5253 smb_SendPacket(vcp, outp);
5254 smb_FreePacket(outp);
5256 else { /* asynchronous */
5257 lock_ObtainMutex(&fidp->mx);
5258 fidp->raw_writers--;
5259 if (fidp->raw_writers == 0)
5260 thrd_SetEvent(fidp->raw_write_event);
5261 lock_ReleaseMutex(&fidp->mx);
5264 /* Give back raw buffer */
5265 lock_ObtainMutex(&smb_RawBufLock);
5267 *((char **)rawBuf) = smb_RawBufs;
5269 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
5271 smb_RawBufs = rawBuf;
5272 lock_ReleaseMutex(&smb_RawBufLock);
5274 smb_ReleaseFID(fidp);
5275 cm_ReleaseUser(userp);
5278 long smb_ReceiveCoreWriteRawDummy(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5283 long smb_ReceiveCoreWriteRaw(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp, raw_write_cont_t *rwcp)
5286 long count, written = 0;
5293 unsigned short writeMode;
5300 fd = smb_GetSMBParm(inp, 0);
5301 totalCount = smb_GetSMBParm(inp, 1);
5302 count = smb_GetSMBParm(inp, 10);
5303 offset.HighPart = 0; /* too bad */
5304 offset.LowPart = smb_GetSMBParm(inp, 3) | (smb_GetSMBParm(inp, 4) << 16);
5305 writeMode = smb_GetSMBParm(inp, 7);
5307 op = (char *) inp->data;
5308 op += smb_GetSMBParm(inp, 11);
5311 "smb_ReceiveCoreWriteRaw fd %d, off 0x%x, size 0x%x, WriteMode 0x%x",
5312 fd, offset.LowPart, count, writeMode);
5314 fd = smb_ChainFID(fd, inp);
5315 fidp = smb_FindFID(vcp, fd, 0);
5317 return CM_ERROR_BADFD;
5320 userp = smb_GetUser(vcp, inp);
5323 * Work around bug in NT client
5325 * When copying a file, the NT client should first copy the data,
5326 * then copy the last write time. But sometimes the NT client does
5327 * these in the wrong order, so the data copies would inadvertently
5328 * cause the last write time to be overwritten. We try to detect this,
5329 * and don't set client mod time if we think that would go against the
5332 if ((fidp->flags & SMB_FID_LOOKSLIKECOPY) != SMB_FID_LOOKSLIKECOPY) {
5333 fidp->scp->mask |= CM_SCACHEMASK_CLIENTMODTIME;
5334 fidp->scp->clientModTime = time(NULL);
5338 code = smb_WriteData(fidp, &offset, count, op, userp, &written);
5340 code = smb_WriteData(fidp, &offset, count, op, userp, &written, FALSE);
5342 if (code == 0 && written < count)
5343 code = CM_ERROR_PARTIALWRITE;
5345 /* Get a raw buffer */
5348 lock_ObtainMutex(&smb_RawBufLock);
5350 /* Get a raw buf, from head of list */
5351 rawBuf = smb_RawBufs;
5353 smb_RawBufs = *(char **)smb_RawBufs;
5355 smb_RawBufs = _farpeekl(_dos_ds, smb_RawBufs);
5359 code = CM_ERROR_USESTD;
5361 lock_ReleaseMutex(&smb_RawBufLock);
5364 /* Don't allow a premature Close */
5365 if (code == 0 && (writeMode & 1) == 0) {
5366 lock_ObtainMutex(&fidp->mx);
5367 fidp->raw_writers++;
5368 thrd_ResetEvent(fidp->raw_write_event);
5369 lock_ReleaseMutex(&fidp->mx);
5372 smb_ReleaseFID(fidp);
5373 cm_ReleaseUser(userp);
5376 smb_SetSMBParm(outp, 0, written);
5377 smb_SetSMBDataLength(outp, 0);
5378 ((smb_t *)outp)->com = 0x20; /* SMB_COM_WRITE_COMPLETE */
5385 rwcp->offset.HighPart = 0;
5386 rwcp->offset.LowPart = offset.LowPart + count;
5387 rwcp->count = totalCount - count;
5388 rwcp->writeMode = writeMode;
5389 rwcp->alreadyWritten = written;
5391 /* set the packet data length to 3 bytes for the data block header,
5392 * plus the size of the data.
5394 smb_SetSMBParm(outp, 0, 0xffff);
5395 smb_SetSMBDataLength(outp, 0);
5400 long smb_ReceiveCoreRead(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5403 long count, finalCount;
5410 fd = smb_GetSMBParm(inp, 0);
5411 count = smb_GetSMBParm(inp, 1);
5412 offset.HighPart = 0; /* too bad */
5413 offset.LowPart = smb_GetSMBParm(inp, 2) | (smb_GetSMBParm(inp, 3) << 16);
5415 osi_Log3(smb_logp, "smb_ReceiveCoreRead fd %d, off 0x%x, size 0x%x",
5416 fd, offset.LowPart, count);
5418 fd = smb_ChainFID(fd, inp);
5419 fidp = smb_FindFID(vcp, fd, 0);
5421 return CM_ERROR_BADFD;
5424 if (fidp->flags & SMB_FID_IOCTL) {
5425 return smb_IoctlRead(fidp, vcp, inp, outp);
5428 userp = smb_GetUser(vcp, inp);
5430 /* remember this for final results */
5431 smb_SetSMBParm(outp, 0, count);
5432 smb_SetSMBParm(outp, 1, 0);
5433 smb_SetSMBParm(outp, 2, 0);
5434 smb_SetSMBParm(outp, 3, 0);
5435 smb_SetSMBParm(outp, 4, 0);
5437 /* set the packet data length to 3 bytes for the data block header,
5438 * plus the size of the data.
5440 smb_SetSMBDataLength(outp, count+3);
5442 /* get op ptr after putting in the parms, since otherwise we don't
5443 * know where the data really is.
5445 op = smb_GetSMBData(outp, NULL);
5447 /* now emit the data block header: 1 byte of type and 2 bytes of length */
5448 *op++ = 1; /* data block marker */
5449 *op++ = (unsigned char) (count & 0xff);
5450 *op++ = (unsigned char) ((count >> 8) & 0xff);
5453 code = smb_ReadData(fidp, &offset, count, op, userp, &finalCount);
5455 code = smb_ReadData(fidp, &offset, count, op, userp, &finalCount, FALSE);
5458 /* fix some things up */
5459 smb_SetSMBParm(outp, 0, finalCount);
5460 smb_SetSMBDataLength(outp, finalCount+3);
5462 smb_ReleaseFID(fidp);
5464 cm_ReleaseUser(userp);
5468 long smb_ReceiveCoreMakeDir(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5475 cm_scache_t *dscp; /* dir we're dealing with */
5476 cm_scache_t *scp; /* file we're creating */
5478 int initialModeBits;
5488 /* compute initial mode bits based on read-only flag in attributes */
5489 initialModeBits = 0777;
5491 tp = smb_GetSMBData(inp, NULL);
5492 pathp = smb_ParseASCIIBlock(tp, &tp);
5494 if (strcmp(pathp, "\\") == 0)
5495 return CM_ERROR_EXISTS;
5497 spacep = inp->spacep;
5498 smb_StripLastComponent(spacep->data, &lastNamep, pathp);
5500 userp = smb_GetUser(vcp, inp);
5502 caseFold = CM_FLAG_CASEFOLD;
5504 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
5506 code = cm_NameI(cm_rootSCachep, spacep->data,
5507 caseFold | CM_FLAG_FOLLOW | CM_FLAG_CHECKPATH,
5508 userp, tidPathp, &req, &dscp);
5511 cm_ReleaseUser(userp);
5515 /* otherwise, scp points to the parent directory. Do a lookup, and
5516 * fail if we find it. Otherwise, we do the create.
5522 code = cm_Lookup(dscp, lastNamep, 0, userp, &req, &scp);
5523 if (scp) cm_ReleaseSCache(scp);
5524 if (code != CM_ERROR_NOSUCHFILE) {
5525 if (code == 0) code = CM_ERROR_EXISTS;
5526 cm_ReleaseSCache(dscp);
5527 cm_ReleaseUser(userp);
5531 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
5532 setAttr.clientModTime = time(NULL);
5533 code = cm_MakeDir(dscp, lastNamep, 0, &setAttr, userp, &req);
5534 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
5535 smb_NotifyChange(FILE_ACTION_ADDED,
5536 FILE_NOTIFY_CHANGE_DIR_NAME,
5537 dscp, lastNamep, NULL, TRUE);
5539 /* we don't need this any longer */
5540 cm_ReleaseSCache(dscp);
5543 /* something went wrong creating or truncating the file */
5544 cm_ReleaseUser(userp);
5548 /* otherwise we succeeded */
5549 smb_SetSMBDataLength(outp, 0);
5550 cm_ReleaseUser(userp);
5555 BOOL smb_IsLegalFilename(char *filename)
5558 * Find the longest substring of filename that does not contain
5559 * any of the chars in illegalChars. If that substring is less
5560 * than the length of the whole string, then one or more of the
5561 * illegal chars is in filename.
5563 if (strcspn(filename, illegalChars) < strlen(filename))
5569 long smb_ReceiveCoreCreate(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5577 cm_scache_t *dscp; /* dir we're dealing with */
5578 cm_scache_t *scp; /* file we're creating */
5580 int initialModeBits;
5592 excl = (inp->inCom == 0x03)? 0 : 1;
5594 attributes = smb_GetSMBParm(inp, 0);
5595 dosTime = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
5597 /* compute initial mode bits based on read-only flag in attributes */
5598 initialModeBits = 0666;
5599 if (attributes & 1) initialModeBits &= ~0222;
5601 tp = smb_GetSMBData(inp, NULL);
5602 pathp = smb_ParseASCIIBlock(tp, &tp);
5604 spacep = inp->spacep;
5605 smb_StripLastComponent(spacep->data, &lastNamep, pathp);
5607 userp = smb_GetUser(vcp, inp);
5609 caseFold = CM_FLAG_CASEFOLD;
5611 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
5612 code = cm_NameI(cm_rootSCachep, spacep->data, caseFold | CM_FLAG_FOLLOW,
5613 userp, tidPathp, &req, &dscp);
5616 cm_ReleaseUser(userp);
5620 /* otherwise, scp points to the parent directory. Do a lookup, and
5621 * truncate the file if we find it, otherwise we create the file.
5623 if (!lastNamep) lastNamep = pathp;
5626 if (!smb_IsLegalFilename(lastNamep))
5627 return CM_ERROR_BADNTFILENAME;
5629 osi_Log1(smb_logp, "SMB receive create [%s]", osi_LogSaveString( smb_logp, pathp ));
5630 #ifdef DEBUG_VERBOSE
5633 hexp = osi_HexifyString( lastNamep );
5634 DEBUG_EVENT2("AFS", "CoreCreate H[%s] A[%s]", hexp, lastNamep );
5639 code = cm_Lookup(dscp, lastNamep, 0, userp, &req, &scp);
5640 if (code && code != CM_ERROR_NOSUCHFILE) {
5641 cm_ReleaseSCache(dscp);
5642 cm_ReleaseUser(userp);
5646 /* if we get here, if code is 0, the file exists and is represented by
5647 * scp. Otherwise, we have to create it.
5651 /* oops, file shouldn't be there */
5652 cm_ReleaseSCache(dscp);
5653 cm_ReleaseSCache(scp);
5654 cm_ReleaseUser(userp);
5655 return CM_ERROR_EXISTS;
5658 setAttr.mask = CM_ATTRMASK_LENGTH;
5659 setAttr.length.LowPart = 0;
5660 setAttr.length.HighPart = 0;
5661 code = cm_SetAttr(scp, &setAttr, userp, &req);
5664 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
5665 smb_UnixTimeFromDosUTime(&setAttr.clientModTime, dosTime);
5666 code = cm_Create(dscp, lastNamep, 0, &setAttr, &scp, userp,
5668 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
5669 smb_NotifyChange(FILE_ACTION_ADDED,
5670 FILE_NOTIFY_CHANGE_FILE_NAME,
5671 dscp, lastNamep, NULL, TRUE);
5672 if (!excl && code == CM_ERROR_EXISTS) {
5673 /* not an exclusive create, and someone else tried
5674 * creating it already, then we open it anyway. We
5675 * don't bother retrying after this, since if this next
5676 * fails, that means that the file was deleted after
5677 * we started this call.
5679 code = cm_Lookup(dscp, lastNamep, caseFold, userp,
5682 setAttr.mask = CM_ATTRMASK_LENGTH;
5683 setAttr.length.LowPart = 0;
5684 setAttr.length.HighPart = 0;
5685 code = cm_SetAttr(scp, &setAttr, userp, &req);
5690 /* we don't need this any longer */
5691 cm_ReleaseSCache(dscp);
5694 /* something went wrong creating or truncating the file */
5695 if (scp) cm_ReleaseSCache(scp);
5696 cm_ReleaseUser(userp);
5700 /* make sure we only open files */
5701 if (scp->fileType != CM_SCACHETYPE_FILE) {
5702 cm_ReleaseSCache(scp);
5703 cm_ReleaseUser(userp);
5704 return CM_ERROR_ISDIR;
5707 /* now all we have to do is open the file itself */
5708 fidp = smb_FindFID(vcp, 0, SMB_FLAG_CREATE);
5711 /* save a pointer to the vnode */
5714 /* always create it open for read/write */
5715 fidp->flags |= (SMB_FID_OPENREAD | SMB_FID_OPENWRITE);
5717 smb_ReleaseFID(fidp);
5719 smb_SetSMBParm(outp, 0, fidp->fid);
5720 smb_SetSMBDataLength(outp, 0);
5722 cm_Open(scp, 0, userp);
5724 cm_ReleaseUser(userp);
5725 /* leave scp held since we put it in fidp->scp */
5729 long smb_ReceiveCoreSeek(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
5742 fd = smb_GetSMBParm(inp, 0);
5743 whence = smb_GetSMBParm(inp, 1);
5744 offset = smb_GetSMBParm(inp, 2) | (smb_GetSMBParm(inp, 3) << 16);
5746 /* try to find the file descriptor */
5747 fd = smb_ChainFID(fd, inp);
5748 fidp = smb_FindFID(vcp, fd, 0);
5749 if (!fidp || (fidp->flags & SMB_FID_IOCTL)) {
5750 return CM_ERROR_BADFD;
5753 userp = smb_GetUser(vcp, inp);
5755 lock_ObtainMutex(&fidp->mx);
5757 lock_ObtainMutex(&scp->mx);
5758 code = cm_SyncOp(scp, NULL, userp, &req, 0,
5759 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
5762 /* offset from current offset */
5763 offset += fidp->offset;
5765 else if (whence == 2) {
5766 /* offset from current EOF */
5767 offset += scp->length.LowPart;
5769 fidp->offset = offset;
5770 smb_SetSMBParm(outp, 0, offset & 0xffff);
5771 smb_SetSMBParm(outp, 1, (offset>>16) & 0xffff);
5772 smb_SetSMBDataLength(outp, 0);
5774 lock_ReleaseMutex(&scp->mx);
5775 lock_ReleaseMutex(&fidp->mx);
5776 smb_ReleaseFID(fidp);
5777 cm_ReleaseUser(userp);
5781 /* dispatch all of the requests received in a packet. Due to chaining, this may
5782 * be more than one request.
5784 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
5785 NCB *ncbp, raw_write_cont_t *rwcp)
5789 unsigned long code = 0;
5790 unsigned char *outWctp;
5791 int nparms; /* # of bytes of parameters */
5793 int nbytes; /* bytes of data, excluding count */
5796 unsigned short errCode;
5797 unsigned long NTStatus;
5799 unsigned char errClass;
5800 unsigned int oldGen;
5801 DWORD oldTime, newTime;
5803 /* get easy pointer to the data */
5804 smbp = (smb_t *) inp->data;
5806 if (!(outp->flags & SMB_PACKETFLAG_SUSPENDED)) {
5807 /* setup the basic parms for the initial request in the packet */
5808 inp->inCom = smbp->com;
5809 inp->wctp = &smbp->wct;
5811 inp->ncb_length = ncbp->ncb_length;
5816 if (ncbp->ncb_length < offsetof(struct smb, vdata)) {
5817 /* log it and discard it */
5822 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
5823 sprintf(s, "SMB message too short, len %d", ncbp->ncb_length);
5825 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1007, NULL,
5826 1, ncbp->ncb_length, ptbuf, inp);
5827 DeregisterEventSource(h);
5829 osi_Log1(smb_logp, "SMB message too short, len %d",
5836 /* We are an ongoing op */
5837 thrd_Increment(&ongoingOps);
5839 /* set up response packet for receiving output */
5840 if (!(outp->flags & SMB_PACKETFLAG_SUSPENDED))
5841 smb_FormatResponsePacket(vcp, inp, outp);
5842 outWctp = outp->wctp;
5844 /* Remember session generation number and time */
5845 oldGen = sessionGen;
5846 oldTime = GetCurrentTime();
5848 while(inp->inCom != 0xff) {
5849 dp = &smb_dispatchTable[inp->inCom];
5851 if (outp->flags & SMB_PACKETFLAG_SUSPENDED) {
5852 outp->flags &= ~SMB_PACKETFLAG_SUSPENDED;
5853 code = outp->resumeCode;
5857 /* process each request in the packet; inCom, wctp and inCount
5858 * are already set up.
5860 osi_Log2(smb_logp, "SMB received op 0x%x lsn %d", inp->inCom,
5863 /* now do the dispatch */
5864 /* start by formatting the response record a little, as a default */
5865 if (dp->flags & SMB_DISPATCHFLAG_CHAINED) {
5867 outWctp[1] = 0xff; /* no operation */
5868 outWctp[2] = 0; /* padding */
5873 /* not a chained request, this is a more reasonable default */
5874 outWctp[0] = 0; /* wct of zero */
5875 outWctp[1] = 0; /* and bcc (word) of zero */
5879 /* once set, stays set. Doesn't matter, since we never chain
5880 * "no response" calls.
5882 if (dp->flags & SMB_DISPATCHFLAG_NORESPONSE)
5886 /* we have a recognized operation */
5888 if (inp->inCom == 0x1d)
5890 code = smb_ReceiveCoreWriteRaw (vcp, inp, outp,
5893 osi_LogEvent("AFS Dispatch %s",(myCrt_Dispatch(inp->inCom)),"vcp[%x] lana[%d] lsn[%d]",(int)vcp,vcp->lana,vcp->lsn);
5894 osi_Log4(smb_logp,"Dispatch %s vcp[%x] lana[%d] lsn[%d]",(myCrt_Dispatch(inp->inCom)),vcp,vcp->lana,vcp->lsn);
5895 code = (*(dp->procp)) (vcp, inp, outp);
5896 osi_LogEvent("AFS Dispatch return",NULL,"Code[%d]",(code==0)?0:code-CM_ERROR_BASE);
5897 osi_Log1(smb_logp,"Dispatch return code[%d]",(code==0)?0:code-CM_ERROR_BASE);
5900 if (oldGen != sessionGen) {
5905 newTime = GetCurrentTime();
5906 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
5907 sprintf(s, "Pkt straddled session startup, took %d ms, ncb length %d",
5908 newTime - oldTime, ncbp->ncb_length);
5910 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0,
5911 1005, NULL, 1, ncbp->ncb_length, ptbuf, smbp);
5912 DeregisterEventSource(h);
5914 osi_Log1(smb_logp, "Pkt straddled session startup, "
5915 "ncb length %d", ncbp->ncb_length);
5920 /* bad opcode, fail the request, after displaying it */
5923 #endif /* NOTSERVICE */
5927 sprintf(tbuffer, "Received bad SMB req 0x%x", inp->inCom);
5928 code = (*smb_MBfunc)(NULL, tbuffer, "Cancel: don't show again",
5929 MB_OKCANCEL|MB_SERVICE_NOTIFICATION);
5930 if (code == IDCANCEL) showErrors = 0;
5933 code = CM_ERROR_BADOP;
5936 /* catastrophic failure: log as much as possible */
5937 if (code == CM_ERROR_BADSMB) {
5944 "Invalid SMB, ncb_length %d",
5947 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
5948 sprintf(s, "Invalid SMB message, length %d",
5951 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1002, NULL,
5952 1, ncbp->ncb_length, ptbuf, smbp);
5953 DeregisterEventSource(h);
5956 #endif /* NOTSERVICE */
5958 osi_Log1(smb_logp, "Invalid SMB message, length %d",
5962 code = CM_ERROR_INVAL;
5965 if (outp->flags & SMB_PACKETFLAG_NOSEND) {
5966 thrd_Decrement(&ongoingOps);
5971 /* now, if we failed, turn the current response into an empty
5972 * one, and fill in the response packet's error code.
5975 if (vcp->flags & SMB_VCFLAG_STATUS32) {
5976 smb_MapNTError(code, &NTStatus);
5977 outWctp = outp->wctp;
5978 smbp = (smb_t *) &outp->data;
5979 if (code != CM_ERROR_PARTIALWRITE
5980 && code != CM_ERROR_BUFFERTOOSMALL
5981 && code != CM_ERROR_GSSCONTINUE) {
5982 /* nuke wct and bcc. For a partial
5983 * write or an in-process authentication handshake,
5984 * assume they're OK.
5990 smbp->rcls = (unsigned char) (NTStatus & 0xff);
5991 smbp->reh = (unsigned char) ((NTStatus >> 8) & 0xff);
5992 smbp->errLow = (unsigned char) ((NTStatus >> 16) & 0xff);
5993 smbp->errHigh = (unsigned char) ((NTStatus >> 24) & 0xff);
5994 smbp->flg2 |= 0x4000;
5998 smb_MapCoreError(code, vcp, &errCode, &errClass);
5999 outWctp = outp->wctp;
6000 smbp = (smb_t *) &outp->data;
6001 if (code != CM_ERROR_PARTIALWRITE) {
6002 /* nuke wct and bcc. For a partial
6003 * write, assume they're OK.
6009 smbp->errLow = (unsigned char) (errCode & 0xff);
6010 smbp->errHigh = (unsigned char) ((errCode >> 8) & 0xff);
6011 smbp->rcls = errClass;
6014 } /* error occurred */
6016 /* if we're here, we've finished one request. Look to see if
6017 * this is a chained opcode. If it is, setup things to process
6018 * the chained request, and setup the output buffer to hold the
6019 * chained response. Start by finding the next input record.
6021 if (!(dp->flags & SMB_DISPATCHFLAG_CHAINED))
6022 break; /* not a chained req */
6023 tp = inp->wctp; /* points to start of last request */
6024 /* in a chained request, the first two
6025 * parm fields are required, and are
6026 * AndXCommand/AndXReserved and
6028 if (tp[0] < 2) break;
6029 if (tp[1] == 0xff) break; /* no more chained opcodes */
6031 inp->wctp = inp->data + tp[3] + (tp[4] << 8);
6034 /* and now append the next output request to the end of this
6035 * last request. Begin by finding out where the last response
6036 * ends, since that's where we'll put our new response.
6038 outWctp = outp->wctp; /* ptr to out parameters */
6039 osi_assert (outWctp[0] >= 2); /* need this for all chained requests */
6040 nparms = outWctp[0] << 1;
6041 tp = outWctp + nparms + 1; /* now points to bcc field */
6042 nbytes = tp[0] + (tp[1] << 8); /* # of data bytes */
6043 tp += 2 /* for the count itself */ + nbytes;
6044 /* tp now points to the new output record; go back and patch the
6045 * second parameter (off2) to point to the new record.
6047 temp = (unsigned int)tp - ((unsigned int) outp->data);
6048 outWctp[3] = (unsigned char) (temp & 0xff);
6049 outWctp[4] = (unsigned char) ((temp >> 8) & 0xff);
6050 outWctp[2] = 0; /* padding */
6051 outWctp[1] = inp->inCom; /* next opcode */
6053 /* finally, setup for the next iteration */
6056 } /* while loop over all requests in the packet */
6058 /* done logging out, turn off logging-out flag */
6059 if (!(inp->flags & SMB_PACKETFLAG_PROFILE_UPDATE_OK)) {
6060 vcp->justLoggedOut = NULL;
6063 free(loggedOutName);
6064 loggedOutName = NULL;
6065 smb_ReleaseUID(loggedOutUserp);
6066 loggedOutUserp = NULL;
6070 /* now send the output packet, and return */
6072 smb_SendPacket(vcp, outp);
6073 thrd_Decrement(&ongoingOps);
6075 if (!(vcp->flags & SMB_VCFLAG_ALREADYDEAD)) {
6077 smb_ReleaseVC(active_vcp);
6079 "Replacing active_vcp %x with %x", active_vcp, vcp);
6083 last_msg_time = GetCurrentTime();
6085 else if (active_vcp == vcp) {
6086 smb_ReleaseVC(active_vcp);
6094 /* Wait for Netbios() calls to return, and make the results available to server
6095 * threads. Note that server threads can't wait on the NCBevents array
6096 * themselves, because NCB events are manual-reset, and the servers would race
6097 * each other to reset them.
6099 void smb_ClientWaiter(void *parmp)
6105 code = thrd_WaitForMultipleObjects_Event(numNCBs, NCBevents,
6107 if (code == WAIT_OBJECT_0)
6110 /* error checking */
6111 if (code >= WAIT_ABANDONED_0 && code < (WAIT_ABANDONED_0 + numNCBs))
6113 int abandonIdx = code - WAIT_ABANDONED_0;
6114 afsi_log("Error: smb_ClientWaiter event %d abandoned, errno %d\n", abandonIdx, GetLastError());
6117 if (code == WAIT_IO_COMPLETION)
6119 afsi_log("Error: smb_ClientWaiter WAIT_IO_COMPLETION\n");
6123 if (code == WAIT_TIMEOUT)
6125 afsi_log("Error: smb_ClientWaiter WAIT_TIMEOUT, errno %d\n", GetLastError());
6128 if (code == WAIT_FAILED)
6130 afsi_log("Error: smb_ClientWaiter WAIT_FAILED, errno %d\n", GetLastError());
6133 idx = code - WAIT_OBJECT_0;
6135 /* check idx range! */
6136 if (idx < 0 || idx > (sizeof(NCBevents) / sizeof(NCBevents[0])))
6138 /* this is fatal - log as much as possible */
6139 afsi_log("Fatal: NCBevents idx [ %d ] out of range.\n", idx);
6143 thrd_ResetEvent(NCBevents[idx]);
6144 thrd_SetEvent(NCBreturns[0][idx]);
6150 * Try to have one NCBRECV request waiting for every live session. Not more
6151 * than one, because if there is more than one, it's hard to handle Write Raw.
6153 void smb_ServerWaiter(void *parmp)
6156 int idx_session, idx_NCB;
6164 code = thrd_WaitForMultipleObjects_Event(numSessions, SessionEvents,
6166 if (code == WAIT_OBJECT_0)
6169 if (code >= WAIT_ABANDONED_0 && code < (WAIT_ABANDONED_0 + numSessions))
6171 int abandonIdx = code - WAIT_ABANDONED_0;
6172 afsi_log("Error: smb_ServerWaiter (SessionEvents) event %d abandoned, errno %d\n", abandonIdx, GetLastError());
6175 if (code == WAIT_IO_COMPLETION)
6177 afsi_log("Error: smb_ServerWaiter (SessionEvents) WAIT_IO_COMPLETION\n");
6181 if (code == WAIT_TIMEOUT)
6183 afsi_log("Error: smb_ServerWaiter (SessionEvents) WAIT_TIMEOUT, errno %d\n", GetLastError());
6186 if (code == WAIT_FAILED)
6188 afsi_log("Error: smb_ServerWaiter (SessionEvents) WAIT_FAILED, errno %d\n", GetLastError());
6191 idx_session = code - WAIT_OBJECT_0;
6193 /* check idx range! */
6194 if (idx_session < 0 || idx_session > (sizeof(SessionEvents) / sizeof(SessionEvents[0])))
6196 /* this is fatal - log as much as possible */
6197 afsi_log("Fatal: session idx [ %d ] out of range.\n", idx_session);
6203 code = thrd_WaitForMultipleObjects_Event(numNCBs, NCBavails,
6205 if (code == WAIT_OBJECT_0)
6208 /* error checking */
6209 if (code >= WAIT_ABANDONED_0 && code < (WAIT_ABANDONED_0 + numNCBs))
6211 int abandonIdx = code - WAIT_ABANDONED_0;
6212 afsi_log("Error: smb_ClientWaiter (NCBavails) event %d abandoned, errno %d\n", abandonIdx, GetLastError());
6215 if (code == WAIT_IO_COMPLETION)
6217 afsi_log("Error: smb_ClientWaiter (NCBavails) WAIT_IO_COMPLETION\n");
6221 if (code == WAIT_TIMEOUT)
6223 afsi_log("Error: smb_ClientWaiter (NCBavails) WAIT_TIMEOUT, errno %d\n", GetLastError());
6226 if (code == WAIT_FAILED)
6228 afsi_log("Error: smb_ClientWaiter (NCBavails) WAIT_FAILED, errno %d\n", GetLastError());
6231 idx_NCB = code - WAIT_OBJECT_0;
6233 /* check idx range! */
6234 if (idx_NCB < 0 || idx_NCB > (sizeof(NCBsessions) / sizeof(NCBsessions[0])))
6236 /* this is fatal - log as much as possible */
6237 afsi_log("Fatal: idx_NCB [ %d ] out of range.\n", idx_NCB);
6241 /* Link them together */
6242 NCBsessions[idx_NCB] = idx_session;
6245 ncbp = NCBs[idx_NCB];
6247 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
6249 ncbp->ncb_lsn = (unsigned char) LSNs[idx_session];
6250 ncbp->ncb_command = NCBRECV | ASYNCH;
6251 ncbp->ncb_lana_num = lanas[idx_session];
6253 ncbp->ncb_buffer = (unsigned char *) bufs[idx_NCB];
6254 ncbp->ncb_event = NCBevents[idx_NCB];
6255 ncbp->ncb_length = SMB_PACKETSIZE;
6258 ncbp->ncb_buffer = bufs[idx_NCB]->dos_pkt;
6259 ((smb_ncb_t*)ncbp)->orig_pkt = bufs[idx_NCB];
6260 ncbp->ncb_event = NCBreturns[0][idx_NCB];
6261 ncbp->ncb_length = SMB_PACKETSIZE;
6262 Netbios(ncbp, dos_ncb);
6268 * The top level loop for handling SMB request messages. Each server thread
6269 * has its own NCB and buffer for sending replies (outncbp, outbufp), but the
6270 * NCB and buffer for the incoming request are loaned to us.
6272 * Write Raw trickery starts here. When we get a Write Raw, we are supposed
6273 * to immediately send a request for the rest of the data. This must come
6274 * before any other traffic for that session, so we delay setting the session
6275 * event until that data has come in.
6277 void smb_Server(VOID *parmp)
6279 int myIdx = (int) parmp;
6283 smb_packet_t *outbufp;
6285 int idx_NCB, idx_session;
6287 smb_vc_t *vcp = NULL;
6294 outbufp = GetPacket();
6295 outbufp->ncbp = outncbp;
6299 /* check for demo expiration */
6301 unsigned long tod = time((void *) 0);
6302 if (tod > EXPIREDATE) {
6303 (*smb_MBfunc)(NULL, "AFS demo expiration",
6305 MB_OK|MB_ICONSTOP|MB_SETFOREGROUND|MB_SERVICE_NOTIFICATION);
6309 #endif /* !NOEXPIRE */
6311 code = thrd_WaitForMultipleObjects_Event(numNCBs, NCBreturns[myIdx],
6313 if (code == WAIT_OBJECT_0) {
6317 /* error checking */
6318 if (code >= WAIT_ABANDONED_0 && code < (WAIT_ABANDONED_0 + numNCBs))
6320 int abandonIdx = code - WAIT_ABANDONED_0;
6321 afsi_log("Error: smb_Server ( NCBreturns[%d] ) event %d abandoned, errno %d\n", myIdx, abandonIdx, GetLastError());
6324 if (code == WAIT_IO_COMPLETION)
6326 afsi_log("Error: smb_Server ( NCBreturns[%d] ) WAIT_IO_COMPLETION\n", myIdx);
6330 if (code == WAIT_TIMEOUT)
6332 afsi_log("Error: smb_Server ( NCBreturns[%d] ) WAIT_TIMEOUT, errno %d\n", myIdx, GetLastError());
6335 if (code == WAIT_FAILED)
6337 afsi_log("Error: smb_Server ( NCBreturns[%d] ) WAIT_FAILED, errno %d\n", myIdx, GetLastError());
6340 idx_NCB = code - WAIT_OBJECT_0;
6342 /* check idx range! */
6343 if (idx_NCB < 0 || idx_NCB > (sizeof(NCBs) / sizeof(NCBs[0])))
6345 /* this is fatal - log as much as possible */
6346 afsi_log("Fatal: idx_NCB [ %d ] out of range.\n", idx_NCB);
6350 ncbp = NCBs[idx_NCB];
6352 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
6354 idx_session = NCBsessions[idx_NCB];
6355 rc = ncbp->ncb_retcode;
6357 if (rc != NRC_PENDING && rc != NRC_GOODRET)
6358 osi_Log1(smb_logp, "NCBRECV failure code %d", rc);
6361 case NRC_GOODRET: break;
6364 /* Can this happen? Or is it just my
6371 /* Client closed session */
6372 if (reportSessionStartups)
6374 afsi_log("session [ %d ] closed", idx_session);
6376 dead_sessions[idx_session] = TRUE;
6379 vcp = smb_FindVC(ncbp->ncb_lsn, 0, lanas[idx_session]);
6380 /* Should also release vcp. [done] 2004-05-11 jaltman
6382 * sanity check that all TID's are gone.
6384 * TODO: check if we could use LSNs[idx_session] instead,
6385 * also cleanup after dead vcp
6390 "dead_vcp already set, %x",
6392 if (!dead_vcp && !(vcp->flags & SMB_VCFLAG_ALREADYDEAD)) {
6394 "setting dead_vcp %x, user struct %x",
6398 vcp->flags |= SMB_VCFLAG_ALREADYDEAD;
6400 if (vcp->justLoggedOut) {
6402 loggedOutTime = vcp->logoffTime;
6404 strdup(vcp->justLoggedOut->unp->name);
6405 loggedOutUserp = vcp->justLoggedOut;
6406 lock_ObtainWrite(&smb_rctLock);
6407 loggedOutUserp->refCount++;
6408 lock_ReleaseWrite(&smb_rctLock);
6414 /* Treat as transient error */
6421 osi_Log1(smb_logp, "dispatch smb recv failed, message incomplete, ncb_length %d",
6423 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
6424 sprintf(s, "SMB message incomplete, length %d",
6427 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0,
6429 ncbp->ncb_length, ptbuf,
6431 DeregisterEventSource(h);
6434 "dispatch smb recv failed, message incomplete, ncb_length %d",
6437 "SMB message incomplete, "
6438 "length %d", ncbp->ncb_length);
6442 * We used to discard the packet.
6443 * Instead, try handling it normally.
6451 /* A weird error code. Log it, sleep, and
6453 if (vcp && vcp->errorCount++ > 3) {
6454 afsi_log("session [ %d ] closed, vcp->errorCount = %d", idx_session, vcp->errorCount);
6455 dead_sessions[idx_session] = TRUE;
6459 thrd_SetEvent(SessionEvents[idx_session]);
6464 /* Success, so now dispatch on all the data in the packet */
6466 smb_concurrentCalls++;
6467 if (smb_concurrentCalls > smb_maxObsConcurrentCalls)
6468 smb_maxObsConcurrentCalls = smb_concurrentCalls;
6472 vcp = smb_FindVC(ncbp->ncb_lsn, 0, ncbp->ncb_lana_num);
6474 * If at this point vcp is NULL (implies that packet was invalid)
6475 * then we are in big trouble. This means either :
6476 * a) we have the wrong NCB.
6477 * b) Netbios screwed up the call.
6478 * Obviously this implies that
6479 * ( LSNs[idx_session] != ncbp->ncb_lsn ||
6480 * lanas[idx_session] != ncbp->ncb_lana_num )
6481 * Either way, we can't do anything with this packet.
6482 * Log, sleep and resume.
6491 "LSNs[idx_session]=[%d],"
6492 "lanas[idx_session]=[%d],"
6493 "ncbp->ncb_lsn=[%d],"
6494 "ncbp->ncb_lana_num=[%d]",
6498 ncbp->ncb_lana_num);
6502 h = RegisterEventSource(NULL,AFS_DAEMON_EVENT_NAME);
6504 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1001, NULL,1,sizeof(*ncbp),ptbuf,(void*)ncbp);
6505 DeregisterEventSource(h);
6508 /* Also log in the trace log. */
6509 osi_Log4(smb_logp, "Server: BAD VCP!"
6510 "LSNs[idx_session]=[%d],"
6511 "lanas[idx_session]=[%d],"
6512 "ncbp->ncb_lsn=[%d],"
6513 "ncbp->ncb_lana_num=[%d]",
6517 ncbp->ncb_lana_num);
6519 /* thrd_Sleep(1000); Don't bother sleeping */
6520 thrd_SetEvent(SessionEvents[idx_session]);
6521 smb_concurrentCalls--;
6526 vcp->errorCount = 0;
6527 bufp = (struct smb_packet *) ncbp->ncb_buffer;
6529 bufp = ((smb_ncb_t *) ncbp)->orig_pkt;
6530 /* copy whole packet to virtual memory */
6531 /*fprintf(stderr, "smb_Server: copying dos packet at 0x%x, "
6533 bufp->dos_pkt / 16, bufp);*/
6535 dosmemget(bufp->dos_pkt, ncbp->ncb_length, bufp->data);
6537 smbp = (smb_t *)bufp->data;
6540 #if !defined(DJGPP) && !defined(AFS_WIN32_ENV)
6544 if (smbp->com == 0x1d) {
6545 /* Special handling for Write Raw */
6546 raw_write_cont_t rwc;
6547 EVENT_HANDLE rwevent;
6548 char eventName[MAX_PATH];
6550 smb_DispatchPacket(vcp, bufp, outbufp, ncbp, &rwc);
6551 if (rwc.code == 0) {
6552 rwevent = thrd_CreateEvent(NULL, FALSE, FALSE, TEXT("smb_Server() rwevent"));
6553 if ( GetLastError() == ERROR_ALREADY_EXISTS )
6554 afsi_log("Event Object Already Exists: %s", eventName);
6555 ncbp->ncb_command = NCBRECV | ASYNCH;
6556 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
6557 ncbp->ncb_lana_num = vcp->lana;
6558 ncbp->ncb_buffer = rwc.buf;
6559 ncbp->ncb_length = 65535;
6560 ncbp->ncb_event = rwevent;
6564 Netbios(ncbp, dos_ncb);
6566 rcode = thrd_WaitForSingleObject_Event(rwevent, RAWTIMEOUT);
6567 thrd_CloseHandle(rwevent);
6569 thrd_SetEvent(SessionEvents[idx_session]);
6571 smb_CompleteWriteRaw(vcp, bufp, outbufp, ncbp, &rwc);
6572 } else if (smbp->com == 0xa0) {
6574 * Serialize the handling for NT Transact
6577 smb_DispatchPacket(vcp, bufp, outbufp, ncbp, NULL);
6578 thrd_SetEvent(SessionEvents[idx_session]);
6580 thrd_SetEvent(SessionEvents[idx_session]);
6581 /* TODO: what else needs to be serialized? */
6582 smb_DispatchPacket(vcp, bufp, outbufp, ncbp, NULL);
6584 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
6586 __except( smb_ServerExceptionFilter() ) {
6590 smb_concurrentCalls--;
6593 thrd_SetEvent(NCBavails[idx_NCB]);
6600 * Exception filter for the server threads. If an exception occurs in the
6601 * dispatch routines, which is where exceptions are most common, then do a
6602 * force trace and give control to upstream exception handlers. Useful for
6605 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
6606 DWORD smb_ServerExceptionFilter(void) {
6607 /* While this is not the best time to do a trace, if it succeeds, then
6608 * we have a trace (assuming tracing was enabled). Otherwise, this should
6609 * throw a second exception.
6614 ptbuf[0] = "Unhandled exception forcing trace";
6616 h = RegisterEventSource(NULL,AFS_DAEMON_EVENT_NAME);
6618 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1001, NULL,1,0,ptbuf,NULL);
6619 DeregisterEventSource(h);
6622 afsd_ForceTrace(TRUE);
6623 return EXCEPTION_CONTINUE_SEARCH;
6628 * Create a new NCB and associated events, packet buffer, and "space" buffer.
6629 * If the number of server threads is M, and the number of live sessions is
6630 * N, then the number of NCB's in use at any time either waiting for, or
6631 * holding, received messages is M + N, so that is how many NCB's get created.
6633 void InitNCBslot(int idx)
6635 struct smb_packet *bufp;
6636 EVENT_HANDLE retHandle;
6638 char eventName[MAX_PATH];
6640 osi_assert( idx < (sizeof(NCBs) / sizeof(NCBs[0])) );
6642 NCBs[idx] = GetNCB();
6643 sprintf(eventName,"NCBavails[%d]", idx);
6644 NCBavails[idx] = thrd_CreateEvent(NULL, FALSE, TRUE, eventName);
6645 if ( GetLastError() == ERROR_ALREADY_EXISTS )
6646 afsi_log("Event Object Already Exists: %s", eventName);
6648 sprintf(eventName,"NCBevents[%d]", idx);
6649 NCBevents[idx] = thrd_CreateEvent(NULL, TRUE, FALSE, eventName);
6650 if ( GetLastError() == ERROR_ALREADY_EXISTS )
6651 afsi_log("Event Object Already Exists: %s", eventName);
6653 sprintf(eventName,"NCBReturns[0<=i<smb_NumServerThreads][%d]", idx);
6654 retHandle = thrd_CreateEvent(NULL, FALSE, FALSE, eventName);
6655 if ( GetLastError() == ERROR_ALREADY_EXISTS )
6656 afsi_log("Event Object Already Exists: %s", eventName);
6657 for (i=0; i<smb_NumServerThreads; i++)
6658 NCBreturns[i][idx] = retHandle;
6660 bufp->spacep = cm_GetSpace();
6664 /* listen for new connections */
6665 void smb_Listener(void *parmp)
6673 char rname[NCBNAMSZ+1];
6674 char cname[MAX_COMPUTERNAME_LENGTH+1];
6675 int cnamelen = MAX_COMPUTERNAME_LENGTH+1;
6680 int lana = (int) parmp;
6684 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
6687 /* retrieve computer name */
6688 GetComputerName(cname, &cnamelen);
6692 memset(ncbp, 0, sizeof(NCB));
6696 /* check for demo expiration */
6698 unsigned long tod = time((void *) 0);
6699 if (tod > EXPIREDATE) {
6700 (*smb_MBfunc)(NULL, "AFS demo expiration",
6702 MB_OK|MB_ICONSTOP|MB_SETFOREGROUND|MB_SERVICE_NOTIFICATION);
6710 #endif /* !NOEXPIRE */
6712 ncbp->ncb_command = NCBLISTEN;
6713 ncbp->ncb_rto = 0; /* No receive timeout */
6714 ncbp->ncb_sto = 0; /* No send timeout */
6716 /* pad out with spaces instead of null termination */
6717 len = strlen(smb_localNamep);
6718 strncpy(ncbp->ncb_name, smb_localNamep, NCBNAMSZ);
6719 for(i=len; i<NCBNAMSZ; i++) ncbp->ncb_name[i] = ' ';
6721 strcpy(ncbp->ncb_callname, "*");
6722 for(i=1; i<NCBNAMSZ; i++) ncbp->ncb_callname[i] = ' ';
6724 ncbp->ncb_lana_num = lana;
6727 code = Netbios(ncbp);
6729 code = Netbios(ncbp, dos_ncb);
6738 /* terminate silently if shutdown flag is set */
6739 if (smbShutdownFlag == 1) {
6748 "NCBLISTEN lana=%d failed with code %d",
6749 ncbp->ncb_lana_num, code);
6751 "Client exiting due to network failure. Please restart client.\n");
6755 "Client exiting due to network failure. Please restart client.\n"
6756 "NCBLISTEN lana=%d failed with code %d",
6757 ncbp->ncb_lana_num, code);
6759 code = (*smb_MBfunc)(NULL, tbuffer, "AFS Client Service: Fatal Error",
6760 MB_OK|MB_SERVICE_NOTIFICATION);
6761 osi_assert(tbuffer);
6764 fprintf(stderr, "NCBLISTEN lana=%d failed with code %d\n",
6765 ncbp->ncb_lana_num, code);
6766 fprintf(stderr, "\nClient exiting due to network failure "
6767 "(possibly due to power-saving mode)\n");
6768 fprintf(stderr, "Please restart client.\n");
6769 afs_exit(AFS_EXITCODE_NETWORK_FAILURE);
6773 /* check for remote conns */
6774 /* first get remote name and insert null terminator */
6775 memcpy(rname, ncbp->ncb_callname, NCBNAMSZ);
6776 for (i=NCBNAMSZ; i>0; i--) {
6777 if (rname[i-1] != ' ' && rname[i-1] != 0) {
6783 /* compare with local name */
6785 if (strncmp(rname, cname, NCBNAMSZ) != 0)
6786 flags |= SMB_VCFLAG_REMOTECONN;
6788 osi_Log1(smb_logp, "New session lsn %d", ncbp->ncb_lsn);
6790 lock_ObtainMutex(&smb_ListenerLock);
6792 /* New generation */
6795 /* Log session startup */
6797 fprintf(stderr, "New session(ncb_lsn,ncb_lana_num) %d,%d starting from host "
6799 ncbp->ncb_lsn,ncbp->ncb_lana_num, rname);
6801 afsi_log("New session(ncb_lsn,ncb_lana_num) (%d,%d) starting from host %s, %d ongoing ops",
6802 ncbp->ncb_lsn,ncbp->ncb_lana_num, rname, ongoingOps);
6804 if (reportSessionStartups) {
6810 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
6811 sprintf(s, "SMB session startup, %d ongoing ops", ongoingOps);
6813 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1004, NULL,
6815 DeregisterEventSource(h);
6817 afsi_log("NCBLISTEN completed, call from %s",rname);
6818 osi_Log1(smb_logp, "SMB session startup, %d ongoing ops",
6821 fprintf(stderr, "%s: New session %d starting from host %s\n",
6822 asctime(localtime(&now)), ncbp->ncb_lsn, rname);
6827 /* now ncbp->ncb_lsn is the connection ID */
6828 vcp = smb_FindVC(ncbp->ncb_lsn, SMB_FLAG_CREATE, ncbp->ncb_lana_num);
6829 vcp->flags |= flags;
6830 strcpy(vcp->rname, rname);
6832 /* Allocate slot in session arrays */
6833 /* Re-use dead session if possible, otherwise add one more */
6834 /* But don't look at session[0], it is reserved */
6835 for (i = 1; i < numSessions; i++) {
6836 if (dead_sessions[i]) {
6837 afsi_log("connecting to dead session [ %d ]", i);
6838 dead_sessions[i] = FALSE;
6843 /* assert that we do not exceed the maximum number of sessions or NCBs.
6844 * we should probably want to wait for a session to be freed in case
6848 osi_assert(i < Sessionmax - 1);
6849 osi_assert(numNCBs < NCBmax - 1); /* if we pass this test we can allocate one more */
6851 LSNs[i] = ncbp->ncb_lsn;
6852 lanas[i] = ncbp->ncb_lana_num;
6854 if (i == numSessions) {
6855 /* Add new NCB for new session */
6856 char eventName[MAX_PATH];
6858 osi_Log1(smb_logp, "smb_Listener creating new session %d", i);
6860 InitNCBslot(numNCBs);
6862 thrd_SetEvent(NCBavails[0]);
6863 thrd_SetEvent(NCBevents[0]);
6864 for (j = 0; j < smb_NumServerThreads; j++)
6865 thrd_SetEvent(NCBreturns[j][0]);
6866 /* Also add new session event */
6867 sprintf(eventName, "SessionEvents[%d]", i);
6868 SessionEvents[i] = thrd_CreateEvent(NULL, FALSE, TRUE, eventName);
6869 if ( GetLastError() == ERROR_ALREADY_EXISTS )
6870 afsi_log("Event Object Already Exists: %s", eventName);
6872 afsi_log("increasing numNCBs [ %d ] numSessions [ %d ]", numNCBs, numSessions);
6873 thrd_SetEvent(SessionEvents[0]);
6875 thrd_SetEvent(SessionEvents[i]);
6878 lock_ReleaseMutex(&smb_ListenerLock);
6880 } /* dispatch while loop */
6883 /* initialize Netbios */
6884 void smb_NetbiosInit()
6890 int i, lana, code, l;
6892 int delname_tried=0;
6895 OSVERSIONINFO Version;
6897 /* AFAIK, this is the default for the ms loopback adapter.*/
6898 unsigned char kWLA_MAC[6] = { 0x02, 0x00, 0x4c, 0x4f, 0x4f, 0x50 };
6899 /*******************************************************************/
6901 /* Get the version of Windows */
6902 memset(&Version, 0x00, sizeof(Version));
6903 Version.dwOSVersionInfoSize = sizeof(Version);
6904 GetVersionEx(&Version);
6906 /* setup the NCB system */
6909 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
6913 if (smb_LANadapter == -1) {
6914 ncbp->ncb_command = NCBENUM;
6915 ncbp->ncb_buffer = (PUCHAR)&lana_list;
6916 ncbp->ncb_length = sizeof(lana_list);
6917 code = Netbios(ncbp);
6919 sprintf(s, "Netbios NCBENUM error code %d", code);
6921 osi_panic(s, __FILE__, __LINE__);
6925 lana_list.length = 1;
6926 lana_list.lana[0] = smb_LANadapter;
6929 for (i = 0; i < lana_list.length; i++) {
6930 /* reset the adaptor: in Win32, this is required for every process, and
6931 * acts as an init call, not as a real hardware reset.
6933 ncbp->ncb_command = NCBRESET;
6934 ncbp->ncb_callname[0] = 100;
6935 ncbp->ncb_callname[2] = 100;
6936 ncbp->ncb_lana_num = lana_list.lana[i];
6937 code = Netbios(ncbp);
6939 code = ncbp->ncb_retcode;
6941 sprintf(s, "Netbios NCBRESET lana %d error code %d", lana_list.lana[i], code);
6943 lana_list.lana[i] = 255; /* invalid lana */
6945 sprintf(s, "Netbios NCBRESET lana %d succeeded", lana_list.lana[i]);
6950 /* for DJGPP, there is no NCBENUM and NCBRESET is a real reset. so
6951 we will just fake the LANA list */
6952 if (smb_LANadapter == -1) {
6953 for (i = 0; i < 8; i++)
6954 lana_list.lana[i] = i;
6955 lana_list.length = 8;
6958 lana_list.length = 1;
6959 lana_list.lana[0] = smb_LANadapter;
6963 /* and declare our name so we can receive connections */
6964 memset(ncbp, 0, sizeof(*ncbp));
6965 len=lstrlen(smb_localNamep);
6966 memset(smb_sharename,' ',NCBNAMSZ);
6967 memcpy(smb_sharename,smb_localNamep,len);
6968 sprintf(s, "lana_list.length %d", lana_list.length);
6971 /* Keep the name so we can unregister it later */
6972 for (l = 0; l < lana_list.length; l++) {
6973 lana = lana_list.lana[l];
6975 ncbp->ncb_command = NCBADDNAME;
6976 ncbp->ncb_lana_num = lana;
6977 memcpy(ncbp->ncb_name,smb_sharename,NCBNAMSZ);
6979 code = Netbios(ncbp);
6981 code = Netbios(ncbp, dos_ncb);
6984 afsi_log("Netbios NCBADDNAME lana=%d code=%d retcode=%d complete=%d",
6985 lana, code, ncbp->ncb_retcode,ncbp->ncb_cmd_cplt);
6987 char name[NCBNAMSZ+1];
6989 memcpy(name,ncbp->ncb_name,NCBNAMSZ);
6990 afsi_log("Netbios NCBADDNAME added new name >%s<",name);
6993 if (code == 0) code = ncbp->ncb_retcode;
6995 afsi_log("Netbios NCBADDNAME succeeded on lana %d\n", lana);
6997 /* we only use one LANA with djgpp */
6998 lana_list.lana[0] = lana;
6999 lana_list.length = 1;
7003 sprintf(s, "Netbios NCBADDNAME lana %d error code %d", lana, code);
7005 if (code == NRC_BRIDGE) { /* invalid LANA num */
7006 lana_list.lana[l] = 255;
7009 else if (code == NRC_DUPNAME) {
7010 afsi_log("Name already exists; try to delete it");
7011 memset(ncbp, 0, sizeof(*ncbp));
7012 ncbp->ncb_command = NCBDELNAME;
7013 memcpy(ncbp->ncb_name,smb_sharename,NCBNAMSZ);
7014 ncbp->ncb_lana_num = lana;
7016 code = Netbios(ncbp);
7018 code = Netbios(ncbp, dos_ncb);
7020 if (code == 0) code = ncbp->ncb_retcode;
7022 sprintf(s, "Netbios NCBDELNAME lana %d error code %d\n", lana, code);
7025 if (code != 0 || delname_tried) {
7026 lana_list.lana[l] = 255;
7028 else if (code == 0) {
7029 if (!delname_tried) {
7037 sprintf(s, "Netbios NCBADDNAME lana %d error code %d", lana, code);
7039 lana_list.lana[l] = 255; /* invalid lana */
7040 osi_panic(s, __FILE__, __LINE__);
7044 lana_found = 1; /* at least one worked */
7051 osi_assert(lana_list.length >= 0);
7053 sprintf(s, "No valid LANA numbers found!");
7054 osi_panic(s, __FILE__, __LINE__);
7057 /* we're done with the NCB now */
7061 void smb_Init(osi_log_t *logp, char *snamep, int useV3, int LANadapt,
7078 EVENT_HANDLE retHandle;
7079 char eventName[MAX_PATH];
7082 smb_MBfunc = aMBfunc;
7086 /* check for demo expiration */
7088 unsigned long tod = time((void *) 0);
7089 if (tod > EXPIREDATE) {
7091 (*smb_MBfunc)(NULL, "AFS demo expiration", "afsd",
7092 MB_OK|MB_ICONSTOP|MB_SETFOREGROUND|MB_SERVICE_NOTIFICATION);
7095 fprintf(stderr, "AFS demo expiration\n");
7100 #endif /* !NOEXPIRE */
7103 smb_LANadapter = LANadapt;
7105 /* Initialize smb_localZero */
7106 myTime.tm_isdst = -1; /* compute whether on DST or not */
7107 myTime.tm_year = 70;
7113 smb_localZero = mktime(&myTime);
7115 /* Initialize kludge-GMT */
7116 smb_CalculateNowTZ();
7118 /* initialize the remote debugging log */
7121 /* remember the name */
7122 len = strlen(snamep);
7123 smb_localNamep = malloc(len+1);
7124 strcpy(smb_localNamep, snamep);
7125 afsi_log("smb_localNamep is >%s<", smb_localNamep);
7127 /* and the global lock */
7128 lock_InitializeRWLock(&smb_globalLock, "smb global lock");
7129 lock_InitializeRWLock(&smb_rctLock, "smb refct and tree struct lock");
7131 /* Raw I/O data structures */
7132 lock_InitializeMutex(&smb_RawBufLock, "smb raw buffer lock");
7134 lock_InitializeMutex(&smb_ListenerLock, "smb listener lock");
7136 /* 4 Raw I/O buffers */
7138 smb_RawBufs = calloc(65536,1);
7139 *((char **)smb_RawBufs) = NULL;
7140 for (i=0; i<3; i++) {
7141 char *rawBuf = calloc(65536,1);
7142 *((char **)rawBuf) = smb_RawBufs;
7143 smb_RawBufs = rawBuf;
7146 npar = 65536 >> 4; /* number of paragraphs */
7147 seg = __dpmi_allocate_dos_memory(npar, &smb_RawBufSel[0]);
7149 afsi_log("Cannot allocate %d paragraphs of DOS memory",
7151 osi_panic("",__FILE__,__LINE__);
7154 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
7157 smb_RawBufs = (seg * 16) + 0; /* DOS physical address */
7159 _farpokel(_dos_ds, smb_RawBufs, NULL);
7160 for (i=0; i<SMB_RAW_BUFS-1; i++) {
7161 npar = 65536 >> 4; /* number of paragraphs */
7162 seg = __dpmi_allocate_dos_memory(npar, &smb_RawBufSel[i+1]);
7164 afsi_log("Cannot allocate %d paragraphs of DOS memory",
7166 osi_panic("",__FILE__,__LINE__);
7169 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
7172 rawBuf = (seg * 16) + 0; /* DOS physical address */
7173 /*_farpokel(_dos_ds, smb_RawBufs, smb_RawBufs);*/
7174 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
7175 smb_RawBufs = rawBuf;
7179 /* global free lists */
7180 smb_ncbFreeListp = NULL;
7181 smb_packetFreeListp = NULL;
7185 /* Initialize listener and server structures */
7187 memset(dead_sessions, 0, sizeof(dead_sessions));
7188 sprintf(eventName, "SessionEvents[0]");
7189 SessionEvents[0] = thrd_CreateEvent(NULL, FALSE, FALSE, eventName);
7190 if ( GetLastError() == ERROR_ALREADY_EXISTS )
7191 afsi_log("Event Object Already Exists: %s", eventName);
7193 smb_NumServerThreads = nThreads;
7194 sprintf(eventName, "NCBavails[0]");
7195 NCBavails[0] = thrd_CreateEvent(NULL, FALSE, FALSE, eventName);
7196 if ( GetLastError() == ERROR_ALREADY_EXISTS )
7197 afsi_log("Event Object Already Exists: %s", eventName);
7198 sprintf(eventName, "NCBevents[0]");
7199 NCBevents[0] = thrd_CreateEvent(NULL, FALSE, FALSE, eventName);
7200 if ( GetLastError() == ERROR_ALREADY_EXISTS )
7201 afsi_log("Event Object Already Exists: %s", eventName);
7202 NCBreturns = malloc(nThreads * sizeof(EVENT_HANDLE *));
7203 sprintf(eventName, "NCBreturns[0<=i<smb_NumServerThreads][0]");
7204 retHandle = thrd_CreateEvent(NULL, FALSE, FALSE, eventName);
7205 if ( GetLastError() == ERROR_ALREADY_EXISTS )
7206 afsi_log("Event Object Already Exists: %s", eventName);
7207 for (i = 0; i < smb_NumServerThreads; i++) {
7208 NCBreturns[i] = malloc(NCBmax * sizeof(EVENT_HANDLE));
7209 NCBreturns[i][0] = retHandle;
7211 for (i = 1; i <= nThreads; i++)
7213 numNCBs = nThreads + 1;
7215 /* Initialize dispatch table */
7216 memset(&smb_dispatchTable, 0, sizeof(smb_dispatchTable));
7217 smb_dispatchTable[0x00].procp = smb_ReceiveCoreMakeDir;
7218 smb_dispatchTable[0x01].procp = smb_ReceiveCoreRemoveDir;
7219 smb_dispatchTable[0x02].procp = smb_ReceiveCoreOpen;
7220 smb_dispatchTable[0x03].procp = smb_ReceiveCoreCreate;
7221 smb_dispatchTable[0x04].procp = smb_ReceiveCoreClose;
7222 smb_dispatchTable[0x05].procp = smb_ReceiveCoreFlush;
7223 smb_dispatchTable[0x06].procp = smb_ReceiveCoreUnlink;
7224 smb_dispatchTable[0x07].procp = smb_ReceiveCoreRename;
7225 smb_dispatchTable[0x08].procp = smb_ReceiveCoreGetFileAttributes;
7226 smb_dispatchTable[0x09].procp = smb_ReceiveCoreSetFileAttributes;
7227 smb_dispatchTable[0x0a].procp = smb_ReceiveCoreRead;
7228 smb_dispatchTable[0x0b].procp = smb_ReceiveCoreWrite;
7229 smb_dispatchTable[0x0c].procp = smb_ReceiveCoreLockRecord;
7230 smb_dispatchTable[0x0d].procp = smb_ReceiveCoreUnlockRecord;
7231 smb_dispatchTable[0x0e].procp = smb_SendCoreBadOp; /* create temporary */
7232 smb_dispatchTable[0x0f].procp = smb_ReceiveCoreCreate;
7233 smb_dispatchTable[0x10].procp = smb_ReceiveCoreCheckPath;
7234 smb_dispatchTable[0x11].procp = smb_SendCoreBadOp; /* process exit */
7235 smb_dispatchTable[0x12].procp = smb_ReceiveCoreSeek;
7236 smb_dispatchTable[0x1a].procp = smb_ReceiveCoreReadRaw;
7237 /* Set NORESPONSE because smb_ReceiveCoreReadRaw() does the responses itself */
7238 smb_dispatchTable[0x1a].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7239 smb_dispatchTable[0x1d].procp = smb_ReceiveCoreWriteRawDummy;
7240 smb_dispatchTable[0x22].procp = smb_ReceiveV3SetAttributes;
7241 smb_dispatchTable[0x23].procp = smb_ReceiveV3GetAttributes;
7242 smb_dispatchTable[0x24].procp = smb_ReceiveV3LockingX;
7243 smb_dispatchTable[0x24].flags |= SMB_DISPATCHFLAG_CHAINED;
7244 smb_dispatchTable[0x25].procp = smb_ReceiveV3Trans;
7245 smb_dispatchTable[0x25].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7246 smb_dispatchTable[0x26].procp = smb_ReceiveV3Trans;
7247 smb_dispatchTable[0x26].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7248 smb_dispatchTable[0x29].procp = smb_SendCoreBadOp; /* copy file */
7249 smb_dispatchTable[0x2b].procp = smb_ReceiveCoreEcho;
7250 /* Set NORESPONSE because smb_ReceiveCoreEcho() does the responses itself */
7251 smb_dispatchTable[0x2b].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7252 smb_dispatchTable[0x2d].procp = smb_ReceiveV3OpenX;
7253 smb_dispatchTable[0x2d].flags |= SMB_DISPATCHFLAG_CHAINED;
7254 smb_dispatchTable[0x2e].procp = smb_ReceiveV3ReadX;
7255 smb_dispatchTable[0x2e].flags |= SMB_DISPATCHFLAG_CHAINED;
7256 smb_dispatchTable[0x32].procp = smb_ReceiveV3Tran2A; /* both are same */
7257 smb_dispatchTable[0x32].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7258 smb_dispatchTable[0x33].procp = smb_ReceiveV3Tran2A;
7259 smb_dispatchTable[0x33].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7260 smb_dispatchTable[0x34].procp = smb_ReceiveV3FindClose;
7261 smb_dispatchTable[0x35].procp = smb_ReceiveV3FindNotifyClose;
7262 smb_dispatchTable[0x70].procp = smb_ReceiveCoreTreeConnect;
7263 smb_dispatchTable[0x71].procp = smb_ReceiveCoreTreeDisconnect;
7264 smb_dispatchTable[0x72].procp = smb_ReceiveNegotiate;
7265 smb_dispatchTable[0x73].procp = smb_ReceiveV3SessionSetupX;
7266 smb_dispatchTable[0x73].flags |= SMB_DISPATCHFLAG_CHAINED;
7267 smb_dispatchTable[0x74].procp = smb_ReceiveV3UserLogoffX;
7268 smb_dispatchTable[0x74].flags |= SMB_DISPATCHFLAG_CHAINED;
7269 smb_dispatchTable[0x75].procp = smb_ReceiveV3TreeConnectX;
7270 smb_dispatchTable[0x75].flags |= SMB_DISPATCHFLAG_CHAINED;
7271 smb_dispatchTable[0x80].procp = smb_ReceiveCoreGetDiskAttributes;
7272 smb_dispatchTable[0x81].procp = smb_ReceiveCoreSearchDir;
7273 smb_dispatchTable[0xA0].procp = smb_ReceiveNTTransact;
7274 smb_dispatchTable[0xA2].procp = smb_ReceiveNTCreateX;
7275 smb_dispatchTable[0xA2].flags |= SMB_DISPATCHFLAG_CHAINED;
7276 smb_dispatchTable[0xA4].procp = smb_ReceiveNTCancel;
7277 smb_dispatchTable[0xA4].flags |= SMB_DISPATCHFLAG_NORESPONSE;
7278 smb_dispatchTable[0xc0].procp = smb_SendCoreBadOp;
7279 smb_dispatchTable[0xc1].procp = smb_SendCoreBadOp;
7280 smb_dispatchTable[0xc2].procp = smb_SendCoreBadOp;
7281 smb_dispatchTable[0xc3].procp = smb_SendCoreBadOp;
7282 for(i=0xd0; i<= 0xd7; i++) {
7283 smb_dispatchTable[i].procp = smb_SendCoreBadOp;
7286 /* setup tran 2 dispatch table */
7287 smb_tran2DispatchTable[0].procp = smb_ReceiveTran2Open;
7288 smb_tran2DispatchTable[1].procp = smb_ReceiveTran2SearchDir; /* FindFirst */
7289 smb_tran2DispatchTable[2].procp = smb_ReceiveTran2SearchDir; /* FindNext */
7290 smb_tran2DispatchTable[3].procp = smb_ReceiveTran2QFSInfo;
7291 smb_tran2DispatchTable[4].procp = smb_ReceiveTran2SetFSInfo;
7292 smb_tran2DispatchTable[5].procp = smb_ReceiveTran2QPathInfo;
7293 smb_tran2DispatchTable[6].procp = smb_ReceiveTran2SetPathInfo;
7294 smb_tran2DispatchTable[7].procp = smb_ReceiveTran2QFileInfo;
7295 smb_tran2DispatchTable[8].procp = smb_ReceiveTran2SetFileInfo;
7296 smb_tran2DispatchTable[9].procp = smb_ReceiveTran2FSCTL;
7297 smb_tran2DispatchTable[10].procp = smb_ReceiveTran2IOCTL;
7298 smb_tran2DispatchTable[11].procp = smb_ReceiveTran2FindNotifyFirst;
7299 smb_tran2DispatchTable[12].procp = smb_ReceiveTran2FindNotifyNext;
7300 smb_tran2DispatchTable[13].procp = smb_ReceiveTran2MKDir;
7302 /* setup the rap dispatch table */
7303 memset(smb_rapDispatchTable, 0, sizeof(smb_rapDispatchTable));
7304 smb_rapDispatchTable[0].procp = smb_ReceiveRAPNetShareEnum;
7305 smb_rapDispatchTable[1].procp = smb_ReceiveRAPNetShareGetInfo;
7306 smb_rapDispatchTable[63].procp = smb_ReceiveRAPNetWkstaGetInfo;
7307 smb_rapDispatchTable[13].procp = smb_ReceiveRAPNetServerGetInfo;
7311 /* if we are doing SMB authentication we have register outselves as a logon process */
7312 if (smb_authType != SMB_AUTH_NONE) {
7314 LSA_STRING afsProcessName;
7315 LSA_OPERATIONAL_MODE dummy; /*junk*/
7317 afsProcessName.Buffer = "OpenAFSClientDaemon";
7318 afsProcessName.Length = strlen(afsProcessName.Buffer);
7319 afsProcessName.MaximumLength = afsProcessName.Length + 1;
7321 nts = LsaRegisterLogonProcess(&afsProcessName, &smb_lsaHandle, &dummy);
7323 if (nts == STATUS_SUCCESS) {
7324 LSA_STRING packageName;
7325 /* we are registered. Find out the security package id */
7326 packageName.Buffer = MSV1_0_PACKAGE_NAME;
7327 packageName.Length = strlen(packageName.Buffer);
7328 packageName.MaximumLength = packageName.Length + 1;
7329 nts = LsaLookupAuthenticationPackage(smb_lsaHandle, &packageName , &smb_lsaSecPackage);
7330 if (nts == STATUS_SUCCESS) {
7331 smb_lsaLogonOrigin.Buffer = "OpenAFS";
7332 smb_lsaLogonOrigin.Length = strlen(smb_lsaLogonOrigin.Buffer);
7333 smb_lsaLogonOrigin.MaximumLength = smb_lsaLogonOrigin.Length + 1;
7335 afsi_log("Can't determine security package name for NTLM!! NTSTATUS=[%l]",nts);
7338 afsi_log("Can't register logon process!! NTSTATUS=[%l]",nts);
7341 if (nts != STATUS_SUCCESS) {
7342 /* something went wrong. We report the error and revert back to no authentication
7343 because we can't perform any auth requests without a successful lsa handle
7344 or sec package id. */
7345 afsi_log("Reverting to NO SMB AUTH");
7346 smb_authType = SMB_AUTH_NONE;
7347 } else if ( smb_authType == SMB_AUTH_EXTENDED) {
7348 /* Test to see if there is anything to negotiate. If SPNEGO is not going to be used
7349 * then the only option is NTLMSSP anyway; so just fallback.
7354 smb_NegotiateExtendedSecurity(&secBlob, &secBlobLength);
7355 if (secBlobLength == 0) {
7356 smb_authType = SMB_AUTH_NTLM;
7357 afsi_log("Reverting to SMB AUTH NTLM");
7365 /* Now get ourselves a domain name. */
7366 /* For now we are using the local computer name as the domain name.
7367 * It is actually the domain for local logins, and we are acting as
7368 * a local SMB server.
7370 bufsize = sizeof(smb_ServerDomainName) - 1;
7371 GetComputerName(smb_ServerDomainName, &bufsize);
7372 smb_ServerDomainNameLength = bufsize + 1; /* bufsize doesn't include terminator */
7373 afsi_log("Setting SMB server domain name to [%s]", smb_ServerDomainName);
7376 /* Start listeners, waiters, servers, and daemons */
7378 for (i = 0; i < lana_list.length; i++) {
7379 if (lana_list.lana[i] == 255) continue;
7380 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_Listener,
7381 (void*)lana_list.lana[i], 0, &lpid, "smb_Listener");
7382 osi_assert(phandle != NULL);
7383 thrd_CloseHandle(phandle);
7387 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_ClientWaiter,
7388 NULL, 0, &lpid, "smb_ClientWaiter");
7389 osi_assert(phandle != NULL);
7390 thrd_CloseHandle(phandle);
7393 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_ServerWaiter,
7394 NULL, 0, &lpid, "smb_ServerWaiter");
7395 osi_assert(phandle != NULL);
7396 thrd_CloseHandle(phandle);
7398 for (i=0; i<nThreads; i++) {
7399 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_Server,
7400 (void *) i, 0, &lpid, "smb_Server");
7401 osi_assert(phandle != NULL);
7402 thrd_CloseHandle(phandle);
7405 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_Daemon,
7406 NULL, 0, &lpid, "smb_Daemon");
7407 osi_assert(phandle != NULL);
7408 thrd_CloseHandle(phandle);
7410 phandle = thrd_Create(NULL, 65536, (ThreadFunc) smb_WaitingLocksDaemon,
7411 NULL, 0, &lpid, "smb_WaitingLocksDaemon");
7412 osi_assert(phandle != NULL);
7413 thrd_CloseHandle(phandle);
7422 void smb_Shutdown(void)
7431 /*fprintf(stderr, "Entering smb_Shutdown\n");*/
7433 /* setup the NCB system */
7436 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
7439 /* Block new sessions by setting shutdown flag */
7440 smbShutdownFlag = 1;
7442 /* Hang up all sessions */
7443 memset((char *)ncbp, 0, sizeof(NCB));
7444 for (i = 1; i < numSessions; i++)
7446 if (dead_sessions[i])
7449 /*fprintf(stderr, "NCBHANGUP session %d LSN %d\n", i, LSNs[i]);*/
7450 ncbp->ncb_command = NCBHANGUP;
7451 ncbp->ncb_lana_num = lanas[i]; /*smb_LANadapter;*/
7452 ncbp->ncb_lsn = LSNs[i];
7454 code = Netbios(ncbp);
7456 code = Netbios(ncbp, dos_ncb);
7458 /*fprintf(stderr, "returned from NCBHANGUP session %d LSN %d\n", i, LSNs[i]);*/
7459 if (code == 0) code = ncbp->ncb_retcode;
7461 osi_Log1(smb_logp, "Netbios NCBHANGUP error code %d", code);
7462 fprintf(stderr, "Session %d Netbios NCBHANGUP error code %d", i, code);
7466 /* Delete Netbios name */
7467 memset((char *)ncbp, 0, sizeof(NCB));
7468 for (i = 0; i < lana_list.length; i++) {
7469 if (lana_list.lana[i] == 255) continue;
7470 ncbp->ncb_command = NCBDELNAME;
7471 ncbp->ncb_lana_num = lana_list.lana[i];
7472 memcpy(ncbp->ncb_name,smb_sharename,NCBNAMSZ);
7474 code = Netbios(ncbp);
7476 code = Netbios(ncbp, dos_ncb);
7478 if (code == 0) code = ncbp->ncb_retcode;
7480 fprintf(stderr, "Netbios NCBDELNAME lana %d error code %d",
7481 ncbp->ncb_lana_num, code);
7487 /* Get the UNC \\<servername>\<sharename> prefix. */
7488 char *smb_GetSharename()
7492 /* Make sure we have been properly initialized. */
7493 if (smb_localNamep == NULL)
7496 /* Allocate space for \\<servername>\<sharename>, plus the
7499 name = malloc(strlen(smb_localNamep) + strlen("ALL") + 4);
7500 sprintf(name, "\\\\%s\\%s", smb_localNamep, "ALL");
7506 void smb_LogPacket(smb_packet_t *packet)
7509 unsigned length, paramlen, datalen, i, j;
7511 char hex[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
7515 osi_Log0(smb_logp, "*** SMB packet dump ***");
7517 vp = (BYTE *) packet->data;
7519 datalen = *((WORD*)(vp + (paramlen = ((unsigned)*(vp+20)) << 1)));
7520 length = paramlen + 2 + datalen;
7523 for(i=0;i < length; i+=16)
7525 memset( buf, ' ', 80 );
7530 buf[strlen(buf)] = ' ';
7532 cp = (BYTE*) buf + 7;
7534 for(j=0;j < 16 && (i+j)<length; j++)
7536 *(cp++) = hex[vp[i+j] >> 4];
7537 *(cp++) = hex[vp[i+j] & 0xf];
7547 for(j=0;j < 16 && (i+j)<length;j++)
7549 *(cp++) = ( 32 <= vp[i+j] && 128 > vp[i+j] )? vp[i+j]:'.';
7560 osi_Log0( smb_logp, buf );
7563 osi_Log0(smb_logp, "*** End SMB packet dump ***");
7567 #endif /* NOTSERVICE */
7569 int smb_DumpVCP(FILE *outputFile, char *cookie)
7576 lock_ObtainRead(&smb_rctLock);
7578 sprintf(output, "begin dumping vcpsp\n");
7579 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7581 for(vcp = smb_allVCsp; vcp; vcp=vcp->nextp)
7585 sprintf(output, "%s vcp=0x%08X, refCount=%d, flags=%d, vcID=%d, lsn=%d, uidCounter=%d, tidCounter=%d, fidCounter=%d\n",
7586 cookie, vcp, vcp->refCount, vcp->flags, vcp->vcID, vcp->lsn, vcp->uidCounter, vcp->tidCounter, vcp->fidCounter);
7587 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7589 sprintf(output, "begin dumping fidsp\n");
7590 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7592 for (fidp = vcp->fidsp; fidp; fidp = (smb_fid_t *) osi_QNext(&fidp->q))
7594 sprintf(output, "%s -- fidp=0x%08X, refCount=%d, fid=%d, vcp=0x%08X, scp=0x%08X, ioctlp=0x%08X, NTopen_pathp=%s, NTopen_wholepathp=%s\n",
7595 cookie, fidp, fidp->refCount, fidp->fid, fidp->vcp, fidp->scp, fidp->ioctlp,
7596 fidp->NTopen_pathp ? fidp->NTopen_pathp : "NULL",
7597 fidp->NTopen_wholepathp ? fidp->NTopen_wholepathp : "NULL");
7598 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7601 sprintf(output, "done dumping fidsp\n");
7602 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7605 sprintf(output, "done dumping vcpsp\n");
7606 WriteFile(outputFile, output, strlen(output), &zilch, NULL);
7608 lock_ReleaseRead(&smb_rctLock);