Esempio n. 1
0
/**
 * Update remote authorize_keys file for the selected object.
 * @param $object_id (int) Object ID.
 * @param $newkeys (string) New keys to add.
 * @param $overwrite (boolean) If true removes any key not managed by RackMap.
 * @param $keep (boolean) If true keeps existing keys.
 * @return boolean true in case of success, false otherwise.
 */
function F_updateRemoteKeys($object_id = 0, $newkeys = '', $overwrite = false, $keep = false)
{
    global $l, $db;
    require_once '../config/tce_config.php';
    // get object data
    $objdata = F_get_object_data_array($object_id);
    // get FQDN
    if (isset($objdata['attribute']['FQDN']) and !empty($objdata['attribute']['FQDN'])) {
        // sanitize host name
        $host = preg_replace('/[^A-Za-z0-9\\.\\-]*/', '', $objdata['attribute']['FQDN']);
        // download the authorize_keys file on a temporary file
        $tmpfile = K_PATH_CACHE . 'sshkeys/authorized_keys.' . $host;
        $cmd = 'scp -q -o StrictHostKeyChecking=no root@' . $host . ':/root/.ssh/authorized_keys ' . $tmpfile;
        exec($cmd);
        // create a backup of original permission file
        $cmd = 'scp -q -o StrictHostKeyChecking=no ' . $tmpfile . ' root@' . $host . ':/root/.ssh/authorized_keys.BAK' . date('YmdHis');
        exec($cmd);
        // check if the file has been downloaded
        if (file_exists($tmpfile)) {
            // read the file
            $keys = file($tmpfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            $objkeys = $newkeys;
            // analyze each existing key
            foreach ($keys as $key) {
                if (preg_match('/[\\s]RACKMAP_STATIC$/', $key, $matches) > 0) {
                    // static keys are required by RackMap to access remote servers
                    $objkeys .= $key . K_NEWLINE;
                } elseif (preg_match('/[\\s]RACKMAP_MANAGED$/', $key, $matches) > 0) {
                    // existing keys managed by rackmap are removed (unless $keep is true)
                    if ($keep) {
                        $objkeys .= $key . K_NEWLINE;
                    }
                } elseif (!$overwrite) {
                    // keys unmanaged by RackMap are removed only in overwrite mode, otherwise are preserved
                    $objkeys .= $key . K_NEWLINE;
                }
            }
            // replace temp file with new keys
            if (file_put_contents($tmpfile, $objkeys) !== false) {
                // upload file
                $cmd = 'scp -q -o StrictHostKeyChecking=no ' . $tmpfile . ' root@' . $host . ':/root/.ssh/authorized_keys';
                exec($cmd);
                unlink($tmpfile);
                return true;
            }
        }
        // end if file exist
    }
    return false;
}
Esempio n. 2
0
/**
 * Get an HTML representation of objects on the selected rack.
 * @param $dcn_id (int) ID of selected datacenter.
 * @param $sts_id (int) ID of selected suite.
 * @param $rck_id (int) ID of selected rack.
 * @param $rack_data (array) Array of rack data objects.
 * @return string.
 */
function getRackStack($dcn_id, $sts_id, $rck_id, &$rack_data)
{
    global $l, $db;
    require_once '../config/tce_config.php';
    require_once 'tce_functions_group_permissions.php';
    $user_id = intval($_SESSION['session_user_id']);
    $dcn_id = intval($dcn_id);
    $sts_id = intval($sts_id);
    $rck_id = intval($rck_id);
    // get ID for 'guest' object type
    $guest_obt_id = F_getObjectTypeID('guest');
    $rck_permission = F_getUserPermission($user_id, K_TABLE_RACK_GROUPS, $rck_id);
    // get objects on rack
    $rackstack = array();
    $sql = 'SELECT * FROM ' . K_TABLE_OBJECTS . ', ' . K_TABLE_LOCATIONS . ' WHERE obj_id=loc_obj_id AND loc_rack_id=' . $rck_id . ' ORDER BY loc_row_top DESC, loc_front DESC, loc_center DESC, loc_rear DESC';
    if ($r = F_db_query($sql, $db)) {
        while ($m = F_db_fetch_assoc($r)) {
            $obj_permission = F_getUserPermission($user_id, K_TABLE_OBJECT_GROUPS, $m['obj_id']);
            if ($obj_permission > 0) {
                // get object position on the row
                $rpos = bindec(intval($m['loc_front']) . intval($m['loc_center']) . intval($m['loc_rear']));
                switch ($m['loc_side']) {
                    case 'left':
                    case 'right':
                        $rkey = $m['loc_side'] . '_' . $m['loc_row_top'];
                        if (F_getBoolean($m['loc_front'])) {
                            $rkey .= '_front';
                        } elseif (F_getBoolean($m['loc_rear'])) {
                            $rkey .= '_rear';
                        }
                        break;
                    case 'top':
                    case 'bottom':
                        $rkey = $m['loc_side'] . '_' . $m['loc_row_top'] . '_' . $rpos;
                        break;
                    case '-':
                    default:
                        $rkey = $m['loc_row_top'] . '_' . $rpos;
                        break;
                }
                $rackstack[$rkey] = $m;
                // get guest list
                $rackstack[$rkey]['guests'] = F_getGuestList($m['obj_id'], $guest_obt_id);
                // get capacity for each object on rack
                $capacity = array();
                F_get_object_data_array($m['obj_id'], $ilo, $capacity);
                if (isset($capacity) and !empty($capacity)) {
                    $rackstack[$rkey]['capacity'] = $capacity;
                }
            }
        }
    } else {
        F_display_db_error();
    }
    $rackobjs = '<table class="rack">' . K_NEWLINE;
    $rackobjs .= '<tr style="font-size:80%;"><th>#</th>';
    $rackobjs .= '<th title="' . $l['w_front'] . ' - ' . $l['w_left'] . '">FL</th><th title="' . $l['w_front'] . ' - ' . $l['w_right'] . '">FR</th>';
    $rackobjs .= '<th style="min-width:50px;">' . $l['w_front'] . '</th>';
    $rackobjs .= '<th style="min-width:50px;">' . $l['w_center'] . '</th>';
    $rackobjs .= '<th style="min-width:50px;">' . $l['w_rear'] . '</th>';
    $rackobjs .= '<th title="' . $l['w_rear'] . ' - ' . $l['w_left'] . '">RL</th><th title="' . $l['w_rear'] . ' - ' . $l['w_right'] . '">RR</th></tr>';
    $nextrow_front = $rack_data['rck_height'] + 1;
    $nextrow_center = $nextrow_front;
    $nextrow_rear = $nextrow_front;
    $nextrow_frontleft = $nextrow_front;
    $nextrow_frontright = $nextrow_front;
    $nextrow_rearleft = $nextrow_front;
    $nextrow_rearright = $nextrow_front;
    // initialize array to store the number of free slots
    $freeslots = array('3L' => 0, '2L' => 0, '1L' => 0);
    for ($pos = $rack_data['rck_height'] + 1; $pos >= 0; --$pos) {
        if ($pos > $rack_data['rck_height']) {
            // top
            $slidenum = $l['w_top'];
            $oidx = 'top_' . $pos;
        } elseif ($pos == 0) {
            // bottom
            $slidenum = $l['w_bottom'];
            $oidx = 'bottom_' . $pos;
        } else {
            $slidenum = $pos;
            $oidx = $pos;
        }
        $rackobjs .= '<tr>' . K_NEWLINE;
        $rackobjs .= '<td>' . $slidenum . '</td>';
        // object capacity
        $objcap = '';
        // LEFT-FRONT RACK SIDE
        if (isset($rackstack['left_' . $pos . '_front'])) {
            $oidx = 'left_' . $pos . '_front';
            if (isset($rackstack[$oidx]['capacity'])) {
                $objcap = F_getObjectCapacityString($rackstack[$oidx]['capacity']);
            }
            $lrowspan = 1 + $pos - $rackstack[$oidx]['loc_row_bottom'];
            $rackobjs .= '<td style="max-width:20px;" rowspan="' . $lrowspan . '" class="rackobject" id="robj_' . $rackstack[$oidx]['obj_id'] . '"><a class="vtext" href="tce_view_object.php?dcn_id=' . $dcn_id . '&amp;sts_id=' . $sts_id . '&amp;rck_id=' . $rck_id . '&amp;obj_id=' . $rackstack[$oidx]['obj_id'] . '#object" title="' . $l['w_show_details'] . '">' . $rackstack[$oidx]['obj_name'] . ' - ' . $rackstack[$oidx]['obj_label'] . ' - ' . $rackstack[$oidx]['obj_tag'] . $objcap . '</a></td>';
            $nextrow_frontleft = $rackstack[$oidx]['loc_row_bottom'] - 1;
        } elseif ($pos <= $nextrow_frontleft) {
            if ($rck_permission > 1) {
                $rackobjs .= '<td><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_side=left&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
            } else {
                $rackobjs .= '<td>&nbsp;</td>' . K_NEWLINE;
            }
        }
        // RIGHT-FRONT RACK SIDE
        if (isset($rackstack['right_' . $pos . '_front'])) {
            $oidx = 'right_' . $pos . '_front';
            if (isset($rackstack[$oidx]['capacity'])) {
                $objcap = F_getObjectCapacityString($rackstack[$oidx]['capacity']);
            }
            $lrowspan = 1 + $pos - $rackstack[$oidx]['loc_row_bottom'];
            $rackobjs .= '<td style="max-width:20px;" rowspan="' . $lrowspan . '" class="rackobject" id="robj_' . $rackstack[$oidx]['obj_id'] . '"><a class="vtext" href="tce_view_object.php?dcn_id=' . $dcn_id . '&amp;sts_id=' . $sts_id . '&amp;rck_id=' . $rck_id . '&amp;obj_id=' . $rackstack[$oidx]['obj_id'] . '#object" title="' . $l['w_show_details'] . '">' . $rackstack[$oidx]['obj_name'] . ' - ' . $rackstack[$oidx]['obj_label'] . ' - ' . $rackstack[$oidx]['obj_tag'] . $objcap . '</a></td>';
            $nextrow_frontright = $rackstack[$oidx]['loc_row_bottom'] - 1;
        } elseif ($pos <= $nextrow_frontright) {
            if ($rck_permission > 1) {
                $rackobjs .= '<td><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_side=right&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
            } else {
                $rackobjs .= '<td>&nbsp;</td>' . K_NEWLINE;
            }
        }
        // possible positions for single object on a rack row are:
        // 000 invalid
        // 001 1
        // 010 2
        // 011 3
        // 100 4
        // 101 invalid
        // 110 6
        // 111 7
        $rowpos = array(7, 6, 4, 3, 2, 1);
        $tmpfront = false;
        $tmpcenter = false;
        $tmprear = false;
        // for each possible position combinations
        foreach ($rowpos as $rp) {
            $oidxr = $oidx . '_' . $rp;
            if (isset($rackstack[$oidxr])) {
                if (isset($rackstack[$oidxr]['capacity'])) {
                    $objcap = F_getObjectCapacityString($rackstack[$oidxr]['capacity']);
                }
                // print object
                $rowspan = 1 + $pos - $rackstack[$oidxr]['loc_row_bottom'];
                // calculate object width (1, 2 or 3)
                $obj_cols = intval($rackstack[$oidxr]['loc_front']) + intval($rackstack[$oidxr]['loc_center']) + intval($rackstack[$oidxr]['loc_rear']);
                // print object
                $obj_cell = '<td rowspan="' . $rowspan . '" colspan="' . $obj_cols . '" class="rackobject" id="robj_' . $rackstack[$oidxr]['obj_id'] . '"><a href="tce_view_object.php?dcn_id=' . $dcn_id . '&amp;sts_id=' . $sts_id . '&amp;rck_id=' . $rck_id . '&amp;obj_id=' . $rackstack[$oidxr]['obj_id'] . '#object" title="' . $l['w_show_details'] . '">' . $rackstack[$oidxr]['obj_name'] . ' - ' . $rackstack[$oidxr]['obj_label'] . ' - ' . $rackstack[$oidxr]['obj_tag'] . $objcap . '</a></td>';
                $nextrow = $rackstack[$oidxr]['loc_row_bottom'] - 1;
                if ($rackstack[$oidxr]['loc_front']) {
                    $tmpfront = true;
                    $nextrow_front = $nextrow;
                }
                if ($rackstack[$oidxr]['loc_center']) {
                    $tmpcenter = true;
                    $nextrow_center = $nextrow;
                }
                if ($rackstack[$oidxr]['loc_rear']) {
                    $tmprear = true;
                    $nextrow_rear = $nextrow;
                }
                $empty_cols = 0;
                if ($rp < 4) {
                    if (!$tmpfront) {
                        if ($pos <= $nextrow_front) {
                            ++$empty_cols;
                        }
                    }
                    if ($rp < 2) {
                        if (!$tmpcenter) {
                            if ($pos <= $nextrow_center) {
                                ++$empty_cols;
                            }
                        }
                    }
                }
                // check for empty slots before this
                if ($empty_cols > 0) {
                    if ($rp != 1 or $empty_cols != 1) {
                        if ($rck_permission > 1) {
                            $rackobjs .= '<td colspan="' . $empty_cols . '"><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
                        } else {
                            $rackobjs .= '<td colspan="' . $empty_cols . '">&nbsp;</td>' . K_NEWLINE;
                        }
                        // count free slots
                        if ($pos > 0 and $pos <= $rack_data['rck_height']) {
                            $freeslots[$empty_cols . 'L'] += 1;
                        }
                    } else {
                        // you cannot add an object in the middle
                        $rackobjs .= '<td colspan="' . $empty_cols . '" style="background-color:#aaaaaa;">&nbsp;</td>' . K_NEWLINE;
                    }
                }
                // print object
                $rackobjs .= $obj_cell;
            }
        }
        // end for each position
        // print last empty rows
        $empty_cols = 0;
        if (!$tmprear) {
            if ($pos <= $nextrow_rear) {
                ++$empty_cols;
            }
            if (!$tmpcenter) {
                if ($pos <= $nextrow_center) {
                    ++$empty_cols;
                }
                if (!$tmpfront) {
                    if ($pos <= $nextrow_front) {
                        ++$empty_cols;
                    }
                }
            }
        }
        if ($empty_cols > 0) {
            if ($rck_permission > 1) {
                $rackobjs .= '<td colspan="' . $empty_cols . '"><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
            } else {
                $rackobjs .= '<td colspan="' . $empty_cols . '">&nbsp;</td>' . K_NEWLINE;
            }
            // count free slots
            if ($pos > 0 and $pos <= $rack_data['rck_height']) {
                $freeslots[$empty_cols . 'L'] += 1;
            }
        }
        // LEFT-REAR RACK SIDE
        if (isset($rackstack['left_' . $pos . '_rear'])) {
            $oidx = 'left_' . $pos . '_rear';
            if (isset($rackstack[$oidx]['capacity'])) {
                $objcap = F_getObjectCapacityString($rackstack[$oidx]['capacity']);
            }
            $lrowspan = 1 + $pos - $rackstack[$oidx]['loc_row_bottom'];
            $rackobjs .= '<td style="max-width:20px;" rowspan="' . $lrowspan . '" class="rackobject" id="robj_' . $rackstack[$oidx]['obj_id'] . '"><a class="vtext" href="tce_view_object.php?dcn_id=' . $dcn_id . '&amp;sts_id=' . $sts_id . '&amp;rck_id=' . $rck_id . '&amp;obj_id=' . $rackstack[$oidx]['obj_id'] . '#object" title="' . $l['w_show_details'] . '">' . $rackstack[$oidx]['obj_name'] . ' - ' . $rackstack[$oidx]['obj_label'] . ' - ' . $rackstack[$oidx]['obj_tag'] . $objcap . '</a></td>';
            $nextrow_rearleft = $rackstack[$oidx]['loc_row_bottom'] - 1;
        } elseif ($pos <= $nextrow_rearleft) {
            if ($rck_permission > 1) {
                $rackobjs .= '<td><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_side=left&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
            } else {
                $rackobjs .= '<td>&nbsp;</td>' . K_NEWLINE;
            }
        }
        // RIGHT-REAR RACK SIDE
        if (isset($rackstack['right_' . $pos . '_rear'])) {
            $oidx = 'right_' . $pos . '_rear';
            if (isset($rackstack[$oidx]['capacity'])) {
                $objcap = F_getObjectCapacityString($rackstack[$oidx]['capacity']);
            }
            $lrowspan = 1 + $pos - $rackstack[$oidx]['loc_row_bottom'];
            $rackobjs .= '<td style="max-width:20px;" rowspan="' . $lrowspan . '" class="rackobject" id="robj_' . $rackstack[$oidx]['obj_id'] . '"><a class="vtext" href="tce_view_object.php?dcn_id=' . $dcn_id . '&amp;sts_id=' . $sts_id . '&amp;rck_id=' . $rck_id . '&amp;obj_id=' . $rackstack[$oidx]['obj_id'] . '#object" title="' . $l['w_show_details'] . '">' . $rackstack[$oidx]['obj_name'] . ' - ' . $rackstack[$oidx]['obj_label'] . ' - ' . $rackstack[$oidx]['obj_tag'] . $objcap . '</a></td>';
            $nextrow_rearright = $rackstack[$oidx]['loc_row_bottom'] - 1;
        } elseif ($pos <= $nextrow_rearright) {
            if ($rck_permission > 1) {
                $rackobjs .= '<td><a href="tce_edit_objects.php?loc_rack_id=' . $rck_id . '&amp;loc_side=right&amp;loc_row_top=' . $pos . '&amp;loc_row_bottom=' . $pos . '" title="' . $l['t_object_editor'] . '">' . $l['w_new'] . '</a></td>' . K_NEWLINE;
            } else {
                $rackobjs .= '<td>&nbsp;</td>' . K_NEWLINE;
            }
        }
        $rackobjs .= '</tr>' . K_NEWLINE;
    }
    // store rack stack
    $rack_data['rackstack'] = $rackstack;
    // store free slots info
    $rack_data['free_slots'] = $freeslots;
    $rackobjs .= '<tr><td colspan="8" style="background-color:#ffffcc;color:#000000;text-align:right;">' . $l['t_free_1u_slots'] . ': 3L=' . $rack_data['free_slots']['3L'] . ', 2L=' . $rack_data['free_slots']['2L'] . ', 1L=' . $rack_data['free_slots']['1L'] . '</td></tr>' . K_NEWLINE;
    $rackobjs .= '</table>' . K_NEWLINE;
    return $rackobjs;
}
Esempio n. 3
0
         // rack data
         if ($m['rck_id'] != $prev_rck_id) {
             $rck_perm = F_getUserPermission($user_id, K_TABLE_RACK_GROUPS, $m['rck_id']);
             if (($dcn_perm & 1) > 0 and ($sts_perm & 1) > 0 and ($rck_perm & 1) > 0) {
                 $rck_value = $m;
                 $data['asset']['datacenter']['DCN' . $m['dcn_id']]['suite']['STS' . $m['sts_id']]['rack']['RCK' . $m['rck_id']] = array('rck_id' => $m['rck_id'], 'rck_sts_id' => $m['rck_sts_id'], 'rck_name' => $m['rck_name'], 'rck_description' => $m['rck_description'], 'rck_label' => $m['rck_label'], 'rck_tag' => $m['rck_tag'], 'rck_height' => $m['rck_height'], 'rck_position_x' => $m['rck_position_x'], 'rck_position_y' => $m['rck_position_y'], 'rck_table' => getRackStack($m['dcn_id'], $m['sts_id'], $m['rck_id'], $rck_value), 'rck_rackstack' => $rck_value['rackstack'], 'rck_free_slots' => $rck_value['free_slots'], 'object' => array());
                 if ($userlevel >= K_AUTH_ADMINISTRATOR) {
                     $data['asset']['datacenter']['DCN' . $m['dcn_id']]['suite']['STS' . $m['sts_id']]['rack']['RCK' . $m['rck_id']]['rck_permissions'] = F_getGroupsPermsDesc(F_getGroupsPermissions(K_TABLE_RACK_GROUPS, $m['rck_id']));
                 }
             }
             $prev_rck_id = $m['rck_id'];
         }
         $obj_perm = F_getUserPermission($user_id, K_TABLE_OBJECT_GROUPS, $m['obj_id']);
         if (($obj_perm & 1) > 0) {
             // object data
             $objdata = F_get_object_data_array($m['obj_id'], $ilo, $capacity);
             if (isset($capacity) and !empty($capacity)) {
                 $objdata['capacity'] = $capacity;
             }
             // get all groups IDs
             $perm_groups = array_merge($perm_groups, array_keys($objdata['permissions']));
             if ($userlevel >= K_AUTH_ADMINISTRATOR) {
                 $objdata['permissions'] = F_getGroupsPermsDesc($objdata['permissions']);
             }
             $data['asset']['datacenter']['DCN' . $m['dcn_id']]['suite']['STS' . $m['sts_id']]['rack']['RCK' . $m['rck_id']]['object']['OBJ' . $m['obj_id']] = $objdata;
         }
     }
 } else {
     F_display_db_error();
 }
 // filter data by removing unwanted fields