Added built-in help.
[cert-checker.git] / main.c
1 /******************************************************************************
2  * Filename: main.c
3  * Description:
4  *
5  * Version: 1.0
6  * Created: Oct 09 2010 19:14:12
7  * Last modified: Oct 09 2010 19:14:12
8  *
9  * Author: Ladislav L├íska
10  * e-mail: ladislav.laska@gmail.com
11  *
12  ******************************************************************************/
13
14 #define _XOPEN_SOURCE 500
15
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netdb.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <signal.h>
27
28 #define S_UNREACHABLE -1
29 #define S_NO_X509 -2
30 #define S_OK 0
31 #define S_WARNING 1
32 #define S_ERROR 2
33 #define S_UNKNOWN 3
34
35 int warning_after = 30;
36 int error_after = 7;
37 int verbose = 0;
38
39 #define LOG_LEVEL 0
40
41 #define die(msg) { fprintf(stderr, "Error: " msg "\n" ); exit(3); }
42 #define gnutls_die(code) { gnutls_perror(code); exit(3); }
43
44 char errmsg[256];
45
46 void print_help();
47
48 int tcp_open( char *hostname, char *service ) {
49         struct addrinfo hints;
50         struct addrinfo *result, *result_ptr;
51         int err, sfd;
52
53         /* Set hints */
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);
58         if (err) {
59                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
60                 exit(3);
61         }
62
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; 
67
68                 if (connect(sfd, result_ptr->ai_addr, result_ptr->ai_addrlen) != -1)
69                         break; /* Success */
70                 
71                 close(sfd);     /* connect(2) failed. */
72         }
73
74         if (result_ptr == NULL) {
75                 /* No address succeeded */
76                 return -1;
77         }
78
79         freeaddrinfo(result);
80
81         return sfd;
82 }
83
84 int check( char * hostname, char *service ) {
85         int state = S_OK;
86         int err;
87
88         gnutls_session_t session;
89         gnutls_certificate_credentials_t xcred;
90
91         /* x509 stuff */
92         
93         err = gnutls_certificate_allocate_credentials( &xcred );
94         if (err < 0) gnutls_die(err);
95
96         err = gnutls_init( &session, GNUTLS_CLIENT );
97         if (err < 0) gnutls_die(err);
98
99         /* priority init? */
100         err = gnutls_priority_set_direct(session, "EXPORT", NULL);
101         if (err < 0) gnutls_die(err);
102
103         err = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE, xcred );
104         if (err < 0) gnutls_die(err);
105
106         /* Connect to server */
107
108         int fd = tcp_open( hostname, service );
109         
110         if (fd == -1) {
111                 state= S_UNREACHABLE;
112                 goto cleanup;
113         }
114
115         /* Socket opened, establish tls connection */
116
117         /* Associate socket with session */
118         gnutls_transport_set_ptr( session, (gnutls_transport_ptr_t) fd );
119         
120         /* Do handshake */
121         err = gnutls_handshake( session );
122         if (err < 0) gnutls_die(err);
123         
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;
129
130         if ( gnutls_certificate_type_get( session ) != GNUTLS_CRT_X509 ) {
131                 state = S_NO_X509;
132                 goto cleanup;
133         }
134
135         cert_list = gnutls_certificate_get_peers( session, &cert_list_size );
136
137         today = time(NULL);
138
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)) {
146                         state = S_WARNING;
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 );
149                 }
150                 if ((state <= S_WARNING) && (expires_in <= error_after)) {
151                         state = S_ERROR;
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 );
154                 }
155                 if (state == S_OK) {
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 );
158                 }
159         }
160
161         /* Clean up */
162         err = gnutls_bye( session, GNUTLS_SHUT_WR );
163         if (err < 0) gnutls_die(err);
164         close( fd );
165         cleanup:
166         gnutls_deinit( session );
167         gnutls_certificate_free_credentials( xcred );
168
169         return state;
170 }
171
172 void log_func( int level, char *msg ) {
173         fprintf(stderr, "[%2i] %s", level, msg);
174 }
175
176 /* 
177  * This signal handler is wrong, but it's just a failsafe. 
178  */
179 void sig_handler(int k) {
180         fputs("Timeout.", stderr);      
181         exit(S_UNKNOWN);
182 }
183
184 int main(int argc, char **argv) {
185         char *hostname;
186         char *service = NULL;
187
188         int opt;
189
190         while ((opt = getopt(argc, argv, "hvw:c:H:p:s:")) != -1) {
191                 switch (opt) {
192                         case 'w':
193                                 warning_after = atoi(optarg);
194                                 break;
195                         case 'c':
196                                 error_after = atoi(optarg);
197                                 break;
198                         case 'H':
199                                 hostname = strdup(optarg);
200                                 break;
201                         case 'p':                       
202                         case 's':
203                                 if (service != NULL) die("Only one service can be specified.");
204                                 service = strdup(optarg);
205                                 break;
206                         case 'h':
207                                 print_help();
208                                 exit(0);
209                         case 'v':
210                                 verbose++;
211                         default: break;
212                 }
213         }
214
215         if (argc <= 1) die("No address to try.");
216
217         gnutls_global_set_log_function((gnutls_log_func) log_func);
218         gnutls_global_set_log_level(LOG_LEVEL);
219
220
221         /* Initialize gnutls */
222         int err;
223         if ((err = gnutls_global_init())) {
224                 gnutls_perror(err);
225                 exit(3);
226         };
227
228         sprintf(errmsg, "OK");
229         int state = 0;
230
231         fflush(stdout);
232
233         /* Setup alarm */
234         /* TODO: doesn't work. */
235         signal(SIGALRM, sig_handler);
236         alarm(9*60);
237
238         /* Do checking */
239         state = check(hostname, service);
240         if (state < 0)
241                 printf("Internal error.");
242         
243         gnutls_global_deinit();
244
245         free(hostname);
246         free(service);
247
248         printf("%s\n", errmsg);
249         return (state < 0) ? 127 : state;
250 }
251
252 void print_help() {
253         printf(
254                 "Usage: cert-checker [options] -H hostname -p|s port|service\n"
255                 "  Where options could be: \n"
256                 "       -h      this help\n"
257                 "       -w      warning level (in days, default 30)\n"
258                 "       -c      critical level (in days, default 7)\n"
259                 "       -v      verbosity level\n"      
260         );
261 }