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$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/proc.h>
44 #include <sys/lock.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sysctl.h>
47 #include <sys/socket.h>
48 #include <sys/signalvar.h>
49 #include <sys/wait.h>
50 #include <sys/unistd.h>
51
52 #include <machine/stdarg.h>
53
54 #include <sys/iconv.h>
55
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_rq.h>
59 #include <netsmb/smb_subr.h>
60
61 MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
62 MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
63 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
64
65 smb_unichar smb_unieol = 0;
66
67 void
68 smb_makescred(struct smb_cred *scred, struct proc *p, struct ucred *cred)
69 {
70 if (p) {
71 scred->scr_p = p;
72 scred->scr_cred = cred ? cred : p->p_ucred;
73 } else {
74 scred->scr_p = NULL;
75 scred->scr_cred = cred ? cred : NULL;
76 }
77 }
78
79 int
80 smb_proc_intr(struct proc *p)
81 {
82 sigset_t tmpset;
83
84 if (p == NULL)
85 return 0;
86 tmpset = p->p_siglist;
87 SIGSETNAND(tmpset, p->p_sigmask);
88 SIGSETNAND(tmpset, p->p_sigignore);
89 if (SIGNOTEMPTY(p->p_siglist) && SMB_SIGMASK(tmpset))
90 return EINTR;
91 return 0;
92 }
93
94 char *
95 smb_strdup(const char *s)
96 {
97 char *p;
98 int 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, int maxlen)
114 {
115 char *p, bt;
116 int len = 0;
117
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 copyin(s, p, len);
129 return p;
130 }
131
132 /*
133 * duplicate memory block from a user space.
134 */
135 void *
136 smb_memdupin(void *umem, int len)
137 {
138 char *p;
139
140 if (len > 8 * 1024)
141 return NULL;
142 p = malloc(len, M_SMBSTR, M_WAITOK);
143 if (copyin(umem, p, len) == 0)
144 return p;
145 free(p, M_SMBSTR);
146 return NULL;
147 }
148
149 /*
150 * duplicate memory block in the kernel space.
151 */
152 void *
153 smb_memdup(const void *umem, int len)
154 {
155 char *p;
156
157 if (len > 8 * 1024)
158 return NULL;
159 p = malloc(len, M_SMBSTR, M_WAITOK);
160 if (p == NULL)
161 return NULL;
162 bcopy(umem, p, len);
163 return p;
164 }
165
166 void
167 smb_strfree(char *s)
168 {
169 free(s, M_SMBSTR);
170 }
171
172 void
173 smb_memfree(void *s)
174 {
175 free(s, M_SMBSTR);
176 }
177
178 void *
179 smb_zmalloc(unsigned long size, struct malloc_type *type, int flags)
180 {
181
182 return malloc(size, type, flags | M_ZERO);
183 }
184
185 void
186 smb_strtouni(u_int16_t *dst, const char *src)
187 {
188 while (*src) {
189 *dst++ = htole16(*src++);
190 }
191 *dst = 0;
192 }
193
194 #ifdef SMB_SOCKETDATA_DEBUG
195 void
196 m_dumpm(struct mbuf *m) {
197 char *p;
198 int len;
199 printf("d=");
200 while(m) {
201 p=mtod(m,char *);
202 len=m->m_len;
203 printf("(%d)",len);
204 while(len--){
205 printf("%02x ",((int)*(p++)) & 0xff);
206 }
207 m=m->m_next;
208 };
209 printf("\n");
210 }
211 #endif
212
213 int
214 smb_maperror(int eclass, int eno)
215 {
216 if (eclass == 0 && eno == 0)
217 return 0;
218 switch (eclass) {
219 case ERRDOS:
220 switch (eno) {
221 case ERRbadfunc:
222 case ERRbadmcb:
223 case ERRbadenv:
224 case ERRbadformat:
225 case ERRrmuns:
226 return EINVAL;
227 case ERRbadfile:
228 case ERRbadpath:
229 case ERRremcd:
230 case 66: /* nt returns it when share not available */
231 case 67: /* observed from nt4sp6 when sharename wrong */
232 return ENOENT;
233 case ERRnofids:
234 return EMFILE;
235 case ERRnoaccess:
236 case ERRbadshare:
237 return EACCES;
238 case ERRbadfid:
239 return EBADF;
240 case ERRnomem:
241 return ENOMEM; /* actually remote no mem... */
242 case ERRbadmem:
243 return EFAULT;
244 case ERRbadaccess:
245 return EACCES;
246 case ERRbaddata:
247 return E2BIG;
248 case ERRbaddrive:
249 case ERRnotready: /* nt */
250 return ENXIO;
251 case ERRdiffdevice:
252 return EXDEV;
253 case ERRnofiles:
254 return 0; /* eeof ? */
255 return ETXTBSY;
256 case ERRlock:
257 return EDEADLK;
258 case ERRfilexists:
259 return EEXIST;
260 case 123: /* dunno what is it, but samba maps as noent */
261 return ENOENT;
262 case 145: /* samba */
263 return ENOTEMPTY;
264 case 183:
265 return EEXIST;
266 case ERRquota:
267 return EDQUOT;
268 }
269 break;
270 case ERRSRV:
271 switch (eno) {
272 case ERRerror:
273 return EINVAL;
274 case ERRbadpw:
275 case ERRpasswordExpired:
276 return EAUTH;
277 case ERRaccess:
278 return EACCES;
279 case ERRinvnid:
280 return ENETRESET;
281 case ERRinvnetname:
282 SMBERROR("NetBIOS name is invalid\n");
283 return EAUTH;
284 case 3: /* reserved and returned */
285 return EIO;
286 case ERRaccountExpired:
287 case ERRbadClient:
288 case ERRbadLogonTime:
289 return EPERM;
290 case ERRnosupport:
291 return EBADRPC;
292 }
293 break;
294 case ERRHRD:
295 switch (eno) {
296 case ERRnowrite:
297 return EROFS;
298 case ERRbadunit:
299 return ENODEV;
300 case ERRnotready:
301 case ERRbadcmd:
302 case ERRdata:
303 return EIO;
304 case ERRbadreq:
305 return EBADRPC;
306 case ERRbadshare:
307 return ETXTBSY;
308 case ERRlock:
309 return EDEADLK;
310 }
311 break;
312 }
313 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
314 return EBADRPC;
315 }
316
317 static int
318 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, int len)
319 {
320 int outlen = len;
321
322 return iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &len, &dst, &outlen);
323 }
324
325 int
326 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
327 int size, int caseopt)
328 {
329 struct iconv_drv *dp = vcp->vc_toserver;
330
331 if (size == 0)
332 return 0;
333 if (dp == NULL) {
334 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
335 }
336 mbp->mb_copy = smb_copy_iconv;
337 mbp->mb_udata = dp;
338 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
339 }
340
341 int
342 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
343 int caseopt)
344 {
345 int error;
346
347 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
348 if (error)
349 return error;
350 return mb_put_uint8(mbp, 0);
351 }
352
353 int
354 smb_put_asunistring(struct smb_rq *rqp, const char *src)
355 {
356 struct mbchain *mbp = &rqp->sr_rq;
357 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
358 u_char c;
359 int error;
360
361 while (*src) {
362 iconv_convmem(dp, &c, src++, 1);
363 error = mb_put_uint16le(mbp, c);
364 if (error)
365 return error;
366 }
367 return mb_put_uint16le(mbp, 0);
368 }
369
370 int
371 smb_checksmp(void)
372 {
373 size_t olen, plen;
374 int name[2];
375 int ncpu, error;
376
377 name[0] = CTL_HW;
378 name[1] = HW_NCPU;
379 olen = sizeof(ncpu);
380 error = kernel_sysctl(curproc, name, 2, &ncpu, &olen, NULL, 0, &plen);
381 if (error)
382 return error;
383 #ifndef SMP
384 if (ncpu > 1) {
385 printf("error: module compiled without SMP support\n");
386 return EPERM;
387 }
388 #else
389 if (ncpu < 2) {
390 printf("warning: only one CPU active on in SMP kernel ?\n");
391 }
392 #endif
393 return 0;
394 }
395
396 /*
397 * Create a kernel process/thread/whatever. It shares it's address space
398 * with proc0 - ie: kernel only.
399 */
400 int
401 kthread_create2(void (*func)(void *), void *arg,
402 struct proc **newpp, int flags, const char *fmt, ...)
403 {
404 int error;
405 va_list ap;
406 struct proc *p2;
407
408 if (!proc0.p_stats || proc0.p_stats->p_start.tv_sec == 0) {
409 panic("kthread_create called too soon");
410 }
411
412 error = fork1(&proc0, RFMEM | RFFDG | RFPROC | flags, &p2);
413 if (error)
414 return error;
415
416 /* save a global descriptor, if desired */
417 if (newpp != NULL)
418 *newpp = p2;
419
420 /* this is a non-swapped system process */
421 p2->p_flag |= P_INMEM | P_SYSTEM;
422 p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
423 PHOLD(p2);
424
425 /* set up arg0 for 'ps', et al */
426 va_start(ap, fmt);
427 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
428 va_end(ap);
429
430 /* call the processes' main()... */
431 cpu_set_fork_handler(p2, func, arg);
432
433 return 0;
434 }
435
436 int
437 msleep(void *chan, struct simplelock *mtx, int pri, const char *wmesg, int timo)
438 {
439 int error;
440
441 if (mtx)
442 simple_unlock(mtx);
443 error = tsleep(chan, pri, wmesg, timo);
444 if ((pri & PDROP) == 0 && mtx)
445 simple_lock(mtx);
446 return error;
447 }
Cache object: 7aea7f97e108ed363aba4247a9b7b256
|