/**
 * File:	clients/nis_server.ycp
 * Package:	Configuration of NIS server
 * Summary:	Main file
 * Authors:	Martin Vidner <mvidner@suse.cz>
 *
 * $Id: nis_server.ycp 21320 2005-02-07 15:40:38Z jsuchome $
 *
 * Main file for NIS server configuration. Uses all other files.
 */

{

/***
 * <h3>Configuration of the NIS server</h3>
 */

textdomain "nis_server";

import "CommandLine";
import "IP";
import "Nis";
import "NisServer";
import "Package";
import "Report";
import "Summary";

include "nis_server/wizards.ycp";

/* The main () */
y2milestone ("----------------------------------------");
y2milestone ("NisServer module started");

// --------------------------------------------------------------------------
// --------------------------------- cmd-line handlers

/**
 * Print summary of basic options
 * @return boolean false
 */
define boolean NisServerSummaryHandler (map options ) {

    if (NisServer::ui_what == `none)
    {
	// summary label
	CommandLine::Print (_("No NIS Server is configured."));
	return false;
    }

    if (NisServer::ui_what == `master)
	// summary label
	CommandLine::Print (_("A NIS master server is configured."));
    else
	// summary label
	CommandLine::Print (_("A NIS slave server is configured."));

    // summary label
    CommandLine::Print (sformat ("%1: %2", _("NIS Domain"),
	NisServer::domain !=""? NisServer::domain : Summary::NotConfigured()));

    if (NisServer::ui_what == `master)
    {
	// summary label:
	CommandLine::Print (_("YP Source Directory: ") + NisServer::pwd_srcdir);
	// summary label:
	CommandLine::Print (_("Available Maps: ") +
	    mergestring (NisServer::maps, ","));
    }
    else
    {
	// summary label:
	CommandLine::Print (_("NIS Master Server: ") +
	    (NisServer::ui_master_ip != "" ?
		NisServer::ui_master_ip : Summary::NotConfigured ()));

    }
    list<term> allow_query = ItemizeSecurenets (NisServer::securenets);
    if (size (allow_query) > 0)
    {
	string net = mergestring (
	    (list<string>) maplist (term t, allow_query,
		``(mergestring ([t[1]:"", t[2]:""], ":"))), "\n");
	// summary label (netmask:network shows output format)
	CommandLine::Print (sformat (_("Hosts Allowed to Query Server (netmask:network):
%1"), net));
    }
    return false; // do not call Write...
}

// stop server
define boolean NisServerStopHandler (map options) {

    if (NisServer::ui_what == `none)
	return false;
    NisServer::ui_what =	`none;
    return true;
}

// check if neccessary packages are installed
define boolean check_packages () {

    list<string> packages = NisServer::required_packages;
    if (!Package::InstalledAny (packages))
    {
	// error message
	Report::Error (sformat (_("Required packages (%1) are not installed."),
	    mergestring (NisServer::required_packages, ",")));
	return false;
    }
    return true;
}


// check validity of "securenets" command line option
// on error, report error message and return nil
define list<map> update_securenets (string securenets) {

    list<string> nets	= splitstring (securenets, ",;");
    string error		= "";
    integer i		= -1;
    list<term> items	= maplist (string net, nets, {
	list netlist	= splitstring (net, ":");
        string netmask	= netlist[0]:"";
	string network	= netlist[1]:"";
	if (netmask != "0.0.0.0" && !IP::Check4 (netmask))
	{
	    // error message
	    error	= error + sformat (_("Invalid netmask: %1.\n"), netmask);
	}
	if (!IP::Check4 (network))
	{
	    // error message
            error   = error + sformat (_("Invalid network: %1.\n"), network);
	}
	i = i + 1;
	return `item (`id (i), netmask, network);
    });
    if (error != "")
    {
	Report::Error (error + IP::Valid4());
	return nil;
    }
    return MergeNetsEntries (NisServer::securenets, items);
}

// configure master server
define boolean NisServerMasterHandler (map options) {

    if (!check_packages ())
	return false;

    string domain	= NisServer::domain;
    if (options["domain"]:"" != "")
    {
	domain		= options["domain"]:"";
    }
    if (!Nis::check_nisdomainname (domain))
    {
	Report::Error (Nis::valid_nisdomainname ());
	return false;
    }
    string pwd_srcdir	= NisServer::pwd_srcdir;
    if (options["ypdir"]:"" != "")
    {
	pwd_srcdir	= options["ypdir"]:pwd_srcdir;
    }

    list<map> securenets	= NisServer::securenets;
    if (options["securenets"]:"" != "")
    {
	securenets = update_securenets (options["securenets"]:"");
	if (securenets == nil)
	    return false;
    }

    list<string> maps		= NisServer::maps;
    if (options["maps"]:"" != "")
    {
	maps			= splitstring (options["maps"]:"", ",;:");
	list<string> all	= NisServer::GetAllMaps ();
	map files		= listmap (string table, all, {
	    return $[table : "/etc/" + table];
	});
	foreach (string table, ["passwd", "group", "passwd.adjunct", "shadow"],
	{
	    files[table] = sformat ("%1/%2", pwd_srcdir, table);
	});
	files["netgrp"] = "/etc/netgroup";
        files["mail"]	= "/etc/mail/aliases";

	all = filter (string table, all, {
	    if (NisServer::merge_passwd && table == "shadow")
		return false;
	    if (contains (NisServer::maps, table)) // current are ok?
		return true;
	    return SCR::Read (.target.size, files[table]:"/") >= 0;
	});
	list<string> unsup =
	    filter (string table, maps, ``(!contains (all, table)));
	if (size (unsup) > 0)
	{
	    // error message
	    Report::Error (sformat (_("These maps are not supported:\n%1"),
		mergestring (unsup, ",")));
	    return false;
	}
    }


    if (options["yppasswd"]:"" != "")
    {
	NisServer::start_yppasswdd	= options["yppasswd"]:"" == "yes";
    }

    NisServer::maps		= maps;
    NisServer::securenets	= securenets;
    NisServer::pwd_srcdir	= pwd_srcdir;
    NisServer::domain		= domain;
    NisServer::ui_what		= `master;
    return true;
}

// configure NIS slave server
define boolean NisServerSlaveHandler (map options) {

    if (!check_packages ())
	return false;

    string domain	= NisServer::domain;
    if (options["domain"]:"" != "")
    {
	domain		= options["domain"]:"";
    }
    if (!Nis::check_nisdomainname (domain))
    {
	Report::Error (Nis::valid_nisdomainname ());
	return false;
    }

    list<map> securenets	= NisServer::securenets;
    if (options["securenets"]:"" != "")
    {
	securenets = update_securenets (options["securenets"]:"");
	if (securenets == nil)
	    return false;
    }

    string master_ip	= NisServer::ui_master_ip;
    if (options["master_ip"]:"" != "")
    {
	master_ip	= options["master_ip"]:"";
    }
    if (master_ip == "")
    {
	// error message
	Report::Error (_("NIS master server IP was not specified."));
	return false;
    }
    if (!IP::Check4(master_ip))
    {
	Report::Error (IP::Valid4());
	return false;
    }

    NisServer::securenets	= securenets;
    NisServer::domain		= domain;
    NisServer::ui_master_ip	= master_ip;
    NisServer::ui_what		= `slave;
    return true;
}

/* the command line description map */
map cmdline = $[
    "id"		: "nis-server",
    // translators: command line help text for nis server module
    "help"		: _("NIS server configuration module."),
    "guihandler"	: NisServerSequence,
    "initialize"	: NisServer::Read,
    "finish"		: NisServer::Write,
    "actions"		: $[
	"summary" :$[
	    "handler"	: NisServerSummaryHandler,
	    // command line help text for 'summary' action
	    "help"	: _("Configuration summary of NIS server")
	],
	"stop"		: $[
	    "handler"	: NisServerStopHandler,
	    // command line action help
	    "help"	: _("Stop NIS server"),
	],
	"master"	: $[
	    "handler"	: NisServerMasterHandler,
	    // command line action help
	    "help"	: _("Configure NIS master server"),
	],
	"slave"		: $[
	    "handler"	: NisServerSlaveHandler,
	    // command line action help
	    "help"	: _("Configure NIS slave server"),
	],
	// configure
    ],
    "options"		: $[
	"domain"	:$[
	    // command line help text for the 'domain' option
	    "help"	: _("NIS domain"),
	    "type"	: "string"
	],
	"master_ip"	: $[
	    // command line help text for the 'master_ip' option
	    "help"	: _("IP address of master NIS server"),
	    "type"	: "string"
	],
	"yppasswd"	: $[
	    // command line help text for the 'yppasswd' option
	    "help"	: _("Start or stop yppasswd service"),
	    "type"      : "enum",
            "typespec"  : [ "yes", "no" ],
	],
	"ypdir"		: $[
	    // command line help text for the 'ypdir' option
	    "help"	: _("YP source directory"),
	    "type"	: "string"
	],
	"maps"		: $[
	    // command line help text for the 'maps' option
	    "help"	: _("Maps distributed by server"),
	    "type"	: "string"
	],
	"securenets"	: $[
	    // command line help text for the 'hosts' option
	    "help"	: _("Hosts allowed to query server ('netmask:network')"),
	    "type"	: "string"
	],
    ],
    "mappings"		: $[
	"summary"	: [],
	"master"	: [ "domain", "yppasswd", "ypdir", "maps","securenets"],
	"slave"		: [ "domain", "master_ip", "securenets" ],
    ]
];

any ret = CommandLine::Run (cmdline);

y2debug("ret=%1", ret);

/* Finish */
y2milestone("NisServer module finished");
y2milestone("----------------------------------------");

return ret;

/* EOF */
}
