| 1 | /* |
|---|
| 2 | * nocat.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | #ifdef HAVE_NOCAT |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <bcmnvram.h> |
|---|
| 25 | #include <shutils.h> |
|---|
| 26 | #include <utils.h> |
|---|
| 27 | #include <syslog.h> |
|---|
| 28 | #include <signal.h> |
|---|
| 29 | #include <errno.h> |
|---|
| 30 | #include <sys/stat.h> |
|---|
| 31 | #include <services.h> |
|---|
| 32 | #define NOCAT_CONF "/tmp/etc/nocat.conf" |
|---|
| 33 | |
|---|
| 34 | /* |
|---|
| 35 | * BPsmythe: Return the local network for the NOCAT conf file |
|---|
| 36 | */ |
|---|
| 37 | static char *_get_network(char *ipaddr, char *snmask) |
|---|
| 38 | { |
|---|
| 39 | u_long ipaddr2long(char *ipstr) { |
|---|
| 40 | int ip[4]; |
|---|
| 41 | char *tmp = malloc(4 * sizeof(char)); |
|---|
| 42 | |
|---|
| 43 | ip[0] = atoi(strncpy(tmp, ipstr, strcspn(ipstr, "."))); |
|---|
| 44 | ipstr = strstr(ipstr, "."); |
|---|
| 45 | ipstr++; |
|---|
| 46 | strcpy(tmp, " "); |
|---|
| 47 | ip[1] = atoi(strncpy(tmp, ipstr, strcspn(ipstr, "."))); |
|---|
| 48 | ipstr = strstr(ipstr, "."); |
|---|
| 49 | ipstr++; |
|---|
| 50 | strcpy(tmp, " "); |
|---|
| 51 | ip[2] = atoi(strncpy(tmp, ipstr, strcspn(ipstr, "."))); |
|---|
| 52 | ipstr = strstr(ipstr, "."); |
|---|
| 53 | ipstr++; |
|---|
| 54 | strcpy(tmp, " "); |
|---|
| 55 | ip[3] = atoi(ipstr); |
|---|
| 56 | |
|---|
| 57 | free(tmp); |
|---|
| 58 | return ((ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | char *long2ipaddr(u_long addr) { |
|---|
| 62 | static char buff[32]; |
|---|
| 63 | |
|---|
| 64 | sprintf(buff, "%ld.%ld.%ld.%ld", |
|---|
| 65 | (addr >> 24 & 0xff), |
|---|
| 66 | (addr >> 16 & 0xff), (addr >> 8 & 0xff), (addr & 0xff)); |
|---|
| 67 | |
|---|
| 68 | return buff; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | static char network[32]; |
|---|
| 72 | |
|---|
| 73 | strcpy(network, long2ipaddr(ipaddr2long(ipaddr) & ipaddr2long(snmask))); |
|---|
| 74 | |
|---|
| 75 | return network; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /* |
|---|
| 79 | * end BPsmythe |
|---|
| 80 | */ |
|---|
| 81 | |
|---|
| 82 | int mk_nocat_conf(void) |
|---|
| 83 | { |
|---|
| 84 | FILE *fp; |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | * BPsmythe: Write out a nocat.conf file |
|---|
| 88 | */ |
|---|
| 89 | if (!(fp = fopen(NOCAT_CONF, "w"))) { |
|---|
| 90 | perror(NOCAT_CONF); |
|---|
| 91 | return errno; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | fprintf(fp, "#\n"); |
|---|
| 95 | |
|---|
| 96 | /* |
|---|
| 97 | * settings that need to be set based on router configurations |
|---|
| 98 | */ |
|---|
| 99 | /* |
|---|
| 100 | * These are now autodetected on WRT54G via: lan_ifname and wan_ifname |
|---|
| 101 | */ |
|---|
| 102 | /* |
|---|
| 103 | * fprintf(fp, "InternalDevice\t%s\n", nvram_safe_get("lan_ifname")); |
|---|
| 104 | */ |
|---|
| 105 | /* |
|---|
| 106 | * fprintf(fp, "ExternalDevice\t%s\n", nvram_safe_get("wan_ifname")); |
|---|
| 107 | */ |
|---|
| 108 | /* |
|---|
| 109 | * fprintf(fp, "InternalDevice\t%s\n", nvram_safe_get("NC_InternalDevice") ); |
|---|
| 110 | * fprintf(fp, "ExternalDevice\t%s\n", nvram_safe_get("NC_ExternalDevice") ); |
|---|
| 111 | * // InsideIP is now depreciated, use GatewayAddr |
|---|
| 112 | * fprintf(fp, "InsideIP\t%s\n", nvram_safe_get("lan_ipaddr")); |
|---|
| 113 | * fprintf(fp, "LocalNetwork\t%s/%s\n", |
|---|
| 114 | get_network(nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask")), |
|---|
| 115 | nvram_safe_get("lan_netmask") ); |
|---|
| 116 | */ |
|---|
| 117 | /* |
|---|
| 118 | * These are now hardcoded as the defaults |
|---|
| 119 | */ |
|---|
| 120 | // fprintf(fp, "SplashForm\t%s\n", "splash.html"); |
|---|
| 121 | // fprintf(fp, "StatusForm\t%s\n", "status.html"); |
|---|
| 122 | |
|---|
| 123 | fprintf(fp, "RouteOnly\t%s\n", nvram_safe_get("NC_RouteOnly")); |
|---|
| 124 | |
|---|
| 125 | /* |
|---|
| 126 | * These are user defined, eventually via the web page |
|---|
| 127 | */ |
|---|
| 128 | fprintf(fp, "Verbosity\t%s\n", nvram_safe_get("NC_Verbosity")); |
|---|
| 129 | fprintf(fp, "GatewayName\t%s\n", nvram_safe_get("NC_GatewayName")); |
|---|
| 130 | fprintf(fp, "GatewayAddr\t%s\n", |
|---|
| 131 | nvram_default_get("NC_GatewayAddr", |
|---|
| 132 | nvram_safe_get("lan_ipaddr"))); |
|---|
| 133 | fprintf(fp, "InternalDevice\t%s\n", |
|---|
| 134 | nvram_default_get("NC_ifname", nvram_safe_get("lan_ifname"))); |
|---|
| 135 | fprintf(fp, "GatewayPort\t%s\n", nvram_safe_get("NC_GatewayPort")); |
|---|
| 136 | if (nvram_match("port_swap", "1")) |
|---|
| 137 | fprintf(fp, "GatewayMAC\t%s\n", nvram_safe_get("et1macaddr")); |
|---|
| 138 | else |
|---|
| 139 | fprintf(fp, "GatewayMAC\t%s\n", nvram_safe_get("et0macaddr")); |
|---|
| 140 | fprintf(fp, "GatewayPassword\t%s\n", nvram_safe_get("NC_Password")); |
|---|
| 141 | fprintf(fp, "GatewayMode\t%s\n", nvram_safe_get("NC_GatewayMode")); |
|---|
| 142 | fprintf(fp, "DocumentRoot\t%s\n", nvram_safe_get("NC_DocumentRoot")); |
|---|
| 143 | if (nvram_invmatch("NC_SplashURL", "")) { |
|---|
| 144 | fprintf(fp, "SplashURL\t%s\n", nvram_safe_get("NC_SplashURL")); |
|---|
| 145 | fprintf(fp, "SplashURLTimeout\t%s\n", |
|---|
| 146 | nvram_safe_get("NC_SplashURLTimeout")); |
|---|
| 147 | } |
|---|
| 148 | /* |
|---|
| 149 | * do we really need this? |
|---|
| 150 | */ |
|---|
| 151 | fprintf(fp, "LeaseFile\t%s\n", |
|---|
| 152 | nvram_default_get("NC_LeaseFile", "/tmp/nocat.leases")); |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | * Open-mode and common options |
|---|
| 156 | */ |
|---|
| 157 | fprintf(fp, "FirewallPath\t%s\n", "/usr/libexec/nocat/"); |
|---|
| 158 | fprintf(fp, "ExcludePorts\t%s\n", nvram_safe_get("NC_ExcludePorts")); |
|---|
| 159 | fprintf(fp, "IncludePorts\t%s\n", nvram_safe_get("NC_IncludePorts")); |
|---|
| 160 | fprintf(fp, "AllowedWebHosts\t%s %s\n", nvram_safe_get("lan_ipaddr"), |
|---|
| 161 | nvram_safe_get("NC_AllowedWebHosts")); |
|---|
| 162 | /* |
|---|
| 163 | * TJaqua: Added MACWhiteList to ignore given machines or routers on the |
|---|
| 164 | * local net (e.g. routers with an alternate Auth). |
|---|
| 165 | */ |
|---|
| 166 | fprintf(fp, "MACWhiteList\t%s\n", nvram_safe_get("NC_MACWhiteList")); |
|---|
| 167 | /* |
|---|
| 168 | * TJaqua: Added AnyDNS to pass through any client-defined servers. |
|---|
| 169 | */ |
|---|
| 170 | if (!strcmp(nvram_safe_get("NC_AnyDNS"), "1")) { |
|---|
| 171 | fprintf(fp, "AnyDNS\t%s\n", nvram_safe_get("NC_AnyDNS")); |
|---|
| 172 | } else { |
|---|
| 173 | /* |
|---|
| 174 | * Irving - Rework getting DNS |
|---|
| 175 | */ |
|---|
| 176 | struct dns_lists *dns_list = NULL; |
|---|
| 177 | |
|---|
| 178 | dns_list = get_dns_list(); |
|---|
| 179 | if (!dns_list || dns_list->num_servers == 0) { |
|---|
| 180 | fprintf(fp, "DNSAddr \t%s\n", |
|---|
| 181 | nvram_safe_get("lan_ipaddr")); |
|---|
| 182 | } else { |
|---|
| 183 | fprintf(fp, "DNSAddr \t%s %s %s\n", |
|---|
| 184 | dns_list->dns_server[0], |
|---|
| 185 | dns_list->dns_server[1], |
|---|
| 186 | dns_list->dns_server[2]); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | fprintf(fp, "HomePage\t%s\n", nvram_safe_get("NC_HomePage")); |
|---|
| 190 | fprintf(fp, "ForcedRedirect\t%s\n", |
|---|
| 191 | nvram_safe_get("NC_ForcedRedirect")); |
|---|
| 192 | // fprintf( fp, "PeerCheckTimeout\t%s\n", |
|---|
| 193 | // nvram_safe_get( "NC_PeerChecktimeout" ) ); |
|---|
| 194 | fprintf(fp, "IdleTimeout\t%s\n", nvram_safe_get("NC_IdleTimeout")); |
|---|
| 195 | fprintf(fp, "MaxMissedARP\t%s\n", nvram_safe_get("NC_MaxMissedARP")); |
|---|
| 196 | fprintf(fp, "LoginTimeout\t%s\n", nvram_safe_get("NC_LoginTimeout")); |
|---|
| 197 | fprintf(fp, "RenewTimeout\t%s\n", nvram_safe_get("NC_RenewTimeout")); |
|---|
| 198 | |
|---|
| 199 | /* |
|---|
| 200 | * defined for RADIUS fprintf(fp, "AuthServiceAddr\t%s\n", |
|---|
| 201 | * nvram_safe_get("NC_AuthServiceAddr") ); fprintf(fp, "LoginPage\t%s\n", |
|---|
| 202 | * nvram_safe_get("NC_LoginPage") ); fprintf(fp, "ConfirmPage\t%s\n", |
|---|
| 203 | * nvram_safe_get("NC_ConfirmPage") ); fprintf(fp, "LogoutPage\t%s\n", |
|---|
| 204 | * nvram_safe_get("NC_LogoutPage") ); fprintf(fp, |
|---|
| 205 | * "RADIUSAuthServer\t%s\n", nvram_safe_get("NC_RADIUSAuthServer") ); |
|---|
| 206 | * fprintf(fp, "RADIUSAuthPort\t%s\n", |
|---|
| 207 | * nvram_safe_get("NC_RADIUSAuthPort") ); fprintf(fp, |
|---|
| 208 | * "RADIUSAuthSecret\t%s\n", nvram_safe_get("NC_RADIUSAuthSecret") ); |
|---|
| 209 | * fprintf(fp, "RADIUSAuthNASIdentifier\t%s\n", |
|---|
| 210 | * nvram_safe_get("NC_RADIUSAuthNASIdentifier") ); fprintf(fp, |
|---|
| 211 | * "RADIUSAuthWait\t%s\n", nvram_safe_get("NC_RADIUSAuthWait") ); |
|---|
| 212 | * fprintf(fp, "RADIUSAuthRetries\t%s\n", |
|---|
| 213 | * nvram_safe_get("NC_RADIUSAuthRetries") ); fprintf(fp, |
|---|
| 214 | * "RADIUSAcctServer\t%s\n", nvram_safe_get("NC_RADIUSAcctServer") ); |
|---|
| 215 | * fprintf(fp, "RADIUSAcctPort\t%s\n", |
|---|
| 216 | * nvram_safe_get("NC_RADIUSAcctPort") ); fprintf(fp, |
|---|
| 217 | * "RADIUSAcctSecret\t%s\n", nvram_safe_get("NC_RADIUSAcctSecret") ); |
|---|
| 218 | * fprintf(fp, "RADIUSAcctNASIdentifier\t%s\n", |
|---|
| 219 | * nvram_safe_get("NC_RADIUSAcctNASIdentifier") ); fprintf(fp, |
|---|
| 220 | * "RADIUSAcctWait\t%s\n", nvram_safe_get("NC_RADIUSAcctWait") ); |
|---|
| 221 | * fprintf(fp, "RADIUSAcctRetries\t%s\n", |
|---|
| 222 | * nvram_safe_get("NC_RADIUSAcctRetries") ); |
|---|
| 223 | */ |
|---|
| 224 | |
|---|
| 225 | /* |
|---|
| 226 | * defined for second radius server fprintf(fp, |
|---|
| 227 | * "RADIUSAuth1Server\t%s\n", nvram_safe_get("NC_RADIUSAuth1Server") ); |
|---|
| 228 | * fprintf(fp, "RADIUSAuth1Port\t%s\n", |
|---|
| 229 | * nvram_safe_get("NC_RADIUSAuth1Port") ); fprintf(fp, |
|---|
| 230 | * "RADIUSAuth1Secret\t%s\n", nvram_safe_get("NC_RADIUSAuth1Secret") ); |
|---|
| 231 | * fprintf(fp, "RADIUSAuth1NASIdentifier\t%s\n", |
|---|
| 232 | * nvram_safe_get("NC_RADIUSAuth1NASIdentifier") ); fprintf(fp, |
|---|
| 233 | * "RADIUSAcct1Server\t%s\n", nvram_safe_get("NC_RADIUSAcct1Server") ); |
|---|
| 234 | * fprintf(fp, "RADIUSAcct1Port\t%s\n", |
|---|
| 235 | * nvram_safe_get("NC_RADIUSAcct1Port") ); fprintf(fp, |
|---|
| 236 | * "RADIUSAcct1Secret\t%s\n", nvram_safe_get("NC_RADIUSAcct1Secret") ); |
|---|
| 237 | * fprintf(fp, "RADIUSAcct1NASIdentifier\t%s\n", |
|---|
| 238 | * nvram_safe_get("NC_RADIUSAcct1NASIdentifier") ); |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | fclose(fp); |
|---|
| 242 | /* |
|---|
| 243 | * end BPsmythe |
|---|
| 244 | */ |
|---|
| 245 | fprintf(stderr, "Wrote: %s\n", NOCAT_CONF); |
|---|
| 246 | |
|---|
| 247 | return 0; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | void start_splashd(void) |
|---|
| 251 | { |
|---|
| 252 | int ret = 0; |
|---|
| 253 | FILE *fp; |
|---|
| 254 | |
|---|
| 255 | if (!nvram_match("NC_enable", "1")) |
|---|
| 256 | return; |
|---|
| 257 | |
|---|
| 258 | /* |
|---|
| 259 | * Irving - make sure our WAN link is up first. if not, check_ps will |
|---|
| 260 | * start us later |
|---|
| 261 | */ |
|---|
| 262 | if (nvram_match("wan_ipaddr", "0.0.0.0")) |
|---|
| 263 | return; |
|---|
| 264 | insmod("ipt_mark"); |
|---|
| 265 | insmod("ipt_mac"); |
|---|
| 266 | insmod("xt_mark"); |
|---|
| 267 | insmod("xt_mac"); |
|---|
| 268 | |
|---|
| 269 | mk_nocat_conf(); |
|---|
| 270 | |
|---|
| 271 | if (!(fp = fopen("/tmp/start_splashd.sh", "w"))) { |
|---|
| 272 | perror("/tmp/start_splashd.sh"); |
|---|
| 273 | return; |
|---|
| 274 | } |
|---|
| 275 | fprintf(fp, "#!/bin/sh\n"); |
|---|
| 276 | fprintf(fp, "sleep 20\n"); |
|---|
| 277 | fprintf(fp, "splashd >> /tmp/nocat.log 2>&1 &\n"); |
|---|
| 278 | fclose(fp); |
|---|
| 279 | chmod("/tmp/start_splashd.sh", 0700); |
|---|
| 280 | system2("/tmp/start_splashd.sh&"); |
|---|
| 281 | dd_syslog(LOG_INFO, "splashd : splash daemon successfully started\n"); |
|---|
| 282 | |
|---|
| 283 | cprintf("done\n"); |
|---|
| 284 | return; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | void stop_splashd(void) |
|---|
| 288 | { |
|---|
| 289 | |
|---|
| 290 | if (pidof("splashd") > 0) { |
|---|
| 291 | dd_syslog(LOG_INFO, |
|---|
| 292 | "splashd : splash daemon successfully stopped\n"); |
|---|
| 293 | killall("splashd", SIGTERM); |
|---|
| 294 | eval("/usr/libexec/nocat/clear.fw"); |
|---|
| 295 | stop_firewall(); // evil |
|---|
| 296 | stop_wland(); |
|---|
| 297 | stop_wshaper(); |
|---|
| 298 | start_firewall(); |
|---|
| 299 | start_wshaper(); |
|---|
| 300 | start_wland(); |
|---|
| 301 | cprintf("done\n"); |
|---|
| 302 | } |
|---|
| 303 | return; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | #endif |
|---|