1
2 /*
3
4 File: ftpproxy/config.c
5
6 Copyright (C) 2003 Andreas Schoenberg <asg@ftpproxy.org>
7
8 This software is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31
32 #include <errno.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <pwd.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/fcntl.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <syslog.h>
45 #include <sys/time.h>
46
47 #include "ftp.h"
48 #include "lib.h"
49
50
51
get_yesno(char ** from,char * par,char * filename,int lineno)52 static int get_yesno(char **from, char *par, char *filename, int lineno)
53 {
54 char word[80];
55
56 if (**from == 0) {
57 fprintf (stderr, "%s: missing parameter: %s, %s:%d\n", program, par, filename, lineno);
58 exit (1);
59 }
60
61 get_word(from, word, sizeof(word));
62 if (strcmp(word, "yes") == 0)
63 return (1);
64 else if (strcmp(word, "no") == 0)
65 return (0);
66
67 fprintf (stderr, "%s: bad parameter value: %s, parameter= %s, %s:%d\n", program, word, par, filename, lineno);
68 exit (1);
69
70 return (0);
71 }
72
get_parameter(char ** from,char * par,char * value,int size,char * filename,int lineno)73 static char *get_parameter(char **from, char *par, char *value, int size,
74 char *filename, int lineno)
75 {
76 if (**from == 0) {
77 fprintf (stderr, "%s: missing parameter: %s, %s:%d\n", program, par, filename, lineno);
78 exit (1);
79 }
80
81 copy_string(value, *from, size);
82 if (strcmp(value, "-") == 0)
83 *value = 0;
84
85 return (value);
86 }
87
get_number(char ** from,char * par,char * filename,int lineno)88 static unsigned int get_number(char **from, char *par, char *filename, int lineno)
89 {
90 char word[20];
91 unsigned int val;
92
93 get_parameter(from, par, word, sizeof(word), filename, lineno);
94 val = strtoul(word, NULL, 0);
95
96 return (val);
97 }
98
99
readconfig(config_t * config,char * filename,char * section)100 int readconfig(config_t *config, char *filename, char *section)
101 {
102 int lineno, insection, havesection, sectioncount;
103 char *p, word[80], line[300], sectname[80];
104 FILE *fp;
105
106 if ((fp = fopen(filename, "r")) == NULL) {
107 fprintf (stderr, "%s: can't open configuration file: %s\n", program, filename);
108 exit (1);
109 }
110
111 sectioncount = 0;
112 if (*section == 0) {
113 insection = 1;
114 havesection = 1;
115 }
116 else {
117 snprintf (sectname, sizeof(sectname) - 2, "[%s]", section);
118 insection = 0;
119 havesection = 0;
120 }
121
122 lineno = 0;
123 while (fgets(line, sizeof(line), fp) != NULL) {
124 lineno++;
125 if ((p = strchr(line, '#')) != NULL)
126 *p = 0;
127
128 if (*line == '[') {
129 sectioncount++;
130 if (*section == 0)
131 break;
132
133 p = line;
134 get_word(&p, word, sizeof(word));
135 if (strcmp(word, sectname) != 0)
136 insection = 0;
137 else {
138 insection = 1;
139 havesection = 1;
140 }
141
142 continue;
143 }
144
145 if (insection == 0)
146 continue;
147
148 p = skip_ws(noctrl(line));
149 if (*p == 0)
150 continue;
151
152 get_word(&p, word, sizeof(word));
153 strlwr(word);
154 p = skip_ws(p);
155
156 if (strcmp(word, "debug") == 0)
157 debug = get_yesno(&p, word, filename, lineno);
158
159 else if (strcmp(word, "acp") == 0)
160 get_parameter(&p, word, config->acp, sizeof(config->acp), filename, lineno);
161 else if (strcmp(word, "ccp") == 0)
162 get_parameter(&p, word, config->ccp, sizeof(config->ccp), filename, lineno);
163 else if (strcmp(word, "ctp") == 0)
164 get_parameter(&p, word, config->ctp, sizeof(config->ctp), filename, lineno);
165
166 else if (strcmp(word, "allow-anyremote") == 0)
167 config->allow_anyremote = get_yesno(&p, word, filename, lineno);
168 else if (strcmp(word, "allow-blanks") == 0)
169 config->allow_blanks = get_yesno(&p, word, filename, lineno);
170 else if (strcmp(word, "allow-passwdblanks") == 0)
171 config->allow_passwdblanks = get_yesno(&p, word, filename, lineno);
172 else if (strcmp(word, "extra-logging") == 0)
173 extralog = get_yesno(&p, word, filename, lineno);
174 else if (strcmp(word, "monitormode") == 0)
175 config->monitor = get_yesno(&p, word, filename, lineno);
176 else if (strcmp(word, "proxy-routing") == 0)
177 config->use_last_at = get_yesno(&p, word, filename, lineno);
178 else if (strcmp(word, "selectserver") == 0) {
179 config->selectserver = get_yesno(&p, word, filename, lineno);
180 *config->server = 0;
181 }
182 else if (strcmp(word, "server") == 0) {
183 get_parameter(&p, word, config->server, sizeof(config->server), filename, lineno);
184 config->selectserver = 0;
185 }
186 else if (strcmp(word, "serverlist") == 0)
187 config->serverlist = strdup(skip_ws(p));
188 else if (strcmp(word, "sourceip") == 0)
189 get_parameter(&p, word, config->sourceip, sizeof(config->sourceip), filename, lineno);
190
191 else if (strcmp(word, "bind") == 0) {
192 bindport = get_number(&p, word, filename, lineno);
193 daemonmode = 1;
194 }
195 else if (strcmp(word, "timeout") == 0) {
196 config->timeout = get_number(&p, word, filename, lineno);
197 if (config->timeout < 60)
198 config->timeout = 60;
199 }
200 else if (strcmp(word, "xferlog") == 0)
201 get_parameter(&p, word, config->xferlog, sizeof(config->xferlog), filename, lineno);
202 else {
203 fprintf (stderr, "%s: unknown parameter: %s, %s:%d\n", program, word, filename, lineno);
204 exit (1);
205 }
206 }
207
208 fclose (fp);
209 if (*section != 0 && sectioncount == 0)
210 havesection = 1;
211
212 return (havesection);
213 }
214
215
216
217 #define printyesno(x, y) (y != 0? printf ("%s: %s\n", x, (y == 0)? "no": "yes"): 0)
218 #define printnum(x, y) ((y > 0)? printf ("%s: %u\n", x, y): 0)
219 #define printstring(x, y) ((y != NULL && *y != 0)? printf("%s: %s\n", x, y): 0)
220
printconfig(config_t * config)221 int printconfig(config_t *config)
222 {
223 printf ("debug: %s\n", (debug == 0)? "no": "yes");
224
225 printstring ("acp", config->acp);
226 printyesno ("allow-anyremote", config->allow_anyremote);
227 printyesno ("allow-blanks", config->allow_blanks);
228 printyesno ("allow-passwdblanks", config->allow_passwdblanks);
229 printnum ("bind", bindport);
230 printstring ("ccp", config->ccp);
231 printstring ("ctp", config->ctp);
232 printyesno ("extra-logging", extralog);
233 printyesno ("monitormode", config->monitor);
234 printyesno ("proxy-routing", config->use_last_at);
235 printf ("selectserver: %s\n", (config->selectserver == 0)? "no": "yes");
236 printstring ("server", config->server);
237 printstring ("serverlist", config->serverlist);
238 printnum ("timeout", config->timeout);
239
240 return (0);
241 }
242
243