XRootD
XrdSectestClient.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S e c t e s t C l i e n t . c c */
4 /* */
5 /* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 /* Syntax: testClient [-b] [-d] [-h host] [-l] [sectoken]
32 
33  See the help() function for an explanation of the above.
34 */
35 
36 #include <unistd.h>
37 #include <cctype>
38 #include <cerrno>
39 #include <cstdlib>
40 #include <cstring>
41 #include <cstdio>
42 #include <sys/param.h>
43 
44 #include "XrdNet/XrdNetAddr.hh"
45 #include "XrdSys/XrdSysHeaders.hh"
47 
48 /******************************************************************************/
49 /* G l o b a l D e f i n i t i o n s */
50 /******************************************************************************/
51 
52 extern "C"
53 {
54 extern XrdSecProtocol *XrdSecGetProtocol(const char *hostname,
55  XrdNetAddrInfo &endPoint,
56  XrdSecParameters &parms,
57  XrdOucErrInfo *einfo=0);
58 }
59 
60 /******************************************************************************/
61 /* L O C A L D E F I N I T I O N S */
62 /******************************************************************************/
63 
64 #define H(x) fprintf(stderr,x); fprintf(stderr, "\n");
65 #define I(x) fprintf(stderr, "\n"); H(x)
66 
67 /******************************************************************************/
68 /* m a i n */
69 /******************************************************************************/
70 
71 int main(int argc, char **argv)
72 {
73 char *tohex(char *inbuff, int inlen, char *outbuff);
74 
75 char *protocols=0, *hostspec=0;
76 
77 XrdNetAddr theAddr;
78 
79 int putbin = 0, putlen = 0;
80 char kbuff[8192];
81 char c;
82 
83 XrdSecCredentials *cred;
84 XrdSecParameters SecToken;
85 XrdSecProtocol *pp;
86 int DebugON = 0;
87 void help(int);
88 
89 
90  /*Get all of the options.
91  */
92  while ((c=getopt(argc,argv,"bdlh:")) != (char)EOF)
93  { switch(c)
94  {
95  case 'b': putbin = 1; break;
96  case 'd': DebugON = 1; break;
97  case 'h': hostspec = optarg; break;
98  case 'l': putlen = 1; break;
99  default: help(1);
100  }
101  }
102 
103 // Check if the security token is the last argument
104 //
105  if (optind < argc) protocols = argv[optind++];
106 
107 /*Make sure no more parameters exist.
108 */
109  if (optind < argc)
110  {std::cerr <<"testClient: Extraneous parameter, '" <<argv[optind] <<"'." <<std::endl;
111  help(2);
112  }
113 
114 // Determine protocol string
115 //
116  if (!protocols && !(protocols = getenv("XrdSecSECTOKEN")))
117  {std::cerr <<"testClient: Security protocol string not specified." <<std::endl;
118  help(2);
119  }
120  SecToken.size = strlen(protocols);
121  SecToken.buffer = protocols;
122 
123 // if hostname given, get the hostname address
124 //
125  if (hostspec && (eText = theAddr(hostspec,0)))
126  {std::cerr <<"testServer: Unable to resolve '" <<hostspec <<"'; " <<eText <<std::endl;
127  exit(1);
128  } else theAddr.Set("localhost",0);
129 
130 // Do debug processing
131 //
132  if (DebugON)
133  {putenv((char *)"XrdSecDEBUG=1");
134  std::cerr <<"testClient: security token='" <<protocols <<"'" <<std::endl;
135  }
136 
137 // Get the protocol
138 //
139  pp = XrdSecGetProtocol(hostspec, theAddr, SecToken, 0);
140  if (!pp) {std::cerr << "Unable to get protocol." <<std::endl; exit(1);}
141 
142 // Get credentials using this context
143 //
144  pp->addrInfo = &theAddr;
145  cred = pp->getCredentials();
146  if (!cred)
147  {std::cerr << "Unable to get credentials," <<std::endl;
148  exit(1);
149  }
150  if (DebugON)
151  std::cerr << "testClient: credentials size=" <<cred->size <<std::endl;
152 
153 // Write out the credentials
154 //
155  if (putbin)
156  {if (putlen)
157  {if (fwrite(&cred->size, sizeof(cred->size), 1, stdout) != sizeof(cred->size))
158  {std::cerr << "Unable to write credentials length" <<std::endl;
159  exit(1);}}
160  if (fwrite((char *) cred->buffer, cred->size, 1, stdout) != (size_t) cred->size)
161  {std::cerr << "Unable to write credentials" <<std::endl;
162  exit(1);}
163  } else {
164  if (putlen) printf("%s",
165  tohex((char *)&cred->size, sizeof(cred->size), kbuff));
166  printf("%s\n", tohex((char *) cred->buffer, cred->size, kbuff));
167  }
168 
169 // All done.
170 //
171  pp->Delete();
172 }
173 
174 char *tohex(char *inbuff, int inlen, char *outbuff) {
175  static char hv[] = "0123456789abcdef";
176  int i, j = 0;
177  for (i = 0; i < inlen; i++) {
178  outbuff[j++] = hv[(inbuff[i] >> 4) & 0x0f];
179  outbuff[j++] = hv[ inbuff[i] & 0x0f];
180  }
181  outbuff[j] = '\0';
182  return outbuff;
183  }
184 
185 /*help prints hout the obvious.
186 */
187 void help(int rc) {
188 /* Use H macro to avoid Sun string catenation bug. */
189 I("Syntax: testClient [ options ] [sectoken]")
190 I("Options: -b -d -l -h host")
191 I("Function: Request for credentials relative to an operation.")
192 
193 if (rc > 1) exit(rc);
194 I("options: (defaults: -o 01")
195 I("-b output the ticket in binary format (i.e., not hexchar).")
196 I("-d turns on debugging.")
197 I("-l prefixes the ticket with its 4-byte length.")
198 I("-h host the requesting hostname (default is localhost).")
199 I("Notes: 1. Variable XrdSecSECTOKEN must contain the security token,")
200 H(" sectoken, if it is not specified on the command line.")
201 exit(rc);
202 }
int DebugON
int optind
size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
int main(int argc, char **argv)
void help(int rc)
#define I(x)
#define H(x)
char * tohex(char *inbuff, int inlen, char *outbuff)
XrdSecProtocol * XrdSecGetProtocol(const char *hostname, XrdNetAddrInfo &endPoint, XrdSecParameters &parms, XrdOucErrInfo *einfo=0)
Definition: XrdSecClient.cc:86
const char * Set(const char *hSpec, int pNum=PortInSpec)
Definition: XrdNetAddr.cc:216
virtual XrdSecCredentials * getCredentials(XrdSecParameters *parm=0, XrdOucErrInfo *einfo=0)=0
virtual void Delete()=0
Delete the protocol object. DO NOT use C++ delete() on this object.
Generic structure to pass security information back and forth.
char * buffer
Pointer to the buffer.
int size
Size of the buffer or length of data in the buffer.