2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
36 #include <sys/vnode.h>
38 #include <sys/mount.h>
39 #include <sys/endian.h>
45 #include <netsmb/smb.h>
46 #include <netsmb/smb_subr.h>
47 #include <netsmb/smb_rq.h>
48 #include <netsmb/smb_conn.h>
50 #include <fs/smbfs/smbfs.h>
51 #include <fs/smbfs/smbfs_node.h>
52 #include <fs/smbfs/smbfs_subr.h>
55 * Lack of inode numbers leads us to the problem of generating them.
56 * Partially this problem can be solved by having a dir/file cache
57 * with inode numbers generated from the incremented by one counter.
58 * However this way will require too much kernel memory, gives all
59 * sorts of locking and consistency problems, not to mentinon counter overflows.
60 * So, I'm decided to use a hash function to generate pseudo random (and unique)
64 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
73 MD5Update(&md5, name, nmlen);
74 MD5Final((u_char *)state, &md5);
75 for (i = 0, ino = 0; i < 4; i++)
77 return dnp->n_ino + ino;
81 ino = dnp->n_ino + smbfs_hash(name, nmlen);
88 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
89 struct smb_cred *scred)
91 struct smb_share *ssp = np->n_mount->sm_share;
97 if (op == SMB_LOCK_SHARED)
98 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
100 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred, &rqp);
103 smb_rq_getrequest(rqp, &mbp);
105 mb_put_uint8(mbp, 0xff); /* secondary command */
106 mb_put_uint8(mbp, 0); /* MBZ */
107 mb_put_uint16le(mbp, 0);
108 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
109 mb_put_uint8(mbp, ltype); /* locktype */
110 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
111 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
112 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
113 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
116 mb_put_uint16le(mbp, pid);
117 mb_put_uint32le(mbp, start);
118 mb_put_uint32le(mbp, end - start);
120 error = smb_rq_simple(rqp);
126 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
127 off_t start, off_t end, struct smb_cred *scred)
129 struct smb_share *ssp = np->n_mount->sm_share;
131 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
133 * TODO: use LOCK_BYTE_RANGE here.
137 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
141 smbfs_query_info_fs(struct smb_share *ssp, struct statfs *sbp,
142 struct smb_cred *scred)
144 struct smb_t2rq *t2p;
148 int64_t units, funits;
151 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
155 mbp = &t2p->t2_tparam;
157 mb_put_uint16le(mbp, SMB_QUERY_FS_SIZE_INFO);
158 t2p->t2_maxpcount = 2;
159 t2p->t2_maxdcount = sizeof(int64_t) * 2 + sizeof(uint32_t) * 2;
160 error = smb_t2_request(t2p);
165 mdp = &t2p->t2_rdata;
166 md_get_int64le(mdp, &units);
167 md_get_int64le(mdp, &funits);
168 md_get_uint32le(mdp, &bpu);
169 md_get_uint32le(mdp, &bsize);
170 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
171 sbp->f_blocks= (uint64_t)units; /* total data blocks in filesystem */
172 sbp->f_bfree = (uint64_t)funits;/* free blocks in fs */
173 sbp->f_bavail= (uint64_t)funits;/* free blocks avail to non-superuser */
174 sbp->f_files = 0xffff; /* total file nodes in filesystem */
175 sbp->f_ffree = 0xffff; /* free file nodes in fs */
181 smbfs_query_info_alloc(struct smb_share *ssp, struct statfs *sbp,
182 struct smb_cred *scred)
184 struct smb_t2rq *t2p;
188 u_int32_t units, bpu, funits;
191 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
195 mbp = &t2p->t2_tparam;
197 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
198 t2p->t2_maxpcount = 4;
199 t2p->t2_maxdcount = 4 * 4 + 2;
200 error = smb_t2_request(t2p);
205 mdp = &t2p->t2_rdata;
206 md_get_uint32(mdp, NULL); /* fs id */
207 md_get_uint32le(mdp, &bpu);
208 md_get_uint32le(mdp, &units);
209 md_get_uint32le(mdp, &funits);
210 md_get_uint16le(mdp, &bsize);
211 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
212 sbp->f_blocks= units; /* total data blocks in filesystem */
213 sbp->f_bfree = funits; /* free blocks in fs */
214 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
215 sbp->f_files = 0xffff; /* total file nodes in filesystem */
216 sbp->f_ffree = 0xffff; /* free file nodes in fs */
222 smbfs_query_info_disk(struct smb_share *ssp, struct statfs *sbp,
223 struct smb_cred *scred)
227 u_int16_t units, bpu, bsize, funits;
230 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK,
238 error = smb_rq_simple(rqp);
243 smb_rq_getreply(rqp, &mdp);
244 md_get_uint16le(mdp, &units);
245 md_get_uint16le(mdp, &bpu);
246 md_get_uint16le(mdp, &bsize);
247 md_get_uint16le(mdp, &funits);
248 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
249 sbp->f_blocks= units; /* total data blocks in filesystem */
250 sbp->f_bfree = funits; /* free blocks in fs */
251 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
252 sbp->f_files = 0xffff; /* total file nodes in filesystem */
253 sbp->f_ffree = 0xffff; /* free file nodes in fs */
259 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
260 struct smb_cred *scred)
263 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) {
264 if (smbfs_query_info_fs(ssp, sbp, scred) == 0)
266 if (smbfs_query_info_alloc(ssp, sbp, scred) == 0)
269 return (smbfs_query_info_disk(ssp, sbp, scred));
273 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
275 struct smb_t2rq *t2p;
276 struct smb_share *ssp = np->n_mount->sm_share;
280 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
284 mbp = &t2p->t2_tparam;
286 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
287 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
288 mb_put_uint32le(mbp, 0);
289 mbp = &t2p->t2_tdata;
291 mb_put_int64le(mbp, newsize);
292 mb_put_uint32le(mbp, 0); /* padding */
293 mb_put_uint16le(mbp, 0);
294 t2p->t2_maxpcount = 2;
295 t2p->t2_maxdcount = 0;
296 error = smb_t2_request(t2p);
302 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
304 struct smb_share *ssp = np->n_mount->sm_share;
309 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
310 SMBTOV(np)->v_type != VREG)
311 return 0; /* not a regular open file */
312 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_FLUSH, scred, &rqp);
315 smb_rq_getrequest(rqp, &mbp);
317 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
321 error = smb_rq_simple(rqp);
324 np->n_flag &= ~NFLUSHWIRE;
329 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
331 if (np->n_flag & NFLUSHWIRE)
332 return (smb_smb_flush(np, scred));
337 smbfs_smb_setfsize(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
339 struct smb_share *ssp = np->n_mount->sm_share;
344 if (!smbfs_smb_seteof(np, newsize, scred)) {
345 np->n_flag |= NFLUSHWIRE;
348 /* XXX: We should use SMB_COM_WRITE_ANDX to support large offsets */
349 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
352 smb_rq_getrequest(rqp, &mbp);
354 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
355 mb_put_uint16le(mbp, 0);
356 mb_put_uint32le(mbp, (uint32_t)newsize);
357 mb_put_uint16le(mbp, 0);
360 mb_put_uint8(mbp, SMB_DT_DATA);
361 mb_put_uint16le(mbp, 0);
363 error = smb_rq_simple(rqp);
369 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
370 struct smbfattr *fap, struct smb_cred *scred)
373 struct smb_share *ssp = np->n_mount->sm_share;
381 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred,
385 smb_rq_getrequest(rqp, &mbp);
389 mb_put_uint8(mbp, SMB_DT_ASCII);
391 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
395 error = smb_rq_simple(rqp);
398 smb_rq_getreply(rqp, &mdp);
399 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
403 md_get_uint16le(mdp, &wattr);
404 fap->fa_attr = wattr;
406 * Be careful using the time returned here, as
407 * with FAT on NT4SP6, at least, the time returned is low
408 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
409 * over about every seven minutes!
411 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
412 if (lint) /* avoid bogus zero returns */
413 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
415 md_get_uint32le(mdp, &lint);
423 * Set DOS file attributes. mtime should be NULL for dialects above lm10
426 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
427 struct smb_cred *scred)
430 struct smb_share *ssp = np->n_mount->sm_share;
435 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred,
439 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
440 smb_rq_getrequest(rqp, &mbp);
442 mb_put_uint16le(mbp, attr);
444 smb_time_local2server(mtime, svtz, &time);
447 mb_put_uint32le(mbp, time); /* mtime */
448 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
451 mb_put_uint8(mbp, SMB_DT_ASCII);
453 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
456 mb_put_uint8(mbp, SMB_DT_ASCII);
457 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
459 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
461 mb_put_uint8(mbp, 0);
463 error = smb_rq_simple(rqp);
465 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
474 * Note, win95 doesn't support this call.
477 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
478 struct timespec *atime, int attr, struct smb_cred *scred)
480 struct smb_t2rq *t2p;
481 struct smb_share *ssp = np->n_mount->sm_share;
482 struct smb_vc *vcp = SSTOVC(ssp);
484 u_int16_t date, time;
487 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
491 mbp = &t2p->t2_tparam;
493 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
494 mb_put_uint32le(mbp, 0); /* MBZ */
495 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
496 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
501 tzoff = vcp->vc_sopt.sv_tz;
502 mbp = &t2p->t2_tdata;
504 mb_put_uint32le(mbp, 0); /* creation time */
506 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
509 mb_put_uint16le(mbp, date);
510 mb_put_uint16le(mbp, time);
512 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
515 mb_put_uint16le(mbp, date);
516 mb_put_uint16le(mbp, time);
517 mb_put_uint32le(mbp, 0); /* file size */
518 mb_put_uint32le(mbp, 0); /* allocation unit size */
519 mb_put_uint16le(mbp, attr); /* DOS attr */
520 mb_put_uint32le(mbp, 0); /* EA size */
521 t2p->t2_maxpcount = 5 * 2;
522 t2p->t2_maxdcount = vcp->vc_txmax;
523 error = smb_t2_request(t2p);
529 * NT level. Specially for win9x
532 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
533 struct timespec *atime, struct smb_cred *scred)
535 struct smb_t2rq *t2p;
536 struct smb_share *ssp = np->n_mount->sm_share;
537 struct smb_vc *vcp = SSTOVC(ssp);
542 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
546 mbp = &t2p->t2_tparam;
548 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
549 mb_put_uint32le(mbp, 0); /* MBZ */
550 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
551 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
556 tzoff = vcp->vc_sopt.sv_tz;
557 mbp = &t2p->t2_tdata;
559 mb_put_int64le(mbp, 0); /* creation time */
561 smb_time_local2NT(atime, tzoff, &tm);
564 mb_put_int64le(mbp, tm);
566 smb_time_local2NT(mtime, tzoff, &tm);
569 mb_put_int64le(mbp, tm);
570 mb_put_int64le(mbp, tm); /* change time */
571 mb_put_uint32le(mbp, attr); /* attr */
572 t2p->t2_maxpcount = 24;
573 t2p->t2_maxdcount = 56;
574 error = smb_t2_request(t2p);
580 * Set file atime and mtime. Doesn't supported by core dialect.
583 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
584 struct timespec *atime, struct smb_cred *scred)
587 struct smb_share *ssp = np->n_mount->sm_share;
589 u_int16_t date, time;
592 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred,
596 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
597 smb_rq_getrequest(rqp, &mbp);
599 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
600 mb_put_uint32le(mbp, 0); /* creation time */
603 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
606 mb_put_uint16le(mbp, date);
607 mb_put_uint16le(mbp, time);
609 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
612 mb_put_uint16le(mbp, date);
613 mb_put_uint16le(mbp, time);
617 error = smb_rq_simple(rqp);
618 SMBSDEBUG("%d\n", error);
624 * Set DOS file attributes.
625 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
628 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
629 struct timespec *atime, struct smb_cred *scred)
631 struct smb_t2rq *t2p;
632 struct smb_share *ssp = np->n_mount->sm_share;
637 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
641 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
642 mbp = &t2p->t2_tparam;
644 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
645 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
646 mb_put_uint32le(mbp, 0);
647 mbp = &t2p->t2_tdata;
649 mb_put_int64le(mbp, 0); /* creation time */
651 smb_time_local2NT(atime, svtz, &tm);
654 mb_put_int64le(mbp, tm);
656 smb_time_local2NT(mtime, svtz, &tm);
659 mb_put_int64le(mbp, tm);
660 mb_put_int64le(mbp, tm); /* change time */
661 mb_put_uint16le(mbp, attr);
662 mb_put_uint32le(mbp, 0); /* padding */
663 mb_put_uint16le(mbp, 0);
664 t2p->t2_maxpcount = 2;
665 t2p->t2_maxdcount = 0;
666 error = smb_t2_request(t2p);
672 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
675 struct smb_share *ssp = np->n_mount->sm_share;
679 u_int16_t fid, wattr, grantedmode;
682 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_OPEN, scred, &rqp);
685 smb_rq_getrequest(rqp, &mbp);
687 mb_put_uint16le(mbp, accmode);
688 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
691 mb_put_uint8(mbp, SMB_DT_ASCII);
693 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
697 error = smb_rq_simple(rqp);
700 smb_rq_getreply(rqp, &mdp);
701 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
705 md_get_uint16(mdp, &fid);
706 md_get_uint16le(mdp, &wattr);
707 md_get_uint32(mdp, NULL); /* mtime */
708 md_get_uint32(mdp, NULL); /* fsize */
709 md_get_uint16le(mdp, &grantedmode);
711 * TODO: refresh attributes from this reply
718 np->n_rwstate = grantedmode;
723 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
724 struct smb_cred *scred)
731 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CLOSE, scred, &rqp);
734 smb_rq_getrequest(rqp, &mbp);
736 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
738 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
741 mb_put_uint32le(mbp, time);
745 error = smb_rq_simple(rqp);
751 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
752 struct smb_cred *scred)
755 struct smb_share *ssp = dnp->n_mount->sm_share;
758 struct timespec ctime;
764 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE, scred, &rqp);
767 smb_rq_getrequest(rqp, &mbp);
769 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
771 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
772 mb_put_uint32le(mbp, tm);
775 mb_put_uint8(mbp, SMB_DT_ASCII);
776 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
779 error = smb_rq_simple(rqp);
781 smb_rq_getreply(rqp, &mdp);
782 md_get_uint8(mdp, &wc);
784 md_get_uint16(mdp, &fid);
792 smbfs_smb_close(ssp, fid, &ctime, scred);
797 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
800 struct smb_share *ssp = np->n_mount->sm_share;
804 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE, scred, &rqp);
807 smb_rq_getrequest(rqp, &mbp);
809 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
812 mb_put_uint8(mbp, SMB_DT_ASCII);
813 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
816 error = smb_rq_simple(rqp);
823 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
824 const char *tname, int tnmlen, struct smb_cred *scred)
827 struct smb_share *ssp = src->n_mount->sm_share;
831 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_RENAME, scred, &rqp);
834 smb_rq_getrequest(rqp, &mbp);
836 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
839 mb_put_uint8(mbp, SMB_DT_ASCII);
841 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
844 mb_put_uint8(mbp, SMB_DT_ASCII);
845 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
849 error = smb_rq_simple(rqp);
856 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
857 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
860 struct smb_share *ssp = src->n_mount->sm_share;
864 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_MOVE, scred, &rqp);
867 smb_rq_getrequest(rqp, &mbp);
869 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
870 mb_put_uint16le(mbp, 0x20); /* delete target file */
871 mb_put_uint16le(mbp, flags);
874 mb_put_uint8(mbp, SMB_DT_ASCII);
876 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
879 mb_put_uint8(mbp, SMB_DT_ASCII);
880 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
884 error = smb_rq_simple(rqp);
891 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
892 struct smb_cred *scred)
895 struct smb_share *ssp = dnp->n_mount->sm_share;
899 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred,
903 smb_rq_getrequest(rqp, &mbp);
907 mb_put_uint8(mbp, SMB_DT_ASCII);
908 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
911 error = smb_rq_simple(rqp);
918 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
921 struct smb_share *ssp = np->n_mount->sm_share;
925 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred,
929 smb_rq_getrequest(rqp, &mbp);
933 mb_put_uint8(mbp, SMB_DT_ASCII);
934 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
937 error = smb_rq_simple(rqp);
944 smbfs_smb_search(struct smbfs_fctx *ctx)
946 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
951 u_int16_t ec, dlen, bc;
952 int maxent, error, iseof = 0;
954 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
956 smb_rq_done(ctx->f_rq);
959 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
963 smb_rq_getrequest(rqp, &mbp);
965 mb_put_uint16le(mbp, maxent); /* max entries to return */
966 mb_put_uint16le(mbp, ctx->f_attrmask);
969 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
970 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
971 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
974 mb_put_uint8(mbp, SMB_DT_VARIABLE);
975 mb_put_uint16le(mbp, 0); /* context length */
976 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
978 if (SMB_UNICODE_STRINGS(vcp)) {
980 mb_put_uint8(mbp, 0);
982 mb_put_uint8(mbp, 0); /* file name length */
983 mb_put_uint8(mbp, SMB_DT_VARIABLE);
984 mb_put_uint16le(mbp, SMB_SKEYLEN);
985 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
988 error = smb_rq_simple(rqp);
990 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
993 ctx->f_flags |= SMBFS_RDD_EOF;
997 smb_rq_getreply(rqp, &mdp);
998 md_get_uint8(mdp, &wc);
1000 return iseof ? ENOENT : EBADRPC;
1001 md_get_uint16le(mdp, &ec);
1005 md_get_uint16le(mdp, &bc);
1009 md_get_uint8(mdp, &bt);
1010 if (bt != SMB_DT_VARIABLE)
1012 md_get_uint16le(mdp, &dlen);
1013 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1019 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1020 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1022 ctx->f_attrmask = attr;
1024 if (wclen == 1 && wildcard[0] == '*') {
1025 ctx->f_wildcard = "*.*";
1028 ctx->f_wildcard = wildcard;
1029 ctx->f_wclen = wclen;
1032 ctx->f_wildcard = NULL;
1035 ctx->f_name = ctx->f_fname;
1040 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1042 struct mdchain *mbp;
1046 u_int16_t date, time;
1050 if (ctx->f_ecnt == 0) {
1051 if (ctx->f_flags & SMBFS_RDD_EOF)
1053 ctx->f_left = ctx->f_limit = limit;
1054 error = smbfs_smb_search(ctx);
1059 smb_rq_getreply(rqp, &mbp);
1060 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1061 md_get_uint8(mbp, &battr);
1062 md_get_uint16le(mbp, &time);
1063 md_get_uint16le(mbp, &date);
1064 md_get_uint32le(mbp, &size);
1066 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1067 cp[sizeof(ctx->f_fname) - 1] = 0;
1068 cp += strlen(cp) - 1;
1069 while (*cp == ' ' && cp >= ctx->f_name)
1071 ctx->f_attr.fa_attr = battr;
1072 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1073 &ctx->f_attr.fa_mtime);
1074 ctx->f_attr.fa_size = size;
1075 ctx->f_nmlen = strlen(ctx->f_name);
1082 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1085 smb_rq_done(ctx->f_rq);
1090 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1093 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1095 struct smb_t2rq *t2p;
1096 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1097 struct mbchain *mbp;
1098 struct mdchain *mdp;
1099 u_int16_t tw, flags;
1103 smb_t2_done(ctx->f_t2);
1106 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1107 flags = 8 | 2; /* <resume> | <close if EOS> */
1108 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1109 flags |= 1; /* close search after this request */
1110 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1112 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1113 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1114 ctx->f_scred, &t2p);
1118 mbp = &t2p->t2_tparam;
1120 mb_put_uint16le(mbp, ctx->f_attrmask);
1121 mb_put_uint16le(mbp, ctx->f_limit);
1122 mb_put_uint16le(mbp, flags);
1123 mb_put_uint16le(mbp, ctx->f_infolevel);
1124 mb_put_uint32le(mbp, 0);
1125 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1129 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1130 ctx->f_scred, &t2p);
1134 mbp = &t2p->t2_tparam;
1136 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1137 mb_put_uint16le(mbp, ctx->f_limit);
1138 mb_put_uint16le(mbp, ctx->f_infolevel);
1139 mb_put_uint32le(mbp, 0); /* resume key */
1140 mb_put_uint16le(mbp, flags);
1142 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1144 mb_put_uint8(mbp, 0); /* resume file name */
1148 tv.tv_usec = 200 * 1000; /* 200ms */
1149 if (vcp->vc_flags & SMBC_WIN95) {
1151 * some implementations suggests to sleep here
1152 * for 200ms, due to the bug in the Win95.
1153 * I've didn't notice any problem, but put code
1156 pause("fix95", tvtohz(&tv));
1160 t2p->t2_maxpcount = 5 * 2;
1161 t2p->t2_maxdcount = vcp->vc_txmax;
1162 error = smb_t2_request(t2p);
1165 mdp = &t2p->t2_rparam;
1166 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1167 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1169 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1171 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1174 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1177 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1178 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1180 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1182 if (ctx->f_ecnt == 0) {
1183 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1186 ctx->f_rnameofs = tw;
1187 mdp = &t2p->t2_rdata;
1188 if (mdp->md_top == NULL) {
1189 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1192 if (mdp->md_top->m_len == 0) {
1193 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1201 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1204 struct mbchain *mbp;
1207 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2,
1208 ctx->f_scred, &rqp);
1211 smb_rq_getrequest(rqp, &mbp);
1213 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1217 error = smb_rq_simple(rqp);
1223 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1224 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1226 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1227 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1229 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1230 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1231 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1232 ctx->f_attrmask = attr;
1233 ctx->f_wildcard = wildcard;
1234 ctx->f_wclen = wclen;
1239 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1241 struct mdchain *mbp;
1242 struct smb_t2rq *t2p;
1245 u_int16_t date, time, wattr;
1246 u_int32_t size, next, dattr;
1248 int error, svtz, cnt, fxsz, nmlen, recsz;
1250 if (ctx->f_ecnt == 0) {
1251 if (ctx->f_flags & SMBFS_RDD_EOF)
1253 ctx->f_left = ctx->f_limit = limit;
1254 error = smbfs_smb_trans2find2(ctx);
1259 mbp = &t2p->t2_rdata;
1260 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1261 switch (ctx->f_infolevel) {
1262 case SMB_INFO_STANDARD:
1265 md_get_uint16le(mbp, &date);
1266 md_get_uint16le(mbp, &time); /* creation time */
1267 md_get_uint16le(mbp, &date);
1268 md_get_uint16le(mbp, &time); /* access time */
1269 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1270 md_get_uint16le(mbp, &date);
1271 md_get_uint16le(mbp, &time); /* access time */
1272 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1273 md_get_uint32le(mbp, &size);
1274 ctx->f_attr.fa_size = size;
1275 md_get_uint32(mbp, NULL); /* allocation size */
1276 md_get_uint16le(mbp, &wattr);
1277 ctx->f_attr.fa_attr = wattr;
1278 md_get_uint8(mbp, &tb);
1281 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1283 case SMB_FIND_FILE_DIRECTORY_INFO:
1284 md_get_uint32le(mbp, &next);
1285 md_get_uint32(mbp, NULL); /* file index */
1286 md_get_int64(mbp, NULL); /* creation time */
1287 md_get_int64le(mbp, &lint);
1288 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1289 md_get_int64le(mbp, &lint);
1290 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1291 md_get_int64le(mbp, &lint);
1292 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1293 md_get_int64le(mbp, &lint); /* file size */
1294 ctx->f_attr.fa_size = lint;
1295 md_get_int64(mbp, NULL); /* real size (should use) */
1296 md_get_uint32le(mbp, &dattr); /* EA */
1297 ctx->f_attr.fa_attr = dattr;
1298 md_get_uint32le(mbp, &size); /* name len */
1300 recsz = next ? next : fxsz + size;
1303 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1306 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1307 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1309 nmlen = min(size, SMB_MAXFNAMELEN);
1311 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1315 cnt = next - nmlen - fxsz;
1317 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1319 SMBERROR("out of sync\n");
1323 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1324 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1327 if (nmlen && cp[nmlen - 1] == 0)
1332 next = ctx->f_eofs + recsz;
1333 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1334 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1336 * Server needs a resume filename.
1338 if (ctx->f_rnamelen <= nmlen) {
1340 free(ctx->f_rname, M_SMBFSDATA);
1341 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1342 ctx->f_rnamelen = nmlen;
1344 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1345 ctx->f_rname[nmlen] = 0;
1346 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1348 ctx->f_nmlen = nmlen;
1356 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1359 free(ctx->f_name, M_SMBFSDATA);
1361 smb_t2_done(ctx->f_t2);
1362 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1363 smbfs_smb_findclose2(ctx);
1368 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1369 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1371 struct smbfs_fctx *ctx;
1374 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK | M_ZERO);
1375 ctx->f_ssp = dnp->n_mount->sm_share;
1377 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1378 ctx->f_scred = scred;
1379 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1380 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1381 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1382 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1384 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1386 smbfs_findclose(ctx, scred);
1393 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1400 limit *= 4; /* imperical */
1401 ctx->f_scred = scred;
1403 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1404 error = smbfs_findnextLM1(ctx, limit);
1406 error = smbfs_findnextLM2(ctx, limit);
1409 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1410 if ((ctx->f_nmlen == 2 &&
1411 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1412 (ctx->f_nmlen == 4 &&
1413 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1416 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1417 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1418 ctx->f_name[1] == '.'))
1422 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1423 ctx->f_dnp->n_mount->sm_caseopt);
1424 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1429 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1431 ctx->f_scred = scred;
1432 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1433 smbfs_findcloseLM1(ctx);
1435 smbfs_findcloseLM2(ctx);
1437 free(ctx->f_rname, M_SMBFSDATA);
1438 free(ctx, M_SMBFSDATA);
1443 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1444 struct smbfattr *fap, struct smb_cred *scred)
1446 struct smbfs_fctx *ctx;
1449 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1450 bzero(fap, sizeof(*fap));
1451 fap->fa_attr = SMB_FA_DIR;
1455 MPASS(!(nmlen == 2 && name[0] == '.' && name[1] == '.'));
1456 MPASS(!(nmlen == 1 && name[0] == '.'));
1457 ASSERT_VOP_ELOCKED(dnp->n_vnode, "smbfs_smb_lookup");
1458 error = smbfs_findopen(dnp, name, nmlen,
1459 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1462 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1463 error = smbfs_findnext(ctx, 1, scred);
1467 fap->fa_ino = dnp->n_ino;
1469 smbfs_findclose(ctx, scred);