4 * Methods for accessing the PPTPD config file and searching for
7 * $Id: configfile.c,v 1.2 2004/04/22 10:48:16 quozl Exp $
19 #include "configfile.h"
20 #include "our_syslog.h"
22 /* Local function prototypes */
23 static FILE *open_config_file(char *filename
);
24 static void close_config_file(FILE * file
);
29 * This method opens up the file specified by 'filename' and searches
30 * through the file for 'keyword'. If 'keyword' is found any string
31 * following it is stored in 'value'.
33 * args: filename (IN) - config filename
34 * keyword (IN) - word to search for in config file
35 * value (OUT) - value of keyword
37 * retn: -1 on error, 0 if keyword not found, 1 on value success
39 int read_config_file(char *filename
, char *keyword
, char *value
)
42 int len
= 0, keyword_len
= 0;
46 char buffer
[MAX_CONFIG_STRING_SIZE
];
50 keyword_len
= strlen(keyword
);
52 in
= open_config_file(filename
);
54 /* Couldn't find config file, or permission denied */
57 while ((fgets(buffer
, MAX_CONFIG_STRING_SIZE
- 1, in
)) != NULL
) {
58 /* ignore long lines */
59 if (buffer
[(len
= strlen(buffer
)) - 1] != '\n') {
60 syslog(LOG_ERR
, "Long config file line ignored.");
62 fgets(buffer
, MAX_CONFIG_STRING_SIZE
- 1, in
);
63 while (buffer
[strlen(buffer
) - 1] != '\n');
67 len
--; /* For the NL at the end */
69 if (buffer
[len
] != ' ' && buffer
[len
] != '\t')
77 /* Short-circuit blank lines and comments */
78 if (!len
|| *buff_ptr
== '#')
81 /* Non-blank lines starting with a space are an error */
83 if (*buff_ptr
== ' ' || *buff_ptr
== '\t') {
84 syslog(LOG_ERR
, "Config file line starts with a space: %s", buff_ptr
);
88 /* At this point we have a line trimmed for trailing spaces. */
89 /* Now we need to check if the keyword matches, and if so */
90 /* then get the value (if any). */
92 /* Check if it's the right keyword */
95 if (*buff_ptr
== ' ' || *buff_ptr
== '\t')
97 } while (*++buff_ptr
);
99 len
= buff_ptr
- buffer
;
100 if (len
== keyword_len
&& !strncmp(buffer
, keyword
, len
)) {
106 close_config_file(in
);
109 /* Right keyword, now get the value (if any) */
112 if (*buff_ptr
!= ' ' && *buff_ptr
!= '\t')
115 } while (*++buff_ptr
);
117 strcpy(value
, buff_ptr
);
120 /* didn't find it - better luck next time */
128 * Opens up the PPTPD config file for reading.
130 * args: filename - the config filename (eg. '/etc/pptpd.conf')
132 * retn: NULL on error, file descriptor on success
135 static FILE *open_config_file(char *filename
)
138 static int first
= 1;
140 if ((in
= fopen(filename
, "r")) == NULL
) {
141 /* Couldn't open config file */
154 * Closes the PPTPD config file descriptor
157 static void close_config_file(FILE * in
)