Completed
Push — master ( a1987d...ef66da )
by Terrence
21:32 queued 06:26
created

MyProxy::getDefaultLifetime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CILogon\Service;
4
5
use CILogon\Service\Util;
6
7
define('MYPROXY_LOGON', '/usr/bin/myproxy-logon');
8
define('MYPROXY_HOST', 'myproxy.cilogon.org');
9
define('MYPROXY_PORT', '7512');
10
define('MYPROXY_LIFETIME', '12');
11
12
/**
13
 * MyProxy
14
 */
15
class MyProxy
16
{
17
    /**
18
     * getMyProxyCredential
19
     *
20
     * This function gets an X.509 credential (as a string) for a user.
21
     *
22
     * @param string $username The MyProxy user name (-l)
23
     * @param string $passphrase (Optional) The MyProxy password for the
24
     *        username (-S). Defaults to empty string.  NOTE: If $passphrase
25
     *        is non-empty, you CANNOT set a $certreq.
26
     * @param string $server (Optional) The MyProxy server to connect to (-s).
27
     *        Defaults to MYPROXY_HOST.
28
     * @param int $port (Optional) The port for the MyProxy server (-p).
29
     *        Defaults to MYPROXY_PORT.
30
     * @param int $lifetime (Optional) The life of the proxy in hours (-t).
31
     *        Defaults to MYPROXY_LIFETIME hours.
32
     * @param string $usercert (Optional) The X509_USER_CERT environment
33
     *        variable, OR the X509_USER_PROXY environment variable if
34
     *        $userkey is set to the empty string.  Defaults to empty string.
35
     * @param string $userkey (Optional) The X509_USER_KEY environment
36
     *        variable. Defaults to empty string.
37
     * @param string $certreq (Optional) A certificate request created by the
38
     *        openssl req command (--certreq).  Defaults to empty string.
39
     *        NOTE: If $certreq is non-empty, you CANNOT set a $passphrase.
40
     * @param string $env (Optional) Extra environment variables in the form
41
     *        of space-separated 'key=value' pairs.
42
     * @return string An X509 credential in a string upon success, or
43
     *         an empty string upon failure.
44
     */
45
    public static function getMyProxyCredential(
46
        $username,
47
        $passphrase = '',
48
        $server = MYPROXY_HOST,
49
        $port = MYPROXY_PORT,
50
        $lifetime = MYPROXY_LIFETIME,
51
        $usercert = '',
52
        $userkey = '',
53
        $certreq = '',
54
        $env = ''
55
    ) {
56
        $retstr = '';
57
58
        // Make sure the username passed in is not empty
59
        if (strlen($username) == 0) {
60
            Util::sendErrorAlert(
61
                'getMyProxyCredential Error',
62
                'MyProxy Error = Missing MyProxy username'
63
            );
64
            return $retstr;
65
        }
66
67
        // Don't allow weird port numbers, i.e. negative or over 65535
68
        if (($port < 0) || ($port > 65535)) {
69
            $port = MYPROXY_PORT;
70
        }
71
72
        // Don't allow weird lifetimes, i.e. negative or over 5 years
73
        if (($lifetime < 0) || ($lifetime > 43800)) {
74
            $lifetime = MYPROXY_LIFETIME;
75
        }
76
77
        // If the usercert (X509_USER_CERT) is specified, check to see if
78
        // the userkey (X509_USER_KEY) was as well.  If not, set userkey to
79
        // usercert, in effect making usercert act like X509_USER_PROXY. Then,
80
        // set the USER_CERT_ENV variable to bundle the two parameters into a
81
        // single variable holding the two X509_USER_* environment variables.
82
        $USER_CERT_ENV = '';
83
        if (strlen($usercert) > 0) {
84
            if (strlen($userkey) == 0) {
85
                $userkey = $usercert;
86
            }
87
            $USER_CERT_ENV = 'X509_USER_CERT=' . escapeshellarg($usercert) .
88
                             ' ' .
89
                             'X509_USER_KEY='  . escapeshellarg($userkey);
90
        }
91
92
        // Run the myproxy-logon command and capture the output and any error
93
        unset($output);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
94
        $cmd = '/bin/env ' .
95
               $USER_CERT_ENV . ' ' .
96
               $env . ' ' .
97
               'MYPROXY_SOCKET_TIMEOUT=1 ' .
98
               MYPROXY_LOGON . ' ' .
99
               ' -s ' . escapeshellarg($server) .
100
               " -p $port" .
101
               " -t $lifetime" .
102
               ' -l ' . escapeshellarg($username) .
103
               ' -S -o -' .
104
               ((strlen($certreq) > 0) ?
105
                   (' --certreq - <<< ' . escapeshellarg($certreq)) : '') .
106
               ((strlen($passphrase) > 0) ?
107
                   (' <<< ' . escapeshellarg($passphrase)) : ' -n') .
108
               ' 2>&1';
109
        exec($cmd, $output, $return_val);
110
        $retstr = implode("\n", $output);
111
112
        if ($return_val > 0) {
113
            Util::sendErrorAlert(
114
                'getMyProxyCredential Error',
115
                "MyProxy Error = $return_val\nMyProxy Output= $retstr"
116
            );
117
            $retstr = '';
118
        }
119
120
        return $retstr;
121
    }
122
123
    /**
124
     * getDefaultLifetime
125
     *
126
     * This function returns the value of the class defined
127
     * MYPROXY_LIFETIME as an int, which may be needed in '/secure/getuser'
128
     * when getting a certificate.
129
     *
130
     * @return int The value of MYPROXY_LIFETIME
131
     */
132
    public static function getDefaultLifetime()
133
    {
134
        return (int)MYPROXY_LIFETIME;
135
    }
136
}
137