xref: /freebsd/contrib/smbfs/lib/smb/subr.c (revision c45579ad414930948f2c8d4eb07fc751561bd80a)
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: subr.c,v 1.11 2001/04/16 04:33:01 bp Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/errno.h>
38 #include <sys/sysctl.h>
39 #include <sys/syscall.h>
40 #include <unistd.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <err.h>
47 
48 #include <netsmb/netbios.h>
49 #include <netsmb/smb_lib.h>
50 #include <netsmb/nb_lib.h>
51 #include <cflib.h>
52 
53 extern char *__progname;
54 
55 static int smblib_initialized;
56 
57 struct rcfile *smb_rc;
58 
59 int
60 smb_lib_init(void)
61 {
62 	int error;
63 	int kv;
64 	size_t kvlen = sizeof(kv);
65 
66 	if (smblib_initialized)
67 		return 0;
68 #if __FreeBSD_version > 400000
69 	error = sysctlbyname("net.smb.version", &kv, &kvlen, NULL, 0);
70 	if (error) {
71 		warnx("%s: can't find kernel module\n", __FUNCTION__);
72 		return error;
73 	}
74 	if (NSMB_VERSION != kv) {
75 		warnx("%s: kernel module version(%d) don't match library(%d).\n", __FUNCTION__, kv, NSMB_VERSION);
76 		return EINVAL;
77 	}
78 #endif
79 	if ((error = nls_setlocale("")) != 0) {
80 		warnx("%s: can't initialise locale\n", __FUNCTION__);
81 		return error;
82 	}
83 	smblib_initialized++;
84 	return 0;
85 }
86 
87 /*
88  * Print a (descriptive) error message
89  * error values:
90  *  	   0 - no specific error code available;
91  *  1..32767 - system error
92  */
93 void
94 smb_error(const char *fmt, int error,...) {
95 	va_list ap;
96 	const char *cp;
97 	int errtype = error & SMB_ERRTYPE_MASK;
98 
99 	fprintf(stderr, "%s: ", __progname);
100 	va_start(ap, error);
101 	vfprintf(stderr, fmt, ap);
102 	va_end(ap);
103 	if (error == -1)
104 		error = errno;
105 	else
106 		error &= ~SMB_ERRTYPE_MASK;
107 	switch (errtype) {
108 	    case SMB_SYS_ERROR:
109 		if (error)
110 			fprintf(stderr, ": syserr = %s\n", strerror(error));
111 		else
112 			fprintf(stderr, "\n");
113 		break;
114 	    case SMB_RAP_ERROR:
115 		fprintf(stderr, ": raperr = %d (0x%04x)\n", error, error);
116 		break;
117 	    case SMB_NB_ERROR:
118 		cp = nb_strerror(error);
119 		if (cp == NULL)
120 			fprintf(stderr, ": nberr = unknown (0x%04x)\n", error);
121 		else
122 			fprintf(stderr, ": nberr = %s\n", cp);
123 		break;
124 	    default:
125 		fprintf(stderr, "\n");
126 	}
127 }
128 
129 char *
130 smb_printb(char *dest, int flags, const struct smb_bitname *bnp) {
131 	int first = 1;
132 
133 	strcpy(dest, "<");
134 	for(; bnp->bn_bit; bnp++) {
135 		if (flags & bnp->bn_bit) {
136 			strcat(dest, bnp->bn_name);
137 			first = 0;
138 		}
139 		if (!first && (flags & bnp[1].bn_bit))
140 			strcat(dest, "|");
141 	}
142 	strcat(dest, ">");
143 	return dest;
144 }
145 
146 /*
147  * first read ~/.smbrc, next try to merge SMB_CFG_FILE
148  */
149 int
150 smb_open_rcfile(void)
151 {
152 	char *home, *fn;
153 	int error;
154 
155 	home = getenv("HOME");
156 	if (home) {
157 		fn = malloc(strlen(home) + 20);
158 		sprintf(fn, "%s/.nsmbrc", home);
159 		error = rc_open(fn, "r", &smb_rc);
160 		free(fn);
161 	}
162 	error = rc_merge(SMB_CFG_FILE, &smb_rc);
163 	if (smb_rc == NULL) {
164 		printf("Warning: no cfg file(s) found.\n");
165 		return ENOENT;
166 	}
167 	return 0;
168 }
169 
170 void *
171 smb_dumptree(void)
172 {
173 	size_t len;
174 	void *p;
175 	int error;
176 
177 	error = sysctlbyname("net.smb.treedump", NULL, &len, NULL, 0);
178 	if (error)
179 		return NULL;
180 	p = malloc(len);
181 	if (p == NULL)
182 		return NULL;
183 	error = sysctlbyname("net.smb.treedump", p, &len, NULL, 0);
184 	if (error) {
185 		free(p);
186 		return NULL;
187 	}
188 	return p;
189 }
190 
191 void
192 smb_simplecrypt(char *dst, const char *src)
193 {
194 	int ch, pos;
195 
196 	*dst++ = '$';
197 	*dst++ = '$';
198 	*dst++ = '1';
199 	pos = 27;
200 	while (*src) {
201 		ch = *src++;
202 		if (isascii(ch))
203 		    ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) :
204 			  islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch);
205 		ch ^= pos;
206 		pos += 13;
207 		sprintf(dst, "%02x", ch);
208 		dst += 2;
209 	}
210 	*dst = 0;
211 }
212 
213 int
214 smb_simpledecrypt(char *dst, const char *src)
215 {
216 	char *ep, hexval[3];
217 	int len, ch, pos;
218 
219 	if (strncmp(src, "$$1", 3) != 0)
220 		return EINVAL;
221 	src += 3;
222 	len = strlen(src);
223 	if (len & 1)
224 		return EINVAL;
225 	len /= 2;
226 	hexval[2] = 0;
227 	pos = 27;
228 	while (len--) {
229 		hexval[0] = *src++;
230 		hexval[1] = *src++;
231 		ch = strtoul(hexval, &ep, 16);
232 		if (*ep != 0)
233 			return EINVAL;
234 		ch ^= pos;
235 		pos += 13;
236 		if (isascii(ch))
237 		    ch = (isupper(ch) ? ('A' + (ch - 'A' + 13) % 26) :
238 			  islower(ch) ? ('a' + (ch - 'a' + 13) % 26) : ch);
239 		*dst++ = ch;
240 	}
241 	*dst = 0;
242 	return 0;
243 }
244