| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <xpap.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
static XPA xpahead=NULL; |
| 19 |
|
| 20 |
|
| 21 |
static int stimeout=XPA_SHORT_TIMEOUT; |
| 22 |
static int ltimeout=XPA_LONG_TIMEOUT; |
| 23 |
static int ctimeout=XPA_CONNECT_TIMEOUT; |
| 24 |
static int verbosity=XPA_VERBOSITY; |
| 25 |
static int guseacl=1; |
| 26 |
static int etimestamp=0; |
| 27 |
static int nsregister=1; |
| 28 |
static int sigusr1=1; |
| 29 |
static int vercheck=1; |
| 30 |
static char *tmpdir=NULL; |
| 31 |
#if HAVE_ATEXIT |
| 32 |
static int atexitinit=0; |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
static char activefds[FD_SETSIZE]; |
| 37 |
static char nsmethod[SZ_LINE]; |
| 38 |
static char nsusers[SZ_LINE]; |
| 39 |
|
| 40 |
|
| 41 |
static int mtype=0; |
| 42 |
|
| 43 |
|
| 44 |
static int swidth=FD_SETSIZE; |
| 45 |
|
| 46 |
|
| 47 |
extern int use_localhost; |
| 48 |
|
| 49 |
|
| 50 |
char *xpaMessbuf[] = { |
| 51 |
"oh, what a mess we've made!", |
| 52 |
"authentication failed", |
| 53 |
"xpa connection refused", |
| 54 |
"can't resolve host name for xpa", |
| 55 |
"can't read initialization info for xpa", |
| 56 |
"invalid xpa command in initialization string", |
| 57 |
"no receive function defined for this xpa", |
| 58 |
"no send function defined for this xpa", |
| 59 |
"no info function defined for this xpa", |
| 60 |
"undefined command for this xpa", |
| 61 |
"missing command for this xpa", |
| 62 |
"name does not match target template", |
| 63 |
"can't create data channel socket", |
| 64 |
"could not read data buf (possible timeout)", |
| 65 |
"illegal command or command switch" |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
static char ctimebuf[SZ_LINE]; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#ifdef ANSI_FUNC |
| 84 |
static int |
| 85 |
XPAProxyConnect(XPA xpa, char *method, |
| 86 |
unsigned int *rip, unsigned short *rport, char *rname) |
| 87 |
#else |
| 88 |
static int XPAProxyConnect(xpa, method, rip, rport, rname) |
| 89 |
XPA xpa; |
| 90 |
char *method; |
| 91 |
unsigned int *rip; |
| 92 |
unsigned short *rport; |
| 93 |
char *rname; |
| 94 |
#endif |
| 95 |
{ |
| 96 |
int ofd; |
| 97 |
int tries=0; |
| 98 |
int keep_alive=1; |
| 99 |
unsigned int ip; |
| 100 |
unsigned short port; |
| 101 |
struct sockaddr_in sock_in; |
| 102 |
#if HAVE_SYS_UN_H |
| 103 |
struct sockaddr_un sock_un; |
| 104 |
#endif |
| 105 |
|
| 106 |
FPRINTF((stderr, "%sXPAProxyConnect to %s\n", _sp, method)); |
| 107 |
|
| 108 |
if( rip ) *rip = 0; |
| 109 |
if( rport ) *rport = 0; |
| 110 |
if( rname ) *rname = '\0'; |
| 111 |
|
| 112 |
switch(XPAMethod(method)){ |
| 113 |
case XPA_INET: |
| 114 |
again1: |
| 115 |
if( !XPAParseIpPort(method, &ip, &port) ){ |
| 116 |
goto error; |
| 117 |
} |
| 118 |
|
| 119 |
if( (ip == gethostip("$host")) && (tries == 0) ){ |
| 120 |
ip = gethostip("$localhost"); |
| 121 |
} |
| 122 |
|
| 123 |
if( (ofd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){ |
| 124 |
PERROR(("XPAProxyConnect: socket()")); |
| 125 |
goto error; |
| 126 |
} |
| 127 |
setsockopt(ofd, SOL_SOCKET, SO_KEEPALIVE, |
| 128 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 129 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 130 |
sock_in.sin_family = AF_INET; |
| 131 |
sock_in.sin_addr.s_addr = htonl(ip); |
| 132 |
sock_in.sin_port = htons(port); |
| 133 |
|
| 134 |
if(connect(ofd, (struct sockaddr *)&sock_in, sizeof(sock_in))<0){ |
| 135 |
xclose(ofd); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if( tries < 2 ){ |
| 140 |
tries++; |
| 141 |
goto again1; |
| 142 |
} |
| 143 |
|
| 144 |
else{ |
| 145 |
PERROR(("XPAProxyConnect: connect()")); |
| 146 |
goto error; |
| 147 |
} |
| 148 |
} |
| 149 |
break; |
| 150 |
#if HAVE_SYS_UN_H |
| 151 |
case XPA_UNIX: |
| 152 |
again2: |
| 153 |
ip = 0; |
| 154 |
port = 0; |
| 155 |
|
| 156 |
if( (ofd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){ |
| 157 |
goto error; |
| 158 |
} |
| 159 |
setsockopt(ofd, SOL_SOCKET, SO_KEEPALIVE, |
| 160 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 161 |
memset((char *)&sock_un, 0, sizeof(sock_un)); |
| 162 |
sock_un.sun_family = AF_UNIX; |
| 163 |
strcpy(sock_un.sun_path, method); |
| 164 |
|
| 165 |
if(connect(ofd, (struct sockaddr *)&sock_un, sizeof(sock_un))<0){ |
| 166 |
xclose(ofd); |
| 167 |
|
| 168 |
|
| 169 |
if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){ |
| 170 |
tries++; |
| 171 |
XPASleep(10); |
| 172 |
goto again2; |
| 173 |
} |
| 174 |
|
| 175 |
else{ |
| 176 |
goto error; |
| 177 |
} |
| 178 |
} |
| 179 |
break; |
| 180 |
#endif |
| 181 |
default: |
| 182 |
goto error; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if( rip ) *rip = ip; |
| 187 |
if( rport ) *rport = port; |
| 188 |
if( rname ){ |
| 189 |
strncpy(rname, method, SZ_LINE-1); |
| 190 |
rname[SZ_LINE-1] = '\0'; |
| 191 |
} |
| 192 |
return(ofd); |
| 193 |
|
| 194 |
error: |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#ifdef ANSI_FUNC |
| 211 |
static XPAComm |
| 212 |
CommNew (XPA xpa, int fd, unsigned int ip, int port, char *name, NS ns) |
| 213 |
#else |
| 214 |
static XPAComm CommNew(xpa, fd, ip, port, name, ns) |
| 215 |
XPA xpa; |
| 216 |
int fd; |
| 217 |
unsigned int ip; |
| 218 |
int port; |
| 219 |
char *name; |
| 220 |
NS ns; |
| 221 |
#endif |
| 222 |
{ |
| 223 |
XPAComm comm, cur; |
| 224 |
int i; |
| 225 |
socklen_t slen; |
| 226 |
struct sockaddr_in sock_in; |
| 227 |
#if HAVE_SYS_UN_H |
| 228 |
struct sockaddr_un sock_un; |
| 229 |
#endif |
| 230 |
|
| 231 |
|
| 232 |
if( (comm = (XPAComm)xcalloc(1, sizeof(XPACommRec))) == NULL ) |
| 233 |
return(NULL); |
| 234 |
|
| 235 |
|
| 236 |
if( fd < 0 ){ |
| 237 |
|
| 238 |
switch(mtype){ |
| 239 |
case XPA_INET: |
| 240 |
while( 1 ){ |
| 241 |
slen = sizeof(struct sockaddr_in); |
| 242 |
if((comm->cmdfd=xaccept(xpa->fd,(struct sockaddr *)&sock_in,&slen))>=0){ |
| 243 |
comm->cmdip = ntohl(sock_in.sin_addr.s_addr); |
| 244 |
comm->cmdport = ntohs(sock_in.sin_port); |
| 245 |
break; |
| 246 |
} |
| 247 |
else{ |
| 248 |
if( xerrno == EINTR ) |
| 249 |
continue; |
| 250 |
else{ |
| 251 |
xfree(comm); |
| 252 |
return(NULL); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
break; |
| 257 |
#if HAVE_SYS_UN_H |
| 258 |
case XPA_UNIX: |
| 259 |
while( 1 ){ |
| 260 |
slen = sizeof(struct sockaddr_un); |
| 261 |
if((comm->cmdfd=xaccept(xpa->fd,(struct sockaddr *)&sock_un,&slen))>=0){ |
| 262 |
comm->cmdname = xstrdup(sock_un.sun_path); |
| 263 |
break; |
| 264 |
} |
| 265 |
else{ |
| 266 |
if( xerrno == EINTR ) |
| 267 |
continue; |
| 268 |
else{ |
| 269 |
xfree(comm); |
| 270 |
return(NULL); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
break; |
| 275 |
#endif |
| 276 |
default: |
| 277 |
xfree(comm); |
| 278 |
return(NULL); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
else{ |
| 283 |
switch(mtype){ |
| 284 |
case XPA_INET: |
| 285 |
comm->cmdip = ip; |
| 286 |
comm->cmdport = port; |
| 287 |
break; |
| 288 |
case XPA_UNIX: |
| 289 |
comm->cmdname = xstrdup(name); |
| 290 |
break; |
| 291 |
} |
| 292 |
comm->cmdfd = fd; |
| 293 |
|
| 294 |
comm->ns = ns; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
xfcntl(comm->cmdfd, F_SETFD, FD_CLOEXEC); |
| 300 |
|
| 301 |
comm->datafd = -1; |
| 302 |
|
| 303 |
comm->ack = 1; |
| 304 |
|
| 305 |
comm->cendian = "?"; |
| 306 |
|
| 307 |
for(i=0; i<XPA_CMDS+1; i++){ |
| 308 |
comm->acl[i] = -1; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
if( xpa->commhead == NULL ){ |
| 313 |
xpa->commhead = comm; |
| 314 |
} |
| 315 |
else{ |
| 316 |
for(cur=xpa->commhead; cur->next!=NULL; cur=cur->next){ |
| 317 |
; |
| 318 |
} |
| 319 |
cur->next = comm; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
if( xpa->seladd ) |
| 324 |
comm->selcptr = (xpa->seladd)(xpa, comm->cmdfd); |
| 325 |
|
| 326 |
|
| 327 |
XPAActive(xpa, comm, 1); |
| 328 |
|
| 329 |
FPRINTF((stderr, "%sCommNew: ip=%x port=%d fd=%d\n", _sp, |
| 330 |
comm->cmdip, comm->cmdport, comm->cmdfd)); |
| 331 |
|
| 332 |
|
| 333 |
return(comm); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
#ifdef ANSI_FUNC |
| 348 |
static void |
| 349 |
CommFree (XPA xpa, XPAComm comm, int flag) |
| 350 |
#else |
| 351 |
static void CommFree(xpa, comm, flag) |
| 352 |
XPA xpa; |
| 353 |
XPAComm comm; |
| 354 |
int flag; |
| 355 |
#endif |
| 356 |
{ |
| 357 |
XPAComm cur; |
| 358 |
|
| 359 |
if( !comm ) |
| 360 |
return; |
| 361 |
|
| 362 |
FPRINTF((stderr, "%sCommFree: ip=%x port=%d fd=%d dfd=%d\n", _sp, |
| 363 |
comm->cmdip, comm->cmdport, comm->cmdfd, comm->datafd)); |
| 364 |
|
| 365 |
if( xpa ){ |
| 366 |
if( xpa->commhead ){ |
| 367 |
if( xpa->commhead == comm ){ |
| 368 |
xpa->commhead = comm->next; |
| 369 |
} |
| 370 |
else{ |
| 371 |
for(cur=xpa->commhead; cur!=NULL; cur=cur->next){ |
| 372 |
if( cur->next == comm ){ |
| 373 |
cur->next = comm->next; |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
else{ |
| 382 |
for(xpa=xpahead; xpa!=NULL; xpa=xpa->next){ |
| 383 |
if( xpa->commhead ){ |
| 384 |
if( xpa->commhead == comm ){ |
| 385 |
xpa->commhead = comm->next; |
| 386 |
} |
| 387 |
else{ |
| 388 |
for(cur=xpa->commhead; cur!=NULL; cur=cur->next){ |
| 389 |
if( cur->next == comm ){ |
| 390 |
cur->next = comm->next; |
| 391 |
break; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
if( flag && (comm->cmdfd >= 0) ){ |
| 401 |
FPRINTF((stderr, "%sCommFree closing cmd fd: %d\n", _sp, comm->cmdfd)); |
| 402 |
|
| 403 |
if( comm->cmdfd < FD_SETSIZE ) |
| 404 |
activefds[comm->cmdfd] = 0; |
| 405 |
|
| 406 |
if( xpa && xpa->seldel && comm->selcptr ){ |
| 407 |
(xpa->seldel)(comm->selcptr); |
| 408 |
comm->selcptr = NULL; |
| 409 |
} |
| 410 |
|
| 411 |
xclose(comm->cmdfd); |
| 412 |
} |
| 413 |
|
| 414 |
XPACloseData(xpa, comm); |
| 415 |
|
| 416 |
if( comm->cmdname != NULL ){ |
| 417 |
unlink(comm->cmdname); |
| 418 |
xfree(comm->cmdname); |
| 419 |
} |
| 420 |
if( comm->dataname != NULL ){ |
| 421 |
unlink(comm->dataname); |
| 422 |
xfree(comm->dataname); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
if( comm->id != NULL ) xfree(comm->id); |
| 427 |
if( comm->info != NULL ) xfree(comm->info); |
| 428 |
if( comm->target != NULL ) xfree(comm->target); |
| 429 |
if( comm->paramlist != NULL ) xfree(comm->paramlist); |
| 430 |
|
| 431 |
|
| 432 |
if( comm->ns ){ |
| 433 |
comm->ns->nproxy -= 1; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
if( xpa ){ |
| 438 |
xpa->comm = NULL; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
xfree(comm); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
#ifdef ANSI_FUNC |
| 457 |
static NS |
| 458 |
XPANSOpen (XPA xpa, char *host, int force) |
| 459 |
#else |
| 460 |
static NS XPANSOpen(xpa, host, force) |
| 461 |
XPA xpa; |
| 462 |
char *host; |
| 463 |
int force; |
| 464 |
#endif |
| 465 |
{ |
| 466 |
int i; |
| 467 |
int status; |
| 468 |
int xnsfd=0; |
| 469 |
int keep_alive=1; |
| 470 |
int tries=0; |
| 471 |
int ip=0; |
| 472 |
int dowarn=0; |
| 473 |
int contmode=XPA_CONNECT_TIMEOUT_MODE; |
| 474 |
unsigned short xnsport=0; |
| 475 |
unsigned int xnsip=0; |
| 476 |
static int errinit=0; |
| 477 |
char *s; |
| 478 |
char *path; |
| 479 |
char *method; |
| 480 |
char nscmd[SZ_LINE]; |
| 481 |
char tbuf[SZ_LINE]; |
| 482 |
char tbuf2[SZ_LINE]; |
| 483 |
struct sockaddr_in sock_in; |
| 484 |
struct passwd *pw; |
| 485 |
#if HAVE_SYS_UN_H |
| 486 |
struct sockaddr_un sock_un; |
| 487 |
#endif |
| 488 |
char *findname=NULL; |
| 489 |
NS ns, cur; |
| 490 |
|
| 491 |
|
| 492 |
method = XPANSMethod(host, 0); |
| 493 |
|
| 494 |
|
| 495 |
if( xpa ){ |
| 496 |
for(ns=xpa->nshead; ns!=NULL; ns=ns->next){ |
| 497 |
if( !strcmp(ns->method, method) ){ |
| 498 |
|
| 499 |
if( force >= 0 ){ |
| 500 |
if( (XPAPuts(xpa, ns->fd, "status\n", stimeout) >0) && |
| 501 |
(XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0) && |
| 502 |
!strncmp(tbuf, "XPA$OK", 6) ){ |
| 503 |
FPRINTF((stderr, "%sXPANSOpen: found existing ns: %s\n", |
| 504 |
_sp, ns->method)); |
| 505 |
return ns; |
| 506 |
} |
| 507 |
|
| 508 |
else{ |
| 509 |
FPRINTF((stderr, "%sXPANSOpen: existing ns is dead: %s\n", |
| 510 |
_sp, ns->method)); |
| 511 |
XPANSClose(xpa, ns); |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
else{ |
| 517 |
return ns; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if( force == -1 ) |
| 525 |
return(NULL); |
| 526 |
|
| 527 |
|
| 528 |
*nscmd = '\0'; |
| 529 |
|
| 530 |
|
| 531 |
if( (s=(char *)getenv("XPA_NSUSERS")) != NULL ) |
| 532 |
strncpy(nsusers, s, SZ_LINE-1); |
| 533 |
|
| 534 |
else if( (s=(char *)getenv("LOGNAME")) != NULL ) |
| 535 |
strncpy(nsusers, s, SZ_LINE-1); |
| 536 |
#if HAVE_MINGW32==0 |
| 537 |
|
| 538 |
else if( (pw=getpwuid(geteuid())) ) |
| 539 |
strncpy(nsusers, pw->pw_name, SZ_LINE-1); |
| 540 |
#endif |
| 541 |
|
| 542 |
if( *nsusers == '\0' ) |
| 543 |
strcpy(nsusers, "*"); |
| 544 |
|
| 545 |
nsusers[SZ_LINE-1] = '\0'; |
| 546 |
|
| 547 |
|
| 548 |
switch(mtype){ |
| 549 |
case XPA_INET: |
| 550 |
again1: |
| 551 |
XPAParseIpPort(method, &xnsip, &xnsport); |
| 552 |
|
| 553 |
if( (xnsip == gethostip("$host")) && (tries == 0) ){ |
| 554 |
xnsip = gethostip("$localhost"); |
| 555 |
} |
| 556 |
if( xnsip == 0 ){ |
| 557 |
fprintf(stderr, |
| 558 |
"XPA$ERROR: invalid host name specified: %s.\n", method); |
| 559 |
return(NULL); |
| 560 |
} |
| 561 |
if( (xnsfd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ) |
| 562 |
goto nons; |
| 563 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 564 |
sock_in.sin_family = AF_INET; |
| 565 |
sock_in.sin_addr.s_addr = htonl(xnsip); |
| 566 |
sock_in.sin_port = htons(xnsport); |
| 567 |
|
| 568 |
if( ctimeout <= 0 ) contmode = 0; |
| 569 |
switch(contmode){ |
| 570 |
case 1: |
| 571 |
#if HAVE_MINGW32==0 |
| 572 |
status=alrmconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout); |
| 573 |
#else |
| 574 |
status=noblkconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout); |
| 575 |
#endif |
| 576 |
break; |
| 577 |
case 2: |
| 578 |
status=noblkconnect(xnsfd, (void *)&sock_in, sizeof(sock_in), ctimeout); |
| 579 |
break; |
| 580 |
default: |
| 581 |
status=connect(xnsfd, (struct sockaddr *)&sock_in, sizeof(sock_in)); |
| 582 |
break; |
| 583 |
} |
| 584 |
if( status == 0 ){ |
| 585 |
FPRINTF((stderr, "%sXPANSOpen: connect to xpans\n", _sp)); |
| 586 |
goto okns; |
| 587 |
} |
| 588 |
else{ |
| 589 |
xclose(xnsfd); |
| 590 |
|
| 591 |
if( (xerrno == ECONNREFUSED) && (tries < 1) ){ |
| 592 |
tries++; |
| 593 |
goto again1; |
| 594 |
} |
| 595 |
} |
| 596 |
break; |
| 597 |
#if HAVE_SYS_UN_H |
| 598 |
case XPA_UNIX: |
| 599 |
again2: |
| 600 |
|
| 601 |
if( (xnsfd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) |
| 602 |
goto nons; |
| 603 |
memset((char *)&sock_un, 0, sizeof(sock_un)); |
| 604 |
sock_un.sun_family = AF_UNIX; |
| 605 |
strcpy(sock_un.sun_path, method); |
| 606 |
xnsip = 0; |
| 607 |
|
| 608 |
status=connect(xnsfd, (struct sockaddr *)&sock_un, sizeof(sock_un)); |
| 609 |
if( status == 0 ){ |
| 610 |
FPRINTF((stderr, "%sXPANSOpen: connect to xpans\n", _sp)); |
| 611 |
goto okns; |
| 612 |
} |
| 613 |
else{ |
| 614 |
xclose(xnsfd); |
| 615 |
|
| 616 |
|
| 617 |
if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){ |
| 618 |
tries++; |
| 619 |
XPASleep(10); |
| 620 |
goto again2; |
| 621 |
} |
| 622 |
} |
| 623 |
break; |
| 624 |
#endif |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
if( force == 0 ) |
| 629 |
goto noforce; |
| 630 |
|
| 631 |
|
| 632 |
if( *nscmd == '\0' ){ |
| 633 |
if( (mtype == XPA_UNIX) || LOCALIP(xnsip) ){ |
| 634 |
path = (char *)getenv("PATH"); |
| 635 |
findname = (char *)Find(XPANSNAME, "x", NULL, path); |
| 636 |
#if HAVE_CYGWIN |
| 637 |
|
| 638 |
if( !findname ) findname = (char *)Find(XPANSNAME, "x", NULL, "."); |
| 639 |
#endif |
| 640 |
} |
| 641 |
if( findname != NULL ){ |
| 642 |
switch(mtype){ |
| 643 |
case XPA_INET: |
| 644 |
#if USE_LAUNCH |
| 645 |
|
| 646 |
for(i=0; i<strlen(findname); i++){ |
| 647 |
if( findname[i] == ' '){ |
| 648 |
findname[i] = LAUNCH_SPACE; |
| 649 |
} |
| 650 |
} |
| 651 |
snprintf(nscmd, SZ_LINE, "%s -e -p %d -l %s/xpans_%d.log", |
| 652 |
findname, xnsport, tmpdir, xnsport); |
| 653 |
#else |
| 654 |
snprintf(nscmd, SZ_LINE, "\"%s\" -e -p %d -l %s/xpans_%d.log &\n", |
| 655 |
findname, xnsport, tmpdir, xnsport); |
| 656 |
#endif |
| 657 |
break; |
| 658 |
#if HAVE_SYS_UN_H |
| 659 |
case XPA_UNIX: |
| 660 |
#if USE_LAUNCH |
| 661 |
|
| 662 |
for(i=0; i<strlen(findname); i++){ |
| 663 |
if( findname[i] == ' '){ |
| 664 |
findname[i] = LAUNCH_SPACE; |
| 665 |
} |
| 666 |
} |
| 667 |
snprintf(nscmd, SZ_LINE, "%s -e -f %s -l %s.log", |
| 668 |
findname, method, method); |
| 669 |
#else |
| 670 |
snprintf(nscmd, SZ_LINE, "\"%s\" -e -f %s -l %s.log &\n", |
| 671 |
findname, method, method); |
| 672 |
#endif |
| 673 |
break; |
| 674 |
#endif |
| 675 |
} |
| 676 |
} |
| 677 |
else{ |
| 678 |
*nscmd = '\0'; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
if((*nscmd != '\0') && ((mtype == XPA_UNIX) || LOCALIP(xnsip)) ){ |
| 685 |
FPRINTF((stderr, "%sLaunching: %s\n", _sp, nscmd)); |
| 686 |
#if USE_LAUNCH |
| 687 |
if( Launch(nscmd, 0, NULL, NULL) != 0 ) |
| 688 |
goto nons; |
| 689 |
#else |
| 690 |
if( system(nscmd) != 0 ) |
| 691 |
goto nons; |
| 692 |
#endif |
| 693 |
|
| 694 |
for(tries=0; tries<XPA_RETRIES; tries++){ |
| 695 |
switch(mtype){ |
| 696 |
case XPA_INET: |
| 697 |
|
| 698 |
if( (xnsip == gethostip("$host")) && (tries % 2 == 0) ){ |
| 699 |
xnsip = gethostip("$localhost"); |
| 700 |
} |
| 701 |
if( xnsip == 0 ){ |
| 702 |
fprintf(stderr, |
| 703 |
"XPA$ERROR: invalid host name specified: %s.\n", method); |
| 704 |
return(NULL); |
| 705 |
} |
| 706 |
if( (xnsfd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ) |
| 707 |
goto nons; |
| 708 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 709 |
sock_in.sin_family = AF_INET; |
| 710 |
sock_in.sin_addr.s_addr = htonl(xnsip); |
| 711 |
sock_in.sin_port = htons(xnsport); |
| 712 |
FPRINTF((stderr, "%sXPANSOPEN: attempting connect to %x\n", |
| 713 |
_sp, xnsip)); |
| 714 |
if(connect(xnsfd, (struct sockaddr *)&sock_in, sizeof(sock_in)) ==0) |
| 715 |
goto okns; |
| 716 |
break; |
| 717 |
#if HAVE_SYS_UN_H |
| 718 |
case XPA_UNIX: |
| 719 |
if( (xnsfd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) |
| 720 |
goto nons; |
| 721 |
if(connect(xnsfd, (struct sockaddr *)&sock_un, sizeof(sock_un)) ==0) |
| 722 |
goto okns; |
| 723 |
break; |
| 724 |
#endif |
| 725 |
} |
| 726 |
xclose(xnsfd); |
| 727 |
XPASleep(XPA_NSDELAY); |
| 728 |
} |
| 729 |
|
| 730 |
goto nons; |
| 731 |
} |
| 732 |
|
| 733 |
else{ |
| 734 |
goto nons; |
| 735 |
} |
| 736 |
|
| 737 |
okns: |
| 738 |
|
| 739 |
xfcntl(xnsfd, F_SETFD, FD_CLOEXEC); |
| 740 |
setsockopt(xnsfd, SOL_SOCKET, SO_KEEPALIVE, |
| 741 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 742 |
|
| 743 |
if( (ns = (NS)xcalloc(1, sizeof(NSRec))) == NULL ){ |
| 744 |
xclose(xnsfd); |
| 745 |
return NULL; |
| 746 |
} |
| 747 |
ns->method = xstrdup(method); |
| 748 |
ns->host = xstrdup(host); |
| 749 |
ns->fd = xnsfd; |
| 750 |
FPRINTF((stderr, "%sXPANSOpen: host %s opened on fd %d\n", _sp, |
| 751 |
host?host:"<unknown>", xnsfd)); |
| 752 |
switch(mtype){ |
| 753 |
case XPA_INET: |
| 754 |
ns->ip = xnsip; |
| 755 |
ns->port = xnsport; |
| 756 |
break; |
| 757 |
#if HAVE_SYS_UN_H |
| 758 |
case XPA_UNIX: |
| 759 |
ns->name = xstrdup(method); |
| 760 |
break; |
| 761 |
#endif |
| 762 |
} |
| 763 |
if( xpa ){ |
| 764 |
|
| 765 |
if( xpa->nshead == NULL ){ |
| 766 |
xpa->nshead = ns; |
| 767 |
} |
| 768 |
else{ |
| 769 |
for(cur=xpa->nshead; cur->next!=NULL; cur=cur->next) |
| 770 |
; |
| 771 |
cur->next = ns; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
snprintf(tbuf, SZ_LINE, "version %s\n", XPA_VERSION); |
| 776 |
if( (XPAPuts(xpa, ns->fd, tbuf, stimeout) >0) && |
| 777 |
(XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0) ){ |
| 778 |
if( word(tbuf, tbuf2, &ip) ){ |
| 779 |
if( !strcmp(tbuf2, "XPA$VERSION") ){ |
| 780 |
if( word(tbuf, tbuf2, &ip) ){ |
| 781 |
|
| 782 |
dowarn = (XPAVersionCheck(XPA_VERSION, tbuf2)>0); |
| 783 |
} |
| 784 |
else{ |
| 785 |
strcpy(tbuf2, "unknown/pre-2.1 (noversion)"); |
| 786 |
dowarn = 1; |
| 787 |
} |
| 788 |
} |
| 789 |
else{ |
| 790 |
strcpy(tbuf2, "unknown/pre-2.1 (badformat)"); |
| 791 |
dowarn = 1; |
| 792 |
} |
| 793 |
} |
| 794 |
FPRINTF((stderr, "%sXPANSOpen: version info: %s\n", _sp, tbuf)); |
| 795 |
if( dowarn ){ |
| 796 |
XPAVersionWarn(XPA_VERSION, tbuf2); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
if( findname != NULL ) xfree(findname); |
| 802 |
return(ns); |
| 803 |
|
| 804 |
nons: |
| 805 |
|
| 806 |
|
| 807 |
if( XPAPort(xpa) >0 ) |
| 808 |
return NULL; |
| 809 |
switch(mtype){ |
| 810 |
case XPA_INET: |
| 811 |
if( !errinit && verbosity ){ |
| 812 |
if( LOCALIP(xnsip) ){ |
| 813 |
fprintf(stderr, |
| 814 |
"XPA$WARNING: xpans needs to be running on this machine.\n"); |
| 815 |
} |
| 816 |
else{ |
| 817 |
fprintf(stderr, |
| 818 |
"XPA$WARNING: xpans needs to be running on machine: "); |
| 819 |
fprintf(stderr, "%s\n", getiphost(xnsip)); |
| 820 |
} |
| 821 |
} |
| 822 |
break; |
| 823 |
#if HAVE_SYS_UN_H |
| 824 |
case XPA_UNIX: |
| 825 |
if( !errinit && verbosity ){ |
| 826 |
fprintf(stderr, |
| 827 |
"XPA$WARNING: xpans needs to be running on this machine.\n"); |
| 828 |
} |
| 829 |
break; |
| 830 |
#endif |
| 831 |
default: |
| 832 |
break; |
| 833 |
} |
| 834 |
if( xpa && verbosity ){ |
| 835 |
if( !errinit ){ |
| 836 |
|
| 837 |
if( *nscmd == '\0' ){ |
| 838 |
switch(mtype){ |
| 839 |
case XPA_INET: |
| 840 |
snprintf(nscmd, SZ_LINE, "xpans -e -p %d -l %s/xpans_%d.log", |
| 841 |
xnsport, tmpdir, xnsport); |
| 842 |
break; |
| 843 |
#if HAVE_SYS_UN_H |
| 844 |
case XPA_UNIX: |
| 845 |
snprintf(nscmd, SZ_LINE, "xpans -e -f %s -l %s.log", method, method); |
| 846 |
break; |
| 847 |
#endif |
| 848 |
} |
| 849 |
} |
| 850 |
fprintf(stderr, "Please start xpans using the command:\n\t%s\n", nscmd); |
| 851 |
fprintf(stderr, "Once xpans is running, register all xpas in this process using:\n"); |
| 852 |
fprintf(stderr, "\txpaset -p %s -nsconnect\n", xpa->method); |
| 853 |
} |
| 854 |
fprintf(stderr, "For now, contact %s using:", xpa->name); |
| 855 |
fprintf(stderr, " xpaset %s .. or xpaget %s ..\n", |
| 856 |
xpa->method, xpa->method); |
| 857 |
} |
| 858 |
errinit++; |
| 859 |
|
| 860 |
noforce: |
| 861 |
if( findname != NULL ) xfree(findname); |
| 862 |
return(NULL); |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
#ifdef ANSI_FUNC |
| 878 |
static int |
| 879 |
_XPAFree (XPA xpa) |
| 880 |
#else |
| 881 |
static int _XPAFree(xpa) |
| 882 |
XPA xpa; |
| 883 |
#endif |
| 884 |
{ |
| 885 |
char tbuf[SZ_LINE]; |
| 886 |
XPACmd cmd, tcmd; |
| 887 |
XPAComm comm, tcomm; |
| 888 |
XPAClip clip, tclip; |
| 889 |
NS ns, tns; |
| 890 |
|
| 891 |
|
| 892 |
if( xpa == NULL ) |
| 893 |
return(-1); |
| 894 |
|
| 895 |
FPRINTF((stderr, "%s_XPAFree: freeing xpa struct\n", _sp)); |
| 896 |
|
| 897 |
if( xpa->type != NULL ) |
| 898 |
XPANSDel(xpa, NULL, NULL); |
| 899 |
|
| 900 |
|
| 901 |
for(cmd=xpa->commands; cmd!=NULL; ){ |
| 902 |
tcmd = cmd->next; |
| 903 |
XPACmdDel(xpa, cmd); |
| 904 |
cmd = tcmd; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
XPAListDel(&xpahead, xpa); |
| 909 |
|
| 910 |
|
| 911 |
if( xpa->fd >= 0 ) |
| 912 |
xclose(xpa->fd); |
| 913 |
|
| 914 |
|
| 915 |
switch(mtype){ |
| 916 |
#if HAVE_SYS_UN_H |
| 917 |
case XPA_UNIX: |
| 918 |
|
| 919 |
unlink(xpa->method); |
| 920 |
snprintf(tbuf, SZ_LINE, "%s_data", xpa->method); |
| 921 |
unlink(tbuf); |
| 922 |
break; |
| 923 |
#endif |
| 924 |
default: |
| 925 |
break; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
if( xpa->version ) xfree(xpa->version); |
| 930 |
if( xpa->type ) xfree(xpa->type); |
| 931 |
if( xpa->method ) xfree(xpa->method); |
| 932 |
if( xpa->xclass ) xfree(xpa->xclass); |
| 933 |
if( xpa->name ) xfree(xpa->name); |
| 934 |
if( xpa->help ) xfree(xpa->help); |
| 935 |
if( xpa->sendian ) xfree(xpa->sendian); |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
if( xpa->seldel && xpa->selptr ){ |
| 940 |
(xpa->seldel)(xpa->selptr); |
| 941 |
xpa->selptr = NULL; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
for(comm=xpa->commhead; comm!=NULL; ){ |
| 946 |
tcomm = comm->next; |
| 947 |
CommFree(xpa, comm, 1); |
| 948 |
comm = tcomm; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
for(clip=xpa->cliphead; clip!=NULL; ){ |
| 953 |
tclip = clip->next; |
| 954 |
ClipBoardFree(xpa, clip); |
| 955 |
clip = tclip; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
for(ns=xpa->nshead; ns!=NULL; ){ |
| 960 |
tns = ns->next; |
| 961 |
XPANSClose(xpa, ns); |
| 962 |
ns = tns; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
xfree((char *)xpa); |
| 967 |
|
| 968 |
return(0); |
| 969 |
} |
| 970 |
|
| 971 |
#if HAVE_ATEXIT |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
#ifdef ANSI_FUNC |
| 986 |
static void |
| 987 |
_XPAAtExit (void) |
| 988 |
#else |
| 989 |
static void _XPAAtExit() |
| 990 |
#endif |
| 991 |
{ |
| 992 |
XPA xpa, txpa; |
| 993 |
static int done=0; |
| 994 |
|
| 995 |
if( !done ){ |
| 996 |
|
| 997 |
if( !atexitinit ) return; |
| 998 |
|
| 999 |
if( atexitinit != getpid() ) return; |
| 1000 |
FPRINTF((stderr, "calling XPAAtExit\n")); |
| 1001 |
for(xpa=xpahead; xpa!=NULL; ){ |
| 1002 |
|
| 1003 |
txpa = xpa->next; |
| 1004 |
_XPAFree(xpa); |
| 1005 |
xpa = txpa; |
| 1006 |
} |
| 1007 |
|
| 1008 |
xsocketcleanup(); |
| 1009 |
|
| 1010 |
done++; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
#ifdef ANSI_FUNC |
| 1026 |
void |
| 1027 |
XPAAtExit (void) |
| 1028 |
#else |
| 1029 |
void XPAAtExit() |
| 1030 |
#endif |
| 1031 |
{ |
| 1032 |
if( !atexitinit ){ |
| 1033 |
atexit(_XPAAtExit); |
| 1034 |
atexitinit = getpid(); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
#endif |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
#ifdef ANSI_FUNC |
| 1063 |
int |
| 1064 |
_XPAValid (XPA head, XPA xpa, char *type) |
| 1065 |
#else |
| 1066 |
int _XPAValid(head, xpa, type) |
| 1067 |
XPA head; |
| 1068 |
XPA xpa; |
| 1069 |
char *type; |
| 1070 |
#endif |
| 1071 |
{ |
| 1072 |
XPA cur; |
| 1073 |
|
| 1074 |
if( xpa == NULL ) |
| 1075 |
return(0); |
| 1076 |
for(cur=head; cur!=NULL; cur=cur->next){ |
| 1077 |
if( (cur == xpa) && !strcspn(cur->type, type) ){ |
| 1078 |
return(1); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
return(0); |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
#ifdef ANSI_FUNC |
| 1096 |
int |
| 1097 |
XPAValid (XPA xpa) |
| 1098 |
#else |
| 1099 |
int XPAValid(xpa) |
| 1100 |
XPA xpa; |
| 1101 |
#endif |
| 1102 |
{ |
| 1103 |
if( _XPAValid(xpahead, xpa, XPA_ACLS) ) |
| 1104 |
return(1); |
| 1105 |
else |
| 1106 |
return(0); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
#ifdef ANSI_FUNC |
| 1122 |
int |
| 1123 |
XPAEndian(void) |
| 1124 |
#else |
| 1125 |
int XPAEndian() |
| 1126 |
#endif |
| 1127 |
{ |
| 1128 |
union |
| 1129 |
{ |
| 1130 |
long l; |
| 1131 |
char c[sizeof (long)]; |
| 1132 |
} u; |
| 1133 |
u.l = 1; |
| 1134 |
return(u.c[sizeof (long) - 1] == 1); |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
#ifdef ANSI_FUNC |
| 1149 |
XPA |
| 1150 |
XPAListHead (void) |
| 1151 |
#else |
| 1152 |
XPA XPAListHead() |
| 1153 |
#endif |
| 1154 |
{ |
| 1155 |
return(xpahead); |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
#ifdef ANSI_FUNC |
| 1170 |
void |
| 1171 |
XPAListAdd (XPA *head, XPA xpa) |
| 1172 |
#else |
| 1173 |
void XPAListAdd(head, xpa) |
| 1174 |
XPA *head; |
| 1175 |
XPA xpa; |
| 1176 |
#endif |
| 1177 |
{ |
| 1178 |
XPA cur; |
| 1179 |
|
| 1180 |
if( *head == NULL ){ |
| 1181 |
*head = xpa; |
| 1182 |
} |
| 1183 |
else{ |
| 1184 |
for(cur=*head; cur->next!=NULL; cur=cur->next) |
| 1185 |
; |
| 1186 |
cur->next = xpa; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
#ifdef ANSI_FUNC |
| 1202 |
void |
| 1203 |
XPAListDel (XPA *head, XPA xpa) |
| 1204 |
#else |
| 1205 |
void XPAListDel(head, xpa) |
| 1206 |
XPA *head; |
| 1207 |
XPA xpa; |
| 1208 |
#endif |
| 1209 |
{ |
| 1210 |
XPA cur; |
| 1211 |
|
| 1212 |
|
| 1213 |
if( *head ){ |
| 1214 |
if( *head == xpa ){ |
| 1215 |
*head = xpa->next; |
| 1216 |
} |
| 1217 |
else{ |
| 1218 |
for(cur=*head; cur!=NULL; cur=cur->next){ |
| 1219 |
if( cur->next == xpa ){ |
| 1220 |
cur->next = xpa->next; |
| 1221 |
break; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
} |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
#ifdef ANSI_FUNC |
| 1240 |
int |
| 1241 |
XPAActive (XPA xpa, XPAComm comm, int flag) |
| 1242 |
#else |
| 1243 |
int XPAActive(xpa, comm, flag) |
| 1244 |
XPA xpa; |
| 1245 |
XPAComm comm; |
| 1246 |
int flag; |
| 1247 |
#endif |
| 1248 |
{ |
| 1249 |
int prev=0; |
| 1250 |
|
| 1251 |
|
| 1252 |
if( !xpa ) return(0); |
| 1253 |
|
| 1254 |
switch(flag){ |
| 1255 |
|
| 1256 |
case 0: |
| 1257 |
if( (xpa->fd >= 0) && (xpa->fd < FD_SETSIZE) ){ |
| 1258 |
FPRINTF((stderr, "%sXPAActive: clearing fd %d\n", _sp, xpa->fd)); |
| 1259 |
prev = activefds[xpa->fd]; |
| 1260 |
activefds[xpa->fd] = 0; |
| 1261 |
if( xpa->seloff && xpa->selptr ) |
| 1262 |
(xpa->seloff)(xpa->selptr); |
| 1263 |
} |
| 1264 |
if( comm ){ |
| 1265 |
if( (comm->cmdfd >= 0) && (comm->cmdfd < FD_SETSIZE) ){ |
| 1266 |
activefds[comm->cmdfd] = 0; |
| 1267 |
if( xpa->seloff && comm->selcptr ) |
| 1268 |
(xpa->seloff)(comm->selcptr); |
| 1269 |
} |
| 1270 |
if( (comm->datafd >= 0) && (comm->datafd < FD_SETSIZE) ){ |
| 1271 |
activefds[comm->datafd] = 0; |
| 1272 |
if( xpa->seloff && comm->seldptr ) |
| 1273 |
(xpa->seloff)(comm->seldptr); |
| 1274 |
} |
| 1275 |
} |
| 1276 |
break; |
| 1277 |
|
| 1278 |
case 1: |
| 1279 |
if( (xpa->fd >= 0) && (xpa->fd < FD_SETSIZE) ){ |
| 1280 |
FPRINTF((stderr, "%sXPAActive: activating fd %d\n", _sp, xpa->fd)); |
| 1281 |
prev = activefds[xpa->fd]; |
| 1282 |
activefds[xpa->fd] = 1; |
| 1283 |
if( xpa->selon && xpa->selptr ) |
| 1284 |
(xpa->selon)(xpa->selptr); |
| 1285 |
} |
| 1286 |
if( comm ){ |
| 1287 |
if( (comm->cmdfd >= 0) && (comm->cmdfd < FD_SETSIZE) ){ |
| 1288 |
activefds[comm->cmdfd] = 1; |
| 1289 |
if( xpa->selon && comm->selcptr ) |
| 1290 |
(xpa->selon)(comm->selcptr); |
| 1291 |
} |
| 1292 |
if( (comm->datafd >= 0) && (comm->datafd < FD_SETSIZE) ){ |
| 1293 |
activefds[comm->datafd] = 1; |
| 1294 |
if( xpa->selon && comm->seldptr ) |
| 1295 |
(xpa->selon)(comm->seldptr); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
break; |
| 1299 |
default: |
| 1300 |
break; |
| 1301 |
} |
| 1302 |
return(prev); |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
#ifdef ANSI_FUNC |
| 1317 |
int |
| 1318 |
XPAActiveFd (int fd) |
| 1319 |
#else |
| 1320 |
int XPAActiveFd(fd) |
| 1321 |
int fd; |
| 1322 |
#endif |
| 1323 |
{ |
| 1324 |
if( (fd >= 0) && (fd < FD_SETSIZE) && (activefds[fd] > 0) ) |
| 1325 |
return(1); |
| 1326 |
else |
| 1327 |
return(0); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
#ifdef ANSI_FUNC |
| 1342 |
int |
| 1343 |
XPAAddSelect (XPA xpa, fd_set *readfdsptr) |
| 1344 |
#else |
| 1345 |
int XPAAddSelect(xpa, readfdsptr) |
| 1346 |
XPA xpa; |
| 1347 |
fd_set *readfdsptr; |
| 1348 |
#endif |
| 1349 |
{ |
| 1350 |
XPA cur; |
| 1351 |
XPAComm comm; |
| 1352 |
int got=0; |
| 1353 |
|
| 1354 |
|
| 1355 |
if( readfdsptr == NULL ) |
| 1356 |
return(0); |
| 1357 |
|
| 1358 |
|
| 1359 |
if( xpa != NULL ){ |
| 1360 |
if( XPAActiveFd(xpa->fd) ){ |
| 1361 |
FD_SET(xpa->fd, readfdsptr); |
| 1362 |
got++; |
| 1363 |
|
| 1364 |
for(comm=xpa->commhead; comm!=NULL; comm=comm->next){ |
| 1365 |
if( XPAActiveFd(comm->cmdfd) ){ |
| 1366 |
FD_SET(comm->cmdfd, readfdsptr); |
| 1367 |
got++; |
| 1368 |
} |
| 1369 |
if( XPAActiveFd(comm->datafd) && (comm->datafd != comm->cmdfd) ){ |
| 1370 |
FD_SET(comm->datafd, readfdsptr); |
| 1371 |
got++; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
else{ |
| 1378 |
for(cur=xpahead; cur!=NULL; cur=cur->next){ |
| 1379 |
if( XPAActiveFd(cur->fd) ){ |
| 1380 |
FPRINTF((stderr, "%sXPAAddSelect: adding fd %d\n", _sp, cur->fd)); |
| 1381 |
FD_SET(cur->fd, readfdsptr); |
| 1382 |
got++; |
| 1383 |
|
| 1384 |
for(comm=cur->commhead; comm!=NULL; comm=comm->next){ |
| 1385 |
if( XPAActiveFd(comm->cmdfd) ){ |
| 1386 |
FPRINTF((stderr, "%sXPAAddSelect: adding cmdfd %d\n", |
| 1387 |
_sp, comm->cmdfd)); |
| 1388 |
FD_SET(comm->cmdfd, readfdsptr); |
| 1389 |
got++; |
| 1390 |
} |
| 1391 |
if( XPAActiveFd(comm->datafd) && (comm->datafd != comm->cmdfd) ){ |
| 1392 |
FPRINTF((stderr, "%sXPAAddSelect: adding datafd %d\n", |
| 1393 |
_sp, comm->datafd)); |
| 1394 |
FD_SET(comm->datafd, readfdsptr); |
| 1395 |
got++; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
return(got); |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
#ifdef ANSI_FUNC |
| 1416 |
int |
| 1417 |
XPAProcessSelect (fd_set *readfdsptr, int maxreq) |
| 1418 |
#else |
| 1419 |
int XPAProcessSelect(readfdsptr, maxreq) |
| 1420 |
fd_set *readfdsptr; |
| 1421 |
int maxreq; |
| 1422 |
#endif |
| 1423 |
{ |
| 1424 |
int got=0; |
| 1425 |
XPA xpa; |
| 1426 |
XPAComm comm; |
| 1427 |
#ifdef OLD |
| 1428 |
XPA txpa; |
| 1429 |
XPAComm tcomm; |
| 1430 |
#endif |
| 1431 |
|
| 1432 |
|
| 1433 |
if( maxreq < 0 ){ |
| 1434 |
maxreq = 0; |
| 1435 |
} |
| 1436 |
|
| 1437 |
again: |
| 1438 |
for(xpa=xpahead; xpa!=NULL; xpa=xpa->next){ |
| 1439 |
|
| 1440 |
for(comm=xpa->commhead; comm!=NULL; comm=comm->next){ |
| 1441 |
if( (comm->cmdfd >=0) && FD_ISSET(comm->cmdfd, readfdsptr) ){ |
| 1442 |
FD_CLR(comm->cmdfd, readfdsptr); |
| 1443 |
XPAHandler(xpa, comm->cmdfd); |
| 1444 |
got++; |
| 1445 |
if( maxreq && (got >= maxreq) ) return(got); |
| 1446 |
goto again; |
| 1447 |
} |
| 1448 |
} |
| 1449 |
|
| 1450 |
for(comm=xpa->commhead; comm!=NULL; comm=comm->next){ |
| 1451 |
if( (comm->datafd >=0) && FD_ISSET(comm->datafd, readfdsptr) ){ |
| 1452 |
FD_CLR(comm->datafd, readfdsptr); |
| 1453 |
XPAHandler(xpa, comm->datafd); |
| 1454 |
got++; |
| 1455 |
if( maxreq && (got >= maxreq) ) return(got); |
| 1456 |
goto again; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
if( (xpa->fd >= 0) && FD_ISSET(xpa->fd, readfdsptr) ){ |
| 1461 |
FD_CLR(xpa->fd, readfdsptr); |
| 1462 |
XPAHandler(xpa, xpa->fd); |
| 1463 |
got++; |
| 1464 |
if( maxreq && (got >= maxreq) ) return(got); |
| 1465 |
goto again; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
#ifdef OLD |
| 1470 |
for(xpa=xpahead; xpa!=NULL; ){ |
| 1471 |
txpa = xpa->next; |
| 1472 |
|
| 1473 |
for(comm=xpa->commhead; comm!=NULL; ){ |
| 1474 |
tcomm = comm->next; |
| 1475 |
if( (comm->cmdfd >=0) && FD_ISSET(comm->cmdfd, readfdsptr) ){ |
| 1476 |
FD_CLR(comm->cmdfd, readfdsptr); |
| 1477 |
FPRINTF((stderr, "%sXPAProcessSelect: cmd %d\n", _sp, comm->cmdfd)); |
| 1478 |
|
| 1479 |
if( XPAHandler(xpa, comm->cmdfd) != XPA_RTN_OK ){ |
| 1480 |
goto nextxpa; |
| 1481 |
} |
| 1482 |
got++; |
| 1483 |
|
| 1484 |
if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){ |
| 1485 |
goto nextxpa; |
| 1486 |
} |
| 1487 |
|
| 1488 |
if( maxreq && (got >= maxreq) ){ |
| 1489 |
return(got); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
comm = tcomm; |
| 1493 |
} |
| 1494 |
|
| 1495 |
for(comm=xpa->commhead; comm!=NULL; ){ |
| 1496 |
tcomm = comm->next; |
| 1497 |
if( (comm->datafd >=0) && FD_ISSET(comm->datafd, readfdsptr) ){ |
| 1498 |
FD_CLR(comm->datafd, readfdsptr); |
| 1499 |
FPRINTF((stderr, "%sXPAProcessSelect: data %d\n", _sp, comm->datafd)); |
| 1500 |
|
| 1501 |
if( XPAHandler(xpa, comm->datafd) != XPA_RTN_OK ){ |
| 1502 |
goto nextxpa; |
| 1503 |
} |
| 1504 |
got++; |
| 1505 |
|
| 1506 |
if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){ |
| 1507 |
goto nextxpa; |
| 1508 |
} |
| 1509 |
|
| 1510 |
if( maxreq && (got >= maxreq) ){ |
| 1511 |
return(got); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
comm = tcomm; |
| 1515 |
} |
| 1516 |
|
| 1517 |
if( (xpa->fd >= 0) && FD_ISSET(xpa->fd, readfdsptr) ){ |
| 1518 |
FD_CLR(xpa->fd, readfdsptr); |
| 1519 |
FPRINTF((stderr, "%sXPAProcessSelect: xpa %d\n", _sp, xpa->fd)); |
| 1520 |
|
| 1521 |
if( XPAHandler(xpa, xpa->fd) != XPA_RTN_OK ){ |
| 1522 |
goto nextxpa; |
| 1523 |
} |
| 1524 |
got++; |
| 1525 |
|
| 1526 |
if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){ |
| 1527 |
goto nextxpa; |
| 1528 |
} |
| 1529 |
|
| 1530 |
if( maxreq && (got >= maxreq) ){ |
| 1531 |
return(got); |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
nextxpa: |
| 1536 |
|
| 1537 |
if( _XPAValid(xpahead, txpa, XPA_ACLS) ) |
| 1538 |
xpa = txpa; |
| 1539 |
else |
| 1540 |
break; |
| 1541 |
} |
| 1542 |
#endif |
| 1543 |
FPRINTF((stderr, "%sXPAProcessSelect: returns %d\n", _sp, got)); |
| 1544 |
return(got); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
#ifdef ANSI_FUNC |
| 1559 |
void |
| 1560 |
XPACloseData (XPA xpa, XPAComm comm) |
| 1561 |
#else |
| 1562 |
void XPACloseData(xpa, comm) |
| 1563 |
XPA xpa; |
| 1564 |
XPAComm comm; |
| 1565 |
#endif |
| 1566 |
{ |
| 1567 |
|
| 1568 |
if( comm && (comm->datafd >=0) ){ |
| 1569 |
if( comm->cmdfd != comm->datafd ){ |
| 1570 |
FPRINTF((stderr, "%sXPACloseData: close fd %d for cmd fd %d\n", _sp, |
| 1571 |
comm->datafd, comm->cmdfd)); |
| 1572 |
|
| 1573 |
if( comm->datafd < FD_SETSIZE ) |
| 1574 |
activefds[comm->datafd] = 0; |
| 1575 |
|
| 1576 |
if( xpa && xpa->seldel && comm->seldptr ){ |
| 1577 |
(xpa->seldel)(comm->seldptr); |
| 1578 |
comm->seldptr = NULL; |
| 1579 |
} |
| 1580 |
|
| 1581 |
xclose(comm->datafd); |
| 1582 |
} |
| 1583 |
|
| 1584 |
comm->datafd = -1; |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
#ifdef ANSI_FUNC |
| 1600 |
int |
| 1601 |
XPAHandler (XPA xpa, int fd) |
| 1602 |
#else |
| 1603 |
int XPAHandler(xpa, fd) |
| 1604 |
XPA xpa; |
| 1605 |
int fd; |
| 1606 |
#endif |
| 1607 |
{ |
| 1608 |
char tbuf[SZ_LINE]; |
| 1609 |
char tbuf2[SZ_LINE]; |
| 1610 |
char lbuf[SZ_LINE]; |
| 1611 |
char cmd[SZ_LINE]; |
| 1612 |
char target[SZ_LINE]; |
| 1613 |
char method[SZ_LINE]; |
| 1614 |
char ctmpl[SZ_LINE]; |
| 1615 |
char ntmpl[SZ_LINE]; |
| 1616 |
char *paramlist=NULL; |
| 1617 |
char *acl; |
| 1618 |
int save_ack; |
| 1619 |
int tcmd; |
| 1620 |
int got=0; |
| 1621 |
int lp=0; |
| 1622 |
int cmdfd=-1; |
| 1623 |
unsigned short port=0; |
| 1624 |
unsigned int ip=0; |
| 1625 |
struct timeval tv; |
| 1626 |
XPAComm comm=NULL, tcomm=NULL, xcomm=NULL, ocomm=NULL; |
| 1627 |
XPA txpa=NULL; |
| 1628 |
NS ns=NULL; |
| 1629 |
fd_set readfds; |
| 1630 |
FPRINTF((stderr, "%sXPAHandler: entering with fd %d\n", _sp, fd)); |
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
for(txpa=xpahead; txpa!=NULL; txpa=txpa->next){ |
| 1637 |
if( txpa == xpa ) break; |
| 1638 |
} |
| 1639 |
if( txpa == NULL ){ |
| 1640 |
FPRINTF((stderr, "%sXPAHandler: xpa record %p is not in list\n", |
| 1641 |
_sp, xpa)); |
| 1642 |
return(XPA_RTN_NOCMD); |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
FD_ZERO(&readfds); |
| 1651 |
FD_SET(fd, &readfds); |
| 1652 |
tv.tv_sec = 0; |
| 1653 |
tv.tv_usec = 0; |
| 1654 |
while( xselect(fd+1, &readfds, NULL, NULL, &tv) <=0 ){ |
| 1655 |
if( xerrno == EINTR ) |
| 1656 |
continue; |
| 1657 |
FPRINTF((stderr, "%sXPAHandler: xpa fd %d is not ready\n", _sp, fd)); |
| 1658 |
return(XPA_RTN_NOCMD); |
| 1659 |
} |
| 1660 |
|
| 1661 |
|
| 1662 |
if( fd == xpa->fd ){ |
| 1663 |
if( (comm = CommNew(xpa, -1, 0, 0, NULL, NULL)) == NULL ) |
| 1664 |
return(XPA_RTN_NOCONN); |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
else{ |
| 1671 |
FPRINTF((stderr, "%sXPAHandler: returning after CommNew on fd %d\n", |
| 1672 |
_sp, fd)); |
| 1673 |
got = XPA_RTN_OK; |
| 1674 |
goto end; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
|
| 1679 |
for(comm=xpa->commhead; comm!= NULL; comm=comm->next){ |
| 1680 |
if( (fd == comm->cmdfd) || (fd == comm->datafd) ){ |
| 1681 |
break; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
|
| 1685 |
if( comm == NULL ){ |
| 1686 |
|
| 1687 |
if( XPAGets(xpa, fd, tbuf, SZ_LINE, 1) >0 ){ |
| 1688 |
got = XPA_RTN_UNCMD; |
| 1689 |
FPRINTF((stderr, "%sXPAHandler: fd %d received extra message:\n%s\n", |
| 1690 |
_sp, fd, tbuf)); |
| 1691 |
} |
| 1692 |
else{ |
| 1693 |
FPRINTF((stderr, "%sXPAHandler: no active comm record for fd %d\n", |
| 1694 |
_sp, fd)); |
| 1695 |
got = XPA_RTN_NOCMD2; |
| 1696 |
} |
| 1697 |
goto error; |
| 1698 |
} |
| 1699 |
|
| 1700 |
if( comm->status & XPA_STATUS_ACTIVE ){ |
| 1701 |
FPRINTF((stderr, "%sXPAHandler: fd %d returning to avoid recursion\n", |
| 1702 |
_sp, fd)); |
| 1703 |
got = 0; |
| 1704 |
goto end; |
| 1705 |
} |
| 1706 |
|
| 1707 |
ocomm = xpa->comm; |
| 1708 |
xpa->comm = comm; |
| 1709 |
|
| 1710 |
comm->message = 0; |
| 1711 |
|
| 1712 |
|
| 1713 |
if( fd == comm->datafd ){ |
| 1714 |
FPRINTF((stderr, "%sXPAHandler: jumping to cb for %d\n", _sp, fd)); |
| 1715 |
goto cb; |
| 1716 |
} |
| 1717 |
|
| 1718 |
|
| 1719 |
retry: |
| 1720 |
|
| 1721 |
lp = 0; |
| 1722 |
|
| 1723 |
if( XPAGets(xpa, comm->cmdfd, lbuf, SZ_LINE, stimeout) <=0 ){ |
| 1724 |
FPRINTF((stderr, "%sXPAHandler: fd %d read EOF\n", _sp, comm->cmdfd)); |
| 1725 |
got = XPA_RTN_OK; |
| 1726 |
goto eof; |
| 1727 |
} |
| 1728 |
|
| 1729 |
else if( (*lbuf == '\n') || (*lbuf == '\r') || !strcmp(lbuf, "telnet") ){ |
| 1730 |
if( (mtype == XPA_UNIX) || LOCALIP(comm->cmdip) ){ |
| 1731 |
if( comm->telnet == 0 ) |
| 1732 |
XPAPuts(xpa, comm->cmdfd, "Entering telnet mode ...\n", stimeout); |
| 1733 |
comm->telnet = 1; |
| 1734 |
comm->ack = 0; |
| 1735 |
comm->datafd = comm->cmdfd; |
| 1736 |
stimeout = -1; |
| 1737 |
goto retry; |
| 1738 |
} |
| 1739 |
else{ |
| 1740 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 1741 |
got = XPA_RTN_NOCMD; |
| 1742 |
goto error; |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
FPRINTF((stderr, "%sXPAHandler: fd %d read command: %s", |
| 1747 |
_sp, comm->cmdfd, lbuf)); |
| 1748 |
|
| 1749 |
|
| 1750 |
if( !word(lbuf, cmd, &lp) ){ |
| 1751 |
FPRINTF((stderr, "%sXPAHandler: missing target for fd %d\n", |
| 1752 |
_sp, comm->cmd)); |
| 1753 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 1754 |
got = XPA_RTN_NOCMD; |
| 1755 |
goto error; |
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
if( !strcmp(cmd, "help") ){ |
| 1760 |
if( (mtype == XPA_UNIX) || LOCALIP(comm->cmdip) ){ |
| 1761 |
XPAPuts(xpa, comm->cmdfd, |
| 1762 |
"xpaset|xpaget|xpainfo|xpaaccess [switches] class:name [params]\n", |
| 1763 |
stimeout); |
| 1764 |
XPAPuts(xpa, comm->cmdfd, |
| 1765 |
"switches:\n", |
| 1766 |
stimeout); |
| 1767 |
XPAPuts(xpa, comm->cmdfd, |
| 1768 |
"\t-a\t\tclient wants to accept() data connection\n", |
| 1769 |
stimeout); |
| 1770 |
XPAPuts(xpa, comm->cmdfd, |
| 1771 |
"\t-e <b|l>\tclient endian-ness: big(b) or little(l)\n", |
| 1772 |
stimeout); |
| 1773 |
XPAPuts(xpa, comm->cmdfd, |
| 1774 |
"\t-i id\t\tclient id string\n", |
| 1775 |
stimeout); |
| 1776 |
XPAPuts(xpa, comm->cmdfd, |
| 1777 |
"\t-p <proxyinfo>\t\tfrom xpans (e.g., for proxy processing)\n", |
| 1778 |
stimeout); |
| 1779 |
XPAPuts(xpa, comm->cmdfd, |
| 1780 |
"\t-n\t\tdon't ack back to client\n", |
| 1781 |
stimeout); |
| 1782 |
XPAPuts(xpa, comm->cmdfd, |
| 1783 |
"\t-t\t\tenter telnet mode (local only)\n", |
| 1784 |
stimeout); |
| 1785 |
|
| 1786 |
comm->telnet = 1; |
| 1787 |
comm->ack = 0; |
| 1788 |
goto retry; |
| 1789 |
} |
| 1790 |
else{ |
| 1791 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 1792 |
got = XPA_RTN_NOCMD; |
| 1793 |
goto error; |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
if( !strcmp(cmd, "xpaset") ) |
| 1799 |
tcmd = XPA_SET; |
| 1800 |
else if( !strcmp(cmd, "xpaget") ) |
| 1801 |
tcmd = XPA_GET; |
| 1802 |
else if( !strcmp(cmd, "xpainfo") ) |
| 1803 |
tcmd = XPA_INFO; |
| 1804 |
else if( !strcmp(cmd, "xpaaccess") ) |
| 1805 |
tcmd = XPA_ACCESS; |
| 1806 |
else if( !strcmp(cmd, "xpadata") ) |
| 1807 |
tcmd = XPA_DATA; |
| 1808 |
else if( !strcmp(cmd, "xpaaccept") ) |
| 1809 |
tcmd = XPA_ACCEPT; |
| 1810 |
else if( !strcmp(cmd, "xpanagle") || !strcmp(cmd, "XPA$OK") ) |
| 1811 |
tcmd = XPA_NAGLE; |
| 1812 |
else{ |
| 1813 |
FPRINTF((stderr, "%sXPAHandler: unknown command '%s' for fd %d\n", |
| 1814 |
_sp, cmd, comm->cmdfd)); |
| 1815 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 1816 |
got = XPA_RTN_NOCMD; |
| 1817 |
goto error; |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
while( word(lbuf, tbuf, &lp) ){ |
| 1822 |
if( *tbuf != '-' ) |
| 1823 |
break; |
| 1824 |
if( !strcmp(tbuf, "-a") ){ |
| 1825 |
comm->mode |= COMM_CONNECT; |
| 1826 |
} |
| 1827 |
else if( !strcmp(tbuf, "-e") ){ |
| 1828 |
if( word(lbuf, tbuf, &lp) ){ |
| 1829 |
if( *tbuf == 'b' ) |
| 1830 |
comm->cendian = "big"; |
| 1831 |
else if( *tbuf == 'l' ) |
| 1832 |
comm->cendian = "little"; |
| 1833 |
} |
| 1834 |
else{ |
| 1835 |
got = XPA_RTN_ILLCMD; |
| 1836 |
goto error; |
| 1837 |
} |
| 1838 |
} |
| 1839 |
else if( !strcmp(tbuf, "-f") ){ |
| 1840 |
|
| 1841 |
#if HAVE_MINGW32==0 |
| 1842 |
if( !word(lbuf, tbuf, &lp) || (sscanf(tbuf, "%p", &xcomm) != 1) ){ |
| 1843 |
#else |
| 1844 |
if( !word(lbuf, tbuf, &lp) || (sscanf(tbuf, "%x", &xcomm) != 1) ){ |
| 1845 |
#endif |
| 1846 |
got = XPA_RTN_ILLCMD; |
| 1847 |
goto done; |
| 1848 |
} |
| 1849 |
if( !word(lbuf, tbuf, &lp) || ((cmdfd = atoi(tbuf)) < 0) ){ |
| 1850 |
got = XPA_RTN_ILLCMD; |
| 1851 |
goto done; |
| 1852 |
} |
| 1853 |
} |
| 1854 |
else if( !strcmp(tbuf, "-i") ){ |
| 1855 |
if( word(lbuf, tbuf, &lp) ){ |
| 1856 |
if( comm->id != NULL ) xfree(comm->id); |
| 1857 |
comm->id = xstrdup(tbuf); |
| 1858 |
} |
| 1859 |
else{ |
| 1860 |
XPAError(xpa, xpaMessbuf[XPA_RTN_ILLCMD]); |
| 1861 |
got = XPA_RTN_ILLCMD; |
| 1862 |
goto error; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
else if( !strcmp(tbuf, "-n") ){ |
| 1866 |
comm->ack = 0; |
| 1867 |
} |
| 1868 |
else if( !strcmp(tbuf, "-p") ){ |
| 1869 |
if( word(lbuf, tbuf, &lp) ){ |
| 1870 |
if( comm->info != NULL ) xfree(comm->info); |
| 1871 |
comm->info = xstrdup(tbuf); |
| 1872 |
} |
| 1873 |
else{ |
| 1874 |
XPAError(xpa, xpaMessbuf[XPA_RTN_ILLCMD]); |
| 1875 |
got = XPA_RTN_ILLCMD; |
| 1876 |
goto error; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
else if( !strcmp(tbuf, "-t") ){ |
| 1880 |
comm->telnet = 1; |
| 1881 |
} |
| 1882 |
else{ |
| 1883 |
break; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
|
| 1888 |
if( comm->id == NULL ){ |
| 1889 |
comm->id = xstrdup("?"); |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
if( tcmd == XPA_NAGLE ){ |
| 1894 |
|
| 1895 |
if( comm->usebuf ){ |
| 1896 |
got = XPA_RTN_OK; |
| 1897 |
goto end; |
| 1898 |
} |
| 1899 |
|
| 1900 |
else{ |
| 1901 |
goto cb; |
| 1902 |
} |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
if( tcmd == XPA_ACCEPT ){ |
| 1907 |
FPRINTF((stderr, "%scmd is xpaaccept from client %d: %s", _sp, fd, lbuf)); |
| 1908 |
|
| 1909 |
lp = 0; |
| 1910 |
if( !word(lbuf, tbuf, &lp) || |
| 1911 |
strcmp(tbuf, "xpaaccept") || |
| 1912 |
!word(lbuf, method, &lp) ){ |
| 1913 |
got = -1; |
| 1914 |
goto error; |
| 1915 |
} |
| 1916 |
if( (cmdfd=XPAProxyConnect(xpa, method, &ip, &port, tbuf)) <0 ){ |
| 1917 |
FPRINTF((stderr, "%sXPAProxyConnect failed for: %d\n", _sp, fd)); |
| 1918 |
got = -1; |
| 1919 |
goto error; |
| 1920 |
} |
| 1921 |
if( (tcomm = CommNew(xpa, cmdfd, ip, port, method, NULL)) == NULL ){ |
| 1922 |
got = XPA_RTN_NOCONN; |
| 1923 |
goto error; |
| 1924 |
} |
| 1925 |
else{ |
| 1926 |
|
| 1927 |
FPRINTF((stderr, |
| 1928 |
"%sXPAHandler: fd %d exiting after proxy connect\n", _sp, |
| 1929 |
tcomm->cmdfd)); |
| 1930 |
got = XPA_RTN_OK; |
| 1931 |
goto end; |
| 1932 |
} |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
if( tcmd == XPA_DATA ){ |
| 1937 |
FPRINTF((stderr, "%sXPAHandler: processing data fd %d\n", |
| 1938 |
_sp, comm->cmdfd)); |
| 1939 |
|
| 1940 |
for(tcomm=xpa->commhead; tcomm!= NULL; tcomm=tcomm->next){ |
| 1941 |
if( (tcomm == xcomm) && (tcomm->cmdfd == cmdfd) ){ |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
if( tcomm ){ |
| 1947 |
|
| 1948 |
tcomm->datafd = comm->cmdfd; |
| 1949 |
tcomm->seldptr = comm->selcptr; |
| 1950 |
if( tcomm->dataname ) xfree(tcomm->dataname); |
| 1951 |
tcomm->dataname = xstrdup(comm->cmdname); |
| 1952 |
|
| 1953 |
CommFree(xpa, comm, 0); |
| 1954 |
|
| 1955 |
comm = tcomm; |
| 1956 |
xpa->comm = comm; |
| 1957 |
FPRINTF((stderr, "%sXPAHandler: found cmd fd %d for this data fd %d\n", |
| 1958 |
_sp, comm->cmdfd, comm->datafd)); |
| 1959 |
if( comm->cmd == XPA_SET ){ |
| 1960 |
FPRINTF((stderr, "%sXPAHandler: data %d returning await xpaset\n", |
| 1961 |
_sp, comm->datafd)); |
| 1962 |
got = XPA_RTN_OK; |
| 1963 |
goto end; |
| 1964 |
} |
| 1965 |
else{ |
| 1966 |
FPRINTF((stderr, "%sXPAHandler: data %d going on to process data\n", |
| 1967 |
_sp, comm->datafd)); |
| 1968 |
goto cb; |
| 1969 |
} |
| 1970 |
} |
| 1971 |
else{ |
| 1972 |
FPRINTF((stderr, "%sXPAHandler: data fd has no corresponding cmd %d\n", |
| 1973 |
_sp, comm->cmdfd)); |
| 1974 |
XPAError(xpa, xpaMessbuf[XPA_RTN_ILLCMD]); |
| 1975 |
got = XPA_RTN_ILLCMD; |
| 1976 |
goto error; |
| 1977 |
} |
| 1978 |
} |
| 1979 |
|
| 1980 |
|
| 1981 |
if( *tbuf == '\0' ){ |
| 1982 |
FPRINTF((stderr, "%sXPAHandler: missing target for fd %d\n", |
| 1983 |
_sp, comm->cmdfd)); |
| 1984 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 1985 |
got = XPA_RTN_NOCMD; |
| 1986 |
goto error; |
| 1987 |
} |
| 1988 |
else{ |
| 1989 |
strcpy(target, tbuf); |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
if( strcmp(xpa->xclass, XPANS_CLASS) || strcmp(xpa->name, XPANS_NAME) ){ |
| 1994 |
XPAParseName(target, ctmpl, ntmpl, SZ_LINE); |
| 1995 |
if( (strcmp(ctmpl,"?") && !tmatch(xpa->xclass, ctmpl)) || |
| 1996 |
(strcmp(ntmpl,"?") && !tmatch(xpa->name, ntmpl)) ){ |
| 1997 |
got = XPA_RTN_NOTARG; |
| 1998 |
goto error; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
if( comm->target != NULL ) xfree(comm->target); |
| 2002 |
comm->target = xstrdup(target); |
| 2003 |
|
| 2004 |
|
| 2005 |
paramlist = &(lbuf[lp]); |
| 2006 |
nowhite(paramlist, paramlist); |
| 2007 |
if( comm->paramlist != NULL ) xfree(comm->paramlist); |
| 2008 |
comm->paramlist = xstrdup(paramlist); |
| 2009 |
|
| 2010 |
comm->cmd = tcmd; |
| 2011 |
|
| 2012 |
FPRINTF((stderr, |
| 2013 |
"%sXPAHandler: processing command fd %d for target %s (%d)\n", |
| 2014 |
_sp, comm->cmdfd, target, comm->cmd)); |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
lp = 0; |
| 2019 |
if( XPACmdLookupReserved(xpa, comm->paramlist, &lp) ){ |
| 2020 |
comm->mode |= COMM_RESERVED; |
| 2021 |
} |
| 2022 |
|
| 2023 |
|
| 2024 |
switch(comm->cmd){ |
| 2025 |
case XPA_SET: |
| 2026 |
if( comm->mode & COMM_RESERVED ){ |
| 2027 |
comm->usebuf = 1; |
| 2028 |
comm->useacl = guseacl && (mtype != XPA_UNIX); |
| 2029 |
acl = "s"; |
| 2030 |
} |
| 2031 |
else if( xpa->receive_callback ){ |
| 2032 |
comm->usebuf = (xpa->receive_mode & XPA_MODE_BUF); |
| 2033 |
comm->useacl = guseacl && |
| 2034 |
(xpa->receive_mode & XPA_MODE_ACL) && (mtype != XPA_UNIX); |
| 2035 |
acl = "s"; |
| 2036 |
} |
| 2037 |
else{ |
| 2038 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOREC]); |
| 2039 |
got = XPA_RTN_NOREC; |
| 2040 |
goto error; |
| 2041 |
} |
| 2042 |
break; |
| 2043 |
case XPA_GET: |
| 2044 |
if( comm->mode & COMM_RESERVED ){ |
| 2045 |
comm->usebuf = 1; |
| 2046 |
comm->useacl = guseacl && (mtype != XPA_UNIX); |
| 2047 |
acl = "g"; |
| 2048 |
} |
| 2049 |
else if( xpa->send_callback ){ |
| 2050 |
comm->usebuf = (xpa->send_mode & XPA_MODE_BUF); |
| 2051 |
comm->useacl = guseacl && |
| 2052 |
(xpa->send_mode & XPA_MODE_ACL) && (mtype != XPA_UNIX); |
| 2053 |
acl = "g"; |
| 2054 |
} |
| 2055 |
else{ |
| 2056 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOSEND]); |
| 2057 |
got = XPA_RTN_NOSEND; |
| 2058 |
goto error; |
| 2059 |
} |
| 2060 |
break; |
| 2061 |
case XPA_INFO: |
| 2062 |
if( xpa->info_callback ){ |
| 2063 |
comm->usebuf = 0; |
| 2064 |
comm->useacl = guseacl && |
| 2065 |
(xpa->info_mode & XPA_MODE_ACL) && (mtype != XPA_UNIX); |
| 2066 |
acl = "i"; |
| 2067 |
} |
| 2068 |
else{ |
| 2069 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOINFO]); |
| 2070 |
got = XPA_RTN_NOINFO; |
| 2071 |
goto error; |
| 2072 |
} |
| 2073 |
break; |
| 2074 |
case XPA_ACCESS: |
| 2075 |
comm->usebuf = 0; |
| 2076 |
comm->useacl = guseacl && (mtype != XPA_UNIX); |
| 2077 |
|
| 2078 |
snprintf(tbuf2, SZ_LINE, "%sa%s", _sp, comm->paramlist?comm->paramlist:""); |
| 2079 |
acl = tbuf2; |
| 2080 |
break; |
| 2081 |
default: |
| 2082 |
FPRINTF((stderr, "%sXPAHandler: invalid access control check for fd %d\n", |
| 2083 |
_sp, comm->cmdfd)); |
| 2084 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOCMD]); |
| 2085 |
got = XPA_RTN_NOCMD; |
| 2086 |
goto error; |
| 2087 |
} |
| 2088 |
|
| 2089 |
|
| 2090 |
if( comm->useacl ){ |
| 2091 |
|
| 2092 |
if( comm->acl[comm->cmd] < 0 ){ |
| 2093 |
comm->acl[comm->cmd] = XPAAclCheck(xpa, comm->cmdip, acl); |
| 2094 |
} |
| 2095 |
|
| 2096 |
if( comm->acl[comm->cmd] <= 0 ){ |
| 2097 |
FPRINTF((stderr, "%sXPAHandler: fd %d FAILED acl check\n", |
| 2098 |
_sp, comm->cmdfd)); |
| 2099 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NOAUTH]); |
| 2100 |
got = XPA_RTN_NOAUTH; |
| 2101 |
goto error; |
| 2102 |
} |
| 2103 |
FPRINTF((stderr, "%sXPAHandler: fd %d passed acl check\n", |
| 2104 |
_sp, comm->cmdfd)); |
| 2105 |
} |
| 2106 |
|
| 2107 |
|
| 2108 |
if( comm->telnet ){ |
| 2109 |
comm->datafd = comm->cmdfd; |
| 2110 |
} |
| 2111 |
|
| 2112 |
else if( (comm->cmd == XPA_INFO) && !comm->ack ){ |
| 2113 |
goto cb; |
| 2114 |
} |
| 2115 |
|
| 2116 |
else{ |
| 2117 |
if( comm->usebuf ){ |
| 2118 |
if( comm->mode & COMM_CONNECT ){ |
| 2119 |
snprintf(lbuf, SZ_LINE, "%s XPA$DATA accept %p %d (%s:%s %s)\n", |
| 2120 |
comm->id, comm, comm->cmdfd, |
| 2121 |
xpa->xclass, xpa->name, xpa->method); |
| 2122 |
} |
| 2123 |
else{ |
| 2124 |
snprintf(lbuf, SZ_LINE, "%s XPA$DATA connect %p %d (%s:%s %s)\n", |
| 2125 |
comm->id, comm, comm->cmdfd, |
| 2126 |
xpa->xclass, xpa->name, xpa->method); |
| 2127 |
} |
| 2128 |
} |
| 2129 |
|
| 2130 |
else{ |
| 2131 |
snprintf(lbuf, SZ_LINE, "%s XPA$NODATA (%s:%s %s)\n", |
| 2132 |
comm->id, xpa->xclass, xpa->name, xpa->method); |
| 2133 |
} |
| 2134 |
FPRINTF((stderr, "%sXPAHandler: fd %d sends string: %s", |
| 2135 |
_sp, comm->cmdfd, lbuf)); |
| 2136 |
if( XPAPuts(xpa, comm->cmdfd, lbuf, stimeout) <= 0 ){ |
| 2137 |
FPRINTF((stderr, "%sXPAHandler: fd %d couldn't send string: %s", |
| 2138 |
_sp, comm->cmdfd, lbuf)); |
| 2139 |
got = -1; |
| 2140 |
goto error; |
| 2141 |
} |
| 2142 |
|
| 2143 |
FPRINTF((stderr, |
| 2144 |
"%sXPAHandler: fd %d exiting to wait for nagle or connect req\n", |
| 2145 |
_sp, comm->cmdfd)); |
| 2146 |
got = XPA_RTN_OK; |
| 2147 |
goto end; |
| 2148 |
} |
| 2149 |
|
| 2150 |
|
| 2151 |
cb: |
| 2152 |
|
| 2153 |
comm->status |= XPA_STATUS_ACTIVE; |
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
XPAActive(xpa, comm, 0); |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
comm->buf = NULL; |
| 2162 |
comm->len = 0; |
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
if( !comm->ack && (comm->cmd != XPA_INFO) ){ |
| 2167 |
FPRINTF((stderr, "%sXPAHandler: sending OK to non-acking client %d %d\n", |
| 2168 |
_sp, comm->cmdfd, comm->datafd)); |
| 2169 |
comm->ack = 1; |
| 2170 |
XPAOK(xpa); |
| 2171 |
comm->ack = 0; |
| 2172 |
} |
| 2173 |
|
| 2174 |
|
| 2175 |
switch(comm->cmd){ |
| 2176 |
case XPA_GET: |
| 2177 |
FPRINTF((stderr, |
| 2178 |
"%sXPAHandler: processing xpaget for %d %d\n", _sp, |
| 2179 |
comm->cmdfd, comm->datafd)); |
| 2180 |
|
| 2181 |
if( comm->mode & COMM_RESERVED ){ |
| 2182 |
got = XPASendCommands(xpa->send_data, xpa, comm->paramlist, |
| 2183 |
&(comm->buf), &(comm->len)); |
| 2184 |
} |
| 2185 |
else{ |
| 2186 |
got = (xpa->send_callback)(xpa->send_data, xpa, comm->paramlist, |
| 2187 |
&(comm->buf), &(comm->len)); |
| 2188 |
} |
| 2189 |
if( (got == 0) && (comm->buf != NULL) && (comm->len > 0) ){ |
| 2190 |
if( XPAPutBuf(xpa, comm->datafd, comm->buf, comm->len, ltimeout) < 0 ){ |
| 2191 |
PERROR(("XPAHandler write buf")); |
| 2192 |
XPAError(xpa, "XPA could not write data to client"); |
| 2193 |
goto done; |
| 2194 |
} |
| 2195 |
} |
| 2196 |
if( (xpa->send_mode & XPA_MODE_FREEBUF) && (comm->buf != NULL) ){ |
| 2197 |
if( xpa->comm->myfree != NULL ){ |
| 2198 |
if( xpa->comm->myfree_ptr != NULL ){ |
| 2199 |
xpa->comm->myfree(xpa->comm->myfree_ptr); |
| 2200 |
} |
| 2201 |
else{ |
| 2202 |
xpa->comm->myfree(comm->buf); |
| 2203 |
} |
| 2204 |
} |
| 2205 |
else{ |
| 2206 |
xfree(comm->buf); |
| 2207 |
} |
| 2208 |
} |
| 2209 |
|
| 2210 |
if( !comm->message ){ |
| 2211 |
if( got ) |
| 2212 |
XPAError(xpa, "error detected in send callback routine"); |
| 2213 |
else |
| 2214 |
XPAOK(xpa); |
| 2215 |
} |
| 2216 |
FPRINTF((stderr, |
| 2217 |
"%sXPAHandler: finished xpaget for %d %d\n", _sp, |
| 2218 |
comm->cmdfd, comm->datafd)); |
| 2219 |
break; |
| 2220 |
case XPA_SET: |
| 2221 |
FPRINTF((stderr, |
| 2222 |
"%sXPAHandler: processing xpaset for %d %d\n", _sp, |
| 2223 |
comm->cmdfd, comm->datafd)); |
| 2224 |
|
| 2225 |
if( comm->mode & COMM_RESERVED ){ |
| 2226 |
got=XPAReceiveCommands(xpa->receive_data, xpa, |
| 2227 |
comm->paramlist, NULL, 0); |
| 2228 |
} |
| 2229 |
else{ |
| 2230 |
|
| 2231 |
if( (comm->datafd >= 0) && |
| 2232 |
comm->usebuf && (xpa->receive_mode & XPA_MODE_FILLBUF) ){ |
| 2233 |
if(XPAGetBuf(xpa, comm->datafd, &(comm->buf), &(comm->len), -1) <0){ |
| 2234 |
XPAError(xpa, xpaMessbuf[XPA_RTN_NODATA]); |
| 2235 |
FPRINTF((stderr, "%sXPAHandler: no data for XPAGetBuf on %d\n", _sp, |
| 2236 |
comm->datafd)); |
| 2237 |
got = -1; |
| 2238 |
goto done; |
| 2239 |
} |
| 2240 |
else{ |
| 2241 |
|
| 2242 |
XPACloseData(xpa, comm); |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
got = (xpa->receive_callback)(xpa->receive_data, xpa, |
| 2247 |
comm->paramlist, comm->buf, comm->len); |
| 2248 |
} |
| 2249 |
if( (xpa->receive_mode & XPA_MODE_FREEBUF) && (comm->buf != NULL) ){ |
| 2250 |
if( xpa->comm->myfree != NULL ){ |
| 2251 |
if( xpa->comm->myfree_ptr != NULL ){ |
| 2252 |
xpa->comm->myfree(xpa->comm->myfree_ptr); |
| 2253 |
} |
| 2254 |
else{ |
| 2255 |
xpa->comm->myfree(comm->buf); |
| 2256 |
} |
| 2257 |
} |
| 2258 |
else{ |
| 2259 |
xfree(comm->buf); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
if( !comm->message ){ |
| 2264 |
if( got ){ |
| 2265 |
XPAError(xpa, "error detected in receive callback routine"); |
| 2266 |
} |
| 2267 |
else{ |
| 2268 |
XPAOK(xpa); |
| 2269 |
} |
| 2270 |
} |
| 2271 |
FPRINTF((stderr, |
| 2272 |
"%sXPAHandler: finished xpaset for %d %d\n", _sp, |
| 2273 |
comm->cmdfd, comm->datafd)); |
| 2274 |
break; |
| 2275 |
case XPA_INFO: |
| 2276 |
|
| 2277 |
if( comm->ack ) |
| 2278 |
XPAOK(xpa); |
| 2279 |
save_ack = comm->ack; |
| 2280 |
|
| 2281 |
comm->ack = 0; |
| 2282 |
|
| 2283 |
(xpa->info_callback)(xpa->info_data, xpa, comm->paramlist); |
| 2284 |
comm->ack = save_ack; |
| 2285 |
break; |
| 2286 |
case XPA_ACCESS: |
| 2287 |
|
| 2288 |
comm->ack = 1; |
| 2289 |
|
| 2290 |
if( comm->paramlist && *comm->paramlist ){ |
| 2291 |
char *s; |
| 2292 |
for(s=comm->paramlist; *s; s++){ |
| 2293 |
switch(*s){ |
| 2294 |
case 'g': |
| 2295 |
if( !xpa->send_callback ){ |
| 2296 |
XPAError(xpa, "no send callback (i.e., can't xpaget)"); |
| 2297 |
goto done; |
| 2298 |
} |
| 2299 |
break; |
| 2300 |
case 'i': |
| 2301 |
if( !xpa->info_callback ){ |
| 2302 |
XPAError(xpa, "no info callback (i.e., can't xpainfo)"); |
| 2303 |
goto done; |
| 2304 |
} |
| 2305 |
break; |
| 2306 |
case 's': |
| 2307 |
if( !xpa->receive_callback ){ |
| 2308 |
XPAError(xpa, "no receive callback (i.e., can't xpaset)"); |
| 2309 |
goto done; |
| 2310 |
} |
| 2311 |
break; |
| 2312 |
case 'a': |
| 2313 |
break; |
| 2314 |
default: |
| 2315 |
XPAError(xpa, "unknown xpa access type"); |
| 2316 |
goto done; |
| 2317 |
} |
| 2318 |
} |
| 2319 |
|
| 2320 |
XPAOK(xpa); |
| 2321 |
} |
| 2322 |
|
| 2323 |
else |
| 2324 |
XPAOK(xpa); |
| 2325 |
break; |
| 2326 |
default: |
| 2327 |
|
| 2328 |
FPRINTF((stderr, "%sXPAHandler: invalid command #%d for fd %d\n", |
| 2329 |
_sp, comm->cmd, comm->cmdfd)); |
| 2330 |
got = XPA_RTN_NOCMD; |
| 2331 |
goto error; |
| 2332 |
break; |
| 2333 |
} |
| 2334 |
|
| 2335 |
done: |
| 2336 |
FPRINTF((stderr, |
| 2337 |
"%sXPAHandler: finished processing %d with status %d\n", _sp, |
| 2338 |
fd, got)); |
| 2339 |
|
| 2340 |
XPAActive(xpa, comm, 1); |
| 2341 |
|
| 2342 |
|
| 2343 |
if( comm ){ |
| 2344 |
|
| 2345 |
XPACloseData(xpa, comm); |
| 2346 |
|
| 2347 |
comm->status &= ~XPA_STATUS_ACTIVE; |
| 2348 |
} |
| 2349 |
|
| 2350 |
|
| 2351 |
goto end; |
| 2352 |
|
| 2353 |
eof: |
| 2354 |
|
| 2355 |
if( comm ){ |
| 2356 |
ns = comm->ns; |
| 2357 |
CommFree(xpa, comm, 1); |
| 2358 |
comm = NULL; |
| 2359 |
if( ns && !ns->nproxy && !ns->nxpa ){ |
| 2360 |
FPRINTF((stderr, "%sXPAHandler: closing ns %s\n", _sp, ns->name)); |
| 2361 |
XPANSClose(xpa, ns); |
| 2362 |
} |
| 2363 |
} |
| 2364 |
|
| 2365 |
goto end; |
| 2366 |
|
| 2367 |
|
| 2368 |
error: |
| 2369 |
FPRINTF((stderr, "%sXPAHandler ERROR: return code %d on %d\n", |
| 2370 |
_sp, got, fd)); |
| 2371 |
|
| 2372 |
XPAActive(xpa, comm, 1); |
| 2373 |
|
| 2374 |
xpa->comm = NULL; |
| 2375 |
|
| 2376 |
if( comm ){ |
| 2377 |
if( comm->ns ) |
| 2378 |
CommFree(xpa, comm, 0); |
| 2379 |
else |
| 2380 |
CommFree(xpa, comm, 1); |
| 2381 |
comm = NULL; |
| 2382 |
} |
| 2383 |
|
| 2384 |
goto end; |
| 2385 |
|
| 2386 |
end: |
| 2387 |
|
| 2388 |
if( xpa->status & XPA_STATUS_FREE ){ |
| 2389 |
XPAFree(xpa); |
| 2390 |
} |
| 2391 |
|
| 2392 |
else{ |
| 2393 |
xpa->comm = ocomm; |
| 2394 |
} |
| 2395 |
|
| 2396 |
|
| 2397 |
return(got); |
| 2398 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
#ifdef ANSI_FUNC |
| 2412 |
void |
| 2413 |
XPAMode (char *mode, int *flag, char *name, int mask, int def) |
| 2414 |
#else |
| 2415 |
void XPAMode(mode, flag, name, mask, def) |
| 2416 |
char *mode; |
| 2417 |
int *flag; |
| 2418 |
char *name; |
| 2419 |
int mask; |
| 2420 |
int def; |
| 2421 |
#endif |
| 2422 |
{ |
| 2423 |
char tbuf[SZ_LINE]; |
| 2424 |
char s[SZ_LINE]; |
| 2425 |
|
| 2426 |
|
| 2427 |
if( mode && *mode ){ |
| 2428 |
strncpy(s, mode, SZ_LINE-1); |
| 2429 |
s[SZ_LINE-1] = '\0'; |
| 2430 |
} |
| 2431 |
else |
| 2432 |
goto error; |
| 2433 |
|
| 2434 |
if( keyword(s, name, tbuf, SZ_LINE) ){ |
| 2435 |
if( istrue(tbuf) ) |
| 2436 |
*flag |= mask; |
| 2437 |
else |
| 2438 |
*flag &= ~mask; |
| 2439 |
return; |
| 2440 |
} |
| 2441 |
else |
| 2442 |
goto error; |
| 2443 |
|
| 2444 |
error: |
| 2445 |
if( def ) |
| 2446 |
*flag |= mask; |
| 2447 |
else |
| 2448 |
*flag &= ~mask; |
| 2449 |
} |
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
#ifdef ANSI_FUNC |
| 2463 |
int |
| 2464 |
XPAShortTimeout (void) |
| 2465 |
#else |
| 2466 |
int XPAShortTimeout() |
| 2467 |
#endif |
| 2468 |
{ |
| 2469 |
return(stimeout); |
| 2470 |
} |
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
#ifdef ANSI_FUNC |
| 2484 |
int |
| 2485 |
XPALongTimeout (void) |
| 2486 |
#else |
| 2487 |
int XPALongTimeout() |
| 2488 |
#endif |
| 2489 |
{ |
| 2490 |
return(ltimeout); |
| 2491 |
} |
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
#ifdef ANSI_FUNC |
| 2505 |
int |
| 2506 |
XPAReceiveLTimeout (void *client_data, void *call_data, char *paramlist, |
| 2507 |
char *buf, size_t len) |
| 2508 |
#else |
| 2509 |
int XPAReceiveLTimeout(client_data, call_data, paramlist, buf, len) |
| 2510 |
void *client_data; |
| 2511 |
void *call_data; |
| 2512 |
char *paramlist; |
| 2513 |
char *buf; |
| 2514 |
size_t len; |
| 2515 |
#endif |
| 2516 |
{ |
| 2517 |
XPA xpa = (XPA)call_data; |
| 2518 |
char tbuf[SZ_LINE]; |
| 2519 |
char *s; |
| 2520 |
|
| 2521 |
if( paramlist && *paramlist ){ |
| 2522 |
strncpy(tbuf, paramlist, SZ_LINE-1); |
| 2523 |
tbuf[SZ_LINE-1] = '\0'; |
| 2524 |
nocr(tbuf); |
| 2525 |
culc(tbuf); |
| 2526 |
if( !strcmp(tbuf, "reset") ){ |
| 2527 |
ltimeout = XPA_LONG_TIMEOUT; |
| 2528 |
if( (s=(char *)getenv("XPA_LONG_TIMEOUT")) != NULL ) |
| 2529 |
ltimeout = atoi(s); |
| 2530 |
} |
| 2531 |
else{ |
| 2532 |
ltimeout = atoi(tbuf); |
| 2533 |
} |
| 2534 |
return(0); |
| 2535 |
} |
| 2536 |
else{ |
| 2537 |
XPAError(xpa, "missing long timeout value"); |
| 2538 |
return(-1); |
| 2539 |
} |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
#ifdef ANSI_FUNC |
| 2554 |
int |
| 2555 |
XPASendLTimeout (void *client_data, void *call_data, char *paramlist, |
| 2556 |
char **buf, size_t *len) |
| 2557 |
#else |
| 2558 |
int XPASendLTimeout(client_data, call_data, paramlist, buf, len) |
| 2559 |
void *client_data; |
| 2560 |
void *call_data; |
| 2561 |
char *paramlist; |
| 2562 |
char **buf; |
| 2563 |
size_t *len; |
| 2564 |
#endif |
| 2565 |
{ |
| 2566 |
XPA xpa = (XPA)call_data; |
| 2567 |
char tbuf[SZ_LINE]; |
| 2568 |
|
| 2569 |
snprintf(tbuf, SZ_LINE, "%d\n", ltimeout); |
| 2570 |
send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0); |
| 2571 |
return(0); |
| 2572 |
} |
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
#ifdef ANSI_FUNC |
| 2586 |
int |
| 2587 |
XPAReceiveSTimeout (void *client_data, void *call_data, char *paramlist, |
| 2588 |
char *buf, size_t len) |
| 2589 |
#else |
| 2590 |
int XPAReceiveSTimeout(client_data, call_data, paramlist, buf, len) |
| 2591 |
void *client_data; |
| 2592 |
void *call_data; |
| 2593 |
char *paramlist; |
| 2594 |
char *buf; |
| 2595 |
size_t len; |
| 2596 |
#endif |
| 2597 |
{ |
| 2598 |
XPA xpa = (XPA)call_data; |
| 2599 |
char tbuf[SZ_LINE]; |
| 2600 |
char *s; |
| 2601 |
|
| 2602 |
if( paramlist && *paramlist ){ |
| 2603 |
strncpy(tbuf, paramlist, SZ_LINE-1); |
| 2604 |
tbuf[SZ_LINE-1] = '\0'; |
| 2605 |
nocr(tbuf); |
| 2606 |
culc(tbuf); |
| 2607 |
if( !strcmp(tbuf, "reset") ){ |
| 2608 |
stimeout = XPA_SHORT_TIMEOUT; |
| 2609 |
if( (s=(char *)getenv("XPA_SHORT_TIMEOUT")) != NULL ) |
| 2610 |
stimeout = atoi(s); |
| 2611 |
} |
| 2612 |
else{ |
| 2613 |
stimeout = atoi(tbuf); |
| 2614 |
} |
| 2615 |
return(0); |
| 2616 |
} |
| 2617 |
else{ |
| 2618 |
XPAError(xpa, "missing short timeout value"); |
| 2619 |
return(-1); |
| 2620 |
} |
| 2621 |
} |
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
#ifdef ANSI_FUNC |
| 2635 |
int |
| 2636 |
XPASendSTimeout (void *client_data, void *call_data, char *paramlist, |
| 2637 |
char **buf, size_t *len) |
| 2638 |
#else |
| 2639 |
int XPASendSTimeout(client_data, call_data, paramlist, buf, len) |
| 2640 |
void *client_data; |
| 2641 |
void *call_data; |
| 2642 |
char *paramlist; |
| 2643 |
char **buf; |
| 2644 |
size_t *len; |
| 2645 |
#endif |
| 2646 |
{ |
| 2647 |
XPA xpa = (XPA)call_data; |
| 2648 |
char tbuf[SZ_LINE]; |
| 2649 |
|
| 2650 |
snprintf(tbuf, SZ_LINE, "%d\n", stimeout); |
| 2651 |
send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0); |
| 2652 |
return(0); |
| 2653 |
} |
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
#ifdef ANSI_FUNC |
| 2667 |
int |
| 2668 |
XPAVerbosity (void) |
| 2669 |
#else |
| 2670 |
int XPAVerbosity() |
| 2671 |
#endif |
| 2672 |
{ |
| 2673 |
return(verbosity); |
| 2674 |
} |
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
#ifdef ANSI_FUNC |
| 2688 |
int |
| 2689 |
XPASigusr1 (void) |
| 2690 |
#else |
| 2691 |
int XPASigusr1() |
| 2692 |
#endif |
| 2693 |
{ |
| 2694 |
return(sigusr1); |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
#ifdef ANSI_FUNC |
| 2709 |
void |
| 2710 |
XPAInitEnv (void) |
| 2711 |
#else |
| 2712 |
void XPAInitEnv() |
| 2713 |
#endif |
| 2714 |
{ |
| 2715 |
char *s; |
| 2716 |
|
| 2717 |
if( !tmpdir ){ |
| 2718 |
|
| 2719 |
mtype=XPAMethod(NULL); |
| 2720 |
|
| 2721 |
if( mtype != XPA_UNIX ){ |
| 2722 |
XPAAclNew(NULL, 0); |
| 2723 |
XPAPortNew(NULL, 0); |
| 2724 |
} |
| 2725 |
|
| 2726 |
if( (s=(char *)getenv("XPA_SHORT_TIMEOUT")) != NULL ) |
| 2727 |
stimeout = atoi(s); |
| 2728 |
|
| 2729 |
if( (s=(char *)getenv("XPA_LONG_TIMEOUT")) != NULL ) |
| 2730 |
ltimeout = atoi(s); |
| 2731 |
|
| 2732 |
if( (s=(char *)getenv("XPA_CONNECT_TIMEOUT")) != NULL ) |
| 2733 |
ctimeout = atoi(s); |
| 2734 |
|
| 2735 |
if( (s=(char *)getenv("XPA_VERBOSITY")) != NULL ){ |
| 2736 |
if( istrue(s) ) |
| 2737 |
verbosity = XPA_VERBOSITY; |
| 2738 |
else if( isfalse(s) ) |
| 2739 |
verbosity = 0; |
| 2740 |
else |
| 2741 |
verbosity = atoi(s); |
| 2742 |
if( verbosity < 0 ) |
| 2743 |
verbosity = 0; |
| 2744 |
} |
| 2745 |
|
| 2746 |
if( (s=(char *)getenv("XPA_ACL")) != NULL ) |
| 2747 |
guseacl = istrue(s); |
| 2748 |
|
| 2749 |
if( (s=(char *)getenv("XPA_TIMESTAMP_ERRORS")) != NULL ) |
| 2750 |
etimestamp = istrue(s); |
| 2751 |
|
| 2752 |
if( (s=(char *)getenv("XPA_NSREGISTER")) != NULL ) |
| 2753 |
nsregister = istrue(s); |
| 2754 |
|
| 2755 |
if( (s=(char *)getenv("XPA_SIGUSR1")) != NULL ) |
| 2756 |
sigusr1 = istrue(s); |
| 2757 |
|
| 2758 |
if( (s=(char *)getenv("XPA_VERSIONCHECK")) != NULL ){ |
| 2759 |
if( istrue(s) ) |
| 2760 |
vercheck = 1; |
| 2761 |
else if( isfalse(s) ) |
| 2762 |
vercheck = 0; |
| 2763 |
else |
| 2764 |
vercheck = atoi(s); |
| 2765 |
} |
| 2766 |
|
| 2767 |
if( (s=(char *)getenv("XPA_IOCALLSXPA")) != NULL ){ |
| 2768 |
if( istrue(s) ){ |
| 2769 |
XPAIOCallsXPA(1); |
| 2770 |
} |
| 2771 |
else if( isfalse(s) ){ |
| 2772 |
XPAIOCallsXPA(0); |
| 2773 |
} |
| 2774 |
} |
| 2775 |
|
| 2776 |
if( tmpdir == NULL ){ |
| 2777 |
if( (s=(char *)getenv("XPA_TMPDIR")) != NULL ) |
| 2778 |
tmpdir = xstrdup(s); |
| 2779 |
else if( (s=(char *)getenv("TMPDIR")) != NULL ) |
| 2780 |
tmpdir = xstrdup(s); |
| 2781 |
else if( (s=(char *)getenv("TMP")) != NULL ) |
| 2782 |
tmpdir = xstrdup(s); |
| 2783 |
else |
| 2784 |
tmpdir = xstrdup(XPA_TMPDIR); |
| 2785 |
} |
| 2786 |
|
| 2787 |
xmkdir(tmpdir, 0777); |
| 2788 |
xchmod(tmpdir, 0777); |
| 2789 |
#if HAVE_MINGW32==0 |
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
xsignal_sigpipe(); |
| 2794 |
#endif |
| 2795 |
|
| 2796 |
xsocketstartup(); |
| 2797 |
} |
| 2798 |
} |
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
#ifdef ANSI_FUNC |
| 2812 |
int |
| 2813 |
XPANSAdd (XPA xpa, char *host, char *mode) |
| 2814 |
#else |
| 2815 |
int XPANSAdd(xpa, host, mode) |
| 2816 |
XPA xpa; |
| 2817 |
char *host; |
| 2818 |
char *mode; |
| 2819 |
#endif |
| 2820 |
{ |
| 2821 |
char username[SZ_LINE]; |
| 2822 |
char tbuf[SZ_LINE]; |
| 2823 |
char xmode[SZ_LINE]; |
| 2824 |
char *cmd="add"; |
| 2825 |
char *s; |
| 2826 |
NS ns; |
| 2827 |
struct passwd *pw; |
| 2828 |
|
| 2829 |
|
| 2830 |
if( !xpa ) |
| 2831 |
return(0); |
| 2832 |
|
| 2833 |
|
| 2834 |
if( !strcmp(xpa->name, XPANS_NAME) ){ |
| 2835 |
return(0); |
| 2836 |
} |
| 2837 |
|
| 2838 |
|
| 2839 |
if( mode ){ |
| 2840 |
strncpy(xmode, mode, SZ_LINE-1); |
| 2841 |
xmode[SZ_LINE-1] = '\0'; |
| 2842 |
if( keyword(xmode, "proxy", tbuf, SZ_LINE) && istrue(tbuf) ){ |
| 2843 |
cmd="addproxy"; |
| 2844 |
} |
| 2845 |
} |
| 2846 |
|
| 2847 |
|
| 2848 |
if( (ns=XPANSOpen(xpa, host, 1)) != NULL ){ |
| 2849 |
|
| 2850 |
if( (s=(char *)getenv("XPA_LOGNAME")) != NULL ) |
| 2851 |
strncpy(username, s, SZ_LINE-1); |
| 2852 |
else if( (s=(char *)getenv("LOGNAME")) != NULL ) |
| 2853 |
strncpy(username, s, SZ_LINE-1); |
| 2854 |
#if HAVE_MINGW32==0 |
| 2855 |
|
| 2856 |
else if( (pw=getpwuid(geteuid())) ) |
| 2857 |
strncpy(username, pw->pw_name, SZ_LINE-1); |
| 2858 |
#endif |
| 2859 |
|
| 2860 |
if( *username == '\0' ) |
| 2861 |
strcpy(username, "unknown"); |
| 2862 |
|
| 2863 |
username[SZ_LINE-1] = '\0'; |
| 2864 |
|
| 2865 |
|
| 2866 |
snprintf(tbuf, SZ_LINE, "%s %s %s:%s %s %s\n", |
| 2867 |
cmd, xpa->method, xpa->xclass, xpa->name, xpa->type, username); |
| 2868 |
if( XPAPuts(xpa, ns->fd, tbuf, stimeout) < 0 ){ |
| 2869 |
return(-1); |
| 2870 |
} |
| 2871 |
|
| 2872 |
if( XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0 ){ |
| 2873 |
if( !strncmp(tbuf, "XPA$OK", 6) ){ |
| 2874 |
|
| 2875 |
if( !strcmp(cmd, "addproxy") && xpa ){ |
| 2876 |
if( CommNew(xpa, ns->fd, ns->ip, ns->port, ns->name, ns) ){ |
| 2877 |
|
| 2878 |
ns->nproxy += 1; |
| 2879 |
} |
| 2880 |
} |
| 2881 |
else{ |
| 2882 |
|
| 2883 |
ns->nxpa += 1; |
| 2884 |
} |
| 2885 |
return(0); |
| 2886 |
} |
| 2887 |
else if( !strncmp(tbuf, "XPA$EXISTS", 10) ) |
| 2888 |
return(0); |
| 2889 |
else |
| 2890 |
return(-1); |
| 2891 |
} |
| 2892 |
else{ |
| 2893 |
return(-1); |
| 2894 |
} |
| 2895 |
} |
| 2896 |
else{ |
| 2897 |
return(-1); |
| 2898 |
} |
| 2899 |
} |
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
#ifdef ANSI_FUNC |
| 2913 |
int |
| 2914 |
XPANSDel (XPA xpa, char *host, char *mode) |
| 2915 |
#else |
| 2916 |
int XPANSDel(xpa, host, mode) |
| 2917 |
XPA xpa; |
| 2918 |
char *host; |
| 2919 |
char *mode; |
| 2920 |
#endif |
| 2921 |
{ |
| 2922 |
int got=0; |
| 2923 |
char tbuf[SZ_LINE]; |
| 2924 |
char xmode[SZ_LINE]; |
| 2925 |
char *cmd="del"; |
| 2926 |
NS ns; |
| 2927 |
|
| 2928 |
|
| 2929 |
if( !xpa ) |
| 2930 |
return(0); |
| 2931 |
|
| 2932 |
|
| 2933 |
if( xpa->name && !strcmp(xpa->name, XPANS_NAME) ){ |
| 2934 |
return(0); |
| 2935 |
} |
| 2936 |
|
| 2937 |
|
| 2938 |
if( (xpa->method == NULL) || (*xpa->method == '\0') ){ |
| 2939 |
return(0); |
| 2940 |
} |
| 2941 |
|
| 2942 |
|
| 2943 |
if( mode ){ |
| 2944 |
strncpy(xmode, mode, SZ_LINE-1); |
| 2945 |
xmode[SZ_LINE-1] = '\0'; |
| 2946 |
if( keyword(xmode, "proxy", tbuf, SZ_LINE) && istrue(tbuf) ){ |
| 2947 |
cmd="delproxy"; |
| 2948 |
} |
| 2949 |
} |
| 2950 |
|
| 2951 |
|
| 2952 |
if( (ns=XPANSOpen(xpa, host, -1)) != NULL ){ |
| 2953 |
|
| 2954 |
snprintf(tbuf, SZ_LINE, "%s %s\n", cmd, xpa->method); |
| 2955 |
XPAPuts(xpa, ns->fd, tbuf, stimeout); |
| 2956 |
|
| 2957 |
if( XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0 ){ |
| 2958 |
if( !strncmp(tbuf, "XPA$OK", 6) ){ |
| 2959 |
|
| 2960 |
ns->nxpa -= 1; |
| 2961 |
|
| 2962 |
if( !ns->nxpa && !ns->nproxy ){ |
| 2963 |
XPANSClose(xpa, ns); |
| 2964 |
} |
| 2965 |
} |
| 2966 |
else{ |
| 2967 |
got = -1; |
| 2968 |
} |
| 2969 |
} |
| 2970 |
else{ |
| 2971 |
got = -1; |
| 2972 |
} |
| 2973 |
} |
| 2974 |
else |
| 2975 |
got = -1; |
| 2976 |
return(got); |
| 2977 |
} |
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
#ifdef ANSI_FUNC |
| 2991 |
int |
| 2992 |
XPAVersionCheck (char *serv, char *nsv) |
| 2993 |
#else |
| 2994 |
int XPAVersionCheck(serv, nsv) |
| 2995 |
char *serv; |
| 2996 |
char *nsv; |
| 2997 |
#endif |
| 2998 |
{ |
| 2999 |
int i; |
| 3000 |
int ip1=0; |
| 3001 |
int ip2=0; |
| 3002 |
int v1=0; |
| 3003 |
int v2=0; |
| 3004 |
int got=0; |
| 3005 |
int vsize=2; |
| 3006 |
char s1[SZ_LINE]; |
| 3007 |
char s2[SZ_LINE]; |
| 3008 |
|
| 3009 |
|
| 3010 |
if( vercheck <=0 ) |
| 3011 |
return(0); |
| 3012 |
|
| 3013 |
if( !word(serv, s1, &ip1) || !word(nsv, s2, &ip2) ) |
| 3014 |
return(1); |
| 3015 |
|
| 3016 |
if( !strcasecmp(s1, s2) ) |
| 3017 |
return(0); |
| 3018 |
|
| 3019 |
|
| 3020 |
newdtable(".be"); |
| 3021 |
|
| 3022 |
for(i=0; i<vsize; i++){ |
| 3023 |
if( !word(serv, s1, &ip1) || !word(nsv, s2, &ip2) ){ |
| 3024 |
break; |
| 3025 |
} |
| 3026 |
v1 = atoi(s1); |
| 3027 |
v2 = atoi(s2); |
| 3028 |
if( v1 > v2 ){ |
| 3029 |
got = 1; |
| 3030 |
break; |
| 3031 |
} |
| 3032 |
if( v1 < v2 ){ |
| 3033 |
got = -1; |
| 3034 |
break; |
| 3035 |
} |
| 3036 |
} |
| 3037 |
|
| 3038 |
freedtable(); |
| 3039 |
return(got); |
| 3040 |
} |
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
|
| 3053 |
#ifdef ANSI_FUNC |
| 3054 |
void |
| 3055 |
XPAVersionWarn (char *serv, char *nsv) |
| 3056 |
#else |
| 3057 |
void XPAVersionWarn(serv, nsv) |
| 3058 |
char *serv; |
| 3059 |
char *nsv; |
| 3060 |
#endif |
| 3061 |
{ |
| 3062 |
|
| 3063 |
if( vercheck <=0 ) |
| 3064 |
return; |
| 3065 |
|
| 3066 |
|
| 3067 |
fprintf(stderr, |
| 3068 |
"XPA$WARNING: version mismatch detected between XPA-enabled server (%s)\n", serv?serv:"unknown"); |
| 3069 |
fprintf(stderr, "and xpans (%s).", nsv?nsv:"unknown"); |
| 3070 |
fprintf(stderr, " You probably will get bad results.\n"); |
| 3071 |
fprintf(stderr, "Please consider updating XPA to match the XPA-enabled server you are running.\n"); |
| 3072 |
|
| 3073 |
|
| 3074 |
vercheck--; |
| 3075 |
} |
| 3076 |
|
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
|
| 3082 |
|
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
|
| 3093 |
|
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
#ifdef ANSI_FUNC |
| 3099 |
int |
| 3100 |
XPAMethod (char *method) |
| 3101 |
#else |
| 3102 |
int XPAMethod(method) |
| 3103 |
char *method; |
| 3104 |
#endif |
| 3105 |
{ |
| 3106 |
char *s1; |
| 3107 |
|
| 3108 |
|
| 3109 |
if( method == NULL ){ |
| 3110 |
if( mtype == 0 ){ |
| 3111 |
s1 = (char *)getenv("XPA_METHOD"); |
| 3112 |
if( s1 == NULL ) |
| 3113 |
mtype = XPA_INET; |
| 3114 |
else if( !strcasecmp(s1, "inet") ) |
| 3115 |
mtype = XPA_INET; |
| 3116 |
else if( !strcasecmp(s1, "localhost") ){ |
| 3117 |
mtype = XPA_INET; |
| 3118 |
use_localhost = 1; |
| 3119 |
} |
| 3120 |
else if( !strcasecmp(s1, "unix") ){ |
| 3121 |
#if HAVE_SYS_UN_H |
| 3122 |
mtype = XPA_UNIX; |
| 3123 |
#else |
| 3124 |
mtype = XPA_INET; |
| 3125 |
use_localhost = 1; |
| 3126 |
#endif |
| 3127 |
} |
| 3128 |
else if( !strcasecmp(s1, "local") ){ |
| 3129 |
#if HAVE_SYS_UN_H |
| 3130 |
mtype = XPA_UNIX; |
| 3131 |
#else |
| 3132 |
mtype = XPA_INET; |
| 3133 |
use_localhost = 1; |
| 3134 |
|
| 3135 |
#endif |
| 3136 |
} |
| 3137 |
else |
| 3138 |
mtype = XPA_INET; |
| 3139 |
} |
| 3140 |
return(mtype); |
| 3141 |
} |
| 3142 |
|
| 3143 |
else{ |
| 3144 |
|
| 3145 |
if( strchr(method, ':') != NULL ) |
| 3146 |
return(XPA_INET); |
| 3147 |
else |
| 3148 |
#if HAVE_SYS_UN_H |
| 3149 |
return(XPA_UNIX); |
| 3150 |
#else |
| 3151 |
return(XPA_INET); |
| 3152 |
#endif |
| 3153 |
} |
| 3154 |
} |
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
#ifdef ANSI_FUNC |
| 3169 |
char * |
| 3170 |
XPANSMethod (char *host, int flag) |
| 3171 |
#else |
| 3172 |
char *XPANSMethod(host, flag) |
| 3173 |
char *host; |
| 3174 |
int flag; |
| 3175 |
#endif |
| 3176 |
{ |
| 3177 |
char *s, *t; |
| 3178 |
char tbuf[SZ_LINE]; |
| 3179 |
int i; |
| 3180 |
int ip; |
| 3181 |
int port; |
| 3182 |
unsigned int bip; |
| 3183 |
unsigned short bport; |
| 3184 |
|
| 3185 |
switch( XPAMethod(host) ){ |
| 3186 |
case XPA_INET: |
| 3187 |
if( (host != NULL) && (*host != '\0') ) |
| 3188 |
strncpy(nsmethod, host, SZ_LINE-1); |
| 3189 |
else if( (s=(char *)getenv("XPA_NSINET")) != NULL ) |
| 3190 |
strncpy(nsmethod, s, SZ_LINE-1); |
| 3191 |
else |
| 3192 |
strncpy(nsmethod, XPA_NSINET, SZ_LINE-1); |
| 3193 |
|
| 3194 |
nsmethod[SZ_LINE-1] = '\0'; |
| 3195 |
|
| 3196 |
|
| 3197 |
if( flag ){ |
| 3198 |
if( (s=strrchr(nsmethod, ':')) != NULL ){ |
| 3199 |
|
| 3200 |
t = s+1; |
| 3201 |
|
| 3202 |
XPAParseIpPort(nsmethod, &bip, &bport); |
| 3203 |
newdtable(","); |
| 3204 |
for(ip=0, i=0; i<=flag; i++){ |
| 3205 |
if( !word(t, tbuf, &ip) ){ |
| 3206 |
*tbuf = '\0'; |
| 3207 |
break; |
| 3208 |
} |
| 3209 |
} |
| 3210 |
freedtable(); |
| 3211 |
if( *tbuf ) |
| 3212 |
port = atoi(tbuf); |
| 3213 |
else |
| 3214 |
port = bport + flag; |
| 3215 |
snprintf(t, SZ_LINE, "%d", port); |
| 3216 |
} |
| 3217 |
} |
| 3218 |
break; |
| 3219 |
#if HAVE_SYS_UN_H |
| 3220 |
case XPA_UNIX: |
| 3221 |
if( host != NULL ) |
| 3222 |
strncpy(nsmethod, host, SZ_LINE-1); |
| 3223 |
else if( (s=(char *)getenv("XPA_NSUNIX")) != NULL ) |
| 3224 |
strncpy(nsmethod, s, SZ_LINE-1); |
| 3225 |
else |
| 3226 |
snprintf(nsmethod, SZ_LINE, "%s/%s", tmpdir, XPA_NSUNIX); |
| 3227 |
|
| 3228 |
nsmethod[SZ_LINE-1] = '\0'; |
| 3229 |
|
| 3230 |
|
| 3231 |
if( flag ){ |
| 3232 |
|
| 3233 |
s = strrchr(nsmethod, '.'); |
| 3234 |
t = strrchr(nsmethod, '/'); |
| 3235 |
if( (s != NULL) && (s > t) ) |
| 3236 |
*s = '\0'; |
| 3237 |
snprintf(tbuf, SZ_LINE, ".xpa-%d", flag); |
| 3238 |
strcat(nsmethod, tbuf); |
| 3239 |
} |
| 3240 |
break; |
| 3241 |
#endif |
| 3242 |
default: |
| 3243 |
if( (s=(char *)getenv("XPA_NSINET")) != NULL ) |
| 3244 |
strncpy(nsmethod, s, SZ_LINE-1); |
| 3245 |
else |
| 3246 |
strncpy(nsmethod, XPA_NSINET, SZ_LINE-1); |
| 3247 |
|
| 3248 |
nsmethod[SZ_LINE-1] = '\0'; |
| 3249 |
break; |
| 3250 |
} |
| 3251 |
|
| 3252 |
|
| 3253 |
return(nsmethod); |
| 3254 |
} |
| 3255 |
|
| 3256 |
|
| 3257 |
|
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
|
| 3263 |
|
| 3264 |
|
| 3265 |
|
| 3266 |
|
| 3267 |
#ifdef ANSI_FUNC |
| 3268 |
void |
| 3269 |
XPAParseName (char *xpaname, char *xclass, char *name, int len) |
| 3270 |
#else |
| 3271 |
void XPAParseName(xpaname, xclass, name, len) |
| 3272 |
char *xpaname; |
| 3273 |
char *xclass; |
| 3274 |
char *name; |
| 3275 |
int len; |
| 3276 |
#endif |
| 3277 |
{ |
| 3278 |
char *s; |
| 3279 |
char *t; |
| 3280 |
char *cptr=NULL; |
| 3281 |
char *nptr=NULL; |
| 3282 |
|
| 3283 |
|
| 3284 |
if( (xpaname == NULL) || (*xpaname == '\0') ){ |
| 3285 |
strncpy(xclass, "*", len-1); |
| 3286 |
strncpy(name, "*", len-1); |
| 3287 |
return; |
| 3288 |
} |
| 3289 |
|
| 3290 |
|
| 3291 |
s = xstrdup(xpaname); |
| 3292 |
if( (t=(char *)strchr(s, ':')) != NULL ){ |
| 3293 |
cptr = s; |
| 3294 |
*t = '\0'; |
| 3295 |
nptr = t+1; |
| 3296 |
} |
| 3297 |
else{ |
| 3298 |
nptr = s; |
| 3299 |
cptr = "*"; |
| 3300 |
} |
| 3301 |
if( *cptr == '\0' ) |
| 3302 |
cptr = "*"; |
| 3303 |
if( *nptr == '\0' ) |
| 3304 |
nptr = "*"; |
| 3305 |
|
| 3306 |
strncpy(xclass, cptr, len-1); |
| 3307 |
strncpy(name, nptr, len-1); |
| 3308 |
xfree(s); |
| 3309 |
} |
| 3310 |
|
| 3311 |
|
| 3312 |
|
| 3313 |
|
| 3314 |
|
| 3315 |
|
| 3316 |
|
| 3317 |
|
| 3318 |
|
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
#ifdef ANSI_FUNC |
| 3323 |
int |
| 3324 |
XPAParseIpPort (char *host, unsigned int *ip, unsigned short *port) |
| 3325 |
#else |
| 3326 |
int XPAParseIpPort(host, ip, port) |
| 3327 |
char *host; |
| 3328 |
unsigned int *ip; |
| 3329 |
unsigned short *port; |
| 3330 |
#endif |
| 3331 |
{ |
| 3332 |
char *s1, *s2, *s3, *p1, *p2; |
| 3333 |
int got; |
| 3334 |
|
| 3335 |
|
| 3336 |
if( (host == NULL) || (*host == '\0') ) |
| 3337 |
return(0); |
| 3338 |
|
| 3339 |
|
| 3340 |
s1 = xstrdup(host); |
| 3341 |
|
| 3342 |
if( (p1 = (char *)strchr(s1, ',')) != NULL ){ |
| 3343 |
*p1 = '\0'; |
| 3344 |
} |
| 3345 |
|
| 3346 |
|
| 3347 |
if( (p1 = (char *)strchr(s1, ':')) == NULL ){ |
| 3348 |
|
| 3349 |
p1 = s1; |
| 3350 |
s2 = NULL; |
| 3351 |
} else { |
| 3352 |
|
| 3353 |
*p1 = '\0'; |
| 3354 |
p1++; |
| 3355 |
s2 = s1; |
| 3356 |
} |
| 3357 |
|
| 3358 |
|
| 3359 |
p2 = NULL; |
| 3360 |
if( p1 && !strcmp(p1, "$port") ) |
| 3361 |
*port = XPA_NSPORT; |
| 3362 |
|
| 3363 |
else |
| 3364 |
*port = (unsigned short)strtol(p1, &p2, 0); |
| 3365 |
|
| 3366 |
if( *port <=0 || (p1 == p2) || (p2 && (*p2 != '\0')) ){ |
| 3367 |
*ip = 0; |
| 3368 |
*port = 0; |
| 3369 |
got = 0; |
| 3370 |
goto done; |
| 3371 |
} |
| 3372 |
|
| 3373 |
|
| 3374 |
if( s2 && *s2 ){ |
| 3375 |
|
| 3376 |
*ip = strtoul16(s2, &s3); |
| 3377 |
if( *s3 == '\0' ){ |
| 3378 |
got = 1; |
| 3379 |
goto done; |
| 3380 |
} |
| 3381 |
} |
| 3382 |
|
| 3383 |
if( (*ip = gethostip(s2)) == 0 ){ |
| 3384 |
*port = 0; |
| 3385 |
got = 0; |
| 3386 |
} |
| 3387 |
else{ |
| 3388 |
got = 1; |
| 3389 |
} |
| 3390 |
|
| 3391 |
done: |
| 3392 |
xfree(s1); |
| 3393 |
return(got); |
| 3394 |
} |
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
|
| 3400 |
|
| 3401 |
|
| 3402 |
|
| 3403 |
|
| 3404 |
|
| 3405 |
|
| 3406 |
|
| 3407 |
#ifdef ANSI_FUNC |
| 3408 |
int |
| 3409 |
XPAParseUnixSocket (char *host) |
| 3410 |
#else |
| 3411 |
int XPAParseUnixSocket(host) |
| 3412 |
char *host; |
| 3413 |
#endif |
| 3414 |
{ |
| 3415 |
struct stat buf; |
| 3416 |
|
| 3417 |
|
| 3418 |
if( !strncmp(host, tmpdir, strlen(tmpdir)) && |
| 3419 |
!stat(host, &buf) ){ |
| 3420 |
return(1); |
| 3421 |
} |
| 3422 |
else{ |
| 3423 |
return(0); |
| 3424 |
} |
| 3425 |
} |
| 3426 |
|
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
|
| 3431 |
|
| 3432 |
|
| 3433 |
|
| 3434 |
|
| 3435 |
|
| 3436 |
|
| 3437 |
|
| 3438 |
#ifdef ANSI_FUNC |
| 3439 |
int |
| 3440 |
XPAOK (XPA xpa) |
| 3441 |
#else |
| 3442 |
int XPAOK(xpa) |
| 3443 |
XPA xpa; |
| 3444 |
#endif |
| 3445 |
{ |
| 3446 |
int len; |
| 3447 |
int status=0; |
| 3448 |
char tbuf[SZ_LINE]; |
| 3449 |
|
| 3450 |
|
| 3451 |
if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) ) |
| 3452 |
return(-1); |
| 3453 |
|
| 3454 |
|
| 3455 |
if( !(xpa_status(xpa) & XPA_STATUS_ACTIVE) || (xpa_ack(xpa) == 1) ){ |
| 3456 |
snprintf(tbuf, SZ_LINE, "%s XPA$OK (%s:%s %s)\n", |
| 3457 |
xpa_id(xpa), xpa_class(xpa), xpa_name(xpa), xpa_method(xpa)); |
| 3458 |
len = strlen(tbuf); |
| 3459 |
if( XPAPuts(xpa, xpa_cmdfd(xpa), tbuf, stimeout) != len ){ |
| 3460 |
status = -1; |
| 3461 |
} |
| 3462 |
} |
| 3463 |
|
| 3464 |
|
| 3465 |
xpa->comm->message = 1; |
| 3466 |
|
| 3467 |
|
| 3468 |
return(status); |
| 3469 |
} |
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
|
| 3474 |
|
| 3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
|
| 3479 |
|
| 3480 |
|
| 3481 |
|
| 3482 |
#ifdef ANSI_FUNC |
| 3483 |
char * |
| 3484 |
XPATimestamp(void) |
| 3485 |
#else |
| 3486 |
char *XPATimestamp() |
| 3487 |
#endif |
| 3488 |
{ |
| 3489 |
time_t t; |
| 3490 |
struct tm *lt; |
| 3491 |
|
| 3492 |
*ctimebuf = '\0'; |
| 3493 |
if( etimestamp ){ |
| 3494 |
if( (t = time(NULL)) != (time_t)-1 ){ |
| 3495 |
if( (lt = localtime(&t)) != NULL ){ |
| 3496 |
snprintf(ctimebuf, SZ_LINE, " %02d/%02d/%d:%d:%d:%d", |
| 3497 |
lt->tm_mday, lt->tm_mon+1, lt->tm_year+1900, |
| 3498 |
lt->tm_hour, lt->tm_min, lt->tm_sec); |
| 3499 |
} |
| 3500 |
} |
| 3501 |
} |
| 3502 |
return ctimebuf; |
| 3503 |
} |
| 3504 |
|
| 3505 |
|
| 3506 |
|
| 3507 |
|
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
|
| 3512 |
|
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
#ifdef ANSI_FUNC |
| 3517 |
int |
| 3518 |
XPAError (XPA xpa, char *s) |
| 3519 |
#else |
| 3520 |
int XPAError(xpa, s) |
| 3521 |
XPA xpa; |
| 3522 |
char *s; |
| 3523 |
#endif |
| 3524 |
{ |
| 3525 |
int status=0; |
| 3526 |
int ip=0; |
| 3527 |
char tbuf[SZ_LINE]; |
| 3528 |
char *t; |
| 3529 |
char *u; |
| 3530 |
|
| 3531 |
|
| 3532 |
if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) ) |
| 3533 |
return(-1); |
| 3534 |
|
| 3535 |
|
| 3536 |
if( !(xpa_status(xpa) & XPA_STATUS_ACTIVE) || (xpa_ack(xpa) == 1) ){ |
| 3537 |
t = xstrdup(s); |
| 3538 |
|
| 3539 |
nowhite(t, t); |
| 3540 |
if( !strncmp(t, "XPA$", 4) ) |
| 3541 |
word(t, tbuf, &ip); |
| 3542 |
u = (char *)xcalloc(strlen(t)+SZ_LINE, sizeof(char)); |
| 3543 |
|
| 3544 |
snprintf(u, SZ_LINE, "%s XPA$ERROR %s (%s:%s %s%s)\n", |
| 3545 |
xpa_id(xpa), |
| 3546 |
&t[ip], xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), |
| 3547 |
XPATimestamp()); |
| 3548 |
if( XPAPuts(xpa, xpa_cmdfd(xpa), u, stimeout) != (int)strlen(u) ){ |
| 3549 |
status = -1; |
| 3550 |
} |
| 3551 |
if( t ) |
| 3552 |
xfree(t); |
| 3553 |
if( u ) |
| 3554 |
xfree(u); |
| 3555 |
} |
| 3556 |
|
| 3557 |
|
| 3558 |
xpa->comm->message = 1; |
| 3559 |
|
| 3560 |
|
| 3561 |
return(status); |
| 3562 |
} |
| 3563 |
|
| 3564 |
|
| 3565 |
|
| 3566 |
|
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
#ifdef ANSI_FUNC |
| 3576 |
int |
| 3577 |
XPAMessage (XPA xpa, char *s) |
| 3578 |
#else |
| 3579 |
int XPAMessage(xpa, s) |
| 3580 |
XPA xpa; |
| 3581 |
char *s; |
| 3582 |
#endif |
| 3583 |
{ |
| 3584 |
int status=0; |
| 3585 |
int ip=0; |
| 3586 |
char tbuf[SZ_LINE]; |
| 3587 |
char *t; |
| 3588 |
char *u; |
| 3589 |
|
| 3590 |
|
| 3591 |
if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) ) |
| 3592 |
return(-1); |
| 3593 |
|
| 3594 |
|
| 3595 |
if( !(xpa_status(xpa) & XPA_STATUS_ACTIVE) || (xpa_ack(xpa) == 1) ){ |
| 3596 |
t = xstrdup(s); |
| 3597 |
|
| 3598 |
nowhite(t, t); |
| 3599 |
if( !strncmp(t, "XPA$", 4) ) |
| 3600 |
word(t, tbuf, &ip); |
| 3601 |
u = (char *)xcalloc(strlen(t)+SZ_LINE, sizeof(char)); |
| 3602 |
|
| 3603 |
snprintf(u, SZ_LINE, "%s XPA$MESSAGE %s (%s:%s %s%s)\n", |
| 3604 |
xpa_id(xpa), |
| 3605 |
&t[ip], xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), |
| 3606 |
XPATimestamp()); |
| 3607 |
if( XPAPuts(xpa, xpa_cmdfd(xpa), u, stimeout) != (int)strlen(u) ){ |
| 3608 |
status = -1; |
| 3609 |
} |
| 3610 |
if( t ) |
| 3611 |
xfree(t); |
| 3612 |
if( u ) |
| 3613 |
xfree(u); |
| 3614 |
} |
| 3615 |
|
| 3616 |
|
| 3617 |
xpa->comm->message = 1; |
| 3618 |
|
| 3619 |
|
| 3620 |
return(status); |
| 3621 |
} |
| 3622 |
|
| 3623 |
|
| 3624 |
|
| 3625 |
|
| 3626 |
|
| 3627 |
|
| 3628 |
|
| 3629 |
|
| 3630 |
|
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
#ifdef ANSI_FUNC |
| 3635 |
char * |
| 3636 |
XPAArgvParamlist (int argc, char **argv, int start) |
| 3637 |
#else |
| 3638 |
char *XPAArgvParamlist(argc, argv, start) |
| 3639 |
int argc; |
| 3640 |
char **argv; |
| 3641 |
int start; |
| 3642 |
#endif |
| 3643 |
{ |
| 3644 |
int plen; |
| 3645 |
int i; |
| 3646 |
char *paramlist; |
| 3647 |
|
| 3648 |
|
| 3649 |
for(plen=0, i=start; i<argc; i++){ |
| 3650 |
plen += (strlen(argv[i])+1); |
| 3651 |
} |
| 3652 |
|
| 3653 |
|
| 3654 |
if( (paramlist = (char *)xcalloc(plen+1, sizeof(char))) == NULL ){ |
| 3655 |
return(NULL); |
| 3656 |
} |
| 3657 |
|
| 3658 |
|
| 3659 |
for(i=start; i<argc; i++){ |
| 3660 |
strcat(paramlist, argv[i]); |
| 3661 |
strcat(paramlist, " "); |
| 3662 |
} |
| 3663 |
|
| 3664 |
|
| 3665 |
nowhite(paramlist, paramlist); |
| 3666 |
|
| 3667 |
|
| 3668 |
return(paramlist); |
| 3669 |
} |
| 3670 |
|
| 3671 |
|
| 3672 |
|
| 3673 |
|
| 3674 |
|
| 3675 |
|
| 3676 |
|
| 3677 |
|
| 3678 |
|
| 3679 |
|
| 3680 |
|
| 3681 |
|
| 3682 |
#ifdef ANSI_FUNC |
| 3683 |
int |
| 3684 |
XPANSLookup (XPA xpa, char *tname, char *ttype, |
| 3685 |
char ***xclasses, char ***names, char ***methods, char ***infos) |
| 3686 |
#else |
| 3687 |
int XPANSLookup(xpa, tname, ttype, xclasses, names, methods, infos) |
| 3688 |
XPA xpa; |
| 3689 |
char *tname; |
| 3690 |
char *ttype; |
| 3691 |
char ***xclasses; |
| 3692 |
char ***names; |
| 3693 |
char ***methods; |
| 3694 |
char ***infos; |
| 3695 |
#endif |
| 3696 |
{ |
| 3697 |
unsigned short port; |
| 3698 |
unsigned int ip; |
| 3699 |
int own; |
| 3700 |
int lp=0; |
| 3701 |
int got=0; |
| 3702 |
int nentry=100; |
| 3703 |
char *xtype; |
| 3704 |
char xclass[SZ_LINE]; |
| 3705 |
char name[SZ_LINE]; |
| 3706 |
char method[SZ_LINE]; |
| 3707 |
char info[SZ_LINE]; |
| 3708 |
char user[SZ_LINE]; |
| 3709 |
char type[SZ_LINE]; |
| 3710 |
char tbuf[SZ_LINE]; |
| 3711 |
char lbuf[SZ_LINE]; |
| 3712 |
NS ns; |
| 3713 |
XPA txpa; |
| 3714 |
|
| 3715 |
|
| 3716 |
XPAInitEnv(); |
| 3717 |
|
| 3718 |
|
| 3719 |
if( (ttype == NULL) || (*ttype == '\0') ) |
| 3720 |
xtype = "*"; |
| 3721 |
|
| 3722 |
else if( *ttype == 'a' ) |
| 3723 |
xtype = "*"; |
| 3724 |
else |
| 3725 |
xtype = ttype; |
| 3726 |
|
| 3727 |
|
| 3728 |
if( !strcmp(tname, XPANS_NAME) ){ |
| 3729 |
*xclasses = (char **)xmalloc(sizeof(char *)); |
| 3730 |
*names = (char **)xmalloc(sizeof(char *)); |
| 3731 |
*methods = (char **)xmalloc(sizeof(char *)); |
| 3732 |
*infos = (char **)xmalloc(sizeof(char *)); |
| 3733 |
(*xclasses)[0] = xstrdup(XPANS_CLASS); |
| 3734 |
(*names)[0] = xstrdup(XPANS_NAME); |
| 3735 |
(*methods)[0] = xstrdup(XPANSMethod(NULL, 1)); |
| 3736 |
(*infos)[0] = xstrdup(XPA_DEF_CLIENT_INFO); |
| 3737 |
return(1); |
| 3738 |
} |
| 3739 |
|
| 3740 |
|
| 3741 |
if( XPAParseIpPort(tname, &ip, &port) ){ |
| 3742 |
*xclasses = (char **)xmalloc(sizeof(char *)); |
| 3743 |
*names = (char **)xmalloc(sizeof(char *)); |
| 3744 |
*methods = (char **)xmalloc(sizeof(char *)); |
| 3745 |
*infos = (char **)xmalloc(sizeof(char *)); |
| 3746 |
(*xclasses)[0] = xstrdup("?"); |
| 3747 |
(*names)[0] = xstrdup("?"); |
| 3748 |
(*methods)[0] = xstrdup(tname); |
| 3749 |
(*infos)[0] = xstrdup(XPA_DEF_CLIENT_INFO); |
| 3750 |
return(1); |
| 3751 |
} |
| 3752 |
|
| 3753 |
|
| 3754 |
if( XPAParseUnixSocket(tname) ){ |
| 3755 |
*xclasses = (char **)xmalloc(sizeof(char *)); |
| 3756 |
*names = (char **)xmalloc(sizeof(char *)); |
| 3757 |
*methods = (char **)xmalloc(sizeof(char *)); |
| 3758 |
*infos = (char **)xmalloc(sizeof(char *)); |
| 3759 |
(*xclasses)[0] = xstrdup("?"); |
| 3760 |
(*names)[0] = xstrdup("?"); |
| 3761 |
(*methods)[0] = xstrdup(tname); |
| 3762 |
(*infos)[0] = xstrdup(XPA_DEF_CLIENT_INFO); |
| 3763 |
return(1); |
| 3764 |
} |
| 3765 |
|
| 3766 |
|
| 3767 |
*xclasses = (char **)xmalloc(nentry * sizeof(char *)); |
| 3768 |
*names = (char **)xmalloc(nentry * sizeof(char *)); |
| 3769 |
*methods = (char **)xmalloc(nentry * sizeof(char *)); |
| 3770 |
*infos = (char **)xmalloc(nentry * sizeof(char *)); |
| 3771 |
|
| 3772 |
|
| 3773 |
if( (ns=XPANSOpen(xpa, NULL, 0)) != NULL ){ |
| 3774 |
while( word(tname, lbuf, &lp) ){ |
| 3775 |
XPAParseName(lbuf, xclass, name, SZ_LINE); |
| 3776 |
|
| 3777 |
snprintf(tbuf, SZ_LINE, "lookup %s:%s %s %s\n", |
| 3778 |
xclass, name, xtype, nsusers); |
| 3779 |
FPRINTF((stderr, "%sXPANSLookup: sending command %s", _sp, tbuf)); |
| 3780 |
XPAPuts(xpa, ns->fd, tbuf, stimeout); |
| 3781 |
|
| 3782 |
while( 1 ){ |
| 3783 |
if( XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) <=0 ){ |
| 3784 |
FPRINTF((stderr, "%sXPANSLookup: unexpected EOF from xpans\n", _sp)); |
| 3785 |
break; |
| 3786 |
} |
| 3787 |
FPRINTF((stderr, "%sXPANSLookup: receiving %s", _sp, tbuf)); |
| 3788 |
|
| 3789 |
if( !strncmp(tbuf, "XPA$", 4) ) |
| 3790 |
break; |
| 3791 |
|
| 3792 |
if( sscanf(tbuf, "%s %s %s %s %s %s\n", |
| 3793 |
xclass, name, type, method, user, info) != EOF ){ |
| 3794 |
|
| 3795 |
|
| 3796 |
for(own=0, txpa=xpahead; txpa!=NULL; txpa=txpa->next){ |
| 3797 |
if( !strcmp(txpa->xclass, xclass) && !strcmp(txpa->name, name) && |
| 3798 |
!strcmp(txpa->method, method) ){ |
| 3799 |
own = 1; |
| 3800 |
break; |
| 3801 |
} |
| 3802 |
} |
| 3803 |
|
| 3804 |
if( own ) |
| 3805 |
continue; |
| 3806 |
|
| 3807 |
if( got >= nentry ){ |
| 3808 |
nentry *= 2; |
| 3809 |
*xclasses = (char **)xrealloc(*xclasses, nentry * sizeof(char *)); |
| 3810 |
*names = (char **)xrealloc(*names, nentry * sizeof(char *)); |
| 3811 |
*methods = (char **)xrealloc(*methods, nentry * sizeof(char *)); |
| 3812 |
*infos = (char **)xrealloc(*infos, nentry * sizeof(char *)); |
| 3813 |
} |
| 3814 |
|
| 3815 |
(*xclasses)[got] = xstrdup(xclass); |
| 3816 |
(*names)[got] = xstrdup(name); |
| 3817 |
(*methods)[got] = xstrdup(method); |
| 3818 |
(*infos)[got] = xstrdup(info); |
| 3819 |
got++; |
| 3820 |
} |
| 3821 |
} |
| 3822 |
} |
| 3823 |
|
| 3824 |
if( xpa == NULL ){ |
| 3825 |
XPANSClose(NULL, ns); |
| 3826 |
} |
| 3827 |
} |
| 3828 |
|
| 3829 |
if( got > 0 ){ |
| 3830 |
*xclasses = (char **)xrealloc(*xclasses, got * sizeof(char *)); |
| 3831 |
*names = (char **)xrealloc(*names, got * sizeof(char *)); |
| 3832 |
*methods = (char **)xrealloc(*methods, got * sizeof(char *)); |
| 3833 |
*infos = (char **)xrealloc(*infos, got * sizeof(char *)); |
| 3834 |
} |
| 3835 |
else{ |
| 3836 |
xfree(*xclasses); |
| 3837 |
xfree(*names); |
| 3838 |
xfree(*methods); |
| 3839 |
xfree(*infos); |
| 3840 |
} |
| 3841 |
FPRINTF((stderr, "%sXPANSLookup: found %d entries\n", _sp, got)); |
| 3842 |
return(got); |
| 3843 |
} |
| 3844 |
|
| 3845 |
|
| 3846 |
|
| 3847 |
|
| 3848 |
|
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
|
| 3853 |
|
| 3854 |
|
| 3855 |
|
| 3856 |
#ifdef ANSI_FUNC |
| 3857 |
int |
| 3858 |
XPANSClose (XPA xpa, NS ns) |
| 3859 |
#else |
| 3860 |
int XPANSClose(xpa, ns) |
| 3861 |
XPA xpa; |
| 3862 |
NS ns; |
| 3863 |
#endif |
| 3864 |
{ |
| 3865 |
NS cur; |
| 3866 |
XPAComm comm, tcomm; |
| 3867 |
|
| 3868 |
if( !ns ) |
| 3869 |
return(-1); |
| 3870 |
|
| 3871 |
|
| 3872 |
if( xpa ){ |
| 3873 |
if( xpa->nshead ){ |
| 3874 |
if( xpa->nshead == ns ){ |
| 3875 |
xpa->nshead = ns->next; |
| 3876 |
} |
| 3877 |
else{ |
| 3878 |
for(cur=xpa->nshead; cur!=NULL; cur=cur->next){ |
| 3879 |
if( cur->next == ns ){ |
| 3880 |
cur->next = ns->next; |
| 3881 |
break; |
| 3882 |
} |
| 3883 |
} |
| 3884 |
} |
| 3885 |
} |
| 3886 |
|
| 3887 |
for(comm=xpa->commhead; comm!=NULL; ){ |
| 3888 |
tcomm = comm->next; |
| 3889 |
if( comm->ns == ns ){ |
| 3890 |
CommFree(xpa, comm, 0); |
| 3891 |
} |
| 3892 |
comm = tcomm; |
| 3893 |
} |
| 3894 |
} |
| 3895 |
|
| 3896 |
|
| 3897 |
if( ns->fd >=0 ){ |
| 3898 |
xclose(ns->fd); |
| 3899 |
} |
| 3900 |
|
| 3901 |
if( ns->method) xfree(ns->method); |
| 3902 |
if( ns->host ) xfree(ns->host); |
| 3903 |
if( ns->name ) xfree(ns->name); |
| 3904 |
if( ns ) xfree(ns); |
| 3905 |
return(0); |
| 3906 |
} |
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
|
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
|
| 3917 |
|
| 3918 |
|
| 3919 |
#ifdef ANSI_FUNC |
| 3920 |
int |
| 3921 |
XPANSKeepAlive (XPA xpa, int type) |
| 3922 |
#else |
| 3923 |
int XPANSKeepAlive(xpa, type) |
| 3924 |
XPA xpa; |
| 3925 |
int type; |
| 3926 |
#endif |
| 3927 |
{ |
| 3928 |
NS ns; |
| 3929 |
int got=0; |
| 3930 |
|
| 3931 |
|
| 3932 |
if( !xpa ) |
| 3933 |
return(-1); |
| 3934 |
|
| 3935 |
|
| 3936 |
if( !type ) |
| 3937 |
type=DEF_KA_TYPE; |
| 3938 |
|
| 3939 |
|
| 3940 |
for(ns=xpa->nshead; ns!=NULL; ns=ns->next){ |
| 3941 |
if( ((type&1) && (ns->nxpa>0)) || ((type&2) && (ns->nproxy>0)) ){ |
| 3942 |
FPRINTF((stderr, "%sXPANSKeepAlive: sending ka to %d\n", _sp, ns->fd)); |
| 3943 |
#if USE_KA_OOB |
| 3944 |
|
| 3945 |
got = send(ns->fd, "\n", 1, MSG_OOB); |
| 3946 |
#else |
| 3947 |
|
| 3948 |
got = send(ns->fd, "\n", 1, 0); |
| 3949 |
#endif |
| 3950 |
} |
| 3951 |
} |
| 3952 |
return(got); |
| 3953 |
} |
| 3954 |
|
| 3955 |
|
| 3956 |
|
| 3957 |
|
| 3958 |
|
| 3959 |
|
| 3960 |
|
| 3961 |
|
| 3962 |
|
| 3963 |
|
| 3964 |
|
| 3965 |
|
| 3966 |
|
| 3967 |
|
| 3968 |
|
| 3969 |
|
| 3970 |
#ifdef ANSI_FUNC |
| 3971 |
XPA |
| 3972 |
XPANew (char *xclass, char *name, char *help, |
| 3973 |
SendCb send_callback, void *send_data, char *send_mode, |
| 3974 |
ReceiveCb rec_callback, void *rec_data, char *rec_mode) |
| 3975 |
#else |
| 3976 |
XPA XPANew(xclass, name, help, |
| 3977 |
send_callback, send_data, send_mode, |
| 3978 |
rec_callback, rec_data, rec_mode) |
| 3979 |
char *xclass; |
| 3980 |
char *name; |
| 3981 |
char *help; |
| 3982 |
SendCb send_callback; |
| 3983 |
void *send_data; |
| 3984 |
char *send_mode; |
| 3985 |
ReceiveCb rec_callback; |
| 3986 |
void *rec_data; |
| 3987 |
char *rec_mode; |
| 3988 |
#endif |
| 3989 |
{ |
| 3990 |
unsigned short port; |
| 3991 |
unsigned int ip; |
| 3992 |
int got; |
| 3993 |
int oum; |
| 3994 |
int keep_alive=1; |
| 3995 |
int reuse_addr=1; |
| 3996 |
char tbuf[SZ_LINE]; |
| 3997 |
char tbuf2[SZ_LINE]; |
| 3998 |
char tfile[SZ_LINE]; |
| 3999 |
char *tfptr; |
| 4000 |
XPA xpa; |
| 4001 |
socklen_t slen = sizeof(struct sockaddr_in); |
| 4002 |
struct sockaddr_in sock_in; |
| 4003 |
#if HAVE_SYS_UN_H |
| 4004 |
struct sockaddr_un sock_un; |
| 4005 |
#endif |
| 4006 |
|
| 4007 |
|
| 4008 |
XPAInitEnv(); |
| 4009 |
|
| 4010 |
XPAInitReserved(); |
| 4011 |
|
| 4012 |
|
| 4013 |
if( (name == NULL) || (*name == '\0') || strchr(name, ':') ) |
| 4014 |
return(NULL); |
| 4015 |
|
| 4016 |
|
| 4017 |
if( xclass && *xclass && (strlen(xclass) > XPA_NAMELEN) ){ |
| 4018 |
if( verbosity ) |
| 4019 |
fprintf(stderr, "XPA$ERROR: class designator too long\n"); |
| 4020 |
return(NULL); |
| 4021 |
} |
| 4022 |
if( strlen(name) > XPA_NAMELEN ){ |
| 4023 |
if( verbosity ) |
| 4024 |
fprintf(stderr, "XPA$ERROR: name designator too long\n"); |
| 4025 |
return(NULL); |
| 4026 |
} |
| 4027 |
|
| 4028 |
|
| 4029 |
if( (send_callback == NULL) && (rec_callback == NULL ) ){ |
| 4030 |
if( verbosity ) |
| 4031 |
fprintf(stderr, "XPA$ERROR: requires send and/or receive callback\n"); |
| 4032 |
return(NULL); |
| 4033 |
} |
| 4034 |
|
| 4035 |
|
| 4036 |
if( (xpa = (XPA)xcalloc(1, sizeof(XPARec))) == NULL ) |
| 4037 |
return(NULL); |
| 4038 |
|
| 4039 |
|
| 4040 |
xpa->version = xstrdup(XPA_VERSION); |
| 4041 |
xpa->type = (char *)xcalloc(10, sizeof(char)); |
| 4042 |
if( xclass && *xclass ) |
| 4043 |
xpa->xclass = xstrdup(xclass); |
| 4044 |
else |
| 4045 |
xpa->xclass = xstrdup("*"); |
| 4046 |
xpa->name = xstrdup(name); |
| 4047 |
xpa->help = xstrdup(help); |
| 4048 |
|
| 4049 |
xpa->sendian = XPAEndian() ? xstrdup("big") : xstrdup("little"); |
| 4050 |
|
| 4051 |
|
| 4052 |
if( send_callback != NULL ){ |
| 4053 |
xpa->send_callback = send_callback; |
| 4054 |
xpa->send_data = send_data; |
| 4055 |
strcat(xpa->type, "g"); |
| 4056 |
|
| 4057 |
xpa->send_mode = XPA_DEF_MODE_SEND; |
| 4058 |
XPAMode(send_mode, &(xpa->send_mode), "freebuf", XPA_MODE_FREEBUF,1); |
| 4059 |
XPAMode(send_mode, &(xpa->send_mode), "acl", XPA_MODE_ACL, 1); |
| 4060 |
} |
| 4061 |
|
| 4062 |
|
| 4063 |
if( rec_callback != NULL ){ |
| 4064 |
xpa->receive_callback = rec_callback; |
| 4065 |
xpa->receive_data = rec_data; |
| 4066 |
strcat(xpa->type, "s"); |
| 4067 |
|
| 4068 |
xpa->receive_mode = XPA_DEF_MODE_REC; |
| 4069 |
XPAMode(rec_mode, &(xpa->receive_mode), "buf", XPA_MODE_BUF, 1); |
| 4070 |
XPAMode(rec_mode, &(xpa->receive_mode), "fillbuf", XPA_MODE_FILLBUF, 1); |
| 4071 |
XPAMode(rec_mode, &(xpa->receive_mode), "freebuf", XPA_MODE_FREEBUF, 1); |
| 4072 |
XPAMode(rec_mode, &(xpa->receive_mode), "acl", XPA_MODE_ACL, 1); |
| 4073 |
} |
| 4074 |
|
| 4075 |
|
| 4076 |
switch(mtype){ |
| 4077 |
case XPA_INET: |
| 4078 |
|
| 4079 |
if( (xpa->fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ) |
| 4080 |
goto error; |
| 4081 |
setsockopt(xpa->fd, SOL_SOCKET, SO_KEEPALIVE, |
| 4082 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 4083 |
setsockopt(xpa->fd, SOL_SOCKET, SO_REUSEADDR, |
| 4084 |
(char *)&reuse_addr, sizeof(reuse_addr)); |
| 4085 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 4086 |
sock_in.sin_family = AF_INET; |
| 4087 |
|
| 4088 |
if( use_localhost ) |
| 4089 |
sock_in.sin_addr.s_addr = htonl(gethostip("$localhost")); |
| 4090 |
|
| 4091 |
else |
| 4092 |
sock_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 4093 |
|
| 4094 |
if( !strcmp(xpa->name, XPANS_NAME) ){ |
| 4095 |
XPAParseIpPort(XPANSMethod(NULL, 1), &ip, &port); |
| 4096 |
sock_in.sin_port = htons(port); |
| 4097 |
} |
| 4098 |
else{ |
| 4099 |
sock_in.sin_port = htons(XPAPort(xpa)); |
| 4100 |
} |
| 4101 |
|
| 4102 |
if( xbind(xpa->fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ) |
| 4103 |
goto error; |
| 4104 |
|
| 4105 |
if( getsockname(xpa->fd, (struct sockaddr *)&sock_in, &slen) < 0 ) |
| 4106 |
goto error; |
| 4107 |
else{ |
| 4108 |
|
| 4109 |
gethost(tbuf2, SZ_LINE); |
| 4110 |
snprintf(tbuf, SZ_LINE, "%x:%d", |
| 4111 |
gethostip(tbuf2), ntohs(sock_in.sin_port)); |
| 4112 |
xpa->method = xstrdup(tbuf); |
| 4113 |
} |
| 4114 |
break; |
| 4115 |
#if HAVE_SYS_UN_H |
| 4116 |
case XPA_UNIX: |
| 4117 |
|
| 4118 |
if( (xpa->fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) |
| 4119 |
goto error; |
| 4120 |
setsockopt(xpa->fd, SOL_SOCKET, SO_KEEPALIVE, |
| 4121 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 4122 |
setsockopt(xpa->fd, SOL_SOCKET, SO_REUSEADDR, |
| 4123 |
(char *)&reuse_addr, sizeof(reuse_addr)); |
| 4124 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 4125 |
sock_un.sun_family = AF_UNIX; |
| 4126 |
|
| 4127 |
if( !strcmp(xpa->name, XPANS_NAME) ){ |
| 4128 |
strcpy(tbuf, XPANSMethod(NULL, 1)); |
| 4129 |
} |
| 4130 |
else{ |
| 4131 |
|
| 4132 |
snprintf(tfile, SZ_LINE, "%s_%s.%d", |
| 4133 |
xpa->xclass, xpa->name, (int)getpid()); |
| 4134 |
|
| 4135 |
for(tfptr = tfile; *tfptr != '\0'; tfptr++){ |
| 4136 |
if( *tfptr == '/' ) |
| 4137 |
*tfptr = '_'; |
| 4138 |
} |
| 4139 |
|
| 4140 |
snprintf(tbuf, SZ_LINE, "%s/%s", tmpdir, tfile); |
| 4141 |
} |
| 4142 |
|
| 4143 |
unlink (tbuf); |
| 4144 |
strcpy(sock_un.sun_path, tbuf); |
| 4145 |
|
| 4146 |
oum = umask(0); |
| 4147 |
|
| 4148 |
got = xbind(xpa->fd, (struct sockaddr *)&sock_un, sizeof(sock_un)); |
| 4149 |
|
| 4150 |
umask(oum); |
| 4151 |
|
| 4152 |
if( got < 0 ) |
| 4153 |
goto error; |
| 4154 |
|
| 4155 |
xpa->method = xstrdup(tbuf); |
| 4156 |
break; |
| 4157 |
#endif |
| 4158 |
default: |
| 4159 |
goto error; |
| 4160 |
} |
| 4161 |
|
| 4162 |
|
| 4163 |
if( listen(xpa->fd, XPA_MAXLISTEN) < 0 ) |
| 4164 |
goto error; |
| 4165 |
|
| 4166 |
|
| 4167 |
xfcntl(xpa->fd, F_SETFD, FD_CLOEXEC); |
| 4168 |
|
| 4169 |
|
| 4170 |
XPAListAdd(&xpahead, xpa); |
| 4171 |
|
| 4172 |
|
| 4173 |
if( nsregister ) |
| 4174 |
XPANSAdd(xpa, NULL, NULL); |
| 4175 |
|
| 4176 |
|
| 4177 |
XPAActive(xpa, NULL, 1); |
| 4178 |
|
| 4179 |
#if NO_AUTOMATIC_HAVE_ATEXIT |
| 4180 |
|
| 4181 |
if( !atexitinit ){ |
| 4182 |
atexit(_XPAAtExit); |
| 4183 |
atexitinit = getpid(); |
| 4184 |
} |
| 4185 |
#endif |
| 4186 |
|
| 4187 |
|
| 4188 |
return(xpa); |
| 4189 |
|
| 4190 |
error: |
| 4191 |
if( verbosity ){ |
| 4192 |
perror("XPANew"); |
| 4193 |
} |
| 4194 |
_XPAFree(xpa); |
| 4195 |
return(NULL); |
| 4196 |
} |
| 4197 |
|
| 4198 |
|
| 4199 |
|
| 4200 |
|
| 4201 |
|
| 4202 |
|
| 4203 |
|
| 4204 |
|
| 4205 |
|
| 4206 |
|
| 4207 |
|
| 4208 |
|
| 4209 |
#ifdef ANSI_FUNC |
| 4210 |
int |
| 4211 |
XPAFree (XPA xpa) |
| 4212 |
#else |
| 4213 |
int XPAFree(xpa) |
| 4214 |
XPA xpa; |
| 4215 |
#endif |
| 4216 |
{ |
| 4217 |
|
| 4218 |
if( _XPAValid(xpahead, xpa, XPA_ACLS) ){ |
| 4219 |
if( xpa_status(xpa) & XPA_STATUS_ACTIVE ){ |
| 4220 |
xpa->status |= XPA_STATUS_FREE; |
| 4221 |
FPRINTF((stderr, "%sXPAFree: marking xpa struct for later free'ing\n", |
| 4222 |
_sp)); |
| 4223 |
return(0); |
| 4224 |
} |
| 4225 |
else{ |
| 4226 |
|
| 4227 |
FPRINTF((stderr, "%sXPAFree: freeing xpa struct\n", _sp)); |
| 4228 |
return(_XPAFree(xpa)); |
| 4229 |
} |
| 4230 |
} |
| 4231 |
else{ |
| 4232 |
return(-1); |
| 4233 |
} |
| 4234 |
} |
| 4235 |
|
| 4236 |
|
| 4237 |
|
| 4238 |
|
| 4239 |
|
| 4240 |
|
| 4241 |
|
| 4242 |
|
| 4243 |
|
| 4244 |
|
| 4245 |
|
| 4246 |
|
| 4247 |
|
| 4248 |
|
| 4249 |
#ifdef ANSI_FUNC |
| 4250 |
XPA |
| 4251 |
XPAInfoNew (char *xclass, char *name, |
| 4252 |
InfoCb info_callback, void *info_data, char *info_mode) |
| 4253 |
#else |
| 4254 |
XPA XPAInfoNew(xclass, name, info_callback, info_data, info_mode) |
| 4255 |
char *xclass; |
| 4256 |
char *name; |
| 4257 |
InfoCb info_callback; |
| 4258 |
void *info_data; |
| 4259 |
char *info_mode; |
| 4260 |
#endif |
| 4261 |
{ |
| 4262 |
int got; |
| 4263 |
int oum; |
| 4264 |
int keep_alive=1; |
| 4265 |
int reuse_addr=1; |
| 4266 |
char tbuf[SZ_LINE]; |
| 4267 |
char tbuf2[SZ_LINE]; |
| 4268 |
char tfile[SZ_LINE]; |
| 4269 |
char *tfptr; |
| 4270 |
XPA xpa; |
| 4271 |
socklen_t slen = sizeof(struct sockaddr_in); |
| 4272 |
struct sockaddr_in sock_in; |
| 4273 |
#if HAVE_SYS_UN_H |
| 4274 |
struct sockaddr_un sock_un; |
| 4275 |
#endif |
| 4276 |
|
| 4277 |
|
| 4278 |
XPAInitEnv(); |
| 4279 |
|
| 4280 |
XPAInitReserved(); |
| 4281 |
|
| 4282 |
|
| 4283 |
if( (name == NULL) || (*name == '\0') || strchr(name, ':') ) |
| 4284 |
return(NULL); |
| 4285 |
|
| 4286 |
|
| 4287 |
if( info_callback == NULL ){ |
| 4288 |
if( verbosity ){ |
| 4289 |
fprintf(stderr, "XPA$ERROR: requires info callback\n"); |
| 4290 |
} |
| 4291 |
return(NULL); |
| 4292 |
} |
| 4293 |
|
| 4294 |
|
| 4295 |
if( (xpa = (XPA)xcalloc(1, sizeof(XPARec))) == NULL ) |
| 4296 |
return(NULL); |
| 4297 |
|
| 4298 |
xpa->version = xstrdup(XPA_VERSION); |
| 4299 |
xpa->type = (char *)xcalloc(10, sizeof(char)); |
| 4300 |
|
| 4301 |
if( xclass != NULL ) |
| 4302 |
xpa->xclass = xstrdup(xclass); |
| 4303 |
else |
| 4304 |
xpa->xclass = xstrdup("*"); |
| 4305 |
xpa->name = xstrdup(name); |
| 4306 |
|
| 4307 |
|
| 4308 |
xpa->info_callback = info_callback; |
| 4309 |
xpa->info_data = info_data; |
| 4310 |
strcat(xpa->type, "i"); |
| 4311 |
|
| 4312 |
xpa->info_mode = XPA_DEF_MODE_INFO; |
| 4313 |
XPAMode(info_mode, &(xpa->info_mode), "acl", XPA_MODE_ACL, 1); |
| 4314 |
|
| 4315 |
|
| 4316 |
switch(mtype){ |
| 4317 |
case XPA_INET: |
| 4318 |
|
| 4319 |
if( (xpa->fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ) |
| 4320 |
goto error; |
| 4321 |
setsockopt(xpa->fd, SOL_SOCKET, SO_KEEPALIVE, |
| 4322 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 4323 |
setsockopt(xpa->fd, SOL_SOCKET, SO_REUSEADDR, |
| 4324 |
(char *)&reuse_addr, sizeof(reuse_addr)); |
| 4325 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 4326 |
sock_in.sin_family = AF_INET; |
| 4327 |
|
| 4328 |
if( use_localhost ) |
| 4329 |
sock_in.sin_addr.s_addr = htonl(gethostip("$localhost")); |
| 4330 |
|
| 4331 |
else |
| 4332 |
sock_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 4333 |
sock_in.sin_port = htons(XPAPort(xpa)); |
| 4334 |
|
| 4335 |
if( xbind(xpa->fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ) |
| 4336 |
goto error; |
| 4337 |
|
| 4338 |
if( getsockname(xpa->fd, (struct sockaddr *)&sock_in, &slen) < 0 ) |
| 4339 |
goto error; |
| 4340 |
else{ |
| 4341 |
|
| 4342 |
gethost(tbuf2, SZ_LINE); |
| 4343 |
snprintf(tbuf, SZ_LINE, "%x:%d", |
| 4344 |
gethostip(tbuf2), ntohs(sock_in.sin_port)); |
| 4345 |
xpa->method = xstrdup(tbuf); |
| 4346 |
} |
| 4347 |
break; |
| 4348 |
#if HAVE_SYS_UN_H |
| 4349 |
case XPA_UNIX: |
| 4350 |
|
| 4351 |
snprintf(tfile, SZ_LINE, "%s_%s.%d", |
| 4352 |
xpa->xclass, xpa->name, (int)getpid()); |
| 4353 |
|
| 4354 |
for(tfptr = tfile; *tfptr != '\0'; tfptr++){ |
| 4355 |
if( *tfptr == '/' ) |
| 4356 |
*tfptr = '_'; |
| 4357 |
} |
| 4358 |
|
| 4359 |
snprintf(tbuf, SZ_LINE, "%s/%s", tmpdir, tfile); |
| 4360 |
|
| 4361 |
unlink (tbuf); |
| 4362 |
|
| 4363 |
if( (xpa->fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) |
| 4364 |
goto error; |
| 4365 |
setsockopt(xpa->fd, SOL_SOCKET, SO_KEEPALIVE, |
| 4366 |
(char *)&keep_alive, sizeof(keep_alive)); |
| 4367 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 4368 |
sock_un.sun_family = AF_UNIX; |
| 4369 |
strcpy(sock_un.sun_path, tbuf); |
| 4370 |
|
| 4371 |
oum = umask(0); |
| 4372 |
|
| 4373 |
got = xbind(xpa->fd, (struct sockaddr *)&sock_un, sizeof(sock_un)); |
| 4374 |
|
| 4375 |
umask(oum); |
| 4376 |
|
| 4377 |
if( got < 0 ) |
| 4378 |
goto error; |
| 4379 |
|
| 4380 |
xpa->method = xstrdup(tbuf); |
| 4381 |
break; |
| 4382 |
#endif |
| 4383 |
default: |
| 4384 |
goto error; |
| 4385 |
} |
| 4386 |
|
| 4387 |
|
| 4388 |
if( listen(xpa->fd, XPA_MAXLISTEN) < 0 ) |
| 4389 |
goto error; |
| 4390 |
|
| 4391 |
|
| 4392 |
xfcntl(xpa->fd, F_SETFD, FD_CLOEXEC); |
| 4393 |
|
| 4394 |
|
| 4395 |
XPAListAdd(&xpahead, xpa); |
| 4396 |
|
| 4397 |
|
| 4398 |
if( nsregister ) |
| 4399 |
XPANSAdd(xpa, NULL, NULL); |
| 4400 |
|
| 4401 |
|
| 4402 |
XPAActive(xpa, NULL, 1); |
| 4403 |
|
| 4404 |
#if NO_AUTOMATIC_HAVE_ATEXIT |
| 4405 |
|
| 4406 |
if( !atexitinit ){ |
| 4407 |
atexit(_XPAAtExit); |
| 4408 |
atexitinit = getpid(); |
| 4409 |
} |
| 4410 |
#endif |
| 4411 |
|
| 4412 |
|
| 4413 |
return(xpa); |
| 4414 |
|
| 4415 |
error: |
| 4416 |
XPAFree(xpa); |
| 4417 |
return(NULL); |
| 4418 |
} |
| 4419 |
|
| 4420 |
|
| 4421 |
|
| 4422 |
|
| 4423 |
|
| 4424 |
|
| 4425 |
|
| 4426 |
|
| 4427 |
|
| 4428 |
|
| 4429 |
|
| 4430 |
|
| 4431 |
|
| 4432 |
|
| 4433 |
#ifdef ANSI_FUNC |
| 4434 |
int |
| 4435 |
XPAPoll (int msec, int maxreq) |
| 4436 |
#else |
| 4437 |
int XPAPoll(msec, maxreq) |
| 4438 |
int msec; |
| 4439 |
int maxreq; |
| 4440 |
#endif |
| 4441 |
{ |
| 4442 |
int sgot; |
| 4443 |
int got=0; |
| 4444 |
fd_set readfds; |
| 4445 |
struct timeval tv; |
| 4446 |
struct timeval *tvp; |
| 4447 |
|
| 4448 |
again: |
| 4449 |
if( msec >= 0 ){ |
| 4450 |
tv.tv_sec = msec / 1000; |
| 4451 |
tv.tv_usec = (msec % 1000) * 1000; |
| 4452 |
tvp = &tv; |
| 4453 |
} |
| 4454 |
|
| 4455 |
else{ |
| 4456 |
tvp = NULL; |
| 4457 |
} |
| 4458 |
FD_ZERO(&readfds); |
| 4459 |
if( XPAAddSelect(NULL, &readfds) ){ |
| 4460 |
sgot = xselect(swidth, &readfds, NULL, NULL, tvp); |
| 4461 |
|
| 4462 |
if( sgot < 0 ){ |
| 4463 |
if( xerrno == EINTR ) |
| 4464 |
goto again; |
| 4465 |
if( verbosity ){ |
| 4466 |
perror("XPAPoll() select"); |
| 4467 |
} |
| 4468 |
exit(1); |
| 4469 |
} |
| 4470 |
|
| 4471 |
else if( sgot == 0 ) |
| 4472 |
; |
| 4473 |
|
| 4474 |
else{ |
| 4475 |
|
| 4476 |
if( maxreq < 0 ) |
| 4477 |
return(sgot); |
| 4478 |
else |
| 4479 |
got = XPAProcessSelect(&readfds, maxreq); |
| 4480 |
} |
| 4481 |
} |
| 4482 |
return(got); |
| 4483 |
} |
| 4484 |
|
| 4485 |
|
| 4486 |
|
| 4487 |
|
| 4488 |
|
| 4489 |
|
| 4490 |
|
| 4491 |
|
| 4492 |
|
| 4493 |
|
| 4494 |
|
| 4495 |
|
| 4496 |
#ifdef ANSI_FUNC |
| 4497 |
int |
| 4498 |
XPAMainLoop (void) |
| 4499 |
#else |
| 4500 |
int XPAMainLoop() |
| 4501 |
#endif |
| 4502 |
{ |
| 4503 |
int sgot; |
| 4504 |
int got=0; |
| 4505 |
fd_set readfds; |
| 4506 |
|
| 4507 |
|
| 4508 |
FD_ZERO(&readfds); |
| 4509 |
while( XPAAddSelect(NULL, &readfds) ){ |
| 4510 |
FPRINTF((stderr, "%sXPAMainLoop: waiting for select() ...\n", _sp)); |
| 4511 |
sgot = xselect(swidth, &readfds, NULL, NULL, NULL); |
| 4512 |
FPRINTF((stderr, "%sXPAMainLoop: select() returned: %d\n", _sp, sgot)); |
| 4513 |
|
| 4514 |
if( sgot < 0 ){ |
| 4515 |
if( xerrno == EINTR ){ |
| 4516 |
FD_ZERO(&readfds); |
| 4517 |
continue; |
| 4518 |
} |
| 4519 |
if( verbosity ){ |
| 4520 |
perror("XPAMainLoop() select"); |
| 4521 |
} |
| 4522 |
exit(1); |
| 4523 |
} |
| 4524 |
|
| 4525 |
else if( sgot == 0 ) |
| 4526 |
; |
| 4527 |
|
| 4528 |
else |
| 4529 |
got += XPAProcessSelect(&readfds, 0); |
| 4530 |
FD_ZERO(&readfds); |
| 4531 |
} |
| 4532 |
return(got); |
| 4533 |
} |
| 4534 |
|
| 4535 |
|
| 4536 |
|
| 4537 |
|
| 4538 |
|
| 4539 |
|
| 4540 |
|
| 4541 |
|
| 4542 |
|
| 4543 |
|
| 4544 |
|
| 4545 |
|
| 4546 |
#ifdef ANSI_FUNC |
| 4547 |
void |
| 4548 |
XPASleep (int msec) |
| 4549 |
#else |
| 4550 |
void XPASleep(msec) |
| 4551 |
int msec; |
| 4552 |
#endif |
| 4553 |
{ |
| 4554 |
struct timeval tv; |
| 4555 |
|
| 4556 |
if( msec > 0 ){ |
| 4557 |
tv.tv_sec = msec / 1000; |
| 4558 |
tv.tv_usec = (msec % 1000) * 1000; |
| 4559 |
xselect(1, NULL, NULL, NULL, &tv); |
| 4560 |
} |
| 4561 |
} |
| 4562 |
|
| 4563 |
|
| 4564 |
|
| 4565 |
|
| 4566 |
|
| 4567 |
|
| 4568 |
|
| 4569 |
|
| 4570 |
|
| 4571 |
|
| 4572 |
|
| 4573 |
|
| 4574 |
#ifdef ANSI_FUNC |
| 4575 |
int |
| 4576 |
XPAMtype (void) |
| 4577 |
#else |
| 4578 |
int XPAMtype() |
| 4579 |
#endif |
| 4580 |
{ |
| 4581 |
return(mtype); |
| 4582 |
} |
| 4583 |
|
| 4584 |
|
| 4585 |
|
| 4586 |
|
| 4587 |
|
| 4588 |
|
| 4589 |
|
| 4590 |
|
| 4591 |
|
| 4592 |
|
| 4593 |
|
| 4594 |
|
| 4595 |
#ifdef ANSI_FUNC |
| 4596 |
char * |
| 4597 |
XPATmpdir (void) |
| 4598 |
#else |
| 4599 |
char * XPATmpdir() |
| 4600 |
#endif |
| 4601 |
{ |
| 4602 |
return(tmpdir); |
| 4603 |
} |
| 4604 |
|
| 4605 |
|
| 4606 |
|
| 4607 |
|
| 4608 |
|
| 4609 |
|
| 4610 |
|
| 4611 |
|
| 4612 |
|
| 4613 |
|
| 4614 |
|
| 4615 |
|
| 4616 |
|
| 4617 |
#ifdef ANSI_FUNC |
| 4618 |
int |
| 4619 |
XPASetBuf (XPA xpa, char *buf, size_t len, int xcopy) |
| 4620 |
#else |
| 4621 |
int XPASetBuf(xpa, buf, len, xcopy) |
| 4622 |
XPA xpa; |
| 4623 |
char *buf; |
| 4624 |
size_t len; |
| 4625 |
int xcopy; |
| 4626 |
#endif |
| 4627 |
{ |
| 4628 |
|
| 4629 |
if( !xpa || !xpa->comm ) |
| 4630 |
return(-1); |
| 4631 |
|
| 4632 |
if( xcopy ){ |
| 4633 |
xpa->comm->len = len; |
| 4634 |
if( (xpa->comm->buf = (char *)xmalloc(len)) == NULL ) |
| 4635 |
return(-1); |
| 4636 |
memcpy(xpa->comm->buf, buf, len); |
| 4637 |
} |
| 4638 |
else{ |
| 4639 |
xpa->comm->len = len; |
| 4640 |
xpa->comm->buf = buf; |
| 4641 |
} |
| 4642 |
return(0); |
| 4643 |
} |
| 4644 |
|
| 4645 |
|
| 4646 |
|
| 4647 |
|
| 4648 |
|
| 4649 |
|
| 4650 |
|
| 4651 |
|
| 4652 |
|
| 4653 |
|
| 4654 |
|
| 4655 |
|
| 4656 |
|
| 4657 |
#ifdef ANSI_FUNC |
| 4658 |
int |
| 4659 |
XPASetFree (XPA xpa, MyFree myfree, void *myfree_ptr) |
| 4660 |
#else |
| 4661 |
int XPASetFree(xpa, myfree, myfree_ptr) |
| 4662 |
XPA xpa; |
| 4663 |
MyFree myfree; |
| 4664 |
void *myfree_ptr; |
| 4665 |
#endif |
| 4666 |
{ |
| 4667 |
|
| 4668 |
if( !xpa || !xpa->comm ) return(-1); |
| 4669 |
|
| 4670 |
xpa->comm->myfree = myfree; |
| 4671 |
xpa->comm->myfree_ptr = myfree_ptr; |
| 4672 |
return(0); |
| 4673 |
} |
| 4674 |
|
| 4675 |
|
| 4676 |
|
| 4677 |
|
| 4678 |
|
| 4679 |
|
| 4680 |
|
| 4681 |
|
| 4682 |
|
| 4683 |
|
| 4684 |
|
| 4685 |
|
| 4686 |
#ifdef ANSI_FUNC |
| 4687 |
void XPACleanup (void) |
| 4688 |
#else |
| 4689 |
void XPACleanup () |
| 4690 |
#endif |
| 4691 |
{ |
| 4692 |
XPAFreeReserved(); |
| 4693 |
XPAAclFree(); |
| 4694 |
if( tmpdir ){ |
| 4695 |
xfree(tmpdir); |
| 4696 |
tmpdir = NULL; |
| 4697 |
} |
| 4698 |
} |
| 4699 |
|
| 4700 |
#ifdef ANSI_FUNC |
| 4701 |
void XPASaveJmp(void *env) |
| 4702 |
#else |
| 4703 |
void XPASaveJmp(env) |
| 4704 |
void *env; |
| 4705 |
#endif |
| 4706 |
{ |
| 4707 |
#if HAVE_SETJMP_H |
| 4708 |
xalloc_savejmp((jmp_buf *)env); |
| 4709 |
#else |
| 4710 |
return; |
| 4711 |
#endif |
| 4712 |
} |