Back to index

nordugrid-arc-nox  1.1.0~rc6
test2myproxyserver_get.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <iostream>
00006 #include <signal.h>
00007 #include <fstream>
00008 
00009 #include <arc/ArcConfig.h>
00010 #include <arc/Logger.h>
00011 #include <arc/URL.h>
00012 #include <arc/message/SOAPEnvelope.h>
00013 #include <arc/message/PayloadSOAP.h>
00014 #include <arc/message/MCC.h>
00015 #include <arc/message/MCCLoader.h>
00016 #include <arc/client/ClientInterface.h>
00017 #include <arc/credential/Credential.h>
00018 #ifdef WIN32
00019 #include <arc/win32.h>
00020 #endif
00021 
00022 int main(void) {
00023   setlocale(LC_ALL, "");
00024 
00025   Arc::Logger logger(Arc::Logger::getRootLogger(), "Test2MyProxyServer");
00026   Arc::LogStream logcerr(std::cerr);
00027   logcerr.setFormat(Arc::ShortFormat);
00028   Arc::Logger::getRootLogger().addDestination(logcerr);
00029   Arc::Logger::getRootLogger().setThreshold(Arc::DEBUG);
00030 
00031   std::string cert("../../tests/echo/usercert.pem");
00032   std::string key("../../tests/echo/userkey.pem");
00033   std::string cadir("../../tests/echo/certificates/");
00034   Arc::Credential signer(cert, key, cadir, "");
00035 
00036   //Generate a temporary self-signed proxy certificate
00037   //to contact the myproxy server
00038   std::string private_key, signing_cert, signing_cert_chain;
00039   std::string out_file("./tmpproxy.pem");
00040 
00041   Arc::Time t;
00042   int keybits = 1024;
00043   std::string req_str;
00044   Arc::Credential cred_request(t, Arc::Period(12 * 3600), keybits, "rfc", "inheritAll", "", -1);
00045   cred_request.GenerateRequest(req_str);
00046 
00047   Arc::Credential proxy;
00048   proxy.InquireRequest(req_str);
00049 
00050   signer.SignRequest(&proxy, out_file.c_str());
00051 
00052   cred_request.OutputPrivatekey(private_key);
00053   signer.OutputCertificate(signing_cert);
00054   signer.OutputCertificateChain(signing_cert_chain);
00055   std::ofstream out_f(out_file.c_str(), std::ofstream::app);
00056   out_f.write(private_key.c_str(), private_key.size());
00057   out_f.write(signing_cert.c_str(), signing_cert.size());
00058   out_f.write(signing_cert_chain.c_str(), signing_cert_chain.size());
00059   out_f.close();
00060 
00061 
00062   //Contact the myproxy server to get a delegated certificate from that server
00063 
00064   // The message which will be sent to myproxy server
00065   //"GET" command
00066   std::string send_msg("VERSION=MYPROXYv2\n COMMAND=0\n USERNAME=mytest\n PASSPHRASE=123456\n LIFETIME=43200\n");
00067 
00068   std::cout << "Send message to peer end through GSS communication: " << send_msg << " Size: " << send_msg.length() << std::endl;
00069 
00070   //For "GET" command, client authentication is optional
00071   Arc::MCCConfig cfg;
00072   cfg.AddProxy(out_file);
00073   cfg.AddCADir("$HOME/.globus/certificates/");
00074 
00075   //Arc::ClientTCP client(cfg, "127.0.0.1", 7512, Arc::GSISec, 60);
00076   Arc::ClientTCP client(cfg, "knowarc1.grid.niif.hu", 7512, Arc::GSISec, 60);
00077 
00078   Arc::PayloadRaw request;
00079   request.Insert(send_msg.c_str(), 0, send_msg.length());
00080   //Arc::PayloadRawInterface& buffer = dynamic_cast<Arc::PayloadRawInterface&>(request);
00081   //std::cout<<"Message in PayloadRaw:  "<<((Arc::PayloadRawInterface&)buffer).Content()<<std::endl;
00082 
00083   Arc::PayloadStreamInterface *response = NULL;
00084   Arc::MCC_Status status = client.process(&request, &response, true);
00085   if (!status) {
00086     logger.msg(Arc::ERROR, (std::string)status);
00087     if (response)
00088       delete response;
00089     return 1;
00090   }
00091   if (!response) {
00092     logger.msg(Arc::ERROR, "No stream response");
00093     return 1;
00094   }
00095 
00096   std::string ret_str;
00097   char ret_buf[1024];
00098   memset(ret_buf, 0, 1024);
00099   int len;
00100   do {
00101     len = 1024;
00102     response->Get(&ret_buf[0], len);
00103     ret_str.append(ret_buf, len);
00104     memset(ret_buf, 0, 1024);
00105   } while (len == 1024);
00106 
00107   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", ret_str.c_str(), ret_str.length());
00108 
00109   if (response) {
00110     delete response;
00111     response = NULL;
00112   }
00113 
00114 
00115   //Generate a certificate request,
00116   //and send it to myproxy server
00117   std::string x509_req_str;
00118   Arc::Time start;
00119   Arc::Credential x509_request(start, Arc::Period(), 1024);
00120   x509_request.GenerateRequest(x509_req_str, true);
00121   std::string proxy_key_str;
00122   x509_request.OutputPrivatekey(proxy_key_str);
00123 
00124   Arc::PayloadRaw request1;
00125   request1.Insert(x509_req_str.c_str(), 0, x509_req_str.length());
00126   status = client.process(&request1, &response, true);
00127   if (!status) {
00128     logger.msg(Arc::ERROR, (std::string)status);
00129     if (response)
00130       delete response;
00131     return 1;
00132   }
00133   if (!response) {
00134     logger.msg(Arc::ERROR, "No stream response");
00135     return 1;
00136   }
00137 
00138   std::string ret_str1;
00139   memset(ret_buf, 0, 1024);
00140   do {
00141     len = 1024;
00142     response->Get(&ret_buf[0], len);
00143     ret_str1.append(ret_buf, len);
00144     memset(ret_buf, 0, 1024);
00145   } while (len == 1024);
00146   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", ret_str1.c_str(), ret_str1.length());
00147 
00148   BIO *bio = BIO_new(BIO_s_mem());
00149   BIO_write(bio, (unsigned char*)(ret_str1.c_str()), ret_str1.length());
00150   unsigned char number_of_certs;
00151   BIO_read(bio, &number_of_certs, sizeof(number_of_certs));
00152   logger.msg(Arc::INFO, "There are %d certificates in the returned msg", number_of_certs);
00153   std::string proxy_cert_str;
00154   for (;;) {
00155     char s[256];
00156     int l = BIO_read(bio, s, sizeof(s));
00157     if (l <= 0)
00158       break;
00159     proxy_cert_str.append(s, l);
00160   }
00161   BIO_free_all(bio);
00162 
00163   //Output the PEM formated proxy certificate
00164   std::string tmpcert_file("tmpcert.pem");
00165   std::ofstream tmpcert_f(tmpcert_file.c_str());
00166   std::string tmpkey_file("tmpkey.pem");
00167   std::ofstream tmpkey_f(tmpkey_file.c_str());
00168   tmpcert_f.write(proxy_cert_str.c_str(), proxy_cert_str.size());
00169   tmpkey_f.write(proxy_key_str.c_str(), proxy_key_str.size());
00170   tmpcert_f.close();
00171   tmpkey_f.close();
00172 
00173   Arc::Credential proxy_cred(tmpcert_file, tmpkey_file, cadir, "");
00174   std::string proxy_cred_str_pem;
00175   std::string proxy_cred_file("proxy_cred.pem");
00176   std::ofstream proxy_cred_f(proxy_cred_file.c_str());
00177   proxy_cred.OutputCertificate(proxy_cred_str_pem);
00178   proxy_cred.OutputPrivatekey(proxy_cred_str_pem);
00179   proxy_cred.OutputCertificateChain(proxy_cred_str_pem);
00180   proxy_cred_f.write(proxy_cred_str_pem.c_str(), proxy_cred_str_pem.size());
00181   proxy_cred_f.close();
00182 
00183 
00184   //Myproxy server will then return a standard response message
00185   std::string ret_str2;
00186   memset(ret_buf, 0, 1024);
00187   do {
00188     len = 1024;
00189     response->Get(&ret_buf[0], len);
00190     ret_str2.append(ret_buf, len);
00191     memset(ret_buf, 0, 1024);
00192   } while (len == 1024);
00193   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", ret_str2.c_str(), ret_str2.length());
00194 
00195   if (response) {
00196     delete response;
00197     response = NULL;
00198   }
00199 
00200   return 0;
00201 }