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
|
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/../site/include/cli_check.php');
require_once($config['base_path'] . '/lib/api_automation_tools.php');
/* switch to main database for cli's */
if ($config['poller_id'] > 1) {
db_switch_remote_to_main();
}
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms) == 0) {
display_help();
exit(1);
} else {
$userId = 0;
/* TODO replace magic numbers by global constants, treat user_admin as well */
$itemTypes = array('graph' => 1, 'tree' => 2, 'host' => 3, 'graph_template' => 4);
$itemType = 0;
$itemId = 0;
$hostId = 0;
$quietMode = false;
$displayGroups = false;
$displayUsers = false;
$displayTrees = false;
$displayHosts = false;
$displayGraphs = false;
$displayGraphTemplates = false;
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '--user-id':
$userId = $value;
break;
case '--item-type':
/* TODO replace magic numbers by global constants, treat user_admin as well */
if ( ($value == 'graph') || ($value == 'tree') || ($value == 'host') || ($value == 'graph_template')) {
$itemType = $itemTypes[$value];
} else {
print "ERROR: Invalid Item Type: ($value)\n\n";
display_help();
exit(1);
}
break;
case '--item-id':
$itemId = $value;
break;
case '--host-id':
$hostId = $value;
break;
case '--list-groups':
$displayGroups = true;
break;
case '--list-users':
$displayUsers = true;
break;
case '--list-trees':
$displayTrees = true;
break;
case '--list-hosts':
$displayHosts = true;
break;
case '--list-graphs':
$displayGraphs = true;
break;
case '--list-graph-templates':
$displayGraphTemplates = true;
break;
case '--quiet':
$quietMode = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit(0);
case '--help':
case '-H':
case '-h':
display_help();
exit(0);
default:
print "ERROR: Invalid Argument: ($arg)\n\n";
display_help();
exit(1);
}
}
if ($displayGroups) {
displayGroups($quietMode);
exit(1);
}
if ($displayUsers) {
displayUsers($quietMode);
exit(1);
}
if ($displayTrees) {
displayTrees($quietMode);
exit(1);
}
if ($displayHosts) {
$hosts = getHosts();
displayHosts($hosts, $quietMode);
exit(1);
}
if ($displayGraphs) {
if (!isset($hostId) || ($hostId === 0) || (!db_fetch_cell("SELECT id FROM host WHERE id=$hostId"))) {
print "ERROR: You must supply a valid host_id before you can list its graphs\n";
print "Try --list-hosts\n";
display_help();
exit(1);
} else {
displayHostGraphs($hostId, $quietMode);
exit(1);
}
}
if ($displayGraphTemplates) {
$graphTemplates = getGraphTemplates();
displayGraphTemplates($graphTemplates, $quietMode);
exit(1);
}
/* verify, that a valid userid is provided */
$userIds = array();
if (isset($userId) && $userId > 0) {
/* verify existing user id */
if ( db_fetch_cell("SELECT id FROM user_auth WHERE id=$userId") ) {
array_push($userIds, $userId);
} else {
print "ERROR: Invalid Userid: ($value)\n\n";
display_help();
exit(1);
}
}
/* now, we should have at least one verified userid */
/* verify --item-id */
if ($itemType == 0) {
print "ERROR: --item-type missing. Please specify.\n\n";
display_help();
exit(1);
}
if ($itemId == 0) {
print "ERROR: --item-id missing. Please specify.\n\n";
display_help();
exit(1);
}
/* TODO replace magic numbers by global constants, treat user_admin as well */
switch ($itemType) {
case 1: /* graph */
if (!db_fetch_cell("SELECT local_graph_id FROM graph_templates_graph WHERE local_graph_id=$itemId") ) {
print "ERROR: Invalid Graph item id: ($itemId)\n\n";
display_help();
exit(1);
}
break;
case 2: /* tree */
if (!db_fetch_cell("SELECT id FROM graph_tree WHERE id=$itemId") ) {
print "ERROR: Invalid Tree item id: ($itemId)\n\n";
display_help();
exit(1);
}
break;
case 3: /* host */
if (!db_fetch_cell("SELECT id FROM host WHERE id=$itemId") ) {
print "ERROR: Invalid Host item id: ($itemId)\n\n";
display_help();
exit(1);
}
break;
case 4: /* graph_template */
if (!db_fetch_cell("SELECT id FROM graph_templates WHERE id=$itemId") ) {
print "ERROR: Invalid Graph Template item id: ($itemId)\n\n";
display_help();
exit(1);
}
break;
}
/* verified item-id */
foreach ($userIds as $id) {
db_execute("REPLACE INTO user_auth_perms (user_id, item_id, type) VALUES ($id, $itemId, $itemType)");
}
}
/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Add Permissions Utility, Version $version, " . COPYRIGHT_YEARS . "\n";
}
function display_help() {
display_version();
print "\nusage: add_perms.php [ --user-id=[ID] ]\n";
print " --item-type=[graph|tree|host|graph_template]\n";
print " --item-id [--quiet]\n\n";
print "Where item-id is the id of the object of type item-type\n\n";
print "List Options:\n";
print " --list-users\n";
print " --list-trees\n";
print " --list-graph-templates\n";
print " --list-graphs --host-id=[ID]\n";
}
function displayGroups() {
/**
* Todo implement
*/
print 'This option has not yet been implemented';
}
|