/ inc / ldap_comunication.h
ldap_comunication.h
  1  /**
  2   * @file ldap_comunication.h
  3   * @author Rene Ceska xceska06 (xceska06@stud.fit.vutbr.cz)
  4   * @brief Functions for communication with ldap client
  5   * @date 2023-11-19
  6   */
  7  #ifndef LDAP_COMUNICATION_H
  8  #define LDAP_COMUNICATION_H
  9  #include "inc/AndFilterObject.h"
 10  #include "inc/BerBoolObject.h"
 11  #include "inc/BerEnumObject.h"
 12  #include "inc/BerIntObject.h"
 13  #include "inc/BerObject.h"
 14  #include "inc/BerParser.h"
 15  #include "inc/BerSequenceObject.h"
 16  #include "inc/BerSetObject.h"
 17  #include "inc/BerStringObject.h"
 18  #include "inc/BerUndefinedObject.h"
 19  #include "inc/DatabaseController.h"
 20  #include "inc/DatabaseObject.h"
 21  #include "inc/EqualityMatchFilterObject.h"
 22  #include "inc/FilterObject.h"
 23  #include "inc/NotFilterObject.h"
 24  #include "inc/OrFilterObject.h"
 25  #include "inc/SubstringFilterObject.h"
 26  #include "inc/ber_constants.h"
 27  #include "inc/ber_helper_functions.h"
 28  #include "inc/database_helper_functions.h"
 29  #include "inc/filter_helper_functions.h"
 30  #include <algorithm>
 31  #include <arpa/inet.h>
 32  #include <netinet/in.h>
 33  #include <stdio.h>
 34  #include <stdlib.h>
 35  #include <string.h>
 36  #include <string>
 37  #include <sys/resource.h>
 38  #include <sys/socket.h>
 39  #include <sys/time.h>
 40  #include <sys/types.h>
 41  #include <sys/wait.h>
 42  #include <unistd.h>
 43  #include <vector>
 44  
 45  typedef struct searchedAttributes {
 46    bool cn;
 47    bool email;
 48    bool uid;
 49  } searchedAttributesType;
 50  
 51  // enum for attributes (cn, email, uid)
 52  typedef enum { cn, email, uid } atributeDescriptions;
 53  
 54  // sequence - envelope
 55  //     int   - message ID
 56  //     application 3 - search request
 57  //     octed string - base object
 58  //     enum - scope
 59  //     enum - derefAliases
 60  //     int - sizeLimit
 61  //     int - timeLimit
 62  //     bool - typesOnly
 63  //     sequence - FilterObject
 64  //     sequence - attributes
 65  
 66  typedef struct searchRequest {
 67    int messageIDLength;
 68    unsigned int sizeLimit;
 69    searchedAttributesType attributes;
 70  } searchRequestType;
 71  
 72  /**
 73   * @brief Initialize the search result entry envelope
 74   *
 75   * @param searchRequest search request envelope for which the search result entry
 76   * @param LDAPDN LDAPDN of the entry
 77   * @return BerObject*
 78   */
 79  BerObject *InitSearchResultEntry(BerObject *searchRequest,
 80                                   std::vector<unsigned char> LDAPDN);
 81  
 82  /**
 83   * @brief Adds an attribute to the search result entry envelope
 84   *
 85   * @param envelope search result entry envelope
 86   * @param attributeDescription
 87   * @param attributeValue
 88   * @return int
 89   */
 90  int AddToSearchResultEntry(BerObject *envelope,
 91                             std::vector<unsigned char> &attributeDescription,
 92                             std::vector<unsigned char> &attributeValue);
 93  /**
 94   * @brief checks if the search request is valid
 95   *
 96   * @param searchRequest
 97   * @return int 0 if valid, -1 if inavalid application sequence, -2 if invalid message id or whole envelope
 98   */
 99  int checkSearchRequest(BerObject *searchRequest);
100  
101  /**
102   * @brief sends notice of disconnection to the client
103   *
104   * @param comSocket socket to send the notice to
105   * @param errCode error code
106   * @return int
107   */
108  int sendNoticeOfDisconnection(int comSocket, char errCode);
109  
110  /**
111   * @brief sends search result entry to the client
112   *
113   * @param envelope search request envelope
114   * @param comSocket socket to send the envelope to
115   * @return int
116   */
117  int searchRequestHandler(BerObject *searchRequest, int comm_socket,
118                           const char *dbPath);
119  
120  /**
121   * @brief Create a Bind Response object
122   *
123   * @param bindRequest
124   * @param resultCode
125   * @return BerObject*
126   */
127  BerObject *CreateBindResponse(BerObject *bindRequest, int resultCode);
128  
129  /**
130   * @brief loads the envelope from the client, waits until all the date are received
131   *
132   * @param bindRequest returns the envelope as a vector of unsigned chars
133   * @param comm_socket socket to receive the envelope from
134   * @return int 0 if success, -1 if error ocured
135   */
136  int loadEnvelope(std::vector<unsigned char> &bindRequest, int comm_socket) ;
137  
138  /**
139   * @brief sends the search result done envelope to the client
140   *
141   * @param searchRequest  search request envelope for which the search result done is
142   * @param comm_socket  socket to send the envelope to
143   * @param result_code
144   * @return int
145   */
146  int sendSearchResultDone(BerSequenceObject *searchRequest, int comm_socket,
147                           unsigned int result_code);
148  #endif