-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailManager.cpp
More file actions
534 lines (431 loc) · 12.7 KB
/
Copy pathMailManager.cpp
File metadata and controls
534 lines (431 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "MailManager.h"
#include <libopi/FetchmailConfig.h>
#include <libopi/ServiceHelper.h>
#include <libopi/MailConfig.h>
#include <libutils/UserGroups.h>
#include <libutils/Process.h>
#include <libutils/Logger.h>
using namespace OPI;
//TODO: move to sysconfig or better, move storage to secop
#define FETCHMAILRC "/var/opi/etc/fetchmailrc"
namespace KGP
{
MailManager::MailManager(): fetchmailupdated(false), postfixupdated(false)
{
logg << Logger::Notice << "Mailmanager initialized" << lend;
}
MailManager &MailManager::Instance()
{
static MailManager mgr;
return mgr;
}
void MailManager::DeleteUser(const string &user)
{
// Remove any fetchmail accounts
list<map<string,string>> accounts = this->GetRemoteAccounts( user );
for(map<string,string>& account: accounts)
{
this->DeleteRemoteAccount( account["host"], account["identity"]);
}
// Delete all incoming addresses
this->DeleteAddresses( user );
// Delete user from localmail
this->RemoveLocalAddress(user);
// If in aliases, remove as well
this->RemoveUserAliases( user );
}
bool MailManager::AddToAdmin(const string &user)
{
if( ! this->AddUserAlias("/^postmaster@/",user+"@localdomain") ||
! this->AddUserAlias("/^root@/",user+"@localdomain"))
{
return false;
}
return true;
}
bool MailManager::RemoveFromAdmin(const string &user)
{
if( ! this->RemoveUserAlias("/^postmaster@/",user+"@localdomain") ||
! this->RemoveUserAlias("/^root@/",user+"@localdomain") )
{
return false;
}
return true;
}
bool MailManager::SetHostname(const string &name, const string& domain)
{
try
{
File::Write("/etc/mailname", name + "."+ domain, File::UserRW | File::GroupRead | File::OtherRead);
bool res = false;
stringstream cmd;
cmd << "/usr/sbin/postconf -e \"myhostname=" << name << "." << domain <<"\"";
tie(res, std::ignore) = Process::Exec(cmd.str() );
if( ! res )
{
this->global_error = "Failed to update postfix hostname";
logg << Logger::Error << this->global_error << lend;
return false;
}
}
catch (std::runtime_error& err)
{
this->global_error = string("Failed to set mail hostname: ") + err.what();
logg << Logger::Error << this->global_error << lend;
return false;
}
return true;
}
list<string> MailManager::GetDomains()
{
return MailConfig().GetDomains();
}
void MailManager::AddDomain(const string &domain)
{
MailConfig mc;
mc.AddDomain(domain);
mc.WriteConfig();
this->postfixupdated = true;
}
void MailManager::DeleteDomain(const string &domain)
{
MailConfig mc;
mc.DeleteDomain( domain );
mc.WriteConfig();
this->postfixupdated = true;
}
void MailManager::SetAddress(const string &domain, const string &address, const string &user)
{
MailConfig mc;
mc.SetAddress(domain, address, user);
mc.WriteConfig();
this->postfixupdated = true;
}
void MailManager::DeleteAddress(const string &domain, const string &address)
{
MailConfig mc;
mc.DeleteAddress(domain, address);
mc.WriteConfig();
this->postfixupdated = true;
}
void MailManager::DeleteAddresses(const string &user)
{
MailConfig mc;
list<string> doms = mc.GetDomains();
for( const auto& domain: doms)
{
list<tuple<string,string>> addrs = mc.GetAddresses(domain);
for( const tuple<string,string> &addr: addrs)
{
string localpart, auser;
tie(localpart, auser) = addr;
if( auser == user )
{
mc.DeleteAddress(domain,localpart);
}
}
}
mc.WriteConfig();
this->postfixupdated = true;
}
list<tuple<string, string> > MailManager::GetAddresses(const string &domain)
{
MailConfig mc;
return mc.GetAddresses(domain);
}
void MailManager::ChangeDomain(const string &from, const string &to)
{
MailConfig mc;
mc.ChangeDomain(from, to);
mc.WriteConfig();
this->postfixupdated = true;
}
tuple<string, string> MailManager::GetAddress(const string &domain, const string &address)
{
return MailConfig().GetAddress(domain, address);
}
bool MailManager::hasDomain(const string &domain)
{
return MailConfig().hasDomain(domain);
}
bool MailManager::hasAddress(const string &domain, const string &address)
{
return MailConfig().hasAddress(domain, address);
}
bool MailManager::SetLocalAddress(const string &user)
{
const string localmail(SCFG.GetKeyAsString("filesystem", "storagemount")+SCFG.GetKeyAsString("mail", "localmail"));
try
{
// Add user to localdomain mailboxfile
OPI::MailMapFile mmf( localmail );
mmf.ReadConfig();
mmf.SetAddress("localdomain", user, user);
mmf.WriteConfig();
if( chown( localmail.c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0 )
{
this->global_error = string("Failed to chown localmail (")+strerror(errno)+")";
logg << Logger::Error << this->global_error << lend;
return false;
}
}
catch( runtime_error& err)
{
this->global_error = string("Failed to add local mail address (")+err.what()+")";
return false;
}
this->postfixupdated = true;
return true;
}
bool MailManager::RemoveLocalAddress(const string &user)
{
const string localmail(SCFG.GetKeyAsString("filesystem", "storagemount")+SCFG.GetKeyAsString("mail", "localmail"));
try
{
// Remove user from localdomain mailboxfile
OPI::MailMapFile mmf( localmail );
mmf.ReadConfig();
mmf.DeleteAddress("localdomain", user);
mmf.WriteConfig();
}
catch( runtime_error& err)
{
this->global_error = string("Failed to remove local mail address (")+err.what()+")";
logg << Logger::Error << this->global_error << lend;
return false;
}
this->postfixupdated = true;
return true;
}
list<map<string, string> > MailManager::GetRemoteAccounts(const string &user)
{
FetchmailConfig fc( FETCHMAILRC );
return fc.GetAccounts( user );
}
map<string, string> MailManager::GetRemoteAccount(const string &hostname, const string &identity)
{
FetchmailConfig fc( FETCHMAILRC );
return fc.GetAccount( hostname, identity);
}
void MailManager::AddRemoteAccount(const string &email, const string &host, const string &identity, const string &password, const string &user, bool ssl)
{
FetchmailConfig fc( FETCHMAILRC);
fc.AddAccount(email, host, identity, password, user, ssl );
fc.WriteConfig();
this->fetchmailupdated = true;
}
void MailManager::UpdateRemoteAccount(const string &email, const string &host, const string &identity, const string &password, const string &user, bool ssl)
{
FetchmailConfig fc( FETCHMAILRC );
fc.UpdateAccount(email, host, identity, password, user, ssl );
fc.WriteConfig();
this->fetchmailupdated = true;
}
void MailManager::DeleteRemoteAccount(const string &hostname, const string &identity)
{
FetchmailConfig fc( FETCHMAILRC );
fc.DeleteAccount(hostname, identity);
fc.WriteConfig();
this->fetchmailupdated = true;
}
list<string> MailManager::GetAliases()
{
list<string> ret;
const string virtual_aliases(SCFG.GetKeyAsString("filesystem", "storagemount") + SCFG.GetKeyAsString("mail","virtualalias"));
try
{
OPI::MailAliasFile mf( virtual_aliases );
ret = mf.GetAliases();
}
catch( runtime_error& err)
{
this->global_error = string("Failed to retrieve aliases (")+err.what()+")";
}
return ret;
}
list<string> MailManager::GetAliasUsers(const string &alias)
{
list<string> ret;
const string virtual_aliases(SCFG.GetKeyAsString("filesystem", "storagemount") + SCFG.GetKeyAsString("mail","virtualalias"));
try
{
OPI::MailAliasFile mf( virtual_aliases );
ret = mf.GetUsers( alias );
}
catch( runtime_error& err)
{
this->global_error = string("Failed to retrieve aliases (")+err.what()+")";
}
return ret;
}
bool MailManager::AddUserAlias(const string &alias, const string &user)
{
const string virtual_aliases(SCFG.GetKeyAsString("filesystem", "storagemount") + SCFG.GetKeyAsString("mail","virtualalias"));
try
{
OPI::MailAliasFile mf( virtual_aliases );
mf.AddUser(alias,user);
mf.WriteConfig();
}
catch( runtime_error& err)
{
this->global_error = string("Failed to add user alias (")+err.what()+")";
return false;
}
if ( chown( virtual_aliases.c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0 )
{
this->global_error = strerror(errno);
logg << Logger::Error << "Failed to chown aliases file: " << this->global_error << lend;
return false;
}
this->postfixupdated = true;
return true;
}
bool MailManager::RemoveUserAlias(const string &alias, const string &user)
{
const string virtual_aliases(SCFG.GetKeyAsString("filesystem", "storagemount") + SCFG.GetKeyAsString("mail","virtualalias"));
try
{
OPI::MailAliasFile mf( virtual_aliases );
mf.RemoveUser(alias, user);
mf.WriteConfig();
}
catch( runtime_error& err)
{
this->global_error = string("Failed to remove user alias (")+err.what()+")";
logg << Logger::Error << this->global_error << lend;
return false;
}
return true;
}
bool MailManager::RemoveUserAliases(const string &user)
{
list<string> aliases = this->GetAliases();
for( const string& alias: aliases)
{
list<string> users = this->GetAliasUsers( alias );
for( const string& auser: users)
{
if( auser == user )
{
if( ! this->RemoveUserAlias(alias, user) )
{
return false;
}
}
}
}
return true;
}
// From OPI-B
// Merge of update_postfix and reload fetchmail
bool MailManager::Synchronize(bool force)
{
bool p_ret = true; // Postfixreturn
bool f_ret = true; // Fetchmailreturn
SysConfig sysconfig;
if( this->postfixupdated || force )
{
bool status = false;
string aliases = sysconfig.GetKeyAsString("filesystem","storagemount") + "/" + sysconfig.GetKeyAsString("mail","vmailbox");
tie(status, std::ignore) = Utils::Process::Exec( "/usr/sbin/postmap " + aliases );
if( !status )
{
this->global_error = "Falied to process aliases file";
logg << Logger::Error << this->global_error << lend;
p_ret = false;
}
string saslpwd = sysconfig.GetKeyAsString("filesystem","storagemount") + "/" + sysconfig.GetKeyAsString("mail","saslpasswd");
tie(status, std::ignore) = Utils::Process::Exec( "/usr/sbin/postmap " + saslpwd );
if( !status )
{
this->global_error = "Falied to process sasl password file";
logg << Logger::Error << this->global_error << lend;
p_ret = false;
}
string localmail = sysconfig.GetKeyAsString("filesystem","storagemount") + "/" + sysconfig.GetKeyAsString("mail","localmail");
tie(status, std::ignore) = Utils::Process::Exec( "/usr/sbin/postmap " + localmail );
if( !status )
{
this->global_error = "Falied to process local mail file";
logg << Logger::Error << this->global_error << lend;
p_ret = false;
}
status = ServiceHelper::Reload("postfix");
if( !status )
{
this->global_error = "Falied to reload postfix";
logg << Logger::Error << this->global_error << lend;
p_ret = false;
}
if( p_ret )
{
this->postfixupdated = false;
}
}
if ( this->fetchmailupdated || force )
{
f_ret = ServiceHelper::Stop( "fetchmail" );
f_ret &= ServiceHelper::Start( "fetchmail" );
if( f_ret)
{
this->fetchmailupdated = false;
}
else
{
this->global_error = "Falied to restart fetchmail";
logg << Logger::Error << this->global_error << lend;
}
}
return p_ret && f_ret;
}
// From opi-b postfix_fixpaths
void MailManager::SetupEnvironment()
{
SysConfig sysconfig;
string aliases = sysconfig.GetKeyAsString("filesystem","storagemount") + sysconfig.GetKeyAsString("mail","vmailbox");
if( ! File::FileExists( aliases ) )
{
File::Write( aliases, "", File::UserRW);
}
string saslpwd = sysconfig.GetKeyAsString("filesystem","storagemount") + sysconfig.GetKeyAsString("mail","saslpasswd");
if( ! File::FileExists( saslpwd ) )
{
File::Write( saslpwd, "", File::UserRW);
}
string domains = sysconfig.GetKeyAsString("filesystem","storagemount") + sysconfig.GetKeyAsString("mail","vdomains");
if( ! File::FileExists( domains ) )
{
File::Write( domains, "", File::UserRW);
}
string localmail = sysconfig.GetKeyAsString("filesystem","storagemount") + sysconfig.GetKeyAsString("mail","localmail");
if( ! File::FileExists( localmail ) )
{
File::Write( localmail, "", File::UserRW);
}
if( chown( aliases.c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0)
{
logg << Logger::Error << "Failed to change owner on aliases file"<<lend;
}
if( chown( saslpwd.c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0)
{
logg << Logger::Error << "Failed to change owner on saslpasswd file"<<lend;
}
if( chown( domains.c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0)
{
logg << Logger::Error << "Failed to change owner on domain file"<<lend;
}
if( chown( File::GetPath(domains).c_str(), User::UserToUID("postfix"), Group::GroupToGID("postfix") ) != 0)
{
logg << Logger::Error << "Failed to change owner on config directory"<<lend;
}
if( chmod( File::GetPath(domains).c_str(), File::UserRWX ) != 0)
{
logg << Logger::Error << "Failed to change mode on config directory"<<lend;
}
}
MailManager::~MailManager()
{
logg << Logger::Notice << "Mailmanager destroyed" << lend;
}
} // Namespace KGP