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