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
|
//+=============================================================================
//
// file : DbUtils.cpp
//
// description : C++ source for the DbUtils.
//
// project : TANGO Device Server
//
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango 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 3 of the License, or
// (at your option) any later version.
//
// Tango 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.
//
// You should have received a copy of the GNU General Public License
// along with Tango. If not, see <http://www.gnu.org/licenses/>.
//
//
// $Author$
//
// $Revision$
// $Date$
//
//=============================================================================
#include <tango/tango.h>
#include "TangoAccessControl.h"
#include "TangoAccessControlClass.h"
namespace TangoAccessControl_ns
{
//+------------------------------------------------------------------
/**
* Remove the Fully Qualify Domain Name for tango less than 5.2 compatibility
*/
//+------------------------------------------------------------------
std::string TangoAccessControl::removeFQDN(std::string s)
{
std::string::size_type pos = s.find('.');
if (pos == std::string::npos)
return s;
else
return s.substr(0, pos);
}
//+----------------------------------------------------------------------------
//
// method : TangoAccessControl::mysql_connection()
//
// description : Execute a SQL query , ignore the result.
//
//-----------------------------------------------------------------------------
void TangoAccessControl::mysql_connection()
{
// Initialise variables to default values
//--------------------------------------------
#ifndef HAVE_CONFIG_H
char *database = (char *)"tango";
#else
char *database = (char *)TANGO_DB_NAME;
#endif
const char *mysql_user = NULL;
const char *mysql_password = NULL;
const char *mysql_host = NULL;
const char *mysql_name = NULL;
unsigned int port_num = 0;
WARN_STREAM << "AccessControl::init_device() create database device " << device_name << std::endl;
// Initialise mysql database structure and connect to TANGO database
mysql_init(&mysql);
DummyDev d;
std::string my_user,my_password,my_host,my_name;
std::string ho,port;
if (d.get_env_var("MYSQL_USER",my_user) != -1)
{
mysql_user = my_user.c_str();
}
if (d.get_env_var("MYSQL_PASSWORD",my_password) != -1)
{
mysql_password = my_password.c_str();
}
if (d.get_env_var("MYSQL_HOST",my_host) != -1)
{
std::string::size_type pos = my_host.find(':');
if (pos != std::string::npos)
{
ho = my_host.substr(0,pos);
pos++;
port = my_host.substr(pos);
std::stringstream ss(port);
ss >> port_num;
if (!ss)
port_num = 0;
mysql_host = ho.c_str();
}
else
mysql_host = my_host.c_str();
}
if (d.get_env_var("MYSQL_DATABASE",my_name) != -1)
{
mysql_name = my_name.c_str();
}
if (mysql_name != NULL)
{
database = const_cast<char *>(mysql_name);
}
WARN_STREAM << "AccessControl::init_device() mysql database user = " << mysql_user
<< " , password = " << mysql_password << std::endl;
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
#if (MYSQL_VERSION_ID > 50000)
if(mysql_get_client_version() >= 50013)
{
my_bool my_auto_reconnect=1;
if (mysql_options(&mysql,MYSQL_OPT_RECONNECT,&my_auto_reconnect) !=0)
{
ERROR_STREAM << "AccessControl: error setting mysql auto reconnection: " << mysql_error(&mysql) << std::endl;
}
else
{
WARN_STREAM << "AccessControl: set mysql auto reconnect to true" << std::endl;
}
}
#endif
if (!mysql_real_connect(&mysql, mysql_host, mysql_user, mysql_password, database, port_num, NULL, 0))
{
TangoSys_MemStream out_stream;
out_stream << "Failed to connect to TANGO database (error = "
<< mysql_error(&mysql) << ")" << std::ends;
Tango::Except::throw_exception(
(const char *)"CANNOT_CONNECT_MYSQL",
out_stream.str(),
(const char *)"AccessControl::init_device()");
}
}
//+----------------------------------------------------------------------------
//
// method : TangoAccessControl::simple_query()
//
// description : Execute a SQL query , ignore the result.
//
//-----------------------------------------------------------------------------
void TangoAccessControl::simple_query(std::string sql_query,const char *method)
{
TangoSys_OMemStream o;
TangoSys_OMemStream o2;
if (mysql_real_query(&mysql, sql_query.c_str(),sql_query.length()) != 0)
{
WARN_STREAM << "TangoAccessControl::" << method << " failed to query TANGO database:" << std::endl;
WARN_STREAM << " query = " << sql_query << std::endl;
WARN_STREAM << " (SQL error=" << mysql_error(&mysql) << ")" << std::endl;
o << "Failed to query TANGO database (error=" << mysql_error(&mysql) << ")";
o2 << "TangoAccessControl::" << method;
Tango::Except::throw_exception((const char *)AC_SQLError,o.str(),o2.str());
}
}
//+----------------------------------------------------------------------------
//
// method : TangoAccessControl::query()
//
// description : Execute a SQL query and return the result.
//
//-----------------------------------------------------------------------------
MYSQL_RES *TangoAccessControl::query(std::string sql_query,const char *method) {
TangoSys_OMemStream o;
TangoSys_OMemStream o2;
MYSQL_RES *result;
if (mysql_real_query(&mysql, sql_query.c_str(),sql_query.length()) != 0)
{
WARN_STREAM << "TangoAccessControl::" << method << " failed to query TANGO database:" << std::endl;
WARN_STREAM << " query = " << sql_query << std::endl;
WARN_STREAM << " (SQL error=" << mysql_error(&mysql) << ")" << std::endl;
o << "Failed to query TANGO database (error=" << mysql_error(&mysql) << ")";
o2 << "TangoAccessControl::" << method;
Tango::Except::throw_exception((const char *)AC_SQLError,o.str(),o2.str());
}
if ((result = mysql_store_result(&mysql)) == NULL)
{
WARN_STREAM << "TangoAccessControl:: " << method << " : mysql_store_result() failed (error=" << mysql_error(&mysql) << ")" << std::endl;
o << "mysql_store_result() failed (error=" << mysql_error(&mysql) << ")";
o2 << "TangoAccessControl::" << method;
Tango::Except::throw_exception((const char *)AC_SQLError,o.str(),o2.str());
}
return result;
}
//============================================================
/**
* split device name in domain faily member in a std::vector.
*/
//============================================================
std::vector<std::string> TangoAccessControl::get_dev_members(std::string &devname)
{
std::vector<std::string> v;
std::string::size_type pos = devname.find('/');
std::string::size_type pos2 = devname.find('/', pos+1);
// domain
v.push_back(devname.substr(0, pos));
pos++;
// family
v.push_back(devname.substr(pos, pos2-pos));
pos2++;
// member
v.push_back(devname.substr(pos2, devname.length()-pos2));
return v;
}
//============================================================
/**
* split IP address in members in a std::vector.
*/
//============================================================
std::vector<std::string> TangoAccessControl::get_ip_add_members(std::string &devname)
{
std::vector<std::string> v;
std::string::size_type pos = devname.find('.');
std::string::size_type pos1 = devname.find('.', pos+1);
std::string::size_type pos2 = devname.find('.', pos1+1);
v.push_back(devname.substr(0, pos));
pos++;
v.push_back(devname.substr(pos, pos1-pos));
pos1++;
v.push_back(devname.substr(pos1, pos2-pos1));
pos2++;
v.push_back(devname.substr(pos2, devname.length()-pos2));
return v;
}
//============================================================
//============================================================
std::vector<AccessStruct>
TangoAccessControl::get_access_for_user_address(std::string &user, std::string &ip_add)
{
std::vector<std::string> v_add = get_ip_add_members(ip_add);
std::vector<AccessStruct> as_read;
TangoSys_MemStream sql_query_stream;
// First, check if something defined for user.
sql_query_stream << "SELECT count(*) FROM access_address WHERE user=\"" << user << "\"";
MYSQL_RES *res = query(sql_query_stream.str(), "ac_get_device_by_user()");
MYSQL_ROW ro = mysql_fetch_row(res);
sql_query_stream.str("");
sql_query_stream <<
"SELECT DISTINCT address FROM access_address WHERE ";
if ((ro[0])[0] != '0')
{
// Something found.
// User definition
sql_query_stream <<
"(user=\"" << user << "\") AND ";
}
else
{
// User definition
sql_query_stream <<
"(user=\"" << user << "\" OR user=\"*\") AND ";
}
// IP address definition
sql_query_stream <<
"(address=\"*.*.*.*\" OR " <<
"address=\"" << v_add[0] << ".*.*.*\" OR " <<
"address=\"" << v_add[0] << "." << v_add[1] << ".*.*\" OR " <<
"address=\"" << v_add[0] << "." << v_add[1] <<
"." << v_add[2] <<".*\" OR " <<
"address=\"" << ip_add << "\" ) ORDER BY address DESC";
//cout << "ac_get_access(): sql_query " << sql_query_stream.str() << std::endl;
MYSQL_RES *result = query(sql_query_stream.str(), "ac_get_device_by_user()");
int n_rows = mysql_num_rows(result);
if (n_rows > 0)
{
for (int i=0; i<n_rows; i++)
{
MYSQL_ROW row = mysql_fetch_row(result);
if (row != NULL)
{
AccessStruct acs;
acs.user = user;
acs.address = row[0];
as_read.push_back(acs);
}
}
}
mysql_free_result(result);
mysql_free_result(res);
return as_read;
}
//============================================================
//============================================================
std::string TangoAccessControl::get_access_for_user_device(std::string &user, std::string &device)
{
std::string deviceLower = device;
transform(deviceLower.begin(), deviceLower.end(), deviceLower.begin(), ::tolower);
std::vector<std::string> members = get_dev_members(deviceLower);
std::string retval("read");
TangoSys_MemStream sql_query_stream;
sql_query_stream <<
"SELECT DISTINCT user,device,rights FROM access_device WHERE " <<
"(user=\"" << user << "\" OR user=\"*\") ORDER BY device";
//cout << "ac_get_access(): sql_query " << sql_query_stream.str() << std::endl;
MYSQL_RES *result = query(sql_query_stream.str(), "ac_get_device_by_user()");
int n_rows = mysql_num_rows(result);
std::vector<AccessStruct> as_user;
std::vector<AccessStruct> as_all;
if (n_rows > 0)
{
for (int i=0; i<n_rows; i++)
{
MYSQL_ROW row = mysql_fetch_row(result);
if (row != NULL)
{
AccessStruct acs;
acs.user = row[0];
acs.device = row[1];
transform(acs.device.begin(), acs.device.end(), acs.device.begin(), ::tolower);
acs.rights = row[2];
/*
cout << " object[" << i << "} : " << acs.user
<< "|" << acs.device
<< "|" << acs.rights << std::endl;
*/
std::string tmp(row[0]);
if (tmp==user)
as_user.push_back(acs);
else
as_all.push_back(acs);
}
}
}
mysql_free_result(result);
// If both empty --> read
if (as_user.empty() && as_all.empty())
return retval;
// Get user rigths
std::string user_rights = get_rigths(as_user, members);
if (user_rights != "unknown") {
//cout << user << " " << user_rights << std::endl;
// if right has been matched -> return it
return user_rights;
}
else {
// Else return rights for all users
std::string all_rights = get_rigths(as_all, members);
if (all_rights != "unknown" && all_rights != "write") {
all_rights = "read";
}
return all_rights;
}
}
//============================================================
//============================================================
std::string TangoAccessControl::get_rigths(std::vector<AccessStruct> as, std::vector<std::string> members)
{
std::vector<AccessStruct> matches;
// Get list of structure matching device name
for (unsigned int i=0 ; i<as.size() ; i++) {
std::vector<std::string> expMembers = get_dev_members(as[i].device);
bool found = true;
for (unsigned int j=0 ; found && j<members.size() ; j++) {
found = match(expMembers[j], members[j]);
}
// If found for the 3 members, add to std::vector
if (found) {
matches.push_back(as[i]);
}
}
// Not found
if (matches.empty())
return "unknown";
// if only one, return this one.
if (matches.size()==1) {
return matches[0].rights;
}
else {
// Check if at least one is read only
for (unsigned int i=0 ; i<matches.size() ; i++) {
if (matches[i].rights=="read")
return "read";
}
return "write";
}
}
//============================================================
//============================================================
bool TangoAccessControl::match(std::string expression, std::string member)
{
if (expression=="*" || expression==member)
return true;
std::string::size_type pos = expression.find('*');
if (pos != std::string::npos) {
if (pos==0) {
// starts with '*'
pos++;
std::string s = expression.substr(pos);
return (member.find(s)!=std::string::npos);
}
else {
// get starting before '*'
std::string s1 = expression.substr(0, pos);
pos++;
// get ending after '*'
std::string s2 = expression.substr(pos);
if (s2.empty())
return (member.find(s1)==0);
else
return (member.find(s1)==0 && member.find(s2)==pos);
}
}
return false;
}
//============================================================
//============================================================
void TangoAccessControl::register_service(std::string servicename, std::string instname, std::string devname)
{
// Get service property
std::vector<std::string> services;
Tango::DbData data;
data.push_back(Tango::DbDatum(SERVICE_PROP_NAME));
Tango::Util *tg = Tango::Util::instance();
tg->get_database()->get_property(CONTROL_SYSTEM, data);
if (data[0].is_empty()==false) data[0] >> services;
// Build what to be searched
TangoSys_MemStream new_line;
new_line << servicename << "/" << instname;
std::string target(new_line.str());
transform(target.begin(), target.end(), target.begin(), ::tolower);
new_line << ":" << devname;
// Search if already exists
bool exists = false;
std::vector<std::string>::iterator pos = services.begin();
for (unsigned int i=0 ; i<services.size() ; i++, pos++)
{
std::string::size_type spos = services[i].find(':');
if (spos != std::string::npos) // found
{
std::string s = services[i].substr(0, spos);
transform(s.begin(), s.end(), s.begin(), ::tolower);
if (s==target)
{
// If exists --> replace
pos = services.erase(pos);
services.insert(pos, new_line.str());
exists = true;
}
}
}
// Else add it
if (!exists)
services.push_back(new_line.str());
data[0] << services;
tg->get_database()->put_property(CONTROL_SYSTEM, data);
}
//============================================================
//============================================================
void TangoAccessControl::unregister_service(std::string servicename, std::string instname, std::string devname)
{
// Get service property
std::vector<std::string> services;
Tango::DbData data;
data.push_back(Tango::DbDatum(SERVICE_PROP_NAME));
Tango::Util *tg = Tango::Util::instance();
tg->get_database()->get_property(CONTROL_SYSTEM, data);
if (data[0].is_empty()==false) data[0] >> services;
// Build what to be searched
TangoSys_MemStream line;
line << servicename << "/" << instname << ":" << devname;
std::string target(line.str());
transform(target.begin(), target.end(), target.begin(), ::tolower);
// Search if exists
std::vector<std::string>::iterator pos = services.begin();
for (unsigned int i=0 ; i<services.size() ; i++, pos++)
{
std::string s(services[i]);
transform(s.begin(), s.end(), s.begin(), ::tolower);
if (s==target)
{
// If exists --> remove
services.erase(pos);
}
}
data[0] << services;
tg->get_database()->put_property(CONTROL_SYSTEM, data);
}
} // namespace
|