XrdSectestServer.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                   X r d S e c t e s t S e r v e r . 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: XrdSectestServer.cc 27487 2009-02-18 13:17:34Z ganis $
00012 
00013 const char *XrdSectestServerCVSID = "$Id: XrdSectestServer.cc 27487 2009-02-18 13:17:34Z ganis $";
00014 
00015 #include <unistd.h>
00016 #include <ctype.h>
00017 #include <errno.h>
00018 #include <stdlib.h>
00019 #include <strings.h>
00020 #include <stdio.h>
00021 #include <netdb.h>
00022 #include <arpa/inet.h>
00023 #include <sys/param.h>
00024 #include <sys/socket.h>
00025 
00026 #include "XrdOuc/XrdOucErrInfo.hh"
00027 #include "XrdSys/XrdSysHeaders.hh"
00028 #include "XrdSys/XrdSysLogger.hh"
00029 #include "XrdSec/XrdSecInterface.hh"
00030   
00031 /******************************************************************************/
00032 /*                    L O C A L   D E F I N I T I O N S                       */
00033 /******************************************************************************/
00034 
00035 #define H(x)         fprintf(stderr,x); fprintf(stderr, "\n");
00036 #define I(x)         fprintf(stderr, "\n"); H(x)
00037 #define insx(a,b)    sprintf(errbuff,a,b)
00038 #define insy(a,b,c)  sprintf(errbuff,a,b,c)
00039 
00040 typedef unsigned char uchar;
00041 
00042 /******************************************************************************/
00043 /*                      g l o b a l   v a r i a b l e s                       */
00044 /******************************************************************************/
00045 
00046 /* Define the execution control structure.
00047 */
00048 struct myOpts {
00049   int  debug;            /* 1 -> Enable debugging.                   */
00050   int  bin;              /* 1 -> Input cred in binary format.        */
00051   int  xtra;             /* 1 -> Perform null cred test              */
00052   int  online;           /* 1 -> Filename is actual hex cred.        */
00053   char *cfn;             /* -> config file                           */
00054   char *host;            /* -> hostname                              */
00055   char *inpt;            /* -> Input stream name.                    */
00056   FILE *infid;           /* -> Input stream (normally stdin).        */
00057   } opts;
00058 
00059 /* Define global variables.
00060 */
00061 char errbuff[256];
00062 #ifndef C_Block
00063 char hexbuff[256];
00064 #else
00065 char hexbuff[sizeof(C_Block)+8];
00066 #endif
00067 
00068 /******************************************************************************/
00069 /*                  f u n c t i o n   d e f i n i t i o n s                   */
00070 /******************************************************************************/
00071 
00072 int getbintix(uchar *buff, int blen);
00073 void getargs(int argc, char **argv);
00074 int  unhex(uchar *ibuff, uchar *obuff, int blen);
00075 int  cvtx(uchar idig, uchar *odig);
00076 void getline(uchar *buff, int blen);
00077 char *Ereason( );
00078 int emsg(int rc,char *msg);
00079 void help(int rc);
00080 void xerr(int x);
00081 
00082 /******************************************************************************/
00083 /*                          M A I N   P R O G R A M                           */
00084 /******************************************************************************/
00085 
00086 int main(int argc, char **argv)
00087 {
00088   int i, rc;
00089   struct sockaddr    caddr;
00090   struct sockaddr_in *netaddr = (struct sockaddr_in *)&caddr;
00091 
00092   XrdOucErrInfo einfo;
00093   XrdSysLogger Logger;
00094   XrdSecService *ServerSecurity;
00095   XrdSecParameters *parmp;
00096   XrdSecCredentials cred((char *)malloc(8192), 8192);
00097   XrdSecProtocol *pp;
00098   unsigned char bbuff[4096];
00099 
00100 // Parse the argument list.
00101 //
00102    getargs(argc, argv);
00103 
00104 // if hostname given, get the hostname address
00105 //
00106    if (opts.host)
00107       {struct hostent *hp;
00108        if (!(hp = gethostbyname(opts.host)))
00109           {cerr <<"testServer: host '" <<opts.host <<"' not found." <<endl;
00110            exit(1);
00111           }
00112        memcpy((void *)&netaddr->sin_addr.s_addr, hp->h_addr_list[0],
00113               sizeof(netaddr->sin_addr.s_addr));
00114       }
00115       else {netaddr->sin_family = AF_INET;
00116             netaddr->sin_port   = 0;
00117             netaddr->sin_addr.s_addr = 0x80000001;
00118             opts.host = (char *)"localhost";
00119            }
00120 
00121 // Create a new security server
00122 //
00123    ServerSecurity = XrdSecgetService(&Logger, opts.cfn);
00124    if (!ServerSecurity) 
00125       {cerr <<"testServer: Unable to create server." <<endl; exit(1);}
00126 
00127 // Get the security token and display it
00128 //
00129    const char *sect = ServerSecurity->getParms(i, opts.host);
00130    if (!sect) cerr <<"testServer: No security token for " <<opts.host <<endl;
00131       else cerr <<"testServer: " <<i <<" bytes of SecToken='" <<sect <<"'" <<endl;
00132 
00133 //Get the credentials from whatever source was specified
00134 //
00135   if (opts.bin) cred.size = getbintix((uchar *)cred.buffer, cred.size);
00136      else {if (opts.online) strcpy((char *)bbuff, opts.inpt);
00137                else getline(bbuff, sizeof(bbuff));
00138            cred.size = unhex(bbuff, (uchar *)cred.buffer, cred.size);
00139           }
00140 
00141 // Verify the length
00142 //
00143    if (cred.size < 0) emsg(100,(char *)"Invalid credentials format.");
00144 
00145 // Get the protocol
00146 //
00147    if (!(pp = ServerSecurity->getProtocol(opts.host,
00148                                           (const sockaddr &)caddr,
00149                                           (const XrdSecCredentials *)&cred,
00150                                           &einfo)))
00151       {rc = einfo.getErrInfo();
00152        cerr << "testServer: getProtocol error " <<rc <<"; ";
00153        cerr  <<einfo.getErrText() <<endl;
00154        exit(1);
00155       }
00156 
00157 // Now convert the credentials
00158 //
00159    if (pp->Authenticate(&cred, &parmp, &einfo) < 0)
00160       {rc = einfo.getErrInfo();
00161        cerr << "testServer: Authenticate error " <<rc <<"; ";
00162        cerr  <<einfo.getErrText() <<endl;
00163        exit(1);
00164       }
00165 
00166 // Tell everyone what the client identity is.
00167 //
00168       cout <<(pp->Entity.name ? pp->Entity.name : "?")
00169            <<"@" <<(pp->Entity.host ? pp->Entity.host : "?")
00170            <<" prot=" <<pp->Entity.prot <<endl;
00171 
00172 // All done
00173 //
00174    exit(0);
00175 }
00176 
00177 /*getbintix: get binary credentials into an array.
00178 */
00179 int getbintix(uchar *buff, int blen) {
00180 int i, j;
00181     for (i = 0; i < blen; i++)
00182         if ((j = getc(opts.infid)) >= 0) buff[i] = (uchar)j;
00183            else if (j == EOF) return i;
00184                 else xerr(insx("Error reading cred; %s.", Ereason()));
00185     xerr(insx("Cred longer than %d bytes.", blen));
00186     return -1;
00187 }
00188 
00189 /******************************************************************************/
00190 /*                        Command Line Processing                             */
00191 /******************************************************************************/
00192 
00193 /* getargs: parse through argv obtaining options and parameters.
00194 */
00195 void getargs(int argc, char **argv)
00196   {
00197   extern int optind; extern char *optarg; char c;
00198 
00199 /* Establish defaults here.
00200 */
00201   opts.debug    = 0;
00202   opts.bin      = 0;
00203   opts.online   = 0;
00204   opts.cfn      = 0;
00205   opts.host     = 0;
00206   opts.xtra     = 0;
00207   opts.inpt     = (char *)"";
00208   opts.infid    = stdin;
00209   opts.cfn      = 0;
00210 
00211 /* Process the options
00212 */
00213 while ((c=getopt(argc,argv,"c:h:i:k:p:bdx")) != (char)EOF)
00214   { switch(c)
00215     {
00216     case 'b': opts.bin = 1;                            break;
00217     case 'c': opts.cfn  = optarg;                      break;
00218     case 'd': opts.debug = 1;                          break;
00219     case 'h': opts.host = optarg;                      break;
00220     case 'i': opts.inpt = optarg;                      break;
00221     case 'x': opts.xtra = 1;                           break;
00222     case '?': help(1);
00223     }
00224   }
00225 
00226 /*Get the credentials, if specified on the command line.
00227 */
00228 if (optind < argc) {opts.inpt = argv[optind++]; opts.online = 1;}
00229 
00230 /*Make sure no more parameters exist.
00231 */
00232 if (optind < argc) xerr(insx("Extraneous parameter, '%s'.", argv[optind]));
00233 
00234 /*If the input stream is other than stdin, verify that it exists.
00235 */
00236 if (opts.inpt[0] != '\000' && !opts.online
00237    && (!(opts.infid = fopen(opts.inpt, "r"))) )
00238    xerr(insy("Cannot open '%s'; %s.", opts.inpt, Ereason() ));
00239 
00240 /* Make sure that -i * and -b are not specified together.
00241 */
00242 if (opts.online && opts.bin) 
00243     emsg(8, (char *)"-b is incompatible with inline creds.");
00244 
00245 /*All done
00246 */
00247   return;
00248     }
00249 
00250 /******************************************************************************/
00251 /*                          Utility  Function                                 */
00252 /******************************************************************************/
00253   
00254 /* unhex() converts a hex character string to its binary equivalent. The result
00255    is placed in the passed buffer. It returns the number of bytes extracted.
00256    An error results in a -1 response (including uneven hex digits). The
00257    input buffer must be terminated with a null.
00258 */
00259 int  unhex(uchar *ibuff, uchar *obuff, int blen) {
00260 int  i=0, j;
00261 uchar dig1, dig2;
00262 
00263 for (j = 0; j < blen; j++) {
00264   if (!ibuff[i]) return j;
00265   if (!cvtx(ibuff[i++], &dig1) || !cvtx(ibuff[i++], &dig2)) return -1;
00266   obuff[j] = (dig1 << 4) | dig2;
00267   }
00268 return -1; /* Buffer overflow */
00269  }
00270 
00271 int cvtx(uchar idig, uchar *odig) {
00272 if (idig >= '0' && idig <= '9') {*odig = idig & (uchar)0x0f; return 1;}
00273 idig = idig | (uchar)0x20; /* Change to lower case. */
00274 if (idig < 'a' || idig > 'f') return 0;
00275 *odig = (idig & (uchar)0x0f) + (uchar)0x09;
00276 return 1;
00277 }
00278 
00279 /*getline() gets a newline terminated string from the expected input source.
00280 */
00281 void getline(uchar *buff, int blen) {
00282   int i;
00283   if (!fgets((char *)buff, blen, opts.infid)) return;
00284   for (i = 0; i < blen; i++)
00285       if (buff[i] == '\n') {buff[i] = '\000'; break;}
00286   return;
00287   }
00288 
00289 char *Ereason( ) {
00290   return strerror(errno);
00291   }
00292 
00293 /*xerr: print message on standard error using the errbuff as source of message.
00294 */
00295 void xerr(int x) { emsg(8, errbuff); }
00296 
00297 /*emsg: print message on standard error.
00298 */
00299 int emsg(int rc,char *msg) {
00300     cerr << "testServer: " <<msg <<endl;
00301     if (!rc) return 0;
00302     exit(rc);
00303     }
00304 
00305 /*help prints hout the obvious.
00306 */
00307 void help(int rc) {
00308 /* Use H macro to avoid Sun string catenation bug. */
00309 I("Syntax:   testServer [ options ] cred ]")
00310 I("Options:  -b -c config -d  -h -i input -t")
00311 H("          -p principal[.instance][@realm] -s sep")
00312 I("Function: Display the credentials contents.")
00313 
00314 if (rc > 1) exit(rc);
00315 I("options:  (defaults: -k /etc/srvtab\\n")
00316 I("-b        indicates the cred is in binary format (i.e., not hexchar).")
00317 I("-c cfn    the config file.")
00318 I("-d        turns on debugging.")
00319 I("-h host   the incomming hostname.")
00320 I("-i input  specifies the input stream (e.g., fname) if other than stdin.")
00321 H("          This -i is ignored if cred is specified on the command line.")
00322 exit(rc);
00323 }

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