-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobus_gridftp_server_posix.c
1575 lines (1408 loc) · 51.2 KB
/
globus_gridftp_server_posix.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************/
/* globus_gridftp_server_posix.c */
/* */
/* Auther: Wei Yang (Stanford Linear Accelerator Center, 2007) */
/* */
/* Globus Gridftp 4.x Data Storage Interface module using POSIX IO */
/* */
/* The following functions are copied from the original globus DSI */
/* module for "file" */
/* */
/* globus_l_gfs_file_copy_stat() */
/* globus_l_gfs_file_destroy_stat() */
/* globus_l_gfs_file_partition_path() */
/* globus_l_gfs_posix_stat() */
/* globus_l_gfs_file_delete_dir() */
/* */
/************************************************************************/
/* ChangeLog:
2009-03-16: Wei Yang [email protected]
* add Adler32 checksum. (need -lz when linking the .so)
2016-02-23: Wei Yang [email protected]
* Return errno for various operations
2017-04-13: Wei Yang [email protected]
* add MD5 checksum (need -lssl when linking the .so)
* Add GRIDFTP_APPEND_XROOTD_CGI
2017-05-02: Wei Yang [email protected]
* add support to GLOBUS_GFS_CMD_TRNC
GLOBUS_GFS_CMD_SITE_RDEL
GLOBUS_GFS_CMD_SITE_CHGRP
GLOBUS_GFS_CMD_SITE_UTIME
GLOBUS_GFS_CMD_SITE_SYMLINK
2020-09-28: Wei Yang [email protected]
* send progress mark during internal MD5 checksuming
*/
/* $Id: $ */
/************************************************************************/
/* How to compile: */
/* */
/* This file should be compiled along with the globus 4.0.x source code.*/
/* Please copy this file to source-trees/gridftp/server/src/dsi_bones */
/* and make adjustment to the Makefile. You may want to read the */
/* README.txt file in that directory first. It will be compiled to a */
/* shared library file libglobus_gridftp_server_posix_gcc32dbg.so. */
/************************************************************************/
/* How to use it with xrootd: */
/* */
/* This code does not use any Xrootd specific functions. So it should */
/* work with other types of storage in principle. However, we only */
/* tested it with xrootd. We have used it under vdt1.3.9, 1.6.1 and */
/* OSG 0.4.0, 0.6. */
/* */
/* The idea is to overload the Posix IO functions by those provided in */
/* the xrootd Posix interface. To do it, modify VDT's gridftp start up */
/* script to something like this: */
/* */
/* #!/bin/sh */
/* */
/* . /opt/vdt/setup.sh */
/* */
/* XRDLIB="/path_to_xrootd_lib_dir" */
/* if [ -z "$LD_LIBRARY_PATH" ]; then */
/* export LD_LIBRARY_PATH="$XRDLIB" */
/* else */
/* export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$XRDLIB" */
/* fi */
/* export LD_PRELOAD=$XRDLIB/libXrdPosixPreload.so */
/* export XROOTD_VMP="host:1094:/path1=/path2" */
/* */
/* exec /opt/vdt/globus/sbin/globus-gridftp-server -dsi posix */
/* */
/* Note that the option in the last line (-dsi posix) will load */
/* libglobus_gridftp_server_posix_gcc32dbg.so, So the .so should be in */
/* the search path of LD_LIBRARY_PATH */
/* */
/* Please refer to src/XrdPosix/README for description on how to use */
/* environment variable XROOTD_VMP */
/************************************************************************/
/*
ATLAS FTS adds an extra '/' before path. This cause a problem to xrootd
based storage because it uses a environment varialbe XROOTD_VMP to translate
gsiftp url to xrootd path. So these leading '/'s should be removed. Here
is the psuedo-code:
while (pathname[0] == '/' && pathname[1] == '/') { pathname++; }
*/
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>
#include <utime.h>
#include <dirent.h>
#include <errno.h>
#include <zlib.h>
#include <openssl/md5.h>
#include "globus_gridftp_server.h"
static
globus_version_t local_version =
{
0, /* major version number */
1, /* minor version number */
1170189432,
0 /* branch ID */
};
typedef struct globus_l_gfs_posix_handle_s
{
char * pathname;
int fd;
char seekable;
globus_size_t block_size;
globus_off_t block_length;
globus_off_t offset;
globus_bool_t done;
globus_gfs_operation_t op;
int optimal_count;
int outstanding;
globus_mutex_t mutex;
} globus_l_gfs_posix_handle_t;
char err_msg[256];
static int local_io_block_size = 0;
static int local_io_count = 0;
/*************************************************************************
* start
* -----
* This function is called when a new session is initialized, ie a user
* connectes to the server. This hook gives the dsi an oppertunity to
* set internal state that will be threaded through to all other
* function calls associated with this session. And an oppertunity to
* reject the user.
*
* finished_info.info.session.session_arg should be set to an DSI
* defined data structure. This pointer will be passed as the void *
* user_arg parameter to all other interface functions.
*
* NOTE: at nice wrapper function should exist that hides the details
* of the finished_info structure, but it currently does not.
* The DSI developer should jsut follow this template for now
************************************************************************/
static
void
globus_l_gfs_posix_start(
globus_gfs_operation_t op,
globus_gfs_session_info_t * session_info)
{
globus_l_gfs_posix_handle_t * posix_handle;
globus_gfs_finished_info_t finished_info;
struct passwd * pw;
GlobusGFSName(globus_l_gfs_posix_start);
globus_gridftp_server_set_checksum_support(op, "MD5:10");
posix_handle = (globus_l_gfs_posix_handle_t *)
globus_malloc(sizeof(globus_l_gfs_posix_handle_t));
posix_handle->fd = 0;
memset(&finished_info, '\0', sizeof(globus_gfs_finished_info_t));
finished_info.type = GLOBUS_GFS_OP_SESSION_START;
finished_info.result = GLOBUS_SUCCESS;
finished_info.info.session.session_arg = posix_handle;
finished_info.info.session.username = session_info->username;
pw = getpwuid(getuid());
finished_info.info.session.home_dir = pw->pw_dir;
globus_gridftp_server_operation_finished(
op, GLOBUS_SUCCESS, &finished_info);
}
/*************************************************************************
* destroy
* -------
* This is called when a session ends, ie client quits or disconnects.
* The dsi should clean up all memory they associated wit the session
* here.
************************************************************************/
static
void
globus_l_gfs_posix_destroy(
void * user_arg)
{
globus_l_gfs_posix_handle_t * posix_handle;
posix_handle = (globus_l_gfs_posix_handle_t *) user_arg;
globus_free(posix_handle);
}
/*************************************************************************
* stat
* ----
* This interface function is called whenever the server needs
* information about a given file or resource. It is called then an
* LIST is sent by the client, when the server needs to verify that
* a file exists and has the proper permissions, etc.
************************************************************************/
/*
static
void
globus_l_gfs_posix_stat(
globus_gfs_operation_t op,
globus_gfs_stat_info_t * stat_info,
void * user_arg)
{
globus_gfs_stat_t stat_array[1];
int stat_count = 1;
globus_l_gfs_posix_handle_t * posix_handle;
struct stat statbuf;
globus_result_t rc;
GlobusGFSName(globus_l_gfs_posix_stat);
posix_handle = (globus_l_gfs_posix_handle_t *) user_arg;
if (stat(PathName, &statbuf) == 0)
{
stat_array[0].mode = statbuf.st_mode;
stat_array[0].nlink = statbuf.st_nlink;
stat_array[0].uid = statbuf.st_uid;
stat_array[0].gid = statbuf.st_gid;
stat_array[0].size = statbuf.st_size;
stat_array[0].mtime = statbuf.st_mtime;
stat_array[0].atime = statbuf.st_atime;
stat_array[0].ctime = statbuf.st_ctime;
stat_array[0].dev = statbuf.st_dev;
stat_array[0].ino = statbuf.st_ino;
globus_gridftp_server_finished_stat(
op, GLOBUS_SUCCESS, stat_array, stat_count);
}
else
{
rc = GlobusGFSErrorGeneric("stat() fail");
globus_gridftp_server_finished_stat(op, rc, NULL, 0);
}
}
*/
void
globus_l_gfs_file_copy_stat(
globus_gfs_stat_t * stat_object,
struct stat * stat_buf,
const char * filename,
const char * symlink_target)
{
GlobusGFSName(globus_l_gfs_file_copy_stat);
stat_object->mode = stat_buf->st_mode;
stat_object->nlink = stat_buf->st_nlink;
stat_object->uid = stat_buf->st_uid;
stat_object->gid = stat_buf->st_gid;
stat_object->size = stat_buf->st_size;
stat_object->mtime = stat_buf->st_mtime;
stat_object->atime = stat_buf->st_atime;
stat_object->ctime = stat_buf->st_ctime;
stat_object->dev = stat_buf->st_dev;
stat_object->ino = stat_buf->st_ino;
if(filename && *filename)
{
stat_object->name = strdup(filename);
}
else
{
stat_object->name = NULL;
}
if(symlink_target && *symlink_target)
{
stat_object->symlink_target = strdup(symlink_target);
}
else
{
stat_object->symlink_target = NULL;
}
}
static
void
globus_l_gfs_file_destroy_stat(
globus_gfs_stat_t * stat_array,
int stat_count)
{
int i;
GlobusGFSName(globus_l_gfs_file_destroy_stat);
for(i = 0; i < stat_count; i++)
{
if(stat_array[i].name != NULL)
{
globus_free(stat_array[i].name);
}
if(stat_array[i].symlink_target != NULL)
{
globus_free(stat_array[i].symlink_target);
}
}
globus_free(stat_array);
}
/* basepath and filename must be MAXPATHLEN long
* the pathname may be absolute or relative, basepath will be the same */
static
void
globus_l_gfs_file_partition_path(
const char * pathname,
char * basepath,
char * filename)
{
char buf[MAXPATHLEN];
char * filepart;
GlobusGFSName(globus_l_gfs_file_partition_path);
strncpy(buf, pathname, MAXPATHLEN);
buf[MAXPATHLEN - 1] = '\0';
filepart = strrchr(buf, '/');
while(filepart && !*(filepart + 1) && filepart != buf)
{
*filepart = '\0';
filepart = strrchr(buf, '/');
}
if(!filepart)
{
strcpy(filename, buf);
basepath[0] = '\0';
}
else
{
if(filepart == buf)
{
if(!*(filepart + 1))
{
basepath[0] = '\0';
filename[0] = '/';
filename[1] = '\0';
}
else
{
*filepart++ = '\0';
basepath[0] = '/';
basepath[1] = '\0';
strcpy(filename, filepart);
}
}
else
{
*filepart++ = '\0';
strcpy(basepath, buf);
strcpy(filename, filepart);
}
}
}
static
void
globus_l_gfs_posix_stat(
globus_gfs_operation_t op,
globus_gfs_stat_info_t * stat_info,
void * user_arg)
{
globus_result_t result;
struct stat stat_buf;
globus_gfs_stat_t * stat_array;
int stat_count = 0;
DIR * dir;
char basepath[MAXPATHLEN];
char filename[MAXPATHLEN];
char symlink_target[MAXPATHLEN];
char * PathName;
GlobusGFSName(globus_l_gfs_posix_stat);
PathName=stat_info->pathname;
/*
If we do stat_info->pathname++, it will cause third-party transfer
hanging if there is a leading // in path. Don't know why. To work
around, we replaced it with PathName.
*/
while (PathName[0] == '/' && PathName[1] == '/')
{
PathName++;
}
/* lstat is the same as stat when not operating on a link */
if(lstat(PathName, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("stat", errno);
goto error_stat1;
}
/* if this is a link we still need to stat to get the info we are
interested in and then use realpath() to get the full path of
the symlink target */
*symlink_target = '\0';
if(S_ISLNK(stat_buf.st_mode))
{
if(stat(PathName, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("stat", errno);
goto error_stat1;
}
if(realpath(PathName, symlink_target) == NULL)
{
result = GlobusGFSErrorSystemError("realpath", errno);
goto error_stat1;
}
}
globus_l_gfs_file_partition_path(PathName, basepath, filename);
if(!S_ISDIR(stat_buf.st_mode) || stat_info->file_only)
{
stat_array = (globus_gfs_stat_t *)
globus_malloc(sizeof(globus_gfs_stat_t));
if(!stat_array)
{
result = GlobusGFSErrorMemory("stat_array");
goto error_alloc1;
}
globus_l_gfs_file_copy_stat(
stat_array, &stat_buf, filename, symlink_target);
stat_count = 1;
}
else
{
struct dirent * dir_entry;
int i;
char dir_path[MAXPATHLEN];
dir = opendir(PathName);
if(!dir)
{
result = GlobusGFSErrorSystemError("opendir", errno);
goto error_open;
}
stat_count = 0;
while(globus_libc_readdir_r(dir, &dir_entry) == 0 && dir_entry)
{
stat_count++;
globus_free(dir_entry);
}
rewinddir(dir);
stat_array = (globus_gfs_stat_t *)
globus_malloc(sizeof(globus_gfs_stat_t) * stat_count);
if(!stat_array)
{
result = GlobusGFSErrorMemory("stat_array");
goto error_alloc2;
}
snprintf(dir_path, sizeof(dir_path), "%s/%s", basepath, filename);
dir_path[MAXPATHLEN - 1] = '\0';
for(i = 0;
globus_libc_readdir_r(dir, &dir_entry) == 0 && dir_entry;
i++)
{
char tmp_path[MAXPATHLEN];
char *path;
snprintf(tmp_path, sizeof(tmp_path), "%s/%s", dir_path, dir_entry->d_name);
tmp_path[MAXPATHLEN - 1] = '\0';
path=tmp_path;
/* function globus_l_gfs_file_partition_path() seems to add two
extra '/'s to the beginning of tmp_path. XROOTD is sensitive
to the extra '/'s not defined in XROOTD_VMP so we remove them */
if (path[0] == '/' && path[1] == '/') { path++; }
while (path[0] == '/' && path[1] == '/') { path++; }
/* lstat is the same as stat when not operating on a link */
if(lstat(path, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("lstat", errno);
globus_free(dir_entry);
/* just skip invalid entries */
stat_count--;
i--;
continue;
}
/* if this is a link we still need to stat to get the info we are
interested in and then use realpath() to get the full path of
the symlink target */
*symlink_target = '\0';
if(S_ISLNK(stat_buf.st_mode))
{
if(stat(path, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("stat", errno);
globus_free(dir_entry);
/* just skip invalid entries */
stat_count--;
i--;
continue;
}
if(realpath(path, symlink_target) == NULL)
{
result = GlobusGFSErrorSystemError("realpath", errno);
globus_free(dir_entry);
/* just skip invalid entries */
stat_count--;
i--;
continue;
}
}
globus_l_gfs_file_copy_stat(
&stat_array[i], &stat_buf, dir_entry->d_name, symlink_target);
globus_free(dir_entry);
}
if(i != stat_count)
{
result = GlobusGFSErrorSystemError("readdir", errno);
goto error_read;
}
closedir(dir);
}
globus_gridftp_server_finished_stat(
op, GLOBUS_SUCCESS, stat_array, stat_count);
globus_l_gfs_file_destroy_stat(stat_array, stat_count);
return;
error_read:
globus_l_gfs_file_destroy_stat(stat_array, stat_count);
error_alloc2:
closedir(dir);
error_open:
error_alloc1:
error_stat1:
globus_gridftp_server_finished_stat(op, result, NULL, 0);
/* GlobusGFSFileDebugExitWithError(); */
}
/* recursively delete a directory */
static
globus_result_t
globus_l_gfs_file_delete_dir(
const char * pathname)
{
globus_result_t result;
int rc;
DIR * dir;
struct stat stat_buf;
struct dirent * dir_entry;
int i;
char path[MAXPATHLEN];
GlobusGFSName(globus_l_gfs_file_delete_dir);
GlobusGFSFileDebugEnter();
/* lstat is the same as stat when not operating on a link */
if(lstat(pathname, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("stat", errno);
goto error_stat;
}
if(!S_ISDIR(stat_buf.st_mode))
{
/* remove anything that isn't a dir -- don't follow links */
rc = unlink(pathname);
if(rc != 0)
{
result = GlobusGFSErrorSystemError("unlink", errno);
goto error_unlink1;
}
}
else
{
dir = globus_libc_opendir(pathname);
if(!dir)
{
result = GlobusGFSErrorSystemError("opendir", errno);
goto error_open;
}
for(i = 0;
globus_libc_readdir_r(dir, &dir_entry) == 0 && dir_entry;
i++)
{
if(dir_entry->d_name[0] == '.' &&
(dir_entry->d_name[1] == '\0' ||
(dir_entry->d_name[1] == '.' && dir_entry->d_name[2] == '\0')))
{
globus_free(dir_entry);
continue;
}
snprintf(path, sizeof(path), "%s/%s", pathname, dir_entry->d_name);
path[MAXPATHLEN - 1] = '\0';
/* lstat is the same as stat when not operating on a link */
if(lstat(path, &stat_buf) != 0)
{
result = GlobusGFSErrorSystemError("lstat", errno);
globus_free(dir_entry);
/* just skip invalid entries */
continue;
}
if(!S_ISDIR(stat_buf.st_mode))
{
/* remove anything that isn't a dir -- don't follow links */
rc = unlink(path);
if(rc != 0)
{
result = GlobusGFSErrorSystemError("unlink", errno);
goto error_unlink2;
}
}
else
{
result = globus_l_gfs_file_delete_dir(path);
if(result != GLOBUS_SUCCESS)
{
goto error_recurse;
}
}
globus_free(dir_entry);
}
closedir(dir);
rc = rmdir(pathname);
if(rc != 0)
{
result = GlobusGFSErrorSystemError("rmdir", errno);
goto error_rmdir;
}
}
GlobusGFSFileDebugExit();
return GLOBUS_SUCCESS;
error_recurse:
error_unlink2:
closedir(dir);
globus_free(dir_entry);
error_open:
error_stat:
error_unlink1:
error_rmdir:
GlobusGFSFileDebugExitWithError();
return result;
}
/* change group */
static
globus_result_t
globus_l_gfs_posix_chgrp(
const char * pathname,
const char * group)
{
int rc;
globus_result_t result;
struct group * grp_info;
int grp_id;
char* endpt;
GlobusGFSName(globus_l_gfs_posix_chgrp);
grp_info = getgrnam(group);
if(grp_info != NULL)
{
grp_id = grp_info->gr_gid;
}
else
{
grp_id = strtol(group, &endpt, 10);
if(*group == '\0' || *endpt != '\0')
{
errno = EPERM;
return GLOBUS_FAILURE;
}
}
if(grp_id < 0) {
errno = EPERM;
return GLOBUS_FAILURE;
}
if (chown(pathname, -1, grp_id) != 0) return GLOBUS_FAILURE;
return GLOBUS_SUCCESS;
}
/* cksm from external call main initially include path */
char cksm[512];
/*************************************************************************
* Adler23 checksum
************************************************************************/
globus_result_t
globus_l_gfs_posix_cksm_adler32(
globus_gfs_operation_t op,
char * filename)
{
int rc, fd, len;
char *ext_adler32, buf[65536], ext_cmd[1024], *pt;
FILE *F;
struct stat stbuf;
uLong adler;
ext_adler32 = NULL;
if ((ext_adler32 = getenv("GRIDFTP_CKSUM_EXT_ADLER32")) != NULL)
{
strcpy(ext_cmd, ext_adler32);
strcat(ext_cmd, " ");
strcat(ext_cmd, filename);
F = popen(ext_cmd, "r");
if (F == NULL) return GLOBUS_FAILURE;
fscanf(F, "%s", cksm);
pclose(F);
pt = strchr(cksm, ' ');
if (pt != NULL) pt[0] = '\0'; /* take the first string */
}
else /* calculate adler32 */
{
rc = stat(filename, &stbuf);
if (rc != 0 || ! S_ISREG(stbuf.st_mode) || (fd = open(filename,O_RDONLY)) < 0)
return GLOBUS_FAILURE;
adler = adler32(0L, Z_NULL, 0);
while ((len = read(fd, buf, 65536)) > 0)
adler = adler32(adler, buf, len);
close(fd);
sprintf(cksm, "%08x", adler);
cksm[8] = '\0';
}
globus_gridftp_server_finished_command(op, GLOBUS_SUCCESS, cksm);
return GLOBUS_SUCCESS;
}
/*************************************************************************
* MD5 checksum
************************************************************************/
struct globus_l_gfs_posix_cksm_md5_cb_t
{
globus_gfs_operation_t op;
MD5_CTX c;
int fd;
globus_off_t fsize;
globus_off_t blocksize;
globus_off_t offset;
globus_off_t length;
globus_off_t total_bytes;
int marker_freq;
time_t t_lastmarker;
};
#define MAXBLOCSIZE4CKSM 4*1024*1024
void
globus_l_gfs_posix_cksm_md5_cb(
void * user_arg)
{
char buffer[MAXBLOCSIZE4CKSM+1];
globus_off_t readlen;
globus_result_t result;
unsigned char md5digest[MD5_DIGEST_LENGTH];
int i;
time_t t;
struct globus_l_gfs_posix_cksm_md5_cb_t * md5updt;
md5updt = (struct globus_l_gfs_posix_cksm_md5_cb_t *) user_arg;
if ( md5updt->length == 0 )
{
close(md5updt->fd);
MD5_Final(md5digest, &(md5updt->c));
for(i = 0; i < MD5_DIGEST_LENGTH; ++i)
sprintf(&cksm[i*2], "%02x", (unsigned int)md5digest[i]);
cksm[MD5_DIGEST_LENGTH*2+1] = '\0';
globus_gridftp_server_finished_command(md5updt->op, GLOBUS_SUCCESS, cksm);
globus_free(md5updt);
}
else
{
readlen = read(md5updt->fd, buffer, ( md5updt->length > md5updt->blocksize ?
md5updt->blocksize : md5updt->length ) );
if (readlen > 0 && errno == 0)
{
md5updt->offset += readlen;
md5updt->length -= readlen;
md5updt->total_bytes += readlen;
MD5_Update(&(md5updt->c), buffer, readlen);
t = time(NULL);
if ( (t - md5updt->t_lastmarker) > md5updt->marker_freq )
{
md5updt->t_lastmarker = t;
char count[128];
sprintf(count, "%"GLOBUS_OFF_T_FORMAT, md5updt->total_bytes);
globus_gridftp_server_intermediate_command(md5updt->op, GLOBUS_SUCCESS, count);
}
}
else
{ // something isn't right, we will end getting a timeout
errno = 0;
sleep(2);
}
result = globus_callback_register_oneshot( NULL,
NULL,
globus_l_gfs_posix_cksm_md5_cb,
md5updt);
if(result != GLOBUS_SUCCESS)
{
result = GlobusGFSErrorWrapFailed(
"globus_callback_register_oneshot", result);
globus_panic(NULL, result, "oneshot failed, no recovery");
}
}
}
globus_result_t
globus_l_gfs_posix_cksm_md5(
globus_gfs_operation_t op,
char * filename,
globus_off_t offset,
globus_off_t length)
{
char *ext_md5, ext_cmd[1024], *pt;
FILE *F;
int rc, fd;
struct stat stbuf;
globus_result_t result;
ext_md5 = NULL;
if ((ext_md5 = getenv("GRIDFTP_CKSUM_EXT_MD5")) != NULL)
{
strcpy(ext_cmd, ext_md5);
strcat(ext_cmd, " ");
strcat(ext_cmd, filename);
F = popen(ext_cmd, "r");
if (F == NULL) return GLOBUS_FAILURE;
fscanf(F, "%s", cksm);
pclose(F);
pt = strchr(cksm, ' ');
if (pt != NULL) pt[0] = '\0'; /* take the first string */
globus_gridftp_server_finished_command(op, GLOBUS_SUCCESS, cksm);
}
else /* calculate md5 */
{
struct globus_l_gfs_posix_cksm_md5_cb_t * md5updt;
rc = stat(filename, &stbuf);
if (rc != 0 || ! S_ISREG(stbuf.st_mode) || (fd = open(filename,O_RDONLY)) < 0)
return GLOBUS_FAILURE;
md5updt = globus_malloc(sizeof( struct globus_l_gfs_posix_cksm_md5_cb_t));
MD5_Init(&(md5updt->c));
if (length < 0 || (offset + length) > stbuf.st_size)
length = stbuf.st_size - offset;
lseek(fd, offset, SEEK_SET);
md5updt->op = op;
md5updt->fd = fd;
md5updt->blocksize = MAXBLOCSIZE4CKSM;
md5updt->fsize = stbuf.st_size;
md5updt->offset = offset;
md5updt->length = length;
md5updt->total_bytes = 0;
globus_gridftp_server_get_update_interval(op, &md5updt->marker_freq);
md5updt->t_lastmarker = time(NULL);
result = globus_callback_register_oneshot( NULL,
NULL,
globus_l_gfs_posix_cksm_md5_cb,
md5updt);
if(result != GLOBUS_SUCCESS)
{
result = GlobusGFSErrorWrapFailed(
"globus_callback_register_oneshot", result);
globus_panic(NULL, result, "oneshot failed, no recovery");
}
}
return GLOBUS_SUCCESS;
}
/*************************************************************************
* command
* -------
* This interface function is called when the client sends a 'command'.
* commands are such things as mkdir, remdir, delete. The complete
* enumeration is below.
*
* To determine which command is being requested look at:
* cmd_info->command
*
* GLOBUS_GFS_CMD_MKD = 1,
* GLOBUS_GFS_CMD_RMD,
* GLOBUS_GFS_CMD_DELE,
* GLOBUS_GFS_CMD_RNTO,
* GLOBUS_GFS_CMD_RNFR,
* GLOBUS_GFS_CMD_CKSM,
* GLOBUS_GFS_CMD_SITE_CHMOD,
* GLOBUS_GFS_CMD_SITE_DSI
************************************************************************/
static
void
globus_l_gfs_posix_command(
globus_gfs_operation_t op,
globus_gfs_command_info_t * cmd_info,
void * user_arg)
{
char * PathName;
globus_l_gfs_posix_handle_t * posix_handle;
globus_result_t rc;
struct utimbuf ubuf;
GlobusGFSName(globus_l_gfs_posix_command);
posix_handle = (globus_l_gfs_posix_handle_t *) user_arg;
PathName=cmd_info->pathname;
while (PathName[0] == '/' && PathName[1] == '/')
{
PathName++;
}
rc = GLOBUS_SUCCESS;
switch(cmd_info->command)
{
case GLOBUS_GFS_CMD_MKD:
(mkdir(PathName, 0777) == 0) ||
(rc = GlobusGFSErrorSystemError("mkdir", errno));
break;
case GLOBUS_GFS_CMD_RMD:
(rmdir(PathName) == 0) ||
(rc = GlobusGFSErrorSystemError("rmdir", errno));
break;
case GLOBUS_GFS_CMD_DELE:
(unlink(PathName) == 0) ||
(rc = GlobusGFSErrorSystemError("unlink", errno));
break;
case GLOBUS_GFS_CMD_TRNC:
(truncate(PathName, cmd_info->cksm_offset) == 0) ||
(rc = GlobusGFSErrorSystemError("truncate", errno));
break;
case GLOBUS_GFS_CMD_SITE_RDEL:
rc = globus_l_gfs_file_delete_dir(PathName);
break;
case GLOBUS_GFS_CMD_RNTO:
(rename(cmd_info->rnfr_pathname, PathName) == 0) ||
(rc = GlobusGFSErrorSystemError("rename", errno));
break;
case GLOBUS_GFS_CMD_SITE_CHMOD:
(chmod(PathName, cmd_info->chmod_mode) == 0) ||
(rc = GlobusGFSErrorSystemError("chmod", errno));
break;
case GLOBUS_GFS_CMD_SITE_CHGRP:
(globus_l_gfs_posix_chgrp(PathName, cmd_info->chgrp_group) == 0) ||
(rc = GlobusGFSErrorSystemError("chgrp", errno));
break;
case GLOBUS_GFS_CMD_SITE_UTIME:
ubuf.modtime = cmd_info->utime_time;
ubuf.actime = time(NULL);
(utime(PathName, &ubuf) == 0) ||
(rc = GlobusGFSErrorSystemError("utime", errno));
break;
case GLOBUS_GFS_CMD_SITE_SYMLINK:
(symlink(cmd_info->from_pathname, cmd_info->pathname) == 0) ||
(rc = GlobusGFSErrorSystemError("symlink", errno));
break;
case GLOBUS_GFS_CMD_CKSM:
if (!strcmp(cmd_info->cksm_alg, "adler32") ||
!strcmp(cmd_info->cksm_alg, "ADLER32"))
rc = globus_l_gfs_posix_cksm_adler32(op,
PathName);
else if (!strcmp(cmd_info->cksm_alg, "md5") ||
!strcmp(cmd_info->cksm_alg, "MD5"))
rc = globus_l_gfs_posix_cksm_md5(op,