Debian Codegraph @

xpa/xpans.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 #if HAVE_LIBPTHREAD
8 #include <pthread.h>
9 static pthread_mutex_t xpans_mutex = PTHREAD_MUTEX_INITIALIZER;
10 #endif
11
12 #define MAX_ERRORS 5
13
14 static int doproxy=0;
15 static unsigned int localhost_ip=0;
16
17 extern char *optarg;
18 extern int optind;
19
20 typedef struct entrec{
21 struct entrec *next;
22 char *method;
23 char *xclass;
24 char *name;
25 char *type;
26 char *user;
27 char *info;
28 } *Entry, EntryRec;
29
30 typedef struct reqrec{
31 struct reqrec *next;
32 int sock;
33 unsigned int ip;
34 int port;
35 Entry entry;
36 } *Req, ReqRec;
37
38 static char *helpbuf = "xpans commands:\nhelp\t\t\t\t# print this help message\nlist\t\t\t\t# list all entries\nlookup class:name type user\t# lookup entries of this type and user\n";
39
40 static int keepalive=0;
41 static int ksec=0;
42 static int mtype = 0;
43 static int nentry = 0;
44 static int exconn=0;
45 static int sock=-1;
46 static char *logfile=NULL;
47 static time_t lastt=(time_t)0;
48 static time_t curt=(time_t)0;
49 static FILE *securefp=NULL;
50 static Req reqhead=NULL;
51
52 static int LookupReq _PRx((Req xreq, char *lbuf, int flag));
53 static int ListReq _PRx((Req xreq, int flag));
54 static void HelpReq _PRx((Req xreq, int flag));
55 #ifdef __STDC__
56 static void SecureLog(char *format, ...);
57 #else
58 static void SecureLog();
59 #endif
60
61 #if HAVE_LIBPTHREAD
62 /*
63 *----------------------------------------------------------------------------
64 *
65 * Routine: doxpaloop
66 *
67 * Purpose: start up XPAMainLoop in another thread
68 *
69 * Returns: NONE
70 *
71 *----------------------------------------------------------------------------
72 */
73 #ifdef ANSI_FUNC
74 void *doxpaloop(void *arg)
75 #else
76 void *doxpaloop(arg)
77 void *arg;
78 #endif
79 {
80 XPAMainLoop();
81 return (void *)NULL;
82 }
83 #endif
84
85 /*
86 *----------------------------------------------------------------------------
87 *
88 * Routine: receive_proxy
89 *
90 * Purpose: receive callback for a proxy request
91 *
92 * Returns: 0 for success, -1 for failure
93 *
94 *----------------------------------------------------------------------------
95 */
96 #ifdef ANSI_FUNC
97 static int
98 receive_proxy (void *client_data, void *call_data, char *paramlist,
99 char *buf, int len)
100 #else
101 int receive_proxy(client_data, call_data, paramlist, buf, len)
102 void *client_data;
103 void *call_data;
104 char *paramlist;
105 char *buf;
106 int len;
107 #endif
108 {
109 XPA xpa = (XPA)call_data;
110 char *xtemplate;
111 char *mode;
112 char *err;
113 char *s;
114 char xmode[SZ_LINE];
115 char tbuf[SZ_LINE];
116 int fd=0;
117 int pfd=-1;
118 int got=0;
119 Req req=NULL;
120
121 xtemplate = xpa->comm->target;
122 mode = xpa->comm->info;
123 fd = xpa->comm->datafd;
124
125 /* get proxy fd and associated xpans request struct */
126 if( mode ){
127 strcpy(xmode, mode);
128 if( keyword(xmode, "nsproxy", tbuf, SZ_LINE) ){
129 pfd = strtol(tbuf, &s, 0);
130 if( s != tbuf ){
131 for(req=reqhead; req!=NULL; req=req->next){
132 if( req->sock == pfd ){
133 break;
134 }
135 }
136 }
137 }
138 }
139 if( pfd < 0 ){
140 snprintf(tbuf, SZ_LINE, "invalid or missing proxy fd");
141 XPAError(xpa, tbuf);
142 return(-1);
143 }
144 else if( req == NULL ){
145 snprintf(tbuf, SZ_LINE, "could not find xpans fd for proxy fd %d", pfd);
146 XPAError(xpa, tbuf);
147 return(-1);
148 }
149
150 FPRINTF((stderr, "%sreceive_proxy: fd=%d xtmpl=%s mode=%s paramlist=%s\n",
151 _sp, xpa->comm->cmdfd, xtemplate, mode,
152 paramlist?paramlist:"NONE"));
153
154 if( XPASetFd(xpa, xtemplate, paramlist, mode, fd, NULL, &err, 1) ){
155 /* display errors and free up strings */
156 if( err != NULL ){
157 XPAError(xpa, err);
158 xfree(err);
159 got = -1;
160 }
161 }
162 else{
163 got = -1;
164 }
165
166 FPRINTF((stderr, "%sreceive_proxy done\n", _sp));
167 return(got);
168 }
169
170 /*
171 *----------------------------------------------------------------------------
172 *
173 * Routine: send_proxy
174 *
175 * Purpose: send callback for a proxy request
176 *
177 * Returns: 0 for success, -1 for failure
178 *
179 *----------------------------------------------------------------------------
180 */
181 #ifdef ANSI_FUNC
182 static int
183 send_proxy (void *client_data, void *call_data, char *paramlist,
184 char **buf, size_t *len)
185 #else
186 static int send_proxy(client_data, call_data, paramlist, buf, len)
187 void *client_data;
188 void *call_data;
189 char *paramlist;
190 char **buf;
191 size_t *len;
192 #endif
193 {
194 XPA xpa = (XPA)call_data;
195 char *xtemplate;
196 char *mode;
197 char *err;
198 char *s;
199 char xmode[SZ_LINE];
200 char tbuf[SZ_LINE];
201 int fd=0;
202 int pfd=-1;
203 int got=0;
204 Req req=NULL;
205
206 xtemplate = xpa->comm->target;
207 mode = xpa->comm->info;
208 fd = xpa->comm->datafd;
209
210 /* get proxy fd and associated xpans request struct */
211 if( mode ){
212 strcpy(xmode, mode);
213 if( keyword(xmode, "nsproxy", tbuf, SZ_LINE) ){
214 pfd = strtol(tbuf, &s, 0);
215 if( s != tbuf ){
216 for(req=reqhead; req!=NULL; req=req->next){
217 if( req->sock == pfd ){
218 break;
219 }
220 }
221 }
222 }
223 }
224 if( pfd < 0 ){
225 snprintf(tbuf, SZ_LINE, "invalid or missing proxy fd");
226 XPAError(xpa, tbuf);
227 return(-1);
228 }
229 else if( req == NULL ){
230 snprintf(tbuf, SZ_LINE, "could not find xpans fd for proxy fd %d", pfd);
231 XPAError(xpa, tbuf);
232 return(-1);
233 }
234
235 FPRINTF((stderr, "%ssend_proxy: fd=%d xtemplate=%s mode=%s paramlist=%s\n",
236 _sp, xpa->comm->cmdfd, xtemplate, mode, paramlist?paramlist:"NONE"));
237
238 if( XPAGetFd(xpa, xtemplate, paramlist, mode, &fd, NULL, &err, 1) ){
239 /* display errors and free up strings */
240 if( err != NULL ){
241 XPAError(xpa, err);
242 xfree(err);
243 got = -1;
244 }
245 }
246 else{
247 got = -1;
248 }
249
250 FPRINTF((stderr, "%ssend_proxy done\n", _sp));
251 return(got);
252 }
253
254 /*
255 *----------------------------------------------------------------------------
256 *
257 * Routine: receive_cb
258 *
259 * Purpose: receive callback for XPA access point
260 *
261 * Returns: 0 for success, -1 for failure
262 *
263 *----------------------------------------------------------------------------
264 */
265 #ifdef ANSI_FUNC
266 static int
267 receive_cb (void *client_data, void *call_data, char *paramlist,
268 char *buf, size_t len)
269 #else
270 int receive_cb(client_data, call_data, paramlist, buf, len)
271 void *client_data;
272 void *call_data;
273 char *paramlist;
274 char *buf;
275 size_t len;
276 #endif
277 {
278 XPA xpa = (XPA)call_data;
279 char tbuf[SZ_LINE];
280
281 /* if target is not xpans, we have a proxy request */
282 snprintf(tbuf, SZ_LINE, "%s:%s", XPANS_CLASS, XPANS_NAME);
283 if( strcmp(xpa->comm->target, tbuf) ){
284 if( doproxy )
285 return(receive_proxy(client_data, call_data, paramlist, buf, len));
286 else{
287 XPAError(xpa, "proxy requests not enabled in this xpans");
288 return(-1);
289 }
290 }
291
292 /* nothing to do for normal receive callback */
293 XPAError(xpa, "no receive function defined for xpans");
294 return(-1);
295 }
296
297 /*
298 *----------------------------------------------------------------------------
299 *
300 * Routine: send_cb
301 *
302 * Purpose: send callback for XPA access point
303 *
304 * Returns: 0 for success, -1 for failure
305 *
306 *----------------------------------------------------------------------------
307 */
308 #ifdef ANSI_FUNC
309 static int
310 send_cb (void *client_data, void *call_data, char *paramlist,
311 char **buf, size_t *len)
312 #else
313 static int send_cb(client_data, call_data, paramlist, buf, len)
314 void *client_data;
315 void *call_data;
316 char *paramlist;
317 char **buf;
318 size_t *len;
319 #endif
320 {
321 XPA xpa = (XPA)call_data;
322 Req req;
323 char tbuf[SZ_LINE];
324 char ebuf[SZ_LINE];
325 char tmpl[SZ_LINE];
326 char type[SZ_LINE];
327 char users[SZ_LINE];
328 int got=0;
329 int wp=0;
330
331 /* if target is not xpans, we have a proxy request */
332 snprintf(tbuf, SZ_LINE, "%s:%s", XPANS_CLASS, XPANS_NAME);
333 if( strcmp(xpa->comm->target, tbuf) ){
334 if( doproxy )
335 return(send_proxy(client_data, call_data, paramlist, buf, len));
336 else{
337 XPAError(xpa, "proxy requests not enabled in this xpans");
338 return(-1);
339 }
340 }
341
342 if( paramlist && *paramlist )
343 SecureLog("xpaget from host %x:%d (%s): %s",
344 xpa->comm->cmdip, xpa->comm->cmdport,
345 getiphost(xpa->comm->cmdip),
346 (paramlist && *paramlist)?paramlist:"<no params>");
347 else
348 SecureLog("xpaget from host %x:%d (%s)",
349 xpa->comm->cmdip, xpa->comm->cmdport,
350 getiphost(xpa->comm->cmdip));
351 if( (req = (Req)xcalloc(1, sizeof(ReqRec))) == NULL )
352 return(-1);
353 if( xpa_datafd(xpa) >= 0 ){
354 req->sock = xpa_datafd(xpa);
355 }
356
357 #if HAVE_LIBPTHREAD
358 /* lock the mutex before processing a request */
359 if( doproxy >= 2 ) pthread_mutex_lock(&xpans_mutex);
360 #endif
361
362 /* execute the appropriate routine */
363 if( paramlist && *paramlist && word(paramlist, tbuf, &wp) ){
364 if( !strcmp(tbuf, "list") ){
365 ListReq(req, 0);
366 }
367 else if( !strcmp(tbuf, "help") ){
368 HelpReq(req, 0);
369 }
370 else if( !strcmp(tbuf, "lookup") ){
371 if( word(paramlist, tmpl, &wp ) ){
372 /* look for type */
373 if( !word(paramlist, type, &wp ) )
374 strcpy(type, XPA_ACLS);
375 /* look for users */
376 if( !word(paramlist, users, &wp ) )
377 strcpy(users, "*");
378 snprintf(tbuf, SZ_LINE, "%s %s %s", tmpl, type, users);
379 LookupReq(req, tbuf, 0);
380 }
381 else{
382 strcpy(ebuf,
383 "XPA$ERROR 'lookup' requires class:name [type] [user]\n");
384 XPAPuts(NULL, req->sock, ebuf, XPALongTimeout());
385 got = -1;
386 }
387 }
388 else{
389 snprintf(ebuf, SZ_LINE, "XPA$ERROR unknown command '%s'\n", tbuf);
390 XPAPuts(NULL, req->sock, ebuf, XPALongTimeout());
391 got = -1;
392 }
393 }
394 else {
395 ListReq(req, 0);
396 }
397
398 #if HAVE_LIBPTHREAD
399 /* unlock the mutex */
400 if( doproxy >= 2 ) pthread_mutex_unlock(&xpans_mutex);
401 #endif
402
403 /* clean up */
404 if( req ) xfree(req);
405 return(got);
406 }
407
408 /*
409 *----------------------------------------------------------------------------
410 *
411 * Routine: Log
412 *
413 * Purpose: write all names to a backup log
414 *
415 * Returns: none
416 *
417 *----------------------------------------------------------------------------
418 */
419 #ifdef ANSI_FUNC
420 static void
421 Log (void)
422 #else
423 static void Log()
424 #endif
425 {
426 FILE *fp;
427 Req req;
428 Entry entry;
429
430 if( !logfile )
431 return;
432 if( !strcasecmp(logfile, "stdout") )
433 fp = stdout;
434 else if( (fp=fopen(logfile, "w")) == NULL )
435 return;
436 for(req=reqhead; req!=NULL; req=req->next){
437 for(entry=req->entry; entry!=NULL; entry=entry->next){
438 fprintf(fp, "# add %s %s %s %s %s\n",
439 entry->method, entry->xclass,
440 entry->name, entry->type, entry->user);
441 /* last one */
442 if( entry->next == NULL )
443 fprintf(fp, "xpaset -p %s -nsconnect\n", entry->method);
444 }
445 }
446 if( fp != stdout )
447 fclose(fp);
448 }
449
450 /*
451 *----------------------------------------------------------------------------
452 *
453 * Routine: SecureLog
454 *
455 * Purpose: write security info to a backup log
456 *
457 * Returns: none
458 *
459 *----------------------------------------------------------------------------
460 */
461 #ifdef __STDC__
462 void SecureLog(char *format, ...)
463 {
464 char sbuf[SZ_LINE];
465 time_t t;
466 va_list args;
467 va_start(args, format);
468 #else
469 void SecureLog(va_alist) va_dcl
470 {
471 char *format;
472 char sbuf[SZ_LINE];
473 time_t t;
474 va_list args;
475
476 va_start(args);
477 format = va_arg(args, char *);
478 #endif
479 if( securefp == NULL )
480 return;
481 t = time(NULL);
482 vsnprintf(sbuf, SZ_LINE, format, args);
483 fprintf(securefp, "%s", sbuf);
484 fprintf(securefp, "\t%s", ctime(&t));
485 fflush(securefp);
486 }
487
488 /*
489 *----------------------------------------------------------------------------
490 *
491 * Routine: SplitArg
492 *
493 * Purpose: split the specified argument by changing a ":" to a space
494 * splitting is done in place
495 *
496 * Returns: none
497 *
498 *----------------------------------------------------------------------------
499 */
500 #ifdef ANSI_FUNC
501 static void
502 SplitArg (char *buf, int arg)
503 #else
504 static void SplitArg(buf, arg)
505 char *buf;
506 int arg;
507 #endif
508 {
509 int i;
510 char *s;
511
512 /* point to beginning of buffer */
513 s = buf;
514 /* skip over previous args */
515 for(i=0; i<arg; i++){
516 /* skip up to white space */
517 while( *s && !isspace((int)*s) )
518 s++;
519 /* skip over white space to next arg */
520 while( *s && isspace((int)*s) )
521 s++;
522 }
523 /* we now are pointing at the arg in question.
524 look for a ':' (up to next white space) and change to space */
525 while( *s && !isspace((int)*s) ){
526 if( *s == ':' ){
527 *s = ' ';
528 break;
529 }
530 else
531 s++;
532 }
533 }
534
535 /*
536 *----------------------------------------------------------------------------
537 *
538 * Routine: NewEntry
539 *
540 * Purpose: allocate a new XPA entry
541 *
542 * Returns: entry struct
543 *
544 *----------------------------------------------------------------------------
545 */
546 #ifdef ANSI_FUNC
547 static int
548 NewEntry (Req req,
549 char *method, char *xclass, char *name,
550 char *type, char *user, char *info)
551 #else
552 static int NewEntry(req, method, xclass, name, type, user, info)
553 Req req;
554 char *method;
555 char *xclass;
556 char *name;
557 char *type;
558 char *user;
559 char *info;
560 #endif
561 {
562 Entry entry, cur;
563 Req xreq;
564
565 /* don't duplicate with any other entry */
566 for(xreq=reqhead; xreq!=NULL; xreq=xreq->next){
567 for(entry=xreq->entry; entry!=NULL; entry=entry->next){
568 if( !strcmp(entry->method, method) &&
569 !strcmp(entry->xclass, xclass) &&
570 !strcmp(entry->name, name) &&
571 !strcmp(entry->type, type) &&
572 !strcmp(entry->user, user) &&
573 !strcmp(entry->info, info) )
574 return(1);
575 }
576 }
577
578 /* allocate new entry */
579 if( (entry = (Entry)xcalloc(1, sizeof(EntryRec))) == NULL )
580 return(-1);
581
582 /* fill in the blanks */
583 entry->xclass = xstrdup(xclass);
584 entry->name = xstrdup(name);
585 entry->method = xstrdup(method);
586 entry->type = xstrdup(type);
587 entry->user = xstrdup(user);
588 entry->info = xstrdup(info);
589
590 FPRINTF((stderr, "%sNewEntry: %s %s %s %s %s %s\n", _sp,
591 xclass, name, method, type, user, info));
592
593 /* add this to end of the list */
594 if( req->entry == NULL ){
595 req->entry = entry;
596 }
597 else{
598 for(cur=req->entry; cur->next!=NULL; cur=cur->next)
599 ;
600 cur->next = entry;
601 }
602
603 /* inc the total number of entries */
604 nentry++;
605
606 /* return the news */
607 return(0);
608 }
609
610 /*
611 *----------------------------------------------------------------------------
612 *
613 * Routine: FreeEntry
614 *
615 * Purpose: free up an XPA entry
616 *
617 * Returns: none
618 *
619 *----------------------------------------------------------------------------
620 */
621 #ifdef ANSI_FUNC
622 static void
623 FreeEntry (Req req, Entry entry)
624 #else
625 static void FreeEntry(req, entry)
626 Req req;
627 Entry entry;
628 #endif
629 {
630 Entry cur;
631
632 /* remove this entry from the list so it can't be found */
633 if( entry == req->entry ){
634 req->entry = req->entry->next;
635 }
636 else{
637 for(cur=req->entry; cur!=NULL; cur=cur->next){
638 if( entry == cur->next ){
639 cur->next = entry->next;
640 break;
641 }
642 }
643 }
644
645 FPRINTF((stderr, "%sFreeEntry: %s %s\n", _sp, entry->xclass, entry->name));
646
647 /* now free this struct */
648 if( entry->method )
649 xfree(entry->method);
650 if( entry->xclass )
651 xfree(entry->xclass);
652 if( entry->name )
653 xfree(entry->name);
654 if( entry->type )
655 xfree(entry->type);
656 if( entry->user )
657 xfree(entry->user);
658 if( entry->info )
659 xfree(entry->info);
660 xfree(entry);
661 }
662
663 /*
664 *----------------------------------------------------------------------------
665 *
666 * Routine: DelEntry
667 *
668 * Purpose: Delete an XPA entry
669 *
670 * Returns: none
671 *
672 *----------------------------------------------------------------------------
673 */
674 #ifdef ANSI_FUNC
675 static int
676 DelEntry (Req req, char *method)
677 #else
678 static int DelEntry(req, method)
679 Req req;
680 char *method;
681 #endif
682 {
683 Entry cur, tcur;
684 int got=-1;
685
686 for(cur=req->entry; cur!=NULL; ){
687 tcur = cur->next;
688 if( ((method == NULL) || (*method == '\0')) ||
689 (!strcmp(method, "@") && (*cur->method == '@')) ||
690 !strcmp(cur->method, method) ){
691 FreeEntry(req, cur);
692 got = 0;
693 }
694 cur = tcur;
695 }
696 return(got);
697 }
698
699 /*
700 *----------------------------------------------------------------------------
701 *
702 * Routine: FirewallEntry
703 *
704 * Purpose: Correct the method to take a firewall into account
705 * we do this by taking the ip from the socket packet instead
706 * of the specified ip, if they differ
707 *
708 * Returns: none
709 *
710 *----------------------------------------------------------------------------
711 */
712 #ifdef ANSI_FUNC
713 static void
714 FirewallEntry (Req req, char *method)
715 #else
716 static void FirewallEntry(req, method)
717 Req req;
718 char *method;
719 #endif
720 {
721 unsigned int ip;
722 unsigned short port;
723
724 if( mtype != XPA_INET )
725 return;
726 if( XPAParseIpPort(method, &ip, &port) ){
727 if( (ip != req->ip) && (req->ip != localhost_ip) ){
728 SecureLog("firewall %d: changing ip from %x to %x",
729 req->sock, ip, req->ip);
730 snprintf(method, SZ_LINE, "%x:%d", req->ip, port);
731 }
732 }
733 }
734
735 /*
736 *----------------------------------------------------------------------------
737 *
738 * Routine: NewReq
739 *
740 * Purpose: allocate a new XPA request struct
741 *
742 * Returns: Req struct
743 *
744 *----------------------------------------------------------------------------
745 */
746 #ifdef ANSI_FUNC
747 static Req
748 NewReq (int sock, unsigned int ip, int port)
749 #else
750 static Req NewReq(sock, ip, port)
751 int sock;
752 unsigned int ip;
753 int port;
754 #endif
755 {
756 Req req;
757 Req cur;
758
759 if( (req = (Req)xcalloc(1, sizeof(ReqRec))) == NULL )
760 return(NULL);
761
762 /* fill in the blanks */
763 req->sock = sock;
764 req->ip = ip;
765 req->port = port;
766
767 /* add this to end of the list */
768 if( reqhead == NULL ){
769 reqhead = req;
770 }
771 else{
772 for(cur=reqhead; cur->next!=NULL; cur=cur->next)
773 ;
774 cur->next = req;
775 }
776 /* return the news */
777 return(req);
778 }
779
780 /*
781 *----------------------------------------------------------------------------
782 *
783 * Routine: FreeReq
784 *
785 * Purpose: free up an XPA request entry
786 *
787 * Returns: none
788 *
789 *----------------------------------------------------------------------------
790 */
791 #ifdef ANSI_FUNC
792 static void
793 FreeReq (Req req)
794 #else
795 static void FreeReq(req)
796 Req req;
797 #endif
798 {
799 Req cur;
800 /* remove this entry from the list so it can't be found */
801 if( req == reqhead ){
802 reqhead = req->next;
803 }
804 else{
805 for(cur=reqhead; cur!=NULL; cur=cur->next){
806 if( req == cur->next ){
807 cur->next = req->next;
808 break;
809 }
810 }
811 }
812 /* close the communication channel */
813 close(req->sock);
814 /* now free this struct */
815 xfree(req);
816 }
817
818 /*
819 *----------------------------------------------------------------------------
820 *
821 * Routine: AddReq
822 *
823 * Purpose: add an XPA entry to the list
824 *
825 * Returns: none
826 *
827 *----------------------------------------------------------------------------
828 */
829 #ifdef ANSI_FUNC
830 static void
831 AddReq (Req req, char *lbuf, int flag)
832 #else
833 static void AddReq(req, lbuf, flag)
834 Req req;
835 char *lbuf;
836 int flag;
837 #endif
838 {
839 int got;
840 char xclass[SZ_LINE];
841 char name[SZ_LINE];
842 char method[SZ_LINE];
843 char omethod[SZ_LINE];
844 char type[SZ_LINE];
845 char user[SZ_LINE];
846 char tbuf[SZ_LINE];
847
848 SplitArg(lbuf, 1);
849 got = sscanf(lbuf, "%s %s %s %s %s", method, xclass, name, type, user);
850 if( got == 5 ){
851 /* fix method if we can determine its been through a firewall */
852 strcpy(omethod, method);
853 FirewallEntry(req, method);
854 if( !strcmp(omethod, method) )
855 strcpy(tbuf, method);
856 else
857 snprintf(tbuf, SZ_LINE, "%s,%s", method, omethod);
858 /* add the new entry */
859 got = NewEntry(req, tbuf, xclass, name, type, user, XPA_DEF_CLIENT_INFO);
860 Log();
861 if( flag ){
862 switch(got){
863 case -1:
864 XPAPuts(NULL, req->sock, "XPA$ERROR could not add entry\n",
865 XPALongTimeout());
866 break;
867 case 0:
868 XPAPuts(NULL, req->sock, "XPA$OK\n", XPALongTimeout());
869 break;
870 case 1:
871 XPAPuts(NULL, req->sock, "XPA$EXISTS entry already exists\n",
872 XPALongTimeout());
873 break;
874 default:
875 XPAPuts(NULL, req->sock, "XPA$ERROR could not add entry\n",
876 XPALongTimeout());
877 break;
878 }
879 }
880 }
881 else{
882 strcpy(tbuf,
883 "XPA$ERROR 'add' requires 4 args: ip:port class:name type user\n");
884 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
885 }
886 }
887
888 /*
889 *----------------------------------------------------------------------------
890 *
891 * Routine: AddProxyReq
892 *
893 * Purpose: add an XPA proxy entry to the list
894 *
895 * Returns: none
896 *
897 *----------------------------------------------------------------------------
898 */
899 #ifdef ANSI_FUNC
900 static void
901 AddProxyReq (Req req, char *lbuf, int flag)
902 #else
903 static void AddProxyReq(req, lbuf, flag)
904 Req req;
905 char *lbuf;
906 int flag;
907 #endif
908 {
909 int got=0;
910 char xclass[SZ_LINE];
911 char name[SZ_LINE];
912 char method[SZ_LINE];
913 char omethod[SZ_LINE];
914 char type[SZ_LINE];
915 char user[SZ_LINE];
916 char info[SZ_LINE];
917 char tbuf[SZ_LINE];
918
919 /* make sure we are accepting proxy requests */
920 if( !doproxy ){
921 strcpy(tbuf,
922 "XPA$ERROR: proxy requests not enabled in this xpans\n");
923 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
924 return;
925 }
926
927 SplitArg(lbuf, 1);
928 got = sscanf(lbuf, "%s %s %s %s %s", method, xclass, name, type, user);
929 if( got == 5 ){
930 if( XPAMethod(method) == XPA_INET ){
931 /* fix method if we can determine its been through a firewall */
932 strcpy(omethod, method);
933 FirewallEntry(req, method);
934 if( !strcmp(omethod, method) )
935 snprintf(tbuf, SZ_LINE, "@%s", method);
936 else
937 snprintf(tbuf, SZ_LINE, "@%s,%s", method, omethod);
938 }
939 else{
940 strcpy(tbuf,
941 "XPA$ERROR 'proxy' requires INET method\n");
942 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
943 return;
944 }
945 /* save info */
946 snprintf(info, SZ_LINE, "nsproxy=%d,ns=(%s,%s,%s,%s),dofork=true",
947 req->sock, xclass, name, method, omethod);
948 /* add the new entry */
949 got = NewEntry(req, tbuf, xclass, name, type, user, info);
950 Log();
951 if( flag ){
952 switch(got){
953 case -1:
954 XPAPuts(NULL, req->sock, "XPA$ERROR could not add entry\n",
955 XPALongTimeout());
956 break;
957 case 0:
958 XPAPuts(NULL, req->sock, "XPA$OK\n", XPALongTimeout());
959 break;
960 case 1:
961 XPAPuts(NULL, req->sock, "XPA$EXISTS entry already exists\n",
962 XPALongTimeout());
963 break;
964 default:
965 XPAPuts(NULL, req->sock, "XPA$ERROR could not add entry\n",
966 XPALongTimeout());
967 break;
968 }
969 }
970 }
971 else{
972 strcpy(tbuf,
973 "XPA$ERROR 'proxy' requires 4 args: ip:port class:name type user\n");
974 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
975 }
976 }
977
978 /*
979 *----------------------------------------------------------------------------
980 *
981 * Routine: DelReq
982 *
983 * Purpose: delete an XPA entry from the list
984 *
985 * Returns: none
986 *
987 *----------------------------------------------------------------------------
988 */
989 #ifdef ANSI_FUNC
990 static void
991 DelReq (Req req, char *lbuf, int flag)
992 #else
993 static void DelReq(req, lbuf, flag)
994 Req req;
995 char *lbuf;
996 int flag;
997 #endif
998 {
999 int got=0;
1000 char method[SZ_LINE];
1001 char omethod[SZ_LINE];
1002 char tbuf[SZ_LINE];
1003
1004 if( lbuf != NULL ){
1005 if( sscanf(lbuf, "%s", method) == 1 ){
1006 /* fix method if we can determine its been through a firewall */
1007 strcpy(omethod, method);
1008 FirewallEntry(req, method);
1009 if( !strcmp(omethod, method) )
1010 strcpy(tbuf, method);
1011 else
1012 snprintf(tbuf, SZ_LINE, "%s,%s", method, omethod);
1013 got = DelEntry(req, tbuf);
1014 Log();
1015 if( flag ){
1016 if( got == 0 )
1017 XPAPuts(NULL, req->sock, "XPA$OK\n", XPALongTimeout());
1018 else
1019 XPAPuts(NULL, req->sock, "XPA$ERROR entry does not exist\n",
1020 XPALongTimeout());
1021 }
1022 }
1023 else{
1024 strcpy(tbuf, "XPA$ERROR 'del' requires 1 arg: ip:port\n");
1025 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
1026 }
1027 }
1028 else{
1029 /* connection is closed -- free all entries for req, and delete req */
1030 FPRINTF((stderr, "%sxpans request really died: %d\n", _sp, req->sock));
1031 DelEntry(req, NULL);
1032 FreeReq(req);
1033 Log();
1034 }
1035 }
1036
1037 /*
1038 *----------------------------------------------------------------------------
1039 *
1040 * Routine: DelProxyReq
1041 *
1042 * Purpose: delete an XPA entry from the list
1043 *
1044 * Returns: none
1045 *
1046 *----------------------------------------------------------------------------
1047 */
1048 #ifdef ANSI_FUNC
1049 static void
1050 DelProxyReq (Req req, char *lbuf, int flag)
1051 #else
1052 static void DelProxyReq(req, lbuf, flag)
1053 Req req;
1054 char *lbuf;
1055 int flag;
1056 #endif
1057 {
1058 int got=0;
1059 char method[SZ_LINE];
1060 char omethod[SZ_LINE];
1061 char tbuf[SZ_LINE];
1062
1063 /* make sure we are accepting proxy requests */
1064 if( !doproxy ){
1065 strcpy(tbuf,
1066 "XPA$ERROR: proxy requests not enabled in this xpans\n");
1067 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
1068 return;
1069 }
1070
1071 if( lbuf != NULL ){
1072 if( sscanf(lbuf, "%s", method) == 1 ){
1073 /* fix method if we can determine its been through a firewall */
1074 strcpy(omethod, method);
1075 FirewallEntry(req, method);
1076 if( !strcmp(omethod, method) )
1077 strcpy(tbuf, method);
1078 else
1079 snprintf(tbuf, SZ_LINE, "@%s,%s", method, omethod);
1080 /* free the specified entry */
1081 got = DelEntry(req, tbuf);
1082 Log();
1083 if( flag ){
1084 if( got == 0 )
1085 XPAPuts(NULL, req->sock, "XPA$OK\n", XPALongTimeout());
1086 else
1087 XPAPuts(NULL, req->sock, "XPA$ERROR entry does not exist\n",
1088 XPALongTimeout());
1089 }
1090 }
1091 else{
1092 strcpy(tbuf, "XPA$ERROR 'del' requires 1 arg: ip:port\n");
1093 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
1094 }
1095 }
1096 else{
1097 /* connection is closed -- free all entries for req, and delete req */
1098 DelEntry(req, "@");
1099 /* if this is the last entry, delete the request struct as well */
1100 if( req->entry == NULL ){
1101 FreeReq(req);
1102 }
1103 Log();
1104 }
1105 }
1106
1107 /*
1108 *----------------------------------------------------------------------------
1109 *
1110 * Routine: LookupReq
1111 *
1112 * Purpose: lookup a template in the XPA list
1113 *
1114 * Returns: number of matched lookups
1115 *
1116 *----------------------------------------------------------------------------
1117 */
1118 #ifdef ANSI_FUNC
1119 static int
1120 LookupReq (Req xreq, char *lbuf, int flag)
1121 #else
1122 static int LookupReq(xreq, lbuf, flag)
1123 Req xreq;
1124 char *lbuf;
1125 int flag;
1126 #endif
1127 {
1128 int i;
1129 int nrec;
1130 int got=0;
1131 int domethod=0;
1132 int slen=1024;
1133 int *lens;
1134 char args[4][SZ_LINE];
1135 char ctmpl[SZ_LINE];
1136 char ntmpl[SZ_LINE];
1137 char tbuf[SZ_LINE];
1138 char type[SZ_LINE];
1139 char users[SZ_LINE];
1140 char method[SZ_LINE];
1141 char *user;
1142 char *usercopy;
1143 char **strings;
1144 char *tstring;
1145 Req req;
1146 Entry entry;
1147
1148 SplitArg(lbuf, 0);
1149 nrec = sscanf(lbuf, "%s %s %s %s", args[0], args[1], args[2], args[3]);
1150 switch(nrec){
1151 case 0:
1152 goto error;
1153 case 1:
1154 strcpy(ctmpl, "*");
1155 strcpy(ntmpl, args[0]);
1156 strcpy(type, "*");
1157 strcpy(users, "*");
1158 if( *args[0] == '@' ){
1159 strcpy(method, args[0]);
1160 domethod = 1;
1161 }
1162 break;
1163 case 2:
1164 strcpy(ctmpl, "*");
1165 strcpy(ntmpl, args[0]);
1166 strcpy(type, args[1]);
1167 strcpy(users, "*");
1168 if( *args[0] == '@' ){
1169 snprintf(method, SZ_LINE, "%s:%s", args[0], args[1]);
1170 domethod = 1;
1171 }
1172 break;
1173 case 3:
1174 strcpy(ctmpl, "*");
1175 strcpy(ntmpl, args[0]);
1176 strcpy(type, args[1]);
1177 strcpy(users, args[2]);
1178 if( *args[0] == '@' ){
1179 strcpy(method, args[0]);
1180 domethod = 1;
1181 }
1182 break;
1183 case 4:
1184 strcpy(ctmpl, args[0]);
1185 strcpy(ntmpl, args[1]);
1186 strcpy(type, args[2]);
1187 strcpy(users, args[3]);
1188 if( *args[0] == '@' ){
1189 snprintf(method, SZ_LINE, "%s:%s", args[0], args[1]);
1190 domethod = 1;
1191 }
1192 break;
1193 case 5:
1194 goto error;
1195 }
1196 strings = (char **)xmalloc(slen * sizeof(char *));
1197 lens = (int *)xmalloc(slen * sizeof(int));
1198 lens[0] = 0;
1199 for(req=reqhead; req!=NULL; req=req->next){
1200 for(entry=req->entry; entry!=NULL; entry=entry->next){
1201 /* check method or class:name */
1202 if( domethod ){
1203 if( strcmp(entry->method, method) )
1204 continue;
1205 }
1206 else{
1207 if( !tmatch(entry->xclass, ctmpl) || !tmatch(entry->name, ntmpl) )
1208 continue;
1209 }
1210 /* check type */
1211 if( strcmp(type, "*") && !strpbrk(entry->type, type) )
1212 continue;
1213 /* check user */
1214 if( !strcmp(users, "*") || !strcmp(users, entry->user) ){
1215 user = entry->user;
1216 }
1217 else{
1218 user = NULL;
1219 usercopy = (char *)xstrdup(users);
1220 for(user=(char *)strtok(usercopy, " :;,");
1221 user!=NULL;
1222 user=(char *)strtok(NULL," :;,")){
1223 if ( !strcasecmp(user, entry->user) ){
1224 break;
1225 }
1226 }
1227 if( usercopy )
1228 xfree(usercopy);
1229 }
1230 if( !user )
1231 continue;
1232 /* made it through all checks! */
1233 if( domethod || (*entry->method == '@') ){
1234 snprintf(tbuf, SZ_LINE, "%s %s %s %s %s %s\n",
1235 entry->xclass, entry->name,
1236 entry->type, XPANSMethod(NULL,1), entry->user, entry->info);
1237 FPRINTF((stderr, "%sLookupReq: method lookup found:\n%s", _sp, tbuf));
1238 }
1239 else{
1240 snprintf(tbuf, SZ_LINE, "%s %s %s %s %s %s\n",
1241 entry->xclass, entry->name,
1242 entry->type, entry->method, entry->user, entry->info);
1243 FPRINTF((stderr, "%sLookupReq: class/name lookup got:\n%s",
1244 _sp, tbuf));
1245 }
1246 if( got >= (slen-2) ){
1247 slen *= 2;
1248 strings = (char **)xrealloc(strings, slen * sizeof(char *));
1249 lens = (int *)xrealloc(lens, slen * sizeof(int));
1250 }
1251 strings[got] = xstrdup(tbuf);
1252 lens[got+1] = lens[got] + strlen(strings[got]);
1253 got++;
1254 }
1255 }
1256 if( flag ){
1257 strings[got] = xstrdup("XPA$OK\n");
1258 lens[got+1] = lens[got] + strlen(strings[got]);
1259 got++;
1260 }
1261 /* write one buffer load: we have to avoid multiple writes in a row
1262 because tcp_delay buffers these (i.e., the Nagle algorithm) and kills
1263 the performance */
1264 if( got > 0 ){
1265 tstring = (char *)xcalloc(lens[got]+1, sizeof(char));
1266 for(i=0; i<got; i++){
1267 if( strings[i] ){
1268 strcpy(&(tstring[lens[i]]), strings[i]);
1269 }
1270 }
1271 XPAPutBuf(NULL, xreq->sock, tstring, lens[got], XPALongTimeout());
1272 for(i=0; i<got; i++){
1273 if( strings[i] )
1274 xfree(strings[i]);
1275 }
1276 if( tstring )
1277 xfree(tstring);
1278 }
1279 if( strings )
1280 xfree(strings);
1281 if( lens )
1282 xfree(lens);
1283 return(got);
1284
1285 error:
1286 strcpy(tbuf,
1287 "XPA$ERROR xpans 'lookup' requires: class:name [type] [user]\n");
1288 XPAPuts(NULL, xreq->sock, tbuf, XPALongTimeout());
1289 return(0);
1290 }
1291
1292 /*
1293 *----------------------------------------------------------------------------
1294 *
1295 * Routine: HelpReq
1296 *
1297 * Purpose: send help message
1298 *
1299 * Returns: none
1300 *
1301 *----------------------------------------------------------------------------
1302 */
1303 #ifdef ANSI_FUNC
1304 static void
1305 HelpReq (Req xreq, int flag)
1306 #else
1307 static void HelpReq(xreq, flag)
1308 Req xreq;
1309 int flag;
1310 #endif
1311 {
1312 /* XPAPuts(NULL, xreq->sock, helpbuf, XPALongTimeout()); */
1313 XPAPutBuf(NULL, xreq->sock, helpbuf, strlen(helpbuf), XPALongTimeout());
1314 if( flag )
1315 XPAPuts(NULL, xreq->sock, "XPA$OK\n", XPALongTimeout());
1316 }
1317
1318 /*
1319 *----------------------------------------------------------------------------
1320 *
1321 * Routine: ListReq
1322 *
1323 * Purpose: list all entries in the XPA list
1324 *
1325 * Returns: number of entries listed
1326 *
1327 *----------------------------------------------------------------------------
1328 */
1329 #ifdef ANSI_FUNC
1330 static int
1331 ListReq (Req xreq, int flag)
1332 #else
1333 static int ListReq(xreq, flag)
1334 Req xreq;
1335 int flag;
1336 #endif
1337 {
1338 int got=0;
1339 char tbuf[SZ_LINE];
1340 Req req;
1341 Entry entry;
1342
1343 for(req=reqhead; req!=NULL; req=req->next){
1344 for(entry=req->entry; entry!=NULL; entry=entry->next){
1345 snprintf(tbuf, SZ_LINE, "%s %s %s %s %s\n",
1346 entry->xclass, entry->name,
1347 entry->type, entry->method, entry->user);
1348 XPAPuts(NULL, xreq->sock, tbuf, XPALongTimeout());
1349 got++;
1350 }
1351 }
1352 if( flag )
1353 XPAPuts(NULL, xreq->sock, "XPA$OK\n", XPALongTimeout());
1354 return(got);
1355 }
1356
1357 /*
1358 *----------------------------------------------------------------------------
1359 *
1360 * Routine: StatusReq
1361 *
1362 * Purpose: send short "alive" message to inquiring server
1363 *
1364 * Returns: NONE
1365 *
1366 *----------------------------------------------------------------------------
1367 */
1368 #ifdef ANSI_FUNC
1369 static void
1370 StatusReq (Req xreq)
1371 #else
1372 static void StatusReq(xreq)
1373 Req xreq;
1374 #endif
1375 {
1376 XPAPuts(NULL, xreq->sock, "XPA$OK\n", XPALongTimeout());
1377 }
1378
1379 /*
1380 *----------------------------------------------------------------------------
1381 *
1382 * Routine: VersionReq
1383 *
1384 * Purpose: send version info to inquiring server
1385 *
1386 * Returns: NONE
1387 *
1388 *----------------------------------------------------------------------------
1389 */
1390 #ifdef ANSI_FUNC
1391 static void
1392 VersionReq (Req xreq, char *vstr)
1393 #else
1394 static void VersionReq(xreq, vstr)
1395 Req xreq;
1396 char *vstr;
1397 #endif
1398 {
1399 int ip=0;
1400 int dowarn=0;
1401 char tbuf[SZ_LINE];
1402
1403 /* version check: server version should be <= our version */
1404 if( word(vstr, tbuf, &ip) ){
1405 dowarn = (XPAVersionCheck(XPA_VERSION, tbuf)>0);
1406 }
1407 else{
1408 strcpy(tbuf, "unknown/pre-2.1");
1409 dowarn = 1;
1410 }
1411 if( dowarn )
1412 XPAVersionWarn(tbuf, XPA_VERSION);
1413 snprintf(tbuf, SZ_LINE, "XPA$VERSION %s\n", XPA_VERSION);
1414 XPAPuts(NULL, xreq->sock, tbuf, XPALongTimeout());
1415 }
1416
1417 #ifdef ANSI_FUNC
1418 void
1419 usage (char *s)
1420 #else
1421 void usage(s)
1422 char *s;
1423 #endif
1424 {
1425 fprintf(stderr, "\n");
1426 fprintf(stderr, "usage: xpans [-h] [-e] [-k msec] [-l log] [-p port] [-s slog] [-P n] \n");
1427 fprintf(stderr, "switches:\n");
1428 fprintf(stderr, "\t-h\tprint this message\n");
1429 fprintf(stderr, "\t-e\texit when there are no more XPA connections\n");
1430 fprintf(stderr, "\t-k msec\tsend keepalive messages every n sec\n");
1431 fprintf(stderr, "\t-l file\tlog data base entries to specified file\n");
1432 fprintf(stderr, "\t-p port\tlisten for connections on specified port\n");
1433 fprintf(stderr, "\t-s file\tlog security info to specified file\n");
1434 fprintf(stderr, "\t-P 1|2\taccept proxy requests (1) using separate thread (2) \n");
1435 fprintf(stderr, "\t--version display version and exit\n");
1436 fprintf(stderr, "\n(version: %s)\n", XPA_VERSION);
1437 exit(1);
1438 }
1439
1440 /*
1441 *----------------------------------------------------------------------------
1442 *
1443 * Routine: KeepAliveReq
1444 *
1445 * Purpose: send keep alive to all open connections
1446 *
1447 * Returns: number of entries processed
1448 *
1449 *----------------------------------------------------------------------------
1450 */
1451 #ifdef ANSI_FUNC
1452 static int
1453 KeepAliveReq (void)
1454 #else
1455 static int KeepAliveReq()
1456 #endif
1457 {
1458 int got=0;
1459 Req req;
1460
1461 /* return if keepalive is turned off */
1462 if( !keepalive )
1463 return(0);
1464 /* get current time */
1465 curt = time(NULL);
1466 /* if keep alive time has passed, send keep alive messages */
1467 if( (curt - lastt) > ksec ){
1468 for(req=reqhead; req!=NULL; req=req->next){
1469 send(req->sock, " ", 1, MSG_OOB);
1470 got++;
1471 }
1472 lastt = curt;
1473 }
1474 return(got);
1475 }
1476
1477 #ifdef ANSI_FUNC
1478 int
1479 main (int argc, char **argv)
1480 #else
1481 int
1482 main(argc, argv)
1483 int argc;
1484 char **argv;
1485 #endif
1486 {
1487 char lbuf[SZ_LINE];
1488 char tbuf[SZ_LINE];
1489 char cmd[SZ_LINE];
1490 char method[SZ_LINE];
1491 int c;
1492 int sock2;
1493 int width;
1494 int got;
1495 int wp;
1496 int cmdport;
1497 int sval;
1498 int nerr=0;
1499 int oum;
1500 int llen;
1501 int reuse_addr=1;
1502 int keep_alive=1;
1503 unsigned int ip;
1504 unsigned int cmdip;
1505 unsigned short port=0;
1506 socklen_t slen=sizeof(struct sockaddr_in);
1507 struct sockaddr_in sock_in;
1508 struct sockaddr_in sock_in2;
1509 #if HAVE_LIBPTHREAD
1510 pthread_t tid;
1511 #endif
1512 #if HAVE_SYS_UN_H
1513 struct sockaddr_un sock_un;
1514 struct sockaddr_un sock_un2;
1515 char lockfile[SZ_LINE];
1516 int lockfd=-1;
1517 struct flock lock;
1518 #endif
1519 fd_set readfds;
1520 struct timeval tv;
1521 struct timeval *tvp;
1522 Req req, treq;
1523 XPA xpa;
1524
1525 /* display version and exit, if necessary */
1526 if( (argc == 2) && !strcmp(argv[1], "--version") ){
1527 fprintf(stderr, "%s\n", XPA_VERSION);
1528 exit(0);
1529 }
1530
1531 /* init the XPA environment */
1532 XPAInitEnv();
1533
1534 /* Disable SIGPIPE so we do not die if the client dies.
1535 * Rather, we will get an EOF on reading or writing.
1536 */
1537 xsignal_sigpipe();
1538
1539 /* we want the args in the same order in which they arrived, and
1540 gnu getopt sometimes changes things without this */
1541 putenv("POSIXLY_CORRECT=true");
1542
1543 /* process command line args */
1544 while ((c = getopt(argc, argv, "ef:hk:l:p:P:s:")) != -1){
1545 switch(c){
1546 case 'h':
1547 usage(argv[0]);
1548 exit(0);
1549 case 'e':
1550 exconn = 1;
1551 break;
1552 case 'f':
1553 #if HAVE_SYS_UN_H
1554 /* method is unix with specified file */
1555 putenv(xstrdup("XPA_METHOD=unix"));
1556 snprintf(tbuf, SZ_LINE, "XPA_NSUNIX=%s", optarg);
1557 putenv(xstrdup(tbuf));
1558 break;
1559 #else
1560 fprintf(stderr, "XPA$ERROR: UNIX sockets not supported on this host\n");
1561 exit(1);
1562 break;
1563 #endif
1564 case 'k':
1565 fprintf(stderr,
1566 "XPA$KEEPALIVE: URG TCP data is changed by some Cisco routers into in-band data.\n");
1567 fprintf(stderr,
1568 "Encountering such a router will break the keep-alive function and may break your\n");
1569 fprintf(stderr, "XPA server as well. Proceed with caution!\n");
1570 keepalive = 1;
1571 ksec = atoi(optarg);
1572 if( ksec <= 0 )
1573 ksec = 1;
1574 break;
1575 case 'l':
1576 logfile = optarg;
1577 break;
1578 case 'p':
1579 /* method is inet with specified port */
1580 putenv(xstrdup("XPA_METHOD=inet"));
1581 snprintf(tbuf, SZ_LINE, "XPA_NSINET=$host:%d", atoi(optarg));
1582 putenv(xstrdup(tbuf));
1583 break;
1584 case 'P':
1585 if( istrue(optarg) )
1586 doproxy = 1;
1587 else if( isfalse(optarg) )
1588 doproxy = 0;
1589 else
1590 doproxy = atoi(optarg);
1591 if( doproxy < 0 )
1592 doproxy = 0;
1593 #if HAVE_LIBPTHREAD==0
1594 if( doproxy >= 2 ){
1595 fprintf(stderr,
1596 "XPA$ERROR: xpans not built with thread support on this host\n");
1597 exit(1);
1598 }
1599 #endif
1600 break;
1601 case 's':
1602 if( !strcasecmp(optarg, "stdout") ){
1603 securefp = stdout;
1604 }
1605 else if( (securefp=fopen(optarg, "w")) == NULL ){
1606 perror("securefp");
1607 exit(1);
1608 }
1609 break;
1610 }
1611 }
1612
1613 /* get default ip and port */
1614 strcpy(method, XPANSMethod(NULL, 0));
1615 mtype = XPAMethod(method);
1616 localhost_ip = gethostip("$localhost");
1617
1618 /* start secure logging, if necessary */
1619 SecureLog("Starting xpans: %s", method);
1620 /* set up communication method */
1621 switch(mtype){
1622 case XPA_INET:
1623 XPAParseIpPort(method, &ip, &port);
1624 if( (sock = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
1625 if( XPAVerbosity() > 1 )
1626 perror("xpans socket()");
1627 exit(1);
1628 }
1629 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1630 (char *)&keep_alive, sizeof(keep_alive));
1631 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1632 (char *)&reuse_addr, sizeof(reuse_addr));
1633 memset((char *)&sock_in, 0, sizeof(sock_in));
1634 sock_in.sin_family = AF_INET;
1635 /* localhost only */
1636 if( ip == localhost_ip )
1637 sock_in.sin_addr.s_addr = htonl(ip);
1638 /* any address will do */
1639 else
1640 sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
1641 sock_in.sin_port = htons(port);
1642 /* bind to a port -- an error indicates that another xpans is running */
1643 if( xbind(sock, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
1644 if( XPAVerbosity() > 1 )
1645 perror("xpans bind()");
1646 exit(1);
1647 }
1648 SecureLog("Opening INET socket: %d", sock);
1649 break;
1650 #if HAVE_SYS_UN_H
1651 case XPA_UNIX:
1652 /* with unix sockets, we lock a special file to signal any new xpans
1653 process that it is not needed. This behavior mimicks the bind()
1654 error with inet sockets */
1655 snprintf(lockfile, SZ_LINE, "%s-lock", method);
1656 if( (lockfd=open(lockfile, O_CREAT|O_RDWR, 0666)) >=0 ){
1657 lock.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
1658 lock.l_start = 0; /* byte offset, relative to l_whence */
1659 lock.l_whence = SEEK_SET; /* SEEK_SET, SEE_CUR, SEEK_END */
1660 lock.l_len = 1; /* #bytes (0 means to EOF) */
1661 /* if we can't get the lock, there is an xpans already running */
1662 if( xfcntl(lockfd, F_SETLK, &lock) < 0 ){
1663 close(lockfd);
1664 if( XPAVerbosity() > 1 )
1665 fprintf(stderr, "XPA$ERROR: xpans already running\n");
1666 exit(1);
1667 }
1668 }
1669 unlink(method);
1670 if( (sock = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
1671 if( XPAVerbosity() > 1 )
1672 perror("xpans socket()");
1673 exit(1);
1674 }
1675 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1676 (char *)&keep_alive, sizeof(keep_alive));
1677 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1678 (char *)&reuse_addr, sizeof(reuse_addr));
1679 memset((char *)&sock_un, 0, sizeof(sock_un));
1680 sock_un.sun_family = AF_UNIX;
1681 strcpy(sock_un.sun_path, method);
1682 /* unset umask so that everyone can read and write */
1683 oum = umask(0);
1684 /* bind to a port */
1685 if( xbind(sock, (struct sockaddr *)&sock_un, sizeof(sock_un)) < 0 ){
1686 if( XPAVerbosity() > 1 )
1687 perror("xpans bind()");
1688 exit(1);
1689 }
1690 /* reset umask */
1691 umask(oum);
1692 SecureLog("opened UNIX socket: %d", sock);
1693 break;
1694 #endif
1695 default:
1696 break;
1697 }
1698
1699 /* listen for connections */
1700 if( listen(sock, XPA_MAXLISTEN) < 0 ){
1701 if( XPAVerbosity() > 1 )
1702 perror("xpans listen()");
1703 exit(1);
1704 }
1705 /* make sure we close on exec */
1706 xfcntl(sock, F_SETFD, FD_CLOEXEC);
1707
1708 /* add an XPA access point for external processing */
1709 if( !(xpa=XPANew(XPANS_CLASS, XPANS_NAME, helpbuf,
1710 send_cb, NULL, "fillbuf=false",
1711 receive_cb, NULL, "fillbuf=false")) ){
1712 if( XPAVerbosity() > 1 )
1713 fprintf(stderr, "XPA$ERROR: failed to create access point for xpans\n");
1714 exit(1);
1715 }
1716 SecureLog("XPA access point: %s", xpa->name);
1717
1718 /* init select parameters */
1719 width = FD_SETSIZE;
1720
1721 #if HAVE_LIBPTHREAD
1722 /* start up new thread for XPA main loop, if necessary */
1723 if( doproxy >= 2 ){
1724 if( pthread_create(&tid, NULL, doxpaloop, NULL) != 0 ){
1725 fprintf(stderr,
1726 "XPA$ERROR: can't create new thread for XPA handler in xpans\n");
1727 exit(1);
1728 }
1729 }
1730 #endif
1731
1732 /* enter processing loop */
1733 while( 1 ){
1734 /* reset flag */
1735 FD_ZERO(&readfds);
1736 /* add main listening socket */
1737 FD_SET(sock, &readfds);
1738 /* add request lines */
1739 for(got=0, req=reqhead; req!=NULL; req=req->next){
1740 FD_SET(req->sock, &readfds);
1741 got++;
1742 }
1743 /* add XPA selections */
1744 if( doproxy < 2 ) XPAAddSelect(NULL, &readfds);
1745 /* if we once had entries but do not have them now, we might be done */
1746 if( exconn && nentry && !got ){
1747 goto done;
1748 }
1749 if( keepalive ){
1750 tv.tv_sec = ksec;
1751 tv.tv_usec = 0;
1752 tvp = &tv;
1753 }
1754 else{
1755 tvp = NULL;
1756 }
1757 /* wait for next request */
1758 sval = xselect(width, &readfds, NULL, NULL, tvp);
1759 if( sval > 0 ){
1760 #if HAVE_LIBPTHREAD
1761 /* lock the mutex before processing a reqest */
1762 if( doproxy >= 2 ) pthread_mutex_lock(&xpans_mutex);
1763 #endif
1764 /* process a new major request */
1765 if( FD_ISSET(sock, &readfds) ){
1766 /* new request */
1767 switch(mtype){
1768 case XPA_INET:
1769 while( 1 ){
1770 slen = sizeof(struct sockaddr_in);
1771 if((sock2=xaccept(sock, (struct sockaddr *)&sock_in2, &slen))>=0){
1772 cmdip = ntohl(sock_in2.sin_addr.s_addr);
1773 cmdport = ntohs(sock_in2.sin_port);
1774 /* make sure we close on exec */
1775 xfcntl(sock2, F_SETFD, FD_CLOEXEC);
1776 NewReq(sock2, cmdip, cmdport);
1777 SecureLog("accept %d: %x:%d (%s)",
1778 sock2, cmdip, cmdport, getiphost(cmdip));
1779 break;
1780 }
1781 else{
1782 if( errno == EINTR )
1783 continue;
1784 else
1785 break;
1786 }
1787 }
1788 break;
1789 #if HAVE_SYS_UN_H
1790 case XPA_UNIX:
1791 while( 1 ){
1792 slen = sizeof(struct sockaddr_un);
1793 if( (sock2=xaccept(sock, (struct sockaddr *)&sock_un2, &slen))>=0){
1794 /* make sure we close on exec */
1795 xfcntl(sock2, F_SETFD, FD_CLOEXEC);
1796 NewReq(sock2, 0, 0);
1797 SecureLog("accept from local socket");
1798 break;
1799 }
1800 else{
1801 if( errno == EINTR )
1802 continue;
1803 else
1804 break;
1805 }
1806 }
1807 break;
1808 #endif
1809 default:
1810 break;
1811 }
1812 }
1813 /* process an existing request line */
1814 for(got=0, req=reqhead; req!=NULL; ){
1815 treq = req->next;
1816 if( FD_ISSET(req->sock, &readfds) ){
1817 FPRINTF((stderr, "%sxpans existing request: %d\n", _sp, req->sock));
1818 if( XPAGets(NULL, req->sock, lbuf, SZ_LINE, XPAShortTimeout()) >0 ){
1819 llen = strlen(lbuf) - 1;
1820 if( (lbuf[llen]) == '\n' ){
1821 /* ignore a single new-line, its a keep-alive message */
1822 if( llen == 0){
1823 FPRINTF((stderr, "%sxpans ignoring keep-alive\n", _sp));
1824 req = treq;
1825 continue;
1826 }
1827 /* else remove new-line */
1828 else{
1829 lbuf[llen] = '\0';
1830 }
1831 }
1832 FPRINTF((stderr, "%sxpans request: %s\n", _sp, lbuf));
1833 SecureLog("cmd %d: %s", req->sock, lbuf);
1834 /* process another request from this process */
1835 wp = 0;
1836 /* get first token: command */
1837 if( !word(lbuf, cmd, &wp) ){
1838 strcpy(tbuf, "XPA$ERROR no xpans command specified\n");
1839 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
1840 }
1841 else{
1842 /* skip white space */
1843 while( isspace((int)lbuf[wp]) )
1844 wp++;
1845 /* lookup the command */
1846 if( !strcmp(cmd, "status") ){
1847 StatusReq(req);
1848 }
1849 else if( !strcmp(cmd, "version") ){
1850 VersionReq(req, &(lbuf[wp]));
1851 }
1852 else if( !strcmp(cmd, "add") ){
1853 AddReq(req, &(lbuf[wp]), 1);
1854 }
1855 else if( !strcmp(cmd, "addproxy") ){
1856 AddProxyReq(req, &(lbuf[wp]), 1);
1857 }
1858 else if( !strcmp(cmd, "del") ){
1859 DelReq(req, &(lbuf[wp]), 1);
1860 }
1861 else if( !strcmp(cmd, "delproxy") ){
1862 DelProxyReq(req, &(lbuf[wp]), 1);
1863 }
1864 else if( !strcmp(cmd, "help") ){
1865 HelpReq(req, 1);
1866 }
1867 else if( !strcmp(cmd, "list") ){
1868 ListReq(req, 1);
1869 }
1870 else if( !strcmp(cmd, "lookup") ){
1871 LookupReq(req, &(lbuf[wp]), 1);
1872 }
1873 else{
1874 SecureLog("ignoring unknown command");
1875 snprintf(tbuf, SZ_LINE,
1876 "XPA$ERROR unknown xpans request: %s\n", lbuf);
1877 XPAPuts(NULL, req->sock, tbuf, XPALongTimeout());
1878 }
1879 }
1880 }
1881 else{
1882 /* process dies: delete all entries associated with this sock */
1883 FPRINTF((stderr, "%sxpans request died: %d\n", _sp, req->sock));
1884 DelReq(req, NULL, 1);
1885 }
1886 }
1887 req = treq;
1888 }
1889
1890 /* process xpa requests */
1891 if( doproxy < 2 ) XPAProcessSelect(&readfds, 0);
1892 #if HAVE_LIBPTHREAD
1893 /* unlock the mutex */
1894 if( doproxy >= 2 ) pthread_mutex_unlock(&xpans_mutex);
1895 #endif
1896 }
1897 /* keep alive timer went off */
1898 else if( sval == 0 ){
1899 ;
1900 }
1901 /* error on select() */
1902 else{
1903 /* restart system call if interrupted */
1904 if( errno != EINTR ){
1905 /* all others are problematic */
1906 if( XPAVerbosity() > 1 )
1907 perror("xpans select()");
1908 if( ++nerr >= MAX_ERRORS ){
1909 if( XPAVerbosity() > 1 )
1910 fprintf(stderr,
1911 "XPA$ERROR: too many select() errors in xpans\n");
1912 goto done;
1913 }
1914 }
1915 }
1916 /* see if its time to send keepalive probe */
1917 if( keepalive )
1918 KeepAliveReq();
1919 }
1920
1921 done:
1922 if( sock >=0 ) close(sock);
1923 if( securefp && (securefp != stdout) ) fclose(securefp);
1924 #if HAVE_SYS_UN_H
1925 if( mtype == XPA_UNIX ){
1926 unlink(method);
1927 if( lockfd >= 0 ){
1928 close(lockfd);
1929 unlink(lockfile);
1930 }
1931 }
1932 #endif
1933 XPAFree(xpa);
1934 return(0);
1935 }