• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

yapet/main.cc

Go to the documentation of this file.
00001 // $Id: main.cc 3356 2010-09-18 21:06:17Z rafi $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 // Additional permission under GNU GPL version 3 section 7
00021 //
00022 // If you modify this program, or any covered work, by linking or combining it
00023 // with the OpenSSL project's OpenSSL library (or a modified version of that
00024 // library), containing parts covered by the terms of the OpenSSL or SSLeay
00025 // licenses, Rafael Ostertag grants you additional permission to convey the
00026 // resulting work.  Corresponding Source for a non-source form of such a
00027 // combination shall include the source code for the parts of OpenSSL used as
00028 // well as that of the covered work.
00029 //
00030 
00031 #ifdef HAVE_CONFIG_H
00032 # include <config.h>
00033 #endif
00034 
00035 #ifdef HAVE_NCURSES_H
00036 # include <ncurses.h>
00037 #else // HAVE_NCURSES_H
00038 # ifdef HAVE_CURSES_H
00039 #  include <curses.h>
00040 # else
00041 #  error "Neither curses.h nor ncurses.h available"
00042 # endif // HAVE_CURSES_H
00043 #endif // HAVE_NCURSES_H
00044 #include "curswa.h" // Leave this here. It depends on the above includes.
00045 
00046 #ifdef TIME_WITH_SYS_TIME
00047 # include <sys/time.h>
00048 # include <time.h>
00049 #else
00050 # ifdef HAVE_SYS_TIME_H
00051 #  include <sys/time.h>
00052 # else
00053 #  include <time.h>
00054 # endif
00055 #endif
00056 
00057 #ifdef HAVE_UNISTD_H
00058 # include <unistd.h>
00059 #endif
00060 
00061 #ifdef HAVE_SYS_RESOURCE_H
00062 # include <sys/resource.h>
00063 #endif
00064 
00065 #ifdef HAVE_STRING_H
00066 # include <string.h>
00067 #endif
00068 
00069 #ifdef HAVE_ERRNO_H
00070 # include <errno.h>
00071 #endif
00072 
00073 #ifdef HAVE_IOSTREAM
00074 # include <iostream>
00075 #endif
00076 
00077 #ifdef HAVE_STRING
00078 # include <string>
00079 #endif
00080 
00081 #ifdef HAVE_GETOPT_H
00082 # include <getopt.h>
00083 #endif
00084 
00085 #ifdef HAVE_CRYPTO_H
00086 # include <openssl/crypto.h>
00087 #endif
00088 
00089 #include "../intl.h"
00090 #include "fileopen.h"
00091 #include "mainwindow.h"
00092 #include "cfg.h"
00093 #include "consts.h"
00094 #include "globals.h"
00095 
00105 const char COPYRIGHT[] = "YAPET -- Yet Another Password Encryption Tool\n" \
00106              "Copyright (C) 2008-2010  Rafael Ostertag\n"               \
00107              "\n"                                \
00108              "This program is free software: you can redistribute it and/or modify\n" \
00109              "it under the terms of the GNU General Public License as published by\n" \
00110              "the Free Software Foundation, either version 3 of the License, or\n" \
00111              "(at your option) any later version.\n"             \
00112              "\n"                                \
00113              "This program is distributed in the hope that it will be useful,\n" \
00114              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"  \
00115              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"   \
00116              "GNU General Public License for more details.\n"            \
00117              "\n"                                \
00118              "You should have received a copy of the GNU General Public License\n" \
00119              "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n";
00120 
00121 
00122 void set_rlimit() {
00123 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
00124     rlimit rl;
00125     rl.rlim_cur = 0;
00126     rl.rlim_max = 0;
00127     int retval = setrlimit (RLIMIT_CORE, &rl);
00128 
00129     if (retval != 0) {
00130     std::cerr << _ ("Failed to suppress the creation of core file.")
00131           << std::endl
00132           << _ ("The error message is: ") << strerror (errno)
00133           << std::endl
00134           << _ ("In case a core file is created, it may contain clear text passwords.")
00135           << std::endl
00136           << std::endl
00137           << _ ("Press <ENTER> to continue")
00138           << std::endl;
00139     std::cin.ignore(1,'\n');
00140     }
00141 
00142 #else
00143     std::cerr << _ ("Cannot suppress the creation of core file.")
00144           << std::endl
00145           << _ ("In case a core file is created, it may contain clear text passwords.")
00146           << std::endl
00147           << std::endl
00148           << _ ("Press <ENTER> to continue")
00149           << std::endl;
00150     std::cin.ignore(1,'\n');
00151 #endif
00152 }
00153 
00154 void show_version() {
00155     std::cout << PACKAGE_STRING << std::endl;
00156     std::cout << _("Architecture: ");
00157 #ifdef SIZEOF_INT_P
00158     std::cout << 8 * SIZEOF_INT_P << "bit";
00159 #endif
00160 #ifdef WORDS_BIGENDIAN
00161     std::cout << " big endian" << std::endl;
00162 #else
00163     std::cout << " little endian" << std::endl;
00164 #endif
00165 
00166 #ifdef HAVE_SSLEAY_VERSION
00167     std::cout << _ ("SSL Version: ") << SSLeay_version (SSLEAY_VERSION) << std::endl;
00168 #endif
00169 #ifdef NCURSES_VERSION
00170     std::cout << _ ("Curses Implementation: ") << "ncurses (" << NCURSES_VERSION << ")" << std::endl;
00171 #else   // NCURSES_VERSION
00172 #ifdef _XOPEN_CURSES
00173     std::cout << _ ("Curses Implementation: ") << "XOpen Curses" << std::endl;
00174 #else   // _XOPEN_CURSES
00175     std::cout << _ ("Curses Implementation: ") << "System Curses" << std::endl;
00176 #endif  // _XOPEN_CURSES
00177 #endif  // NCURSES_VERSION
00178 #if defined(HAVE_TERMINALTITLE) && defined(HAVE_TERMNAME)
00179     std::cout << _ ("Compiled with support for terminal title") << std::endl;
00180 #else
00181     std::cout << _ ("Compiled without support for terminal title") << std::endl;
00182 #endif
00183 #ifdef ENABLE_PWGEN
00184     std::cout << _ ("Compiled with password generator") << std::endl;
00185 #else
00186     std::cout << _ ("Compiled without password generator") << std::endl;
00187 #endif
00188 #if !defined(HAVE_FSTAT) || !defined(HAVE_GETUID) || !defined(HAVE_FCHMOD) || !defined(HAVE_FCHOWN)
00189     std::cout << _ ("Support for file security NOT available") << std::endl;
00190 #endif
00191 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
00192     std::cout << _ ("Creation of core file is suppressed") << std::endl;
00193 #else
00194     std::cout << _ ("Creation of core files is NOT suppressed") << std::endl;
00195 #endif
00196 }
00197 
00198 void show_copyright() {
00199     std::cout << COPYRIGHT << std::endl;
00200 }
00201 
00202 void show_help (char* prgname) {
00203     show_version();
00204     std::cout << std::endl;
00205     std::cout << prgname
00206           << " [-chV] [-i | -r <rcfile>] [-s | -S] [-t <sec>] [<filename>]"
00207           << std::endl
00208           << std::endl;
00209     std::cout << "-c, --copyright\t\t" << _ ("show copyright information")
00210           << std::endl
00211           << std::endl;
00212     std::cout << "-h, --help\t\t" << _ ("show this help text")
00213           << std::endl
00214           << std::endl;
00215     std::cout << "-i, --ignore-rc\t\t" << _ ("do not read the configuration file.")
00216           << std::endl
00217           << std::endl;
00218     std::cout << "-r, --rc-file\t\t" << _ ("read the configuration file specified by <rcfile>.\n" \
00219                        "\t\t\tIf this option is not provided, it defaults to\n" \
00220                        "\t\t\t$HOME/.yapet unless -i is specified.")
00221           << std::endl
00222           << std::endl;
00223     std::cout << "-s, --no-file-security\t" << _ ("disable check of owner and file permissions.\n" \
00224           "\t\t\tWhen creating new files, the file mode is set\n" \
00225           "\t\t\tto 0644.")
00226           << std::endl
00227           << std::endl;
00228     std::cout << "-S, --file-security\t" << _ ("enable check of owner and file permissions.\n" \
00229           "\t\t\tWhen creating new files, the file mode is set\n" \
00230           "\t\t\tto 0600.")
00231           << std::endl
00232           << std::endl;
00233     std::cout << "-t, --timeout\t\t" << _ ("the time-out in seconds until the screen is locked.\n" \
00234                        "\t\t\tDefault: 600 sec.")
00235           << std::endl
00236           << std::endl;
00237     std::cout << "-V, --version\t\t" << _ ("show the version of ") << PACKAGE_NAME
00238           << std::endl
00239           << std::endl;
00240     std::cout << "<filename>\t\t" << _ ("open the specified file <filename>")
00241           << std::endl
00242           << std::endl;
00243     char buff[512];
00244     snprintf (buff, 512, _ ("%s stores passwords encrypted on disk using the blowfish encryption\n" \
00245                 "algorithm. Point your browser to http://www.guengel.ch/myapps/yapet/\n"
00246                 "for more information about %s."), PACKAGE_NAME, PACKAGE_NAME);
00247     std::cout << buff
00248           << std::endl
00249           << std::endl;
00250 }
00251 
00252 int main (int argc, char** argv) {
00253     set_rlimit();
00254 #ifdef ENABLE_NLS
00255     setlocale (LC_MESSAGES, "");
00256     bindtextdomain (PACKAGE, LOCALEDIR);
00257     textdomain (PACKAGE);
00258 #endif
00259     YAPET::CONFIG::Config config;
00260     // If empty, default is taken
00261     std::string cfgfilepath;
00262     int c;
00263 #ifdef HAVE_GETOPT_LONG
00264     struct option long_options[] = {
00265     { (char*) "copyright", no_argument, NULL, 'c'},
00266     { (char*) "help", no_argument, NULL, 'h'},
00267     { (char*) "ignore-rc", no_argument, NULL, 'i'},
00268     { (char*) "rc-file", required_argument, NULL, 'r'},
00269     { (char*) "no-file-security", no_argument, NULL, 's'},
00270     { (char*) "file-security", no_argument, NULL, 'S'},
00271     { (char*) "timeout", required_argument, NULL, 't'},
00272     { (char*) "version", no_argument, NULL, 'V'},
00273     {NULL, 0, NULL, 0}
00274     };
00275 
00276     while ( (c = getopt_long (argc, argv, ":chir:sSt:V", long_options, NULL) ) != -1) {
00277 #else // HAVE_GETOPT_LONG
00278     extern char *optarg;
00279     extern int optopt, optind;
00280 
00281     while ( (c = getopt (argc, argv, ":c(copyright)h(help)i(ignore-rc)r:(rc-file)s(no-file-security)S(file-security)t:(timeout)V(version)") ) != -1) {
00282 #endif // HAVE_GETOPT_LONG
00283 
00284     switch (c) {
00285         case 'c':
00286         show_copyright();
00287         return 0;
00288         case 'h':
00289         show_help (argv[0]);
00290         return 0;
00291         case 'i':
00292         config.setIgnorerc (true);
00293         break;
00294         case 'r':
00295         cfgfilepath = optarg;
00296         break;
00297         case 'V':
00298         show_version();
00299         return 0;
00300         case 's':
00301         config.setFilesecurity (false);
00302         break;
00303         case 'S':
00304         config.setFilesecurity (true);
00305         break;
00306         case 't':
00307         unsigned int timeout;
00308         sscanf (optarg, "%u", &timeout);
00309         config.setTimeout (timeout);
00310         break;
00311         case ':':
00312         std::cerr << "-" << (char) optopt << _ (" without argument")
00313               << std::endl;
00314         return 1;
00315         case '?':
00316         std::cerr << _ ("unknown argument") << " '" << (char) optopt << "'"
00317               << std::endl;
00318         return 1;
00319     }
00320     }
00321 
00322     if (optind < argc) {
00323     config.setPetFile (argv[optind]);
00324     }
00325 
00326     config.loadConfigFile (cfgfilepath);
00327     // Make sure the .pet suffix is there
00328 #ifndef NDEBUG
00329     std::string _tmp__ = config.getPetFile();
00330 #endif
00331     assert (_tmp__.empty() ||
00332         _tmp__.find (YAPET::CONSTS::Consts::getDefaultSuffix(),
00333              _tmp__.length() -
00334              YAPET::CONSTS::Consts::getDefaultSuffix().length() )
00335         != std::string::npos);
00336     // Make sure we have a cleaned up file path
00337     assert (_tmp__.find ("//", 0) == std::string::npos);
00338 #ifndef CFGDEBUG
00339     try {
00340     YAPET::UI::BaseWindow::initCurses();
00341     } catch (YAPET::UI::UIException &ex) {
00342     YAPET::UI::BaseWindow::endCurses();
00343     std::cerr << ex.what() << std::endl;
00344     return 1;
00345     }
00346     MainWindow* mainwin = NULL;
00347 
00348     YAPET::GLOBALS::Globals::setPWGenRNG(config.getPWGenRNG());
00349     YAPET::GLOBALS::Globals::setCharacterPools(config.getCharPools());
00350     YAPET::GLOBALS::Globals::setPasswordLength(config.getPWGenPWLen());
00351     YAPET::GLOBALS::Globals::setAllowLockQuit(config.getAllowLockQuit());
00352     YAPET::GLOBALS::Globals::setPwInputTimeout(config.getPwInputTimeout());
00353 
00354     try {
00355 
00356     mainwin = new MainWindow (config.getTimeout(), config.getFilesecurity() );
00357     // filename may be empty
00358     mainwin->run (config.getPetFile() );
00359     delete mainwin;
00360     } catch (std::exception& ex) {
00361     if (mainwin != NULL)
00362         delete mainwin;
00363 
00364     YAPET::UI::BaseWindow::endCurses();
00365     std::cerr << ex.what() << std::endl << std::endl;
00366     return 1;
00367     }
00368 
00369     YAPET::UI::BaseWindow::endCurses();
00370 #else
00371     config.getPetFile();
00372     config.getTimeout();
00373     config.getFilesecurity();
00374 #endif
00375     return 0;
00376 }

Generated on Sun Sep 19 2010 15:37:14 for YAPET by  doxygen 1.7.1