fixing double waitpid
[oweals/gnunet.git] / src / transport / gnunet-transport-certificate-creation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  */
27 #include "platform.h"
28 #include "gnunet_disk_lib.h"
29 #include "gnunet_os_lib.h"
30
31
32 static void 
33 removecerts (const char *file1, 
34              const char *file2)
35 {
36   if (GNUNET_DISK_file_test (file1) == GNUNET_YES)
37     {
38       CHMOD (file1, 0777);
39       REMOVE (file1);
40     }
41   if (GNUNET_DISK_file_test (file2) == GNUNET_YES)
42     {
43       CHMOD (file2, 0777);
44       REMOVE (file2);
45     }
46 }
47
48
49 int
50 main (int argc, char **argv)
51 {
52   struct GNUNET_OS_Process *openssl;
53   enum GNUNET_OS_ProcessStatusType status_type;
54   unsigned long code;
55
56   if (argc != 3)
57     return 1;
58   close (2); /* no output to stderr */
59   removecerts (argv[1], argv[2]);
60   /* Create RSA Private Key */
61   /* openssl genrsa -out $1 1024 2> /dev/null */
62   openssl = GNUNET_OS_start_process (NULL, NULL,
63                                      "openssl", 
64                                      "openssl",
65                                      "genrsa", "-out", argv[1], "1024",
66                                      NULL);
67   if (openssl == NULL)
68     return 2;
69   if (GNUNET_OS_process_wait (openssl) != GNUNET_OK)
70     {
71       GNUNET_OS_process_kill (openssl, SIGTERM);
72       removecerts (argv[1], argv[2]);
73       return 3;
74     }
75   if (GNUNET_OS_process_status (openssl, &status_type, &code) != GNUNET_OK)
76     {
77       GNUNET_OS_process_kill (openssl, SIGTERM);
78       removecerts (argv[1], argv[2]);
79       return 4;
80     }
81   if (status_type != GNUNET_OS_PROCESS_EXITED || code != 0)
82     {
83       GNUNET_OS_process_kill (openssl, SIGTERM);
84       removecerts (argv[1], argv[2]);
85       return 5;
86     }
87   GNUNET_OS_process_close (openssl);
88   
89   /* Create a self-signed certificate in batch mode using rsa key*/
90   /* openssl req -batch -days 365 -out $2 -new -x509 -key $1 2> /dev/null */
91   openssl = GNUNET_OS_start_process (NULL, NULL, 
92                                      "openssl", 
93                                      "openssl", 
94                                      "req", "-batch", "-days", "365", 
95                                      "-out", argv[2], "-new", "-x509", "-key", argv[1], 
96                                      NULL);
97   if (openssl == NULL)
98     return 6;
99   if (GNUNET_OS_process_wait (openssl) != GNUNET_OK)
100     {
101       GNUNET_OS_process_kill (openssl, SIGTERM);
102       removecerts (argv[1], argv[2]);
103       return 7;
104     }
105   if (GNUNET_OS_process_status (openssl, &status_type, &code) != GNUNET_OK)
106     {
107       GNUNET_OS_process_kill (openssl, SIGTERM);
108       removecerts (argv[1], argv[2]);
109       return 8;
110     }
111   if (status_type != GNUNET_OS_PROCESS_EXITED || code != 0)
112     {
113       GNUNET_OS_process_kill (openssl, SIGTERM);
114       removecerts (argv[1], argv[2]);
115       return 9;
116     }
117   GNUNET_OS_process_close (openssl);
118   CHMOD (argv[1], 0400);
119   CHMOD (argv[2], 0400);
120   return 0;
121 }
122
123 /* end of gnunet-transport-certificate-creation.c */