aboutsummaryrefslogtreecommitdiffstats
path: root/notes/projects/radius/sql/ippool-schema.sql
diff options
context:
space:
mode:
Diffstat (limited to 'notes/projects/radius/sql/ippool-schema.sql')
-rw-r--r--notes/projects/radius/sql/ippool-schema.sql175
1 files changed, 158 insertions, 17 deletions
diff --git a/notes/projects/radius/sql/ippool-schema.sql b/notes/projects/radius/sql/ippool-schema.sql
index 5dfc5e1..df2bfeb 100644
--- a/notes/projects/radius/sql/ippool-schema.sql
+++ b/notes/projects/radius/sql/ippool-schema.sql
@@ -8,6 +8,8 @@
8 8
9 9
10SET ROLE radius_ippool; 10SET ROLE radius_ippool;
11SET client_min_messages TO WARNING;
12\set ON_ERROR_STOP on
11 13
12 14
13-- ////////////// 15-- //////////////
@@ -103,15 +105,15 @@ CREATE INDEX
103 * id 105 * id
104 * : The primary key of a specific entry 106 * : The primary key of a specific entry
105 * 107 *
108 * network
109 * : network address and network size of block
110 *
106 * poolId 111 * poolId
107 * : reference to the pool type of the network 112 * : reference to the pool type of the network
108 * 113 *
109 * regionId 114 * regionId
110 * : reference to the physical region served by the block 115 * : reference to the physical region served by the block
111 * 116 *
112 * net
113 * : network address and network size of block
114 *
115 * active 117 * active
116 * : assignable to customer if true, disabled/inactive if false 118 * : assignable to customer if true, disabled/inactive if false
117 * 119 *
@@ -121,9 +123,9 @@ CREATE INDEX
121CREATE TABLE IF NOT EXISTS netblock 123CREATE TABLE IF NOT EXISTS netblock
122( 124(
123 id SERIAL, 125 id SERIAL,
126 network CIDR NOT NULL UNIQUE,
124 poolId INTEGER NOT NULL, 127 poolId INTEGER NOT NULL,
125 regionId INTEGER, 128 regionId INTEGER,
126 network CIDR NOT NULL UNIQUE,
127 active BOOLEAN NOT NULL DEFAULT TRUE, 129 active BOOLEAN NOT NULL DEFAULT TRUE,
128 description VARCHAR(32), 130 description VARCHAR(32),
129 PRIMARY KEY ( id ), 131 PRIMARY KEY ( id ),
@@ -150,39 +152,39 @@ CREATE INDEX
150 * address 152 * address
151 * : user assignable IP address 153 * : user assignable IP address
152 * 154 *
153 * poolId
154 * : reference to the pool type of the network
155 *
156 * assigned
157 * : TRUE if currently assigned, FALSE if available
158 *
159 * modified 155 * modified
160 * : timestamp generated when entry is modified 156 * : timestamp generated when entry is modified
161 * 157 *
162 * username 158 * username
163 * : username currently assigned or last assigned address 159 * : username currently assigned or last assigned address
160 *
161 * acctSession
162 * : RADIUS accounting session currently holding address
163 *
164 * nasId
165 * : reference to RADIUS NAS client currently holding address
164 */ 166 */
165CREATE TABLE IF NOT EXISTS ip 167CREATE TABLE IF NOT EXISTS ip
166( 168(
167 address INET, 169 address INET,
168 poolId INTEGER NOT NULL,
169 assigned BOOLEAN NOT NULL DEFAULT FALSE,
170 modified timestamp WITH TIME ZONE NOT NULL 170 modified timestamp WITH TIME ZONE NOT NULL
171 DEFAULT TIMESTAMP 'epoch', 171 DEFAULT TIMESTAMP 'epoch',
172 username VARCHAR(64), 172 username VARCHAR(64),
173 acctSession VARCHAR(32),
174 nasId INTEGER,
173 PRIMARY KEY ( address ), 175 PRIMARY KEY ( address ),
174 FOREIGN KEY ( poolId ) 176 FOREIGN KEY ( nasId )
175 REFERENCES pool 177 REFERENCES nas
176 ( id ) 178 ( id )
177); 179);
178CREATE INDEX 180CREATE INDEX
179 IF NOT EXISTS ip_index 181 IF NOT EXISTS ip_index
180 ON ip 182 ON ip
181 ( address, 183 ( address,
182 poolId,
183 assigned,
184 modified, 184 modified,
185 username 185 username,
186 acctSession,
187 nasId
186 ); 188 );
187 189
188 190
@@ -270,6 +272,137 @@ $$;
270 272
271 273
272/* 274/*
275 * Creates entry for network block
276 *
277 * % SELECT netblock_add(
278 * netAddress := '198.51.100.0/24',
279 * poolName := 'PPPoE'
280 * );
281 * % SELECT netblock_add(
282 * netAddress := '203.0.113.0/24',
283 * poolName := 'DHCP',
284 * regionName := 'Soldotna',
285 * netActive := FALSE,
286 * netDesc := 'Lab IP Pool'
287 * );
288 *
289 * netAddress
290 * : network address and network size being added to database
291 *
292 * poolName
293 * : IP pool network is being assigned
294 *
295 * regionName
296 * : the name of the region in which the network will physically serve
297 *
298 * netActive
299 * : TRUE if addresses within network are assignable, FALSE if addresses are
300 * : not assignable
301 *
302 * netDesc
303 * : human readable description of the network
304 *
305 * Returns the `id` of the created nas client
306 */
307CREATE OR REPLACE FUNCTION netblock_add
308(
309 netAddress CIDR,
310 poolName VARCHAR(32),
311 regionName VARCHAR(32) DEFAULT NULL,
312 netActive BOOLEAN DEFAULT TRUE,
313 netDesc VARCHAR(128) DEFAULT NULL
314)
315 RETURNS INTEGER
316 LANGUAGE plpgsql
317 AS $$
318DECLARE
319 netRegionId INTEGER DEFAULT NULL;
320 netPoolId INTEGER;
321 returnId INTEGER DEFAULT 1;
322BEGIN
323
324 -- searches for region id
325 if ( regionName IS NOT NULL ) THEN
326 SELECT id INTO netRegionId
327 FROM region
328 WHERE name LIKE lower(regionName);
329 END IF;
330
331 -- searches for pool id
332 SELECT id INTO netPoolId
333 FROM pool
334 WHERE name LIKE lower(poolName);
335 IF ( netPoolId IS NULL ) THEN
336 RAISE EXCEPTION 'invalid pool';
337 END IF;
338
339 -- adds new pool
340 INSERT INTO netblock
341 ( network,
342 poolId,
343 regionId,
344 active,
345 description
346 )
347 VALUES ( netAddress,
348 netPoolId,
349 netRegionId,
350 netActive,
351 netDesc
352 )
353 RETURNING id INTO returnId;
354
355 RETURN returnId;
356END;
357$$;
358
359
360/*
361 * updates timestamp of addresses
362 *
363 * returns trigger
364 */
365CREATE OR REPLACE FUNCTION netblock_trigger
366( )
367 RETURNS trigger
368 LANGUAGE plpgsql
369 AS $$
370DECLARE
371 resultCount INTEGER;
372BEGIN
373
374 -- verifies that new region does not conflict with new network
375 SELECT count(id) INTO resultCount
376 FROM netblock
377 WHERE regionId != NEW.regionId
378 AND
379 ( NEW.network >>= network
380 OR
381 NEW.network <<= network
382 );
383 IF ( resultCount != 0 ) THEN
384 RAISE EXCEPTION 'conflict with existing network regions';
385 END IF;
386
387 -- verifies that new region does not conflict with new network
388 SELECT count(id) INTO resultCount
389 FROM netblock
390 WHERE poolId != NEW.poolId
391 AND
392 ( NEW.network >>= network
393 OR
394 NEW.network <<= network
395 );
396 IF ( resultCount != 0 ) THEN
397 RAISE EXCEPTION 'conflict with existing network pools';
398 END IF;
399
400 RETURN NEW;
401END;
402$$;
403
404
405/*
273 * Creates entry for IP Pool 406 * Creates entry for IP Pool
274 * 407 *
275 * % SELECT pool_add( 408 * % SELECT pool_add(
@@ -373,4 +506,12 @@ CREATE TRIGGER ip_timestamp
373 EXECUTE PROCEDURE ip_timestamp(); 506 EXECUTE PROCEDURE ip_timestamp();
374 507
375 508
509-- netblock table triggers
510CREATE TRIGGER netblock_trigger
511 BEFORE INSERT OR UPDATE
512 ON netblock
513 FOR EACH ROW
514 EXECUTE PROCEDURE netblock_trigger();
515
516
376/* end of sql */ 517/* end of sql */