aesm_config.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "aesm_config.h"
  32. #include "aesm_proxy_type.h"
  33. #include "oal.h"
  34. #include "default_url_info.hh"
  35. #include <sys/types.h>
  36. #include <regex.h>
  37. #include <stdio.h>
  38. #define AESM_CONFIG_FILE "/etc/aesmd.conf"
  39. #define MAX_LINE 1024
  40. #define URL_PATTERN "[[:blank:]]*(http[s]?://[^[:blank:]]*)[[:blank:]]*" //pattern used to match a URL which should be started with http:// or https://
  41. #define OPTION_COMMENT "(#.*)?"
  42. enum _config_value_t{
  43. config_comment,
  44. config_space,
  45. config_white_list_url,
  46. config_aesm_proxy_url,
  47. config_aesm_proxy_type,
  48. config_value_nums
  49. };
  50. struct _config_patterns_t{
  51. enum _config_value_t id;
  52. const char *pattern;
  53. }config_patterns[]={
  54. {config_comment, "^[[:blank:]]*#"}, //matching a line with comments only (It is started by #)
  55. {config_space, "^[[:blank:]]*$"}, //matching empty line
  56. {config_white_list_url, "^[[:blank:]]*whitelist[[:blank:]]*url[[:blank:]]*=" URL_PATTERN OPTION_COMMENT "$"}, //matching line in format: whilelist url = ....
  57. {config_aesm_proxy_url,"^[[:blank:]]*aesm[[:blank:]]*proxy[[:blank:]]*=" URL_PATTERN OPTION_COMMENT "$"}, //matching line in format: aesm proxy = ...
  58. {config_aesm_proxy_type, "^[[:blank:]]*proxy[[:blank:]]*type[[:blank:]]*=[[:blank:]]([^[:blank:]]+)[[:blank:]]*" OPTION_COMMENT "$"}//matching line in format: proxy type = [direct|default|manual]
  59. };
  60. #define NUM_CONFIG_PATTERNS (sizeof(config_patterns)/sizeof(config_patterns[0]))
  61. typedef struct _config_entry_t{
  62. bool initialized;
  63. regex_t reg;
  64. } config_entry_t;
  65. //static function to initialize all regular expression pattern
  66. static void init_config_patterns(config_entry_t entries[])
  67. {
  68. uint32_t i;
  69. for(i=0;i<NUM_CONFIG_PATTERNS;++i){
  70. uint32_t entry_id = config_patterns[i].id;
  71. if(entry_id>=config_value_nums){
  72. AESM_DBG_ERROR("config id %d is too large", entry_id);
  73. continue;
  74. }
  75. if(entries[entry_id].initialized){
  76. AESM_DBG_ERROR("duplicated item for config id %d",entry_id);
  77. continue;
  78. }
  79. if(regcomp(&entries[entry_id].reg,config_patterns[i].pattern, REG_EXTENDED|REG_ICASE)!=0){
  80. AESM_DBG_ERROR("Invalid config pattern %s", config_patterns[i].pattern);
  81. continue;
  82. }
  83. entries[entry_id].initialized=true;
  84. }
  85. }
  86. static void release_config_patterns(config_entry_t entries[])
  87. {
  88. uint32_t i;
  89. for(i=0;i<config_value_nums;++i){
  90. if(entries[i].initialized){
  91. entries[i].initialized=false;
  92. regfree(&entries[i].reg);
  93. }
  94. }
  95. }
  96. static const char *proxy_type_name[]={
  97. "direct",
  98. "default",
  99. "manual"
  100. };
  101. #define NUM_PROXY_TYPE (sizeof(proxy_type_name)/sizeof(proxy_type_name[0]))
  102. //function to decode proxy type from string to integer value
  103. static uint32_t read_aesm_proxy_type(const char *string, uint32_t len)
  104. {
  105. uint32_t i;
  106. for(i=0;i<NUM_PROXY_TYPE;++i){
  107. if(strncasecmp(proxy_type_name[i],string,len)==0){
  108. return i;
  109. }
  110. }
  111. AESM_DBG_TRACE("Invalid proxy type %.*s",len,string);
  112. return (uint32_t)NUM_PROXY_TYPE;
  113. }
  114. #define MAX_MATCHED_REG_EXP 3
  115. //Function to processing one line in config file
  116. // If any pattern is matched, get the correspondent data and set it into the output parameter 'infos'
  117. static bool config_process_one_line(const char *line, config_entry_t entries[], aesm_config_infos_t& infos)
  118. {
  119. uint32_t i;
  120. regmatch_t matches[MAX_MATCHED_REG_EXP];
  121. for(i=0;i<config_value_nums;++i){
  122. if(!entries[i].initialized){
  123. continue;
  124. }
  125. if(regexec(&entries[i].reg, line, MAX_MATCHED_REG_EXP, matches, 0)==0){
  126. switch(i){
  127. case config_comment:
  128. case config_space:
  129. //ignore comment and space only line
  130. break;
  131. case config_white_list_url://Matching White List URL setting
  132. if(matches[1].rm_eo-matches[1].rm_so>=MAX_PATH){
  133. AESM_DBG_ERROR("too long white list url in config file");
  134. }else{
  135. memcpy(infos.white_list_url, line+matches[1].rm_so,matches[1].rm_eo-matches[1].rm_so);
  136. infos.white_list_url[matches[1].rm_eo-matches[1].rm_so]='\0';
  137. }
  138. break;
  139. case config_aesm_proxy_url:
  140. if(matches[1].rm_eo-matches[1].rm_so>=MAX_PATH){
  141. AESM_DBG_ERROR("too long aesm proxy url in config file");
  142. }else{
  143. memcpy(infos.aesm_proxy, line+matches[1].rm_so,matches[1].rm_eo-matches[1].rm_so);
  144. infos.aesm_proxy[matches[1].rm_eo-matches[1].rm_so]='\0';
  145. }
  146. break;
  147. case config_aesm_proxy_type://It is a proxy type, we need change the string to integer by calling function read_aesm_proxy_type
  148. infos.proxy_type = read_aesm_proxy_type(line+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
  149. break;
  150. default:
  151. AESM_DBG_ERROR("reg exp type %d not processed", i);
  152. break;
  153. }
  154. break;
  155. }
  156. }
  157. if(i>=config_value_nums){//the line matching nothing
  158. AESM_DBG_ERROR("aesm config file error: invalid line[%s]",line);
  159. return false;
  160. }
  161. return true;
  162. }
  163. bool read_aesm_config(aesm_config_infos_t& infos)
  164. {
  165. char line[MAX_LINE];
  166. int line_no=0;
  167. bool ret = true;
  168. config_entry_t entries[config_value_nums];
  169. memset(&entries,0,sizeof(entries));
  170. memset(&infos, 0, sizeof(aesm_config_infos_t));
  171. strcpy(infos.white_list_url, DEFAULT_WHITE_LIST_URL);
  172. infos.proxy_type = AESM_PROXY_TYPE_DEFAULT_PROXY;
  173. FILE *f =fopen(AESM_CONFIG_FILE, "r");
  174. if(f==NULL){
  175. AESM_DBG_ERROR("Cannnot read aesm config file %s",AESM_CONFIG_FILE);
  176. return false;
  177. }
  178. init_config_patterns(entries);
  179. while(fgets(line, MAX_LINE, f)!=NULL){
  180. size_t len=strlen(line);
  181. if(len>0&&line[len-1]=='\n')line[len-1]='\0';//remove the line ending
  182. line_no++;
  183. if(!config_process_one_line(line, entries, infos)){
  184. AESM_LOG_WARN("format error in file %s:%d [%s]",AESM_CONFIG_FILE, line_no, line);
  185. ret = false;//continue process the file but save the error status
  186. }
  187. }
  188. release_config_patterns(entries);
  189. fclose(f);
  190. if(infos.proxy_type>=NUM_PROXY_TYPE||
  191. (infos.proxy_type==AESM_PROXY_TYPE_MANUAL_PROXY&&infos.aesm_proxy[0]=='\0')){
  192. AESM_DBG_WARN("Invalid proxy type %d",infos.proxy_type);
  193. infos.proxy_type = AESM_PROXY_TYPE_DIRECT_ACCESS;
  194. ret = false;
  195. }
  196. return ret;
  197. }