fix div by zero
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file transport/gnunet-transport-certificate-creation.c
21  * @brief create certificate for HTTPS transport
22  * @author LRN
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27 #ifndef WINDOWS
28 /**
29  * Turn the given file descriptor in to '/dev/null'.
30  *
31  * @param fd fd to bind to /dev/null
32  * @param flags flags to use (O_RDONLY or O_WRONLY)
33  */
34 static void
35 make_dev_zero (int fd,
36                int flags)
37 {
38   int z;
39
40   GNUNET_assert (0 == close (fd));
41   z = open ("/dev/null", flags);
42   GNUNET_assert (-1 != z);
43   if (z == fd)
44     return;
45   GNUNET_break (fd == dup2 (z, fd));
46   GNUNET_assert (0 == close (z));
47 }
48 #endif
49
50
51 static void
52 removecerts (const char *file1,
53              const char *file2)
54 {
55   if (GNUNET_YES == GNUNET_DISK_file_test (file1))
56   {
57     if (0 != CHMOD (file1, S_IWUSR | S_IRUSR))
58       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file1);
59     if (0 != REMOVE (file1))
60       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file1);
61   }
62   if (GNUNET_YES == GNUNET_DISK_file_test (file2))
63   {
64     if (0 != CHMOD (file2, S_IWUSR | S_IRUSR))
65       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", file2);
66     if (0 != REMOVE (file2))
67       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", file2);
68   }
69 }
70
71
72 int
73 main (int argc, char **argv)
74 {
75   struct GNUNET_OS_Process *openssl;
76
77   if (3 != argc)
78   {
79     fprintf (stderr,
80              "Invalid arguments.\n");
81     return 1;
82   }
83   removecerts (argv[1], argv[2]);
84   (void) GNUNET_DISK_directory_create_for_file (argv[1]);
85   (void) GNUNET_DISK_directory_create_for_file (argv[2]);
86   /* eliminate stderr */
87 #if WINDOWS
88   (void) close (2);
89 #else
90   make_dev_zero (2, O_WRONLY);
91 #endif
92   /* Create RSA Private Key */
93   /* openssl genrsa -out $1 1024 2> /dev/null */
94   openssl =
95       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
96                                NULL, NULL, NULL,
97                                "openssl", "openssl", "genrsa",
98                                "-out", argv[1], "1024", NULL);
99   if (NULL == openssl)
100   {
101     fprintf (stderr,
102              "Failed to run openssl.  Is openssl installed?\n");
103     return 2;
104   }
105   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
106   GNUNET_OS_process_destroy (openssl);
107
108   /* Create a self-signed certificate in batch mode using rsa key */
109   /* openssl req -batch -days 365 -out $2 -new -x509 -key $1 2> /dev/null */
110   openssl =
111       GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
112                                NULL, NULL, NULL,
113                                "openssl", "openssl", "req",
114                                "-batch", "-days", "365", "-out", argv[2],
115                                "-new", "-x509", "-key", argv[1], NULL);
116   if (NULL == openssl)
117   {
118     fprintf (stderr,
119              "Failed to create self-signed certificate with openssl.\n");
120     return 3;
121   }
122   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (openssl));
123   GNUNET_OS_process_destroy (openssl);
124   if (0 != CHMOD (argv[1], S_IRUSR))
125     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[1]);
126   if (0 != CHMOD (argv[2], S_IRUSR))
127     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", argv[2]);
128   return 0;
129 }
130
131 /* end of gnunet-transport-certificate-creation.c */