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
smb_makescred(struct smb_cred * scred,struct thread * td,struct ucred * cred)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
smb_td_intr(struct thread * td)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 *
smb_strdup(const char * s)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 *
smb_strdupin(char * s,size_t maxlen)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 *
smb_memdupin(void * umem,size_t len)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 *
smb_memdup(const void * umem,int len)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 bcopy(umem, p, len);
153 return p;
154 }
155
156 void
smb_strfree(char * s)157 smb_strfree(char *s)
158 {
159 free(s, M_SMBSTR);
160 }
161
162 void
smb_memfree(void * s)163 smb_memfree(void *s)
164 {
165 free(s, M_SMBSTR);
166 }
167
168 void *
smb_zmalloc(size_t size,struct malloc_type * type,int flags)169 smb_zmalloc(size_t size, struct malloc_type *type, int flags)
170 {
171
172 return malloc(size, type, flags | M_ZERO);
173 }
174
175 void
smb_strtouni(u_int16_t * dst,const char * src)176 smb_strtouni(u_int16_t *dst, const char *src)
177 {
178 while (*src) {
179 *dst++ = htole16(*src++);
180 }
181 *dst = 0;
182 }
183
184 #ifdef SMB_SOCKETDATA_DEBUG
185 void
m_dumpm(struct mbuf * m)186 m_dumpm(struct mbuf *m) {
187 char *p;
188 size_t len;
189 printf("d=");
190 while(m) {
191 p=mtod(m,char *);
192 len=m->m_len;
193 printf("(%zu)",len);
194 while(len--){
195 printf("%02x ",((int)*(p++)) & 0xff);
196 }
197 m=m->m_next;
198 }
199 printf("\n");
200 }
201 #endif
202
203 int
smb_maperror(int eclass,int eno)204 smb_maperror(int eclass, int eno)
205 {
206 if (eclass == 0 && eno == 0)
207 return 0;
208 switch (eclass) {
209 case ERRDOS:
210 switch (eno) {
211 case ERRbadfunc:
212 case ERRbadmcb:
213 case ERRbadenv:
214 case ERRbadformat:
215 case ERRrmuns:
216 return EINVAL;
217 case ERRbadfile:
218 case ERRbadpath:
219 case ERRremcd:
220 case 66: /* nt returns it when share not available */
221 case 67: /* observed from nt4sp6 when sharename wrong */
222 return ENOENT;
223 case ERRnofids:
224 return EMFILE;
225 case ERRnoaccess:
226 case ERRbadshare:
227 return EACCES;
228 case ERRbadfid:
229 return EBADF;
230 case ERRnomem:
231 return ENOMEM; /* actually remote no mem... */
232 case ERRbadmem:
233 return EFAULT;
234 case ERRbadaccess:
235 return EACCES;
236 case ERRbaddata:
237 return E2BIG;
238 case ERRbaddrive:
239 case ERRnotready: /* nt */
240 return ENXIO;
241 case ERRdiffdevice:
242 return EXDEV;
243 case ERRnofiles:
244 return 0; /* eeof ? */
245 return ETXTBSY;
246 case ERRlock:
247 return EDEADLK;
248 case ERRfilexists:
249 return EEXIST;
250 case 123: /* dunno what is it, but samba maps as noent */
251 return ENOENT;
252 case 145: /* samba */
253 return ENOTEMPTY;
254 case ERRnotlocked:
255 return 0; /* file become unlocked */
256 case 183:
257 return EEXIST;
258 case ERRquota:
259 return EDQUOT;
260 }
261 break;
262 case ERRSRV:
263 switch (eno) {
264 case ERRerror:
265 return EINVAL;
266 case ERRbadpw:
267 case ERRpasswordExpired:
268 return EAUTH;
269 case ERRaccess:
270 return EACCES;
271 case ERRinvnid:
272 return ENETRESET;
273 case ERRinvnetname:
274 SMBERROR("NetBIOS name is invalid\n");
275 return EAUTH;
276 case 3: /* reserved and returned */
277 return EIO;
278 case ERRaccountExpired:
279 case ERRbadClient:
280 case ERRbadLogonTime:
281 return EPERM;
282 case ERRnosupport:
283 return EBADRPC;
284 }
285 break;
286 case ERRHRD:
287 switch (eno) {
288 case ERRnowrite:
289 return EROFS;
290 case ERRbadunit:
291 return ENODEV;
292 case ERRnotready:
293 case ERRbadcmd:
294 case ERRdata:
295 return EIO;
296 case ERRbadreq:
297 return EBADRPC;
298 case ERRbadshare:
299 return ETXTBSY;
300 case ERRlock:
301 return EDEADLK;
302 }
303 break;
304 }
305 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
306 return EBADRPC;
307 }
308
309 static int
smb_copy_iconv(struct mbchain * mbp,c_caddr_t src,caddr_t dst,size_t * srclen,size_t * dstlen)310 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst,
311 size_t *srclen, size_t *dstlen)
312 {
313 int error;
314 size_t inlen = *srclen, outlen = *dstlen;
315
316 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen,
317 &dst, &outlen);
318 if (inlen != *srclen || outlen != *dstlen) {
319 *srclen -= inlen;
320 *dstlen -= outlen;
321 return 0;
322 } else
323 return error;
324 }
325
326 int
smb_put_dmem(struct mbchain * mbp,struct smb_vc * vcp,const char * src,size_t size,int caseopt)327 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
328 size_t size, int caseopt)
329 {
330 struct iconv_drv *dp = vcp->vc_toserver;
331
332 if (size == 0)
333 return 0;
334 if (dp == NULL) {
335 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
336 }
337 mbp->mb_copy = smb_copy_iconv;
338 mbp->mb_udata = dp;
339 if (SMB_UNICODE_STRINGS(vcp))
340 mb_put_padbyte(mbp);
341 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
342 }
343
344 int
smb_put_dstring(struct mbchain * mbp,struct smb_vc * vcp,const char * src,int caseopt)345 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
346 int caseopt)
347 {
348 int error;
349
350 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
351 if (error)
352 return error;
353 if (SMB_UNICODE_STRINGS(vcp))
354 return mb_put_uint16le(mbp, 0);
355 return mb_put_uint8(mbp, 0);
356 }
357
358 int
smb_put_asunistring(struct smb_rq * rqp,const char * src)359 smb_put_asunistring(struct smb_rq *rqp, const char *src)
360 {
361 struct mbchain *mbp = &rqp->sr_rq;
362 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
363 u_char c;
364 int error;
365
366 while (*src) {
367 iconv_convmem(dp, &c, src++, 1);
368 error = mb_put_uint16le(mbp, c);
369 if (error)
370 return error;
371 }
372 return mb_put_uint16le(mbp, 0);
373 }
374