]>
Commit | Line | Data |
---|---|---|
0e035311 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2012 IPFire Network Development Team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
49ec20d8 | 22 | HOSTAPD_CONTROL_INTERFACE_DIR="/run/hostapd/ctrl" |
0e035311 MT |
23 | |
24 | function hostapd_config_write() { | |
25 | local device=${1} | |
49ec20d8 | 26 | assert isset device |
0e035311 | 27 | |
49ec20d8 MT |
28 | local file=${2} |
29 | assert isset file | |
30 | ||
31 | # Shift the device and file argument. | |
32 | shift 2 | |
0e035311 MT |
33 | |
34 | local broadcast_ssid | |
35 | local channel | |
36 | local country_code | |
37 | local encryption | |
4ed541a9 | 38 | local ieee80211d="1" |
0e035311 MT |
39 | local key |
40 | local mode | |
41 | local ssid | |
42 | ||
43 | while [ $# -gt 0 ]; do | |
44 | case "${1}" in | |
45 | --broadcast-ssid=*) | |
49ec20d8 | 46 | broadcast_ssid=$(cli_get_val ${1}) |
0e035311 MT |
47 | ;; |
48 | --channel=*) | |
49ec20d8 | 49 | channel=$(cli_get_val ${1}) |
0e035311 MT |
50 | ;; |
51 | --country-code=*) | |
49ec20d8 | 52 | country_code=$(cli_get_val ${1}) |
0e035311 MT |
53 | ;; |
54 | --mode=*) | |
49ec20d8 | 55 | mode=$(cli_get_val ${1}) |
0e035311 MT |
56 | ;; |
57 | --ssid=*) | |
49ec20d8 | 58 | ssid=$(cli_get_val ${1}) |
0e035311 MT |
59 | ;; |
60 | --encryption=*) | |
61 | encryption=$(cli_get_val ${1}) | |
62 | ;; | |
4ed541a9 MT |
63 | --ieee80211d=*) |
64 | local val="$(cli_get_val "${1}")" | |
65 | if enabled val; then | |
66 | ieee80211d="1" | |
67 | else | |
68 | ieee80211d="0" | |
69 | fi | |
70 | ;; | |
0e035311 MT |
71 | --key=*) |
72 | key=$(cli_get_val ${1}) | |
73 | ;; | |
74 | *) | |
75 | warning_log "Ignoring unknown argument '${1}'." | |
76 | ;; | |
77 | esac | |
78 | shift | |
79 | done | |
80 | ||
81 | assert isset broadcast_ssid | |
82 | assert isbool broadcast_ssid | |
83 | ||
84 | assert isset channel | |
85 | assert isinteger channel | |
86 | ||
87 | assert isset country_code | |
88 | assert isset mode | |
89 | assert isset ssid | |
90 | ||
91 | # Check if key is set when encryption is used. | |
92 | if isset encryption; then | |
93 | assert isoneof encryption WPA WPA2 WPA/WPA2 | |
94 | assert isset key | |
95 | fi | |
96 | ||
49ec20d8 MT |
97 | # Create configuration directory. |
98 | local config_dir=$(dirname ${file}) | |
99 | mkdir -p ${HOSTAPD_CONTROL_INTERFACE_DIR} ${config_dir} 2>/dev/null | |
100 | ||
101 | config_header "hostapd" > ${file} | |
102 | ||
103 | # Interface configuration | |
104 | ( | |
105 | print "# Interface configuration" | |
106 | print "driver=nl80211" | |
107 | print "interface=${device}" | |
108 | ||
109 | ) >> ${file} | |
110 | ||
111 | # Wireless configuration | |
0e035311 MT |
112 | local ignore_broadcast_ssid |
113 | if enabled broadcast_ssid; then | |
114 | ignore_broadcast_ssid="0" | |
115 | else | |
116 | ignore_broadcast_ssid="1" | |
117 | fi | |
118 | ||
119 | local hw_mode ieee80211n="0" | |
120 | if [ "${mode}" = "n" ]; then | |
121 | if [ ${channel} -le 15 ]; then | |
122 | hw_mode="g" | |
123 | else | |
124 | hw_mode="a" | |
125 | fi | |
126 | ieee80211n="1" | |
ba5bb630 MT |
127 | else |
128 | hw_mode="${mode}" | |
0e035311 MT |
129 | fi |
130 | ||
49ec20d8 MT |
131 | ( |
132 | print "# Wireless configuration" | |
133 | print "channel=${channel}" | |
134 | print "country_code=${country_code}" | |
135 | print "hw_mode=${hw_mode}" | |
4ed541a9 | 136 | print "ieee80211d=${ieee80211d}" |
49ec20d8 MT |
137 | print "ieee80211n=${ieee80211n}" |
138 | print "ignore_broadcast_ssid=${ignore_broadcast_ssid}" | |
0e035311 | 139 | |
49ec20d8 MT |
140 | if contains_spaces "${ssid}"; then |
141 | print "ssid=\"${ssid}\"" | |
142 | else | |
143 | print "ssid=${ssid}" | |
144 | fi | |
0e035311 | 145 | |
49ec20d8 MT |
146 | |
147 | ) >> ${file} | |
0e035311 | 148 | |
49ec20d8 MT |
149 | # Control interface. |
150 | ( | |
151 | print "# Control interface" | |
152 | print "ctrl_interface=${HOSTAPD_CONTROL_INTERFACE_DIR}" | |
153 | print "ctrl_interface_group=0" | |
154 | ||
155 | ) >> ${file} | |
0e035311 | 156 | |
49ec20d8 | 157 | # Encryption settings |
0e035311 MT |
158 | if isset encryption; then |
159 | local encryption_mode=0 | |
160 | case "${encryption}" in | |
161 | WPA) | |
162 | encryption_mode=1 | |
163 | ;; | |
164 | WPA2) | |
165 | encryption_mode=2 | |
166 | ;; | |
167 | WPA/WPA2) | |
168 | encryption_mode=3 | |
169 | ;; | |
170 | esac | |
171 | ||
49ec20d8 MT |
172 | ( |
173 | print "# Encryption settings" | |
174 | print "wpa=${encryption_mode}" | |
175 | print "wpa_passphrase=${key}" | |
176 | print "wpa_key_mgmt=WPA-PSK" | |
177 | print "wpa_pairwise=TKIP" | |
178 | print "rsn_pairwise=CCMP" | |
179 | ||
180 | ) >> ${file} | |
0e035311 MT |
181 | fi |
182 | ||
183 | return ${EXIT_OK} | |
184 | } | |
185 | ||
186 | function hostapd_start() { | |
187 | local device=${1} | |
0e035311 MT |
188 | assert isset device |
189 | ||
0e035311 MT |
190 | service_start "hostapd@${device}.service" |
191 | local ret=$? | |
192 | ||
49ec20d8 MT |
193 | if [ ${ret} -eq ${EXIT_OK} ]; then |
194 | log DEBUG "hostapd has been successfully started on '${device}'" | |
195 | else | |
196 | log ERROR "Could not start hostapd on '${device}': ${ret}" | |
197 | return ${EXIT_ERROR} | |
198 | fi | |
199 | ||
200 | return ${EXIT_OK} | |
0e035311 MT |
201 | } |
202 | ||
203 | function hostapd_stop() { | |
204 | local device=${1} | |
205 | assert isset device | |
206 | ||
207 | service_stop "hostapd@${device}.service" | |
0e035311 | 208 | } |