@@ -149,13 +149,17 @@ static const struct {
149
149
"sslallowedcipherlist" , oSSLAllowedCipherList }, {
150
150
NULL , oBadOption },};
151
151
152
- static void config_notnull (const void * parm , const char * parmname );
152
+ static void config_notnull (const void * , const char * );
153
153
static int parse_boolean_value (char * );
154
154
static void parse_auth_server (FILE * , const char * , int * );
155
- static int _parse_firewall_rule (const char * ruleset , char * leftover );
155
+ static int _parse_firewall_rule (const char * , char * );
156
156
static void parse_firewall_ruleset (const char * , FILE * , const char * , int * );
157
+ static void parse_trusted_mac_list (const char * );
158
+ static void parse_popular_servers (const char * );
159
+ static void validate_popular_servers (void );
160
+ static void add_popular_server (const char * );
157
161
158
- static OpCodes config_parse_token (const char * cp , const char * filename , int linenum );
162
+ static OpCodes config_parse_token (const char * , const char * , int );
159
163
160
164
/** Accessor for the current gateway configuration
161
165
@return: A pointer to the current config. The pointer isn't opaque, but should be treated as READ-ONLY
633
637
config_read (const char * filename )
634
638
{
635
639
FILE * fd ;
636
- char line [MAX_BUF ], * s , * p1 , * p2 ;
640
+ char line [MAX_BUF ], * s , * p1 , * p2 , * rawarg = NULL ;
637
641
int linenum = 0 , opcode , value ;
638
642
size_t len ;
639
643
@@ -669,7 +673,7 @@ config_read(const char *filename)
669
673
break ;
670
674
len = strlen (p1 );
671
675
}
672
-
676
+ rawarg = safe_strdup ( p1 );
673
677
if ((p2 = strchr (p1 , ' ' ))) {
674
678
p2 [0 ] = '\0' ;
675
679
} else if ((p2 = strstr (p1 , "\r\n" ))) {
@@ -722,7 +726,7 @@ config_read(const char *filename)
722
726
parse_trusted_mac_list (p1 );
723
727
break ;
724
728
case oPopularServers :
725
- parse_popular_servers (p1 );
729
+ parse_popular_servers (rawarg );
726
730
break ;
727
731
case oHTTPDName :
728
732
config .httpdname = safe_strdup (p1 );
@@ -791,6 +795,10 @@ config_read(const char *filename)
791
795
}
792
796
}
793
797
}
798
+ if (rawarg ) {
799
+ free (rawarg );
800
+ rawarg = NULL ;
801
+ }
794
802
}
795
803
796
804
if (config .httpdusername && !config .httpdpassword ) {
@@ -823,7 +831,8 @@ parse_boolean_value(char *line)
823
831
return -1 ;
824
832
}
825
833
826
- /* Parse possiblemac to see if it is valid MAC address format */
834
+ /**
835
+ * Parse possiblemac to see if it is valid MAC address format */
827
836
int
828
837
check_mac_format (char * possiblemac )
829
838
{
@@ -834,7 +843,10 @@ check_mac_format(char *possiblemac)
834
843
hex2 , hex2 , hex2 , hex2 , hex2 , hex2 ) == 6 ;
835
844
}
836
845
837
- void
846
+ /** @internal
847
+ * Parse the trusted mac list.
848
+ */
849
+ static void
838
850
parse_trusted_mac_list (const char * ptr )
839
851
{
840
852
char * ptrcopy = NULL ;
@@ -849,7 +861,7 @@ parse_trusted_mac_list(const char *ptr)
849
861
/* strsep modifies original, so let's make a copy */
850
862
ptrcopy = safe_strdup (ptr );
851
863
852
- while ((possiblemac = strsep (& ptrcopy , ", " ))) {
864
+ while ((possiblemac = strsep (& ptrcopy , "," ))) {
853
865
/* check for valid format */
854
866
if (!check_mac_format (possiblemac )) {
855
867
debug (LOG_ERR ,
@@ -906,46 +918,61 @@ parse_trusted_mac_list(const char *ptr)
906
918
907
919
}
908
920
909
- void
921
+ /** @internal
922
+ * Add a popular server to the list. It prepends for simplicity.
923
+ * @param server The hostname to add.
924
+ */
925
+ static void
926
+ add_popular_server (const char * server )
927
+ {
928
+ t_popular_server * p = NULL ;
929
+
930
+ p = (t_popular_server * )safe_malloc (sizeof (t_popular_server ));
931
+ p -> hostname = safe_strdup (server );
932
+
933
+ if (config .popular_servers == NULL ) {
934
+ p -> next = NULL ;
935
+ config .popular_servers = p ;
936
+ } else {
937
+ p -> next = config .popular_servers ;
938
+ config .popular_servers = p ;
939
+ }
940
+ }
941
+
942
+ static void
910
943
parse_popular_servers (const char * ptr )
911
944
{
912
945
char * ptrcopy = NULL ;
913
946
char * hostname = NULL ;
914
- t_popular_server * p = NULL ;
947
+ char * tmp = NULL ;
915
948
916
949
debug (LOG_DEBUG , "Parsing string [%s] for popular servers" , ptr );
917
950
918
- // max length of domain name is 253 characters
919
- hostname = safe_malloc (254 );
920
-
921
951
/* strsep modifies original, so let's make a copy */
922
952
ptrcopy = safe_strdup (ptr );
923
953
924
- while ((hostname = strsep (& ptrcopy , ", " ))) {
925
- if (strcmp (hostname , "" ) == 0 ) {
954
+ while ((hostname = strsep (& ptrcopy , "," ))) { /* hostname does *not* need allocation. strsep
955
+ provides a pointer in ptrcopy. */
956
+ /* Skip leading spaces. */
957
+ while (* hostname != '\0' && isblank (* hostname )) {
958
+ hostname ++ ;
959
+ }
960
+ if (* hostname == '\0' ) { /* Equivalent to strcmp(hostname, "") == 0 */
926
961
continue ;
927
962
}
928
- debug (LOG_DEBUG , "Adding Popular Server [%s] to list" , hostname );
929
-
930
- if (config .popular_servers == NULL ) {
931
- config .popular_servers = safe_malloc (sizeof (t_popular_server ));
932
- config .popular_servers -> hostname = safe_strdup (hostname );
933
- config .popular_servers -> next = NULL ;
934
- } else {
935
- p = config .popular_servers ;
936
- /* Advance to the last entry */
937
- while (p -> next != NULL ) {
938
- p = p -> next ;
939
- }
940
- p -> next = safe_malloc (sizeof (t_popular_server ));
941
- p = p -> next ;
942
- p -> hostname = safe_strdup (hostname );
943
- p -> next = NULL ;
963
+ /* Remove any trailing blanks. */
964
+ tmp = hostname ;
965
+ while (* tmp != '\0' && !isblank (* tmp )) {
966
+ tmp ++ ;
944
967
}
968
+ if (* tmp != '\0' && isblank (* tmp )) {
969
+ * tmp = '\0' ;
970
+ }
971
+ debug (LOG_DEBUG , "Adding Popular Server [%s] to list" , hostname );
972
+ add_popular_server (hostname );
945
973
}
946
974
947
975
free (ptrcopy );
948
- free (hostname );
949
976
}
950
977
951
978
/** Verifies if the configuration is complete and valid. Terminates the program if it isn't */
@@ -954,13 +981,27 @@ config_validate(void)
954
981
{
955
982
config_notnull (config .gw_interface , "GatewayInterface" );
956
983
config_notnull (config .auth_servers , "AuthServer" );
984
+ validate_popular_servers ();
957
985
958
986
if (missing_parms ) {
959
987
debug (LOG_ERR , "Configuration is not complete, exiting..." );
960
988
exit (-1 );
961
989
}
962
990
}
963
991
992
+ /** @internal
993
+ * Validate that popular servers are populated or log a warning and set a default.
994
+ */
995
+ static void
996
+ validate_popular_servers (void )
997
+ {
998
+ if (config .popular_servers == NULL ) {
999
+ debug (LOG_WARNING , "PopularServers not set in config file, this will become fatal in a future version." );
1000
+ add_popular_server ("www.google.com" );
1001
+ add_popular_server ("www.yahoo.com" );
1002
+ }
1003
+ }
1004
+
964
1005
/** @internal
965
1006
Verifies that a required parameter is not a null pointer
966
1007
*/
0 commit comments