Okay, so it's been quite a number of years since I've done any C (college?), but I'm a fairly decent programmer in other languages (Java, PHP, Perl, etc.).
I'm writing a C module for AIX to be used when a user changes his/her password. It's referred to as the "Password check method" or "pwdcheck" setting for the system.
I customized example C code for this module to make it do what I wanted (check to make sure the password contains characters from three of the four character types). Here is the code:
Now, when I compile this module (cc -e pwdrestrict_method -o pwdrestrict pwdrestrict.c), copy the compiled executable to /usr/lib/pwdrestrict and point a test user's pwdcheck property to it, when I change that user's password, here is the behavior I get:Code:#include <string.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <ctype.h>
#define errDiff "Your password must contain at least one character of 3 of the 4 types: lower, upper, digit and special character\n"
int pwdrestrict_method (UserName, NewPassword, OldPassword, Message)
char * UserName;
char * NewPassword;
char * OldPassword;
char ** Message;
{
char * localMessage;
int charCnt = 0;
int i = 0;
int lCnt = 0, uCnt = 0, dCnt = 0, pCnt = 0, cTypes = 0;
charCnt = strlen(NewPassword);
for (i=0; i<charCnt; i++) {
if (islower(NewPassword[i]))
lCnt++;
else if (isupper(NewPassword[i]))
uCnt++;
else if (isdigit(NewPassword[i]))
dCnt++;
else if (ispunct(NewPassword[i]))
pCnt++;
}
printf("NewPassword=%s lCnt=%d uCnt=%d dCnt=%d pCnt=%d charCnt=%d\n",NewPassword,lCnt,uCnt,dCnt,pCnt,charCnt);
cTypes = (lCnt!=0) + (uCnt!=0) + (dCnt!=0) + (pCnt!=0);
printf("cTypes=%d\n",cTypes);
if (cTypes < 3) {
localMessage = (char *) malloc(strlen(errDiff+1));
strcpy(localMessage, errDiff);
*Message = localMessage;
errno = EPERM;
return 1;
}
Message = NULL;
return 0;
}
If the first password typed doesn't meet the requirements (say, "12345"), the error message "errDiff" is displayed to the user and they are re-prompted to choose another password.
If the second password typed meets the requirements, they are prompted to enter the password twice and the password is set successfully.
If the first password typed meets the requirements, I get a segfault and the password prompt exits (obviously).
All subsequent runs of the passwd command "reset" this behavior.
I am using IBM's xlC compiler to compile this code, and I don't know if they have a debugger like the GNU C compiler has (which I don't really know how to use anyway, but I found plenty of how-tos out there for).
So can anyone please help me identify where my problem might be? Or tell me some way to debug this issue?
I've tried adding the debug output (printf) statements in there to watch what the variables are doing, and I've tried commenting out various chunks of code to use process of elimination to determine where the segfault is coming from, but it seems to happen no matter what I leave it or leave out...
Thanks in advance for any and all help!
Regards,
Jon Heese