NULL terminate strncpy destination string
[dockapps/wmnotify.git] / src / ssl.c
index 6df0b23..d4cd91a 100644 (file)
--- a/src/ssl.c
+++ b/src/ssl.c
-/* ssl.c */\r
-\r
-/* Based on ssl_client.c (Sean Walton and Macmillan Publishers). */\r
-\r
-\r
-#if HAVE_CONFIG_H\r
-#  include "config.h"\r
-#endif\r
-\r
-#if HAVE_SSL\r
-\r
-/* Define filename_M */\r
-#define SSL_M 1\r
-\r
-#include <stdio.h>\r
-#include <errno.h>\r
-#include <unistd.h>\r
-#include <malloc.h>\r
-#include <string.h>\r
-#include <sys/socket.h>\r
-#include <resolv.h>\r
-#include <netdb.h>\r
-#include <openssl/ssl.h>\r
-#include <openssl/err.h>\r
-\r
-#include "common.h"\r
-#include "wmnotify.h"\r
-#include "ssl.h"\r
-\r
-\r
-/* InitCTX - initialize the SSL engine. */\r
-SSL_CTX *\r
-InitCTX( void )\r
-{\r
-  SSL_METHOD *method;\r
-  SSL_CTX *ctx;\r
-  \r
-  SSL_library_init();             /* Load cryptos, et.al. */\r
-  SSL_load_error_strings();       /* Bring in and register error messages */\r
-  method = SSLv2_client_method(); /* Create new client-method instance */\r
-  ctx = SSL_CTX_new(method);      /* Create new context */\r
-  if( ctx == NULL ) {\r
-    ERR_print_errors_fp(stderr);\r
-    abort();\r
-  }\r
-  return ctx;\r
-}\r
-\r
-\r
-/* ShowCerts - print out the certificates. */\r
-void\r
-ShowCerts( SSL *ssl )\r
-{\r
-  X509 *cert;\r
-  char *line;\r
-  \r
-  cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */\r
-  if ( cert != NULL ) {\r
-    printf("Server certificates:\n");\r
-    line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);\r
-    printf("Subject: %s\n", line);\r
-    free(line); /* free the malloc'ed string */\r
-    line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);\r
-    printf("Issuer: %s\n", line);\r
-    free(line); /* free the malloc'ed string */\r
-    X509_free(cert); /* free the malloc'ed certificate copy */\r
-  }\r
-  else {\r
-    printf("No certificates.\n");\r
-  }\r
-}\r
-\r
-\r
-int\r
-InitSSL( int sock_fd )\r
-{\r
-  ssl_infos.ctx = InitCTX();  \r
-  ssl_infos.ssl = SSL_new( ssl_infos.ctx ); /* create new SSL connection state */\r
-  if( ssl_infos.ssl == NULL ) {\r
-    printf( "%s: Error in SSL_new()\n", PACKAGE );\r
-    return EXIT_FAILURE;\r
-  }\r
-\r
-  SSL_set_fd( ssl_infos.ssl, sock_fd ); /* attach the socket descriptor */\r
-  if( SSL_connect( ssl_infos.ssl ) == FAIL ) { /* perform the connection */\r
-    ERR_print_errors_fp(stderr);\r
-    return EXIT_FAILURE;\r
-  }\r
-\r
-  if( wmnotify_infos.debug ) {\r
-    printf("Connected with %s encryption\n", SSL_get_cipher( ssl_infos.ssl ));\r
-    ShowCerts( ssl_infos.ssl ); /* get any certs */\r
-  }\r
-\r
-  return EXIT_SUCCESS;\r
-}\r
-\r
-\r
-#endif /* HAVE_SSL */\r
+/*
+ * ssl.c
+ *
+ * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
+ * Based on ssl_client.c (Sean Walton and Macmillan Publishers).
+ *
+ * This file is released under the GPLv2
+ */
+
+#if HAVE_CONFIG_H
+#  include "config.h"
+#endif /*  */
+
+#if HAVE_SSL
+
+/* Define filename_M */
+#define SSL_M 1
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <malloc.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <resolv.h>
+#include <netdb.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
+#include "common.h"
+#include "wmnotify.h"
+#include "ssl.h"
+
+/* InitCTX - initialize the SSL engine. */
+SSL_CTX *InitCTX(void)
+{
+       const SSL_METHOD *method;
+       SSL_CTX *ctx;
+       SSL_library_init();     /* Load cryptos, et.al. */
+       SSL_load_error_strings(); /* Bring in and register error messages */
+       method = SSLv23_client_method(); /*
+                                         * Indicate we support SSLv2, SSLv3 and
+                                         * TLSv1 methods.
+                                         */
+       ctx = SSL_CTX_new(method); /* Create new context */
+       if (ctx == NULL) {
+               ERR_print_errors_fp(stderr);
+               abort();
+       }
+       return ctx;
+}
+
+
+/* ShowCerts - print out the certificates. */
+void  ShowCerts(SSL *ssl)
+{
+       X509 *cert;
+       char *line;
+       cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
+       if (cert != NULL) {
+               printf("Server certificates:\n");
+               line =
+                       X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
+               printf("Subject: %s\n", line);
+               free(line);     /* free the malloc'ed string */
+               line =
+                       X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
+               printf("Issuer: %s\n", line);
+               free(line);     /* free the malloc'ed string */
+               X509_free(cert); /* free the malloc'ed certificate copy */
+       } else {
+               printf("No certificates.\n");
+       }
+}
+
+int  InitSSL(int sock_fd)
+{
+       ssl_infos.ctx = InitCTX();
+       ssl_infos.ssl = SSL_new(ssl_infos.ctx); /*
+                                                * create new SSL connection
+                                                * state
+                                                */
+       if (ssl_infos.ssl == NULL) {
+               printf("%s: Error in SSL_new()\n", PACKAGE);
+               return EXIT_FAILURE;
+       }
+       SSL_set_fd(ssl_infos.ssl, sock_fd); /* attach the socket descriptor */
+       if (SSL_connect(ssl_infos.ssl) == FAIL) { /* perform the connection */
+               ERR_print_errors_fp(stderr);
+               return EXIT_FAILURE;
+       }
+       if (wmnotify_infos.debug) {
+               printf("Connected with %s encryption\n",
+                      SSL_get_cipher(ssl_infos.ssl));
+               ShowCerts(ssl_infos.ssl);       /* get any certs */
+       }
+       return EXIT_SUCCESS;
+}
+
+
+#endif /* HAVE_SSL */