LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (https://www.linuxquestions.org/questions/linux-security-4/)
-   -   In C, given userID and password, can I verify the user? (https://www.linuxquestions.org/questions/linux-security-4/in-c-given-userid-and-password-can-i-verify-the-user-4175554650/)

mfoley 09-27-2015 12:46 PM

In C, given userID and password, can I verify the user?
 
I have a C program used for authentication with an email server. The server passes the C program the username and password. Is there a function I can call, e.g. verify_user(username, password)?

mfoley 09-27-2015 01:18 PM

More info: I've come across this link http://www.tldp.org/HOWTO/Shadow-Password-HOWTO-8.html which may or may not be what I am looking for. But, I do not have libshadow.a on my system, nor referenced headers like shadow/pwauth.h, but I do have shadow.h

I'm using Slackware64 14.1. The system says package shadow-4.1.5.1-x86_64-3_slack14.1 is installed.

Need help!

mfoley 09-28-2015 12:52 PM

I figured it out. The following example assumes argv[1] is the username and argv[2] is the plain-text password to authenticate. Note that access to /etc/shadow can only be done as root. Compile the program below with:

$ gcc testauth.c -o testauth -lcrypt

Code:

/*
 * Print information from the password entry
 * about the user name given as argv[1].
 */

int main( int argc, char** argv )
{
    char        *epasswd;

    struct spwd* sp;

        if (argc < 2) {
                printf("s username \n", argv[0]);
                return(EXIT_FAILURE);
        }

    if( ( sp = getspnam( argv[1] ) ) == (struct spwd*)0) {
      fprintf( stderr, "getspnam: unknown s\n",
        argv[1] );
      return( EXIT_FAILURE );
    }
    printf( "login name\t%s\n", sp->sp_namp );
    printf( "password\t%s\n", sp->sp_pwdp );

epasswd = crypt(argv[2], sp->sp_pwdp);
printf("crypt:\t\t%s, %d\n", epasswd, strcmp(epasswd, sp->sp_pwdp));
    return( EXIT_SUCCESS );
}



All times are GMT -5. The time now is 08:27 AM.