Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

thd_trusthost.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 /*-- 21 Feb 2001: modified to be more flexible --*/
00008 
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include <stdio.h>
00012 
00013 #include "afni_environ.h"
00014 #include "Amalloc.h"
00015 
00016 static int     host_num  = 0 ;
00017 static char ** host_list = NULL ;
00018 
00019 static char *init_hosts[] = { /* Initial list of OK computers */
00020     "141.106.106." ,            /* MCW computers (we're so trustworthy) */
00021     "128.231."     ,           /* NIH computers (also very trustworthy) */
00022     "127.0.0.1"    ,           /* localhost is always OK */
00023     "192.168."                 /* private class B networks */
00024 } ;
00025 #define INIT_NUM (sizeof(init_hosts)/sizeof(char *))
00026 #define HSIZE    32
00027 
00028 #define USE_NIML
00029 #ifdef  USE_NIML
00030 # include "niml.h"
00031 #endif
00032 
00033 /*----------------------------------------------------------------
00034    Return the Internet address (in 'dot' format, as a string)
00035    given the name of the host.  If NULL is returned, some
00036    error occurrrrred.  The string is malloc()-ed.
00037 ------------------------------------------------------------------*/
00038 
00039 #include <sys/types.h>
00040 #include <sys/socket.h>
00041 #include <netinet/in.h>
00042 #include <netdb.h>
00043 #include <arpa/inet.h>
00044 
00045 static char * xxx_name_to_inet( char *host )
00046 {
00047    struct hostent *hostp ;
00048    char *iname = NULL , *str ;
00049    int ll ;
00050 
00051    if( host == NULL || host[0] == '\0' ) return NULL ;
00052 
00053    hostp = gethostbyname(host) ; if( hostp == NULL ) return NULL ;
00054 
00055    str = inet_ntoa(*((struct in_addr *)(hostp->h_addr))) ;
00056    if( str == NULL || str[0] == '\0' ) return NULL ;
00057 
00058    ll = strlen(str) ; iname = AFMALL(char, ll+1) ; strcpy(iname,str) ;
00059    return iname ;
00060 }
00061 
00062 /*--------------------------------------------------------------------------
00063   Add a host to the trusted list
00064 ----------------------------------------------------------------------------*/
00065 
00066 #include <ctype.h>
00067 
00068 static void add_TRUST_host( char *hnam )
00069 {
00070    char *hh=NULL ;
00071    int nh,ii ;
00072 
00073    if( hnam == NULL || hnam[0] == '\0' ) return ;
00074 
00075    /* see if host name is consistent with 012.345.678.901 format */
00076 
00077    nh = strlen(hnam) ;
00078    for( ii=0 ; ii < nh ; ii++ )
00079       if( !isdigit(hnam[ii]) && hnam[ii] != '.' ) break ;
00080 
00081    if( ii < nh ){                     /* not a dotted number */
00082       hh = xxx_name_to_inet( hnam ) ; /* so do a lookup on it */
00083       if( hh == NULL ) return ;       /* failed? */
00084 
00085    } else if( nh > HSIZE-1 ){         /* something bad? */
00086       return ;
00087    } else {
00088       hh = hnam ;                     /* store dotted number */
00089    }
00090 
00091    host_list = (char **) realloc(host_list,sizeof(char *)*(host_num+1)) ;
00092    host_list[host_num] = (char *) malloc(HSIZE) ;
00093    strcpy( host_list[host_num] , hh ) ; host_num++ ;
00094 
00095    if( hh != hnam ) free(hh) ;
00096    return ;
00097 }
00098 
00099 /*---------------------------------------------------------------------------
00100    Initialize the trusted list from the internal table and the environment
00101 -----------------------------------------------------------------------------*/
00102 
00103 static void init_TRUST_list(void)
00104 {
00105    int ii ;
00106    char ename[HSIZE] , *str ;
00107 
00108    if( host_num == 0 ){
00109       host_num = INIT_NUM ;
00110       host_list = (char **) malloc( sizeof(char *) * INIT_NUM ) ;
00111       for( ii=0 ; ii < INIT_NUM ; ii++ ){
00112          host_list[ii] = (char *) malloc(HSIZE) ;
00113          strcpy( host_list[ii] , init_hosts[ii] ) ;
00114       }
00115 
00116       str = my_getenv("AFNI_TRUSTHOST") ;
00117       if( str != NULL ) add_TRUST_host(str) ;
00118 
00119       for( ii=1 ; ii <= 99 ; ii++ ){
00120          sprintf(ename,"AFNI_TRUSTHOST_%d",ii) ; str = my_getenv(ename) ;
00121          if( str == NULL && ii <= 9 ){
00122            sprintf(ename,"AFNI_TRUSTHOST_%02d",ii) ; str = my_getenv(ename) ;
00123          }
00124          if( str != NULL ) add_TRUST_host(str) ;
00125       }
00126    }
00127 
00128    return ;
00129 }
00130 
00131 /*---------------------------------------------------------------------------
00132   Externally callable routine to add a host to the trusted list
00133 -----------------------------------------------------------------------------*/
00134 
00135 void TRUST_addhost( char *hostname )
00136 {
00137    if( hostname == NULL || hostname[0] == '\0' ) return ;
00138    if( host_num == 0 ) init_TRUST_list() ;
00139    add_TRUST_host(hostname) ;
00140 #ifdef USE_NIML
00141    NI_add_trusted_host(hostname) ;
00142 #endif
00143    return ;
00144 }
00145 
00146 /*---------------------------------------------------------------------------
00147    return 1 if we like this host (specified in 'dot' notation), 0 if we don't
00148 -----------------------------------------------------------------------------*/
00149 
00150 int TRUST_host( char *hostid )
00151 {
00152    int ii ;
00153 
00154    if( host_num == 0 ) init_TRUST_list() ;
00155 
00156    if( hostid == NULL || hostid[0] == '\0' ) return 0 ;
00157 
00158    for( ii=0 ; ii < host_num ; ii++ )
00159       if( strstr(hostid,host_list[ii]) == hostid ) return 1 ;
00160 
00161    return 0 ;
00162 }
 

Powered by Plone

This site conforms to the following standards: