File: XMLRPC.pm

package info (click to toggle)
haci 0.98c-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,240 kB
  • sloc: perl: 23,790; javascript: 1,817; sh: 225; makefile: 8
file content (1099 lines) | stat: -rw-r--r-- 33,129 bytes parent folder | download | duplicates (3)
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
package HaCi::XMLRPC;

use strict;
use warnings;

use Frontier::RPC2;
use Data::Dumper;
use Encode;
use File::Basename;
use Digest::MD5 qw/md5_hex/;
use Time::HiRes qw/usleep/;

use HaCi::Conf;
use HaCi::HaCi;
use HaCi::Utils;
use HaCi::Mathematics;
use HaCi::Log qw/warnl debug/;

=head1 Class XMLRPC

This class provides a XML-RPC API for HaCi.

Basic usage:

	my $api         = new Frontier::Client(url => "http://$server/RPC2");
	my $session     = $api->call('login', [$user, $pass]);
	die 'Login failed!' unless $session;

	print Dumper($api->call($method, $session, @args));

	$api->call('logout', $session);

	exit 0;

=cut

my $aclCacheHandle	= undef;
my $netCacheHandle	= undef;
my $lockName		= undef;

$SIG{__DIE__}	= sub {&releaseLock($lockName) if defined $lockName};

my $methods	= {
	search										=> \&search,
	getFreeSubnets						=> \&getFreeSubnets,
	getFreeSubnetsFromSearch	=> \&getFreeSubnetsFromSearch,
	assignFreeSubnet					=> \&assignFreeSubnet,
	getSubnets								=> \&getSubnets,
	getNetworkDetails					=> \&getNetworkDetails,
	listRoots									=> \&listRoots,
	addRoot										=> \&addRoot,
	editRoot									=> \&editRoot,
	delRoot										=> \&delRoot,
	addNet										=> \&addNet,
	editNet										=> \&editNet,
	delNet										=> \&delNet,
	login											=> \&login,
	logout										=> \&logout,
};

sub handler {
	my $r						= shift;
	my $coder				= Frontier::RPC2->new();
	(my $workDir		= dirname( __FILE__ )) =~ s#/modules/HaCi##;
	&HaCi::Conf::init($workDir);

	my $content	= '';
	$r->read($content, $r->headers_in->get('Content-Length'));

	my $response	= '';
	eval {
		$response	= &serve($coder, $content);
	};
	if ($@) {
		$r->content_type("text/xml");
		$r->print(encode("UTF-8", $coder->encode_fault(1, 'Error: ' . $@)));
		return Apache2::Const::SERVER_ERROR;
	} else {
		#$r->content_type("application/json");
		$r->content_type("text/xml");
		$r->print(encode("UTF-8", $response));
		return Apache2::Const::OK;
	}
}

sub finalize {
	if (defined $netCacheHandle) {
		warn "Cannot set Cache (netCache-DB!)\n" unless $netCacheHandle->set('DB', $HaCi::HaCi::netCache->{DB});
		warn "Cannot set Cache (netCache-FILL!)\n" unless $netCacheHandle->set('FILL', $HaCi::HaCi::netCache->{FILL});
		warn "Cannot set Cache (netCache-NET!)\n" unless $netCacheHandle->set('NET', $HaCi::HaCi::netCache->{NET});
	}

	if (defined $aclCacheHandle) {
		warn "Cannot set Cache (aclCache!)\n" unless $aclCacheHandle->set('HASH', $HaCi::HaCi::aclCache);
	}
	$HaCi::HaCi::session->flush();
}

sub init {
	&HaCi::Utils::getConfig();

	($aclCacheHandle, $netCacheHandle)	= &HaCi::Utils::initCache();
	if (defined $netCacheHandle) {
		$HaCi::HaCi::netCache->{DB}		= $netCacheHandle->get('DB');
		$HaCi::HaCi::netCache->{NET}	= $netCacheHandle->get('NET');
		$HaCi::HaCi::netCache->{FILL}	= $netCacheHandle->get('FILL');
	} else {
		$HaCi::HaCi::netCache->{DB}		= {};
		$HaCi::HaCi::netCache->{NET}	= {};
		$HaCi::HaCi::netCache->{FILL}	= {};
	}
	$HaCi::HaCi::aclCache	= (defined $aclCacheHandle) ? $aclCacheHandle->get('HASH') : {};

	&HaCi::Utils::initTables();
}

sub serve {
	my $coder	= shift;
	my $xml		= shift;
	$xml		=~ s/(<\?XML\s+VERSION)/\L$1\E/;

	my $call;
	eval {
		$call	= $coder->decode($xml)
	};
	if ($@) {
		return $coder->encode_fault(1, "error decoding RPC.\n" . $@);
	}
	
	if ($call->{'type'} ne 'call') {
		return $coder->encode_fault(2,"expected RPC \`methodCall', got \`$call->{'type'}'\n");
	}

	my $method	= $call->{'method_name'};
	if (!defined $methods->{$method}) {
 		return $coder->encode_fault(3, "no such method \`$method'\n");
	}

	&init();
	return $coder->encode_fault(7, "error executing RPC \`$method'.\n" . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}))
		if $#{$HaCi::Conf::conf->{var}->{warnl}} > -1;

	my @values		= @{$call->{'value'}};
	my $authInfo	= shift @values;
	return $coder->encode_fault(7, "Authentication error: " . $HaCi::Conf::conf->{var}->{authenticationError}) unless &authenticate($authInfo);

  my $result;
	eval {
		$result	= &{$methods->{$method}}(@values);
	};
  if ($@) {
		return $coder->encode_fault(4, "error executing RPC \`$method'.\n" . $@);
	}
	
	my $response_xml = $coder->encode_response($result);

	&finalize();
	
	return $response_xml;
}

sub authenticate {
	my $authInfo	= shift;

	my $user				= undef;
	my $pass				= undef;
	my $sessionUser	= undef;
	my $bLogin			= 0;
	if (ref($authInfo) eq 'ARRAY') {
		&debug("Authentiating via username and password");
		$user									= ${$authInfo}[0];
		$pass									= ${$authInfo}[1];
		$HaCi::HaCi::session	= &HaCi::HaCi::getSession();
		$bLogin								= 1;
	}
	elsif (ref($authInfo) eq '') {
		&debug("Authentiating via sessionID: " . $authInfo);
		$HaCi::HaCi::session	= &HaCi::HaCi::getSession($authInfo);
		$sessionUser					= $HaCi::HaCi::session->param('username');
	}
	my $auth	= &HaCi::HaCi::authentication($user, $pass, $bLogin, $sessionUser);
	if ($auth) {
		&HaCi::Utils::getRights();
	}

	return $auth;
}

=over

=item login()

Login and return your session

 Params

 Returns
    - session        [string]    session token you can use for authentication

=cut

sub login {
	&debug("Login: return session id");
	return $HaCi::HaCi::session->id();
}

=item logout()

Logout

 Params

 Returns

=cut

sub logout {
	&debug("Logout: delete session");
	$HaCi::HaCi::session->delete();
	$HaCi::HaCi::session->flush();

	return 1;
}

=item search()

Search networks

 Params
    - search          [string]  string to search for
    - state           [string]  filter the result by that network state (ALLOCATED PA, ASSIGNED, ...)
    - exact           [string]  don't do a substring search
    - template name   [string]  filter the result by that template name
    - template query  [hash]    filter the result by defining special values for template entries (i.e.: {name => 'POOL1'})
    - root name       [string]  limit the search in that root
    - nrOfFreeSubs    [boolean] compute the number of free subnets in each found network
    - withDetails     [boolean] show details for found networks
    - tags            [string]  return only networks with this tags, seperate with spaces (i.e.: 'OR foo bar', 'AND foo bar')
 Returns
    - networks        [array]    array of found network blocks (hash)
                                 - netID
                                 - network
                                 - rootName
                                 - description
                                 - state
                                 - nrOfFreeSubs
 Example
    my $networks = search('Test', 'ASSIGNED', 0, 'DSL-POOL', {name => 'Pool1'}, 'DSL-Test-Pool-Root', 0);

=cut

sub search {
	my $searchStr		= shift || '';
	my $state				= shift || '';
	my $exact				= shift || 0;
	my $tmplName		= shift || '';
	my $tmplQuery		= shift || {};
	my $rootName		= shift || '';
	my $shNrOfFree	= shift || 0;
	my $withDetails	= shift || 0;
	my $tagsT				= shift || '';
	$tagsT					=~ s/^\s+//;
	$tagsT					=~ s/\s+$//;
	my @tags				= split(/\s+/, $tagsT);
	my $tagOp				= ($tagsT ne '') ? shift @tags : 0;
	if ($tagsT ne '' && scalar @tags == 0) {
		@tags		= ($tagOp);
		$tagOp	= 'OR';
	}

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('search');

	my $tmplID	= ($tmplName ne '') ? &HaCi::Utils::tmplName2ID($tmplName) : -1;
	unless (defined $tmplID) {
		&daw("No such Template found! ($tmplName)\n");
	}

	my $rootID	= ($rootName ne '') ? &HaCi::Utils::rootName2ID($rootName) : -1;
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}
	$rootID	= 0 if $rootID == -1;

	my $tmplBox	= {};
	if ($tmplID != -1) {
		foreach (keys %{$tmplQuery}) {
			my $key						= $_;
			my $value					= $tmplQuery->{$key};
			my $tmplEntryID		= &HaCi::Utils::tmplEntryDescr2EntryID($tmplID, $key);
			next unless defined $tmplEntryID;

			$tmplBox->{$tmplEntryID}	= $value;
		}
	}

	my $stateID	= -1;
	if ($state ne '') {
		$stateID	= &HaCi::Utils::networkStateName2ID($state);
		&daw("State not known! ($state)\nAvailable states: " . join(', ', map {$_->{name}} @{$HaCi::Conf::conf->{static}->{misc}->{networkstates}}) . "\n") unless $stateID;
	}

	my $networks	= &HaCi::Utils::search(
		$searchStr, 
		($exact) ? 0 : 1, 
		0, 
		$shNrOfFree, 
		$rootID, 
		$stateID,
		$tmplID, 
		$tmplBox,
		\@tags,
		$tagOp,
	);

	map {
		delete $_->{url};
		delete $_->{nrOfFreeSubs} unless $shNrOfFree;
		if ($withDetails) {
			my $orig				= $_;
			$_							= &getNetworkDetails($_->{rootName}, $_->{network});
			$_->{netID}			= $_->{ID};
			$_->{tmplName}	= &HaCi::Utils::tmplID2Name($_->{tmplID});
			$_->{rootName}	= $rootName;
			$_->{nrOfFreeSubs}	= $orig->{nrOfFreeSubs} if $shNrOfFree;
			delete $_->{ipv6ID};
			delete $_->{ID};
			delete $_->{tmplID};
			delete $_->{rootID};
		}
	} @{$networks};

	return $networks;
}

=item getFreeSubnets()

Find free subnets in a specified root and supernet and return the wanted amount of subnets

 Params
    - root name       [string]  search free subnets in that root
    - supernet        [network] search free subnets in that supernet (e.g. 192.168.0.0/24, 2001::/120)
    - cidr            [integer] specify the target cidr of your subnets (e.g. 30)
    - amount          [integer] how many subnets you want to get
 Returns
    - networks        [array]   array of free subnets found (hash)
                                - network
 Example
    my $freeSubnets = getFreeSubnets('Test root', '10.184.120.0/23', 32, 1);

=cut

sub getFreeSubnets {
	my $rootName	= shift;
	my $network		= shift;
	my $size			= shift || 0;
	my $amount		= shift || 0;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('search');
	&daw("You have to provide a root name!\n") unless $rootName;
	my $rootID	= &HaCi::Utils::rootName2ID($rootName);
	unless ($rootID) {
		&daw("No such Root found! ($rootName)\n");
		return;
	}

	&daw("You have to provide a network from which you want to get a free subnet!\n") unless $network;

	my $ipv6				= ($network =~ /:/) ? 1 : 0;
	my $networkDec  = ($ipv6) ? &HaCi::Mathematics::netv62Dec($network) : &HaCi::Mathematics::net2dec($network);
	my $ipv6ID			= ($ipv6) ? &HaCi::Utils::netv6Dec2ipv6ID($networkDec) : '';
	my $netID				= &HaCi::Utils::getNetID($rootID, $networkDec, $ipv6ID);

	unless ($netID) {
		&daw("No such network found! ($network)\n");
		return;
	}
	unless (&HaCi::Utils::checkNetACL($netID, 'r')) {
		&daw("Not enouph permissions to read this Network\n");
		return;
	}

	my @freeSubnets	= &HaCi::Utils::getFreeSubnets($netID, 0, $size, $amount || 1);
	return \@freeSubnets;
}

=item getFreeSubnetsFromSearch()

Search networks and return free subnets from them

 Params
    - search          [string]  string to search for
    - state           [string]  filter the result by that network state (ALLOCATED PA, ASSIGNED, ...)
    - exact           [string]  don't do a substring search
    - template name   [string]  filter the result by that template name
    - template query  [hash]    filter the result by defining special values for template entries (i.e.: {name => 'POOL1'})
    - root name       [string]  limit the search in that root
    - cidr            [integer] specify the target cidr of your subnets (e.g. 30)
    - amount          [integer] how many subnets you want to get
 Returns
    - networks        [array]   array of free subnets found (hash)
                                - network
                                - root name
 Example
    my $freeSubnets = getFreeSubnetsFromSearch('search me', '', 1, '', {}, 'Test-Root', 29, 3);

=cut

sub getFreeSubnetsFromSearch {
	my $searchStr	= shift || '';
	my $state			= shift || '';
	my $exact			= shift || 0;
	my $tmplName	= shift || '';
	my $tmplQuery	= shift || {};
	my $rootName	= shift || '';
	my $size			= shift || 0;
	my $amount		= shift || 0;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('search');
	&daw("You have to provide a search string!\n") unless $searchStr;

	my $tmplID	= ($tmplName ne '') ? &HaCi::Utils::tmplName2ID($tmplName) : -1;
	unless (defined $tmplID) {
		&daw("No such Template found! ($tmplName)\n");
	}

	my $rootID	= ($rootName ne '') ? &HaCi::Utils::rootName2ID($rootName) : -1;
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}
	$rootID	= 0 if $rootID == -1;

	my $tmplBox	= {};
	if ($tmplID > -1) {
		foreach (keys %{$tmplQuery}) {
			my $key						= $_;
			my $value					= $tmplQuery->{$key};
			my $tmplEntryID		= &HaCi::Utils::tmplEntryDescr2EntryID($tmplID, $key);
			next unless defined $tmplEntryID;

			$tmplBox->{$tmplEntryID}	= $value;
		}
	}

	my $stateID	= -1;
	if ($state ne '') {
		$stateID	= &HaCi::Utils::networkStateName2ID($state);
		&daw("State not known! ($state)\nAvailable states: " . join(', ', map {$_->{name}} @{$HaCi::Conf::conf->{static}->{misc}->{networkstates}}) . "\n") unless $stateID;
	}

	my $networks	= &HaCi::Utils::searchAndGetFreeSubnets(
		$searchStr, 
		($exact) ? 0 : 1, 
		0, 
		0, 
		$rootID, 
		$stateID,
		$tmplID, 
		$tmplBox, 
		$size, 
		$amount
	);

	map {
		delete $_->{url};
		delete $_->{dec};
		delete $_->{rootID};
	} @{$networks};

	return $networks;
}

=item listRoots()

Add a root to HaCi

 Params
 Returns
    - roots        [array]    array of found roots (hash)
                                 - ID
                                 - name
                                 - ipv6 (boolean)

=cut

sub listRoots {

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('showRoots');
	
	my $roots	= HaCi::Utils::getRoots();

	return $roots
}

=item addRoot()

Add a root to HaCi

 Params
    - root name       [string]  add a root with this name
    - description     [string]  description of the root
    - ipv6            [boolean] this root contains IPv6 networks
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub addRoot {
	my $rootName		= shift || '';
	my $descr				= shift || '';
	my $ipv6				= shift || 0;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('addRoot');

	my $success	= &HaCi::Utils::addRoot(
		$rootName,
		$descr,
		$ipv6,
	);

	my $error	= ($success) ? '' : "error adding a new root '$rootName': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item editRoot()

Edit an existing root

 Params
    - root name      [string]  edit this root
    - new root name  [string]  change root name
    - description    [string]  new description of the root
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub editRoot {
	my $rootName		= shift || '';
	my $newRootName	= shift || '';
	my $description	= shift || '';
	$newRootName		= $rootName if $newRootName eq '';

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('editRoot');
	&daw("No root name committed!\n") unless $rootName;

	my $rootID	= &HaCi::Utils::rootName2ID($rootName);
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}

	my $root			= &HaCi::Utils::getMaintInfosFromRoot($rootID);
	$description	= $root->{description} if $description eq '';

	my $success	= &HaCi::Utils::addRoot(
		$newRootName,
		$description,
		$root->{ipv6},
		$rootID,
		1,
	);

	my $error	= ($success) ? '' : "error while modifying this root '$rootName': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item delRoot()

Delete a root from HaCi

 Params
    - root name       [string]  remove the root
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub delRoot {
	my $rootName		= shift || '';

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('editRoot');
	&daw("No root name committed!\n") unless $rootName;

	my $rootID	= &HaCi::Utils::rootName2ID($rootName);
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}

	my $success	= &HaCi::Utils::delRoot($rootID);
	my $error		= ($success) ? '' : "error removing this root '$rootName': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item addNet()

Add a network to HaCi

 Params
    - root name       [string]  add the network to this root
    - network         [network] add this network (e.g. 192.168.0.1/32, 2001::dead:beef:0/125)
    - description     [string]  description of the network
    - state           [string]  state of the network (e.g. ALLOCATED PA, ASSIGNED, ...)
    - def subnet Size [integer] define a default subnet cidr size (e.g. 30)
    - template name   [string]  assign a template to the network
    - template values [hash]    pass template values (e.g. {hostname => 'abc.de', os => 'redhat'})
    - tags						[string]  add tags (seperate by comma)
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub addNet {
	my $rootName		= shift || '';
	my $network			= shift || '';
	my $descr				= shift || '';
	my $state				= shift || '';
	my $defSubSize	= shift || 0;
	my $tmplName		= shift || '';
	my $tmplValues	= shift || {};
	my $tags				= shift || '';
	my $isIPv4      = qr/(\d{1,3}\.){3}\d{1,3}/i;
	my $isIPv6      = qr/(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:)(?<!\.)\z/i;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('addNet');

	my ($ip, $cidr)	= split(/\//, $network);
	&daw("This '$ip' is not a valid ip address (i.e.: 1.2.3.4/30, 2001:dead:beaf::/66)\n") if $ip !~ /$isIPv4/ && $ip !~ /$isIPv6/;

	my $stateID	= 0;
	if ($state ne '') {
		$stateID	= &HaCi::Utils::networkStateName2ID($state);
		&daw("State not known! ($state)\nAvailable states: " . join(', ', map {$_->{name}} @{$HaCi::Conf::conf->{static}->{misc}->{networkstates}}) . "\n") unless $stateID;
	}

	my $tmplID	= ($tmplName ne '') ? &HaCi::Utils::tmplName2ID($tmplName) : -1;
	unless (defined $tmplID) {
		&daw("No such Template found! ($tmplName)\n");
	}
	my $rootID	= ($rootName ne '') ? &HaCi::Utils::rootName2ID($rootName) : -1;
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}
	$rootID	= 0 if $rootID == -1;

	my $tmplValueBox	= {};
	if ($tmplID != -1) {
		my $tmplEntries	= &HaCi::Utils::getTemplateEntries($tmplID, 0, 0, 1, 0, 1);
		foreach (keys %{$tmplEntries}) {
			my $id		= $_;
			my $name	= $tmplEntries->{$id};
			foreach (keys %{$tmplValues}) {
				if ($name eq $_) {
					$tmplValueBox->{$id}	= $tmplValues->{$_};
				}
			}
		}
	}

	my $success	= &HaCi::Utils::addNet(
		0,
		$rootID,
		$ip,
		$cidr,
		$descr,
		$stateID,
		$tmplID,
		$defSubSize,
		1,
		$tmplValueBox,
		0,
		$tags,
	);

	my $error	= ($success) ? '' : "error adding this new network '$ip/$cidr': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item editNet()

Edit an existing network

 Params
    - root name   [string]  edit the network in this root
    - network     [network] edit this network (e.g. 192.168.0.1/32, 2001::dead:beef:0/125)
    - changes     [hash]    changes (valid attributes are: network description state rootName defSubnetSize tmplName tmplValues)
                            e.g.:
                             {description => 'test_8'}
                             {network => '192.168.0.8/29', defSubnetSize=>30, state=>'FREE', rootName=>'larsux.de'}
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub editNet {
	my $rootName		= shift || '';
	my $network			= shift || '';
	my $changeData	= shift || {};
	my @keys				= qw(network description state rootName defSubnetSize tmplName tmplValues tags);

	my $isIPv4      = qr/(\d{1,3}\.){3}\d{1,3}/i;
	my $isIPv6      = qr/(((?=(?>.*?::)(?!.*::)))(::)?([0-9A-F]{1,4}::?){0,5}|([0-9A-F]{1,4}:){6})(\2([0-9A-F]{1,4}(::?|$)){0,2}|((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\.|$)){4}|[0-9A-F]{1,4}:[0-9A-F]{1,4})(?<![^:]:)(?<!\.)\z/i;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('editNet');
	&daw("No root name committed!\n") unless $rootName;
	&daw("No network committed!\n") unless $network;
	&daw("The third parameter ('changeData') must be a hash!\n") unless ref($changeData) eq 'HASH';

	foreach (keys %{$changeData}) {
		my $key	= $_;
		unless (grep {/^$key$/} @keys) {
			&daw("Unknown change-attribute: '$key'. Valid attributes are: " . join(', ', @keys) . "\n");
		}
	}

	my $rootID	= &HaCi::Utils::rootName2ID($rootName);
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}

	my $newRootID	= $rootID;
	if (exists $changeData->{rootName}) {
		$newRootID	= &HaCi::Utils::rootName2ID($changeData->{rootName});
		unless ($newRootID) {
			&daw("No such root found! ($changeData->{rootName})\n");
		}
	}

	my $newNetwork				= (exists $changeData->{network}) ? $changeData->{network} : $network;
	my ($ip, $cidr)				= split(/\//, $network);
	my ($newIP, $newCIDR)	= split(/\//, $newNetwork);
	&daw("This '$ip' is not a valid ip address (i.e.: 1.2.3.4/30, 2001:dead:beaf::/66)\n") if $ip !~ /$isIPv4/ && $ip !~ /$isIPv6/;
	&daw("This '$newIP' is not a valid ip address (i.e.: 1.2.3.4/30, 2001:dead:beaf::/66)\n") if $newIP !~ /$isIPv4/ && $newIP !~ /$isIPv6/;

	my $ipv6				= ($network =~ /:/) ? 1 : 0;
	my $networkDec  = ($ipv6) ? &HaCi::Mathematics::netv62Dec($network) : &HaCi::Mathematics::net2dec($network);
	my $ipv6ID			= ($ipv6) ? &HaCi::Utils::netv6Dec2ipv6ID($networkDec) : '';
	my $netID				= &HaCi::Utils::getNetID($rootID, $networkDec, $ipv6ID);
	unless ($netID) {
		&daw("No such network found! ($network)\n");
		return;
	}
	my $netDetails	= &HaCi::Utils::getMaintInfosFromNet($netID);
	my $descr				= (exists $changeData->{description}) ? $changeData->{description} : $netDetails->{description};
	my $defSubSize	= (exists $changeData->{defSubnetSize}) ? $changeData->{defSubnetSize} : $netDetails->{defSubnetSize};
	my $tags				= (exists $changeData->{tags}) ? $changeData->{tags} : join(',', @{$netDetails->{tags}});

	my $stateID	= $netDetails->{state};
	if (exists $changeData->{state}) {
		$stateID	= &HaCi::Utils::networkStateName2ID($changeData->{state});
		&daw("State not known! ($changeData->{state})\nAvailable states: " . join(', ', map {$_->{name}} @{$HaCi::Conf::conf->{static}->{misc}->{networkstates}}) . "\n") unless $stateID;
	}

	my $tmplID	= (exists $changeData->{tmplName}) ? $changeData->{tmplName} : $netDetails->{tmplID};
	if (exists $changeData->{tmplName}) {
		$tmplID	= &HaCi::Utils::tmplName2ID($changeData->{tmplName});
		unless (defined $tmplID) {
			&daw("No such Template found! ($changeData->{tmplName})\n");
		}
	}

	my $tmplValueBox	= 0;
	if (exists $changeData->{tmplValues}) {
		$tmplValueBox	= {};
		die "Template values must be a hash\n" unless ref($changeData->{tmplValues}) eq 'HASH';
		my $tmplEntries	= &HaCi::Utils::getTemplateEntries($tmplID, 0, 0, 1, 0, 1);
		foreach (keys %{$tmplEntries}) {
			my $id		= $_;
			my $name	= $tmplEntries->{$id};
			foreach (keys %{$changeData->{tmplValues}}) {
				if ($name eq $_) {
					$tmplValueBox->{$id}	= $changeData->{tmplValues}->{$_};
				}
			}
		}
	}

	my $success	= &HaCi::Utils::addNet(
		$netID,
		$newRootID,
		$newIP,
		$newCIDR,
		$descr,
		$stateID,
		$tmplID,
		$defSubSize,
		1,
		$tmplValueBox,
		1,
		$tags,
	);

	my $error	= ($success) ? '' : "error while modifying this network '$ip/$cidr': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item delNet()

Delete a network from HaCi

 Params
    - root name       [string]  remove the netork from this root
    - network         [network] remove this network (e.g. 192.168.0.1/32, 2001::dead:beef:0/125)
    - network lock    [integer] lock network for X seconds
    - withSubnets     [boolean] also remve subnets
 Returns
    - success         [string] 0 on success, error-String at error

=cut

sub delNet {
	my $rootName			= shift || '';
	my $network				= shift || 0;
	my $networkLock		= shift || $HaCi::Conf::conf->{user}->{misc}->{dftnetworklock} || 0;
	my $bWithSubnets	= shift || 0;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('editNet');

	my $rootID	= ($rootName ne '') ? &HaCi::Utils::rootName2ID($rootName) : -1;
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}
	$rootID	= 0 if $rootID == -1;

	my $ipv6				= ($network =~ /:/) ? 1 : 0;
	my $networkDec  = ($ipv6) ? &HaCi::Mathematics::netv62Dec($network) : &HaCi::Mathematics::net2dec($network);
	my $ipv6ID			= ($ipv6) ? &HaCi::Utils::netv6Dec2ipv6ID($networkDec) : '';
	my $netID				= &HaCi::Utils::getNetID($rootID, $networkDec, $ipv6ID);

	unless ($netID) {
		&daw("No such network found! ($network)\n");
		return;
	}

	my $success	= &HaCi::Utils::delNet($netID, $bWithSubnets, 0, $networkLock);
	my $error	= ($success) ? '' : "error removing this new network '$network': " . join("\n", @{$HaCi::Conf::conf->{var}->{warnl}}) . "\n";
	&warnl($error) if $error;

	return ($success) ? 0 : $error;
}

=item assignFreeSubnet()

Find the next free subnet and assign it in one step

 Params
    - root name       [string]  search free subnets in that root
    - supernet        [network] search free subnets in that supernet (e.g. 192.168.0.0/24, 2001::/120)
    - cidr            [integer] specify the target cidr of your subnets (e.g. 30)
    - description     [string]  description of the network
    - state           [string]  state of the network (e.g. ALLOCATED PA, ASSIGNED, ...)
    - def subnet Size [integer] define a default subnet cidr size (e.g. 30)
    - template name   [string]  assign a template to the network
    - template values [hash]    pass template values (e.g. {hostname => 'abc.de', os => 'redhat'})
 Returns
    - network details [hash]    new network assigned:
                                - netID
                                - network
                                - modify date
                                - ipv6 (boolean)
                                - description
                                - state
                                - create from
                                - create date
                                - default subnet cidr size
                                - template name
                                - modify from

=cut

sub assignFreeSubnet {
	my $rootName			= shift;
	my $network				= shift;
	my $size					= shift || 0;
	my $descr					= shift || '';
	my $state					= shift || '';
	my $defSubSize		= shift || 0;
	my $tmplName			= shift || '';
	my $tmplValueBox	= shift || {};
	my $secondTry			= shift || 0;
	my $amount				= 1;

	&getLock($network);

	my $freeNetworks	= &getFreeSubnets($rootName, $network, $size, $amount);
	&daw("No free subnets found!\n") if scalar @{$freeNetworks} < 1;
	my $freeNet	= ${$freeNetworks}[0];

	if (my $error = &addNet($rootName, $freeNet, $descr, $state, $defSubSize, $tmplName, $tmplValueBox)) {
		if (($error =~ /allready exists/ || $error =~ /already exists/) && !$secondTry) { # retry one time
			&warnl("Network already exists! Retry one more time...");
			&releaseLock($network);
			usleep(rand(1000000));
			return &assignFreeSubnet($rootName, $network, $size, $descr, $state, $defSubSize, $tmplName, $tmplValueBox, 1);
		} else {
			&releaseLock($network);
			&daw($error);
		}
	} else {
		my $netDetails					= &getNetworkDetails($rootName, $freeNet);
		$netDetails->{netID} 		= $netDetails->{ID};
		$netDetails->{tmplName} = &HaCi::Utils::tmplID2Name($netDetails->{tmplID});
		$netDetails->{rootName} = $rootName;
		delete $netDetails->{ipv6ID};
		delete $netDetails->{ID};
		delete $netDetails->{tmplID};
		delete $netDetails->{rootID};

		&releaseLock($network);
		return $netDetails;
	}
}

=item getNetworkDetails()

Get details from a network in HaCi

 Params
    - root name       [string]  get details of a network in this root
    - network         [network] get details of this network (e.g. 192.168.0.1/32, 2001::dead:beef:0/125)
 Returns
    - network         [hash]    network details:
                                - netID
                                - network
                                - modify date
                                - ipv6 (boolean)
                                - description
                                - state
                                - create from
                                - create date
                                - default subnet cidr size
                                - template name
                                - modify from

=cut

sub getNetworkDetails {
	my $rootName	= shift;
	my $network		= shift;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('showNetDet');

	my $rootID	= ($rootName ne '') ? &HaCi::Utils::rootName2ID($rootName) : -1;
	unless ($rootID) {
		&daw("No such root found! ($rootName)\n");
	}
	$rootID	= 0 if $rootID == -1;

	my $ipv6				= ($network =~ /:/) ? 1 : 0;
	my $networkDec  = ($ipv6) ? &HaCi::Mathematics::netv62Dec($network) : &HaCi::Mathematics::net2dec($network);
	my $ipv6ID			= ($ipv6) ? &HaCi::Utils::netv6Dec2ipv6ID($networkDec) : '';
	my $netID				= &HaCi::Utils::getNetID($rootID, $networkDec, $ipv6ID);

	unless ($netID) {
		&daw("No such network found! ($network)\n");
		return;
	}

	my $netDetails	= &HaCi::Utils::getMaintInfosFromNet($netID);
	$netDetails->{network}	= $network;
	$netDetails->{state}		= &HaCi::Utils::networkStateID2Name($netDetails->{state});

	if ($netDetails->{tmplID}) {
		$netDetails->{templateValues}	= &HaCi::Utils::getTemplateData($netID, $netDetails->{tmplID}, 1);
		$netDetails->{template}				= &HaCi::Utils::tmplID2Name($netDetails->{tmplID});
	}

	return $netDetails;
}

sub daw {
	my $msg	= shift;

	&warnl($msg);
	die $msg;
}

sub getLock {
	my $name	= shift;
	my $fileName	= md5_hex($name) . '.lock';
	my $lockFile	= $HaCi::Conf::conf->{static}->{path}->{lockpath} . '/' . $fileName;

	my $waitCnter	= 5;
	while (-f $lockFile) {
		warn "Lockfile '$lockFile' for '$name' found. Waiting...\n";
		usleep(rand(1000000));
		last unless $waitCnter--;
	}

	if (-f $lockFile) {
		my $lockProc	= 'unkonwn';
		unless (open F, $lockFile) {
			warn "Cannot open lockfile '$lockFile' for reading: $!\n";
		} else {
			$lockProc	= <F>;
			close F;
		}
		warn "Locking prozess '$lockProc' seems to die. Overwriting lockfile '$lockFile'.\n";
	}

	open F, '>' . $lockFile or warn "Cannot open lockfile '$lockFile' for writing: $!\n";
	print F $$;
	close F;

	$lockName	= $name;
}

sub releaseLock {
	my $name	= shift;
	my $fileName	= md5_hex($name) . '.lock';
	my $lockFile	= $HaCi::Conf::conf->{static}->{path}->{lockpath} . '/' . $fileName;

	unlink $lockFile or warn "Cannot release lockfile '$lockFile': $!\n";

	$lockName	= undef;
}

=item getSubnets()

Get subnets from a root and optional from a supernet

 Params
    - root name       [string]  get subnets from this root
    - supernet        [network] get subnets from this network (e.g. 192.168.0.1/32, 2001::dead:beef:0/125)
 Returns
    - networks        [array]    array of found networks (hash)
                                 - netID
                                 - network
                                 - modify date
                                 - ipv6 (boolean)
                                 - description
                                 - state
                                 - create from
                                 - create date
                                 - default subnet cidr size
                                 - template name
                                 - modify from
 Example
    my $subnets = getSubnets('Test');
    my $subnets = getSubnets('Test', '192.168.0.0/24');

=cut
sub getSubnets {
	my $rootName	= shift;
	my $network		= shift || 0;

	&daw("Permission denied!\n") unless &HaCi::Utils::checkRight('showNets');
	&daw("You have to provide a root name!\n") unless $rootName;
	my $rootID	= &HaCi::Utils::rootName2ID($rootName);
	unless ($rootID) {
		&daw("No such Root found! ($rootName)\n");
		return;
	}
	my $ipv6	= &HaCi::Utils::rootID2ipv6($rootID);

	my @childs	= ();
	if ($network) {
		my $networkDec  = ($ipv6) ? &HaCi::Mathematics::netv62Dec($network) : &HaCi::Mathematics::net2dec($network);
		my $ipv6ID			= ($ipv6) ? &HaCi::Utils::netv6Dec2ipv6ID($networkDec) : '';
		my $netID				= &HaCi::Utils::getNetID($rootID, $networkDec, $ipv6ID);

		@childs	= &HaCi::Utils::getNetworkChilds($netID, 0, 0);
	} else {
		@childs	= &HaCi::Utils::getNetworkChilds($rootID, 1, 0);
	}

	my @childDetails	= map { 
		my $networkDec					= $_->{network};
		my $network							= ($ipv6) ? &HaCi::Mathematics::netv6Dec2net($networkDec) : &HaCi::Mathematics::dec2net($networkDec);
		my $netDetails					= &getNetworkDetails($rootName, $network);
		$netDetails->{netID} 		= $netDetails->{ID};
		$netDetails->{tmplName} = &HaCi::Utils::tmplID2Name($netDetails->{tmplID});
		$netDetails->{rootName} = $rootName;
		delete $netDetails->{ipv6ID};
		delete $netDetails->{ID};
		delete $netDetails->{tmplID};
		delete $netDetails->{rootID};

		$netDetails;
	} @childs;

	return \@childDetails;
}

=back

=cut

1;

# vim:ts=2:sts=2:sw=2