|
0
|
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"
|
|
2
|
24 |
#include "cli++/cli++.h"
|
|
0
|
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);
|
|
2
|
103 |
vpwentry vpw(user, pwcrypt(pass), maildir, 0, true);
|
|
|
104 |
response resp = domain.set(&vpw, true);
|
|
0
|
105 |
if(!resp) {
|
|
|
106 |
errors++;
|
|
|
107 |
if(!o_quiet)
|
|
|
108 |
ferr << "vaddusers: error adding the virtual user, skipping aliases:\n "
|
|
|
109 |
<< resp.msg << endl;
|
|
|
110 |
return;
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
if(!o_quiet)
|
|
|
114 |
fout << "added user '" << user << "'";
|
|
|
115 |
for(;;) {
|
|
|
116 |
mystring alias = get_word(str);
|
|
|
117 |
if(!alias)
|
|
|
118 |
break;
|
|
|
119 |
alias = alias.lower();
|
|
|
120 |
if(domain.exists(alias)) {
|
|
|
121 |
fout << endl;
|
|
|
122 |
errors++;
|
|
|
123 |
if(!o_quiet)
|
|
|
124 |
ferr << "vaddusers: warning: alias '" << alias
|
|
|
125 |
<< "' already exists, skipping." << endl;
|
|
|
126 |
continue;
|
|
|
127 |
}
|
|
2
|
128 |
vpwentry vpw(alias, "*", 0, user, false);
|
|
0
|
129 |
response resp = domain.set(&vpw, true);
|
|
|
130 |
if(!resp) {
|
|
|
131 |
fout << endl;
|
|
|
132 |
errors++;
|
|
|
133 |
if(!o_quiet)
|
|
|
134 |
ferr << "vaddusers: warning: adding the alias '" << alias
|
|
|
135 |
<< "' failed:\n " << resp.msg << endl;
|
|
|
136 |
}
|
|
|
137 |
if(!o_quiet)
|
|
|
138 |
fout << ", alias '" << alias << "'";
|
|
|
139 |
}
|
|
|
140 |
fout << endl;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
int cli_main(int, char*[])
|
|
|
144 |
{
|
|
|
145 |
if(!go_home())
|
|
|
146 |
return 1;
|
|
|
147 |
mystring line;
|
|
|
148 |
while(fin.getline(line))
|
|
|
149 |
add_one(line);
|
|
|
150 |
if(errors) {
|
|
|
151 |
if(!o_quiet)
|
|
|
152 |
ferr << "vaddusers: " << errors << " errors were encountered." << endl;
|
|
|
153 |
return 1;
|
|
|
154 |
}
|
|
|
155 |
return 0;
|
|
|
156 |
}
|