-
Notifications
You must be signed in to change notification settings - Fork 2
/
Courier.php
438 lines (365 loc) · 7.98 KB
/
Courier.php
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
<?php
/**
* Couier class
*
* @package Framework
*/
require_once('Machine.php');
require_once('API.php');
define('MAIL_DIR', '/var/mail/virtual/');
define('MAIL_ALIAS', '/etc/valiases/');
define('MAIL_UID', 8);
define('MAIL_USER', 'mail');
define('MAIL_GROUP', 'mail');
/**
* Courier management class.
*
* @author Marco Ceppi <[email protected]>
* @since November 9, 2010
* @package Framework
* @subpackage Courier
*/
class Courier
{
/**
* Users Method
*
* @return array|false List of users
*/
public static function accounts()
{
$users = array();
if( !$data_array = file('/etc/courier/userdb') )
{
return false;
}
foreach( $data_array as $line )
{
list($user, $opts) = explode("\t", $line);
$tmp = array();
$opts = explode('|', $opts);
foreach( $opts as $opt )
{
list($key, $val) = explode('=', $opt);
$tmp[$key] = $val;
}
$tmp['size'] = trim(shell_exec("du -sh " . $tmp['mail'] . " | awk '{ print $1 }'"));
$users[$user] = $tmp;
}
return $users;
}
/**
* User Method
*
* @param string $username
*
* @return array|false List of user details
*/
public static function account( $username )
{
$users = self::accounts();
if( array_key_exists($username, $users) )
{
return $users[$username];
}
else
{
return false;
}
}
public static function aliases( $domain = NULL )
{
if( !is_null($domain) )
{
if( !Machine::is_domain($domain) )
{
return false;
}
}
else
{
$domain = '*';
}
$aliases = array();
$alias_files = glob("/etc/valiases/$domain");
foreach( $alias_files as $alias )
{
$domain = basename($alias);
if( !$file = file($alias, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES) )
{
return false;
}
foreach( $file as $line )
{
list($user, $forwarder) = explode(': ', $line, 2);
if( $user != '*' )
{
$alias = explode(', ', $forwarder);
$aliases[$domain][$user] = $alias;
}
else
{
$aliases[$domain][$user] = array($forwarder);
}
}
}
return $aliases;
}
public static function alias( $domain, $data )
{
if( Machine::is_domain($domain) )
{
$out = array();
if( is_array($data) )
{
foreach( $data as $user => $alias )
{
if( is_array($alias) )
{
$alias = @implode(', ', $alias);
}
$out[] = "$user: $alias";
}
$out = @implode("\n", $out);
}
file_put_contents("/etc/valiases/$domain", $out);
return true;
}
else
{
return false;
}
}
/**
* Rebuild Courier User Databse
*
* @return bool
*/
public static function userdb()
{
system('makeuserdb', $status);
return ( $status > 0 ) ? false : true;
}
/**
* Create maildir Directory
*
* @param string $dir Directory to create as maildir
*
* @return bool
*/
public static function maildir( $dir )
{
system("maildirmake $dir", $status);
return ( $status > 0 ) ? false : true;
}
/**
* Add Courier mail account
*
* @param string $email Email account (username) to add
* @param string $home Path for home
* @param string $mail Maildir directory
* @param int $mail_uid System uid for Mail user
* @param int $mail_gid System gid for Mail user
*
* @return bool
*/
public static function add_account( $email, $home, $mail, $mail_uid = MAIL_UID, $mail_gid = MAIL_UID )
{
if( is_null($mail_uid) || is_null($mail_gid) )
{
$userdata = Machine::user(MAIL_USER);
$mail_uid = (is_null($mail_uid)) ? $userdata['uid'] : $mail_uid;
$mail_gid = (is_null($mail_gid)) ? $userdata['gid'] : $mail_gid;
}
if( !is_dir($home) )
{
if( !mkdir( $home ) )
{
return false;
}
}
else
{
return false;
}
self::maildir($mail);
Machine::chown($home, 'mail', 'mail', true);
system("userdb $email set uid=$mail_uid gid=$mail_gid home=$home mail=$mail", $status);
if( $status > 0 )
{
//Whoops. We've got an issue. Backup slowly.
self::del_account($email);
return false;
}
else
{
self::userdb();
return true;
}
}
/**
* Remove Courier mail account
*
* @param string $username Email user to remove from Courier Database
*
* @return bool
*/
public static function del_account( $username )
{
if( !$userdata = self::account($username) )
{
return false;
}
Machine::rm($userdata['home'], true);
Machine::rm($userdata['home']);
system("userdb $username del", $status);
self::userdb();
return ( $status > 0 ) ? false : true;
}
/**
* Remove Courier mail forwarder
*
* @param string $address Email user to remove from Courier Database
*
* @return bool
*/
public static function del_forwarder( $address )
{
list($username, $domain) = explode('@', $address);
$aliases = self::aliases($domain);
if( array_key_exists($username, $aliases[$domain]) )
{
unset($aliases[$domain][$username]);
if( !self::alias($domain, $aliases[$domain]) )
{
return false;
}
}
return true;
}
public static function password($account, $password)
{
if( !self::account($account) )
{
return false;
}
system('echo "' . $password . '" | userdbpw -md5 | userdb ' . $account . ' set systempw', $status);
if( $status > 0 )
{
return false;
}
else
{
self::userdb();
}
return true;
}
public static function forward($from, $to, $overwrite = false)
{
list($username, $domain) = explode('@', $from);
if( Machine::is_domain($domain) )
{
$aliases = self::aliases($domain);
if( !array_key_exists($username, $aliases[$domain]) )
{
$useraliases = array_reverse($aliases[$domain], true);
$useraliases[$username] = array();
$useraliases = array_reverse($useraliases, true);
$aliases[$domain] = $useraliases;
}
if( $username == '*' )
{
$aliases[$domain][$username][0] = $to;
if( !self::alias($domain, $aliases[$domain]) )
{
return false;
}
}
else
{
if( $overwrite )
{
$to = explode(',', str_replace(' ', '', $to));
$aliases[$domain][$username] = $to;
if( !self::alias($domain, $aliases[$domain]) )
{
return false;
}
}
else
{
if( !array_unshift($aliases[$domain][$username], $to) > 1 )
{
return false;
}
else
{
if( !self::alias($domain, $aliases[$domain]) )
{
return false;
}
}
}
}
return true;
}
else
{
return false;
}
}
public static function setup($domain)
{
if( !is_dir(MAIL_DIR . $domain) )
{
if( !mkdir(MAIL_DIR . $domain) )
{
return false;
}
Machine::chown(MAIL_DIR . $domain, MAIL_USER, MAIL_GROUP);
}
if( !is_file(MAIL_ALIAS . $domain) )
{
if( !file_put_contents(MAIL_ALIAS . $domain, '*: :fail: No user at this address.') )
{
return false;
}
}
return true;
}
}
// Move Courier over
class Courier_Server extends Courier
{
}
class Courier_Client extends API
{
public static function accounts()
{
return json_decode(static::get(static::$server, 'email/account'), true);
}
public static function aliases( $domain )
{
return json_decode(static::get(static::$server, 'email/alias/' . $domain), true);
}
public static function password( $account, $password )
{
return static::set(static::$server, 'email/account/password', array('account' => $account, 'password' => $password));
}
public static function add_account( $email )
{
return static::set(static::$server, 'email/account/create', array('email' => $email));
}
public static function forward( $email, $to )
{
return static::set(static::$server, 'email/alias/' . $email, array('to' => $to));
}
public static function del_account( $email )
{
return static::del(static::$server, 'email/account/' . $email, array('to' => $to));
}
public static function del_alias( $email )
{
return static::del(static::$server, 'email/alias/' . $email, array('to' => $to));
}
}