]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - src/install+setup/install/upgrade-v12-v13.c
2cbedb84678658642c05735d53ff36fb030567b4
[people/teissler/ipfire-2.x.git] / src / install+setup / install / upgrade-v12-v13.c
1 /*
2 * This file is part of the IPCop Firewall.
3 *
4 * IPCop is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * IPCop is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with IPCop; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright 2002: Mark Wormgoor <mark@wormgoor.com>
19 *
20 * $Id: upgrade-v12-v13.c,v 1.2.2.3 2004/11/11 09:39:25 alanh Exp $
21 *
22 */
23
24 #include "install.h"
25
26 void _convert_ppp_settings() {
27 DIR *dirp;
28 struct dirent *dp;
29 char filename[STRING_SIZE];
30
31 dirp = opendir( "/harddisk/var/ipfire/ppp" );
32 while ( (dp = readdir( dirp )) != NULL ) {
33 if ( strstr( dp->d_name, "settings" ) == dp->d_name ) {
34 snprintf (filename, STRING_SIZE-1, "%s/%s",
35 "/harddisk/var/ipfire/ppp", dp->d_name);
36
37 /* reduce furthur replacements from commands below */
38 replace (filename, "TYPE=modem", "");
39 replace (filename, "COMPORT=ttyS0", "TYPE=modem\nCOMPORT=ttyS0");
40 replace (filename, "COMPORT=ttyS1", "TYPE=modem\nCOMPORT=ttyS1");
41 replace (filename, "COMPORT=ttyS2", "TYPE=modem\nCOMPORT=ttyS2");
42 replace (filename, "COMPORT=ttyS3", "TYPE=modem\nCOMPORT=ttyS3");
43 /* reduce furthur replacements from commands below */
44 replace (filename, "TYPE=isdn", "");
45 replace (filename, "COMPORT=isdn1", "TYPE=isdn\nCOMPORT=isdn1");
46 replace (filename, "COMPORT=isdn2", "TYPE=isdn\nCOMPORT=isdn2");
47 replace (filename, "COMPORT=pppoe", "TYPE=pppoe");
48 replace (filename, "COMPORT=pptp", "TYPE=pptp");
49 replace (filename, "COMPORT=usbadsl", "TYPE=alcatelusb");
50 replace (filename, "COMPORT=pppoa", "TYPE=pulsardsl");
51
52 chown (filename, 99, 99);
53 }
54 }
55 (void) closedir( dirp );
56 }
57
58 int _convert_xtaccess() {
59 int count=1, count2=0;
60 FILE *portfw1, *portfw2;
61 char portsrctemp[STRING_SIZE], portdsttemp[STRING_SIZE];
62 char *portproto, *portsrcip, *portsrcport, *portdstip, *portdstport, *portenabled, *portremip;
63
64 FILE *xtaccess1, *xtaccess2;
65 char xtsrctemp[STRING_SIZE], xtdsttemp[STRING_SIZE];
66 char *xtproto, *xtsrcip, *xtdstip, *xtdstport, *xtenabled;
67
68 if (!(portfw1 = fopen ("/harddisk/var/ipfire/portfw/config", "r"))) return 1;
69 if (!(portfw2 = fopen ("/harddisk/var/ipfire/portfw/config.new", "w")))
70 {
71 fclose(portfw1);
72 return 1;
73 }
74
75 while (fgets (portsrctemp, STRING_SIZE, portfw1) != NULL) {
76 count2 = 0;
77 portproto = NULL;
78 portsrcip = NULL;
79 portsrcport = NULL;
80 portdstip = NULL;
81 portdstport = NULL;
82 portremip = NULL;
83 portenabled = NULL;
84
85 if (!(xtaccess1 = fopen ("/harddisk/var/ipfire/xtaccess/config", "r")))
86 {
87 fclose(portfw1);
88 fclose(portfw2);
89 return 1;
90 }
91 if (!(xtaccess2 = fopen ("/harddisk/var/ipfire/xtaccess/config.new", "w")))
92 {
93 fclose(portfw1);
94 fclose(portfw2);
95 fclose(xtaccess1);
96 return 1;
97 }
98
99 if (isdigit (portsrctemp[0])) {
100 /* Already converted to new format */
101 fputs(portsrctemp, portfw2);
102 continue;
103 }
104
105 if (portsrctemp[strlen(portsrctemp) - 1] == '\n')
106 portsrctemp[strlen(portsrctemp) - 1] = '\0';
107 portproto = strtok (portsrctemp, ",");
108 portsrcport = strtok (NULL, ",");
109 portdstip = strtok (NULL, ",");
110 portdstport = strtok (NULL, ",");
111 portenabled = strtok (NULL, ",");
112 portsrcip = strtok (NULL, ",");
113 portremip = strtok (NULL, ",");
114 if (!(portproto && portsrcport && portdstip &&
115 portdstport && portenabled ))
116 continue;
117
118 if (portsrcip == NULL) portsrcip = strdup ("0.0.0.0");
119
120 while (fgets (xtsrctemp, STRING_SIZE, xtaccess1)) {
121 xtproto = NULL;
122 xtsrcip = NULL;
123 xtdstip = NULL;
124 xtdstport = NULL;
125 xtenabled = NULL;
126
127 if (xtsrctemp[strlen(xtsrctemp) - 1] == '\n')
128 xtsrctemp[strlen(xtsrctemp) - 1] = '\0';
129 xtproto = strtok (xtsrctemp, ",");
130 xtsrcip = strtok (NULL, ",");
131 xtdstport = strtok (NULL, ",");
132 xtenabled = strtok (NULL, ",");
133 xtdstip = strtok (NULL, ",");
134 if (!(xtproto && xtsrcip && xtdstport && xtenabled)) continue;
135
136 if (xtdstip == NULL) xtdstip = strdup ("0.0.0.0");
137
138 if (strcmp (portproto, xtproto) == 0 &&
139 strcmp (portsrcport, xtdstport) == 0 &&
140 strcmp (portsrcip, xtdstip) == 0) {
141 portremip = strdup (xtsrcip);
142 if ((strcmp (portremip, "0.0.0.0/0") == 0) && (count2 == 0)) {
143 sprintf (portdsttemp, "%d,%d,%s,%s,%s,%s,%s,%s,%s\n",
144 count, count2, portproto, portsrcport, portdstip,
145 portdstport, portenabled, portsrcip, portremip);
146 fputs(portdsttemp, portfw2);
147 } else {
148 if (count2 == 0) {
149 sprintf (portdsttemp, "%d,%d,%s,%s,%s,%s,%s,%s,%d\n",
150 count,count2,portproto, portsrcport, portdstip,
151 portdstport, portenabled, portsrcip, 0);
152 fputs(portdsttemp, portfw2);
153 count2++;
154 }
155 sprintf (portdsttemp, "%d,%d,%s,%d,%s,%s,%s,%d,%s\n",
156 count,count2,portproto, 0, portdstip,
157 portdstport, portenabled, 0, portremip);
158 fputs(portdsttemp, portfw2);
159 }
160 count2++;
161 } else {
162 sprintf (xtdsttemp, "%s,%s,%s,%s,%s\n",
163 xtproto, xtsrcip, xtdstport, xtenabled, xtdstip);
164 fputs(xtdsttemp, xtaccess2);
165 }
166 }
167
168 /* Close source and destination xtaccess files */
169 fclose (xtaccess1);
170 fclose (xtaccess2);
171
172 /* Move the new xtaccess file */
173 rename ("/harddisk/var/ipfire/xtaccess/config.new",
174 "/harddisk/var/ipfire/xtaccess/config");
175
176 /* If no external access line existed, add a no access line */
177 if (count2 == 0) {
178 if (portremip == NULL) portremip = strdup ("127.0.0.1/32");
179
180 /* Print new port forwarding line to file */
181 sprintf (portdsttemp, "%d,%d,%s,%s,%s,%s,%s,%s,%s\n",
182 count, count2, portproto, portsrcport, portdstip,
183 portdstport, portenabled, portsrcip, portremip);
184 fputs(portdsttemp, portfw2);
185 }
186 count++;
187 }
188
189 /* Close source and destination portfw files */
190 fclose (portfw1);
191 fclose (portfw2);
192
193 /* Move the new portfw file */
194 rename ("/harddisk/var/ipfire/portfw/config.new",
195 "/harddisk/var/ipfire/portfw/config");
196 chown ("/harddisk/var/ipfire/xtaccess/config", 99, 99);
197 chown ("/harddisk/var/ipfire/portfw/config", 99, 99);
198
199 return 0;
200 }
201
202 int _convert_pulsardsl() {
203 DIR *dirp;
204 struct dirent *dp;
205 char filename[STRING_SIZE];
206 FILE *settings, *pulsardsl;
207 char line[STRING_SIZE];
208
209 if (!(pulsardsl = fopen ("/harddisk/var/ipfire/pciadsl/settings", "r"))) return 1;
210
211 dirp = opendir( "/harddisk/var/ipfire/ppp" );
212 while ( (dp = readdir( dirp )) != NULL ) {
213 if ( strstr( dp->d_name, "settings" ) == dp->d_name ) {
214 snprintf (filename, STRING_SIZE-1, "%s/%s",
215 "/harddisk/var/ipfire/ppp", dp->d_name);
216 if (!(settings = fopen (filename, "r+"))) {
217 closedir(dirp);
218 fclose(pulsardsl);
219 return 1;
220 }
221 while (fgets (line, STRING_SIZE, settings) != NULL) {
222 if (strstr (line, "TYPE=pulsardsl") == line) {
223 fseek(settings,0,SEEK_END);
224 rewind(pulsardsl);
225 while (fgets(line, STRING_SIZE, pulsardsl) != NULL) {
226 fputs (line, settings);
227 }
228 fclose (settings);
229 chown (filename, 99, 99);
230 }
231 }
232 }
233 }
234 fclose(pulsardsl);
235 (void) closedir( dirp );
236
237
238 return 0;
239 }
240
241 int _convert_pulsardsl_ethernet() {
242 DIR *dirp;
243 struct dirent *dp;
244 FILE *ethernet, *settings;
245 char line[STRING_SIZE];
246 char type[STRING_SIZE];
247 char ip[STRING_SIZE];
248 char filename[STRING_SIZE];
249
250 if (!(ethernet = fopen ("/harddisk/var/ipfire/ethernet/settings", "r"))) return 1;
251
252 while (fgets (line, STRING_SIZE, ethernet) != NULL) {
253 if (strstr (line, "RED_DRIVER=pciadsl") == line) {
254 rewind (ethernet);
255 while (fgets (line, STRING_SIZE, ethernet) != NULL) {
256 if (strstr (line, "RED_TYPE") == line) {
257 strcpy (type, line + 9*sizeof(char));
258 if (type[strlen(type) - 1] == '\n')
259 type[strlen(type) - 1] = '\0';
260 }
261 if (strstr (line, "RED_ADDRESS") == line) {
262 strcpy (ip, line + 12*sizeof(char));
263 if (ip[strlen(ip) - 1] == '\n')
264 type[strlen(ip) - 1] = '\0';
265 }
266 fclose (ethernet);
267
268 replace ("/harddisk/var/ipfire/ethernet/settings", "RED_DEV=eth1", "RED_DEV=");
269 replace ("/harddisk/var/ipfire/ethernet/settings", "CONFIG_TYPE=2", "CONFIG_TYPE=0");
270 replace ("/harddisk/var/ipfire/ethernet/settings", "CONFIG_TYPE=3", "CONFIG_TYPE=1");
271 replace ("/harddisk/var/ipfire/ethernet/settings", "RED_DEV=eth2", "RED_DEV=");
272 chown ("/harddisk/var/ipfire/ethernet/settings", 99, 99);
273
274 dirp = opendir( "/harddisk/var/ipfire/ppp" );
275 while ( (dp = readdir( dirp )) != NULL ) {
276 if ( strstr( dp->d_name, "settings-" ) == dp->d_name ) {
277 snprintf (filename, STRING_SIZE-1, "%s/%s",
278 "/harddisk/var/ipfire/ppp", dp->d_name);
279 if (!(settings = fopen (filename, "r+")))
280 {
281 closedir(dirp);
282 return 1;
283 }
284 while (fgets (line, STRING_SIZE, settings) != NULL) {
285 if (strstr (line, "TYPE=pulsardsl") == line) {
286 fseek(settings,0,SEEK_END);
287 fprintf (settings, "METHOD=%s\n", type);
288 fprintf (settings, "IP=%s\n", ip);
289 fclose (settings);
290 chown (filename, 99, 99);
291 }
292 }
293 }
294 }
295 (void) closedir( dirp );
296 }
297 }
298 }
299
300 return 0;
301 }
302
303 int upgrade_v12_v13() {
304 struct stat s;
305 replace ("/harddisk/var/ipfire/ethernet/settings", "rtl8139", "8139too");
306 replace ("/harddisk/var/ipfire/vpn/ipsec.conf", "auto=add", "auto=start");
307 chown ("/harddisk/var/ipfire/vpn/ipsec.conf", 99, 99);
308 chown ("/harddisk/var/ipfire/ethernet/settings", 99, 99);
309 chown ("/harddisk/var/ipfire/main/settings", 99, 99);
310 _convert_ppp_settings();
311 _convert_xtaccess();
312 _convert_pulsardsl();
313 _convert_pulsardsl_ethernet();
314
315 /* Rename usbadsl directory */
316 stat ("/harddisk/var/ipfire/usbadsl", &s);
317 if (S_ISDIR(s.st_mode)) {
318 remove ("/harddisk/var/ipfire/usbadsl/settings");
319 if (! system("/bin/chroot /harddisk /bin/rm -rf /var/ipfire/alcatelusb"))
320 rename ("/harddisk/var/ipfire/usbadsl", "/harddisk/var/ipfire/alcatelusb");
321 }
322
323 /* Rename pciadsl module and directory */
324 remove ("/harddisk/var/ipfire/pulsar/settings");
325 rename ("/harddisk/var/ipfire/pciadsl/pciadsl.o", "/harddisk/var/ipfire/pciadsl/pulsar.o");
326 stat ("/harddisk/var/ipfire/pciadsl", &s);
327 if (S_ISDIR(s.st_mode)) {
328 if (! system("/bin/chroot /harddisk /bin/rm -rf /var/ipfire/pulsardsl"))
329 rename ("/harddisk/var/ipfire/pciadsl", "/harddisk/var/ipfire/pulsardsl");
330 }
331
332 /* Change squid cache directory */
333 replace ("/harddisk/var/ipfire/proxy/squid.conf", "/var/spool/squid", "/var/log/cache");
334 chown ("/harddisk/var/ipfire/proxy/squid.conf", 99, 99);
335
336 /* Change setup user shell */
337 replace ("/harddisk/etc/passwd", ":/usr/local/sbin/setup", ":/bin/bash -c /usr/local/sbin/setup");
338
339 return 0;
340 }