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>
| 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h>
|
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 55MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data"); 56MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data"); 57MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data"); 58 59smb_unichar smb_unieol = 0; 60 61void 62smb_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 73int 74smb_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 98char * 99smb_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 */ 116char * 117smb_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 */ 143void * 144smb_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 */ 160void * 161smb_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 174void 175smb_strfree(char *s) 176{ 177 free(s, M_SMBSTR); 178} 179 180void 181smb_memfree(void *s) 182{ 183 free(s, M_SMBSTR); 184} 185 186void * 187smb_zmalloc(unsigned long size, struct malloc_type *type, int flags) 188{ 189 190 return malloc(size, type, flags | M_ZERO); 191} 192 193void 194smb_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 203void 204m_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 221int 222smb_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 327static int 328smb_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 344int 345smb_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 360int 361smb_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 372int 373smb_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}
| 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/endian.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/proc.h> 36#include <sys/lock.h> 37#include <sys/sysctl.h> 38#include <sys/socket.h> 39#include <sys/signalvar.h> 40#include <sys/mbuf.h> 41 42#include <sys/iconv.h> 43 44#include <netsmb/smb.h> 45#include <netsmb/smb_conn.h> 46#include <netsmb/smb_rq.h> 47#include <netsmb/smb_subr.h> 48 49MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data"); 50MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data"); 51MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data"); 52 53smb_unichar smb_unieol = 0; 54 55void 56smb_makescred(struct smb_cred *scred, struct thread *td, struct ucred *cred) 57{ 58 if (td) { 59 scred->scr_td = td; 60 scred->scr_cred = cred ? cred : td->td_ucred; 61 } else { 62 scred->scr_td = NULL; 63 scred->scr_cred = cred ? cred : NULL; 64 } 65} 66 67int 68smb_td_intr(struct thread *td) 69{ 70 struct proc *p; 71 sigset_t tmpset; 72 73 if (td == NULL) 74 return 0; 75 76 p = td->td_proc; 77 PROC_LOCK(p); 78 tmpset = p->p_siglist; 79 SIGSETOR(tmpset, td->td_siglist); 80 SIGSETNAND(tmpset, td->td_sigmask); 81 mtx_lock(&p->p_sigacts->ps_mtx); 82 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore); 83 mtx_unlock(&p->p_sigacts->ps_mtx); 84 if (SIGNOTEMPTY(td->td_siglist) && SMB_SIGMASK(tmpset)) { 85 PROC_UNLOCK(p); 86 return EINTR; 87 } 88 PROC_UNLOCK(p); 89 return 0; 90} 91 92char * 93smb_strdup(const char *s) 94{ 95 char *p; 96 int len; 97 98 len = s ? strlen(s) + 1 : 1; 99 p = malloc(len, M_SMBSTR, M_WAITOK); 100 if (s) 101 bcopy(s, p, len); 102 else 103 *p = 0; 104 return p; 105} 106 107/* 108 * duplicate string from a user space. 109 */ 110char * 111smb_strdupin(char *s, int maxlen) 112{ 113 char *p, bt; 114 int error, len = 0; 115 116 for (p = s; ;p++) { 117 if (copyin(p, &bt, 1)) 118 return NULL; 119 len++; 120 if (maxlen && len > maxlen) 121 return NULL; 122 if (bt == 0) 123 break; 124 } 125 p = malloc(len, M_SMBSTR, M_WAITOK); 126 error = copyin(s, p, len); 127 if (error) { 128 free(p, M_SMBSTR); 129 return (NULL); 130 } 131 return p; 132} 133 134/* 135 * duplicate memory block from a user space. 136 */ 137void * 138smb_memdupin(void *umem, int len) 139{ 140 char *p; 141 142 if (len > 8 * 1024) 143 return NULL; 144 p = malloc(len, M_SMBSTR, M_WAITOK); 145 if (copyin(umem, p, len) == 0) 146 return p; 147 free(p, M_SMBSTR); 148 return NULL; 149} 150 151/* 152 * duplicate memory block in the kernel space. 153 */ 154void * 155smb_memdup(const void *umem, int len) 156{ 157 char *p; 158 159 if (len > 8 * 1024) 160 return NULL; 161 p = malloc(len, M_SMBSTR, M_WAITOK); 162 if (p == NULL) 163 return NULL; 164 bcopy(umem, p, len); 165 return p; 166} 167 168void 169smb_strfree(char *s) 170{ 171 free(s, M_SMBSTR); 172} 173 174void 175smb_memfree(void *s) 176{ 177 free(s, M_SMBSTR); 178} 179 180void * 181smb_zmalloc(unsigned long size, struct malloc_type *type, int flags) 182{ 183 184 return malloc(size, type, flags | M_ZERO); 185} 186 187void 188smb_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 197void 198m_dumpm(struct mbuf *m) { 199 char *p; 200 int len; 201 printf("d="); 202 while(m) { 203 p=mtod(m,char *); 204 len=m->m_len; 205 printf("(%d)",len); 206 while(len--){ 207 printf("%02x ",((int)*(p++)) & 0xff); 208 } 209 m=m->m_next; 210 }; 211 printf("\n"); 212} 213#endif 214 215int 216smb_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 ERRnotlocked: 267 return 0; /* file become unlocked */ 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 321static int 322smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, 323 size_t *srclen, size_t *dstlen) 324{ 325 int error; 326 size_t inlen = *srclen, outlen = *dstlen; 327 328 error = iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &inlen, 329 &dst, &outlen); 330 if (inlen != *srclen || outlen != *dstlen) { 331 *srclen -= inlen; 332 *dstlen -= outlen; 333 return 0; 334 } else 335 return error; 336} 337 338int 339smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 340 int size, int caseopt) 341{ 342 struct iconv_drv *dp = vcp->vc_toserver; 343 344 if (size == 0) 345 return 0; 346 if (dp == NULL) { 347 return mb_put_mem(mbp, src, size, MB_MSYSTEM); 348 } 349 mbp->mb_copy = smb_copy_iconv; 350 mbp->mb_udata = dp; 351 return mb_put_mem(mbp, src, size, MB_MCUSTOM); 352} 353 354int 355smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src, 356 int caseopt) 357{ 358 int error; 359 360 error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt); 361 if (error) 362 return error; 363 return mb_put_uint8(mbp, 0); 364} 365 366int 367smb_put_asunistring(struct smb_rq *rqp, const char *src) 368{ 369 struct mbchain *mbp = &rqp->sr_rq; 370 struct iconv_drv *dp = rqp->sr_vc->vc_toserver; 371 u_char c; 372 int error; 373 374 while (*src) { 375 iconv_convmem(dp, &c, src++, 1); 376 error = mb_put_uint16le(mbp, c); 377 if (error) 378 return error; 379 } 380 return mb_put_uint16le(mbp, 0); 381}
|