1 |
|
package org.apache.maven.wagon.providers.sshext; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import org.apache.maven.wagon.AbstractWagon; |
20 |
|
import org.apache.maven.wagon.CommandExecutionException; |
21 |
|
import org.apache.maven.wagon.CommandExecutor; |
22 |
|
import org.apache.maven.wagon.PathUtils; |
23 |
|
import org.apache.maven.wagon.PermissionModeUtils; |
24 |
|
import org.apache.maven.wagon.ResourceDoesNotExistException; |
25 |
|
import org.apache.maven.wagon.TransferFailedException; |
26 |
|
import org.apache.maven.wagon.WagonConstants; |
27 |
|
import org.apache.maven.wagon.authentication.AuthenticationException; |
28 |
|
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
29 |
|
import org.apache.maven.wagon.authorization.AuthorizationException; |
30 |
|
import org.apache.maven.wagon.events.TransferEvent; |
31 |
|
import org.apache.maven.wagon.repository.RepositoryPermissions; |
32 |
|
import org.apache.maven.wagon.resource.Resource; |
33 |
|
import org.codehaus.plexus.util.FileUtils; |
34 |
|
import org.codehaus.plexus.util.StringUtils; |
35 |
|
import org.codehaus.plexus.util.cli.CommandLineException; |
36 |
|
import org.codehaus.plexus.util.cli.CommandLineUtils; |
37 |
|
import org.codehaus.plexus.util.cli.Commandline; |
38 |
|
|
39 |
|
import java.io.File; |
40 |
|
import java.io.IOException; |
41 |
|
import java.util.List; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
0 |
public class ScpExternalWagon |
52 |
|
extends AbstractWagon |
53 |
|
implements CommandExecutor |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
0 |
private String scpExecutable = "scp"; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
0 |
private String sshExecutable = "ssh"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private String scpArgs; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private String sshArgs; |
82 |
|
|
83 |
|
private int port; |
84 |
|
|
85 |
|
private File privateKey; |
86 |
|
|
87 |
|
private String password; |
88 |
|
|
89 |
|
private static final int SSH_FATAL_EXIT_CODE = 255; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
public void openConnection() |
96 |
|
throws AuthenticationException |
97 |
|
{ |
98 |
0 |
if ( authenticationInfo == null ) |
99 |
|
{ |
100 |
0 |
authenticationInfo = new AuthenticationInfo(); |
101 |
|
} |
102 |
|
|
103 |
0 |
if ( authenticationInfo.getUserName() == null ) |
104 |
|
{ |
105 |
0 |
authenticationInfo.setUserName( System.getProperty( "user.name" ) ); |
106 |
|
} |
107 |
|
|
108 |
0 |
port = getRepository().getPort(); |
109 |
|
|
110 |
|
|
111 |
0 |
if ( authenticationInfo.getPassword() == null ) |
112 |
|
{ |
113 |
|
File privateKey; |
114 |
|
|
115 |
0 |
if ( authenticationInfo.getPrivateKey() != null ) |
116 |
|
{ |
117 |
0 |
privateKey = new File( authenticationInfo.getPrivateKey() ); |
118 |
0 |
} |
119 |
|
else |
120 |
|
{ |
121 |
0 |
privateKey = findPrivateKey(); |
122 |
|
} |
123 |
|
|
124 |
0 |
if ( privateKey.exists() ) |
125 |
|
{ |
126 |
0 |
if ( authenticationInfo.getPassphrase() == null ) |
127 |
|
{ |
128 |
0 |
authenticationInfo.setPassphrase( "" ); |
129 |
|
} |
130 |
|
|
131 |
0 |
fireSessionDebug( "Using private key: " + privateKey ); |
132 |
|
|
133 |
0 |
this.privateKey = privateKey; |
134 |
|
} |
135 |
0 |
} |
136 |
|
else |
137 |
|
{ |
138 |
0 |
this.password = authenticationInfo.getPassword(); |
139 |
|
} |
140 |
|
|
141 |
0 |
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
private File findPrivateKey() |
148 |
|
{ |
149 |
0 |
String privateKeyDirectory = System.getProperty( "wagon.privateKeyDirectory" ); |
150 |
|
|
151 |
0 |
if ( privateKeyDirectory == null ) |
152 |
|
{ |
153 |
0 |
privateKeyDirectory = System.getProperty( "user.home" ); |
154 |
|
} |
155 |
|
|
156 |
0 |
File privateKey = new File( privateKeyDirectory, ".ssh/id_dsa" ); |
157 |
|
|
158 |
0 |
if ( !privateKey.exists() ) |
159 |
|
{ |
160 |
0 |
privateKey = new File( privateKeyDirectory, ".ssh/id_rsa" ); |
161 |
|
} |
162 |
|
|
163 |
0 |
return privateKey; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
public void closeConnection() |
171 |
|
{ |
172 |
|
|
173 |
0 |
} |
174 |
|
|
175 |
|
public void executeCommand( String command, boolean ignoreFailures ) |
176 |
|
throws CommandExecutionException |
177 |
|
{ |
178 |
0 |
boolean putty = sshExecutable.indexOf( "plink" ) >= 0; |
179 |
|
|
180 |
0 |
Commandline cl = createBaseCommandLine( putty, sshExecutable ); |
181 |
|
|
182 |
0 |
if ( port != WagonConstants.UNKNOWN_PORT ) |
183 |
|
{ |
184 |
0 |
if ( putty ) |
185 |
|
{ |
186 |
0 |
cl.createArgument().setLine( "-P " + port ); |
187 |
0 |
} |
188 |
|
else |
189 |
|
{ |
190 |
0 |
cl.createArgument().setLine( "-p " + port ); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
0 |
if ( sshArgs != null ) |
195 |
|
{ |
196 |
0 |
cl.createArgument().setLine( sshArgs ); |
197 |
|
} |
198 |
0 |
String remoteHost = authenticationInfo.getUserName() + "@" + getRepository().getHost(); |
199 |
|
|
200 |
0 |
cl.createArgument().setValue( remoteHost ); |
201 |
|
|
202 |
0 |
cl.createArgument().setValue( command ); |
203 |
|
|
204 |
0 |
fireSessionDebug( "Executing command: " + cl.toString() ); |
205 |
|
|
206 |
|
try |
207 |
|
{ |
208 |
0 |
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); |
209 |
0 |
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
210 |
0 |
int exitCode = CommandLineUtils.executeCommandLine( cl, out, err ); |
211 |
0 |
fireSessionDebug( out.getOutput() ); |
212 |
0 |
fireSessionDebug( err.getOutput() ); |
213 |
0 |
if ( exitCode != 0 ) |
214 |
|
{ |
215 |
0 |
if ( !ignoreFailures || exitCode == SSH_FATAL_EXIT_CODE ) |
216 |
|
{ |
217 |
0 |
throw new CommandExecutionException( "Exit code " + exitCode + " - " + err.getOutput() ); |
218 |
|
} |
219 |
|
} |
220 |
|
} |
221 |
0 |
catch ( CommandLineException e ) |
222 |
|
{ |
223 |
0 |
throw new CommandExecutionException( "Error executing command line", e ); |
224 |
0 |
} |
225 |
0 |
} |
226 |
|
|
227 |
|
private Commandline createBaseCommandLine( boolean putty, String executable ) |
228 |
|
{ |
229 |
0 |
Commandline cl = new Commandline(); |
230 |
|
|
231 |
0 |
cl.setExecutable( executable ); |
232 |
|
|
233 |
0 |
if ( privateKey != null ) |
234 |
|
{ |
235 |
0 |
cl.createArgument().setValue( "-i" ); |
236 |
0 |
cl.createArgument().setFile( privateKey ); |
237 |
|
} |
238 |
|
|
239 |
0 |
if ( putty && password != null ) |
240 |
|
{ |
241 |
0 |
cl.createArgument().setValue( "-pw" ); |
242 |
0 |
cl.createArgument().setValue( password ); |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
0 |
if ( putty ) |
247 |
|
{ |
248 |
0 |
cl.createArgument().setValue( "-batch" ); |
249 |
0 |
} |
250 |
|
else |
251 |
|
{ |
252 |
0 |
cl.createArgument().setValue( "-o" ); |
253 |
0 |
cl.createArgument().setValue( "BatchMode yes" ); |
254 |
|
} |
255 |
0 |
return cl; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
private void executeScpCommand( File localFile, String remoteFile, boolean put ) |
260 |
|
throws TransferFailedException, ResourceDoesNotExistException |
261 |
|
{ |
262 |
0 |
boolean putty = scpExecutable.indexOf( "pscp" ) >= 0; |
263 |
|
|
264 |
0 |
Commandline cl = createBaseCommandLine( putty, scpExecutable ); |
265 |
|
|
266 |
0 |
cl.setWorkingDirectory( localFile.getParentFile().getAbsolutePath() ); |
267 |
|
|
268 |
0 |
if ( port != WagonConstants.UNKNOWN_PORT ) |
269 |
|
{ |
270 |
0 |
cl.createArgument().setLine( "-P " + port ); |
271 |
|
} |
272 |
|
|
273 |
0 |
if ( scpArgs != null ) |
274 |
|
{ |
275 |
0 |
cl.createArgument().setLine( scpArgs ); |
276 |
|
} |
277 |
0 |
String qualifiedRemoteFile = |
278 |
|
authenticationInfo.getUserName() + "@" + getRepository().getHost() + ":" + remoteFile; |
279 |
0 |
if ( put ) |
280 |
|
{ |
281 |
0 |
cl.createArgument().setValue( localFile.getName() ); |
282 |
0 |
cl.createArgument().setValue( qualifiedRemoteFile ); |
283 |
0 |
} |
284 |
|
else |
285 |
|
{ |
286 |
0 |
cl.createArgument().setValue( qualifiedRemoteFile ); |
287 |
0 |
cl.createArgument().setValue( localFile.getName() ); |
288 |
|
} |
289 |
|
|
290 |
0 |
fireSessionDebug( "Executing command: " + cl.toString() ); |
291 |
|
|
292 |
|
try |
293 |
|
{ |
294 |
0 |
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
295 |
0 |
int exitCode = CommandLineUtils.executeCommandLine( cl, null, err ); |
296 |
0 |
if ( exitCode != 0 ) |
297 |
|
{ |
298 |
0 |
if ( !put && err.getOutput().trim().toLowerCase().endsWith( "no such file or directory" ) ) |
299 |
|
{ |
300 |
0 |
throw new ResourceDoesNotExistException( err.getOutput() ); |
301 |
|
} |
302 |
|
else |
303 |
|
{ |
304 |
0 |
throw new TransferFailedException( "Exit code: " + exitCode + " - " + err.getOutput() ); |
305 |
|
} |
306 |
|
} |
307 |
|
} |
308 |
0 |
catch ( CommandLineException e ) |
309 |
|
{ |
310 |
0 |
throw new TransferFailedException( "Error executing command line", e ); |
311 |
0 |
} |
312 |
0 |
} |
313 |
|
|
314 |
|
public void put( File source, String destination ) |
315 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
316 |
|
{ |
317 |
0 |
Resource resource = new Resource( destination ); |
318 |
|
|
319 |
0 |
firePutInitiated( resource, source ); |
320 |
|
|
321 |
0 |
if ( !source.exists() ) |
322 |
|
{ |
323 |
0 |
throw new TransferFailedException( "Specified source file does not exist: " + source ); |
324 |
|
} |
325 |
|
|
326 |
0 |
String basedir = getRepository().getBasedir(); |
327 |
|
|
328 |
0 |
String resourceName = StringUtils.replace( destination, "\\", "/" ); |
329 |
|
|
330 |
0 |
String dir = PathUtils.dirname( resourceName ); |
331 |
|
|
332 |
0 |
dir = StringUtils.replace( dir, "\\", "/" ); |
333 |
|
|
334 |
0 |
String umaskCmd = null; |
335 |
0 |
if ( getRepository().getPermissions() != null ) |
336 |
|
{ |
337 |
0 |
String dirPerms = getRepository().getPermissions().getDirectoryMode(); |
338 |
|
|
339 |
0 |
if ( dirPerms != null ) |
340 |
|
{ |
341 |
0 |
umaskCmd = "umask " + PermissionModeUtils.getUserMaskFor( dirPerms ); |
342 |
|
} |
343 |
|
} |
344 |
|
|
345 |
0 |
String mkdirCmd = "mkdir -p " + basedir + "/" + dir + "\n"; |
346 |
|
|
347 |
0 |
if ( umaskCmd != null ) |
348 |
|
{ |
349 |
0 |
mkdirCmd = umaskCmd + "; " + mkdirCmd; |
350 |
|
} |
351 |
|
|
352 |
|
try |
353 |
|
{ |
354 |
0 |
executeCommand( mkdirCmd ); |
355 |
|
} |
356 |
0 |
catch ( CommandExecutionException e ) |
357 |
|
{ |
358 |
0 |
throw new TransferFailedException( "Error executing command for transfer", e ); |
359 |
0 |
} |
360 |
|
|
361 |
0 |
firePutStarted( resource, source ); |
362 |
|
|
363 |
0 |
executeScpCommand( source, basedir + "/" + resourceName, true ); |
364 |
|
|
365 |
0 |
postProcessListeners( resource, source, TransferEvent.REQUEST_PUT ); |
366 |
|
|
367 |
|
try |
368 |
|
{ |
369 |
0 |
RepositoryPermissions permissions = getRepository().getPermissions(); |
370 |
|
|
371 |
0 |
if ( permissions != null && permissions.getGroup() != null ) |
372 |
|
{ |
373 |
0 |
executeCommand( "chgrp -f " + permissions.getGroup() + " " + basedir + "/" + resourceName + "\n", |
374 |
|
true ); |
375 |
|
} |
376 |
|
|
377 |
0 |
String fileMode = "644"; |
378 |
0 |
if ( permissions != null && permissions.getFileMode() != null ) |
379 |
|
{ |
380 |
0 |
fileMode = permissions.getFileMode(); |
381 |
|
} |
382 |
0 |
executeCommand( "chmod -f " + fileMode + " " + basedir + "/" + resourceName + "\n", true ); |
383 |
|
} |
384 |
0 |
catch ( CommandExecutionException e ) |
385 |
|
{ |
386 |
0 |
throw new TransferFailedException( "Error executing command for transfer", e ); |
387 |
0 |
} |
388 |
0 |
firePutCompleted( resource, source ); |
389 |
0 |
} |
390 |
|
|
391 |
|
public void executeCommand( String command ) |
392 |
|
throws CommandExecutionException |
393 |
|
{ |
394 |
0 |
executeCommand( command, false ); |
395 |
0 |
} |
396 |
|
|
397 |
|
public void get( String resourceName, File destination ) |
398 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
399 |
|
{ |
400 |
0 |
String basedir = getRepository().getBasedir(); |
401 |
|
|
402 |
0 |
String path = StringUtils.replace( resourceName, "\\", "/" ); |
403 |
|
|
404 |
0 |
createParentDirectories( destination ); |
405 |
|
|
406 |
0 |
Resource resource = new Resource( path ); |
407 |
|
|
408 |
0 |
fireGetStarted( resource, destination ); |
409 |
|
|
410 |
0 |
executeScpCommand( destination, basedir + "/" + path, false ); |
411 |
|
|
412 |
0 |
postProcessListeners( resource, destination, TransferEvent.REQUEST_GET ); |
413 |
|
|
414 |
0 |
fireGetCompleted( resource, destination ); |
415 |
0 |
} |
416 |
|
|
417 |
|
public boolean getIfNewer( String resourceName, File destination, long timestamp ) |
418 |
|
{ |
419 |
0 |
throw new UnsupportedOperationException( "getIfNewer is scp wagon must be still implemented" ); |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
public String getScpExecutable() |
430 |
|
{ |
431 |
0 |
return scpExecutable; |
432 |
|
} |
433 |
|
|
434 |
|
public void setScpExecutable( String scpExecutable ) |
435 |
|
{ |
436 |
0 |
this.scpExecutable = scpExecutable; |
437 |
0 |
} |
438 |
|
|
439 |
|
public String getSshExecutable() |
440 |
|
{ |
441 |
0 |
return sshExecutable; |
442 |
|
} |
443 |
|
|
444 |
|
public void setSshExecutable( String sshExecutable ) |
445 |
|
{ |
446 |
0 |
this.sshExecutable = sshExecutable; |
447 |
0 |
} |
448 |
|
|
449 |
|
public String getScpArgs() |
450 |
|
{ |
451 |
0 |
return scpArgs; |
452 |
|
} |
453 |
|
|
454 |
|
public void setScpArgs( String scpArgs ) |
455 |
|
{ |
456 |
0 |
this.scpArgs = scpArgs; |
457 |
0 |
} |
458 |
|
|
459 |
|
public String getSshArgs() |
460 |
|
{ |
461 |
0 |
return sshArgs; |
462 |
|
} |
463 |
|
|
464 |
|
public void setSshArgs( String sshArgs ) |
465 |
|
{ |
466 |
0 |
this.sshArgs = sshArgs; |
467 |
0 |
} |
468 |
|
|
469 |
|
public void putDirectory( File sourceDirectory, String destinationDirectory ) |
470 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
471 |
|
{ |
472 |
0 |
String basedir = getRepository().getBasedir(); |
473 |
|
|
474 |
0 |
String dir = StringUtils.replace( destinationDirectory, "\\", "/" ); |
475 |
|
|
476 |
0 |
String path = getPath( basedir, dir ); |
477 |
|
try |
478 |
|
{ |
479 |
0 |
if ( getRepository().getPermissions() != null ) |
480 |
|
{ |
481 |
0 |
String dirPerms = getRepository().getPermissions().getDirectoryMode(); |
482 |
|
|
483 |
0 |
if ( dirPerms != null ) |
484 |
|
{ |
485 |
0 |
String umaskCmd = "umask " + PermissionModeUtils.getUserMaskFor( dirPerms ); |
486 |
0 |
executeCommand( umaskCmd ); |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
0 |
String mkdirCmd = "mkdir -p " + path; |
491 |
|
|
492 |
0 |
executeCommand( mkdirCmd ); |
493 |
|
} |
494 |
0 |
catch ( CommandExecutionException e ) |
495 |
|
{ |
496 |
0 |
throw new TransferFailedException( "Error performing commands for file transfer", e ); |
497 |
0 |
} |
498 |
|
|
499 |
|
File zipFile; |
500 |
|
try |
501 |
|
{ |
502 |
0 |
zipFile = File.createTempFile( "wagon", ".zip" ); |
503 |
0 |
zipFile.deleteOnExit(); |
504 |
|
|
505 |
0 |
List files = FileUtils.getFileNames( sourceDirectory, "**/**", "", false ); |
506 |
|
|
507 |
0 |
createZip( files, zipFile, sourceDirectory ); |
508 |
|
} |
509 |
0 |
catch ( IOException e ) |
510 |
|
{ |
511 |
0 |
throw new TransferFailedException( "Unable to create ZIP archive of directory", e ); |
512 |
0 |
} |
513 |
|
|
514 |
0 |
put( zipFile, getPath( dir, zipFile.getName() ) ); |
515 |
|
|
516 |
|
try |
517 |
|
{ |
518 |
0 |
executeCommand( "cd " + path + "; unzip -o " + zipFile.getName() + "; rm -f " + zipFile.getName() ); |
519 |
|
|
520 |
0 |
zipFile.delete(); |
521 |
|
|
522 |
0 |
RepositoryPermissions permissions = getRepository().getPermissions(); |
523 |
|
|
524 |
0 |
if ( permissions != null && permissions.getGroup() != null ) |
525 |
|
{ |
526 |
0 |
executeCommand( "chgrp -Rf " + permissions.getGroup() + " " + path ); |
527 |
|
} |
528 |
|
|
529 |
0 |
if ( permissions != null && permissions.getFileMode() != null ) |
530 |
|
{ |
531 |
0 |
executeCommand( "chmod -Rf " + permissions.getFileMode() + " " + path ); |
532 |
|
} |
533 |
|
} |
534 |
0 |
catch ( CommandExecutionException e ) |
535 |
|
{ |
536 |
0 |
throw new TransferFailedException( "Error performing commands for file transfer", e ); |
537 |
0 |
} |
538 |
0 |
} |
539 |
|
|
540 |
|
public boolean supportsDirectoryCopy() |
541 |
|
{ |
542 |
0 |
return true; |
543 |
|
} |
544 |
|
} |