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