| 1 | package cPanel::APIClient::Transport::CLISync; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | cPanel::APIClient::Transport::CLISync | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | Call cPanel as an unprivileged user: | ||
| 15 | |||
| 16 | my $cp = cPanel::APIClient->create( | ||
| 17 | service = 'cpanel', | ||
| 18 | transport => ['CLISync'], | ||
| 19 | ); | ||
| 20 | |||
| 21 | Call cPanel for C<bob> as root: | ||
| 22 | |||
| 23 | my $cp = cPanel::APIClient->create( | ||
| 24 | service = 'cpanel', | ||
| 25 | transport => ['CLISync'], | ||
| 26 | credentials => { username => 'bob' }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | Call WHM as root: | ||
| 30 | |||
| 31 | my $cp = cPanel::APIClient->create( | ||
| 32 | service = 'whm', | ||
| 33 | transport => ['CLISync'], | ||
| 34 | ); | ||
| 35 | |||
| 36 | Call WHM for reseller C<sue> as root: | ||
| 37 | |||
| 38 | my $cp = cPanel::APIClient->create( | ||
| 39 | service = 'whm', | ||
| 40 | transport => ['CLISync'], | ||
| 41 | credentials => { username => 'sue' }, | ||
| 42 | ); | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | This module implements synchronous local transport (via cPanel & WHM’s | ||
| 47 | CLI API commands) for API requests. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | #---------------------------------------------------------------------- | ||
| 52 | |||
| 53 | use parent qw( cPanel::APIClient::Transport ); | ||
| 54 | |||
| 55 | use cPanel::APIClient::X (); | ||
| 56 | |||
| 57 | use IPC::Run (); | ||
| 58 | |||
| 59 | #---------------------------------------------------------------------- | ||
| 60 | |||
| 61 | sub NEEDS_CREDENTIALS { 0 } | ||
| 62 | |||
| 63 | our $_DEFAULT_TIMEOUT = 60; | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | my ( $class, $authn, %opts ) = @_; | ||
| 67 | |||
| 68 | if ( $authn && !$authn->isa('cPanel::APIClient::Authn::Username') ) { | ||
| 69 | die( __PACKAGE__ . ": Only username authentication is allowed!" ); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $svcname = $opts{'service_name'}; | ||
| 73 | if ( $svcname ne 'whm' && !$authn && _is_admin() ) { | ||
| 74 | die "$svcname requires a username when accessed locally as administrator!$/"; | ||
| 75 | } | ||
| 76 | |||
| 77 | return bless { | ||
| 78 | authn => $authn, | ||
| 79 | timeout => $opts{'timeout'} || $_DEFAULT_TIMEOUT, | ||
| 80 | }, $class; | ||
| 81 | } | ||
| 82 | |||
| 83 | # Does this work on non-UNIX platforms? | ||
| 84 | sub _is_admin { | ||
| 85 | return !$>; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub request { | ||
| 89 | my ( $self, $service_obj, $request_obj ) = @_; | ||
| 90 | |||
| 91 | my @cmd = $request_obj->get_cli_command( $self->{'authn'} ); | ||
| 92 | |||
| 93 | my $out = q<>; | ||
| 94 | |||
| 95 | IPC::Run::run( | ||
| 96 | \@cmd, | ||
| 97 | \q<>, | ||
| 98 | \$out, | ||
| 99 | \*STDERR, | ||
| 100 | IPC::Run::timeout( $self->{'timeout'} ), | ||
| 101 | ) or die cPanel::APIClient::X->create( 'CommandFailed', \@cmd, $? ); | ||
| 102 | |||
| 103 | return $request_obj->parse_cli_response($out); | ||
| 104 | } | ||
| 105 | |||
| 106 | =head1 LICENSE | ||
| 107 | |||
| 108 | Copyright 2020 cPanel, L. L. C. All rights reserved. L<http://cpanel.net> | ||
| 109 | |||
| 110 | This is free software; you can redistribute it and/or modify it under the | ||
| 111 | same terms as Perl itself. See L<perlartistic>. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | 1; |