Home | History | Annotate | Download | only in smb
      1 /*
      2  * Copyright (c) 2000, Boris Popov
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      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.
     19  *
     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
     30  * SUCH DAMAGE.
     31  *
     32  * $Id: file.c,v 1.4 2004/12/13 00:25:21 lindak Exp $
     33  */
     34 
     35 /*
     36  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     37  * Use is subject to license terms.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/time.h>
     43 #include <sys/mount.h>
     44 #include <fcntl.h>
     45 #include <ctype.h>
     46 #include <errno.h>
     47 #include <stdio.h>
     48 #include <string.h>
     49 #include <strings.h>
     50 #include <stdlib.h>
     51 #include <pwd.h>
     52 #include <grp.h>
     53 #include <unistd.h>
     54 #include <libintl.h>
     55 
     56 #include <sys/types.h>
     57 #include <sys/file.h>
     58 
     59 #include <netsmb/smb.h>
     60 #include <netsmb/smb_lib.h>
     61 
     62 #include "private.h"
     63 
     64 int
     65 smb_fh_close(struct smb_ctx *ctx, int fh)
     66 {
     67 	struct smb_rq	*rqp;
     68 	struct mbdata	*mbp;
     69 	int error;
     70 
     71 	error = smb_rq_init(ctx, SMB_COM_CLOSE, &rqp);
     72 	if (error != 0)
     73 		return (error);
     74 	mbp = smb_rq_getrequest(rqp);
     75 	smb_rq_wstart(rqp);
     76 	mb_put_uint16le(mbp, (uint16_t)fh);
     77 	mb_put_uint32le(mbp, 0);	/* time stamp */
     78 	smb_rq_wend(rqp);
     79 	mb_put_uint16le(mbp, 0);	/* byte count */
     80 
     81 	error = smb_rq_simple(rqp);
     82 	smb_rq_done(rqp);
     83 
     84 	return (error);
     85 }
     86 
     87 int
     88 smb_fh_ntcreate(
     89 	struct smb_ctx *ctx, char *path,
     90 	int flags, int req_acc, int efattr,
     91 	int share_acc, int open_disp,
     92 	int create_opts, int impersonation,
     93 	int *fhp, uint32_t *action_taken)
     94 {
     95 	struct smb_rq	*rqp;
     96 	struct mbdata	*mbp;
     97 	char		*pathsizep;
     98 	int		pathstart, pathsize;
     99 	int		error, flags2, uc;
    100 	uint16_t	fh;
    101 	uint8_t		wc;
    102 
    103 	flags2 = smb_ctx_flags2(ctx);
    104 	if (flags2 == -1)
    105 		return (EIO);
    106 	uc = flags2 & SMB_FLAGS2_UNICODE;
    107 
    108 	error = smb_rq_init(ctx, SMB_COM_NT_CREATE_ANDX, &rqp);
    109 	if (error != 0)
    110 		return (error);
    111 
    112 	mbp = smb_rq_getrequest(rqp);
    113 	smb_rq_wstart(rqp);
    114 	mb_put_uint16le(mbp, 0xff);	/* secondary command */
    115 	mb_put_uint16le(mbp, 0);	/* offset to next command (none) */
    116 	mb_put_uint8(mbp, 0);		/* MBZ (pad?) */
    117 	(void) mb_fit(mbp, 2, &pathsizep); /* path size - fill in below */
    118 	mb_put_uint32le(mbp, flags);	/* create flags (oplock) */
    119 	mb_put_uint32le(mbp, 0);	/* FID - basis for path if not root */
    120 	mb_put_uint32le(mbp, req_acc);
    121 	mb_put_uint64le(mbp, 0);		/* initial alloc. size */
    122 	mb_put_uint32le(mbp, efattr);		/* ext. file attributes */
    123 	mb_put_uint32le(mbp, share_acc);	/* share access mode */
    124 	mb_put_uint32le(mbp, open_disp);	/* open disposition */
    125 	mb_put_uint32le(mbp, create_opts);  /* create_options */
    126 	mb_put_uint32le(mbp, impersonation);
    127 	mb_put_uint8(mbp, 0);	/* security flags (?) */
    128 	smb_rq_wend(rqp);
    129 	smb_rq_bstart(rqp);
    130 	if (uc) {
    131 		/*
    132 		 * We're about to put a unicode string.  We know
    133 		 * we're misaligned at this point, and need to
    134 		 * save the mb_count at the start of the string,
    135 		 * not at the alignment padding placed before it.
    136 		 * So add the algnment padding by hand here.
    137 		 */
    138 		mb_put_uint8(mbp, 0);
    139 	}
    140 	pathstart = mbp->mb_count;
    141 	mb_put_string(mbp, path, uc);
    142 	smb_rq_bend(rqp);
    143 
    144 	/* Now go back and fill in pathsizep */
    145 	pathsize = mbp->mb_count - pathstart;
    146 	pathsizep[0] = pathsize & 0xFF;
    147 	pathsizep[1] = (pathsize >> 8);
    148 
    149 	error = smb_rq_simple(rqp);
    150 	if (error)
    151 		goto out;
    152 
    153 	mbp = smb_rq_getreply(rqp);
    154 	/*
    155 	 * spec says 26 for word count, but 34 words are defined
    156 	 * and observed from win2000
    157 	 */
    158 	error = md_get_uint8(mbp, &wc);
    159 	if (error || wc < 26) {
    160 		smb_error(dgettext(TEXT_DOMAIN,
    161 		    "%s: open failed, bad word count"), 0, path);
    162 		error = EBADRPC;
    163 		goto out;
    164 	}
    165 	md_get_uint8(mbp, NULL);	/* secondary cmd */
    166 	md_get_uint8(mbp, NULL);	/* mbz */
    167 	md_get_uint16le(mbp, NULL);	/* andxoffset */
    168 	md_get_uint8(mbp, NULL);	/* oplock lvl granted */
    169 	md_get_uint16le(mbp, &fh);	/* FID */
    170 	md_get_uint32le(mbp, action_taken);
    171 #if 0	/* skip decoding the rest */
    172 	md_get_uint64le(mbp, NULL);	/* creation time */
    173 	md_get_uint64le(mbp, NULL);	/* access time */
    174 	md_get_uint64le(mbp, NULL);	/* write time */
    175 	md_get_uint64le(mbp, NULL);	/* change time */
    176 	md_get_uint32le(mbp, NULL);	/* attributes */
    177 	md_get_uint64le(mbp, NULL);	/* allocation size */
    178 	md_get_uint64le(mbp, NULL);	/* EOF */
    179 	md_get_uint16le(mbp, NULL);	/* file type */
    180 	md_get_uint16le(mbp, NULL);	/* device state */
    181 	md_get_uint8(mbp, NULL);	/* directory (boolean) */
    182 #endif
    183 
    184 	/* success! */
    185 	*fhp = fh;
    186 	error = 0;
    187 
    188 out:
    189 	smb_rq_done(rqp);
    190 
    191 	return (error);
    192 }
    193 
    194 /*
    195  * Conveinence wrapper for smb_fh_ntcreate
    196  * Converts Unix-style open call to NTCreate.
    197  */
    198 int
    199 smb_fh_open(struct smb_ctx *ctx, const char *path, int oflag, int *fhp)
    200 {
    201 	int error, mode, open_disp, req_acc, share_acc;
    202 	char *p, *ntpath = NULL;
    203 
    204 	/*
    205 	 * Map O_RDONLY, O_WRONLY, O_RDWR
    206 	 * to FREAD, FWRITE
    207 	 */
    208 	mode = (oflag & 3) + 1;
    209 
    210 	/*
    211 	 * Compute requested access, share access.
    212 	 */
    213 	req_acc = (
    214 	    STD_RIGHT_READ_CONTROL_ACCESS |
    215 	    STD_RIGHT_SYNCHRONIZE_ACCESS);
    216 	share_acc = NTCREATEX_SHARE_ACCESS_NONE;
    217 	if (mode & FREAD) {
    218 		req_acc |= (
    219 		    SA_RIGHT_FILE_READ_DATA |
    220 		    SA_RIGHT_FILE_READ_EA |
    221 		    SA_RIGHT_FILE_READ_ATTRIBUTES);
    222 		share_acc |= NTCREATEX_SHARE_ACCESS_READ;
    223 	}
    224 	if (mode & FWRITE) {
    225 		req_acc |= (
    226 		    SA_RIGHT_FILE_WRITE_DATA |
    227 		    SA_RIGHT_FILE_APPEND_DATA |
    228 		    SA_RIGHT_FILE_WRITE_EA |
    229 		    SA_RIGHT_FILE_WRITE_ATTRIBUTES);
    230 		share_acc |= NTCREATEX_SHARE_ACCESS_WRITE;
    231 	}
    232 
    233 	/*
    234 	 * Compute open disposition
    235 	 */
    236 	if (oflag & FCREAT) {
    237 		/* Creat if necessary. */
    238 		if (oflag & FEXCL) {
    239 			/* exclusive */
    240 			open_disp = NTCREATEX_DISP_CREATE;
    241 		} else if (oflag & FTRUNC)
    242 			open_disp = NTCREATEX_DISP_OVERWRITE_IF;
    243 		else
    244 			open_disp = NTCREATEX_DISP_OPEN_IF;
    245 	} else {
    246 		/* Not creating. */
    247 		if (oflag & FTRUNC)
    248 			open_disp = NTCREATEX_DISP_OVERWRITE;
    249 		else
    250 			open_disp = NTCREATEX_DISP_OPEN;
    251 	}
    252 
    253 	/*
    254 	 * Convert Unix path to NT (backslashes)
    255 	 */
    256 	ntpath = strdup(path);
    257 	if (ntpath == NULL)
    258 		return (ENOMEM);
    259 	for (p = ntpath; *p; p++)
    260 		if (*p == '/')
    261 			*p = '\\';
    262 
    263 	error = smb_fh_ntcreate(ctx, ntpath, 0, /* flags */
    264 	    req_acc, SMB_EFA_NORMAL, share_acc, open_disp,
    265 	    NTCREATEX_OPTIONS_NON_DIRECTORY_FILE,
    266 	    NTCREATEX_IMPERSONATION_IMPERSONATION,
    267 	    fhp, NULL);
    268 	free(ntpath);
    269 
    270 	return (error);
    271 }
    272 
    273 int
    274 smb_fh_read(struct smb_ctx *ctx, int fh, off_t offset, size_t count,
    275 	char *dst)
    276 {
    277 	struct smbioc_rw rwrq;
    278 
    279 	bzero(&rwrq, sizeof (rwrq));
    280 	rwrq.ioc_fh = fh;
    281 	rwrq.ioc_base = dst;
    282 	rwrq.ioc_cnt = count;
    283 	rwrq.ioc_offset = offset;
    284 	if (ioctl(ctx->ct_dev_fd, SMBIOC_READ, &rwrq) == -1) {
    285 		return (-1);
    286 	}
    287 	return (rwrq.ioc_cnt);
    288 }
    289 
    290 int
    291 smb_fh_write(struct smb_ctx *ctx, int fh, off_t offset, size_t count,
    292 	const char *src)
    293 {
    294 	struct smbioc_rw rwrq;
    295 
    296 	bzero(&rwrq, sizeof (rwrq));
    297 	rwrq.ioc_fh = fh;
    298 	rwrq.ioc_base = (char *)src;
    299 	rwrq.ioc_cnt = count;
    300 	rwrq.ioc_offset = offset;
    301 	if (ioctl(ctx->ct_dev_fd, SMBIOC_WRITE, &rwrq) == -1) {
    302 		return (-1);
    303 	}
    304 	return (rwrq.ioc_cnt);
    305 }
    306 
    307 /*
    308  * Do a TRANSACT_NAMED_PIPE, which is basically just a
    309  * pipe write and pipe read, all in one round trip.
    310  *
    311  * tdlen, tdata describe the data to send.
    312  * rdlen, rdata on input describe the receive buffer,
    313  * and on output *rdlen is the received length.
    314  */
    315 int
    316 smb_fh_xactnp(struct smb_ctx *ctx, int fh,
    317 	int tdlen, const char *tdata,	/* transmit */
    318 	int *rdlen, char *rdata,	/* receive */
    319 	int *more)
    320 {
    321 	int		err, rparamcnt;
    322 	uint16_t	setup[2];
    323 
    324 	setup[0] = TRANS_TRANSACT_NAMED_PIPE;
    325 	setup[1] = fh;
    326 	rparamcnt = 0;
    327 
    328 	err = smb_t2_request(ctx, 2, setup, "\\PIPE\\",
    329 	    0, NULL,	/* TX paramcnt, params */
    330 	    tdlen, (void *)tdata,
    331 	    &rparamcnt, NULL,	/* no RX params */
    332 	    rdlen, rdata, more);
    333 
    334 	if (err)
    335 		*rdlen = 0;
    336 
    337 	return (err);
    338 }
    339