2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/sys/netsmb/smb_subr.c,v 1.1.2.2 2001/09/03 08:55:11 bp Exp $
33 * $DragonFly: src/sys/netproto/smb/smb_subr.c,v 1.28 2007/02/25 23:17:13 corecode Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/malloc.h>
43 #include <sys/resourcevar.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46 #include <sys/signalvar.h>
47 #include <sys/signal2.h>
49 #include <sys/unistd.h>
51 #include <machine/stdarg.h>
53 #include <sys/iconv.h>
60 MALLOC_DEFINE(M_SMBDATA
, "SMBDATA", "Misc netsmb data");
61 MALLOC_DEFINE(M_SMBSTR
, "SMBSTR", "netsmb string data");
62 MALLOC_DEFINE(M_SMBTEMP
, "SMBTEMP", "Temp netsmb data");
64 smb_unichar smb_unieol
= 0;
67 smb_makescred(struct smb_cred
*scred
, struct thread
*td
, struct ucred
*cred
)
70 if (td
&& td
->td_proc
) {
71 scred
->scr_cred
= cred
? cred
: td
->td_proc
->p_ucred
;
73 scred
->scr_cred
= cred
? cred
: NULL
;
78 smb_proc_intr(struct thread
*td
)
84 if (td
== NULL
|| (p
= td
->td_proc
) == NULL
)
87 tmpset
= lwp_sigpend(lp
);
88 SIGSETNAND(tmpset
, lp
->lwp_sigmask
);
89 SIGSETNAND(tmpset
, p
->p_sigignore
);
90 if (SIGNOTEMPTY(tmpset
) && SMB_SIGMASK(tmpset
))
96 smb_strdup(const char *s
)
101 len
= s
? strlen(s
) + 1 : 1;
102 p
= kmalloc(len
, M_SMBSTR
, M_WAITOK
);
111 * duplicate string from a user space.
114 smb_strdupin(char *s
, int maxlen
)
120 if (copyin(p
, &bt
, 1))
123 if (maxlen
&& len
> maxlen
)
128 p
= kmalloc(len
, M_SMBSTR
, M_WAITOK
);
134 * duplicate memory block from a user space.
137 smb_memdupin(void *umem
, int len
)
143 p
= kmalloc(len
, M_SMBSTR
, M_WAITOK
);
144 if (copyin(umem
, p
, len
) == 0)
151 * duplicate memory block in the kernel space.
154 smb_memdup(const void *umem
, int len
)
160 p
= kmalloc(len
, M_SMBSTR
, M_WAITOK
);
180 smb_zmalloc(unsigned long size
, struct malloc_type
*type
, int flags
)
183 return kmalloc(size
, type
, flags
| M_ZERO
);
187 smb_strtouni(u_int16_t
*dst
, const char *src
)
190 *dst
++ = htoles(*src
++);
195 #ifdef SMB_SOCKETDATA_DEBUG
197 m_dumpm(struct mbuf
*m
) {
206 kprintf("%02x ",((int)*(p
++)) & 0xff);
215 smb_maperror(int eclass
, int eno
)
217 if (eclass
== 0 && eno
== 0)
231 case 66: /* nt returns it when share not available */
232 case 67: /* observed from nt4sp6 when sharename wrong */
242 return ENOMEM
; /* actually remote no mem... */
250 case ERRnotready
: /* nt */
255 return 0; /* eeof ? */
261 case 123: /* dunno what is it, but samba maps as noent */
263 case 145: /* samba */
280 SMBERROR("NetBIOS name is invalid\n");
282 case 3: /* reserved and returned */
284 case 2239: /* NT: account exists but disabled */
307 SMBERROR("Unmapped error %d:%d\n", eclass
, eno
);
312 smb_copy_iconv(struct mbchain
*mbp
, c_caddr_t src
, caddr_t dst
, int len
)
316 return iconv_conv((struct iconv_drv
*)mbp
->mb_udata
, &src
, &len
, &dst
, &outlen
);
320 smb_put_dmem(struct mbchain
*mbp
, struct smb_vc
*vcp
, const char *src
,
321 int size
, int caseopt
)
323 struct iconv_drv
*dp
= vcp
->vc_toserver
;
328 return mb_put_mem(mbp
, src
, size
, MB_MSYSTEM
);
330 mbp
->mb_copy
= smb_copy_iconv
;
332 return mb_put_mem(mbp
, src
, size
, MB_MCUSTOM
);
336 smb_put_dstring(struct mbchain
*mbp
, struct smb_vc
*vcp
, const char *src
,
341 error
= smb_put_dmem(mbp
, vcp
, src
, strlen(src
), caseopt
);
344 return mb_put_uint8(mbp
, 0);
348 smb_put_asunistring(struct smb_rq
*rqp
, const char *src
)
350 struct mbchain
*mbp
= &rqp
->sr_rq
;
351 struct iconv_drv
*dp
= rqp
->sr_vc
->vc_toserver
;
356 iconv_convmem(dp
, &c
, src
++, 1);
357 error
= mb_put_uint16le(mbp
, c
);
361 return mb_put_uint16le(mbp
, 0);
365 * Create a kernel process/thread/whatever. It shares it's address space
366 * with proc0 - ie: kernel only.
368 * XXX only the SMB protocol uses this, we should convert this mess to a
369 * pure thread when possible.
372 kthread_create2(void (*func
)(void *), void *arg
,
373 struct proc
**newpp
, int flags
, const char *fmt
, ...)
380 error
= fork1(&lwp0
, RFMEM
| RFFDG
| RFPROC
| flags
, &p2
);
384 /* save a global descriptor, if desired */
388 /* this is a non-swapped system process */
389 p2
->p_flag
|= P_SYSTEM
;
390 p2
->p_sigacts
->ps_flag
|= PS_NOCLDWAIT
;
392 lp2
= ONLY_LWP_IN_PROC(p2
);
394 /* set up arg0 for 'ps', et al */
396 kvsnprintf(p2
->p_comm
, sizeof(p2
->p_comm
), fmt
, ap
);
399 /* call the processes' main()... */
400 cpu_set_fork_handler(lp2
, func
, arg
);
401 start_forked_proc(&lwp0
, p2
);
413 * smb_sleep() icky compat routine. Leave the token held through the tsleep
414 * to interlock against the sleep. Remember that the token could be lost
415 * since we blocked, so reget or release as appropriate.
418 smb_sleep(void *chan
, struct smb_slock
*sl
, int slpflags
, const char *wmesg
, int timo
)
424 tsleep_interlock(chan
);
426 error
= tsleep(chan
, slpflags
, wmesg
, timo
);
427 if ((slpflags
& PDROP
) == 0)
431 error
= tsleep(chan
, slpflags
, wmesg
, timo
);