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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
/*-
* Copyright (c) 2003 Tim J. Robbins.
* Copyright (c) 1999, 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/uio.h>
#include <sys/ioccom.h>
#include <netncp/ncp.h>
#include <netncp/ncp_conn.h>
#include <netncp/ncp_subr.h>
#include <netncp/ncp_ncp.h>
#include <netncp/ncp_user.h>
#include <netncp/ncp_rq.h>
#include <netncp/ncp_nls.h>
#include <netncp/ncpio.h>
int ncp_version = NCP_VERSION;
SYSCTL_NODE(_net, OID_AUTO, ncp, CTLFLAG_RW, NULL, "NetWare requester");
SYSCTL_INT(_net_ncp, OID_AUTO, version, CTLFLAG_RD, &ncp_version, 0, "");
MODULE_VERSION(ncp, 1);
MODULE_DEPEND(ncp, libmchain, 1, 1, 1);
static struct cdev *ncp_dev;
static d_ioctl_t ncp_ioctl;
static struct cdevsw ncp_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
.d_ioctl = ncp_ioctl,
.d_name = "ncp",
};
static int ncp_conn_frag_rq(struct ncp_conn *, struct thread *,
struct ncp_conn_frag *);
static int ncp_conn_handler(struct thread *, struct ncpioc_request *,
struct ncp_conn *, struct ncp_handle *);
static int sncp_conn_scan(struct thread *, struct ncpioc_connscan *);
static int sncp_connect(struct thread *, struct ncpioc_connect *);
static int sncp_request(struct thread *, struct ncpioc_request *);
static int
ncp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
{
switch (cmd) {
case NCPIOC_CONNECT:
return (sncp_connect(td, (struct ncpioc_connect *)data));
case NCPIOC_CONNSCAN:
return (sncp_conn_scan(td, (struct ncpioc_connscan *)data));
case NCPIOC_REQUEST:
return (sncp_request(td, (struct ncpioc_request *)data));
}
return (EINVAL);
}
/*
* Attach to NCP server
*/
static int
sncp_connect(struct thread *td, struct ncpioc_connect *args)
{
int connHandle = 0, error;
struct ncp_conn *conn;
struct ncp_handle *handle;
struct ncp_conn_args li;
checkbad(copyin(args->ioc_li,&li,sizeof(li)));
/* XXX Should be useracc() */
checkbad(copyout(&connHandle,args->ioc_connhandle,
sizeof(connHandle)));
li.password = li.user = NULL;
error = ncp_conn_getattached(&li, td, td->td_ucred, NCPM_WRITE | NCPM_EXECUTE, &conn);
if (error) {
error = ncp_conn_alloc(&li, td, td->td_ucred, &conn);
if (error)
goto bad;
error = ncp_conn_reconnect(conn);
if (error)
ncp_conn_free(conn);
}
if (!error) {
error = ncp_conn_gethandle(conn, td, &handle);
copyout(&handle->nh_id, args->ioc_connhandle,
sizeof(args->ioc_connhandle));
ncp_conn_unlock(conn,td);
}
bad:
return error;
}
static int
sncp_request(struct thread *td, struct ncpioc_request *args)
{
struct ncp_rq *rqp;
struct ncp_conn *conn;
struct ncp_handle *handle;
int error = 0, rqsize;
error = ncp_conn_findhandle(args->ioc_connhandle, td, &handle);
if (error)
return error;
conn = handle->nh_conn;
if (args->ioc_fn == NCP_CONN)
return ncp_conn_handler(td, args, conn, handle);
error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
if (error)
return(error);
error = ncp_rq_alloc(args->ioc_fn, conn, td, td->td_ucred, &rqp);
if (error)
return error;
if (rqsize) {
error = mb_put_mem(&rqp->rq, (caddr_t)args->ioc_ncpbuf->packet,
rqsize, MB_MUSER);
if (error)
goto bad;
}
rqp->nr_flags |= NCPR_DONTFREEONERR;
error = ncp_request(rqp);
if (error == 0 && rqp->nr_rpsize)
error = md_get_mem(&rqp->rp, (caddr_t)args->ioc_ncpbuf->packet,
rqp->nr_rpsize, MB_MUSER);
copyout(&rqp->nr_cs, &args->ioc_ncpbuf->cs, sizeof(rqp->nr_cs));
copyout(&rqp->nr_cc, &args->ioc_ncpbuf->cc, sizeof(rqp->nr_cc));
copyout(&rqp->nr_rpsize, &args->ioc_ncpbuf->rpsize, sizeof(rqp->nr_rpsize));
bad:
ncp_rq_done(rqp);
return error;
}
static int
ncp_mod_login(struct ncp_conn *conn, char *user, int objtype, char *password,
struct thread *td, struct ucred *cred)
{
int error;
if (ncp_suser(cred) != 0 && cred->cr_uid != conn->nc_owner->cr_uid)
return EACCES;
conn->li.user = ncp_str_dup(user);
if (conn->li.user == NULL)
return ENOMEM;
conn->li.password = ncp_str_dup(password);
if (conn->li.password == NULL) {
error = ENOMEM;
goto bad;
}
ncp_str_upper(conn->li.user);
if ((conn->li.opt & NCP_OPT_NOUPCASEPASS) == 0)
ncp_str_upper(conn->li.password);
conn->li.objtype = objtype;
error = ncp_conn_login(conn, td, cred);
return error;
bad:
if (conn->li.user) {
free(conn->li.user, M_NCPDATA);
conn->li.user = NULL;
}
if (conn->li.password) {
free(conn->li.password, M_NCPDATA);
conn->li.password = NULL;
}
return error;
}
static int
ncp_conn_handler(struct thread *td, struct ncpioc_request *args,
struct ncp_conn *conn, struct ncp_handle *hp)
{
int error = 0, rqsize, subfn;
struct ucred *cred;
char *pdata;
cred = td->td_ucred;
error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
if (error)
return(error);
error = 0;
pdata = args->ioc_ncpbuf->packet;
subfn = *(pdata++) & 0xff;
rqsize--;
switch (subfn) {
case NCP_CONN_READ: case NCP_CONN_WRITE: {
struct ncp_rw rwrq;
struct uio auio;
struct iovec iov;
if (rqsize != sizeof(rwrq))
return (EBADRPC);
error = copyin(pdata,&rwrq,rqsize);
if (error)
return (error);
iov.iov_base = rwrq.nrw_base;
iov.iov_len = rwrq.nrw_cnt;
auio.uio_iov = &iov;
auio.uio_iovcnt = 1;
auio.uio_offset = rwrq.nrw_offset;
auio.uio_resid = rwrq.nrw_cnt;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_rw = (subfn == NCP_CONN_READ) ? UIO_READ : UIO_WRITE;
auio.uio_td = td;
if (subfn == NCP_CONN_READ)
error = ncp_read(conn, &rwrq.nrw_fh, &auio, cred);
else
error = ncp_write(conn, &rwrq.nrw_fh, &auio, cred);
rwrq.nrw_cnt -= auio.uio_resid;
/*td->td_retval[0] = rwrq.nrw_cnt;*/
break;
} /* case int_read/write */
case NCP_CONN_SETFLAGS: {
u_int16_t mask, flags;
error = copyin(pdata,&mask, sizeof(mask));
if (error)
return error;
pdata += sizeof(mask);
error = copyin(pdata,&flags,sizeof(flags));
if (error)
return error;
error = ncp_conn_lock(conn, td, cred, NCPM_WRITE);
if (error)
return error;
if (mask & NCPFL_PERMANENT) {
conn->flags &= ~NCPFL_PERMANENT;
conn->flags |= (flags & NCPFL_PERMANENT);
}
if (mask & NCPFL_PRIMARY) {
error = ncp_conn_setprimary(conn, flags & NCPFL_PRIMARY);
if (error) {
ncp_conn_unlock(conn, td);
break;
}
}
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_LOGIN: {
struct ncp_conn_login la;
if (rqsize != sizeof(la))
return EBADRPC;
if (conn->flags & NCPFL_LOGGED)
return EALREADY;
if ((error = copyin(pdata,&la,rqsize)) != 0)
break;
error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE | NCPM_WRITE);
if (error)
return error;
error = ncp_mod_login(conn, la.username, la.objtype,
la.password, td, td->td_ucred);
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_GETINFO: {
struct ncp_conn_stat ncs;
int len = sizeof(ncs);
error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
if (error)
return error;
ncp_conn_getinfo(conn, &ncs);
copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
error = copyout(&ncs, &args->ioc_ncpbuf->packet, len);
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_GETUSER: {
int len;
error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
if (error)
return error;
len = (conn->li.user) ? strlen(conn->li.user) + 1 : 0;
copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
if (len) {
error = copyout(conn->li.user,
&args->ioc_ncpbuf->packet, len);
}
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_CONN2REF: {
int len = sizeof(int);
error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
if (error)
return error;
copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
if (len) {
error = copyout(&conn->nc_id,
&args->ioc_ncpbuf->packet, len);
}
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_FRAG: {
struct ncp_conn_frag nf;
if (rqsize != sizeof(nf))
return (EBADRPC);
if ((error = copyin(pdata, &nf, rqsize)) != 0) break;
error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
if (error)
return error;
error = ncp_conn_frag_rq(conn, td, &nf);
ncp_conn_unlock(conn, td);
copyout(&nf, &pdata, sizeof(nf));
td->td_retval[0] = error;
break;
}
case NCP_CONN_DUP: {
struct ncp_handle *newhp;
int len = sizeof(NWCONN_HANDLE);
error = ncp_conn_lock(conn, td, cred, NCPM_READ);
if (error) break;
copyout(&len, &args->ioc_ncpbuf->rpsize, len);
error = ncp_conn_gethandle(conn, td, &newhp);
if (!error)
error = copyout(&newhp->nh_id,
args->ioc_ncpbuf->packet, len);
ncp_conn_unlock(conn, td);
break;
}
case NCP_CONN_CONNCLOSE: {
error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
if (error) break;
ncp_conn_puthandle(hp, td, 0);
error = ncp_conn_free(conn);
if (error)
ncp_conn_unlock(conn, td);
break;
}
default:
error = EOPNOTSUPP;
}
return error;
}
static int
sncp_conn_scan(struct thread *td, struct ncpioc_connscan *args)
{
int connHandle = 0, error;
struct ncp_conn_args li, *lip;
struct ncp_conn *conn;
struct ncp_handle *hp;
char *user = NULL, *password = NULL;
if (args->ioc_li) {
if (copyin(args->ioc_li, &li, sizeof(li)))
return EFAULT;
lip = &li;
} else {
lip = NULL;
}
if (lip != NULL) {
lip->server[sizeof(lip->server)-1]=0; /* just to make sure */
ncp_str_upper(lip->server);
if (lip->user) {
user = ncp_str_dup(lip->user);
if (user == NULL)
return EINVAL;
ncp_str_upper(user);
}
if (lip->password) {
password = ncp_str_dup(lip->password);
if (password == NULL) {
if (user)
free(user, M_NCPDATA);
return EINVAL;
}
ncp_str_upper(password);
}
lip->user = user;
lip->password = password;
}
error = ncp_conn_getbyli(lip, td, td->td_ucred, NCPM_EXECUTE, &conn);
if (!error) { /* already have this login */
ncp_conn_gethandle(conn, td, &hp);
connHandle = hp->nh_id;
ncp_conn_unlock(conn, td);
copyout(&connHandle, args->ioc_connhandle, sizeof(connHandle));
}
if (user)
free(user, M_NCPDATA);
if (password)
free(password, M_NCPDATA);
return error;
}
int
ncp_conn_frag_rq(struct ncp_conn *conn, struct thread *td,
struct ncp_conn_frag *nfp)
{
NW_FRAGMENT *fp;
struct ncp_rq *rqp;
u_int32_t fsize;
int error, i, rpsize;
error = ncp_rq_alloc(nfp->fn, conn, td, td->td_ucred, &rqp);
if (error)
return error;
for(fp = nfp->rqf, i = 0; i < nfp->rqfcnt; i++, fp++) {
error = mb_put_mem(&rqp->rq, (caddr_t)fp->fragAddress, fp->fragSize, MB_MUSER);
if (error)
goto bad;
}
rqp->nr_flags |= NCPR_DONTFREEONERR;
error = ncp_request(rqp);
if (error)
goto bad;
rpsize = rqp->nr_rpsize;
if (rpsize && nfp->rpfcnt) {
for(fp = nfp->rpf, i = 0; i < nfp->rpfcnt; i++, fp++) {
error = copyin(&fp->fragSize, &fsize, sizeof (fsize));
if (error)
break;
fsize = min(fsize, rpsize);
error = md_get_mem(&rqp->rp, (caddr_t)fp->fragAddress, fsize, MB_MUSER);
if (error)
break;
rpsize -= fsize;
error = copyout(&fsize, &fp->fragSize, sizeof (fsize));
if (error)
break;
}
}
nfp->cs = rqp->nr_cs;
nfp->cc = rqp->nr_cc;
bad:
ncp_rq_done(rqp);
return error;
}
static int
ncp_load(void)
{
int error;
if ((error = ncp_init()) != 0)
return (error);
ncp_dev = make_dev(&ncp_cdevsw, 0, 0, 0, 0666, "ncp");
printf("ncp_load: loaded\n");
return (0);
}
static int
ncp_unload(void)
{
int error;
error = ncp_done();
if (error)
return (error);
destroy_dev(ncp_dev);
printf("ncp_unload: unloaded\n");
return (0);
}
static int
ncp_mod_handler(module_t mod, int type, void *data)
{
int error;
switch (type) {
case MOD_LOAD:
error = ncp_load();
break;
case MOD_UNLOAD:
error = ncp_unload();
break;
default:
error = EINVAL;
}
return error;
}
static moduledata_t ncp_mod = {
"ncp",
ncp_mod_handler,
NULL
};
DECLARE_MODULE(ncp, ncp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
|