|
1 |
#!/usr/bin/python2.6 |
1 |
#!/usr/bin/python2.6 |
2 |
# -*- coding: utf-8 -*- |
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
# pylint: disable-msg=E0611,W0621,C0103,E1101,W0601 |
3 |
# |
4 |
# |
4 |
# Univention Admin Modules |
5 |
# Univention Admin Modules |
5 |
# easy to create and modify dns objects |
6 |
"""Create and modify dns objects easily.""" |
6 |
# |
7 |
# |
7 |
# Copyright 2004-2012 Univention GmbH |
8 |
# Copyright 2004-2012 Univention GmbH |
8 |
# |
9 |
# |
|
32 |
# <http://www.gnu.org/licenses/>. |
33 |
# <http://www.gnu.org/licenses/>. |
33 |
|
34 |
|
34 |
|
35 |
|
35 |
import os, sys, getopt, codecs, string, time |
36 |
import sys, time |
36 |
import univention.debug |
37 |
from optparse import OptionParser |
37 |
univention.debug.init('/var/log/univention/admin-cmd.log', 1, 1) |
38 |
import univention.debug as ud |
|
|
39 |
ud.init('/var/log/univention/admin-cmd.log', 1, 1) |
38 |
|
40 |
|
39 |
import univention.config_registry |
41 |
import univention.config_registry |
40 |
import univention.admin.uldap |
42 |
import univention.admin.uldap as uldap |
41 |
import univention.admin.config |
43 |
import univention.admin.config |
42 |
import univention.admin.modules |
44 |
import univention.admin.modules |
43 |
import univention.admin.objects |
45 |
import univention.admin.objects |
44 |
import univention.admin.filter |
46 |
import univention.admin.filter |
45 |
import univention.admin.handlers.dns.forward_zone |
47 |
import univention.admin.handlers.dns.forward_zone as forward_zone |
46 |
import univention.admin.handlers.dns.reverse_zone |
48 |
import univention.admin.handlers.dns.reverse_zone as reverse_zone |
47 |
import univention.admin.handlers.dns.host_record |
49 |
import univention.admin.handlers.dns.host_record as host_record |
48 |
import univention.admin.handlers.dns.txt_record |
50 |
import univention.admin.handlers.dns.txt_record as txt_record |
49 |
import univention.admin.handlers.dns.ptr_record |
51 |
import univention.admin.handlers.dns.ptr_record as ptr_record |
50 |
import univention.admin.handlers.dns.srv_record |
52 |
import univention.admin.handlers.dns.srv_record as srv_record |
51 |
import univention.admin.handlers.dns.alias |
53 |
import univention.admin.handlers.dns.alias as alias |
52 |
import univention.admin.uexceptions |
54 |
import univention.admin.uexceptions |
53 |
|
55 |
|
54 |
options={} |
56 |
|
55 |
options['ignore-missing-zone']=0 |
57 |
def parse(): |
56 |
options['ignore-exists']=0 |
58 |
"""Parse command line.""" |
57 |
options['quiet']=0 |
59 |
usage = 'Usage: %prog zone command type [args]' |
58 |
options['reverse']=0 |
60 |
description = ''' |
59 |
options['overwrite']=0 |
61 |
%prog my.dns.zone add srv ldap tcp 0 100 7389 master.fqdn |
60 |
options['stoptls']=0 |
62 |
%prog my.dns.zone remove srv ldap tcp 0 100 7389 master.fqdn |
61 |
options['binddn']=0 |
63 |
%prog my.dns.zone add txt entry-name "Some text" |
62 |
options['bindpwd']=0 |
64 |
%prog 192.168.122 add ptr 42 host.fqdn --reverse |
63 |
options['help']=0 |
65 |
%prog my.dns.zone add cname univention-repository apt.knut.univention.de. |
64 |
|
66 |
%prog my.dns.zone add a host 1.2.3.4 5.6.7.8 |
65 |
def usage(): |
67 |
%prog my.dns.zone add zone root@fqdn 1 28800 7200 25200 10800 ns.fqdn |
66 |
print >>sys.stderr, 'Usage: %s command [args]' % sys.argv[0] |
68 |
''' # FIXME: OptionParser reformats this |
67 |
|
69 |
global parser |
68 |
def help(): |
70 |
parser = OptionParser(usage=usage, description=description) |
69 |
print >>sys.stderr,"Valid Commands are:" |
71 |
parser.add_option('--ignore-missing-zone', |
70 |
for elements in options.keys(): |
72 |
action='store_true', dest='ignore_missing_zone', |
71 |
print >>sys.stderr,elements |
73 |
help='Skip if zone does not exist') |
72 |
|
74 |
parser.add_option('--ignore-exists', |
73 |
for elements in sys.argv[1:]: |
75 |
action='store_true', dest='ignore_exists', |
74 |
if elements.startswith('--'): |
76 |
help='Skip if entry already exists') |
75 |
elements=elements.replace('--','') |
77 |
parser.add_option('--quiet', |
76 |
if elements not in options.keys(): |
78 |
action='store_true', dest='quiet', |
77 |
err = '%s: Unknown Command %s' % (sys.argv[0], elements) |
79 |
help='Turn off verbose messages') # not implemented |
78 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, err) |
80 |
parser.add_option('--reverse', |
79 |
print err |
81 |
action='store_true', dest='reverse', |
80 |
sys.exit(1) |
82 |
help='Modify revers zone instead of forward zone') |
81 |
|
83 |
parser.add_option('--overwrite', |
82 |
opts, args=getopt.getopt(sys.argv[1:], '', ['ignore-missing-zone', 'quiet', 'reverse', 'ignore-exists', 'stoptls', 'binddn=', 'bindpwd=', 'overwrite','help']) |
84 |
action='store_true', dest='overwrite', |
83 |
|
85 |
help='Overwrite exising record') |
84 |
|
86 |
parser.add_option('--stoptls', |
85 |
for opt, val in opts: |
87 |
action='store_true', dest='stoptls', |
86 |
if opt == '--ignore-missing-zone': |
88 |
help='Disable TLS') |
87 |
options['ignore-missing-zone']=1 |
89 |
parser.add_option('--binddn', |
88 |
elif opt == '--ignore-exists': |
90 |
action='store', dest='binddn', |
89 |
options['ignore-exists']=1 |
91 |
help='bind DN') |
90 |
elif opt == '--quiet': |
92 |
parser.add_option('--bindpwd', |
91 |
options['quiet']=1 |
93 |
action='store', dest='bindpwd', |
92 |
elif opt == '--reverse': |
94 |
help='bind password') |
93 |
options['reverse']=1 |
95 |
|
94 |
elif opt == '--stoptls': |
96 |
options, arguments = parser.parse_args() |
95 |
options['stoptls']=1 |
97 |
msg = None |
96 |
elif opt == '--overwrite': |
98 |
if options.binddn and not options.bindpwd: |
97 |
options['overwrite']=1 |
99 |
msg = 'authentication error: missing --bindpwd' |
98 |
elif opt == '--binddn': |
100 |
elif not options.binddn and options.bindpwd: |
99 |
options['binddn']=val |
101 |
msg = 'authentication error: missing --binddn' |
100 |
elif opt == '--bindpwd': |
102 |
if msg: |
101 |
options['bindpwd']=val |
103 |
ud.debug(ud.ADMIN, ud.ERROR, msg) |
102 |
elif opt == '--help': |
104 |
parser.error(msg) |
103 |
help() |
105 |
|
104 |
sys.exit(1) |
106 |
try: |
105 |
|
107 |
zone_name, command, typ = arguments[0:3] |
106 |
co=None |
108 |
except ValueError: |
107 |
lo=None |
109 |
parser.error('Missing command or value. See --help.') |
108 |
position=None |
110 |
|
109 |
if options['stoptls']: |
111 |
return options, zone_name, command, typ, arguments[3:] |
110 |
start_tls=0 |
112 |
|
111 |
else: |
113 |
|
112 |
start_tls=2 |
|
|
113 |
|
114 |
configRegistry=univention.config_registry.ConfigRegistry() |
115 |
configRegistry.load() |
116 |
|
117 |
timeout=time.time() + 120 # 2 minutes from now |
118 |
def bind(): |
114 |
def bind(): |
119 |
if time.time() > timeout: |
115 |
"""Bind to LDAP service.""" |
120 |
err = '%s: LDAP server %s unreachable, aborting' % (sys.argv[0], configRegistry['ldap/master']) |
116 |
ucr = univention.config_registry.ConfigRegistry() |
121 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, err) |
117 |
ucr.load() |
122 |
print err |
118 |
timeout = time.time() + 120 # 2 minutes from now |
123 |
sys.exit(1) |
119 |
if time.time() > timeout: |
124 |
if options['binddn'] and options['bindpwd']: |
120 |
err = '%s: LDAP server %s unreachable, aborting' % \ |
125 |
try: |
121 |
(sys.argv[0], ucr['ldap/master']) |
126 |
lo=univention.admin.uldap.access(host=configRegistry['ldap/master'], port=int(configRegistry.get('ldap/master/port', '7389')), base=configRegistry['ldap/base'], binddn=options['binddn'], bindpw=options['bindpwd'], start_tls=start_tls) |
122 |
ud.debug(ud.ADMIN, ud.WARN, err) |
127 |
position=univention.admin.uldap.position(lo.base) |
123 |
print >> sys.stderr, err |
128 |
return lo, position |
124 |
sys.exit(1) |
129 |
except univention.admin.uexceptions.authFail, e: |
125 |
if options.stoptls: |
130 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) |
126 |
start_tls = 0 |
131 |
print 'authentication error: %s' % str(e) |
127 |
else: |
132 |
sys.exit(1) |
128 |
start_tls = 2 |
133 |
except univention.admin.uexceptions.ldapError, e: |
129 |
try: |
134 |
msg = '%s: timeout while trying to contact LDAP server %s' % (sys.argv[0], configRegistry['ldap/master']) |
130 |
if options.binddn and options.bindpwd: |
135 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, msg) |
131 |
lo = uldap.access( |
136 |
print msg |
132 |
host=ucr['ldap/master'], |
137 |
time.sleep(10) |
133 |
port=int(ucr.get('ldap/master/port', '7389')), |
138 |
lo, position = bind() |
134 |
base=ucr['ldap/base'], |
139 |
return lo, position |
135 |
binddn=options.binddn, |
140 |
except univention.admin.uldap.ldap.LDAPError, e: |
136 |
bindpw=options.bindpwd, |
141 |
msg = '%s: timeout while trying to contact LDAP server %s' % (sys.argv[0], configRegistry['ldap/master']) |
137 |
start_tls=start_tls) |
142 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, msg) |
138 |
position = uldap.position(lo.base) |
143 |
print msg |
139 |
else: |
144 |
time.sleep(10) |
140 |
lo, position = uldap.getAdminConnection(start_tls) |
145 |
lo, position = bind() |
141 |
return lo, position |
146 |
return lo, position |
142 |
except univention.admin.uexceptions.authFail, ex: |
147 |
elif not options['binddn'] and not options['bindpwd']: |
143 |
msg = 'authentication error: %s' % (ex,) |
148 |
try: |
144 |
ud.debug(ud.ADMIN, ud.WARN, msg) |
149 |
lo, position=univention.admin.uldap.getAdminConnection(start_tls) |
145 |
print >> sys.stderr, msg |
150 |
return lo, position |
146 |
sys.exit(1) |
151 |
except univention.admin.uexceptions.authFail, e: |
147 |
except (univention.admin.uexceptions.ldapError, |
152 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: %s' % str(e)) |
148 |
uldap.ldap.LDAPError): |
153 |
print 'authentication error: %s' % str(e) |
149 |
msg = '%s: timeout while trying to contact LDAP server %s' % \ |
154 |
sys.exit(1) |
150 |
(sys.argv[0], ucr['ldap/master']) |
155 |
except univention.admin.uexceptions.ldapError, e: |
151 |
ud.debug(ud.ADMIN, ud.WARN, msg) |
156 |
msg = '%s: timeout while trying to contact LDAP server %s' % (sys.argv[0], configRegistry['ldap/master']) |
152 |
print >> sys.stderr, msg |
157 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, msg) |
153 |
time.sleep(10) |
158 |
print msg |
154 |
return bind() # recursion |
159 |
time.sleep(10) |
155 |
|
160 |
lo, position = bind() |
|
|
161 |
return lo, position |
162 |
except univention.admin.uldap.ldap.LDAPError, e: |
163 |
msg = '%s: timeout while trying to contact LDAP server %s' % (sys.argv[0], configRegistry['ldap/master']) |
164 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, msg) |
165 |
print msg |
166 |
time.sleep(10) |
167 |
lo, position = bind() |
168 |
return lo, position |
169 |
elif options['binddn']: |
170 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: missing --bindpwd') |
171 |
print 'authentication error: missing --bindpwd' |
172 |
sys.exit(1) |
173 |
else: # options['bindpwd'] |
174 |
univention.debug.debug(univention.debug.ADMIN, univention.debug.WARN, 'authentication error: missing --binddn') |
175 |
print 'authentication error: missing --binddn' |
176 |
sys.exit(1) |
177 |
lo, position = bind() |
178 |
|
156 |
|
179 |
def lookup_zone(zone_name): |
157 |
def lookup_zone(zone_name): |
180 |
if not options['reverse']: |
158 |
"""Lookup zone and return UDM object.""" |
181 |
zones=univention.admin.handlers.dns.forward_zone.lookup(co, lo, '(zone=%s)' % zone_name, scope='domain', base=position.getDomain(), unique=1) |
159 |
if not options.reverse: |
182 |
else: |
160 |
zones = forward_zone.lookup(co, lo, '(zone=%s)' % (zone_name,), |
183 |
zones=univention.admin.handlers.dns.reverse_zone.lookup(co, lo, '(subnet=%s)' % zone_name, scope='domain', base=position.getDomain(), unique=1) |
161 |
scope='domain', base=position.getDomain(), unique=1) |
184 |
if not zones: |
162 |
else: |
185 |
if options['ignore-missing-zone']: |
163 |
zones = reverse_zone.lookup(co, lo, '(subnet=%s)' % (zone_name,), |
186 |
sys.exit(0) |
164 |
scope='domain', base=position.getDomain(), unique=1) |
187 |
else: |
165 |
if not zones: |
188 |
print >>sys.stderr, 'E: Zone %s does not exist.' % zone_name |
166 |
if options.ignore_missing_zone: |
189 |
sys.exit(1) |
167 |
sys.exit(0) |
190 |
return zones[0] |
168 |
else: |
191 |
|
169 |
print >> sys.stderr, 'E: Zone %s does not exist.' % (zone_name,) |
192 |
try: |
170 |
sys.exit(1) |
193 |
zone_name=args[0] |
171 |
return zones[0] |
194 |
command=args[1] |
172 |
|
195 |
type=args[2] |
173 |
|
196 |
except IndexError: |
174 |
def add_srv_record(service, protocol, priority, weight, port, host): |
197 |
print >>sys.stderr, 'E: Missing command or value. Use --help for further Information' |
175 |
"""Add DNS service record.""" |
198 |
sys.exit(1) |
176 |
name = [service, protocol] |
199 |
|
177 |
location = [priority, weight, port, host] |
200 |
if type != 'zone' or command == 'remove': |
178 |
filt = univention.admin.filter.expression('name', name) |
201 |
zone=lookup_zone(zone_name) |
179 |
|
202 |
position.setDn(zone.dn) |
180 |
records = srv_record.lookup(co, lo, filt, scope='domain', |
203 |
else: |
181 |
base=position.getDomain(), superordinate=zone, unique=1) |
204 |
position.setDn(univention.admin.config.getDefaultContainer(lo, 'dns/')) |
182 |
if records: |
205 |
|
183 |
record = records[0] |
206 |
def add_srv_record(args): |
184 |
else: |
207 |
service=args[0] |
185 |
record = srv_record.object(co, lo, position, superordinate=zone) |
208 |
protocol=args[1] |
186 |
record['name'] = name |
209 |
location=args[2:6] |
187 |
|
210 |
|
188 |
loc = record['location'] |
211 |
name=[service, protocol] |
189 |
if location in loc: |
212 |
filter=univention.admin.filter.expression('name', name) |
190 |
return |
213 |
|
191 |
loc.append(location) |
214 |
records=univention.admin.handlers.dns.srv_record.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
192 |
record['location'] = loc |
215 |
if records: |
193 |
|
216 |
record=records[0] |
194 |
if records: |
217 |
else: |
195 |
record.modify() |
218 |
record=univention.admin.handlers.dns.srv_record.object(co, lo, position, superordinate=zone) |
196 |
else: |
219 |
record['name']=name |
197 |
record.create() |
220 |
|
198 |
|
221 |
l=record['location'] |
199 |
|
222 |
if location in l: |
200 |
def remove_srv_record(service, protocol, priority, weight, port, host): |
223 |
return |
201 |
"""Remove DNS service record.""" |
224 |
l.append(location) |
202 |
name = [service, protocol] |
225 |
record['location']=l |
203 |
location = [priority, weight, port, host] |
226 |
|
204 |
filt = univention.admin.filter.expression('name', name) |
227 |
if records: |
205 |
|
228 |
record.modify() |
206 |
records = srv_record.lookup(co, lo, filt, scope='domain', |
229 |
else: |
207 |
base=position.getDomain(), superordinate=zone, unique=1) |
230 |
record.create() |
208 |
if records: |
231 |
|
209 |
record = records[0] |
232 |
def remove_srv_record(args): |
210 |
else: |
233 |
service=args[0] |
211 |
record = srv_record.object(co, lo, position, superordinate=zone) |
234 |
protocol=args[1] |
212 |
record['name'] = name |
235 |
location=args[2:6] |
213 |
|
236 |
|
214 |
loc = record['location'] |
237 |
name=[service, protocol] |
215 |
if location in loc: |
238 |
filter=univention.admin.filter.expression('name', name) |
216 |
loc.remove(location) |
239 |
|
217 |
record['location'] = loc |
240 |
records=univention.admin.handlers.dns.srv_record.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
218 |
|
241 |
if records: |
219 |
if records: |
242 |
record=records[0] |
220 |
record.modify() |
243 |
else: |
221 |
|
244 |
record=univention.admin.handlers.dns.srv_record.object(co, lo, position, superordinate=zone) |
222 |
|
245 |
record['name']=name |
223 |
def add_txt_record(name, text): |
246 |
|
224 |
"""Add DNS text record.""" |
247 |
l=record['location'] |
225 |
filt = univention.admin.filter.expression('name', name) |
248 |
if location in l: |
226 |
records = txt_record.lookup(co, lo, filt, scope='domain', |
249 |
l.remove(location) |
227 |
base=position.getDomain(), superordinate=zone, unique=1) |
250 |
record['location']=l |
228 |
if records: |
251 |
|
229 |
record = records[0] |
252 |
if records: |
230 |
else: |
253 |
record.modify() |
231 |
record = txt_record.object(co, lo, position, superordinate=zone) |
254 |
|
232 |
record['name'] = name |
255 |
def add_txt_record(args): |
233 |
record['zonettl'] = ['80600'] |
256 |
name=args[0] |
234 |
|
257 |
text=args[1] |
235 |
tmp = record['txt'] |
258 |
|
236 |
if text in tmp: |
259 |
filter=univention.admin.filter.expression('name', name) |
237 |
return |
260 |
records=univention.admin.handlers.dns.txt_record.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
238 |
tmp.append(text) |
261 |
if records: |
239 |
record['txt'] = tmp |
262 |
record=records[0] |
240 |
|
263 |
else: |
241 |
if records: |
264 |
record=univention.admin.handlers.dns.txt_record.object(co, lo, position, superordinate=zone) |
242 |
record.modify() |
265 |
record['name']=name |
243 |
else: |
266 |
record['zonettl']=['80600'] |
244 |
record.create() |
267 |
|
245 |
|
268 |
t=record['txt'] |
246 |
|
269 |
if text in t: |
247 |
def add_a_record(name, *adresses): |
270 |
return |
248 |
"""Add DNS IPv4 address records.""" |
271 |
t.append(text) |
249 |
filt = univention.admin.filter.expression('name', name) |
272 |
record['txt']=t |
250 |
records = host_record.lookup(co, lo, filt, scope='domain', |
273 |
|
251 |
base=position.getDomain(), superordinate=zone, unique=1) |
274 |
if records: |
252 |
if records: |
275 |
record.modify() |
253 |
record = records[0] |
276 |
else: |
254 |
else: |
277 |
record.create() |
255 |
record = host_record.object(co, lo, position, superordinate=zone) |
278 |
|
256 |
record['name'] = name |
279 |
def add_a_record(args): |
257 |
record['zonettl'] = ['80600'] |
280 |
name=args[0] |
258 |
|
281 |
a=args[1:] |
259 |
tmp = record['a'] |
282 |
|
260 |
for addr in adresses: |
283 |
filter=univention.admin.filter.expression('name', name) |
261 |
if not addr in tmp: |
284 |
records=univention.admin.handlers.dns.host_record.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
262 |
tmp.append(addr) |
285 |
if records: |
263 |
record['a'] = tmp |
286 |
record=records[0] |
264 |
|
287 |
else: |
265 |
if records: |
288 |
record=univention.admin.handlers.dns.host_record.object(co, lo, position, superordinate=zone) |
266 |
record.modify() |
289 |
record['name']=name |
267 |
else: |
290 |
record['zonettl']=['80600'] |
268 |
record.create() |
291 |
|
269 |
|
292 |
t=record['a'] |
270 |
|
293 |
for i in a: |
271 |
def add_cname_record(name, cname): |
294 |
if not i in t: |
272 |
"""Add DNS canonical name record.""" |
295 |
t.append(i) |
273 |
filt = univention.admin.filter.expression('name', name) |
296 |
record['a']=t |
274 |
records = alias.lookup(co, lo, filt, scope='domain', |
297 |
|
275 |
base=position.getDomain(), superordinate=zone, unique=1) |
298 |
if records: |
276 |
if records: |
299 |
record.modify() |
277 |
record = records[0] |
300 |
else: |
278 |
else: |
301 |
record.create() |
279 |
record = alias.object(co, lo, position, superordinate=zone) |
302 |
|
280 |
record['name'] = name |
303 |
|
281 |
record['zonettl'] = ['80600'] |
304 |
def add_cname_record(args): |
282 |
|
305 |
name=args[0] |
283 |
if record['cname'] and not (record['cname'] == cname or |
306 |
cname=args[1] |
284 |
options.overwrite or options.ignore_exists): |
307 |
|
285 |
print >> sys.stderr, 'E: Record exists and points to different address' |
308 |
filter=univention.admin.filter.expression('name', name) |
286 |
sys.exit(1) |
309 |
records=univention.admin.handlers.dns.alias.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
287 |
if record['cname'] == cname: |
310 |
if records: |
288 |
return |
311 |
record=records[0] |
289 |
record['cname'] = cname |
312 |
else: |
290 |
|
313 |
record=univention.admin.handlers.dns.alias.object(co, lo, position, superordinate=zone) |
291 |
if records: |
314 |
record['name']=name |
292 |
record.modify() |
315 |
record['zonettl']=['80600'] |
293 |
else: |
316 |
|
294 |
record.create() |
317 |
if record['cname'] and not record['cname'] == cname and not options['overwrite'] and not options['ignore-exists']: |
295 |
|
318 |
print >>sys.stderr, 'E: Record exists and points to different address' |
296 |
|
319 |
sys.exit(1) |
297 |
def add_ptr_record(address, ptr): |
320 |
if record['cname'] == cname: |
298 |
"""Add DNS pointer record.""" |
321 |
return |
299 |
filt = univention.admin.filter.expression('address', address) |
322 |
record['cname']=cname |
300 |
records = ptr_record.lookup(co, lo, filt, scope='domain', |
323 |
|
301 |
base=position.getDomain(), superordinate=zone, unique=1) |
324 |
if records: |
302 |
if records: |
325 |
record.modify() |
303 |
record = records[0] |
326 |
else: |
304 |
else: |
327 |
record.create() |
305 |
record = ptr_record.object(co, lo, position, superordinate=zone) |
328 |
|
306 |
record['address'] = address |
329 |
|
307 |
|
330 |
def add_ptr_record(args): |
308 |
if record['ptr_record'] and not (record['ptr_record'] == ptr or |
331 |
address=args[0] |
309 |
options.overwrite): |
332 |
ptr=args[1] |
310 |
print >> sys.stderr, 'E: Record exists and points to different address' |
333 |
|
311 |
sys.exit(1) |
334 |
filter=univention.admin.filter.expression('address', address) |
312 |
if record['ptr_record'] == ptr: |
335 |
records=univention.admin.handlers.dns.ptr_record.lookup(co, lo, filter, scope='domain', base=position.getDomain(), superordinate=zone, unique=1) |
313 |
return |
336 |
if records: |
314 |
record['ptr_record'] = ptr |
337 |
record=records[0] |
315 |
|
338 |
else: |
316 |
if records: |
339 |
record=univention.admin.handlers.dns.ptr_record.object(co, lo, position, superordinate=zone) |
317 |
record.modify() |
340 |
record['address']=address |
318 |
else: |
341 |
|
319 |
record.create() |
342 |
if record['ptr_record'] and not record['ptr_record'] == ptr and not options['overwrite']: |
320 |
|
343 |
print >>sys.stderr, 'E: Record exists and points to different address' |
321 |
|
344 |
sys.exit(1) |
322 |
def add_zone(contact, serial, refresh, retry, expire, ttl, *nameserver): |
345 |
if record['ptr_record'] == ptr: |
323 |
"""Add DNS zone.""" |
346 |
return |
324 |
if not options.reverse: |
347 |
record['ptr_record']=ptr |
325 |
zone = forward_zone.object(co, lo, position) |
348 |
|
326 |
zone['zone'] = zone_name |
349 |
if records: |
327 |
else: |
350 |
record.modify() |
328 |
zone = reverse_zone.object(co, lo, position) |
351 |
else: |
329 |
zone['subnet'] = zone_name |
352 |
record.create() |
330 |
zone['contact'] = contact |
353 |
|
331 |
zone['serial'] = serial |
354 |
def add_zone(args): |
332 |
zone['refresh'] = [refresh] |
355 |
contact, serial, refresh, retry, expire, ttl=args[0:6] |
333 |
zone['retry'] = [retry] |
356 |
nameserver=args[6:] |
334 |
zone['expire'] = [expire] |
357 |
|
335 |
zone['ttl'] = [ttl] |
358 |
if not options['reverse']: |
336 |
zone['nameserver'] = list(nameserver) |
359 |
zone=univention.admin.handlers.dns.forward_zone.object(co, lo, position) |
337 |
zone.create() |
360 |
zone['zone']=zone_name |
338 |
|
361 |
else: |
339 |
|
362 |
zone=univention.admin.handlers.dns.reverse_zone.object(co, lo, position) |
340 |
def main(): |
363 |
zone['subnet']=zone_name |
341 |
"""Run DNS edit.""" |
364 |
zone['contact']=contact |
342 |
global options |
365 |
zone['serial']=serial |
343 |
global zone_name |
366 |
zone['refresh']=[refresh] |
344 |
options, zone_name, command, typ, args = parse() |
367 |
zone['retry']=[retry] |
345 |
global co |
368 |
zone['expire']=[expire] |
346 |
co = None |
369 |
zone['ttl']=[ttl] |
347 |
global lo |
370 |
zone['nameserver']=nameserver |
348 |
global position |
371 |
zone.create() |
349 |
lo, position = bind() |
372 |
|
350 |
|
373 |
if command == 'add': |
351 |
if typ != 'zone' or command == 'remove': |
374 |
nargs=args[3:] |
352 |
global zone |
375 |
|
353 |
zone = lookup_zone(zone_name) |
376 |
print 'Adding %s record "%s" to zone %s...' % (type.upper(), string.join(nargs, ' '), zone_name), |
354 |
position.setDn(zone.dn) |
377 |
|
355 |
else: |
378 |
try: |
356 |
position.setDn(univention.admin.config.getDefaultContainer(lo, 'dns/')) |
379 |
if type == 'srv': |
357 |
|
380 |
add_srv_record(nargs) |
358 |
try: |
381 |
elif type == 'txt': |
359 |
if command == 'add': |
382 |
add_txt_record(nargs) |
360 |
print 'Adding %s record "%s" to zone %s...' % \ |
383 |
elif type == 'ptr' and options['reverse']: |
361 |
(typ.upper(), ' '.join(args), zone_name) |
384 |
add_ptr_record(nargs) |
362 |
if typ == 'srv': |
385 |
elif type == 'cname': |
363 |
add_srv_record(*args) |
386 |
add_cname_record(nargs) |
364 |
elif typ == 'txt': |
387 |
elif type == 'a': |
365 |
add_txt_record(*args) |
388 |
add_a_record(nargs) |
366 |
elif typ == 'ptr' and options.reverse: |
389 |
elif type == 'zone': |
367 |
add_ptr_record(*args) |
390 |
add_zone(nargs) |
368 |
elif typ == 'cname': |
391 |
except univention.admin.uexceptions.objectExists: |
369 |
add_cname_record(*args) |
392 |
if not options['ignore-exists']: |
370 |
elif typ == 'a': |
393 |
print >>sys.stderr, 'E: Object exists' |
371 |
add_a_record(*args) |
394 |
sys.exit(1) |
372 |
elif typ == 'zone': |
395 |
except ValueError,e: |
373 |
add_zone(*args) |
396 |
print 'failed' |
374 |
else: |
397 |
print 'E: %s'%e |
375 |
parser.error('Unknown type "%s"' % (typ,)) |
398 |
sys.exit(1) |
376 |
print 'done' |
399 |
except univention.admin.uexceptions.valueInvalidSyntax,err: |
377 |
elif command == 'remove': |
400 |
print 'failed' |
378 |
if typ == 'srv': |
401 |
print 'E: %s'%err |
379 |
remove_srv_record(*args) |
402 |
sys.exit(1) |
380 |
else: |
403 |
|
381 |
parser.error('Unknown type "%s"' % (typ,)) |
404 |
print 'done' |
382 |
else: |
405 |
elif command == 'remove': |
383 |
parser.error('E: No valid command "%s". See --help.' % (command,)) |
406 |
nargs=args[3:] |
384 |
except univention.admin.uexceptions.objectExists, ex: |
407 |
if type == 'srv': |
385 |
if not options.ignore_exists: |
408 |
remove_srv_record(nargs) |
386 |
print >> sys.stderr, 'E: Object "%s" exists' % (ex,) |
409 |
pass |
387 |
sys.exit(1) |
410 |
else: |
388 |
except (ValueError, TypeError, |
411 |
print >>sys.stderr, 'E: No valid command found. Use --help for further information' |
389 |
univention.admin.uexceptions.valueInvalidSyntax, |
412 |
sys.exit(1) |
390 |
univention.admin.uexceptions.valueRequired), ex: |
|
|
391 |
print >> sys.stderr, 'E: failed %s' % (ex,) |
392 |
sys.exit(1) |
393 |
|
394 |
|
395 |
if __name__ == '__main__': |
396 |
main() |