|
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 <stdio.h>
|
|
|
19 |
#include <sys/stat.h>
|
|
|
20 |
#include "fdbuf/fdbuf.h"
|
|
|
21 |
#include "config/configrc.h"
|
|
|
22 |
#include "misc/stat_fns.h"
|
|
2
|
23 |
#include "cli++/cli++.h"
|
|
0
|
24 |
#include "misc/exec.h"
|
|
|
25 |
#include "vcommand.h"
|
|
|
26 |
|
|
|
27 |
const char* cli_program = "vsetup";
|
|
|
28 |
const char* cli_help_prefix = "Sets up a virtual domain for its first use\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 sets up the basic set of necessary files needed to use an
|
|
|
37 |
// account as a virtual domain with vmailmgr.
|
|
|
38 |
// The users directory is created if it does not exist.
|
|
|
39 |
// A F<.qmail-default> file is created with the proper contents.
|
|
|
40 |
// If a F<.qmail-default> previously existed, it is renamed to
|
|
|
41 |
// F<.qmail-default~>.
|
|
|
42 |
// Three system aliases (C<root>, C<mailer-daemon>, and C<postmaster>)
|
|
|
43 |
// are created to point to the configured postmaster.
|
|
|
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 |
// RETURN VALUE
|
|
|
52 |
//
|
|
|
53 |
// 0 if all files and directories are successfully created, 1 otherwise.
|
|
|
54 |
|
|
|
55 |
// SEE ALSO
|
|
|
56 |
//
|
|
|
57 |
// vdeliver(1), vmailmgrd(8)
|
|
|
58 |
|
|
|
59 |
// NOTES
|
|
|
60 |
//
|
|
|
61 |
// This program expects the environment variable C<HOME> to be set, and
|
|
|
62 |
// executes a change directory to the contents of it before starting. It
|
|
|
63 |
// is also required that you change user to the domain owner before using
|
|
|
64 |
// these utilities.
|
|
|
65 |
|
|
|
66 |
mystring user_dir;
|
|
|
67 |
|
|
|
68 |
bool setup_user_dir()
|
|
|
69 |
{
|
|
|
70 |
if(!is_exist(user_dir.c_str())) {
|
|
|
71 |
if(mkdir(user_dir.c_str(), 0755)) {
|
|
|
72 |
if(!o_quiet)
|
|
|
73 |
ferr << "vsetup: error: could not create users directory.\n";
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
else
|
|
|
77 |
if(!o_quiet)
|
|
|
78 |
fout << "vsetup: created users directory.\n";
|
|
|
79 |
}
|
|
|
80 |
else
|
|
|
81 |
if(!o_quiet)
|
|
|
82 |
fout << "vsetup: users directory already exists.\n";
|
|
|
83 |
return true;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
bool setup_qmail_default()
|
|
|
87 |
{
|
|
|
88 |
if(is_exist(".qmail-default")) {
|
|
|
89 |
if(!o_quiet)
|
|
|
90 |
ferr << "vsetup: warning: '.qmail-default' file exists, renaming to "
|
|
|
91 |
"'.qmail-default~'.\n";
|
|
|
92 |
if(rename(".qmail-default", ".qmail-default~")) {
|
|
|
93 |
if(!o_quiet)
|
|
|
94 |
ferr << "vsetup: error: rename failed.\n";
|
|
|
95 |
return false;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
fdobuf out(".qmail-default", fdobuf::create | fdobuf::excl, 0644);
|
|
|
99 |
if(!out) {
|
|
|
100 |
if(!o_quiet)
|
|
|
101 |
ferr << "vsetup: error: unable to open file '.qmail-default' for output.\n";
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
out << "|" BINDIR "/vdeliver\n";
|
|
|
105 |
if(!out.flush() || !out.close()) {
|
|
|
106 |
if(!o_quiet)
|
|
|
107 |
ferr << "vsetup: error: writing to file '.qmail-default' failed.\n";
|
|
|
108 |
return false;
|
|
|
109 |
}
|
|
|
110 |
if(!o_quiet)
|
|
|
111 |
fout << "vsetup: wrote '.qmail-default' file.\n";
|
|
|
112 |
return true;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
bool setup_alias(mystring alias, const mystring& dest)
|
|
|
116 |
{
|
|
|
117 |
alias = alias.lower();
|
|
|
118 |
if(domain.exists(alias)) {
|
|
|
119 |
if(!o_quiet)
|
|
|
120 |
ferr << "vsetup: warning: user '" << alias
|
|
|
121 |
<< "' already exists, skipping.\n";
|
|
|
122 |
return true;
|
|
|
123 |
}
|
|
2
|
124 |
vpwentry vpw(alias, "*", domain.userdir(alias), dest, true);
|
|
0
|
125 |
response resp = domain.set(&vpw, true);
|
|
|
126 |
if(!resp) {
|
|
|
127 |
if(!o_quiet)
|
|
|
128 |
ferr << "vsetup: error: adding alias '" << alias << "' failed:\n "
|
|
|
129 |
<< resp.msg << endl;
|
|
|
130 |
return false;
|
|
|
131 |
}
|
|
|
132 |
if(!o_quiet)
|
|
|
133 |
fout << "vsetup: added alias '" << alias << "'\n";
|
|
|
134 |
return true;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
bool setup_passwd()
|
|
|
138 |
{
|
|
|
139 |
mystring email = config->postmaster_email();
|
|
|
140 |
mystring_iter iter(config->postmaster_aliases().str());
|
|
|
141 |
while(iter) {
|
|
|
142 |
if(!setup_alias(*iter, email))
|
|
|
143 |
break;
|
|
|
144 |
++iter;
|
|
|
145 |
}
|
|
|
146 |
return !iter;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
int cli_main(int, char* [])
|
|
|
150 |
{
|
|
|
151 |
if(!go_home())
|
|
|
152 |
return 1;
|
|
|
153 |
|
|
|
154 |
user_dir = config->user_dir();
|
|
|
155 |
|
|
|
156 |
if(execute("vsetup-pre")) {
|
|
|
157 |
if(!o_quiet)
|
|
|
158 |
ferr << "vsetup: Execution of 'vsetup-pre' failed!\n";
|
|
|
159 |
return 1;
|
|
|
160 |
}
|
|
|
161 |
if(!setup_user_dir())
|
|
|
162 |
return 1;
|
|
|
163 |
if(!setup_qmail_default())
|
|
|
164 |
return 1;
|
|
|
165 |
if(!setup_passwd())
|
|
|
166 |
return 1;
|
|
|
167 |
if(execute("vsetup-post")) {
|
|
|
168 |
if(!o_quiet)
|
|
|
169 |
ferr << "vsetup: Execution of 'vsetup-post' failed!\n";
|
|
|
170 |
return 1;
|
|
|
171 |
}
|
|
|
172 |
return 0;
|
|
|
173 |
}
|