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