From fc0d7ffc24b7fb01291492f81c3d84773fe93bf7 Mon Sep 17 00:00:00 2001 From: cvs2svn Date: Wed, 25 Jan 2006 15:30:38 +0000 Subject: [PATCH] This commit was manufactured by cvs2svn to create tag 'gridsite- core_R_1_1_17'. Sprout from glite_branch_1_5_0 2006-01-25 15:30:37 UTC Andrew McNab '-fPIC for ia64' Cherrypick from master 2005-11-25 14:24:12 UTC yibiao 'Initial revision: Server-side CGI for remote copy': org.gridsite.core/src/gridsite-copy.c --- org.gridsite.core/src/gridsite-copy.c | 155 ++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 org.gridsite.core/src/gridsite-copy.c diff --git a/org.gridsite.core/src/gridsite-copy.c b/org.gridsite.core/src/gridsite-copy.c new file mode 100644 index 0000000..d59231f --- /dev/null +++ b/org.gridsite.core/src/gridsite-copy.c @@ -0,0 +1,155 @@ +/* + Copyright (c) 2005, Yibiao Li, University of Manchester + All rights reserved. + + Redistribution and use in source and binary forms, with or + without modification, are permitted provided that the following + conditions are met: + + o Redistributions of source code must retain the above + copyright notice, this list of conditions and the following + disclaimer. + o Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/////////////////////////////////////////////////////////////////// +// +// compile: gcc -lcurl gridsite-copy.c -o gridsite-copy.cgi +// usage: cp gridsite-copy.cgi to the cgi-bin directory +// and map the COPY method to gridsite-copy.cgi +// by adding a line in httpd.conf: +// script COPY /cgi-bin/gridsite-copy.cgi +// +/////////////////////////////////////////////////////////////////// +#include +#include +#include +#include +#include +#include +#include + +extern char **environ; + +size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) +{ + int written = fwrite(ptr, size, nmemb, (FILE *)stream); + return written; +} + +int main( int argn, char **argv ) +{ + char *getenv(); + + CURL *curl; + CURLcode res; + struct tms s_time, e_time; + FILE *fout; + + char *requestURI; + int grstPerm, srcsecure; + char passcode[100]; + char destination[500], destDir[400], destName[100]; + char *ptr, *ptr1; + + times(&s_time); + passcode[0]='\0'; + char *capath="/etc/grid-security/certificates"; + + printf("Content-type: text/html\n\n"); + printf("HTTP COPY\n"); + printf("

HTTP FILE COPY

\n"); + + curl = curl_easy_init(); + printf("Server: Initialized!\n"); + if(curl) { + //get the request URI + requestURI = curl_getenv("REQUEST_URI"); + if( strncmp( requestURI, "https://", 8 )==0 )srcsecure=1; + else srcsecure=0; + printf("The request URL is %s\n", requestURI); + + //get the destination directory and file name + strcpy(destination, getenv("HTTP_DESTINATION")); + ptr=destination; + ptr1 = strrchr(ptr, '/'); + ptr1+=1; + strcpy( destName, ptr1 ); + *ptr1 = '\0'; + strcpy( destDir, ptr ); + + // get the one time passcode from cookie string. + // the segmenty of code is tested on 19th sep. 2005 + if( (ptr=curl_getenv("HTTP_COOKIE")) != NULL) + { + ptr += 17; + strcpy( passcode, ptr ); + } + + //get permision attributes + grstPerm = atoi(curl_getenv("GRST_DESTINATION_PERM")); + + if( grstPerm & 8 ) // write right + { + curl_easy_setopt(curl, CURLOPT_VERBOSE, 0); + + if( srcsecure == 1 ) + { + curl_easy_setopt(curl, CURLOPT_COOKIE, passcode ); + curl_easy_setopt(curl, CURLOPT_CAPATH, capath ); + } + + curl_easy_setopt(curl, CURLOPT_URL, requestURI ); + + strcpy( destination, getenv("GRST_DESTINATION_TRANSLATED")); + fout = fopen( destination, "w" ); + if( fout == NULL ){ + printf("cannot open file to write,"); + printf(" maybe you have no right to write in the directory.\n"); + exit(-1); + } + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fout ); + res = curl_easy_perform(curl); + if( res!=0 ) + { + printf("Server: There are some things wrong with OPT parameters.%d \n", res); + } + else printf("Server: The file has been successfully copied.\n"); + fclose(fout); + } + else + { + printf("You have no permission to write in the destination directory.\n"); + } + + curl_easy_cleanup(curl); + } + else{ + printf("Server: cannot initialize CURL!\n"); + } + + curl_global_cleanup(); + + times(&e_time); + printf("Server: copying time %ld seconds\n", e_time.tms_utime-s_time.tms_utime); + printf("\n"); + return 0; +} -- 1.8.2.3