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/*
22148c5f4Alan Wright * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2387b8175Joyce McIntosh * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
24da6c28aamw */
25da6c28aamw
26da6c28aamw#include <ctype.h>
27da6c28aamw#include <stdio.h>
28148c5f4Alan Wright#include <stdarg.h>
29148c5f4Alan Wright#include <unistd.h>
30148c5f4Alan Wright#include <sys/fcntl.h>
31da6c28aamw#include <string.h>
321fcced4Jordan Brown#include <strings.h>
33da6c28aamw#include <stdlib.h>
34da6c28aamw#include <pthread.h>
35da6c28aamw#include <sys/varargs.h>
36da6c28aamw#include <sys/types.h>
376d57f83Alan Wright#include <sys/mnttab.h>
38b89a833natalie li - Sun Microsystems - Irvine United States#include <tiuser.h>
39b89a833natalie li - Sun Microsystems - Irvine United States#include <netconfig.h>
40b89a833natalie li - Sun Microsystems - Irvine United States#include <netdir.h>
41b89a833natalie li - Sun Microsystems - Irvine United States#include <sys/systeminfo.h>
42b89a833natalie li - Sun Microsystems - Irvine United States#include <sys/utsname.h>
436d57f83Alan Wright#include <libzfs.h>
441fcced4Jordan Brown#include <dlfcn.h>
45148c5f4Alan Wright#include <time.h>
46148c5f4Alan Wright#include <syslog.h>
476d57f83Alan Wright#include <smbsrv/string.h>
486d57f83Alan Wright#include <smbsrv/libsmb.h>
49b89a833natalie li - Sun Microsystems - Irvine United States
501fcced4Jordan Brown#define	SMB_LIB_ALT	"/usr/lib/smbsrv/libsmbex.so"
511fcced4Jordan Brown
52b89a833natalie li - Sun Microsystems - Irvine United Statesstatic boolean_t smb_netgroup_match(struct nd_hostservlist *, char *, int);
53b89a833natalie li - Sun Microsystems - Irvine United States
549b241b4Yuri Pankovextern int __multi_innetgr();
55b89a833natalie li - Sun Microsystems - Irvine United Statesextern int __netdir_getbyaddr_nosrv(struct netconfig *,
56b89a833natalie li - Sun Microsystems - Irvine United States    struct nd_hostservlist **, struct netbuf *);
57da6c28aamw
58da6c28aamw#define	C2H(c)		"0123456789ABCDEF"[(c)]
59da6c28aamw#define	H2C(c)    (((c) >= '0' && (c) <= '9') ? ((c) - '0') :     \
60da6c28aamw	((c) >= 'a' && (c) <= 'f') ? ((c) - 'a' + 10) :         \
61da6c28aamw	((c) >= 'A' && (c) <= 'F') ? ((c) - 'A' + 10) :         \
62da6c28aamw	'\0')
63da6c28aamw#define	DEFAULT_SBOX_SIZE		256
64da6c28aamw
65da6c28aamw/*
66da6c28aamw *
67da6c28aamw * hexdump
68da6c28aamw *
69da6c28aamw * Simple hex dump display function. Displays nbytes of buffer in hex and
70da6c28aamw * printable format. Non-printing characters are shown as '.'. It is safe
71da6c28aamw * to pass a null pointer. Each line begins with the offset. If nbytes is
72da6c28aamw * 0, the line will be blank except for the offset. Example output:
73da6c28aamw *
74da6c28aamw * 00000000  54 68 69 73 20 69 73 20 61 20 70 72 6F 67 72 61  This is a progra
75da6c28aamw * 00000010  6D 20 74 65 73 74 2E 00                          m test..
76da6c28aamw *
77da6c28aamw */
78da6c28aamwvoid
79dc20a30ashexdump_offset(unsigned char *buffer, int nbytes, unsigned long *start)
80da6c28aamw{
81da6c28aamw	static char *hex = "0123456789ABCDEF";
82da6c28aamw	int i, count;
83da6c28aamw	int offset;
84da6c28aamw	unsigned char *p;
85da6c28aamw	char ascbuf[64];
86da6c28aamw	char hexbuf[64];
87da6c28aamw	char *ap = ascbuf;
88da6c28aamw	char *hp = hexbuf;
89da6c28aamw
90dc20a30as	if ((p = buffer) == NULL)
91da6c28aamw		return;
92da6c28aamw
93da6c28aamw	offset = *start;
94da6c28aamw
95da6c28aamw	*ap = '\0';
96da6c28aamw	*hp = '\0';
97da6c28aamw	count = 0;
98da6c28aamw
99da6c28aamw	for (i = 0; i < nbytes; ++i) {
100da6c28aamw		if (i && (i % 16) == 0) {
101dc20a30as			smb_tracef("%06X %s  %s", offset, hexbuf, ascbuf);
102da6c28aamw			ap = ascbuf;
103da6c28aamw			hp = hexbuf;
104da6c28aamw			count = 0;
105da6c28aamw			offset += 16;
106da6c28aamw		}
107da6c28aamw
108da6c28aamw		ap += sprintf(ap, "%c",
109da6c28aamw		    (*p >= 0x20 && *p < 0x7F) ? *p : '.');
110da6c28aamw		hp += sprintf(hp, " %c%c",
111da6c28aamw		    hex[(*p >> 4) & 0x0F], hex[(*p & 0x0F)]);
112da6c28aamw		++p;
113da6c28aamw		++count;
114da6c28aamw	}
115da6c28aamw
116da6c28aamw	if (count) {
117dc20a30as		smb_tracef("%06X %-48s  %s", offset, hexbuf, ascbuf);
118da6c28aamw		offset += count;
119da6c28aamw	}
120da6c28aamw
121da6c28aamw	*start = offset;
122da6c28aamw}
123da6c28aamw
124da6c28aamwvoid
125da6c28aamwhexdump(unsigned char *buffer, int nbytes)
126da6c28aamw{
127da6c28aamw	unsigned long start = 0;
128da6c28aamw
129dc20a30as	hexdump_offset(buffer, nbytes, &start);
130da6c28aamw}
131da6c28aamw
132da6c28aamw/*
133da6c28aamw * bintohex
134da6c28aamw *
135da6c28aamw * Converts the given binary data (srcbuf) to
136da6c28aamw * its equivalent hex chars (hexbuf).
137da6c28aamw *
138da6c28aamw * hexlen should be at least twice as srclen.
139da6c28aamw * if hexbuf is not big enough returns 0.
140da6c28aamw * otherwise returns number of valid chars in
141da6c28aamw * hexbuf which is srclen * 2.
142da6c28aamw */
143da6c28aamwsize_t
144da6c28aamwbintohex(const char *srcbuf, size_t srclen,
145da6c28aamw    char *hexbuf, size_t hexlen)
146da6c28aamw{
147da6c28aamw	size_t outlen;
148da6c28aamw	char c;
149da6c28aamw
150da6c28aamw	outlen = srclen << 1;
151da6c28aamw
152da6c28aamw	if (hexlen < outlen)
153da6c28aamw		return (0);
154da6c28aamw
155da6c28aamw	while (srclen-- > 0) {
156da6c28aamw		c = *srcbuf++;
157da6c28aamw		*hexbuf++ = C2H(c & 0xF);
158da6c28aamw		*hexbuf++ = C2H((c >> 4) & 0xF);
159da6c28aamw	}
160da6c28aamw
161da6c28aamw	return (outlen);
162da6c28aamw}
163da6c28aamw
164da6c28aamw/*
165da6c28aamw * hextobin
166da6c28aamw *
167da6c28aamw * Converts hex to binary.
168da6c28aamw *
169da6c28aamw * Assuming hexbuf only contains hex digits (chars)
170da6c28aamw * this function convert every two bytes of hexbuf
171