Skip to content

Commit

Permalink
IZPACK-1688: Update PR #704 (#729)
Browse files Browse the repository at this point in the history
* Fix to solve problem 'reduce' is not defined

Traceback (most recent call last):
  File "izpack2app\izpack2app.py", line 53, in <module>
    main()
  File "izpack2app\izpack2app.py", line 43, in main
    plist_content = reduce(reducer, plist.readlines(), '').replace('__JAR__', jar_name)
NameError: name 'reduce' is not defined

* Update izpack2app.py

* sudo panel refactoring for real use case usage. Password now can be exported for external scritpts as variable.

Sudo panel localization using current langpacks

Added linux .run format creation and example for ant usage

Minor fix for python 3 print function error

* IZPACK-1688: Sudo panel refactoring for real use case usage. Cleanup PR
704.

Co-authored-by: Paolo Ucchino <ucchino@gmail.com>
  • Loading branch information
akuhtz and ucchino committed Nov 10, 2020
1 parent ba38d5c commit 63d13cf
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 104 deletions.
Expand Up @@ -117,6 +117,12 @@

<!-- InfoPanel strings -->
<str id="InfoPanel.info" txt="Please read the following information: "/>

<!-- SudoPanel strings -->
<str id="SudoPanel.info" txt="For installing administrator privileges are necessary"/>
<str id="SudoPanel.tip" txt="Please note that passwords are case-sensitive"/>
<str id="SudoPanel.specifyAdminPassword" txt="Please specify your password:"/>
<str id="SudoPanel.invalidPassword" txt="Invalid password or non-administrator privileges"/>

<!-- PathInputPanel strings -->
<str id="PathInputPanel.isfile"
Expand Down
Expand Up @@ -120,6 +120,11 @@

<str id="InfoPanel.info" txt="Leggere attentamente le seguenti informazioni: "/>

<str id="SudoPanel.info" txt="Per effettuare l'installazione sono necessari privilegi di amministratore"/>
<str id="SudoPanel.tip" txt="Si prega di notare che le password fanno distinzione fra maiuscolo e minuscolo"/>
<str id="SudoPanel.specifyAdminPassword" txt="Per favore inserisca la propria password di amministratore:"/>
<str id="SudoPanel.invalidPassword" txt="Password errata o privilegi non sufficienti"/>

<str id="PathInputPanel.required" txt="La cartella selezionata deve esistere."/>
<str id="PathInputPanel.required.forModificationInstallation" txt="La cartella selezionata deve esistere."/>
<str id="PathInputPanel.notValid" txt="La cartella selezionata non contiene il prodotto richiesto."/>
Expand Down
195 changes: 95 additions & 100 deletions izpack-panel/src/main/java/com/izforge/izpack/panels/sudo/SudoPanel.java
Expand Up @@ -21,37 +21,49 @@

package com.izforge.izpack.panels.sudo;

import com.izforge.izpack.api.data.ExecutableFile;
import static com.izforge.izpack.util.Platform.Name.UNIX;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.izforge.izpack.api.data.Panel;
import com.izforge.izpack.api.data.ParsableFile;
import com.izforge.izpack.api.data.binding.OsModel;
import com.izforge.izpack.api.resource.Resources;
import com.izforge.izpack.api.substitutor.VariableSubstitutor;
import com.izforge.izpack.gui.LabelFactory;
import com.izforge.izpack.installer.data.GUIInstallData;
import com.izforge.izpack.installer.gui.InstallerFrame;
import com.izforge.izpack.installer.gui.IzPanel;
import com.izforge.izpack.installer.unpacker.ScriptParser;
import com.izforge.izpack.util.FileExecutor;
import com.izforge.izpack.util.Platform;
import com.izforge.izpack.util.PlatformModelMatcher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* The packs selection panel class.
*
* @author Jan Blok
* @since November 27, 2003
* @author Jan Blok - Pier Paolo Ucchino
* @since November 27, 2003 - November 29, 2019
*/
public class SudoPanel extends IzPanel implements ActionListener
{
/**
* The logger.
*/
private static Logger logger = Logger.getLogger(SudoPanel.class.getName());

/**
*
Expand All @@ -62,16 +74,6 @@ public class SudoPanel extends IzPanel implements ActionListener

private boolean isValid = false;

/**
* Replaces variables in scripts.
*/
private final VariableSubstitutor replacer;

/**
* The platform-model matcher.
*/
private final PlatformModelMatcher matcher;

/**
* The constructor.
*
Expand All @@ -86,24 +88,14 @@ public SudoPanel(Panel panel, InstallerFrame parent, GUIInstallData installData,
VariableSubstitutor replacer, PlatformModelMatcher matcher)
{
super(panel, parent, installData, resources);
this.replacer = replacer;
this.matcher = matcher;

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

add(LabelFactory
.create(
/* installData.getLangpack().getString("SudoPanel.info") */
"For installing administrator privileges are necessary",
JLabel.TRAILING));
add(LabelFactory.create(getString("SudoPanel.info"),JLabel.TRAILING));

add(Box.createRigidArea(new Dimension(0, 5)));

add(LabelFactory
.create(
/* installData.getLangpack().getString("SudoPanel.tip") */
"Please note that passwords are case-sensitive",
parent.getIcons().get("tip"), JLabel.TRAILING));
add(LabelFactory.create(getString("SudoPanel.tip"),parent.getIcons().get("tip"), JLabel.TRAILING));

add(Box.createRigidArea(new Dimension(0, 5)));

Expand All @@ -112,14 +104,8 @@ public SudoPanel(Panel panel, InstallerFrame parent, GUIInstallData installData,
spacePanel.setAlignmentY(CENTER_ALIGNMENT);
spacePanel.setBorder(BorderFactory.createEmptyBorder(80, 30, 0, 50));
spacePanel.setLayout(new BorderLayout(5, 5));
spacePanel
.add(
LabelFactory
.create(
/* installData.getLangpack().getString("SudoPanel.specifyAdminPassword") */
"Please specify your password:"),
BorderLayout.NORTH);
passwordField = new JPasswordField();
spacePanel.add(LabelFactory.create(getString("SudoPanel.specifyAdminPassword")),BorderLayout.NORTH);
passwordField = new JTextField();
passwordField.addActionListener(this);
JPanel space2Panel = new JPanel();
space2Panel.setLayout(new BorderLayout());
Expand All @@ -142,6 +128,7 @@ public void panelActivate()
*
* @param e The event.
*/
@Override
public void actionPerformed(ActionEvent e)
{
doSudoCmd();
Expand All @@ -152,69 +139,29 @@ public void actionPerformed(ActionEvent e)

private void doSudoCmd()
{
Platform platform = installData.getPlatform();

String pass = passwordField.getText();

File file = null;
isValid = false;

try
{
// write file in /tmp
file = new File("/tmp/cmd_sudo.sh");// ""c:/temp/run.bat""
FileOutputStream fos = new FileOutputStream(file);
fos.write("echo $password | sudo -S ls\nexit $?".getBytes()); // "echo
// $password
// >
// pipo.txt"
fos.close();

// execute
List<OsModel> oses = new ArrayList<OsModel>();
oses.add(new OsModel("unix", null, null, null, null));

ParsableFile parsableFile = new ParsableFile(file.getAbsolutePath(), null, null, oses);
ScriptParser scriptParser = new ScriptParser(replacer, matcher);
scriptParser.parse(parsableFile);

ArrayList<ExecutableFile> executableFiles = new ArrayList<ExecutableFile>();
ExecutableFile executableFile = new ExecutableFile(file.getAbsolutePath(),
ExecutableFile.POSTINSTALL, ExecutableFile.ABORT, oses,
false);
executableFiles.add(executableFile);
FileExecutor fileExecutor = new FileExecutor(executableFiles);
int retval = fileExecutor.executeFiles(ExecutableFile.POSTINSTALL, matcher, this);
if (retval == 0)
if(platform.isA(UNIX))
{
this.installData.setVariable("password", pass);
isValid = true;
isValid = checkUnixRootPassByCommandline(pass);

if (isValid)
{
installData.setVariable("password", pass);
}
}
// else is already showing dialog
// {
// JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
// check your password", "Error", JOptionPane.ERROR_MESSAGE);
// }
}
catch (Exception e)
{
// JOptionPane.showMessageDialog(this, "Cannot execute 'sudo' cmd,
// check your password", "Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
logger.log(Level.WARNING, e.getMessage(), e);
isValid = false;
}
try
{
if (file != null && file.exists())
{
file.delete();// you don't
}
// want the file
// with password
// tobe arround,
// in case of
// error
}
catch (Exception e)
{
// ignore
}
}

/**
Expand All @@ -230,9 +177,57 @@ public boolean isValidated()
}
if (!isValid)
{
JOptionPane.showInternalMessageDialog(this, "Password", "Password is not valid",
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this,getString("SudoPanel.invalidPassword"),"Error",JOptionPane.ERROR_MESSAGE);
}
return isValid;
}

private static List<String> getSudoCommandline(String password, String command) {
if(password == null) password = "";
if(command == null) command = "";

List<String> cmd = new ArrayList<String>();

cmd.add("/bin/bash");
cmd.add("-c");
cmd.add("echo " + password + " | sudo -S " + command.trim());

return cmd;
}

public static void execCmdAuthByCommandline(String password,String commandString) throws Exception {
List<String> command = getSudoCommandline(password,commandString);

get(command);
}

public static boolean checkUnixRootPassByCommandline(String password) throws Exception {
boolean root = false;

String dir = "/test_root_" + new Date().getTime();

execCmdAuthByCommandline(password, "mkdir " + dir);

if (new File(dir).exists() == true) {
execCmdAuthByCommandline(password, "rm -rf " + dir);
root = true;
}

return root;
}

public static int get(List<String> command) throws Exception {

int result = -1;

try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.inheritIO();

result = processBuilder.start().waitFor();
} catch (Exception reason) {
throw new Exception("Problem executing command",reason);
}
return result;
}
}
@@ -1,14 +1,15 @@
package com.izforge.izpack.test.util;

import sun.misc.URLClassPath;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

//import jdk.internal.loader.URLClassPath;
import sun.misc.URLClassPath;

/**
* jar Classloading manipulation class
*
Expand Down
@@ -0,0 +1,64 @@
/*
* Copyright 2019 gucchino.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils.wrappers.izpack2run;

/**
*
* @author gucchino
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.System.exit;

public class Merge2In1 {

public static void Merge2In1(File file1, File file2, File outputFile) throws IOException {

FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
FileOutputStream fos = new FileOutputStream(outputFile);

int len = 0;

byte[] buf = new byte[1024 * 1024]; // 1MB buffer

while ((len = fis1.read(buf)) != -1) {
fos.write(buf, 0, len);
}

while ((len = fis2.read(buf)) != -1) {
fos.write(buf, 0, len);
}

fos.close();
}

public static void main(String[] argv) throws IOException {

if(argv.length != 3) {
System.out.println("3 parameters required: file-input1, file-input2, file-output3");
exit(0);
}

File f1 = new File(argv[0]);
File f2 = new File(argv[1]);
File f3 = new File(argv[2]);

Merge2In1(f1,f2,f3);
}
}

0 comments on commit 63d13cf

Please sign in to comment.