dd34190458c4d0626e38dcc30e6e49690e34523f
[dragonfly.git] / lib / libncp / ncpl_subr.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/lib/libncp/ncpl_subr.c,v 1.3 2000/01/01 14:21:31 bp Exp $
33  * $DragonFly: src/lib/libncp/ncpl_subr.c,v 1.5 2008/09/30 16:57:05 swildner Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/errno.h>
39 #include <sys/sysctl.h>
40 #include <sys/syscall.h>
41 #include <unistd.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdarg.h>
47
48 #include <netncp/ncp_lib.h>
49 #include <netncp/ncp_rcfile.h>
50 #include <netncp/ncp_nls.h>
51 /*#include <netncp/ncp_cfg.h>*/
52 #include "ncp_mod.h"
53
54 extern char *__progname;
55
56 int sysentoffset;
57
58 void
59 ncp_add_word_lh(struct ncp_buf *conn, u_int16_t x) {
60         setwle(conn->packet, conn->rqsize, x);
61         conn->rqsize += 2;
62         return;
63 }
64
65 void
66 ncp_add_dword_lh(struct ncp_buf *conn, u_int32_t x) {
67         setdle(conn->packet, conn->rqsize, x);
68         conn->rqsize += 4;
69         return;
70 }
71
72 void
73 ncp_add_word_hl(struct ncp_buf *conn, u_int16_t x){
74         setwbe(conn->packet, conn->rqsize, x);
75         conn->rqsize += 2;
76         return;
77 }
78
79 void
80 ncp_add_dword_hl(struct ncp_buf *conn, u_int32_t x) {
81         setdbe(conn->packet, conn->rqsize, x);
82         conn->rqsize += 4;
83         return;
84 }
85
86 void
87 ncp_add_mem(struct ncp_buf *conn, const void *source, int size) {
88         memcpy(conn->packet+conn->rqsize, source, size);
89         conn->rqsize += size;
90         return;
91 }
92
93 void
94 ncp_add_mem_nls(struct ncp_buf *conn, const void *source, int size) {
95         ncp_nls_mem_u2n(conn->packet+conn->rqsize, source, size);
96         conn->rqsize += size;
97         return;
98 }
99
100 void
101 ncp_add_pstring(struct ncp_buf *conn, const char *s) {
102         int len = strlen(s);
103         if (len > 255) {
104                 ncp_printf("ncp_add_pstring: string too long: %s\n", s);
105                 len = 255;
106         }
107         ncp_add_byte(conn, len);
108         ncp_add_mem(conn, s, len);
109         return;
110 }
111
112 void
113 ncp_add_handle_path(struct ncp_buf *conn, nuint32 volNumber, nuint32 dirNumber,
114         int handleFlag, const char *path)
115 {
116         ncp_add_byte(conn, volNumber);
117         ncp_add_dword_lh(conn, dirNumber);
118         ncp_add_byte(conn, handleFlag);
119         if (path) {
120                 ncp_add_byte(conn, 1);          /* 1 component */
121                 ncp_add_pstring(conn, path);
122         } else {
123                 ncp_add_byte(conn, 0);
124         }
125 }
126
127 void
128 ncp_init_request(struct ncp_buf *conn) {
129         conn->rqsize = 0;
130         conn->rpsize = 0;
131 }
132
133 void
134 ncp_init_request_s(struct ncp_buf *conn, int subfn) {
135         ncp_init_request(conn);
136         ncp_add_word_lh(conn, 0);
137         ncp_add_byte(conn, subfn);
138 }
139
140 u_int16_t
141 ncp_reply_word_hl(struct ncp_buf *conn, int offset) {
142         return getwbe(ncp_reply_data(conn, offset), 0);
143 }
144
145 u_int16_t
146 ncp_reply_word_lh(struct ncp_buf *conn, int offset) {
147         return getwle(ncp_reply_data(conn, offset), 0);
148 }
149
150 u_int32_t
151 ncp_reply_dword_hl(struct ncp_buf *conn, int offset) {
152         return getdbe(ncp_reply_data(conn, offset), 0);
153 }
154
155 u_int32_t
156 ncp_reply_dword_lh(struct ncp_buf *conn, int offset) {
157         return getdle(ncp_reply_data(conn, offset), 0);
158 }
159
160
161 int
162 ncp_connect(struct ncp_conn_args *li, int *connHandle) {
163         return syscall(NCP_CONNECT,li,connHandle);
164 }
165
166 int
167 ncp_disconnect(int cH) {
168         DECLARE_RQ;
169
170         ncp_init_request(conn);
171         ncp_add_byte(conn, NCP_CONN_CONNCLOSE);
172         return ncp_conn_request(cH, conn);
173 }
174
175 int
176 ncp_request(int connHandle,int function, struct ncp_buf *ncpbuf){
177         int err = syscall(SNCP_REQUEST,connHandle,function,ncpbuf);
178         return (err<0) ? errno : 0;
179 }
180
181 int
182 ncp_conn_request(int connHandle, struct ncp_buf *ncpbuf){
183         return syscall(SNCP_REQUEST, connHandle, NCP_CONN, ncpbuf);
184 }
185
186 int
187 ncp_conn_scan(struct ncp_conn_loginfo *li, int *connid) {
188         return syscall(NCP_CONNSCAN,li, connid);
189 }
190
191 NWCCODE
192 NWRequest(NWCONN_HANDLE cH, nuint16 fn,
193         nuint16 nrq, NW_FRAGMENT* rq, 
194         nuint16 nrp, NW_FRAGMENT* rp) 
195 {
196         int error;
197         struct ncp_conn_frag nf;
198         DECLARE_RQ;
199
200         ncp_init_request(conn);
201         ncp_add_byte(conn, NCP_CONN_FRAG);
202         nf.fn = fn;
203         nf.rqfcnt = nrq;
204         nf.rqf = rq;
205         nf.rpf = rp;
206         nf.rpfcnt = nrp;
207         ncp_add_mem(conn, &nf, sizeof(nf));
208         error = ncp_conn_request(cH, conn);
209         return error;
210 }
211
212
213 int
214 ncp_initlib(void){
215         int error;
216         size_t len = sizeof(sysentoffset);
217         int kv;
218         size_t kvlen = sizeof(kv);
219         static int ncp_initialized;
220
221         if (ncp_initialized)
222                 return 0;
223         error = sysctlbyname("net.ncp.sysent", &sysentoffset, &len, NULL, 0);
224         if (error) {
225                 fprintf(stderr, "%s: can't find kernel module\n", __func__);
226                 return error;
227         }
228         error = sysctlbyname("net.ncp.version", &kv, &kvlen, NULL, 0);
229         if (error) {
230                 fprintf(stderr, "%s: kernel module is old, please recompile it.\n", __func__);
231                 return error;
232         }
233         if (NCP_VERSION != kv) {
234                 fprintf(stderr, "%s: kernel module version(%d) don't match library(%d).\n", __func__, kv, NCP_VERSION);
235                 return EINVAL;
236         }
237         if ((error = ncp_nls_setrecode(0)) != 0) {
238                 fprintf(stderr, "%s: can't initialise recode\n", __func__);
239                 return error;
240         }
241         if ((error = ncp_nls_setlocale("")) != 0) {
242                 fprintf(stderr, "%s: can't initialise locale\n", __func__);
243                 return error;
244         }
245         ncp_initialized++;
246         return 0;
247 }
248
249
250 /*
251  */
252 int     ncp_opterr = 1,         /* if error message should be printed */
253         ncp_optind = 1,         /* index into parent argv vector */
254         ncp_optopt,                     /* character checked for validity */
255         ncp_optreset;           /* reset getopt */
256 char    *ncp_optarg;            /* argument associated with option */
257
258 #define BADCH   (int)'?'
259 #define BADARG  (int)':'
260 #define EMSG    ""
261
262 int
263 ncp_getopt(int nargc, char * const *nargv, const char *ostr)
264 {
265         static char *place = EMSG;              /* option letter processing */
266         char *oli;                              /* option letter list index */
267         int tmpind;
268
269         if (ncp_optreset || !*place) {          /* update scanning pointer */
270                 ncp_optreset = 0;
271                 tmpind = ncp_optind;
272                 while (1) {
273                         if (tmpind >= nargc) {
274                                 place = EMSG;
275                                 return (-1);
276                         }
277                         if (*(place = nargv[tmpind]) != '-') {
278                                 tmpind++;
279                                 continue;       /* lookup next option */
280                         }
281                         if (place[1] && *++place == '-') {      /* found "--" */
282                                 ncp_optind = ++tmpind;
283                                 place = EMSG;
284                                 return (-1);
285                         }
286                         ncp_optind = tmpind;
287                         break;
288                 }
289         }                                       /* option letter okay? */
290         if ((ncp_optopt = (int)*place++) == (int)':' ||
291             !(oli = strchr(ostr, ncp_optopt))) {
292                 /*
293                  * if the user didn't specify '-' as an option,
294                  * assume it means -1.
295                  */
296                 if (ncp_optopt == (int)'-')
297                         return (-1);
298                 if (!*place)
299                         ++ncp_optind;
300                 if (ncp_opterr && *ostr != ':')
301                         (void)fprintf(stderr,
302                             "%s: illegal option -- %c\n", __progname, ncp_optopt);
303                 return (BADCH);
304         }
305         if (*++oli != ':') {                    /* don't need argument */
306                 ncp_optarg = NULL;
307                 if (!*place)
308                         ++ncp_optind;
309         }
310         else {                                  /* need an argument */
311                 if (*place)                     /* no white space */
312                         ncp_optarg = place;
313                 else if (nargc <= ++ncp_optind) {       /* no arg */
314                         place = EMSG;
315                         if (*ostr == ':')
316                                 return (BADARG);
317                         if (ncp_opterr)
318                                 (void)fprintf(stderr,
319                                     "%s: option requires an argument -- %c\n",
320                                     __progname, ncp_optopt);
321                         return (BADCH);
322                 }
323                 else                            /* white space */
324                         ncp_optarg = nargv[ncp_optind];
325                 place = EMSG;
326                 ++ncp_optind;
327         }
328         return (ncp_optopt);                    /* dump back option letter */
329 }
330 /*
331  * misc options parsing routines
332  */
333 int
334 ncp_args_parserc(struct ncp_args *na, char *sect, ncp_setopt_t *set_callback) {
335         int len, error;
336
337         for (; na->opt; na++) {
338                 switch (na->at) {
339                     case NCA_STR:
340                         if (rc_getstringptr(ncp_rc,sect,na->name,&na->str) == 0) {
341                                 len = strlen(na->str);
342                                 if (len > na->ival) {
343                                         fprintf(stderr,"rc: Argument for option '%c' (%s) too long\n",na->opt,na->name);
344                                         return EINVAL;
345                                 }
346                                 set_callback(na);
347                         }
348                         break;
349                     case NCA_BOOL:
350                         error = rc_getbool(ncp_rc,sect,na->name,&na->ival);
351                         if (error == ENOENT) break;
352                         if (error) return EINVAL;
353                         set_callback(na);
354                         break;
355                     case NCA_INT:
356                         if (rc_getint(ncp_rc,sect,na->name,&na->ival) == 0) {
357                                 if (((na->flag & NAFL_HAVEMIN) && 
358                                      (na->ival < na->min)) || 
359                                     ((na->flag & NAFL_HAVEMAX) && 
360                                      (na->ival > na->max))) {
361                                         fprintf(stderr,"rc: Argument for option '%c' (%s) should be in [%d-%d] range\n",na->opt,na->name,na->min,na->max);
362                                         return EINVAL;
363                                 }
364                                 set_callback(na);
365                         };
366                         break;
367                     default:
368                         break;
369                 }
370         }
371         return 0;
372 }
373
374 int
375 ncp_args_parseopt(struct ncp_args *na, int opt, char *optarg, ncp_setopt_t *set_callback) {
376         int len;
377
378         for (; na->opt; na++) {
379                 if (na->opt != opt) continue;
380                 switch (na->at) {
381                     case NCA_STR:
382                         na->str = optarg;
383                         if (optarg) {
384                                 len = strlen(na->str);
385                                 if (len > na->ival) {
386                                         fprintf(stderr,"opt: Argument for option '%c' (%s) too long\n",na->opt,na->name);
387                                         return EINVAL;
388                                 }
389                                 set_callback(na);
390                         }
391                         break;
392                     case NCA_BOOL:
393                         na->ival = 0;
394                         set_callback(na);
395                         break;
396                     case NCA_INT:
397                         errno = 0;
398                         na->ival = strtol(optarg, NULL, 0);
399                         if (errno) {
400                                 fprintf(stderr,"opt: Invalid integer value for option '%c' (%s).\n",na->opt,na->name);
401                                 return EINVAL;
402                         }
403                         if (((na->flag & NAFL_HAVEMIN) && 
404                              (na->ival < na->min)) || 
405                             ((na->flag & NAFL_HAVEMAX) && 
406                              (na->ival > na->max))) {
407                                 fprintf(stderr,"opt: Argument for option '%c' (%s) should be in [%d-%d] range\n",na->opt,na->name,na->min,na->max);
408                                 return EINVAL;
409                         }
410                         set_callback(na);
411                         break;
412                     default:
413                         break;
414                 }
415                 break;
416         }
417         return 0;
418 }
419
420 /*
421  * Print a (descriptive) error message
422  * error values:
423  *         0 - no specific error code available;
424  *  -999..-1 - NDS error
425  *  1..32767 - system error
426  *  the rest - requester error;
427  */
428 void
429 ncp_error(char *fmt, int error,...) {
430         va_list ap;
431
432         fprintf(stderr, "%s: ", __progname);
433         va_start(ap, error);
434         vfprintf(stderr, fmt, ap);
435         va_end(ap);
436         if (error == -1)
437                 error = errno;
438         if (error > -1000 && error < 0) {
439                 fprintf(stderr, ": dserr = %d\n", error);
440         } else if (error & 0x8000) {
441                 fprintf(stderr, ": nwerr = %04x\n", error);
442         } else if (error) {
443                 fprintf(stderr, ": syserr = %s\n", strerror(error));
444         } else
445                 fprintf(stderr, "\n");
446 }
447
448 char *
449 ncp_printb(char *dest, int flags, const struct ncp_bitname *bnp) {
450         int first = 1;
451
452         strcpy(dest, "<");
453         for(; bnp->bn_bit; bnp++) {
454                 if (flags & bnp->bn_bit) {
455                         strcat(dest, bnp->bn_name);
456                         first = 0;
457                 }
458                 if (!first && (flags & bnp[1].bn_bit))
459                         strcat(dest, "|");
460         }
461         strcat(dest, ">");
462         return dest;
463 }