commands/vpasswd2cdb.cc
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include <string.h>
       
    19 #include "cdb++/cdb++.h"
       
    20 #include "cli/cli.h"
       
    21 #include "config/configrc.h"
       
    22 #include "fdbuf/fdbuf.h"
       
    23 #include "mystring/mystring.h"
       
    24 #include "vpwentry/vpwentry.h"
       
    25 #include "vcommand.h"
       
    26 
       
    27 const char* cli_program = "vpasswd2cdb";
       
    28 const char* cli_help_prefix = "Converts text password tables to CDB format\n";
       
    29 const char* cli_help_suffix =
       
    30 "Reads in a standard virtual password table in the current directory,
       
    31 and writes it out to a CDB table.  The file names for the text and CDB
       
    32 tables are determined from the configuration file.";
       
    33 const char* cli_args_usage = "";
       
    34 const int cli_args_min = 0;
       
    35 const int cli_args_max = 0;
       
    36 
       
    37 static int o_quiet = false;
       
    38 
       
    39 cli_option cli_options[] = {
       
    40   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    41     "Suppress all status messages", 0 },
       
    42   {0}
       
    43 };
       
    44 
       
    45 static bool getpw(fdibuf& in, vpwentry& out)
       
    46 {
       
    47   mystring buf;
       
    48   if(!in.getline(buf))
       
    49     return false;
       
    50 
       
    51   int first = buf.find_first(':');
       
    52   if(first < 0) return false;
       
    53 
       
    54   return out.from_record(buf.left(first), buf.c_str() + first);
       
    55 }
       
    56 
       
    57 int cli_main(int, char* [])
       
    58 {
       
    59   if(!go_home())
       
    60     return 1;
       
    61 
       
    62   fdibuf in(password_file.c_str());
       
    63   if(!in) {
       
    64     if(!o_quiet)
       
    65       ferr << "Can't open password table named '"
       
    66 	   << password_file << "'." << endl;
       
    67     return 1;
       
    68   }
       
    69   mystring cdbname = password_file + ".cdb";
       
    70   mystring cdbtmp = cdbname + ".tmp";
       
    71   cdb_writer cdb(cdbtmp, 0600);
       
    72   if(!cdb) {
       
    73     if(!o_quiet)
       
    74       ferr << "Can't open CDB temporary file named '" << cdbtmp << "'."
       
    75 	   << endl;
       
    76     return 1;
       
    77   }
       
    78   vpwentry vpw;
       
    79   while(getpw(in, vpw)) {
       
    80     if(!cdb.put(vpw.name, vpw.to_record())) {
       
    81       if(!o_quiet)
       
    82 	ferr << "Failed to add record to CDB table." << endl;
       
    83       return 1;
       
    84     }
       
    85   }
       
    86   if(!cdb.end(cdbname)) {
       
    87     if(!o_quiet)
       
    88       ferr << "Failed to finish CDB table into '"
       
    89 	   << password_file << ".cdb'." << endl;
       
    90     return 1;
       
    91   }
       
    92   return 0;
       
    93 }