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