2 * SPDX-License-Identifier: BSD-2-Clause
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
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>
37 #include <sys/endian.h>
43 #include <netsmb/smb.h>
44 #include <netsmb/smb_subr.h>
45 #include <netsmb/smb_rq.h>
46 #include <netsmb/smb_conn.h>
48 #include <fs/smbfs/smbfs.h>
49 #include <fs/smbfs/smbfs_node.h>
50 #include <fs/smbfs/smbfs_subr.h>
53 * Lack of inode numbers leads us to the problem of generating them.
54 * Partially this problem can be solved by having a dir/file cache
55 * with inode numbers generated from the incremented by one counter.
56 * However this way will require too much kernel memory, gives all
57 * sorts of locking and consistency problems, not to mentinon counter overflows.
58 * So, I'm decided to use a hash function to generate pseudo random (and unique)
62 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
71 MD5Update(&md5, name, nmlen);
72 MD5Final((u_char *)state, &md5);
73 for (i = 0, ino = 0; i < 4; i++)
75 return dnp->n_ino + ino;
79 ino = dnp->n_ino + smbfs_hash(name, nmlen);
86 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
87 struct smb_cred *scred)
89 struct smb_share *ssp = np->n_mount->sm_share;
95 if (op == SMB_LOCK_SHARED)
96 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
98 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred, &rqp);
101 smb_rq_getrequest(rqp, &mbp);
103 mb_put_uint8(mbp, 0xff); /* secondary command */
104 mb_put_uint8(mbp, 0); /* MBZ */
105 mb_put_uint16le(mbp, 0);
106 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
107 mb_put_uint8(mbp, ltype); /* locktype */
108 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
109 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
110 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
111 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
114 mb_put_uint16le(mbp, pid);
115 mb_put_uint32le(mbp, start);
116 mb_put_uint32le(mbp, end - start);
118 error = smb_rq_simple(rqp);
124 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
125 off_t start, off_t end, struct smb_cred *scred)
127 struct smb_share *ssp = np->n_mount->sm_share;
129 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
131 * TODO: use LOCK_BYTE_RANGE here.
135 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
139 smbfs_query_info_fs(struct smb_share *ssp, struct statfs *sbp,
140 struct smb_cred *scred)
142 struct smb_t2rq *t2p;
146 int64_t units, funits;
149 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
153 mbp = &t2p->t2_tparam;
155 mb_put_uint16le(mbp, SMB_QUERY_FS_SIZE_INFO);
156 t2p->t2_maxpcount = 2;
157 t2p->t2_maxdcount = sizeof(int64_t) * 2 + sizeof(uint32_t) * 2;
158 error = smb_t2_request(t2p);
163 mdp = &t2p->t2_rdata;
164 md_get_int64le(mdp, &units);
165 md_get_int64le(mdp, &funits);
166 md_get_uint32le(mdp, &bpu);
167 md_get_uint32le(mdp, &bsize);
168 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
169 sbp->f_blocks= (uint64_t)units; /* total data blocks in filesystem */
170 sbp->f_bfree = (uint64_t)funits;/* free blocks in fs */
171 sbp->f_bavail= (uint64_t)funits;/* free blocks avail to non-superuser */
172 sbp->f_files = 0xffff; /* total file nodes in filesystem */
173 sbp->f_ffree = 0xffff; /* free file nodes in fs */
179 smbfs_query_info_alloc(struct smb_share *ssp, struct statfs *sbp,
180 struct smb_cred *scred)
182 struct smb_t2rq *t2p;
186 u_int32_t units, bpu, funits;
189 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
193 mbp = &t2p->t2_tparam;
195 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
196 t2p->t2_maxpcount = 4;
197 t2p->t2_maxdcount = 4 * 4 + 2;
198 error = smb_t2_request(t2p);
203 mdp = &t2p->t2_rdata;
204 md_get_uint32(mdp, NULL); /* fs id */
205 md_get_uint32le(mdp, &bpu);
206 md_get_uint32le(mdp, &units);
207 md_get_uint32le(mdp, &funits);
208 md_get_uint16le(mdp, &bsize);
209 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
210 sbp->f_blocks= units; /* total data blocks in filesystem */
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 filesystem */
214 sbp->f_ffree = 0xffff; /* free file nodes in fs */
220 smbfs_query_info_disk(struct smb_share *ssp, struct statfs *sbp,
221 struct smb_cred *scred)
225 u_int16_t units, bpu, bsize, funits;
228 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK,
236 error = smb_rq_simple(rqp);
241 smb_rq_getreply(rqp, &mdp);
242 md_get_uint16le(mdp, &units);
243 md_get_uint16le(mdp, &bpu);
244 md_get_uint16le(mdp, &bsize);
245 md_get_uint16le(mdp, &funits);
246 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
247 sbp->f_blocks= units; /* total data blocks in filesystem */
248 sbp->f_bfree = funits; /* free blocks in fs */
249 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
250 sbp->f_files = 0xffff; /* total file nodes in filesystem */
251 sbp->f_ffree = 0xffff; /* free file nodes in fs */
257 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
258 struct smb_cred *scred)
261 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) {
262 if (smbfs_query_info_fs(ssp, sbp, scred) == 0)
264 if (smbfs_query_info_alloc(ssp, sbp, scred) == 0)
267 return (smbfs_query_info_disk(ssp, sbp, scred));
271 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
273 struct smb_t2rq *t2p;
274 struct smb_share *ssp = np->n_mount->sm_share;
278 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
282 mbp = &t2p->t2_tparam;
284 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
285 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
286 mb_put_uint32le(mbp, 0);
287 mbp = &t2p->t2_tdata;
289 mb_put_int64le(mbp, newsize);
290 mb_put_uint32le(mbp, 0); /* padding */
291 mb_put_uint16le(mbp, 0);
292 t2p->t2_maxpcount = 2;
293 t2p->t2_maxdcount = 0;
294 error = smb_t2_request(t2p);
300 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
302 struct smb_share *ssp = np->n_mount->sm_share;
307 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
308 SMBTOV(np)->v_type != VREG)
309 return 0; /* not a regular open file */
310 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_FLUSH, scred, &rqp);
313 smb_rq_getrequest(rqp, &mbp);
315 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
319 error = smb_rq_simple(rqp);
322 np->n_flag &= ~NFLUSHWIRE;
327 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
329 if (np->n_flag & NFLUSHWIRE)
330 return (smb_smb_flush(np, scred));
335 smbfs_smb_setfsize(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
337 struct smb_share *ssp = np->n_mount->sm_share;
342 if (!smbfs_smb_seteof(np, newsize, scred)) {
343 np->n_flag |= NFLUSHWIRE;
346 /* XXX: We should use SMB_COM_WRITE_ANDX to support large offsets */
347 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
350 smb_rq_getrequest(rqp, &mbp);
352 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
353 mb_put_uint16le(mbp, 0);
354 mb_put_uint32le(mbp, (uint32_t)newsize);
355 mb_put_uint16le(mbp, 0);
358 mb_put_uint8(mbp, SMB_DT_DATA);
359 mb_put_uint16le(mbp, 0);
361 error = smb_rq_simple(rqp);
367 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
368 struct smbfattr *fap, struct smb_cred *scred)
371 struct smb_share *ssp = np->n_mount->sm_share;
379 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred,
383 smb_rq_getrequest(rqp, &mbp);
387 mb_put_uint8(mbp, SMB_DT_ASCII);
389 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
393 error = smb_rq_simple(rqp);
396 smb_rq_getreply(rqp, &mdp);
397 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
401 md_get_uint16le(mdp, &wattr);
402 fap->fa_attr = wattr;
404 * Be careful using the time returned here, as
405 * with FAT on NT4SP6, at least, the time returned is low
406 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
407 * over about every seven minutes!
409 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
410 if (lint) /* avoid bogus zero returns */
411 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
413 md_get_uint32le(mdp, &lint);
421 * Set DOS file attributes. mtime should be NULL for dialects above lm10
424 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
425 struct smb_cred *scred)
428 struct smb_share *ssp = np->n_mount->sm_share;
433 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred,
437 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
438 smb_rq_getrequest(rqp, &mbp);
440 mb_put_uint16le(mbp, attr);
442 smb_time_local2server(mtime, svtz, &time);
445 mb_put_uint32le(mbp, time); /* mtime */
446 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
449 mb_put_uint8(mbp, SMB_DT_ASCII);
451 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
454 mb_put_uint8(mbp, SMB_DT_ASCII);
455 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
457 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
459 mb_put_uint8(mbp, 0);
461 error = smb_rq_simple(rqp);
463 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
472 * Note, win95 doesn't support this call.
475 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
476 struct timespec *atime, int attr, struct smb_cred *scred)
478 struct smb_t2rq *t2p;
479 struct smb_share *ssp = np->n_mount->sm_share;
480 struct smb_vc *vcp = SSTOVC(ssp);
482 u_int16_t date, time;
485 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
489 mbp = &t2p->t2_tparam;
491 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
492 mb_put_uint32le(mbp, 0); /* MBZ */
493 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
494 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
499 tzoff = vcp->vc_sopt.sv_tz;
500 mbp = &t2p->t2_tdata;
502 mb_put_uint32le(mbp, 0); /* creation time */
504 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
507 mb_put_uint16le(mbp, date);
508 mb_put_uint16le(mbp, time);
510 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
513 mb_put_uint16le(mbp, date);
514 mb_put_uint16le(mbp, time);
515 mb_put_uint32le(mbp, 0); /* file size */
516 mb_put_uint32le(mbp, 0); /* allocation unit size */
517 mb_put_uint16le(mbp, attr); /* DOS attr */
518 mb_put_uint32le(mbp, 0); /* EA size */
519 t2p->t2_maxpcount = 5 * 2;
520 t2p->t2_maxdcount = vcp->vc_txmax;
521 error = smb_t2_request(t2p);
527 * NT level. Specially for win9x
530 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
531 struct timespec *atime, struct smb_cred *scred)
533 struct smb_t2rq *t2p;
534 struct smb_share *ssp = np->n_mount->sm_share;
535 struct smb_vc *vcp = SSTOVC(ssp);
540 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
544 mbp = &t2p->t2_tparam;
546 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
547 mb_put_uint32le(mbp, 0); /* MBZ */
548 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
549 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
554 tzoff = vcp->vc_sopt.sv_tz;
555 mbp = &t2p->t2_tdata;
557 mb_put_int64le(mbp, 0); /* creation time */
559 smb_time_local2NT(atime, tzoff, &tm);
562 mb_put_int64le(mbp, tm);
564 smb_time_local2NT(mtime, tzoff, &tm);
567 mb_put_int64le(mbp, tm);
568 mb_put_int64le(mbp, tm); /* change time */
569 mb_put_uint32le(mbp, attr); /* attr */
570 t2p->t2_maxpcount = 24;
571 t2p->t2_maxdcount = 56;
572 error = smb_t2_request(t2p);
578 * Set file atime and mtime. Doesn't supported by core dialect.
581 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
582 struct timespec *atime, struct smb_cred *scred)
585 struct smb_share *ssp = np->n_mount->sm_share;
587 u_int16_t date, time;
590 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred,
594 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
595 smb_rq_getrequest(rqp, &mbp);
597 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
598 mb_put_uint32le(mbp, 0); /* creation time */
601 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
604 mb_put_uint16le(mbp, date);
605 mb_put_uint16le(mbp, time);
607 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
610 mb_put_uint16le(mbp, date);
611 mb_put_uint16le(mbp, time);
615 error = smb_rq_simple(rqp);
616 SMBSDEBUG("%d\n", error);
622 * Set DOS file attributes.
623 * Looks like this call can be used only if SMB_CAP_NT_SMBS bit is on.
626 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
627 struct timespec *atime, struct smb_cred *scred)
629 struct smb_t2rq *t2p;
630 struct smb_share *ssp = np->n_mount->sm_share;
635 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
639 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
640 mbp = &t2p->t2_tparam;
642 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
643 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
644 mb_put_uint32le(mbp, 0);
645 mbp = &t2p->t2_tdata;
647 mb_put_int64le(mbp, 0); /* creation time */
649 smb_time_local2NT(atime, svtz, &tm);
652 mb_put_int64le(mbp, tm);
654 smb_time_local2NT(mtime, svtz, &tm);
657 mb_put_int64le(mbp, tm);
658 mb_put_int64le(mbp, tm); /* change time */
659 mb_put_uint16le(mbp, attr);
660 mb_put_uint32le(mbp, 0); /* padding */
661 mb_put_uint16le(mbp, 0);
662 t2p->t2_maxpcount = 2;
663 t2p->t2_maxdcount = 0;
664 error = smb_t2_request(t2p);
670 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
673 struct smb_share *ssp = np->n_mount->sm_share;
677 u_int16_t fid, wattr, grantedmode;
680 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_OPEN, scred, &rqp);
683 smb_rq_getrequest(rqp, &mbp);
685 mb_put_uint16le(mbp, accmode);
686 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
689 mb_put_uint8(mbp, SMB_DT_ASCII);
691 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
695 error = smb_rq_simple(rqp);
698 smb_rq_getreply(rqp, &mdp);
699 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
703 md_get_uint16(mdp, &fid);
704 md_get_uint16le(mdp, &wattr);
705 md_get_uint32(mdp, NULL); /* mtime */
706 md_get_uint32(mdp, NULL); /* fsize */
707 md_get_uint16le(mdp, &grantedmode);
709 * TODO: refresh attributes from this reply
716 np->n_rwstate = grantedmode;
721 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
722 struct smb_cred *scred)
729 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CLOSE, scred, &rqp);
732 smb_rq_getrequest(rqp, &mbp);
734 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
736 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
739 mb_put_uint32le(mbp, time);
743 error = smb_rq_simple(rqp);
749 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
750 struct smb_cred *scred)
753 struct smb_share *ssp = dnp->n_mount->sm_share;
756 struct timespec ctime;
762 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE, scred, &rqp);
765 smb_rq_getrequest(rqp, &mbp);
767 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
769 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
770 mb_put_uint32le(mbp, tm);
773 mb_put_uint8(mbp, SMB_DT_ASCII);
774 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
777 error = smb_rq_simple(rqp);
779 smb_rq_getreply(rqp, &mdp);
780 md_get_uint8(mdp, &wc);
782 md_get_uint16(mdp, &fid);
790 smbfs_smb_close(ssp, fid, &ctime, scred);
795 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
798 struct smb_share *ssp = np->n_mount->sm_share;
802 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE, scred, &rqp);
805 smb_rq_getrequest(rqp, &mbp);
807 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
810 mb_put_uint8(mbp, SMB_DT_ASCII);
811 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
814 error = smb_rq_simple(rqp);
821 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
822 const char *tname, int tnmlen, struct smb_cred *scred)
825 struct smb_share *ssp = src->n_mount->sm_share;
829 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_RENAME, scred, &rqp);
832 smb_rq_getrequest(rqp, &mbp);
834 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
837 mb_put_uint8(mbp, SMB_DT_ASCII);
839 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
842 mb_put_uint8(mbp, SMB_DT_ASCII);
843 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
847 error = smb_rq_simple(rqp);
854 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
855 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
858 struct smb_share *ssp = src->n_mount->sm_share;
862 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_MOVE, scred, &rqp);
865 smb_rq_getrequest(rqp, &mbp);
867 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
868 mb_put_uint16le(mbp, 0x20); /* delete target file */
869 mb_put_uint16le(mbp, flags);
872 mb_put_uint8(mbp, SMB_DT_ASCII);
874 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
877 mb_put_uint8(mbp, SMB_DT_ASCII);
878 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
882 error = smb_rq_simple(rqp);
889 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
890 struct smb_cred *scred)
893 struct smb_share *ssp = dnp->n_mount->sm_share;
897 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred,
901 smb_rq_getrequest(rqp, &mbp);
905 mb_put_uint8(mbp, SMB_DT_ASCII);
906 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
909 error = smb_rq_simple(rqp);
916 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
919 struct smb_share *ssp = np->n_mount->sm_share;
923 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred,
927 smb_rq_getrequest(rqp, &mbp);
931 mb_put_uint8(mbp, SMB_DT_ASCII);
932 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
935 error = smb_rq_simple(rqp);
942 smbfs_smb_search(struct smbfs_fctx *ctx)
944 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
949 u_int16_t ec, dlen, bc;
950 int maxent, error, iseof = 0;
952 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
954 smb_rq_done(ctx->f_rq);
957 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
961 smb_rq_getrequest(rqp, &mbp);
963 mb_put_uint16le(mbp, maxent); /* max entries to return */
964 mb_put_uint16le(mbp, ctx->f_attrmask);
967 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
968 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
969 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
972 mb_put_uint8(mbp, SMB_DT_VARIABLE);
973 mb_put_uint16le(mbp, 0); /* context length */
974 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
976 if (SMB_UNICODE_STRINGS(vcp)) {
978 mb_put_uint8(mbp, 0);
980 mb_put_uint8(mbp, 0); /* file name length */
981 mb_put_uint8(mbp, SMB_DT_VARIABLE);
982 mb_put_uint16le(mbp, SMB_SKEYLEN);
983 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
986 error = smb_rq_simple(rqp);
988 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
991 ctx->f_flags |= SMBFS_RDD_EOF;
995 smb_rq_getreply(rqp, &mdp);
996 md_get_uint8(mdp, &wc);
998 return iseof ? ENOENT : EBADRPC;
999 md_get_uint16le(mdp, &ec);
1003 md_get_uint16le(mdp, &bc);
1007 md_get_uint8(mdp, &bt);
1008 if (bt != SMB_DT_VARIABLE)
1010 md_get_uint16le(mdp, &dlen);
1011 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1017 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1018 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1020 ctx->f_attrmask = attr;
1022 if (wclen == 1 && wildcard[0] == '*') {
1023 ctx->f_wildcard = "*.*";
1026 ctx->f_wildcard = wildcard;
1027 ctx->f_wclen = wclen;
1030 ctx->f_wildcard = NULL;
1033 ctx->f_name = ctx->f_fname;
1038 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1040 struct mdchain *mbp;
1044 u_int16_t date, time;
1048 if (ctx->f_ecnt == 0) {
1049 if (ctx->f_flags & SMBFS_RDD_EOF)
1051 ctx->f_left = ctx->f_limit = limit;
1052 error = smbfs_smb_search(ctx);
1057 smb_rq_getreply(rqp, &mbp);
1058 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1059 md_get_uint8(mbp, &battr);
1060 md_get_uint16le(mbp, &time);
1061 md_get_uint16le(mbp, &date);
1062 md_get_uint32le(mbp, &size);
1064 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1065 cp[sizeof(ctx->f_fname) - 1] = 0;
1066 cp += strlen(cp) - 1;
1067 while (*cp == ' ' && cp >= ctx->f_name)
1069 ctx->f_attr.fa_attr = battr;
1070 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1071 &ctx->f_attr.fa_mtime);
1072 ctx->f_attr.fa_size = size;
1073 ctx->f_nmlen = strlen(ctx->f_name);
1080 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1083 smb_rq_done(ctx->f_rq);
1088 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1091 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1093 struct smb_t2rq *t2p;
1094 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1095 struct mbchain *mbp;
1096 struct mdchain *mdp;
1097 u_int16_t tw, flags;
1101 smb_t2_done(ctx->f_t2);
1104 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1105 flags = 8 | 2; /* <resume> | <close if EOS> */
1106 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1107 flags |= 1; /* close search after this request */
1108 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1110 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1111 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1112 ctx->f_scred, &t2p);
1116 mbp = &t2p->t2_tparam;
1118 mb_put_uint16le(mbp, ctx->f_attrmask);
1119 mb_put_uint16le(mbp, ctx->f_limit);
1120 mb_put_uint16le(mbp, flags);
1121 mb_put_uint16le(mbp, ctx->f_infolevel);
1122 mb_put_uint32le(mbp, 0);
1123 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1127 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1128 ctx->f_scred, &t2p);
1132 mbp = &t2p->t2_tparam;
1134 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1135 mb_put_uint16le(mbp, ctx->f_limit);
1136 mb_put_uint16le(mbp, ctx->f_infolevel);
1137 mb_put_uint32le(mbp, 0); /* resume key */
1138 mb_put_uint16le(mbp, flags);
1140 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1142 mb_put_uint8(mbp, 0); /* resume file name */
1146 tv.tv_usec = 200 * 1000; /* 200ms */
1147 if (vcp->vc_flags & SMBC_WIN95) {
1149 * some implementations suggests to sleep here
1150 * for 200ms, due to the bug in the Win95.
1151 * I've didn't notice any problem, but put code
1154 pause("fix95", tvtohz(&tv));
1158 t2p->t2_maxpcount = 5 * 2;
1159 t2p->t2_maxdcount = vcp->vc_txmax;
1160 error = smb_t2_request(t2p);
1163 mdp = &t2p->t2_rparam;
1164 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1165 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1167 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1169 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1172 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1175 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1176 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1178 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1180 if (ctx->f_ecnt == 0) {
1181 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1184 ctx->f_rnameofs = tw;
1185 mdp = &t2p->t2_rdata;
1186 if (mdp->md_top == NULL) {
1187 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1190 if (mdp->md_top->m_len == 0) {
1191 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1199 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1202 struct mbchain *mbp;
1205 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2,
1206 ctx->f_scred, &rqp);
1209 smb_rq_getrequest(rqp, &mbp);
1211 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1215 error = smb_rq_simple(rqp);
1221 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1222 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1224 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1225 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1227 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1228 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1229 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1230 ctx->f_attrmask = attr;
1231 ctx->f_wildcard = wildcard;
1232 ctx->f_wclen = wclen;
1237 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1239 struct mdchain *mbp;
1240 struct smb_t2rq *t2p;
1243 u_int16_t date, time, wattr;
1244 u_int32_t size, next, dattr;
1246 int error, svtz, cnt, fxsz, nmlen, recsz;
1248 if (ctx->f_ecnt == 0) {
1249 if (ctx->f_flags & SMBFS_RDD_EOF)
1251 ctx->f_left = ctx->f_limit = limit;
1252 error = smbfs_smb_trans2find2(ctx);
1257 mbp = &t2p->t2_rdata;
1258 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1259 switch (ctx->f_infolevel) {
1260 case SMB_INFO_STANDARD:
1263 md_get_uint16le(mbp, &date);
1264 md_get_uint16le(mbp, &time); /* creation time */
1265 md_get_uint16le(mbp, &date);
1266 md_get_uint16le(mbp, &time); /* access time */
1267 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1268 md_get_uint16le(mbp, &date);
1269 md_get_uint16le(mbp, &time); /* access time */
1270 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1271 md_get_uint32le(mbp, &size);
1272 ctx->f_attr.fa_size = size;
1273 md_get_uint32(mbp, NULL); /* allocation size */
1274 md_get_uint16le(mbp, &wattr);
1275 ctx->f_attr.fa_attr = wattr;
1276 md_get_uint8(mbp, &tb);
1279 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1281 case SMB_FIND_FILE_DIRECTORY_INFO:
1282 md_get_uint32le(mbp, &next);
1283 md_get_uint32(mbp, NULL); /* file index */
1284 md_get_int64(mbp, NULL); /* creation time */
1285 md_get_int64le(mbp, &lint);
1286 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1287 md_get_int64le(mbp, &lint);
1288 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1289 md_get_int64le(mbp, &lint);
1290 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1291 md_get_int64le(mbp, &lint); /* file size */
1292 ctx->f_attr.fa_size = lint;
1293 md_get_int64(mbp, NULL); /* real size (should use) */
1294 md_get_uint32le(mbp, &dattr); /* EA */
1295 ctx->f_attr.fa_attr = dattr;
1296 md_get_uint32le(mbp, &size); /* name len */
1298 recsz = next ? next : fxsz + size;
1301 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1304 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1305 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1307 nmlen = min(size, SMB_MAXFNAMELEN);
1309 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1313 cnt = next - nmlen - fxsz;
1315 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1317 SMBERROR("out of sync\n");
1321 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1322 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1325 if (nmlen && cp[nmlen - 1] == 0)
1330 next = ctx->f_eofs + recsz;
1331 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1332 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1334 * Server needs a resume filename.
1336 if (ctx->f_rnamelen <= nmlen) {
1338 free(ctx->f_rname, M_SMBFSDATA);
1339 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1340 ctx->f_rnamelen = nmlen;
1342 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1343 ctx->f_rname[nmlen] = 0;
1344 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1346 ctx->f_nmlen = nmlen;
1354 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1357 free(ctx->f_name, M_SMBFSDATA);
1359 smb_t2_done(ctx->f_t2);
1360 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1361 smbfs_smb_findclose2(ctx);
1366 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1367 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1369 struct smbfs_fctx *ctx;
1372 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK | M_ZERO);
1373 ctx->f_ssp = dnp->n_mount->sm_share;
1375 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1376 ctx->f_scred = scred;
1377 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1378 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1379 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1380 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1382 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1384 smbfs_findclose(ctx, scred);
1391 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1398 limit *= 4; /* imperical */
1399 ctx->f_scred = scred;
1401 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1402 error = smbfs_findnextLM1(ctx, limit);
1404 error = smbfs_findnextLM2(ctx, limit);
1407 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1408 if ((ctx->f_nmlen == 2 &&
1409 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1410 (ctx->f_nmlen == 4 &&
1411 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1414 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1415 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1416 ctx->f_name[1] == '.'))
1420 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1421 ctx->f_dnp->n_mount->sm_caseopt);
1422 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1427 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1429 ctx->f_scred = scred;
1430 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1431 smbfs_findcloseLM1(ctx);
1433 smbfs_findcloseLM2(ctx);
1435 free(ctx->f_rname, M_SMBFSDATA);
1436 free(ctx, M_SMBFSDATA);
1441 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1442 struct smbfattr *fap, struct smb_cred *scred)
1444 struct smbfs_fctx *ctx;
1447 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1448 bzero(fap, sizeof(*fap));
1449 fap->fa_attr = SMB_FA_DIR;
1453 MPASS(!(nmlen == 2 && name[0] == '.' && name[1] == '.'));
1454 MPASS(!(nmlen == 1 && name[0] == '.'));
1455 ASSERT_VOP_ELOCKED(dnp->n_vnode, "smbfs_smb_lookup");
1456 error = smbfs_findopen(dnp, name, nmlen,
1457 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1460 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1461 error = smbfs_findnext(ctx, 1, scred);
1465 fap->fa_ino = dnp->n_ino;
1467 smbfs_findclose(ctx, scred);