PPTP VPN fixes and improvements.
[tomato.git] / release / src / router / rc / pptp_client.c
blob58190b1c105348ac5f684589289637a5700a9431
1 /*
2 PPTP CLIENT start/stop and configuration for Tomato
3 by Jean-Yves Avenard (c) 2008-2011
4 */
6 #include "rc.h"
8 #define BUF_SIZE 128
9 #define IF_SIZE 8
11 // Line number as text string
12 #define __LINE_T__ __LINE_T_(__LINE__)
13 #define __LINE_T_(x) __LINE_T(x)
14 #define __LINE_T(x) # x
16 #define vpnlog(x...) syslog(LOG_DEBUG, __LINE_T__ ": " x)
18 void start_pptp_client(void)
20 FILE *fd;
21 int ok = 0;
22 int i;
23 char *p;
24 char buffer[BUF_SIZE];
25 char *argv[5];
26 int argc = 0;
28 sprintf(buffer, "pptpclient");
29 if ( pidof(buffer) >= 0 )
31 // PPTP already running
32 return;
34 unlink("/etc/vpn/ip-down");
35 unlink("/etc/vpn/ip-up");
36 unlink("/etc/vpn/ip-vpn");
37 unlink("/etc/vpn/options.vpn");
38 unlink("/etc/vpn");
39 unlink("/tmp/ppp");
40 mkdir("/tmp/ppp",0700);
41 mkdir("/etc/vpn",0700);
42 mkdir("/etc/vpn",0700);
43 ok |= symlink("/rom/etc/vpn/ip-down", "/etc/vpn/ip-down");
44 ok |= symlink("/rom/etc/vpn/ip-up", "/etc/vpn/ip-up");
45 // Make sure symbolic link exists
46 sprintf(buffer, "/etc/vpn/pptpclient");
47 unlink(buffer);
48 ok |= symlink("/usr/sbin/pppd", buffer);
50 if (ok)
52 stop_pptp_client();
53 return;
56 if ( (fd = fopen("/etc/vpn/options.vpn", "w")) != NULL )
58 ok = 1;
59 fprintf(fd,
60 "lock\n"
61 "noauth\n"
62 "refuse-eap\n"
63 "lcp-echo-failure 3\n"
64 "lcp-echo-interval 2\n"
65 "maxfail 0\n"
66 "persist\n"
67 "plugin pptp.so\n"
68 "pptp_server %s\n", nvram_safe_get("pptp_client_srvip"));
69 i = nvram_get_int("pptp_client_peerdns"); //0: disable, 1 enable
70 if (i > 0)
71 fprintf(fd,"usepeerdns\n");
72 fprintf(fd,"idle 0\n"
73 "ip-up-script /etc/vpn/ip-up\n"
74 "ip-down-script /etc/vpn/ip-down\n"
75 "ipparam kelokepptpd\n");
77 if ((p = nvram_get("pptp_client_mtu")) == NULL)
78 p = "1450";
79 if (!nvram_get_int("pptp_client_mtuenable"))
80 p = "1450";
81 fprintf(fd,"mtu %s\n", p);
83 if (!nvram_get_int("pptp_client_mruenable"))
85 if ((p = nvram_get("pptp_client_mru")) == NULL)
86 p = "1450";
87 fprintf(fd,"mru %s\n", p);
90 if ((p = nvram_get("pptp_client_username")) == NULL)
91 ok = 0;
92 else
93 fprintf(fd,"user %s\n", p);
95 if ((p = nvram_get("pptp_client_passwd")) == NULL)
96 ok = 0;
97 else
98 fprintf(fd,"password %s\n", p);
99 switch (get_wan_proto())
101 case WP_PPPOE:
102 case WP_PPTP:
103 case WP_L2TP:
104 p = "1";
105 break;
106 default:
107 p = "0";
108 break;
110 strcpy(buffer,"");
111 switch (nvram_get_int("pptp_client_crypt"))
113 case 1:
114 fprintf(fd, "nomppe\n");
115 break;
116 case 2:
117 fprintf(fd, "nomppe-40\n");
118 fprintf(fd, "require-mppe-128\n");
119 break;
120 case 3:
121 fprintf(fd, "require-mppe\n");
122 break;
123 default:
124 break;
126 if (!nvram_get_int("pptp_client_stateless"))
127 fprintf(fd, "mppe-stateful\n");
128 else
129 fprintf(fd, "nomppe-stateful\n");
130 fprintf(fd, "unit %s\n", p);
131 fprintf(fd, "%s\n", nvram_safe_get("pptp_client_custom"));
132 fclose(fd);
134 if (ok)
136 // force route to PPTP server via WAN
137 eval("route", "add", nvram_safe_get("pptp_client_srvip"), "gw", wan_gateway(),
138 "dev", nvram_safe_get("wan_iface"));
139 sprintf(buffer, "/etc/vpn/pptpclient file /etc/vpn/options.vpn");
140 for (argv[argc=0] = strtok(&buffer[0], " "); argv[argc] != NULL; argv[++argc] = strtok(NULL, " "));
141 if ( _eval(argv, NULL, 0, NULL) )
143 stop_pptp_client();
144 return;
147 else
148 stop_pptp_client();
152 void stop_pptp_client(void)
154 int argc;
155 char *argv[8];
156 char buffer[BUF_SIZE];
158 killall("pptpclient", SIGTERM);
160 sprintf(buffer, "rm -rf /etc/vpn/pptpclient /etc/vpn/ip-down /etc/vpn/ip-up /etc/vpn/options.vpn /tmp/ppp/resolv.conf");
161 for (argv[argc=0] = strtok(&buffer[0], " "); argv[argc] != NULL; argv[++argc] = strtok(NULL, " "));
162 _eval(argv, NULL, 0, NULL);
164 rmdir("/etc/vpn");
165 rmdir("/tmp/ppp");
168 void clear_pptp_route(void)
170 // remove route to PPTP server
171 eval("route", "del", nvram_safe_get("pptp_client_srvip"), "dev", nvram_safe_get("wan_iface"));
174 int write_pptpvpn_resolv(FILE* f)
176 FILE *dnsf;
177 int usepeer;
178 char ch;
180 if ((usepeer = nvram_get_int("pptp_client_peerdns")) <= 0)
182 vpnlog("pptp peerdns disabled");
183 return 0;
185 dnsf = fopen("/tmp/ppp/resolv.conf", "r");
186 if (dnsf == NULL)
188 vpnlog("/tmp/ppp/resolv.conf can't be opened");
189 return 0;
191 while( !feof(dnsf) )
193 ch = fgetc(dnsf);
194 fputc(ch==EOF?'\n':ch, f);
196 fclose(dnsf);
198 return (usepeer == 2) ? 1 : 0;