XrdSectestClient.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                   X r d S e c t e s t C l i e n t . c c                    */
00004 /*                                                                            */
00005 /* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010 
00011 //       $Id: XrdSectestClient.cc 27487 2009-02-18 13:17:34Z ganis $
00012 
00013 const char *XrdSectestClientCVSID = "$Id: XrdSectestClient.cc 27487 2009-02-18 13:17:34Z ganis $";
00014 
00015 /* Syntax: testClient [-b] [-d] [-h host] [-l] [sectoken]
00016 
00017    See the help() function for an explanation of the above.
00018 */
00019   
00020 #include <unistd.h>
00021 #include <ctype.h>
00022 #include <errno.h>
00023 #include <stdlib.h>
00024 #include <strings.h>
00025 #include <stdio.h>
00026 #include <netdb.h>
00027 #include <arpa/inet.h>
00028 #include <sys/param.h>
00029 #include <sys/socket.h>
00030 
00031 #include "XrdSys/XrdSysHeaders.hh"
00032 #include "XrdSec/XrdSecInterface.hh"
00033   
00034 /******************************************************************************/
00035 /*                    L O C A L   D E F I N I T I O N S                       */
00036 /******************************************************************************/
00037 
00038 #define H(x)         fprintf(stderr,x); fprintf(stderr, "\n");
00039 #define I(x)         fprintf(stderr, "\n"); H(x)
00040 
00041 /******************************************************************************/
00042 /*                                  m a i n                                   */
00043 /******************************************************************************/
00044   
00045 int main(int argc, char **argv)
00046 {
00047 char *tohex(char *inbuff, int inlen, char *outbuff);
00048 
00049 char *protocols=0, *hostspec=0;
00050 
00051 struct sockaddr_in netaddr;
00052 netaddr.sin_family = AF_INET;
00053 netaddr.sin_port   = 0;
00054 netaddr.sin_addr.s_addr = 0x80000001;
00055 
00056 int putbin = 0, putlen = 0;
00057 char kbuff[8192];
00058 char c;
00059 
00060 XrdSecCredentials *cred;
00061 XrdSecParameters   SecToken;
00062 XrdSecProtocol    *pp;
00063 int DebugON = 0;
00064 void help(int);
00065 
00066 
00067    /*Get all of the options.
00068     */
00069     while ((c=getopt(argc,argv,"bdlh:")) != (char)EOF)
00070       { switch(c)
00071         {
00072         case 'b': putbin = 1;                         break;
00073         case 'd': DebugON = 1;                        break;
00074         case 'h': hostspec = optarg;                  break;
00075         case 'l': putlen = 1;                         break;
00076         default:  help(1);
00077         }
00078       }
00079 
00080 // Check if the security token is the last argument
00081 //
00082    if (optind < argc) protocols = argv[optind++];
00083 
00084 /*Make sure no more parameters exist.
00085 */
00086    if (optind < argc) 
00087       {cerr <<"testClient: Extraneous parameter, '" <<argv[optind] <<"'." <<endl;
00088        help(2);
00089       }
00090 
00091 // Determine protocol string
00092 //
00093    if (!protocols && !(protocols = getenv("XrdSecSECTOKEN")))
00094       {cerr <<"testClient: Security protocol string not specified." <<endl;
00095        help(2);
00096       }
00097    SecToken.size = strlen(protocols);
00098    SecToken.buffer = protocols;
00099 
00100 // if hostname given, get the hostname address
00101 //
00102    if (hostspec)
00103       {struct hostent *hp;
00104        if (!(hp = gethostbyname(hostspec)))
00105           {cerr <<"testServer: host '" <<hostspec <<"' not found." <<endl;
00106            exit(1);
00107           }
00108        memcpy((void *)&netaddr.sin_addr.s_addr, hp->h_addr_list[0],
00109               sizeof(netaddr.sin_addr.s_addr));
00110       } else hostspec = (char *)"localhost";
00111 
00112 // Do debug processing
00113 //
00114    if (DebugON)
00115       {putenv((char *)"XrdSecDEBUG=1");
00116        cerr <<"testClient: security token='" <<protocols <<"'" <<endl;
00117       }
00118 
00119 // Get the protocol
00120 //
00121    pp = XrdSecGetProtocol(hostspec, (const struct sockaddr &)netaddr,SecToken,0);
00122    if (!pp) {cerr << "Unable to get protocol." <<endl; exit(1);}
00123 
00124 // Get credentials using this context
00125 //
00126    cred = pp->getCredentials();
00127    if (!cred)
00128       {cerr << "Unable to get credentials," <<endl;
00129        exit(1);
00130       }
00131    if (DebugON)
00132       cerr << "testClient: credentials size=" <<cred->size <<endl;
00133 
00134 // Write out the credentials
00135 //
00136    if (putbin)
00137       {if (putlen)
00138           {if (fwrite(&cred->size, sizeof(cred->size), 1, stdout) != sizeof(cred->size))
00139               {cerr << "Unable to write credentials length" <<endl; 
00140                exit(1);}}
00141        if (fwrite((char *) cred->buffer, cred->size, 1, stdout) != (size_t) cred->size)
00142           {cerr << "Unable to write credentials" <<endl; 
00143            exit(1);}
00144       } else {
00145        if (putlen) printf("%s",
00146                 tohex((char *)&cred->size, sizeof(cred->size), kbuff));
00147        printf("%s\n", tohex((char *) cred->buffer, cred->size, kbuff));
00148       }
00149 
00150 // All done.
00151 //
00152    pp->Delete();
00153 }
00154 
00155 char *tohex(char *inbuff, int inlen, char *outbuff) {
00156      static char hv[] = "0123456789abcdef";
00157      int i, j = 0;
00158      for (i = 0; i < inlen; i++) {
00159          outbuff[j++] = hv[(inbuff[i] >> 4) & 0x0f];
00160          outbuff[j++] = hv[ inbuff[i]       & 0x0f];
00161          }
00162      outbuff[j] = '\0';
00163      return outbuff;
00164      }
00165 
00166 /*help prints hout the obvious.
00167 */
00168 void help(int rc) {
00169 /* Use H macro to avoid Sun string catenation bug. */
00170 I("Syntax:   testClient [ options ] [sectoken]")
00171 I("Options:  -b -d -l -h host")
00172 I("Function: Request for credentials relative to an operation.")
00173 
00174 if (rc > 1) exit(rc);
00175 I("options:  (defaults: -o 01")
00176 I("-b        output the ticket in binary format (i.e., not hexchar).")
00177 I("-d        turns on debugging.")
00178 I("-l        prefixes the ticket with its 4-byte length.")
00179 I("-h host   the requesting hostname (default is localhost).")
00180 I("Notes:    1. Variable XrdSecSECTOKEN must contain the security token,")
00181 H("             sectoken, if it is not specified on the command line.")
00182 exit(rc);
00183 }

Generated on Tue Jul 5 14:46:57 2011 for ROOT_528-00b_version by  doxygen 1.5.1