1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21da6c28aamw/*
229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23da6c28aamw * Use is subject to license terms.
24b819ceaGordon Ross *
25b819ceaGordon Ross * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
26da6c28aamw */
27da6c28aamw
28da6c28aamw/*
299fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Legacy encode/decode routines for door clients and servers.
30da6c28aamw */
31da6c28aamw
32b819ceaGordon Ross#if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
339fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <errno.h>
349fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <string.h>
359fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <strings.h>
369fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#else
37da6c28aamw#include <sys/types.h>
38da6c28aamw#include <sys/sunddi.h>
39da6c28aamw#include <sys/errno.h>
40da6c28aamw#endif
41da6c28aamw
423299f39Gordon Ross#include <smb/wintypes.h>
439fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/smb_share.h>
449fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/smb_door.h>
459fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/alloc.h>
469fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States#include <smbsrv/smbinfo.h>
47da6c28aamw
48da6c28aamwsmb_dr_ctx_t *
49da6c28aamwsmb_dr_decode_start(char *ptr, int size)
50da6c28aamw{
51da6c28aamw	smb_dr_ctx_t *ctx = MEM_MALLOC("CommonDoor", sizeof (smb_dr_ctx_t));
52da6c28aamw	if (ctx) {
53da6c28aamw		ctx->start_ptr = ctx->ptr = ptr;
54da6c28aamw		ctx->end_ptr = ptr + size;
55da6c28aamw		ctx->status = 0;
56da6c28aamw	}
57da6c28aamw	return (ctx);
58da6c28aamw}
59da6c28aamw
60da6c28aamwint
61da6c28aamwsmb_dr_decode_finish(smb_dr_ctx_t *ctx)
62da6c28aamw{
63da6c28aamw	int status = ctx->status;
649fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	if (status == 0 && ctx->ptr != ctx->end_ptr)
65da6c28aamw		status = ENOTEMPTY;
669fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
67da6c28aamw	MEM_FREE("CommonDoor", ctx);
68da6c28aamw	return (status);
69da6c28aamw}
70da6c28aamw
71da6c28aamwsmb_dr_ctx_t *
72da6c28aamwsmb_dr_encode_start(char *ptr, int size)
73da6c28aamw{
74da6c28aamw	smb_dr_ctx_t *ctx = MEM_MALLOC("CommonDoor",  sizeof (smb_dr_ctx_t));
75da6c28aamw	if (ctx) {
76da6c28aamw		ctx->start_ptr = ctx->ptr = ptr;
77da6c28aamw		ctx->end_ptr = ptr + size;
78da6c28aamw		ctx->status = 0;
79da6c28aamw	}
80da6c28aamw	return (ctx);
81da6c28aamw}
82da6c28aamw
83da6c28aamwint
84da6c28aamwsmb_dr_encode_finish(smb_dr_ctx_t *ctx, unsigned int *used)
85da6c28aamw{
86da6c28aamw	int status = ctx->status;
87da6c28aamw	if (status == 0) {
88da6c28aamw		if (ctx->ptr < ctx->end_ptr) {
89da6c28aamw			/*LINTED E_PTRDIFF_OVERFLOW*/
90da6c28aamw			*used = ctx->ptr - ctx->start_ptr;
919fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
92da6c28aamw			status = ENOSPC;
939fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
94da6c28aamw	}
95da6c28aamw
96da6c28aamw	MEM_FREE("CommonDoor", ctx);
97da6c28aamw	return (status);
98da6c28aamw}
99da6c28aamw
100da6c28aamwDWORD
101da6c28aamwsmb_dr_get_dword(smb_dr_ctx_t *ctx)
102da6c28aamw{
103da6c28aamw	DWORD num = 0;
104da6c28aamw	if (ctx->status == 0) {
105da6c28aamw		if (ctx->ptr + sizeof (DWORD) <= ctx->end_ptr) {
106da6c28aamw			(void) memcpy(&num, ctx->ptr, sizeof (DWORD));
107da6c28aamw			ctx->ptr += sizeof (DWORD);
1089fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
109da6c28aamw			ctx->status = ENOSPC;
1109fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
111da6c28aamw	}
112da6c28aamw	return (num);
113da6c28aamw}
114da6c28aamw
115da6c28aamwint32_t
116da6c28aamwsmb_dr_get_int32(smb_dr_ctx_t *ctx)
117da6c28aamw{
118da6c28aamw	int32_t num = 0;
119da6c28aamw	if (ctx->status == 0) {
120da6c28aamw		if (ctx->ptr + sizeof (int32_t) <= ctx->end_ptr) {
121da6c28aamw			(void) memcpy(&num, ctx->ptr, sizeof (int32_t));
122da6c28aamw			ctx->ptr += sizeof (int32_t);
1239fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
124da6c28aamw			ctx->status = ENOSPC;
1259fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
126da6c28aamw	}
127da6c28aamw	return (num);
128da6c28aamw}
129da6c28aamw
130da6c28aamwuint32_t
131da6c28aamwsmb_dr_get_uint32(smb_dr_ctx_t *ctx)
132da6c28aamw{
133da6c28aamw	return ((uint32_t)smb_dr_get_int32(ctx));
134da6c28aamw}
135da6c28aamw
136da6c28aamwchar *
137da6c28aamwsmb_dr_get_string(smb_dr_ctx_t *ctx)
138da6c28aamw{
139da6c28aamw	char *buf = NULL;
140da6c28aamw	int len = smb_dr_get_int32(ctx);
141da6c28aamw
142da6c28aamw	if (ctx->status == 0) {
143da6c28aamw		if (len == -1)
144da6c28aamw			return (buf);
145da6c28aamw
146da6c28aamw		if (ctx->ptr + len <= ctx->end_ptr) {
147da6c28aamw			buf = MEM_MALLOC("CommonDoor", len +1);
148da6c28aamw			if (buf) {
1499fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States				if (len == 0) {
150da6c28aamw					(void) strcpy(buf, "");
1519fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States				} else {
152da6c28aamw					(void) memcpy(buf, ctx->ptr, len);
153da6c28aamw					ctx->ptr += len;
154da6c28aamw					*(buf + len) = '\0';
155da6c28aamw				}
1569fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			} else {
157b819ceaGordon Ross#if !defined(_KERNEL) && !defined(_FAKE_KERNEL)
158da6c28aamw				ctx->status = errno;
159da6c28aamw#else
160da6c28aamw				ctx->status = ENOMEM;
161da6c28aamw#endif
1629fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			}
1639fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
164da6c28aamw			ctx->status = ENOSPC;
1659fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
166da6c28aamw	}
167da6c28aamw	return (buf);
168da6c28aamw}
169da6c28aamw
170da6c28aamwvoid
171da6c28aamwsmb_dr_put_dword(smb_dr_ctx_t *ctx, DWORD num)
172da6c28aamw{
173da6c28aamw	if (ctx->status == 0) {
174da6c28aamw		if (ctx->ptr + sizeof (DWORD) <= ctx->end_ptr) {
175da6c28aamw			(void) memcpy(ctx->ptr, &num, sizeof (DWORD));
176da6c28aamw			ctx->ptr += sizeof (DWORD);
1779fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
178da6c28aamw			ctx->status = ENOSPC;
1799fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
180da6c28aamw	}
181da6c28aamw}
182da6c28aamw
183da6c28aamwvoid
184da6c28aamwsmb_dr_put_int32(smb_dr_ctx_t *ctx, int32_t num)
185da6c28aamw{
186da6c28aamw	if (ctx->status == 0) {
187da6c28aamw		if (ctx->ptr + sizeof (int32_t) <= ctx->end_ptr) {
188da6c28aamw			(void) memcpy(ctx->ptr, &num, sizeof (int32_t));
189da6c28aamw			ctx->ptr += sizeof (int32_t);
1909fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
191da6c28aamw			ctx->status = ENOSPC;
1929fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
193da6c28aamw	}
194da6c28aamw}
195da6c28aamw
196da6c28aamwvoid
197da6c28aamwsmb_dr_put_uint32(smb_dr_ctx_t *ctx, uint32_t num)
198da6c28aamw{
199da6c28aamw	smb_dr_put_int32(ctx, (int32_t)num);
200da6c28aamw}
201da6c28aamw
202da6c28aamwvoid
203c8ec8eejose borregosmb_dr_put_string(smb_dr_ctx_t *ctx, const char *buf)
204da6c28aamw{
205da6c28aamw	int len;
206da6c28aamw
207da6c28aamw	if (!buf)
208da6c28aamw		len = -1;
209da6c28aamw	else
210da6c28aamw		len = strlen(buf);
211da6c28aamw
212da6c28aamw	if (ctx->status == 0) {
213da6c28aamw		smb_dr_put_int32(ctx, len);
214da6c28aamw		if (len <= 0)
215da6c28aamw			return;
216da6c28aamw
217da6c28aamw		if (ctx->ptr + len <= ctx->end_ptr) {
218da6c28aamw			(void) memcpy(ctx->ptr, buf, len);
219da6c28aamw			ctx->ptr += len;
2209fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
221da6c28aamw			ctx->status = ENOSPC;
2229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
223da6c28aamw	}
224da6c28aamw}
225da6c28aamw
226da6c28aamwvoid
227da6c28aamwsmb_dr_free_string(char *buf)
228da6c28aamw{
229da6c28aamw	if (buf)
230da6c28aamw		MEM_FREE("CommonDoor", buf);
231da6c28aamw}
232da6c28aamw
233da6c28aamwint64_t
234da6c28aamwsmb_dr_get_int64(smb_dr_ctx_t *ctx)
235da6c28aamw{
236da6c28aamw	int64_t num = 0;
237da6c28aamw	if (ctx->status == 0) {
238da6c28aamw		if (ctx->ptr + sizeof (int64_t) <= ctx->end_ptr) {
239da6c28aamw			(void) memcpy(&num, ctx->ptr, sizeof (int64_t));
240da6c28aamw			ctx->ptr += sizeof (int64_t);
2419fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
242da6c28aamw			ctx->status = ENOSPC;
2439fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
244da6c28aamw	}
245da6c28aamw	return (num);
246da6c28aamw}
247da6c28aamw
248da6c28aamwuint64_t
249da6c28aamwsmb_dr_get_uint64(smb_dr_ctx_t *ctx)
250da6c28aamw{
251da6c28aamw	return ((uint64_t)smb_dr_get_int64(ctx));
252da6c28aamw}
253da6c28aamw
254da6c28aamw
255da6c28aamwvoid
256da6c28aamwsmb_dr_put_int64(smb_dr_ctx_t *ctx, int64_t num)
257da6c28aamw{
258da6c28aamw	if (ctx->status == 0) {
259da6c28aamw		if (ctx->ptr + sizeof (int64_t) <= ctx->end_ptr) {
260da6c28aamw			(void) memcpy(ctx->ptr, &num, sizeof (int64_t));
261da6c28aamw			ctx->ptr += sizeof (int64_t);
2629fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
263da6c28aamw			ctx->status = ENOSPC;
2649fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
265da6c28aamw	}
266da6c28aamw}
267da6c28aamw
268da6c28aamwvoid
269da6c28aamwsmb_dr_put_uint64(smb_dr_ctx_t *ctx, uint64_t num)
270da6c28aamw{
271da6c28aamw	smb_dr_put_int64(ctx, (int64_t)num);
272da6c28aamw}
273da6c28aamw
274da6c28aamwvoid
275da6c28aamwsmb_dr_put_short(smb_dr_ctx_t *ctx, short num)
276da6c28aamw{
277da6c28aamw	if (ctx->status == 0) {
278da6c28aamw		if (ctx->ptr + sizeof (short) <= ctx->end_ptr) {
279da6c28aamw			(void) memcpy(ctx->ptr, &num, sizeof (short));
280da6c28aamw			ctx->ptr += sizeof (short);
2819fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
282da6c28aamw			ctx->status = ENOSPC;
2839fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
284da6c28aamw	}
285da6c28aamw}
286da6c28aamw
287da6c28aamwshort
288da6c28aamwsmb_dr_get_short(smb_dr_ctx_t *ctx)
289da6c28aamw{
290da6c28aamw	short num = 0;
291da6c28aamw	if (ctx->status == 0) {
292da6c28aamw		if (ctx->ptr + sizeof (short) <= ctx->end_ptr) {
293da6c28aamw			(void) memcpy(&num, ctx->ptr, sizeof (short));
294da6c28aamw			ctx->ptr += sizeof (short);
2959fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
296da6c28aamw			ctx->status = ENOSPC;
2979fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
298da6c28aamw	}
299da6c28aamw	return (num);
300da6c28aamw}
301da6c28aamw
302da6c28aamwvoid
303da6c28aamwsmb_dr_put_ushort(smb_dr_ctx_t *ctx, unsigned short num)
304da6c28aamw{
305da6c28aamw	smb_dr_put_short(ctx, (short)num);
306da6c28aamw}
307da6c28aamw
308da6c28aamwunsigned short
309da6c28aamwsmb_dr_get_ushort(smb_dr_ctx_t *ctx)
310da6c28aamw{
311da6c28aamw	return ((unsigned short)smb_dr_get_short(ctx));
312da6c28aamw}
313da6c28aamw
314da6c28aamwvoid
315da6c28aamwsmb_dr_put_word(smb_dr_ctx_t *ctx, WORD num)
316da6c28aamw{
317da6c28aamw	smb_dr_put_ushort(ctx, num);
318da6c28aamw}
319da6c28aamw
320da6c28aamwWORD
321da6c28aamwsmb_dr_get_word(smb_dr_ctx_t *ctx)
322da6c28aamw{
323da6c28aamw	return (smb_dr_get_ushort(ctx));
324da6c28aamw}
325da6c28aamw
326da6c28aamwvoid
327da6c28aamwsmb_dr_put_BYTE(smb_dr_ctx_t *ctx, BYTE byte)
328da6c28aamw{
329da6c28aamw	if (ctx->status == 0) {
330da6c28aamw		if (ctx->ptr + sizeof (BYTE) <= ctx->end_ptr) {
331da6c28aamw			(void) memcpy(ctx->ptr, &byte, sizeof (BYTE));
332da6c28aamw			ctx->ptr += sizeof (BYTE);
3339fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
334da6c28aamw			ctx->status = ENOSPC;
3359fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
336da6c28aamw	}
337da6c28aamw}
338da6c28aamw
339da6c28aamwBYTE
340da6c28aamwsmb_dr_get_BYTE(smb_dr_ctx_t *ctx)
341da6c28aamw{
342da6c28aamw	BYTE byte = 0;
343da6c28aamw	if (ctx->status == 0) {
344da6c28aamw		if (ctx->ptr + sizeof (BYTE) <= ctx->end_ptr) {
345da6c28aamw			(void) memcpy(&byte, ctx->ptr, sizeof (BYTE));
346da6c28aamw			ctx->ptr += sizeof (BYTE);
3479fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
348da6c28aamw			ctx->status = ENOSPC;
3499fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
350da6c28aamw	}
351da6c28aamw	return (byte);
352da6c28aamw}
353da6c28aamw
354da6c28aamwvoid
355da6c28aamwsmb_dr_put_buf(smb_dr_ctx_t *ctx, unsigned char *start, int len)
356da6c28aamw{
357da6c28aamw	smb_dr_put_int32(ctx, len);
358da6c28aamw	if (ctx->status == 0) {
359da6c28aamw		if (ctx->ptr + len <= ctx->end_ptr) {
360da6c28aamw			(void) memcpy(ctx->ptr, start, len);
361da6c28aamw			ctx->ptr += len;
3629fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
363da6c28aamw			ctx->status = ENOSPC;
3649fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
365da6c28aamw	}
366da6c28aamw}
367da6c28aamw
368da6c28aamwint
369da6c28aamwsmb_dr_get_buf(smb_dr_ctx_t *ctx, unsigned char *buf, int bufsize)
370da6c28aamw{
371da6c28aamw	int len = -1;
372da6c28aamw
373da6c28aamw	if (!buf)
374da6c28aamw		return (-1);
375da6c28aamw
376da6c28aamw	len = smb_dr_get_int32(ctx);
377da6c28aamw	if (ctx->status == 0) {
378da6c28aamw		if (bufsize < len) {
379da6c28aamw			ctx->status = ENOSPC;
380da6c28aamw			return (-2);
381da6c28aamw		}
382da6c28aamw
383da6c28aamw		if (ctx->ptr + len <= ctx->end_ptr) {
384da6c28aamw			(void) memcpy(buf, ctx->ptr, len);
385da6c28aamw			ctx->ptr += len;
386da6c28aamw		} else {
387da6c28aamw			ctx->status = ENOSPC;
388da6c28aamw			return (-3);
389da6c28aamw		}
390da6c28aamw	}
391da6c28aamw
392da6c28aamw	return (len);
393da6c28aamw}
3949fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
3959fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
3969fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statessmb_dr_get_share(smb_dr_ctx_t *ctx, smb_share_t *si)
3979fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States{
3989fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	if (ctx->status == 0) {
3999fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		if (smb_dr_get_int32(ctx)) {
4009fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			(void) memcpy(si, ctx->ptr, sizeof (smb_share_t));
4019fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			ctx->ptr += sizeof (smb_share_t);
4029fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
4039fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			bzero(si, sizeof (smb_share_t));
4049fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
4059fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	} else {
4069fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		bzero(si, sizeof (smb_share_t));
4079fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	}
4089fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States}
4099fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States
4109fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statesvoid
4119fb67eaafshin salek ardakani - Sun Microsystems - Irvine United Statessmb_dr_put_share(smb_dr_ctx_t *ctx, smb_share_t *si)
4129fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States{
4139fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	if (si) {
4149fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		smb_dr_put_int32(ctx, 1);
4159fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		if (ctx->ptr + sizeof (smb_share_t) <= ctx->end_ptr) {
4169fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			(void) memcpy(ctx->ptr, si, sizeof (smb_share_t));
4179fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			ctx->ptr += sizeof (smb_share_t);
4189fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		} else {
4199fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States			ctx->status = ENOSPC;
4209fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		}
4219fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	} else {
4229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States		smb_dr_put_int32(ctx, 0);
4239fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	}
4249fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States}
425