|
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/pwcrypt.h" |
|
23 #include "config/configrc.h" |
|
24 #include "cli/cli.h" |
|
25 #include "vcommand.h" |
|
26 |
|
27 const char* cli_program = "vaddusers"; |
|
28 const char* cli_help_prefix = "Add users to a virtual domain\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 set up a list of users within a virtual host. |
|
37 // The list is taken from standard input. |
|
38 // Each line in the list contains the user name, pass phrase, and an |
|
39 // optional list of aliases, all seperated by whitespace. |
|
40 // It will attempt to add each listed user to the virtual password table |
|
41 // and to create a mail directory for the new user, as well as attempting |
|
42 // to add entries for each of the named aliases. |
|
43 // If any step fails, the remainder of that line is ignored and |
|
44 // processing continues. |
|
45 |
|
46 cli_option cli_options[] = { |
|
47 { 0, "quiet", cli_option::flag, true, &o_quiet, |
|
48 "Suppress all status messages", 0 }, |
|
49 {0} |
|
50 }; |
|
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 |
|
62 // SEE ALSO |
|
63 // |
|
64 // vadduser(1), vsetup(1) |
|
65 |
|
66 static int errors = 0; |
|
67 |
|
68 mystring get_word(mystring& line) |
|
69 { |
|
70 unsigned i = 0; |
|
71 while(i < line.length() && isspace(line[i])) |
|
72 ++i; |
|
73 unsigned start = i; |
|
74 while(i < line.length() && !isspace(line[i])) |
|
75 ++i; |
|
76 mystring word = line.sub(start, i-start); |
|
77 line = line.right(i); |
|
78 return word; |
|
79 } |
|
80 |
|
81 void add_one(const mystring& line) |
|
82 { |
|
83 mystring str = line; |
|
84 mystring user = get_word(str); |
|
85 mystring pass = get_word(str); |
|
86 if(!user || !pass) { |
|
87 errors++; |
|
88 if(!o_quiet) |
|
89 ferr << "vaddusers: invalid line, ignoring:\n " |
|
90 << line << endl; |
|
91 return; |
|
92 } |
|
93 user = user.lower(); |
|
94 if(domain.exists(user)) { |
|
95 errors++; |
|
96 if(!o_quiet) |
|
97 ferr << "vaddusers: error: user '" << user |
|
98 << "' already exists, skipping.\n"; |
|
99 return; |
|
100 } |
|
101 { |
|
102 mystring maildir = domain.userdir(user); |
|
103 vpwentry vpw(user, pwcrypt(pass), maildir, 0); |
|
104 vpw.set_defaults(true, true); |
|
105 response resp = domain.set(&vpw, true, maildir); |
|
106 if(!resp) { |
|
107 errors++; |
|
108 if(!o_quiet) |
|
109 ferr << "vaddusers: error adding the virtual user, skipping aliases:\n " |
|
110 << resp.msg << endl; |
|
111 return; |
|
112 } |
|
113 } |
|
114 if(!o_quiet) |
|
115 fout << "added user '" << user << "'"; |
|
116 for(;;) { |
|
117 mystring alias = get_word(str); |
|
118 if(!alias) |
|
119 break; |
|
120 alias = alias.lower(); |
|
121 if(domain.exists(alias)) { |
|
122 fout << endl; |
|
123 errors++; |
|
124 if(!o_quiet) |
|
125 ferr << "vaddusers: warning: alias '" << alias |
|
126 << "' already exists, skipping." << endl; |
|
127 continue; |
|
128 } |
|
129 vpwentry vpw(alias, "*", 0, user); |
|
130 vpw.set_defaults(true, true); |
|
131 response resp = domain.set(&vpw, true); |
|
132 if(!resp) { |
|
133 fout << endl; |
|
134 errors++; |
|
135 if(!o_quiet) |
|
136 ferr << "vaddusers: warning: adding the alias '" << alias |
|
137 << "' failed:\n " << resp.msg << endl; |
|
138 } |
|
139 if(!o_quiet) |
|
140 fout << ", alias '" << alias << "'"; |
|
141 } |
|
142 fout << endl; |
|
143 } |
|
144 |
|
145 int cli_main(int, char*[]) |
|
146 { |
|
147 if(!go_home()) |
|
148 return 1; |
|
149 mystring line; |
|
150 while(fin.getline(line)) |
|
151 add_one(line); |
|
152 if(errors) { |
|
153 if(!o_quiet) |
|
154 ferr << "vaddusers: " << errors << " errors were encountered." << endl; |
|
155 return 1; |
|
156 } |
|
157 return 0; |
|
158 } |