forked from MelodiaDev/ftpfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp.c
846 lines (805 loc) · 27.3 KB
/
ftp.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include "ftp.h"
#include "sock.h"
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/time.h>
#include <linux/stat.h>
int ftp_info_init(struct ftp_info **info, struct sockaddr_in addr, const char *user, const char *pass, int max_sock) {
*info = (struct ftp_info*)kmalloc(sizeof(struct ftp_info), GFP_KERNEL);
if (*info == NULL)
goto error0;
(*info)->user = (char*)kmalloc(strlen(user) + 1, GFP_KERNEL);
if ((*info)->user == NULL)
goto error1;
(*info)->pass = (char*)kmalloc(strlen(pass) + 1, GFP_KERNEL);
if ((*info)->pass == NULL)
goto error2;
(*info)->conn_list = (struct ftp_conn_info*)kmalloc(sizeof(struct ftp_conn_info) * max_sock, GFP_KERNEL);
if ((*info)->conn_list == NULL)
goto error3;
memcpy(&(*info)->addr, &addr, sizeof(struct sockaddr_in));
strcpy((*info)->user, user);
strcpy((*info)->pass, pass);
(*info)->max_sock = max_sock;
memset((*info)->conn_list, 0, sizeof(struct ftp_conn_info) * max_sock);
sema_init(&(*info)->sem, max_sock);
sema_init(&(*info)->mutex, 1);
return 0;
error3:
kfree((*info)->pass);
error2:
kfree((*info)->user);
error1:
kfree(*info);
error0:
return -ENOMEM;
}
void ftp_info_destroy(struct ftp_info *info) {
kfree(info->user);
kfree(info->pass);
kfree(info->conn_list);
kfree(info);
}
void ftp_file_info_destroy(unsigned long len, struct ftp_file_info *files) {
unsigned long i = 0;
for (; i < len; i++)
kfree(files[i].name);
kfree(files);
}
/* Close a session. */
static void ftp_conn_close(struct ftp_conn_info *conn) {
if (conn->data_sock != NULL) {
sock_release(conn->data_sock);
if (conn->cmd != NULL)
kfree(conn->cmd);
}
if (conn->control_sock != NULL)
sock_release(conn->control_sock);
conn->control_sock = conn->data_sock = NULL;
}
/* Send an FTP command. Return 0 for success and negative value for error.
* If there is an error in connection, this session is closed. */
static int ftp_conn_send(struct ftp_conn_info *conn, const char *cmd) {
int len, sent = 0, ret;
char *buf = (char*)kmalloc(strlen(cmd) + 3, GFP_KERNEL);
if (buf == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(buf, "%s\r\n", cmd);
len = strlen(buf);
while (sent < len) {
ret = sock_send(conn->control_sock, buf + sent, len - sent);
if (ret < 0)
goto error1;
sent += ret;
}
kfree(buf);
return 0;
error1:
kfree(buf);
error0:
ftp_conn_close(conn);
return ret;
}
/* Receive an FTP response. Return the status code for success and negative
* value for error. If <resp> is not NULL, store the first line of response
* in <resp>, which should later be kfree()d.
* If there is an error in connection or the response is not understood,
* the whole session is closed. */
static int ftp_conn_recv(struct ftp_conn_info *conn, char **resp) {
char *buf, *buf2;
int ret = sock_readline(conn->control_sock, &buf), code;
if (ret <= 0) {
if (ret == 0)
ret = -ECONNRESET;
goto error0;
}
if (ret < 6 || (buf[3] != ' ' && buf[3] != '-')
|| !isdigit(buf[0]) || !isdigit(buf[1]) || !isdigit(buf[2])
|| buf[0] == '0') {
ret = -EIO;
goto error1;
}
sscanf(buf, "%d", &code);
/* multiline response */
if (buf[3] == '-') {
while (1) {
ret = sock_readline(conn->control_sock, &buf2);
if (ret >= 4 && buf[0] == buf2[0] && buf[1] == buf2[1] && buf[2] == buf2[2] && buf2[3] == ' ') {
if (resp != NULL)
*resp = buf;
else
kfree(buf);
kfree(buf2);
return code;
}
if (ret <= 0) {
if (ret == 0)
ret = -ECONNRESET;
goto error1;
}
kfree(buf2);
}
/* single-line response */
} else {
if (resp != NULL)
*resp = buf;
else
kfree(buf);
return code;
}
error1:
kfree(buf);
error0:
ftp_conn_close(conn);
return ret;
}
/* Close the data transfer connection. After closing the connection, this
* function sends an ABOR command for confirmation, and if this transaction
* is not successful, the whole session is closed. */
static void ftp_conn_data_close(struct ftp_conn_info *conn) {
int ret;
if (conn->data_sock == NULL)
return;
pr_debug("closed: %s\n", conn->cmd);
sock_release(conn->data_sock);
conn->data_sock = NULL;
if (conn->cmd != NULL)
kfree(conn->cmd);
if (ftp_conn_send(conn, "ABOR") < 0 || ((ret = ftp_conn_recv(conn, NULL)) != 426 && ret != 226 && ret != 225)
|| (ret != 225 && (ret = ftp_conn_recv(conn, NULL)) != 225 && ret != 226))
ftp_conn_close(conn);
}
/* Connect to the FTP server, log in and set other stuffs. Return 0 for success
* and negative value for error. On error the session is not created. */
static int ftp_conn_connect(struct ftp_info *info, struct ftp_conn_info *conn) {
int ret, bufsize, tmp;
char *buf;
bufsize = strlen(info->user) + 6;
tmp = strlen(info->pass) + 6;
if (tmp > bufsize)
bufsize = tmp;
/* create control socket and connect to server */
if ((ret = sock_create(AF_INET, SOCK_STREAM, 0, &conn->control_sock)) < 0)
goto error0;
pr_debug("sock created, connecting to %u,%d\n", info->addr.sin_addr.s_addr, info->addr.sin_port);
if ((ret = conn->control_sock->ops->connect(conn->control_sock, (struct sockaddr*)&info->addr, sizeof(struct sockaddr_in), 0)) < 0)
goto error1;
pr_debug("connected to server\n");
/* receive initial response */
if ((ret = ftp_conn_recv(conn, NULL)) != 220) {
if (ret >= 0)
ret = -EPERM;
goto error1;
}
pr_debug("received initial response\n");
/* allocate buffer and do authentication */
buf = (char*)kmalloc(bufsize, GFP_KERNEL);
if (buf == NULL) {
ret = -ENOMEM;
goto error1;
}
sprintf(buf, "USER %s", info->user);
if ((ret = ftp_conn_send(conn, buf)) < 0 || ((ret = ftp_conn_recv(conn, NULL)) != 230 && ret != 331)) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
pr_debug("user ok\n");
if (ret == 331) {
sprintf(buf, "PASS %s", info->pass);
if ((ret = ftp_conn_send(conn, buf)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 230) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
}
pr_debug("password ok\n");
kfree(buf);
/* set transaction type to binary */
if ((ret = ftp_conn_send(conn, "TYPE I")) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 200) {
if (ret >= 0)
ret = -EIO;
goto error1;
}
pr_debug("TYPE I ok, connection opened\n");
return 0;
error2:
kfree(buf);
error1:
ftp_conn_close(conn);
error0:
return ret;
}
/* Open data transfer connection in PASV mode. Return 0 for success and
* negative value for error. On error the connection is not established. */
static int ftp_conn_open_pasv(struct ftp_conn_info *conn) {
char *resp, *ptr;
struct sockaddr_in data_addr;
int seg[6], i, ret;
/* send PASV command */
if ((ret = ftp_conn_send(conn, "PASV")) < 0)
goto error0;
if ((ret = ftp_conn_recv(conn, &resp)) != 227) {
if (ret >= 0) {
ret = -EIO;
kfree(resp);
}
goto error0;
}
pr_debug("pasv ok\n");
/* parse response and get IP and port to connect to */
ptr = resp + 4;
for (; *ptr != 0 && (*ptr < '0' || *ptr > '9'); ptr++);
if (*ptr == 0 || sscanf(ptr, "%d,%d,%d,%d,%d,%d", &seg[0], &seg[1], &seg[2], &seg[3], &seg[4], &seg[5]) < 6) {
kfree(resp);
ret = -EIO;
goto error0;
}
kfree(resp);
data_addr.sin_family = AF_INET;
for (i = 0; i < 6; i++)
if (seg[i] < 0 || seg[i] >= 256) {
ret = -EIO;
goto error0;
}
for (i = 0; i < 4; i++)
((unsigned char*)&data_addr.sin_addr)[i] = seg[i];
pr_debug("got pasv port no: %d\n", (seg[4] << 8) + seg[5]);
data_addr.sin_port = htons((seg[4] << 8) + seg[5]);
/* create data socket and connect to the given address */
if ((ret = sock_create(AF_INET, SOCK_STREAM, 0, &conn->data_sock)) < 0)
goto error0;
if ((ret = conn->data_sock->ops->connect(conn->data_sock, (struct sockaddr*)&data_addr, sizeof(struct sockaddr_in), 0)) < 0)
goto error1;
pr_debug("connected to pasv port, pasv succeeded\n");
return 0;
error1:
ftp_conn_data_close(conn);
error0:
return ret;
}
/* Find a session to use. If <cmd> is not NULL, it means that data transfer
* is also needed. */
static void ftp_find_conn(struct ftp_info *info, const char *cmd, unsigned long offset, struct ftp_conn_info **conn) {
int i;
down(&info->mutex);
if (cmd != NULL) {
/* if data transfer is needed and there is a session with desired
* state, return this session at once */
for (i = 0; i < info->max_sock; i++)
if (info->conn_list[i].used == 0 && info->conn_list[i].data_sock != NULL
&& strcmp(info->conn_list[i].cmd, cmd) == 0 && info->conn_list[i].offset == offset) {
info->conn_list[i].used = 1;
*conn = &info->conn_list[i];
up(&info->mutex);
return;
}
/* if there is not a suitable session, try to avoid deadlock: for
* RETR(read), close STOR(write)s on the same file; for STOR, close
* RETRs and other STORs on the same file */
for (i = 0; i < info->max_sock; i++)
if (info->conn_list[i].used == 0 && info->conn_list[i].data_sock != NULL
&& ((strncmp(info->conn_list[i].cmd, cmd, 4) != 0
&& strcmp(info->conn_list[i].cmd + 7, cmd + 7) == 0)
|| (strncmp(cmd, "STOR", 4) == 0
&& strcmp(info->conn_list[i].cmd, cmd) == 0))) {
info->conn_list[i].used = 1;
up(&info->mutex);
ftp_conn_data_close(&info->conn_list[i]);
down(&info->mutex);
info->conn_list[i].used = 0;
}
}
/* try to find a session with no data transfer currently */
for (i = 0; i < info->max_sock; i++)
if (info->conn_list[i].used == 0 && info->conn_list[i].data_sock == NULL) {
info->conn_list[i].used = 1;
*conn = &info->conn_list[i];
up(&info->mutex);
return;
}
/* there are only sessions with data transfer connections, close data
* transfer */
for (i = 0; i < info->max_sock; i++)
if (info->conn_list[i].used == 0) {
info->conn_list[i].used = 1;
*conn = &info->conn_list[i];
up(&info->mutex);
ftp_conn_data_close(*conn);
return;
}
/* this line should never be reached */
}
/* Release a session resource. */
static void ftp_release_conn(struct ftp_info *info, struct ftp_conn_info *conn) {
down(&info->mutex);
conn->used = 0;
up(&info->mutex);
up(&info->sem);
}
/* Request a session resource. This session should be established. On success,
* 0 is returned and the session is stored in <conn>. On error, negative value
* is returned and <conn> is not affected. */
static int ftp_request_conn(struct ftp_info *info, struct ftp_conn_info **conn) {
struct ftp_conn_info *tmp_conn;
int ret;
down(&info->sem);
/* find a session */
ftp_find_conn(info, NULL, 0, &tmp_conn);
/* if the session is not established, connect to FTP server */
if (tmp_conn->control_sock == NULL && (ret = ftp_conn_connect(info, tmp_conn)) < 0)
goto error;
*conn = tmp_conn;
return 0;
error:
ftp_release_conn(info, tmp_conn);
return ret;
}
/* Request a session resource. This session should be established and also its
* data transfer connection should be created for command <cmd> with offset
* <offset>. On success, 0 is returned and the session is stored in <conn>. On
* error, negative value is returned and <conn> is not affected. */
static int ftp_request_conn_open_pasv(struct ftp_info *info, struct ftp_conn_info **conn, const char *cmd, unsigned long offset) {
struct ftp_conn_info *tmp_conn;
char *tmp_cmd, buf[256];
int ret;
down(&info->sem);
/* find a session */
ftp_find_conn(info, cmd, offset, &tmp_conn);
/* if the session is already suitable, return immediately */
if (tmp_conn->data_sock != NULL) {
*conn = tmp_conn;
return 0;
}
/* if the session is not established, connect to FTP server */
if (tmp_conn->control_sock == NULL && (ret = ftp_conn_connect(info, tmp_conn)) < 0)
goto error0;
/* open data transfer connection */
if ((ret = ftp_conn_open_pasv(tmp_conn)) < 0)
goto error0;
tmp_conn->cmd = NULL;
/* set restarting offset */
if (offset) {
sprintf(buf, "REST %ld", offset);
if ((ret = ftp_conn_send(tmp_conn, buf)) < 0 || (ret = ftp_conn_recv(tmp_conn, NULL)) != 350) {
if (ret >= 0)
ret = -EIO;
goto error1;
}
}
/* send the command */
if ((ret = ftp_conn_send(tmp_conn, cmd)) < 0 || (ret = ftp_conn_recv(tmp_conn, NULL)) != 150) {
if (ret >= 0)
ret = -EPERM;
goto error1;
}
/* set corresponding <cmd> and <offset> in session info */
tmp_cmd = (char*)kmalloc(strlen(cmd) + 1, GFP_KERNEL);
if (tmp_cmd == NULL) {
ret = -ENOMEM;
goto error1;
}
strcpy(tmp_cmd, cmd);
tmp_conn->cmd = tmp_cmd;
tmp_conn->offset = offset;
*conn = tmp_conn;
return 0;
error1:
ftp_conn_data_close(tmp_conn);
error0:
ftp_release_conn(info, tmp_conn);
return ret;
}
int ftp_read_file(struct ftp_info *info, const char *file, unsigned long offset, char *buf, unsigned long len) {
struct ftp_conn_info *conn;
/* prepare command */
char *cmd = (char*)kmalloc(strlen(file) + 8, GFP_KERNEL);
int ret;
if (cmd == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "RETR ./%s", file);
/* request a session */
if ((ret = ftp_request_conn_open_pasv(info, &conn, cmd, offset)) < 0)
goto error1;
/* retrive data and increase <offset> in session info */
ret = sock_recv(conn->data_sock, buf, len);
if (ret < 0)
goto error2;
conn->offset += ret;
/* release the session */
ftp_release_conn(info, conn);
kfree(cmd);
return ret;
error2:
/* on error, data transfer connection is closed */
ftp_conn_data_close(conn);
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_write_file(struct ftp_info *info, const char *file, unsigned long offset, const char *buf, unsigned long len) {
/* see ftp_read_file() for explanation */
struct ftp_conn_info *conn;
char *cmd = (char*)kmalloc(strlen(file) + 8, GFP_KERNEL);
int ret;
if (cmd == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "STOR ./%s", file);
if ((ret = ftp_request_conn_open_pasv(info, &conn, cmd, offset)) < 0)
goto error1;
ret = sock_send(conn->data_sock, buf, len);
if (ret < 0)
goto error2;
conn->offset += ret;
ftp_release_conn(info, conn);
kfree(cmd);
return ret;
error2:
ftp_conn_data_close(conn);
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
void ftp_close_file(struct ftp_info *info, const char *file) {
int i;
down(&info->mutex);
/* all data transfer connections related to <file> is closed */
for (i = 0; i < info->max_sock; i++)
if (info->conn_list[i].used == 0 && info->conn_list[i].data_sock != NULL
&& strcmp(info->conn_list[i].cmd + 7, file) == 0) {
info->conn_list[i].used = 1;
up(&info->mutex);
ftp_conn_data_close(&info->conn_list[i]);
down(&info->mutex);
info->conn_list[i].used = 0;
}
up(&info->mutex);
}
int ftp_read_dir(struct ftp_info *info, const char *path, unsigned long *len, struct ftp_file_info **files) {
static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct ftp_conn_info *conn;
struct ftp_file_info *tmp_files;
int ret = 0, i = 0, current_year = 0, year = 0, month = 0, day = 0, hour = 0, min = 0;
unsigned long tmp_len, buf_len, tmp;
struct timeval time;
struct tm tm;
char *cmd = (char*)kmalloc(strlen(path) + 12, GFP_KERNEL), *line, *ptr, *next, next_backup;
if (cmd == NULL) {
ret = -ENOMEM;
goto error0;
}
/* prepare command */
sprintf(cmd, "LIST -al ./%s", path);
if ((ret = ftp_request_conn_open_pasv(info, &conn, cmd, 0)) < 0)
goto error1;
/* get current year */
/* XXX: due to flaw in FTP protocol, current year at server
* side cannot be determined, use that at client side as an approximation */
do_gettimeofday(&time);
time_to_tm(time.tv_sec, 0, &tm);
current_year = tm.tm_year;
/* allocate an array of 16 elements */
tmp_len = 0;
buf_len = 16;
tmp_files = (struct ftp_file_info*)kmalloc(buf_len * sizeof(struct ftp_file_info), GFP_KERNEL);
if (tmp_files == NULL) {
ret = -ENOMEM;
goto error2;
}
memset(tmp_files, 0, buf_len * sizeof(struct ftp_file_info));
/* read lines from server and parse them */
/* XXX: FTP protocol does not specify the format returned for LIST command,
* and here we assume the response is of the same format as ls(1) */
while ((ret = sock_readline(conn->data_sock, &line)) > 0) {
/* buffer full, allocate an array of doubled size and copy data */
if (tmp_len == buf_len) {
struct ftp_file_info *tmp_files2 = (struct ftp_file_info*)kmalloc(2 * buf_len * sizeof(struct ftp_file_info), GFP_KERNEL);
if (tmp_files2 == NULL) {
ret = -ENOMEM;
goto error3;
}
memcpy(tmp_files2, tmp_files, buf_len * sizeof(struct ftp_file_info));
memset(tmp_files2 + buf_len, 0, buf_len * sizeof(struct ftp_file_info));
kfree(tmp_files);
tmp_files = tmp_files2;
buf_len *= 2;
}
/* read the first 8 fields */
ptr = line;
for (i = 0; i < 8; i++) {
for (; *ptr != 0 && *ptr == ' '; ptr++);
if (*ptr == 0) {
ret = -EIO;
goto error4;
}
next = ptr;
for (; *next != 0 && *next != ' '; next++);
next_backup = *next;
*next = 0;
switch (i) {
/* mode */
case 0:
if (next - ptr != 10) {
ret = -EIO;
goto error4;
}
if (ptr[0] == 'd') tmp_files[tmp_len].mode |= S_IFDIR;
else if (ptr[0] == 'l') tmp_files[tmp_len].mode |= S_IFLNK;
else tmp_files[tmp_len].mode |= S_IFREG;
if (ptr[1] == 'r') tmp_files[tmp_len].mode |= S_IRUSR;
if (ptr[2] == 'w') tmp_files[tmp_len].mode |= S_IWUSR;
if (ptr[3] == 'x') tmp_files[tmp_len].mode |= S_IXUSR;
if (ptr[4] == 'r') tmp_files[tmp_len].mode |= S_IRGRP;
if (ptr[5] == 'w') tmp_files[tmp_len].mode |= S_IWGRP;
if (ptr[6] == 'x') tmp_files[tmp_len].mode |= S_IXGRP;
if (ptr[7] == 'r') tmp_files[tmp_len].mode |= S_IROTH;
if (ptr[8] == 'w') tmp_files[tmp_len].mode |= S_IWOTH;
if (ptr[9] == 'x') tmp_files[tmp_len].mode |= S_IXOTH;
break;
/* number of links */
case 1:
if (sscanf(ptr, "%lu", &tmp) < 1) {
ret = -EIO;
goto error4;
}
tmp_files[tmp_len].nlink = tmp;
break;
/* owner and group */
/* TODO: these fields should be taken into consideration */
case 2: case 3:
break;
/* file size */
case 4:
if (sscanf(ptr, "%lu", &tmp) < 1) {
ret = -EIO;
goto error4;
}
tmp_files[tmp_len].size = tmp;
break;
/* month, represented in abbreviated month name */
case 5:
if (next - ptr != 3) {
ret = -EIO;
goto error4;
}
month = 0;
for (; month < 12; month++)
if (strcmp(months[month], ptr) == 0)
break;
if (month == 12) {
ret = -EIO;
goto error4;
}
month++;
break;
/* day */
case 6:
if (sscanf(ptr, "%d", &day) < 1) {
ret = -EIO;
goto error4;
}
break;
/* hour:minute if last modification time is in the current
* year, or year */
case 7:
if (sscanf(ptr, "%d:%d", &hour, &min) == 2)
year = current_year;
else if (sscanf(ptr, "%d", &year) == 1)
/* XXX: set hour and minute to zero */
hour = min = 0;
else {
ret = -EIO;
goto error4;
}
/* XXX: set second to zero */
tmp_files[tmp_len].mtime = mktime(year, month, day, hour, min, 0);
break;
}
*next = next_backup;
ptr = next;
}
/* rest of the line is the name of the file */
/* TODO: link */
/* XXX: the name is succeeded by '-> target' if the file is a link,
* but if the file or target itself contains '->', ambiguity is
* present */
for (; *ptr != 0 && *ptr == ' '; ptr++);
if (*ptr == 0) {
ret = -EIO;
goto error4;
}
next = ptr + strlen(ptr);
/* strip line endings */
for (; next > ptr && (*(next - 1) == '\r' || *(next - 1) == '\n'); *(--next) = 0);
tmp_files[tmp_len].name = kmalloc(strlen(ptr) + 1, GFP_KERNEL);
if (tmp_files[tmp_len].name == NULL) {
ret = -ENOMEM;
goto error4;
}
strcpy(tmp_files[tmp_len].name, ptr);
tmp_len++;
/* free the space */
kfree(line);
}
if (ret < 0)
goto error3;
/* finalization, close data connection */
if ((ret = ftp_conn_recv(conn, NULL)) != 226) {
ret = -EIO;
goto error3;
}
ftp_conn_data_close(conn);
kfree(cmd);
*files = tmp_files;
*len = tmp_len;
ftp_release_conn(info, conn);
return 0;
error4:
kfree(line);
error3:
ftp_file_info_destroy(tmp_len, tmp_files);
error2:
ftp_conn_data_close(conn);
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_rename(struct ftp_info *info, const char *oldpath, const char *newpath) {
struct ftp_conn_info *conn;
char *cmd;
int bufsize, tmp, ret;
bufsize = strlen(oldpath) + 8;
tmp = strlen(newpath) + 8;
if (tmp > bufsize) bufsize = tmp;
if ((cmd = kmalloc(bufsize, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto error0;
}
/* request a session */
if ((ret = ftp_request_conn(info, &conn)) < 0)
goto error1;
/* first piece of command */
sprintf(cmd, "RNFR ./%s", oldpath);
if ((ret = ftp_conn_send(conn, cmd)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 350) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
/* second piece */
sprintf(cmd, "RNTO ./%s", newpath);
if ((ret = ftp_conn_send(conn, cmd)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 250) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
kfree(cmd);
ftp_release_conn(info, conn);
return 0;
error2:
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_create_file(struct ftp_info *info, const char *file) {
/* see ftp_read_file() for further explanation */
struct ftp_conn_info *conn;
char *cmd;
int ret;
if ((cmd = kmalloc(strlen(file) + 8, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "STOR ./%s", file);
if ((ret = ftp_request_conn_open_pasv(info, &conn, cmd, 0)) < 0)
goto error1;
ftp_conn_data_close(conn);
if ((ret = ftp_conn_recv(conn, NULL)) != 226) {
if (ret >= 0)
ret = -EIO;
goto error2;
}
kfree(cmd);
ftp_release_conn(info, conn);
return 0;
error2:
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_remove_file(struct ftp_info *info, const char *file) {
/* see ftp_rename() for explanation */
struct ftp_conn_info *conn;
char *cmd;
int ret;
if ((cmd = kmalloc(strlen(file) + 8, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "DELE ./%s", file);
if ((ret = ftp_request_conn(info, &conn)) < 0)
goto error1;
if ((ret = ftp_conn_send(conn, cmd)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 250) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
kfree(cmd);
ftp_release_conn(info, conn);
return 0;
error2:
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_create_dir(struct ftp_info *info, const char *path) {
/* see ftp_rename() for explanation */
struct ftp_conn_info *conn;
char *cmd;
int ret;
if ((cmd = kmalloc(strlen(path) + 7, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "MKD ./%s", path);
if ((ret = ftp_request_conn(info, &conn)) < 0)
goto error1;
if ((ret = ftp_conn_send(conn, cmd)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 257) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
kfree(cmd);
ftp_release_conn(info, conn);
return 0;
error2:
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}
int ftp_remove_dir(struct ftp_info *info, const char *path) {
/* see ftp_rename() for explanation */
struct ftp_conn_info *conn;
char *cmd;
int ret;
if ((cmd = kmalloc(strlen(path) + 7, GFP_KERNEL)) == NULL) {
ret = -ENOMEM;
goto error0;
}
sprintf(cmd, "RMD ./%s", path);
if ((ret = ftp_request_conn(info, &conn)) < 0)
goto error1;
if ((ret = ftp_conn_send(conn, cmd)) < 0 || (ret = ftp_conn_recv(conn, NULL)) != 250) {
if (ret >= 0)
ret = -EPERM;
goto error2;
}
kfree(cmd);
ftp_release_conn(info, conn);
return 0;
error2:
ftp_release_conn(info, conn);
error1:
kfree(cmd);
error0:
return ret;
}