yapet/main.cc

Go to the documentation of this file.
00001 // $Id: main.cc,v 1.11 2008-02-25 15:16:12 rafi Exp $
00002 //
00003 // YAPET -- Yet Another Password Encryption Tool
00004 // Copyright (C) 2008  Rafael Ostertag
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #ifdef HAVE_NCURSES_H
00025 # include <ncurses.h>
00026 #else // HAVE_NCURSES_H
00027 # ifdef HAVE_CURSES_H
00028 #  include <curses.h>
00029 # else
00030 #  error "Neither curses.h nor ncurses.h available"
00031 # endif // HAVE_CURSES_H
00032 #endif // HAVE_NCURSES_H
00033 #include "curswa.h" // Leave this here. It depends on the above includes.
00034 
00035 #ifdef TIME_WITH_SYS_TIME
00036 # include <sys/time.h>
00037 # include <time.h>
00038 #else
00039 # ifdef HAVE_SYS_TIME_H
00040 #  include <sys/time.h>
00041 # else
00042 #  include <time.h>
00043 # endif
00044 #endif
00045 
00046 #ifdef HAVE_UNISTD_H
00047 # include <unistd.h>
00048 #endif
00049 
00050 #ifdef HAVE_SYS_RESOURCE_H
00051 # include <sys/resource.h>
00052 #endif
00053 
00054 #ifdef HAVE_STRING_H
00055 # include <string.h>
00056 #endif
00057 
00058 #ifdef HAVE_ERRNO_H
00059 # include <errno.h>
00060 #endif
00061 
00062 #ifdef HAVE_IOSTREAM
00063 # include <iostream>
00064 #endif
00065 
00066 #ifdef HAVE_STRING
00067 # include <string>
00068 #endif
00069 
00070 #ifdef HAVE_GETOPT_H
00071 # include <getopt.h>
00072 #endif
00073 
00074 #include "fileopen.h" // for the endswith() functions
00075 #include "mainwindow.h"
00076 
00086 const char COPYRIGHT[] = "YAPET -- Yet Another Password Encryption Tool\n" \
00087     "Copyright (C) 2008  Rafael Ostertag\n"             \
00088     "\n"                                \
00089     "This program is free software: you can redistribute it and/or modify\n" \
00090     "it under the terms of the GNU General Public License as published by\n" \
00091     "the Free Software Foundation, either version 3 of the License, or\n" \
00092     "(at your option) any later version.\n"             \
00093     "\n"                                \
00094     "This program is distributed in the hope that it will be useful,\n" \
00095     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"  \
00096     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"   \
00097     "GNU General Public License for more details.\n"            \
00098     "\n"                                \
00099     "You should have received a copy of the GNU General Public License\n" \
00100     "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n";
00101 
00102 
00103 void set_rlimit() {
00104 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE)
00105     rlimit rl;
00106 
00107     rl.rlim_cur = 0;
00108     rl.rlim_max = 0;
00109     int retval = setrlimit(RLIMIT_CORE, &rl);
00110     if (retval != 0) {
00111     std::cerr << "Failed to suppress the creation of core file."
00112           << std::endl
00113           << "The error message is: " << strerror(errno)
00114           << std::endl
00115           << "In case a core file is created, it may contain clear text passwords."
00116           << std::endl
00117           << std::endl
00118           << "Press <ENTER> to continue"
00119           << std::endl;
00120     char tmp;
00121     std::cin >> tmp;
00122     }
00123 #else
00124     std::cerr << "Cannot suppress the creation of core file."
00125           << std::endl
00126           << "In case a core file is created, it may contain clear text passwords."
00127           << std::endl
00128           << std::endl
00129           << "Press <ENTER> to continue"
00130           << std::endl;
00131     char tmp;
00132     std::cin >> tmp;
00133 #endif
00134 }
00135 
00136 void show_version() {
00137     std::cout << PACKAGE_STRING << std::endl;
00138 }
00139 
00140 void show_copyright() {
00141     std::cout << COPYRIGHT << std::endl;
00142 }
00143 
00144 void show_help(char* prgname) {
00145     show_version();
00146     std::cout << std::endl;
00147     std::cout << prgname
00148           << " [-c] [-h] [-V] [<filename>]"
00149           << std::endl
00150           << std::endl;
00151     std::cout << "-c, --copyright\tshow copyright information"
00152           << std::endl
00153           << std::endl;
00154     std::cout << "-h, --help\tshow this help text"
00155           << std::endl
00156           << std::endl;
00157     std::cout << "-V, --version\tshow the version of " PACKAGE_NAME
00158           << std::endl
00159           << std::endl;
00160     std::cout << "<filename>\topen the specified file <filename>"
00161           << std::endl
00162           << std::endl;
00163     std::cout << PACKAGE_NAME " stores passwords encrypted on disk using the blowfish algorithm."
00164           << std::endl;
00165     std::cout << "The encryption key is computed from the master password using md5, sha1, and"
00166           << std::endl;
00167     std::cout << "ripemd-160 digest algorithms producing a 448 bit (56 bytes) key."
00168           << std::endl
00169           << std::endl;
00170 }
00171 
00172 int main (int argc, char** argv) {
00173     set_rlimit();
00174 
00175     int c;
00176     std::string filename;
00177 #ifdef HAVE_GETOPT_LONG
00178     struct option long_options[] = {
00179     {"copyright", no_argument, NULL, 'c'},
00180     {"help", no_argument, NULL, 'h'},
00181     {"version", no_argument, NULL, 'V'},
00182     {NULL,0,NULL,0}
00183     };
00184     while ( (c = getopt_long(argc, argv, ":chV", long_options, NULL)) != -1) {
00185 #else // HAVE_GETOPT_LONG
00186     while ( (c = getopt(argc, argv, ":c(copyright)h(help)V(version)")) != -1) {
00187 #endif // HAVE_GETOPT_LONG
00188     switch (c) {
00189     case 'c':
00190         show_copyright();
00191         return 0;
00192     case 'h':
00193         show_help(argv[0]);
00194         return 0;
00195     case 'V':
00196         show_version();
00197         return 0;
00198     case ':':
00199         std::cerr << "-" << (char)optopt << " without argument"
00200               << std::endl;
00201         return 1;
00202     case '?':
00203         std::cerr << "unknown argument '" << (char)optopt << "'"
00204               << std::endl;
00205         return 1;
00206     }
00207     }
00208 
00209     if (argc > 1) {
00210     filename = argv[argc-1];
00211 
00212     if (!endswith(filename, ".pet"))
00213     filename+=".pet";
00214     }
00215 
00216     YAPETUI::BaseWindow::initCurses();
00217 
00218     MainWindow* mainwin = NULL;
00219     try {
00220     mainwin = new MainWindow();
00221     // filename may be empty
00222     mainwin->run(filename);
00223     delete mainwin;
00224     } catch (std::exception& ex) {
00225     if (mainwin != NULL)
00226         delete mainwin;
00227     YAPETUI::BaseWindow::endCurses();
00228     std::cerr << ex.what() << std::endl << std::endl;
00229     return 1;
00230     }
00231 
00232     YAPETUI::BaseWindow::endCurses();
00233 
00234     return 0;
00235 }

Generated on Wed Feb 27 16:15:42 2008 for YAPET by  doxygen 1.5.4