1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <sys/signalvar.h>
#include <sys/mbuf.h>
#include <sys/iconv.h>
#include <netsmb/smb.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_rq.h>
#include <netsmb/smb_subr.h>
MALLOC_DEFINE(M_SMBDATA, "SMBDATA", "Misc netsmb data");
MALLOC_DEFINE(M_SMBSTR, "SMBSTR", "netsmb string data");
MALLOC_DEFINE(M_SMBTEMP, "SMBTEMP", "Temp netsmb data");
smb_unichar smb_unieol = 0;
void
smb_makescred(struct smb_cred *scred, struct proc *p, struct ucred *cred)
{
if (p) {
scred->scr_p = p;
scred->scr_cred = cred ? cred : p->p_ucred;
} else {
scred->scr_p = NULL;
scred->scr_cred = cred ? cred : NULL;
}
}
int
smb_proc_intr(struct proc *p)
{
sigset_t tmpset;
if (p == NULL)
return 0;
tmpset = p->p_siglist;
SIGSETNAND(tmpset, p->p_sigmask);
SIGSETNAND(tmpset, p->p_sigignore);
if (SIGNOTEMPTY(p->p_siglist) && SMB_SIGMASK(tmpset))
return EINTR;
return 0;
}
char *
smb_strdup(const char *s)
{
char *p;
int len;
len = s ? strlen(s) + 1 : 1;
p = malloc(len, M_SMBSTR, M_WAITOK);
if (s)
bcopy(s, p, len);
else
*p = 0;
return p;
}
/*
* duplicate string from a user space.
*/
char *
smb_strdupin(char *s, int maxlen)
{
char *p, bt;
int len = 0;
for (p = s; ;p++) {
if (copyin(p, &bt, 1))
return NULL;
len++;
if (maxlen && len > maxlen)
return NULL;
if (bt == 0)
break;
}
p = malloc(len, M_SMBSTR, M_WAITOK);
copyin(s, p, len);
return p;
}
/*
* duplicate memory block from a user space.
*/
void *
smb_memdupin(void *umem, int len)
{
char *p;
if (len > 8 * 1024)
return NULL;
p = malloc(len, M_SMBSTR, M_WAITOK);
if (copyin(umem, p, len) == 0)
return p;
free(p, M_SMBSTR);
return NULL;
}
/*
* duplicate memory block in the kernel space.
*/
void *
smb_memdup(const void *umem, int len)
{
char *p;
if (len > 8 * 1024)
return NULL;
p = malloc(len, M_SMBSTR, M_WAITOK);
if (p == NULL)
return NULL;
bcopy(umem, p, len);
return p;
}
void
smb_strfree(char *s)
{
free(s, M_SMBSTR);
}
void
smb_memfree(void *s)
{
free(s, M_SMBSTR);
}
void *
smb_zmalloc(unsigned long size, struct malloc_type *type, int flags)
{
return malloc(size, type, flags | M_ZERO);
}
void
smb_strtouni(u_int16_t *dst, const char *src)
{
while (*src) {
*dst++ = htoles(*src++);
}
*dst = 0;
}
#ifdef SMB_SOCKETDATA_DEBUG
void
m_dumpm(struct mbuf *m) {
char *p;
int len;
printf("d=");
while(m) {
p=mtod(m,char *);
len=m->m_len;
printf("(%d)",len);
while(len--){
printf("%02x ",((int)*(p++)) & 0xff);
}
m=m->m_next;
};
printf("\n");
}
#endif
int
smb_maperror(int eclass, int eno)
{
if (eclass == 0 && eno == 0)
return 0;
switch (eclass) {
case ERRDOS:
switch (eno) {
case ERRbadfunc:
case ERRbadmcb:
case ERRbadenv:
case ERRbadformat:
case ERRrmuns:
return EINVAL;
case ERRbadfile:
case ERRbadpath:
case ERRremcd:
case 66: /* nt returns it when share not available */
case 67: /* observed from nt4sp6 when sharename wrong */
return ENOENT;
case ERRnofids:
return EMFILE;
case ERRnoaccess:
case ERRbadshare:
return EACCES;
case ERRbadfid:
return EBADF;
case ERRnomem:
return ENOMEM; /* actually remote no mem... */
case ERRbadmem:
return EFAULT;
case ERRbadaccess:
return EACCES;
case ERRbaddata:
return E2BIG;
case ERRbaddrive:
case ERRnotready: /* nt */
return ENXIO;
case ERRdiffdevice:
return EXDEV;
case ERRnofiles:
return 0; /* eeof ? */
return ETXTBSY;
case ERRlock:
return EDEADLK;
case ERRfilexists:
return EEXIST;
case 123: /* dunno what is it, but samba maps as noent */
return ENOENT;
case 145: /* samba */
return ENOTEMPTY;
case 183:
return EEXIST;
}
break;
case ERRSRV:
switch (eno) {
case ERRerror:
return EINVAL;
case ERRbadpw:
return EAUTH;
case ERRaccess:
return EACCES;
case ERRinvnid:
return ENETRESET;
case ERRinvnetname:
SMBERROR("NetBIOS name is invalid\n");
return EAUTH;
case 3: /* reserved and returned */
return EIO;
case 2239: /* NT: account exists but disabled */
return EPERM;
}
break;
case ERRHRD:
switch (eno) {
case ERRnowrite:
return EROFS;
case ERRbadunit:
return ENODEV;
case ERRnotready:
case ERRbadcmd:
case ERRdata:
return EIO;
case ERRbadreq:
return EBADRPC;
case ERRbadshare:
return ETXTBSY;
case ERRlock:
return EDEADLK;
}
break;
}
SMBERROR("Unmapped error %d:%d\n", eclass, eno);
return EBADRPC;
}
static int
smb_copy_iconv(struct mbchain *mbp, c_caddr_t src, caddr_t dst, int len)
{
int outlen = len;
return iconv_conv((struct iconv_drv*)mbp->mb_udata, &src, &len, &dst, &outlen);
}
int
smb_put_dmem(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
int size, int caseopt)
{
struct iconv_drv *dp = vcp->vc_toserver;
if (size == 0)
return 0;
if (dp == NULL) {
return mb_put_mem(mbp, src, size, MB_MSYSTEM);
}
mbp->mb_copy = smb_copy_iconv;
mbp->mb_udata = dp;
return mb_put_mem(mbp, src, size, MB_MCUSTOM);
}
int
smb_put_dstring(struct mbchain *mbp, struct smb_vc *vcp, const char *src,
int caseopt)
{
int error;
error = smb_put_dmem(mbp, vcp, src, strlen(src), caseopt);
if (error)
return error;
return mb_put_uint8(mbp, 0);
}
int
smb_put_asunistring(struct smb_rq *rqp, const char *src)
{
struct mbchain *mbp = &rqp->sr_rq;
struct iconv_drv *dp = rqp->sr_vc->vc_toserver;
u_char c;
int error;
while (*src) {
iconv_convmem(dp, &c, src++, 1);
error = mb_put_uint16le(mbp, c);
if (error)
return error;
}
return mb_put_uint16le(mbp, 0);
}
|