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 #define NOMOREFILESFIX 1
14 #include <afs/param.h>
20 #include <sys/timeb.h>
35 #include <winnt/osi_malloc.h>
37 /* These characters are illegal in Windows filenames */
38 static char *illegalChars = "\\/:*?\"<>|";
39 BOOL isWindows2000 = FALSE;
41 smb_vc_t *dead_vcp = NULL;
42 smb_vc_t *active_vcp = NULL;
44 char *loggedOutName = NULL;
45 smb_user_t *loggedOutUserp = NULL;
46 unsigned long loggedOutTime;
49 int smbShutdownFlag = 0;
52 int smb_LogoffTokenTransfer;
53 unsigned long smb_LogoffTransferTimeout;
55 DWORD last_msg_time = 0;
59 unsigned int sessionGen = 0;
63 osi_hyper_t hzero = {0, 0};
64 osi_hyper_t hones = {0xFFFFFFFF, -1};
67 osi_rwlock_t smb_globalLock;
68 osi_rwlock_t smb_rctLock;
69 osi_rwlock_t smb_ListenerLock;
72 unsigned char smb_sharename[NCBNAMSZ+1] = {0};
75 long smb_maxObsConcurrentCalls=0;
76 long smb_concurrentCalls=0;
78 smb_dispatch_t smb_dispatchTable[SMB_NOPCODES];
80 smb_packet_t *smb_packetFreeListp;
81 smb_ncb_t *smb_ncbFreeListp;
83 int smb_NumServerThreads;
85 int numNCBs, numSessions;
88 EVENT_HANDLE NCBavails[NCBmax], NCBevents[NCBmax];
89 EVENT_HANDLE **NCBreturns;
90 DWORD NCBsessions[NCBmax];
92 struct smb_packet *bufs[NCBmax];
94 #define Sessionmax 100
95 EVENT_HANDLE SessionEvents[Sessionmax];
96 unsigned short LSNs[Sessionmax];
97 int lanas[Sessionmax];
98 BOOL dead_sessions[Sessionmax];
102 osi_mutex_t smb_RawBufLock;
104 #define SMB_RAW_BUFS 4
106 int smb_RawBufSel[SMB_RAW_BUFS];
111 #define RAWTIMEOUT INFINITE
114 typedef struct raw_write_cont {
127 /* dir search stuff */
128 long smb_dirSearchCounter = 1;
129 smb_dirSearch_t *smb_firstDirSearchp;
130 smb_dirSearch_t *smb_lastDirSearchp;
132 /* global state about V3 protocols */
133 int smb_useV3; /* try to negotiate V3 */
136 /* MessageBox or something like it */
137 int (WINAPI *smb_MBfunc)(HWND, LPCTSTR, LPCTSTR, UINT) = NULL;
141 * Time in Unix format of midnight, 1/1/1970 local time.
142 * When added to dosUTime, gives Unix (AFS) time.
146 /* Time difference for converting to kludge-GMT */
149 char *smb_localNamep;
151 smb_vc_t *smb_allVCsp;
153 smb_username_t *usernamesp = NULL;
155 smb_waitingLock_t *smb_allWaitingLocks;
158 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
159 NCB *ncbp, raw_write_cont_t *rwcp);
160 void smb_NetbiosInit();
161 extern char cm_HostName[];
163 extern char cm_confDir[];
167 #define LPTSTR char *
168 #define GetComputerName(str, sizep) \
169 strcpy((str), cm_HostName); \
170 *(sizep) = strlen(cm_HostName)
176 * To build an expiring version, comment out the definition of NOEXPIRE,
177 * and set the definition of EXPIREDATE to the desired value.
180 #define EXPIREDATE 834000000 /* Wed Jun 5 1996 */
183 char * myCrt_Dispatch(int i)
188 return "unknown SMB op";
190 return "(00)ReceiveCoreMakeDir";
192 return "(01)ReceiveCoreRemoveDir";
194 return "(02)ReceiveCoreOpen";
196 return "(03)ReceiveCoreCreate";
198 return "(04)ReceiveCoreClose";
200 return "(05)ReceiveCoreFlush";
202 return "(06)ReceiveCoreUnlink";
204 return "(07)ReceiveCoreRename";
206 return "(08)ReceiveCoreGetFileAttributes";
208 return "(09)ReceiveCoreSetFileAttributes";
210 return "(0a)ReceiveCoreRead";
212 return "(0b)ReceiveCoreWrite";
214 return "(0c)ReceiveCoreLockRecord";
216 return "(0d)ReceiveCoreUnlockRecord";
218 return "(0e)SendCoreBadOp";
220 return "(0f)ReceiveCoreCreate";
222 return "(10)ReceiveCoreCheckPath";
224 return "(11)SendCoreBadOp";
226 return "(12)ReceiveCoreSeek";
228 return "(1a)ReceiveCoreReadRaw";
230 return "(1d)ReceiveCoreWriteRawDummy";
232 return "(22)ReceiveV3SetAttributes";
234 return "(23)ReceiveV3GetAttributes";
236 return "(24)ReceiveV3LockingX";
238 return "(29)SendCoreBadOp";
240 return "(2b)ReceiveCoreEcho";
242 return "(2d)ReceiveV3OpenX";
244 return "(2e)ReceiveV3ReadX";
246 return "(32)ReceiveV3Tran2A";
248 return "(33)ReceiveV3Tran2A";
250 return "(34)ReceiveV3FindClose";
252 return "(35)ReceiveV3FindNotifyClose";
254 return "(70)ReceiveCoreTreeConnect";
256 return "(71)ReceiveCoreTreeDisconnect";
258 return "(72)ReceiveNegotiate";
260 return "(73)ReceiveV3SessionSetupX";
262 return "(74)ReceiveV3UserLogoffX";
264 return "(75)ReceiveV3TreeConnectX";
266 return "(80)ReceiveCoreGetDiskAttributes";
268 return "(81)ReceiveCoreSearchDir";
270 return "(A0)ReceiveNTTransact";
272 return "(A2)ReceiveNTCreateX";
274 return "(A4)ReceiveNTCancel";
276 return "(c0)SendCoreBadOp";
278 return "(c1)SendCoreBadOp";
280 return "(c2)SendCoreBadOp";
282 return "(c3)SendCoreBadOp";
286 char * myCrt_2Dispatch(int i)
291 return "unknown SMB op-2";
293 return "S(00)CreateFile";
295 return "S(01)FindFirst";
297 return "S(02)FindNext"; /* FindNext */
299 return "S(03)QueryFileSystem_ReceiveTran2QFSInfo";
303 return "S(05)QueryFileInfo_ReceiveTran2QPathInfo";
305 return "S(06)SetFileInfo_ReceiveTran2SetPathInfo";
307 return "S(07)SetInfoHandle_ReceiveTran2QFileInfo";
309 return "S(08)??_ReceiveTran2SetFileInfo";
311 return "S(09)??_ReceiveTran2FSCTL";
313 return "S(0a)_ReceiveTran2IOCTL";
315 return "S(0b)_ReceiveTran2FindNotifyFirst";
317 return "S(0c)_ReceiveTran2FindNotifyNext";
319 return "S(0d)CreateDirectory_ReceiveTran2MKDir";
323 /* scache must be locked */
324 unsigned int smb_Attributes(cm_scache_t *scp)
328 if (scp->fileType == CM_SCACHETYPE_DIRECTORY
329 || scp->fileType == CM_SCACHETYPE_MOUNTPOINT)
335 * We used to mark a file RO if it was in an RO volume, but that
336 * turns out to be impolitic in NT. See defect 10007.
339 if ((scp->unixModeBits & 0222) == 0 || (scp->flags & CM_SCACHEFLAG_RO))
341 if ((scp->unixModeBits & 0222) == 0)
342 attrs |= 1; /* turn on read-only flag */
347 static int ExtractBits(WORD bits, short start, short len)
354 num = bits << (16 - end);
355 num = num >> ((16 - end) + start);
361 void ShowUnixTime(char *FuncName, long unixTime)
366 smb_LargeSearchTimeFromUnixTime(&ft, unixTime);
368 if (!FileTimeToDosDateTime(&ft, &wDate, &wTime))
369 osi_Log1(afsd_logp, "Failed to convert filetime to dos datetime: %d", GetLastError());
371 int day, month, year, sec, min, hour;
374 day = ExtractBits(wDate, 0, 5);
375 month = ExtractBits(wDate, 5, 4);
376 year = ExtractBits(wDate, 9, 7) + 1980;
378 sec = ExtractBits(wTime, 0, 5);
379 min = ExtractBits(wTime, 5, 6);
380 hour = ExtractBits(wTime, 11, 5);
382 sprintf(msg, "%s = %02d-%02d-%04d %02d:%02d:%02d", FuncName, month, day, year, hour, min, sec);
383 osi_Log1(afsd_logp, "%s", osi_LogSaveString(afsd_logp, msg));
389 /* Determine if we are observing daylight savings time */
390 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
392 TIME_ZONE_INFORMATION timeZoneInformation;
393 SYSTEMTIME utc, local, localDST;
395 /* Get the time zone info. NT uses this to calc if we are in DST. */
396 GetTimeZoneInformation(&timeZoneInformation);
398 /* Return the daylight bias */
399 *pDstBias = timeZoneInformation.DaylightBias;
401 /* Return the bias */
402 *pBias = timeZoneInformation.Bias;
404 /* Now determine if DST is being observed */
406 /* Get the UTC (GMT) time */
409 /* Convert UTC time to local time using the time zone info. If we are
410 observing DST, the calculated local time will include this.
412 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &localDST);
414 /* Set the daylight bias to 0. The daylight bias is the amount of change
415 in time that we use for daylight savings time. By setting this to 0
416 we cause there to be no change in time during daylight savings time.
418 timeZoneInformation.DaylightBias = 0;
420 /* Convert the utc time to local time again, but this time without any
421 adjustment for daylight savings time.
423 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &local);
425 /* If the two times are different, then it means that the localDST that
426 we calculated includes the daylight bias, and therefore we are
427 observing daylight savings time.
429 *pDST = localDST.wHour != local.wHour;
432 /* Determine if we are observing daylight savings time */
433 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
439 *pDstBias = -60; /* where can this be different? */
445 void CompensateForSmbClientLastWriteTimeBugs(long *pLastWriteTime)
447 BOOL dst; /* Will be TRUE if observing DST */
448 LONG dstBias; /* Offset from local time if observing DST */
449 LONG bias; /* Offset from GMT for local time */
452 * This function will adjust the last write time to compensate
453 * for two bugs in the smb client:
455 * 1) During Daylight Savings Time, the LastWriteTime is ahead
456 * in time by the DaylightBias (ignoring the sign - the
457 * DaylightBias is always stored as a negative number). If
458 * the DaylightBias is -60, then the LastWriteTime will be
459 * ahead by 60 minutes.
461 * 2) If the local time zone is a positive offset from GMT, then
462 * the LastWriteTime will be the correct local time plus the
463 * Bias (ignoring the sign - a positive offset from GMT is
464 * always stored as a negative Bias). If the Bias is -120,
465 * then the LastWriteTime will be ahead by 120 minutes.
467 * These bugs can occur at the same time.
470 GetTimeZoneInfo(&dst, &dstBias, &bias);
472 /* First adjust for DST */
474 *pLastWriteTime -= (-dstBias * 60); /* Convert dstBias to seconds */
476 /* Now adjust for a positive offset from GMT (a negative bias). */
478 *pLastWriteTime -= (-bias * 60); /* Convert bias to seconds */
482 * Calculate the difference (in seconds) between local time and GMT.
483 * This enables us to convert file times to kludge-GMT.
489 struct tm gmt_tm, local_tm;
490 int days, hours, minutes, seconds;
493 gmt_tm = *(gmtime(&t));
494 local_tm = *(localtime(&t));
496 days = local_tm.tm_yday - gmt_tm.tm_yday;
497 hours = 24 * days + local_tm.tm_hour - gmt_tm.tm_hour;
498 minutes = 60 * hours + local_tm.tm_min - gmt_tm.tm_min;
499 seconds = 60 * minutes + local_tm.tm_sec - gmt_tm.tm_sec;
505 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
510 long ersatz_unixTime;
513 * Must use kludge-GMT instead of real GMT.
514 * kludge-GMT is computed by adding time zone difference to localtime.
517 * ltp = gmtime(&unixTime);
519 ersatz_unixTime = unixTime - smb_NowTZ;
520 ltp = localtime(&ersatz_unixTime);
522 /* if we fail, make up something */
525 localJunk.tm_year = 89 - 20;
526 localJunk.tm_mon = 4;
527 localJunk.tm_mday = 12;
528 localJunk.tm_hour = 0;
529 localJunk.tm_min = 0;
530 localJunk.tm_sec = 0;
533 stm.wYear = ltp->tm_year + 1900;
534 stm.wMonth = ltp->tm_mon + 1;
535 stm.wDayOfWeek = ltp->tm_wday;
536 stm.wDay = ltp->tm_mday;
537 stm.wHour = ltp->tm_hour;
538 stm.wMinute = ltp->tm_min;
539 stm.wSecond = ltp->tm_sec;
540 stm.wMilliseconds = 0;
542 SystemTimeToFileTime(&stm, largeTimep);
545 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
547 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
548 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 ??? */
549 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
551 int leap_years = 89; /* leap years betw 1/1/1601 and 1/1/1970 */
553 /* set ft to number of 100ns intervals betw 1/1/1601 and 1/1/1970 GMT */
554 *ft = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years)
556 *ft = LargeIntegerMultiplyByLong(*ft, 60);
557 *ft = LargeIntegerMultiplyByLong(*ft, 10000000);
560 ut = ConvertLongToLargeInteger(unixTime);
561 ut = LargeIntegerMultiplyByLong(ut, 10000000);
562 *ft = LargeIntegerAdd(*ft, ut);
567 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
573 FileTimeToSystemTime(largeTimep, &stm);
575 lt.tm_year = stm.wYear - 1900;
576 lt.tm_mon = stm.wMonth - 1;
577 lt.tm_wday = stm.wDayOfWeek;
578 lt.tm_mday = stm.wDay;
579 lt.tm_hour = stm.wHour;
580 lt.tm_min = stm.wMinute;
581 lt.tm_sec = stm.wSecond;
584 save_timezone = _timezone;
585 _timezone += smb_NowTZ;
586 *unixTimep = mktime(<);
587 _timezone = save_timezone;
590 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
592 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
593 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 GMT? */
594 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
598 /* set to number of 100ns intervals betw 1/1/1601 and 1/1/1970 */
599 a = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years) * 24 * 60
601 a = LargeIntegerMultiplyByLong(a, 60);
602 a = LargeIntegerMultiplyByLong(a, 10000000);
604 /* subtract it from ft */
605 a = LargeIntegerSubtract(*ft, a);
607 /* divide down to seconds */
608 *unixTimep = LargeIntegerDivideByLong(a, 10000000);
612 void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime)
619 ltp = localtime((time_t*) &unixTime);
621 /* if we fail, make up something */
624 localJunk.tm_year = 89 - 20;
625 localJunk.tm_mon = 4;
626 localJunk.tm_mday = 12;
627 localJunk.tm_hour = 0;
628 localJunk.tm_min = 0;
629 localJunk.tm_sec = 0;
632 dosDate = ((ltp->tm_year-80)<<9) | ((ltp->tm_mon+1) << 5) | (ltp->tm_mday);
633 dosTime = (ltp->tm_hour<<11) | (ltp->tm_min << 5) | (ltp->tm_sec / 2);
634 *dosTimep = (dosDate<<16) | dosTime;
637 void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime)
639 unsigned short dosDate;
640 unsigned short dosTime;
643 dosDate = searchTime & 0xffff;
644 dosTime = (searchTime >> 16) & 0xffff;
646 localTm.tm_year = 80 + ((dosDate>>9) & 0x3f);
647 localTm.tm_mon = ((dosDate >> 5) & 0xf) - 1; /* January is 0 in localTm */
648 localTm.tm_mday = (dosDate) & 0x1f;
649 localTm.tm_hour = (dosTime>>11) & 0x1f;
650 localTm.tm_min = (dosTime >> 5) & 0x3f;
651 localTm.tm_sec = (dosTime & 0x1f) * 2;
652 localTm.tm_isdst = -1; /* compute whether DST in effect */
654 *unixTimep = mktime(&localTm);
657 void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime)
659 *dosUTimep = unixTime - smb_localZero;
662 void smb_UnixTimeFromDosUTime(long *unixTimep, long dosTime)
665 *unixTimep = dosTime + smb_localZero;
667 /* dosTime seems to be already adjusted for GMT */
668 *unixTimep = dosTime;
672 smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana)
676 lock_ObtainWrite(&smb_rctLock);
677 for(vcp = smb_allVCsp; vcp; vcp=vcp->nextp) {
678 if (lsn == vcp->lsn && lana == vcp->lana) {
683 if (!vcp && (flags & SMB_FLAG_CREATE)) {
684 vcp = malloc(sizeof(*vcp));
685 memset(vcp, 0, sizeof(*vcp));
689 vcp->uidCounter = 1; /* UID 0 is reserved for blank user */
690 vcp->nextp = smb_allVCsp;
692 lock_InitializeMutex(&vcp->mx, "vc_t mutex");
696 lock_ReleaseWrite(&smb_rctLock);
700 int smb_IsStarMask(char *maskp)
705 for(i=0; i<11; i++) {
707 if (tc == '?' || tc == '*' || tc == '>') return 1;
712 void smb_ReleaseVC(smb_vc_t *vcp)
714 lock_ObtainWrite(&smb_rctLock);
715 osi_assert(vcp->refCount-- > 0);
716 lock_ReleaseWrite(&smb_rctLock);
719 void smb_HoldVC(smb_vc_t *vcp)
721 lock_ObtainWrite(&smb_rctLock);
723 lock_ReleaseWrite(&smb_rctLock);
726 smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags)
730 lock_ObtainWrite(&smb_rctLock);
731 for(tidp = vcp->tidsp; tidp; tidp = tidp->nextp) {
732 if (tid == tidp->tid) {
737 if (!tidp && (flags & SMB_FLAG_CREATE)) {
738 tidp = malloc(sizeof(*tidp));
739 memset(tidp, 0, sizeof(*tidp));
740 tidp->nextp = vcp->tidsp;
744 lock_InitializeMutex(&tidp->mx, "tid_t mutex");
747 lock_ReleaseWrite(&smb_rctLock);
751 void smb_ReleaseTID(smb_tid_t *tidp)
758 lock_ObtainWrite(&smb_rctLock);
759 osi_assert(tidp->refCount-- > 0);
760 if (tidp->refCount == 0 && (tidp->flags & SMB_TIDFLAG_DELETE)) {
761 ltpp = &tidp->vcp->tidsp;
762 for(tp = *ltpp; tp; ltpp = &tp->nextp, tp = *ltpp) {
763 if (tp == tidp) break;
765 osi_assert(tp != NULL);
767 lock_FinalizeMutex(&tidp->mx);
768 userp = tidp->userp; /* remember to drop ref later */
770 lock_ReleaseWrite(&smb_rctLock);
772 cm_ReleaseUser(userp);
776 smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags)
778 smb_user_t *uidp = NULL;
780 lock_ObtainWrite(&smb_rctLock);
781 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
782 if (uid == uidp->userID) {
784 osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL," VCP[%x] found-uid[%d] name[%s]",vcp,uidp->userID,(uidp->unp) ? uidp->unp->name : "");
788 if (!uidp && (flags & SMB_FLAG_CREATE)) {
789 uidp = malloc(sizeof(*uidp));
790 memset(uidp, 0, sizeof(*uidp));
791 uidp->nextp = vcp->usersp;
795 lock_InitializeMutex(&uidp->mx, "uid_t mutex");
797 osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL,"VCP[%x] new-uid[%d] name[%s]",vcp,uidp->userID,(uidp->unp ? uidp->unp->name : ""));
799 lock_ReleaseWrite(&smb_rctLock);
803 smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags)
805 smb_username_t *unp= NULL;
807 lock_ObtainWrite(&smb_rctLock);
808 for(unp = usernamesp; unp; unp = unp->nextp) {
809 if (stricmp(unp->name, usern) == 0 &&
810 stricmp(unp->machine, machine) == 0) {
815 if (!unp && (flags & SMB_FLAG_CREATE)) {
816 unp = malloc(sizeof(*unp));
817 memset(unp, 0, sizeof(*unp));
818 unp->nextp = usernamesp;
819 unp->name = strdup(usern);
820 unp->machine = strdup(machine);
822 lock_InitializeMutex(&unp->mx, "username_t mutex");
824 lock_ReleaseWrite(&smb_rctLock);
828 smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern)
830 smb_user_t *uidp= NULL;
832 lock_ObtainWrite(&smb_rctLock);
833 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
836 if (stricmp(uidp->unp->name, usern) == 0) {
838 osi_LogEvent("AFS smb_FindUserByNameThisSession",NULL,"VCP[%x] uid[%d] match-name[%s]",vcp,uidp->userID,usern);
843 lock_ReleaseWrite(&smb_rctLock);
846 void smb_ReleaseUID(smb_user_t *uidp)
853 lock_ObtainWrite(&smb_rctLock);
854 osi_assert(uidp->refCount-- > 0);
855 if (uidp->refCount == 0 && (uidp->flags & SMB_USERFLAG_DELETE)) {
856 lupp = &uidp->vcp->usersp;
857 for(up = *lupp; up; lupp = &up->nextp, up = *lupp) {
858 if (up == uidp) break;
860 osi_assert(up != NULL);
862 lock_FinalizeMutex(&uidp->mx);
864 userp = uidp->unp->userp; /* remember to drop ref later */
866 lock_ReleaseWrite(&smb_rctLock);
868 cm_ReleaseUserVCRef(userp);
869 cm_ReleaseUser(userp);
873 /* retrieve a held reference to a user structure corresponding to an incoming
875 * corresponding release function is cm_ReleaseUser.
877 cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp)
883 smbp = (smb_t *) inp;
884 uidp = smb_FindUID(vcp, smbp->uid, 0);
885 if ((!uidp) || (!uidp->unp))
888 lock_ObtainMutex(&uidp->mx);
889 up = uidp->unp->userp;
891 lock_ReleaseMutex(&uidp->mx);
893 smb_ReleaseUID(uidp);
899 * Return a pointer to a pathname extracted from a TID structure. The
900 * TID structure is not held; assume it won't go away.
902 char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid)
907 tidp = smb_FindTID(vcp, tid, 0);
908 tpath = tidp->pathname;
909 smb_ReleaseTID(tidp);
913 /* check to see if we have a chained fid, that is, a fid that comes from an
914 * OpenAndX message that ran earlier in this packet. In this case, the fid
915 * field in a read, for example, request, isn't set, since the value is
916 * supposed to be inherited from the openAndX call.
918 int smb_ChainFID(int fid, smb_packet_t *inp)
920 if (inp->fid == 0 || inp->inCount == 0) return fid;
921 else return inp->fid;
924 /* are we a priv'd user? What does this mean on NT? */
925 int smb_SUser(cm_user_t *userp)
930 /* find a file ID. If pass in 0, we'll allocate on on a create operation. */
931 smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags)
936 /* figure out if we need to allocate a new file ID */
939 fid = vcp->fidCounter;
943 lock_ObtainWrite(&smb_rctLock);
945 for(fidp = vcp->fidsp; fidp; fidp = (smb_fid_t *) osi_QNext(&fidp->q)) {
946 if (fid == fidp->fid) {
949 if (fid == 0) fid = 1;
956 if (!fidp && (flags & SMB_FLAG_CREATE)) {
957 fidp = malloc(sizeof(*fidp));
958 memset(fidp, 0, sizeof(*fidp));
959 osi_QAdd((osi_queue_t **)&vcp->fidsp, &fidp->q);
962 lock_InitializeMutex(&fidp->mx, "fid_t mutex");
964 fidp->curr_chunk = fidp->prev_chunk = -2;
965 fidp->raw_write_event = thrd_CreateEvent(NULL, FALSE, TRUE, NULL);
967 vcp->fidCounter = fid+1;
968 if (vcp->fidCounter == 0) vcp->fidCounter = 1;
971 lock_ReleaseWrite(&smb_rctLock);
975 void smb_ReleaseFID(smb_fid_t *fidp)
982 lock_ObtainWrite(&smb_rctLock);
983 osi_assert(fidp->refCount-- > 0);
984 if (fidp->refCount == 0 && (fidp->flags & SMB_FID_DELETE)) {
986 if (!(fidp->flags & SMB_FID_IOCTL))
988 osi_QRemove((osi_queue_t **) &vcp->fidsp, &fidp->q);
989 thrd_CloseHandle(fidp->raw_write_event);
991 /* and see if there is ioctl stuff to free */
992 ioctlp = fidp->ioctlp;
994 if (ioctlp->prefix) cm_FreeSpace(ioctlp->prefix);
995 if (ioctlp->inAllocp) free(ioctlp->inAllocp);
996 if (ioctlp->outAllocp) free(ioctlp->outAllocp);
1002 lock_ReleaseWrite(&smb_rctLock);
1004 /* now release the scache structure */
1005 if (scp) cm_ReleaseSCache(scp);
1009 * Case-insensitive search for one string in another;
1010 * used to find variable names in submount pathnames.
1012 static char *smb_stristr(char *str1, char *str2)
1016 for (cursor = str1; *cursor; cursor++)
1017 if (stricmp(cursor, str2) == 0)
1024 * Substitute a variable value for its name in a submount pathname. Variable
1025 * name has been identified by smb_stristr() and is in substr. Variable name
1026 * length (plus one) is in substr_size. Variable value is in newstr.
1028 static void smb_subst(char *str1, char *substr, unsigned int substr_size,
1033 strcpy(temp, substr + substr_size - 1);
1034 strcpy(substr, newstr);
1038 char VNUserName[] = "%USERNAME%";
1039 char VNLCUserName[] = "%LCUSERNAME%";
1040 char VNComputerName[] = "%COMPUTERNAME%";
1041 char VNLCComputerName[] = "%LCCOMPUTERNAME%";
1043 /* List available shares */
1044 int smb_ListShares()
1048 char shareBuf[4096];
1056 /*strcpy(shareNameList[num_shares], "all");
1057 strcpy(pathNameList[num_shares++], "/afs");*/
1058 fprintf(stderr, "The following shares are available:\n");
1059 fprintf(stderr, "Share Name (AFS Path)\n");
1060 fprintf(stderr, "---------------------\n");
1061 fprintf(stderr, "\\\\%s\\%-16s (/afs)\n", smb_localNamep, "ALL");
1064 code = GetWindowsDirectory(sbmtpath, sizeof(sbmtpath));
1065 if (code == 0 || code > sizeof(sbmtpath)) return -1;
1067 strcpy(sbmtpath, cm_confDir);
1069 strcat(sbmtpath, "/afsdsbmt.ini");
1070 len = GetPrivateProfileString("AFS Submounts", NULL, NULL,
1071 shareBuf, sizeof(shareBuf),
1077 this_share = shareBuf;
1081 /*strcpy(shareNameList[num_shares], this_share);*/
1082 len = GetPrivateProfileString("AFS Submounts", this_share,
1086 if (!len) return num_shares;
1088 if (strncmp(p, "/afs", 4) != 0)
1091 if (*p == '\\') *p = '/'; /* change to / */
1095 fprintf(stderr, "\\\\%s\\%-16s (%s%s)\n",
1096 smb_localNamep, this_share, (print_afs ? "/afs" : "\0"),
1099 while (*this_share != NULL) this_share++; /* find next NULL */
1100 this_share++; /* skip past the NULL */
1101 } while (*this_share != NULL); /* stop at final NULL */
1106 /* find a shareName in the table of submounts */
1107 int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName,
1111 char pathName[1024];
1119 if (strcmp(shareName, "IPC$") == 0) {
1124 if (_stricmp(shareName, "all") == 0) {
1130 strcpy(sbmtpath, "afsdsbmt.ini");
1132 strcpy(sbmtpath, cm_confDir);
1133 strcat(sbmtpath, "/afsdsbmt.ini");
1135 len = GetPrivateProfileString("AFS Submounts", shareName, "",
1136 pathName, sizeof(pathName), sbmtpath);
1137 if (len == 0 || len == sizeof(pathName) - 1) {
1142 /* We can accept either unix or PC style AFS pathnames. Convert
1143 Unix-style to PC style here for internal use. */
1145 if (strncmp(p, "/afs", 4) == 0)
1146 p += 4; /* skip /afs */
1149 if (*q == '/') *q = '\\'; /* change to \ */
1155 if (var = smb_stristr(p, VNUserName)) {
1156 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
1157 if (uidp && uidp->unp)
1158 smb_subst(p, var, sizeof(VNUserName),
1161 smb_subst(p, var, sizeof(VNUserName),
1163 smb_ReleaseUID(uidp);
1165 else if (var = smb_stristr(p, VNLCUserName)) {
1166 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
1167 if (uidp && uidp->unp)
1168 strcpy(temp, uidp->unp->name);
1169 else strcpy(temp, " ");
1171 smb_subst(p, var, sizeof(VNLCUserName), temp);
1172 smb_ReleaseUID(uidp);
1174 else if (var = smb_stristr(p, VNComputerName)) {
1175 sizeTemp = sizeof(temp);
1176 GetComputerName((LPTSTR)temp, &sizeTemp);
1177 smb_subst(p, var, sizeof(VNComputerName),
1180 else if (var = smb_stristr(p, VNLCComputerName)) {
1181 sizeTemp = sizeof(temp);
1182 GetComputerName((LPTSTR)temp, &sizeTemp);
1184 smb_subst(p, var, sizeof(VNLCComputerName),
1190 *pathNamep = strdup(p);
1194 /* find a dir search structure by cookie value, and return it held.
1195 * Must be called with smb_globalLock held.
1197 smb_dirSearch_t *smb_FindDirSearchNL(long cookie)
1199 smb_dirSearch_t *dsp;
1201 for(dsp = smb_firstDirSearchp; dsp; dsp = (smb_dirSearch_t *) osi_QNext(&dsp->q)) {
1202 if (dsp->cookie == cookie) {
1203 if (dsp != smb_firstDirSearchp) {
1204 /* move to head of LRU queue, too, if we're not already there */
1205 if (smb_lastDirSearchp == (smb_dirSearch_t *) &dsp->q)
1206 smb_lastDirSearchp = (smb_dirSearch_t *)
1208 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1209 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1210 if (!smb_lastDirSearchp)
1211 smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1220 void smb_DeleteDirSearch(smb_dirSearch_t *dsp)
1222 lock_ObtainWrite(&smb_globalLock);
1223 dsp->flags |= SMB_DIRSEARCH_DELETE;
1224 lock_ReleaseWrite(&smb_globalLock);
1225 lock_ObtainMutex(&dsp->mx);
1226 if(dsp->scp != NULL) {
1227 lock_ObtainMutex(&dsp->scp->mx);
1228 if (dsp->flags & SMB_DIRSEARCH_BULKST) {
1229 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
1230 dsp->scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
1231 dsp->scp->bulkStatProgress = hones;
1233 lock_ReleaseMutex(&dsp->scp->mx);
1235 lock_ReleaseMutex(&dsp->mx);
1238 void smb_ReleaseDirSearch(smb_dirSearch_t *dsp)
1244 lock_ObtainWrite(&smb_globalLock);
1245 osi_assert(dsp->refCount-- > 0);
1246 if (dsp->refCount == 0 && (dsp->flags & SMB_DIRSEARCH_DELETE)) {
1247 if (&dsp->q == (osi_queue_t *) smb_lastDirSearchp)
1248 smb_lastDirSearchp = (smb_dirSearch_t *) osi_QPrev(&smb_lastDirSearchp->q);
1249 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1250 lock_FinalizeMutex(&dsp->mx);
1254 lock_ReleaseWrite(&smb_globalLock);
1256 /* do this now to avoid spurious locking hierarchy creation */
1257 if (scp) cm_ReleaseSCache(scp);
1260 /* find a dir search structure by cookie value, and return it held */
1261 smb_dirSearch_t *smb_FindDirSearch(long cookie)
1263 smb_dirSearch_t *dsp;
1265 lock_ObtainWrite(&smb_globalLock);
1266 dsp = smb_FindDirSearchNL(cookie);
1267 lock_ReleaseWrite(&smb_globalLock);
1271 /* GC some dir search entries, in the address space expected by the specific protocol.
1272 * Must be called with smb_globalLock held; release the lock temporarily.
1274 #define SMB_DIRSEARCH_GCMAX 10 /* how many at once */
1275 void smb_GCDirSearches(int isV3)
1277 smb_dirSearch_t *prevp;
1278 smb_dirSearch_t *tp;
1279 smb_dirSearch_t *victimsp[SMB_DIRSEARCH_GCMAX];
1283 victimCount = 0; /* how many have we got so far */
1284 for(tp = smb_lastDirSearchp; tp; tp=prevp) {
1285 prevp = (smb_dirSearch_t *) osi_QPrev(&tp->q); /* we'll move tp from queue, so
1288 /* if no one is using this guy, and we're either in the new protocol,
1289 * or we're in the old one and this is a small enough ID to be useful
1290 * to the old protocol, GC this guy.
1292 if (tp->refCount == 0 && (isV3 || tp->cookie <= 255)) {
1293 /* hold and delete */
1294 tp->flags |= SMB_DIRSEARCH_DELETE;
1295 victimsp[victimCount++] = tp;
1299 /* don't do more than this */
1300 if (victimCount >= SMB_DIRSEARCH_GCMAX) break;
1303 /* now release them */
1304 lock_ReleaseWrite(&smb_globalLock);
1305 for(i = 0; i < victimCount; i++) {
1306 smb_ReleaseDirSearch(victimsp[i]);
1308 lock_ObtainWrite(&smb_globalLock);
1311 /* function for allocating a dir search entry. We need these to remember enough context
1312 * since we don't get passed the path from call to call during a directory search.
1314 * Returns a held dir search structure, and bumps the reference count on the vnode,
1315 * since it saves a pointer to the vnode.
1317 smb_dirSearch_t *smb_NewDirSearch(int isV3)
1319 smb_dirSearch_t *dsp;
1323 lock_ObtainWrite(&smb_globalLock);
1326 /* what's the biggest ID allowed in this version of the protocol */
1327 if (isV3) maxAllowed = 65535;
1328 else maxAllowed = 255;
1331 /* twice so we have enough tries to find guys we GC after one pass;
1332 * 10 extra is just in case I mis-counted.
1334 if (++counter > 2*maxAllowed+10) osi_panic("afsd: dir search cookie leak",
1335 __FILE__, __LINE__);
1336 if (smb_dirSearchCounter > maxAllowed) {
1337 smb_dirSearchCounter = 1;
1338 smb_GCDirSearches(isV3); /* GC some (drops global lock) */
1340 dsp = smb_FindDirSearchNL(smb_dirSearchCounter);
1342 /* don't need to watch for refcount zero and deleted, since
1343 * we haven't dropped the global lock.
1346 ++smb_dirSearchCounter;
1350 dsp = malloc(sizeof(*dsp));
1351 memset(dsp, 0, sizeof(*dsp));
1352 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1353 if (!smb_lastDirSearchp) smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1354 dsp->cookie = smb_dirSearchCounter;
1355 ++smb_dirSearchCounter;
1357 lock_InitializeMutex(&dsp->mx, "cm_dirSearch_t");
1358 dsp->lastTime = osi_Time();
1361 lock_ReleaseWrite(&smb_globalLock);
1365 static smb_packet_t *GetPacket(void)
1369 unsigned int npar, seg, tb_sel;
1372 lock_ObtainWrite(&smb_globalLock);
1373 tbp = smb_packetFreeListp;
1374 if (tbp) smb_packetFreeListp = tbp->nextp;
1375 lock_ReleaseWrite(&smb_globalLock);
1378 tbp = GlobalAlloc(GMEM_FIXED, 65540);
1380 tbp = malloc(sizeof(smb_packet_t));
1382 tbp->magic = SMB_PACKETMAGIC;
1385 tbp->resumeCode = 0;
1391 tbp->ncb_length = 0;
1395 npar = SMB_PACKETSIZE >> 4; /* number of paragraphs */
1398 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1400 afsi_log("Cannot allocate %d paragraphs of DOS memory",
1402 osi_panic("",__FILE__,__LINE__);
1405 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
1410 tbp->dos_pkt = (seg * 16) + 0; /* DOS physical address */
1411 tbp->dos_pkt_sel = tb_sel;
1414 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1419 smb_packet_t *smb_CopyPacket(smb_packet_t *pkt)
1423 memcpy(tbp, pkt, sizeof(smb_packet_t));
1424 tbp->wctp = tbp->data + ((unsigned int)pkt->wctp -
1425 (unsigned int)pkt->data);
1429 static NCB *GetNCB(void)
1434 unsigned int npar, seg, tb_sel;
1437 lock_ObtainWrite(&smb_globalLock);
1438 tbp = smb_ncbFreeListp;
1439 if (tbp) smb_ncbFreeListp = tbp->nextp;
1440 lock_ReleaseWrite(&smb_globalLock);
1443 tbp = GlobalAlloc(GMEM_FIXED, sizeof(*tbp));
1445 tbp = malloc(sizeof(*tbp));
1446 npar = (sizeof(NCB)+15) >> 4; /* number of paragraphs */
1449 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1451 afsi_log("Cannot allocate %d paragraphs of DOS mem in GetNCB",
1453 osi_panic("",__FILE__,__LINE__);
1455 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X in GetNCB",
1460 tbp->dos_ncb = (seg * 16) + 0; /* DOS physical address */
1461 tbp->dos_ncb_sel = tb_sel;
1463 tbp->magic = SMB_NCBMAGIC;
1466 osi_assert(tbp->magic == SMB_NCBMAGIC);
1468 memset(&tbp->ncb, 0, sizeof(NCB));
1471 dos_memset(tbp->dos_ncb, 0, sizeof(NCB));
1476 void smb_FreePacket(smb_packet_t *tbp)
1478 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1480 lock_ObtainWrite(&smb_globalLock);
1481 tbp->nextp = smb_packetFreeListp;
1482 smb_packetFreeListp = tbp;
1483 tbp->magic = SMB_PACKETMAGIC;
1486 tbp->resumeCode = 0;
1492 tbp->ncb_length = 0;
1494 lock_ReleaseWrite(&smb_globalLock);
1497 static void FreeNCB(NCB *bufferp)
1501 tbp = (smb_ncb_t *) bufferp;
1502 osi_assert(tbp->magic == SMB_NCBMAGIC);
1504 lock_ObtainWrite(&smb_globalLock);
1505 tbp->nextp = smb_ncbFreeListp;
1506 smb_ncbFreeListp = tbp;
1507 lock_ReleaseWrite(&smb_globalLock);
1510 /* get a ptr to the data part of a packet, and its count */
1511 unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp)
1515 unsigned char *afterParmsp;
1517 parmBytes = *smbp->wctp << 1;
1518 afterParmsp = smbp->wctp + parmBytes + 1;
1520 dataBytes = afterParmsp[0] + (afterParmsp[1]<<8);
1521 if (nbytesp) *nbytesp = dataBytes;
1523 /* don't forget to skip the data byte count, since it follows
1524 * the parameters; that's where the "2" comes from below.
1526 return (unsigned char *) (afterParmsp + 2);
1529 /* must set all the returned parameters before playing around with the
1530 * data region, since the data region is located past the end of the
1531 * variable number of parameters.
1533 void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize)
1535 unsigned char *afterParmsp;
1537 afterParmsp = smbp->wctp + ((*smbp->wctp)<<1) + 1;
1539 *afterParmsp++ = dsize & 0xff;
1540 *afterParmsp = (dsize>>8) & 0xff;
1543 /* return the parm'th parameter in the smbp packet */
1544 unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm)
1547 unsigned char *parmDatap;
1549 parmCount = *smbp->wctp;
1551 if (parm >= parmCount) {
1556 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1557 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1558 parm, parmCount, smbp->ncb_length);
1560 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1561 1, smbp->ncb_length, ptbuf, smbp);
1562 DeregisterEventSource(h);
1566 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1567 parm, parmCount, smbp->ncb_length);
1568 osi_Log0(afsd_logp, s);
1570 osi_panic(s, __FILE__, __LINE__);
1572 parmDatap = smbp->wctp + (2*parm) + 1;
1574 return parmDatap[0] + (parmDatap[1] << 8);
1577 /* return the parm'th parameter in the smbp packet */
1578 unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset)
1581 unsigned char *parmDatap;
1583 parmCount = *smbp->wctp;
1585 if (parm * 2 + offset >= parmCount * 2) {
1590 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1591 sprintf(s, "Bad SMB param %d offset %d out of %d, ncb len %d",
1592 parm, offset, parmCount, smbp->ncb_length);
1594 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1595 1, smbp->ncb_length, ptbuf, smbp);
1596 DeregisterEventSource(h);
1600 sprintf(s, "Bad SMB param %d offset %d out of %d, "
1602 parm, offset, parmCount, smbp->ncb_length);
1603 osi_Log0(afsd_logp, s);
1606 osi_panic(s, __FILE__, __LINE__);
1608 parmDatap = smbp->wctp + (2*parm) + 1 + offset;
1610 return parmDatap[0] + (parmDatap[1] << 8);
1613 void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue)
1617 /* make sure we have enough slots */
1618 if (*smbp->wctp <= slot) *smbp->wctp = slot+1;
1620 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1621 *parmDatap++ = parmValue & 0xff;
1622 *parmDatap = (parmValue>>8) & 0xff;
1625 void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue)
1629 /* make sure we have enough slots */
1630 if (*smbp->wctp <= slot) *smbp->wctp = slot+2;
1632 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1633 *parmDatap++ = parmValue & 0xff;
1634 *parmDatap++ = (parmValue>>8) & 0xff;
1635 *parmDatap++ = (parmValue>>16) & 0xff;
1636 *parmDatap++ = (parmValue>>24) & 0xff;
1639 void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep)
1644 /* make sure we have enough slots */
1645 if (*smbp->wctp <= slot) *smbp->wctp = slot+4;
1647 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1649 *parmDatap++ = *parmValuep++;
1652 void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue)
1656 /* make sure we have enough slots */
1657 if (*smbp->wctp <= slot) {
1658 if (smbp->oddByte) {
1660 *smbp->wctp = slot+1;
1665 parmDatap = smbp->wctp + 2*slot + 1 + (1 - smbp->oddByte);
1666 *parmDatap++ = parmValue & 0xff;
1669 void smb_StripLastComponent(char *outPathp, char **lastComponentp, char *inPathp)
1673 lastSlashp = strrchr(inPathp, '\\');
1675 *lastComponentp = lastSlashp;
1678 if (inPathp == lastSlashp) break;
1679 *outPathp++ = *inPathp++;
1688 unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp)
1690 if (*inp++ != 0x4) return NULL;
1692 *chainpp = inp + strlen(inp) + 1; /* skip over null-terminated string */
1697 unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp)
1701 if (*inp++ != 0x5) return NULL;
1702 tlen = inp[0] + (inp[1]<<8);
1703 inp += 2; /* skip length field */
1706 *chainpp = inp + tlen;
1709 if (lengthp) *lengthp = tlen;
1714 /* format a packet as a response */
1715 void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *op)
1720 outp = (smb_t *) op;
1722 /* zero the basic structure through the smb_wct field, and zero the data
1723 * size field, assuming that wct stays zero; otherwise, you have to
1724 * explicitly set the data size field, too.
1726 inSmbp = (smb_t *) inp;
1727 memset(outp, 0, sizeof(smb_t)+2);
1733 outp->com = inSmbp->com;
1734 outp->tid = inSmbp->tid;
1735 outp->pid = inSmbp->pid;
1736 outp->uid = inSmbp->uid;
1737 outp->mid = inSmbp->mid;
1738 outp->res[0] = inSmbp->res[0];
1739 outp->res[1] = inSmbp->res[1];
1740 op->inCom = inSmbp->com;
1742 outp->reb = 0x80; /* SERVER_RESP */
1743 outp->flg2 = 0x1; /* KNOWS_LONG_NAMES */
1745 /* copy fields in generic packet area */
1746 op->wctp = &outp->wct;
1749 /* send a (probably response) packet; vcp tells us to whom to send it.
1750 * we compute the length by looking at wct and bcc fields.
1752 void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp)
1769 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
1772 memset((char *)ncbp, 0, sizeof(NCB));
1774 extra = 2 * (*inp->wctp); /* space used by parms, in bytes */
1775 tp = inp->wctp + 1+ extra; /* points to count of data bytes */
1776 extra += tp[0] + (tp[1]<<8);
1777 extra += ((unsigned int)inp->wctp - (unsigned int)inp->data); /* distance to last wct field */
1778 extra += 3; /* wct and length fields */
1780 ncbp->ncb_length = extra; /* bytes to send */
1781 ncbp->ncb_lsn = (unsigned char) vcp->lsn; /* vc to use */
1782 ncbp->ncb_lana_num = vcp->lana;
1783 ncbp->ncb_command = NCBSEND; /* op means send data */
1785 ncbp->ncb_buffer = (char *) inp;/* packet */
1786 code = Netbios(ncbp);
1788 ncbp->ncb_buffer = inp->dos_pkt;/* packet */
1789 ((smb_ncb_t*)ncbp)->orig_pkt = inp;
1791 /* copy header information from virtual to DOS address space */
1792 dosmemput((char*)inp, SMB_PACKETSIZE, inp->dos_pkt);
1793 code = Netbios(ncbp, dos_ncb);
1797 osi_Log1(afsd_logp, "SendPacket failure code %d", code);
1803 void smb_MapNTError(long code, unsigned long *NTStatusp)
1805 unsigned long NTStatus;
1807 /* map CM_ERROR_* errors to NT 32-bit status codes */
1808 if (code == CM_ERROR_NOSUCHCELL) {
1809 NTStatus = 0xC000000FL; /* No such file */
1811 else if (code == CM_ERROR_NOSUCHVOLUME) {
1812 NTStatus = 0xC000000FL; /* No such file */
1814 else if (code == CM_ERROR_TIMEDOUT) {
1815 NTStatus = 0xC00000CFL; /* Paused */
1817 else if (code == CM_ERROR_RETRY) {
1818 NTStatus = 0xC000022DL; /* Retry */
1820 else if (code == CM_ERROR_NOACCESS) {
1821 NTStatus = 0xC0000022L; /* Access denied */
1823 else if (code == CM_ERROR_READONLY) {
1824 NTStatus = 0xC00000A2L; /* Write protected */
1826 else if (code == CM_ERROR_NOSUCHFILE) {
1827 NTStatus = 0xC000000FL; /* No such file */
1829 else if (code == CM_ERROR_NOSUCHPATH) {
1830 NTStatus = 0xC000003AL; /* Object path not found */
1832 else if (code == CM_ERROR_TOOBIG) {
1833 NTStatus = 0xC000007BL; /* Invalid image format */
1835 else if (code == CM_ERROR_INVAL) {
1836 NTStatus = 0xC000000DL; /* Invalid parameter */
1838 else if (code == CM_ERROR_BADFD) {
1839 NTStatus = 0xC0000008L; /* Invalid handle */
1841 else if (code == CM_ERROR_BADFDOP) {
1842 NTStatus = 0xC0000022L; /* Access denied */
1844 else if (code == CM_ERROR_EXISTS) {
1845 NTStatus = 0xC0000035L; /* Object name collision */
1847 else if (code == CM_ERROR_NOTEMPTY) {
1848 NTStatus = 0xC0000101L; /* Directory not empty */
1850 else if (code == CM_ERROR_CROSSDEVLINK) {
1851 NTStatus = 0xC00000D4L; /* Not same device */
1853 else if (code == CM_ERROR_NOTDIR) {
1854 NTStatus = 0xC0000103L; /* Not a directory */
1856 else if (code == CM_ERROR_ISDIR) {
1857 NTStatus = 0xC00000BAL; /* File is a directory */
1859 else if (code == CM_ERROR_BADOP) {
1860 NTStatus = 0xC09820FFL; /* SMB no support */
1862 else if (code == CM_ERROR_BADSHARENAME) {
1863 NTStatus = 0xC00000CCL; /* Bad network name */
1865 else if (code == CM_ERROR_NOIPC) {
1866 NTStatus = 0xC00000CCL; /* Bad network name */
1868 else if (code == CM_ERROR_CLOCKSKEW) {
1869 NTStatus = 0xC0000133L; /* Time difference at DC */
1871 else if (code == CM_ERROR_BADTID) {
1872 NTStatus = 0xC0982005L; /* SMB bad TID */
1874 else if (code == CM_ERROR_USESTD) {
1875 NTStatus = 0xC09820FBL; /* SMB use standard */
1877 else if (code == CM_ERROR_QUOTA) {
1878 NTStatus = 0xC0000044L; /* Quota exceeded */
1880 else if (code == CM_ERROR_SPACE) {
1881 NTStatus = 0xC000007FL; /* Disk full */
1883 else if (code == CM_ERROR_ATSYS) {
1884 NTStatus = 0xC0000033L; /* Object name invalid */
1886 else if (code == CM_ERROR_BADNTFILENAME) {
1887 NTStatus = 0xC0000033L; /* Object name invalid */
1889 else if (code == CM_ERROR_WOULDBLOCK) {
1890 NTStatus = 0xC0000055L; /* Lock not granted */
1892 else if (code == CM_ERROR_PARTIALWRITE) {
1893 NTStatus = 0xC000007FL; /* Disk full */
1895 else if (code == CM_ERROR_BUFFERTOOSMALL) {
1896 NTStatus = 0xC0000023L; /* Buffer too small */
1899 NTStatus = 0xC0982001L; /* SMB non-specific error */
1902 *NTStatusp = NTStatus;
1903 osi_Log2(afsd_logp, "SMB SEND code %x as NT %x", code, NTStatus);
1906 void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
1907 unsigned char *classp)
1909 unsigned char class;
1910 unsigned short error;
1912 /* map CM_ERROR_* errors to SMB errors */
1913 if (code == CM_ERROR_NOSUCHCELL) {
1915 error = 3; /* bad path */
1917 else if (code == CM_ERROR_NOSUCHVOLUME) {
1919 error = 3; /* bad path */
1921 else if (code == CM_ERROR_TIMEDOUT) {
1923 error = 81; /* server is paused */
1925 else if (code == CM_ERROR_RETRY) {
1926 class = 2; /* shouldn't happen */
1929 else if (code == CM_ERROR_NOACCESS) {
1931 error = 4; /* bad access */
1933 else if (code == CM_ERROR_READONLY) {
1935 error = 19; /* read only */
1937 else if (code == CM_ERROR_NOSUCHFILE) {
1939 error = 2; /* ENOENT! */
1941 else if (code == CM_ERROR_NOSUCHPATH) {
1943 error = 3; /* Bad path */
1945 else if (code == CM_ERROR_TOOBIG) {
1947 error = 11; /* bad format */
1949 else if (code == CM_ERROR_INVAL) {
1950 class = 2; /* server non-specific error code */
1953 else if (code == CM_ERROR_BADFD) {
1955 error = 6; /* invalid file handle */
1957 else if (code == CM_ERROR_BADFDOP) {
1958 class = 1; /* invalid op on FD */
1961 else if (code == CM_ERROR_EXISTS) {
1963 error = 80; /* file already exists */
1965 else if (code == CM_ERROR_NOTEMPTY) {
1967 error = 5; /* delete directory not empty */
1969 else if (code == CM_ERROR_CROSSDEVLINK) {
1971 error = 17; /* EXDEV */
1973 else if (code == CM_ERROR_NOTDIR) {
1974 class = 1; /* bad path */
1977 else if (code == CM_ERROR_ISDIR) {
1978 class = 1; /* access denied; DOS doesn't have a good match */
1981 else if (code == CM_ERROR_BADOP) {
1985 else if (code == CM_ERROR_BADSHARENAME) {
1989 else if (code == CM_ERROR_NOIPC) {
1993 else if (code == CM_ERROR_CLOCKSKEW) {
1994 class = 1; /* invalid function */
1997 else if (code == CM_ERROR_BADTID) {
2001 else if (code == CM_ERROR_USESTD) {
2005 else if (code == CM_ERROR_REMOTECONN) {
2009 else if (code == CM_ERROR_QUOTA) {
2010 if (vcp->flags & SMB_VCFLAG_USEV3) {
2012 error = 39; /* disk full */
2016 error = 5; /* access denied */
2019 else if (code == CM_ERROR_SPACE) {
2020 if (vcp->flags & SMB_VCFLAG_USEV3) {
2022 error = 39; /* disk full */
2026 error = 5; /* access denied */
2029 else if (code == CM_ERROR_PARTIALWRITE) {
2031 error = 39; /* disk full */
2033 else if (code == CM_ERROR_ATSYS) {
2035 error = 2; /* ENOENT */
2037 else if (code == CM_ERROR_WOULDBLOCK) {
2039 error = 33; /* lock conflict */
2041 else if (code == CM_ERROR_NOFILES) {
2043 error = 18; /* no files in search */
2045 else if (code == CM_ERROR_RENAME_IDENTICAL) {
2047 error = 183; /* Samba uses this */
2056 osi_Log3(afsd_logp, "SMB SEND code %x as SMB %d: %d", code, class, error);
2059 long smb_SendCoreBadOp(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2061 return CM_ERROR_BADOP;
2064 long smb_ReceiveCoreEcho(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2066 unsigned short EchoCount, i;
2067 char *data, *outdata;
2070 EchoCount = (unsigned short) smb_GetSMBParm(inp, 0);
2072 for (i=1; i<=EchoCount; i++) {
2073 data = smb_GetSMBData(inp, &dataSize);
2074 smb_SetSMBParm(outp, 0, i);
2075 smb_SetSMBDataLength(outp, dataSize);
2076 outdata = smb_GetSMBData(outp, NULL);
2077 memcpy(outdata, data, dataSize);
2078 smb_SendPacket(vcp, outp);
2084 long smb_ReceiveCoreReadRaw(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2087 long count, minCount, finalCount;
2091 cm_user_t *userp = NULL;
2095 char *rawBuf = NULL;
2097 dos_ptr rawBuf = NULL;
2104 fd = smb_GetSMBParm(inp, 0);
2105 count = smb_GetSMBParm(inp, 3);
2106 minCount = smb_GetSMBParm(inp, 4);
2107 offset.HighPart = 0; /* too bad */
2108 offset.LowPart = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
2110 osi_Log3(afsd_logp, "smb_ReceieveCoreReadRaw fd %d, off 0x%x, size 0x%x",
2111 fd, offset.LowPart, count);
2113 fidp = smb_FindFID(vcp, fd, 0);
2117 lock_ObtainMutex(&smb_RawBufLock);
2119 /* Get a raw buf, from head of list */
2120 rawBuf = smb_RawBufs;
2122 smb_RawBufs = *(char **)smb_RawBufs;
2124 smb_RawBufs = _farpeekl(_dos_ds, smb_RawBufs);
2127 lock_ReleaseMutex(&smb_RawBufLock);
2131 if (fidp->flags & SMB_FID_IOCTL)
2134 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp);
2136 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp, rawBuf);
2139 /* Give back raw buffer */
2140 lock_ObtainMutex(&smb_RawBufLock);
2142 *((char **) rawBuf) = smb_RawBufs;
2144 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2147 smb_RawBufs = rawBuf;
2148 lock_ReleaseMutex(&smb_RawBufLock);
2153 userp = smb_GetUser(vcp, inp);
2156 code = smb_ReadData(fidp, &offset, count, rawBuf, userp, &finalCount);
2158 /* have to give ReadData flag so it will treat buffer as DOS mem. */
2159 code = smb_ReadData(fidp, &offset, count, (unsigned char *)rawBuf,
2160 userp, &finalCount, TRUE /* rawFlag */);
2167 cm_ReleaseUser(userp);
2169 smb_ReleaseFID(fidp);
2174 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
2176 memset((char *)ncbp, 0, sizeof(NCB));
2178 ncbp->ncb_length = (unsigned short) finalCount;
2179 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
2180 ncbp->ncb_lana_num = vcp->lana;
2181 ncbp->ncb_command = NCBSEND;
2182 ncbp->ncb_buffer = rawBuf;
2185 code = Netbios(ncbp);
2187 code = Netbios(ncbp, dos_ncb);
2190 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
2193 /* Give back raw buffer */
2194 lock_ObtainMutex(&smb_RawBufLock);
2196 *((char **) rawBuf) = smb_RawBufs;
2198 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2201 smb_RawBufs = rawBuf;
2202 lock_ReleaseMutex(&smb_RawBufLock);
2208 long smb_ReceiveCoreLockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2213 long smb_ReceiveCoreUnlockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2218 long smb_ReceiveNegotiate(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2224 int protoIndex; /* index we're using */
2229 char protocol_array[10][1024]; /* protocol signature of the client */
2232 osi_Log1(afsd_logp, "SMB receive negotiate; %d + 1 ongoing ops",
2236 DWORD now = GetCurrentTime();
2237 if (now - last_msg_time >= 30000
2238 && now - last_msg_time <= 90000) {
2240 "Setting dead_vcp %x", active_vcp);
2241 dead_vcp = active_vcp;
2242 dead_vcp->flags |= SMB_VCFLAG_ALREADYDEAD;
2247 inp->flags |= SMB_PACKETFLAG_PROFILE_UPDATE_OK;
2249 namep = smb_GetSMBData(inp, &dbytes);
2252 coreProtoIndex = -1; /* not found */
2255 while(namex < dbytes) {
2256 osi_Log1(afsd_logp, "Protocol %s",
2257 osi_LogSaveString(afsd_logp, namep+1));
2258 strcpy(protocol_array[tcounter], namep+1);
2260 /* namep points at the first protocol, or really, a 0x02
2261 * byte preceding the null-terminated ASCII name.
2263 if (strcmp("PC NETWORK PROGRAM 1.0", namep+1) == 0) {
2264 coreProtoIndex = tcounter;
2266 else if (smb_useV3 && strcmp("LM1.2X002", namep+1) == 0) {
2267 v3ProtoIndex = tcounter;
2269 else if (smb_useV3 && strcmp("NT LM 0.12", namep+1) == 0) {
2270 NTProtoIndex = tcounter;
2273 /* compute size of protocol entry */
2274 entryLength = strlen(namep+1);
2275 entryLength += 2; /* 0x02 bytes and null termination */
2277 /* advance over this protocol entry */
2278 namex += entryLength;
2279 namep += entryLength;
2280 tcounter++; /* which proto entry we're looking at */
2282 #ifndef NOMOREFILESFIX
2284 * NOTE: We can determine what OS (NT4.0, W2K, W9X, etc)
2285 * the client is running by reading the protocol signature.
2286 * ie. the order in which it sends us the protocol list.
2288 * Special handling for Windows 2000 clients (defect 11765 )
2290 if (tcounter == 6) {
2292 smb_t *ip = (smb_t *) inp;
2293 smb_t *op = (smb_t *) outp;
2295 if ((strcmp("PC NETWORK PROGRAM 1.0", protocol_array[0]) == 0) &&
2296 (strcmp("LANMAN1.0", protocol_array[1]) == 0) &&
2297 (strcmp("Windows for Workgroups 3.1a", protocol_array[2]) == 0) &&
2298 (strcmp("LM1.2X002", protocol_array[3]) == 0) &&
2299 (strcmp("LANMAN2.1", protocol_array[4]) == 0) &&
2300 (strcmp("NT LM 0.12", protocol_array[5]) == 0)) {
2301 isWindows2000 = TRUE;
2302 osi_Log0(afsd_logp, "Looks like a Windows 2000 client");
2304 * HACK: for now - just negotiate a lower protocol till we
2305 * figure out which flag/flag2 or some other field
2306 * (capabilities maybe?) to set in order for this to work
2307 * correctly with Windows 2000 clients (defect 11765)
2310 /* Things to try (after looking at tcpdump output could be
2311 * setting flags and flags2 to 0x98 and 0xc853 like this
2312 * op->reb = 0x98; op->flg2 = 0xc853;
2313 * osi_Log2(afsd_logp, "Flags:0x%x Flags2:0x%x", ip->reb, ip->flg2);
2320 if (NTProtoIndex != -1) {
2321 protoIndex = NTProtoIndex;
2322 vcp->flags |= (SMB_VCFLAG_USENT | SMB_VCFLAG_USEV3);
2324 else if (v3ProtoIndex != -1) {
2325 protoIndex = v3ProtoIndex;
2326 vcp->flags |= SMB_VCFLAG_USEV3;
2328 else if (coreProtoIndex != -1) {
2329 protoIndex = coreProtoIndex;
2330 vcp->flags |= SMB_VCFLAG_USECORE;
2332 else protoIndex = -1;
2334 if (protoIndex == -1)
2335 return CM_ERROR_INVAL;
2336 else if (NTProtoIndex != -1) {
2337 smb_SetSMBParm(outp, 0, protoIndex);
2338 smb_SetSMBParmByte(outp, 1, 0); /* share level security, no passwd encrypt */
2339 smb_SetSMBParm(outp, 1, 8); /* max multiplexed requests */
2340 smb_SetSMBParm(outp, 2, 100); /* max VCs per consumer/server connection */
2341 smb_SetSMBParmLong(outp, 3, SMB_PACKETSIZE); /* xmit buffer size */
2342 smb_SetSMBParmLong(outp, 5, 65536); /* raw buffer size */
2343 smb_SetSMBParm(outp, 7, 1); /* next 2: session key */
2344 smb_SetSMBParm(outp, 8, 1);
2346 * Tried changing the capabilities to support for W2K - defect 117695
2347 * Maybe something else needs to be changed here?
2351 smb_SetSMBParmLong(outp, 9, 0x43fd);
2353 smb_SetSMBParmLong(outp, 9, 0x251);
2355 smb_SetSMBParmLong(outp, 9, 0x251); /* Capabilities: *
2356 * 32-bit error codes *
2360 smb_SetSMBParmLong(outp, 11, 0);/* XXX server time: do we need? */
2361 smb_SetSMBParmLong(outp, 13, 0);/* XXX server date: do we need? */
2362 smb_SetSMBParm(outp, 15, 0); /* XXX server tzone: do we need? */
2363 smb_SetSMBParmByte(outp, 16, 0);/* Encryption key length */
2364 smb_SetSMBDataLength(outp, 0); /* perhaps should specify 8 bytes anyway */
2366 else if (v3ProtoIndex != -1) {
2367 smb_SetSMBParm(outp, 0, protoIndex);
2368 smb_SetSMBParm(outp, 1, 0); /* share level security, no passwd encrypt */
2369 smb_SetSMBParm(outp, 2, SMB_PACKETSIZE);
2370 smb_SetSMBParm(outp, 3, 8); /* max multiplexed requests */
2371 smb_SetSMBParm(outp, 4, 100); /* max VCs per consumer/server connection */
2372 smb_SetSMBParm(outp, 5, 0); /* no support of block mode for read or write */
2373 smb_SetSMBParm(outp, 6, 1); /* next 2: session key */
2374 smb_SetSMBParm(outp, 7, 1);
2375 smb_SetSMBParm(outp, 8, 0); /* XXX server time: do we need? */
2376 smb_SetSMBParm(outp, 9, 0); /* XXX server date: do we need? */
2377 smb_SetSMBParm(outp, 10, 0); /* XXX server tzone: do we need? */
2378 smb_SetSMBParm(outp, 11, 0); /* resvd */
2379 smb_SetSMBParm(outp, 12, 0); /* resvd */
2380 smb_SetSMBDataLength(outp, 0); /* perhaps should specify 8 bytes anyway */
2382 else if (coreProtoIndex != -1) {
2383 smb_SetSMBParm(outp, 0, protoIndex);
2384 smb_SetSMBDataLength(outp, 0);
2389 void smb_Daemon(void *parmp)
2396 if ((count % 360) == 0) /* every hour */
2397 smb_CalculateNowTZ();
2398 /* XXX GC dir search entries */
2402 void smb_WaitingLocksDaemon()
2404 smb_waitingLock_t *wL, *nwL;
2407 smb_packet_t *inp, *outp;
2412 lock_ObtainWrite(&smb_globalLock);
2413 nwL = smb_allWaitingLocks;
2415 osi_SleepW((long)&smb_allWaitingLocks, &smb_globalLock);
2424 lock_ObtainWrite(&smb_globalLock);
2426 nwL = (smb_waitingLock_t *) osi_QNext(&wL->q);
2427 lock_ReleaseWrite(&smb_globalLock);
2428 code = cm_RetryLock((cm_file_lock_t *) wL->lockp,
2429 wL->vcp->flags & SMB_VCFLAG_ALREADYDEAD);
2430 if (code == CM_ERROR_WOULDBLOCK) {
2432 if (wL->timeRemaining != 0xffffffff
2433 && (wL->timeRemaining -= 1000) < 0)
2442 ncbp->ncb_length = inp->ncb_length;
2443 inp->spacep = cm_GetSpace();
2445 /* Remove waitingLock from list */
2446 lock_ObtainWrite(&smb_globalLock);
2447 osi_QRemove((osi_queue_t **)&smb_allWaitingLocks,
2449 lock_ReleaseWrite(&smb_globalLock);
2451 /* Resume packet processing */
2453 smb_SetSMBDataLength(outp, 0);
2454 outp->flags |= SMB_PACKETFLAG_SUSPENDED;
2455 outp->resumeCode = code;
2457 smb_DispatchPacket(vcp, inp, outp, ncbp, NULL);
2460 cm_FreeSpace(inp->spacep);
2461 smb_FreePacket(inp);
2462 smb_FreePacket(outp);
2470 long smb_ReceiveCoreGetDiskAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2472 osi_Log0(afsd_logp, "SMB receive get disk attributes");
2474 smb_SetSMBParm(outp, 0, 32000);
2475 smb_SetSMBParm(outp, 1, 64);
2476 smb_SetSMBParm(outp, 2, 1024);
2477 smb_SetSMBParm(outp, 3, 30000);
2478 smb_SetSMBParm(outp, 4, 0);
2479 smb_SetSMBDataLength(outp, 0);
2483 long smb_ReceiveCoreTreeConnect(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *rsp)
2486 unsigned short newTid;
2487 char shareName[256];
2495 osi_Log0(afsd_logp, "SMB receive tree connect");
2497 /* parse input parameters */
2498 tp = smb_GetSMBData(inp, NULL);
2499 pathp = smb_ParseASCIIBlock(tp, &tp);
2500 passwordp = smb_ParseASCIIBlock(tp, &tp);
2501 tp = strrchr(pathp, '\\');
2503 return CM_ERROR_BADSMB;
2504 strcpy(shareName, tp+1);
2506 userp = smb_GetUser(vcp, inp);
2508 lock_ObtainMutex(&vcp->mx);
2509 newTid = vcp->tidCounter++;
2510 lock_ReleaseMutex(&vcp->mx);
2512 tidp = smb_FindTID(vcp, newTid, SMB_FLAG_CREATE);
2513 shareFound = smb_FindShare(vcp, inp, shareName, &sharePath);
2515 smb_ReleaseTID(tidp);
2516 return CM_ERROR_BADSHARENAME;
2518 lock_ObtainMutex(&tidp->mx);
2519 tidp->userp = userp;
2520 tidp->pathname = sharePath;
2521 lock_ReleaseMutex(&tidp->mx);
2522 smb_ReleaseTID(tidp);
2524 smb_SetSMBParm(rsp, 0, SMB_PACKETSIZE);
2525 smb_SetSMBParm(rsp, 1, newTid);
2526 smb_SetSMBDataLength(rsp, 0);
2528 osi_Log1(afsd_logp, "SMB tree connect created ID %d", newTid);
2532 unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp)
2536 if (*inp++ != 0x1) return NULL;
2537 tlen = inp[0] + (inp[1]<<8);
2538 inp += 2; /* skip length field */
2541 *chainpp = inp + tlen;
2544 if (lengthp) *lengthp = tlen;
2549 /* set maskp to the mask part of the incoming path.
2550 * Mask is 11 bytes long (8.3 with the dot elided).
2551 * Returns true if succeeds with a valid name, otherwise it does
2552 * its best, but returns false.
2554 int smb_Get8Dot3MaskFromPath(unsigned char *maskp, unsigned char *pathp)
2562 /* starts off valid */
2565 /* mask starts out all blanks */
2566 memset(maskp, ' ', 11);
2568 /* find last backslash, or use whole thing if there is none */
2569 tp = strrchr(pathp, '\\');
2570 if (!tp) tp = pathp;
2571 else tp++; /* skip slash */
2575 /* names starting with a dot are illegal */
2576 if (*tp == '.') valid8Dot3 = 0;
2580 if (tc == 0) return valid8Dot3;
2581 if (tc == '.' || tc == '"') break;
2582 if (i < 8) *up++ = tc;
2583 else valid8Dot3 = 0;
2586 /* if we get here, tp point after the dot */
2587 up = maskp+8; /* ext goes here */
2590 if (tc == 0) return valid8Dot3;
2593 if (tc == '.' || tc == '"') valid8Dot3 = 0;
2595 /* copy extension if not too long */
2596 if (i < 3) *up++ = tc;
2597 else valid8Dot3 = 0;
2601 int smb_Match8Dot3Mask(char *unixNamep, char *maskp)
2611 /* XXX redo this, calling smb_V3MatchMask with a converted mask */
2613 valid = smb_Get8Dot3MaskFromPath(umask, unixNamep);
2614 if (!valid) return 0;
2616 /* otherwise, we have a valid 8.3 name; see if we have a match,
2617 * treating '?' as a wildcard in maskp (but not in the file name).
2619 tp1 = umask; /* real name, in mask format */
2620 tp2 = maskp; /* mask, in mask format */
2621 for(i=0; i<11; i++) {
2622 tc1 = *tp1++; /* char from real name */
2623 tc2 = *tp2++; /* char from mask */
2624 tc1 = (char) cm_foldUpper[(unsigned char)tc1];
2625 tc2 = (char) cm_foldUpper[(unsigned char)tc2];
2626 if (tc1 == tc2) continue;
2627 if (tc2 == '?' && tc1 != ' ') continue;
2628 if (tc2 == '>') continue;
2632 /* we got a match */
2636 char *smb_FindMask(char *pathp)
2640 tp = strrchr(pathp, '\\'); /* find last slash */
2642 if (tp) return tp+1; /* skip the slash */
2643 else return pathp; /* no slash, return the entire path */
2646 long smb_ReceiveCoreSearchVolume(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2648 unsigned char *pathp;
2650 unsigned char mask[11];
2651 unsigned char *statBlockp;
2652 unsigned char initStatBlock[21];
2655 osi_Log0(afsd_logp, "SMB receive search volume");
2657 /* pull pathname and stat block out of request */
2658 tp = smb_GetSMBData(inp, NULL);
2659 pathp = smb_ParseASCIIBlock(tp, (char **) &tp);
2660 osi_assert(pathp != NULL);
2661 statBlockp = smb_ParseVblBlock(tp, (char **) &tp, &statLen);
2662 osi_assert(statBlockp != NULL);
2664 statBlockp = initStatBlock;
2668 /* for returning to caller */
2669 smb_Get8Dot3MaskFromPath(mask, pathp);
2671 smb_SetSMBParm(outp, 0, 1); /* we're returning one entry */
2672 tp = smb_GetSMBData(outp, NULL);
2674 *tp++ = 43; /* bytes in a dir entry */
2675 *tp++ = 0; /* high byte in counter */
2677 /* now marshall the dir entry, starting with the search status */
2678 *tp++ = statBlockp[0]; /* Reserved */
2679 memcpy(tp, mask, 11); tp += 11; /* FileName */
2681 /* now pass back server use info, with 1st byte non-zero */
2683 memset(tp, 0, 4); tp += 4; /* reserved for server use */
2685 memcpy(tp, statBlockp+17, 4); tp += 4; /* reserved for consumer */
2687 *tp++ = 0x8; /* attribute: volume */
2697 /* 4 byte file size */
2703 /* finally, null-terminated 8.3 pathname, which we set to AFS */
2704 memset(tp, ' ', 13);
2707 /* set the length of the data part of the packet to 43 + 3, for the dir
2708 * entry plus the 5 and the length fields.
2710 smb_SetSMBDataLength(outp, 46);
2714 long smb_ApplyDirListPatches(smb_dirListPatch_t **dirPatchespp,
2715 cm_user_t *userp, cm_req_t *reqp)
2723 smb_dirListPatch_t *patchp;
2724 smb_dirListPatch_t *npatchp;
2726 for(patchp = *dirPatchespp; patchp; patchp =
2727 (smb_dirListPatch_t *) osi_QNext(&patchp->q)) {
2728 code = cm_GetSCache(&patchp->fid, &scp, userp, reqp);
2730 lock_ObtainMutex(&scp->mx);
2731 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2732 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2734 lock_ReleaseMutex(&scp->mx);
2735 cm_ReleaseSCache(scp);
2738 dptr = patchp->dptr;
2740 attr = smb_Attributes(scp);
2744 smb_SearchTimeFromUnixTime(&dosTime, scp->clientModTime);
2747 shortTemp = dosTime & 0xffff;
2748 *((u_short *)dptr) = shortTemp;
2751 /* and copy out date */
2752 shortTemp = (dosTime>>16) & 0xffff;
2753 *((u_short *)dptr) = shortTemp;
2756 /* copy out file length */
2757 *((u_long *)dptr) = scp->length.LowPart;
2759 lock_ReleaseMutex(&scp->mx);
2760 cm_ReleaseSCache(scp);
2763 /* now free the patches */
2764 for(patchp = *dirPatchespp; patchp; patchp = npatchp) {
2765 npatchp = (smb_dirListPatch_t *) osi_QNext(&patchp->q);
2769 /* and mark the list as empty */
2770 *dirPatchespp = NULL;
2775 long smb_ReceiveCoreSearchDir(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2784 smb_dirListPatch_t *dirListPatchesp;
2785 smb_dirListPatch_t *curPatchp;
2789 osi_hyper_t dirLength;
2790 osi_hyper_t bufferOffset;
2791 osi_hyper_t curOffset;
2793 unsigned char *inCookiep;
2794 smb_dirSearch_t *dsp;
2798 unsigned long clientCookie;
2799 cm_pageHeader_t *pageHeaderp;
2800 cm_user_t *userp = NULL;
2807 long nextEntryCookie;
2808 int numDirChunks; /* # of 32 byte dir chunks in this entry */
2809 char resByte; /* reserved byte from the cookie */
2810 char *op; /* output data ptr */
2811 char *origOp; /* original value of op */
2812 cm_space_t *spacep; /* for pathname buffer */
2823 maxCount = smb_GetSMBParm(inp, 0);
2825 dirListPatchesp = NULL;
2827 caseFold = CM_FLAG_CASEFOLD;
2829 tp = smb_GetSMBData(inp, NULL);
2830 pathp = smb_ParseASCIIBlock(tp, &tp);
2831 inCookiep = smb_ParseVblBlock(tp, &tp, &dataLength);
2833 /* bail out if request looks bad */
2834 if (!tp || !pathp) {
2835 return CM_ERROR_BADSMB;
2838 /* We can handle long names */
2839 if (vcp->flags & SMB_VCFLAG_USENT)
2840 ((smb_t *)outp)->flg2 |= 0x40; /* IS_LONG_NAME */
2842 /* make sure we got a whole search status */
2843 if (dataLength < 21) {
2844 nextCookie = 0; /* start at the beginning of the dir */
2847 attribute = smb_GetSMBParm(inp, 1);
2849 /* handle volume info in another function */
2850 if (attribute & 0x8)
2851 return smb_ReceiveCoreSearchVolume(vcp, inp, outp);
2853 osi_Log2(afsd_logp, "SMB receive search dir count %d |%s|",
2854 maxCount, osi_LogSaveString(afsd_logp, pathp));
2856 if (*pathp == 0) { /* null pathp, treat as root dir */
2857 if (!(attribute & 0x10)) /* exclude dirs */
2858 return CM_ERROR_NOFILES;
2862 dsp = smb_NewDirSearch(0);
2863 dsp->attribute = attribute;
2864 smb_Get8Dot3MaskFromPath(mask, pathp);
2865 memcpy(dsp->mask, mask, 11);
2867 /* track if this is likely to match a lot of entries */
2868 if (smb_IsStarMask(mask)) starPattern = 1;
2869 else starPattern = 0;
2872 /* pull the next cookie value out of the search status block */
2873 nextCookie = inCookiep[13] + (inCookiep[14]<<8) + (inCookiep[15]<<16)
2874 + (inCookiep[16]<<24);
2875 dsp = smb_FindDirSearch(inCookiep[12]);
2877 /* can't find dir search status; fatal error */
2878 return CM_ERROR_BADFD;
2880 attribute = dsp->attribute;
2881 resByte = inCookiep[0];
2883 /* copy out client cookie, in host byte order. Don't bother
2884 * interpreting it, since we're just passing it through, anyway.
2886 memcpy(&clientCookie, &inCookiep[17], 4);
2888 memcpy(mask, dsp->mask, 11);
2890 /* assume we're doing a star match if it has continued for more
2896 osi_Log3(afsd_logp, "SMB dir search cookie 0x%x, connection %d, attr 0x%x",
2897 nextCookie, dsp->cookie, attribute);
2899 userp = smb_GetUser(vcp, inp);
2901 /* try to get the vnode for the path name next */
2902 lock_ObtainMutex(&dsp->mx);
2909 spacep = inp->spacep;
2910 smb_StripLastComponent(spacep->data, NULL, pathp);
2911 lock_ReleaseMutex(&dsp->mx);
2912 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
2913 code = cm_NameI(cm_rootSCachep, spacep->data,
2914 caseFold | CM_FLAG_FOLLOW, userp, tidPathp, &req, &scp);
2915 lock_ObtainMutex(&dsp->mx);
2917 if (dsp->scp != 0) cm_ReleaseSCache(dsp->scp);
2919 /* we need one hold for the entry we just stored into,
2920 * and one for our own processing. When we're done with this
2921 * function, we'll drop the one for our own processing.
2922 * We held it once from the namei call, and so we do another hold
2926 lock_ObtainMutex(&scp->mx);
2927 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
2928 && LargeIntegerGreaterOrEqualToZero(scp->bulkStatProgress)) {
2929 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
2930 dsp->flags |= SMB_DIRSEARCH_BULKST;
2932 lock_ReleaseMutex(&scp->mx);
2935 lock_ReleaseMutex(&dsp->mx);
2937 cm_ReleaseUser(userp);
2938 smb_DeleteDirSearch(dsp);
2939 smb_ReleaseDirSearch(dsp);
2943 /* reserves space for parameter; we'll adjust it again later to the
2944 * real count of the # of entries we returned once we've actually
2945 * assembled the directory listing.
2947 smb_SetSMBParm(outp, 0, 0);
2949 /* get the directory size */
2950 lock_ObtainMutex(&scp->mx);
2951 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2952 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2954 lock_ReleaseMutex(&scp->mx);
2955 cm_ReleaseSCache(scp);
2956 cm_ReleaseUser(userp);
2957 smb_DeleteDirSearch(dsp);
2958 smb_ReleaseDirSearch(dsp);
2962 dirLength = scp->length;
2964 bufferOffset.LowPart = bufferOffset.HighPart = 0;
2965 curOffset.HighPart = 0;
2966 curOffset.LowPart = nextCookie;
2967 origOp = op = smb_GetSMBData(outp, NULL);
2968 /* and write out the basic header */
2969 *op++ = 5; /* variable block */
2970 op += 2; /* skip vbl block length; we'll fill it in later */
2974 /* make sure that curOffset.LowPart doesn't point to the first
2975 * 32 bytes in the 2nd through last dir page, and that it doesn't
2976 * point at the first 13 32-byte chunks in the first dir page,
2977 * since those are dir and page headers, and don't contain useful
2980 temp = curOffset.LowPart & (2048-1);
2981 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
2982 /* we're in the first page */
2983 if (temp < 13*32) temp = 13*32;
2986 /* we're in a later dir page */
2987 if (temp < 32) temp = 32;
2990 /* make sure the low order 5 bits are zero */
2993 /* now put temp bits back ito curOffset.LowPart */
2994 curOffset.LowPart &= ~(2048-1);
2995 curOffset.LowPart |= temp;
2997 /* check if we've returned all the names that will fit in the
3000 if (returnedNames >= maxCount) break;
3002 /* check if we've passed the dir's EOF */
3003 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength)) break;
3005 /* see if we can use the bufferp we have now; compute in which page
3006 * the current offset would be, and check whether that's the offset
3007 * of the buffer we have. If not, get the buffer.
3009 thyper.HighPart = curOffset.HighPart;
3010 thyper.LowPart = curOffset.LowPart & ~(buf_bufferSize-1);
3011 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
3014 buf_Release(bufferp);
3017 lock_ReleaseMutex(&scp->mx);
3018 lock_ObtainRead(&scp->bufCreateLock);
3019 code = buf_Get(scp, &thyper, &bufferp);
3020 lock_ReleaseRead(&scp->bufCreateLock);
3022 /* now, if we're doing a star match, do bulk fetching of all of
3023 * the status info for files in the dir.
3026 smb_ApplyDirListPatches(&dirListPatchesp, userp,
3028 if ((dsp->flags & SMB_DIRSEARCH_BULKST)
3029 && LargeIntegerGreaterThanOrEqualTo(thyper,
3030 scp->bulkStatProgress)) {
3031 /* Don't bulk stat if risking timeout */
3032 int now = GetCurrentTime();
3033 if (now - req.startTime > 5000) {
3034 scp->bulkStatProgress = thyper;
3035 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
3036 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
3038 cm_TryBulkStat(scp, &thyper,
3043 lock_ObtainMutex(&scp->mx);
3045 bufferOffset = thyper;
3047 /* now get the data in the cache */
3049 code = cm_SyncOp(scp, bufferp, userp, &req,
3051 CM_SCACHESYNC_NEEDCALLBACK
3052 | CM_SCACHESYNC_READ);
3055 if (cm_HaveBuffer(scp, bufferp, 0)) break;
3057 /* otherwise, load the buffer and try again */
3058 code = cm_GetBuffer(scp, bufferp, NULL, userp,
3063 buf_Release(bufferp);
3067 } /* if (wrong buffer) ... */
3069 /* now we have the buffer containing the entry we're interested in; copy
3070 * it out if it represents a non-deleted entry.
3072 entryInDir = curOffset.LowPart & (2048-1);
3073 entryInBuffer = curOffset.LowPart & (buf_bufferSize - 1);
3075 /* page header will help tell us which entries are free. Page header
3076 * can change more often than once per buffer, since AFS 3 dir page size
3077 * may be less than (but not more than a buffer package buffer.
3079 temp = curOffset.LowPart & (buf_bufferSize - 1); /* only look intra-buffer */
3080 temp &= ~(2048 - 1); /* turn off intra-page bits */
3081 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
3083 /* now determine which entry we're looking at in the page. If it is
3084 * free (there's a free bitmap at the start of the dir), we should
3085 * skip these 32 bytes.
3087 slotInPage = (entryInDir & 0x7e0) >> 5;
3088 if (!(pageHeaderp->freeBitmap[slotInPage>>3] & (1 << (slotInPage & 0x7)))) {
3089 /* this entry is free */
3090 numDirChunks = 1; /* only skip this guy */
3094 tp = bufferp->datap + entryInBuffer;
3095 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
3097 /* while we're here, compute the next entry's location, too,
3098 * since we'll need it when writing out the cookie into the dir
3101 * XXXX Probably should do more sanity checking.
3103 numDirChunks = cm_NameEntries(dep->name, NULL);
3105 /* compute the offset of the cookie representing the next entry */
3106 nextEntryCookie = curOffset.LowPart + (CM_DIR_CHUNKSIZE * numDirChunks);
3108 /* Compute 8.3 name if necessary */
3109 actualName = dep->name;
3110 if (dep->fid.vnode != 0 && !cm_Is8Dot3(actualName)) {
3111 cm_Gen8Dot3Name(dep, shortName, &shortNameEnd);
3112 actualName = shortName;
3115 if (dep->fid.vnode != 0 && smb_Match8Dot3Mask(actualName, mask)) {
3116 /* this is one of the entries to use: it is not deleted
3117 * and it matches the star pattern we're looking for.
3120 /* Eliminate entries that don't match requested
3122 if (!(dsp->attribute & 0x10)) /* no directories */
3124 /* We have already done the cm_TryBulkStat above */
3125 fid.cell = scp->fid.cell;
3126 fid.volume = scp->fid.volume;
3127 fid.vnode = ntohl(dep->fid.vnode);
3128 fid.unique = ntohl(dep->fid.unique);
3129 fileType = cm_FindFileType(&fid);
3130 osi_Log2(afsd_logp, "smb_ReceiveCoreSearchDir: file %s "
3131 "has filetype %d", dep->name,
3133 if (fileType == CM_SCACHETYPE_DIRECTORY)
3138 memcpy(op, mask, 11); op += 11;
3139 *op++ = (char) dsp->cookie; /* they say it must be non-zero */
3140 *op++ = nextEntryCookie & 0xff;
3141 *op++ = (nextEntryCookie>>8) & 0xff;
3142 *op++ = (nextEntryCookie>>16) & 0xff;
3143 *op++ = (nextEntryCookie>>24) & 0xff;
3144 memcpy(op, &clientCookie, 4); op += 4;
3146 /* now we emit the attribute. This is sort of tricky,
3147 * since we need to really stat the file to find out
3148 * what type of entry we've got. Right now, we're
3149 * copying out data from a buffer, while holding the
3150 * scp locked, so it isn't really convenient to stat
3151 * something now. We'll put in a place holder now,
3152 * and make a second pass before returning this to get
3153 * the real attributes. So, we just skip the data for
3154 * now, and adjust it later. We allocate a patch
3155 * record to make it easy to find this point later.
3156 * The replay will happen at a time when it is safe to
3157 * unlock the directory.
3159 curPatchp = malloc(sizeof(*curPatchp));
3160 osi_QAdd((osi_queue_t **) &dirListPatchesp, &curPatchp->q);
3161 curPatchp->dptr = op;
3162 curPatchp->fid.cell = scp->fid.cell;
3163 curPatchp->fid.volume = scp->fid.volume;
3164 curPatchp->fid.vnode = ntohl(dep->fid.vnode);
3165 curPatchp->fid.unique = ntohl(dep->fid.unique);
3166 op += 9; /* skip attr, time, date and size */
3168 /* zero out name area. The spec says to pad with
3169 * spaces, but Samba doesn't, and neither do we.
3173 /* finally, we get to copy out the name; we know that
3174 * it fits in 8.3 or the pattern wouldn't match, but it
3175 * never hurts to be sure.
3177 strncpy(op, actualName, 13);
3179 /* Uppercase if requested by client */
3180 if ((((smb_t *)inp)->flg2 & 1) == 0)
3185 /* now, adjust the # of entries copied */
3187 } /* if we're including this name */
3190 /* and adjust curOffset to be where the new cookie is */
3191 thyper.HighPart = 0;
3192 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
3193 curOffset = LargeIntegerAdd(thyper, curOffset);
3194 } /* while copying data for dir listing */
3196 /* release the mutex */
3197 lock_ReleaseMutex(&scp->mx);
3198 if (bufferp) buf_Release(bufferp);
3200 /* apply and free last set of patches; if not doing a star match, this
3201 * will be empty, but better safe (and freeing everything) than sorry.
3203 smb_ApplyDirListPatches(&dirListPatchesp, userp, &req);
3205 /* special return code for unsuccessful search */
3206 if (code == 0 && dataLength < 21 && returnedNames == 0)
3207 code = CM_ERROR_NOFILES;
3209 osi_Log2(afsd_logp, "SMB search dir done, %d names, code %d",
3210 returnedNames, code);
3213 smb_DeleteDirSearch(dsp);
3214 smb_ReleaseDirSearch(dsp);
3215 cm_ReleaseSCache(scp);
3216 cm_ReleaseUser(userp);
3220 /* finalize the output buffer */
3221 smb_SetSMBParm(outp, 0, returnedNames);
3222 temp = (long) (op - origOp);
3223 smb_SetSMBDataLength(outp, temp);
3225 /* the data area is a variable block, which has a 5 (already there)
3226 * followed by the length of the # of data bytes. We now know this to
3227 * be "temp," although that includes the 3 bytes of vbl block header.
3228 * Deduct for them and fill in the length field.
3230 temp -= 3; /* deduct vbl block info */
3231 osi_assert(temp == (43 * returnedNames));
3232 origOp[1] = temp & 0xff;
3233 origOp[2] = (temp>>8) & 0xff;
3234 if (returnedNames == 0) smb_DeleteDirSearch(dsp);
3235 smb_ReleaseDirSearch(dsp);
3236 cm_ReleaseSCache(scp);
3237 cm_ReleaseUser(userp);
3241 /* verify that this is a valid path to a directory. I don't know why they
3242 * don't use the get file attributes call.
3244 long smb_ReceiveCoreCheckPath(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3248 cm_scache_t *rootScp;
3249 cm_scache_t *newScp;
3258 pathp = smb_GetSMBData(inp, NULL);
3259 pathp = smb_ParseASCIIBlock(pathp, NULL);
3260 osi_Log1(afsd_logp, "SMB receive check path %s",
3261 osi_LogSaveString(afsd_logp, pathp));
3264 return CM_ERROR_BADFD;
3267 rootScp = cm_rootSCachep;
3269 userp = smb_GetUser(vcp, inp);
3271 caseFold = CM_FLAG_CASEFOLD;
3273 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3274 code = cm_NameI(rootScp, pathp,
3275 caseFold | CM_FLAG_FOLLOW | CM_FLAG_CHECKPATH,
3276 userp, tidPathp, &req, &newScp);
3279 cm_ReleaseUser(userp);
3283 /* now lock the vnode with a callback; returns with newScp locked */
3284 lock_ObtainMutex(&newScp->mx);
3285 code = cm_SyncOp(newScp, NULL, userp, &req, PRSFS_LOOKUP,
3286 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
3287 if (code && code != CM_ERROR_NOACCESS) {
3288 lock_ReleaseMutex(&newScp->mx);
3289 cm_ReleaseSCache(newScp);
3290 cm_ReleaseUser(userp);
3294 attrs = smb_Attributes(newScp);
3296 if (!(attrs & 0x10))
3297 code = CM_ERROR_NOTDIR;
3299 lock_ReleaseMutex(&newScp->mx);
3301 cm_ReleaseSCache(newScp);
3302 cm_ReleaseUser(userp);
3306 long smb_ReceiveCoreSetFileAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3310 cm_scache_t *rootScp;
3311 unsigned short attribute;
3313 cm_scache_t *newScp;
3322 /* decode basic attributes we're passed */
3323 attribute = smb_GetSMBParm(inp, 0);
3324 dosTime = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
3326 pathp = smb_GetSMBData(inp, NULL);
3327 pathp = smb_ParseASCIIBlock(pathp, NULL);
3330 return CM_ERROR_BADSMB;
3333 osi_Log2(afsd_logp, "SMB receive setfile attributes time %d, attr 0x%x",
3334 dosTime, attribute);
3336 rootScp = cm_rootSCachep;
3338 userp = smb_GetUser(vcp, inp);
3340 caseFold = CM_FLAG_CASEFOLD;
3342 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3343 code = cm_NameI(rootScp, pathp, caseFold | CM_FLAG_FOLLOW, userp,
3344 tidPathp, &req, &newScp);
3347 cm_ReleaseUser(userp);
3351 /* now lock the vnode with a callback; returns with newScp locked; we
3352 * need the current status to determine what the new status is, in some
3355 lock_ObtainMutex(&newScp->mx);
3356 code = cm_SyncOp(newScp, NULL, userp, &req, 0,
3357 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
3359 lock_ReleaseMutex(&newScp->mx);
3360 cm_ReleaseSCache(newScp);
3361 cm_ReleaseUser(userp);
3365 /* Check for RO volume */
3366 if (newScp->flags & CM_SCACHEFLAG_RO) {
3367 lock_ReleaseMutex(&newScp->mx);
3368 cm_ReleaseSCache(newScp);
3369 cm_ReleaseUser(userp);
3370 return CM_ERROR_READONLY;
3373 /* prepare for setattr call */
3376 attr.mask |= CM_ATTRMASK_CLIENTMODTIME;
3377 smb_UnixTimeFromDosUTime(&attr.clientModTime, dosTime);
3379 if ((newScp->unixModeBits & 0222) && (attribute & 1) != 0) {
3380 /* we're told to make a writable file read-only */
3381 attr.unixModeBits = newScp->unixModeBits & ~0222;
3382 attr.mask |= CM_ATTRMASK_UNIXMODEBITS;
3384 else if ((newScp->unixModeBits & 0222) == 0 && (attribute & 1) == 0) {
3385 /* we're told to make a read-only file writable */
3386 attr.unixModeBits = newScp->unixModeBits | 0222;
3387 attr.mask |= CM_ATTRMASK_UNIXMODEBITS;
3389 lock_ReleaseMutex(&newScp->mx);
3391 /* now call setattr */
3393 code = cm_SetAttr(newScp, &attr, userp, &req);
3397 cm_ReleaseSCache(newScp);
3398 cm_ReleaseUser(userp);
3403 long smb_ReceiveCoreGetFileAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3407 cm_scache_t *rootScp;
3408 cm_scache_t *newScp, *dscp;
3420 pathp = smb_GetSMBData(inp, NULL);
3421 pathp = smb_ParseASCIIBlock(pathp, NULL);
3424 return CM_ERROR_BADSMB;