forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAs.java
More file actions
257 lines (232 loc) · 9.23 KB
/
Copy pathRunAs.java
File metadata and controls
257 lines (232 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package javaforce.utils;
import javaforce.*;
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* Created : Oct 28, 2015
*
* @author pquiring
*/
public class RunAs extends javax.swing.JFrame {
/**
* Creates new form RunAs
*/
public RunAs() {
initComponents();
JF.centerWindow(this);
JF.assignHotKey(this.getRootPane(), cancel, java.awt.event.KeyEvent.VK_ESCAPE);
JF.assignHotKey(this.getRootPane(), execute, java.awt.event.KeyEvent.VK_ENTER);
loadApps();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
program = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
username = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
password = new javax.swing.JPasswordField();
execute = new javax.swing.JButton();
cancel = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
domain = new javax.swing.JTextField();
pick = new javax.swing.JButton();
jLabel4.setText("jLabel4");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("RunAs");
jLabel1.setText("Application");
program.setEditable(true);
jLabel2.setText("Username");
jLabel3.setText("Password");
execute.setText("Execute");
execute.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
executeActionPerformed(evt);
}
});
cancel.setText("Cancel");
cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelActionPerformed(evt);
}
});
jLabel5.setText("Domain");
pick.setText("Select");
pick.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pickActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(cancel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(execute))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel5)
.addComponent(jLabel1)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(password)
.addComponent(domain)
.addComponent(username)
.addGroup(layout.createSequentialGroup()
.addComponent(program, 0, 451, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pick)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(program, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(pick))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(domain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(execute)
.addComponent(cancel))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
System.exit(0);
}//GEN-LAST:event_cancelActionPerformed
private void executeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeActionPerformed
String app = (String)program.getSelectedItem();
if (app == null || app.length() == 0) return;
saveApps();
String dom = domain.getText();
if (dom.length() == 0) {
dom = System.getenv("COMPUTERNAME");
}
String user = username.getText();
String pass = new String(password.getPassword());
ArrayList<String> cmd = new ArrayList<String>();
cmd.add("psexec.exe");
cmd.add("-d");
cmd.add("-u");
cmd.add(dom + "\\" + user);
cmd.add("-p");
cmd.add(pass);
String cl[] = ((String)program.getSelectedItem()).split("[|]");
for(int a=0;a<cl.length;a++) {
cmd.add(cl[a]);
}
String ca[] = cmd.toArray(new String[cmd.size()]);
/*
String out = "";
for(int a=0;a<ca.length;a++) {
out += " ";
out += ca[a];
}
JF.showMessage("test", out);
*/
try {
ShellProcess sp = new ShellProcess();
sp.run(ca, true);
System.exit(0);
} catch (Exception e) {
JF.showError("Error", e.toString());
}
}//GEN-LAST:event_executeActionPerformed
private void pickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pickActionPerformed
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(new FileNameExtensionFilter("Application (*.exe)", "exe"));
chooser.setCurrentDirectory(new File(JF.getCurrentPath()));
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
program.addItem(chooser.getSelectedFile().toString());
program.setSelectedIndex(program.getItemCount()-1);
}//GEN-LAST:event_pickActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RunAs().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancel;
private javax.swing.JTextField domain;
private javax.swing.JButton execute;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPasswordField password;
private javax.swing.JButton pick;
private javax.swing.JComboBox<String> program;
private javax.swing.JTextField username;
// End of variables declaration//GEN-END:variables
public static final String apps = "runas_apps.ini";
public void loadApps() {
try {
BufferedReader br = new BufferedReader(new FileReader(apps));
String ln;
while ((ln = br.readLine()) != null) {
if (ln.length() > 0) {
program.addItem(ln);
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveApps() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(apps));
int cnt = program.getItemCount();
for(int a=0;a<cnt;a++) {
String ln = program.getItemAt(a);
bw.write(ln, 0, ln.length());
bw.write("\r\n", 0, 2);
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}