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 "fdbuf/fdbuf.h"
19 #include <stdlib.h>
20 #include "mystring/mystring.h"
21 #include "misc/passwdfn.h"
22 #include "config/configrc.h"
23 #include "cli++/cli++.h"
24 #include "vcommand.h"
25 
26 const char* cli_program = "vpasswd";
27 const char* cli_help_prefix = "Changes the password for an individual virtual user\n";
28 const char* cli_help_suffix = "";
29 const char* cli_args_usage = "USERNAME";
30 const int cli_args_min = 1;
31 const int cli_args_max = 1;
32 
33 static int o_quiet = false;
34 
35 // This program is used to change a user's password within a virtual
36 // domain.
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 // RETURN VALUE
45 //
46 // 0 if the password was changed successfully, nonzero otherwise.
47 
48 // SEE ALSO
49 //
50 // checkvpw(1)
51 
52 // NOTES
53 //
54 // You must have either created the users subdirectory by hand or run the
55 // F<vsetup> program before using this program.
56 //
57 // This program expects the environment variable C<HOME> to be set, and
58 // executes a change directory to the contents of it before starting.  It
59 // is also required that you change user to the domain owner before using
60 // these utilities.
61 
cli_main(int,char * argv[])62 int cli_main(int, char* argv[])
63 {
64   if(!go_home())
65     return 1;
66 
67   mystring username = argv[0];
68   username = username.lower();
69 
70   mystring pass = getpasswd("vpasswd");
71   if(pass.length() == 0)
72     return 1;
73 
74   response resp = domain.chattr(username, vdomain::ATTR_PASS, pass);
75 
76   if(!resp) {
77     if(!o_quiet)
78       ferr << "vpasswd: error changing the password for user '"
79 	   << username << "':\n  " << resp.msg << endl;
80     return 1;
81   }
82   else {
83     if(!o_quiet)
84       fout << "vpasswd: password for user '" << username
85 	   << "' successfully changed." << endl;
86     return 0;
87   }
88 }
89