make more use of testing lib
[oweals/gnunet.git] / src / transport / gnunet-transport-certificate-creation.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #ifndef WINDOWS
30 /**
31  * Turn the given file descriptor in to '/dev/null'.
32  *
33  * @param fd fd to bind to /dev/null
34  * @param flags flags to use (O_RDONLY or O_WRONLY)
35  */
36 static void
37 make_dev_zero (int fd,
38                int flags)
39 {
40   int z;
41
42   GNUNET_assert (0 == close (fd));
43   z = open ("/dev/null", flags);
44   GNUNET_assert (-1 != z);
45   if (z == fd)
46     return;
47   dup2 (z, fd);
48   GNUNET_assert (0 == close (z));
49 }
50 #endif
51
52
53 static void
54 removecerts (const char *file1,
55              const char *file2)
56 {
57   if (GNUNET_YES == GNUNET_DISK_file_test (file1))
58   {
59     if (0 != CHMOD (file1, S_IWUSR | S_IRUSR))
60       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file1);
61     if (0 != REMOVE (file1))
62       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file1);
63   }
64   if (GNUNET_YES == GNUNET_DISK_file_test (file2))
65   {
66     if (0 != CHMOD (file2, S_IWUSR | S_IRUSR))
67       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file2);
68     if (0 != REMOVE (file2))
69       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file2);
70   }
71 }
72
73
74 int
75 main (int argc, char **argv)
76 {
77   struct GNUNET_OS_Process *openssl;
78
79   if (3 != argc)
80   {
81     fprintf (stderr,
82              "Invalid arguments.\n");
83     return 1;
84   }
85   removecerts (argv[1], argv[2]);
86   (void) GNUNET_DISK_directory_create_for_file (argv[1]);
87   (void) GNUNET_DISK_directory_create_for_file (argv[2]);
88   /* eliminate stderr */
89 #if WINDOWS
90   (void) close (2);
91 #else
92   make_dev_zero (2, O_WRONLY);
93 #endif
94   /* Create RSA Private Key */
95   /* openssl genrsa -out $1 1024 2> /dev/null */
96   openssl =
97       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
98                                NULL, NULL, NULL,
99                                "openssl", "openssl", "genrsa",
100                                "-out", argv[1], "1024", NULL);
101   if (NULL == openssl)
102   {
103     fprintf (stderr,
104              "Failed to run openssl.  Is openssl installed?\n");
105     return 2;
106   }
107   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
108   GNUNET_OS_process_destroy (openssl);
109
110   /* Create a self-signed certificate in batch mode using rsa key */
111   /* openssl req -batch -days 365 -out $2 -new -x509 -key $1 2> /dev/null */
112   openssl =
113       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
114                                NULL, NULL, NULL,
115                                "openssl", "openssl", "req",
116                                "-batch", "-days", "365", "-out", argv[2],
117                                "-new", "-x509", "-key", argv[1], NULL);
118   if (NULL == openssl)
119   {
120     fprintf (stderr,
121              "Failed to create self-signed certificate with openssl.\n");
122     return 3;
123   }
124   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
125   GNUNET_OS_process_destroy (openssl);
126   if (0 != CHMOD (argv[1], S_IRUSR))
127     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[1]);
128   if (0 != CHMOD (argv[2], S_IRUSR))
129     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[2]);
130   return 0;
131 }
132
133 /* end of gnunet-transport-certificate-creation.c */