source: server/bin/addtodhcp @ 995b512

918-git-images-111dconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 995b512 was 18eb8d6, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#958: Avoid SQL injection in addtodhcp and listclientlive.

  • Property mode set to 100755
File size: 3.4 KB
Line 
1#!/bin/bash
2#@file    addtodhcp
3#@brief   Append a "host" section for each defined computer to the DHCP configuration file.
4#@usage   addtodhcp [-f FILE] [-r] [-e] [ {LABNAME|COMPUTERNAME} ...]
5#@param   -f, --file FILE   DHCP configuration file (/etc/dhcp/dhcpd.conf, by default)
6#@param   -r, --restart     restart DHCP service
7#@param   -e, --exam        assign to alternative network ("exam mode" from Universidad de Sevilla)
8#@param   LABNAME           only add computers defined in this lab
9#@param   COMPUTERNAME      only add a single computer data
10#@version 1.1.1b - Initial version.
11#@author  Ramón M. Gómez - ETSII Univ. Sevilla
12#@date    2020-02-03
13
14
15# Variables.
16PROG="$(basename "$0")"
17OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
18SERVERCONF=$OPENGNSYS/etc/ogAdmServer.cfg
19DHCPCONF=/etc/dhcp/dhcpd.conf
20DHCPCONFBCK="$DHCPCONF-$(date +"%Y%m%d")"
21
22source $OPENGNSYS/lib/ogfunctions.sh || exit 1
23
24# Show help
25[ "$*" == "help" ] && help
26# Error control.
27[ "$USER" != "root" ] && raiseError access "Need to be root"
28source $SERVERCONF 2>/dev/null || raiseError access "Cannot read OpenGnsys Server configuration file"
29
30# Processing parameters.
31RESOURCES="$*"
32opts=$(getopt -n "$PROG" -l exam,file:,restart -o 'ef:r' -- "$@" ) || raiseError usage
33set -- $opts
34while [ "$1" ]; do
35    case "$1" in
36        -e|--exam)
37            EXAM=1
38            shift ;;
39        -f|--file)
40            eval DHCPCONF=$2
41            shift 2 ;;
42        -r|--restart)
43            RESTART=1
44            shift ;;
45        --)
46            shift; break ;;
47    esac
48done
49[ -f $DHCPCONF ] || raiseError access "Cannot access DHCP configuration file"
50grep -q "^[     ]*\bsubnet\b" $DHCPCONF || raiseError access "Cannot detect any \"group\" clauses in DHCP configuration file"
51grep -q "^[     ]*\bgroup\b" $DHCPCONF && raiseError access "Cannot modify DHCP configuration file with \"group\" clauses"
52
53[ "$*" ] && WHEREEXPR="WHERE $(echo ${*//\'/\\\'} | sed -e "s/\('[^']*'\)/nombreaula=\1 OR nombreordenador=\1 OR/g")"
54WHEREEXPR="${WHEREEXPR% OR}"
55
56# Looking for data.
57SEDEXPR=""
58while read -pe NAME IP MAC ROUTER LAB; do
59    [ "$LAB" ] || break
60    if [ "$EXAM" ]; then
61        IP="${IP/10.1./192.168.}"
62        ROUTER="${ROUTER/10.1./192.168.}"
63    fi
64    # Find any "host" clause.
65    SEDEXPR+="/\bhost $NAME\b/"
66    if ! grep -q "host $NAME.*}" $DHCPCONF; then
67        SEDEXPR+=",/}/"
68    fi
69    if [ "$LAB" != "$LABBCK" ]; then
70        NEWLAB="\\\n"
71        LABBCK="$LAB"
72    else
73        NEWLAB=""
74    fi
75    # Delete the found "host" clause and add a new one.
76    SEDEXPR+="d
77/^[[:space:]]*option[[:space:]]+routers[[:space:]]+\b$ROUTER\b/a ${NEWLAB}host $NAME { hardware ethernet $MAC; fixed-address $IP; }  # $LAB
78"
79done <<<$(dbexec "
80SELECT nombreordenador, ip,
81       CONCAT_WS('', SUBSTR(mac, 1, 2), ':', SUBSTR(mac, 3, 2), ':', SUBSTR(mac, 5, 2), ':',
82                     SUBSTR(mac, 7, 2), ':', SUBSTR(mac, 9, 2), ':', SUBSTR(mac, 11, 2)),
83       ordenadores.router, nombreaula
84  FROM ordenadores
85  JOIN aulas USING (idaula)
86 $WHEREEXPR
87 ORDER BY nombreaula ASC, idordenador ASC;" 2>/dev/null)
88
89# Edit DHCP configuration file.
90[ "$SEDEXPR" ] || raiseError notfound "$RESOURCES"
91cp -a $DHCPCONF $DHCPCONFBCK || raiseError access "Cannot back-up DHCP configuration file"
92sed -i -re "$SEDEXPR" $DHCPCONF
93# Delete duplicate empty lines.
94perl -0777pi -e "s/\n{3,}/\n\n/g" $DHCPCONF
95# Restart the service, if needed.
96[ "$RESTART" ] && restart isc-dhcp-server
97
Note: See TracBrowser for help on using the repository browser.