1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 /*-- 21 Feb 2001: modified to be more flexible --*/
8 
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 
13 #include "afni_environ.h"
14 #include "Amalloc.h"
15 
16 static int     host_num  = 0 ;
17 static char ** host_list = NULL ;
18 
19 static char *init_hosts[] = { /* Initial list of OK computers */
20     "141.106.106." ,           /* MCW computers (we're so trustworthy) */
21     "128.231."     ,           /* NIH computers (also very trustworthy) */
22     "127.0.0.1"    ,           /* localhost is always OK */
23     "192.168."     ,           /* private class B networks */
24     "0.0.0.0"                  /* sometimes this is localhost */
25 } ;
26 #define INIT_NUM (sizeof(init_hosts)/sizeof(char *))
27 #define HSIZE    32
28 
29 #define USE_NIML
30 #ifdef  USE_NIML
31 # include "niml.h"
32 #endif
33 
34 /*----------------------------------------------------------------
35    Return the Internet address (in 'dot' format, as a string)
36    given the name of the host.  If NULL is returned, some
37    error occurrrrred.  The string is malloc()-ed.
38 ------------------------------------------------------------------*/
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netdb.h>
44 #include <arpa/inet.h>
45 
xxx_name_to_inet(char * host)46 static char * xxx_name_to_inet( char *host )
47 {
48    struct hostent *hostp ;
49    char *iname = NULL , *str ;
50    int ll ;
51 
52    if( host == NULL || host[0] == '\0' ) return NULL ;
53 
54    hostp = gethostbyname(host) ; if( hostp == NULL ) return NULL ;
55 
56    str = inet_ntoa(*((struct in_addr *)(hostp->h_addr))) ;
57    if( str == NULL || str[0] == '\0' ) return NULL ;
58 
59    ll = strlen(str) ; iname = AFMALL(char, ll+1) ; strcpy(iname,str) ;
60    return iname ;
61 }
62 
63 /*--------------------------------------------------------------------------
64   Add a host to the trusted list
65 ----------------------------------------------------------------------------*/
66 
67 #include <ctype.h>
68 
add_TRUST_host(char * hnam)69 static void add_TRUST_host( char *hnam )
70 {
71    char *hh=NULL ;
72    int nh,ii ;
73 
74    if( hnam == NULL || hnam[0] == '\0' ) return ;
75 
76    /* see if host name is consistent with 012.345.678.901 format */
77 
78    nh = strlen(hnam) ;
79    for( ii=0 ; ii < nh ; ii++ )
80       if( !isdigit(hnam[ii]) && hnam[ii] != '.' ) break ;
81 
82    if( ii < nh ){                     /* not a dotted number */
83       hh = xxx_name_to_inet( hnam ) ; /* so do a lookup on it */
84       if( hh == NULL ) return ;       /* failed? */
85 
86    } else if( nh > HSIZE-1 ){         /* something bad? */
87       return ;
88    } else {
89       hh = hnam ;                     /* store dotted number */
90    }
91 
92    host_list = (char **) realloc(host_list,sizeof(char *)*(host_num+1)) ;
93    host_list[host_num] = (char *) malloc(HSIZE) ;
94    strcpy( host_list[host_num] , hh ) ; host_num++ ;
95 
96    if( hh != hnam ) free(hh) ;
97    return ;
98 }
99 
100 /*---------------------------------------------------------------------------
101    Initialize the trusted list from the internal table and the environment
102 -----------------------------------------------------------------------------*/
103 
init_TRUST_list(void)104 static void init_TRUST_list(void)
105 {
106    int ii ;
107    char ename[HSIZE] , *str ;
108 
109    if( host_num == 0 ){
110       host_num = INIT_NUM ;
111       host_list = (char **) malloc( sizeof(char *) * INIT_NUM ) ;
112       for( ii=0 ; ii < INIT_NUM ; ii++ ){
113          host_list[ii] = (char *) malloc(HSIZE) ;
114          strcpy( host_list[ii] , init_hosts[ii] ) ;
115       }
116 
117       str = my_getenv("AFNI_TRUSTHOST") ;
118       if( str != NULL ) add_TRUST_host(str) ;
119 
120       for( ii=1 ; ii <= 99 ; ii++ ){
121          sprintf(ename,"AFNI_TRUSTHOST_%d",ii) ; str = my_getenv(ename) ;
122          if( str == NULL && ii <= 9 ){
123            sprintf(ename,"AFNI_TRUSTHOST_%02d",ii) ; str = my_getenv(ename) ;
124          }
125          if( str != NULL ) add_TRUST_host(str) ;
126       }
127    }
128 
129    return ;
130 }
131 
132 /*---------------------------------------------------------------------------
133   Externally callable routine to add a host to the trusted list
134 -----------------------------------------------------------------------------*/
135 
TRUST_addhost(char * hostname)136 void TRUST_addhost( char *hostname )
137 {
138    if( hostname == NULL || hostname[0] == '\0' ) return ;
139    if( host_num == 0 ) init_TRUST_list() ;
140    add_TRUST_host(hostname) ;
141 #ifdef USE_NIML
142    NI_add_trusted_host(hostname) ;
143 #endif
144    return ;
145 }
146 
147 /*---------------------------------------------------------------------------
148    return 1 if we like this host (specified in 'dot' notation), 0 if we don't
149 -----------------------------------------------------------------------------*/
150 
TRUST_host(char * hostid)151 int TRUST_host( char *hostid )
152 {
153    int ii ;
154 
155    if( host_num == 0 ) init_TRUST_list() ;
156 
157    if( hostid == NULL || hostid[0] == '\0' ) return 0 ;
158 
159    for( ii=0 ; ii < host_num ; ii++ )
160       if( strstr(hostid,host_list[ii]) == hostid ) return 1 ;
161 
162    return 0 ;
163 }
164