Skip to content

Commit

Permalink
First version to write script
Browse files Browse the repository at this point in the history
  • Loading branch information
agustim committed Nov 10, 2013
1 parent c192b28 commit 12d559d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 43 deletions.
12 changes: 11 additions & 1 deletion app/Controller/NetworksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ public function isAuthorized($user) {
* @return void
*/
public function index() {
$this->Network->recursive = 0;
//$this->Network->recursive = 0;
$this->Network->unbindModel(
array('hasMany' => array ('Node'))
);
if($this->Auth->user('role') != "admin"){
$this->set('networks', $this->paginate(array('Network.user_id'=>$this->Auth->user('id'))));
} else {
$this->Network->bindModel(
array('belongsTo' =>
array( 'User' => array('className' => 'User',
'foreignKey' => 'user_id')
)
)
);
$this->set('networks', $this->paginate());
}
}
Expand Down
29 changes: 14 additions & 15 deletions app/Controller/NodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,26 @@ public function _configure($id = null, $allconfig = 1, $encrypt = 1) {
if (!$this->Node->exists()) {
throw new NotFoundException(__('Invalid node'));
}
$this->Node->unbindModel(
array('hasMany' => array('Connectto','Connectfrom'))
);
$this->set('allconfig',$allconfig);
$this->set('encrypt', $encrypt);
$node = $this->Node->read(null, $id);
$this->set('node', $node);
if($node['Network']['automaticconnectto']) {
if ($node['Node']['is_gateway']) {
//$this->set('connectfrom',$this->Node->query("select Node.* from node Node where Node.network_id = ".$node['Node']['network_id']." and Node.id <> ".$id." and Node.address <> '' and Node.address is not null"));
$this->set('connectfrom',$this->Node->query("select Node.* from node Node where Node.network_id = ".$node['Node']['network_id']." and Node.id <> ".$id));
} else {
$this->set('connectfrom',$this->Node->query("select Node.* from node Node where Node.network_id = ".$node['Node']['network_id']." and Node.is_gateway"));
}
} else {
$this->set('connectfrom',$this->Node->query("select Node.* from connect c inner join node Node on (c.node_connectto_id = Node.id) where c.node_id = $id"));
}
$this->set('connectto',$this->Node->query("select Node.* from connect c inner join node Node on (c.node_id = Node.id) where c.node_connectto_id = $id"));
$this->Node->unbindModel(
array('belongsTo' => array ('Network'))
);
$this->set('connectto', $this->Node->find('all',
array('recursive'=>0,
'conditions'=>array(
'Node.network_id' => $node['Node']['network_id'],
'Node.id !=' => $node['Node']['id']
)
)
)
);
$this->response->type('text');
//$this->layout='ajax';
$this->render('genscript','ajax');
Configure::write('debug', 0);
//$this->render('genscript');
}

public function configure($id = null, $allconfig = 1, $encrypt = 1) {
Expand Down
4 changes: 2 additions & 2 deletions app/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class UsersController extends AppController {
);

public function isAuthorized($user) {
if (in_array($this->action, array('login','logout','register','add'))) {
if (in_array($this->action, array('login','logout','register'))) {
return true;
}
// Falta arreglar-ho!!!
if (in_array($this->action, array('add','index','view','delete'))) {
if ( $user['role'] == 'admin' && in_array($this->action, array('add','index','view','delete'))) {
return true;
}

Expand Down
Binary file modified app/DataBase/getinconf.db
Binary file not shown.
15 changes: 15 additions & 0 deletions app/View/Networks/index.ctp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<div class="networks index">
<h2><?php echo __('Networks'); ?></h2>
<table cellpadding="0" cellspacing="0" class="table table-bordered table-striped">
Expand All @@ -7,6 +8,13 @@
<th><?php echo $this->Paginator->sort('netmask'); ?></th>
<th><?php echo $this->Paginator->sort('bitmask'); ?></th>
<th><?php echo $this->Paginator->sort('trustednodes','Trusted'); ?></th>
<?php
if ( AuthComponent::user('role') == 'admin' ) {
?>
<th><?php echo $this->Paginator->sort('user_id','User'); ?></th>
<?php
}
?>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php
Expand All @@ -17,6 +25,13 @@
<td><?php echo h($network['Network']['netmask']); ?>&nbsp;</td>
<td><?php echo h($network['Network']['bitmask']); ?>&nbsp;</td>
<td><?php echo ($network['Network']['trustednodes'])?"Yes":"No"; ?></td>
<?php
if ( AuthComponent::user('role') == 'admin' ) {
?>
<td><?php echo h($network['User']['username']); ?></td>
<?php
}
?>
<td class="actions">
<?php echo $this->Html->link("<i class='icon-file'></i>", array('action' => 'view',
$network['Network']['id']), array('class'=> 'btn',
Expand Down
40 changes: 15 additions & 25 deletions app/View/Nodes/genscript.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,37 @@
$pagecode .= "mkdir -p ".$tincpath.$node['Network']['name']."\n";
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/tinc.conf <<EOF\n";

foreach($connectfrom as $cnode) {
foreach($connectto as $cnode) {
if ($cnode['Node']['rsakeypub'] != "" ) {
$pagecode .= "ConnectTo=".$cnode['Node']['name']."\n";
}
}
$pagecode .= "Device=".$node['Node']['device']."\n";
$pagecode .= "Mode=switch\n";
$pagecode .= "Name=".$node['Node']['name']."\n";
$pagecode .= "PrivateKeyFile=/etc/tinc/rsa_key.priv\n";
$pagecode .= "EOF\n";
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/tinc-up <<EOF\n";
$pagecode .= "#!/bin/sh\n";
$pagecode .= 'ifconfig \$INTERFACE '.$node['Node']['ip']." netmask ".$node['Network']['netmask']."\n";
$pagecode .= "iptables -t nat -A POSTROUTING -s ".$node['Network']['ip']."/".$node['Network']['bitmask']." ! -d ".$node['Network']['ip']."/".$node['Network']['bitmask']." -j MASQUERADE\n";
$pagecode .= 'ip -4 addr add '.$node['Node']['ip'].'/'.$node['Network']['bitmask'].' dev \$INTERFACE'."\n";
$pagecode .= 'ip link set \$INTERFACE up'."\n";
$pagecode .= "EOF\n";
$pagecode .= "chmod +x ".$tincpath.$node['Network']['name']."/tinc-up\n";
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/tinc-down <<EOF\n";
$pagecode .= "#!/bin/sh\n";
$pagecode .= 'ifconfig \$INTERFACE down'."\n";
$pagecode .= "iptables -t nat -D POSTROUTING -s ".$node['Network']['ip']."/".$node['Network']['bitmask']." ! -d ".$node['Network']['ip']."/".$node['Network']['bitmask']." -j MASQUERADE\n";
$pagecode .= "EOF\n";
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/tinc-down <<EOF\n";
$pagecode .= "#!/bin/sh\n";
$pagecode .= 'ip link set \$INTERFACE down'."\n";
$pagecode .= 'ip -4 addr del '.$node['Node']['ip'].'/'.$node['Network']['bitmask'].' dev \$INTERFACE'."\n";
$pagecode .= "EOF\n";
$pagecode .= "chmod +x ".$tincpath.$node['Network']['name']."/tinc-down\n";
$pagecode .= "mkdir -p ".$tincpath.$node['Network']['name']."/hosts\n";
}
/* All nodes what are your node connect */
foreach($connectfrom as $cnode) {
if ($cnode['Node']['rsakeypub'] != "" ) {
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/hosts/".$cnode['Node']['name']." <<EOF\n";
$pagecode .= "Compression=11\n";
$pagecode .= "Subnet=".$cnode['Node']['ip']."/".$cnode['Node']['bitmask']."\n";
if($cnode['Node']['address']) { $pagecode .= "Address=".$cnode['Node']['address']."\n"; }
$pagecode .= "\n";
$pagecode .= $cnode['Node']['rsakeypub']."\n";
$pagecode .= "EOF\n";
}
}

/* All nodes to connect you node */
foreach($connectto as $cnode) {
if ($cnode['Node']['rsakeypub'] != "" ) {
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/hosts/".$cnode['Node']['name']." <<EOF\n";
$pagecode .= "Compression=11\n";
$pagecode .= "Subnet=".$cnode['Node']['ip']."/".$cnode['Node']['bitmask']."\n";
$pagecode .= "Compression=10\n";
//$pagecode .= "Subnet=".$cnode['Node']['ip']."/".$cnode['Node']['bitmask']."\n";
if($cnode['Node']['address']) { $pagecode .= "Address=".$cnode['Node']['address']."\n"; }
$pagecode .= "\n";
$pagecode .= $cnode['Node']['rsakeypub']."\n";
Expand All @@ -61,16 +51,16 @@
}
if($allconfig == 1) {
$pagecode .= "cat > ".$tincpath.$node['Network']['name']."/hosts/".$node['Node']['name']." <<EOF\n";
$pagecode .= "Compression=11\n";
$pagecode .= "Subnet=".$node['Node']['ip']."/".$node['Node']['bitmask']."\n";
$pagecode .= "Compression=10\n";
//$pagecode .= "Subnet=".$node['Node']['ip']."/".$node['Node']['bitmask']."\n";
if($node['Node']['address']) {$pagecode .= "Address=".$node['Node']['address']."\n"; }
$pagecode .= "\n";
$pagecode .= $node['Node']['rsakeypub']."\n";
$pagecode .= "EOF\n";
}
$pagecode .= "\n# End Of Script.\n";
if (!$encrypt) {
echo "$pagecode";
echo $pagecode;
} else {
$RSAPATH = ROOT.DS.APP_DIR.DS."Exec".DS;
$EXECPATH = $RSAPATH."encrypt";
Expand Down

0 comments on commit 12d559d

Please sign in to comment.