1
0

configfile.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * configfile.c
  3. *
  4. * Methods for accessing the PPTPD config file and searching for
  5. * PPTPD keywords.
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include "defaults.h"
  14. #include "configfile.h"
  15. #include "our_syslog.h"
  16. /* Local function prototypes */
  17. static FILE *open_config_file(char *filename);
  18. static void close_config_file(FILE * file);
  19. /*
  20. * read_config_file
  21. *
  22. * This method opens up the file specified by 'filename' and searches
  23. * through the file for 'keyword'. If 'keyword' is found any string
  24. * following it is stored in 'value'.
  25. *
  26. * args: filename (IN) - config filename
  27. * keyword (IN) - word to search for in config file
  28. * value (OUT) - value of keyword
  29. *
  30. * retn: -1 on error, 0 if keyword not found, 1 on value success
  31. */
  32. int read_config_file(char *filename, char *keyword, char *value)
  33. {
  34. FILE *in;
  35. int len = 0, keyword_len = 0;
  36. int foundit = 0;
  37. char *buff_ptr;
  38. char buffer[MAX_CONFIG_STRING_SIZE];
  39. *value = '\0';
  40. buff_ptr = buffer;
  41. keyword_len = strlen(keyword);
  42. in = open_config_file(filename);
  43. if (in == NULL) {
  44. /* Couldn't find config file, or permission denied */
  45. return -1;
  46. }
  47. while ((fgets(buffer, MAX_CONFIG_STRING_SIZE - 1, in)) != NULL) {
  48. /* ignore long lines */
  49. if (buffer[(len = strlen(buffer)) - 1] != '\n') {
  50. if (len >= MAX_CONFIG_STRING_SIZE - 2) {
  51. syslog(LOG_ERR, "Long config file line ignored.");
  52. char *p;
  53. do
  54. p = fgets(buffer, MAX_CONFIG_STRING_SIZE - 1, in);
  55. while (p && buffer[strlen(buffer) - 1] != '\n');
  56. continue;
  57. }
  58. } else {
  59. len--; /* For the NL at the end */
  60. }
  61. while (--len >= 0)
  62. if (buffer[len] != ' ' && buffer[len] != '\t')
  63. break;
  64. len++;
  65. buffer[len] = '\0';
  66. buff_ptr = buffer;
  67. /* Short-circuit blank lines and comments */
  68. if (!len || *buff_ptr == '#')
  69. continue;
  70. /* Non-blank lines starting with a space are an error */
  71. if (*buff_ptr == ' ' || *buff_ptr == '\t') {
  72. syslog(LOG_ERR, "Config file line starts with a space: %s", buff_ptr);
  73. continue;
  74. }
  75. /* At this point we have a line trimmed for trailing spaces. */
  76. /* Now we need to check if the keyword matches, and if so */
  77. /* then get the value (if any). */
  78. /* Check if it's the right keyword */
  79. do {
  80. if (*buff_ptr == ' ' || *buff_ptr == '\t')
  81. break;
  82. } while (*++buff_ptr);
  83. len = buff_ptr - buffer;
  84. if (len == keyword_len && !strncmp(buffer, keyword, len)) {
  85. foundit++;
  86. break;
  87. }
  88. }
  89. close_config_file(in);
  90. if (foundit) {
  91. /* Right keyword, now get the value (if any) */
  92. do {
  93. if (*buff_ptr != ' ' && *buff_ptr != '\t')
  94. break;
  95. } while (*++buff_ptr);
  96. strcpy(value, buff_ptr);
  97. return 1;
  98. } else {
  99. /* didn't find it - better luck next time */
  100. return 0;
  101. }
  102. }
  103. /*
  104. * open_config_file
  105. *
  106. * Opens up the PPTPD config file for reading.
  107. *
  108. * args: filename - the config filename (eg. '/etc/pptpd.conf')
  109. *
  110. * retn: NULL on error, file descriptor on success
  111. *
  112. */
  113. static FILE *open_config_file(char *filename)
  114. {
  115. FILE *in;
  116. static int first = 1;
  117. if ((in = fopen(filename, "r")) == NULL) {
  118. /* Couldn't open config file */
  119. if (first) {
  120. perror(filename);
  121. first = 0;
  122. }
  123. return NULL;
  124. }
  125. return in;
  126. }
  127. /*
  128. * close_config_file
  129. *
  130. * Closes the PPTPD config file descriptor
  131. *
  132. */
  133. static void close_config_file(FILE * in)
  134. {
  135. fclose(in);
  136. }