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/malloc.h> 41 #include <sys/proc.h> 42 #include <sys/lock.h> 43 #include <sys/sysctl.h> 44 #include <sys/socket.h> 45 #include <sys/signalvar.h> 46 #include <sys/mbuf.h> 47 48 #include <sys/iconv.h> 49 50 #include <netsmb/smb.h> 51 #include <netsmb/smb_conn.h> 52 #include <netsmb/smb_rq.h> 53 #include <netsmb/smb_subr.h> 54 55 MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data"); 56 MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data"); 57 MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data"); 58 59 smb_unichar smb_unieol = 0; 60 61 void 62 smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred) 63 { 64 if (td) { 65 scred->scr_td = td; 66 scred->scr_cred = cred ? cred : td->td_ucred; 67 } else { 68 scred->scr_td = NULL; 69 scred->scr_cred = cred ? cred : NULL; 70 } 71 } 72 73 int 74 smb_td_intr(struct thread *td) 75 { 76 struct proc *p; 77 sigset_t tmpset; 78 79 if (td == NULL) 80 return 0; 81 82 p = td->td_proc; 83 PROC_LOCK(p); 84 tmpset = p->p_siglist; 85 SIGSETOR(tmpset, td->td_siglist); 86 SIGSETNAND(tmpset, td->td_sigmask); 87 mtx_lock(&p->p_sigacts->ps_mtx); 88 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore); 89 mtx_unlock(&p->p_sigacts->ps_mtx); 90 if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) { 91 PROC_UNLOCK(p); 92 return EINTR; 93 } 94 PROC_UNLOCK(p); 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 = malloc(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 error, 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 = malloc(len, M_SMBSTR, M_WAITOK); 132 error = copyin(s, p, len); 133 if (error) { 134 free(p, M_SMBSTR); 135 return (NULL); 136 } 137 return p; 138 } 139 140 /* 141 * duplicate memory block from a user space. 142 */ 143 void * 144 smb_memdupin(void *umem, int len) 145 { 146 char *p; 147 148 if (len > 8 * 1024) 149 return NULL; 150 p = malloc(len, M_SMBSTR, M_WAITOK); 151 if (copyin(umem, p, len) == 0) 152 return p; 153 free(p, M_SMBSTR); 154 return NULL; 155 } 156 157 /* 158 * duplicate memory block in the kernel space. 159 */ 160 void * 161 smb_memdup(const void *umem, int len) 162 { 163 char *p; 164 165 if (len > 8 * 1024) 166 return NULL; 167 p = malloc(len, M_SMBSTR, M_WAITOK); 168 if (p == NULL) 169 return NULL; 170 bcopy(umem, p, len); 171 return p; 172 } 173 174 void 175 smb_strfree(char *s) 176 { 177 free(s, M_SMBSTR); 178 } 179 180 void 181 smb_memfree(void *s) 182 { 183 free(s, M_SMBSTR); 184 } 185 186 void * 187 smb_zmalloc(unsigned long size, struct malloc_type *type, int flags) 188 { 189 190 return malloc(size, type, flags | M_ZERO); 191 } 192 193 void 194 smb_strtouni(u_int16_t *dst, const char *src) 195 { 196 while (*src) { 197 *dst++ = htole16(*src++); 198 } 199 *dst = 0; 200 } 201 202 #ifdef SMB_SOCKETDATA_DEBUG 203 void 204 m_dumpm(struct mbuf *m) { 205 char *p; 206 int len; 207 printf("d="); 208 while(m) { 209 p=mtod(m,char *); 210 len=m->m_len; 211 printf("(%d)",len); 212 while(len--){ 213 printf("%02x ",((int)*(p++)) & 0xff); 214 } 215 m=m->m_next; 216 }; 217 printf("\n"); 218 } 219 #endif 220 221 int 222 smb_maperror(int eclass, int eno) 223 { 224 if (eclass == 0 && eno == 0) 225 return 0; 226 switch (eclass) { 227 case ERRDOS: 228 switch (eno) { 229 case ERRbadfunc: 230 case ERRbadmcb: 231 case ERRbadenv: 232 case ERRbadformat: 233 case ERRrmuns: 234 return EINVAL; 235 case ERRbadfile: 236 case ERRbadpath: 237 case ERRremcd: 238 case 66: /* nt returns it when share not available */ 239 case 67: /* observed from nt4sp6 when sharename wrong */ 240 return ENOENT; 241 case ERRnofids: 242 return EMFILE; 243 case ERRnoaccess: 244 case ERRbadshare: 245 return EACCES; 246 case ERRbadfid: 247 return EBADF; 248 case ERRnomem: 249 return ENOMEM; /* actually remote no mem... */ 250 case ERRbadmem: 251 return EFAULT; 252 case ERRbadaccess: 253 return EACCES; 254 case ERRbaddata: 255 return E2BIG; 256 case ERRbaddrive: 257 case ERRnotready: /* nt */ 258 return ENXIO; 259 case ERRdiffdevice: 260 return EXDEV; 261 case ERRnofiles: 262 return 0; /* eeof ? */ 263 return ETXTBSY; 264 case ERRlock: 265 return EDEADLK; 266 case ERRfilexists: 267 return EEXIST; 268 case 123: /* dunno what is it, but samba maps as noent */ 269 return ENOENT; 270 case 145: /* samba */ 271 return ENOTEMPTY; 272 case ERRnotlocked: 273 return 0; /* file become unlocked */ 274 case 183: 275 return EEXIST; 276 case ERRquota: 277 return EDQUOT; 278 } 279 break; 280 case ERRSRV: 281 switch (eno) { 282 case ERRerror: 283 return EINVAL; 284 case ERRbadpw: 285 case ERRpasswordExpired: 286 return EAUTH; 287 case ERRaccess: 288 return EACCES; 289 case ERRinvnid: 290 return ENETRESET; 291 case ERRinvnetname: 292 SMBERROR("NetBIOS name is invalid\n"); 293 return EAUTH; 294 case 3: /* reserved and returned */ 295 return EIO; 296 case ERRaccountExpired: 297 case ERRbadClient: 298 case ERRbadLogonTime: 299 return EPERM; 300 case ERRnosupport: 301 return EBADRPC; 302 } 303 break; 304 case ERRHRD: 305 switch (eno) { 306 case ERRnowrite: 307 return EROFS; 308 case ERRbadunit: 309 return ENODEV; 310 case ERRnotready: 311 case ERRbadcmd: 312 case ERRdata: 313 return EIO; 314 case ERRbadreq: 315 return EBADRPC; 316 case ERRbadshare: 317 return ETXTBSY; 318 case ERRlock: 319 return EDEADLK; 320 } 321 break; 322 } 323 SMBERROR("Unmapped error %d:%d\n", eclass, eno); 324 return EBADRPC; 325 } 326 327 static int 328 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, 329 size_t *srclen, size_t *dstlen) 330 { 331 int error; 332 size_t inlen = *srclen, outlen = *dstlen; 333 334 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen, 335 &dst, &outlen); 336 if (inlen != *srclen || outlen != *dstlen) { 337 *srclen -= inlen; 338 *dstlen -= outlen; 339 return 0; 340 } else 341 return error; 342 } 343 344 int 345 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 346 int size, int caseopt) 347 { 348 struct iconv_drv *dp = vcp->vc_toserver; 349 350 if (size == 0) 351 return 0; 352 if (dp == NULL) { 353 return mb_put_mem(mbp, src, size, MB_MSYSTEM); 354 } 355 mbp->mb_copy = smb_copy_iconv; 356 mbp->mb_udata = dp; 357 return mb_put_mem(mbp, src, size, MB_MCUSTOM); 358 } 359 360 int 361 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 362 int caseopt) 363 { 364 int error; 365 366 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt); 367 if (error) 368 return error; 369 return mb_put_uint8(mbp, 0); 370 } 371 372 int 373 smb_put_asunistring(struct smb_rq *rqp, const char *src) 374 { 375 struct mbchain *mbp = &rqp->sr_rq; 376 struct iconv_drv *dp = rqp->sr_vc->vc_toserver; 377 u_char c; 378 int error; 379 380 while (*src) { 381 iconv_convmem(dp, &c, src++, 1); 382 error = mb_put_uint16le(mbp, c); 383 if (error) 384 return error; 385 } 386 return mb_put_uint16le(mbp, 0); 387 } 388