xref: /titanic_50/usr/src/lib/libsmbfs/smb/file.c (revision 613a2f6ba31e891e3d947a356daf5e563d43c1ce)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * Copyright (c) 2000, Boris Popov
34bff34e3Sthurlow  * All rights reserved.
44bff34e3Sthurlow  *
54bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow  * modification, are permitted provided that the following conditions
74bff34e3Sthurlow  * are met:
84bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow  *    must display the following acknowledgement:
154bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
164bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
184bff34e3Sthurlow  *    without specific prior written permission.
194bff34e3Sthurlow  *
204bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow  * SUCH DAMAGE.
314bff34e3Sthurlow  *
324bff34e3Sthurlow  * $Id: file.c,v 1.4 2004/12/13 00:25:21 lindak Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
359c9af259SGordon Ross /*
36*613a2f6bSGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
379c9af259SGordon Ross  * Use is subject to license terms.
389c9af259SGordon Ross  */
394bff34e3Sthurlow 
404bff34e3Sthurlow #include <sys/param.h>
414bff34e3Sthurlow #include <sys/ioctl.h>
424bff34e3Sthurlow #include <sys/time.h>
434bff34e3Sthurlow #include <sys/mount.h>
444bff34e3Sthurlow #include <fcntl.h>
454bff34e3Sthurlow #include <ctype.h>
464bff34e3Sthurlow #include <errno.h>
474bff34e3Sthurlow #include <stdio.h>
484bff34e3Sthurlow #include <string.h>
494bff34e3Sthurlow #include <strings.h>
504bff34e3Sthurlow #include <stdlib.h>
514bff34e3Sthurlow #include <pwd.h>
524bff34e3Sthurlow #include <grp.h>
534bff34e3Sthurlow #include <unistd.h>
549c9af259SGordon Ross #include <libintl.h>
554bff34e3Sthurlow 
564bff34e3Sthurlow #include <sys/types.h>
579c9af259SGordon Ross #include <sys/file.h>
584bff34e3Sthurlow 
59*613a2f6bSGordon Ross #include <netsmb/smb.h>
604bff34e3Sthurlow #include <netsmb/smb_lib.h>
614bff34e3Sthurlow 
629c9af259SGordon Ross #include "private.h"
639c9af259SGordon Ross 
644bff34e3Sthurlow int
65*613a2f6bSGordon Ross smb_fh_close(struct smb_ctx *ctx, int fh)
669c9af259SGordon Ross {
679c9af259SGordon Ross 	struct smb_rq	*rqp;
689c9af259SGordon Ross 	struct mbdata	*mbp;
69*613a2f6bSGordon Ross 	int error;
709c9af259SGordon Ross 
71*613a2f6bSGordon Ross 	error = smb_rq_init(ctx, SMB_COM_CLOSE, &rqp);
72*613a2f6bSGordon Ross 	if (error != 0)
73*613a2f6bSGordon Ross 		return (error);
749c9af259SGordon Ross 	mbp = smb_rq_getrequest(rqp);
75*613a2f6bSGordon Ross 	smb_rq_wstart(rqp);
76*613a2f6bSGordon Ross 	mb_put_uint16le(mbp, (uint16_t)fh);
779c9af259SGordon Ross 	mb_put_uint32le(mbp, 0);	/* time stamp */
789c9af259SGordon Ross 	smb_rq_wend(rqp);
79*613a2f6bSGordon Ross 	mb_put_uint16le(mbp, 0);	/* byte count */
80*613a2f6bSGordon Ross 
81*613a2f6bSGordon Ross 	error = smb_rq_simple(rqp);
829c9af259SGordon Ross 	smb_rq_done(rqp);
839c9af259SGordon Ross 
84*613a2f6bSGordon Ross 	return (error);
859c9af259SGordon Ross }
869c9af259SGordon Ross 
879c9af259SGordon Ross int
889c9af259SGordon Ross smb_fh_ntcreate(
899c9af259SGordon Ross 	struct smb_ctx *ctx, char *path,
909c9af259SGordon Ross 	int flags, int req_acc, int efattr,
919c9af259SGordon Ross 	int share_acc, int open_disp,
929c9af259SGordon Ross 	int create_opts, int impersonation,
93*613a2f6bSGordon Ross 	int *fhp, uint32_t *action_taken)
949c9af259SGordon Ross {
959c9af259SGordon Ross 	struct smb_rq	*rqp;
969c9af259SGordon Ross 	struct mbdata	*mbp;
97*613a2f6bSGordon Ross 	char		*pathsizep;
98*613a2f6bSGordon Ross 	int		pathstart, pathsize;
99*613a2f6bSGordon Ross 	int		error, flags2, uc;
100*613a2f6bSGordon Ross 	uint16_t	fh;
1019c9af259SGordon Ross 	uint8_t		wc;
1029c9af259SGordon Ross 
1039c9af259SGordon Ross 	flags2 = smb_ctx_flags2(ctx);
1049c9af259SGordon Ross 	if (flags2 == -1)
1059c9af259SGordon Ross 		return (EIO);
106*613a2f6bSGordon Ross 	uc = flags2 & SMB_FLAGS2_UNICODE;
1079c9af259SGordon Ross 
108*613a2f6bSGordon Ross 	error = smb_rq_init(ctx, SMB_COM_NT_CREATE_ANDX, &rqp);
1099c9af259SGordon Ross 	if (error != 0)
1109c9af259SGordon Ross 		return (error);
1119c9af259SGordon Ross 
1129c9af259SGordon Ross 	mbp = smb_rq_getrequest(rqp);
113*613a2f6bSGordon Ross 	smb_rq_wstart(rqp);
114*613a2f6bSGordon Ross 	mb_put_uint16le(mbp, 0xff);	/* secondary command */
1159c9af259SGordon Ross 	mb_put_uint16le(mbp, 0);	/* offset to next command (none) */
116*613a2f6bSGordon Ross 	mb_put_uint8(mbp, 0);		/* MBZ (pad?) */
117*613a2f6bSGordon Ross 	mb_fit(mbp, 2, &pathsizep);	/* path size - fill in below */
118*613a2f6bSGordon Ross 	mb_put_uint32le(mbp, flags);	/* create flags (oplock) */
1199c9af259SGordon Ross 	mb_put_uint32le(mbp, 0);	/* FID - basis for path if not root */
1209c9af259SGordon Ross 	mb_put_uint32le(mbp, req_acc);
1219c9af259SGordon Ross 	mb_put_uint64le(mbp, 0);		/* initial alloc. size */
1229c9af259SGordon Ross 	mb_put_uint32le(mbp, efattr);		/* ext. file attributes */
1239c9af259SGordon Ross 	mb_put_uint32le(mbp, share_acc);	/* share access mode */
1249c9af259SGordon Ross 	mb_put_uint32le(mbp, open_disp);	/* open disposition */
1259c9af259SGordon Ross 	mb_put_uint32le(mbp, create_opts);  /* create_options */
126*613a2f6bSGordon Ross 	mb_put_uint32le(mbp, impersonation);
1279c9af259SGordon Ross 	mb_put_uint8(mbp, 0);	/* security flags (?) */
1289c9af259SGordon Ross 	smb_rq_wend(rqp);
129*613a2f6bSGordon Ross 	smb_rq_bstart(rqp);
130*613a2f6bSGordon Ross 	if (uc) {
131*613a2f6bSGordon Ross 		/*
132*613a2f6bSGordon Ross 		 * We're about to put a unicode string.  We know
133*613a2f6bSGordon Ross 		 * we're misaligned at this point, and need to
134*613a2f6bSGordon Ross 		 * save the mb_count at the start of the string,
135*613a2f6bSGordon Ross 		 * not at the alignment padding placed before it.
136*613a2f6bSGordon Ross 		 * So add the algnment padding by hand here.
137*613a2f6bSGordon Ross 		 */
138*613a2f6bSGordon Ross 		mb_put_uint8(mbp, 0);
139*613a2f6bSGordon Ross 	}
140*613a2f6bSGordon Ross 	pathstart = mbp->mb_count;
141*613a2f6bSGordon Ross 	mb_put_dstring(mbp, path, uc);
142*613a2f6bSGordon Ross 	smb_rq_bend(rqp);
1439c9af259SGordon Ross 
144*613a2f6bSGordon Ross 	/* Now go back and fill in pathsizep */
145*613a2f6bSGordon Ross 	pathsize = mbp->mb_count - pathstart;
146*613a2f6bSGordon Ross 	pathsizep[0] = pathsize & 0xFF;
147*613a2f6bSGordon Ross 	pathsizep[1] = (pathsize >> 8);
1489c9af259SGordon Ross 
1499c9af259SGordon Ross 	error = smb_rq_simple(rqp);
1509c9af259SGordon Ross 	if (error)
1519c9af259SGordon Ross 		goto out;
1529c9af259SGordon Ross 
1539c9af259SGordon Ross 	mbp = smb_rq_getreply(rqp);
1549c9af259SGordon Ross 	/*
1559c9af259SGordon Ross 	 * spec says 26 for word count, but 34 words are defined
1569c9af259SGordon Ross 	 * and observed from win2000
1579c9af259SGordon Ross 	 */
158*613a2f6bSGordon Ross 	error = mb_get_uint8(mbp, &wc);
159*613a2f6bSGordon Ross 	if (error || wc < 26) {
1609c9af259SGordon Ross 		smb_error(dgettext(TEXT_DOMAIN,
1619c9af259SGordon Ross 		    "%s: open failed, bad word count"), 0, path);
1629c9af259SGordon Ross 		error = EBADRPC;
1639c9af259SGordon Ross 		goto out;
1649c9af259SGordon Ross 	}
1659c9af259SGordon Ross 	mb_get_uint8(mbp, NULL);	/* secondary cmd */
1669c9af259SGordon Ross 	mb_get_uint8(mbp, NULL);	/* mbz */
1679c9af259SGordon Ross 	mb_get_uint16le(mbp, NULL);	/* andxoffset */
1689c9af259SGordon Ross 	mb_get_uint8(mbp, NULL);	/* oplock lvl granted */
169*613a2f6bSGordon Ross 	mb_get_uint16le(mbp, &fh);	/* FID */
1709c9af259SGordon Ross 	mb_get_uint32le(mbp, action_taken);
1719c9af259SGordon Ross #if 0	/* skip decoding the rest */
1729c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* creation time */
1739c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* access time */
1749c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* write time */
1759c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* change time */
1769c9af259SGordon Ross 	mb_get_uint32le(mbp, NULL);	/* attributes */
1779c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* allocation size */
1789c9af259SGordon Ross 	mb_get_uint64le(mbp, NULL);	/* EOF */
1799c9af259SGordon Ross 	mb_get_uint16le(mbp, NULL);	/* file type */
1809c9af259SGordon Ross 	mb_get_uint16le(mbp, NULL);	/* device state */
1819c9af259SGordon Ross 	mb_get_uint8(mbp, NULL);	/* directory (boolean) */
182*613a2f6bSGordon Ross #endif
183*613a2f6bSGordon Ross 
184*613a2f6bSGordon Ross 	/* success! */
185*613a2f6bSGordon Ross 	*fhp = fh;
186*613a2f6bSGordon Ross 	error = 0;
1879c9af259SGordon Ross 
1889c9af259SGordon Ross out:
1899c9af259SGordon Ross 	smb_rq_done(rqp);
1909c9af259SGordon Ross 
191*613a2f6bSGordon Ross 	return (error);
1929c9af259SGordon Ross }
1939c9af259SGordon Ross 
1949c9af259SGordon Ross /*
1959c9af259SGordon Ross  * Conveinence wrapper for smb_fh_ntcreate
1969c9af259SGordon Ross  * Converts Unix-style open call to NTCreate.
1979c9af259SGordon Ross  */
1989c9af259SGordon Ross int
199*613a2f6bSGordon Ross smb_fh_open(struct smb_ctx *ctx, const char *path, int oflag, int *fhp)
2009c9af259SGordon Ross {
2019c9af259SGordon Ross 	int error, mode, open_disp, req_acc, share_acc;
2029c9af259SGordon Ross 	char *p, *ntpath = NULL;
2039c9af259SGordon Ross 
2049c9af259SGordon Ross 	/*
2059c9af259SGordon Ross 	 * Map O_RDONLY, O_WRONLY, O_RDWR
2069c9af259SGordon Ross 	 * to FREAD, FWRITE
2079c9af259SGordon Ross 	 */
2089c9af259SGordon Ross 	mode = (oflag & 3) + 1;
2099c9af259SGordon Ross 
2109c9af259SGordon Ross 	/*
2119c9af259SGordon Ross 	 * Compute requested access, share access.
2129c9af259SGordon Ross 	 */
2139c9af259SGordon Ross 	req_acc = (
2149c9af259SGordon Ross 	    STD_RIGHT_READ_CONTROL_ACCESS |
2159c9af259SGordon Ross 	    STD_RIGHT_SYNCHRONIZE_ACCESS);
2169c9af259SGordon Ross 	share_acc = NTCREATEX_SHARE_ACCESS_NONE;
2179c9af259SGordon Ross 	if (mode & FREAD) {
2189c9af259SGordon Ross 		req_acc |= (
2199c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_DATA |
2209c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_EA |
2219c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_ATTRIBUTES);
2229c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_READ;
2239c9af259SGordon Ross 	}
2249c9af259SGordon Ross 	if (mode & FWRITE) {
2259c9af259SGordon Ross 		req_acc |= (
2269c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_DATA |
2279c9af259SGordon Ross 		    SA_RIGHT_FILE_APPEND_DATA |
2289c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_EA |
2299c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_ATTRIBUTES);
2309c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_WRITE;
2319c9af259SGordon Ross 	}
2329c9af259SGordon Ross 
2339c9af259SGordon Ross 	/*
2349c9af259SGordon Ross 	 * Compute open disposition
2359c9af259SGordon Ross 	 */
2369c9af259SGordon Ross 	if (oflag & FCREAT) {
2379c9af259SGordon Ross 		/* Creat if necessary. */
2389c9af259SGordon Ross 		if (oflag & FEXCL) {
2399c9af259SGordon Ross 			/* exclusive */
2409c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_CREATE;
2419c9af259SGordon Ross 		} else if (oflag & FTRUNC)
2429c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE_IF;
2439c9af259SGordon Ross 		else
2449c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN_IF;
2459c9af259SGordon Ross 	} else {
2469c9af259SGordon Ross 		/* Not creating. */
2479c9af259SGordon Ross 		if (oflag & FTRUNC)
2489c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE;
2499c9af259SGordon Ross 		else
2509c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN;
2519c9af259SGordon Ross 	}
2529c9af259SGordon Ross 
2539c9af259SGordon Ross 	/*
2549c9af259SGordon Ross 	 * Convert Unix path to NT (backslashes)
2559c9af259SGordon Ross 	 */
2569c9af259SGordon Ross 	ntpath = strdup(path);
2579c9af259SGordon Ross 	if (ntpath == NULL)
2589c9af259SGordon Ross 		return (ENOMEM);
2599c9af259SGordon Ross 	for (p = ntpath; *p; p++)
2609c9af259SGordon Ross 		if (*p == '/')
2619c9af259SGordon Ross 			*p = '\\';
2629c9af259SGordon Ross 
2639c9af259SGordon Ross 	error = smb_fh_ntcreate(ctx, ntpath, 0, /* flags */
2649c9af259SGordon Ross 	    req_acc, SMB_EFA_NORMAL, share_acc, open_disp,
2659c9af259SGordon Ross 	    NTCREATEX_OPTIONS_NON_DIRECTORY_FILE,
2669c9af259SGordon Ross 	    NTCREATEX_IMPERSONATION_IMPERSONATION,
2679c9af259SGordon Ross 	    fhp, NULL);
2689c9af259SGordon Ross 	free(ntpath);
2699c9af259SGordon Ross 
2709c9af259SGordon Ross 	return (error);
2719c9af259SGordon Ross }
2729c9af259SGordon Ross 
2739c9af259SGordon Ross int
274*613a2f6bSGordon Ross smb_fh_read(struct smb_ctx *ctx, int fh, off_t offset, size_t count,
2759c9af259SGordon Ross 	char *dst)
2764bff34e3Sthurlow {
2774bff34e3Sthurlow 	struct smbioc_rw rwrq;
2784bff34e3Sthurlow 
2794bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
2804bff34e3Sthurlow 	rwrq.ioc_fh = fh;
2814bff34e3Sthurlow 	rwrq.ioc_base = dst;
2824bff34e3Sthurlow 	rwrq.ioc_cnt = count;
2834bff34e3Sthurlow 	rwrq.ioc_offset = offset;
284*613a2f6bSGordon Ross 	if (ioctl(ctx->ct_dev_fd, SMBIOC_READ, &rwrq) == -1) {
2854bff34e3Sthurlow 		return (-1);
2864bff34e3Sthurlow 	}
2874bff34e3Sthurlow 	return (rwrq.ioc_cnt);
2884bff34e3Sthurlow }
2894bff34e3Sthurlow 
2904bff34e3Sthurlow int
291*613a2f6bSGordon Ross smb_fh_write(struct smb_ctx *ctx, int fh, off_t offset, size_t count,
2924bff34e3Sthurlow 	const char *src)
2934bff34e3Sthurlow {
2944bff34e3Sthurlow 	struct smbioc_rw rwrq;
2954bff34e3Sthurlow 
2964bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
2974bff34e3Sthurlow 	rwrq.ioc_fh = fh;
2984bff34e3Sthurlow 	rwrq.ioc_base = (char *)src;
2994bff34e3Sthurlow 	rwrq.ioc_cnt = count;
3004bff34e3Sthurlow 	rwrq.ioc_offset = offset;
301*613a2f6bSGordon Ross 	if (ioctl(ctx->ct_dev_fd, SMBIOC_WRITE, &rwrq) == -1) {
3024bff34e3Sthurlow 		return (-1);
3034bff34e3Sthurlow 	}
3044bff34e3Sthurlow 	return (rwrq.ioc_cnt);
3054bff34e3Sthurlow }
3069c9af259SGordon Ross 
3079c9af259SGordon Ross /*
3089c9af259SGordon Ross  * Do a TRANSACT_NAMED_PIPE, which is basically just a
3099c9af259SGordon Ross  * pipe write and pipe read, all in one round trip.
3109c9af259SGordon Ross  *
3119c9af259SGordon Ross  * tdlen, tdata describe the data to send.
3129c9af259SGordon Ross  * rdlen, rdata on input describe the receive buffer,
3139c9af259SGordon Ross  * and on output *rdlen is the received length.
3149c9af259SGordon Ross  */
3159c9af259SGordon Ross int
316*613a2f6bSGordon Ross smb_fh_xactnp(struct smb_ctx *ctx, int fh,
3179c9af259SGordon Ross 	int tdlen, const char *tdata,	/* transmit */
3189c9af259SGordon Ross 	int *rdlen, char *rdata,	/* receive */
3199c9af259SGordon Ross 	int *more)
3209c9af259SGordon Ross {
3219c9af259SGordon Ross 	int		err, rparamcnt;
3229c9af259SGordon Ross 	uint16_t	setup[2];
3239c9af259SGordon Ross 
3249c9af259SGordon Ross 	setup[0] = TRANS_TRANSACT_NAMED_PIPE;
3259c9af259SGordon Ross 	setup[1] = fh;
3269c9af259SGordon Ross 	rparamcnt = 0;
3279c9af259SGordon Ross 
3289c9af259SGordon Ross 	err = smb_t2_request(ctx, 2, setup, "\\PIPE\\",
3299c9af259SGordon Ross 	    0, NULL,	/* TX paramcnt, params */
3309c9af259SGordon Ross 	    tdlen, (void *)tdata,
3319c9af259SGordon Ross 	    &rparamcnt, NULL,	/* no RX params */
3329c9af259SGordon Ross 	    rdlen, rdata, more);
3339c9af259SGordon Ross 
3349c9af259SGordon Ross 	if (err)
3359c9af259SGordon Ross 		*rdlen = 0;
3369c9af259SGordon Ross 
3379c9af259SGordon Ross 	return (err);
3389c9af259SGordon Ross }
339