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