2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/vnode.h>
42 #include <sys/mount.h>
43 #include <sys/endian.h>
49 #include <netsmb/smb.h>
50 #include <netsmb/smb_subr.h>
51 #include <netsmb/smb_rq.h>
52 #include <netsmb/smb_conn.h>
54 #include <fs/smbfs/smbfs.h>
55 #include <fs/smbfs/smbfs_node.h>
56 #include <fs/smbfs/smbfs_subr.h>
59 * Lack of inode numbers leads us to the problem of generating them.
60 * Partially this problem can be solved by having a dir/file cache
61 * with inode numbers generated from the incremented by one counter.
62 * However this way will require too much kernel memory, gives all
63 * sorts of locking and consistency problems, not to mentinon counter overflows.
64 * So, I'm decided to use a hash function to generate pseudo random (and unique)
68 smbfs_getino(struct smbnode *dnp, const char *name, int nmlen)
77 MD5Update(&md5, name, nmlen);
78 MD5Final((u_char *)state, &md5);
79 for (i = 0, ino = 0; i < 4; i++)
81 return dnp->n_ino + ino;
85 ino = dnp->n_ino + smbfs_hash(name, nmlen);
92 smbfs_smb_lockandx(struct smbnode *np, int op, u_int32_t pid, off_t start, off_t end,
93 struct smb_cred *scred)
95 struct smb_share *ssp = np->n_mount->sm_share;
96 struct smb_rq rq, *rqp = &rq;
101 if (op == SMB_LOCK_SHARED)
102 ltype |= SMB_LOCKING_ANDX_SHARED_LOCK;
103 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_LOCKING_ANDX, scred);
106 smb_rq_getrequest(rqp, &mbp);
108 mb_put_uint8(mbp, 0xff); /* secondary command */
109 mb_put_uint8(mbp, 0); /* MBZ */
110 mb_put_uint16le(mbp, 0);
111 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
112 mb_put_uint8(mbp, ltype); /* locktype */
113 mb_put_uint8(mbp, 0); /* oplocklevel - 0 seems is NO_OPLOCK */
114 mb_put_uint32le(mbp, 0); /* timeout - break immediately */
115 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 1 : 0);
116 mb_put_uint16le(mbp, op == SMB_LOCK_RELEASE ? 0 : 1);
119 mb_put_uint16le(mbp, pid);
120 mb_put_uint32le(mbp, start);
121 mb_put_uint32le(mbp, end - start);
123 error = smb_rq_simple(rqp);
129 smbfs_smb_lock(struct smbnode *np, int op, caddr_t id,
130 off_t start, off_t end, struct smb_cred *scred)
132 struct smb_share *ssp = np->n_mount->sm_share;
134 if (SMB_DIALECT(SSTOVC(ssp)) < SMB_DIALECT_LANMAN1_0)
136 * TODO: use LOCK_BYTE_RANGE here.
140 return smbfs_smb_lockandx(np, op, (uintptr_t)id, start, end, scred);
144 smbfs_smb_qpathinfo(struct smbnode *np, struct smbfattr *fap,
145 struct smb_cred *scred, short infolevel)
147 struct smb_share *ssp = np->n_mount->sm_share;
148 struct smb_vc *vcp = SSTOVC(ssp);
149 struct smb_t2rq *t2p;
150 int error, svtz, timesok = 1;
153 u_int16_t date, time, wattr;
155 u_int32_t size, dattr;
157 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_PATH_INFORMATION,
161 mbp = &t2p->t2_tparam;
164 if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12)
165 infolevel = SMB_QUERY_FILE_STANDARD;
167 infolevel = SMB_QUERY_FILE_BASIC_INFO;
169 mb_put_uint16le(mbp, infolevel);
170 mb_put_uint32le(mbp, 0);
171 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs are wrong */
172 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
177 t2p->t2_maxpcount = 2;
178 t2p->t2_maxdcount = vcp->vc_txmax;
179 error = smb_t2_request(t2p);
182 if (infolevel == SMB_QUERY_FILE_STANDARD || error != EINVAL)
184 return smbfs_smb_qpathinfo(np, fap, scred,
185 SMB_QUERY_FILE_STANDARD);
187 mdp = &t2p->t2_rdata;
188 svtz = vcp->vc_sopt.sv_tz;
190 case SMB_QUERY_FILE_STANDARD:
192 md_get_uint16le(mdp, NULL);
193 md_get_uint16le(mdp, NULL); /* creation time */
194 md_get_uint16le(mdp, &date);
195 md_get_uint16le(mdp, &time); /* access time */
198 smb_dos2unixtime(date, time, 0, svtz, &fap->fa_atime);
200 md_get_uint16le(mdp, &date);
201 md_get_uint16le(mdp, &time); /* modify time */
204 smb_dos2unixtime(date, time, 0, svtz, &fap->fa_mtime);
206 md_get_uint32le(mdp, &size);
208 md_get_uint32(mdp, NULL); /* allocation size */
209 md_get_uint16le(mdp, &wattr);
210 fap->fa_attr = wattr;
212 case SMB_QUERY_FILE_BASIC_INFO:
214 md_get_int64(mdp, NULL); /* creation time */
215 md_get_int64le(mdp, &lint);
218 smb_time_NT2local(lint, svtz, &fap->fa_atime);
220 md_get_int64le(mdp, &lint);
223 smb_time_NT2local(lint, svtz, &fap->fa_mtime);
225 md_get_int64le(mdp, &lint);
228 smb_time_NT2local(lint, svtz, &fap->fa_ctime);
230 md_get_uint32le(mdp, &dattr);
231 fap->fa_attr = dattr;
232 md_get_uint32(mdp, NULL);
233 /* XXX could use ALL_INFO to get size */
236 SMBERROR("unexpected info level %d\n", infolevel);
241 * if all times are zero (observed with FAT on NT4SP6)
242 * then fall back to older info level
245 if (infolevel != SMB_QUERY_FILE_STANDARD)
246 return smbfs_smb_qpathinfo(np, fap, scred,
247 SMB_QUERY_FILE_STANDARD);
254 smbfs_smb_statfs2(struct smb_share *ssp, struct statfs *sbp,
255 struct smb_cred *scred)
257 struct smb_t2rq *t2p;
261 u_int32_t units, bpu, funits;
264 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_QUERY_FS_INFORMATION,
268 mbp = &t2p->t2_tparam;
270 mb_put_uint16le(mbp, SMB_INFO_ALLOCATION);
271 t2p->t2_maxpcount = 4;
272 t2p->t2_maxdcount = 4 * 4 + 2;
273 error = smb_t2_request(t2p);
278 mdp = &t2p->t2_rdata;
279 md_get_uint32(mdp, NULL); /* fs id */
280 md_get_uint32le(mdp, &bpu);
281 md_get_uint32le(mdp, &units);
282 md_get_uint32le(mdp, &funits);
283 md_get_uint16le(mdp, &bsize);
284 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
285 sbp->f_blocks= units; /* total data blocks in filesystem */
286 sbp->f_bfree = funits; /* free blocks in fs */
287 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
288 sbp->f_files = 0xffff; /* total file nodes in filesystem */
289 sbp->f_ffree = 0xffff; /* free file nodes in fs */
295 smbfs_smb_statfs(struct smb_share *ssp, struct statfs *sbp,
296 struct smb_cred *scred)
298 struct smb_rq rq, *rqp = &rq;
300 u_int16_t units, bpu, bsize, funits;
303 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION_DISK, scred);
310 error = smb_rq_simple(rqp);
315 smb_rq_getreply(rqp, &mdp);
316 md_get_uint16le(mdp, &units);
317 md_get_uint16le(mdp, &bpu);
318 md_get_uint16le(mdp, &bsize);
319 md_get_uint16le(mdp, &funits);
320 sbp->f_bsize = bpu * bsize; /* fundamental filesystem block size */
321 sbp->f_blocks= units; /* total data blocks in filesystem */
322 sbp->f_bfree = funits; /* free blocks in fs */
323 sbp->f_bavail= funits; /* free blocks avail to non-superuser */
324 sbp->f_files = 0xffff; /* total file nodes in filesystem */
325 sbp->f_ffree = 0xffff; /* free file nodes in fs */
331 smbfs_smb_seteof(struct smbnode *np, int64_t newsize, struct smb_cred *scred)
333 struct smb_t2rq *t2p;
334 struct smb_share *ssp = np->n_mount->sm_share;
338 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
342 mbp = &t2p->t2_tparam;
344 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
345 mb_put_uint16le(mbp, SMB_SET_FILE_END_OF_FILE_INFO);
346 mb_put_uint32le(mbp, 0);
347 mbp = &t2p->t2_tdata;
349 mb_put_int64le(mbp, newsize);
350 mb_put_uint32le(mbp, 0); /* padding */
351 mb_put_uint16le(mbp, 0);
352 t2p->t2_maxpcount = 2;
353 t2p->t2_maxdcount = 0;
354 error = smb_t2_request(t2p);
360 smb_smb_flush(struct smbnode *np, struct smb_cred *scred)
362 struct smb_share *ssp = np->n_mount->sm_share;
363 struct smb_rq rq, *rqp = &rq;
367 if ((np->n_flag & NOPEN) == 0 || !SMBTOV(np) ||
368 SMBTOV(np)->v_type != VREG)
369 return 0; /* not a regular open file */
370 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_FLUSH, scred);
373 smb_rq_getrequest(rqp, &mbp);
375 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
379 error = smb_rq_simple(rqp);
382 np->n_flag &= ~NFLUSHWIRE;
387 smbfs_smb_flush(struct smbnode *np, struct smb_cred *scred)
389 if (np->n_flag & NFLUSHWIRE)
390 return (smb_smb_flush(np, scred));
395 smbfs_smb_setfsize(struct smbnode *np, int newsize, struct smb_cred *scred)
397 struct smb_share *ssp = np->n_mount->sm_share;
398 struct smb_rq rq, *rqp = &rq;
402 if (!smbfs_smb_seteof(np, (int64_t) newsize, scred)) {
403 np->n_flag |= NFLUSHWIRE;
407 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_WRITE, scred);
410 smb_rq_getrequest(rqp, &mbp);
412 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
413 mb_put_uint16le(mbp, 0);
414 mb_put_uint32le(mbp, newsize);
415 mb_put_uint16le(mbp, 0);
418 mb_put_uint8(mbp, SMB_DT_DATA);
419 mb_put_uint16le(mbp, 0);
421 error = smb_rq_simple(rqp);
427 smbfs_smb_query_info(struct smbnode *np, const char *name, int len,
428 struct smbfattr *fap, struct smb_cred *scred)
430 struct smb_rq rq, *rqp = &rq;
431 struct smb_share *ssp = np->n_mount->sm_share;
439 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_QUERY_INFORMATION, scred);
442 smb_rq_getrequest(rqp, &mbp);
446 mb_put_uint8(mbp, SMB_DT_ASCII);
448 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, name, len);
452 error = smb_rq_simple(rqp);
455 smb_rq_getreply(rqp, &mdp);
456 if (md_get_uint8(mdp, &wc) != 0 || wc != 10) {
460 md_get_uint16le(mdp, &wattr);
461 fap->fa_attr = wattr;
463 * Be careful using the time returned here, as
464 * with FAT on NT4SP6, at least, the time returned is low
465 * 32 bits of 100s of nanoseconds (since 1601) so it rolls
466 * over about every seven minutes!
468 md_get_uint32le(mdp, &lint); /* specs: secs since 1970 */
469 if (lint) /* avoid bogus zero returns */
470 smb_time_server2local(lint, SSTOVC(ssp)->vc_sopt.sv_tz,
472 md_get_uint32le(mdp, &lint);
480 * Set DOS file attributes. mtime should be NULL for dialects above lm10
483 smbfs_smb_setpattr(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
484 struct smb_cred *scred)
486 struct smb_rq rq, *rqp = &rq;
487 struct smb_share *ssp = np->n_mount->sm_share;
492 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION, scred);
495 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
496 smb_rq_getrequest(rqp, &mbp);
498 mb_put_uint16le(mbp, attr);
500 smb_time_local2server(mtime, svtz, &time);
503 mb_put_uint32le(mbp, time); /* mtime */
504 mb_put_mem(mbp, NULL, 5 * 2, MB_MZERO);
507 mb_put_uint8(mbp, SMB_DT_ASCII);
509 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
512 mb_put_uint8(mbp, SMB_DT_ASCII);
513 if (SMB_UNICODE_STRINGS(SSTOVC(ssp))) {
515 mb_put_uint8(mbp, 0); /* 1st byte of NULL Unicode char */
517 mb_put_uint8(mbp, 0);
519 error = smb_rq_simple(rqp);
521 SMBERROR("smb_rq_simple(rqp) => error %d\n", error);
530 * Note, win95 doesn't support this call.
533 smbfs_smb_setptime2(struct smbnode *np, struct timespec *mtime,
534 struct timespec *atime, int attr, 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);
540 u_int16_t date, time;
543 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
547 mbp = &t2p->t2_tparam;
549 mb_put_uint16le(mbp, SMB_INFO_STANDARD);
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_uint32le(mbp, 0); /* creation time */
562 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
565 mb_put_uint16le(mbp, date);
566 mb_put_uint16le(mbp, time);
568 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
571 mb_put_uint16le(mbp, date);
572 mb_put_uint16le(mbp, time);
573 mb_put_uint32le(mbp, 0); /* file size */
574 mb_put_uint32le(mbp, 0); /* allocation unit size */
575 mb_put_uint16le(mbp, attr); /* DOS attr */
576 mb_put_uint32le(mbp, 0); /* EA size */
577 t2p->t2_maxpcount = 5 * 2;
578 t2p->t2_maxdcount = vcp->vc_txmax;
579 error = smb_t2_request(t2p);
585 * NT level. Specially for win9x
588 smbfs_smb_setpattrNT(struct smbnode *np, u_short attr, struct timespec *mtime,
589 struct timespec *atime, struct smb_cred *scred)
591 struct smb_t2rq *t2p;
592 struct smb_share *ssp = np->n_mount->sm_share;
593 struct smb_vc *vcp = SSTOVC(ssp);
598 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_PATH_INFORMATION,
602 mbp = &t2p->t2_tparam;
604 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
605 mb_put_uint32le(mbp, 0); /* MBZ */
606 /* mb_put_uint8(mbp, SMB_DT_ASCII); specs incorrect */
607 error = smbfs_fullpath(mbp, vcp, np, NULL, 0);
612 tzoff = vcp->vc_sopt.sv_tz;
613 mbp = &t2p->t2_tdata;
615 mb_put_int64le(mbp, 0); /* creation time */
617 smb_time_local2NT(atime, tzoff, &tm);
620 mb_put_int64le(mbp, tm);
622 smb_time_local2NT(mtime, tzoff, &tm);
625 mb_put_int64le(mbp, tm);
626 mb_put_int64le(mbp, tm); /* change time */
627 mb_put_uint32le(mbp, attr); /* attr */
628 t2p->t2_maxpcount = 24;
629 t2p->t2_maxdcount = 56;
630 error = smb_t2_request(t2p);
636 * Set file atime and mtime. Doesn't supported by core dialect.
639 smbfs_smb_setftime(struct smbnode *np, struct timespec *mtime,
640 struct timespec *atime, struct smb_cred *scred)
642 struct smb_rq rq, *rqp = &rq;
643 struct smb_share *ssp = np->n_mount->sm_share;
645 u_int16_t date, time;
648 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_SET_INFORMATION2, scred);
651 tzoff = SSTOVC(ssp)->vc_sopt.sv_tz;
652 smb_rq_getrequest(rqp, &mbp);
654 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
655 mb_put_uint32le(mbp, 0); /* creation time */
658 smb_time_unix2dos(atime, tzoff, &date, &time, NULL);
661 mb_put_uint16le(mbp, date);
662 mb_put_uint16le(mbp, time);
664 smb_time_unix2dos(mtime, tzoff, &date, &time, NULL);
667 mb_put_uint16le(mbp, date);
668 mb_put_uint16le(mbp, time);
672 error = smb_rq_simple(rqp);
673 SMBSDEBUG("%d\n", error);
679 * Set DOS file attributes.
680 * Looks like this call can be used only if CAP_NT_SMBS bit is on.
683 smbfs_smb_setfattrNT(struct smbnode *np, u_int16_t attr, struct timespec *mtime,
684 struct timespec *atime, struct smb_cred *scred)
686 struct smb_t2rq *t2p;
687 struct smb_share *ssp = np->n_mount->sm_share;
692 error = smb_t2_alloc(SSTOCP(ssp), SMB_TRANS2_SET_FILE_INFORMATION,
696 svtz = SSTOVC(ssp)->vc_sopt.sv_tz;
697 mbp = &t2p->t2_tparam;
699 mb_put_mem(mbp, (caddr_t)&np->n_fid, 2, MB_MSYSTEM);
700 mb_put_uint16le(mbp, SMB_SET_FILE_BASIC_INFO);
701 mb_put_uint32le(mbp, 0);
702 mbp = &t2p->t2_tdata;
704 mb_put_int64le(mbp, 0); /* creation time */
706 smb_time_local2NT(atime, svtz, &tm);
709 mb_put_int64le(mbp, tm);
711 smb_time_local2NT(mtime, svtz, &tm);
714 mb_put_int64le(mbp, tm);
715 mb_put_int64le(mbp, tm); /* change time */
716 mb_put_uint16le(mbp, attr);
717 mb_put_uint32le(mbp, 0); /* padding */
718 mb_put_uint16le(mbp, 0);
719 t2p->t2_maxpcount = 2;
720 t2p->t2_maxdcount = 0;
721 error = smb_t2_request(t2p);
728 smbfs_smb_open(struct smbnode *np, int accmode, struct smb_cred *scred)
730 struct smb_rq rq, *rqp = &rq;
731 struct smb_share *ssp = np->n_mount->sm_share;
735 u_int16_t fid, wattr, grantedmode;
738 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_OPEN, scred);
741 smb_rq_getrequest(rqp, &mbp);
743 mb_put_uint16le(mbp, accmode);
744 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
747 mb_put_uint8(mbp, SMB_DT_ASCII);
749 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
753 error = smb_rq_simple(rqp);
756 smb_rq_getreply(rqp, &mdp);
757 if (md_get_uint8(mdp, &wc) != 0 || wc != 7) {
761 md_get_uint16(mdp, &fid);
762 md_get_uint16le(mdp, &wattr);
763 md_get_uint32(mdp, NULL); /* mtime */
764 md_get_uint32(mdp, NULL); /* fsize */
765 md_get_uint16le(mdp, &grantedmode);
767 * TODO: refresh attributes from this reply
774 np->n_rwstate = grantedmode;
780 smbfs_smb_close(struct smb_share *ssp, u_int16_t fid, struct timespec *mtime,
781 struct smb_cred *scred)
783 struct smb_rq rq, *rqp = &rq;
788 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CLOSE, scred);
791 smb_rq_getrequest(rqp, &mbp);
793 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
795 smb_time_local2server(mtime, SSTOVC(ssp)->vc_sopt.sv_tz, &time);
798 mb_put_uint32le(mbp, time);
802 error = smb_rq_simple(rqp);
808 smbfs_smb_create(struct smbnode *dnp, const char *name, int nmlen,
809 struct smb_cred *scred)
811 struct smb_rq rq, *rqp = &rq;
812 struct smb_share *ssp = dnp->n_mount->sm_share;
815 struct timespec ctime;
821 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE, scred);
824 smb_rq_getrequest(rqp, &mbp);
826 mb_put_uint16le(mbp, SMB_FA_ARCHIVE); /* attributes */
828 smb_time_local2server(&ctime, SSTOVC(ssp)->vc_sopt.sv_tz, &tm);
829 mb_put_uint32le(mbp, tm);
832 mb_put_uint8(mbp, SMB_DT_ASCII);
833 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, nmlen);
836 error = smb_rq_simple(rqp);
838 smb_rq_getreply(rqp, &mdp);
839 md_get_uint8(mdp, &wc);
841 md_get_uint16(mdp, &fid);
849 smbfs_smb_close(ssp, fid, &ctime, scred);
854 smbfs_smb_delete(struct smbnode *np, struct smb_cred *scred)
856 struct smb_rq rq, *rqp = &rq;
857 struct smb_share *ssp = np->n_mount->sm_share;
861 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE, scred);
864 smb_rq_getrequest(rqp, &mbp);
866 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
869 mb_put_uint8(mbp, SMB_DT_ASCII);
870 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
873 error = smb_rq_simple(rqp);
880 smbfs_smb_rename(struct smbnode *src, struct smbnode *tdnp,
881 const char *tname, int tnmlen, struct smb_cred *scred)
883 struct smb_rq rq, *rqp = &rq;
884 struct smb_share *ssp = src->n_mount->sm_share;
888 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_RENAME, scred);
891 smb_rq_getrequest(rqp, &mbp);
893 mb_put_uint16le(mbp, SMB_FA_SYSTEM | SMB_FA_HIDDEN);
896 mb_put_uint8(mbp, SMB_DT_ASCII);
898 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
901 mb_put_uint8(mbp, SMB_DT_ASCII);
902 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
906 error = smb_rq_simple(rqp);
913 smbfs_smb_move(struct smbnode *src, struct smbnode *tdnp,
914 const char *tname, int tnmlen, u_int16_t flags, struct smb_cred *scred)
916 struct smb_rq rq, *rqp = &rq;
917 struct smb_share *ssp = src->n_mount->sm_share;
921 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_MOVE, scred);
924 smb_rq_getrequest(rqp, &mbp);
926 mb_put_uint16le(mbp, SMB_TID_UNKNOWN);
927 mb_put_uint16le(mbp, 0x20); /* delete target file */
928 mb_put_uint16le(mbp, flags);
931 mb_put_uint8(mbp, SMB_DT_ASCII);
933 error = smbfs_fullpath(mbp, SSTOVC(ssp), src, NULL, 0);
936 mb_put_uint8(mbp, SMB_DT_ASCII);
937 error = smbfs_fullpath(mbp, SSTOVC(ssp), tdnp, tname, tnmlen);
941 error = smb_rq_simple(rqp);
948 smbfs_smb_mkdir(struct smbnode *dnp, const char *name, int len,
949 struct smb_cred *scred)
951 struct smb_rq rq, *rqp = &rq;
952 struct smb_share *ssp = dnp->n_mount->sm_share;
956 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_CREATE_DIRECTORY, scred);
959 smb_rq_getrequest(rqp, &mbp);
963 mb_put_uint8(mbp, SMB_DT_ASCII);
964 error = smbfs_fullpath(mbp, SSTOVC(ssp), dnp, name, len);
967 error = smb_rq_simple(rqp);
974 smbfs_smb_rmdir(struct smbnode *np, struct smb_cred *scred)
976 struct smb_rq rq, *rqp = &rq;
977 struct smb_share *ssp = np->n_mount->sm_share;
981 error = smb_rq_init(rqp, SSTOCP(ssp), SMB_COM_DELETE_DIRECTORY, scred);
984 smb_rq_getrequest(rqp, &mbp);
988 mb_put_uint8(mbp, SMB_DT_ASCII);
989 error = smbfs_fullpath(mbp, SSTOVC(ssp), np, NULL, 0);
992 error = smb_rq_simple(rqp);
999 smbfs_smb_search(struct smbfs_fctx *ctx)
1001 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1003 struct mbchain *mbp;
1004 struct mdchain *mdp;
1006 u_int16_t ec, dlen, bc;
1007 int maxent, error, iseof = 0;
1009 maxent = min(ctx->f_left, (vcp->vc_txmax - SMB_HDRLEN - 3) / SMB_DENTRYLEN);
1011 smb_rq_done(ctx->f_rq);
1014 error = smb_rq_alloc(SSTOCP(ctx->f_ssp), SMB_COM_SEARCH, ctx->f_scred, &rqp);
1018 smb_rq_getrequest(rqp, &mbp);
1020 mb_put_uint16le(mbp, maxent); /* max entries to return */
1021 mb_put_uint16le(mbp, ctx->f_attrmask);
1024 mb_put_uint8(mbp, SMB_DT_ASCII); /* buffer format */
1025 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1026 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1029 mb_put_uint8(mbp, SMB_DT_VARIABLE);
1030 mb_put_uint16le(mbp, 0); /* context length */
1031 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1033 if (SMB_UNICODE_STRINGS(vcp)) {
1034 mb_put_padbyte(mbp);
1035 mb_put_uint8(mbp, 0);
1037 mb_put_uint8(mbp, 0); /* file name length */
1038 mb_put_uint8(mbp, SMB_DT_VARIABLE);
1039 mb_put_uint16le(mbp, SMB_SKEYLEN);
1040 mb_put_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1043 error = smb_rq_simple(rqp);
1045 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnofiles) {
1048 ctx->f_flags |= SMBFS_RDD_EOF;
1052 smb_rq_getreply(rqp, &mdp);
1053 md_get_uint8(mdp, &wc);
1055 return iseof ? ENOENT : EBADRPC;
1056 md_get_uint16le(mdp, &ec);
1060 md_get_uint16le(mdp, &bc);
1064 md_get_uint8(mdp, &bt);
1065 if (bt != SMB_DT_VARIABLE)
1067 md_get_uint16le(mdp, &dlen);
1068 if (dlen != bc || dlen % SMB_DENTRYLEN != 0)
1074 smbfs_findopenLM1(struct smbfs_fctx *ctx, struct smbnode *dnp,
1075 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1077 ctx->f_attrmask = attr;
1079 if (wclen == 1 && wildcard[0] == '*') {
1080 ctx->f_wildcard = "*.*";
1083 ctx->f_wildcard = wildcard;
1084 ctx->f_wclen = wclen;
1087 ctx->f_wildcard = NULL;
1090 ctx->f_name = ctx->f_fname;
1095 smbfs_findnextLM1(struct smbfs_fctx *ctx, int limit)
1097 struct mdchain *mbp;
1101 u_int16_t date, time;
1105 if (ctx->f_ecnt == 0) {
1106 if (ctx->f_flags & SMBFS_RDD_EOF)
1108 ctx->f_left = ctx->f_limit = limit;
1109 error = smbfs_smb_search(ctx);
1114 smb_rq_getreply(rqp, &mbp);
1115 md_get_mem(mbp, ctx->f_skey, SMB_SKEYLEN, MB_MSYSTEM);
1116 md_get_uint8(mbp, &battr);
1117 md_get_uint16le(mbp, &time);
1118 md_get_uint16le(mbp, &date);
1119 md_get_uint32le(mbp, &size);
1121 md_get_mem(mbp, cp, sizeof(ctx->f_fname), MB_MSYSTEM);
1122 cp[sizeof(ctx->f_fname) - 1] = 0;
1123 cp += strlen(cp) - 1;
1124 while (*cp == ' ' && cp >= ctx->f_name)
1126 ctx->f_attr.fa_attr = battr;
1127 smb_dos2unixtime(date, time, 0, rqp->sr_vc->vc_sopt.sv_tz,
1128 &ctx->f_attr.fa_mtime);
1129 ctx->f_attr.fa_size = size;
1130 ctx->f_nmlen = strlen(ctx->f_name);
1137 smbfs_findcloseLM1(struct smbfs_fctx *ctx)
1140 smb_rq_done(ctx->f_rq);
1145 * TRANS2_FIND_FIRST2/NEXT2, used for NT LM12 dialect
1148 smbfs_smb_trans2find2(struct smbfs_fctx *ctx)
1150 struct smb_t2rq *t2p;
1151 struct smb_vc *vcp = SSTOVC(ctx->f_ssp);
1152 struct mbchain *mbp;
1153 struct mdchain *mdp;
1154 u_int16_t tw, flags;
1158 smb_t2_done(ctx->f_t2);
1161 ctx->f_flags &= ~SMBFS_RDD_GOTRNAME;
1162 flags = 8 | 2; /* <resume> | <close if EOS> */
1163 if (ctx->f_flags & SMBFS_RDD_FINDSINGLE) {
1164 flags |= 1; /* close search after this request */
1165 ctx->f_flags |= SMBFS_RDD_NOCLOSE;
1167 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1168 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_FIRST2,
1169 ctx->f_scred, &t2p);
1173 mbp = &t2p->t2_tparam;
1175 mb_put_uint16le(mbp, ctx->f_attrmask);
1176 mb_put_uint16le(mbp, ctx->f_limit);
1177 mb_put_uint16le(mbp, flags);
1178 mb_put_uint16le(mbp, ctx->f_infolevel);
1179 mb_put_uint32le(mbp, 0);
1180 error = smbfs_fullpath(mbp, vcp, ctx->f_dnp, ctx->f_wildcard, ctx->f_wclen);
1184 error = smb_t2_alloc(SSTOCP(ctx->f_ssp), SMB_TRANS2_FIND_NEXT2,
1185 ctx->f_scred, &t2p);
1189 mbp = &t2p->t2_tparam;
1191 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1192 mb_put_uint16le(mbp, ctx->f_limit);
1193 mb_put_uint16le(mbp, ctx->f_infolevel);
1194 mb_put_uint32le(mbp, 0); /* resume key */
1195 mb_put_uint16le(mbp, flags);
1197 mb_put_mem(mbp, ctx->f_rname, ctx->f_rnamelen + 1, MB_MSYSTEM);
1199 mb_put_uint8(mbp, 0); /* resume file name */
1203 tv.tv_usec = 200 * 1000; /* 200ms */
1204 if (vcp->vc_flags & SMBC_WIN95) {
1206 * some implementations suggests to sleep here
1207 * for 200ms, due to the bug in the Win95.
1208 * I've didn't notice any problem, but put code
1211 pause("fix95", tvtohz(&tv));
1215 t2p->t2_maxpcount = 5 * 2;
1216 t2p->t2_maxdcount = vcp->vc_txmax;
1217 error = smb_t2_request(t2p);
1220 mdp = &t2p->t2_rparam;
1221 if (ctx->f_flags & SMBFS_RDD_FINDFIRST) {
1222 if ((error = md_get_uint16(mdp, &ctx->f_Sid)) != 0)
1224 ctx->f_flags &= ~SMBFS_RDD_FINDFIRST;
1226 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1229 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1232 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1233 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1235 if ((error = md_get_uint16le(mdp, &tw)) != 0)
1237 if (ctx->f_ecnt == 0) {
1238 ctx->f_flags |= SMBFS_RDD_EOF | SMBFS_RDD_NOCLOSE;
1241 ctx->f_rnameofs = tw;
1242 mdp = &t2p->t2_rdata;
1243 if (mdp->md_top == NULL) {
1244 printf("bug: ecnt = %d, but data is NULL (please report)\n", ctx->f_ecnt);
1247 if (mdp->md_top->m_len == 0) {
1248 printf("bug: ecnt = %d, but m_len = 0 and m_next = %p (please report)\n", ctx->f_ecnt,mbp->mb_top->m_next);
1256 smbfs_smb_findclose2(struct smbfs_fctx *ctx)
1258 struct smb_rq rq, *rqp = &rq;
1259 struct mbchain *mbp;
1262 error = smb_rq_init(rqp, SSTOCP(ctx->f_ssp), SMB_COM_FIND_CLOSE2, ctx->f_scred);
1265 smb_rq_getrequest(rqp, &mbp);
1267 mb_put_mem(mbp, (caddr_t)&ctx->f_Sid, 2, MB_MSYSTEM);
1271 error = smb_rq_simple(rqp);
1277 smbfs_findopenLM2(struct smbfs_fctx *ctx, struct smbnode *dnp,
1278 const char *wildcard, int wclen, int attr, struct smb_cred *scred)
1280 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1281 ctx->f_name = malloc(SMB_MAXFNAMELEN * 2, M_SMBFSDATA, M_WAITOK);
1283 ctx->f_name = malloc(SMB_MAXFNAMELEN, M_SMBFSDATA, M_WAITOK);
1284 if (ctx->f_name == NULL)
1286 ctx->f_infolevel = SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_NTLM0_12 ?
1287 SMB_INFO_STANDARD : SMB_FIND_FILE_DIRECTORY_INFO;
1288 ctx->f_attrmask = attr;
1289 ctx->f_wildcard = wildcard;
1290 ctx->f_wclen = wclen;
1295 smbfs_findnextLM2(struct smbfs_fctx *ctx, int limit)
1297 struct mdchain *mbp;
1298 struct smb_t2rq *t2p;
1301 u_int16_t date, time, wattr;
1302 u_int32_t size, next, dattr;
1304 int error, svtz, cnt, fxsz, nmlen, recsz;
1306 if (ctx->f_ecnt == 0) {
1307 if (ctx->f_flags & SMBFS_RDD_EOF)
1309 ctx->f_left = ctx->f_limit = limit;
1310 error = smbfs_smb_trans2find2(ctx);
1315 mbp = &t2p->t2_rdata;
1316 svtz = SSTOVC(ctx->f_ssp)->vc_sopt.sv_tz;
1317 switch (ctx->f_infolevel) {
1318 case SMB_INFO_STANDARD:
1321 md_get_uint16le(mbp, &date);
1322 md_get_uint16le(mbp, &time); /* creation time */
1323 md_get_uint16le(mbp, &date);
1324 md_get_uint16le(mbp, &time); /* access time */
1325 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_atime);
1326 md_get_uint16le(mbp, &date);
1327 md_get_uint16le(mbp, &time); /* access time */
1328 smb_dos2unixtime(date, time, 0, svtz, &ctx->f_attr.fa_mtime);
1329 md_get_uint32le(mbp, &size);
1330 ctx->f_attr.fa_size = size;
1331 md_get_uint32(mbp, NULL); /* allocation size */
1332 md_get_uint16le(mbp, &wattr);
1333 ctx->f_attr.fa_attr = wattr;
1334 md_get_uint8(mbp, &tb);
1337 recsz = next = 24 + nmlen; /* docs misses zero byte at end */
1339 case SMB_FIND_FILE_DIRECTORY_INFO:
1340 md_get_uint32le(mbp, &next);
1341 md_get_uint32(mbp, NULL); /* file index */
1342 md_get_int64(mbp, NULL); /* creation time */
1343 md_get_int64le(mbp, &lint);
1344 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_atime);
1345 md_get_int64le(mbp, &lint);
1346 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_mtime);
1347 md_get_int64le(mbp, &lint);
1348 smb_time_NT2local(lint, svtz, &ctx->f_attr.fa_ctime);
1349 md_get_int64le(mbp, &lint); /* file size */
1350 ctx->f_attr.fa_size = lint;
1351 md_get_int64(mbp, NULL); /* real size (should use) */
1352 md_get_uint32le(mbp, &dattr); /* EA */
1353 ctx->f_attr.fa_attr = dattr;
1354 md_get_uint32le(mbp, &size); /* name len */
1356 recsz = next ? next : fxsz + size;
1359 SMBERROR("unexpected info level %d\n", ctx->f_infolevel);
1362 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1363 nmlen = min(size, SMB_MAXFNAMELEN * 2);
1365 nmlen = min(size, SMB_MAXFNAMELEN);
1367 error = md_get_mem(mbp, cp, nmlen, MB_MSYSTEM);
1371 cnt = next - nmlen - fxsz;
1373 md_get_mem(mbp, NULL, cnt, MB_MSYSTEM);
1375 SMBERROR("out of sync\n");
1379 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1380 if (nmlen > 1 && cp[nmlen - 1] == 0 && cp[nmlen - 2] == 0)
1383 if (nmlen && cp[nmlen - 1] == 0)
1388 next = ctx->f_eofs + recsz;
1389 if (ctx->f_rnameofs && (ctx->f_flags & SMBFS_RDD_GOTRNAME) == 0 &&
1390 (ctx->f_rnameofs >= ctx->f_eofs && ctx->f_rnameofs < next)) {
1392 * Server needs a resume filename.
1394 if (ctx->f_rnamelen <= nmlen) {
1396 free(ctx->f_rname, M_SMBFSDATA);
1397 ctx->f_rname = malloc(nmlen + 1, M_SMBFSDATA, M_WAITOK);
1398 ctx->f_rnamelen = nmlen;
1400 bcopy(ctx->f_name, ctx->f_rname, nmlen);
1401 ctx->f_rname[nmlen] = 0;
1402 ctx->f_flags |= SMBFS_RDD_GOTRNAME;
1404 ctx->f_nmlen = nmlen;
1412 smbfs_findcloseLM2(struct smbfs_fctx *ctx)
1415 free(ctx->f_name, M_SMBFSDATA);
1417 smb_t2_done(ctx->f_t2);
1418 if ((ctx->f_flags & SMBFS_RDD_NOCLOSE) == 0)
1419 smbfs_smb_findclose2(ctx);
1424 smbfs_findopen(struct smbnode *dnp, const char *wildcard, int wclen, int attr,
1425 struct smb_cred *scred, struct smbfs_fctx **ctxpp)
1427 struct smbfs_fctx *ctx;
1430 ctx = malloc(sizeof(*ctx), M_SMBFSDATA, M_WAITOK);
1433 bzero(ctx, sizeof(*ctx));
1434 ctx->f_ssp = dnp->n_mount->sm_share;
1436 ctx->f_flags = SMBFS_RDD_FINDFIRST;
1437 ctx->f_scred = scred;
1438 if (SMB_DIALECT(SSTOVC(ctx->f_ssp)) < SMB_DIALECT_LANMAN2_0 ||
1439 (dnp->n_mount->sm_flags & SMBFS_MOUNT_NO_LONG)) {
1440 ctx->f_flags |= SMBFS_RDD_USESEARCH;
1441 error = smbfs_findopenLM1(ctx, dnp, wildcard, wclen, attr, scred);
1443 error = smbfs_findopenLM2(ctx, dnp, wildcard, wclen, attr, scred);
1445 smbfs_findclose(ctx, scred);
1452 smbfs_findnext(struct smbfs_fctx *ctx, int limit, struct smb_cred *scred)
1459 limit *= 4; /* imperical */
1460 ctx->f_scred = scred;
1462 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1463 error = smbfs_findnextLM1(ctx, limit);
1465 error = smbfs_findnextLM2(ctx, limit);
1468 if (SMB_UNICODE_STRINGS(SSTOVC(ctx->f_ssp))) {
1469 if ((ctx->f_nmlen == 2 &&
1470 *(u_int16_t *)ctx->f_name == htole16(0x002e)) ||
1471 (ctx->f_nmlen == 4 &&
1472 *(u_int32_t *)ctx->f_name == htole32(0x002e002e)))
1475 if ((ctx->f_nmlen == 1 && ctx->f_name[0] == '.') ||
1476 (ctx->f_nmlen == 2 && ctx->f_name[0] == '.' &&
1477 ctx->f_name[1] == '.'))
1481 smbfs_fname_tolocal(SSTOVC(ctx->f_ssp), ctx->f_name, &ctx->f_nmlen,
1482 ctx->f_dnp->n_mount->sm_caseopt);
1483 ctx->f_attr.fa_ino = smbfs_getino(ctx->f_dnp, ctx->f_name, ctx->f_nmlen);
1488 smbfs_findclose(struct smbfs_fctx *ctx, struct smb_cred *scred)
1490 ctx->f_scred = scred;
1491 if (ctx->f_flags & SMBFS_RDD_USESEARCH) {
1492 smbfs_findcloseLM1(ctx);
1494 smbfs_findcloseLM2(ctx);
1496 free(ctx->f_rname, M_SMBFSDATA);
1497 free(ctx, M_SMBFSDATA);
1502 smbfs_smb_lookup(struct smbnode *dnp, const char *name, int nmlen,
1503 struct smbfattr *fap, struct smb_cred *scred)
1505 struct smbfs_fctx *ctx;
1508 if (dnp == NULL || (dnp->n_ino == 2 && name == NULL)) {
1509 bzero(fap, sizeof(*fap));
1510 fap->fa_attr = SMB_FA_DIR;
1514 if (nmlen == 1 && name[0] == '.') {
1515 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1517 } else if (nmlen == 2 && name[0] == '.' && name[1] == '.') {
1518 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1520 printf("%s: knows NOTHING about '..'\n", __func__);
1523 error = smbfs_findopen(dnp, name, nmlen,
1524 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, scred, &ctx);
1527 ctx->f_flags |= SMBFS_RDD_FINDSINGLE;
1528 error = smbfs_findnext(ctx, 1, scred);
1532 fap->fa_ino = dnp->n_ino;
1534 smbfs_findclose(ctx, scred);