| 1 | /* |
|---|
| 2 | * chillispot.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 | #include <unistd.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <sys/types.h> |
|---|
| 27 | #include <bcmnvram.h> |
|---|
| 28 | #include <shutils.h> |
|---|
| 29 | #include <nvparse.h> |
|---|
| 30 | #include "snmp.h" |
|---|
| 31 | #include <signal.h> |
|---|
| 32 | #include <utils.h> |
|---|
| 33 | #include <syslog.h> |
|---|
| 34 | #include <wlutils.h> |
|---|
| 35 | #include <errno.h> |
|---|
| 36 | |
|---|
| 37 | #ifdef HAVE_CHILLI |
|---|
| 38 | |
|---|
| 39 | void start_chilli(void) |
|---|
| 40 | { |
|---|
| 41 | int ret = 0; |
|---|
| 42 | FILE *fp; |
|---|
| 43 | int i; |
|---|
| 44 | |
|---|
| 45 | if (!nvram_match("chilli_enable", "1")) |
|---|
| 46 | return; |
|---|
| 47 | |
|---|
| 48 | #ifdef HAVE_CHILLILOCAL |
|---|
| 49 | |
|---|
| 50 | if (!(fp = fopen("/tmp/fonusers.local", "w"))) { |
|---|
| 51 | perror("/tmp/fonusers.local"); |
|---|
| 52 | return; |
|---|
| 53 | } |
|---|
| 54 | char *users = nvram_safe_get("fon_userlist"); |
|---|
| 55 | char *u = (char *)malloc(strlen(users) + 1); |
|---|
| 56 | char *o = u; |
|---|
| 57 | |
|---|
| 58 | strcpy(u, users); |
|---|
| 59 | char *sep = strsep(&u, "="); |
|---|
| 60 | |
|---|
| 61 | while (sep != NULL) { |
|---|
| 62 | fprintf(fp, "%s ", sep); |
|---|
| 63 | char *pass = strsep(&u, " "); |
|---|
| 64 | |
|---|
| 65 | fprintf(fp, "%s \n", pass != NULL ? pass : ""); |
|---|
| 66 | sep = strsep(&u, "="); |
|---|
| 67 | } |
|---|
| 68 | free(o); |
|---|
| 69 | fclose(fp); |
|---|
| 70 | #endif |
|---|
| 71 | |
|---|
| 72 | if (!(fp = fopen("/tmp/chilli.conf", "w"))) { |
|---|
| 73 | perror("/tmp/chilli.conf"); |
|---|
| 74 | return; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | fprintf(fp, "radiusserver1 %s\n", nvram_get("chilli_radius")); |
|---|
| 78 | fprintf(fp, "radiusserver2 %s\n", nvram_get("chilli_backup")); |
|---|
| 79 | fprintf(fp, "radiussecret %s\n", nvram_get("chilli_pass")); |
|---|
| 80 | fprintf(fp, "dhcpif %s\n", nvram_safe_get("chilli_interface")); |
|---|
| 81 | |
|---|
| 82 | fprintf(fp, "uamserver %s\n", nvram_get("chilli_url")); |
|---|
| 83 | if (nvram_invmatch("chilli_dns1", "0.0.0.0") |
|---|
| 84 | && nvram_invmatch("chilli_dns1", "")) { |
|---|
| 85 | fprintf(fp, "dns1 %s\n", nvram_get("chilli_dns1")); |
|---|
| 86 | if (nvram_invmatch("sv_localdns", "0.0.0.0") |
|---|
| 87 | && nvram_invmatch("sv_localdns", "")) |
|---|
| 88 | fprintf(fp, "dns2 %s\n", nvram_get("sv_localdns")); |
|---|
| 89 | } else if (nvram_invmatch("sv_localdns", "0.0.0.0") |
|---|
| 90 | && nvram_invmatch("sv_localdns", "")) |
|---|
| 91 | fprintf(fp, "dns1 %s\n", nvram_get("sv_localdns")); |
|---|
| 92 | |
|---|
| 93 | if (nvram_invmatch("chilli_uamsecret", "")) |
|---|
| 94 | fprintf(fp, "uamsecret %s\n", nvram_get("chilli_uamsecret")); |
|---|
| 95 | if (nvram_invmatch("chilli_uamanydns", "0")) |
|---|
| 96 | fprintf(fp, "uamanydns\n"); |
|---|
| 97 | if (nvram_invmatch("chilli_uamallowed", "")) |
|---|
| 98 | fprintf(fp, "uamallowed %s\n", nvram_get("chilli_uamallowed")); |
|---|
| 99 | if (nvram_invmatch("chilli_net", "")) |
|---|
| 100 | fprintf(fp, "net %s\n", nvram_get("chilli_net")); |
|---|
| 101 | if (nvram_match("chilli_macauth", "1")) |
|---|
| 102 | fprintf(fp, "macauth\n"); |
|---|
| 103 | #ifndef HAVE_FON |
|---|
| 104 | if (nvram_match("fon_enable", "1")) { |
|---|
| 105 | #endif |
|---|
| 106 | char hyp[32]; |
|---|
| 107 | |
|---|
| 108 | strcpy(hyp, nvram_safe_get("wl0_hwaddr")); |
|---|
| 109 | for (i = 0; i < strlen(hyp); i++) |
|---|
| 110 | if (hyp[i] == ':') |
|---|
| 111 | hyp[i] = '-'; |
|---|
| 112 | if (i > 0) |
|---|
| 113 | fprintf(fp, "radiusnasid %s\n", hyp); |
|---|
| 114 | nvram_set("chilli_radiusnasid", hyp); |
|---|
| 115 | fprintf(fp, "interval 300\n"); |
|---|
| 116 | #ifndef HAVE_FON |
|---|
| 117 | } else { |
|---|
| 118 | if (nvram_invmatch("chilli_radiusnasid", "")) |
|---|
| 119 | fprintf(fp, "radiusnasid %s\n", |
|---|
| 120 | nvram_get("chilli_radiusnasid")); |
|---|
| 121 | } |
|---|
| 122 | #endif |
|---|
| 123 | |
|---|
| 124 | if (nvram_invmatch("chilli_additional", "")) { |
|---|
| 125 | char *add = nvram_safe_get("chilli_additional"); |
|---|
| 126 | |
|---|
| 127 | i = 0; |
|---|
| 128 | do { |
|---|
| 129 | if (add[i] != 0x0D) |
|---|
| 130 | fprintf(fp, "%c", add[i]); |
|---|
| 131 | } |
|---|
| 132 | while (add[++i]); |
|---|
| 133 | i = 0; |
|---|
| 134 | int a = 0; |
|---|
| 135 | char *filter = strdup(add); |
|---|
| 136 | |
|---|
| 137 | do { |
|---|
| 138 | if (add[i] != 0x0D) |
|---|
| 139 | filter[a++] = add[i]; |
|---|
| 140 | } |
|---|
| 141 | while (add[++i]); |
|---|
| 142 | |
|---|
| 143 | filter[a] = 0; |
|---|
| 144 | if (strcmp(filter, add)) { |
|---|
| 145 | nvram_set("chilli_additional", filter); |
|---|
| 146 | nvram_commit(); |
|---|
| 147 | } |
|---|
| 148 | free(filter); |
|---|
| 149 | } |
|---|
| 150 | fflush(fp); |
|---|
| 151 | fclose(fp); |
|---|
| 152 | /* |
|---|
| 153 | * if (nvram_match ("ntp_enable", "1")) { if (time(0)<1000) { sleep(10); |
|---|
| 154 | * // wait for ntp connectivity } } |
|---|
| 155 | */ |
|---|
| 156 | ret = killall("chilli", SIGTERM); |
|---|
| 157 | ret = killall("chilli", SIGKILL); |
|---|
| 158 | ret = eval("chilli", "-c", "/tmp/chilli.conf"); |
|---|
| 159 | dd_syslog(LOG_INFO, "chilli : chilli daemon successfully started\n"); |
|---|
| 160 | |
|---|
| 161 | cprintf("done\n"); |
|---|
| 162 | return; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void stop_chilli(void) |
|---|
| 166 | { |
|---|
| 167 | int ret = 0; |
|---|
| 168 | |
|---|
| 169 | if (pidof("chilli") > 0) { |
|---|
| 170 | syslog(LOG_INFO, |
|---|
| 171 | "chilli : chilli daemon successfully stopped\n"); |
|---|
| 172 | ret = killall("chilli", SIGKILL); |
|---|
| 173 | } |
|---|
| 174 | cprintf("done\n"); |
|---|
| 175 | return; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | #endif /* HAVE_CHILLI */ |
|---|