2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_smb.c,v 1.1.2.2 2003/01/17 08:20:26 tjr Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/vnode.h>
42 #include <sys/mount.h>
48 #include <netsmb/smb.h>
49 #include <netsmb/smb_subr.h>
50 #include <netsmb/smb_rq.h>
51 #include <netsmb/smb_conn.h>
53 #include <fs/smbfs/smbfs.h>
54 #include <fs/smbfs/smbfs_node.h>
55 #include <fs/smbfs/smbfs_subr.h>
58 * Lack of inode numbers leads us to the problem of generating them.
59 * Partially this problem can be solved by having a dir/file cache
60 * with inode numbers generated from the incremented by one counter.
61 * However this way will require too much kernel memory, gives all
62 * sorts of locking and consistency problems, not to mentinon counter overflows.
63 * So, I'm decided to use a hash function to generate pseudo random (and unique)
67 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
76 MD5Update(&md5, name, nmlen);
77 MD5Final((u_char *)state, &md5);
78 for (i = 0, ino = 0; i < 4; i++)
80 return dnp->n_ino + ino;
84 ino = dnp->n_ino + smbfs_hash(name, nmlen);
91 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
92 struct smb_cred *scred)
94 struct smb_share *ssp = np->n_mount->sm_share;
95 struct smb_rq rq, *rqp = &rq;
100 if (op == SMB_LOCK_SHARED)
101 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
102 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
105 smb_rq_getrequest(rqp, &mbp);
107 mb_put_uint8(mbp, 0xff); /* secondary command */
108 mb_put_uint8(mbp, 0); /* MBZ */
109 mb_put_uint16le(mbp, 0);
110 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
111 mb_put_uint8(mbp, ltype); /* locktype */
112 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
113 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
114 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
115 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
118 mb_put_uint16le(mbp, pid);
119 mb_put_uint32le(mbp, start);
120 mb_put_uint32le(mbp, end - start);
122 error = smb_rq_simple(rqp);
128 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
129 off_t start, off_t end, struct smb_cred *scred)
131 struct smb_share *ssp = np->n_mount->sm_share;
133 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
135 * TODO: use LOCK_BYTE_RANGE here.
139 return smbfs_smb_lockandx(np, op, (u_int32_t)id, start, end, scred);
143 smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
144 struct smb_cred *scred)
146 struct smb_t2rq *t2p;
150 u_int32_t units, bpu, funits;
153 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
157 mbp = &t2p->t2_tparam;
159 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
160 t2p->t2_maxpcount = 4;
161 t2p->t2_maxdcount = 4 * 4 + 2;
162 error = smb_t2_request(t2p);
167 mdp = &t2p->t2_rdata;
168 md_get_uint32(mdp, NULL); /* fs id */
169 md_get_uint32le(mdp, &bpu);
170 md_get_uint32le(mdp, &units);
171 md_get_uint32le(mdp, &funits);
172 md_get_uint16le(mdp, &bsize);
173 sbp->f_bsize = bpu * bsize; /* fundamental file system block size */
174 sbp->f_blocks= units; /* total data blocks in file system */
175 sbp->f_bfree = funits; /* free blocks in fs */
176 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
177 sbp->f_files = 0xffff; /* total file nodes in file system */
178 sbp->f_ffree = 0xffff; /* free file nodes in fs */
184 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
185 struct smb_cred *scred)
187 struct smb_rq rq, *rqp = &rq;
189 u_int16_t units, bpu, bsize, funits;
192 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
199 error = smb_rq_simple(rqp);
204 smb_rq_getreply(rqp, &mdp);
205 md_get_uint16le(mdp, &units);
206 md_get_uint16le(mdp, &bpu);
207 md_get_uint16le(mdp, &bsize);
208 md_get_uint16le(mdp, &funits);
209 sbp->f_bsize = bpu * bsize; /* fundamental file system block size */
210 sbp->f_blocks= units; /* total data blocks in file system */
211 sbp->f_bfree = funits; /* free blocks in fs */
212 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
213 sbp->f_files = 0xffff; /* total file nodes in file system */
214 sbp->f_ffree = 0xffff; /* free file nodes in fs */
220 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
222 struct smb_share *ssp = np->n_mount->sm_share;
223 struct smb_rq rq, *rqp = &rq;
227 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
230 smb_rq_getrequest(rqp, &mbp);
232 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
233 mb_put_uint16le(mbp, 0);
234 mb_put_uint32le(mbp, newsize);
235 mb_put_uint16le(mbp, 0);
238 mb_put_uint8(mbp, SMB_DT_DATA);
239 mb_put_uint16le(mbp, 0);
241 error = smb_rq_simple(rqp);
248 * Set DOS file attributes. mtime should be NULL for dialects above lm10
251 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
252 struct smb_cred *scred)
254 struct smb_rq rq, *rqp = &rq;
255 struct smb_share *ssp = np->n_mount->sm_share;
260 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
263 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
264 smb_rq_getrequest(rqp, &mbp);
266 mb_put_uint16le(mbp, attr);
268 smb_time_local2server(mtime, svtz, &time);
271 mb_put_uint32le(mbp, time); /* mtime */
272 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
275 mb_put_uint8(mbp, SMB_DT_ASCII);
277 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
280 mb_put_uint8(mbp, SMB_DT_ASCII);
281 mb_put_uint8(mbp, 0);
283 error = smb_rq_simple(rqp);
284 SMBERROR("%d\n", error);
293 * Note, win95 doesn't support this call.
296 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
297 struct timespec *atime, int attr, struct smb_cred *scred)
299 struct smb_t2rq *t2p;
300 struct smb_share *ssp = np->n_mount->sm_share;
301 struct smb_vc *vcp = SSTOVC(ssp);
303 u_int16_t date, time;
306 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
310 mbp = &t2p->t2_tparam;
312 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
313 mb_put_uint32le(mbp, 0); /* MBZ */
314 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
319 tzoff = vcp->vc_sopt.sv_tz;
320 mbp = &t2p->t2_tdata;
322 mb_put_uint32le(mbp, 0); /* creation time */
324 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
327 mb_put_uint16le(mbp, date);
328 mb_put_uint16le(mbp, time);
330 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
333 mb_put_uint16le(mbp, date);
334 mb_put_uint16le(mbp, time);
335 mb_put_uint32le(mbp, 0); /* file size */
336 mb_put_uint32le(mbp, 0); /* allocation unit size */
337 mb_put_uint16le(mbp, attr); /* DOS attr */
338 mb_put_uint32le(mbp, 0); /* EA size */
339 t2p->t2_maxpcount = 5 * 2;
340 t2p->t2_maxdcount = vcp->vc_txmax;
341 error = smb_t2_request(t2p);
347 * NT level. Specially for win9x
350 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
351 struct timespec *atime, struct smb_cred *scred)
353 struct smb_t2rq *t2p;
354 struct smb_share *ssp = np->n_mount->sm_share;
355 struct smb_vc *vcp = SSTOVC(ssp);
360 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
364 mbp = &t2p->t2_tparam;
366 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
367 mb_put_uint32le(mbp, 0); /* MBZ */
368 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
373 tzoff = vcp->vc_sopt.sv_tz;
374 mbp = &t2p->t2_tdata;
376 mb_put_int64le(mbp, 0); /* creation time */
378 smb_time_local2NT(atime, tzoff, &tm);
381 mb_put_int64le(mbp, tm);
383 smb_time_local2NT(mtime, tzoff, &tm);
386 mb_put_int64le(mbp, tm);
387 mb_put_int64le(mbp, tm); /* change time */
388 mb_put_uint32le(mbp, attr); /* attr */
389 t2p->t2_maxpcount = 24;
390 t2p->t2_maxdcount = 56;
391 error = smb_t2_request(t2p);
397 * Set file atime and mtime. Doesn't supported by core dialect.
400 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
401 struct timespec *atime, struct smb_cred *scred)
403 struct smb_rq rq, *rqp = &rq;
404 struct smb_share *ssp = np->n_mount->sm_share;
406 u_int16_t date, time;
409 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
412 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
413 smb_rq_getrequest(rqp, &mbp);
415 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
416 mb_put_uint32le(mbp, 0); /* creation time */
419 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
422 mb_put_uint16le(mbp, date);
423 mb_put_uint16le(mbp, time);
425 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
428 mb_put_uint16le(mbp, date);
429 mb_put_uint16le(mbp, time);
433 error = smb_rq_simple(rqp);
434 SMBSDEBUG("%d\n", error);
440 * Set DOS file attributes.
441 * Looks like this call can be used only if CAP_NT_SMBS bit is on.
444 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
445 struct timespec *atime, struct smb_cred *scred)
447 struct smb_t2rq *t2p;
448 struct smb_share *ssp = np->n_mount->sm_share;
453 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
457 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
458 mbp = &t2p->t2_tparam;
460 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
461 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
462 mb_put_uint32le(mbp, 0);
463 mbp = &t2p->t2_tdata;
465 mb_put_int64le(mbp, 0); /* creation time */
467 smb_time_local2NT(atime, svtz, &tm);
470 mb_put_int64le(mbp, tm);
472 smb_time_local2NT(mtime, svtz, &tm);
475 mb_put_int64le(mbp, tm);
476 mb_put_int64le(mbp, tm); /* change time */
477 mb_put_uint16le(mbp, attr);
478 mb_put_uint32le(mbp, 0); /* padding */
479 mb_put_uint16le(mbp, 0);
480 t2p->t2_maxpcount = 2;
481 t2p->t2_maxdcount = 0;
482 error = smb_t2_request(t2p);
489 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
491 struct smb_rq rq, *rqp = &rq;
492 struct smb_share *ssp = np->n_mount->sm_share;
496 u_int16_t fid, wattr, grantedmode;
499 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
502 smb_rq_getrequest(rqp, &mbp);
504 mb_put_uint16le(mbp, accmode);
505 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
508 mb_put_uint8(mbp, SMB_DT_ASCII);
510 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
514 error = smb_rq_simple(rqp);
517 smb_rq_getreply(rqp, &mdp);
518 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
522 md_get_uint16(mdp, &fid);
523 md_get_uint16le(mdp, &wattr);
524 md_get_uint32(mdp, NULL); /* mtime */
525 md_get_uint32(mdp, NULL); /* fsize */
526 md_get_uint16le(mdp, &grantedmode);
528 * TODO: refresh attributes from this reply
535 np->n_rwstate = grantedmode;
541 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
542 struct smb_cred *scred)
544 struct smb_rq rq, *rqp = &rq;
549 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
552 smb_rq_getrequest(rqp, &mbp);
554 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
556 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
559 mb_put_uint32le(mbp, time);
563 error = smb_rq_simple(rqp);
569 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
570 struct smb_cred *scred)
572 struct smb_rq rq, *rqp = &rq;
573 struct smb_share *ssp = dnp->n_mount->sm_share;
576 struct timespec ctime;
582 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
585 smb_rq_getrequest(rqp, &mbp);
587 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
589 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
590 mb_put_uint32le(mbp, tm);
593 mb_put_uint8(mbp, SMB_DT_ASCII);
594 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
597 error = smb_rq_simple(rqp);
599 smb_rq_getreply(rqp, &mdp);
600 md_get_uint8(mdp, &wc);
602 md_get_uint16(mdp, &fid);
610 smbfs_smb_close(ssp, fid, &ctime, scred);
615 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
617 struct smb_rq rq, *rqp = &rq;
618 struct smb_share *ssp = np->n_mount->sm_share;
622 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
625 smb_rq_getrequest(rqp, &mbp);
627 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
630 mb_put_uint8(mbp, SMB_DT_ASCII);
631 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
634 error = smb_rq_simple(rqp);
641 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
642 const char *tname, int tnmlen, struct smb_cred *scred)
644 struct smb_rq rq, *rqp = &rq;
645 struct smb_share *ssp = src->n_mount->sm_share;
649 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
652 smb_rq_getrequest(rqp, &mbp);
654 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
657 mb_put_uint8(mbp, SMB_DT_ASCII);
659 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
662 mb_put_uint8(mbp, SMB_DT_ASCII);
663 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
667 error = smb_rq_simple(rqp);
674 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
675 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
677 struct smb_rq rq, *rqp = &rq;
678 struct smb_share *ssp = src->n_mount->sm_share;
682 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
685 smb_rq_getrequest(rqp, &mbp);
687 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
688 mb_put_uint16le(mbp, 0x20); /* delete target file */
689 mb_put_uint16le(mbp, flags);
692 mb_put_uint8(mbp, SMB_DT_ASCII);
694 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
697 mb_put_uint8(mbp, SMB_DT_ASCII);
698 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
702 error = smb_rq_simple(rqp);
709 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
710 struct smb_cred *scred)
712 struct smb_rq rq, *rqp = &rq;
713 struct smb_share *ssp = dnp->n_mount->sm_share;
717 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
720 smb_rq_getrequest(rqp, &mbp);
724 mb_put_uint8(mbp, SMB_DT_ASCII);
725 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
728 error = smb_rq_simple(rqp);
735 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
737 struct smb_rq rq, *rqp = &rq;
738 struct smb_share *ssp = np->n_mount->sm_share;
742 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
745 smb_rq_getrequest(rqp, &mbp);
749 mb_put_uint8(mbp, SMB_DT_ASCII);
750 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
753 error = smb_rq_simple(rqp);
760 smbfs_smb_search(struct smbfs_fctx *ctx)
762 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
767 u_int16_t ec, dlen, bc;
768 int maxent, error, iseof = 0;
770 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
772 smb_rq_done(ctx->f_rq);
775 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
779 smb_rq_getrequest(rqp, &mbp);
781 mb_put_uint16le(mbp, maxent); /* max entries to return */
782 mb_put_uint16le(mbp, ctx->f_attrmask);
785 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
786 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
787 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
790 mb_put_uint8(mbp, SMB_DT_VARIABLE);
791 mb_put_uint16le(mbp, 0); /* context length */
792 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
794 mb_put_uint8(mbp, 0); /* file name length */
795 mb_put_uint8(mbp, SMB_DT_VARIABLE);
796 mb_put_uint16le(mbp, SMB_SKEYLEN);
797 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
800 error = smb_rq_simple(rqp);
802 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
805 ctx->f_flags |= SMBFS_RDD_EOF;
809 smb_rq_getreply(rqp, &mdp);
810 md_get_uint8(mdp, &wc);
812 return iseof ? ENOENT : EBADRPC;
813 md_get_uint16le(mdp, &ec);
817 md_get_uint16le(mdp, &bc);
821 md_get_uint8(mdp, &bt);
822 if (bt != SMB_DT_VARIABLE)
824 md_get_uint16le(mdp, &dlen);
825 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
831 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
832 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
834 ctx->f_attrmask = attr;
836 if (wclen == 1 && wildcard[0] == '*') {
837 ctx->f_wildcard = "*.*";
840 ctx->f_wildcard = wildcard;
841 ctx->f_wclen = wclen;
844 ctx->f_wildcard = NULL;
847 ctx->f_name = ctx->f_fname;
852 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
858 u_int16_t date, time;
862 if (ctx->f_ecnt == 0) {
863 if (ctx->f_flags & SMBFS_RDD_EOF)
865 ctx->f_left = ctx->f_limit = limit;
866 error = smbfs_smb_search(ctx);
871 smb_rq_getreply(rqp, &mbp);
872 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
873 md_get_uint8(mbp, &battr);
874 md_get_uint16le(mbp, &time);
875 md_get_uint16le(mbp, &date);
876 md_get_uint32le(mbp, &size);
878 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
879 cp[sizeof(ctx->f_fname) - 1] = 0;
880 cp += strlen(cp) - 1;
881 while (*cp == ' ' && cp >= ctx->f_name)
883 ctx->f_attr.fa_attr = battr;
884 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
885 &ctx->f_attr.fa_mtime);
886 ctx->f_attr.fa_size = size;
887 ctx->f_nmlen = strlen(ctx->f_name);
894 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
897 smb_rq_done(ctx->f_rq);
902 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
905 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
907 struct smb_t2rq *t2p;
908 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
915 smb_t2_done(ctx->f_t2);
918 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
919 flags = 8 | 2; /* <resume> | <close if EOS> */
920 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
921 flags |= 1; /* close search after this request */
922 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
924 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
925 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
930 mbp = &t2p->t2_tparam;
932 mb_put_uint16le(mbp, ctx->f_attrmask);
933 mb_put_uint16le(mbp, ctx->f_limit);
934 mb_put_uint16le(mbp, flags);
935 mb_put_uint16le(mbp, ctx->f_infolevel);
936 mb_put_uint32le(mbp, 0);
937 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
941 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
946 mbp = &t2p->t2_tparam;
948 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
949 mb_put_uint16le(mbp, ctx->f_limit);
950 mb_put_uint16le(mbp, ctx->f_infolevel);
951 mb_put_uint32le(mbp, 0); /* resume key */
952 mb_put_uint16le(mbp, flags);
954 mb_put_mem(mbp, ctx->f_rname, strlen(ctx->f_rname) + 1, MB_MSYSTEM);
956 mb_put_uint8(mbp, 0); /* resume file name */
960 tv.tv_usec = 200 * 1000; /* 200ms */
961 if (vcp->vc_flags & SMBC_WIN95) {
963 * some implementations suggests to sleep here
964 * for 200ms, due to the bug in the Win95.
965 * I've didn't notice any problem, but put code
968 tsleep(&flags, PVFS, "fix95", tvtohz(&tv));
972 t2p->t2_maxpcount = 5 * 2;
973 t2p->t2_maxdcount = vcp->vc_txmax;
974 error = smb_t2_request(t2p);
977 mdp = &t2p->t2_rparam;
978 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
979 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
981 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
983 if ((error = md_get_uint16le(mdp, &tw)) != 0)
986 if ((error = md_get_uint16le(mdp, &tw)) != 0)
989 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
990 if ((error = md_get_uint16le(mdp, &tw)) != 0)
992 if ((error = md_get_uint16le(mdp, &tw)) != 0)
994 if (ctx->f_ecnt == 0)
996 ctx->f_rnameofs = tw;
997 mdp = &t2p->t2_rdata;
998 if (mdp->md_top == NULL) {
999 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1002 if (mdp->md_top->m_len == 0) {
1003 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1011 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1013 struct smb_rq rq, *rqp = &rq;
1014 struct mbchain *mbp;
1017 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1020 smb_rq_getrequest(rqp, &mbp);
1022 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1026 error = smb_rq_simple(rqp);
1032 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1033 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1035 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1036 if (ctx->f_name == NULL)
1038 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1039 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1040 ctx->f_attrmask = attr;
1041 ctx->f_wildcard = wildcard;
1042 ctx->f_wclen = wclen;
1047 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1049 struct mdchain *mbp;
1050 struct smb_t2rq *t2p;
1053 u_int16_t date, time, wattr;
1054 u_int32_t size, next, dattr;
1056 int error, svtz, cnt, fxsz, nmlen, recsz;
1058 if (ctx->f_ecnt == 0) {
1059 if (ctx->f_flags & SMBFS_RDD_EOF)
1061 ctx->f_left = ctx->f_limit = limit;
1062 error = smbfs_smb_trans2find2(ctx);
1067 mbp = &t2p->t2_rdata;
1068 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1069 switch (ctx->f_infolevel) {
1070 case SMB_INFO_STANDARD:
1073 md_get_uint16le(mbp, &date);
1074 md_get_uint16le(mbp, &time); /* creation time */
1075 md_get_uint16le(mbp, &date);
1076 md_get_uint16le(mbp, &time); /* access time */
1077 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1078 md_get_uint16le(mbp, &date);
1079 md_get_uint16le(mbp, &time); /* access time */
1080 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1081 md_get_uint32le(mbp, &size);
1082 ctx->f_attr.fa_size = size;
1083 md_get_uint32(mbp, NULL); /* allocation size */
1084 md_get_uint16le(mbp, &wattr);
1085 ctx->f_attr.fa_attr = wattr;
1086 md_get_uint8(mbp, &tb);
1089 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1091 case SMB_FIND_FILE_DIRECTORY_INFO:
1092 md_get_uint32le(mbp, &next);
1093 md_get_uint32(mbp, NULL); /* file index */
1094 md_get_int64(mbp, NULL); /* creation time */
1095 md_get_int64le(mbp, &lint);
1096 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1097 md_get_int64le(mbp, &lint);
1098 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1099 md_get_int64le(mbp, &lint);
1100 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1101 md_get_int64le(mbp, &lint); /* file size */
1102 ctx->f_attr.fa_size = lint;
1103 md_get_int64(mbp, NULL); /* real size (should use) */
1104 md_get_uint32(mbp, &dattr); /* EA */
1105 ctx->f_attr.fa_attr = dattr;
1106 md_get_uint32le(mbp, &size); /* name len */
1108 recsz = next ? next : fxsz + size;
1111 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1114 nmlen = min(size, SMB_MAXFNAMELEN);
1116 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1120 cnt = next - nmlen - fxsz;
1122 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1124 SMBERROR("out of sync\n");
1128 if (nmlen && cp[nmlen - 1] == 0)
1133 next = ctx->f_eofs + recsz;
1134 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1135 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1137 * Server needs a resume filename.
1139 if (ctx->f_rnamelen <= nmlen) {
1141 free(ctx->f_rname, M_SMBFSDATA);
1142 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1143 ctx->f_rnamelen = nmlen;
1145 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1146 ctx->f_rname[nmlen] = 0;
1147 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1149 ctx->f_nmlen = nmlen;
1157 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1160 free(ctx->f_name, M_SMBFSDATA);
1162 smb_t2_done(ctx->f_t2);
1163 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1164 smbfs_smb_findclose2(ctx);
1169 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1170 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1172 struct smbfs_fctx *ctx;
1175 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1178 bzero(ctx, sizeof(*ctx));
1179 ctx->f_ssp = dnp->n_mount->sm_share;
1181 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1182 ctx->f_scred = scred;
1183 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1184 (dnp->n_mount->sm_args.flags & SMBFS_MOUNT_NO_LONG)) {
1185 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1186 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1188 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1190 smbfs_findclose(ctx, scred);
1197 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1204 limit *= 4; /* imperical */
1205 ctx->f_scred = scred;
1207 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1208 error = smbfs_findnextLM1(ctx, limit);
1210 error = smbfs_findnextLM2(ctx, limit);
1213 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1214 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1215 ctx->f_name[1] == '.'))
1219 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, ctx->f_nmlen,
1220 ctx->f_dnp->n_mount->sm_caseopt);
1221 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1226 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1228 ctx->f_scred = scred;
1229 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1230 smbfs_findcloseLM1(ctx);
1232 smbfs_findcloseLM2(ctx);
1234 free(ctx->f_rname, M_SMBFSDATA);
1235 free(ctx, M_SMBFSDATA);
1240 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1241 struct smbfattr *fap, struct smb_cred *scred)
1243 struct smbfs_fctx *ctx;
1246 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1247 bzero(fap, sizeof(*fap));
1248 fap->fa_attr = SMB_FA_DIR;
1252 if (nmlen == 1 && name[0] == '.') {
1253 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1255 } else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
1256 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1258 printf("%s: knows NOTHING about '..'\n", __FUNCTION__);
1261 error = smbfs_findopen(dnp, name, nmlen,
1262 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1265 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1266 error = smbfs_findnext(ctx, 1, scred);
1270 fap->fa_ino = dnp->n_ino;
1272 smbfs_findclose(ctx, scred);