Back to index

nordugrid-arc-nox  1.1.0~rc6
proxy.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <fcntl.h>
00010 #include <unistd.h>
00011 #include <string.h>
00012 
00013 #include "proxy.h"
00014 
00015 #include <arc/Utils.h>
00016 
00017 int prepare_proxy(void) {
00018   int h = -1;
00019   off_t len;
00020   char* buf = NULL;
00021   off_t l,ll;
00022   int res=-1;
00023 
00024   if(getuid() == 0) { /* create temporary proxy */
00025     std::string proxy_file=Arc::GetEnv("X509_USER_PROXY");
00026     if(proxy_file.empty()) goto exit;
00027     h=open(proxy_file.c_str(),O_RDONLY);
00028     if(h==-1) goto exit;
00029     if((len=lseek(h,0,SEEK_END))==-1) goto exit;
00030     if(lseek(h,0,SEEK_SET) != 0) goto exit;
00031     buf=(char*)malloc(len);
00032     if(buf==NULL) goto exit;
00033     for(l=0;l<len;) {
00034       ll=read(h,buf+l,len-l);
00035       if(ll==-1) goto exit;
00036       if(ll==0) break;
00037       l+=ll;
00038     };
00039     close(h); h=-1; len=l;
00040     std::string proxy_file_tmp = proxy_file;
00041     proxy_file_tmp+=".tmp";
00042     h=open(proxy_file_tmp.c_str(),O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR);
00043     if(h==-1) goto exit;
00044     (void)chmod(proxy_file_tmp.c_str(),S_IRUSR | S_IWUSR);
00045     for(l=0;l<len;) {
00046       ll=write(h,buf+l,len-l);
00047       if(ll==1) goto exit;
00048       l+=ll;
00049     };
00050     close(h); h=-1; 
00051     Arc::SetEnv("X509_USER_PROXY",proxy_file_tmp);
00052   };
00053   res=0;
00054  exit:
00055   if(buf) free(buf);
00056   if(h!=-1) close(h);
00057   return res;
00058 }
00059 
00060 int remove_proxy(void) {
00061   if(getuid() == 0) {
00062     std::string proxy_file=Arc::GetEnv("X509_USER_PROXY");
00063     if(proxy_file.empty()) return 0;
00064     remove(proxy_file.c_str());
00065   };
00066   return 0;
00067 }
00068 
00069 int renew_proxy(const char* old_proxy,const char* new_proxy) {
00070   int h = -1;
00071   off_t len,l,ll;
00072   char* buf = NULL;
00073   std::string proxy_file_tmp;
00074   struct stat st;
00075   int res = -1;
00076 
00077   h=open(new_proxy,O_RDONLY);
00078   if(h==-1) {
00079     fprintf(stderr,"Can't open new proxy: %s\n",new_proxy);
00080     goto exit;
00081   };
00082   if((len=lseek(h,0,SEEK_END))==-1) goto exit;
00083   lseek(h,0,SEEK_SET);
00084   if((buf=(char*)(malloc(len))) == NULL) {
00085     fprintf(stderr,"Out of memory\n");
00086     goto exit;
00087   };
00088   for(l=0;l<len;) {
00089     ll=read(h,buf+l,len-l);
00090     if(ll==-1) {
00091       fprintf(stderr,"Can't read new proxy: %s\n",new_proxy);
00092       goto exit;
00093     };
00094     if(ll==0) break;
00095     l+=ll;
00096   };
00097   close(h); h=-1; len=l;
00098   proxy_file_tmp=old_proxy;
00099   proxy_file_tmp+=".renew";
00100   remove(proxy_file_tmp.c_str());
00101   h=open(proxy_file_tmp.c_str(),O_WRONLY | O_CREAT | O_EXCL,S_IRUSR | S_IWUSR);
00102   if(h==-1) {
00103     fprintf(stderr,"Can't create temporary proxy: %s\n",proxy_file_tmp.c_str());
00104     goto exit;
00105   };
00106   (void)chmod(proxy_file_tmp.c_str(),S_IRUSR | S_IWUSR);
00107   for(l=0;l<len;) {
00108     ll=write(h,buf+l,len-l);
00109     if(ll==-1) {
00110       fprintf(stderr,"Can't write temporary proxy: %s\n",proxy_file_tmp.c_str());
00111       goto exit;
00112     };
00113     l+=ll;
00114   };
00115   if(stat(old_proxy,&st) == 0) {
00116     fchown(h,st.st_uid,st.st_gid);
00117     if(remove(old_proxy) != 0) {
00118       fprintf(stderr,"Can't remove proxy: %s\n",old_proxy);
00119       goto exit;
00120     };
00121   };
00122   close(h); h=-1;
00123   if(rename(proxy_file_tmp.c_str(),old_proxy) != 0) {
00124     fprintf(stderr,"Can't rename temporary proxy: %s\n",proxy_file_tmp.c_str());
00125     goto exit;
00126   };
00127   res=0;
00128  exit:
00129   if(h!=-1) close(h);
00130   if(buf) free(buf);
00131   if(!proxy_file_tmp.empty()) remove(proxy_file_tmp.c_str());
00132   return res;
00133 }
00134