1
0

configfile.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.2 2004/04/22 10:48:16 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. syslog(LOG_ERR, "Long config file line ignored.");
  53. do
  54. fgets(buffer, MAX_CONFIG_STRING_SIZE - 1, in);
  55. while (buffer[strlen(buffer) - 1] != '\n');
  56. continue;
  57. }
  58. len--; /* For the NL at the end */
  59. while (--len >= 0)
  60. if (buffer[len] != ' ' && buffer[len] != '\t')
  61. break;
  62. len++;
  63. buffer[len] = '\0';
  64. buff_ptr = buffer;
  65. /* Short-circuit blank lines and comments */
  66. if (!len || *buff_ptr == '#')
  67. continue;
  68. /* Non-blank lines starting with a space are an error */
  69. if (*buff_ptr == ' ' || *buff_ptr == '\t') {
  70. syslog(LOG_ERR, "Config file line starts with a space: %s", buff_ptr);
  71. continue;
  72. }
  73. /* At this point we have a line trimmed for trailing spaces. */
  74. /* Now we need to check if the keyword matches, and if so */
  75. /* then get the value (if any). */
  76. /* Check if it's the right keyword */
  77. do {
  78. if (*buff_ptr == ' ' || *buff_ptr == '\t')
  79. break;
  80. } while (*++buff_ptr);
  81. len = buff_ptr - buffer;
  82. if (len == keyword_len && !strncmp(buffer, keyword, len)) {
  83. foundit++;
  84. break;
  85. }
  86. }
  87. close_config_file(in);
  88. if (foundit) {
  89. /* Right keyword, now get the value (if any) */
  90. do {
  91. if (*buff_ptr != ' ' && *buff_ptr != '\t')
  92. break;
  93. } while (*++buff_ptr);
  94. strcpy(value, buff_ptr);
  95. return 1;
  96. } else {
  97. /* didn't find it - better luck next time */
  98. return 0;
  99. }
  100. }
  101. /*
  102. * open_config_file
  103. *
  104. * Opens up the PPTPD config file for reading.
  105. *
  106. * args: filename - the config filename (eg. '/etc/pptpd.conf')
  107. *
  108. * retn: NULL on error, file descriptor on success
  109. *
  110. */
  111. static FILE *open_config_file(char *filename)
  112. {
  113. FILE *in;
  114. static int first = 1;
  115. if ((in = fopen(filename, "r")) == NULL) {
  116. /* Couldn't open config file */
  117. if (first) {
  118. perror(filename);
  119. first = 0;
  120. }
  121. return NULL;
  122. }
  123. return in;
  124. }
  125. /*
  126. * close_config_file
  127. *
  128. * Closes the PPTPD config file descriptor
  129. *
  130. */
  131. static void close_config_file(FILE * in)
  132. {
  133. fclose(in);
  134. }