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 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 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 = malloc(len, M_SMBSTR, M_WAITOK); 147 if (copyin(umem, p, len) == 0) 148 return p; 149 free(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 = malloc(len, M_SMBSTR, M_WAITOK); 164 if (p == NULL) 165 return NULL; 166 bcopy(umem, p, len); 167 return p; 168 } 169 170 void 171 smb_strfree(char *s) 172 { 173 free(s, M_SMBSTR); 174 } 175 176 void 177 smb_memfree(void *s) 178 { 179 free(s, M_SMBSTR); 180 } 181 182 void * 183 smb_zmalloc(unsigned long size, struct malloc_type *type, int flags) 184 { 185 186 return malloc(size, type, flags | M_ZERO); 187 } 188 189 void 190 smb_strtouni(u_int16_t *dst, const char *src) 191 { 192 while (*src) { 193 *dst++ = htole16(*src++); 194 } 195 *dst = 0; 196 } 197 198 #ifdef SMB_SOCKETDATA_DEBUG 199 void 200 m_dumpm(struct mbuf *m) { 201 char *p; 202 int len; 203 printf("d="); 204 while(m) { 205 p=mtod(m,char *); 206 len=m->m_len; 207 printf("(%d)",len); 208 while(len--){ 209 printf("%02x ",((int)*(p++)) & 0xff); 210 } 211 m=m->m_next; 212 }; 213 printf("\n"); 214 } 215 #endif 216 217 int 218 smb_maperror(int eclass, int eno) 219 { 220 if (eclass == 0 && eno == 0) 221 return 0; 222 switch (eclass) { 223 case ERRDOS: 224 switch (eno) { 225 case ERRbadfunc: 226 case ERRbadmcb: 227 case ERRbadenv: 228 case ERRbadformat: 229 case ERRrmuns: 230 return EINVAL; 231 case ERRbadfile: 232 case ERRbadpath: 233 case ERRremcd: 234 case 66: /* nt returns it when share not available */ 235 case 67: /* observed from nt4sp6 when sharename wrong */ 236 return ENOENT; 237 case ERRnofids: 238 return EMFILE; 239 case ERRnoaccess: 240 case ERRbadshare: 241 return EACCES; 242 case ERRbadfid: 243 return EBADF; 244 case ERRnomem: 245 return ENOMEM; /* actually remote no mem... */ 246 case ERRbadmem: 247 return EFAULT; 248 case ERRbadaccess: 249 return EACCES; 250 case ERRbaddata: 251 return E2BIG; 252 case ERRbaddrive: 253 case ERRnotready: /* nt */ 254 return ENXIO; 255 case ERRdiffdevice: 256 return EXDEV; 257 case ERRnofiles: 258 return 0; /* eeof ? */ 259 return ETXTBSY; 260 case ERRlock: 261 return EDEADLK; 262 case ERRfilexists: 263 return EEXIST; 264 case 123: /* dunno what is it, but samba maps as noent */ 265 return ENOENT; 266 case 145: /* samba */ 267 return ENOTEMPTY; 268 case 183: 269 return EEXIST; 270 case ERRquota: 271 return EDQUOT; 272 } 273 break; 274 case ERRSRV: 275 switch (eno) { 276 case ERRerror: 277 return EINVAL; 278 case ERRbadpw: 279 case ERRpasswordExpired: 280 return EAUTH; 281 case ERRaccess: 282 return EACCES; 283 case ERRinvnid: 284 return ENETRESET; 285 case ERRinvnetname: 286 SMBERROR("NetBIOS name is invalid\n"); 287 return EAUTH; 288 case 3: /* reserved and returned */ 289 return EIO; 290 case ERRaccountExpired: 291 case ERRbadClient: 292 case ERRbadLogonTime: 293 return EPERM; 294 case ERRnosupport: 295 return EBADRPC; 296 } 297 break; 298 case ERRHRD: 299 switch (eno) { 300 case ERRnowrite: 301 return EROFS; 302 case ERRbadunit: 303 return ENODEV; 304 case ERRnotready: 305 case ERRbadcmd: 306 case ERRdata: 307 return EIO; 308 case ERRbadreq: 309 return EBADRPC; 310 case ERRbadshare: 311 return ETXTBSY; 312 case ERRlock: 313 return EDEADLK; 314 } 315 break; 316 } 317 SMBERROR("Unmapped error %d:%d\n", eclass, eno); 318 return EBADRPC; 319 } 320 321 static int 322 smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, size_t len) 323 { 324 size_t outlen = len; 325 326 return iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &len, &dst, &outlen); 327 } 328 329 int 330 smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 331 int size, int caseopt) 332 { 333 struct iconv_drv *dp = vcp->vc_toserver; 334 335 if (size == 0) 336 return 0; 337 if (dp == NULL) { 338 return mb_put_mem(mbp, src, size, MB_MSYSTEM); 339 } 340 mbp->mb_copy = smb_copy_iconv; 341 mbp->mb_udata = dp; 342 return mb_put_mem(mbp, src, size, MB_MCUSTOM); 343 } 344 345 int 346 smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 347 int caseopt) 348 { 349 int error; 350 351 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt); 352 if (error) 353 return error; 354 return mb_put_uint8(mbp, 0); 355 } 356 357 int 358 smb_put_asunistring(struct smb_rq *rqp, const char *src) 359 { 360 struct mbchain *mbp = &rqp->sr_rq; 361 struct iconv_drv *dp = rqp->sr_vc->vc_toserver; 362 u_char c; 363 int error; 364 365 while (*src) { 366 iconv_convmem(dp, &c, src++, 1); 367 error = mb_put_uint16le(mbp, c); 368 if (error) 369 return error; 370 } 371 return mb_put_uint16le(mbp, 0); 372 } 373