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 */
182 smbfs_query_info_alloc(struct smb_share *ssp, struct statfs *sbp,
183 struct smb_cred *scred)
185 struct smb_t2rq *t2p;
189 u_int32_t units, bpu, funits;
192 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
196 mbp = &t2p->t2_tparam;
198 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
199 t2p->t2_maxpcount = 4;
200 t2p->t2_maxdcount = 4 * 4 + 2;
201 error = smb_t2_request(t2p);
206 mdp = &t2p->t2_rdata;
207 md_get_uint32(mdp, NULL); /* fs id */
208 md_get_uint32le(mdp, &bpu);
209 md_get_uint32le(mdp, &units);
210 md_get_uint32le(mdp, &funits);
211 md_get_uint16le(mdp, &bsize);
212 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
213 sbp->f_blocks= units; /* total data blocks in filesystem */
214 sbp->f_bfree = funits; /* free blocks in fs */
215 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
216 sbp->f_files = 0xffff; /* total file nodes in filesystem */
217 sbp->f_ffree = 0xffff; /* free file nodes in fs */
223 smbfs_query_info_disk(struct smb_share *ssp, struct statfs *sbp,
224 struct smb_cred *scred)
228 u_int16_t units, bpu, bsize, funits;
231 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK,
239 error = smb_rq_simple(rqp);
244 smb_rq_getreply(rqp, &mdp);
245 md_get_uint16le(mdp, &units);
246 md_get_uint16le(mdp, &bpu);
247 md_get_uint16le(mdp, &bsize);
248 md_get_uint16le(mdp, &funits);
249 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
250 sbp->f_blocks= units; /* total data blocks in filesystem */
251 sbp->f_bfree = funits; /* free blocks in fs */
252 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
253 sbp->f_files = 0xffff; /* total file nodes in filesystem */
254 sbp->f_ffree = 0xffff; /* free file nodes in fs */
260 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
261 struct smb_cred *scred)
264 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) {
265 if (smbfs_query_info_fs(ssp, sbp, scred) == 0)
267 if (smbfs_query_info_alloc(ssp, sbp, scred) == 0)
270 return (smbfs_query_info_disk(ssp, sbp, scred));
274 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
276 struct smb_t2rq *t2p;
277 struct smb_share *ssp = np->n_mount->sm_share;
281 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
285 mbp = &t2p->t2_tparam;
287 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
288 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
289 mb_put_uint32le(mbp, 0);
290 mbp = &t2p->t2_tdata;
292 mb_put_int64le(mbp, newsize);
293 mb_put_uint32le(mbp, 0); /* padding */
294 mb_put_uint16le(mbp, 0);
295 t2p->t2_maxpcount = 2;
296 t2p->t2_maxdcount = 0;
297 error = smb_t2_request(t2p);
303 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
305 struct smb_share *ssp = np->n_mount->sm_share;
310 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
311 SMBTOV(np)->v_type != VREG)
312 return 0; /* not a regular open file */
313 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_FLUSH, scred, &rqp);
316 smb_rq_getrequest(rqp, &mbp);
318 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
322 error = smb_rq_simple(rqp);
325 np->n_flag &= ~NFLUSHWIRE;
330 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
332 if (np->n_flag & NFLUSHWIRE)
333 return (smb_smb_flush(np, scred));
338 smbfs_smb_setfsize(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
340 struct smb_share *ssp = np->n_mount->sm_share;
345 if (!smbfs_smb_seteof(np, newsize, scred)) {
346 np->n_flag |= NFLUSHWIRE;
349 /* XXX: We should use SMB_COM_WRITE_ANDX to support large offsets */
350 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
353 smb_rq_getrequest(rqp, &mbp);
355 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
356 mb_put_uint16le(mbp, 0);
357 mb_put_uint32le(mbp, (uint32_t)newsize);
358 mb_put_uint16le(mbp, 0);
361 mb_put_uint8(mbp, SMB_DT_DATA);
362 mb_put_uint16le(mbp, 0);
364 error = smb_rq_simple(rqp);
370 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
371 struct smbfattr *fap, struct smb_cred *scred)
374 struct smb_share *ssp = np->n_mount->sm_share;
382 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred,
386 smb_rq_getrequest(rqp, &mbp);
390 mb_put_uint8(mbp, SMB_DT_ASCII);
392 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
396 error = smb_rq_simple(rqp);
399 smb_rq_getreply(rqp, &mdp);
400 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
404 md_get_uint16le(mdp, &wattr);
405 fap->fa_attr = wattr;
407 * Be careful using the time returned here, as
408 * with FAT on NT4SP6, at least, the time returned is low
409 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
410 * over about every seven minutes!
412 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
413 if (lint) /* avoid bogus zero returns */
414 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
416 md_get_uint32le(mdp, &lint);
424 * Set DOS file attributes. mtime should be NULL for dialects above lm10
427 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
428 struct smb_cred *scred)
431 struct smb_share *ssp = np->n_mount->sm_share;
436 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred,
440 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
441 smb_rq_getrequest(rqp, &mbp);
443 mb_put_uint16le(mbp, attr);
445 smb_time_local2server(mtime, svtz, &time);
448 mb_put_uint32le(mbp, time); /* mtime */
449 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
452 mb_put_uint8(mbp, SMB_DT_ASCII);
454 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
457 mb_put_uint8(mbp, SMB_DT_ASCII);
458 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
460 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
462 mb_put_uint8(mbp, 0);
464 error = smb_rq_simple(rqp);
466 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
475 * Note, win95 doesn't support this call.
478 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
479 struct timespec *atime, int attr, struct smb_cred *scred)
481 struct smb_t2rq *t2p;
482 struct smb_share *ssp = np->n_mount->sm_share;
483 struct smb_vc *vcp = SSTOVC(ssp);
485 u_int16_t date, time;
488 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
492 mbp = &t2p->t2_tparam;
494 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
495 mb_put_uint32le(mbp, 0); /* MBZ */
496 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
497 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
502 tzoff = vcp->vc_sopt.sv_tz;
503 mbp = &t2p->t2_tdata;
505 mb_put_uint32le(mbp, 0); /* creation time */
507 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
510 mb_put_uint16le(mbp, date);
511 mb_put_uint16le(mbp, time);
513 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
516 mb_put_uint16le(mbp, date);
517 mb_put_uint16le(mbp, time);
518 mb_put_uint32le(mbp, 0); /* file size */
519 mb_put_uint32le(mbp, 0); /* allocation unit size */
520 mb_put_uint16le(mbp, attr); /* DOS attr */
521 mb_put_uint32le(mbp, 0); /* EA size */
522 t2p->t2_maxpcount = 5 * 2;
523 t2p->t2_maxdcount = vcp->vc_txmax;
524 error = smb_t2_request(t2p);
530 * NT level. Specially for win9x
533 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
534 struct timespec *atime, struct smb_cred *scred)
536 struct smb_t2rq *t2p;
537 struct smb_share *ssp = np->n_mount->sm_share;
538 struct smb_vc *vcp = SSTOVC(ssp);
543 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
547 mbp = &t2p->t2_tparam;
549 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
550 mb_put_uint32le(mbp, 0); /* MBZ */
551 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
552 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
557 tzoff = vcp->vc_sopt.sv_tz;
558 mbp = &t2p->t2_tdata;
560 mb_put_int64le(mbp, 0); /* creation time */
562 smb_time_local2NT(atime, tzoff, &tm);
565 mb_put_int64le(mbp, tm);
567 smb_time_local2NT(mtime, tzoff, &tm);
570 mb_put_int64le(mbp, tm);
571 mb_put_int64le(mbp, tm); /* change time */
572 mb_put_uint32le(mbp, attr); /* attr */
573 t2p->t2_maxpcount = 24;
574 t2p->t2_maxdcount = 56;
575 error = smb_t2_request(t2p);
581 * Set file atime and mtime. Doesn't supported by core dialect.
584 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
585 struct timespec *atime, struct smb_cred *scred)
588 struct smb_share *ssp = np->n_mount->sm_share;
590 u_int16_t date, time;
593 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred,
597 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
598 smb_rq_getrequest(rqp, &mbp);
600 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
601 mb_put_uint32le(mbp, 0); /* creation time */
604 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
607 mb_put_uint16le(mbp, date);
608 mb_put_uint16le(mbp, time);
610 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
613 mb_put_uint16le(mbp, date);
614 mb_put_uint16le(mbp, time);
618 error = smb_rq_simple(rqp);
619 SMBSDEBUG("%d\n", error);
625 * Set DOS file attributes.
626 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
629 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
630 struct timespec *atime, struct smb_cred *scred)
632 struct smb_t2rq *t2p;
633 struct smb_share *ssp = np->n_mount->sm_share;
638 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
642 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
643 mbp = &t2p->t2_tparam;
645 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
646 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
647 mb_put_uint32le(mbp, 0);
648 mbp = &t2p->t2_tdata;
650 mb_put_int64le(mbp, 0); /* creation time */
652 smb_time_local2NT(atime, svtz, &tm);
655 mb_put_int64le(mbp, tm);
657 smb_time_local2NT(mtime, svtz, &tm);
660 mb_put_int64le(mbp, tm);
661 mb_put_int64le(mbp, tm); /* change time */
662 mb_put_uint16le(mbp, attr);
663 mb_put_uint32le(mbp, 0); /* padding */
664 mb_put_uint16le(mbp, 0);
665 t2p->t2_maxpcount = 2;
666 t2p->t2_maxdcount = 0;
667 error = smb_t2_request(t2p);
674 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
677 struct smb_share *ssp = np->n_mount->sm_share;
681 u_int16_t fid, wattr, grantedmode;
684 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_OPEN, scred, &rqp);
687 smb_rq_getrequest(rqp, &mbp);
689 mb_put_uint16le(mbp, accmode);
690 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
693 mb_put_uint8(mbp, SMB_DT_ASCII);
695 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
699 error = smb_rq_simple(rqp);
702 smb_rq_getreply(rqp, &mdp);
703 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
707 md_get_uint16(mdp, &fid);
708 md_get_uint16le(mdp, &wattr);
709 md_get_uint32(mdp, NULL); /* mtime */
710 md_get_uint32(mdp, NULL); /* fsize */
711 md_get_uint16le(mdp, &grantedmode);
713 * TODO: refresh attributes from this reply
720 np->n_rwstate = grantedmode;
726 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
727 struct smb_cred *scred)
734 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CLOSE, scred, &rqp);
737 smb_rq_getrequest(rqp, &mbp);
739 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
741 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
744 mb_put_uint32le(mbp, time);
748 error = smb_rq_simple(rqp);
754 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
755 struct smb_cred *scred)
758 struct smb_share *ssp = dnp->n_mount->sm_share;
761 struct timespec ctime;
767 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE, scred, &rqp);
770 smb_rq_getrequest(rqp, &mbp);
772 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
774 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
775 mb_put_uint32le(mbp, tm);
778 mb_put_uint8(mbp, SMB_DT_ASCII);
779 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
782 error = smb_rq_simple(rqp);
784 smb_rq_getreply(rqp, &mdp);
785 md_get_uint8(mdp, &wc);
787 md_get_uint16(mdp, &fid);
795 smbfs_smb_close(ssp, fid, &ctime, scred);
800 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
803 struct smb_share *ssp = np->n_mount->sm_share;
807 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE, scred, &rqp);
810 smb_rq_getrequest(rqp, &mbp);
812 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
815 mb_put_uint8(mbp, SMB_DT_ASCII);
816 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
819 error = smb_rq_simple(rqp);
826 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
827 const char *tname, int tnmlen, struct smb_cred *scred)
830 struct smb_share *ssp = src->n_mount->sm_share;
834 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_RENAME, scred, &rqp);
837 smb_rq_getrequest(rqp, &mbp);
839 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
842 mb_put_uint8(mbp, SMB_DT_ASCII);
844 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
847 mb_put_uint8(mbp, SMB_DT_ASCII);
848 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
852 error = smb_rq_simple(rqp);
859 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
860 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
863 struct smb_share *ssp = src->n_mount->sm_share;
867 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_MOVE, scred, &rqp);
870 smb_rq_getrequest(rqp, &mbp);
872 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
873 mb_put_uint16le(mbp, 0x20); /* delete target file */
874 mb_put_uint16le(mbp, flags);
877 mb_put_uint8(mbp, SMB_DT_ASCII);
879 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
882 mb_put_uint8(mbp, SMB_DT_ASCII);
883 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
887 error = smb_rq_simple(rqp);
894 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
895 struct smb_cred *scred)
898 struct smb_share *ssp = dnp->n_mount->sm_share;
902 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred,
906 smb_rq_getrequest(rqp, &mbp);
910 mb_put_uint8(mbp, SMB_DT_ASCII);
911 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
914 error = smb_rq_simple(rqp);
921 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
924 struct smb_share *ssp = np->n_mount->sm_share;
928 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred,
932 smb_rq_getrequest(rqp, &mbp);
936 mb_put_uint8(mbp, SMB_DT_ASCII);
937 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
940 error = smb_rq_simple(rqp);
947 smbfs_smb_search(struct smbfs_fctx *ctx)
949 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
954 u_int16_t ec, dlen, bc;
955 int maxent, error, iseof = 0;
957 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
959 smb_rq_done(ctx->f_rq);
962 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
966 smb_rq_getrequest(rqp, &mbp);
968 mb_put_uint16le(mbp, maxent); /* max entries to return */
969 mb_put_uint16le(mbp, ctx->f_attrmask);
972 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
973 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
974 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
977 mb_put_uint8(mbp, SMB_DT_VARIABLE);
978 mb_put_uint16le(mbp, 0); /* context length */
979 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
981 if (SMB_UNICODE_STRINGS(vcp)) {
983 mb_put_uint8(mbp, 0);
985 mb_put_uint8(mbp, 0); /* file name length */
986 mb_put_uint8(mbp, SMB_DT_VARIABLE);
987 mb_put_uint16le(mbp, SMB_SKEYLEN);
988 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
991 error = smb_rq_simple(rqp);
993 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
996 ctx->f_flags |= SMBFS_RDD_EOF;
1000 smb_rq_getreply(rqp, &mdp);
1001 md_get_uint8(mdp, &wc);
1003 return iseof ? ENOENT : EBADRPC;
1004 md_get_uint16le(mdp, &ec);
1008 md_get_uint16le(mdp, &bc);
1012 md_get_uint8(mdp, &bt);
1013 if (bt != SMB_DT_VARIABLE)
1015 md_get_uint16le(mdp, &dlen);
1016 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1022 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1023 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1025 ctx->f_attrmask = attr;
1027 if (wclen == 1 && wildcard[0] == '*') {
1028 ctx->f_wildcard = "*.*";
1031 ctx->f_wildcard = wildcard;
1032 ctx->f_wclen = wclen;
1035 ctx->f_wildcard = NULL;
1038 ctx->f_name = ctx->f_fname;
1043 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1045 struct mdchain *mbp;
1049 u_int16_t date, time;
1053 if (ctx->f_ecnt == 0) {
1054 if (ctx->f_flags & SMBFS_RDD_EOF)
1056 ctx->f_left = ctx->f_limit = limit;
1057 error = smbfs_smb_search(ctx);
1062 smb_rq_getreply(rqp, &mbp);
1063 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1064 md_get_uint8(mbp, &battr);
1065 md_get_uint16le(mbp, &time);
1066 md_get_uint16le(mbp, &date);
1067 md_get_uint32le(mbp, &size);
1069 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1070 cp[sizeof(ctx->f_fname) - 1] = 0;
1071 cp += strlen(cp) - 1;
1072 while (*cp == ' ' && cp >= ctx->f_name)
1074 ctx->f_attr.fa_attr = battr;
1075 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1076 &ctx->f_attr.fa_mtime);
1077 ctx->f_attr.fa_size = size;
1078 ctx->f_nmlen = strlen(ctx->f_name);
1085 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1088 smb_rq_done(ctx->f_rq);
1093 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1096 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1098 struct smb_t2rq *t2p;
1099 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1100 struct mbchain *mbp;
1101 struct mdchain *mdp;
1102 u_int16_t tw, flags;
1106 smb_t2_done(ctx->f_t2);
1109 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1110 flags = 8 | 2; /* <resume> | <close if EOS> */
1111 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1112 flags |= 1; /* close search after this request */
1113 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1115 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1116 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1117 ctx->f_scred, &t2p);
1121 mbp = &t2p->t2_tparam;
1123 mb_put_uint16le(mbp, ctx->f_attrmask);
1124 mb_put_uint16le(mbp, ctx->f_limit);
1125 mb_put_uint16le(mbp, flags);
1126 mb_put_uint16le(mbp, ctx->f_infolevel);
1127 mb_put_uint32le(mbp, 0);
1128 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1132 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1133 ctx->f_scred, &t2p);
1137 mbp = &t2p->t2_tparam;
1139 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1140 mb_put_uint16le(mbp, ctx->f_limit);
1141 mb_put_uint16le(mbp, ctx->f_infolevel);
1142 mb_put_uint32le(mbp, 0); /* resume key */
1143 mb_put_uint16le(mbp, flags);
1145 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1147 mb_put_uint8(mbp, 0); /* resume file name */
1151 tv.tv_usec = 200 * 1000; /* 200ms */
1152 if (vcp->vc_flags & SMBC_WIN95) {
1154 * some implementations suggests to sleep here
1155 * for 200ms, due to the bug in the Win95.
1156 * I've didn't notice any problem, but put code
1159 pause("fix95", tvtohz(&tv));
1163 t2p->t2_maxpcount = 5 * 2;
1164 t2p->t2_maxdcount = vcp->vc_txmax;
1165 error = smb_t2_request(t2p);
1168 mdp = &t2p->t2_rparam;
1169 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1170 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1172 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1174 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1177 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1180 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1181 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1183 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1185 if (ctx->f_ecnt == 0) {
1186 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1189 ctx->f_rnameofs = tw;
1190 mdp = &t2p->t2_rdata;
1191 if (mdp->md_top == NULL) {
1192 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1195 if (mdp->md_top->m_len == 0) {
1196 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1204 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1207 struct mbchain *mbp;
1210 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2,
1211 ctx->f_scred, &rqp);
1214 smb_rq_getrequest(rqp, &mbp);
1216 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1220 error = smb_rq_simple(rqp);
1226 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1227 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1229 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1230 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1232 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1233 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1234 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1235 ctx->f_attrmask = attr;
1236 ctx->f_wildcard = wildcard;
1237 ctx->f_wclen = wclen;
1242 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1244 struct mdchain *mbp;
1245 struct smb_t2rq *t2p;
1248 u_int16_t date, time, wattr;
1249 u_int32_t size, next, dattr;
1251 int error, svtz, cnt, fxsz, nmlen, recsz;
1253 if (ctx->f_ecnt == 0) {
1254 if (ctx->f_flags & SMBFS_RDD_EOF)
1256 ctx->f_left = ctx->f_limit = limit;
1257 error = smbfs_smb_trans2find2(ctx);
1262 mbp = &t2p->t2_rdata;
1263 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1264 switch (ctx->f_infolevel) {
1265 case SMB_INFO_STANDARD:
1268 md_get_uint16le(mbp, &date);
1269 md_get_uint16le(mbp, &time); /* creation time */
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_atime);
1273 md_get_uint16le(mbp, &date);
1274 md_get_uint16le(mbp, &time); /* access time */
1275 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1276 md_get_uint32le(mbp, &size);
1277 ctx->f_attr.fa_size = size;
1278 md_get_uint32(mbp, NULL); /* allocation size */
1279 md_get_uint16le(mbp, &wattr);
1280 ctx->f_attr.fa_attr = wattr;
1281 md_get_uint8(mbp, &tb);
1284 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1286 case SMB_FIND_FILE_DIRECTORY_INFO:
1287 md_get_uint32le(mbp, &next);
1288 md_get_uint32(mbp, NULL); /* file index */
1289 md_get_int64(mbp, NULL); /* creation time */
1290 md_get_int64le(mbp, &lint);
1291 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1292 md_get_int64le(mbp, &lint);
1293 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1294 md_get_int64le(mbp, &lint);
1295 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1296 md_get_int64le(mbp, &lint); /* file size */
1297 ctx->f_attr.fa_size = lint;
1298 md_get_int64(mbp, NULL); /* real size (should use) */
1299 md_get_uint32le(mbp, &dattr); /* EA */
1300 ctx->f_attr.fa_attr = dattr;
1301 md_get_uint32le(mbp, &size); /* name len */
1303 recsz = next ? next : fxsz + size;
1306 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1309 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1310 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1312 nmlen = min(size, SMB_MAXFNAMELEN);
1314 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1318 cnt = next - nmlen - fxsz;
1320 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1322 SMBERROR("out of sync\n");
1326 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1327 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1330 if (nmlen && cp[nmlen - 1] == 0)
1335 next = ctx->f_eofs + recsz;
1336 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1337 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1339 * Server needs a resume filename.
1341 if (ctx->f_rnamelen <= nmlen) {
1343 free(ctx->f_rname, M_SMBFSDATA);
1344 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1345 ctx->f_rnamelen = nmlen;
1347 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1348 ctx->f_rname[nmlen] = 0;
1349 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1351 ctx->f_nmlen = nmlen;
1359 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1362 free(ctx->f_name, M_SMBFSDATA);
1364 smb_t2_done(ctx->f_t2);
1365 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1366 smbfs_smb_findclose2(ctx);
1371 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1372 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1374 struct smbfs_fctx *ctx;
1377 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK | M_ZERO);
1378 ctx->f_ssp = dnp->n_mount->sm_share;
1380 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1381 ctx->f_scred = scred;
1382 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1383 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1384 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1385 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1387 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1389 smbfs_findclose(ctx, scred);
1396 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1403 limit *= 4; /* imperical */
1404 ctx->f_scred = scred;
1406 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1407 error = smbfs_findnextLM1(ctx, limit);
1409 error = smbfs_findnextLM2(ctx, limit);
1412 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1413 if ((ctx->f_nmlen == 2 &&
1414 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1415 (ctx->f_nmlen == 4 &&
1416 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1419 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1420 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1421 ctx->f_name[1] == '.'))
1425 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1426 ctx->f_dnp->n_mount->sm_caseopt);
1427 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1432 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1434 ctx->f_scred = scred;
1435 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1436 smbfs_findcloseLM1(ctx);
1438 smbfs_findcloseLM2(ctx);
1440 free(ctx->f_rname, M_SMBFSDATA);
1441 free(ctx, M_SMBFSDATA);
1446 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1447 struct smbfattr *fap, struct smb_cred *scred)
1449 struct smbfs_fctx *ctx;
1452 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1453 bzero(fap, sizeof(*fap));
1454 fap->fa_attr = SMB_FA_DIR;
1458 MPASS(!(nmlen == 2 && name[0] == '.' && name[1] == '.'));
1459 MPASS(!(nmlen == 1 && name[0] == '.'));
1460 ASSERT_VOP_ELOCKED(dnp->n_vnode, "smbfs_smb_lookup");
1461 error = smbfs_findopen(dnp, name, nmlen,
1462 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1465 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1466 error = smbfs_findnext(ctx, 1, scred);
1470 fap->fa_ino = dnp->n_ino;
1472 smbfs_findclose(ctx, scred);