/usr/share/perl5/EBox/UsersSync/Master.pm is in zentyal-users 2.3.15+quantal1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package EBox::UsersSync::Master;
use strict;
use warnings;
# File containing password for master's web service (to register a new slave)
use constant MASTER_PASSWORDS_FILE => EBox::Config::conf() . 'users/master.htaccess';
# Dir containing certificates for this master
use constant SSL_DIR => EBox::Config::conf() . 'ssl/';
# Certificate of the authorized master
use constant MASTER_CERT => '/var/lib/zentyal/conf/users/master.cert';
use EBox::Exceptions::External;
use EBox::Util::Random;
use EBox::Sudo;
use EBox::SOAPClient;
use EBox::Gettext;
use URI::Escape;
use File::Slurp;
use EBox::UsersSync::Slave;
use Error qw(:try);
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
# Method: confSOAPService
#
# Configure SOAP service to allow Master and Slave queries
#
sub confSOAPService
{
my ($self) = @_;
my $confFile = EBox::Config::conf() . 'users/soap.conf';
my @params;
push (@params, passwords_file => MASTER_PASSWORDS_FILE);
EBox::Module::Base::writeConfFileNoCheck($confFile, 'users/soap.mas', \@params);
my $apache = EBox::Global->modInstance('apache');
$apache->addInclude($confFile);
$apache->addCA(MASTER_CERT) if (-f MASTER_CERT);
}
# SERVER METHODS
# Method: getCertificate
#
# Return Master certificate (to be used when connecting to the slave's SOAP)
#
sub getCertificate()
{
my ($self) = @_;
return read_file(SSL_DIR . 'ssl.cert');
}
# Method: setupMaster
#
# Setup master server to allow new slaves connections
#
sub setupMaster
{
my ($self, $pass) = @_;
defined $pass or
$pass = EBox::Util::Random::generate(15);
my $users = EBox::Global->getInstance()->modInstance('users');
my $table = $users->model('SlavePassword');
my $row = $table->row();
$row->elementByName('password')->setValue($pass);
$row->store();
EBox::Sudo::root(
'rm -f ' . MASTER_PASSWORDS_FILE,
'htpasswd -bc ' . MASTER_PASSWORDS_FILE . ' slave ' . $pass,
);
}
# Method: addSlave
#
# Register a new slave in this master
#
#
sub addSlave
{
my ($self, $host, $port, $cert) = @_;
my $users = EBox::Global->modInstance('users');
my $table = $users->model('Slaves');
EBox::info("Adding a new slave on $host:$port");
my $changed = $users->changed();
my $id = $table->addRow(host => $host, port => $port);
unless ($changed) {
# FIXME do this better when framework available
$users->_saveConfig();
$users->setAsChanged(0);
}
unless (-d EBox::UsersSync::Slave->SLAVES_CERTS_DIR) {
mkdir EBox::UsersSync::Slave->SLAVES_CERTS_DIR;
}
# save slave's cert
write_file(EBox::UsersSync::Slave->SLAVES_CERTS_DIR . $id, $cert);
$users->initialSlaveSync(new EBox::UsersSync::Slave($host, $port, $id));
# Regenerate slave connection password
$self->setupMaster();
}
# CLIENT METHODS
# Method: checkMaster
#
# Checks connection to a given master's SOAP
#
# Parameters:
#
# host - master hostname or ip
# port - master administration port
# password - password for slave connection
#
# Returns 1 if could connect and SOAP replied, 0 otherwise
#
sub checkMaster
{
my ($self, $host, $port, $password) = @_;
my $apache = EBox::Global->modInstance('apache');
$password = uri_escape($password);
local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
my $master = EBox::SOAPClient->instance(
name => 'urn:Users/Master',
proxy => "https://slave:$password\@$host:$port/master",
);
try {
$master->getDN();
} otherwise {
my $ex = shift;
$self->_analyzeException($ex);
};
}
# Method: isSlave
#
# Return 1 if already configured as slave
#
sub isSlave
{
my ($self) = @_;
return (-f MASTER_CERT);
}
# Method: setupSlave
#
# Configure users module as slave of a given master host
#
sub setupSlave
{
my ($self) = @_;
my $users = EBox::Global->modInstance('users');
my $master = $users->model('Master');
if ($users->master() eq 'zentyal') {
# return if already configured
return if ($self->isSlave());
my $host = $master->hostValue();
my $port = $master->portValue();
my $password = $master->passwordValue();
my $apache = EBox::Global->modInstance('apache');
$password = uri_escape($password);
my $client = EBox::SOAPClient->instance(
name => 'urn:Users/Master',
proxy => "https://slave:$password\@$host:$port/master",
);
# Recreate LDAP for the master DN
$self->_recreateLDAP($users, $client);
# get master's certificate
my $cert = $client->getCertificate();
my $client_cert = read_file(SSL_DIR . 'ssl.cert');
try {
$client->registerSlave($apache->port(), $client_cert, 1);
} otherwise {
my $ex = shift;
$self->_analyzeException($ex);
};
# Write master certificate
# (after registering slave, this means everything went well)
write_file(MASTER_CERT, $cert);
}
else {
# return if already disabled
return unless ($self->isSlave());
# disable master access
unlink (MASTER_CERT);
}
}
sub _recreateLDAP
{
my ($self, $users, $client) = @_;
my $dn = $client->getDN();
my $row = $users->model('Mode')->row();
$row->elementByName('dn')->setValue($dn);
$row->store();
# Enable actions (without slave setup to avoid recursion)
$users->enableActions();
# TODO: reenable all LDAP modules
}
sub _analyzeException
{
my ($self, $ex) = @_;
my $msg = $ex->text();
if ($msg =~ m/^401/) {
$msg = __('Invalid password');
}
elsif ($msg =~ m/^500/) {
$msg = __('Connection failed, check host, port and firewall settings');
}
throw EBox::Exceptions::External(__("Couldn't configure Zentyal as slave") . ": $msg.");
}
1;
|