go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/login/login.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package login contains the structures describing request and response for login request.
     6  // Login request should be issued prior to most of the other
     7  // requests (see http://xrootd.org/doc/dev45/XRdv310.pdf, p.10).
     8  // As part of the response, SecurityInformation may be provided,
     9  // indicating that an auth request is required. SecurityInformation
    10  // defines the available authentication protocols together with some additional parameters.
    11  // See XRootD protocol specification, page 127 for further information
    12  // about the format of the SecurityInformation.
    13  package login // import "go-hep.org/x/hep/xrootd/xrdproto/login"
    14  
    15  import (
    16  	"os"
    17  
    18  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    19  )
    20  
    21  // RequestID is the id of the request, it is sent as part of message.
    22  // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format.
    23  const RequestID uint16 = 3007
    24  
    25  // ResponseLength is the length of the Response assuming that SecurityInformation is empty.
    26  const ResponseLength = 16
    27  
    28  // Response is a response for the login request, which contains the session id and the security information.
    29  type Response struct {
    30  	SessionID           [16]byte
    31  	SecurityInformation []byte
    32  }
    33  
    34  // RespID implements xrdproto.Response.RespID.
    35  func (resp *Response) RespID() uint16 { return RequestID }
    36  
    37  // MarshalXrd implements xrdproto.Marshaler.
    38  func (o Response) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    39  	wBuffer.WriteBytes(o.SessionID[:])
    40  	wBuffer.WriteBytes(o.SecurityInformation)
    41  	return nil
    42  }
    43  
    44  // UnmarshalXrd implements xrdproto.Unmarshaler.
    45  func (o *Response) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    46  	rBuffer.ReadBytes(o.SessionID[:])
    47  	o.SecurityInformation = append(o.SecurityInformation, rBuffer.Bytes()...)
    48  	return nil
    49  }
    50  
    51  // Request holds the login request parameters.
    52  type Request struct {
    53  	Pid          int32   // Pid is the process number associated with this connection.
    54  	Username     [8]byte // Username is the unauthenticated name of the user to be associated with the connection.
    55  	_            byte    // Reserved for future use.
    56  	Ability      byte    // Ability are the client's extended capabilities. See xrootd protocol specification, p. 56.
    57  	Capabilities byte    // Capabilities are the Client capabilities. It is 4 for v3.1.0 client without async support.
    58  	Role         byte    // Role is the role being assumed for this login: administrator or regular user.
    59  	Token        []byte  // Token is the token supplied by the previous redirection response, plus optional elements.
    60  }
    61  
    62  // Capabilities for v3.1.0 client without async support.
    63  const clientCapabilities byte = 4
    64  
    65  // NewRequest forms a Request according to provided parameters.
    66  func NewRequest(username, token string) *Request {
    67  	var usernameBytes [8]byte
    68  	copy(usernameBytes[:], username)
    69  
    70  	return &Request{
    71  		Pid:          int32(os.Getpid()),
    72  		Username:     usernameBytes,
    73  		Capabilities: clientCapabilities,
    74  		Token:        []byte(token),
    75  	}
    76  }
    77  
    78  // ReqID implements xrdproto.Request.ReqID.
    79  func (req *Request) ReqID() uint16 { return RequestID }
    80  
    81  // ShouldSign implements xrdproto.Request.ShouldSign.
    82  func (req *Request) ShouldSign() bool { return false }
    83  
    84  // MarshalXrd implements xrdproto.Marshaler.
    85  func (o Request) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    86  	wBuffer.WriteI32(o.Pid)
    87  	wBuffer.WriteBytes(o.Username[:])
    88  	wBuffer.Next(1)
    89  	wBuffer.WriteU8(o.Ability)
    90  	wBuffer.WriteU8(o.Capabilities)
    91  	wBuffer.WriteU8(o.Role)
    92  	wBuffer.WriteLen(len(o.Token))
    93  	wBuffer.WriteBytes(o.Token)
    94  	return nil
    95  }
    96  
    97  // UnmarshalXrd implements xrdproto.Unmarshaler.
    98  func (o *Request) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    99  	o.Pid = rBuffer.ReadI32()
   100  	rBuffer.ReadBytes(o.Username[:])
   101  	rBuffer.Skip(1)
   102  	o.Ability = rBuffer.ReadU8()
   103  	o.Capabilities = rBuffer.ReadU8()
   104  	o.Role = rBuffer.ReadU8()
   105  	o.Token = make([]byte, rBuffer.ReadLen())
   106  	rBuffer.ReadBytes(o.Token)
   107  	return nil
   108  }