commands/vupgrade.cc
changeset 2 b3afb9f1e801
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 #include "misc/maildir.h"
       
    26 
       
    27 const char* cli_program = "vupgrade";
       
    28 const char* cli_help_prefix = "Upgrades the vmailmgr data files for a virtual domain\n";
       
    29 const char* cli_help_suffix =
       
    30 "Reads in a standard virtual password table in the current directory,\n"
       
    31 "and writes it out to the current table format.  The file names for the\n"
       
    32 "input and output tables are determined from the configuration file.\n"
       
    33 "In addition, a user directory is created for each account that does\n"
       
    34 "not have one.";
       
    35 const char* cli_args_usage = "";
       
    36 const int cli_args_min = 0;
       
    37 const int cli_args_max = 0;
       
    38 
       
    39 static int o_quiet = false;
       
    40 
       
    41 cli_option cli_options[] = {
       
    42   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    43     "Suppress all status messages", 0 },
       
    44   {0,}
       
    45 };
       
    46 
       
    47 static fdibuf* passwd_in = 0;
       
    48 static vpwentry* getpw_passwd()
       
    49 {
       
    50   mystring buf;
       
    51   if(!passwd_in->getline(buf))
       
    52     return 0;
       
    53 
       
    54   int first = buf.find_first(':');
       
    55   if(first < 0)
       
    56     return 0;
       
    57 
       
    58   return vpwentry::new_from_record(buf.left(first), buf.c_str() + first);
       
    59 }
       
    60 
       
    61 static vpwtable_reader* vpwtable_in = 0;
       
    62 static vpwentry* getpw_vpwtable()
       
    63 {
       
    64   return vpwtable_in->get();
       
    65 }
       
    66 
       
    67 static vpwentry* (*getpw)();
       
    68 
       
    69 int open_table()
       
    70 {
       
    71   vpwtable_in = domain.table()->start_read();
       
    72   if(vpwtable_in && !!*vpwtable_in)
       
    73     getpw = getpw_vpwtable;
       
    74   else {
       
    75     passwd_in = new fdibuf(password_file.c_str());
       
    76     if(!*passwd_in)
       
    77       return false;
       
    78     getpw = getpw_passwd;
       
    79   }
       
    80   return true;
       
    81 }
       
    82 
       
    83 int cli_main(int, char* [])
       
    84 {
       
    85   if(!go_home())
       
    86     return 1;
       
    87 
       
    88   if(!open_table()) {
       
    89     if(!o_quiet)
       
    90       ferr << "Can't open password table." << endl;
       
    91     return 1;
       
    92   }
       
    93 
       
    94   vpwtable_writer* out = domain.table()->start_write();
       
    95 
       
    96   vpwentry* vpw;
       
    97   while((vpw = getpw()) != 0) {
       
    98     // Create any missing directories
       
    99     if(!vpw->directory) {
       
   100       mystring dir = domain.userdir(vpw->name);
       
   101       if(mkdirp(dir, 0700) == -1) {
       
   102 	out->abort();
       
   103 	if(!o_quiet)
       
   104 	  ferr << "Failed to created user directory '" << dir << "'" << endl;
       
   105 	return 1;
       
   106       }
       
   107       vpw->directory = dir;
       
   108     }
       
   109     if(!out->put(*vpw)) {
       
   110       out->abort();
       
   111       if(!o_quiet)
       
   112 	ferr << "Failed to add record to vpwtable." << endl;
       
   113       return 1;
       
   114     }
       
   115     delete vpw;
       
   116   }
       
   117   if(!out->end()) {
       
   118     if(!o_quiet)
       
   119       ferr << "Failed to finish writing vpwtable." << endl;
       
   120     return 1;
       
   121   }
       
   122   return 0;
       
   123 }