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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#!/bin/sh
# Copyright (C) 2022-2023 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# shellcheck disable=SC1091
# SC1091: Not following: ... was not specified as input (see shellcheck -x).
# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu
# shellcheck disable=SC2034
# SC2034: ... appears unused. Verify use (or export if used externally).
prefix="@prefix@"
# Include utilities. Use installed version if available and
# use build version if it isn't.
if test -f "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"; then
. "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
else
. "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
fi
# Check version.
version=$(mysql_version "${@}")
if test "${version}" != "12.0"; then
printf 'This script upgrades 12.0 to 13.0. '
printf 'Reported version is %s. Skipping upgrade.\n' "${version}"
exit 0
fi
# Get the schema name from database argument. We need this to
# query information_schema for the right database.
for arg in "${@}"
do
if ! printf '%s' "${arg}" | grep -Eq -- '^--'
then
schema="$arg"
break
fi
done
# Make sure we have the schema.
if [ -z "$schema" ]
then
printf "Could not find database schema name in cmd line args: %s\n" "${*}"
exit 255
fi
mysql "$@" <<EOF
-- This line starts the schema upgrade to version 13.0.
-- Create a function that separates a contiguous hexadecimal string
-- into groups of two hexadecimals separated by colons.
DROP FUNCTION IF EXISTS colonSeparatedHex;
DELIMITER $$
CREATE FUNCTION colonSeparatedHex(hex VARCHAR(64))
RETURNS VARCHAR(64)
DETERMINISTIC
BEGIN
-- Declarations
DECLARE i INT;
DECLARE length INT;
DECLARE output VARCHAR(64);
-- Initializations
SET i = 3;
SET length = LENGTH(hex);
-- Add a leading zero if the first octet has a single hexadecimal character.
IF MOD(length, 2) = 1 THEN
SET hex = CONCAT('0', hex);
SET length = length + 1;
END IF;
-- Start with the first octet.
SET output = SUBSTR(hex, 1, 2);
-- Add one octet at a time and a leading colon with each.
label: WHILE i < length DO
SET output = CONCAT(output, ':', SUBSTR(hex, i, 2));
SET i = i + 2;
END WHILE label;
-- Memfile uses lowercase hexadecimals.
SET output = LOWER(output);
RETURN output;
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease4DumpData;
DELIMITER $$
CREATE PROCEDURE lease4DumpData()
BEGIN
SELECT
INET_NTOA(address),
IFNULL(colonSeparatedHex(HEX(hwaddr)), ''),
IFNULL(colonSeparatedHex(HEX(client_id)), ''),
valid_lifetime,
UNIX_TIMESTAMP(expire),
subnet_id,
fqdn_fwd,
fqdn_rev,
REPLACE(hostname, ',', ','),
state,
REPLACE(IFNULL(user_context, ''), ',', ',')
FROM lease4
ORDER BY address;
END $$
DELIMITER ;
-- hwtype and hwaddr_source need to be last to match memfile format.
DROP PROCEDURE IF EXISTS lease6DumpHeader;
DELIMITER $$
CREATE PROCEDURE lease6DumpHeader()
BEGIN
SELECT 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,state,user_context,hwtype,hwaddr_source';
END $$
DELIMITER ;
-- Modify the procedure to output a memfile-ready CSV file.
DROP PROCEDURE IF EXISTS lease6DumpData;
DELIMITER $$
CREATE PROCEDURE lease6DumpData()
BEGIN
SELECT
address,
IFNULL(colonSeparatedHex(HEX(duid)), ''),
valid_lifetime,
UNIX_TIMESTAMP(expire),
subnet_id,
pref_lifetime,
lease_type,
iaid,
prefix_len,
fqdn_fwd,
fqdn_rev,
REPLACE(hostname, ',', ','),
IFNULL(colonSeparatedHex(HEX(hwaddr)), ''),
state,
REPLACE(IFNULL(user_context, ''), ',', ','),
hwtype,
hwaddr_source
FROM lease6
ORDER BY address;
END $$
DELIMITER ;
-- Create a procedure that inserts a v4 lease from memfile data.
DELIMITER $$
CREATE PROCEDURE lease4Upload(
IN address VARCHAR(15),
IN hwaddr VARCHAR(20),
IN client_id VARCHAR(128),
IN valid_lifetime INT UNSIGNED,
IN expire BIGINT UNSIGNED,
IN subnet_id INT UNSIGNED,
IN fqdn_fwd TINYINT,
IN fqdn_rev TINYINT,
IN hostname VARCHAR(255),
IN state INT UNSIGNED,
IN user_context TEXT
)
BEGIN
INSERT INTO lease4 (
address,
hwaddr,
client_id,
valid_lifetime,
expire,
subnet_id,
fqdn_fwd,
fqdn_rev,
hostname,
state,
user_context
) VALUES (
INET_ATON(address),
UNHEX(REPLACE(hwaddr, ':', '')),
UNHEX(REPLACE(client_id, ':', '')),
valid_lifetime,
FROM_UNIXTIME(expire),
subnet_id,
fqdn_fwd,
fqdn_rev,
REPLACE(hostname, ',', ','),
state,
REPLACE(user_context, ',', ',')
);
END $$
DELIMITER ;
-- Create a procedure that inserts a v6 lease from memfile data.
DELIMITER $$
CREATE PROCEDURE lease6Upload(
IN address VARCHAR(39),
IN duid VARCHAR(128),
IN valid_lifetime INT UNSIGNED,
IN expire BIGINT UNSIGNED,
IN subnet_id INT UNSIGNED,
IN pref_lifetime INT UNSIGNED,
IN lease_type TINYINT,
IN iaid INT UNSIGNED,
IN prefix_len TINYINT UNSIGNED,
IN fqdn_fwd TINYINT,
IN fqdn_rev TINYINT,
IN hostname VARCHAR(255),
IN hwaddr VARCHAR(64),
IN state INT UNSIGNED,
IN user_context TEXT,
IN hwtype SMALLINT,
IN hwaddr_source INT UNSIGNED
)
BEGIN
INSERT INTO lease6 (
address,
duid,
valid_lifetime,
expire,
subnet_id,
pref_lifetime,
lease_type,
iaid,
prefix_len,
fqdn_fwd,
fqdn_rev,
hostname,
hwaddr,
state,
user_context,
hwtype,
hwaddr_source
) VALUES (
address,
UNHEX(REPLACE(duid, ':', '')),
valid_lifetime,
FROM_UNIXTIME(expire),
subnet_id,
pref_lifetime,
lease_type,
iaid,
prefix_len,
fqdn_fwd,
fqdn_rev,
REPLACE(hostname, ',', ','),
UNHEX(REPLACE(hwaddr, ':', '')),
state,
REPLACE(user_context, ',', ','),
hwtype,
hwaddr_source
);
END $$
DELIMITER ;
-- Update the schema version number.
UPDATE schema_version
SET version = '13', minor = '0';
-- This line concludes the schema upgrade to version 13.0.
EOF
|