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