-
Notifications
You must be signed in to change notification settings - Fork 17
/
MVSCPFileUpload.m
executable file
·291 lines (242 loc) · 9.16 KB
/
MVSCPFileUpload.m
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// MVSCPFileUpload.m
// FileShuttle
//
// Created by Michael Villar on 12/13/11.
//
#import "MVSCPFileUpload.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <util.h>
#include "sshversion.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface MVSCPFileUpload ()
@property (assign) pid_t scppid;
@property (assign) int masterfd;
- (void)write:(char *)buf;
- (void)parseProgressOutputString:(NSString *)line;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MVSCPFileUpload
extern int errno;
extern char **environ;
int scpconnecting = 0;
@synthesize scppid = scppid_,
masterfd = masterfd_;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)start {
if([self.delegate respondsToSelector:@selector(fileUploadDidStartUpload:)])
[self.delegate fileUploadDidStartUpload:self];
[NSThread detachNewThreadSelector:@selector(startFromThread) toTarget:self withObject:nil];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)execBlockingSSHExecutable:(char *)executable withArguments:(char **)execargs password:(char *)password
{
char ttyname[ MAXPATHLEN ];
int status;
execargs[0] = executable;
if ((self.scppid = forkpty( &masterfd_, ttyname, NULL, NULL ))) {
if ( fcntl( self.masterfd, F_SETFL, O_NONBLOCK ) < 0 ) { /* prevent master from blocking */
}
fd_set readmask;
unichar buf[ MAXPATHLEN ];
int pwsent = 0, validpw = 0, noscp = 0, loading = 0, copying = 0;
NSString *line;
FILE *mfp;
if (( mfp = fdopen( self.masterfd, "r" )) == NULL ) {
[self performSelectorOnMainThread:@selector(error:)
withObject:@"fdopen master fd returned NULL"
waitUntilDone:YES];
return NO;
}
setvbuf( mfp, NULL, _IONBF, 0 );
for ( ;; ) {
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc ] init ];
FD_ZERO( &readmask );
FD_SET( self.masterfd, &readmask );
if ( select( self.masterfd + 1, &readmask, NULL, NULL, NULL ) < 0 ) {
[self performSelectorOnMainThread:@selector(error:)
withObject:@"select() returned a value less than zero"
waitUntilDone:YES];
return NO;
}
if ( FD_ISSET( self.masterfd, &readmask )) {
if ( fgets(( char * )buf, MAXPATHLEN, mfp ) == NULL ) break;
line = [NSString stringWithCString:(char*)buf encoding:NSASCIIStringEncoding];
if (( strstr(( char * )buf, "Password:" ) != NULL
|| strstr(( char * )buf, "password:" ) != NULL
|| strstr(( char * )buf, "passphrase" ) != NULL )
&& !validpw ) {
// Send password
[self write:password];
pwsent = 1;
}
else if ( strchr(( char * )buf, '%' ) != NULL ) {
loading += 1;
// Loading informations
if ( ! copying ) {
copying = 1;
} else {
[ self parseProgressOutputString: line];
}
}
else {
if([line length] > 5 && loading == 0) {
[self performSelectorOnMainThread:@selector(error:)
withObject:line
waitUntilDone:YES];
return NO;
}
}
memset(( char * )buf, '\0', strlen(( char * )buf ));
[ pool release ];
if ( noscp ) break;
}
}
if ( fclose( mfp ) != 0 ) {
NSLog(@"fclose failed: %s", strerror( errno ));
}
( void )close( self.masterfd );
self.scppid = wait( &status );
if ( WIFEXITED( status )) {
// Normal exit
} else if ( WIFSIGNALED( status )) {
NSLog(@"Exit with signal = %d\n", status);
return NO;
} else if ( WIFSTOPPED( status )) {
NSLog(@"WIFSTOPPED\n");
return NO;
}
self.scppid = 0;
return YES;
} else if ( self.scppid < 0 ) {
NSLog( @"forkpty failed: %s", strerror( errno ));
} else {
execve( executable, ( char ** )execargs, environ );
[self performSelectorOnMainThread:@selector(error:)
withObject:@"execve failed"
waitUntilDone:YES];
}
return NO;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)startFromThread {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:self.destination];
NSString *path = url.relativePath;
path = [path stringByReplacingOccurrencesOfString:@" " withString:@"\\ "];
NSString *userathostString = [NSString stringWithFormat:@"%@@%@:%@",
self.username, url.host, path];
char* userathost = (char*)[userathostString UTF8String];
char* password = (char*)[self.password UTF8String];
int portnumber = [url.port intValue];
char* localfile = (char*)[[self.source path] UTF8String];
int scpType = 0;
char executable[ MAXPATHLEN ], *execargs[ 7 ];
char chmodExecutable[ MAXPATHLEN ], chmodPermissions[ 256 ], *chmodargs[ 6 ];
NSString *scpBinary;
scpBinary = @"/usr/bin/scp";
strcpy( executable, [ scpBinary UTF8String ] );
execargs[ 0 ] = executable;
scpconnecting = 1;
char* portarg = (char*)[[[NSNumber numberWithInt:portnumber]stringValue]UTF8String];
//execargs[ 1 ] = "-r";
// execargs[ 1 ] = "-q";
execargs[ 1 ] = "-P";
execargs[ 2 ] = portarg;
execargs[ 3 ] = ( scpType == 0 ? localfile : userathost );
execargs[ 4 ] = ( scpType == 0 ? userathost : localfile );
execargs[ 5 ] = NULL;
if (self.changePermissions) {
NSString *sshBinary = @"/usr/bin/ssh";
strcpy(chmodExecutable, [sshBinary UTF8String]);
strncpy(chmodPermissions, [self.permissionString UTF8String], 256);
NSString *servernameString = [NSString stringWithFormat:@"%@@%@",
self.username, url.host];
char* servername = (char*)[servernameString UTF8String];
NSString *filenameString = path;
char* filename = (char*)[filenameString UTF8String];
chmodargs[ 0 ] = chmodExecutable;
chmodargs[ 1 ] = servername;
chmodargs[ 2 ] = "chmod";
chmodargs[ 3 ] = chmodPermissions;
chmodargs[ 4 ] = filename;
chmodargs[ 5 ] = NULL;
}
BOOL scpStatus = [self execBlockingSSHExecutable:executable withArguments:execargs password:password];
if (!scpStatus) {
[pool release];
return;
}
if (self.changePermissions)
{
BOOL sshStatus = [self execBlockingSSHExecutable:chmodExecutable withArguments:chmodargs password:password];
if (!sshStatus) {
[pool release];
return;
}
}
[pool release];
if([self.delegate respondsToSelector:@selector(fileUploadDidSuccess:)])
[self.delegate performSelectorOnMainThread:@selector(fileUploadDidSuccess:)
withObject:self
waitUntilDone:NO];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)error:(NSString*)error {
if([self.delegate respondsToSelector:@selector(fileUpload:didFailWithError:)])
[self.delegate fileUpload:self
didFailWithError:error];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)write:(char *)buf
{
int wr;
if (( wr = write( self.masterfd, buf, strlen( buf ))) != strlen( buf ))
{
[self performSelectorOnMainThread:@selector(error:)
withObject:@"error"
waitUntilDone:YES];
return;
}
if (( wr = write( self.masterfd, "\n", strlen( "\n" ))) != strlen( "\n" ))
{
[self performSelectorOnMainThread:@selector(error:)
withObject:@"error"
waitUntilDone:YES];
return;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)parseProgressOutputString:(NSString *)line
{
if ([self.delegate respondsToSelector:@selector(fileUpload:didChangeProgression:
bytesRead:totalBytes:)])
{
NSString *percent = nil;
BOOL matched = [line getCapturesWithRegexAndReferences:@"( *)([0-9]*)%( *)", @"${2}",
&percent, nil];
if(matched) {
[self.delegate fileUpload:self
didChangeProgression:[percent floatValue] / 100
bytesRead:-1
totalBytes:-1];
}
}
}
@end