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>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
47 #include <sys/ioccom.h>
49 #include <netncp/ncp.h>
50 #include <netncp/ncp_conn.h>
51 #include <netncp/ncp_subr.h>
52 #include <netncp/ncp_ncp.h>
53 #include <netncp/ncp_user.h>
54 #include <netncp/ncp_rq.h>
55 #include <netncp/ncp_nls.h>
56 #include <netncp/ncpio.h>
58 int ncp_version = NCP_VERSION;
60 SYSCTL_NODE(_net, OID_AUTO, ncp, CTLFLAG_RW, NULL, "NetWare requester");
61 SYSCTL_INT(_net_ncp, OID_AUTO, version, CTLFLAG_RD, &ncp_version, 0, "");
63 MODULE_VERSION(ncp, 1);
64 MODULE_DEPEND(ncp, libmchain, 1, 1, 1);
66 static struct cdev *ncp_dev;
68 static d_ioctl_t ncp_ioctl;
70 static struct cdevsw ncp_cdevsw = {
71 .d_version = D_VERSION,
72 .d_flags = D_NEEDGIANT,
77 static int ncp_conn_frag_rq(struct ncp_conn *, struct thread *,
78 struct ncp_conn_frag *);
79 static int ncp_conn_handler(struct thread *, struct ncpioc_request *,
80 struct ncp_conn *, struct ncp_handle *);
81 static int sncp_conn_scan(struct thread *, struct ncpioc_connscan *);
82 static int sncp_connect(struct thread *, struct ncpioc_connect *);
83 static int sncp_request(struct thread *, struct ncpioc_request *);
86 ncp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
91 return (sncp_connect(td, (struct ncpioc_connect *)data));
93 return (sncp_conn_scan(td, (struct ncpioc_connscan *)data));
95 return (sncp_request(td, (struct ncpioc_request *)data));
101 * Attach to NCP server
105 sncp_connect(struct thread *td, struct ncpioc_connect *args)
107 int connHandle = 0, error;
108 struct ncp_conn *conn;
109 struct ncp_handle *handle;
110 struct ncp_conn_args li;
112 checkbad(copyin(args->ioc_li,&li,sizeof(li)));
113 /* XXX Should be useracc() */
114 checkbad(copyout(&connHandle,args->ioc_connhandle,
115 sizeof(connHandle)));
116 li.password = li.user = NULL;
117 error = ncp_conn_getattached(&li, td, td->td_ucred, NCPM_WRITE | NCPM_EXECUTE, &conn);
119 error = ncp_conn_alloc(&li, td, td->td_ucred, &conn);
122 error = ncp_conn_reconnect(conn);
127 error = ncp_conn_gethandle(conn, td, &handle);
128 copyout(&handle->nh_id, args->ioc_connhandle,
129 sizeof(args->ioc_connhandle));
130 ncp_conn_unlock(conn,td);
137 sncp_request(struct thread *td, struct ncpioc_request *args)
140 struct ncp_conn *conn;
141 struct ncp_handle *handle;
142 int error = 0, rqsize;
144 error = ncp_conn_findhandle(args->ioc_connhandle, td, &handle);
147 conn = handle->nh_conn;
148 if (args->ioc_fn == NCP_CONN)
149 return ncp_conn_handler(td, args, conn, handle);
150 error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
153 error = ncp_rq_alloc(args->ioc_fn, conn, td, td->td_ucred, &rqp);
157 error = mb_put_mem(&rqp->rq, (caddr_t)args->ioc_ncpbuf->packet,
162 rqp->nr_flags |= NCPR_DONTFREEONERR;
163 error = ncp_request(rqp);
164 if (error == 0 && rqp->nr_rpsize)
165 error = md_get_mem(&rqp->rp, (caddr_t)args->ioc_ncpbuf->packet,
166 rqp->nr_rpsize, MB_MUSER);
167 copyout(&rqp->nr_cs, &args->ioc_ncpbuf->cs, sizeof(rqp->nr_cs));
168 copyout(&rqp->nr_cc, &args->ioc_ncpbuf->cc, sizeof(rqp->nr_cc));
169 copyout(&rqp->nr_rpsize, &args->ioc_ncpbuf->rpsize, sizeof(rqp->nr_rpsize));
176 ncp_mod_login(struct ncp_conn *conn, char *user, int objtype, char *password,
177 struct thread *td, struct ucred *cred)
181 if (ncp_suser(cred) != 0 && cred->cr_uid != conn->nc_owner->cr_uid)
183 conn->li.user = ncp_str_dup(user);
184 if (conn->li.user == NULL)
186 conn->li.password = ncp_str_dup(password);
187 if (conn->li.password == NULL) {
191 ncp_str_upper(conn->li.user);
192 if ((conn->li.opt & NCP_OPT_NOUPCASEPASS) == 0)
193 ncp_str_upper(conn->li.password);
194 conn->li.objtype = objtype;
195 error = ncp_conn_login(conn, td, cred);
199 free(conn->li.user, M_NCPDATA);
200 conn->li.user = NULL;
202 if (conn->li.password) {
203 free(conn->li.password, M_NCPDATA);
204 conn->li.password = NULL;
210 ncp_conn_handler(struct thread *td, struct ncpioc_request *args,
211 struct ncp_conn *conn, struct ncp_handle *hp)
213 int error = 0, rqsize, subfn;
219 error = copyin(&args->ioc_ncpbuf->rqsize, &rqsize, sizeof(int));
223 pdata = args->ioc_ncpbuf->packet;
224 subfn = *(pdata++) & 0xff;
227 case NCP_CONN_READ: case NCP_CONN_WRITE: {
232 if (rqsize != sizeof(rwrq))
234 error = copyin(pdata,&rwrq,rqsize);
237 iov.iov_base = rwrq.nrw_base;
238 iov.iov_len = rwrq.nrw_cnt;
241 auio.uio_offset = rwrq.nrw_offset;
242 auio.uio_resid = rwrq.nrw_cnt;
243 auio.uio_segflg = UIO_USERSPACE;
244 auio.uio_rw = (subfn == NCP_CONN_READ) ? UIO_READ : UIO_WRITE;
246 if (subfn == NCP_CONN_READ)
247 error = ncp_read(conn, &rwrq.nrw_fh, &auio, cred);
249 error = ncp_write(conn, &rwrq.nrw_fh, &auio, cred);
250 rwrq.nrw_cnt -= auio.uio_resid;
251 /*td->td_retval[0] = rwrq.nrw_cnt;*/
253 } /* case int_read/write */
254 case NCP_CONN_SETFLAGS: {
255 u_int16_t mask, flags;
257 error = copyin(pdata,&mask, sizeof(mask));
260 pdata += sizeof(mask);
261 error = copyin(pdata,&flags,sizeof(flags));
264 error = ncp_conn_lock(conn, td, cred, NCPM_WRITE);
267 if (mask & NCPFL_PERMANENT) {
268 conn->flags &= ~NCPFL_PERMANENT;
269 conn->flags |= (flags & NCPFL_PERMANENT);
271 if (mask & NCPFL_PRIMARY) {
272 error = ncp_conn_setprimary(conn, flags & NCPFL_PRIMARY);
274 ncp_conn_unlock(conn, td);
278 ncp_conn_unlock(conn, td);
281 case NCP_CONN_LOGIN: {
282 struct ncp_conn_login la;
284 if (rqsize != sizeof(la))
286 if (conn->flags & NCPFL_LOGGED)
288 if ((error = copyin(pdata,&la,rqsize)) != 0)
290 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE | NCPM_WRITE);
293 error = ncp_mod_login(conn, la.username, la.objtype,
294 la.password, td, td->td_ucred);
295 ncp_conn_unlock(conn, td);
298 case NCP_CONN_GETINFO: {
299 struct ncp_conn_stat ncs;
300 int len = sizeof(ncs);
302 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
305 ncp_conn_getinfo(conn, &ncs);
306 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
307 error = copyout(&ncs, &args->ioc_ncpbuf->packet, len);
308 ncp_conn_unlock(conn, td);
311 case NCP_CONN_GETUSER: {
314 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
317 len = (conn->li.user) ? strlen(conn->li.user) + 1 : 0;
318 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
320 error = copyout(conn->li.user,
321 &args->ioc_ncpbuf->packet, len);
323 ncp_conn_unlock(conn, td);
326 case NCP_CONN_CONN2REF: {
327 int len = sizeof(int);
329 error = ncp_conn_lock(conn, td, td->td_ucred, NCPM_READ);
332 copyout(&len, &args->ioc_ncpbuf->rpsize, sizeof(int));
334 error = copyout(&conn->nc_id,
335 &args->ioc_ncpbuf->packet, len);
337 ncp_conn_unlock(conn, td);
340 case NCP_CONN_FRAG: {
341 struct ncp_conn_frag nf;
343 if (rqsize != sizeof(nf))
345 if ((error = copyin(pdata, &nf, rqsize)) != 0) break;
346 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
349 error = ncp_conn_frag_rq(conn, td, &nf);
350 ncp_conn_unlock(conn, td);
351 copyout(&nf, &pdata, sizeof(nf));
352 td->td_retval[0] = error;
356 struct ncp_handle *newhp;
357 int len = sizeof(NWCONN_HANDLE);
359 error = ncp_conn_lock(conn, td, cred, NCPM_READ);
361 copyout(&len, &args->ioc_ncpbuf->rpsize, len);
362 error = ncp_conn_gethandle(conn, td, &newhp);
364 error = copyout(&newhp->nh_id,
365 args->ioc_ncpbuf->packet, len);
366 ncp_conn_unlock(conn, td);
369 case NCP_CONN_CONNCLOSE: {
370 error = ncp_conn_lock(conn, td, cred, NCPM_EXECUTE);
372 ncp_conn_puthandle(hp, td, 0);
373 error = ncp_conn_free(conn);
375 ncp_conn_unlock(conn, td);
385 sncp_conn_scan(struct thread *td, struct ncpioc_connscan *args)
387 int connHandle = 0, error;
388 struct ncp_conn_args li, *lip;
389 struct ncp_conn *conn;
390 struct ncp_handle *hp;
391 char *user = NULL, *password = NULL;
394 if (copyin(args->ioc_li, &li, sizeof(li)))
402 lip->server[sizeof(lip->server)-1]=0; /* just to make sure */
403 ncp_str_upper(lip->server);
405 user = ncp_str_dup(lip->user);
411 password = ncp_str_dup(lip->password);
412 if (password == NULL) {
414 free(user, M_NCPDATA);
417 ncp_str_upper(password);
420 lip->password = password;
422 error = ncp_conn_getbyli(lip, td, td->td_ucred, NCPM_EXECUTE, &conn);
423 if (!error) { /* already have this login */
424 ncp_conn_gethandle(conn, td, &hp);
425 connHandle = hp->nh_id;
426 ncp_conn_unlock(conn, td);
427 copyout(&connHandle, args->ioc_connhandle, sizeof(connHandle));
430 free(user, M_NCPDATA);
432 free(password, M_NCPDATA);
438 ncp_conn_frag_rq(struct ncp_conn *conn, struct thread *td,
439 struct ncp_conn_frag *nfp)
444 int error, i, rpsize;
446 error = ncp_rq_alloc(nfp->fn, conn, td, td->td_ucred, &rqp);
449 for(fp = nfp->rqf, i = 0; i < nfp->rqfcnt; i++, fp++) {
450 error = mb_put_mem(&rqp->rq, (caddr_t)fp->fragAddress, fp->fragSize, MB_MUSER);
454 rqp->nr_flags |= NCPR_DONTFREEONERR;
455 error = ncp_request(rqp);
458 rpsize = rqp->nr_rpsize;
459 if (rpsize && nfp->rpfcnt) {
460 for(fp = nfp->rpf, i = 0; i < nfp->rpfcnt; i++, fp++) {
461 error = copyin(&fp->fragSize, &fsize, sizeof (fsize));
464 fsize = min(fsize, rpsize);
465 error = md_get_mem(&rqp->rp, (caddr_t)fp->fragAddress, fsize, MB_MUSER);
469 error = copyout(&fsize, &fp->fragSize, sizeof (fsize));
474 nfp->cs = rqp->nr_cs;
475 nfp->cc = rqp->nr_cc;
486 if ((error = ncp_init()) != 0)
488 ncp_dev = make_dev(&ncp_cdevsw, 0, 0, 0, 0666, "ncp");
489 printf("ncp_load: loaded\n");
501 destroy_dev(ncp_dev);
502 printf("ncp_unload: unloaded\n");
507 ncp_mod_handler(module_t mod, int type, void *data)
516 error = ncp_unload();
524 static moduledata_t ncp_mod = {
529 DECLARE_MODULE(ncp, ncp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);