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 "lanahelper.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 /* TODO; logout mechanism needs to be thread-safe */
45 char *loggedOutName = NULL;
46 smb_user_t *loggedOutUserp = NULL;
47 unsigned long loggedOutTime;
50 int smbShutdownFlag = 0;
53 int smb_LogoffTokenTransfer;
54 unsigned long smb_LogoffTransferTimeout;
56 DWORD last_msg_time = 0;
60 unsigned int sessionGen = 0;
62 extern void afsi_log(char *pattern, ...);
63 extern HANDLE afsi_file;
65 osi_hyper_t hzero = {0, 0};
66 osi_hyper_t hones = {0xFFFFFFFF, -1};
69 osi_rwlock_t smb_globalLock;
70 osi_rwlock_t smb_rctLock;
71 osi_mutex_t smb_ListenerLock;
74 unsigned char smb_sharename[NCBNAMSZ+1] = {0};
77 long smb_maxObsConcurrentCalls=0;
78 long smb_concurrentCalls=0;
80 smb_dispatch_t smb_dispatchTable[SMB_NOPCODES];
82 smb_packet_t *smb_packetFreeListp;
83 smb_ncb_t *smb_ncbFreeListp;
85 int smb_NumServerThreads;
87 int numNCBs, numSessions;
89 int smb_maxVCPerServer;
90 int smb_maxMpxRequests;
92 #define NCBmax MAXIMUM_WAIT_OBJECTS
93 EVENT_HANDLE NCBavails[NCBmax], NCBevents[NCBmax];
94 EVENT_HANDLE **NCBreturns;
95 DWORD NCBsessions[NCBmax];
97 struct smb_packet *bufs[NCBmax];
99 #define Sessionmax MAXIMUM_WAIT_OBJECTS
100 EVENT_HANDLE SessionEvents[Sessionmax];
101 unsigned short LSNs[Sessionmax];
102 int lanas[Sessionmax];
103 BOOL dead_sessions[Sessionmax];
107 osi_mutex_t smb_RawBufLock;
109 #define SMB_RAW_BUFS 4
111 int smb_RawBufSel[SMB_RAW_BUFS];
116 #define RAWTIMEOUT INFINITE
119 typedef struct raw_write_cont {
132 /* dir search stuff */
133 long smb_dirSearchCounter = 1;
134 smb_dirSearch_t *smb_firstDirSearchp;
135 smb_dirSearch_t *smb_lastDirSearchp;
137 /* hide dot files? */
138 int smb_hideDotFiles;
140 /* global state about V3 protocols */
141 int smb_useV3; /* try to negotiate V3 */
144 /* MessageBox or something like it */
145 int (WINAPI *smb_MBfunc)(HWND, LPCTSTR, LPCTSTR, UINT) = NULL;
149 * Time in Unix format of midnight, 1/1/1970 local time.
150 * When added to dosUTime, gives Unix (AFS) time.
154 /* Time difference for converting to kludge-GMT */
157 char *smb_localNamep = NULL;
159 smb_vc_t *smb_allVCsp;
161 smb_username_t *usernamesp = NULL;
163 smb_waitingLock_t *smb_allWaitingLocks;
166 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
167 NCB *ncbp, raw_write_cont_t *rwcp);
168 void smb_NetbiosInit();
170 #ifndef AFS_WIN95_ENV
171 DWORD smb_ServerExceptionFilter(void);
174 extern char cm_HostName[];
175 extern char cm_confDir[];
179 #define LPTSTR char *
180 #define GetComputerName(str, sizep) \
181 strcpy((str), cm_HostName); \
182 *(sizep) = strlen(cm_HostName)
185 extern char AFSConfigKeyName[];
190 * To build an expiring version, comment out the definition of NOEXPIRE,
191 * and set the definition of EXPIREDATE to the desired value.
194 #define EXPIREDATE 834000000 /* Wed Jun 5 1996 */
197 char * myCrt_Dispatch(int i)
202 return "unknown SMB op";
204 return "(00)ReceiveCoreMakeDir";
206 return "(01)ReceiveCoreRemoveDir";
208 return "(02)ReceiveCoreOpen";
210 return "(03)ReceiveCoreCreate";
212 return "(04)ReceiveCoreClose";
214 return "(05)ReceiveCoreFlush";
216 return "(06)ReceiveCoreUnlink";
218 return "(07)ReceiveCoreRename";
220 return "(08)ReceiveCoreGetFileAttributes";
222 return "(09)ReceiveCoreSetFileAttributes";
224 return "(0a)ReceiveCoreRead";
226 return "(0b)ReceiveCoreWrite";
228 return "(0c)ReceiveCoreLockRecord";
230 return "(0d)ReceiveCoreUnlockRecord";
232 return "(0e)SendCoreBadOp";
234 return "(0f)ReceiveCoreCreate";
236 return "(10)ReceiveCoreCheckPath";
238 return "(11)SendCoreBadOp";
240 return "(12)ReceiveCoreSeek";
242 return "(1a)ReceiveCoreReadRaw";
244 return "(1d)ReceiveCoreWriteRawDummy";
246 return "(22)ReceiveV3SetAttributes";
248 return "(23)ReceiveV3GetAttributes";
250 return "(24)ReceiveV3LockingX";
252 return "(29)SendCoreBadOp";
254 return "(2b)ReceiveCoreEcho";
256 return "(2d)ReceiveV3OpenX";
258 return "(2e)ReceiveV3ReadX";
260 return "(32)ReceiveV3Tran2A";
262 return "(33)ReceiveV3Tran2A";
264 return "(34)ReceiveV3FindClose";
266 return "(35)ReceiveV3FindNotifyClose";
268 return "(70)ReceiveCoreTreeConnect";
270 return "(71)ReceiveCoreTreeDisconnect";
272 return "(72)ReceiveNegotiate";
274 return "(73)ReceiveV3SessionSetupX";
276 return "(74)ReceiveV3UserLogoffX";
278 return "(75)ReceiveV3TreeConnectX";
280 return "(80)ReceiveCoreGetDiskAttributes";
282 return "(81)ReceiveCoreSearchDir";
284 return "(A0)ReceiveNTTransact";
286 return "(A2)ReceiveNTCreateX";
288 return "(A4)ReceiveNTCancel";
290 return "(c0)SendCoreBadOp";
292 return "(c1)SendCoreBadOp";
294 return "(c2)SendCoreBadOp";
296 return "(c3)SendCoreBadOp";
300 char * myCrt_2Dispatch(int i)
305 return "unknown SMB op-2";
307 return "S(00)CreateFile";
309 return "S(01)FindFirst";
311 return "S(02)FindNext"; /* FindNext */
313 return "S(03)QueryFileSystem_ReceiveTran2QFSInfo";
317 return "S(05)QueryFileInfo_ReceiveTran2QPathInfo";
319 return "S(06)SetFileInfo_ReceiveTran2SetPathInfo";
321 return "S(07)SetInfoHandle_ReceiveTran2QFileInfo";
323 return "S(08)??_ReceiveTran2SetFileInfo";
325 return "S(09)??_ReceiveTran2FSCTL";
327 return "S(0a)_ReceiveTran2IOCTL";
329 return "S(0b)_ReceiveTran2FindNotifyFirst";
331 return "S(0c)_ReceiveTran2FindNotifyNext";
333 return "S(0d)CreateDirectory_ReceiveTran2MKDir";
337 /* scache must be locked */
338 unsigned int smb_Attributes(cm_scache_t *scp)
342 if (scp->fileType == CM_SCACHETYPE_DIRECTORY
343 || scp->fileType == CM_SCACHETYPE_MOUNTPOINT)
344 attrs = SMB_ATTR_DIRECTORY;
349 * We used to mark a file RO if it was in an RO volume, but that
350 * turns out to be impolitic in NT. See defect 10007.
353 if ((scp->unixModeBits & 0222) == 0 || (scp->flags & CM_SCACHEFLAG_RO))
355 if ((scp->unixModeBits & 0222) == 0)
356 attrs |= SMB_ATTR_READONLY; /* turn on read-only flag */
361 /* Check if the named file/dir is a dotfile/dotdir */
362 /* String pointed to by lastComp can have leading slashes, but otherwise should have
363 no other patch components */
364 unsigned int smb_IsDotFile(char *lastComp) {
367 /* skip over slashes */
368 for(s=lastComp;*s && (*s == '\\' || *s == '/'); s++);
373 /* nulls, curdir and parent dir doesn't count */
376 if(!*(s + 1)) return 0;
377 if(*(s+1) == '.' && !*(s + 2)) return 0;
383 static int ExtractBits(WORD bits, short start, short len)
390 num = bits << (16 - end);
391 num = num >> ((16 - end) + start);
397 void ShowUnixTime(char *FuncName, long unixTime)
402 smb_LargeSearchTimeFromUnixTime(&ft, unixTime);
404 if (!FileTimeToDosDateTime(&ft, &wDate, &wTime))
405 osi_Log1(afsd_logp, "Failed to convert filetime to dos datetime: %d", GetLastError());
407 int day, month, year, sec, min, hour;
410 day = ExtractBits(wDate, 0, 5);
411 month = ExtractBits(wDate, 5, 4);
412 year = ExtractBits(wDate, 9, 7) + 1980;
414 sec = ExtractBits(wTime, 0, 5);
415 min = ExtractBits(wTime, 5, 6);
416 hour = ExtractBits(wTime, 11, 5);
418 sprintf(msg, "%s = %02d-%02d-%04d %02d:%02d:%02d", FuncName, month, day, year, hour, min, sec);
419 osi_Log1(afsd_logp, "%s", osi_LogSaveString(afsd_logp, msg));
425 /* Determine if we are observing daylight savings time */
426 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
428 TIME_ZONE_INFORMATION timeZoneInformation;
429 SYSTEMTIME utc, local, localDST;
431 /* Get the time zone info. NT uses this to calc if we are in DST. */
432 GetTimeZoneInformation(&timeZoneInformation);
434 /* Return the daylight bias */
435 *pDstBias = timeZoneInformation.DaylightBias;
437 /* Return the bias */
438 *pBias = timeZoneInformation.Bias;
440 /* Now determine if DST is being observed */
442 /* Get the UTC (GMT) time */
445 /* Convert UTC time to local time using the time zone info. If we are
446 observing DST, the calculated local time will include this.
448 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &localDST);
450 /* Set the daylight bias to 0. The daylight bias is the amount of change
451 in time that we use for daylight savings time. By setting this to 0
452 we cause there to be no change in time during daylight savings time.
454 timeZoneInformation.DaylightBias = 0;
456 /* Convert the utc time to local time again, but this time without any
457 adjustment for daylight savings time.
459 SystemTimeToTzSpecificLocalTime(&timeZoneInformation, &utc, &local);
461 /* If the two times are different, then it means that the localDST that
462 we calculated includes the daylight bias, and therefore we are
463 observing daylight savings time.
465 *pDST = localDST.wHour != local.wHour;
468 /* Determine if we are observing daylight savings time */
469 void GetTimeZoneInfo(BOOL *pDST, LONG *pDstBias, LONG *pBias)
475 *pDstBias = -60; /* where can this be different? */
481 void CompensateForSmbClientLastWriteTimeBugs(long *pLastWriteTime)
483 BOOL dst; /* Will be TRUE if observing DST */
484 LONG dstBias; /* Offset from local time if observing DST */
485 LONG bias; /* Offset from GMT for local time */
488 * This function will adjust the last write time to compensate
489 * for two bugs in the smb client:
491 * 1) During Daylight Savings Time, the LastWriteTime is ahead
492 * in time by the DaylightBias (ignoring the sign - the
493 * DaylightBias is always stored as a negative number). If
494 * the DaylightBias is -60, then the LastWriteTime will be
495 * ahead by 60 minutes.
497 * 2) If the local time zone is a positive offset from GMT, then
498 * the LastWriteTime will be the correct local time plus the
499 * Bias (ignoring the sign - a positive offset from GMT is
500 * always stored as a negative Bias). If the Bias is -120,
501 * then the LastWriteTime will be ahead by 120 minutes.
503 * These bugs can occur at the same time.
506 GetTimeZoneInfo(&dst, &dstBias, &bias);
508 /* First adjust for DST */
510 *pLastWriteTime -= (-dstBias * 60); /* Convert dstBias to seconds */
512 /* Now adjust for a positive offset from GMT (a negative bias). */
514 *pLastWriteTime -= (-bias * 60); /* Convert bias to seconds */
518 * Calculate the difference (in seconds) between local time and GMT.
519 * This enables us to convert file times to kludge-GMT.
525 struct tm gmt_tm, local_tm;
526 int days, hours, minutes, seconds;
529 gmt_tm = *(gmtime(&t));
530 local_tm = *(localtime(&t));
532 days = local_tm.tm_yday - gmt_tm.tm_yday;
533 hours = 24 * days + local_tm.tm_hour - gmt_tm.tm_hour;
534 minutes = 60 * hours + local_tm.tm_min - gmt_tm.tm_min;
535 seconds = 60 * minutes + local_tm.tm_sec - gmt_tm.tm_sec;
541 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
546 long ersatz_unixTime;
549 * Must use kludge-GMT instead of real GMT.
550 * kludge-GMT is computed by adding time zone difference to localtime.
553 * ltp = gmtime(&unixTime);
555 ersatz_unixTime = unixTime - smb_NowTZ;
556 ltp = localtime(&ersatz_unixTime);
558 /* if we fail, make up something */
561 localJunk.tm_year = 89 - 20;
562 localJunk.tm_mon = 4;
563 localJunk.tm_mday = 12;
564 localJunk.tm_hour = 0;
565 localJunk.tm_min = 0;
566 localJunk.tm_sec = 0;
569 stm.wYear = ltp->tm_year + 1900;
570 stm.wMonth = ltp->tm_mon + 1;
571 stm.wDayOfWeek = ltp->tm_wday;
572 stm.wDay = ltp->tm_mday;
573 stm.wHour = ltp->tm_hour;
574 stm.wMinute = ltp->tm_min;
575 stm.wSecond = ltp->tm_sec;
576 stm.wMilliseconds = 0;
578 SystemTimeToFileTime(&stm, largeTimep);
581 void smb_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, long unixTime)
583 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
584 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 ??? */
585 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
587 int leap_years = 89; /* leap years betw 1/1/1601 and 1/1/1970 */
589 /* set ft to number of 100ns intervals betw 1/1/1601 and 1/1/1970 GMT */
590 *ft = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years)
592 *ft = LargeIntegerMultiplyByLong(*ft, 60);
593 *ft = LargeIntegerMultiplyByLong(*ft, 10000000);
596 ut = ConvertLongToLargeInteger(unixTime);
597 ut = LargeIntegerMultiplyByLong(ut, 10000000);
598 *ft = LargeIntegerAdd(*ft, ut);
603 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
609 FileTimeToSystemTime(largeTimep, &stm);
611 lt.tm_year = stm.wYear - 1900;
612 lt.tm_mon = stm.wMonth - 1;
613 lt.tm_wday = stm.wDayOfWeek;
614 lt.tm_mday = stm.wDay;
615 lt.tm_hour = stm.wHour;
616 lt.tm_min = stm.wMinute;
617 lt.tm_sec = stm.wSecond;
620 save_timezone = _timezone;
621 _timezone += smb_NowTZ;
622 *unixTimep = mktime(<);
623 _timezone = save_timezone;
626 void smb_UnixTimeFromLargeSearchTime(long *unixTimep, FILETIME *largeTimep)
628 /* unixTime: seconds since 1/1/1970 00:00:00 GMT */
629 /* FILETIME: 100ns intervals since 1/1/1601 00:00:00 GMT? */
630 LARGE_INTEGER *ft = (LARGE_INTEGER *) largeTimep;
634 /* set to number of 100ns intervals betw 1/1/1601 and 1/1/1970 */
635 a = ConvertLongToLargeInteger(((EPOCH_YEAR-1601) * 365 + leap_years) * 24 * 60);
636 a = LargeIntegerMultiplyByLong(a, 60);
637 a = LargeIntegerMultiplyByLong(a, 10000000);
639 /* subtract it from ft */
640 a = LargeIntegerSubtract(*ft, a);
642 /* divide down to seconds */
643 *unixTimep = LargeIntegerDivideByLong(a, 10000000);
647 void smb_SearchTimeFromUnixTime(long *dosTimep, long unixTime)
654 ltp = localtime((time_t*) &unixTime);
656 /* if we fail, make up something */
659 localJunk.tm_year = 89 - 20;
660 localJunk.tm_mon = 4;
661 localJunk.tm_mday = 12;
662 localJunk.tm_hour = 0;
663 localJunk.tm_min = 0;
664 localJunk.tm_sec = 0;
667 dosDate = ((ltp->tm_year-80)<<9) | ((ltp->tm_mon+1) << 5) | (ltp->tm_mday);
668 dosTime = (ltp->tm_hour<<11) | (ltp->tm_min << 5) | (ltp->tm_sec / 2);
669 *dosTimep = (dosDate<<16) | dosTime;
672 void smb_UnixTimeFromSearchTime(long *unixTimep, long searchTime)
674 unsigned short dosDate;
675 unsigned short dosTime;
678 dosDate = searchTime & 0xffff;
679 dosTime = (searchTime >> 16) & 0xffff;
681 localTm.tm_year = 80 + ((dosDate>>9) & 0x3f);
682 localTm.tm_mon = ((dosDate >> 5) & 0xf) - 1; /* January is 0 in localTm */
683 localTm.tm_mday = (dosDate) & 0x1f;
684 localTm.tm_hour = (dosTime>>11) & 0x1f;
685 localTm.tm_min = (dosTime >> 5) & 0x3f;
686 localTm.tm_sec = (dosTime & 0x1f) * 2;
687 localTm.tm_isdst = -1; /* compute whether DST in effect */
689 *unixTimep = mktime(&localTm);
692 void smb_DosUTimeFromUnixTime(long *dosUTimep, long unixTime)
694 *dosUTimep = unixTime - smb_localZero;
697 void smb_UnixTimeFromDosUTime(long *unixTimep, long dosTime)
700 *unixTimep = dosTime + smb_localZero;
702 /* dosTime seems to be already adjusted for GMT */
703 *unixTimep = dosTime;
707 smb_vc_t *smb_FindVC(unsigned short lsn, int flags, int lana)
711 lock_ObtainWrite(&smb_rctLock);
712 for(vcp = smb_allVCsp; vcp; vcp=vcp->nextp) {
713 if (lsn == vcp->lsn && lana == vcp->lana) {
718 if (!vcp && (flags & SMB_FLAG_CREATE)) {
719 vcp = malloc(sizeof(*vcp));
720 memset(vcp, 0, sizeof(*vcp));
724 vcp->uidCounter = 1; /* UID 0 is reserved for blank user */
725 vcp->nextp = smb_allVCsp;
727 lock_InitializeMutex(&vcp->mx, "vc_t mutex");
731 lock_ReleaseWrite(&smb_rctLock);
735 int smb_IsStarMask(char *maskp)
740 for(i=0; i<11; i++) {
742 if (tc == '?' || tc == '*' || tc == '>') return 1;
747 void smb_ReleaseVC(smb_vc_t *vcp)
749 lock_ObtainWrite(&smb_rctLock);
750 osi_assert(vcp->refCount-- > 0);
751 lock_ReleaseWrite(&smb_rctLock);
754 void smb_HoldVC(smb_vc_t *vcp)
756 lock_ObtainWrite(&smb_rctLock);
758 lock_ReleaseWrite(&smb_rctLock);
761 smb_tid_t *smb_FindTID(smb_vc_t *vcp, unsigned short tid, int flags)
765 lock_ObtainWrite(&smb_rctLock);
766 for(tidp = vcp->tidsp; tidp; tidp = tidp->nextp) {
767 if (tid == tidp->tid) {
772 if (!tidp && (flags & SMB_FLAG_CREATE)) {
773 tidp = malloc(sizeof(*tidp));
774 memset(tidp, 0, sizeof(*tidp));
775 tidp->nextp = vcp->tidsp;
779 lock_InitializeMutex(&tidp->mx, "tid_t mutex");
782 lock_ReleaseWrite(&smb_rctLock);
786 void smb_ReleaseTID(smb_tid_t *tidp)
793 lock_ObtainWrite(&smb_rctLock);
794 osi_assert(tidp->refCount-- > 0);
795 if (tidp->refCount == 0 && (tidp->flags & SMB_TIDFLAG_DELETE)) {
796 ltpp = &tidp->vcp->tidsp;
797 for(tp = *ltpp; tp; ltpp = &tp->nextp, tp = *ltpp) {
798 if (tp == tidp) break;
800 osi_assert(tp != NULL);
802 lock_FinalizeMutex(&tidp->mx);
803 userp = tidp->userp; /* remember to drop ref later */
805 lock_ReleaseWrite(&smb_rctLock);
807 cm_ReleaseUser(userp);
811 smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags)
813 smb_user_t *uidp = NULL;
815 lock_ObtainWrite(&smb_rctLock);
816 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
817 if (uid == uidp->userID) {
819 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 : "");
823 if (!uidp && (flags & SMB_FLAG_CREATE)) {
824 uidp = malloc(sizeof(*uidp));
825 memset(uidp, 0, sizeof(*uidp));
826 uidp->nextp = vcp->usersp;
830 lock_InitializeMutex(&uidp->mx, "uid_t mutex");
832 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 : ""));
834 lock_ReleaseWrite(&smb_rctLock);
838 smb_username_t *smb_FindUserByName(char *usern, char *machine, int flags)
840 smb_username_t *unp= NULL;
842 lock_ObtainWrite(&smb_rctLock);
843 for(unp = usernamesp; unp; unp = unp->nextp) {
844 if (stricmp(unp->name, usern) == 0 &&
845 stricmp(unp->machine, machine) == 0) {
850 if (!unp && (flags & SMB_FLAG_CREATE)) {
851 unp = malloc(sizeof(*unp));
852 memset(unp, 0, sizeof(*unp));
854 unp->nextp = usernamesp;
855 unp->name = strdup(usern);
856 unp->machine = strdup(machine);
858 lock_InitializeMutex(&unp->mx, "username_t mutex");
860 lock_ReleaseWrite(&smb_rctLock);
864 smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern)
866 smb_user_t *uidp= NULL;
868 lock_ObtainWrite(&smb_rctLock);
869 for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
872 if (stricmp(uidp->unp->name, usern) == 0) {
874 osi_LogEvent("AFS smb_FindUserByNameThisSession",NULL,"VCP[%x] uid[%d] match-name[%s]",(int)vcp,uidp->userID,usern);
879 lock_ReleaseWrite(&smb_rctLock);
882 void smb_ReleaseUID(smb_user_t *uidp)
889 lock_ObtainWrite(&smb_rctLock);
890 osi_assert(uidp->refCount-- > 0);
891 if (uidp->refCount == 0 && (uidp->flags & SMB_USERFLAG_DELETE)) {
892 lupp = &uidp->vcp->usersp;
893 for(up = *lupp; up; lupp = &up->nextp, up = *lupp) {
894 if (up == uidp) break;
896 osi_assert(up != NULL);
898 lock_FinalizeMutex(&uidp->mx);
900 userp = uidp->unp->userp; /* remember to drop ref later */
902 lock_ReleaseWrite(&smb_rctLock);
904 cm_ReleaseUserVCRef(userp);
905 cm_ReleaseUser(userp);
909 /* retrieve a held reference to a user structure corresponding to an incoming
911 * corresponding release function is cm_ReleaseUser.
913 cm_user_t *smb_GetUser(smb_vc_t *vcp, smb_packet_t *inp)
919 smbp = (smb_t *) inp;
920 uidp = smb_FindUID(vcp, smbp->uid, 0);
921 if ((!uidp) || (!uidp->unp))
924 lock_ObtainMutex(&uidp->mx);
925 up = uidp->unp->userp;
927 lock_ReleaseMutex(&uidp->mx);
929 smb_ReleaseUID(uidp);
935 * Return a pointer to a pathname extracted from a TID structure. The
936 * TID structure is not held; assume it won't go away.
938 char *smb_GetTIDPath(smb_vc_t *vcp, unsigned short tid)
943 tidp = smb_FindTID(vcp, tid, 0);
946 tpath = tidp->pathname;
947 smb_ReleaseTID(tidp);
951 /* check to see if we have a chained fid, that is, a fid that comes from an
952 * OpenAndX message that ran earlier in this packet. In this case, the fid
953 * field in a read, for example, request, isn't set, since the value is
954 * supposed to be inherited from the openAndX call.
956 int smb_ChainFID(int fid, smb_packet_t *inp)
958 if (inp->fid == 0 || inp->inCount == 0)
964 /* are we a priv'd user? What does this mean on NT? */
965 int smb_SUser(cm_user_t *userp)
970 /* find a file ID. If pass in 0, we'll allocate on on a create operation. */
971 smb_fid_t *smb_FindFID(smb_vc_t *vcp, unsigned short fid, int flags)
976 /* figure out if we need to allocate a new file ID */
979 fid = vcp->fidCounter;
983 lock_ObtainWrite(&smb_rctLock);
985 for(fidp = vcp->fidsp; fidp; fidp = (smb_fid_t *) osi_QNext(&fidp->q)) {
986 if (fid == fidp->fid) {
997 if (!fidp && (flags & SMB_FLAG_CREATE)) {
998 char eventName[MAX_PATH];
999 sprintf(eventName,"fid_t event fid=%d", fid);
1000 fidp = malloc(sizeof(*fidp));
1001 memset(fidp, 0, sizeof(*fidp));
1002 osi_QAdd((osi_queue_t **)&vcp->fidsp, &fidp->q);
1005 lock_InitializeMutex(&fidp->mx, "fid_t mutex");
1007 fidp->curr_chunk = fidp->prev_chunk = -2;
1008 fidp->raw_write_event = thrd_CreateEvent(NULL, FALSE, TRUE, eventName);
1009 if ( GetLastError() == ERROR_ALREADY_EXISTS )
1010 afsi_log("Event Object Already Exists: %s", eventName);
1012 vcp->fidCounter = fid+1;
1013 if (vcp->fidCounter == 0) vcp->fidCounter = 1;
1016 lock_ReleaseWrite(&smb_rctLock);
1020 void smb_ReleaseFID(smb_fid_t *fidp)
1024 smb_ioctl_t *ioctlp;
1030 lock_ObtainWrite(&smb_rctLock);
1031 osi_assert(fidp->refCount-- > 0);
1032 if (fidp->refCount == 0 && (fidp->flags & SMB_FID_DELETE)) {
1034 if (!(fidp->flags & SMB_FID_IOCTL))
1036 osi_QRemove((osi_queue_t **) &vcp->fidsp, &fidp->q);
1037 thrd_CloseHandle(fidp->raw_write_event);
1039 /* and see if there is ioctl stuff to free */
1040 ioctlp = fidp->ioctlp;
1042 if (ioctlp->prefix) cm_FreeSpace(ioctlp->prefix);
1043 if (ioctlp->inAllocp) free(ioctlp->inAllocp);
1044 if (ioctlp->outAllocp) free(ioctlp->outAllocp);
1050 lock_ReleaseWrite(&smb_rctLock);
1052 /* now release the scache structure */
1054 cm_ReleaseSCache(scp);
1058 * Case-insensitive search for one string in another;
1059 * used to find variable names in submount pathnames.
1061 static char *smb_stristr(char *str1, char *str2)
1065 for (cursor = str1; *cursor; cursor++)
1066 if (stricmp(cursor, str2) == 0)
1073 * Substitute a variable value for its name in a submount pathname. Variable
1074 * name has been identified by smb_stristr() and is in substr. Variable name
1075 * length (plus one) is in substr_size. Variable value is in newstr.
1077 static void smb_subst(char *str1, char *substr, unsigned int substr_size,
1082 strcpy(temp, substr + substr_size - 1);
1083 strcpy(substr, newstr);
1087 char VNUserName[] = "%USERNAME%";
1088 char VNLCUserName[] = "%LCUSERNAME%";
1089 char VNComputerName[] = "%COMPUTERNAME%";
1090 char VNLCComputerName[] = "%LCCOMPUTERNAME%";
1092 /* List available shares */
1093 int smb_ListShares()
1097 char shareBuf[4096];
1105 /*strcpy(shareNameList[num_shares], "all");
1106 strcpy(pathNameList[num_shares++], "/afs");*/
1107 fprintf(stderr, "The following shares are available:\n");
1108 fprintf(stderr, "Share Name (AFS Path)\n");
1109 fprintf(stderr, "---------------------\n");
1110 fprintf(stderr, "\\\\%s\\%-16s (%s)\n", smb_localNamep, "ALL", cm_mountRoot);
1113 code = GetWindowsDirectory(sbmtpath, sizeof(sbmtpath));
1114 if (code == 0 || code > sizeof(sbmtpath)) return -1;
1116 strcpy(sbmtpath, cm_confDir);
1118 strcat(sbmtpath, "/afsdsbmt.ini");
1119 len = GetPrivateProfileString("AFS Submounts", NULL, NULL,
1120 shareBuf, sizeof(shareBuf),
1126 this_share = shareBuf;
1130 /*strcpy(shareNameList[num_shares], this_share);*/
1131 len = GetPrivateProfileString("AFS Submounts", this_share,
1138 if (strncmp(p, cm_mountRoot, strlen(cm_mountRoot)) != 0)
1141 if (*p == '\\') *p = '/'; /* change to / */
1145 fprintf(stderr, "\\\\%s\\%-16s (%s%s)\n",
1146 smb_localNamep, this_share, (print_afs ? cm_mountRoot : "\0"),
1149 while (*this_share != 0) this_share++; /* find next NUL */
1150 this_share++; /* skip past the NUL */
1151 } while (*this_share != 0); /* stop at final NUL */
1156 /* find a shareName in the table of submounts */
1157 int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName,
1161 char pathName[1024];
1166 char sbmtpath[MAX_PATH];
1170 DWORD allSubmount = 1;
1172 if (strcmp(shareName, "IPC$") == 0) {
1177 /* if allSubmounts == 0, only return the //mountRoot/all share
1178 * if in fact it has been been created in the subMounts table.
1179 * This is to allow sites that want to restrict access to the
1182 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
1183 0, KEY_QUERY_VALUE, &parmKey);
1184 if (code == ERROR_SUCCESS) {
1185 len = sizeof(allSubmount);
1186 code = RegQueryValueEx(parmKey, "AllSubmount", NULL, NULL,
1187 (BYTE *) &allSubmount, &len);
1188 if (code != ERROR_SUCCESS) {
1191 RegCloseKey (parmKey);
1194 if (allSubmount && _stricmp(shareName, "all") == 0) {
1199 /* In case, the all share is disabled we need to still be able
1200 * to handle ioctl requests
1202 if (_stricmp(shareName, "ioctl$") == 0) {
1203 *pathNamep = strdup("/.__ioctl__");
1208 strcpy(sbmtpath, "afsdsbmt.ini");
1210 strcpy(sbmtpath, cm_confDir);
1211 strcat(sbmtpath, "/afsdsbmt.ini");
1213 len = GetPrivateProfileString("AFS Submounts", shareName, "",
1214 pathName, sizeof(pathName), sbmtpath);
1215 if (len != 0 && len != sizeof(pathName) - 1) {
1216 /* We can accept either unix or PC style AFS pathnames. Convert
1217 * Unix-style to PC style here for internal use.
1220 if (strncmp(p, cm_mountRoot, strlen(cm_mountRoot)) == 0)
1221 p += strlen(cm_mountRoot); /* skip mount path */
1224 if (*q == '/') *q = '\\'; /* change to \ */
1230 if (var = smb_stristr(p, VNUserName)) {
1231 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
1232 if (uidp && uidp->unp)
1233 smb_subst(p, var, sizeof(VNUserName),uidp->unp->name);
1235 smb_subst(p, var, sizeof(VNUserName)," ");
1237 smb_ReleaseUID(uidp);
1239 else if (var = smb_stristr(p, VNLCUserName))
1241 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
1242 if (uidp && uidp->unp)
1243 strcpy(temp, uidp->unp->name);
1247 smb_subst(p, var, sizeof(VNLCUserName), temp);
1249 smb_ReleaseUID(uidp);
1251 else if (var = smb_stristr(p, VNComputerName))
1253 sizeTemp = sizeof(temp);
1254 GetComputerName((LPTSTR)temp, &sizeTemp);
1255 smb_subst(p, var, sizeof(VNComputerName), temp);
1257 else if (var = smb_stristr(p, VNLCComputerName))
1259 sizeTemp = sizeof(temp);
1260 GetComputerName((LPTSTR)temp, &sizeTemp);
1262 smb_subst(p, var, sizeof(VNLCComputerName), temp);
1267 *pathNamep = strdup(p);
1270 else /* create \\<netbiosName>\<cellname> */
1272 if (cm_GetCell_Gen(shareName, temp, CM_FLAG_CREATE))
1274 int len = min(strlen(shareName), strlen(temp));
1275 if (!_strnicmp(shareName, temp, len)) { /* partial matches allowed */
1276 sprintf(pathName,"/%s/",temp);
1277 *pathNamep = strdup(strlwr(pathName));
1287 /* find a dir search structure by cookie value, and return it held.
1288 * Must be called with smb_globalLock held.
1290 smb_dirSearch_t *smb_FindDirSearchNL(long cookie)
1292 smb_dirSearch_t *dsp;
1294 for(dsp = smb_firstDirSearchp; dsp; dsp = (smb_dirSearch_t *) osi_QNext(&dsp->q)) {
1295 if (dsp->cookie == cookie) {
1296 if (dsp != smb_firstDirSearchp) {
1297 /* move to head of LRU queue, too, if we're not already there */
1298 if (smb_lastDirSearchp == (smb_dirSearch_t *) &dsp->q)
1299 smb_lastDirSearchp = (smb_dirSearch_t *)
1301 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1302 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1303 if (!smb_lastDirSearchp)
1304 smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1313 void smb_DeleteDirSearch(smb_dirSearch_t *dsp)
1315 lock_ObtainWrite(&smb_globalLock);
1316 dsp->flags |= SMB_DIRSEARCH_DELETE;
1317 lock_ReleaseWrite(&smb_globalLock);
1318 lock_ObtainMutex(&dsp->mx);
1319 if(dsp->scp != NULL) {
1320 lock_ObtainMutex(&dsp->scp->mx);
1321 if (dsp->flags & SMB_DIRSEARCH_BULKST) {
1322 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
1323 dsp->scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
1324 dsp->scp->bulkStatProgress = hones;
1326 lock_ReleaseMutex(&dsp->scp->mx);
1328 lock_ReleaseMutex(&dsp->mx);
1331 void smb_ReleaseDirSearch(smb_dirSearch_t *dsp)
1337 lock_ObtainWrite(&smb_globalLock);
1338 osi_assert(dsp->refCount-- > 0);
1339 if (dsp->refCount == 0 && (dsp->flags & SMB_DIRSEARCH_DELETE)) {
1340 if (&dsp->q == (osi_queue_t *) smb_lastDirSearchp)
1341 smb_lastDirSearchp = (smb_dirSearch_t *) osi_QPrev(&smb_lastDirSearchp->q);
1342 osi_QRemove((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1343 lock_FinalizeMutex(&dsp->mx);
1347 lock_ReleaseWrite(&smb_globalLock);
1349 /* do this now to avoid spurious locking hierarchy creation */
1350 if (scp) cm_ReleaseSCache(scp);
1353 /* find a dir search structure by cookie value, and return it held */
1354 smb_dirSearch_t *smb_FindDirSearch(long cookie)
1356 smb_dirSearch_t *dsp;
1358 lock_ObtainWrite(&smb_globalLock);
1359 dsp = smb_FindDirSearchNL(cookie);
1360 lock_ReleaseWrite(&smb_globalLock);
1364 /* GC some dir search entries, in the address space expected by the specific protocol.
1365 * Must be called with smb_globalLock held; release the lock temporarily.
1367 #define SMB_DIRSEARCH_GCMAX 10 /* how many at once */
1368 void smb_GCDirSearches(int isV3)
1370 smb_dirSearch_t *prevp;
1371 smb_dirSearch_t *tp;
1372 smb_dirSearch_t *victimsp[SMB_DIRSEARCH_GCMAX];
1376 victimCount = 0; /* how many have we got so far */
1377 for(tp = smb_lastDirSearchp; tp; tp=prevp) {
1378 /* we'll move tp from queue, so
1381 prevp = (smb_dirSearch_t *) osi_QPrev(&tp->q);
1382 /* if no one is using this guy, and we're either in the new protocol,
1383 * or we're in the old one and this is a small enough ID to be useful
1384 * to the old protocol, GC this guy.
1386 if (tp->refCount == 0 && (isV3 || tp->cookie <= 255)) {
1387 /* hold and delete */
1388 tp->flags |= SMB_DIRSEARCH_DELETE;
1389 victimsp[victimCount++] = tp;
1393 /* don't do more than this */
1394 if (victimCount >= SMB_DIRSEARCH_GCMAX) break;
1397 /* now release them */
1398 lock_ReleaseWrite(&smb_globalLock);
1399 for(i = 0; i < victimCount; i++) {
1400 smb_ReleaseDirSearch(victimsp[i]);
1402 lock_ObtainWrite(&smb_globalLock);
1405 /* function for allocating a dir search entry. We need these to remember enough context
1406 * since we don't get passed the path from call to call during a directory search.
1408 * Returns a held dir search structure, and bumps the reference count on the vnode,
1409 * since it saves a pointer to the vnode.
1411 smb_dirSearch_t *smb_NewDirSearch(int isV3)
1413 smb_dirSearch_t *dsp;
1417 lock_ObtainWrite(&smb_globalLock);
1420 /* what's the biggest ID allowed in this version of the protocol */
1421 if (isV3) maxAllowed = 65535;
1422 else maxAllowed = 255;
1425 /* twice so we have enough tries to find guys we GC after one pass;
1426 * 10 extra is just in case I mis-counted.
1428 if (++counter > 2*maxAllowed+10) osi_panic("afsd: dir search cookie leak",
1429 __FILE__, __LINE__);
1430 if (smb_dirSearchCounter > maxAllowed) {
1431 smb_dirSearchCounter = 1;
1432 smb_GCDirSearches(isV3); /* GC some (drops global lock) */
1434 dsp = smb_FindDirSearchNL(smb_dirSearchCounter);
1436 /* don't need to watch for refcount zero and deleted, since
1437 * we haven't dropped the global lock.
1440 ++smb_dirSearchCounter;
1444 dsp = malloc(sizeof(*dsp));
1445 memset(dsp, 0, sizeof(*dsp));
1446 osi_QAdd((osi_queue_t **) &smb_firstDirSearchp, &dsp->q);
1447 if (!smb_lastDirSearchp) smb_lastDirSearchp = (smb_dirSearch_t *) &dsp->q;
1448 dsp->cookie = smb_dirSearchCounter;
1449 ++smb_dirSearchCounter;
1451 lock_InitializeMutex(&dsp->mx, "cm_dirSearch_t");
1452 dsp->lastTime = osi_Time();
1455 lock_ReleaseWrite(&smb_globalLock);
1459 static smb_packet_t *GetPacket(void)
1463 unsigned int npar, seg, tb_sel;
1466 lock_ObtainWrite(&smb_globalLock);
1467 tbp = smb_packetFreeListp;
1469 smb_packetFreeListp = tbp->nextp;
1470 lock_ReleaseWrite(&smb_globalLock);
1473 tbp = calloc(65540,1);
1475 tbp = malloc(sizeof(smb_packet_t));
1477 tbp->magic = SMB_PACKETMAGIC;
1480 tbp->resumeCode = 0;
1486 tbp->ncb_length = 0;
1491 npar = SMB_PACKETSIZE >> 4; /* number of paragraphs */
1494 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1496 afsi_log("Cannot allocate %d paragraphs of DOS memory",
1498 osi_panic("",__FILE__,__LINE__);
1501 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
1506 tbp->dos_pkt = (seg * 16) + 0; /* DOS physical address */
1507 tbp->dos_pkt_sel = tb_sel;
1510 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1515 smb_packet_t *smb_CopyPacket(smb_packet_t *pkt)
1519 memcpy(tbp, pkt, sizeof(smb_packet_t));
1520 tbp->wctp = tbp->data + ((unsigned int)pkt->wctp - (unsigned int)pkt->data);
1524 static NCB *GetNCB(void)
1529 unsigned int npar, seg, tb_sel;
1532 lock_ObtainWrite(&smb_globalLock);
1533 tbp = smb_ncbFreeListp;
1535 smb_ncbFreeListp = tbp->nextp;
1536 lock_ReleaseWrite(&smb_globalLock);
1539 tbp = calloc(sizeof(*tbp),1);
1541 tbp = malloc(sizeof(*tbp));
1542 npar = (sizeof(NCB)+15) >> 4; /* number of paragraphs */
1545 __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
1547 afsi_log("Cannot allocate %d paragraphs of DOS mem in GetNCB",
1549 osi_panic("",__FILE__,__LINE__);
1551 afsi_log("Allocated %d paragraphs of DOS mem at 0x%X in GetNCB",
1556 tbp->dos_ncb = (seg * 16) + 0; /* DOS physical address */
1557 tbp->dos_ncb_sel = tb_sel;
1559 tbp->magic = SMB_NCBMAGIC;
1562 osi_assert(tbp->magic == SMB_NCBMAGIC);
1564 memset(&tbp->ncb, 0, sizeof(NCB));
1567 dos_memset(tbp->dos_ncb, 0, sizeof(NCB));
1572 void smb_FreePacket(smb_packet_t *tbp)
1574 osi_assert(tbp->magic == SMB_PACKETMAGIC);
1576 lock_ObtainWrite(&smb_globalLock);
1577 tbp->nextp = smb_packetFreeListp;
1578 smb_packetFreeListp = tbp;
1579 tbp->magic = SMB_PACKETMAGIC;
1582 tbp->resumeCode = 0;
1588 tbp->ncb_length = 0;
1590 lock_ReleaseWrite(&smb_globalLock);
1593 static void FreeNCB(NCB *bufferp)
1597 tbp = (smb_ncb_t *) bufferp;
1598 osi_assert(tbp->magic == SMB_NCBMAGIC);
1600 lock_ObtainWrite(&smb_globalLock);
1601 tbp->nextp = smb_ncbFreeListp;
1602 smb_ncbFreeListp = tbp;
1603 lock_ReleaseWrite(&smb_globalLock);
1606 /* get a ptr to the data part of a packet, and its count */
1607 unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp)
1611 unsigned char *afterParmsp;
1613 parmBytes = *smbp->wctp << 1;
1614 afterParmsp = smbp->wctp + parmBytes + 1;
1616 dataBytes = afterParmsp[0] + (afterParmsp[1]<<8);
1617 if (nbytesp) *nbytesp = dataBytes;
1619 /* don't forget to skip the data byte count, since it follows
1620 * the parameters; that's where the "2" comes from below.
1622 return (unsigned char *) (afterParmsp + 2);
1625 /* must set all the returned parameters before playing around with the
1626 * data region, since the data region is located past the end of the
1627 * variable number of parameters.
1629 void smb_SetSMBDataLength(smb_packet_t *smbp, unsigned int dsize)
1631 unsigned char *afterParmsp;
1633 afterParmsp = smbp->wctp + ((*smbp->wctp)<<1) + 1;
1635 *afterParmsp++ = dsize & 0xff;
1636 *afterParmsp = (dsize>>8) & 0xff;
1639 /* return the parm'th parameter in the smbp packet */
1640 unsigned int smb_GetSMBParm(smb_packet_t *smbp, int parm)
1643 unsigned char *parmDatap;
1645 parmCount = *smbp->wctp;
1647 if (parm >= parmCount) {
1652 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1653 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1654 parm, parmCount, smbp->ncb_length);
1656 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1657 1, smbp->ncb_length, ptbuf, smbp);
1658 DeregisterEventSource(h);
1662 sprintf(s, "Bad SMB param %d out of %d, ncb len %d",
1663 parm, parmCount, smbp->ncb_length);
1664 osi_Log0(afsd_logp, s);
1666 osi_panic(s, __FILE__, __LINE__);
1668 parmDatap = smbp->wctp + (2*parm) + 1;
1670 return parmDatap[0] + (parmDatap[1] << 8);
1673 /* return the parm'th parameter in the smbp packet */
1674 unsigned int smb_GetSMBOffsetParm(smb_packet_t *smbp, int parm, int offset)
1677 unsigned char *parmDatap;
1679 parmCount = *smbp->wctp;
1681 if (parm * 2 + offset >= parmCount * 2) {
1686 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
1687 sprintf(s, "Bad SMB param %d offset %d out of %d, ncb len %d",
1688 parm, offset, parmCount, smbp->ncb_length);
1690 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 1006, NULL,
1691 1, smbp->ncb_length, ptbuf, smbp);
1692 DeregisterEventSource(h);
1696 sprintf(s, "Bad SMB param %d offset %d out of %d, "
1698 parm, offset, parmCount, smbp->ncb_length);
1699 osi_Log0(afsd_logp, s);
1702 osi_panic(s, __FILE__, __LINE__);
1704 parmDatap = smbp->wctp + (2*parm) + 1 + offset;
1706 return parmDatap[0] + (parmDatap[1] << 8);
1709 void smb_SetSMBParm(smb_packet_t *smbp, int slot, unsigned int parmValue)
1713 /* make sure we have enough slots */
1714 if (*smbp->wctp <= slot) *smbp->wctp = slot+1;
1716 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1717 *parmDatap++ = parmValue & 0xff;
1718 *parmDatap = (parmValue>>8) & 0xff;
1721 void smb_SetSMBParmLong(smb_packet_t *smbp, int slot, unsigned int parmValue)
1725 /* make sure we have enough slots */
1726 if (*smbp->wctp <= slot) *smbp->wctp = slot+2;
1728 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1729 *parmDatap++ = parmValue & 0xff;
1730 *parmDatap++ = (parmValue>>8) & 0xff;
1731 *parmDatap++ = (parmValue>>16) & 0xff;
1732 *parmDatap++ = (parmValue>>24) & 0xff;
1735 void smb_SetSMBParmDouble(smb_packet_t *smbp, int slot, char *parmValuep)
1740 /* make sure we have enough slots */
1741 if (*smbp->wctp <= slot) *smbp->wctp = slot+4;
1743 parmDatap = smbp->wctp + 2*slot + 1 + smbp->oddByte;
1745 *parmDatap++ = *parmValuep++;
1748 void smb_SetSMBParmByte(smb_packet_t *smbp, int slot, unsigned int parmValue)
1752 /* make sure we have enough slots */
1753 if (*smbp->wctp <= slot) {
1754 if (smbp->oddByte) {
1756 *smbp->wctp = slot+1;
1761 parmDatap = smbp->wctp + 2*slot + 1 + (1 - smbp->oddByte);
1762 *parmDatap++ = parmValue & 0xff;
1765 void smb_StripLastComponent(char *outPathp, char **lastComponentp, char *inPathp)
1769 lastSlashp = strrchr(inPathp, '\\');
1771 *lastComponentp = lastSlashp;
1774 if (inPathp == lastSlashp)
1776 *outPathp++ = *inPathp++;
1785 unsigned char *smb_ParseASCIIBlock(unsigned char *inp, char **chainpp)
1790 *chainpp = inp + strlen(inp) + 1; /* skip over null-terminated string */
1795 unsigned char *smb_ParseVblBlock(unsigned char *inp, char **chainpp, int *lengthp)
1801 tlen = inp[0] + (inp[1]<<8);
1802 inp += 2; /* skip length field */
1805 *chainpp = inp + tlen;
1814 /* format a packet as a response */
1815 void smb_FormatResponsePacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *op)
1820 outp = (smb_t *) op;
1822 /* zero the basic structure through the smb_wct field, and zero the data
1823 * size field, assuming that wct stays zero; otherwise, you have to
1824 * explicitly set the data size field, too.
1826 inSmbp = (smb_t *) inp;
1827 memset(outp, 0, sizeof(smb_t)+2);
1833 outp->com = inSmbp->com;
1834 outp->tid = inSmbp->tid;
1835 outp->pid = inSmbp->pid;
1836 outp->uid = inSmbp->uid;
1837 outp->mid = inSmbp->mid;
1838 outp->res[0] = inSmbp->res[0];
1839 outp->res[1] = inSmbp->res[1];
1840 op->inCom = inSmbp->com;
1842 outp->reb = 0x80; /* SERVER_RESP */
1843 outp->flg2 = 0x1; /* KNOWS_LONG_NAMES */
1845 /* copy fields in generic packet area */
1846 op->wctp = &outp->wct;
1849 /* send a (probably response) packet; vcp tells us to whom to send it.
1850 * we compute the length by looking at wct and bcc fields.
1852 void smb_SendPacket(smb_vc_t *vcp, smb_packet_t *inp)
1869 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
1872 memset((char *)ncbp, 0, sizeof(NCB));
1874 extra = 2 * (*inp->wctp); /* space used by parms, in bytes */
1875 tp = inp->wctp + 1+ extra; /* points to count of data bytes */
1876 extra += tp[0] + (tp[1]<<8);
1877 extra += ((unsigned int)inp->wctp - (unsigned int)inp->data); /* distance to last wct field */
1878 extra += 3; /* wct and length fields */
1880 ncbp->ncb_length = extra; /* bytes to send */
1881 ncbp->ncb_lsn = (unsigned char) vcp->lsn; /* vc to use */
1882 ncbp->ncb_lana_num = vcp->lana;
1883 ncbp->ncb_command = NCBSEND; /* op means send data */
1885 ncbp->ncb_buffer = (char *) inp;/* packet */
1886 code = Netbios(ncbp);
1888 ncbp->ncb_buffer = inp->dos_pkt;/* packet */
1889 ((smb_ncb_t*)ncbp)->orig_pkt = inp;
1891 /* copy header information from virtual to DOS address space */
1892 dosmemput((char*)inp, SMB_PACKETSIZE, inp->dos_pkt);
1893 code = Netbios(ncbp, dos_ncb);
1897 osi_Log1(afsd_logp, "SendPacket failure code %d", code);
1903 void smb_MapNTError(long code, unsigned long *NTStatusp)
1905 unsigned long NTStatus;
1907 /* map CM_ERROR_* errors to NT 32-bit status codes */
1908 if (code == CM_ERROR_NOSUCHCELL) {
1909 NTStatus = 0xC000000FL; /* No such file */
1911 else if (code == CM_ERROR_NOSUCHVOLUME) {
1912 NTStatus = 0xC000000FL; /* No such file */
1914 else if (code == CM_ERROR_TIMEDOUT) {
1915 NTStatus = 0xC00000CFL; /* Sharing Paused */
1917 else if (code == CM_ERROR_RETRY) {
1918 NTStatus = 0xC000022DL; /* Retry */
1920 else if (code == CM_ERROR_NOACCESS) {
1921 NTStatus = 0xC0000022L; /* Access denied */
1923 else if (code == CM_ERROR_READONLY) {
1924 NTStatus = 0xC00000A2L; /* Write protected */
1926 else if (code == CM_ERROR_NOSUCHFILE) {
1927 NTStatus = 0xC000000FL; /* No such file */
1929 else if (code == CM_ERROR_NOSUCHPATH) {
1930 NTStatus = 0xC000003AL; /* Object path not found */
1932 else if (code == CM_ERROR_TOOBIG) {
1933 NTStatus = 0xC000007BL; /* Invalid image format */
1935 else if (code == CM_ERROR_INVAL) {
1936 NTStatus = 0xC000000DL; /* Invalid parameter */
1938 else if (code == CM_ERROR_BADFD) {
1939 NTStatus = 0xC0000008L; /* Invalid handle */
1941 else if (code == CM_ERROR_BADFDOP) {
1942 NTStatus = 0xC0000022L; /* Access denied */
1944 else if (code == CM_ERROR_EXISTS) {
1945 NTStatus = 0xC0000035L; /* Object name collision */
1947 else if (code == CM_ERROR_NOTEMPTY) {
1948 NTStatus = 0xC0000101L; /* Directory not empty */
1950 else if (code == CM_ERROR_CROSSDEVLINK) {
1951 NTStatus = 0xC00000D4L; /* Not same device */
1953 else if (code == CM_ERROR_NOTDIR) {
1954 NTStatus = 0xC0000103L; /* Not a directory */
1956 else if (code == CM_ERROR_ISDIR) {
1957 NTStatus = 0xC00000BAL; /* File is a directory */
1959 else if (code == CM_ERROR_BADOP) {
1960 NTStatus = 0xC09820FFL; /* SMB no support */
1962 else if (code == CM_ERROR_BADSHARENAME) {
1963 NTStatus = 0xC00000CCL; /* Bad network name */
1965 else if (code == CM_ERROR_NOIPC) {
1967 NTStatus = 0xC0000022L; /* Access Denied */
1969 NTStatus = 0xC000013DL; /* Remote Resources */
1972 else if (code == CM_ERROR_CLOCKSKEW) {
1973 NTStatus = 0xC0000133L; /* Time difference at DC */
1975 else if (code == CM_ERROR_BADTID) {
1976 NTStatus = 0xC0982005L; /* SMB bad TID */
1978 else if (code == CM_ERROR_USESTD) {
1979 NTStatus = 0xC09820FBL; /* SMB use standard */
1981 else if (code == CM_ERROR_QUOTA) {
1982 NTStatus = 0xC0000044L; /* Quota exceeded */
1984 else if (code == CM_ERROR_SPACE) {
1985 NTStatus = 0xC000007FL; /* Disk full */
1987 else if (code == CM_ERROR_ATSYS) {
1988 NTStatus = 0xC0000033L; /* Object name invalid */
1990 else if (code == CM_ERROR_BADNTFILENAME) {
1991 NTStatus = 0xC0000033L; /* Object name invalid */
1993 else if (code == CM_ERROR_WOULDBLOCK) {
1994 NTStatus = 0xC0000055L; /* Lock not granted */
1996 else if (code == CM_ERROR_PARTIALWRITE) {
1997 NTStatus = 0xC000007FL; /* Disk full */
1999 else if (code == CM_ERROR_BUFFERTOOSMALL) {
2000 NTStatus = 0xC0000023L; /* Buffer too small */
2003 NTStatus = 0xC0982001L; /* SMB non-specific error */
2006 *NTStatusp = NTStatus;
2007 osi_Log2(afsd_logp, "SMB SEND code %x as NT %x", code, NTStatus);
2010 void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
2011 unsigned char *classp)
2013 unsigned char class;
2014 unsigned short error;
2016 /* map CM_ERROR_* errors to SMB errors */
2017 if (code == CM_ERROR_NOSUCHCELL) {
2019 error = 3; /* bad path */
2021 else if (code == CM_ERROR_NOSUCHVOLUME) {
2023 error = 3; /* bad path */
2025 else if (code == CM_ERROR_TIMEDOUT) {
2027 error = 81; /* server is paused */
2029 else if (code == CM_ERROR_RETRY) {
2030 class = 2; /* shouldn't happen */
2033 else if (code == CM_ERROR_NOACCESS) {
2035 error = 4; /* bad access */
2037 else if (code == CM_ERROR_READONLY) {
2039 error = 19; /* read only */
2041 else if (code == CM_ERROR_NOSUCHFILE) {
2043 error = 2; /* ENOENT! */
2045 else if (code == CM_ERROR_NOSUCHPATH) {
2047 error = 3; /* Bad path */
2049 else if (code == CM_ERROR_TOOBIG) {
2051 error = 11; /* bad format */
2053 else if (code == CM_ERROR_INVAL) {
2054 class = 2; /* server non-specific error code */
2057 else if (code == CM_ERROR_BADFD) {
2059 error = 6; /* invalid file handle */
2061 else if (code == CM_ERROR_BADFDOP) {
2062 class = 1; /* invalid op on FD */
2065 else if (code == CM_ERROR_EXISTS) {
2067 error = 80; /* file already exists */
2069 else if (code == CM_ERROR_NOTEMPTY) {
2071 error = 5; /* delete directory not empty */
2073 else if (code == CM_ERROR_CROSSDEVLINK) {
2075 error = 17; /* EXDEV */
2077 else if (code == CM_ERROR_NOTDIR) {
2078 class = 1; /* bad path */
2081 else if (code == CM_ERROR_ISDIR) {
2082 class = 1; /* access denied; DOS doesn't have a good match */
2085 else if (code == CM_ERROR_BADOP) {
2089 else if (code == CM_ERROR_BADSHARENAME) {
2093 else if (code == CM_ERROR_NOIPC) {
2095 error = 4; /* bad access */
2097 else if (code == CM_ERROR_CLOCKSKEW) {
2098 class = 1; /* invalid function */
2101 else if (code == CM_ERROR_BADTID) {
2105 else if (code == CM_ERROR_USESTD) {
2109 else if (code == CM_ERROR_REMOTECONN) {
2113 else if (code == CM_ERROR_QUOTA) {
2114 if (vcp->flags & SMB_VCFLAG_USEV3) {
2116 error = 39; /* disk full */
2120 error = 5; /* access denied */
2123 else if (code == CM_ERROR_SPACE) {
2124 if (vcp->flags & SMB_VCFLAG_USEV3) {
2126 error = 39; /* disk full */
2130 error = 5; /* access denied */
2133 else if (code == CM_ERROR_PARTIALWRITE) {
2135 error = 39; /* disk full */
2137 else if (code == CM_ERROR_ATSYS) {
2139 error = 2; /* ENOENT */
2141 else if (code == CM_ERROR_WOULDBLOCK) {
2143 error = 33; /* lock conflict */
2145 else if (code == CM_ERROR_NOFILES) {
2147 error = 18; /* no files in search */
2149 else if (code == CM_ERROR_RENAME_IDENTICAL) {
2151 error = 183; /* Samba uses this */
2160 osi_Log3(afsd_logp, "SMB SEND code %x as SMB %d: %d", code, class, error);
2163 long smb_SendCoreBadOp(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2165 return CM_ERROR_BADOP;
2168 long smb_ReceiveCoreEcho(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2170 unsigned short EchoCount, i;
2171 char *data, *outdata;
2174 EchoCount = (unsigned short) smb_GetSMBParm(inp, 0);
2176 for (i=1; i<=EchoCount; i++) {
2177 data = smb_GetSMBData(inp, &dataSize);
2178 smb_SetSMBParm(outp, 0, i);
2179 smb_SetSMBDataLength(outp, dataSize);
2180 outdata = smb_GetSMBData(outp, NULL);
2181 memcpy(outdata, data, dataSize);
2182 smb_SendPacket(vcp, outp);
2188 long smb_ReceiveCoreReadRaw(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2191 long count, minCount, finalCount;
2195 cm_user_t *userp = NULL;
2199 char *rawBuf = NULL;
2201 dos_ptr rawBuf = NULL;
2208 fd = smb_GetSMBParm(inp, 0);
2209 count = smb_GetSMBParm(inp, 3);
2210 minCount = smb_GetSMBParm(inp, 4);
2211 offset.HighPart = 0; /* too bad */
2212 offset.LowPart = smb_GetSMBParm(inp, 1) | (smb_GetSMBParm(inp, 2) << 16);
2214 osi_Log3(afsd_logp, "smb_ReceieveCoreReadRaw fd %d, off 0x%x, size 0x%x",
2215 fd, offset.LowPart, count);
2217 fidp = smb_FindFID(vcp, fd, 0);
2221 lock_ObtainMutex(&smb_RawBufLock);
2223 /* Get a raw buf, from head of list */
2224 rawBuf = smb_RawBufs;
2226 smb_RawBufs = *(char **)smb_RawBufs;
2228 smb_RawBufs = _farpeekl(_dos_ds, smb_RawBufs);
2231 lock_ReleaseMutex(&smb_RawBufLock);
2235 if (fidp->flags & SMB_FID_IOCTL)
2238 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp);
2240 rc = smb_IoctlReadRaw(fidp, vcp, inp, outp, rawBuf);
2243 /* Give back raw buffer */
2244 lock_ObtainMutex(&smb_RawBufLock);
2246 *((char **) rawBuf) = smb_RawBufs;
2248 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2251 smb_RawBufs = rawBuf;
2252 lock_ReleaseMutex(&smb_RawBufLock);
2255 smb_ReleaseFID(fidp);
2259 userp = smb_GetUser(vcp, inp);
2262 code = smb_ReadData(fidp, &offset, count, rawBuf, userp, &finalCount);
2264 /* have to give ReadData flag so it will treat buffer as DOS mem. */
2265 code = smb_ReadData(fidp, &offset, count, (unsigned char *)rawBuf,
2266 userp, &finalCount, TRUE /* rawFlag */);
2273 cm_ReleaseUser(userp);
2276 smb_ReleaseFID(fidp);
2281 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
2283 memset((char *)ncbp, 0, sizeof(NCB));
2285 ncbp->ncb_length = (unsigned short) finalCount;
2286 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
2287 ncbp->ncb_lana_num = vcp->lana;
2288 ncbp->ncb_command = NCBSEND;
2289 ncbp->ncb_buffer = rawBuf;
2292 code = Netbios(ncbp);
2294 code = Netbios(ncbp, dos_ncb);
2297 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
2300 /* Give back raw buffer */
2301 lock_ObtainMutex(&smb_RawBufLock);
2303 *((char **) rawBuf) = smb_RawBufs;
2305 _farpokel(_dos_ds, rawBuf, smb_RawBufs);
2308 smb_RawBufs = rawBuf;
2309 lock_ReleaseMutex(&smb_RawBufLock);
2315 long smb_ReceiveCoreLockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2320 long smb_ReceiveCoreUnlockRecord(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2325 long smb_ReceiveNegotiate(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2331 int protoIndex; /* index we're using */
2336 char protocol_array[10][1024]; /* protocol signature of the client */
2339 osi_Log1(afsd_logp, "SMB receive negotiate; %d + 1 ongoing ops",
2343 DWORD now = GetCurrentTime();
2344 if (now - last_msg_time >= 30000
2345 && now - last_msg_time <= 90000) {
2347 "Setting dead_vcp %x", active_vcp);
2348 dead_vcp = active_vcp;
2349 dead_vcp->flags |= SMB_VCFLAG_ALREADYDEAD;
2354 inp->flags |= SMB_PACKETFLAG_PROFILE_UPDATE_OK;
2356 namep = smb_GetSMBData(inp, &dbytes);
2359 coreProtoIndex = -1; /* not found */
2362 while(namex < dbytes) {
2363 osi_Log1(afsd_logp, "Protocol %s",
2364 osi_LogSaveString(afsd_logp, namep+1));
2365 strcpy(protocol_array[tcounter], namep+1);
2367 /* namep points at the first protocol, or really, a 0x02
2368 * byte preceding the null-terminated ASCII name.
2370 if (strcmp("PC NETWORK PROGRAM 1.0", namep+1) == 0) {
2371 coreProtoIndex = tcounter;
2373 else if (smb_useV3 && strcmp("LM1.2X002", namep+1) == 0) {
2374 v3ProtoIndex = tcounter;
2376 else if (smb_useV3 && strcmp("NT LM 0.12", namep+1) == 0) {
2377 NTProtoIndex = tcounter;
2380 /* compute size of protocol entry */
2381 entryLength = strlen(namep+1);
2382 entryLength += 2; /* 0x02 bytes and null termination */
2384 /* advance over this protocol entry */
2385 namex += entryLength;
2386 namep += entryLength;
2387 tcounter++; /* which proto entry we're looking at */
2389 #ifndef NOMOREFILESFIX
2391 * NOTE: We can determine what OS (NT4.0, W2K, W9X, etc)
2392 * the client is running by reading the protocol signature.
2393 * ie. the order in which it sends us the protocol list.
2395 * Special handling for Windows 2000 clients (defect 11765 )
2396 * <asanka:11Jun03> Proto signature is the same for Win XP. </>
2398 if (tcounter == 6) {
2400 smb_t *ip = (smb_t *) inp;
2401 smb_t *op = (smb_t *) outp;
2403 if ((strcmp("PC NETWORK PROGRAM 1.0", protocol_array[0]) == 0) &&
2404 (strcmp("LANMAN1.0", protocol_array[1]) == 0) &&
2405 (strcmp("Windows for Workgroups 3.1a", protocol_array[2]) == 0) &&
2406 (strcmp("LM1.2X002", protocol_array[3]) == 0) &&
2407 (strcmp("LANMAN2.1", protocol_array[4]) == 0) &&
2408 (strcmp("NT LM 0.12", protocol_array[5]) == 0)) {
2409 isWindows2000 = TRUE;
2410 osi_Log0(afsd_logp, "Looks like a Windows 2000 client");
2412 * HACK: for now - just negotiate a lower protocol till we
2413 * figure out which flag/flag2 or some other field
2414 * (capabilities maybe?) to set in order for this to work
2415 * correctly with Windows 2000 clients (defect 11765)
2418 /* Things to try (after looking at tcpdump output could be
2419 * setting flags and flags2 to 0x98 and 0xc853 like this
2420 * op->reb = 0x98; op->flg2 = 0xc853;
2421 * osi_Log2(afsd_logp, "Flags:0x%x Flags2:0x%x", ip->reb, ip->flg2);
2425 #endif /* NOMOREFILESFIX */
2427 if (NTProtoIndex != -1) {
2428 protoIndex = NTProtoIndex;
2429 vcp->flags |= (SMB_VCFLAG_USENT | SMB_VCFLAG_USEV3);
2431 else if (v3ProtoIndex != -1) {
2432 protoIndex = v3ProtoIndex;
2433 vcp->flags |= SMB_VCFLAG_USEV3;
2435 else if (coreProtoIndex != -1) {
2436 protoIndex = coreProtoIndex;
2437 vcp->flags |= SMB_VCFLAG_USECORE;
2439 else protoIndex = -1;
2441 if (protoIndex == -1)
2442 return CM_ERROR_INVAL;
2443 else if (NTProtoIndex != -1) {
2444 smb_SetSMBParm(outp, 0, protoIndex);
2445 smb_SetSMBParmByte(outp, 1, 0); /* share level security, no passwd encrypt */
2446 smb_SetSMBParm(outp, 1, smb_maxMpxRequests); /* max multiplexed requests */
2447 smb_SetSMBParm(outp, 2, smb_maxVCPerServer); /* max VCs per consumer/server connection */
2448 smb_SetSMBParmLong(outp, 3, SMB_PACKETSIZE); /* xmit buffer size */
2449 smb_SetSMBParmLong(outp, 5, 65536); /* raw buffer size */
2450 smb_SetSMBParm(outp, 7, 1); /* next 2: session key */
2451 smb_SetSMBParm(outp, 8, 1);
2453 * Tried changing the capabilities to support for W2K - defect 117695
2454 * Maybe something else needs to be changed here?
2458 smb_SetSMBParmLong(outp, 9, 0x43fd);
2460 smb_SetSMBParmLong(outp, 9, 0x251);
2463 * 32-bit error codes *
2467 smb_SetSMBParmLong(outp, 9, 0x251);
2468 smb_SetSMBParmLong(outp, 11, 0);/* XXX server time: do we need? */
2469 smb_SetSMBParmLong(outp, 13, 0);/* XXX server date: do we need? */
2470 smb_SetSMBParm(outp, 15, 0); /* XXX server tzone: do we need? */
2471 smb_SetSMBParmByte(outp, 16, 0);/* Encryption key length */
2472 smb_SetSMBDataLength(outp, 0); /* perhaps should specify 8 bytes anyway */
2474 else if (v3ProtoIndex != -1) {
2475 smb_SetSMBParm(outp, 0, protoIndex);
2476 smb_SetSMBParm(outp, 1, 0); /* share level security, no passwd encrypt */
2477 smb_SetSMBParm(outp, 2, SMB_PACKETSIZE);
2478 smb_SetSMBParm(outp, 3, smb_maxMpxRequests); /* max multiplexed requests */
2479 smb_SetSMBParm(outp, 4, smb_maxVCPerServer); /* max VCs per consumer/server connection */
2480 smb_SetSMBParm(outp, 5, 0); /* no support of block mode for read or write */
2481 smb_SetSMBParm(outp, 6, 1); /* next 2: session key */
2482 smb_SetSMBParm(outp, 7, 1);
2483 smb_SetSMBParm(outp, 8, 0); /* XXX server time: do we need? */
2484 smb_SetSMBParm(outp, 9, 0); /* XXX server date: do we need? */
2485 smb_SetSMBParm(outp, 10, 0); /* XXX server tzone: do we need? */
2486 smb_SetSMBParm(outp, 11, 0); /* resvd */
2487 smb_SetSMBParm(outp, 12, 0); /* resvd */
2488 smb_SetSMBDataLength(outp, 0); /* perhaps should specify 8 bytes anyway */
2490 else if (coreProtoIndex != -1) {
2491 smb_SetSMBParm(outp, 0, protoIndex);
2492 smb_SetSMBDataLength(outp, 0);
2497 void smb_Daemon(void *parmp)
2504 if ((count % 360) == 0) /* every hour */
2505 smb_CalculateNowTZ();
2506 /* XXX GC dir search entries */
2510 void smb_WaitingLocksDaemon()
2512 smb_waitingLock_t *wL, *nwL;
2515 smb_packet_t *inp, *outp;
2520 lock_ObtainWrite(&smb_globalLock);
2521 nwL = smb_allWaitingLocks;
2523 osi_SleepW((long)&smb_allWaitingLocks, &smb_globalLock);
2532 lock_ObtainWrite(&smb_globalLock);
2534 nwL = (smb_waitingLock_t *) osi_QNext(&wL->q);
2535 lock_ReleaseWrite(&smb_globalLock);
2536 code = cm_RetryLock((cm_file_lock_t *) wL->lockp,
2537 wL->vcp->flags & SMB_VCFLAG_ALREADYDEAD);
2538 if (code == CM_ERROR_WOULDBLOCK) {
2540 if (wL->timeRemaining != 0xffffffff
2541 && (wL->timeRemaining -= 1000) < 0)
2550 ncbp->ncb_length = inp->ncb_length;
2551 inp->spacep = cm_GetSpace();
2553 /* Remove waitingLock from list */
2554 lock_ObtainWrite(&smb_globalLock);
2555 osi_QRemove((osi_queue_t **)&smb_allWaitingLocks,
2557 lock_ReleaseWrite(&smb_globalLock);
2559 /* Resume packet processing */
2561 smb_SetSMBDataLength(outp, 0);
2562 outp->flags |= SMB_PACKETFLAG_SUSPENDED;
2563 outp->resumeCode = code;
2565 smb_DispatchPacket(vcp, inp, outp, ncbp, NULL);
2568 cm_FreeSpace(inp->spacep);
2569 smb_FreePacket(inp);
2570 smb_FreePacket(outp);
2578 long smb_ReceiveCoreGetDiskAttributes(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2580 osi_Log0(afsd_logp, "SMB receive get disk attributes");
2582 smb_SetSMBParm(outp, 0, 32000);
2583 smb_SetSMBParm(outp, 1, 64);
2584 smb_SetSMBParm(outp, 2, 1024);
2585 smb_SetSMBParm(outp, 3, 30000);
2586 smb_SetSMBParm(outp, 4, 0);
2587 smb_SetSMBDataLength(outp, 0);
2591 long smb_ReceiveCoreTreeConnect(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *rsp)
2594 unsigned short newTid;
2595 char shareName[256];
2603 osi_Log0(afsd_logp, "SMB receive tree connect");
2605 /* parse input parameters */
2606 tp = smb_GetSMBData(inp, NULL);
2607 pathp = smb_ParseASCIIBlock(tp, &tp);
2608 passwordp = smb_ParseASCIIBlock(tp, &tp);
2609 tp = strrchr(pathp, '\\');
2611 return CM_ERROR_BADSMB;
2612 strcpy(shareName, tp+1);
2614 userp = smb_GetUser(vcp, inp);
2616 lock_ObtainMutex(&vcp->mx);
2617 newTid = vcp->tidCounter++;
2618 lock_ReleaseMutex(&vcp->mx);
2620 tidp = smb_FindTID(vcp, newTid, SMB_FLAG_CREATE);
2621 shareFound = smb_FindShare(vcp, inp, shareName, &sharePath);
2623 smb_ReleaseTID(tidp);
2624 return CM_ERROR_BADSHARENAME;
2626 lock_ObtainMutex(&tidp->mx);
2627 tidp->userp = userp;
2628 tidp->pathname = sharePath;
2629 lock_ReleaseMutex(&tidp->mx);
2630 smb_ReleaseTID(tidp);
2632 smb_SetSMBParm(rsp, 0, SMB_PACKETSIZE);
2633 smb_SetSMBParm(rsp, 1, newTid);
2634 smb_SetSMBDataLength(rsp, 0);
2636 osi_Log1(afsd_logp, "SMB tree connect created ID %d", newTid);
2640 unsigned char *smb_ParseDataBlock(unsigned char *inp, char **chainpp, int *lengthp)
2644 if (*inp++ != 0x1) return NULL;
2645 tlen = inp[0] + (inp[1]<<8);
2646 inp += 2; /* skip length field */
2649 *chainpp = inp + tlen;
2652 if (lengthp) *lengthp = tlen;
2657 /* set maskp to the mask part of the incoming path.
2658 * Mask is 11 bytes long (8.3 with the dot elided).
2659 * Returns true if succeeds with a valid name, otherwise it does
2660 * its best, but returns false.
2662 int smb_Get8Dot3MaskFromPath(unsigned char *maskp, unsigned char *pathp)
2670 /* starts off valid */
2673 /* mask starts out all blanks */
2674 memset(maskp, ' ', 11);
2676 /* find last backslash, or use whole thing if there is none */
2677 tp = strrchr(pathp, '\\');
2678 if (!tp) tp = pathp;
2679 else tp++; /* skip slash */
2683 /* names starting with a dot are illegal */
2684 if (*tp == '.') valid8Dot3 = 0;
2688 if (tc == 0) return valid8Dot3;
2689 if (tc == '.' || tc == '"') break;
2690 if (i < 8) *up++ = tc;
2691 else valid8Dot3 = 0;
2694 /* if we get here, tp point after the dot */
2695 up = maskp+8; /* ext goes here */
2702 if (tc == '.' || tc == '"')
2705 /* copy extension if not too long */
2715 int smb_Match8Dot3Mask(char *unixNamep, char *maskp)
2725 /* XXX redo this, calling smb_V3MatchMask with a converted mask */
2727 valid = smb_Get8Dot3MaskFromPath(umask, unixNamep);
2731 /* otherwise, we have a valid 8.3 name; see if we have a match,
2732 * treating '?' as a wildcard in maskp (but not in the file name).
2734 tp1 = umask; /* real name, in mask format */
2735 tp2 = maskp; /* mask, in mask format */
2736 for(i=0; i<11; i++) {
2737 tc1 = *tp1++; /* char from real name */
2738 tc2 = *tp2++; /* char from mask */
2739 tc1 = (char) cm_foldUpper[(unsigned char)tc1];
2740 tc2 = (char) cm_foldUpper[(unsigned char)tc2];
2743 if (tc2 == '?' && tc1 != ' ')
2750 /* we got a match */
2754 char *smb_FindMask(char *pathp)
2758 tp = strrchr(pathp, '\\'); /* find last slash */
2761 return tp+1; /* skip the slash */
2763 return pathp; /* no slash, return the entire path */
2766 long smb_ReceiveCoreSearchVolume(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2768 unsigned char *pathp;
2770 unsigned char mask[11];
2771 unsigned char *statBlockp;
2772 unsigned char initStatBlock[21];
2775 osi_Log0(afsd_logp, "SMB receive search volume");
2777 /* pull pathname and stat block out of request */
2778 tp = smb_GetSMBData(inp, NULL);
2779 pathp = smb_ParseASCIIBlock(tp, (char **) &tp);
2780 osi_assert(pathp != NULL);
2781 statBlockp = smb_ParseVblBlock(tp, (char **) &tp, &statLen);
2782 osi_assert(statBlockp != NULL);
2784 statBlockp = initStatBlock;
2788 /* for returning to caller */
2789 smb_Get8Dot3MaskFromPath(mask, pathp);
2791 smb_SetSMBParm(outp, 0, 1); /* we're returning one entry */
2792 tp = smb_GetSMBData(outp, NULL);
2794 *tp++ = 43; /* bytes in a dir entry */
2795 *tp++ = 0; /* high byte in counter */
2797 /* now marshall the dir entry, starting with the search status */
2798 *tp++ = statBlockp[0]; /* Reserved */
2799 memcpy(tp, mask, 11); tp += 11; /* FileName */
2801 /* now pass back server use info, with 1st byte non-zero */
2803 memset(tp, 0, 4); tp += 4; /* reserved for server use */
2805 memcpy(tp, statBlockp+17, 4); tp += 4; /* reserved for consumer */
2807 *tp++ = 0x8; /* attribute: volume */
2817 /* 4 byte file size */
2823 /* finally, null-terminated 8.3 pathname, which we set to AFS */
2824 memset(tp, ' ', 13);
2827 /* set the length of the data part of the packet to 43 + 3, for the dir
2828 * entry plus the 5 and the length fields.
2830 smb_SetSMBDataLength(outp, 46);
2834 long smb_ApplyDirListPatches(smb_dirListPatch_t **dirPatchespp,
2835 cm_user_t *userp, cm_req_t *reqp)
2843 smb_dirListPatch_t *patchp;
2844 smb_dirListPatch_t *npatchp;
2846 for(patchp = *dirPatchespp; patchp; patchp =
2847 (smb_dirListPatch_t *) osi_QNext(&patchp->q)) {
2848 code = cm_GetSCache(&patchp->fid, &scp, userp, reqp);
2850 lock_ObtainMutex(&scp->mx);
2851 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2852 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2854 lock_ReleaseMutex(&scp->mx);
2855 cm_ReleaseSCache(scp);
2858 dptr = patchp->dptr;
2860 attr = smb_Attributes(scp);
2861 /* check hidden attribute (the flag is only ON when dot file hiding is on ) */
2862 if( patchp->flags & SMB_DIRLISTPATCH_DOTFILE )
2863 attr |= SMB_ATTR_HIDDEN;
2867 smb_SearchTimeFromUnixTime(&dosTime, scp->clientModTime);
2870 shortTemp = dosTime & 0xffff;
2871 *((u_short *)dptr) = shortTemp;
2874 /* and copy out date */
2875 shortTemp = (dosTime>>16) & 0xffff;
2876 *((u_short *)dptr) = shortTemp;
2879 /* copy out file length */
2880 *((u_long *)dptr) = scp->length.LowPart;
2882 lock_ReleaseMutex(&scp->mx);
2883 cm_ReleaseSCache(scp);
2886 /* now free the patches */
2887 for(patchp = *dirPatchespp; patchp; patchp = npatchp) {
2888 npatchp = (smb_dirListPatch_t *) osi_QNext(&patchp->q);
2892 /* and mark the list as empty */
2893 *dirPatchespp = NULL;
2898 long smb_ReceiveCoreSearchDir(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
2907 smb_dirListPatch_t *dirListPatchesp;
2908 smb_dirListPatch_t *curPatchp;
2912 osi_hyper_t dirLength;
2913 osi_hyper_t bufferOffset;
2914 osi_hyper_t curOffset;
2916 unsigned char *inCookiep;
2917 smb_dirSearch_t *dsp;
2921 unsigned long clientCookie;
2922 cm_pageHeader_t *pageHeaderp;
2923 cm_user_t *userp = NULL;
2930 long nextEntryCookie;
2931 int numDirChunks; /* # of 32 byte dir chunks in this entry */
2932 char resByte; /* reserved byte from the cookie */
2933 char *op; /* output data ptr */
2934 char *origOp; /* original value of op */
2935 cm_space_t *spacep; /* for pathname buffer */
2946 maxCount = smb_GetSMBParm(inp, 0);
2948 dirListPatchesp = NULL;
2950 caseFold = CM_FLAG_CASEFOLD;
2952 tp = smb_GetSMBData(inp, NULL);
2953 pathp = smb_ParseASCIIBlock(tp, &tp);
2954 inCookiep = smb_ParseVblBlock(tp, &tp, &dataLength);
2956 /* bail out if request looks bad */
2957 if (!tp || !pathp) {
2958 return CM_ERROR_BADSMB;
2961 /* We can handle long names */
2962 if (vcp->flags & SMB_VCFLAG_USENT)
2963 ((smb_t *)outp)->flg2 |= 0x40; /* IS_LONG_NAME */
2965 /* make sure we got a whole search status */
2966 if (dataLength < 21) {
2967 nextCookie = 0; /* start at the beginning of the dir */
2970 attribute = smb_GetSMBParm(inp, 1);
2972 /* handle volume info in another function */
2973 if (attribute & 0x8)
2974 return smb_ReceiveCoreSearchVolume(vcp, inp, outp);
2976 osi_Log2(afsd_logp, "SMB receive search dir count %d [%s]",
2977 maxCount, osi_LogSaveString(afsd_logp, pathp));
2979 if (*pathp == 0) { /* null pathp, treat as root dir */
2980 if (!(attribute & SMB_ATTR_DIRECTORY)) /* exclude dirs */
2981 return CM_ERROR_NOFILES;
2985 dsp = smb_NewDirSearch(0);
2986 dsp->attribute = attribute;
2987 smb_Get8Dot3MaskFromPath(mask, pathp);
2988 memcpy(dsp->mask, mask, 11);
2990 /* track if this is likely to match a lot of entries */
2991 if (smb_IsStarMask(mask)) starPattern = 1;
2992 else starPattern = 0;
2995 /* pull the next cookie value out of the search status block */
2996 nextCookie = inCookiep[13] + (inCookiep[14]<<8) + (inCookiep[15]<<16)
2997 + (inCookiep[16]<<24);
2998 dsp = smb_FindDirSearch(inCookiep[12]);
3000 /* can't find dir search status; fatal error */
3001 return CM_ERROR_BADFD;
3003 attribute = dsp->attribute;
3004 resByte = inCookiep[0];
3006 /* copy out client cookie, in host byte order. Don't bother
3007 * interpreting it, since we're just passing it through, anyway.
3009 memcpy(&clientCookie, &inCookiep[17], 4);
3011 memcpy(mask, dsp->mask, 11);
3013 /* assume we're doing a star match if it has continued for more
3019 osi_Log3(afsd_logp, "SMB dir search cookie 0x%x, connection %d, attr 0x%x",
3020 nextCookie, dsp->cookie, attribute);
3022 userp = smb_GetUser(vcp, inp);
3024 /* try to get the vnode for the path name next */
3025 lock_ObtainMutex(&dsp->mx);
3032 spacep = inp->spacep;
3033 smb_StripLastComponent(spacep->data, NULL, pathp);
3034 lock_ReleaseMutex(&dsp->mx);
3035 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3036 code = cm_NameI(cm_rootSCachep, spacep->data,
3037 caseFold | CM_FLAG_FOLLOW, userp, tidPathp, &req, &scp);
3038 lock_ObtainMutex(&dsp->mx);
3040 if (dsp->scp != 0) cm_ReleaseSCache(dsp->scp);
3042 /* we need one hold for the entry we just stored into,
3043 * and one for our own processing. When we're done with this
3044 * function, we'll drop the one for our own processing.
3045 * We held it once from the namei call, and so we do another hold
3049 lock_ObtainMutex(&scp->mx);
3050 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
3051 && LargeIntegerGreaterOrEqualToZero(scp->bulkStatProgress)) {
3052 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
3053 dsp->flags |= SMB_DIRSEARCH_BULKST;
3055 lock_ReleaseMutex(&scp->mx);
3058 lock_ReleaseMutex(&dsp->mx);
3060 cm_ReleaseUser(userp);
3061 smb_DeleteDirSearch(dsp);
3062 smb_ReleaseDirSearch(dsp);
3066 /* reserves space for parameter; we'll adjust it again later to the
3067 * real count of the # of entries we returned once we've actually
3068 * assembled the directory listing.
3070 smb_SetSMBParm(outp, 0, 0);
3072 /* get the directory size */
3073 lock_ObtainMutex(&scp->mx);
3074 code = cm_SyncOp(scp, NULL, userp, &req, 0,
3075 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3077 lock_ReleaseMutex(&scp->mx);
3078 cm_ReleaseSCache(scp);
3079 cm_ReleaseUser(userp);
3080 smb_DeleteDirSearch(dsp);
3081 smb_ReleaseDirSearch(dsp);
3085 dirLength = scp->length;
3087 bufferOffset.LowPart = bufferOffset.HighPart = 0;
3088 curOffset.HighPart = 0;
3089 curOffset.LowPart = nextCookie;
3090 origOp = op = smb_GetSMBData(outp, NULL);
3091 /* and write out the basic header */
3092 *op++ = 5; /* variable block */
3093 op += 2; /* skip vbl block length; we'll fill it in later */
3097 /* make sure that curOffset.LowPart doesn't point to the first
3098 * 32 bytes in the 2nd through last dir page, and that it doesn't
3099 * point at the first 13 32-byte chunks in the first dir page,
3100 * since those are dir and page headers, and don't contain useful
3103 temp = curOffset.LowPart & (2048-1);
3104 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
3105 /* we're in the first page */
3106 if (temp < 13*32) temp = 13*32;
3109 /* we're in a later dir page */
3110 if (temp < 32) temp = 32;
3113 /* make sure the low order 5 bits are zero */
3116 /* now put temp bits back ito curOffset.LowPart */
3117 curOffset.LowPart &= ~(2048-1);
3118 curOffset.LowPart |= temp;
3120 /* check if we've returned all the names that will fit in the
3123 if (returnedNames >= maxCount)
3126 /* check if we've passed the dir's EOF */
3127 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength)) break;
3129 /* see if we can use the bufferp we have now; compute in which page
3130 * the current offset would be, and check whether that's the offset
3131 * of the buffer we have. If not, get the buffer.
3133 thyper.HighPart = curOffset.HighPart;
3134 thyper.LowPart = curOffset.LowPart & ~(buf_bufferSize-1);
3135 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
3138 buf_Release(bufferp);
3141 lock_ReleaseMutex(&scp->mx);
3142 lock_ObtainRead(&scp->bufCreateLock);
3143 code = buf_Get(scp, &thyper, &bufferp);
3144 lock_ReleaseRead(&scp->bufCreateLock);
3146 /* now, if we're doing a star match, do bulk fetching of all of
3147 * the status info for files in the dir.
3150 smb_ApplyDirListPatches(&dirListPatchesp, userp,
3152 if ((dsp->flags & SMB_DIRSEARCH_BULKST)
3153 && LargeIntegerGreaterThanOrEqualTo(thyper,
3154 scp->bulkStatProgress)) {
3155 /* Don't bulk stat if risking timeout */
3156 int now = GetCurrentTime();
3157 if (now - req.startTime > 5000) {
3158 scp->bulkStatProgress = thyper;
3159 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
3160 dsp->flags &= ~SMB_DIRSEARCH_BULKST;
3162 cm_TryBulkStat(scp, &thyper, userp, &req);
3166 lock_ObtainMutex(&scp->mx);
3169 bufferOffset = thyper;
3171 /* now get the data in the cache */
3173 code = cm_SyncOp(scp, bufferp, userp, &req,
3175 CM_SCACHESYNC_NEEDCALLBACK
3176 | CM_SCACHESYNC_READ);
3179 if (cm_HaveBuffer(scp, bufferp, 0)) break;
3181 /* otherwise, load the buffer and try again */
3182 code = cm_GetBuffer(scp, bufferp, NULL, userp,
3187 buf_Release(bufferp);
3191 } /* if (wrong buffer) ... */
3193 /* now we have the buffer containing the entry we're interested in; copy
3194 * it out if it represents a non-deleted entry.
3196 entryInDir = curOffset.LowPart & (2048-1);
3197 entryInBuffer = curOffset.LowPart & (buf_bufferSize - 1);
3199 /* page header will help tell us which entries are free. Page header
3200 * can change more often than once per buffer, since AFS 3 dir page size
3201 * may be less than (but not more than a buffer package buffer.
3203 temp = curOffset.LowPart & (buf_bufferSize - 1); /* only look intra-buffer */
3204 temp &= ~(2048 - 1); /* turn off intra-page bits */
3205 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
3207 /* now determine which entry we're looking at in the page. If it is
3208 * free (there's a free bitmap at the start of the dir), we should
3209 * skip these 32 bytes.
3211 slotInPage = (entryInDir & 0x7e0) >> 5;
3212 if (!(pageHeaderp->freeBitmap[slotInPage>>3] & (1 << (slotInPage & 0x7)))) {
3213 /* this entry is free */
3214 numDirChunks = 1; /* only skip this guy */
3218 tp = bufferp->datap + entryInBuffer;
3219 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
3221 /* while we're here, compute the next entry's location, too,
3222 * since we'll need it when writing out the cookie into the dir
3225 * XXXX Probably should do more sanity checking.
3227 numDirChunks = cm_NameEntries(dep->name, NULL);
3229 /* compute the offset of the cookie representing the next entry */
3230 nextEntryCookie = curOffset.LowPart + (CM_DIR_CHUNKSIZE * numDirChunks);
3232 /* Compute 8.3 name if necessary */
3233 actualName = dep->name;
3234 if (dep->fid.vnode != 0 && !cm_Is8Dot3(actualName)) {
3235 cm_Gen8Dot3Name(dep, shortName, &shortNameEnd);
3236 actualName = shortName;
3239 if (dep->fid.vnode != 0 && smb_Match8Dot3Mask(actualName, mask)) {
3240 /* this is one of the entries to use: it is not deleted
3241 * and it matches the star pattern we're looking for.
3244 /* Eliminate entries that don't match requested
3247 /* no hidden files */
3248 if(smb_hideDotFiles && !(dsp->attribute & SMB_ATTR_HIDDEN) && smb_IsDotFile(actualName))
3251 if (!(dsp->attribute & SMB_ATTR_DIRECTORY)) /* no directories */
3253 /* We have already done the cm_TryBulkStat above */
3254 fid.cell = scp->fid.cell;
3255 fid.volume = scp->fid.volume;
3256 fid.vnode = ntohl(dep->fid.vnode);
3257 fid.unique = ntohl(dep->fid.unique);
3258 fileType = cm_FindFileType(&fid);
3259 osi_Log2(afsd_logp, "smb_ReceiveCoreSearchDir: file %s "
3260 "has filetype %d", dep->name,
3262 if (fileType == CM_SCACHETYPE_DIRECTORY)
3267 memcpy(op, mask, 11); op += 11;
3268 *op++ = (char) dsp->cookie; /* they say it must be non-zero */
3269 *op++ = nextEntryCookie & 0xff;
3270 *op++ = (nextEntryCookie>>8) & 0xff;
3271 *op++ = (nextEntryCookie>>16) & 0xff;
3272 *op++ = (nextEntryCookie>>24) & 0xff;
3273 memcpy(op, &clientCookie, 4); op += 4;
3275 /* now we emit the attribute. This is sort of tricky,
3276 * since we need to really stat the file to find out
3277 * what type of entry we've got. Right now, we're
3278 * copying out data from a buffer, while holding the
3279 * scp locked, so it isn't really convenient to stat
3280 * something now. We'll put in a place holder now,
3281 * and make a second pass before returning this to get
3282 * the real attributes. So, we just skip the data for
3283 * now, and adjust it later. We allocate a patch
3284 * record to make it easy to find this point later.
3285 * The replay will happen at a time when it is safe to
3286 * unlock the directory.
3288 curPatchp = malloc(sizeof(*curPatchp));
3289 osi_QAdd((osi_queue_t **) &dirListPatchesp, &curPatchp->q);
3290 curPatchp->dptr = op;
3291 curPatchp->fid.cell = scp->fid.cell;
3292 curPatchp->fid.volume = scp->fid.volume;
3293 curPatchp->fid.vnode = ntohl(dep->fid.vnode);
3294 curPatchp->fid.unique = ntohl(dep->fid.unique);
3296 /* do hidden attribute here since name won't be around when applying
3300 if ( smb_hideDotFiles && smb_IsDotFile(actualName) )
3301 curPatchp->flags = SMB_DIRLISTPATCH_DOTFILE;
3303 curPatchp->flags = 0;
3305 op += 9; /* skip attr, time, date and size */
3307 /* zero out name area. The spec says to pad with
3308 * spaces, but Samba doesn't, and neither do we.
3312 /* finally, we get to copy out the name; we know that
3313 * it fits in 8.3 or the pattern wouldn't match, but it
3314 * never hurts to be sure.
3316 strncpy(op, actualName, 13);
3318 /* Uppercase if requested by client */
3319 if ((((smb_t *)inp)->flg2 & 1) == 0)
3324 /* now, adjust the # of entries copied */
3326 } /* if we're including this name */
3329 /* and adjust curOffset to be where the new cookie is */
3330 thyper.HighPart = 0;
3331 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
3332 curOffset = LargeIntegerAdd(thyper, curOffset);
3333 } /* while copying data for dir listing */
3335 /* release the mutex */
3336 lock_ReleaseMutex(&scp->mx);
3337 if (bufferp) buf_Release(bufferp);
3339 /* apply and free last set of patches; if not doing a star match, this
3340 * will be empty, but better safe (and freeing everything) than sorry.
3342 smb_ApplyDirListPatches(&dirListPatchesp, userp, &req);
3344 /* special return code for unsuccessful search */
3345 if (code == 0 && dataLength < 21 && returnedNames == 0)
3346 code = CM_ERROR_NOFILES;
3348 osi_Log2(afsd_logp, "SMB search dir done, %d names, code %d",
3349 returnedNames, code);
3352 smb_DeleteDirSearch(dsp);
3353 smb_ReleaseDirSearch(dsp);
3354 cm_ReleaseSCache(scp);
3355 cm_ReleaseUser(userp);
3359 /* finalize the output buffer */
3360 smb_SetSMBParm(outp, 0, returnedNames);
3361 temp = (long) (op - origOp);
3362 smb_SetSMBDataLength(outp, temp);
3364 /* the data area is a variable block, which has a 5 (already there)
3365 * followed by the length of the # of data bytes. We now know this to
3366 * be "temp," although that includes the 3 bytes of vbl block header.
3367 * Deduct for them and fill in the length field.
3369 temp -= 3; /* deduct vbl block info */
3370 osi_assert(temp == (43 * returnedNames));
3371 origOp[1] = temp & 0xff;
3372 origOp[2] = (temp>>8) & 0xff;
3373 if (returnedNames == 0) smb_DeleteDirSearch(dsp);
3374 smb_ReleaseDirSearch(dsp);
3375 cm_ReleaseSCache(scp);
3376 cm_ReleaseUser(userp);
3380 /* verify that this is a valid path to a directory. I don't know why they
3381 * don't use the get file attributes call.
3383 long smb_ReceiveCoreCheckPath(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
3387 cm_scache_t *rootScp;
3388 cm_scache_t *newScp;
3397 pathp = smb_GetSMBData(inp, NULL);
3398 pathp = smb_ParseASCIIBlock(pathp, NULL);
3399 osi_Log1(afsd_logp, "SMB receive check path %s",
3400 osi_LogSaveString(afsd_logp, pathp));
3403 return CM_ERROR_BADFD;
3406 rootScp = cm_rootSCachep;
3408 userp = smb_GetUser(vcp, inp);
3410 caseFold = CM_FLAG_CASEFOLD;
3412 tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
3413 code = cm_NameI(rootScp, pathp,
3414 caseFold | CM_FLAG_FOLLOW | CM_FLAG_CHECKPATH,
3415 userp, tidPathp, &req, &newScp);
3418 cm_ReleaseUser(userp);