summaryrefslogtreecommitdiffstats
path: root/server/bin/setserveraddr
blob: 6694e3c4a194096615e0c97eb8c381b5b2705b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash

#/**
#@file    setserveraddr
#@brief   Assign default IP address to OpenGnsys services.
#@usage   setserveraddr { IPAddress | NetIface }
#@param   IPAddress  IP address assigned to a network interface
#@param   NetIface   network interface name defined by the operating system
#@version Initial version.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2011-01-25
#@version 1.0.5 - Regenerate configuration files.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2014-06-06
#@version 1.1.1 - Updating menu URLs and PXE files.
#@author  Ramón M. Gómez - ETSII Univ. Sevilla
#@date    2018-11-15
#*/ ##


# Variables.
OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
PXEDIRS="$OPENGNSYS/tftpboot/menu.lst $OPENGNSYS/tftpboot/grub"
DEFAULTFILE=/etc/default/opengnsys

# Functions.
source $OPENGNSYS/lib/ogfunctions.sh || exit 1

# Show help or version number.
[ "$*" == "help" ] && help
[ "$*" == "version" ] && version
# Checking parameters.
[ "$USER" != "root" ] && raiseError access "Need to be root"
[ $# -ne 1 ] && raiseError usage
[ -r $DEFAULTFILE ] || raiseError access "Cannot read default configuration file"
for f in $OPENGNSYS/{etc/ogserver.json,etc/ogAdmRepo.cfg,www/controlacceso.php,client/ogClient/cfg/ogclient.json}; do
	[ -w $f ] || raiseError access "Cannot write to file: $f"
done

# Detecting network interfaces.
DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}')
for DEV in $DEVICES; do
	# If the network interface is found, get its IP address.
	IP=$(ip -o addr show dev "$DEV" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}')
	if [ "$DEV" == "$1" ] || [ "$IP" == "$1" ]; then
		SERVERIP="$IP"
		SERVERDEV="$DEV"
	fi
done

# Checking if IP address has been detected.
if [ -n "$SERVERIP" ]; then
	# Showing warning to inform that initiated clients may hang.
	read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER
	[ "${ANSWER,,}" != "y" ] && raiseError cancel "Do nothing"
	# Temporary files.
	tmpfile=$(mktemp /tmp/og.XXXXX)
	trap "rm -f $tmpfile" 1 2 3 6 9 15

	# Checking whether the DHCP settings need to be changed.
	CHANGE=0
	for f in /etc/{dhcp,hcp3}/dhcpd.conf; do
		if [ -f $f ]; then
			# Changing DHCP "next-server" parameter.
			file="${f/./-$SERVERDEV.}"
			sed -e "s/next-server.*/next-server $SERVERIP;/" \
			    -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile
			# Copying and linking file if there are changes.
			if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
				mv $tmpfile $file
				chmod 644 $file
				ln -f $file $f
				CHANGE=1
			fi
		fi
	done
	# Restarting DHCP service if its configuration has changed.
	if [ $CHANGE == 1 ]; then
		for s in isc-dhcp-server dhcp3-server dhcpd; do
			restart $s &>/dev/null && break
		done
	else
		echo "DHCP configuration has not changed."
	fi

	# Saving old IP address.
	source_json_config $OPENGNSYS/etc/ogserver.json
	OLDSERVERIP=$ServidorAdm
	# Checking if configuration files need to be modified.
	CHANGE=0
	for f in $OPENGNSYS/{etc/ogAdmRepo.cfg,www/controlacceso.php}; do
		# Updating configuration variables (if URL does not contain "localhost").
		sed -e "s,\(ServidorAdm\|IPlocal\)=.*,\1=$SERVERIP," \
		    -e "s,^INTERFACE=.*,INTERFACE=$SERVERDEV," \
		    -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \
		    -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile
		file="${f/./-$SERVERDEV.}"
		# Copying updated file, if needed.
		if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then
			cp $tmpfile $file
			ln -f $file $f
			CHANGE=1
		fi
	done

	f="$OPENGNSYS"/etc/ogserver.json
	file="${f/./-$SERVERDEV.}"
	step_one=$(mktemp)
	jq --arg ip "$SERVERIP" '.rest.ip = $ip' "$file" > "$step_one"
	step_two=$(mktemp)
	jq --arg iface "$SERVERDEV" '.wol.interface = $iface' "$step_one" > "$step_two"
	if [ ! "$f" -ef "$file" ] || ! diff -q "$step_two" "$file" &>/dev/null; then
		cp "$step_two" "$file"
		ln -f "$file" "$f"
		CHANGE=1
	fi

	# Processing when something has changed.
	if [ $CHANGE == 1 ]; then
		# Config ogClient.
		IP_PATTERN="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
		sed -i -e "s/$IP_PATTERN/$SERVERIP/g" \
			  "$OPENGNSYS"/client/ogClient/cfg/ogclient.json
		# Restart OpenGnsys services.
		echo "Restarting services..."
		restart opengnsys >/dev/null
		source $DEFAULTFILE
		# If OpenGnsys Server is active, updating the database.
		if [ "$RUN_OGADMSERVER" == "yes" ]; then
			# Updating IP addresses and menu URLs.
			dbexec "
UPDATE entornos
   SET ipserveradm='$SERVERIP'
 WHERE identorno=1;
UPDATE repositorios
   SET ip='$SERVERIP'
 WHERE ip='$OLDSERVERIP';
UPDATE menus
   SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'),
       htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');"
			# Updating all PXE files.
			find $PXEDIRS -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \;
		fi

		# Showing manual task to do after execution.
		cat << EOT
Default server interface set to: $SERVERDEV ($SERVERIP)

Manual tasks:
- Check DHCP configuration file and restart service, if needed.
- Check PXE files.
- Log-in as Web Console user:
  - Check menu URLs.
- Note: Run "settoken" script to update authentication tokens.
EOT
	else
		# Showing message if nothing changes.
		echo "Default interface has not changed: $1"
	fi
else
	# Error if network interface is not found.
	raiseError notfound "Network device"
fi

# Removing temporary files.
rm -f $tmpfile