Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dr. Watson

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
I have a program that is occassionally getting Dr. Watson errors: Exception: access violation (0xc0000005, Address: 0x00401948.

I know this is a lot to ask, but I've included the c code below. Can anyone see anything obvious that could be causing this?

Thanks,
James


/* File: arealdap.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <ldap.h>

#include &quot;area.h&quot;

/*****************************************************************************/
/* */
/* Action Request System External Authentication (AREA) Sample */
/* */
/*****************************************************************************/
/* Description: This is a sample external authentication server based on */
/* LDAP. */
/* */
/* This example uses the Netscape LDAP SDK available from Netscape */
/* Communications Corporation. Schema and attribute names reflect those */
/* commonly found on a Netscape Directory Server. Your own configuration */
/* may differ. */
/* */
/* Possible enhancements to this example: */
/* */
/* + Cache user and/or group membership information so that we do not */
/* have to go to the LDAP server for every request. */
/* */
/* + and/or, be smarter about when to flush the AR Server's cache of user*/
/* user information. */
/* */
/* + Generalize to work with other directory configurations and schemas. */
/* */
/*****************************************************************************/

#define MAX_HOST_LEN 50
#define MAX_PEOPLEBASE_LEN 25
#define MAX_GROUPBASE_LEN 25

char MY_HOST[MAX_HOST_LEN];
char temp_port[10];
int MY_PORT;
char MY_PEOPLEBASE[MAX_PEOPLEBASE_LEN];
char MY_GROUPBASE[MAX_GROUPBASE_LEN];
FILE *fp, *ff;
struct tm *local;
time_t t;
char file_name[80], date[80], month[80], day[80], systime[80], year[80], form_date[80],temp[80];
char date1, date2, date3, date4, date5, date6, date7, date8;

/*****************************************************************************/
/* */
/* AREAInitializationCallback */
/* */
/*****************************************************************************/
/* Description: This function is called by main() which is defined in the */
/* Action Request System External Authentication (AREA) core library. */
/* */
/* A customized authentication server must define this function. Any */
/* global information or resources should be initialized here. */
/* */
/* A return value of zero signifies success. A non-zero return value */
/* will cause the process to exit() with the return value as the exit */
/* code. */
/* */
/*****************************************************************************/

int AREAInitializationCallback(
int argc,
char *argv[]
)
{

//get time
t = time(NULL);
local = localtime(&t);
strcpy(date, asctime(local));
sscanf(date, &quot;%*s %s %s %s %s&quot;, month, day, systime, year);
sscanf(systime, &quot;%c%c%c%c%c%c%c%c&quot;, &date1, &date2, &date3, &date4, &date5, &date6, &date7, &date8);
systime[0] = date1;
systime[1] = date2;
systime[2] = date4;
systime[3] = date5;
systime[4] = date7;
systime[5] = date8;
systime[6] = 0;

if (strcmp(month, &quot;Jan&quot;) == 0)
{
strcpy(month, &quot;01&quot;);
}
else if (strcmp(month, &quot;Feb&quot;) == 0)
{
strcpy(month, &quot;02&quot;);
}
if (strcmp(month, &quot;Mar&quot;) == 0)
{
strcpy(month, &quot;03&quot;);
}
else if (strcmp(month, &quot;Apr&quot;) == 0)
{
strcpy(month, &quot;04&quot;);
}
if (strcmp(month, &quot;May&quot;) == 0)
{
strcpy(month, &quot;05&quot;);
}
else if (strcmp(month, &quot;Jun&quot;) == 0)
{
strcpy(month, &quot;06&quot;);
}
if (strcmp(month, &quot;Jul&quot;) == 0)
{
strcpy(month, &quot;07&quot;);
}
else if (strcmp(month, &quot;Aug&quot;) == 0)
{
strcpy(month, &quot;08&quot;);
}
else if (strcmp(month, &quot;Sep&quot;) == 0)
{
strcpy(month, &quot;09&quot;);
}
else if (strcmp(month, &quot;Oct&quot;) == 0)
{
strcpy(month, &quot;10&quot;);
}
if (strcmp(month, &quot;Nov&quot;) == 0)
{
strcpy(month, &quot;11&quot;);
}
else if (strcmp(month, &quot;Dec&quot;) == 0)
{
strcpy(month, &quot;12&quot;);
}

if (strlen(day) == 1) /* If the day is already two characters, no need to do anything. */
{
strcpy(temp, &quot;0&quot;); /* add the leading &quot;0&quot; */
strcat(temp, day); /* put on the single digit day */
strcpy(day, temp); /* copy the two digit version back in the original */
}

strcpy(file_name, &quot;arealdap.log.&quot;);
strcat(file_name, month);
strcat(file_name, day);
strcat(file_name, year);
strcat(file_name, &quot;-&quot;);
strcat(file_name, systime);
strcat(file_name, &quot;.log&quot;);

/* See if the file arealdap.log exists, without modifying it */
if ((fp = fopen(&quot;arealdap.log&quot;, &quot;r&quot;)) == NULL)
{
/* File doesn't exist, open it for writing */
if ((fp = fopen(&quot;arealdap.log&quot;, &quot;w&quot;)) == NULL)
{
/* File didn't open correctly due to an error */

/* Do any error handling here */
}
else
{
/* File opened successfully for writing */
}
}
else
{
/* File exists. Close it, since we want to rename it. */
fclose(fp);

/* rename the old file */

rename(&quot;arealdap.log&quot;, file_name);

/* Open a new file for writing */
if ((fp = fopen(&quot;arealdap.log&quot;, &quot;w&quot;)) == NULL)
{
/* File didn't open correctly due to an error */
}
else
{
/* File opened successfully for writing */
}
}
if (argc != 4)
{
printf(&quot;Usage: arealdap [LDAP HOST] [LDAP PORT] [BASE DN]\n&quot;);
fprintf(fp,&quot;Parameters not found. \n&quot;);
exit(1);
}

MY_HOST[0] = '\0';
MY_PEOPLEBASE[0] = '\0';
strcpy(MY_HOST, argv[1]);
MY_PORT = atoi(argv[2]);
strcpy(MY_PEOPLEBASE, argv[3]);

fprintf(fp,&quot;Log started %s&quot;,date);
fprintf(fp,&quot;MY_HOST is %s\n&quot;, MY_HOST);
fprintf(fp,&quot;MY_PORT is %d\n&quot;, MY_PORT);
fprintf(fp,&quot;MY_PEOPLEBASE is %s\n&quot;, MY_PEOPLEBASE);
fclose(fp);
/* This sample has the data allocated and deallocated with every request */
/* from the AR System server. As such, we have nothing to do here. */

return 0;
}


/*****************************************************************************/
/* */
/* AREAVerifyLoginCallback */
/* */
/*****************************************************************************/
/* Description: This function is called by the AREA core library whenever a */
/* request is made by the AR System Server to authenticate a user. */
/* */
/* The basic idea is to authenticate (also known as &quot;binding&quot;) with the */
/* the LDAP server. If this is successful, we go on to get other */
/* information such as e-mail address and group membership. */
/* */
/*****************************************************************************/

void AREAVerifyLoginCallback(
ARNameType user,
ARNameType password,
ARNameType networkAddr,
AREAResponseStruct **response
)
{

LDAP *ld;
LDAPMessage *result, *e;
char *dn;
char *email;
char search[64];
char filter[256];
ARBoolean valid;
ARBoolean found;
static char *attrs[3] = {&quot;cn&quot;, &quot;uniquemember&quot;, NULL};

*response = (AREAResponseStruct *) malloc (sizeof(AREAResponseStruct));
if (*response == NULL)
return;

/* Initialize as a failed login */
(*response)->licenseMask = AREA_LICENSE_MASK_ALL;
(*response)->licenseWrite = AR_LICENSE_TYPE_NONE;
(*response)->licenseFTS = AR_LICENSE_TYPE_NONE;
(*response)->licenseRes1 = AR_LICENSE_TYPE_NONE;
(*response)->groups = NULL;
(*response)->notifyMech = AR_NOTIFY_NONE;
(*response)->email = NULL;
(*response)->loginStatus = AREA_LOGIN_FAILED;
(*response)->messageText = NULL;
(*response)->logText = NULL;
(*response)->modTime = 0;


fp = fopen(&quot;arealdap.log&quot;, &quot;a&quot;);
fprintf(fp,&quot;-------------------------------------------\n&quot;);
t = time(NULL);
local = localtime(&t);
fprintf(fp,&quot;%s&quot;,asctime(local));
fprintf(fp,&quot;Start to validate user: %s\n&quot;,user);

ld = ldap_init(MY_HOST, MY_PORT);
/* get a handle to an LDAP connection */
if (ld == NULL)
return;
else
fprintf (fp,&quot;Got handle to LDAP Connection.\n&quot;);

/*
* Authenticate to the directory to do the search.
*/
if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS)
return;
else
fprintf (fp,&quot;Bind to LDAP successful.\n&quot;);


sprintf(search, &quot;%s&quot;, MY_PEOPLEBASE);
sprintf(filter, &quot;(cn=%s)&quot;, user);
if (ldap_search_s(ld, search, LDAP_SCOPE_SUBTREE,
filter, NULL, 0, &result) != LDAP_SUCCESS)
return;
else
fprintf (fp,&quot;LDAP Search Successful.\n&quot;);

/*
* Run through the attributes of each entry looking for a match.
*/
valid = FALSE;
found = FALSE;
email = NULL;


for (e = ldap_first_entry(ld, result); e != NULL; e = ldap_next_entry(ld, e))
{
found = TRUE;
dn = ldap_get_dn(ld, e);
if (dn == NULL)
{
continue;
}
if ((dn[0] == '\0') || (password[0] == '\0'))
{
continue;
}

if (ldap_simple_bind_s(ld, dn, password) != LDAP_SUCCESS)
{
continue;
}
valid = TRUE;
}

ldap_unbind(ld);

if ((found == TRUE) && (valid == TRUE))
{
fprintf(fp,&quot;Found user and the password is good.\n&quot;);
/*
* We found a match and the password is good.
*/
(*response)->licenseMask = '\0';
(*response)->licenseWrite = '\0';
(*response)->licenseFTS = '\0';
(*response)->licenseRes1 = '\0';
(*response)->groups = NULL;
(*response)->notifyMech = '\0';
(*response)->email = NULL;
(*response)->loginStatus = AREA_LOGIN_SUCCESS;
(*response)->messageText = NULL;
}
else if (found == TRUE)
{
fprintf(fp, &quot;User found, but password is bad.\n&quot;);
/*
* We found a match and the password is bad.
*/
(*response)->loginStatus = AREA_LOGIN_FAILED;
(*response)->messageText = NULL;
(void) free (email);
}
else
{
fprintf(fp,&quot;User Not Found.\n&quot;);
/*
* Otherwise, we do not know the user.
*/
(*response)->loginStatus = AREA_LOGIN_UNKNOWN_USER;
(*response)->messageText = NULL;
(void) free (email);
}

fclose(fp);
return;
}


/*****************************************************************************/
/* */
/* AREANeedToSyncCallback */
/* */
/*****************************************************************************/
/* Description: This function is called by the AREA core library */
/* periodically at the request of the AR System server. */
/* */
/* A customized authentication server must define this function. Any */
/* global information or resources accessed here must be protected with */
/* appropriate mutual exclusion locks to be multi-thread safe. */
/* */
/* A non-zero return value will instruct the AR System server to flush */
/* its cache of user information. */
/* */
/*****************************************************************************/

int AREANeedToSyncCallback(void)
{
return 0;
}


/*****************************************************************************/
/* */
/* AREAFreeCallback */
/* */
/*****************************************************************************/
/* Description: This function is called by the AREA core library after a */
/* response is made to the AR System Server to authenticate a user. */
/* */
/* A pointer to the response structure returned by */
/* AREAVerifyLoginCallback() is passed as a parameter. */
/* */
/* A customized authentication server must define this function. Any */
/* global information or resources accessed here must be protected with */
/* appropriate mutual exclusion locks to be multi-thread safe. */
/* */
/* A customized authentication server must return a response. A NULL */
/* response will be interpreted as a failed login attempt. */
/* */
/*****************************************************************************/

void AREAFreeCallback(
AREAResponseStruct *response
)
{
/* Since we allocated the response in AREAVerifyLoginCallback(), we */
/* should free it here. */

if (response != NULL)
{
if (response->email != NULL)
(void) free(response->email);
(void) free (response);
}

return;
}


/*****************************************************************************/
/* */
/* AREATerminationCallback */
/* */
/*****************************************************************************/
/* Description: This function is called by the AREA core library after a */
/* response is made to the AR System Server to authenticate a user. */
/* */
/* Resources created in AREAInitializationCallback may be destroyed here. */
/* */
/* A customized authentication server must define this function. Any */
/* global information or resources accessed here must be protected with */
/* appropriate mutual exclusion locks to be multi-thread safe. */
/* */
/*****************************************************************************/

void AREATerminationCallback(void)
{
/* We have nothing to do here since we did not allocate or create any */
/* global resources in AREAInitializationCallback(). */

return;
}
 
Woooow!
That's a lot of code to look at...
Why don't you use a debugger to find out where the problem occurs?

anyway, wirhout looking at all the code (sorry about this...) the problem is ussualy caused by overwriting memory with bad pointer arithmetics. You should check for your pointers.

You have a lot of statically allocated strings there also, maybe you exceed their size from time to time... From example, if you have:
Code:
char str[10];
strcpy(str,&quot;123456789101112131415&quot;);
you will get an access violation. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
I will try the debugging feature of my compiler and see if it shows anything. I don't think there's much chance that I'm exceeding any string limitations.

Thanks,
James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top