|
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" |
|
23 #include "cli/cli.h" |
|
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 } |
|
124 vpwentry vpw(alias, "*", 0, dest); |
|
125 vpw.set_defaults(true, true); |
|
126 response resp = domain.set(&vpw, true); |
|
127 if(!resp) { |
|
128 if(!o_quiet) |
|
129 ferr << "vsetup: error: adding alias '" << alias << "' failed:\n " |
|
130 << resp.msg << endl; |
|
131 return false; |
|
132 } |
|
133 if(!o_quiet) |
|
134 fout << "vsetup: added alias '" << alias << "'\n"; |
|
135 return true; |
|
136 } |
|
137 |
|
138 bool setup_passwd() |
|
139 { |
|
140 mystring email = config->postmaster_email(); |
|
141 mystring_iter iter(config->postmaster_aliases().str()); |
|
142 while(iter) { |
|
143 if(!setup_alias(*iter, email)) |
|
144 break; |
|
145 ++iter; |
|
146 } |
|
147 return !iter; |
|
148 } |
|
149 |
|
150 int cli_main(int, char* []) |
|
151 { |
|
152 if(!go_home()) |
|
153 return 1; |
|
154 |
|
155 user_dir = config->user_dir(); |
|
156 |
|
157 if(execute("vsetup-pre")) { |
|
158 if(!o_quiet) |
|
159 ferr << "vsetup: Execution of 'vsetup-pre' failed!\n"; |
|
160 return 1; |
|
161 } |
|
162 if(!setup_user_dir()) |
|
163 return 1; |
|
164 if(!setup_qmail_default()) |
|
165 return 1; |
|
166 if(!setup_passwd()) |
|
167 return 1; |
|
168 if(execute("vsetup-post")) { |
|
169 if(!o_quiet) |
|
170 ferr << "vsetup: Execution of 'vsetup-post' failed!\n"; |
|
171 return 1; |
|
172 } |
|
173 return 0; |
|
174 } |