HP3000-L Archives

October 2001, Week 4

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Richard Sheehan <[log in to unmask]>
Reply To:
Richard Sheehan <[log in to unmask]>
Date:
Fri, 26 Oct 2001 18:16:19 -0600
Content-Type:
text/plain
Parts/Attachments:
text/plain (514 lines)
Dave,
Thanks for your response.
Gavin has provided some significant assistance so far.

Here is the ldapauth.c I have put together along with a simple
ldaptest.c
program.

The output is after the <--- End Code --->

<--- Begin Code --->
/*Program Name: ldaptest.c
  Date        : Oct 26, 2001
*/

#include <stdio.h>

extern void ldapauth(char*, char*, char*, char*, char*, int*);

int main(){
  char host[]="beau2.isu.edu";
  char uid[]="admtest";
  char ou[]="People";
  char o[]="isu.edu";
  char pw[]="12testadm";
  int result;

  ldapauth(host,uid,ou,o,pw,&result);
  printf("result = %d\n",result);
  }

/*Program Name: ldapauth.c
  Date        : Oct 25, 2001
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ldap.h"

/*
Supply the following values to authenticate to a desired LDAP server
t_host   = hostname
t_uid    = uid (uid = username)
t_ou     = ou  (ou  = Poeple)
t_o      = o   (o   = isu.edu)
t_pw     = pw  password
t_result = result an integer value to be evaluated by the calling
program

The following value will be returned
t_result = 0 - Success, !0 - Fail

*/

void ldapauth(char *t_host,
              char *t_uid,
              char *t_ou,
              char *t_o,
              char *t_pw,
              int  *t_result)
{
  LDAP *ld;
  int   rc;

  char *host, *uid, *ou, *o, *pw;

  char *ptr, *buffer;
  char *dn;

  int templen    = 0,
      len_t_host = 0,
      len_t_uid  = 0,
      len_t_ou   = 0,
      len_t_o    = 0,
      len_t_pw   = 0;

  int buffer_len = 0,
      num_tokens = 0;

  if (t_host   == NULL ||
      t_uid    == NULL ||
      t_ou     == NULL ||
      t_o      == NULL ||
      t_pw     == NULL ||
      t_result == NULL) /* Check to make sure all parms were passed in!
*/
    return;

/* The lengths of the strings passed to this function are
   determined in the by strlen.  This means that the passing
   program must terminate each string with the null character.
   This is accomplished in quick by letting the string = trun(
   string) + char(0)-null.
   See Call 3052843: Question about Do External Common Area

*/

  len_t_host = (int) strlen(t_host);
  len_t_uid  = (int) strlen(t_uid);
  len_t_ou   = (int) strlen(t_ou);
  len_t_o    = (int) strlen(t_o);
  len_t_pw   = (int) strlen(t_pw);

  printf("%s,%d\n",t_host,len_t_host);
  printf("%s,%d\n",t_uid, len_t_uid );
  printf("%s,%d\n",t_ou,  len_t_ou  );
  printf("%s,%d\n",t_o,   len_t_o   );
  printf("%s,%d\n",t_pw,  len_t_pw  );
  printf("%d\n",  *t_result);

/* strtok is use to clean up the externally allocated strings.
   The length of each parameter is declared in the Power House
   source and are padded on on either side, based on the
   justification of the particular value being passed, provided
   any room is left, with spaces (ascii(32)).
*/

/* Build host string from passed in string t_host.
   Note:  if a port other than default is needed,
   specify host in the host:port format.
   default port is defined in ldap.h as LDAP_PORT */

  printf("Starting host malloc\n");

  host = (char*) malloc(len_t_host + 1);
  if (host == NULL){
    *t_result = 1100; /* could not allocate memory for host string */
    return;
    }

  buffer = (char*) malloc(len_t_host + 1);
  if (buffer == NULL){
    free(host);
    *t_result = 1200; /* could not allocate memory for host buffer */
    return;
    }

  strncat(buffer,t_host,len_t_host);

  ptr = strtok(buffer,", ");

  strcpy(host,ptr);
  while(ptr!=NULL){
    ptr = strtok(NULL,", ");
    if (ptr!=NULL){
      strcat(host," ");
      strcat(host,ptr);
      }
    }
  free(buffer);

/* Build uid string from passed in string t_uid.  */

  printf("Starting uid malloc\n");
  uid = (char*) malloc(len_t_uid + 6 + 1); /* + 6 for uid= and
                                              ,' ' at the end. */

  if (uid == NULL){
    free(host);
    *t_result = 2100; /* could not allocate memory for uid string */
    return;
    }

  buffer = (char*) malloc(len_t_uid + 1);
  if (buffer == NULL){
    free(host);
    free(uid);
    *t_result = 2200; /* could not allocate memory for uid buffer */
    return;
    }

  strncat(buffer,t_uid,len_t_uid);

  ptr = strtok(buffer,", ");

  strcpy(uid,"uid=\0");
  strcat(uid,ptr);

  ptr = strtok(NULL,", ");

  if (ptr != NULL){
    free(host);
    free(uid);
    free(buffer);
    *t_result = 2300; /* uid cannot have multiple values */
    return;
    }
  strcat(uid,", \0");
  free(buffer);

/* Build ou string from passed in string t_ou.  */

  printf("Starting ou malloc\n");
  ou = (char*) malloc(len_t_ou + 1);
  if (ou == NULL){
    free(host);
    free(uid);
    *t_result = 3100; /* could not allocate memory for ou string */
    return;
    }

  strncat(ou,t_ou,len_t_ou);

  ptr = strtok(ou,", ");
  num_tokens = 0;

  while (ptr!=NULL){
    num_tokens++;
    ptr=strtok(NULL,", ");
    }

  free(ou); /* recreate ou with enough room for ou=, . */

  ou = (char*) malloc(len_t_ou + num_tokens*5 + 1); /* + num_tokens*5
                                                       for ou=$,' ' */
  if (ou == NULL){
    free(host);
    free(uid);
    *t_result = 3110; /* could not allocate memory for ou string */
    return;
    }

  buffer = (char*) malloc(len_t_ou + 1);
  if (buffer == NULL){
    free(host);
    free(uid);
    free(ou);
    *t_result = 3200; /* could not allocated memory for ou buffer */
    return;
    }

  strncat(buffer,t_ou,len_t_ou);

  strcpy(ou,"ou=\0");
  ptr = strtok(buffer,", ");
  strcat(ou,ptr);

  while (ptr!=NULL){
    ptr=strtok(NULL,", ");
    if (ptr!=NULL){
      strcat(ou,", ou=\0");
      strcat(ou,ptr);
      }
    }
  strcat(ou,", \0");
  free(buffer);

/* Build o string from passed in string t_o. */

  printf("Starting o malloc\n");
  o = (char*) malloc (len_t_o + 2 + 1); /* + 2 for o= */
  if (o == NULL){
    free(host);
    free(uid);
    free(ou);
    *t_result = 4100; /* could not allocate memory for o string. */
    return;
    }

  buffer = (char*) malloc (len_t_o + 1);
  if (buffer == NULL){
    free(host);
    free(uid);
    free(ou);
    free(o);
    *t_result = 4200; /* could not allocate memory for o buffer. */
    return;
    }

  strncat(buffer,t_o,len_t_o);

  strcpy(o,"o=\0");
  ptr = strtok(buffer,", ");
  strcat(o,ptr);
  ptr = strtok(NULL,", ");

  if (ptr != NULL){
    free(host);
    free(uid);
    free(ou);
    free(o);
    free(buffer);
    *t_result = 4300; /* o cannot have multiple values. */
    return;
    }
  free(buffer);

/* build pw string from passed in string t_pw */

  printf("Starting pw malloc\n");
  pw = (char*) malloc(len_t_pw);
  if (pw == NULL){
    free(host);
    free(uid);
    free(ou);
    free(o);
    *t_result = 4400; /* could not allocate memory for pw string */
    return;
    }

  strcpy(pw,t_pw);

  printf("Starting dn malloc\n");

  dn = (char*) malloc((int)strlen(uid)+(int)strlen(ou)+(int)strlen(o));
  if (dn == NULL){
    free(host);
    free(uid);
    free(ou);
    free(o);
    *t_result = 4500; /* could not allocate memory for dn string */
    return;
    }

  strcat(dn,uid);
  strcat(dn,ou);
  strcat(dn,o);

  free(uid);
  free(ou);
  free(o);

/* Get a handle to an LDAP connection. */

  printf("Get an LDAP handle\n%s,%d\n",host,LDAP_PORT);

  ld = ldap_init(host,LDAP_PORT);
  printf("ld = %p\n",ld);
  if (ld == NULL){
    printf("free in ld == NULL\n");
    free(host);
    free(pw);
    free(dn);
    printf("Setting t_result currently %d\n",*t_result);
    *t_result=4600; /* could no create a handle to LDAP */
    printf("t_result now %d\n",*t_result);
    return;
    }

/* Bind to the LDAP server via authentication */

  printf("Bind to LDAP\n");

  rc = ldap_simple_bind_s(ld,dn,pw);
  if (rc != LDAP_SUCCESS){
    ldap_unbind(ld);
    free(host);
    free(pw);
    free(dn);
    *t_result=rc;
    return;
    }

  ldap_unbind( ld );
  free(host);
  free(pw);
  free(dn);
  *t_result = 0; /* Successfull authentication */
  return;
 }

int ffs(i)
{
   static unsigned char table[] =
     {
       0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
     };
   unsigned long int a;
   unsigned long int x = i & -i;

   a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ?  16 : 24);

   return table[x >> a] + a;
}
<--- End Code --->

:LINKEDIT
HP Link Editor/iX (HP30315A.06.17) Copyright Hewlett-Packard Co 1986


LinkEd> ALTPROG PROG=LDAPTEST;XL=LDAPAUTH.PUB.SHEEHAN

LinkEd> EXIT

:LDAPTEST
beau2.isu.edu,13
admtest,7
People,6
isu.edu,7
12testadm,9
0
Starting host malloc
Starting uid malloc
Starting ou malloc
Starting o malloc
Starting pw malloc
Starting dn malloc
Get an LDAP handle
beau2.isu.edu,389
**** Illegal data address (TRAPS 65).

ABORT: LDAPTEST.PUB.SHEEHAN
NM USER  11c.000a9468 _malloc+$32c
NM USER  1c.00142a34 nsldapi_malloc+$34
Program terminated in an error state. (CIERR 976)
:


Gavin Scott wrote:
>Here's where I stand right now.
>From your earlier XL listing:
>
>LinkEd> listxl
>
>*** start of LISTXL>
>    end of LISTXL annotated by ***
>
>LIBRARY NAME   : LDAPXL
>XL LIST        : /usr/lib/libldap.sl,/usr/lib/liblber.sl,/lib/libc.sl
>VERSION        : 85082112
>MODULE COUNT   : 2
>MODULE LIMIT   : 500
>
>Shows that gcc built the XL with dependent libraries for
>/usr/lib/libldap.sl, etc.  I would expect these functions would be in >this
>library.
>
>Can you do a LISTXL on /usr/lib/libldap.sl to confirm that the library is
>there and see if it contains the missing functions?
>
>Usually your own code will include some header file (ldap.h for example)
>that declares all of the functions for you.  Even if you don't declare >them,
>it will generally find them if they are available in the XL LIST for the
>program (though the calling sequence might not be correct). So if you >forget
>to include the header file, you *generally* won't get unresolved >externals
>(though it's possible that a header file would be redefining the names >you
>call into other internal names).
>
>You also might try writing a simple C test program to call your XL >routine
>rather than using Quick, since Quick has its own XL file(s) and might be
>complicating things.
>
>Gavin


Dave Waroff wrote:
>
> Strictly, all externals must be declared. The LDAP library
> should have a header file with all of the library functions
> declared. However, the unresolveds in your message look
> like internal, low-level functions; perhaps part of LDAP
> didn't make it into the XL.
>
> > -----Original Message-----
> > From: Richard Sheehan [mailto:[log in to unmask]]
> > Sent: Thursday, October 25, 2001 6:01 PM
> > To: [log in to unmask]
> > Subject: Re: Help: Build an XL containing C LDAP subroutines
> >
> >
> > Thanks again Gavin.
> >
> > Get rid of one, get a couple more
> >
> > I included the ffs() implementation you gave.
> >
> > Now I'm getting these -- Do I need to declare each of the
> > functions I'm
> > using as external? wouldn't this be resolved by the compiler?
> >
> > quick auto=testpass xl=ldapxl.richard.hp967
> >
> > UNRESOLVED EXTERNALS: ldap_unbind  (LDRERR 512)
> > UNRESOLVED EXTERNALS: ldap_init  (LDRERR 512)
> > UNRESOLVED EXTERNALS: ldap_simple_bind_s  (LDRERR 512)
> > run QUICK.CURRENT.COGNOS;INFO="AUTO=testpass ";
> > XL="ldapxl.richard.hp967
> > *PHLIBX
> > L "; PRI=DS
> > Unable to load program to be run. (CIERR 625)
> > HP3: /
> >
> >
> > --
> > Richard Sheehan,
> > Administrative Systems - IT Programmer Analyst
> > Idaho State University Computing & Communications
> > Campus Box 8037, Pocatello, ID 83209-8037
> > Phone: 208.282.3861 - Fax: 208.282.3673
> > Email: [log in to unmask]
> >
> > * To join/leave the list, search archives, change list settings, *
> > * etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
> >

--
Richard Sheehan,
Administrative Systems - IT Programmer Analyst
Idaho State University Computing & Communications
Campus Box 8037, Pocatello, ID 83209-8037
Phone: 208.282.3861 - Fax: 208.282.3673
Email: [log in to unmask]

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2