You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

403 lines
9.5 KiB

  1. /**
  2. * xrdp: A Remote Desktop Protocol server.
  3. *
  4. * Copyright (C) Jay Sorg 2004-2014
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * read a config file
  19. */
  20. #if defined(HAVE_CONFIG_H)
  21. #include <config_ac.h>
  22. #endif
  23. #include "arch.h"
  24. #include "os_calls.h"
  25. #include "list.h"
  26. #include "file.h"
  27. #include "parse.h"
  28. #define FILE_MAX_LINE_BYTES 2048
  29. static int
  30. file_read_ini_line(struct stream *s, char *text, int text_bytes);
  31. /*****************************************************************************/
  32. /* look up for a section name within str (i.e. pattern [section_name])
  33. * if a section name is found, this function return 1 and copy the section
  34. * inplace of str. */
  35. static int
  36. line_lookup_for_section_name(char *str, int str_bytes)
  37. {
  38. int name_index_start;
  39. int index;
  40. char c;
  41. name_index_start = -1;
  42. index = 0;
  43. while ((c = str[index]) != 0)
  44. {
  45. if (c == '[')
  46. {
  47. name_index_start = index + 1;
  48. }
  49. else if (c == ']' && name_index_start > 0)
  50. {
  51. if (name_index_start + index >= str_bytes)
  52. {
  53. return 0;
  54. }
  55. for (index = index - name_index_start; index > 0; index--)
  56. {
  57. str[0] = str[name_index_start];
  58. str++;
  59. }
  60. str[0] = 0;
  61. return 1;
  62. }
  63. ++index;
  64. }
  65. return 0;
  66. }
  67. /*****************************************************************************/
  68. /* returns error
  69. returns 0 if everything is ok
  70. returns 1 if problem reading file */
  71. static int
  72. l_file_read_sections(int fd, int max_file_size, struct list *names)
  73. {
  74. struct stream *s;
  75. char text[FILE_MAX_LINE_BYTES];
  76. int len;
  77. int rv;
  78. rv = 0;
  79. g_file_seek(fd, 0);
  80. g_memset(text, 0, FILE_MAX_LINE_BYTES);
  81. list_clear(names);
  82. make_stream(s);
  83. init_stream(s, max_file_size);
  84. len = g_file_read(fd, s->data, max_file_size);
  85. if (len > 0)
  86. {
  87. s->end = s->p + len;
  88. while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
  89. {
  90. if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
  91. {
  92. list_add_item(names, (tbus)g_strdup(text));
  93. }
  94. }
  95. }
  96. else if (len < 0)
  97. {
  98. rv = 1;
  99. }
  100. free_stream(s);
  101. return rv;
  102. }
  103. /*****************************************************************************/
  104. /* Read a line in the stream 's', removing comments.
  105. * returns error
  106. * returns 0 if everything is ok
  107. * returns 1 if problem reading file */
  108. static int
  109. file_read_ini_line(struct stream *s, char *text, int text_bytes)
  110. {
  111. int i;
  112. int skip_to_end;
  113. int at_end;
  114. char c;
  115. skip_to_end = 0;
  116. if (!s_check_rem(s, 1))
  117. {
  118. return 1;
  119. }
  120. i = 0;
  121. in_uint8(s, c);
  122. while (c != 10 && c != 13)
  123. {
  124. /* these mean skip the rest of the line */
  125. if (c == '#' || c == '!' || c == ';')
  126. {
  127. skip_to_end = 1;
  128. }
  129. if (!skip_to_end)
  130. {
  131. text[i] = c;
  132. i++;
  133. if (i >= text_bytes)
  134. {
  135. return 1;
  136. }
  137. }
  138. if (s_check_rem(s, 1))
  139. {
  140. in_uint8(s, c);
  141. }
  142. else
  143. {
  144. c = 0;
  145. break;
  146. }
  147. }
  148. if (c == 10 || c == 13)
  149. {
  150. at_end = 0;
  151. while (c == 10 || c == 13)
  152. {
  153. if (s_check_rem(s, 1))
  154. {
  155. in_uint8(s, c);
  156. }
  157. else
  158. {
  159. at_end = 1;
  160. break;
  161. }
  162. }
  163. if (!at_end)
  164. {
  165. s->p--;
  166. }
  167. }
  168. text[i] = 0;
  169. return 0;
  170. }
  171. /*****************************************************************************/
  172. /* returns error */
  173. static int
  174. file_split_name_value(char *text, char *name, char *value)
  175. {
  176. int len;
  177. int i;
  178. int value_index;
  179. int name_index;
  180. int on_to;
  181. value_index = 0;
  182. name_index = 0;
  183. on_to = 0;
  184. name[0] = 0;
  185. value[0] = 0;
  186. len = g_strlen(text);
  187. for (i = 0; i < len; i++)
  188. {
  189. if (text[i] == '=' && !on_to)
  190. {
  191. on_to = 1;
  192. }
  193. else if (on_to)
  194. {
  195. value[value_index] = text[i];
  196. value_index++;
  197. value[value_index] = 0;
  198. }
  199. else
  200. {
  201. name[name_index] = text[i];
  202. name_index++;
  203. name[name_index] = 0;
  204. }
  205. }
  206. g_strtrim(name, 3); /* trim both right and left */
  207. g_strtrim(value, 3); /* trim both right and left */
  208. return 0;
  209. }
  210. /*****************************************************************************/
  211. /* return error */
  212. static int
  213. l_file_read_section(int fd, int max_file_size, const char *section,
  214. struct list *names, struct list *values)
  215. {
  216. struct stream *s;
  217. char *data;
  218. char *text;
  219. char *name;
  220. char *value;
  221. char *lvalue;
  222. int len;
  223. int file_size;
  224. data = (char *) g_malloc(FILE_MAX_LINE_BYTES * 3, 0);
  225. text = data;
  226. name = text + FILE_MAX_LINE_BYTES;
  227. value = name + FILE_MAX_LINE_BYTES;
  228. file_size = 32 * 1024; /* 32 K file size limit */
  229. g_file_seek(fd, 0);
  230. g_memset(text, 0, FILE_MAX_LINE_BYTES);
  231. list_clear(names);
  232. list_clear(values);
  233. make_stream(s);
  234. init_stream(s, file_size);
  235. len = g_file_read(fd, s->data, file_size);
  236. if (len > 0)
  237. {
  238. s->end = s->p + len;
  239. while (file_read_ini_line(s, text, FILE_MAX_LINE_BYTES) == 0)
  240. {
  241. if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
  242. {
  243. if (g_strcasecmp(section, text) == 0)
  244. {
  245. while (file_read_ini_line(s, text,
  246. FILE_MAX_LINE_BYTES) == 0)
  247. {
  248. if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
  249. {
  250. break;
  251. }
  252. if (g_strlen(text) > 0)
  253. {
  254. file_split_name_value(text, name, value);
  255. list_add_item(names, (tbus)g_strdup(name));
  256. if (value[0] == '$')
  257. {
  258. lvalue = g_getenv(value + 1);
  259. if (lvalue != 0)
  260. {
  261. list_add_item(values, (tbus)g_strdup(lvalue));
  262. }
  263. else
  264. {
  265. list_add_item(values, (tbus)g_strdup(""));
  266. }
  267. }
  268. else
  269. {
  270. list_add_item(values, (tbus)g_strdup(value));
  271. }
  272. }
  273. }
  274. free_stream(s);
  275. g_free(data);
  276. return 0;
  277. }
  278. }
  279. }
  280. }
  281. free_stream(s);
  282. g_free(data);
  283. return 1;
  284. }
  285. /*****************************************************************************/
  286. /* returns error
  287. returns 0 if everything is ok
  288. returns 1 if problem reading file */
  289. /* 32 K file size limit */
  290. int
  291. file_read_sections(int fd, struct list *names)
  292. {
  293. return l_file_read_sections(fd, 32 * 1024, names);
  294. }
  295. /*****************************************************************************/
  296. /* return error */
  297. /* this function should be preferred over file_read_sections because it can
  298. read any file size */
  299. int
  300. file_by_name_read_sections(const char *file_name, struct list *names)
  301. {
  302. int fd;
  303. int file_size;
  304. int rv;
  305. file_size = g_file_get_size(file_name);
  306. if (file_size < 1)
  307. {
  308. return 1;
  309. }
  310. fd = g_file_open_ex(file_name, 1, 0, 0, 0);
  311. if (fd < 0)
  312. {
  313. return 1;
  314. }
  315. rv = l_file_read_sections(fd, file_size, names);
  316. g_file_close(fd);
  317. return rv;
  318. }
  319. /*****************************************************************************/
  320. /* return error */
  321. /* 32 K file size limit */
  322. int
  323. file_read_section(int fd, const char *section,
  324. struct list *names, struct list *values)
  325. {
  326. return l_file_read_section(fd, 32 * 1024, section, names, values);
  327. }
  328. /*****************************************************************************/
  329. /* return error */
  330. /* this function should be preferred over file_read_section because it can
  331. read any file size */
  332. int
  333. file_by_name_read_section(const char *file_name, const char *section,
  334. struct list *names, struct list *values)
  335. {
  336. int fd;
  337. int file_size;
  338. int rv;
  339. file_size = g_file_get_size(file_name);
  340. if (file_size < 1)
  341. {
  342. return 1;
  343. }
  344. fd = g_file_open_ex(file_name, 1, 0, 0, 0);
  345. if (fd < 0)
  346. {
  347. return 1;
  348. }
  349. rv = l_file_read_section(fd, file_size, section, names, values);
  350. g_file_close(fd);
  351. return rv;
  352. }