Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to Write a PAM Enabled "Hello world" program ; Help please

Hi All,

I have tried to read some documentation regarding PAM and found many resources that describe it in detail but fail to give one
simple/small example to demonstrate how to use PAM in an
application.

For Example if i want to write a "Hello World" program that needs user to supply his password when the program is run for authentication ;
how do i write it ?

I tried the below it did not work...
=======================

/* main.c */

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

printf("Hello World in Linux
" ;

return(0) ;

}

=======================

$gcc -o testhello main.c -lpam

=======================

added the testhello binary to /bin and created /etc/pam.d/testhello file with config lines same as that of passwd service.

======================

ran the testhello service.It did not work.It executes with out asking the password.

=======================

Comments

  • akalexi312akalexi312 Member Posts: 31
    I have finally solved this issue.

    First thing my code snippet i put below was absolutely incomplete.
    It was not at all PAM Aware.I needed to use pam header files and
    Link pam and pam_misc shared libs while compiling.

    Here is the code that clicked.The file name i used was checkuser.c
    compile it using gcc -o checkuser checkuser.c -lpam -lpam_misc

    -------------CODE START-------------------------------------
    /*
    This program was contributed by Shane Watts
    [modifications by AGM]

    You need to add the following (or equivalent) to the /etc/pam.conf
    file.


    #include
    #include
    #include

    static struct pam_conv conv = {
    misc_conv,
    NULL
    };

    int main(int argc, char *argv[])
    {
    pam_handle_t *pamh=NULL;
    int retval;
    const char *user="nobody";

    if(argc == 2) {
    user = argv[1];
    }

    if(argc > 2) {
    fprintf(stderr, "Usage: check_user [username]
    ");
    exit(1);
    }

    retval = pam_start("check_user", user, &conv, &pamh);

    if (retval == PAM_SUCCESS)
    retval = pam_authenticate(pamh, 0); /* is user really user? */

    if (retval == PAM_SUCCESS)
    retval = pam_acct_mgmt(pamh, 0); /* permitted access? */

    /* This is where we have been authorized or not. */

    if (retval == PAM_SUCCESS) {
    fprintf(stdout, "Authenticated
    ");
    } else {
    fprintf(stdout, "Not Authenticated
    ");
    }

    if (pam_end(pamh,retval) != PAM_SUCCESS) { /* close Linux-PAM */
    pamh = NULL;
    fprintf(stderr, "check_user: failed to release authenticator
    ");
    exit(1);
    }

    return ( retval == PAM_SUCCESS ? 0:1 ); /* indicate success */
    }

    -------------CODE END-------------------------


    ======================================================
    : Hi All,
    :
    : I have tried to read some documentation regarding PAM and found many resources that describe it in detail but fail to give one
    : simple/small example to demonstrate how to use PAM in an
    : application.
    :
    : For Example if i want to write a "Hello World" program that needs user to supply his password when the program is run for authentication ;
    : how do i write it ?
    :
    : I tried the below it did not work...
    : =======================
    :
    : /* main.c */
    :
    : Main(int argc,char *argv[]) {
    :
    : printf("Hello World in Linux
    " ;
    :
    : return(0) ;
    :
    : }
    :
    : =======================
    :
    : $gcc -o testhello main.c -lpam
    :
    : =======================
    :
    : added the testhello binary to /bin and created /etc/pam.d/testhello file with config lines same as that of passwd service.
    :
    : ======================
    :
    : ran the testhello service.It did not work.It executes with out asking the password.
    :
    : =======================
    :
    :

Sign In or Register to comment.