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
|
# -*- coding: utf-8 -*-
# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import commands
import pykolab
from pykolab.auth import Auth
from pykolab.translate import _
log = pykolab.getLogger('pykolab.cli')
conf = pykolab.getConf()
def __init__():
commands.register('add_alias', execute, description="Add alias.")
def execute(*args, **kw):
try:
primary_rcpt_address = conf.cli_args.pop(0)
try:
secondary_rcpt_address = conf.cli_args.pop(0)
except:
print >> sys.stderr, _("Specify the (new) alias address")
sys.exit(1)
except:
print >> sys.stderr, _("Specify the existing recipient address")
sys.exit(1)
if len(primary_rcpt_address.split('@')) > 1:
primary_rcpt_domain = primary_rcpt_address.split('@')[-1]
else:
primary_rcpt_domain = conf.get('kolab', 'primary_domain')
auth = Auth(domain=primary_rcpt_domain)
domains = auth.list_domains()
#print domains
if len(secondary_rcpt_address.split('@')) > 1:
secondary_rcpt_domain = secondary_rcpt_address.split('@')[-1]
else:
secondary_rcpt_domain = conf.get('kolab', 'primary_domain')
# Check if either is in fact a domain
if not primary_rcpt_domain.lower() in domains.keys():
print >> sys.stderr, _("Domain %r is not a local domain") % (primary_rcpt_domain)
sys.exit(1)
if not secondary_rcpt_domain.lower() in domains.keys():
print >> sys.stderr, _("Domain %r is not a local domain") % (secondary_rcpt_domain)
sys.exit(1)
if not primary_rcpt_domain == secondary_rcpt_domain:
if not domains[primary_rcpt_domain] == domains[secondary_rcpt_domain]:
print >> sys.stderr, _("Primary and secondary domain do not have the same parent domain")
sys.exit(1)
primary_recipient_dn = auth.find_recipient(primary_rcpt_address)
if primary_recipient_dn == [] or len(primary_recipient_dn) == 0:
print >> sys.stderr, _("No such recipient %r") % (primary_rcpt_address)
sys.exit(1)
secondary_recipient_dn = auth.find_recipient(secondary_rcpt_address)
if not secondary_recipient_dn == [] and not len(secondary_recipient_dn) == 0:
print >> sys.stderr, _("Recipient for alias %r already exists") % (secondary_rcpt_address)
sys.exit(1)
rcpt_attrs = conf.get_list('ldap', 'mail_attributes')
primary_rcpt_attr = rcpt_attrs[0]
if len(rcpt_attrs) >= 2:
secondary_rcpt_attr = rcpt_attrs[1]
else:
print >> sys.stderr, _("Environment is not configured for " + \
"users to hold secondary mail attributes")
sys.exit(1)
primary_recipient = auth.get_entry_attributes(primary_rcpt_domain, primary_recipient_dn, rcpt_attrs)
if not primary_recipient.has_key(primary_rcpt_attr):
print >> sys.stderr, _("Recipient %r is not the primary recipient for address %r") % (primary_recipient, primary_rcpt_address)
sys.exit(1)
if not primary_recipient.has_key(secondary_rcpt_attr):
auth.set_entry_attributes(primary_rcpt_domain, primary_recipient_dn, {secondary_rcpt_attr: [ secondary_rcpt_address ] })
else:
if isinstance(primary_recipient[secondary_rcpt_attr], basestring):
new_secondary_rcpt_attrs = [
primary_recipient[secondary_rcpt_attr],
secondary_rcpt_address
]
else:
new_secondary_rcpt_attrs = \
primary_recipient[secondary_rcpt_attr] + \
[ secondary_rcpt_address ]
auth.set_entry_attributes(
primary_rcpt_domain,
primary_recipient_dn,
{
secondary_rcpt_attr: new_secondary_rcpt_attrs
}
)
|