1 /******************************************************************************
6 * Created: Oct 09 2010 19:14:12
7 * Last modified: Oct 09 2010 19:14:12
9 * Author: Ladislav Láska
10 * e-mail: ladislav.laska@gmail.com
12 ******************************************************************************/
14 #define _XOPEN_SOURCE 500
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
28 #define S_UNREACHABLE -1
35 int warning_after = 30;
41 #define die(msg) { fprintf(stderr, "Error: " msg "\n" ); exit(3); }
42 #define gnutls_die(code) { gnutls_perror(code); exit(3); }
48 int tcp_open( char *hostname, char *service ) {
49 struct addrinfo hints;
50 struct addrinfo *result, *result_ptr;
54 memset(&hints, 0, sizeof(struct addrinfo));
55 hints.ai_family = AF_UNSPEC;
56 hints.ai_socktype = SOCK_STREAM;
57 err = getaddrinfo(hostname, service, &hints, &result);
59 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
63 for (result_ptr = result; result_ptr != NULL; result_ptr = result_ptr->ai_next) {
64 sfd = socket(result_ptr->ai_family, result_ptr->ai_socktype,
65 result_ptr->ai_protocol);
66 if (sfd == -1) continue;
68 if (connect(sfd, result_ptr->ai_addr, result_ptr->ai_addrlen) != -1)
71 close(sfd); /* connect(2) failed. */
74 if (result_ptr == NULL) {
75 /* No address succeeded */
84 int check( char * hostname, char *service ) {
88 gnutls_session_t session;
89 gnutls_certificate_credentials_t xcred;
93 err = gnutls_certificate_allocate_credentials( &xcred );
94 if (err < 0) gnutls_die(err);
96 err = gnutls_init( &session, GNUTLS_CLIENT );
97 if (err < 0) gnutls_die(err);
100 err = gnutls_priority_set_direct(session, "EXPORT", NULL);
101 if (err < 0) gnutls_die(err);
103 err = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE, xcred );
104 if (err < 0) gnutls_die(err);
106 /* Connect to server */
108 long fd = tcp_open( hostname, service );
111 state= S_UNREACHABLE;
115 /* Socket opened, establish tls connection */
117 /* Associate socket with session */
118 gnutls_transport_set_ptr( session, (gnutls_transport_ptr_t) fd );
121 err = gnutls_handshake( session );
122 if (err < 0) gnutls_die(err);
124 /* Get server certificate. */
125 const gnutls_datum_t *cert_list;
126 unsigned int cert_list_size = 0;
127 time_t expiration_time, today;
128 gnutls_x509_crt_t cert;
130 if ( gnutls_certificate_type_get( session ) != GNUTLS_CRT_X509 ) {
135 cert_list = gnutls_certificate_get_peers( session, &cert_list_size );
139 for (int i = 0; i < cert_list_size; i++) {
140 gnutls_x509_crt_init( &cert );
141 gnutls_x509_crt_import( cert, &cert_list[0], GNUTLS_X509_FMT_DER );
142 expiration_time = gnutls_x509_crt_get_expiration_time( cert );
143 int expires_in = (expiration_time - today) / 86400;
144 struct tm * t = gmtime( &expiration_time );
145 if ((state == S_OK) && (expires_in <= warning_after)) {
147 sprintf(errmsg, "Warning - Will expire in %i days (%i-%02i-%02i).", expires_in,
148 t->tm_year+1900, t->tm_mon+1, t->tm_mday );
150 if ((state <= S_WARNING) && (expires_in <= error_after)) {
152 sprintf(errmsg, "Critical - Will expire in %i days (%i-%02i-%02i).", expires_in,
153 t->tm_year+1900, t->tm_mon+1, t->tm_mday );
156 sprintf(errmsg, "OK - Will expire in %i days (%i-%02i-%02i).", expires_in,
157 t->tm_year+1900, t->tm_mon+1, t->tm_mday );
162 err = gnutls_bye( session, GNUTLS_SHUT_WR );
163 if (err < 0) gnutls_die(err);
166 gnutls_deinit( session );
167 gnutls_certificate_free_credentials( xcred );
172 void log_func( int level, char *msg ) {
173 fprintf(stderr, "[%2i] %s", level, msg);
177 * This signal handler is wrong, but it's just a failsafe.
179 void sig_handler(int k) {
180 fputs("Timeout.", stderr);
184 int main(int argc, char **argv) {
186 char *service = NULL;
190 while ((opt = getopt(argc, argv, "hvw:c:H:p:s:")) != -1) {
193 warning_after = atoi(optarg);
196 error_after = atoi(optarg);
199 hostname = strdup(optarg);
203 if (service != NULL) die("Only one service can be specified.");
204 service = strdup(optarg);
215 if (argc <= 1) die("No address to try.");
217 gnutls_global_set_log_function((gnutls_log_func) log_func);
218 gnutls_global_set_log_level(LOG_LEVEL);
221 /* Initialize gnutls */
223 if ((err = gnutls_global_init())) {
228 sprintf(errmsg, "OK");
234 /* TODO: doesn't work. */
235 signal(SIGALRM, sig_handler);
239 state = check(hostname, service);
241 printf("Internal error.");
243 gnutls_global_deinit();
248 printf("%s\n", errmsg);
249 return (state < 0) ? 127 : state;
254 "Usage: cert-checker [options] -H hostname -p|s port|service\n"
255 " Where options could be: \n"
257 " -w warning level (in days, default 30)\n"
258 " -c critical level (in days, default 7)\n"
259 " -v verbosity level\n"