-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuslims_certs.php
172 lines (139 loc) · 4.01 KB
/
uslims_certs.php
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
<?php
### user defines
$scigap_host = "api.scigap.org:9930";
$httpdconfigdir = "/etc/httpd/conf.d";
### end user defines
$self = __FILE__;
$notes = <<<__EOD
usage: $self {options} {db_config_file}
displays info about certificates
Options
--help : print this information and exit
--expiry : displays expiry info about certs
__EOD;
require "utility.php";
$u_argv = $argv;
array_shift( $u_argv ); # first element is program name
$anyargs = false;
$expiry = false;
while( count( $u_argv ) && substr( $u_argv[ 0 ], 0, 1 ) == "-" ) {
$anyargs = true;
switch( $arg = $u_argv[ 0 ] ) {
case "--help": {
echo $notes;
exit;
}
case "--expiry": {
array_shift( $u_argv );
$expiry = true;
break;
}
default:
error_exit( "\nUnknown option '$u_argv[0]'\n\n$notes" );
}
}
$config_file = "db_config.php";
if ( count( $u_argv ) ) {
$use_config_file = array_shift( $u_argv );
} else {
$use_config_file = $config_file;
}
if ( !$anyargs || count( $u_argv ) ) {
echo $notes;
exit;
}
if ( !file_exists( $use_config_file ) ) {
fwrite( STDERR, "$self:
$use_config_file does not exist
to fix:
cp ${config_file}.template $use_config_file
and edit with appropriate values
")
;
exit(-1);
}
function extractdates( $str ) {
$dates = [];
if ( preg_match( "/^notBefore=(.*)$/m", $str, $matches ) && count( $matches ) > 1 ) {
$dates[ "notbefore" ] = $matches[ 1 ];
} else {
$dates[ "notbefore" ] = "not found";
}
if ( preg_match( "/^notAfter=(.*)$/m", $str, $matches ) && count( $matches ) > 1 ) {
$dates[ "notafter" ] = $matches[ 1 ];
} else {
$dates[ "notafter" ] = "not found";
}
return $dates;
}
function timetoexpiry( $date ) {
if ( $date == "not found" ) {
return "unknown";
}
$remainder = dt_duration_minutes( dt_now(), new DateTime( $date ) );
$days = floor( $remainder / (60 * 24) );
$remainder -= $days * 60 * 24;
$hours = floor( $remainder / 60 );
$remainder -= $hours * 60;
return
sprintf(
"%4dd %2dh %2dm"
,$days
,$hours
,$remainder
);
}
if ( $expiry ) {
if ( !is_admin( false ) ) {
error_exit( "This option must be run by root or a sudo enabled user" );
}
## header
$breakline = echoline( '-', 40 + 3 + 25 + 3 + 25 + 3 + 15, false );
$fmt = "%-40s | %-25s | %-25s | %-15s\n";
$out =
$breakline
. sprintf(
$fmt
, 'certificate source'
, 'valid not before'
, 'valid not after'
, 'time to expiry'
)
. $breakline
;
## $scigap_host
$status = '';
$dates = extractdates( trim( run_cmd( "echo | openssl s_client -connect $scigap_host 2>&1 | openssl x509 -noout -dates 2>&1 | grep -P '^not'" ) ) );
$out .=
sprintf(
$fmt
, $scigap_host
, $dates[ "notbefore" ]
, $dates[ "notafter" ]
, timetoexpiry( $dates[ "notafter" ] )
);
## https
# check httpd configs
$httpd_sslcerts = explode( "\n", trim( run_cmd( "cd $httpdconfigdir && grep -P '^\s*SSLCertificateFile' *conf" ) ) );
foreach ( $httpd_sslcerts as $v ) {
if ( $cert = preg_split( "/(:|\s+)/", $v ) ) {
if ( count( $cert ) > 2 ) {
$csource = $cert[ 0 ];
$cfile = $cert[ 2 ];
$dates = extractdates( trim( run_cmd( "sudo openssl x509 --dates -noout -in $cfile" ) ) );
$out .=
sprintf(
$fmt
, $csource
, $dates[ "notbefore" ]
, $dates[ "notafter" ]
, timetoexpiry( $dates[ "notafter" ] )
);
}
}
}
## close up
$out .= $breakline;
echo $out;
exit;
}