Back to index

nordugrid-arc-nox  1.1.0~rc6
test2myproxyserver_put.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 delegate a certificate into that server
00063 
00064   // The message which will be sent to myproxy server
00065   //"PUT" command
00066   std::string send_msg("VERSION=MYPROXYv2\n COMMAND=1\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   Arc::MCCConfig cfg;
00071   cfg.AddProxy(out_file);
00072   cfg.AddCADir("$HOME/.globus/certificates/");
00073 
00074   //Arc::ClientTCP client(cfg, "127.0.0.1", 7512, Arc::GSISec, 60);
00075   Arc::ClientTCP client(cfg, "knowarc1.grid.niif.hu", 7512, Arc::GSISec, 60);
00076 
00077   Arc::PayloadRaw request;
00078   request.Insert(send_msg.c_str(), 0, send_msg.length());
00079   //Arc::PayloadRawInterface& buffer = dynamic_cast<Arc::PayloadRawInterface&>(request);
00080   //std::cout<<"Message in PayloadRaw:  "<<((Arc::PayloadRawInterface&)buffer).Content()<<std::endl;
00081 
00082   Arc::PayloadStreamInterface *response = NULL;
00083   Arc::MCC_Status status = client.process(&request, &response, true);
00084   if (!status) {
00085     logger.msg(Arc::ERROR, (std::string)status);
00086     if (response)
00087       delete response;
00088     return 1;
00089   }
00090   if (!response) {
00091     logger.msg(Arc::ERROR, "No stream response");
00092     return 1;
00093   }
00094 
00095   std::string ret_str;
00096   char ret_buf[1024];
00097   memset(ret_buf, 0, 1024);
00098   int len;
00099   do {
00100     len = 1024;
00101     response->Get(&ret_buf[0], len);
00102     ret_str.append(ret_buf, len);
00103     memset(ret_buf, 0, 1024);
00104   } while (len == 1024);
00105 
00106   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", ret_str.c_str(), ret_str.length());
00107 
00108   //Myproxy server will send back another message which includes
00109   //the certificate request in DER format
00110   std::string x509ret_str;
00111   memset(ret_buf, 0, 1024);
00112   do {
00113     len = 1024;
00114     response->Get(&ret_buf[0], len);
00115     x509ret_str.append(ret_buf, len);
00116     memset(ret_buf, 0, 1024);
00117   } while (len == 1024);
00118 
00119   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", x509ret_str.c_str(), x509ret_str.length());
00120 
00121   if (response) {
00122     delete response;
00123     response = NULL;
00124   }
00125 
00126   std::string tmp_req_file("myproxy_req.pem");
00127   std::ofstream tmp_req_out(tmp_req_file.c_str());
00128   tmp_req_out.write(x509ret_str.c_str(), x509ret_str.size());
00129   tmp_req_out.close();
00130 
00131 
00132   Arc::Credential signer1(out_file, "", cadir, "");
00133 
00134   Arc::Credential proxy1;
00135   std::string signedcert1, signing_cert1, signing_cert_chain1;
00136   proxy1.InquireRequest(x509ret_str, false, true);
00137   if (!(signer1.SignRequest(&proxy1, signedcert1, true))) {
00138     logger.msg(Arc::ERROR, "Delegate proxy failed");
00139     return 1;
00140   }
00141 
00142   signer1.OutputCertificate(signing_cert1, true);
00143   signer1.OutputCertificateChain(signing_cert_chain1, true);
00144   signedcert1.append(signing_cert1).append(signing_cert_chain1);
00145   //std::cout<<"Signing cert: "<<signing_cert1<<std::endl;
00146   //std::cout<<"Chain: "<<signing_cert_chain1<<std::endl;
00147 
00148 
00149   //logger.msg(Arc::INFO, "Generated proxy certificate: %s", signedcert1.c_str());
00150 
00151   std::string tmp_cert_file("myproxy_cert.pem");
00152   std::ofstream tmp_cert_out(tmp_cert_file.c_str());
00153   tmp_cert_out.write(signedcert1.c_str(), signedcert1.size());
00154   tmp_cert_out.close();
00155 
00156 
00157   //Send back the proxy certificate to myproxy server
00158   Arc::PayloadRaw request1;
00159 
00160   //Caculate the numbers of certifictes as the beginning of the message
00161   unsigned char number_of_certs;
00162   number_of_certs = signer1.GetCertNumofChain() + 2;
00163   BIO *bio = BIO_new(BIO_s_mem());
00164   std::cout << BIO_write(bio, &number_of_certs, sizeof(number_of_certs)) << std::endl;
00165   std::string start;
00166   for (;;) {
00167     char s[256];
00168     int l = BIO_read(bio, s, sizeof(s));
00169     if (l <= 0)
00170       break;
00171     start.append(s, l);
00172   }
00173   BIO_free_all(bio);
00174   signedcert1.insert(0, start);
00175 
00176   request1.Insert(signedcert1.c_str(), 0, signedcert1.length());
00177   status = client.process(&request1, &response, true);
00178   if (!status) {
00179     logger.msg(Arc::ERROR, (std::string)status);
00180     if (response)
00181       delete response;
00182     return 1;
00183   }
00184   if (!response) {
00185     logger.msg(Arc::ERROR, "No stream response");
00186     return 1;
00187   }
00188 
00189   std::string ret_str1;
00190   memset(ret_buf, 0, 1024);
00191   do {
00192     len = 1024;
00193     response->Get(&ret_buf[0], len);
00194     ret_str1.append(ret_buf, len);
00195     memset(ret_buf, 0, 1024);
00196   } while (len == 1024);
00197   logger.msg(Arc::INFO, "Returned msg from myproxy server: %s   %d", ret_str1.c_str(), ret_str1.length());
00198 
00199 
00200   if (response) {
00201     delete response;
00202     response = NULL;
00203   }
00204 
00205   return 0;
00206 }