#!/usr/local/bin/tclsh8.6

#
# Add a host in Netmagis database
#
# Syntax:
#   dnsaddhost <fqdn> <ip> <viewname>
#
# History
#   2004/09/24 : pda/jean : design from cgi script
#   2005/04/11 : pda/jean : adaptation
#   2007/10/25 : jean     : log modify actions
#   2010/12/18 : pda      : use new install system
#   2013/03/28 : pda/jean : add views
#

source /usr/local/lib/netmagis/libnetmagis.tcl

##############################################################################
# Small utility functions
##############################################################################

proc syntax-error {argv0} {
    regsub {.*/} $argv0 {} argv0
    return "usage: $argv0 fqdn ip view"
}

##############################################################################
# Main program
##############################################################################

proc main {argv0 argv} {
    global conf

    #
    # Initialization
    #

    set msg [d init-script dbfd $argv0 false tabcor]
    if {$msg ne ""} then {
	d error "$msg"
    }

    #
    # Argument checking
    #

    if {[llength $argv] != 3} then {
	d error [syntax-error $argv0]
	return 1
    }
    lassign $argv fqdn addr view

    #
    # Host name validation
    #

    set msg [check-fqdn-syntax $dbfd $fqdn name domain iddom]
    if {$msg ne ""} then {
	d error $msg
    }
    set name [string tolower $name]

    #
    # Check access to view
    #

    set idview [u viewid $view]
    if {$idview == -1} then {
	d error [mc "Invalid view '%s'" $view]
    }
    if {! [u isallowedview $idview]} then {
	d error [mc "Access denied to view '%s'" $view]
    }

    #
    # Check access to name
    #

    set msg [check-authorized-host $dbfd $tabcor(idcor) $name $domain $idview trr "host"]
    if {$msg ne ""} then {
	d error $msg
    }
    set iddom $trr(iddom)

    #
    # Does the name exist, and is already associated to an IP address?
    #

    set mac ""
    set comment ""
    set iddhcpprof 0
    set hinfo ""
    set sendsmtp 0
    set ttl -1
    set comment ""
    set respname ""
    set respmail ""

    set rrexists 0
    if {$trr(idrr) ne ""} then {
	set rrexists 1

	set mac		$trr(mac)
	set iddhcpprof	$trr(iddhcpprof)
	set sendsmtp	$trr(sendsmtp)
	set ttl		$trr(ttl)
	set comment	$trr(comment)
	set respname	$trr(respname)
	set respmail	$trr(respmail)
    }

    #
    # Check syntax of IP address
    #

    set msg [check-ip-syntax $dbfd $addr "inet"]
    if {$msg ne ""} then {
	d error $msg
    }

    #
    # Check that we have access to tthis IP address
    #

    if {! [check-authorized-ip $dbfd $tabcor(idcor) $addr]} then {
	d error [mc "Access denied to IP address '%s'" $addr]
    }

    #
    # Check that this address does not exist yet
    #

    if {[read-rr-by-ip $dbfd $addr $idview trrbidon]} then {
	d error [mc "IP address '%s' already exists" $addr]
    }

    #
    # Insert host and return result
    #

    set msg [add-host $dbfd trr $name $iddom $idview $addr \
    				$mac $iddhcpprof 0 $sendsmtp \
				$ttl $comment $respname $respmail $tabcor(idcor)]
    if {$msg ne ""} then {
	d error $msg
    }

    #
    # End
    #

    d end
    return 0
}

exit [main $argv0 $argv]
