1 | #!/bin/bash |
---|
2 | |
---|
3 | #/** |
---|
4 | #@file setserveraddr |
---|
5 | #@brief Assign default IP address to OpenGnsys services. |
---|
6 | #@usage setserveraddr { IPAddress | NetIface } |
---|
7 | #@param IPAddress IP address assigned to a network interface |
---|
8 | #@param NetIface network interface name defined by the operating system |
---|
9 | #@version Initial version. |
---|
10 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
11 | #@date 2011-01-25 |
---|
12 | #@version 1.0.5 - Regenerate configuration files. |
---|
13 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
14 | #@date 2014-06-06 |
---|
15 | #@version 1.1.1 - Updating menu URLs and PXE files. |
---|
16 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
17 | #@date 2018-11-15 |
---|
18 | #*/ ## |
---|
19 | |
---|
20 | |
---|
21 | # Variables. |
---|
22 | PROG=$(basename $0) |
---|
23 | OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} |
---|
24 | PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub" |
---|
25 | DEFAULTFILE=/etc/default/opengnsys |
---|
26 | |
---|
27 | # Functions. |
---|
28 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
29 | |
---|
30 | # Show help or version number. |
---|
31 | [ "$*" == "help" ] && help |
---|
32 | [ "$*" == "version" ] && version |
---|
33 | # Checking parameters. |
---|
34 | [ "$USER" != "root" ] && raiseError access "Need to be root" |
---|
35 | [ $# -ne 1 ] && raiseError usage |
---|
36 | [ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file" |
---|
37 | for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do |
---|
38 | [ -w $f ] || raiseError access "Cannot write to file: $f" |
---|
39 | done |
---|
40 | |
---|
41 | # Detecting network interfaces. |
---|
42 | DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}') |
---|
43 | for DEV in $DEVICES; do |
---|
44 | # If the network interface is found, get its IP address. |
---|
45 | IP=$(ip -o addr show dev "$DEV" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}') |
---|
46 | if [ "$DEV" == "$1" ] || [ "$IP" == "$1" ]; then |
---|
47 | SERVERIP="$IP" |
---|
48 | SERVERDEV="$DEV" |
---|
49 | fi |
---|
50 | done |
---|
51 | |
---|
52 | # Checking if IP address has been detected. |
---|
53 | if [ -n "$SERVERIP" ]; then |
---|
54 | # Showing warning to inform that initiated clients may hang. |
---|
55 | read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER |
---|
56 | [ "${ANSWER,,}" != "y" ] && raiseError cancel "Do nothing" |
---|
57 | # Temporary files. |
---|
58 | tmpfile=$(mktemp /tmp/og.XXXXX) |
---|
59 | trap "rm -f $tmpfile" 1 2 3 6 9 15 |
---|
60 | |
---|
61 | # Checking whether the DHCP settings need to be changed. |
---|
62 | CHANGE=0 |
---|
63 | for f in /etc/{dhcp,hcp3}/dhcpd.conf; do |
---|
64 | if [ -f $f ]; then |
---|
65 | # Changing DHCP "next-server" parameter. |
---|
66 | file="${f/./-$SERVERDEV.}" |
---|
67 | sed -e "s/next-server.*/next-server $SERVERIP;/" \ |
---|
68 | -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile |
---|
69 | # Copying and linking file if there are changes. |
---|
70 | if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then |
---|
71 | mv $tmpfile $file |
---|
72 | chmod 644 $file |
---|
73 | ln -f $file $f |
---|
74 | CHANGE=1 |
---|
75 | fi |
---|
76 | fi |
---|
77 | done |
---|
78 | # Restarting DHCP service if its configuration has changed. |
---|
79 | if [ $CHANGE == 1 ]; then |
---|
80 | for s in isc-dhcp-server dhcp3-server dhcpd; do |
---|
81 | restart $s &>/dev/null && break |
---|
82 | done |
---|
83 | else |
---|
84 | echolog "DHCP configuration has not changed." |
---|
85 | fi |
---|
86 | |
---|
87 | # Saving old IP address. |
---|
88 | source $OPENGNSYS/etc/ogAdmServer.cfg |
---|
89 | OLDSERVERIP=$ServidorAdm |
---|
90 | # Checking if configuration files need to be modified. |
---|
91 | CHANGE=0 |
---|
92 | for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do |
---|
93 | # Updating configuration variables (if URL does not contain "localhost"). |
---|
94 | sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \ |
---|
95 | -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \ |
---|
96 | -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \ |
---|
97 | -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile |
---|
98 | file="${f/./-$SERVERDEV.}" |
---|
99 | # Copying updated file, if needed. |
---|
100 | if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then |
---|
101 | cp $tmpfile $file |
---|
102 | ln -f $file $f |
---|
103 | CHANGE=1 |
---|
104 | fi |
---|
105 | done |
---|
106 | |
---|
107 | # Processing when something has changed. |
---|
108 | if [ $CHANGE == 1 ]; then |
---|
109 | # Restart OpenGnsys services. |
---|
110 | echolog "Restarting services..." |
---|
111 | restart opengnsys >/dev/null |
---|
112 | source $DEFAULTFILE |
---|
113 | # If OpenGnsys Server is active, updating the database. |
---|
114 | if [ "$RUN_OGADMSERVER" == "yes" ]; then |
---|
115 | # Updating IP addresses and menu URLs. |
---|
116 | dbexec " |
---|
117 | UPDATE entornos |
---|
118 | SET ipserveradm='$SERVERIP' |
---|
119 | WHERE identorno=1; |
---|
120 | UPDATE repositorios |
---|
121 | SET ip='$SERVERIP' |
---|
122 | WHERE ip='$OLDSERVERIP'; |
---|
123 | UPDATE menus |
---|
124 | SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'), |
---|
125 | htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');" |
---|
126 | # Updating all PXE files. |
---|
127 | find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \; |
---|
128 | fi |
---|
129 | |
---|
130 | # Showing manual task to do after execution. |
---|
131 | echolog "Default server interface set to: $SERVERDEV ($SERVERIP)" |
---|
132 | cat << EOT |
---|
133 | |
---|
134 | Manual tasks: |
---|
135 | - Check DHCP configuration file and restart service, if needed. |
---|
136 | - Check PXE files. |
---|
137 | - Log-in as Web Console user: |
---|
138 | - Check menu URLs. |
---|
139 | - Note: Run "settoken" script to update authentication tokens. |
---|
140 | EOT |
---|
141 | else |
---|
142 | # Showing message if nothing changes. |
---|
143 | echolog "Default interface has not changed: $1" |
---|
144 | fi |
---|
145 | else |
---|
146 | # Error if network interface is not found. |
---|
147 | raiseError notfound "Network device" |
---|
148 | fi |
---|
149 | |
---|
150 | # Removing temporary files. |
---|
151 | rm -f $tmpfile |
---|
152 | |
---|