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