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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/*
* IP Engineering RADIUS Servers
*
* ippool-schema.sql - builds database schema for RADIUS IP Pools
*
* Usage: psql -f ippool-schema.sql radius_ippool
*/
SET ROLE radius_ippool;
SET client_min_messages TO WARNING;
\set ON_ERROR_STOP on
-- //////////////
-- // //
-- // Tables //
-- // //
-- //////////////
/*
* RADIUS Pool Type
*
* id
* : The primary key of a specific entry
*
* name
* : the pool name/type
*
* description
* : human readable description of pool
*/
CREATE TABLE IF NOT EXISTS pool
(
id SERIAL,
name VARCHAR(32) NOT NULL UNIQUE,
description VARCHAR(128),
PRIMARY KEY ( id )
);
CREATE INDEX
IF NOT EXISTS pool_index
ON pool
( id );
/*
* RADIUS geographic region
*
* id
* : The primary key of a specific entry
*
* name
* : the region's name/locality
*
* description
* : human readable description of region
*/
CREATE TABLE IF NOT EXISTS region
(
id SERIAL,
name VARCHAR(32) NOT NULL,
description VARCHAR(128),
PRIMARY KEY ( id )
);
CREATE INDEX
IF NOT EXISTS region_index
ON region
( id );
/*
* RADIUS NAS client
*
* id
* : The primary key of a specific entry
*
* name
* : the NAS truncated DNS name (i.e. br1.cwc)
*
* regionId
* : reference to the region in which the NAS is physically located
*/
CREATE TABLE IF NOT EXISTS nas
(
id SERIAL,
name VARCHAR(32) NOT NULL,
regionId INTEGER NOT NULL,
PRIMARY KEY ( id ),
FOREIGN KEY ( regionId )
REFERENCES region
( id )
);
CREATE INDEX
IF NOT EXISTS nas_index
ON nas
( id,
regionId
);
/*
* RADIUS network block
*
* id
* : The primary key of a specific entry
*
* network
* : network address and network size of block
*
* poolId
* : reference to the pool type of the network
*
* regionId
* : reference to the physical region served by the block
*
* active
* : assignable to customer if true, disabled/inactive if false
*
* description
* : human readable description of network block
*/
CREATE TABLE IF NOT EXISTS netblock
(
id SERIAL,
network CIDR NOT NULL UNIQUE,
poolId INTEGER NOT NULL,
regionId INTEGER,
active BOOLEAN NOT NULL DEFAULT TRUE,
description VARCHAR(256),
PRIMARY KEY ( id ),
FOREIGN KEY ( poolId )
REFERENCES pool
( id ),
FOREIGN KEY ( regionId )
REFERENCES region
( id )
);
CREATE INDEX
IF NOT EXISTS netblock_index
ON netblock
( id,
poolId,
network,
active
);
/*
* RADIUS user-assignable IP address
*
* address
* : user assignable IP address
*
* modified
* : timestamp generated when entry is modified
*
* username
* : username currently assigned or last assigned address
*
* acctSession
* : RADIUS accounting session currently holding address
*
* nasId
* : reference to RADIUS NAS client currently holding address
*/
CREATE TABLE IF NOT EXISTS ip
(
address INET,
modified timestamp WITH TIME ZONE NOT NULL
DEFAULT TIMESTAMP 'epoch',
username VARCHAR(64),
acctSession VARCHAR(32),
nasId INTEGER,
PRIMARY KEY ( address ),
FOREIGN KEY ( nasId )
REFERENCES nas
( id )
);
CREATE INDEX
IF NOT EXISTS ip_index
ON ip
( address,
modified,
username,
acctSession,
nasId
);
-- /////////////////
-- // //
-- // Functions //
-- // //
-- /////////////////
/*
* Creates IP entries for the given network block
*
* % SELECT ip_add_network(
* ipNetwork := '198.51.100.0/24'
* );
*
* ipNetwork
* : network address and network size being added to database
*
* Returns the number of new IP addresses registered
*/
CREATE OR REPLACE FUNCTION ip_add_network
(
ipNetwork CIDR
)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
ipAddress INET;
resultCount INTEGER;
ipAdded INTEGER;
ipMask INTEGER;
BEGIN
-- searches for a matching netblock
SELECT count(network) INTO resultCount
FROM netblock
WHERE network >>= ipNetwork;
IF ( resultCount = 0 ) THEN
RAISE EXCEPTION 'matching netblock not found';
END IF;
-- restricts access to IPv4 addresses
IF ( family(ipNetwork) != 4 ) THEN
RAISE EXCEPTION 'only IPv4 addresses are supported';
END IF;
-- sets starting values
ipAdded := 0;
ipAddress := set_masklen( network(ipNetwork), 32 );
-- loops through individual IP addresses
LOOP
EXIT WHEN ipAddress > set_masklen( broadcast(ipNetwork), 32 );
-- searches for existing IP address
SELECT count( address ) INTO resultCount
FROM ip
WHERE address = ipAddress;
-- defines IP address if not defined and counts results
IF ( resultCount = 0 ) THEN
INSERT INTO ip
( address )
VALUES ( ipAddress );
ipAdded := ipAdded + 1;
END IF;
ipAddress := ipAddress + 1;
END LOOP;
RETURN ipAdded;
END;
$$;
/*
* updates timestamp of addresses
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION ip_assign
(
assignIp INET,
assignUser TEXT,
assignNas TEXT DEFAULT NULL,
assignSess TEXT DEFAULT NULL
)
RETURNS BOOLEAN
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
-- sets defaults
IF ( assignSess IS NULL ) THEN
assignSess := 'user:' || assignUser;
END IF;
-- updates record
UPDATE ip
SET nasId = ( SELECT id FROM nas WHERE name LIKE assignNas ),
username = assignUser,
acctSession = assignSess
WHERE address = set_masklen( assignIp, 32 );
RETURN FOUND;
END;
$$;
/*
* updates timestamp of addresses
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION ip_trigger
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
-- prevents IP address from being modified
IF ( ( TG_OP = 'UPDATE' ) AND ( NEW.address != OLD.address ) ) THEN
RAISE EXCEPTION 'address cannot be modified';
END IF;
-- forces initial values
IF ( TG_OP = 'INSERT' ) THEN
NEW.username := NULL;
NEW.acctSession := NULL;
NEW.nasId := NULL;
END IF;
-- determines mask for single IP address
IF ( family(NEW.address) != 4 ) THEN
RAISE EXCEPTION 'only IPv4 addresses are supported';
END IF;
-- update timestamp
IF ( TG_OP = 'UPDATE' ) THEN
NEW.modified := current_timestamp;
ELSIF ( TG_OP = 'INSERT' ) THEN
NEW.modified := TIMESTAMP 'epoch';
END IF;
RETURN NEW;
END;
$$;
/*
* updates timestamp of addresses
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION ip_trigger_delete
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
-- verifies ip is not assigned to a RADIUS accounting session
IF ( OLD.acctSession IS NOT NULL ) THEN
RAISE EXCEPTION 'IP address is still assigned to accounting session';
END IF;
-- verifies ip is not assigned to a RADIUS accounting session
IF ( OLD.nasId IS NOT NULL ) THEN
RAISE EXCEPTION 'IP address is still assigned to NAS session';
END IF;
RETURN NEW;
END;
$$;
/*
* updates timestamp of addresses
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION ip_trigger_truncate
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
resultCount INTEGER;
BEGIN
-- searches for assigned IP addresses
SELECT count( address ) INTO resultCount
FROM ip
WHERE acctSession IS NOT NULL
OR nasId IS NOT NULL;
IF ( resultCount != 0 ) THEN
RAISE EXCEPTION 'IP addresses are still assigned to sessions';
END IF;
RETURN NEW;
END;
$$;
/*
* Creates entry for NAS client
*
* % SELECT nas_add(
* nasName := 'br1.cwc',
* regionName := 'Anchorage'
* );
*
* nasName
* : name of NAS being added to database
*
* regionName
* : the name of the region in which the NAS is physically located
*
* Returns the `id` of the created nas client
*/
CREATE OR REPLACE FUNCTION nas_add
(
nasName VARCHAR(32),
regionName VARCHAR(128)
)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
nasRegionId INTEGER;
returnId INTEGER;
BEGIN
-- searches for region id
SELECT id INTO nasRegionId
FROM region
WHERE name LIKE lower(regionName);
IF ( nasRegionId IS NULL ) THEN
RAISE EXCEPTION 'invalid region';
END IF;
-- adds new pool
INSERT INTO nas
( name,
regionId
)
VALUES ( lower(nasName),
nasRegionId
)
RETURNING id INTO returnId;
RETURN returnId;
END;
$$;
/*
* Creates entry for network block
*
* % SELECT netblock_add(
* netAddress := '198.51.100.0/24',
* poolName := 'PPPoE'
* );
* % SELECT netblock_add(
* netAddress := '203.0.113.0/24',
* poolName := 'DHCP',
* regionName := 'Soldotna',
* netActive := FALSE,
* netDesc := 'Lab IP Pool',
* addIpEntries := TRUE
* );
*
* netAddress
* : network address and network size being added to database
*
* poolName
* : IP pool network is being assigned
*
* regionName
* : the name of the region in which the network will physically serve
*
* netActive
* : TRUE if addresses within network are assignable, FALSE if addresses are
* : not assignable
*
* netDesc
* : human readable description of the network
*
* addIpEntries
* : add coorisponding ip table entries for network
*
* Returns the `id` of the created nas client
*/
CREATE OR REPLACE FUNCTION netblock_add
(
netAddress CIDR,
poolName VARCHAR(32),
regionName VARCHAR(32) DEFAULT NULL,
netActive BOOLEAN DEFAULT TRUE,
netDesc VARCHAR(128) DEFAULT NULL,
addIpEntries BOOLEAN DEFAULT FALSE
)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
netRegionId INTEGER DEFAULT NULL;
netPoolId INTEGER;
returnId INTEGER DEFAULT 1;
BEGIN
-- searches for region id
if ( regionName IS NOT NULL ) THEN
SELECT id INTO netRegionId
FROM region
WHERE name LIKE lower(regionName);
END IF;
-- searches for pool id
SELECT id INTO netPoolId
FROM pool
WHERE name LIKE lower(poolName);
IF ( netPoolId IS NULL ) THEN
RAISE EXCEPTION 'invalid pool';
END IF;
-- adds new pool
INSERT INTO netblock
( network,
poolId,
regionId,
active,
description
)
VALUES ( netAddress,
netPoolId,
netRegionId,
netActive,
netDesc
)
RETURNING id INTO returnId;
-- adds IP entries
if ( addIpEntries IS NOT FALSE ) THEN
PERFORM ip_add_network( ipNetwork := netAddress );
END IF;
RETURN returnId;
END;
$$;
/*
* adds contraints to netblocks
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION netblock_trigger
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
resultCount INTEGER;
BEGIN
-- verifies network is IPv4
IF ( family(NEW.network) != 4 ) THEN
RAISE EXCEPTION 'only IPv4 networks are supported';
END IF;
-- verifies that new region does not conflict with new network
SELECT count(id) INTO resultCount
FROM netblock
WHERE regionId != NEW.regionId
AND
( NEW.network >>= network
OR
NEW.network <<= network
);
IF ( resultCount != 0 ) THEN
RAISE EXCEPTION 'conflict with existing network regions';
END IF;
-- verifies that new region does not conflict with new network
SELECT count(id) INTO resultCount
FROM netblock
WHERE poolId != NEW.poolId
AND
( NEW.network >>= network
OR
NEW.network <<= network
);
IF ( resultCount != 0 ) THEN
RAISE EXCEPTION 'conflict with existing network pools';
END IF;
RETURN NEW;
END;
$$;
/*
* adds contraints to netblocks
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION netblock_trigger_delete
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
resultCount INTEGER;
BEGIN
-- checks for IP addresses which would be orphaned
SELECT count( address ) INTO resultCount
FROM ip
WHERE address NOT IN
(
SELECT address
FROM ip
INNER JOIN netblock
ON ip.address <<= netblock.network
WHERE ip.address <<= OLD.network
AND netblock.network <> OLD.network
)
AND address <<= OLD.network;
IF ( resultCount != 0 ) THEN
RAISE EXCEPTION 'IP addresses still defined within netblock';
END IF;
RETURN NEW;
END;
$$;
/*
* adds contraints to netblocks
*
* returns trigger
*/
CREATE OR REPLACE FUNCTION netblock_trigger_truncate
( )
RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
resultCount INTEGER;
BEGIN
-- checks for IP addresses which would be orphaned
SELECT count( address ) INTO resultCount
FROM ip;
IF ( resultCount != 0 ) THEN
RAISE EXCEPTION 'IP addresses still defined';
END IF;
RETURN NEW;
END;
$$;
/*
* Creates entry for IP Pool
*
* % SELECT pool_add(
* poolName := 'dhcp'
* );
* % SELECT pool_add(
* poolName := 'dhcp',
* poolDesc := 'Pool of IP addresses for PPPoE connections'
* );
*
* poolName
* : name of pool being created in the database
*
* poolDesc
* : human readable description of the pool
*
* Returns the `id` of the created pool
*/
CREATE OR REPLACE FUNCTION pool_add
(
poolName VARCHAR(32),
poolDesc VARCHAR(128) DEFAULT NULL
)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
returnId INTEGER;
BEGIN
-- adds new pool
INSERT INTO pool
( name,
description
)
VALUES ( lower(poolName),
poolDesc
)
RETURNING id INTO returnId;
RETURN returnId;
END;
$$;
/*
* Creates entry for geographic region
*
* % SELECT region_add(
* regionName := 'Anchorage'
* );
* % SELECT region_add(
* regionName := 'Anchorage',
* regionDesc := 'Anchorage Area'
* );
*
* regionName
* : name of region being created in the database
*
* regionDesc
* : human readable description of the region
*
* Returns the `id` of the created pool
*/
CREATE OR REPLACE FUNCTION region_add
(
regionName VARCHAR(32),
regionDesc VARCHAR(128) DEFAULT NULL
)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
returnId INTEGER;
BEGIN
-- adds new pool
INSERT INTO region
( name,
description
)
VALUES ( lower(regionName),
regionDesc
)
RETURNING id INTO returnId;
RETURN returnId;
END;
$$;
-- ////////////////
-- // //
-- // Triggers //
-- // //
-- ////////////////
-- address table triggers
CREATE TRIGGER ip_trigger
BEFORE INSERT OR UPDATE
ON ip
FOR EACH ROW
EXECUTE PROCEDURE ip_trigger();
CREATE TRIGGER ip_trigger_delete
BEFORE DELETE
ON ip
FOR EACH ROW
EXECUTE PROCEDURE ip_trigger_delete();
CREATE TRIGGER ip_trigger_truncate
BEFORE TRUNCATE
ON ip
FOR EACH STATEMENT
EXECUTE PROCEDURE ip_trigger_truncate();
-- netblock table triggers
CREATE TRIGGER netblock_trigger
BEFORE INSERT OR UPDATE
ON netblock
FOR EACH ROW
EXECUTE PROCEDURE netblock_trigger();
CREATE TRIGGER netblock_trigger_delete
BEFORE DELETE
ON netblock
FOR EACH ROW
EXECUTE PROCEDURE netblock_trigger_delete();
CREATE TRIGGER netblock_trigger_truncate
BEFORE TRUNCATE
ON netblock
FOR EACH STATEMENT
EXECUTE PROCEDURE netblock_trigger_truncate();
/* end of sql */
|