1#!%TCLSH%
2
3#
4# Delete host, IP address or alias
5#
6# Called by: suppr
7#
8# Parameters (form or url):
9#   - display page
10#   - name or IP address removal
11#	- action : "del"
12#	- name & domain : name and domain of object to remove
13#	- addr : IP address to remove or FQDN
14#		(addr is exclusive with name & domain)
15#	- idviews : selected (after confirmation) views
16#
17# Note:
18#   If script is called for the first time, addr & name & domain are
19#	empty, so the script display the selection page
20#   When user inputs either a name&domain, or an addr (which may also
21#	be a fqdn), all found objects (in all views) are presented
22#	for confirmation
23#   When some objets are selected (idviews is not empty), removal is
24#	performed
25#
26# History
27#   2002/04/11 : pda/jean : design
28#   2002/05/03 : pda/jean : split 3 modification types
29#   2002/07/09 : pda      : add nologin
30#   2003/05/13 : pda/jean : use auth base
31#   2004/01/14 : pda/jean : add IPv6
32#   2004/08/05 : pda/jean : add mac
33#   2005/04/08 : pda/jean : add dhcpprofile
34#   2007/10/25 : jean     : log modify actions
35#   2008/07/24 : pda/jean : add sendsmtp
36#   2008/07/29 : pda      : use display-rr
37#   2010/10/15 : pda      : add journey
38#   2010/12/14 : pda      : i18n
39#   2010/12/25 : pda      : use cgi-dispatch
40#   2012/11/13 : pda/jean : add views
41#   2012/11/14 : pda/jean : merge name and ip cases
42#
43
44#
45# Template pages used by this script
46#
47
48set conf(page-sel)	del-sel.html
49set conf(page-confirm)	del-confirm.html
50set conf(page-ok)	del-ok.html
51
52#
53# Next actions
54#
55
56set conf(next)		"del"
57set conf(nextmap)	"net"
58set conf(nextlist)	"net"
59set conf(nextsearch)	"search"
60set conf(nextipinact)	"ipinact"
61
62#
63# Script parameters
64#
65
66set conf(tab-confirm) {
67    global {
68	chars {10 normal}
69	columns {5 10 85}
70	align {left}
71	botbar {no}
72    }
73    pattern Line {
74	vbar {no}
75	column {
76	    format {raw}
77	}
78	vbar {no}
79	column {
80	    format {raw}
81	}
82	vbar {no}
83	column {
84	    format {raw}
85	}
86	vbar {no}
87    }
88}
89
90#
91# Netmagis general library
92#
93
94source %LIBNETMAGIS%
95
96# ::webapp::cgidebug ; exit
97
98##############################################################################
99# Ask for confirmation
100##############################################################################
101
102# mode : "addr" or "host-or-alias"
103# object : ip address for "addr" mode, or fqdn for "host-or-alias" mode
104
105proc confirm {dbfd mode object _chkv} {
106    global conf
107    upvar $_chkv chkv
108
109    set idviews $chkv(idviews)
110    set nok [llength $chkv(ok)]
111
112    # display checkbutton for view only if there is more than a single view
113    set dispconfview [expr $nok > 1]
114
115    # terrible hack
116    set empty [::webapp::helem "span" "" "style" "display:none"]
117
118    set lines {}
119    foreach idview $idviews {
120	lassign $chkv($idview) vn msg t
121
122	set buttid "bv$idview"
123	set desc $empty
124
125	array unset trr
126	array set trr $t
127
128	if {$msg ne ""} then {
129	    #
130	    # Error message
131	    #
132	    set check ""
133	    set host [::webapp::html-string $msg]
134	} else {
135	    #
136	    # Get host information
137	    #
138	    lassign [display-rr-masked $dbfd trr $idview {}] link desc
139
140	    if {$mode eq "host-or-alias"} then {
141		#
142		# Information about host found
143		#
144
145		set cname [rr-cname-by-view trr $idview]
146		if {$cname eq ""} then {
147		    # This is not an alias
148		    set host [mc "Remove host '%s'" $link]
149		} else {
150		    # This is an alias, display informations about target host
151		    set host [mc "Remove alias '%s'" $link]
152		}
153	    } else {
154		#
155		# Information about IP address found.
156		# Is it the last IP address?
157		#
158
159		set lip [rr-ip-by-view trr $idview]
160		if {[llength $lip] == 1} then {
161		    set host [mc "Remove host '%s'" $link]
162		} else {
163		    set host [mc {Remove IP address '%1$s' from host '%2$s'} $object $link]
164		}
165	    }
166
167	    #
168	    # View name and check box
169	    #
170
171	    if {$dispconfview} then {
172		set c ""
173	    } else {
174		set c " CHECKED"
175	    }
176	    set check "<INPUT TYPE=CHECKBOX NAME=\"idviews\" ID=\"$buttid\" VALUE=\"$idview\" $c>"
177
178	    set vn [::webapp::helem "label" $vn "for" $buttid]
179	}
180	lappend lines [list Line $check $vn $host]
181	lappend lines [list Line $empty $empty $desc]
182    }
183
184    set seldel [::arrgen::output "html" $conf(tab-confirm) $lines]
185
186    #
187    # Other parameters
188    #
189
190    set hidden [::webapp::form-hidden "addr" $object]
191
192    #
193    # Confirmation needed
194    #
195
196    d urlset "%URLFORM%" $conf(next) {}
197    d urladdnext "%URLFORM%"
198
199    d result $conf(page-confirm) [list \
200					[list %OBJECT% $object] \
201					[list %SELDEL% $seldel] \
202					[list %HIDDEN% $hidden] \
203				    ]
204}
205
206##############################################################################
207# Process removal
208##############################################################################
209
210# mode : "addr" or "host-or-alias"
211# object : ip address for "addr" mode, or fqdn for "host-or-alias" mode
212
213proc remove {dbfd mode object _chkv} {
214    global conf
215    upvar $_chkv chkv
216
217    set idviews $chkv(idviews)
218
219    #
220    # Confirmation is accepted. All view ids have been checked.
221    # Do the removal.
222    #
223
224    d dblock {}
225
226    set result {}
227    foreach idview $idviews {
228	lassign $chkv($idview) vn msg t
229
230	array unset trr
231	array set trr $t
232
233	if {$mode eq "host-or-alias"} then {
234	    set delobj $object
235	    set msg [del-host $dbfd trr $idview]
236	    if {$msg ne ""} then {
237		d dbabort [mc "delete %s" $object/$vn] $msg
238	    }
239	} else {
240	    set msg [del-ip $dbfd $object trr $idview delobj]
241	    if {$msg ne ""} then {
242		d dbabort [mc "delete %s" $object/$vn] $msg
243	    }
244	}
245	lappend result [mc {'%1$s' has been removed from view '%2$s'} $delobj $vn]
246    }
247
248    d dbcommit [mc "delete %s" $object]
249
250    #
251    # Prepare next step in journey
252    #
253
254    switch -- [d nextprog] {
255	search {
256	    d urlset "%URLSUITE%" $conf(nextsearch) [list [d nextargs]]
257	}
258	map {
259	    d urlset "%URLSUITE%" $conf(nextmap) [list {domap {yes}} [d nextargs]]
260	}
261	list {
262	    d urlset "%URLSUITE%" $conf(nextlist) [list {dolist {yes}} [d nextargs]]
263	}
264	ipinact {
265	    d urlset "%URLSUITE%" $conf(nextipinact) [list [d nextargs]]
266	}
267	default {
268	    d urlset "%URLSUITE%" $conf(next) {}
269	}
270    }
271
272    #
273    # End of script: output page and close database
274    #
275
276    set result [join $result "<br>"]
277
278    d result $conf(page-ok) [list \
279				    [list %RESULT% $result] \
280			]
281}
282
283##############################################################################
284# CGI script
285##############################################################################
286
287# History
288#   2002/04/19 : pda/jean : design
289#
290
291d cgi-register {} {
292    {addr	0 1}
293    {name	0 1}
294    {domain	0 1}
295    {idviews	0 9999}
296} {
297    global conf
298
299    #
300    # Display the selection page if no parameter is given
301    #
302
303    if {$addr eq "" && $name eq "" && $domain eq ""} then {
304	#
305	# Get domains authorized for the user
306	#
307
308	set domain [menu-domain $dbfd $tabuid(idcor) "domain" "" ""]
309
310	#
311	# End of script: output page and close database
312	#
313
314	d urlset "%URLFORM%" $conf(next) {}
315	d result $conf(page-sel) [list \
316				    [list %DOMAIN% $domain] \
317				]
318	return 0
319    }
320
321    #
322    # Addr and name/iddom are mutually exclusive
323    #
324
325    if {$addr ne "" && ($name ne "" || $domain ne "")} then {
326	d error [mc "Invalid input"]
327    }
328
329    #
330    # Check wether it is an address (which may be a FQDN) or name+domain
331    #
332
333    if {$addr ne ""} then {
334	set object $addr
335	set msg [check-ip-syntax $dbfd $addr "inet"]
336	if {$msg ne ""} then {
337	    #
338	    # This is not an IP address. It is perhaps a FQDN
339	    #
340	    set msg [check-fqdn-syntax $dbfd $addr name domain]
341	    if {$msg ne ""} then {
342		d error $msg
343	    }
344	    set addr ""
345	} else {
346	    #
347	    # This is an IP address. Check it.
348	    #
349	    if {! [check-authorized-ip $dbfd $tabuid(idcor) $addr]} then {
350		d error [mc "You don't have rights on '%s'" $addr]
351	    }
352	}
353    } else {
354	# Check that name is not empty, or wrong domain
355	set msg [check-fqdn-syntax $dbfd "$name.$domain" name domain]
356	if {$msg ne ""} then {
357	    d error $msg
358	}
359    }
360
361    #
362    # Identify proprer mode for later usage
363    #
364
365    if {$addr eq ""} then {
366	set mode "host-or-alias"
367	set object "$name.$domain"
368    } else {
369	set mode "addr"
370	set object $addr
371    }
372
373    #
374    # Filter view ids
375    #
376
377    set msg [filter-views $dbfd tabuid $mode $object $idviews chkv]
378    if {$msg ne ""} then {
379	d error $msg
380    }
381
382    #
383    # Ask for confirmation if needed
384    #
385
386    if {[llength $idviews] == 0} then {
387	confirm $dbfd $mode $object chkv
388    } else {
389	remove $dbfd $mode $object chkv
390    }
391}
392
393##############################################################################
394# Main procedure
395##############################################################################
396
397d cgi-dispatch "dns" ""
398