| 1 | # Net::IPA.pm -- Perl 5 interface of the (Free)IPA JSON-RPC API | ||
| 2 | # | ||
| 3 | # for more information about this api see: https://vda.li/en/posts/2015/05/28/talking-to-freeipa-api-with-sessions/ | ||
| 4 | # | ||
| 5 | # written by Nicolas Cisco (https://github.com/nickcis) | ||
| 6 | # https://github.com/nickcis/perl-Net-IPA | ||
| 7 | # | ||
| 8 | # Copyright (c) 2016 Nicolas Cisco. All rights reserved. | ||
| 9 | # Licensed under the GPLv3, see LICENSE.md file for more information. | ||
| 10 | |||
| 11 | package Net::IPA; | ||
| 12 | |||
| 13 | our $VERSION = '1.0'; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Net::IPA.pm -- Perl 5 interface of the (Free)IPA JSON-RPC API | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Net::IPA; | ||
| 22 | |||
| 23 | my $ipa = new Net::IPA( | ||
| 24 | hostname => 'ipa.server.com', | ||
| 25 | cacert => '/etc/ipa/ca.cert', | ||
| 26 | ); | ||
| 27 | |||
| 28 | # (a) Login can be done via Kerberos | ||
| 29 | $ipa->login(); | ||
| 30 | |||
| 31 | # (b) Login can be done via Kerberos (creating the ticket) | ||
| 32 | $ipa->login( | ||
| 33 | username => 'admin', | ||
| 34 | keytab => '/etc/ipa/admin.keytab' | ||
| 35 | ); | ||
| 36 | |||
| 37 | # (c) Login can be done using username and password | ||
| 38 | $ipa->login( | ||
| 39 | username => 'admin', | ||
| 40 | password => 'admin-password' | ||
| 41 | ); | ||
| 42 | |||
| 43 | # Control error | ||
| 44 | die $ipa->error if($ipa->error); | ||
| 45 | |||
| 46 | # $user_show is of the type Net::IPA::Response | ||
| 47 | my $user_show = $ipa->user_show('username'); | ||
| 48 | die 'Error: ' . $user_show->error_string() if($user_show->is_error); | ||
| 49 | |||
| 50 | # Requests can be batched | ||
| 51 | use Net::IPA::Methods; | ||
| 52 | my @users_show = $ipa->batch( | ||
| 53 | Net::IPA::Methods::user_show('username1'), | ||
| 54 | Net::IPA::Methods::user_show('username2'), | ||
| 55 | Net::IPA::Methods::user_show('username3'), | ||
| 56 | ); | ||
| 57 | |||
| 58 | foreach my $user_show (@users_show){ | ||
| 59 | # $user_show is of the type Net::IPA::Response | ||
| 60 | if($user_show->is_error){ | ||
| 61 | print 'Error: ' . $user_show->error_string() . "\n"; | ||
| 62 | next; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Do something | ||
| 66 | } | ||
| 67 | |||
| 68 | For methods look at the L<Net::IPA::Methods> module. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 73 | 0 | ▶ | use Net::IPA::Methods; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 74 | use Net::IPA::Response; | ||
| 75 | |||
| 76 | use vars qw($AUTOLOAD); | ||
| 77 | use Carp; | ||
| 78 | |||
| 79 | use JSON; | ||
| 80 | use LWP::UserAgent; # http://search.cpan.org/~ether/libwww-perl-6.15/lib/LWP/UserAgent.pm | ||
| 81 | use LWP::Authen::Negotiate; # http://search.cpan.org/~agrolms/LWP-Authen-Negotiate-0.06/lib/LWP/Authen/Negotiate.pm | ||
| 82 | use HTTP::Cookies; | ||
| 83 | use HTTP::Request; | ||
| 84 | use HTTP::Headers; | ||
| 85 | use File::Spec::Functions qw(catdir); | ||
| 86 | use Authen::Krb5::Easy qw(kcheck kerror); # https://github.com/nickcis/perl-Authen-Krb5-Easy | ||
| 87 | |||
| 88 | use constant { | ||
| 89 | AGENT => 'Perl / IPA', | ||
| 90 | CACERT => "/etc/ipa/ca.crt", | ||
| 91 | URL_TEMPLATE => '{protocol}://{hostname}{endpoint}', | ||
| 92 | BASEPAGE => '/ipa', | ||
| 93 | PROTOCOL => 'https', | ||
| 94 | ROUTE_LOGIN_KERBEROS => "/session/login_kerberos", | ||
| 95 | ROUTE_LOGIN_PASSWORD => "/session/login_password", | ||
| 96 | ROUTE_JSON => "/session/json", | ||
| 97 | IPA_CLIENT_VERSION => "2.156", | ||
| 98 | COOKIE_NAME => 'ipa_session', | ||
| 99 | }; | ||
| 100 | |||
| 101 | sub new | ||
| 102 | { | ||
| 103 | my $proto = shift; | ||
| 104 | my $class = ref($proto) || $proto; | ||
| 105 | my %args = @_; | ||
| 106 | my $self = { | ||
| 107 | _hostname => $args{hostname} || "localhost", | ||
| 108 | _cacert => $args{cacert} || CACERT, | ||
| 109 | _protocol => $args{protocol} || PROTOCOL, | ||
| 110 | _basepage => $args{basepage} || BASEPAGE, | ||
| 111 | _url_template => $args{url_template} || URL_TEMPLATE, | ||
| 112 | _agent => $args{agent} || AGENT, | ||
| 113 | _ua => $args{ua} || undef, | ||
| 114 | _cookie_name => $args{cookie_name} || COOKIE_NAME, | ||
| 115 | _cookie_jar => $args{cookie_jar} || undef, | ||
| 116 | _debug => $args{debug} || undef, | ||
| 117 | _error => undef, | ||
| 118 | _version => $args{version} || IPA_CLIENT_VERSION, | ||
| 119 | _reconnect => exists $args{reconnect} ? $args{reconnect} : 1, | ||
| 120 | _login_args => {}, | ||
| 121 | _expire_time => 0, | ||
| 122 | }; | ||
| 123 | |||
| 124 | bless ($self, $class); | ||
| 125 | $self->_init_ua() unless($self->{_ua}); | ||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub version | ||
| 130 | { | ||
| 131 | my ($self, $version) = @_; | ||
| 132 | $self->{_version} = $version if($version); | ||
| 133 | return $self->{_version}; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _debug | ||
| 137 | { | ||
| 138 | my ($self, $msg) = @_; | ||
| 139 | return unless($self->{_debug}); | ||
| 140 | return $self->{_debug}->($msg) if(ref($self->{_debug}) eq "CODE"); | ||
| 141 | print $msg . "\n"; | ||
| 142 | } | ||
| 143 | |||
| 144 | #** @function _init_ua private | ||
| 145 | # Inicializa LWP::UserAgent para hacer requests | ||
| 146 | #* | ||
| 147 | sub _init_ua | ||
| 148 | { | ||
| 149 | my ($self) = @_; | ||
| 150 | |||
| 151 | $self->{_cookie_jar} = HTTP::Cookies->new unless($self->{_cookie_jar}); | ||
| 152 | |||
| 153 | $self->{_ua} = LWP::UserAgent->new( | ||
| 154 | agent => $self->{_agent}, | ||
| 155 | cookie_jar => $self->{_cookie_jar}, | ||
| 156 | ); | ||
| 157 | |||
| 158 | $self->{_ua}->default_header( | ||
| 159 | referer => $self->_build_url() | ||
| 160 | ); | ||
| 161 | |||
| 162 | $self->{_ua}->ssl_opts( | ||
| 163 | SSL_ca_file => $self->{_cacert} | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | |||
| 167 | #** Crea una url para el endpoint especificado | ||
| 168 | # @params Endpoint | ||
| 169 | # @return Devuelve el url | ||
| 170 | #* | ||
| 171 | sub _build_url | ||
| 172 | { | ||
| 173 | my $self = shift; | ||
| 174 | my $url = $self->{_url_template}; | ||
| 175 | my $endpoint = catdir($self->{_basepage}, @_); | ||
| 176 | |||
| 177 | $url =~ s/{protocol}/$self->{_protocol}/g; | ||
| 178 | $url =~ s/{hostname}/$self->{_hostname}/g; | ||
| 179 | $url =~ s/{endpoint}/$endpoint/g; | ||
| 180 | |||
| 181 | $self->_debug("url: $url"); | ||
| 182 | return $url; | ||
| 183 | } | ||
| 184 | |||
| 185 | #** Performs IPA autentification. | ||
| 186 | # The autentification can be by providing username and password or through kerberos. | ||
| 187 | # In order to autentificate throught kerberos the ticket must already be created, | ||
| 188 | # (i.e: `kinit -k -t <keytab>`) or the param keytab has to be provided. | ||
| 189 | # | ||
| 190 | # If username and password are provided, autentification is done by username and password, if not, | ||
| 191 | # autentification is done through kerberos. Kinit is called if a keytab is provided. | ||
| 192 | # | ||
| 193 | # @params ( | ||
| 194 | # username => [mandatory] IPA username | ||
| 195 | # password => (optional) IPA password | ||
| 196 | # keytab => (optional) Absolute path to the keytab file | ||
| 197 | # ) | ||
| 198 | # | ||
| 199 | # @return 0: Ok, != 0: Error | ||
| 200 | #* | ||
| 201 | sub login | ||
| 202 | { | ||
| 203 | my ($self, %args) = @_; | ||
| 204 | $self->{_login_args} = \%args if($self->{_reconnect}); | ||
| 205 | my $http_headers = $self->{_ua}->default_headers()->clone; | ||
| 206 | my $url; | ||
| 207 | my $data = undef; | ||
| 208 | |||
| 209 | if($args{username} && $args{password}){ | ||
| 210 | $http_headers->header( | ||
| 211 | 'Content-Type' => "application/x-www-form-urlencoded", | ||
| 212 | Accept => "text/plain", | ||
| 213 | ); | ||
| 214 | |||
| 215 | $url = $self->_build_url(ROUTE_LOGIN_PASSWORD); | ||
| 216 | $data = "user=". $args{username} . "&password=" . $args{password}; | ||
| 217 | }else{ | ||
| 218 | if($args{keytab} && $args{username}) { | ||
| 219 | unless(kcheck($args{keytab}, $args{username})){ | ||
| 220 | $self->{_error} = kerror(); | ||
| 221 | return -1; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | $url = $self->_build_url(ROUTE_LOGIN_KERBEROS); | ||
| 225 | } | ||
| 226 | |||
| 227 | my $request = HTTP::Request->new('POST', $url, $http_headers, $data); | ||
| 228 | my $response = $self->{_ua}->request($request); | ||
| 229 | $self->_debug($response->as_string); | ||
| 230 | unless($response->is_success){ | ||
| 231 | $self->{_error} = 'Login Failed :: HTTP Code: ' . $response->code . ' ('. $response->message(). ')'; | ||
| 232 | return 0; | ||
| 233 | } | ||
| 234 | |||
| 235 | $self->_scan_cookies(); | ||
| 236 | |||
| 237 | return 1; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub _scan_cookies | ||
| 241 | { | ||
| 242 | my ($self) = @_; | ||
| 243 | $self->{_cookie_jar}->scan(sub { | ||
| 244 | my ($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $hash) = @_; | ||
| 245 | return unless($key eq $self->{_cookie_name}); | ||
| 246 | $self->{_expire_time} = $expires; | ||
| 247 | }); | ||
| 248 | } | ||
| 249 | |||
| 250 | #** Check current error status. | ||
| 251 | # Lo que devuelva esta funcion se evaluara como verdadero cuando se haya producido algun error | ||
| 252 | # @return False (undef): No error. If there is an error, this method will return a string describing the error. | ||
| 253 | #* | ||
| 254 | sub error | ||
| 255 | { | ||
| 256 | my ($self) = @_; | ||
| 257 | $self->{_error} = $_[1] if(1 > scalar @_); | ||
| 258 | return $self->{_error}; | ||
| 259 | } | ||
| 260 | |||
| 261 | #** Performs a request to the JSON api. | ||
| 262 | # @params $method: Ipa method name | ||
| 263 | # @params $args: (Array ref) Ipa parameters | ||
| 264 | # @params $_kargs (Hash ref) Ipa named parameters | ||
| 265 | # @return Net::IPA::Response | ||
| 266 | #* | ||
| 267 | sub request | ||
| 268 | { | ||
| 269 | my ($self, $method, $args, $_kargs) = @_; | ||
| 270 | |||
| 271 | return $self->_request({ | ||
| 272 | #id => 0, | ||
| 273 | method => $method, | ||
| 274 | params => [ $args, $_kargs ], | ||
| 275 | }); | ||
| 276 | } | ||
| 277 | |||
| 278 | #** [private] Internal method for perfoming api requests. | ||
| 279 | # @params $_options: (Hash ref) { method => , params => [ args, kargs ] } | ||
| 280 | # @return Net::IPA::Response | ||
| 281 | #* | ||
| 282 | sub _request | ||
| 283 | { | ||
| 284 | my ($self, $_options) = @_; | ||
| 285 | |||
| 286 | $self->{_error} = undef; | ||
| 287 | $self->login(%{$self->{_login_args}}) if($self->{_reconnect} && time() > $self->{_expire_time}); | ||
| 288 | |||
| 289 | my $url = $self->_build_url(ROUTE_JSON); | ||
| 290 | my %options = %$_options; | ||
| 291 | $options{params}->[1]->{version} = $self->{_version} if($self->{_version} and not(exists $options{params}->[1]->{version})); | ||
| 292 | |||
| 293 | my $data = to_json(\%options); | ||
| 294 | |||
| 295 | my $http_headers = $self->{_ua}->default_headers()->clone; | ||
| 296 | $http_headers->header( | ||
| 297 | 'Content-Type' => "application/json", | ||
| 298 | Accept => "application/json", | ||
| 299 | ); | ||
| 300 | my $request = HTTP::Request->new('POST', $url, $http_headers, $data); | ||
| 301 | my $response = $self->{_ua}->request($request); | ||
| 302 | |||
| 303 | $self->_debug($data); | ||
| 304 | $self->_debug($response->decoded_content); | ||
| 305 | |||
| 306 | $self->_scan_cookies(); | ||
| 307 | |||
| 308 | return new Net::IPA::Response({ | ||
| 309 | error => { | ||
| 310 | code => -1, | ||
| 311 | name => 'HttpError', | ||
| 312 | message => 'Code: ' . $response->code . ' (' . $response->message() . ')' | ||
| 313 | } | ||
| 314 | }) unless($response->is_success); | ||
| 315 | return new Net::IPA::Response(from_json($response->decoded_content)); | ||
| 316 | } | ||
| 317 | |||
| 318 | #** AUTOLOAD is used to implement the methods of Net::IPA::Methods. | ||
| 319 | # This allows the programmer to call: | ||
| 320 | # $ipa->user_add( ... ); | ||
| 321 | # | ||
| 322 | # Instead of: | ||
| 323 | # $ipa->method('user_add', ... ); | ||
| 324 | # | ||
| 325 | # In order to see all available method check the Net::IPA::Methods module. | ||
| 326 | #* | ||
| 327 | sub AUTOLOAD | ||
| 328 | { | ||
| 329 | my $sub = $AUTOLOAD; | ||
| 330 | (my $name = $sub) =~ s/.*:://; | ||
| 331 | my $method = Net::IPA::Methods->can($name); | ||
| 332 | if($method){ | ||
| 333 | my $self = shift; | ||
| 334 | my $ret = $method->(@_); | ||
| 335 | return $self->_request($ret); | ||
| 336 | } | ||
| 337 | |||
| 338 | croak "Can't locate object method \"$name\" via package \"Net::IPA\""; | ||
| 339 | } | ||
| 340 | |||
| 341 | #** Performs batch requests. | ||
| 342 | # Batch requests are done in order to perform many IPA api requests | ||
| 343 | # in only one http request. | ||
| 344 | # @param @batch: All IPA requests (must be created with Net::IPA::Method:* functions) | ||
| 345 | # @return array of all Net::IPA::Response of the batched actions. | ||
| 346 | #* | ||
| 347 | sub batch | ||
| 348 | { | ||
| 349 | my ($self, @batch) = @_; | ||
| 350 | my $response = $self->request( | ||
| 351 | 'batch', | ||
| 352 | \@batch, | ||
| 353 | {} | ||
| 354 | ); | ||
| 355 | |||
| 356 | my @ret; | ||
| 357 | if($response->is_error || ref($response->{result}->{results}) ne 'ARRAY'){ | ||
| 358 | push @ret, $response; | ||
| 359 | }else{ | ||
| 360 | foreach my $r (@{$response->{result}->{results}}){ | ||
| 361 | push @ret, new Net::IPA::Response($r); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | return wantarray ? @ret : \@ret; | ||
| 365 | } | ||
| 366 | |||
| 367 | 1; | ||