xref: /freebsd/sys/netsmb/smb_subr.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2000-2001 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/lock.h>
37 #include <sys/sysctl.h>
38 #include <sys/socket.h>
39 #include <sys/signalvar.h>
40 #include <sys/mbuf.h>
41 
42 #include <sys/iconv.h>
43 
44 #include <netsmb/smb.h>
45 #include <netsmb/smb_conn.h>
46 #include <netsmb/smb_rq.h>
47 #include <netsmb/smb_subr.h>
48 
49 static MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
50 static MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
51 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
52 
53 smb_unichar smb_unieol = 0;
54 
55 void
56 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
57 {
58 	if (td) {
59 		scred->scr_td = td;
60 		scred->scr_cred = cred ? cred : td->td_ucred;
61 	} else {
62 		scred->scr_td = NULL;
63 		scred->scr_cred = cred ? cred : NULL;
64 	}
65 }
66 
67 int
68 smb_td_intr(struct thread *td)
69 {
70 	struct proc *p;
71 	sigset_t tmpset;
72 
73 	if (td == NULL)
74 		return 0;
75 
76 	p = td->td_proc;
77 	PROC_LOCK(p);
78 	tmpset = p->p_siglist;
79 	SIGSETOR(tmpset, td->td_siglist);
80 	SIGSETNAND(tmpset, td->td_sigmask);
81 	mtx_lock(&p->p_sigacts->ps_mtx);
82 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
83 	mtx_unlock(&p->p_sigacts->ps_mtx);
84 	if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) {
85 		PROC_UNLOCK(p);
86                 return EINTR;
87 	}
88 	PROC_UNLOCK(p);
89 	return 0;
90 }
91 
92 char *
93 smb_strdup(const char *s)
94 {
95 	char *p;
96 	size_t len;
97 
98 	len = s ? strlen(s) + 1 : 1;
99 	p = malloc(len, M_SMBSTR, M_WAITOK);
100 	if (s)
101 		bcopy(s, p, len);
102 	else
103 		*p = 0;
104 	return p;
105 }
106 
107 /*
108  * duplicate string from a user space.
109  */
110 char *
111 smb_strdupin(char *s, size_t maxlen)
112 {
113 	char *p, bt;
114 	int error;
115 	size_t len;
116 
117 	len = 0;
118 	for (p = s; ;p++) {
119 		if (copyin(p, &bt, 1))
120 			return NULL;
121 		len++;
122 		if (maxlen && len > maxlen)
123 			return NULL;
124 		if (bt == 0)
125 			break;
126 	}
127 	p = malloc(len, M_SMBSTR, M_WAITOK);
128 	error = copyin(s, p, len);
129 	if (error) {
130 		free(p, M_SMBSTR);
131 		return (NULL);
132 	}
133 	return p;
134 }
135 
136 /*
137  * duplicate memory block from a user space.
138  */
139 void *
140 smb_memdupin(void *umem, size_t len)
141 {
142 	char *p;
143 
144 	if (len > 8 * 1024)
145 		return NULL;
146 	p = malloc(len, M_SMBSTR, M_WAITOK);
147 	if (copyin(umem, p, len) == 0)
148 		return p;
149 	free(p, M_SMBSTR);
150 	return NULL;
151 }
152 
153 /*
154  * duplicate memory block in the kernel space.
155  */
156 void *
157 smb_memdup(const void *umem, int len)
158 {
159 	char *p;
160 
161 	if (len > 8 * 1024)
162 		return NULL;
163 	p = malloc(len, M_SMBSTR, M_WAITOK);
164 	if (p == NULL)
165 		return NULL;
166 	bcopy(umem, p, len);
167 	return p;
168 }
169 
170 void
171 smb_strfree(char *s)
172 {
173 	free(s, M_SMBSTR);
174 }
175 
176 void
177 smb_memfree(void *s)
178 {
179 	free(s, M_SMBSTR);
180 }
181 
182 void *
183 smb_zmalloc(size_t size, struct malloc_type *type, int flags)
184 {
185 
186 	return malloc(size, type, flags | M_ZERO);
187 }
188 
189 void
190 smb_strtouni(u_int16_t *dst, const char *src)
191 {
192 	while (*src) {
193 		*dst++ = htole16(*src++);
194 	}
195 	*dst = 0;
196 }
197 
198 #ifdef SMB_SOCKETDATA_DEBUG
199 void
200 m_dumpm(struct mbuf *m) {
201 	char *p;
202 	size_t len;
203 	printf("d=");
204 	while(m) {
205 		p=mtod(m,char *);
206 		len=m->m_len;
207 		printf("(%zu)",len);
208 		while(len--){
209 			printf("%02x ",((int)*(p++)) & 0xff);
210 		}
211 		m=m->m_next;
212 	}
213 	printf("\n");
214 }
215 #endif
216 
217 int
218 smb_maperror(int eclass, int eno)
219 {
220 	if (eclass == 0 && eno == 0)
221 		return 0;
222 	switch (eclass) {
223 	    case ERRDOS:
224 		switch (eno) {
225 		    case ERRbadfunc:
226 		    case ERRbadmcb:
227 		    case ERRbadenv:
228 		    case ERRbadformat:
229 		    case ERRrmuns:
230 			return EINVAL;
231 		    case ERRbadfile:
232 		    case ERRbadpath:
233 		    case ERRremcd:
234 		    case 66:		/* nt returns it when share not available */
235 		    case 67:		/* observed from nt4sp6 when sharename wrong */
236 			return ENOENT;
237 		    case ERRnofids:
238 			return EMFILE;
239 		    case ERRnoaccess:
240 		    case ERRbadshare:
241 			return EACCES;
242 		    case ERRbadfid:
243 			return EBADF;
244 		    case ERRnomem:
245 			return ENOMEM;	/* actually remote no mem... */
246 		    case ERRbadmem:
247 			return EFAULT;
248 		    case ERRbadaccess:
249 			return EACCES;
250 		    case ERRbaddata:
251 			return E2BIG;
252 		    case ERRbaddrive:
253 		    case ERRnotready:	/* nt */
254 			return ENXIO;
255 		    case ERRdiffdevice:
256 			return EXDEV;
257 		    case ERRnofiles:
258 			return 0;	/* eeof ? */
259 			return ETXTBSY;
260 		    case ERRlock:
261 			return EDEADLK;
262 		    case ERRfilexists:
263 			return EEXIST;
264 		    case 123:		/* dunno what is it, but samba maps as noent */
265 			return ENOENT;
266 		    case 145:		/* samba */
267 			return ENOTEMPTY;
268 		    case ERRnotlocked:
269 			return 0;	/* file become unlocked */
270 		    case 183:
271 			return EEXIST;
272 		    case ERRquota:
273 			return EDQUOT;
274 		}
275 		break;
276 	    case ERRSRV:
277 		switch (eno) {
278 		    case ERRerror:
279 			return EINVAL;
280 		    case ERRbadpw:
281 		    case ERRpasswordExpired:
282 			return EAUTH;
283 		    case ERRaccess:
284 			return EACCES;
285 		    case ERRinvnid:
286 			return ENETRESET;
287 		    case ERRinvnetname:
288 			SMBERROR("NetBIOS name is invalid\n");
289 			return EAUTH;
290 		    case 3:		/* reserved and returned */
291 			return EIO;
292 		    case ERRaccountExpired:
293 		    case ERRbadClient:
294 		    case ERRbadLogonTime:
295 			return EPERM;
296 		    case ERRnosupport:
297 			return EBADRPC;
298 		}
299 		break;
300 	    case ERRHRD:
301 		switch (eno) {
302 		    case ERRnowrite:
303 			return EROFS;
304 		    case ERRbadunit:
305 			return ENODEV;
306 		    case ERRnotready:
307 		    case ERRbadcmd:
308 		    case ERRdata:
309 			return EIO;
310 		    case ERRbadreq:
311 			return EBADRPC;
312 		    case ERRbadshare:
313 			return ETXTBSY;
314 		    case ERRlock:
315 			return EDEADLK;
316 		}
317 		break;
318 	}
319 	SMBERROR("Unmapped error %d:%d\n", eclass, eno);
320 	return EBADRPC;
321 }
322 
323 static int
324 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
325     size_t *srclen, size_t *dstlen)
326 {
327 	int error;
328 	size_t inlen = *srclen, outlen = *dstlen;
329 
330 	error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
331 	    &dst, &outlen);
332 	if (inlen != *srclen || outlen != *dstlen) {
333 		*srclen -= inlen;
334 		*dstlen -= outlen;
335 		return 0;
336 	} else
337 		return error;
338 }
339 
340 int
341 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
342 	size_t size, int caseopt)
343 {
344 	struct iconv_drv *dp = vcp->vc_toserver;
345 
346 	if (size == 0)
347 		return 0;
348 	if (dp == NULL) {
349 		return mb_put_mem(mbp, src, size, MB_MSYSTEM);
350 	}
351 	mbp->mb_copy = smb_copy_iconv;
352 	mbp->mb_udata = dp;
353 	if (SMB_UNICODE_STRINGS(vcp))
354 		mb_put_padbyte(mbp);
355 	return mb_put_mem(mbp, src, size, MB_MCUSTOM);
356 }
357 
358 int
359 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
360 	int caseopt)
361 {
362 	int error;
363 
364 	error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
365 	if (error)
366 		return error;
367 	if (SMB_UNICODE_STRINGS(vcp))
368 		return mb_put_uint16le(mbp, 0);
369 	return mb_put_uint8(mbp, 0);
370 }
371 
372 int
373 smb_put_asunistring(struct smb_rq *rqp, const char *src)
374 {
375 	struct mbchain *mbp = &rqp->sr_rq;
376 	struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
377 	u_char c;
378 	int error;
379 
380 	while (*src) {
381 		iconv_convmem(dp, &c, src++, 1);
382 		error = mb_put_uint16le(mbp, c);
383 		if (error)
384 			return error;
385 	}
386 	return mb_put_uint16le(mbp, 0);
387 }
388