commands/vpasswds.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
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 <ctype.h>
       
    19 #include <stdlib.h>
       
    20 #include "fdbuf/fdbuf.h"
       
    21 #include "mystring/mystring.h"
       
    22 #include "misc/response.h"
       
    23 #include "config/configrc.h"
       
    24 #include "cli/cli.h"
       
    25 #include "vcommand.h"
       
    26 
       
    27 const char* cli_program = "vpasswds";
       
    28 const char* cli_help_prefix = "Changes the passwords for a list of virtual users\n";
       
    29 const char* cli_help_suffix = "";
       
    30 const char* cli_args_usage = "";
       
    31 const int cli_args_min = 0;
       
    32 const int cli_args_max = 0;
       
    33 
       
    34 static int o_quiet = false;
       
    35 
       
    36 // This program is used to change the password of a list of virtual
       
    37 // users.  The list is read from standard input.
       
    38 // Each line in the list contains the user's name and new pass phrase,
       
    39 // seperated by whitespace.
       
    40 // It will attempt to change the password for each listed user.
       
    41 // If any step fails, a warning is issued and processing continues with
       
    42 // the next line.
       
    43 // Any input folling the password is ignored.
       
    44 
       
    45 cli_option cli_options[] = {
       
    46   { 0, "quiet", cli_option::flag, true, &o_quiet,
       
    47     "Suppress all status messages", 0 },
       
    48   {0}
       
    49 };
       
    50 
       
    51 // SEE ALSO
       
    52 //
       
    53 // vpasswd(1)
       
    54 
       
    55 // NOTES
       
    56 //
       
    57 // You must have either created the users subdirectory by hand or run the
       
    58 // F<vsetup> program before using this program.
       
    59 //
       
    60 // This program expects the environment variable C<HOME> to be set, and
       
    61 // executes a change directory to the contents of it before starting.  It
       
    62 // is also required that you change user to the domain owner before using
       
    63 // these utilities.
       
    64 
       
    65 static int errors = 0;
       
    66 
       
    67 mystring get_word(mystring& line)
       
    68 {
       
    69   unsigned i = 0;
       
    70   while(i < line.length() && isspace(line[i]))
       
    71     ++i;
       
    72   unsigned start = i;
       
    73   while(i < line.length() && !isspace(line[i]))
       
    74     ++i;
       
    75   mystring word = line.sub(start, i-start);
       
    76   line = line.right(i);
       
    77   return word;
       
    78 }
       
    79   
       
    80 void change_one(const mystring& line)
       
    81 {
       
    82   mystring str = line;
       
    83   mystring user = get_word(str);
       
    84   mystring pass = get_word(str);
       
    85   if(!user || !pass) {
       
    86     ++errors;
       
    87     if(!o_quiet)
       
    88       ferr << "vpasswds: invalid line, ignoring:\n  "
       
    89 	   << line << endl;
       
    90     return;
       
    91   }
       
    92   user = user.lower();
       
    93 
       
    94   response resp = domain.chattr(user, vdomain::ATTR_PASS, pass);
       
    95 
       
    96   if(!resp) {
       
    97     ++errors;
       
    98     if(!o_quiet)
       
    99       ferr << "vpasswds: error changing the password for user '"
       
   100 	   << user << "':\n  " << resp.msg << endl;
       
   101     return;
       
   102   }
       
   103   else
       
   104     if(!o_quiet)
       
   105       fout << "vpasswds: password for user '" << user
       
   106 	   << "' successfully changed.\n";
       
   107 }
       
   108 
       
   109 int cli_main(int, char*[])
       
   110 {
       
   111   if(!go_home())
       
   112     return 1;
       
   113   mystring str;
       
   114   while(fin.getline(str))
       
   115     change_one(str);
       
   116   if(errors) {
       
   117     if(!o_quiet)
       
   118       ferr << "vpasswds: " << errors << " errors were encountered." << endl;
       
   119     return 1;
       
   120   }
       
   121   return 0;
       
   122 }