Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / netproto / ncp / ncp_mod.c
1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
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  * $FreeBSD: src/sys/netncp/ncp_mod.c,v 1.2 1999/10/12 10:36:59 bp Exp $
33  * $DragonFly: src/sys/netproto/ncp/ncp_mod.c,v 1.2 2003/06/17 04:28:53 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
38 #include <sys/sysent.h>
39 #include <sys/proc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/uio.h>
44
45 #include <netncp/ncp.h>
46 #include <netncp/ncp_conn.h>
47 #include <netncp/ncp_subr.h>
48 #include <netncp/ncp_ncp.h>
49 #include <netncp/ncp_user.h>
50 #include <netncp/ncp_rq.h>
51 #include <netncp/ncp_nls.h>
52
53 int ncp_version = NCP_VERSION;
54
55 static int ncp_sysent;
56
57 SYSCTL_NODE(_net, OID_AUTO, ncp, CTLFLAG_RW, NULL, "NetWare requester");
58 SYSCTL_INT(_net_ncp, OID_AUTO, sysent, CTLFLAG_RD, &ncp_sysent, 0, "");
59 SYSCTL_INT(_net_ncp, OID_AUTO, version, CTLFLAG_RD, &ncp_version, 0, "");
60
61 static int
62 ncp_conn_frag_rq(struct ncp_conn *conn, struct proc *p, struct ncp_conn_frag *nfp);
63
64 /*
65  * Attach to NCP server
66  */
67 struct sncp_connect_args {
68         struct ncp_conn_args *li;
69         int *connHandle;
70 };
71
72 static int 
73 __P(sncp_connect(struct proc *p, struct sncp_connect_args *uap)){
74         int connHandle = 0, error;
75         struct ncp_conn *conn;
76         struct ncp_handle *handle;
77         struct ncp_conn_args li;
78
79         checkbad(copyin(uap->li,&li,sizeof(li)));
80         checkbad(copyout(&connHandle,uap->connHandle,sizeof(connHandle))); /* check before */
81         li.password = li.user = NULL;
82         error = ncp_conn_getattached(&li, p, p->p_ucred, NCPM_WRITE | NCPM_EXECUTE, &conn);
83         if (error) {
84                 error = ncp_connect(&li, p, p->p_ucred, &conn);
85         }
86         if (!error) {
87                 error = ncp_conn_gethandle(conn, p, &handle);
88                 copyout(&handle->nh_id, uap->connHandle, sizeof(uap->connHandle));
89                 ncp_conn_unlock(conn,p);
90         }
91 bad:
92         p->p_retval[0]=error;
93         return error;
94 }
95
96 struct sncp_request_args {
97         int connHandle;
98         int fn;
99         struct ncp_buf *ncpbuf;
100 };
101
102 static int ncp_conn_handler(struct proc *p, struct sncp_request_args *uap,
103         struct ncp_conn *conn, struct ncp_handle *handle);
104
105 static int
106 __P(sncp_request(struct proc *p, struct sncp_request_args *uap)){
107         int error = 0, rqsize;
108         struct ncp_conn *conn;
109         struct ncp_handle *handle;
110         DECLARE_RQ;
111
112         error = ncp_conn_findhandle(uap->connHandle,p,&handle);
113         if (error) return error;
114         conn = handle->nh_conn;
115         if (uap->fn == NCP_CONN)
116                 return ncp_conn_handler(p, uap, conn, handle);
117         error = copyin(&uap->ncpbuf->rqsize, &rqsize, sizeof(int));
118         if (error) return(error);
119         error = ncp_conn_lock(conn,p,p->p_ucred,NCPM_EXECUTE);
120         if (error) return(error);
121         ncp_rq_head(rqp,NCP_REQUEST,uap->fn,p,p->p_ucred);
122         if (rqsize)
123                 error = ncp_rq_usermem(rqp,(caddr_t)uap->ncpbuf->packet, rqsize);
124         if (!error) {
125                 error = ncp_request(conn, rqp);
126                 if (error == 0 && rqp->rpsize)
127                         ncp_rp_usermem(rqp, (caddr_t)uap->ncpbuf->packet, 
128                                 rqp->rpsize);
129                 copyout(&rqp->cs, &uap->ncpbuf->cs, sizeof(rqp->cs));
130                 copyout(&rqp->cc, &uap->ncpbuf->cc, sizeof(rqp->cc));
131                 copyout(&rqp->rpsize, &uap->ncpbuf->rpsize, sizeof(rqp->rpsize));
132         }
133         ncp_rq_done(rqp);
134         ncp_conn_unlock(conn,p);
135         return error;
136 }
137
138 static int
139 ncp_conn_handler(struct proc *p, struct sncp_request_args *uap,
140         struct ncp_conn *conn, struct ncp_handle *hp)
141 {
142         int error=0, rqsize, subfn;
143         struct ucred *cred;
144         
145         char *pdata;
146
147         cred = p->p_ucred;
148         error = copyin(&uap->ncpbuf->rqsize, &rqsize, sizeof(int));
149         if (error) return(error);
150         error = 0;
151         pdata = uap->ncpbuf->packet;
152         subfn = *(pdata++) & 0xff;
153         rqsize--;
154         switch (subfn) {
155             case NCP_CONN_READ: case NCP_CONN_WRITE: {
156                 struct ncp_rw rwrq;
157                 struct uio auio;
158                 struct iovec iov;
159         
160                 if (rqsize != sizeof(rwrq)) return (EBADRPC);   
161                 error = copyin(pdata,&rwrq,rqsize);
162                 if (error) return (error);
163                 iov.iov_base = rwrq.nrw_base;
164                 iov.iov_len = rwrq.nrw_cnt;
165                 auio.uio_iov = &iov;
166                 auio.uio_iovcnt = 1;
167                 auio.uio_offset = rwrq.nrw_offset;
168                 auio.uio_resid = rwrq.nrw_cnt;
169                 auio.uio_segflg = UIO_USERSPACE;
170                 auio.uio_rw = (subfn == NCP_CONN_READ) ? UIO_READ : UIO_WRITE;
171                 auio.uio_procp = p;
172                 error = ncp_conn_lock(conn,p,cred,NCPM_EXECUTE);
173                 if (error) return(error);
174                 if (subfn == NCP_CONN_READ)
175                         error = ncp_read(conn, &rwrq.nrw_fh, &auio, cred);
176                 else
177                         error = ncp_write(conn, &rwrq.nrw_fh, &auio, cred);
178                 rwrq.nrw_cnt -= auio.uio_resid;
179                 ncp_conn_unlock(conn,p);
180                 p->p_retval[0] = rwrq.nrw_cnt;
181                 break;
182             } /* case int_read/write */
183             case NCP_CONN_SETFLAGS: {
184                 u_int16_t mask, flags;
185
186                 error = copyin(pdata,&mask, sizeof(mask));
187                 if (error) return error;
188                 pdata += sizeof(mask);
189                 error = copyin(pdata,&flags,sizeof(flags));
190                 if (error) return error;
191                 error = ncp_conn_lock(conn,p,cred,NCPM_WRITE);
192                 if (error) return error;
193                 if (mask & NCPFL_PERMANENT) {
194                         conn->flags &= ~NCPFL_PERMANENT;
195                         conn->flags |= (flags & NCPFL_PERMANENT);
196                 }
197                 if (mask & NCPFL_PRIMARY) {
198                         error = ncp_conn_setprimary(conn, flags & NCPFL_PRIMARY);
199                         if (error) {
200                                 ncp_conn_unlock(conn,p);
201                                 break;
202                         }
203                 }
204                 ncp_conn_unlock(conn,p);
205                 break;
206             }
207             case NCP_CONN_LOGIN: {
208                 struct ncp_conn_login la;
209
210                 if (rqsize != sizeof(la)) return (EBADRPC);     
211                 if ((error = copyin(pdata,&la,rqsize)) != 0) break;
212                 error = ncp_conn_lock(conn, p, cred, NCPM_EXECUTE | NCPM_WRITE);
213                 if (error) return error;
214                 error = ncp_login(conn, la.username, la.objtype, la.password, p, p->p_ucred);
215                 ncp_conn_unlock(conn, p);
216                 p->p_retval[0] = error;
217                 break;
218             }
219             case NCP_CONN_GETINFO: {
220                 struct ncp_conn_stat ncs;
221                 int len = sizeof(ncs);
222
223                 error = ncp_conn_lock(conn, p, p->p_ucred, NCPM_READ);
224                 if (error) return error;
225                 ncp_conn_getinfo(conn, &ncs);
226                 copyout(&len, &uap->ncpbuf->rpsize, sizeof(int));
227                 error = copyout(&ncs, &uap->ncpbuf->packet, len);
228                 ncp_conn_unlock(conn, p);
229                 break;
230             }
231             case NCP_CONN_GETUSER: {
232                 int len;
233
234                 error = ncp_conn_lock(conn, p, p->p_ucred, NCPM_READ);
235                 if (error) return error;
236                 len = (conn->li.user) ? strlen(conn->li.user) + 1 : 0;
237                 copyout(&len, &uap->ncpbuf->rpsize, sizeof(int));
238                 if (len) {
239                         error = copyout(conn->li.user, &uap->ncpbuf->packet, len);
240                 }
241                 ncp_conn_unlock(conn, p);
242                 break;
243             }
244             case NCP_CONN_CONN2REF: {
245                 int len = sizeof(int);
246
247                 error = ncp_conn_lock(conn, p, p->p_ucred, NCPM_READ);
248                 if (error) return error;
249                 copyout(&len, &uap->ncpbuf->rpsize, sizeof(int));
250                 if (len) {
251                         error = copyout(&conn->nc_id, &uap->ncpbuf->packet, len);
252                 }
253                 ncp_conn_unlock(conn, p);
254                 break;
255             }
256             case NCP_CONN_FRAG: {
257                 struct ncp_conn_frag nf;
258
259                 if (rqsize != sizeof(nf)) return (EBADRPC);     
260                 if ((error = copyin(pdata, &nf, rqsize)) != 0) break;
261                 error = ncp_conn_lock(conn, p, cred, NCPM_EXECUTE);
262                 if (error) return error;
263                 error = ncp_conn_frag_rq(conn, p, &nf);
264                 ncp_conn_unlock(conn, p);
265                 copyout(&nf, &pdata, sizeof(nf));
266                 p->p_retval[0] = error;
267                 break;
268             }
269             case NCP_CONN_DUP: {
270                 struct ncp_handle *newhp;
271                 int len = sizeof(NWCONN_HANDLE);
272
273                 error = ncp_conn_lock(conn, p, cred, NCPM_READ);
274                 if (error) break;
275                 copyout(&len, &uap->ncpbuf->rpsize, len);
276                 error = ncp_conn_gethandle(conn, p, &newhp);
277                 if (!error)
278                         error = copyout(&newhp->nh_id, uap->ncpbuf->packet, len);
279                 ncp_conn_unlock(conn,p);
280                 break;
281             }
282             case NCP_CONN_CONNCLOSE: {
283                 error = ncp_conn_lock(conn, p, cred, NCPM_EXECUTE);
284                 if (error) break;
285                 ncp_conn_puthandle(hp, p, 0);
286                 error = ncp_disconnect(conn);
287                 if (error)
288                         ncp_conn_unlock(conn, p);
289                 break;
290             }
291             default:
292                     error = EOPNOTSUPP;
293         }
294         return error;
295 }
296
297 struct sncp_conn_scan_args {
298         struct ncp_conn_args *li;
299         int *connHandle;
300 };
301
302 static int 
303 __P(sncp_conn_scan(struct proc *p, struct sncp_conn_scan_args *uap)){
304         int connHandle = 0, error;
305         struct ncp_conn_args li, *lip;
306         struct ncp_conn *conn;
307         struct ncp_handle *hp;
308         char *user = NULL, *password = NULL;
309
310         if (uap->li) {
311                 if (copyin(uap->li,&li,sizeof(li))) return EFAULT;
312                 lip = &li;
313         } else {
314                 lip = NULL;
315         }
316
317         if (lip != NULL) {
318                 lip->server[sizeof(lip->server)-1]=0; /* just to make sure */
319                 ncp_str_upper(lip->server);
320                 if (lip->user) {
321                         user = ncp_str_dup(lip->user);
322                         if (user == NULL) return EINVAL;
323                         ncp_str_upper(user);
324                 }
325                 if (lip->password) {
326                         password = ncp_str_dup(lip->password);
327                         if (password == NULL) {
328                                 if (user)
329                                         free(user, M_NCPDATA);
330                                 return EINVAL;
331                         }
332                         ncp_str_upper(password);
333                 }
334                 lip->user = user;
335                 lip->password = password;
336         }
337         error = ncp_conn_getbyli(lip,p,p->p_ucred,NCPM_EXECUTE,&conn);
338         if (!error) {           /* already have this login */
339                 ncp_conn_gethandle(conn, p, &hp);
340                 connHandle = hp->nh_id;
341                 ncp_conn_unlock(conn,p);
342                 copyout(&connHandle,uap->connHandle,sizeof(connHandle));
343         }
344         if (user) free(user, M_NCPDATA);
345         if (password) free(password, M_NCPDATA);
346         p->p_retval[0] = error;
347         return error;
348
349 }
350
351 int
352 ncp_conn_frag_rq(struct ncp_conn *conn, struct proc *p, struct ncp_conn_frag *nfp){
353         int error = 0, i, rpsize;
354         u_int32_t fsize;
355         NW_FRAGMENT *fp;
356         DECLARE_RQ;
357
358         ncp_rq_head(rqp,NCP_REQUEST,nfp->fn,p,p->p_ucred);
359         if (nfp->rqfcnt) {
360                 for(fp = nfp->rqf, i = 0; i < nfp->rqfcnt; i++, fp++) {
361                         checkbad(ncp_rq_usermem(rqp,(caddr_t)fp->fragAddress, fp->fragSize));
362                 }
363         }
364         checkbad(ncp_request(conn, rqp));
365         rpsize = rqp->rpsize;
366         if (rpsize && nfp->rpfcnt) {
367                 for(fp = nfp->rpf, i = 0; i < nfp->rpfcnt; i++, fp++) {
368                         checkbad(copyin(&fp->fragSize, &fsize, sizeof (fsize)));
369                         fsize = min(fsize, rpsize);
370                         checkbad(ncp_rp_usermem(rqp,(caddr_t)fp->fragAddress, fsize));
371                         rpsize -= fsize;
372                         checkbad(copyout(&fsize, &fp->fragSize, sizeof (fsize)));
373                 }
374         }
375         nfp->cs = rqp->cs;
376         nfp->cc = rqp->cc;
377         NCP_RQ_EXIT;
378         return error;
379 }
380
381 /*
382  * Internal functions, here should be all calls that do not require connection.
383  * To simplify possible future movement to cdev, we use IOCTL macros.
384  * Pretty much of this stolen from ioctl() function.
385  */
386 struct sncp_intfn_args {
387         u_long  com;
388         caddr_t data;
389 };
390
391 static int
392 sncp_intfn(struct proc *p, struct sncp_intfn_args *uap)
393 {
394         return ENOSYS;
395 }
396 /*
397  * define our new system calls
398  */
399 static struct sysent newent[] = {
400         {2,     (sy_call_t*)sncp_conn_scan},
401         {2,     (sy_call_t*)sncp_connect},
402         {2,     (sy_call_t*)sncp_intfn},
403         {3,     (sy_call_t*)sncp_request}
404 };
405
406 #define SC_SIZE sizeof(newent)/sizeof(struct sysent)
407 /*
408  * Miscellaneous modules must have their own save areas...
409  */
410 static struct sysent    oldent[SC_SIZE];        /* save are for old callslot entry*/
411
412 /*
413  * Number of syscall entries for a.out executables
414  */
415 /*#define nsysent SYS_MAXSYSCALL*/
416 #define nsysent (aout_sysvec.sv_size)
417
418
419 static int
420 ncp_load(void) {
421         int i, ff, scnt, err=0;
422
423         while(1) {
424                 /* Search the table looking for an enough number of slots... */
425                 for (scnt=0, ff = -1, i = 0; i < nsysent; i++) {
426                         if (sysent[i].sy_call == (sy_call_t *)lkmnosys) {
427                                 if (ff == -1) {
428                                     ff = i;
429                                     scnt = 1;
430                                 } else {
431                                     scnt++;
432                                     if (scnt == SC_SIZE) break;
433                                 }
434                         } else {
435                                 ff = -1;
436                         }
437                 }
438                 /* out of allocable slots?*/
439                 if(i == nsysent || ff == -1) {
440                         err = ENFILE;
441                         break;
442                 }
443                 err = ncp_init();
444                 if (err) break;
445                 bcopy(&sysent[ff], &oldent, sizeof(struct sysent)*SC_SIZE);
446                 bcopy(&newent, &sysent[ff], sizeof(struct sysent)*SC_SIZE);
447                 ncp_sysent = ff;        /* slot in sysent[]*/
448                 printf("ncp_load: [%d-%d]\n",ff,i);
449                 break;
450         }
451
452         return( err);
453 }
454
455 static int
456 ncp_unload(void) {
457         ncp_done();
458         bcopy(&oldent, &sysent[ncp_sysent], sizeof(struct sysent) * SC_SIZE);
459         printf( "ncp_unload: unloaded\n");
460         return 0;
461 }
462
463 static int
464 ncp_mod_handler(module_t mod, int type, void *data)
465 {
466         int error;
467
468         switch (type) {
469             case MOD_LOAD:
470                 error = ncp_load();
471                 break;
472             case MOD_UNLOAD:
473                 error = ncp_unload();
474                 break;
475             default:
476                 error = EINVAL;
477         }
478         return error;
479 }
480                                                                \
481 static moduledata_t ncp_mod = {
482         "ncp",
483         ncp_mod_handler,
484         NULL
485 };
486 DECLARE_MODULE(ncp, ncp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);