1:1 Userland threading stage 2.20/4:
[dragonfly.git] / sys / netproto / smb / smb_subr.c
blobd45dc010ae28d22f6113cdf80c4eda00f72f40bd
1 /*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
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.
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.
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.27 2007/02/21 15:46:48 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>
40 #include <sys/mbuf.h>
41 #include <sys/proc.h>
42 #include <sys/lock.h>
43 #include <sys/resourcevar.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46 #include <sys/signalvar.h>
47 #include <sys/wait.h>
48 #include <sys/unistd.h>
50 #include <machine/stdarg.h>
52 #include <sys/iconv.h>
54 #include "smb.h"
55 #include "smb_conn.h"
56 #include "smb_rq.h"
57 #include "smb_subr.h"
59 MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
60 MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
61 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
63 smb_unichar smb_unieol = 0;
65 void
66 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred)
68 scred->scr_td = td;
69 if (td && td->td_proc) {
70 scred->scr_cred = cred ? cred : td->td_proc->p_ucred;
71 } else {
72 scred->scr_cred = cred ? cred : NULL;
76 int
77 smb_proc_intr(struct thread *td)
79 sigset_t tmpset;
80 struct proc *p;
81 struct lwp *lp;
83 if (td == NULL || (p = td->td_proc) == NULL)
84 return 0;
85 lp = td->td_lwp;
86 tmpset = lwp_sigpend(lp);
87 SIGSETNAND(tmpset, lp->lwp_sigmask);
88 SIGSETNAND(tmpset, p->p_sigignore);
89 if (SIGNOTEMPTY(tmpset) && SMB_SIGMASK(tmpset))
90 return EINTR;
91 return 0;
94 char *
95 smb_strdup(const char *s)
97 char *p;
98 int len;
100 len = s ? strlen(s) + 1 : 1;
101 p = kmalloc(len, M_SMBSTR, M_WAITOK);
102 if (s)
103 bcopy(s, p, len);
104 else
105 *p = 0;
106 return p;
110 * duplicate string from a user space.
112 char *
113 smb_strdupin(char *s, int maxlen)
115 char *p, bt;
116 int len = 0;
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;
127 p = kmalloc(len, M_SMBSTR, M_WAITOK);
128 copyin(s, p, len);
129 return p;
133 * duplicate memory block from a user space.
135 void *
136 smb_memdupin(void *umem, int len)
138 char *p;
140 if (len > 8 * 1024)
141 return NULL;
142 p = kmalloc(len, M_SMBSTR, M_WAITOK);
143 if (copyin(umem, p, len) == 0)
144 return p;
145 kfree(p, M_SMBSTR);
146 return NULL;
150 * duplicate memory block in the kernel space.
152 void *
153 smb_memdup(const void *umem, int len)
155 char *p;
157 if (len > 8 * 1024)
158 return NULL;
159 p = kmalloc(len, M_SMBSTR, M_WAITOK);
160 if (p == NULL)
161 return NULL;
162 bcopy(umem, p, len);
163 return p;
166 void
167 smb_strfree(char *s)
169 kfree(s, M_SMBSTR);
172 void
173 smb_memfree(void *s)
175 kfree(s, M_SMBSTR);
178 void *
179 smb_zmalloc(unsigned long size, struct malloc_type *type, int flags)
182 return kmalloc(size, type, flags | M_ZERO);
185 void
186 smb_strtouni(u_int16_t *dst, const char *src)
188 while (*src) {
189 *dst++ = htoles(*src++);
191 *dst = 0;
194 #ifdef SMB_SOCKETDATA_DEBUG
195 void
196 m_dumpm(struct mbuf *m) {
197 char *p;
198 int len;
199 kprintf("d=");
200 while(m) {
201 p=mtod(m,char *);
202 len=m->m_len;
203 kprintf("(%d)",len);
204 while(len--){
205 kprintf("%02x ",((int)*(p++)) & 0xff);
207 m=m->m_next;
209 kprintf("\n");
211 #endif
214 smb_maperror(int eclass, int eno)
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;
267 break;
268 case ERRSRV:
269 switch (eno) {
270 case ERRerror:
271 return EINVAL;
272 case ERRbadpw:
273 return EAUTH;
274 case ERRaccess:
275 return EACCES;
276 case ERRinvnid:
277 return ENETRESET;
278 case ERRinvnetname:
279 SMBERROR("NetBIOS name is invalid\n");
280 return EAUTH;
281 case 3: /* reserved and returned */
282 return EIO;
283 case 2239: /* NT: account exists but disabled */
284 return EPERM;
286 break;
287 case ERRHRD:
288 switch (eno) {
289 case ERRnowrite:
290 return EROFS;
291 case ERRbadunit:
292 return ENODEV;
293 case ERRnotready:
294 case ERRbadcmd:
295 case ERRdata:
296 return EIO;
297 case ERRbadreq:
298 return EBADRPC;
299 case ERRbadshare:
300 return ETXTBSY;
301 case ERRlock:
302 return EDEADLK;
304 break;
306 SMBERROR("Unmapped error %d:%d\n", eclass, eno);
307 return EBADRPC;
310 static int
311 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, int len)
313 int outlen = len;
315 return iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &len, &dst, &outlen);
319 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
320 int size, int caseopt)
322 struct iconv_drv *dp = vcp->vc_toserver;
324 if (size == 0)
325 return 0;
326 if (dp == NULL) {
327 return mb_put_mem(mbp, src, size, MB_MSYSTEM);
329 mbp->mb_copy = smb_copy_iconv;
330 mbp->mb_udata = dp;
331 return mb_put_mem(mbp, src, size, MB_MCUSTOM);
335 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
336 int caseopt)
338 int error;
340 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
341 if (error)
342 return error;
343 return mb_put_uint8(mbp, 0);
347 smb_put_asunistring(struct smb_rq *rqp, const char *src)
349 struct mbchain *mbp = &rqp->sr_rq;
350 struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
351 u_char c;
352 int error;
354 while (*src) {
355 iconv_convmem(dp, &c, src++, 1);
356 error = mb_put_uint16le(mbp, c);
357 if (error)
358 return error;
360 return mb_put_uint16le(mbp, 0);
364 * Create a kernel process/thread/whatever. It shares it's address space
365 * with proc0 - ie: kernel only.
367 * XXX only the SMB protocol uses this, we should convert this mess to a
368 * pure thread when possible.
371 kthread_create2(void (*func)(void *), void *arg,
372 struct proc **newpp, int flags, const char *fmt, ...)
374 int error;
375 __va_list ap;
376 struct proc *p2;
377 struct lwp *lp2;
379 error = fork1(&lwp0, RFMEM | RFFDG | RFPROC | flags, &p2);
380 if (error)
381 return error;
383 /* save a global descriptor, if desired */
384 if (newpp != NULL)
385 *newpp = p2;
387 /* this is a non-swapped system process */
388 p2->p_flag |= P_SYSTEM;
389 p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
391 lp2 = ONLY_LWP_IN_PROC(p2);
393 /* set up arg0 for 'ps', et al */
394 __va_start(ap, fmt);
395 kvsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
396 __va_end(ap);
398 /* call the processes' main()... */
399 cpu_set_fork_handler(lp2, func, arg);
400 start_forked_proc(&lwp0, p2);
402 return 0;
405 void
406 kthread_exit2(void)
408 exit1(0);
412 * smb_sleep() icky compat routine. Leave the token held through the tsleep
413 * to interlock against the sleep. Remember that the token could be lost
414 * since we blocked, so reget or release as appropriate.
417 smb_sleep(void *chan, struct smb_slock *sl, int slpflags, const char *wmesg, int timo)
419 int error;
421 if (sl) {
422 crit_enter();
423 tsleep_interlock(chan);
424 smb_sl_unlock(sl);
425 error = tsleep(chan, slpflags, wmesg, timo);
426 if ((slpflags & PDROP) == 0)
427 smb_sl_lock(sl);
428 crit_exit();
429 } else {
430 error = tsleep(chan, slpflags, wmesg, timo);
432 return error;