commands/vpasswd2db.cc
changeset 2 b3afb9f1e801
parent 1 30113bfbe723
child 3 3d1d327cfa68
equal deleted inserted replaced
1:30113bfbe723 2:b3afb9f1e801
     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 "cli/cli.h"
       
    20 #include "config/configrc.h"
       
    21 #include "fdbuf/fdbuf.h"
       
    22 #include "mystring/mystring.h"
       
    23 #include "vpwentry/vpwentry.h"
       
    24 #include "vcommand.h"
       
    25 
       
    26 const char* cli_program = "vpasswd2db";
       
    27 const char* cli_help_prefix = "Converts text password tables to current vpwtable DB format\n";
       
    28 const char* cli_help_suffix =
       
    29 "Reads in a standard virtual password table in the current directory,
       
    30 and writes it out to a table.  The file names for the input and output
       
    31 tables are determined from the configuration file.";
       
    32 const char* cli_args_usage = "";
       
    33 const int cli_args_min = 0;
       
    34 const int cli_args_max = 0;
       
    35 
       
    36 static int o_quiet = false;
       
    37 
       
    38 cli_option cli_options[] = {
       
    39   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    40     "Suppress all status messages", 0 },
       
    41   {0}
       
    42 };
       
    43 
       
    44 static bool getpw(fdibuf& in, vpwentry& out)
       
    45 {
       
    46   mystring buf;
       
    47   if(!in.getline(buf))
       
    48     return false;
       
    49 
       
    50   int first = buf.find_first(':');
       
    51   if(first < 0) return false;
       
    52 
       
    53   return out.from_record(buf.left(first), buf.c_str() + first);
       
    54 }
       
    55 
       
    56 int cli_main(int, char* [])
       
    57 {
       
    58   if(!go_home())
       
    59     return 1;
       
    60 
       
    61   fdibuf in(password_file.c_str());
       
    62   if(!in) {
       
    63     if(!o_quiet)
       
    64       ferr << "Can't open password table named '"
       
    65 	   << password_file << "'." << endl;
       
    66     return 1;
       
    67   }
       
    68 
       
    69   vpwtable* out = domain.table();
       
    70 
       
    71   vpwentry vpw;
       
    72   while(getpw(in, vpw)) {
       
    73     if(!out->put(&vpw, true)) {
       
    74       if(!o_quiet)
       
    75 	ferr << "Failed to add record to vpwtable." << endl;
       
    76       return 1;
       
    77     }
       
    78   }
       
    79   return 0;
       
    80 }