-switching (again) to named sockets, see #2887
[oweals/gnunet.git] / src / transport / gnunet-transport-certificate-creation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/gnunet-transport-certificate-creation.c
23  * @brief create certificate for HTTPS transport
24  * @author LRN
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29
30 static void
31 removecerts (const char *file1,
32              const char *file2)
33 {
34   if (GNUNET_YES == GNUNET_DISK_file_test (file1))
35   {
36     if (0 != CHMOD (file1, S_IWUSR | S_IRUSR))
37       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file1);
38     if (0 != REMOVE (file1))
39       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file1);
40   }
41   if (GNUNET_YES == GNUNET_DISK_file_test (file2))
42   {
43     if (0 != CHMOD (file2, S_IWUSR | S_IRUSR))
44       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file2);
45     if (0 != REMOVE (file2))
46       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file2);
47   }
48 }
49
50
51 int
52 main (int argc, char **argv)
53 {
54   struct GNUNET_OS_Process *openssl;
55
56   if (3 != argc)
57   {
58     fprintf (stderr,
59              "Invalid arguments.\n");
60     return 1;
61   }
62   removecerts (argv[1], argv[2]);
63   (void) GNUNET_DISK_directory_create_for_file (argv[1]);
64   (void) GNUNET_DISK_directory_create_for_file (argv[2]);
65   (void) close (2);                    /* eliminate stderr */
66   /* Create RSA Private Key */
67   /* openssl genrsa -out $1 1024 2> /dev/null */
68   openssl =
69       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "openssl", "openssl", "genrsa",
70                                "-out", argv[1], "1024", NULL);
71   if (NULL == openssl)
72   {
73     fprintf (stderr,
74              "Failed to run openssl.  Is openssl installed?\n");
75     return 2;
76   }
77   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
78   GNUNET_OS_process_destroy (openssl);
79
80   /* Create a self-signed certificate in batch mode using rsa key */
81   /* openssl req -batch -days 365 -out $2 -new -x509 -key $1 2> /dev/null */
82   openssl =
83       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "openssl", "openssl", "req",
84                                "-batch", "-days", "365", "-out", argv[2],
85                                "-new", "-x509", "-key", argv[1], NULL);
86   if (NULL == openssl)
87   {
88     fprintf (stderr,
89              "Failed to create self-signed certificate with openssl.\n");
90     return 3;
91   }
92   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
93   GNUNET_OS_process_destroy (openssl);
94   if (0 != CHMOD (argv[1], S_IRUSR))
95     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[1]);
96   if (0 != CHMOD (argv[2], S_IRUSR))
97     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[2]);
98   return 0;
99 }
100
101 /* end of gnunet-transport-certificate-creation.c */