Home | History | Annotate | Download | only in smbsrv
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     27 
     28 #include <sys/sunddi.h>
     29 #ifndef _KERNEL
     30 #include <string.h>
     31 #include <strings.h>
     32 #endif /* _KERNEL */
     33 #include <smbsrv/smb_xdr.h>
     34 
     35 #ifdef _KERNEL
     36 /*
     37  * xdr_vector():
     38  *
     39  * XDR a fixed length array. Unlike variable-length arrays,
     40  * the storage of fixed length arrays is static and unfreeable.
     41  * > basep: base of the array
     42  * > size: size of the array
     43  * > elemsize: size of each element
     44  * > xdr_elem: routine to XDR each element
     45  */
     46 #define	LASTUNSIGNED ((uint_t)0-1)
     47 bool_t
     48 xdr_vector(XDR *xdrs, char *basep, uint_t nelem,
     49 	uint_t elemsize, xdrproc_t xdr_elem)
     50 {
     51 	uint_t i;
     52 	char *elptr;
     53 
     54 	elptr = basep;
     55 	for (i = 0; i < nelem; i++) {
     56 		if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
     57 			return (FALSE);
     58 		elptr += elemsize;
     59 	}
     60 	return (TRUE);
     61 }
     62 
     63 /*
     64  * XDR an unsigned char
     65  */
     66 bool_t
     67 xdr_u_char(XDR *xdrs, uchar_t *cp)
     68 {
     69 	int i;
     70 
     71 	switch (xdrs->x_op) {
     72 	case XDR_ENCODE:
     73 		i = (*cp);
     74 		return (XDR_PUTINT32(xdrs, &i));
     75 	case XDR_DECODE:
     76 		if (!XDR_GETINT32(xdrs, &i))
     77 			return (FALSE);
     78 		*cp = (uchar_t)i;
     79 		return (TRUE);
     80 	case XDR_FREE:
     81 		return (TRUE);
     82 	}
     83 	return (FALSE);
     84 }
     85 #endif /* _KERNEL */
     86 
     87 bool_t
     88 xdr_smb_dr_string_t(xdrs, objp)
     89 	XDR *xdrs;
     90 	smb_dr_string_t *objp;
     91 {
     92 	if (!xdr_string(xdrs, &objp->buf, ~0))
     93 		return (FALSE);
     94 	return (TRUE);
     95 }
     96 
     97 bool_t
     98 xdr_smb_dr_bytes_t(xdrs, objp)
     99 	XDR *xdrs;
    100 	smb_dr_bytes_t *objp;
    101 {
    102 	if (!xdr_array(xdrs, (char **)&objp->bytes_val,
    103 	    (uint32_t *)&objp->bytes_len, ~0, sizeof (uint8_t),
    104 	    (xdrproc_t)xdr_uint8_t))
    105 		return (FALSE);
    106 	return (TRUE);
    107 }
    108 
    109 /*
    110  * Encode an opipe header structure into a buffer.
    111  */
    112 int
    113 smb_opipe_hdr_encode(smb_opipe_hdr_t *hdr, uint8_t *buf, uint32_t buflen)
    114 {
    115 	XDR xdrs;
    116 	int rc = 0;
    117 
    118 	xdrmem_create(&xdrs, (const caddr_t)buf, buflen, XDR_ENCODE);
    119 
    120 	if (!smb_opipe_hdr_xdr(&xdrs, hdr))
    121 		rc = -1;
    122 
    123 	xdr_destroy(&xdrs);
    124 	return (rc);
    125 }
    126 
    127 /*
    128  * Decode an XDR buffer into an opipe header structure.
    129  */
    130 int
    131 smb_opipe_hdr_decode(smb_opipe_hdr_t *hdr, uint8_t *buf, uint32_t buflen)
    132 {
    133 	XDR xdrs;
    134 	int rc = 0;
    135 
    136 	bzero(hdr, sizeof (smb_opipe_hdr_t));
    137 	xdrmem_create(&xdrs, (const caddr_t)buf, buflen, XDR_DECODE);
    138 
    139 	if (!smb_opipe_hdr_xdr(&xdrs, hdr))
    140 		rc = -1;
    141 
    142 	xdr_destroy(&xdrs);
    143 	return (rc);
    144 }
    145 
    146 bool_t
    147 smb_opipe_hdr_xdr(XDR *xdrs, smb_opipe_hdr_t *objp)
    148 {
    149 	if (!xdr_uint32_t(xdrs, &objp->oh_magic))
    150 		return (FALSE);
    151 	if (!xdr_uint32_t(xdrs, &objp->oh_fid))
    152 		return (FALSE);
    153 	if (!xdr_uint32_t(xdrs, &objp->oh_op))
    154 		return (FALSE);
    155 	if (!xdr_uint32_t(xdrs, &objp->oh_datalen))
    156 		return (FALSE);
    157 	if (!xdr_uint32_t(xdrs, &objp->oh_resid))
    158 		return (FALSE);
    159 	if (!xdr_uint32_t(xdrs, &objp->oh_status))
    160 		return (FALSE);
    161 	return (TRUE);
    162 }
    163 
    164 /*
    165  * Encode an opipe context structure into a buffer.
    166  */
    167 int
    168 smb_opipe_context_encode(smb_opipe_context_t *ctx, uint8_t *buf,
    169     uint32_t buflen)
    170 {
    171 	XDR xdrs;
    172 	int rc = 0;
    173 
    174 	xdrmem_create(&xdrs, (const caddr_t)buf, buflen, XDR_ENCODE);
    175 
    176 	if (!smb_opipe_context_xdr(&xdrs, ctx))
    177 		rc = -1;
    178 
    179 	xdr_destroy(&xdrs);
    180 	return (rc);
    181 }
    182 
    183 /*
    184  * Decode an XDR buffer into an opipe context structure.
    185  */
    186 int
    187 smb_opipe_context_decode(smb_opipe_context_t *ctx, uint8_t *buf,
    188     uint32_t buflen)
    189 {
    190 	XDR xdrs;
    191 	int rc = 0;
    192 
    193 	bzero(ctx, sizeof (smb_opipe_context_t));
    194 	xdrmem_create(&xdrs, (const caddr_t)buf, buflen, XDR_DECODE);
    195 
    196 	if (!smb_opipe_context_xdr(&xdrs, ctx))
    197 		rc = -1;
    198 
    199 	xdr_destroy(&xdrs);
    200 	return (rc);
    201 }
    202 
    203 bool_t
    204 smb_opipe_context_xdr(XDR *xdrs, smb_opipe_context_t *objp)
    205 {
    206 	if (!xdr_uint64_t(xdrs, &objp->oc_session_id))
    207 		return (FALSE);
    208 	if (!xdr_uint16_t(xdrs, &objp->oc_uid))
    209 		return (FALSE);
    210 	if (!xdr_uint16_t(xdrs, &objp->oc_domain_len))
    211 		return (FALSE);
    212 	if (!xdr_string(xdrs, &objp->oc_domain, ~0))
    213 		return (FALSE);
    214 	if (!xdr_uint16_t(xdrs, &objp->oc_account_len))
    215 		return (FALSE);
    216 	if (!xdr_string(xdrs, &objp->oc_account, ~0))
    217 		return (FALSE);
    218 	if (!xdr_uint16_t(xdrs, &objp->oc_workstation_len))
    219 		return (FALSE);
    220 	if (!xdr_string(xdrs, &objp->oc_workstation, ~0))
    221 		return (FALSE);
    222 	if (!xdr_uint32_t(xdrs, &objp->oc_ipaddr))
    223 		return (FALSE);
    224 	if (!xdr_int32_t(xdrs, &objp->oc_native_os))
    225 		return (FALSE);
    226 	if (!xdr_int64_t(xdrs, &objp->oc_logon_time))
    227 		return (FALSE);
    228 	if (!xdr_uint32_t(xdrs, &objp->oc_flags))
    229 		return (FALSE);
    230 	return (TRUE);
    231 }
    232 
    233 
    234 bool_t
    235 xdr_smb_dr_ulist_t(xdrs, objp)
    236 	XDR *xdrs;
    237 	smb_dr_ulist_t *objp;
    238 {
    239 	if (!xdr_uint32_t(xdrs, &objp->dul_cnt))
    240 		return (FALSE);
    241 	if (!xdr_vector(xdrs, (char *)objp->dul_users, SMB_DR_MAX_USERS,
    242 		sizeof (smb_opipe_context_t), (xdrproc_t)smb_opipe_context_xdr))
    243 		return (FALSE);
    244 	return (TRUE);
    245 }
    246 
    247 bool_t
    248 xdr_smb_dr_kshare_t(xdrs, objp)
    249 	XDR *xdrs;
    250 	smb_dr_kshare_t *objp;
    251 {
    252 	if (!xdr_int32_t(xdrs, &objp->k_op))
    253 		return (FALSE);
    254 	if (!xdr_string(xdrs, &objp->k_path, MAXPATHLEN))
    255 		return (FALSE);
    256 	if (!xdr_string(xdrs, &objp->k_sharename, MAXNAMELEN))
    257 		return (FALSE);
    258 	return (TRUE);
    259 }
    260