Debian Codegraph @

xpa/xpa.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 /*
8 *----------------------------------------------------------------------------
9 *
10 *
11 * Private Routines and Data
12 *
13 *
14 *----------------------------------------------------------------------------
15 */
16
17 /* this is the head of the global list -- too lazy to do anything more */
18 static XPA xpahead=NULL;
19
20 /* globals for the xpa environment, controlled by environment variables */
21 static int stimeout=XPA_SHORT_TIMEOUT; /* select, gets timeout in secs */
22 static int ltimeout=XPA_LONG_TIMEOUT; /* fillbuf timeout in secs */
23 static int ctimeout=XPA_CONNECT_TIMEOUT;/* local xpans connect */
24 static int verbosity=XPA_VERBOSITY; /* 0=quiet, 1=normal, 2=full */
25 static int guseacl=1; /* 0=don't use acls, 1=enable acls */
26 static int etimestamp=0; /* 0=don't timestamp errors, 1=do timestamp */
27 static int nsregister=1; /* 0=don't register with xpans, 1=register */
28 static int sigusr1=1; /* 0=don't use sigusr1, 1=enable sigusr1 */
29 static int vercheck=1; /* 0=don't check version, 1=check version */
30 static char *tmpdir=NULL; /* temporary dir for logs. etc. */
31 #if HAVE_ATEXIT
32 static int atexitinit=0; /* whether atexit has been registered */
33 #endif
34
35 /* variables used by all XPAs in this process */
36 static char activefds[FD_SETSIZE];
37 static char nsmethod[SZ_LINE];
38 static char nsusers[SZ_LINE];
39
40 /* global method type: unix or inet */
41 static int mtype=0;
42
43 /* width of select channel flag */
44 static int swidth=FD_SETSIZE;
45
46 /* defined in tcp.c */
47 extern int use_localhost;
48
49 /* erro code strings -- must match the error codes in xpap.h */
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 /* temp static time buffer */
69 static char ctimebuf[SZ_LINE];
70
71 /*
72 *----------------------------------------------------------------------------
73 *
74 * Routine: XPAProxyConnect
75 *
76 * Purpose: connect to a client (in proxy mode)
77 *
78 * Returns: 0 if successfull, otherwise -1
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 /* initialize results */
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 /* use $localhost over $host (we don't trust host to be correct) */
119 if( (ip == gethostip("$host")) && (tries == 0) ){
120 ip = gethostip("$localhost");
121 }
122 /* connect to the server before we go further */
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 /* make the connection with the server */
134 if(connect(ofd, (struct sockaddr *)&sock_in, sizeof(sock_in))<0){
135 xclose(ofd);
136 /* if localhost doesn't work, make one try with the host ip */
137 /* we also try again just in case there was an odd error such
138 as "permission denied", which we have seen once or twice */
139 if( tries < 2 ){
140 tries++;
141 goto again1;
142 }
143 /* give up */
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 /* open a socket and fill in socket information */
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 /* make the connection with the server */
165 if(connect(ofd, (struct sockaddr *)&sock_un, sizeof(sock_un))<0){
166 xclose(ofd);
167 /* Unix sockets get ECONNREFUSED when the listen queue is full,
168 so we try a few times to give the server a chance to recover */
169 if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
170 tries++;
171 XPASleep(10);
172 goto again2;
173 }
174 /* give up */
175 else{
176 goto error;
177 }
178 }
179 break;
180 #endif
181 default:
182 goto error;
183 }
184
185 /* fill in blansk */
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 * Routine: CommNew
202 *
203 * Purpose: allocate a new comm record and add to end of list
204 *
205 * Returns: allocated comm record
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 /* create the comm struct */
232 if( (comm = (XPAComm)xcalloc(1, sizeof(XPACommRec))) == NULL )
233 return(NULL);
234
235 /* if fd < 0, we accept a connection to get the fd */
236 if( fd < 0 ){
237 /* accept the connection */
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 /* all info is supplied */
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 /* store name server record */
294 comm->ns = ns;
295 }
296
297 /* set back pointer */
298 /* make sure we close on exec */
299 xfcntl(comm->cmdfd, F_SETFD, FD_CLOEXEC);
300 /* mark data socket with impossible value */
301 comm->datafd = -1;
302 /* default is to ack */
303 comm->ack = 1;
304 /* set default cendian flag */
305 comm->cendian = "?";
306 /* clear the acl flags */
307 for(i=0; i<XPA_CMDS+1; i++){
308 comm->acl[i] = -1;
309 }
310
311 /* add this comm to end of list of comms */
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 /* we might have to add this fd specially to a non-select event loop */
323 if( xpa->seladd )
324 comm->selcptr = (xpa->seladd)(xpa, comm->cmdfd);
325
326 /* make this fd active */
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 /* return the good news */
333 return(comm);
334 }
335
336 /*
337 *----------------------------------------------------------------------------
338 *
339 * Routine: CommFree
340 *
341 * Purpose: free a comm record and remove from list
342 *
343 * Returns: none
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 /* remove from list of this xpa's comms */
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 /* must check all xpas */
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 /* close socket connections */
400 if( flag && (comm->cmdfd >= 0) ){
401 FPRINTF((stderr, "%sCommFree closing cmd fd: %d\n", _sp, comm->cmdfd));
402 /* remove from active */
403 if( comm->cmdfd < FD_SETSIZE )
404 activefds[comm->cmdfd] = 0;
405 /* delete the comm cmd fd specially from a non-select event loop */
406 if( xpa && xpa->seldel && comm->selcptr ){
407 (xpa->seldel)(comm->selcptr);
408 comm->selcptr = NULL;
409 }
410 /* close file */
411 xclose(comm->cmdfd);
412 }
413 /* close data channel */
414 XPACloseData(xpa, comm);
415 /* if we have a file name (unix sockets), free it */
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 /* free up the space */
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 /* this comm is no longer associated with an ns */
432 if( comm->ns ){
433 comm->ns->nproxy -= 1;
434 }
435
436 /* disassociate from parent xpa */
437 if( xpa ){
438 xpa->comm = NULL;
439 }
440
441 /* free up structure */
442 xfree(comm);
443 }
444
445 /*
446 *----------------------------------------------------------------------------
447 *
448 * Routine: XPANSOpen
449 *
450 * Purpose: open a connection to the name service
451 *
452 * Returns: connection fd on success, -1 on failure
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 /* get name server method */
492 method = XPANSMethod(host, 0);
493
494 /* if the name service already is open, just return fd */
495 if( xpa ){
496 for(ns=xpa->nshead; ns!=NULL; ns=ns->next){
497 if( !strcmp(ns->method, method) ){
498 /* if forcing, see if the connection is valid */
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 /* found the ns, but its not open */
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 /* just return whatever we have if we are not forcing */
516 else{
517 return ns;
518 }
519 }
520 }
521 }
522
523 /* if no existing ns and flag is negative, we are done */
524 if( force == -1 )
525 return(NULL);
526
527 /* we always make up the command afresh */
528 *nscmd = '\0';
529
530 /* get users for this user */
531 if( (s=(char *)getenv("XPA_NSUSERS")) != NULL )
532 strncpy(nsusers, s, SZ_LINE-1);
533 /* default is just this use's userrname, from the environment */
534 else if( (s=(char *)getenv("LOGNAME")) != NULL )
535 strncpy(nsusers, s, SZ_LINE-1);
536 #if HAVE_MINGW32==0
537 /* this is a last resort */
538 else if( (pw=getpwuid(geteuid())) )
539 strncpy(nsusers, pw->pw_name, SZ_LINE-1);
540 #endif
541 /* if nothing good has happened, make it "all" */
542 if( *nsusers == '\0' )
543 strcpy(nsusers, "*");
544 /* null-terminate string */
545 nsusers[SZ_LINE-1] = '\0';
546
547 /* set up communications socket for this method */
548 switch(mtype){
549 case XPA_INET:
550 again1:
551 XPAParseIpPort(method, &xnsip, &xnsport);
552 /* use $localhost over $host (we do not trust host to be correct) */
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 /* try connecting to the name server */
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 /* if localhost doesn't work, make one try with the host ip */
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 /* open a socket and fill in socket information */
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 /* try connecting to the name server */
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 /* Unix sockets get ECONNREFUSED when the listen queue is full,
616 so we try a few times to give the server a chance to recover */
617 if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
618 tries++;
619 XPASleep(10);
620 goto again2;
621 }
622 }
623 break;
624 #endif
625 }
626
627 /* if force is set, we try to start up the name server */
628 if( force == 0 )
629 goto noforce;
630
631 /* make up the xpans command we will use */
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 /* this will help start up xpans under Windows */
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 /* change spaces to special launch space */
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 /* change spaces to special launch space */
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 /* did not find the name server -- start it up if we can, i.e.,
683 if its on the same machine as we are on and we found it in the path */
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 /* enter loop looking to connect */
694 for(tries=0; tries<XPA_RETRIES; tries++){
695 switch(mtype){
696 case XPA_INET:
697 /* use $localhost alternately with $host */
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 /* if we got here, we did not connect */
730 goto nons;
731 }
732 /* name server is on a remote machine, so there is no hope */
733 else{
734 goto nons;
735 }
736
737 okns:
738 /* make sure we close on exec */
739 xfcntl(xnsfd, F_SETFD, FD_CLOEXEC);
740 setsockopt(xnsfd, SOL_SOCKET, SO_KEEPALIVE,
741 (char *)&keep_alive, sizeof(keep_alive));
742 /* fill in new record */
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 /* add to list of name servers */
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 /* check version with xpans */
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 /* version check: our server version should be <= xpans version */
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 /* clean up */
801 if( findname != NULL ) xfree(findname);
802 return(ns);
803
804 nons:
805 /* if we specified an explicit port, we don't need the name server,
806 so don't bother with any warning or error messages */
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 /* make up the command users will need to start xpans */
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 * Routine: _XPAFree
869 *
870 * Purpose: free up memory in the XPA record structure
871 * (internal version)
872 *
873 * Results: 0 on success, -1 for failure
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 /* make sure we have something to do */
892 if( xpa == NULL )
893 return(-1);
894
895 FPRINTF((stderr, "%s_XPAFree: freeing xpa struct\n", _sp));
896 /* remove this xpa from public availability */
897 if( xpa->type != NULL )
898 XPANSDel(xpa, NULL, NULL);
899
900 /* free all sub-commands */
901 for(cmd=xpa->commands; cmd!=NULL; ){
902 tcmd = cmd->next;
903 XPACmdDel(xpa, cmd);
904 cmd = tcmd;
905 }
906
907 /* remove from list of xpas */
908 XPAListDel(&xpahead, xpa);
909
910 /* close down listening socket */
911 if( xpa->fd >= 0 )
912 xclose(xpa->fd);
913
914 /* perform method-specific cleanup */
915 switch(mtype){
916 #if HAVE_SYS_UN_H
917 case XPA_UNIX:
918 /* delete the unix socket files */
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 /* free up space */
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 /* call the select free routine for the listening socket and loop type.
938 we use an indirect routine to avoid having to link Xt, Tcl, etc. */
939 if( xpa->seldel && xpa->selptr ){
940 (xpa->seldel)(xpa->selptr);
941 xpa->selptr = NULL;
942 }
943
944 /* close communication channels */
945 for(comm=xpa->commhead; comm!=NULL; ){
946 tcomm = comm->next;
947 CommFree(xpa, comm, 1);
948 comm = tcomm;
949 }
950
951 /* free up clipboards */
952 for(clip=xpa->cliphead; clip!=NULL; ){
953 tclip = clip->next;
954 ClipBoardFree(xpa, clip);
955 clip = tclip;
956 }
957
958 /* close down the name server and all of the remotes for this xpa */
959 for(ns=xpa->nshead; ns!=NULL; ){
960 tns = ns->next;
961 XPANSClose(xpa, ns);
962 ns = tns;
963 }
964
965 /* free up record struct */
966 xfree((char *)xpa);
967
968 return(0);
969 }
970
971 #if HAVE_ATEXIT
972 /*
973 *---------------------------------------------------------------------------
974 *
975 * Routine: _XPAAtExit
976 *
977 * Purpose: XPA cleanup on exit
978 * The main purpose of this routine is to try to delete the
979 * unix socket files when an XPA server exits.
980 *
981 * Results: none
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 /* return if we were not initialized */
997 if( !atexitinit ) return;
998 /* return if I am not the process who initialized (I'm a child) */
999 if( atexitinit != getpid() ) return;
1000 FPRINTF((stderr, "calling XPAAtExit\n"));
1001 for(xpa=xpahead; xpa!=NULL; ){
1002 /* use temp in case we destroy structure in the cleanup */
1003 txpa = xpa->next;
1004 _XPAFree(xpa);
1005 xpa = txpa;
1006 }
1007 /* platform-dependent cleanup */
1008 xsocketcleanup();
1009 /* done with cleanup */
1010 done++;
1011 }
1012 }
1013
1014 /*
1015 *---------------------------------------------------------------------------
1016 *
1017 * Routine: XPAAtExit
1018 *
1019 * Purpose: set up XPA cleanup on exit
1020 *
1021 * Results: none
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 * Semi-Public Routines
1045 * (mainly used by xpaset and xpaget)
1046 *
1047 *
1048 *----------------------------------------------------------------------------
1049 */
1050
1051 /*
1052 *---------------------------------------------------------------------------
1053 *
1054 * Routine: _XPAValid
1055 *
1056 * Purpose: see if the xpa struct is valid
1057 *
1058 * Results: 1 on success, 0 for failure
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 * Routine: XPAValid
1088 *
1089 * Purpose: see if the xpa struct is valid
1090 *
1091 * Results: 1 on success, 0 for failure
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 * Routine: XPAEndian
1113 *
1114 * Purpose: semi-public routine to return the endian-ness of this
1115 * machine
1116 *
1117 * Results: 0 if little endian, 1 if bigendian
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 * Routine: XPAListHead
1141 *
1142 * Purpose: semi-public routine to return the head of the xpa list
1143 *
1144 * Results: XPA list pointer on success
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 * Routine: XPAListAdd
1162 *
1163 * Purpose: add a member of an xpa list
1164 *
1165 * Results: 1 on success, 0 for failure
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 * Routine: XPAListDel
1194 *
1195 * Purpose: remove a member of an xpa list
1196 *
1197 * Results: 1 on success, 0 for failure
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 /* remove from list of xpas */
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 * Routine: XPAActive
1232 *
1233 * Purpose: make the xpa, cmd and data fds active or inactive for select
1234 *
1235 * Returns: none
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 /* sanity check */
1252 if( !xpa ) return(0);
1253
1254 switch(flag){
1255 /* remove this xpa from the active list */
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 /* add this xpa/comm to the active list */
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 * Routine: XPAActiveFd
1309 *
1310 * Purpose: semi-public routine to return flag if fd is active
1311 *
1312 * Results: 1 is active, 0 is inactive
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 * Routine: XPAAddSelect
1334 *
1335 * Purpose: add one or more xpa sockets to the select flags
1336 *
1337 * Return: number of xpas that were added to the select flags
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 /* better have some place to set the flags */
1355 if( readfdsptr == NULL )
1356 return(0);
1357
1358 /* if a specific xpa was specified, just set its select flags */
1359 if( xpa != NULL ){
1360 if( XPAActiveFd(xpa->fd) ){
1361 FD_SET(xpa->fd, readfdsptr);
1362 got++;
1363 /* note that we only select on coms if the main fd is active */
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 /* otherwise set select flags for all xpas */
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 /* note that we only select on coms if the main fd is active */
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 * Routine: XPAProcessSelect
1408 *
1409 * Purpose: process xpas that have pending reads or writes
1410 *
1411 * Return: number of xpas processed
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 /* <= 0 means do all of them */
1433 if( maxreq < 0 ){
1434 maxreq = 0;
1435 }
1436
1437 again:
1438 for(xpa=xpahead; xpa!=NULL; xpa=xpa->next){
1439 /* handle command requests */
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 /* handle data requests */
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 /* handle new requests */
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 /* handle command requests */
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 /* if we got an error on this xpa, skip processing rest of it */
1479 if( XPAHandler(xpa, comm->cmdfd) != XPA_RTN_OK ){
1480 goto nextxpa;
1481 }
1482 got++;
1483 /* if we freed this xpa, skip processing rest of it */
1484 if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){
1485 goto nextxpa;
1486 }
1487 /* if we are have processed the max reqests, we are done */
1488 if( maxreq && (got >= maxreq) ){
1489 return(got);
1490 }
1491 }
1492 comm = tcomm;
1493 }
1494 /* handle data requests */
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 /* if we got an error on this xpa, skip processing rest of it */
1501 if( XPAHandler(xpa, comm->datafd) != XPA_RTN_OK ){
1502 goto nextxpa;
1503 }
1504 got++;
1505 /* if we freed this xpa, skip processing rest of it */
1506 if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){
1507 goto nextxpa;
1508 }
1509 /* if we are have processed the max reqests, we are done */
1510 if( maxreq && (got >= maxreq) ){
1511 return(got);
1512 }
1513 }
1514 comm = tcomm;
1515 }
1516 /* handle new requests */
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 /* if we got an error on this xpa, skip processing rest of it */
1521 if( XPAHandler(xpa, xpa->fd) != XPA_RTN_OK ){
1522 goto nextxpa;
1523 }
1524 got++;
1525 /* if we freed this xpa, skip processing rest of it */
1526 if( !_XPAValid(xpahead, xpa, XPA_ACLS) ){
1527 goto nextxpa;
1528 }
1529 /* if we are have processed the max reqests, we are done */
1530 if( maxreq && (got >= maxreq) ){
1531 return(got);
1532 }
1533 }
1534
1535 nextxpa:
1536 /* must check to see if last xpa freed the next xpa */
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 * Routine: XPACloseData
1551 *
1552 * Purpose: close data fd if its not the cmd fd
1553 *
1554 * Returns: none
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 /* we close the data channel if its not the command channel */
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 /* remove from active */
1573 if( comm->datafd < FD_SETSIZE )
1574 activefds[comm->datafd] = 0;
1575 /* delete the comm data fd specially from a non-select event loop */
1576 if( xpa && xpa->seldel && comm->seldptr ){
1577 (xpa->seldel)(comm->seldptr);
1578 comm->seldptr = NULL;
1579 }
1580 /* close file */
1581 xclose(comm->datafd);
1582 }
1583 /* reset data channel to impossible value */
1584 comm->datafd = -1;
1585 }
1586 }
1587
1588 /*
1589 *----------------------------------------------------------------------------
1590 *
1591 * Routine: XPAHandler
1592 *
1593 * Purpose: handle one request for an xpaset or xpaget
1594 *
1595 * Return: 0 on success, xpa error code on failure
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 /* this is a defensive measure: we have to guard against an external
1633 loop calling the XPA handler with a bogus XPA record. This has been
1634 seen with the Tcl event loop.
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 /* this is a defensive measure: we have to ensure that there really
1646 * is a request read. It is possible that a user-defined select loop
1647 * might call us to handle a request that we had already handled
1648 * (i.e. we handle a request but can't reset someone else's select flags)
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 /* if this is a first connection, we create a new comm channel and exit */
1662 if( fd == xpa->fd ){
1663 if( (comm = CommNew(xpa, -1, 0, 0, NULL, NULL)) == NULL )
1664 return(XPA_RTN_NOCONN);
1665 /* Return to prevent xpa from finishing before other xpa's are started.
1666 Otherwise, each xpa's associated with a target template will be
1667 processed serially, which will defeat all the hard work done on
1668 the client side to send data to servers when they ask for it.
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 /* look for a currently active comm channel: cmd or data */
1679 for(comm=xpa->commhead; comm!= NULL; comm=comm->next){
1680 if( (fd == comm->cmdfd) || (fd == comm->datafd) ){
1681 break;
1682 }
1683 }
1684 /* make sure we have something */
1685 if( comm == NULL ){
1686 /* read extraenous message */
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 /* but don't recurse */
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 /* set current comm for this xpa */
1707 ocomm = xpa->comm;
1708 xpa->comm = comm;
1709 /* no message sent yet */
1710 comm->message = 0;
1711
1712 /* data ready on data channel: go right to data handling section */
1713 if( fd == comm->datafd ){
1714 FPRINTF((stderr, "%sXPAHandler: jumping to cb for %d\n", _sp, fd));
1715 goto cb;
1716 }
1717
1718 /* cmd channel: we are processing a new command */
1719 retry:
1720 /* reset line buffer pointer for parsing */
1721 lp = 0;
1722 /* read next command */
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 /* new-lines imply we entered telnet mode on local host */
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 /* validate the command */
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 /* send help message (local support only) */
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 /* we must be in telnet mode */
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 /* determine which command was specified */
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 /* process switches */
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 /* [email protected] 2009-09-21 */
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 /* make sure we have some sort of id */
1888 if( comm->id == NULL ){
1889 comm->id = xstrdup("?");
1890 }
1891
1892 /* process nagle ack */
1893 if( tcmd == XPA_NAGLE ){
1894 /* if data is forthcoming, exit and wait for data */
1895 if( comm->usebuf ){
1896 got = XPA_RTN_OK;
1897 goto end;
1898 }
1899 /* go process callback */
1900 else{
1901 goto cb;
1902 }
1903 }
1904
1905 /* connect (proxy) request: connect back to client */
1906 if( tcmd == XPA_ACCEPT ){
1907 FPRINTF((stderr, "%scmd is xpaaccept from client %d: %s", _sp, fd, lbuf));
1908 /* syntax: xpaaccept <method> ... */
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 /* now exit and wait for client to send command */
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 /* data request: find associated command request, and process the command */
1936 if( tcmd == XPA_DATA ){
1937 FPRINTF((stderr, "%sXPAHandler: processing data fd %d\n",
1938 _sp, comm->cmdfd));
1939 /* find the cmd record with which this data is associated */
1940 for(tcomm=xpa->commhead; tcomm!= NULL; tcomm=tcomm->next){
1941 if( (tcomm == xcomm) && (tcomm->cmdfd == cmdfd) ){
1942 break;
1943 }
1944 }
1945 /* if we found an associated command, copy in data info and process */
1946 if( tcomm ){
1947 /* fill in command comm */
1948 tcomm->datafd = comm->cmdfd;
1949 tcomm->seldptr = comm->selcptr;
1950 if( tcomm->dataname ) xfree(tcomm->dataname);
1951 tcomm->dataname = xstrdup(comm->cmdname);
1952 /* done with data comm */
1953 CommFree(xpa, comm, 0);
1954 /* reset comm pointers */
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 /* for a command, the first non-switch word we found was the target */
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 /* validate the target (except for xpans for use as proxy) */
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 /* the rest of the input string is paramlist */
2005 paramlist = &(lbuf[lp]);
2006 nowhite(paramlist, paramlist);
2007 if( comm->paramlist != NULL ) xfree(comm->paramlist);
2008 comm->paramlist = xstrdup(paramlist);
2009 /* save command */
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 /* a reserved command can only be called from the same host as the server,
2017 or from local (unix) sockets */
2018 lp = 0;
2019 if( XPACmdLookupReserved(xpa, comm->paramlist, &lp) ){
2020 comm->mode |= COMM_RESERVED;
2021 }
2022
2023 /* set up command-specific info */
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 /* may as well check access mode as well */
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 /* perform access authentication */
2090 if( comm->useacl ){
2091 /* determine acl for this ip, if necessary */
2092 if( comm->acl[comm->cmd] < 0 ){
2093 comm->acl[comm->cmd] = XPAAclCheck(xpa, comm->cmdip, acl);
2094 }
2095 /* check acl */
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 /* for telnet mode, just use cmdfd for data */
2108 if( comm->telnet ){
2109 comm->datafd = comm->cmdfd;
2110 }
2111 /* for xpainfo and no ack, just go to the vallback */
2112 else if( (comm->cmd == XPA_INFO) && !comm->ack ){
2113 goto cb;
2114 }
2115 /* in general, we must tell the client how we will handle data */
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 /* no data channel being set up */
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 /* now exit and wait for nagle ack and for client to send data */
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 /* data channel complete (or no data): ready to execute the user callback */
2151 cb:
2152 /* we are now active */
2153 comm->status |= XPA_STATUS_ACTIVE;
2154
2155 /* remove the current comm from the list of active fds,
2156 in case the server callback re-enters the event loop */
2157 XPAActive(xpa, comm, 0);
2158
2159 /* zero out buf and len, just to make sure (don't free buf, in case
2160 application is using previous) */
2161 comm->buf = NULL;
2162 comm->len = 0;
2163
2164 /* if we are not ack'ing after callback, do it now so client can exit */
2165 /* but don't do this for xpainfo/noack */
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 /* process command */
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 /* check for a reserved command */
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 /* send client a message, unless its already been done */
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 /* check for a reserved command */
2225 if( comm->mode & COMM_RESERVED ){
2226 got=XPAReceiveCommands(xpa->receive_data, xpa,
2227 comm->paramlist, NULL, 0);
2228 }
2229 else{
2230 /* fill buf if necessary */
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 /* close the data fd now that we are done with it */
2242 XPACloseData(xpa, comm);
2243 }
2244 }
2245 /* execute the receive callback */
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 /* send client an error message, unless its already been done */
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 /* send OK before callback because we do not want the client waiting */
2277 if( comm->ack )
2278 XPAOK(xpa);
2279 save_ack = comm->ack;
2280 /* don't ever ack in callback */
2281 comm->ack = 0;
2282 /* execute the info callback -- don't bother checking for errors */
2283 (xpa->info_callback)(xpa->info_data, xpa, comm->paramlist);
2284 comm->ack = save_ack;
2285 break;
2286 case XPA_ACCESS:
2287 /* return errors -- that how we know if we have access */
2288 comm->ack = 1;
2289 /* type is in paramlist */
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 /* if we got here, its OK */
2320 XPAOK(xpa);
2321 }
2322 /* no type, anything is OK */
2323 else
2324 XPAOK(xpa);
2325 break;
2326 default:
2327 /* something is really wrong if we have no command */
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 /* add xpa back to list of active ones */
2340 XPAActive(xpa, comm, 1);
2341
2342 /* finalize comm record */
2343 if( comm ){
2344 /* close data channel */
2345 XPACloseData(xpa, comm);
2346 /* xpa is no longer active */
2347 comm->status &= ~XPA_STATUS_ACTIVE;
2348 }
2349
2350 /* join common code */
2351 goto end;
2352
2353 eof:
2354 /* on eof, free up comm */
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 /* join common code */
2365 goto end;
2366
2367
2368 error:
2369 FPRINTF((stderr, "%sXPAHandler ERROR: return code %d on %d\n",
2370 _sp, got, fd));
2371 /* add xpa back to list of active ones (but not comm) */
2372 XPAActive(xpa, comm, 1);
2373 /* xpa is no longer using this comm */
2374 xpa->comm = NULL;
2375 /* don't accidentally close ns on error */
2376 if( comm ){
2377 if( comm->ns )
2378 CommFree(xpa, comm, 0);
2379 else
2380 CommFree(xpa, comm, 1);
2381 comm = NULL;
2382 }
2383 /* join common code */
2384 goto end;
2385
2386 end:
2387 /* if a free was requested by the callback, do it now when its safe */
2388 if( xpa->status & XPA_STATUS_FREE ){
2389 XPAFree(xpa);
2390 }
2391 /* restore old value of comm */
2392 else{
2393 xpa->comm = ocomm;
2394 }
2395
2396 /* return the status */
2397 return(got);
2398 }
2399
2400 /*
2401 *----------------------------------------------------------------------------
2402 *
2403 * Routine: XPAMode
2404 *
2405 * Purpose: parse the mode string and set flags
2406 *
2407 * Returns: none
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 /* keyword routine requires an input buffer that can be modified */
2427 if( mode && *mode ){
2428 strncpy(s, mode, SZ_LINE-1);
2429 s[SZ_LINE-1] = '\0';
2430 }
2431 else
2432 goto error;
2433 /* look for the keyword=<value> string */
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 * Routine: XPAShortTimeout
2455 *
2456 * Purpose: return short (select, gets) timeout value
2457 *
2458 * Return: timeout in seconds
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 * Routine: XPALongTimeout
2476 *
2477 * Purpose: return long (fillbuf) timeout value
2478 *
2479 * Return: timeout in seconds
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 * Routine: XPAReceiveLTimeout
2497 *
2498 * Purpose: modify long timeout value for this process
2499 *
2500 * Returns: 0 for success, -1 for failure
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 * Routine: XPASendLTimeout
2546 *
2547 * Purpose: return the long timeout for this process
2548 *
2549 * Returns: 0 for success, -1 for failure
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 * Routine: XPAReceiveSTimeout
2578 *
2579 * Purpose: modify short timeout value for this process
2580 *
2581 * Returns: 0 for success, -1 for failure
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 * Routine: XPASendSTimeout
2627 *
2628 * Purpose: return the short timeout for this process
2629 *
2630 * Returns: 0 for success, -1 for failure
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 * Routine: XPAVerbosity
2659 *
2660 * Purpose: return verbosity value
2661 *
2662 * Return: verbosity value (0, 1, 2)
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 * Routine: XPASigusr1
2680 *
2681 * Purpose: return flag for using sigusr1
2682 *
2683 * Return: 0 or 1
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 * Routine: XPAInitEnv
2701 *
2702 * Purpose: initialize the xpa environment
2703 *
2704 * Return: none
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 /* determine the communication method */
2719 mtype=XPAMethod(NULL);
2720 /* enable access controls and port management */
2721 if( mtype != XPA_UNIX ){
2722 XPAAclNew(NULL, 0);
2723 XPAPortNew(NULL, 0);
2724 }
2725 /* set short (select,gets) timeout, if necessary */
2726 if( (s=(char *)getenv("XPA_SHORT_TIMEOUT")) != NULL )
2727 stimeout = atoi(s);
2728 /* set long (fillbuf) timeout, if necessary */
2729 if( (s=(char *)getenv("XPA_LONG_TIMEOUT")) != NULL )
2730 ltimeout = atoi(s);
2731 /* set xpans connect timeout, if necessary */
2732 if( (s=(char *)getenv("XPA_CONNECT_TIMEOUT")) != NULL )
2733 ctimeout = atoi(s);
2734 /* set verbosity level, if necessary */
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 /* check for acl enable/disable */
2746 if( (s=(char *)getenv("XPA_ACL")) != NULL )
2747 guseacl = istrue(s);
2748 /* check for timestamp on errors */
2749 if( (s=(char *)getenv("XPA_TIMESTAMP_ERRORS")) != NULL )
2750 etimestamp = istrue(s);
2751 /* check for xpans register flag */
2752 if( (s=(char *)getenv("XPA_NSREGISTER")) != NULL )
2753 nsregister = istrue(s);
2754 /* check for use of siguser1 */
2755 if( (s=(char *)getenv("XPA_SIGUSR1")) != NULL )
2756 sigusr1 = istrue(s);
2757 /* check for version checking */
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 /* check for io loop calling xpa */
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 /* make sure we have a temp dir */
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 /* create directory, if necessary */
2787 xmkdir(tmpdir, 0777);
2788 xchmod(tmpdir, 0777);
2789 #if HAVE_MINGW32==0
2790 /* Disable SIGPIPE so we do not die if the client dies.
2791 * Rather, we will get an EOF on reading or writing.
2792 */
2793 xsignal_sigpipe();
2794 #endif
2795 /* platform-dependent startup */
2796 xsocketstartup();
2797 }
2798 }
2799
2800 /*
2801 *----------------------------------------------------------------------------
2802 *
2803 * Routine: XPANSAdd
2804 *
2805 * Purpose: add this XPA to the name service
2806 *
2807 * Returns: 0 on success, -1 on failure
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 /* sanity check */
2830 if( !xpa )
2831 return(0);
2832
2833 /* handle special case of the name server itself -- its a known entry */
2834 if( !strcmp(xpa->name, XPANS_NAME) ){
2835 return(0);
2836 }
2837
2838 /* look for the proxy=<true|false> string */
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 /* open a connection to the name service */
2848 if( (ns=XPANSOpen(xpa, host, 1)) != NULL ){
2849 /* get the user name, from the environment */
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 /* this is a last resort */
2856 else if( (pw=getpwuid(geteuid())) )
2857 strncpy(username, pw->pw_name, SZ_LINE-1);
2858 #endif
2859 /* if nothing good has happened, make it "unknown" */
2860 if( *username == '\0' )
2861 strcpy(username, "unknown");
2862 /* null-terminate string */
2863 username[SZ_LINE-1] = '\0';
2864
2865 /* write the command to add this xpa */
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 /* get result */
2872 if( XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0 ){
2873 if( !strncmp(tbuf, "XPA$OK", 6) ){
2874 /* for proxy, we now must listen for xpa requests on this ns */
2875 if( !strcmp(cmd, "addproxy") && xpa ){
2876 if( CommNew(xpa, ns->fd, ns->ip, ns->port, ns->name, ns) ){
2877 /* one more proxy is using this name server */
2878 ns->nproxy += 1;
2879 }
2880 }
2881 else{
2882 /* one more access point is using this name server */
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 * Routine: XPANSDel
2905 *
2906 * Purpose: remove public knowledge of this XPA from the name service
2907 *
2908 * Returns: none
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 /* sanity check */
2929 if( !xpa )
2930 return(0);
2931
2932 /* handle special case of the name server itself -- its a known entry */
2933 if( xpa->name && !strcmp(xpa->name, XPANS_NAME) ){
2934 return(0);
2935 }
2936
2937 /* if there is no method, just return */
2938 if( (xpa->method == NULL) || (*xpa->method == '\0') ){
2939 return(0);
2940 }
2941
2942 /* look for the proxy=<true|false> string */
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 /* open a connection to the name service */
2952 if( (ns=XPANSOpen(xpa, host, -1)) != NULL ){
2953 /* write the command to delete this xpa */
2954 snprintf(tbuf, SZ_LINE, "%s %s\n", cmd, xpa->method);
2955 XPAPuts(xpa, ns->fd, tbuf, stimeout);
2956 /* get result */
2957 if( XPAGets(xpa, ns->fd, tbuf, SZ_LINE, stimeout) >0 ){
2958 if( !strncmp(tbuf, "XPA$OK", 6) ){
2959 /* one less access point is using this name server */
2960 ns->nxpa -= 1;
2961 /* if there are no more access points using this xpans, close it */
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 * Routine: XPAVersionCheck
2983 *
2984 * Purpose: check our version vs. xpans version
2985 *
2986 * Returns: -1,0,1 for our<=>xpans
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 /* return if not checking version */
3010 if( vercheck <=0 )
3011 return(0);
3012 /* if either does not exist, its a mismatch */
3013 if( !word(serv, s1, &ip1) || !word(nsv, s2, &ip2) )
3014 return(1);
3015 /* if strings are equal, versions are equal */
3016 if( !strcasecmp(s1, s2) )
3017 return(0);
3018
3019 /* format for version is maj.min.patch[be]beta */
3020 newdtable(".be");
3021 /* we check only the major and minor version for incompatibilities */
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 * Routine: XPAVersionWarn
3046 *
3047 * Purpose: warn about mismatched versions
3048 *
3049 * Returns: NONE
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 /* return if not checking version */
3063 if( vercheck <=0 )
3064 return;
3065
3066 /* output warning */
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 /* we did it */
3074 vercheck--;
3075 }
3076
3077 /*
3078 *----------------------------------------------------------------------------
3079 *
3080 *
3081 * Public Routines
3082 *
3083 *
3084 *----------------------------------------------------------------------------
3085 */
3086
3087 /*
3088 *----------------------------------------------------------------------------
3089 *
3090 * Routine: XPAMethod
3091 *
3092 * Purpose: return communication method type
3093 *
3094 * Returns: XPA__INET, XPA_UNIX
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 /* if no method is passed, we return the default method type */
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 /* otherwise, we analyze the input method to get the type */
3143 else{
3144 /* inet is ip:port, else unix filename */
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 * Routine: XPANSMethod
3160 *
3161 * Purpose: return string containing name server method
3162 *
3163 * Returns: name server method string
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 /* always null-terminate */
3194 nsmethod[SZ_LINE-1] = '\0';
3195 /* if flag, we want the XPA access ip and port, not the communication
3196 channel between xpa servers and the name service */
3197 if( flag ){
3198 if( (s=strrchr(nsmethod, ':')) != NULL ){
3199 /* this is where we will overwrite the port */
3200 t = s+1;
3201 /* get base port for default */
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 /* always null-terminate */
3228 nsmethod[SZ_LINE-1] = '\0';
3229 /* if flag is set, we are getting the XPA access file, not the
3230 socket file and we have to change the name slightly */
3231 if( flag ){
3232 /* replace the ending, if possible */
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 /* always null-terminate */
3248 nsmethod[SZ_LINE-1] = '\0';
3249 break;
3250 }
3251
3252 /* return the static method string */
3253 return(nsmethod);
3254 }
3255
3256 /*
3257 *----------------------------------------------------------------------------
3258 *
3259 * Routine: XPAParseName
3260 *
3261 * Purpose: split the xpaname into a class and name string
3262 *
3263 * Results: none
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 /* if nothing is passed to us, allow everything */
3284 if( (xpaname == NULL) || (*xpaname == '\0') ){
3285 strncpy(xclass, "*", len-1);
3286 strncpy(name, "*", len-1);
3287 return;
3288 }
3289
3290 /* split the xpaname into class and name */
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 * Routine: XPAParseIpPort
3315 *
3316 * Purpose: split the host into ip and port
3317 *
3318 * Results: none
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 /* make sure we have something to work with */
3336 if( (host == NULL) || (*host == '\0') )
3337 return(0);
3338
3339 /* parse up the string and look for a port number (which is essential) */
3340 s1 = xstrdup(host);
3341 /* if we have a ",", null it out since what comes after is aux info */
3342 if( (p1 = (char *)strchr(s1, ',')) != NULL ){
3343 *p1 = '\0';
3344 }
3345 /* if we have a ":", we will null it out (so that what comes before is
3346 the host name) and bump past it to point to the port */
3347 if( (p1 = (char *)strchr(s1, ':')) == NULL ){
3348 /* there is no ":", so the whole string is the port */
3349 p1 = s1;
3350 s2 = NULL;
3351 } else {
3352 /* null out ':' and bump port pointer */
3353 *p1 = '\0';
3354 p1++;
3355 s2 = s1;
3356 }
3357
3358 /* get port */
3359 p2 = NULL;
3360 if( p1 && !strcmp(p1, "$port") )
3361 *port = XPA_NSPORT;
3362 /* NB: port number might be followed by other stuff */
3363 else
3364 *port = (unsigned short)strtol(p1, &p2, 0);
3365 /* check for bad port number -- we lose */
3366 if( *port <=0 || (p1 == p2) || (p2 && (*p2 != '\0')) ){
3367 *ip = 0;
3368 *port = 0;
3369 got = 0;
3370 goto done;
3371 }
3372
3373 /* get ip */
3374 if( s2 && *s2 ){
3375 /* see if this already is a hex address in network byte order */
3376 *ip = strtoul16(s2, &s3);
3377 if( *s3 == '\0' ){
3378 got = 1;
3379 goto done;
3380 }
3381 }
3382 /* not hex or proxy -- convert ip string to an ip address */
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 * Routine: XPAParseUnixSocket
3400 *
3401 * Purpose: see if host is actually a unix socket file
3402 *
3403 * Results: 1 if its a socket, 0 otherwise
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 /* see if its a file in the right directory */
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 * Routine: XPAOK
3431 *
3432 * Purpose: send an XPA OK message to the client
3433 *
3434 * Returns: 0 on success, -1 on error
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 /* make sure we have a valid struct */
3451 if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) )
3452 return(-1);
3453
3454 /* send message, if necessary */
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 /* flag that there was a message sent for this xpa */
3465 xpa->comm->message = 1;
3466
3467 /* return status */
3468 return(status);
3469 }
3470
3471 /*
3472 *----------------------------------------------------------------------------
3473 *
3474 * Routine: XPATimestamp
3475 *
3476 * Purpose: generate string with current date/time
3477 *
3478 * Returns: time string (in static buffer)
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 * Routine: XPAError
3509 *
3510 * Purpose: send an XPA error message to the client
3511 *
3512 * Returns: none
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 /* make sure we have a valid struct */
3532 if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) )
3533 return(-1);
3534
3535 /* send message, if necessary */
3536 if( !(xpa_status(xpa) & XPA_STATUS_ACTIVE) || (xpa_ack(xpa) == 1) ){
3537 t = xstrdup(s);
3538 /* get rid of CR in message -- we add one at the end */
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 /* package up and write the message */
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 /* flag that there was a message sent for this xpa */
3558 xpa->comm->message = 1;
3559
3560 /* return status */
3561 return(status);
3562 }
3563
3564 /*
3565 *----------------------------------------------------------------------------
3566 *
3567 * Routine: XPAMessage
3568 *
3569 * Purpose: send an XPA message to the client
3570 *
3571 * Returns: 0 on success, -1 on error
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 /* make sure we have a valid struct */
3591 if( (xpa == NULL) || (xpa_cmdfd(xpa) <0) )
3592 return(-1);
3593
3594 /* send message, if necessary */
3595 if( !(xpa_status(xpa) & XPA_STATUS_ACTIVE) || (xpa_ack(xpa) == 1) ){
3596 t = xstrdup(s);
3597 /* get rid of CR in message -- we add one at the end */
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 /* package up and write the message */
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 /* flag that there was a message sent for this xpa */
3617 xpa->comm->message = 1;
3618
3619 /* return status */
3620 return(status);
3621 }
3622
3623 /*
3624 *----------------------------------------------------------------------------
3625 *
3626 * Routine: XPAArgvParamlist
3627 *
3628 * Purpose: generate a paramlist string from an argv
3629 *
3630 * Results: allocated paramlist string
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 /* get length of paramlist */
3649 for(plen=0, i=start; i<argc; i++){
3650 plen += (strlen(argv[i])+1);
3651 }
3652
3653 /* allocate enough space for it */
3654 if( (paramlist = (char *)xcalloc(plen+1, sizeof(char))) == NULL ){
3655 return(NULL);
3656 }
3657
3658 /* gather up the paramlist */
3659 for(i=start; i<argc; i++){
3660 strcat(paramlist, argv[i]);
3661 strcat(paramlist, " ");
3662 }
3663
3664 /* remove whitespace from beginning and end */
3665 nowhite(paramlist, paramlist);
3666
3667 /* return paramlist */
3668 return(paramlist);
3669 }
3670
3671 /*
3672 *----------------------------------------------------------------------------
3673 *
3674 * Routine: XPANSLookup
3675 *
3676 * Purpose: get name server matches
3677 *
3678 * Returns: number of matches
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 /* do some initialization */
3716 XPAInitEnv();
3717
3718 /* use "*" as default if no type was specified */
3719 if( (ttype == NULL) || (*ttype == '\0') )
3720 xtype = "*";
3721 /* type 'a' means we are only trying to access */
3722 else if( *ttype == 'a' )
3723 xtype = "*";
3724 else
3725 xtype = ttype;
3726
3727 /* special case of name server */
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 /* special case of a string containing ip:port -- avoid trip to ns */
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 /* special case of a string containing unix socket -- avoid trip to ns */
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 /* pre-allocate the various arrays to an absurd number */
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 /* open a connection to the name service */
3773 if( (ns=XPANSOpen(xpa, NULL, 0)) != NULL ){
3774 while( word(tname, lbuf, &lp) ){
3775 XPAParseName(lbuf, xclass, name, SZ_LINE);
3776 /* write the command to add this xpa */
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 /* read matches from the name server */
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 /* XPA$<result> signals end of input */
3789 if( !strncmp(tbuf, "XPA$", 4) )
3790 break;
3791 /* otherwise scan next line */
3792 if( sscanf(tbuf, "%s %s %s %s %s %s\n",
3793 xclass, name, type, method, user, info) != EOF ){
3794 /* make sure this entry is not in the current process
3795 (i.e., we can't ever xpa ourselves!) */
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 /* if this xpa is in the current process, skip it */
3804 if( own )
3805 continue;
3806 /* make sure we have enough space */
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 /* add this entry to the list */
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 /* if we did not add to an xpa record, close up here */
3824 if( xpa == NULL ){
3825 XPANSClose(NULL, ns);
3826 }
3827 }
3828 /* reallocate the exact number of buffers we have */
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 * Routine: XPANSClose
3849 *
3850 * Purpose: close connection to the name service
3851 *
3852 * Returns: none
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 /* remove from xpa list */
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 /* close comms associated with this ns */
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 /* close socket */
3897 if( ns->fd >=0 ){
3898 xclose(ns->fd);
3899 }
3900 /* free up space */
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 * Routine: XPANSKeepAlive
3912 *
3913 * Purpose: send a 1-byte keep-alive packet to each name server
3914 *
3915 * Returns: -1 on error, else 1
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 /* sanity check */
3932 if( !xpa )
3933 return(-1);
3934
3935 /* use default type, if none specified */
3936 if( !type )
3937 type=DEF_KA_TYPE;
3938
3939 /* send keep-alive to deserving xpans instances */
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 /* send as out of band data to avoid mixing with normal data stream */
3945 got = send(ns->fd, "\n", 1, MSG_OOB);
3946 #else
3947 /* cisco routers can clear the URG flag by default, so use in-band */
3948 got = send(ns->fd, "\n", 1, 0);
3949 #endif
3950 }
3951 }
3952 return(got);
3953 }
3954
3955 /*
3956 *----------------------------------------------------------------------------
3957 *
3958 * Routine: XPANew
3959 *
3960 * Purpose: add a new xpa name to a process to allow external
3961 * process to read/write data associated with this name.
3962 *
3963 * If the send callback is defined, it can send to an external process.
3964 * If the receive callback is defined, it can receive from an external process.
3965 *
3966 * Returns: xpa handle associated with this class.name or NULL
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 /* do some initialization */
4008 XPAInitEnv();
4009 /* init the list of reserved commands */
4010 XPAInitReserved();
4011
4012 /* we need a name, but no ":" allowed in the name */
4013 if( (name == NULL) || (*name == '\0') || strchr(name, ':') )
4014 return(NULL);
4015
4016 /* limit the size of the xclass and name designation */
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 /* we need either a send or a receive or both */
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 /* allocate xpa struct */
4036 if( (xpa = (XPA)xcalloc(1, sizeof(XPARec))) == NULL )
4037 return(NULL);
4038
4039 /* fill in the blanks */
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 /* set the value of the server big-endian-ness */
4049 xpa->sendian = XPAEndian() ? xstrdup("big") : xstrdup("little");
4050
4051 /* fill in send information */
4052 if( send_callback != NULL ){
4053 xpa->send_callback = send_callback;
4054 xpa->send_data = send_data;
4055 strcat(xpa->type, "g");
4056 /* process the mode string */
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 /* fill in receive information */
4063 if( rec_callback != NULL ){
4064 xpa->receive_callback = rec_callback;
4065 xpa->receive_data = rec_data;
4066 strcat(xpa->type, "s");
4067 /* process the mode string */
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 /* set up communication method */
4076 switch(mtype){
4077 case XPA_INET:
4078 /* open a socket and fill in socket information */
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 /* localhost only */
4088 if( use_localhost )
4089 sock_in.sin_addr.s_addr = htonl(gethostip("$localhost"));
4090 /* any address will do */
4091 else
4092 sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
4093 /* handle special case of xpans port */
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 /* bind to the ip:port */
4102 if( xbind(xpa->fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 )
4103 goto error;
4104 /* we now can determine which port the system assigned */
4105 if( getsockname(xpa->fd, (struct sockaddr *)&sock_in, &slen) < 0 )
4106 goto error;
4107 else{
4108 /* ip:port is the method */
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 /* open a socket and fill in socket information */
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 /* handle special case of xpans */
4127 if( !strcmp(xpa->name, XPANS_NAME) ){
4128 strcpy(tbuf, XPANSMethod(NULL, 1));
4129 }
4130 else{
4131 /* get filename part, composed of class and name and unique id */
4132 snprintf(tfile, SZ_LINE, "%s_%s.%d",
4133 xpa->xclass, xpa->name, (int)getpid());
4134 /* change "/" to "_" for filename */
4135 for(tfptr = tfile; *tfptr != '\0'; tfptr++){
4136 if( *tfptr == '/' )
4137 *tfptr = '_';
4138 }
4139 /* create full pathname */
4140 snprintf(tbuf, SZ_LINE, "%s/%s", tmpdir, tfile);
4141 }
4142 /* delete old copy */
4143 unlink (tbuf);
4144 strcpy(sock_un.sun_path, tbuf);
4145 /* unset umask so that everyone can read and write */
4146 oum = umask(0);
4147 /* bind to the file */
4148 got = xbind(xpa->fd, (struct sockaddr *)&sock_un, sizeof(sock_un));
4149 /* reset umask */
4150 umask(oum);
4151 /* now check for bind error */
4152 if( got < 0 )
4153 goto error;
4154 /* path is the method */
4155 xpa->method = xstrdup(tbuf);
4156 break;
4157 #endif
4158 default:
4159 goto error;
4160 }
4161
4162 /* listen for connections */
4163 if( listen(xpa->fd, XPA_MAXLISTEN) < 0 )
4164 goto error;
4165
4166 /* make sure we close on exec */
4167 xfcntl(xpa->fd, F_SETFD, FD_CLOEXEC);
4168
4169 /* add this xpa to end of list of xpas */
4170 XPAListAdd(&xpahead, xpa);
4171
4172 /* publish this entry to the world */
4173 if( nsregister )
4174 XPANSAdd(xpa, NULL, NULL);
4175
4176 /* make it active */
4177 XPAActive(xpa, NULL, 1);
4178
4179 #if NO_AUTOMATIC_HAVE_ATEXIT
4180 /* register XPA atexit funtion */
4181 if( !atexitinit ){
4182 atexit(_XPAAtExit);
4183 atexitinit = getpid();
4184 }
4185 #endif
4186
4187 /* return good news */
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 * Routine: XPAFree
4202 *
4203 * Purpose: free up alloc'ed memory in the XPA record structure
4204 *
4205 * Results: 0 on success, -1 for failure
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 /* if status is active, just flag eventual need to free and exit */
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 /* call the primitive routine */
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 * Routine: XPAInfoNew
4240 *
4241 * Purpose: add a new xpa name to a process to allow external
4242 * process to send info messages
4243 *
4244
4245 * Returns: xpa handle associated with this class.name or NULL
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 /* do some initialization */
4278 XPAInitEnv();
4279 /* init the list of reserved commands */
4280 XPAInitReserved();
4281
4282 /* we need a name, but no ":" allowed in the name */
4283 if( (name == NULL) || (*name == '\0') || strchr(name, ':') )
4284 return(NULL);
4285
4286 /* we need an info callback */
4287 if( info_callback == NULL ){
4288 if( verbosity ){
4289 fprintf(stderr, "XPA$ERROR: requires info callback\n");
4290 }
4291 return(NULL);
4292 }
4293
4294 /* allocate xpa struct */
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 /* fill in the blanks */
4301 if( xclass != NULL )
4302 xpa->xclass = xstrdup(xclass);
4303 else
4304 xpa->xclass = xstrdup("*");
4305 xpa->name = xstrdup(name);
4306
4307 /* fill in send information */
4308 xpa->info_callback = info_callback;
4309 xpa->info_data = info_data;
4310 strcat(xpa->type, "i");
4311 /* process the mode string */
4312 xpa->info_mode = XPA_DEF_MODE_INFO;
4313 XPAMode(info_mode, &(xpa->info_mode), "acl", XPA_MODE_ACL, 1);
4314
4315 /* set up communication method */
4316 switch(mtype){
4317 case XPA_INET:
4318 /* open a socket and fill in socket information */
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 /* localhost only */
4328 if( use_localhost )
4329 sock_in.sin_addr.s_addr = htonl(gethostip("$localhost"));
4330 /* any address will do */
4331 else
4332 sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
4333 sock_in.sin_port = htons(XPAPort(xpa));
4334 /* bind to the ip:port */
4335 if( xbind(xpa->fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 )
4336 goto error;
4337 /* we now can determine which port the system assigned */
4338 if( getsockname(xpa->fd, (struct sockaddr *)&sock_in, &slen) < 0 )
4339 goto error;
4340 else{
4341 /* ip:port is the method */
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 /* get filename part, composed of class and name and unique id */
4351 snprintf(tfile, SZ_LINE, "%s_%s.%d",
4352 xpa->xclass, xpa->name, (int)getpid());
4353 /* change "/" to "_" for filename */
4354 for(tfptr = tfile; *tfptr != '\0'; tfptr++){
4355 if( *tfptr == '/' )
4356 *tfptr = '_';
4357 }
4358 /* create full pathname */
4359 snprintf(tbuf, SZ_LINE, "%s/%s", tmpdir, tfile);
4360 /* delete old copy */
4361 unlink (tbuf);
4362 /* open a socket and fill in socket information */
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 /* unset umask so that everyone can read and write */
4371 oum = umask(0);
4372 /* bind to the file */
4373 got = xbind(xpa->fd, (struct sockaddr *)&sock_un, sizeof(sock_un));
4374 /* reset umask */
4375 umask(oum);
4376 /* now check for bind error */
4377 if( got < 0 )
4378 goto error;
4379 /* path is the method */
4380 xpa->method = xstrdup(tbuf);
4381 break;
4382 #endif
4383 default:
4384 goto error;
4385 }
4386
4387 /* listen for connections */
4388 if( listen(xpa->fd, XPA_MAXLISTEN) < 0 )
4389 goto error;
4390
4391 /* make sure we close on exec */
4392 xfcntl(xpa->fd, F_SETFD, FD_CLOEXEC);
4393
4394 /* add this xpa to end of list of xpas */
4395 XPAListAdd(&xpahead, xpa);
4396
4397 /* publish this entry to the world */
4398 if( nsregister )
4399 XPANSAdd(xpa, NULL, NULL);
4400
4401 /* make it active */
4402 XPAActive(xpa, NULL, 1);
4403
4404 #if NO_AUTOMATIC_HAVE_ATEXIT
4405 /* register XPA atexit funtion */
4406 if( !atexitinit ){
4407 atexit(_XPAAtExit);
4408 atexitinit = getpid();
4409 }
4410 #endif
4411
4412 /* return good news */
4413 return(xpa);
4414
4415 error:
4416 XPAFree(xpa);
4417 return(NULL);
4418 }
4419
4420 /*
4421 *----------------------------------------------------------------------------
4422 *
4423 * Routine: XPAPoll
4424 *
4425 * Purpose: non-blocking handling of XPA access points
4426 * timeout in millisecs, but if negative, no timeout is used
4427 *
4428 * Returns: number of requests processed (if maxreq >=0)
4429 * number of requests pending (if maxreq <0)
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 /* negative value means just block and wait */
4455 else{
4456 tvp = NULL;
4457 }
4458 FD_ZERO(&readfds);
4459 if( XPAAddSelect(NULL, &readfds) ){
4460 sgot = xselect(swidth, &readfds, NULL, NULL, tvp);
4461 /* error -- what should we do? */
4462 if( sgot < 0 ){
4463 if( xerrno == EINTR )
4464 goto again;
4465 if( verbosity ){
4466 perror("XPAPoll() select");
4467 }
4468 exit(1);
4469 }
4470 /* timeout -- just return */
4471 else if( sgot == 0 )
4472 ;
4473 /* finally ... something to do */
4474 else{
4475 /* if maxreq < 0, just return how many are ready */
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 * Routine: XPAMainLoop
4489 *
4490 * Purpose: non-X programs event loop for handling XPA
4491 *
4492 * Returns: none
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 /* error -- what should we do? */
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 /* can't happen, since we have no timeout */
4525 else if( sgot == 0 )
4526 ;
4527 /* finally ... something to do */
4528 else
4529 got += XPAProcessSelect(&readfds, 0);
4530 FD_ZERO(&readfds);
4531 }
4532 return(got);
4533 }
4534
4535 /*
4536 *----------------------------------------------------------------------------
4537 *
4538 * Routine: XPASleep
4539 *
4540 * Purpose: sleep for specified milliseconds
4541 *
4542 * Returns: none
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 * Routine: XPAListHead
4567 *
4568 * Purpose: semi-public routine to return the head of the xpa list
4569 *
4570 * Results: XPA list pointer on success
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 * Routine: XPATmpdir
4588 *
4589 * Purpose: semi-public routine to return the tmpdir value
4590 *
4591 * Results: tmpdir
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 * Routine: XPASetBuf
4609 *
4610 * Purpose: set the return buffer in a server send callback
4611 * (use mainly by tcl to transfer tcl buffers to C)
4612 *
4613 * Returns: 0 on success, -1 on failure
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 /* sanity check */
4629 if( !xpa || !xpa->comm )
4630 return(-1);
4631 /* xcopy >0 => make a copy of the data, else just assign */
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 * Routine: XPASetBuf
4649 *
4650 * Purpose: set the return buffer in a server send callback
4651 * (use mainly by tcl to transfer tcl buffers to C)
4652 *
4653 * Returns: 0 on success, -1 on failure
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 /* sanity check */
4668 if( !xpa || !xpa->comm ) return(-1);
4669 /* set the free routine and data pointer */
4670 xpa->comm->myfree = myfree;
4671 xpa->comm->myfree_ptr = myfree_ptr;
4672 return(0);
4673 }
4674
4675 /*
4676 *----------------------------------------------------------------------------
4677 *
4678 * Routine: XPACleanup
4679 *
4680 * Purpose: make valgrind happy by freeing memory
4681 *
4682 * Results: none
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 }