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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
34 #include <sys/vnode.h>
36 #include <sys/mount.h>
42 #include <netsmb/smb.h>
43 #include <netsmb/smb_subr.h>
44 #include <netsmb/smb_rq.h>
45 #include <netsmb/smb_conn.h>
47 #include <fs/smbfs/smbfs.h>
48 #include <fs/smbfs/smbfs_node.h>
49 #include <fs/smbfs/smbfs_subr.h>
52 * Lack of inode numbers leads us to the problem of generating them.
53 * Partially this problem can be solved by having a dir/file cache
54 * with inode numbers generated from the incremented by one counter.
55 * However this way will require too much kernel memory, gives all
56 * sorts of locking and consistency problems, not to mentinon counter overflows.
57 * So, I'm decided to use a hash function to generate pseudo random (and unique)
61 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
70 MD5Update(&md5, name, nmlen);
71 MD5Final((u_char *)state, &md5);
72 for (i = 0, ino = 0; i < 4; i++)
74 return dnp->n_ino + ino;
78 ino = dnp->n_ino + smbfs_hash(name, nmlen);
85 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
86 struct smb_cred *scred)
88 struct smb_share *ssp = np->n_mount->sm_share;
89 struct smb_rq rq, *rqp = &rq;
94 if (op == SMB_LOCK_SHARED)
95 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
96 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
99 smb_rq_getrequest(rqp, &mbp);
101 mb_put_uint8(mbp, 0xff); /* secondary command */
102 mb_put_uint8(mbp, 0); /* MBZ */
103 mb_put_uint16le(mbp, 0);
104 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
105 mb_put_uint8(mbp, ltype); /* locktype */
106 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
107 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
108 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
109 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
112 mb_put_uint16le(mbp, pid);
113 mb_put_uint32le(mbp, start);
114 mb_put_uint32le(mbp, end - start);
116 error = smb_rq_simple(rqp);
122 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
123 off_t start, off_t end, struct smb_cred *scred)
125 struct smb_share *ssp = np->n_mount->sm_share;
127 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
129 * TODO: use LOCK_BYTE_RANGE here.
133 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
137 smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
138 struct smb_cred *scred)
140 struct smb_t2rq *t2p;
144 u_int32_t units, bpu, funits;
147 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
151 mbp = &t2p->t2_tparam;
153 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
154 t2p->t2_maxpcount = 4;
155 t2p->t2_maxdcount = 4 * 4 + 2;
156 error = smb_t2_request(t2p);
161 mdp = &t2p->t2_rdata;
162 md_get_uint32(mdp, NULL); /* fs id */
163 md_get_uint32le(mdp, &bpu);
164 md_get_uint32le(mdp, &units);
165 md_get_uint32le(mdp, &funits);
166 md_get_uint16le(mdp, &bsize);
167 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
168 sbp->f_blocks= units; /* total data blocks in filesystem */
169 sbp->f_bfree = funits; /* free blocks in fs */
170 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
171 sbp->f_files = 0xffff; /* total file nodes in filesystem */
172 sbp->f_ffree = 0xffff; /* free file nodes in fs */
178 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
179 struct smb_cred *scred)
181 struct smb_rq rq, *rqp = &rq;
183 u_int16_t units, bpu, bsize, funits;
186 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
193 error = smb_rq_simple(rqp);
198 smb_rq_getreply(rqp, &mdp);
199 md_get_uint16le(mdp, &units);
200 md_get_uint16le(mdp, &bpu);
201 md_get_uint16le(mdp, &bsize);
202 md_get_uint16le(mdp, &funits);
203 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
204 sbp->f_blocks= units; /* total data blocks in filesystem */
205 sbp->f_bfree = funits; /* free blocks in fs */
206 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
207 sbp->f_files = 0xffff; /* total file nodes in filesystem */
208 sbp->f_ffree = 0xffff; /* free file nodes in fs */
214 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
216 struct smb_t2rq *t2p;
217 struct smb_share *ssp = np->n_mount->sm_share;
221 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
225 mbp = &t2p->t2_tparam;
227 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
228 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
229 mb_put_uint32le(mbp, 0);
230 mbp = &t2p->t2_tdata;
232 mb_put_int64le(mbp, newsize);
233 mb_put_uint32le(mbp, 0); /* padding */
234 mb_put_uint16le(mbp, 0);
235 t2p->t2_maxpcount = 2;
236 t2p->t2_maxdcount = 0;
237 error = smb_t2_request(t2p);
243 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
245 struct smb_share *ssp = np->n_mount->sm_share;
246 struct smb_rq rq, *rqp = &rq;
250 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
251 SMBTOV(np)->v_type != VREG)
252 return 0; /* not a regular open file */
253 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scred);
256 smb_rq_getrequest(rqp, &mbp);
258 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
262 error = smb_rq_simple(rqp);
265 np->n_flag &= ~NFLUSHWIRE;
270 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
272 if (np->n_flag & NFLUSHWIRE)
273 return (smb_smb_flush(np, scred));
278 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
280 struct smb_share *ssp = np->n_mount->sm_share;
281 struct smb_rq rq, *rqp = &rq;
285 if (!smbfs_smb_seteof(np, (int64_t) newsize, scred)) {
286 np->n_flag |= NFLUSHWIRE;
290 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
293 smb_rq_getrequest(rqp, &mbp);
295 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
296 mb_put_uint16le(mbp, 0);
297 mb_put_uint32le(mbp, newsize);
298 mb_put_uint16le(mbp, 0);
301 mb_put_uint8(mbp, SMB_DT_DATA);
302 mb_put_uint16le(mbp, 0);
304 error = smb_rq_simple(rqp);
310 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
311 struct smbfattr *fap, struct smb_cred *scred)
313 struct smb_rq rq, *rqp = &rq;
314 struct smb_share *ssp = np->n_mount->sm_share;
322 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred);
325 smb_rq_getrequest(rqp, &mbp);
329 mb_put_uint8(mbp, SMB_DT_ASCII);
331 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
335 error = smb_rq_simple(rqp);
338 smb_rq_getreply(rqp, &mdp);
339 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
343 md_get_uint16le(mdp, &wattr);
344 fap->fa_attr = wattr;
346 * Be careful using the time returned here, as
347 * with FAT on NT4SP6, at least, the time returned is low
348 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
349 * over about every seven minutes!
351 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
352 if (lint) /* avoid bogus zero returns */
353 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
355 md_get_uint32le(mdp, &lint);
363 * Set DOS file attributes. mtime should be NULL for dialects above lm10
366 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
367 struct smb_cred *scred)
369 struct smb_rq rq, *rqp = &rq;
370 struct smb_share *ssp = np->n_mount->sm_share;
375 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
378 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
379 smb_rq_getrequest(rqp, &mbp);
381 mb_put_uint16le(mbp, attr);
383 smb_time_local2server(mtime, svtz, &time);
386 mb_put_uint32le(mbp, time); /* mtime */
387 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
390 mb_put_uint8(mbp, SMB_DT_ASCII);
392 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
395 mb_put_uint8(mbp, SMB_DT_ASCII);
396 mb_put_uint8(mbp, 0);
398 error = smb_rq_simple(rqp);
400 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
409 * Note, win95 doesn't support this call.
412 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
413 struct timespec *atime, int attr, struct smb_cred *scred)
415 struct smb_t2rq *t2p;
416 struct smb_share *ssp = np->n_mount->sm_share;
417 struct smb_vc *vcp = SSTOVC(ssp);
419 u_int16_t date, time;
422 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
426 mbp = &t2p->t2_tparam;
428 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
429 mb_put_uint32le(mbp, 0); /* MBZ */
430 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
431 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
436 tzoff = vcp->vc_sopt.sv_tz;
437 mbp = &t2p->t2_tdata;
439 mb_put_uint32le(mbp, 0); /* creation time */
441 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
444 mb_put_uint16le(mbp, date);
445 mb_put_uint16le(mbp, time);
447 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
450 mb_put_uint16le(mbp, date);
451 mb_put_uint16le(mbp, time);
452 mb_put_uint32le(mbp, 0); /* file size */
453 mb_put_uint32le(mbp, 0); /* allocation unit size */
454 mb_put_uint16le(mbp, attr); /* DOS attr */
455 mb_put_uint32le(mbp, 0); /* EA size */
456 t2p->t2_maxpcount = 5 * 2;
457 t2p->t2_maxdcount = vcp->vc_txmax;
458 error = smb_t2_request(t2p);
464 * NT level. Specially for win9x
467 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
468 struct timespec *atime, struct smb_cred *scred)
470 struct smb_t2rq *t2p;
471 struct smb_share *ssp = np->n_mount->sm_share;
472 struct smb_vc *vcp = SSTOVC(ssp);
477 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
481 mbp = &t2p->t2_tparam;
483 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
484 mb_put_uint32le(mbp, 0); /* MBZ */
485 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
486 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
491 tzoff = vcp->vc_sopt.sv_tz;
492 mbp = &t2p->t2_tdata;
494 mb_put_int64le(mbp, 0); /* creation time */
496 smb_time_local2NT(atime, tzoff, &tm);
499 mb_put_int64le(mbp, tm);
501 smb_time_local2NT(mtime, tzoff, &tm);
504 mb_put_int64le(mbp, tm);
505 mb_put_int64le(mbp, tm); /* change time */
506 mb_put_uint32le(mbp, attr); /* attr */
507 t2p->t2_maxpcount = 24;
508 t2p->t2_maxdcount = 56;
509 error = smb_t2_request(t2p);
515 * Set file atime and mtime. Doesn't supported by core dialect.
518 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
519 struct timespec *atime, struct smb_cred *scred)
521 struct smb_rq rq, *rqp = &rq;
522 struct smb_share *ssp = np->n_mount->sm_share;
524 u_int16_t date, time;
527 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
530 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
531 smb_rq_getrequest(rqp, &mbp);
533 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
534 mb_put_uint32le(mbp, 0); /* creation time */
537 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
540 mb_put_uint16le(mbp, date);
541 mb_put_uint16le(mbp, time);
543 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
546 mb_put_uint16le(mbp, date);
547 mb_put_uint16le(mbp, time);
551 error = smb_rq_simple(rqp);
552 SMBSDEBUG("%d\n", error);
558 * Set DOS file attributes.
559 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
562 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
563 struct timespec *atime, struct smb_cred *scred)
565 struct smb_t2rq *t2p;
566 struct smb_share *ssp = np->n_mount->sm_share;
571 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
575 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
576 mbp = &t2p->t2_tparam;
578 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
579 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
580 mb_put_uint32le(mbp, 0);
581 mbp = &t2p->t2_tdata;
583 mb_put_int64le(mbp, 0); /* creation time */
585 smb_time_local2NT(atime, svtz, &tm);
588 mb_put_int64le(mbp, tm);
590 smb_time_local2NT(mtime, svtz, &tm);
593 mb_put_int64le(mbp, tm);
594 mb_put_int64le(mbp, tm); /* change time */
595 mb_put_uint16le(mbp, attr);
596 mb_put_uint32le(mbp, 0); /* padding */
597 mb_put_uint16le(mbp, 0);
598 t2p->t2_maxpcount = 2;
599 t2p->t2_maxdcount = 0;
600 error = smb_t2_request(t2p);
607 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
609 struct smb_rq rq, *rqp = &rq;
610 struct smb_share *ssp = np->n_mount->sm_share;
614 u_int16_t fid, wattr, grantedmode;
617 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
620 smb_rq_getrequest(rqp, &mbp);
622 mb_put_uint16le(mbp, accmode);
623 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
626 mb_put_uint8(mbp, SMB_DT_ASCII);
628 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
632 error = smb_rq_simple(rqp);
635 smb_rq_getreply(rqp, &mdp);
636 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
640 md_get_uint16(mdp, &fid);
641 md_get_uint16le(mdp, &wattr);
642 md_get_uint32(mdp, NULL); /* mtime */
643 md_get_uint32(mdp, NULL); /* fsize */
644 md_get_uint16le(mdp, &grantedmode);
646 * TODO: refresh attributes from this reply
653 np->n_rwstate = grantedmode;
659 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
660 struct smb_cred *scred)
662 struct smb_rq rq, *rqp = &rq;
667 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
670 smb_rq_getrequest(rqp, &mbp);
672 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
674 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
677 mb_put_uint32le(mbp, time);
681 error = smb_rq_simple(rqp);
687 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
688 struct smb_cred *scred)
690 struct smb_rq rq, *rqp = &rq;
691 struct smb_share *ssp = dnp->n_mount->sm_share;
694 struct timespec ctime;
700 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
703 smb_rq_getrequest(rqp, &mbp);
705 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
707 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
708 mb_put_uint32le(mbp, tm);
711 mb_put_uint8(mbp, SMB_DT_ASCII);
712 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
715 error = smb_rq_simple(rqp);
717 smb_rq_getreply(rqp, &mdp);
718 md_get_uint8(mdp, &wc);
720 md_get_uint16(mdp, &fid);
728 smbfs_smb_close(ssp, fid, &ctime, scred);
733 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
735 struct smb_rq rq, *rqp = &rq;
736 struct smb_share *ssp = np->n_mount->sm_share;
740 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
743 smb_rq_getrequest(rqp, &mbp);
745 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
748 mb_put_uint8(mbp, SMB_DT_ASCII);
749 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
752 error = smb_rq_simple(rqp);
759 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
760 const char *tname, int tnmlen, struct smb_cred *scred)
762 struct smb_rq rq, *rqp = &rq;
763 struct smb_share *ssp = src->n_mount->sm_share;
767 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
770 smb_rq_getrequest(rqp, &mbp);
772 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
775 mb_put_uint8(mbp, SMB_DT_ASCII);
777 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
780 mb_put_uint8(mbp, SMB_DT_ASCII);
781 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
785 error = smb_rq_simple(rqp);
792 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
793 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
795 struct smb_rq rq, *rqp = &rq;
796 struct smb_share *ssp = src->n_mount->sm_share;
800 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
803 smb_rq_getrequest(rqp, &mbp);
805 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
806 mb_put_uint16le(mbp, 0x20); /* delete target file */
807 mb_put_uint16le(mbp, flags);
810 mb_put_uint8(mbp, SMB_DT_ASCII);
812 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
815 mb_put_uint8(mbp, SMB_DT_ASCII);
816 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
820 error = smb_rq_simple(rqp);
827 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
828 struct smb_cred *scred)
830 struct smb_rq rq, *rqp = &rq;
831 struct smb_share *ssp = dnp->n_mount->sm_share;
835 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
838 smb_rq_getrequest(rqp, &mbp);
842 mb_put_uint8(mbp, SMB_DT_ASCII);
843 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
846 error = smb_rq_simple(rqp);
853 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
855 struct smb_rq rq, *rqp = &rq;
856 struct smb_share *ssp = np->n_mount->sm_share;
860 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
863 smb_rq_getrequest(rqp, &mbp);
867 mb_put_uint8(mbp, SMB_DT_ASCII);
868 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
871 error = smb_rq_simple(rqp);
878 smbfs_smb_search(struct smbfs_fctx *ctx)
880 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
885 u_int16_t ec, dlen, bc;
886 int maxent, error, iseof = 0;
888 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
890 smb_rq_done(ctx->f_rq);
893 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
897 smb_rq_getrequest(rqp, &mbp);
899 mb_put_uint16le(mbp, maxent); /* max entries to return */
900 mb_put_uint16le(mbp, ctx->f_attrmask);
903 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
904 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
905 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
908 mb_put_uint8(mbp, SMB_DT_VARIABLE);
909 mb_put_uint16le(mbp, 0); /* context length */
910 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
912 mb_put_uint8(mbp, 0); /* file name length */
913 mb_put_uint8(mbp, SMB_DT_VARIABLE);
914 mb_put_uint16le(mbp, SMB_SKEYLEN);
915 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
918 error = smb_rq_simple(rqp);
920 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
923 ctx->f_flags |= SMBFS_RDD_EOF;
927 smb_rq_getreply(rqp, &mdp);
928 md_get_uint8(mdp, &wc);
930 return iseof ? ENOENT : EBADRPC;
931 md_get_uint16le(mdp, &ec);
935 md_get_uint16le(mdp, &bc);
939 md_get_uint8(mdp, &bt);
940 if (bt != SMB_DT_VARIABLE)
942 md_get_uint16le(mdp, &dlen);
943 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
949 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
950 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
952 ctx->f_attrmask = attr;
954 if (wclen == 1 && wildcard[0] == '*') {
955 ctx->f_wildcard = "*.*";
958 ctx->f_wildcard = wildcard;
959 ctx->f_wclen = wclen;
962 ctx->f_wildcard = NULL;
965 ctx->f_name = ctx->f_fname;
970 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
976 u_int16_t date, time;
980 if (ctx->f_ecnt == 0) {
981 if (ctx->f_flags & SMBFS_RDD_EOF)
983 ctx->f_left = ctx->f_limit = limit;
984 error = smbfs_smb_search(ctx);
989 smb_rq_getreply(rqp, &mbp);
990 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
991 md_get_uint8(mbp, &battr);
992 md_get_uint16le(mbp, &time);
993 md_get_uint16le(mbp, &date);
994 md_get_uint32le(mbp, &size);
996 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
997 cp[sizeof(ctx->f_fname) - 1] = 0;
998 cp += strlen(cp) - 1;
999 while (*cp == ' ' && cp >= ctx->f_name)
1001 ctx->f_attr.fa_attr = battr;
1002 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1003 &ctx->f_attr.fa_mtime);
1004 ctx->f_attr.fa_size = size;
1005 ctx->f_nmlen = strlen(ctx->f_name);
1012 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1015 smb_rq_done(ctx->f_rq);
1020 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1023 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1025 struct smb_t2rq *t2p;
1026 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1027 struct mbchain *mbp;
1028 struct mdchain *mdp;
1029 u_int16_t tw, flags;
1033 smb_t2_done(ctx->f_t2);
1036 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1037 flags = 8 | 2; /* <resume> | <close if EOS> */
1038 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1039 flags |= 1; /* close search after this request */
1040 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1042 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1043 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1044 ctx->f_scred, &t2p);
1048 mbp = &t2p->t2_tparam;
1050 mb_put_uint16le(mbp, ctx->f_attrmask);
1051 mb_put_uint16le(mbp, ctx->f_limit);
1052 mb_put_uint16le(mbp, flags);
1053 mb_put_uint16le(mbp, ctx->f_infolevel);
1054 mb_put_uint32le(mbp, 0);
1055 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1059 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1060 ctx->f_scred, &t2p);
1064 mbp = &t2p->t2_tparam;
1066 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1067 mb_put_uint16le(mbp, ctx->f_limit);
1068 mb_put_uint16le(mbp, ctx->f_infolevel);
1069 mb_put_uint32le(mbp, 0); /* resume key */
1070 mb_put_uint16le(mbp, flags);
1072 mb_put_mem(mbp, ctx->f_rname, strlen(ctx->f_rname) + 1, MB_MSYSTEM);
1074 mb_put_uint8(mbp, 0); /* resume file name */
1078 tv.tv_usec = 200 * 1000; /* 200ms */
1079 if (vcp->vc_flags & SMBC_WIN95) {
1081 * some implementations suggests to sleep here
1082 * for 200ms, due to the bug in the Win95.
1083 * I've didn't notice any problem, but put code
1086 pause("fix95", tvtohz(&tv));
1090 t2p->t2_maxpcount = 5 * 2;
1091 t2p->t2_maxdcount = vcp->vc_txmax;
1092 error = smb_t2_request(t2p);
1095 mdp = &t2p->t2_rparam;
1096 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1097 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1099 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1101 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1104 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1107 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1108 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1110 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1112 if (ctx->f_ecnt == 0) {
1113 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1116 ctx->f_rnameofs = tw;
1117 mdp = &t2p->t2_rdata;
1118 if (mdp->md_top == NULL) {
1119 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1122 if (mdp->md_top->m_len == 0) {
1123 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1131 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1133 struct smb_rq rq, *rqp = &rq;
1134 struct mbchain *mbp;
1137 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1140 smb_rq_getrequest(rqp, &mbp);
1142 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1146 error = smb_rq_simple(rqp);
1152 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1153 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1155 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1156 if (ctx->f_name == NULL)
1158 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1159 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1160 ctx->f_attrmask = attr;
1161 ctx->f_wildcard = wildcard;
1162 ctx->f_wclen = wclen;
1167 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1169 struct mdchain *mbp;
1170 struct smb_t2rq *t2p;
1173 u_int16_t date, time, wattr;
1174 u_int32_t size, next, dattr;
1176 int error, svtz, cnt, fxsz, nmlen, recsz;
1178 if (ctx->f_ecnt == 0) {
1179 if (ctx->f_flags & SMBFS_RDD_EOF)
1181 ctx->f_left = ctx->f_limit = limit;
1182 error = smbfs_smb_trans2find2(ctx);
1187 mbp = &t2p->t2_rdata;
1188 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1189 switch (ctx->f_infolevel) {
1190 case SMB_INFO_STANDARD:
1193 md_get_uint16le(mbp, &date);
1194 md_get_uint16le(mbp, &time); /* creation time */
1195 md_get_uint16le(mbp, &date);
1196 md_get_uint16le(mbp, &time); /* access time */
1197 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1198 md_get_uint16le(mbp, &date);
1199 md_get_uint16le(mbp, &time); /* access time */
1200 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1201 md_get_uint32le(mbp, &size);
1202 ctx->f_attr.fa_size = size;
1203 md_get_uint32(mbp, NULL); /* allocation size */
1204 md_get_uint16le(mbp, &wattr);
1205 ctx->f_attr.fa_attr = wattr;
1206 md_get_uint8(mbp, &tb);
1209 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1211 case SMB_FIND_FILE_DIRECTORY_INFO:
1212 md_get_uint32le(mbp, &next);
1213 md_get_uint32(mbp, NULL); /* file index */
1214 md_get_int64(mbp, NULL); /* creation time */
1215 md_get_int64le(mbp, &lint);
1216 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1217 md_get_int64le(mbp, &lint);
1218 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1219 md_get_int64le(mbp, &lint);
1220 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1221 md_get_int64le(mbp, &lint); /* file size */
1222 ctx->f_attr.fa_size = lint;
1223 md_get_int64(mbp, NULL); /* real size (should use) */
1224 md_get_uint32le(mbp, &dattr); /* EA */
1225 ctx->f_attr.fa_attr = dattr;
1226 md_get_uint32le(mbp, &size); /* name len */
1228 recsz = next ? next : fxsz + size;
1231 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1234 nmlen = min(size, SMB_MAXFNAMELEN);
1236 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1240 cnt = next - nmlen - fxsz;
1242 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1244 SMBERROR("out of sync\n");
1248 if (nmlen && cp[nmlen - 1] == 0)
1253 next = ctx->f_eofs + recsz;
1254 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1255 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1257 * Server needs a resume filename.
1259 if (ctx->f_rnamelen <= nmlen) {
1261 free(ctx->f_rname, M_SMBFSDATA);
1262 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1263 ctx->f_rnamelen = nmlen;
1265 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1266 ctx->f_rname[nmlen] = 0;
1267 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1269 ctx->f_nmlen = nmlen;
1277 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1280 free(ctx->f_name, M_SMBFSDATA);
1282 smb_t2_done(ctx->f_t2);
1283 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1284 smbfs_smb_findclose2(ctx);
1289 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1290 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1292 struct smbfs_fctx *ctx;
1295 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1298 bzero(ctx, sizeof(*ctx));
1299 ctx->f_ssp = dnp->n_mount->sm_share;
1301 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1302 ctx->f_scred = scred;
1303 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1304 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1305 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1306 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1308 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1310 smbfs_findclose(ctx, scred);
1317 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1324 limit *= 4; /* imperical */
1325 ctx->f_scred = scred;
1327 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1328 error = smbfs_findnextLM1(ctx, limit);
1330 error = smbfs_findnextLM2(ctx, limit);
1333 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1334 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1335 ctx->f_name[1] == '.'))
1339 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1340 ctx->f_dnp->n_mount->sm_caseopt);
1341 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1346 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1348 ctx->f_scred = scred;
1349 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1350 smbfs_findcloseLM1(ctx);
1352 smbfs_findcloseLM2(ctx);
1354 free(ctx->f_rname, M_SMBFSDATA);
1355 free(ctx, M_SMBFSDATA);
1360 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1361 struct smbfattr *fap, struct smb_cred *scred)
1363 struct smbfs_fctx *ctx;
1366 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1367 bzero(fap, sizeof(*fap));
1368 fap->fa_attr = SMB_FA_DIR;
1372 if (nmlen == 1 && name[0] == '.') {
1373 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1375 } else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
1376 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1378 printf("%s: knows NOTHING about '..'\n", __func__);
1381 error = smbfs_findopen(dnp, name, nmlen,
1382 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1385 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1386 error = smbfs_findnext(ctx, 1, scred);
1390 fap->fa_ino = dnp->n_ino;
1392 smbfs_findclose(ctx, scred);