2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 # Parse the command line
23 while [ $# -gt 0 ]; do
33 [ -n "${action}" ] && break
36 .
/usr
/lib
/network
/functions
38 # Read network settings
42 if cli_help_requested
"$@"; then
43 cli_show_man network-settings
47 if [ -n "${1}" ]; then
48 network_settings_set
"$@"
49 network_settings_write
51 network_settings_print
56 if cli_help_requested
"$@"; then
57 cli_show_man network-device
69 local device
="${action}"
73 if ! isset device
; then
74 cli_show_man network-device
78 assert device_exists
${device}
82 cli_device_discover
${device} "$@"
85 device_identify
"${device}" "$@"
88 cli_device_monitor
"${device}" "$@"
91 cli_device_status
${device}
94 cli_device_serial_unlock
${device} "$@"
97 cli_device_send_ussd_command
"${device}" "$@"
100 cli_show_man network-device
109 cli_device_status
() {
113 # Disable debugging output here.
114 local log_disable_stdout
=${LOG_DISABLE_STDOUT}
115 LOG_DISABLE_STDOUT
="true"
117 # Save the type of the device for later.
118 local type=$
(device_get_type
${device})
120 cli_headline
1 "Device status: ${device}"
121 cli_print_fmt1
1 "Name" "${device}"
123 # Handle special devices
126 cli_device_status_phy
"${device}"
130 cli_device_status_serial
"${device}"
135 # Print the device status.
136 device_is_up
${device} &>/dev
/null
141 status
="${CLR_GREEN_B}UP${CLR_RESET}"
144 status
="${CLR_RED_B}DOWN${CLR_RESET}"
148 cli_print_fmt1
1 "Status" "${status}"
149 cli_print_fmt1
1 "Type" "${type}"
151 # Ethernet-compatible?
152 device_is_ethernet_compatible
"${device}" &>/dev
/null
153 cli_print_fmt1
1 "Ethernet-compatible" "$(cli_print_bool $?)"
155 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
158 # Print the link speed for ethernet devices.
159 if device_is_up
${device} &>/dev
/null
; then
160 local link
="$(device_get_link_string "${device}")"
162 cli_print_fmt1
1 "Link" "${link}"
166 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
169 # Print device statistics.
170 cli_device_stats
2 ${device}
172 # Print some more information.
173 device_has_carrier
${device} &>/dev
/null
174 cli_print_fmt1
1 "Has carrier?" "$(cli_print_bool $?)"
176 device_is_promisc
${device} &>/dev
/null
177 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
179 # Supports multiqueue?
180 if device_supports_multiqueue
${device}; then
181 cli_print_fmt1
1 "Multiqueue" "Supported"
185 cli_device_show_queues
2 ${device}
187 # Print all vlan devices.
188 local vlans
=$
(device_get_vlans
${device})
189 if [ -n "${vlans}" ]; then
190 cli_headline
2 "VLAN devices"
193 for vlan
in ${vlans}; do
194 cli_print
2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
201 local phy
="$(device_get_phy "${device}")"
204 cli_print_fmt1
2 "Name" "${phy}"
205 cli_print_fmt1
2 "Address" "$(phy_get_address "${phy}")"
211 # Reset the logging level.
212 LOG_DISABLE_STDOUT
=${log_disable_stdout}
215 cli_device_status_serial
() {
217 assert device_is_serial
${device}
219 serial_is_locked
${device} &>/dev
/null
222 cli_print_fmt1
1 "Locked" "$(cli_print_bool ${locked})"
225 # Cannot go on when the device is locked.
226 [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
228 cli_print_fmt1
1 "Manufacturer" \
229 "$(modem_get_manufacturer ${device})"
230 cli_print_fmt1
1 "Model" \
231 "$(modem_get_model ${device})"
232 cli_print_fmt1
1 "Software version" \
233 "$(modem_get_software_version ${device})"
236 if modem_is_mobile
"${device}"; then
237 cli_print_fmt1
1 "IMEI" \
238 "$(modem_get_device_imei ${device})"
242 if modem_is_mobile
"${device}"; then
243 modem_mobile_network_status
"${device}" 2
248 cli_device_status_phy
() {
250 assert phy_exists
"${phy}"
252 local address
="$(phy_get_address "${phy}")"
253 cli_print_fmt1
1 "Address" "${address}"
256 local devices
="$(phy_get_devices "${phy}")"
257 if isset devices
; then
258 cli_headline
2 "Soft interfaces"
261 for device
in ${devices}; do
262 cli_print
2 "* %s" "${device}"
270 cli_device_discover
() {
274 # This can only be executed for ethernet (or compatible) devices
275 if ! device_is_ethernet_compatible
"${device}"; then
281 while [ $# -gt 0 ]; do
291 device_is_up
${device} && up
=1
292 device_set_up
${device}
294 enabled raw ||
echo "${device}"
299 for hook
in $
(hook_zone_get_all
); do
300 out
=$
(hook_zone_exec
${hook} discover
${device})
303 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
311 echo "${hook}: ${line}"
316 echo "${hook}: FAILED"
322 echo " ${hook} was successful."
330 echo " ${hook} failed."
338 [ "${up}" = "1" ] || device_set_down
${device}
341 cli_device_serial_unlock
() {
342 if cli_help_requested
"$@"; then
343 cli_show_man network-device
350 if ! device_is_serial
${device}; then
351 error
"${device} is not a serial device."
352 error
"Unlocking is only supported for serial devices."
356 # Read the current state of the SIM card.
357 modem_sim_status
${device} &>/dev
/null
358 local sim_status_code
=$?
360 # If the SIM card is already unlocked, we don't need to do anything.
361 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
362 print
"The SIM card is already unlocked."
365 # If the SIM card is in an unknown state, we cannot do anything.
366 elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
367 error
"The SIM card is in an unknown state."
373 local require_new_pin
="false"
376 while ! isinteger code
; do
378 case "${sim_status_code}" in
380 message
="Please enter PIN:"
383 message
="Please enter PUK:"
384 require_new_pin
="true"
389 echo -n "${message} "
391 echo # Print newline.
393 if enabled require_new_pin
; then
398 message
="Please enter a new PIN code:"
401 message
="Please confirm the new PIN code:"
405 echo -n "${message} "
407 echo # Print newline.
409 if [ -n "${new_pin}" ]; then
410 if [ "${new_pin}" != "${new_pin2}" ]; then
411 error
"The entered PIN codes did not match."
421 # Trying to unlock the SIM card.
422 modem_sim_unlock
${device} ${code} ${new_pin}
427 cli_device_send_ussd_command
() {
435 while [ $# -gt 0 ]; do
438 timeout
="$(cli_get_val "${1}")"
441 if isset
command; then
442 warning
"Unrecognized argument: ${1}"
451 assert device_is_serial
"${device}"
454 if isset timeout
; then
455 args
="${args} --timeout=${timeout}"
458 modem_ussd_send_command
"${device}" "${command}" ${args}
462 cli_device_monitor() {
466 if ! device_is_wireless "${device}"; then
467 error "This action only works with wireless devices. Exiting.
"
471 wireless_monitor "${device}"
477 for device in $(device_list); do
478 cli_device_status "${device}"
485 if cli_help_requested "$@
"; then
492 if [ -n "${hostname}" ]; then
493 config_hostname ${hostname}
494 log INFO "Hostname was
set to
'${hostname}'.
"
495 log INFO "Changes
do only take affect after reboot.
"
499 echo "$
(config_hostname
)"
504 if cli_help_requested "$@
"; then
505 cli_show_man network-port
512 if port_exists ${1}; then
518 edit|create|remove|up|down|status|identify)
519 port_${action} "${port}" "$@
"
522 color_cli "port
" "${port}" "$@
"
525 description_cli "port
" "${port}" "$@
"
528 error "Unrecognized argument
: ${action}"
541 error "Unrecognized argument
: ${action}"
549 if cli_help_requested "$@
"; then
550 cli_show_man network-zone
557 if zone_exists ${1}; then
577 cli_zone_port "${zone}" "$@
"
580 cli_zone_rename "${zone}" "$@
"
582 config|disable|down|edit|enable|identify|status|up)
583 zone_${action} ${zone} "$@
"
586 color_cli "zone
" "${zone}" "$@
"
589 description_cli "zone
" ${zone} "$@
"
592 error "Unrecognized argument
: ${action}"
593 cli_show_man network-zone
606 cli_zone_destroy "$@
"
609 if [ -n "${action}" ]; then
610 error "Unrecognized argument
: '${action}'"
614 cli_show_man network-zone
622 if cli_help_requested "$@
" || [ $# -lt 2 ]; then
623 cli_show_man network-zone-new
630 # Removes a zone either immediately, if it is currently down,
631 # or adds a tag that the removal will be done when the zone
632 # is brought down the next time.
634 if cli_help_requested "$@
"; then
635 cli_show_man network-zone
640 assert zone_exists "${zone}"
642 if zone_is_up ${zone}; then
643 echo "Zone
'${zone}' is up and will be removed when it goes down the next
time.
"
644 zone_destroy "${zone}"
646 echo "Removing zone
'${zone}' now...
"
647 zone_destroy_now "${zone}"
654 if cli_help_requested "$@
"; then
655 cli_show_man network-zone-port
660 assert zone_exists "${zone}"
662 if port_exists "${2}"; then
669 zone_port_edit "${zone}" "${port}" "$@
"
672 error "Unrecognised argument
: ${action}"
682 zone_port_attach "${zone}" "$@
"
685 zone_port_detach "${zone}" "$@
"
688 error "Unrecognised argument
: ${action}"
698 if cli_help_requested "$@
"; then
699 cli_show_man network-zone
707 if ! isset name; then
708 error "You need to pass a new name
"
712 if ! zone_name_is_valid "${name}"; then
713 error "Invalid new zone name
: ${name}"
717 # Check if the zone exists
718 if ! zone_exists "${zone}"; then
719 error "Zone
${zone} does not exist
"
723 # Check if a zone with the new name already exists
724 if zone_exists "${name}"; then
725 error "Zone
${name} already exists
"
730 if ! zone_rename "${zone}" "${name}"; then
731 error "Could not rename zone
${zone} to
${name}"
742 if cli_help_requested "$@
"; then
743 cli_show_man network-zone
747 local hook_dir=$(hook_dir ${type})
750 for hook in ${hook_dir}/*; do
751 hook=$(basename ${hook})
752 if hook_exists ${type} ${hook}; then
762 if cli_help_requested "$@
"; then
763 cli_show_man network-dhcp
772 dhcpd_edit ${proto} "$@
"
777 # Make this permanent
778 dhcpd_enable ${proto}
783 # Make this permanent
784 dhcpd_disable ${proto}
787 dhcpd_reload ${proto}
790 cli_dhcpd_subnet ${proto} "$@
"
793 cli_dhcpd_show ${proto} "$@
"
796 error "Unrecognized action
: ${action}"
797 cli_run_help network dhcpvN
810 local settings=$(dhcpd_settings ${proto})
811 assert isset settings
814 dhcpd_global_settings_read ${proto}
816 cli_headline 1 "Dynamic Host Configuration Protocol Daemon
for ${proto/ip/IP}"
820 cli_headline 2 "Lease
times"
821 if isinteger VALID_LIFETIME; then
822 cli_print_fmt1 2 "Valid lifetime
" "$
(format_time
${VALID_LIFETIME})"
825 if isinteger PREFERRED_LIFETIME; then
826 cli_print_fmt1 2 "Preferred lifetime
" "$
(format_time
${PREFERRED_LIFETIME})"
832 cli_print_fmt1 1 "Authoritative
" $(cli_print_enabled AUTHORITATIVE)
835 cli_headline 2 "Lease
times"
836 cli_print_fmt1 2 "Default lease
time" "$
(format_time
${DEFAULT_LEASE_TIME})"
837 cli_print_fmt1 2 "Max. lease
time" "$
(format_time
${MAX_LEASE_TIME})"
839 if isset MIN_LEASE_TIME; then
840 cli_print_fmt1 2 "Min. lease
time" "$
(format_time
${MIN_LEASE_TIME})"
849 dhcpd_global_options_read ${proto}
851 # Print the options if any.
852 if [ ${#options[*]} -gt 0 ]; then
853 cli_headline 2 "Options
"
856 for option in $(dhcpd_options ${proto}); do
857 [ -n "${options[${option}]}" ] || continue
860 "${option}" "${options[${option}]}"
866 local subnets=$(dhcpd_subnet_list ${proto})
867 if [ -n "${subnets}" ]; then
868 cli_headline 2 "Subnets
"
870 for subnet in ${subnets}; do
871 cli_dhcpd_subnet_show ${proto} ${subnet} 2
882 if cli_help_requested "$@
"; then
883 cli_show_man network-dhcp-subnet
892 dhcpd_subnet_new ${proto} "$@
"
895 dhcpd_subnet_remove ${proto} "$@
"
898 local subnet=${action}
900 if ! dhcpd_subnet_exists ${proto} ${subnet}; then
901 error "Subnet
${subnet} does not exist
"
911 dhcpd_subnet_edit ${proto} ${subnet} "$@
"
914 if [ ${ret} -eq ${EXIT_OK} ]; then
915 dhcpd_reload ${proto}
920 cli_dhcpd_subnet_range ${proto} ${subnet} "$@
"
924 cli_dhcpd_subnet_show ${proto} ${subnet} "$@
"
928 cli_dhcpd_subnet_options ${proto} ${subnet} "$@
"
932 error "Unrecognized action
: ${action}"
933 cli_run_help network dhcpvN subnet
940 for subnet in $(dhcpd_subnet_list ${proto}); do
941 cli_dhcpd_subnet_show ${proto} ${subnet}
945 error "Unrecognized action
: ${action}"
946 cli_run_help network dhcpvN subnet
955 cli_dhcpd_subnet_range() {
965 dhcpd_subnet_range_new ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
968 dhcpd_subnet_range_remove ${proto} ${subnet} "$@" || exit ${EXIT_ERROR}
971 error "Unrecognized action
: ${action}"
972 cli_run_help network dhcpvN subnet range
977 dhcpd_reload ${proto}
981 cli_dhcpd_subnet_show() {
982 assert [ $# -ge 2 -a $# -le 3 ]
988 isset level || level=0
990 local $(dhcpd_subnet_settings ${proto})
992 # Read in configuration settings.
993 dhcpd_subnet_read ${proto} ${subnet}
995 cli_headline $(( ${level} + 1 )) "DHCP Subnet Declaration
"
996 cli_print_fmt1 $(( ${level} + 1 )) \
997 "Subnet
" "${ADDRESS}/${PREFIX}"
1002 dhcpd_subnet_options_read "${proto}" "${subnet}"
1004 # Print the options if any.
1005 if [ ${#options[*]} -gt 0 ]; then
1006 cli_headline $(( ${level} + 2 )) "Options
"
1009 for option in $(dhcpd_subnet_options_list ${proto}); do
1010 [ -n "${options[${option}]}" ] || continue
1012 cli_print_fmt1 $(( ${level} + 2 )) \
1013 "${option}" "${options[${option}]}"
1019 cli_headline $(( ${level} + 2 )) "Ranges
"
1021 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet})
1022 if isset ranges; then
1023 local range $(dhcpd_subnet_range_settings ${proto})
1024 for range in ${ranges}; do
1025 dhcpd_subnet_range_read ${proto} ${subnet} ${range}
1027 cli_print $(( ${level} + 2 )) "%s - %s" ${START} ${END}
1030 cli_print $(( ${level} + 2 )) "No ranges have been defined.
"
1036 cli_dhcpd_subnet_options() {
1043 while [ $# -gt 0 ]; do
1046 key=$(cli_get_key ${1})
1047 val=$(cli_get_val "${1}")
1049 dhcpd_subnet_option_set ${proto} ${subnet} ${key} ${val}
1056 if cli_help_requested "$@
"; then
1057 cli_show_man network
1061 local zones=$(zones_get "$@
")
1064 for zone in ${zones}; do
1065 zone_start ${zone} &
1068 wait # until everything is settled
1072 if cli_help_requested "$@
"; then
1073 cli_show_man network
1077 local zones=$(zones_get "$@
")
1080 for zone in ${zones}; do
1084 wait # until everything is settled
1088 if cli_help_requested "$@
"; then
1089 cli_show_man network
1095 # Give the system some time to calm down
1096 sleep ${TIMEOUT_RESTART}
1102 if cli_help_requested "$@
"; then
1103 cli_show_man network
1107 # When dumping status information, the debug
1108 # mode clutters the console which is not what we want.
1109 # Logging on the console is disabled for a short time.
1110 local log_disable_stdout=${LOG_DISABLE_STDOUT}
1111 LOG_DISABLE_STDOUT="true
"
1113 local zones=$(zones_get "$@
")
1116 for zone in ${zones}; do
1121 LOG_DISABLE_STDOUT=${log_disable_stdout}
1125 if cli_help_requested "$@
"; then
1126 cli_show_man network
1130 warning_log "Will
reset the whole network configuration
!!!"
1131 # Force mode is disabled by default
1134 while [ $# -gt 0 ]; do
1143 # If we are not running in force mode, we ask the user if he does know
1145 if ! enabled force; then
1146 if ! cli_yesno "Do you really want to
reset the whole network configuration?
"; then
1151 # Destroy all IPsec VPN connections
1153 for connection in $(ipsec_list_connections); do
1154 ipsec_connection_destroy "${connection}"
1158 for pool in $(ipsec_list_pools); do
1159 ipsec_pool_destroy "${pool}"
1163 ipsec_strongswan_autostart
1165 # Destroy all user-defined security policies
1167 for secpol in $(vpn_security_policies_list_user); do
1168 vpn_security_policies_destroy "${secpol}"
1172 for zone in $(zones_get --all); do
1173 zone_destroy "${zone}"
1177 for port in $(ports_get --all); do
1178 port_destroy "${port}"
1181 # Flush all DNS servers.
1184 # Trigger udev to re-add all physical network devices
1185 cmd_quiet udevadm trigger --action=add --subsystem-match=net
1190 # Help function: will show the default man page to the user.
1191 # Optionally, there are two arguments taken, the type of hook
1192 # and which hook should be shown.
1202 # List all hooks if requested
1203 if [ "${hook}" = "list-hooks
" ]; then
1204 cli_list_hooks ${type}
1208 if ! hook_exists ${type} ${hook}; then
1209 error "No hook with name
'${hook}' could be found
"
1210 exit "${EXIT_ERROR}"
1213 hook_exec ${type} ${hook} help
1216 # In all other cases show the default man page
1218 cli_show_man network
1223 return ${EXIT_ERROR}
1227 if cli_help_requested "$@
"; then
1228 cli_show_man network-dns-server
1233 local cmd=${1}; shift
1234 if [ -z "${cmd}" ]; then
1235 cli_show_man network-dns-server
1239 # Get the new server to process (if any).
1249 if dns_server_exists ${server}; then
1250 error "DNS server
'${server}' already exists
!"
1254 log INFO "Adding new DNS server
: ${server}"
1255 dns_server_add ${server} ${priority}
1258 if ! dns_server_exists ${server}; then
1259 error "DNS server
'${server}' does not exist
!"
1263 log INFO "Removing DNS server
: ${server}"
1264 dns_server_remove ${server} ${priority}
1267 # Just run the update afterwards.
1270 error "No such
command: ${cmd}"
1274 # Update the local DNS configuration after changes have been made.
1289 device-get-by-mac-address)
1290 device_get_by_mac_address "$@
"
1292 ipsec-connection-exists)
1293 ipsec_connection_exists "$@
"
1298 list-dhcpd-ranges-of-subnet)
1299 dhcpd_subnet_range_list "$@
"
1301 list-dhcpd-settings)
1302 dhcpd_global_settings_list "$@
"
1305 dhcpd_subnet_list "$@
"
1307 list-dhcpd-subnet-options)
1308 dhcpd_subnet_options_list "$@
"
1319 list-ipsec-connections)
1320 ipsec_list_connections
1328 list-vpn-security-policies-all)
1329 vpn_security_policies_list_all
1332 network_settings_list
1337 list-next-free-zones)
1340 list-zone-config-ids)
1341 zone_config_list_ids "$@
"
1343 list-zone-config-hids)
1344 zone_config_list_hids "$@
"
1346 vpn-security-policy-exists)
1347 vpn_security_policy_exists "$@
"
1350 zone_name_is_valid "$@
"
1352 zone-config-id-is-valid)
1353 zone_config_id_is_valid "$@
"
1355 zone-config-hid-is-valid)
1356 zone_config_hid_is_valid "$@
"
1359 error "No such
command: ${cmd}"
1367 # Process the given action
1370 # Update resolv.conf(5) when initializing the network
1371 dns_generate_resolvconf
1373 # Also execute all triggers
1374 triggers_execute_all "init
"
1377 settings|hostname|port|device|zone|start|stop|restart|status|reset|route|vpn|wireless)
1381 # DHCP server configuration (automatically detects which protocol to use).
1383 cli_dhcpd ${action/dhcp/ip} "$@
"
1386 # DNS server configuration.
1400 error "Invalid
command given
: ${action}"
1401 cli_usage "network
help"
1402 exit ${EXIT_CONF_ERROR}