configfile.c 3.4 KB

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