ACC SHELL

Path : /usr/share/YaST2/include/sudo/
File Upload :
Current File : //usr/share/YaST2/include/sudo/dialog-spec.ycp

{

textdomain "sudo";

import "Wizard";
import "Sudo";
import "Label";
import "Popup";

include "sudo/helps.ycp";
include "sudo/complex.ycp";

void RedrawCmndTable (list <string> commands) {
    list <term> items = UpdateCmdList(commands);

    UI::ChangeWidget(`id("commands"),`Items, items);
    EnableDisableButtons("command_edit", "command_remove", items);
}


any AddEditUserSpecDialog(string what) {


	string caption =  "";
	map <string, any> spec = $[];
	list <string> commands = [];
	list <string> users = (list <string>) merge(Sudo::all_users, Sudo::GetAliasNames("user"));
	list <string> hosts = prepend( Sudo::GetAliasNames("host"), "ALL") ;
	list <string> run_as = (list <string>) merge(Sudo::all_users, Sudo::GetAliasNames("run_as"));

	if (what == "Add") {
		caption = _("New Sudo Rule");
		//Setting default values
	} else if (what == "Edit") {
		caption = _("Existing Sudo Rule ");
		spec = Sudo::GetRule( Sudo::GetItem() );
		commands = spec["commands"]:[];
	}

	term contents = `VBox (
		`Left(
			`ComboBox(`id("user_name"), `opt(`editable), _("User, Group or User Alias"),sort(string s, string t,users,``(s < t)))
		),
		`Left(
			`ComboBox(`id("host_name"), `opt(`editable), _("Host or Host Alias"),sort(string s, string t,hosts,``(s < t)))
		),
		`Left(
			`ComboBox(`id("run_as"), `opt(`editable), _("RunAs or RunAs Alias"),sort(string s, string t, run_as,``(s < t)))
		),
		`Left(
			`CheckBox(`id("no_passwd"),_("No Password"))
		),
		`Left (
			`Label(_("Commands to Run"))
		),
		`Table(`id("commands"),`header(_("Command"), _("Parameters")),[]),
		`Left(`HBox(
			`PushButton ( `id ( "command_add" ), `opt(`key_F3), " " + Label::AddButton() + " "),
			`PushButton ( `id ( "command_edit" ),  `opt(`key_F4), " " + Label::EditButton() + " "),
			`PushButton ( `id ( "command_remove" ),  `opt(`key_F5), " " + Label::DeleteButton() + " ")
          ))

	);

	Wizard::SetContentsButtons(caption, contents,HELPS["spec_single"]:"",Label::CancelButton(), Label::OKButton());
	Wizard::HideAbortButton(); 
	UI::ChangeWidget(`id("user_name"),`ValidChars, Sudo::ValidCharsUsername);
	UI::ChangeWidget(`id("run_as"),`ValidChars, Sudo::ValidCharsUsername);

	//initialize UI
	UI::ChangeWidget(`id("user_name"),`Value, spec["user"]:"");
	UI::ChangeWidget(`id("host_name"),`Value, spec["host"]:"");
	UI::ChangeWidget(`id("run_as"),`Value, deletechars(spec["run_as"]:"","()"));
	UI::ChangeWidget(`id("no_passwd"),`Value, (spec["tag"]:"" == "NOPASSWD:"));
	RedrawCmndTable(commands);


	any ret = nil;
	while(true) {

	    ret  = UI::UserInput();
	    /* next */
	    if(ret == `next) {
			spec["user"] = UI::QueryWidget(`id("user_name"),`Value);
			spec["host"] = UI::QueryWidget(`id("host_name"),`Value);
			spec["run_as"] = (string)UI::QueryWidget(`id("run_as"),`Value);
			spec["commands"] = commands;

			if ( (boolean) UI::QueryWidget(`id("no_passwd"),`Value)) 
			{ 
			    spec["tag"] = "NOPASSWD:";
			}
			else
			    spec["tag"] = "";

			if (spec["user"]:"" == "") {
				Popup::Error(_("User name must not be empty."));
				UI::SetFocus(`id("user_name"));
				continue;
			}
			if (spec["host"]:"" == "") {
				Popup::Error(_("Host name must not be empty."));
				UI::SetFocus(`id("host_name"));
				continue;
			}
			if (!ValidateHost(spec["host"]:"")) {
				UI::SetFocus(`id("host_name"));
				continue;
			}
			if (spec["commands"]:[] == [] ) {
				Popup::Error(_("Command list must have at least one entry"));
				UI::SetFocus(`id("commands"));
				continue;
			}
			if (spec["run_as"]:"" != "") {
				spec["run_as"] = "(" + spec["run_as"]:"" + ")";
			}
			Sudo::SetRule( Sudo::GetItem(),spec);
			Sudo::SetModified();
	          break;
		/* back */
		} else if(ret == `back) {
	        break;
	    /* add command  */
		} else if (ret == "command_add") {
			string new_command = AddCommandDialog("","");

			if(new_command != "" && !contains(commands, new_command)) {
				y2milestone("%1", new_command);
				commands = add(commands, new_command);
				RedrawCmndTable(commands);
			}
		/* edit command*/
		}else if(ret == "command_edit"){
			integer current_item = (integer) UI::QueryWidget(`id("commands"), `CurrentItem);
			term it = (term) UI::QueryWidget(`id("commands"), `Item(current_item));

			string new_command = AddCommandDialog(it[1]:"", it[2]:"");
			if( new_command !="")
			{
			    commands[current_item] = new_command; 
			    RedrawCmndTable(commands);
			}

		/* remove command */
		} else if (ret == "command_remove") {
			integer current_item = (integer) UI::QueryWidget(`id("commands"), `CurrentItem);

			commands = remove(commands, current_item);
			RedrawCmndTable(commands);
	    } else {
	        y2error("unexpected retcode: %1", ret);
	        continue;
	    }
	}

	initial_screen = "user_specs";
	Wizard::RestoreNextButton();
	return ret;

}

any SaveUserSpec() {
	return `next;
}

}

ACC SHELL 2018