xrootd
XrdSecInterface.hh
Go to the documentation of this file.
1 #ifndef __SEC_INTERFACE_H__
2 #define __SEC_INTERFACE_H__
3 /******************************************************************************/
4 /* */
5 /* X r d S e c I n t e r f a c e . h h */
6 /* */
7 /* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC03-76-SFO0515 with the Department of Energy */
11 /******************************************************************************/
12 
13 #include <errno.h>
14 #ifndef WIN32
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <sys/param.h>
18 #endif
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #if defined(__CYGWIN__) || defined(__FreeBSD__)
23 #include <sys/socket.h>
24 #endif
25 
26 #include "XrdSec/XrdSecEntity.hh"
27 
28 /******************************************************************************/
29 /* X r d S e c C r e d e n t i a l s & X r d S e c P a r a m e t e r s */
30 /******************************************************************************/
31 
32 // The following structure is used to pass security information back and forth
33 //
35 {
36  int size;
37  char *buffer;
38 
39  XrdSecBuffer(char *bp=0, int sz=0) : size(sz), buffer(bp), membuf(bp) {}
40  ~XrdSecBuffer() {if (membuf) free(membuf);}
41 
42 private:
43  char *membuf;
44 };
45 
46 // When the buffer is used for credentials, the start of the buffer always
47 // holds the credential protocol name (e.g., krb4) as a string. The client
48 // will get credentials and the size will be filled out so that the contents
49 // of buffer can be easily transmitted to the server.
50 //
52 
53 // When the buffer is used for parameters, the contents must be interpreted
54 // in the context that it is used. For instance, the server will send the
55 // security configuration parameters on the initial login. The format differs
56 // from. say, the x.500 continuation paremeters that would be sent during
57 // PKI authentication via an "authmore" return status.
58 //
60 
61 /******************************************************************************/
62 /* X r d S e c P r o t o c o l */
63 /******************************************************************************/
64 
65 // The XrdSecProtocol is used to generate authentication credentials and to
66 // authenticate those credentials. For example, When a server indicates
67 // that authentication is needed (i.e., it returns security parameters), the
68 // client must call XrdSecgetProtocol() to get an appropriate XrdSecProtocol
69 // (i.e., one specific to the authentication protocol that needs to be used).
70 // Then the client can use the first form getCredentials() to generate the
71 // appropriate identification information. On subsequent calls in response to
72 // "authmore" the client must use the second form, providing the additional
73 // parameters the the server sends. The server uses Authenticate() to verify
74 // the credentials. When XrdOucErrInfo is null (as it will usually be), error
75 // messages are routed to standard error. So, for example, a client would
76 
77 // 1) Call XrdSecGetProtocol() to get an appropriate XrdSecProtocol
78 // (i.e., one specific to the authentication protocol that needs to be used).
79 // Note that successive calls to XrdSecGetProtocol() using the same
80 // XrdSecParameters will use the subsequent protocol named in the list of
81 // protocols that the server returned. Failure is indicated when the list
82 // is exhausted or none of the protocols apply (which exhausts the list).
83 
84 
85 // 2) Call getCredentials() without supplying any parameters so as to
86 // generate identification information and send them to the server.
87 
88 // 3) If the server indicates "authmore", call getCredentials() supplying
89 // the additional parameters sent by the server. The returned credentials
90 // are then sent to the server using the "authneticate" request code.
91 
92 // 4) Repeat step (3) as often as "authmore" is requested by the server.
93 
94 // The server uses Authenticate() to verify the credentials and getParms()
95 // to generate initial parameters to start the authentication process.
96 
97 // When XrdOucErrInfo is null (as it will usually be), error messages are
98 // are routed to standard error.
99 
100 // Server-side security is handled by the XrdSecServer object and, while
101 // it uses XrdSecProtocol objects to perform authentication, the XrdSecServer
102 // object is used to initialize the security environment and to generate
103 // the appropriate protocol objects at run-time. See XrdSecServer.hh.
104 
105 // MT Requirements: Must be MT_Safe.
106 
107 class XrdOucErrInfo;
108 
110 {
111 public:
112 
113 // The following structure holds the entity's identification. It is filled
114 // in by a successful call to Authenticate().
115 //
117 
118 // Authenticate credentials supplied by the client or server. Upon success,
119 // the XrdSecIdentity structure is completed. The method returns:
120 //
121 // > 0 -> parms present (more authentication needed)
122 // = 0 -> client present (authentication suceeded)
123 // < 0 -> einfo present (error has occured)
124 //
125 virtual int Authenticate (XrdSecCredentials *cred,
126  XrdSecParameters **parms,
127  XrdOucErrInfo *einfo=0)=0;
128 
129 // Generate credentials to be used in the authentication process. Upon
130 // success, return a credentials object. Upon failure, returns null and
131 // einfo, if present, has the reason for the failure.
132 //
134  XrdOucErrInfo *einfo=0)=0;
135 
136 // Encrypt data in inbuff and place it in outbuff.
137 //
138 // Returns: < 0 Failed, the return value is -errno of the reason. Typically,
139 // -EINVAL - one or more arguments are invalid.
140 // -NOTSUP - encryption not supported by the protocol
141 // -EOVERFLOW - outbuff is too small to hold result
142 // -ENOENT - Context not innitialized
143 // = 0 Success, outbuff contains a pointer to the encrypted data.
144 //
145 virtual int Encrypt(const char * /*inbuff*/, // Data to be encrypted
146  int /*inlen*/, // Length of data in inbuff
147  XrdSecBuffer ** /*outbuff*/ // Returns encrypted data
148  ) {return -ENOTSUP;}
149 
150 // Decrypt data in inbuff and place it in outbuff.
151 //
152 // Returns: < 0 Failed,the return value is -errno (see Encrypt).
153 // = 0 Success, outbuff contains a pointer to the encrypted data.
154 //
155 virtual int Decrypt(const char * /*inbuff*/, // Data to be decrypted
156  int /*inlen*/, // Length of data in inbuff
157  XrdSecBuffer ** /*outbuff*/ // Buffer for decrypted data
158  ) {return -ENOTSUP;}
159 
160 // Sign data in inbuff and place the signiture in outbuff.
161 //
162 // Returns: < 0 Failed, returned value is -errno (see Encrypt).
163 // = 0 Success, the return value is the length of the signature
164 // placed in outbuff.
165 //
166 virtual int Sign(const char * /*inbuff*/, // Data to be signed
167  int /*inlen*/, // Length of data in inbuff
168  XrdSecBuffer ** /*outbuff*/ // Buffer for the signature
169  ) {return -ENOTSUP;}
170 
171 // Verify a signature
172 //
173 // Returns: < 0 Failed, returned value is -errno (see Encrypt).
174 // = 0 Signature matches the value in inbuff.
175 // > 0 Failed to verify, signature does not match inbuff data.
176 //
177 virtual int Verify(const char * /*inbuff*/, // Data to be decrypted
178  int /*inlen*/, // Length of data in inbuff
179  const char * /*sigbuff*/, // Buffer for signature
180  int /*siglen*/) // Length if signature
181  {return -ENOTSUP;}
182 
183 // Get the current encryption key
184 //
185 // Returns: < 0 Failed, returned value if -errno (see Encrypt)
186 // >= 0 The size of the encyption key. The supplied buffer of length
187 // size hold the key. If the buffer address is 0, only the
188 // size of the key is returned.
189 //
190 virtual int getKey(char * /*buff*/=0, int /*size*/=0) {return -ENOTSUP;}
191 
192 // Set the current encryption key
193 //
194 // Returns: < 0 Failed, returned value if -errno (see Encrypt)
195 // 0 The new key has been set.
196 //
197 virtual int setKey(char * /*buff*/, int /*size*/) {return -ENOTSUP;}
198 
199 // DO NOT use C++ delete() on this object. Since the same compiler may not
200 // have been used in constructing all shared libraries, you must use the object
201 // specific Delete() method to insure that the object creator's delete is used.
202 //
203 virtual void Delete()=0; // Normally does "delete this"
204 
205  XrdSecProtocol(const char *pName) : Entity(pName) {}
206 protected:
207 
208 virtual ~XrdSecProtocol() {}
209 };
210 
211 /******************************************************************************/
212 /* P r o t o c o l N a m i n g C o n v e n t i o n s */
213 /******************************************************************************/
214 
215 // Each specific protocol resides in a shared library named "libXrdSec<p>.so"
216 // where <p> is the protocol identifier (e.g., krb5, gsi, etc). The library
217 // contains a class derived from the XrdSecProtocol object. The library must
218 // also contain a two extern "C" functions:
219 // 1) XrdSec<p>Init() - for one-time protocol ininitialization, and
220 // 2) XrdSec<p>Object() - for protocol object instantiation.
221 //
222 // extern "C" {char *XrdSecProtocol<p>Init (const char who,
223 // const char *parms,
224 // XrdOucErrInfo *einfo);
225 // }
226 // Is used by the dynamic protocol loader to initialize the protocol when the
227 // shared library is loaded. Parmater who contains 'c' when called on the
228 // client side and 's' when called on the server side. For client initialization,
229 // the parms is null. For server size initialization, parms contains the
230 // parameters specified in the configuration file. The protocol must return
231 // the parameters it needs to have sent to the client during the initial
232 // authentication handshake. If no parameters need to be sent, it must return
233 // the null string. If initialization fails, null must be returned and einfo
234 // must contain the reason for the failure. The storage occupied by the returned
235 // string is not freed by the dynamic loader; therefore, constant strings can
236 // be returned.
237 
238 // MT Requirements: None. Function called once in single-thread mode.
239 
240 // extern "C" {
241 // XrdSecProtocol *XrdSecProtocol<p>Object(const char who,
242 // const char *hostname,
243 // const struct sockaddr &netaddr,
244 // const char *parms,
245 // XrdOucErrInfo *einfo);
246 // }
247 // Is used by the dynamic protocol loader to obtain an instance of the
248 // XrdSecProtocol object. Argument who will contain 'c' for client-side calls
249 // and 's' for server-side calls. When who = 'c' then parms contains the parms
250 // supplied by the protocol at server-side initialization time (see the
251 // function Xrdsec<p>Init*(, explained above). When who = 's', parms is null.
252 
253 // Warning! The protocol *must* allow both 'c' and 's' calls to occur within
254 // the same execution context. This occurs when a server acts like a client.
255 
256 // The naming conventions were chosen to avoid platform dependent run-time
257 // loaders that resolve all addresses with the same name in all shared libraries
258 // to the first address with the same name encountered by the run-time loader.
259 
260 // MT Requirements: Must be MT_Safe.
261 
262 /******************************************************************************/
263 /* X r d S e c G e t P r o t o c o l */
264 /* */
265 /* C l i e n t S i d e U S e O n l y */
266 /******************************************************************************/
267 
268 /* The following external routine creates a security context and returns an
269  XrdSecProtocol object to be used for authentication purposes. The caller
270  provides the host name and IP address of the remote connection along with
271  any parameters provided by the server. A null return means an error occured.
272  Error messages are sent to standard error unless and XrdOucErrInfo class is
273  provided to capture the message. There should be one protocol object per
274  physical TCP/IP connection.
275 
276  When the connection is closed, the protocol's Delete() method should be
277  called to properly delete the object.
278 
279  The following extern "C" function must exist in your shared library.
280 
281 extern "C"
282 {
283  XrdSecProtocol *XrdSecGetProtocol(const char *hostname,
284  const struct sockaddr &netaddr,
285  XrdSecParameters &parms,
286  XrdOucErrInfo *einfo=0)
287  {....}
288 }
289 
290  MT Requirements: Must be MT_Safe.
291 */
292 
293 /******************************************************************************/
294 /* X r d S e c S e r v i c e */
295 /* */
296 /* S e r v e r S i d e U s e O n l y */
297 /******************************************************************************/
298 
299 // The XrdSecService object is the the object that the server uses to obtain
300 // parameters to be passed to the client on initial contact and to create the
301 // appropriate protocol on the initial receipt of the client's credentials.
302 // Server-side processing is a bit more complicated because the set of valid
303 // protocols needs to be configured and that configuration needs to be supplied
304 // to the client so that both can agree on a compatible protocol. This object
305 // is created via a call to XrdSecgetService, defined later on.
306 
308 {
309 public:
310 
311 // = 0 -> No security parameters need to be supplied to the client.
312 // This implies that authentication need not occur.
313 // ! 0 -> Address of the parameter string (which may be host-specigfic if hname
314 // was supplied). Ths length of the string is returned in size.
315 //
316 virtual const char *getParms(int &size, const char *hname=0) = 0;
317 
318 // = 0 -> No protocol can be returned (einfo has the reason)
319 // ! 0 -> Address of protocol object is bing returned. If cred is null,
320 // a host protocol object is returned if so allowed.
321 //
322 virtual XrdSecProtocol *getProtocol(const char *host, // In
323  const struct sockaddr &hadr, // In
324  const XrdSecCredentials *cred, // In
325  XrdOucErrInfo *einfo)=0;// Out
326 
328 virtual ~XrdSecService() {}
329 };
330 
331 // MT Requirements: Must be MT_Safe.
332 
333 /******************************************************************************/
334 /* X r d g e t S e c S e r v i c e */
335 /******************************************************************************/
336 
337 /* The XrdSecSgetService function is called during server initialization to
338  obtain the XrdSecService object. This function must be defined in your
339  shared library and is used by the server to obtain an XrdSecService object
340  used by the server to control server-side authentication.
341 
342 extern "C"
343 {
344  XrdSecService *XrdSecgetService(XrdSysLogger *lp, const char *cfn) {....}
345 }
346 
347  MT Requirements: None. Function called once in single-thread mode.
348 */
349 #endif