MCS  0.3.3-alpha7
mcscreatekey.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 #include <mcs.hh>
23 
24 int main(int argc, char* argv[])
25 {
26 
27 #ifdef HAVE_OPENSSL_SSL_H
28  unsigned int ui;
29  string sslcert = string(MCS_SSLCERT);
30  string sslpriv = string(MCS_SSLPRIV), s;
31 
32  if ((! mcs::File_Dir_Exist(sslcert, ui)) ||
33  (! mcs::File_Dir_Exist(sslpriv, ui)) ) {
34 
35  //Create certificate and private key
36  int i;
37  string s;
38 
39  cout << "Creating SSL private key and certificate for MCS server..." << endl << endl;
40 
41  cout << "The private key can be protected by a password, this is needed if the " << endl;
42  cout << sslpriv << " file can be read by other users." << endl;
43  cout << "If you choose to provide a password then you must enter that password " << endl;
44  cout << "each time you execute the server." << endl << endl;
45 
46  cout << "Protect the private key with a password (y/n) ? [y]" << endl;
47  cin >> s;
48 
49  s = ( ((s == "") || (s[0] == 'y') || (s[0] == 'Y')) ? " -des3 " : "");
50 
51  s = "openssl genrsa " + s + " -out " + string(MCS_SSLPRIV) + " 2048";
52  cout << endl << endl << s << endl;
53  i = system(s.c_str());
54  if (i != 0) {
55  cerr << "Error executing openssl" << endl;
56  exit(i);
57  }
58 
59  s = "openssl req -new -x509 -batch -key " + string(MCS_SSLPRIV) +
60  " -out " + string(MCS_SSLCERT);
61  cout << endl << endl << s << endl;
62 
63  i = system(s.c_str());
64  if (i != 0) {
65  cerr << "Error executing openssl" << endl;
66  exit(i);
67  }
68  }
69  else {
70  cout << "Files for private key and certificate already exists:" << endl;
71  cout << " " << sslpriv << endl;
72  cout << " " << sslcert << endl;
73  }
74 #else //HAVE_OPENSSL_SSL_H
75  cerr << "The SSL support has been disabled, try: ./configure --enable-openssl" << endl;
76  exit(1);
77 #endif //HAVE_OPENSSL_SSL_H
78 
79  //openssl genrsa -des3 -out privkey.pem 2048
80  //openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095
81 }
bool File_Dir_Exist(string fn, unsigned int &size)
Check if a file or directory exists.
Definition: Utils.cc:39
Main include file for all MCS based applications.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018