#!/bin/bash # ------- # File: llxcfg-dhcpd # Description: LliureX DHCP # Author: Luis Antonio Garcia Gisbert # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e # variables # --------- CPKGS="dhcp" # functions # --------- usage(){ CMD="$(basename "$0")" exit_message \ "Usage: $CMD {start|stop|restart|reload|allow|deny|info|listfiles|update|revert|cpkgs}\n\ $CMD grepinfo FIELD\n\ ${CMD} [options] {addregister} [HOSTNAME|HOSTNUMBER] MAC-ADDRESS\n\ ${CMD} [options] {delregister} [HOSTNAME|HOSTNUMBER]\n\ ${CMD} {listregister}\n\ options: -u|--update" } exit_message() { echo -e "$1" >&2 exit 1 } test_mac_address(){ [ -z "$1" ] && exit_message "A mac-address is needed" is_valid_mac_address "$1" || exit_message "The mac-address format is not valid" return 0 } valid_number(){ VALID="$(echo "$1" | egrep "^[[:digit:]]+$")" if [ -z "$VALID" ] ; then echo "" return 0 fi if [ $VALID -le $DHCP_HOST_MAX ] ; then echo $VALID else echo "" fi } valid_name(){ VALID="$(echo "$1" | egrep "^llx-pc[[:digit:]][[:digit:]]+$")" if [ -z $VALID ] ; then echo "" return 0 fi echo "$VALID" } # main # --------- LIB_FILE="/usr/share/lliurex/lliurex-srv-common/lliurex-netfuncs.sh" [ -e "$LIB_FILE" ] || exit_message "Needed library ${LIB_FILE} not found" . ${LIB_FILE} case "$1" in -u|--update) AUTOUPDATE="Y" shift ;; esac ACTION="$1" rc=0 case "$ACTION" in start|stop|restart|reload) llxcfg-cpkg invoke "$ACTION" $CPKGS || rc=$? exit $rc ;; cpkgs) for c in $CPKGS ; do echo $c done exit 0 ;; allow|deny|listfiles|update|revert|info) llxcfg-cpkg "$ACTION" $CPKGS || rc=$? exit $rc ;; grepinfo) [ "$2" ] || usage llxcfg-cpkg grepinfo "$2" $CPKGS || rc=$? exit $rc ;; addregister) HOST_PARAMETER="$2" MAC_ADDRESS="$3" test_mac_address "${MAC_ADDRESS}" HOST_NUM="$(valid_number "$HOST_PARAMETER")" HOST_NAME="$(valid_name "$HOST_PARAMETER")" if [ -z "$HOST_NUM" ] ; then if [ -z "$HOST_NAME" ] ; then exit_message "Invalid hostname or host number" else set_dhcp_host_mac "$(get_dhcp_host_num ${HOST_NAME})" "${MAC_ADDRESS}" fi else if [ -z "$HOST_NAME" ] ; then set_dhcp_host_mac "$HOST_NUM" "${MAC_ADDRESS}" fi fi ;; delregister) HOST_PARAMETER="$2" HOST_NUM="$(valid_number "$HOST_PARAMETER")" HOST_NAME="$(valid_name "$HOST_PARAMETER")" if [ -z "$HOST_NUM" ] ; then if [ -z "$HOST_NAME" ] ; then exit_message "Invalid hostname or host number" else del_dhcp_host_mac "$(get_dhcp_host_num ${HOST_NAME})" fi else if [ -z "$HOST_NAME" ] ; then del_dhcp_host_mac "$HOST_NUM" fi fi ;; listregister) llxcfg-config list "${MAC_CFGDIR}" | while read FILE ; do MAC="$(llxcfg-config read "$MAC_CFGDIR/$FILE")" NUMBER="${FILE##*/}" [ -n $MAC -a -n $NUMBER ] && echo -e "$NUMBER\t$MAC" done ;; *) usage ;; esac [ -z "$AUTOUPDATE" ] || llxcfg-cpkg update $CPKGS || rc=$? exit $rc