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