[Date Prev][Date Next] [Chronological] [Thread] [Top]

ldapsearch ASN.1 problem



Hi everyone,

I have two questions on the client of OpenLDAP.
I am interested in ASN.1. I think that OpenLDAP uses
it
because the RFC2251 (LDAPv3) is defined with this
syntax.

I have created a tiny 'server' which simply prints
informations obtained from a socket. Using ldapsearch
to
request this server, I must be able to analyse some
"Message" format. The program is at the end of this
message.

When I make:

  ldapsearch -h 192.168.0.2 -p 1234 -b ""
'(objectclass=*)'

I obtain:

  NB BYTES RCV: 39:

  30:00110000 25:00100101  2:00000010  1:00000001
   1:00000001 63:01100011 20:00100000  4:00000100
   0:00000000  a:00001010  1:00000001  2:00000010
   a:00001010  1:00000001  0:00000000  2:00000010
   1:00000001  0:00000000  2:00000010  1:00000001
   0:00000000  1:00000001  1:00000001  0:00000000
  87:10000111  b:00001011 6f:01101111 62:01100010
  6a:01101010 65:01100101 63:01100011 74:01110100
  63:01100011 6c:01101100 61:01100001 73:01110011
  73:01110011 30:00110000  0:00000000

In this packet, we can see:

  A sequence:
    30:00110000 25:00100101
  which encapsulates:

    An integer, MessageID, with the value 1:
      2:00000010  1:00000001  1:00000001

    A type with tag 63:
      63:01100011 20:00100000
    This tag represents an [APPLICATION 3]. In the
RFC,
    it corresponds to:
      SearchRequest ::= [APPLICATION 3] SEQUENCE {...
    which is an explicit re-tagged version of a
SEQUENCE.
    This type encapsulates:

      An empty LDAPDN:
        4:00000100  0:00000000

      ...

Now, I have two questions.
- Isn't it necessary to make a bind before sending a
request?
- Why the explicit tag 63 is not followed by the tag
30
corresponding to the encapsulated SEQUENCE?
For example, with a value 0x1234, of the type:

  TypeA ::= [APPLICATION 3] SEQUENCE {
    Value INTEGER  }

we obtain:

  [APP3] Len Val
  43     6
             SEQ Len Val
             30  4
                     INT Len Val
                     2   2   1234

Why don't we have, with a SearchRequest:

  [APP3] Len Val
   43    --
             SEQ Len Val
             30  --
                     LDAPDN Len Val
                     4      0
                     .... 

I sought a long time without finding any response.
If somebody can give me advice, I am very interested.
Thanks a lot.

Marc


Here, is the program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#define MYPORT 1234
#define BACKLOG 10


void Convert(int bin, char *str)
{
  unsigned int mask;

  mask = 0x80;
  while (mask)
  {
    if (bin & mask)
    {
      *str = '1';
    }
    else
    {
      *str = '0';
    }
    str++;
    mask >>= 1;
  }
  *str = 0;
}


int main(void)
{
  int sockfd, new_fd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int sin_size;
  int yes = 1;
  int i ;
  char string[33];
  int numbytes;
  unsigned char buf[100];
  unsigned char *buffer = buf;
  
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) ==
-1) 
  {
    perror("socket");
    exit(1);
  }
  
  if (setsockopt(sockfd,SOL_SOCKET, SO_REUSEADDR,
                 &yes, sizeof(int)) == -1)
  {
    perror("setsockopt");
    exit(1);
  }
  
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(MYPORT);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  memset(&(my_addr.sin_zero), '\0', 8);
  
  if (bind(sockfd, (struct sockaddr *)&my_addr,
           sizeof(struct sockaddr)) == -1)
  {
    perror("bind");
    exit(1);
  }

  if (listen(sockfd, BACKLOG) == -1)
  {
    perror("listen");
    exit(1);
  }

  sin_size = sizeof(struct sockaddr_in);
  if ((new_fd = accept(sockfd,
                       (struct sockaddr *)&their_addr,
                       (socklen_t *)&sin_size)) == -1)
  {
    perror("accept");
    exit(1);
  }
  close(sockfd);

  printf("server: got connection from %s\n",
          inet_ntoa(their_addr.sin_addr));

  if ((numbytes=recv(new_fd, buf, 99, 0)) == -1)
  {
    perror("recv");
    exit(1);
  }

  // Print bytes.
  printf("NB BYTES RCV: %d:\n", numbytes);
  for (i = 0; i < numbytes; i++)
  { 
    int n = buffer[i] & 0xFF;
    if (i % 4 == 0)
      printf("\n");               
    Convert(buffer[i], string);   
    printf("%2x:%s ", n, string);   
  }
  printf("\n");

  close(new_fd);
  exit(0);
} 



	

	
		
__________________________________________________________________
Découvrez le nouveau Yahoo! Mail : 250 Mo d'espace de stockage pour vos mails ! 
Créez votre Yahoo! Mail sur http://fr.mail.yahoo.com/