2 * Copyright (c) 2003 Tim J. Robbins.
3 * Copyright (c) 1999, 2000, 2001 Boris Popov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Boris Popov.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
46 #include <sys/ioccom.h>
48 #include <netncp/ncp.h>
49 #include <netncp/ncp_conn.h>
50 #include <netncp/ncp_subr.h>
51 #include <netncp/ncp_ncp.h>
52 #include <netncp/ncp_user.h>
53 #include <netncp/ncp_rq.h>
54 #include <netncp/ncp_nls.h>
55 #include <netncp/ncpio.h>
57 int ncp_version = NCP_VERSION;
59 SYSCTL_NODE(_net, OID_AUTO, ncp, CTLFLAG_RW, NULL, "NetWare requester");
60 SYSCTL_INT(_net_ncp, OID_AUTO, version, CTLFLAG_RD, &ncp_version, 0, "");
62 MODULE_VERSION(ncp, 1);
63 MODULE_DEPEND(ncp, libmchain, 1, 1, 1);
65 static struct cdev *ncp_dev;
67 static d_ioctl_t ncp_ioctl;
69 static struct cdevsw ncp_cdevsw = {
70 .d_version = D_VERSION,
71 .d_flags = D_NEEDGIANT,
76 static int ncp_conn_frag_rq(struct ncp_conn *, struct thread *,
77 struct ncp_conn_frag *);
78 static int ncp_conn_handler(struct thread *, struct ncpioc_request *,
79 struct ncp_conn *, struct ncp_handle *);
80 static int sncp_conn_scan(struct thread *, struct ncpioc_connscan *);
81 static int sncp_connect(struct thread *, struct ncpioc_connect *);
82 static int sncp_request(struct thread *, struct ncpioc_request *);
85 ncp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
90 return (sncp_connect(td, (struct ncpioc_connect *)data));
92 return (sncp_conn_scan(td, (struct ncpioc_connscan *)data));
94 return (sncp_request(td, (struct ncpioc_request *)data));
100 * Attach to NCP server
104 sncp_connect(struct thread *td, struct ncpioc_connect *args)
106 int connHandle = 0, error;
107 struct ncp_conn *conn;
108 struct ncp_handle *handle;
109 struct ncp_conn_args li;
111 checkbad(copyin(args->ioc_li,&li,sizeof(li)));
112 /* XXX Should be useracc() */
113 checkbad(copyout(&connHandle,args->ioc_connhandle,
114 sizeof(connHandle)));
115 li.password = li.user = NULL;
116 error = ncp_conn_getattached(&li, td, td->td_ucred, NCPM_WRITE | NCPM_EXECUTE, &conn);
118 error = ncp_conn_alloc(&li, td, td->td_ucred, &conn);
121 error = ncp_conn_reconnect(conn);
126 error = ncp_conn_gethandle(conn, td, &handle);
127 copyout(&handle->nh_id, args->ioc_connhandle,
128 sizeof(args->ioc_connhandle));
129 ncp_conn_unlock(conn,td);
136 sncp_request(struct thread *td, struct ncpioc_request *args)
139 struct ncp_conn *conn;
140 struct ncp_handle *handle;
141 int error = 0, rqsize;
143 error = ncp_conn_findhandle(args->ioc_connhandle, td, &handle);
146 conn = handle->nh_conn;
147 if (args->ioc_fn == NCP_CONN)
148 return ncp_conn_handler(td, args, conn, handle);
149 error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
152 error = ncp_rq_alloc(args->ioc_fn, conn, td, td->td_ucred, &rqp);
156 error = mb_put_mem(&rqp->rq, (caddr_t)args->ioc_ncpbuf->packet,
161 rqp->nr_flags |= NCPR_DONTFREEONERR;
162 error = ncp_request(rqp);
163 if (error == 0 && rqp->nr_rpsize)
164 error = md_get_mem(&rqp->rp, (caddr_t)args->ioc_ncpbuf->packet,
165 rqp->nr_rpsize, MB_MUSER);
166 copyout(&rqp->nr_cs, &args->ioc_ncpbuf->cs, sizeof(rqp->nr_cs));
167 copyout(&rqp->nr_cc, &args->ioc_ncpbuf->cc, sizeof(rqp->nr_cc));
168 copyout(&rqp->nr_rpsize, &args->ioc_ncpbuf->rpsize, sizeof(rqp->nr_rpsize));
175 ncp_mod_login(struct ncp_conn *conn, char *user, int objtype, char *password,
176 struct thread *td, struct ucred *cred)
180 if (ncp_suser(cred) != 0 && cred->cr_uid != conn->nc_owner->cr_uid)
182 conn->li.user = ncp_str_dup(user);
183 if (conn->li.user == NULL)
185 conn->li.password = ncp_str_dup(password);
186 if (conn->li.password == NULL) {
190 ncp_str_upper(conn->li.user);
191 if ((conn->li.opt & NCP_OPT_NOUPCASEPASS) == 0)
192 ncp_str_upper(conn->li.password);
193 conn->li.objtype = objtype;
194 error = ncp_conn_login(conn, td, cred);
198 free(conn->li.user, M_NCPDATA);
199 conn->li.user = NULL;
201 if (conn->li.password) {
202 free(conn->li.password, M_NCPDATA);
203 conn->li.password = NULL;
209 ncp_conn_handler(struct thread *td, struct ncpioc_request *args,
210 struct ncp_conn *conn, struct ncp_handle *hp)
212 int error = 0, rqsize, subfn;
218 error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
222 pdata = args->ioc_ncpbuf->packet;
223 subfn = *(pdata++) & 0xff;
226 case NCP_CONN_READ: case NCP_CONN_WRITE: {
231 if (rqsize != sizeof(rwrq))
233 error = copyin(pdata,&rwrq,rqsize);
236 iov.iov_base = rwrq.nrw_base;
237 iov.iov_len = rwrq.nrw_cnt;
240 auio.uio_offset = rwrq.nrw_offset;
241 auio.uio_resid = rwrq.nrw_cnt;
242 auio.uio_segflg = UIO_USERSPACE;
243 auio.uio_rw = (subfn == NCP_CONN_READ) ? UIO_READ : UIO_WRITE;
245 if (subfn == NCP_CONN_READ)
246 error = ncp_read(conn, &rwrq.nrw_fh, &auio, cred);
248 error = ncp_write(conn, &rwrq.nrw_fh, &auio, cred);
249 rwrq.nrw_cnt -= auio.uio_resid;
250 /*td->td_retval[0] = rwrq.nrw_cnt;*/
252 } /* case int_read/write */
253 case NCP_CONN_SETFLAGS: {
254 u_int16_t mask, flags;
256 error = copyin(pdata,&mask, sizeof(mask));
259 pdata += sizeof(mask);
260 error = copyin(pdata,&flags,sizeof(flags));
263 error = ncp_conn_lock(conn, td, cred, NCPM_WRITE);
266 if (mask & NCPFL_PERMANENT) {
267 conn->flags &= ~NCPFL_PERMANENT;
268 conn->flags |= (flags & NCPFL_PERMANENT);
270 if (mask & NCPFL_PRIMARY) {
271 error = ncp_conn_setprimary(conn, flags & NCPFL_PRIMARY);
273 ncp_conn_unlock(conn, td);
277 ncp_conn_unlock(conn, td);
280 case NCP_CONN_LOGIN: {
281 struct ncp_conn_login la;
283 if (rqsize != sizeof(la))
285 if (conn->flags & NCPFL_LOGGED)
287 if ((error = copyin(pdata,&la,rqsize)) != 0)
289 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE | NCPM_WRITE);
292 error = ncp_mod_login(conn, la.username, la.objtype,
293 la.password, td, td->td_ucred);
294 ncp_conn_unlock(conn, td);
297 case NCP_CONN_GETINFO: {
298 struct ncp_conn_stat ncs;
299 int len = sizeof(ncs);
301 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
304 ncp_conn_getinfo(conn, &ncs);
305 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
306 error = copyout(&ncs, &args->ioc_ncpbuf->packet, len);
307 ncp_conn_unlock(conn, td);
310 case NCP_CONN_GETUSER: {
313 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
316 len = (conn->li.user) ? strlen(conn->li.user) + 1 : 0;
317 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
319 error = copyout(conn->li.user,
320 &args->ioc_ncpbuf->packet, len);
322 ncp_conn_unlock(conn, td);
325 case NCP_CONN_CONN2REF: {
326 int len = sizeof(int);
328 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
331 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
333 error = copyout(&conn->nc_id,
334 &args->ioc_ncpbuf->packet, len);
336 ncp_conn_unlock(conn, td);
339 case NCP_CONN_FRAG: {
340 struct ncp_conn_frag nf;
342 if (rqsize != sizeof(nf))
344 if ((error = copyin(pdata, &nf, rqsize)) != 0) break;
345 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
348 error = ncp_conn_frag_rq(conn, td, &nf);
349 ncp_conn_unlock(conn, td);
350 copyout(&nf, &pdata, sizeof(nf));
351 td->td_retval[0] = error;
355 struct ncp_handle *newhp;
356 int len = sizeof(NWCONN_HANDLE);
358 error = ncp_conn_lock(conn, td, cred, NCPM_READ);
360 copyout(&len, &args->ioc_ncpbuf->rpsize, len);
361 error = ncp_conn_gethandle(conn, td, &newhp);
363 error = copyout(&newhp->nh_id,
364 args->ioc_ncpbuf->packet, len);
365 ncp_conn_unlock(conn, td);
368 case NCP_CONN_CONNCLOSE: {
369 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
371 ncp_conn_puthandle(hp, td, 0);
372 error = ncp_conn_free(conn);
374 ncp_conn_unlock(conn, td);
384 sncp_conn_scan(struct thread *td, struct ncpioc_connscan *args)
386 int connHandle = 0, error;
387 struct ncp_conn_args li, *lip;
388 struct ncp_conn *conn;
389 struct ncp_handle *hp;
390 char *user = NULL, *password = NULL;
393 if (copyin(args->ioc_li, &li, sizeof(li)))
401 lip->server[sizeof(lip->server)-1]=0; /* just to make sure */
402 ncp_str_upper(lip->server);
404 user = ncp_str_dup(lip->user);
410 password = ncp_str_dup(lip->password);
411 if (password == NULL) {
413 free(user, M_NCPDATA);
416 ncp_str_upper(password);
419 lip->password = password;
421 error = ncp_conn_getbyli(lip, td, td->td_ucred, NCPM_EXECUTE, &conn);
422 if (!error) { /* already have this login */
423 ncp_conn_gethandle(conn, td, &hp);
424 connHandle = hp->nh_id;
425 ncp_conn_unlock(conn, td);
426 copyout(&connHandle, args->ioc_connhandle, sizeof(connHandle));
429 free(user, M_NCPDATA);
431 free(password, M_NCPDATA);
437 ncp_conn_frag_rq(struct ncp_conn *conn, struct thread *td,
438 struct ncp_conn_frag *nfp)
443 int error, i, rpsize;
445 error = ncp_rq_alloc(nfp->fn, conn, td, td->td_ucred, &rqp);
448 for(fp = nfp->rqf, i = 0; i < nfp->rqfcnt; i++, fp++) {
449 error = mb_put_mem(&rqp->rq, (caddr_t)fp->fragAddress, fp->fragSize, MB_MUSER);
453 rqp->nr_flags |= NCPR_DONTFREEONERR;
454 error = ncp_request(rqp);
457 rpsize = rqp->nr_rpsize;
458 if (rpsize && nfp->rpfcnt) {
459 for(fp = nfp->rpf, i = 0; i < nfp->rpfcnt; i++, fp++) {
460 error = copyin(&fp->fragSize, &fsize, sizeof (fsize));
463 fsize = min(fsize, rpsize);
464 error = md_get_mem(&rqp->rp, (caddr_t)fp->fragAddress, fsize, MB_MUSER);
468 error = copyout(&fsize, &fp->fragSize, sizeof (fsize));
473 nfp->cs = rqp->nr_cs;
474 nfp->cc = rqp->nr_cc;
485 if ((error = ncp_init()) != 0)
487 ncp_dev = make_dev(&ncp_cdevsw, 0, 0, 0, 0666, "ncp");
488 printf("ncp_load: loaded\n");
500 destroy_dev(ncp_dev);
501 printf("ncp_unload: unloaded\n");
506 ncp_mod_handler(module_t mod, int type, void *data)
515 error = ncp_unload();
523 static moduledata_t ncp_mod = {
528 DECLARE_MODULE(ncp, ncp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);