]> granicus.if.org Git - linux-pam/blob - examples/test.c
Relevant BUGIDs: 433460
[linux-pam] / examples / test.c
1 /*
2  * Marc Ewing (marc@redhat.com) - original test code 
3  * Alexander O. Yuriev (alex@bach.cis.temple.edu)
4  * Andrew Morgan (morgan@physics.ucla.edu)
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <pwd.h>
10
11 #include <security/pam_appl.h>
12
13 /* this program is not written to the PAM spec: it tests the
14  * pam_[sg]et_data() functions. Which is usually reserved for modules */
15
16 #include <security/pam_modules.h>
17 #include <security/pam_misc.h>
18
19 #define USERNAMESIZE    1024
20
21 static int test_conv(   int num_msg, 
22                         const struct pam_message **msgm,
23                         struct pam_response **response, 
24                         void *appdata_ptr       )
25 {
26     return 0;
27 }
28
29 static struct pam_conv conv = {
30     test_conv,
31     NULL
32 };
33
34 static int cleanup_func(pam_handle_t *pamh, void *data, int error_status)
35 {
36     printf("Cleaning up!\n");
37     return PAM_SUCCESS;
38 }
39
40 void main( void )
41 {
42         pam_handle_t *pamh;
43         char    *name = ( char *) malloc( USERNAMESIZE + 1 );
44         char    *p = NULL; 
45         char    *s = NULL;
46
47         if (! name )
48                 {
49                         perror( "Ouch, don't have enough memory");
50                         exit( -1 );
51                 }
52     
53
54
55
56         fprintf( stdout, "Enter a name of a user to authenticate : ");
57         name = fgets( name , USERNAMESIZE, stdin );
58         if ( !name )
59                 {
60                         perror ( "Hey, how can authenticate "
61                                  "someone whos name I don't know?" );
62                         exit ( -1 );
63                 }
64         
65         *( name + strlen ( name ) - 1 ) = 0;
66
67         pam_start( "login", name, &conv, &pamh  );
68
69         p = x_strdup( getpass ("Password: ") );
70         if ( !p )
71                 {
72                         perror ( "You love NULL pointers, "
73                                  "don't you? I don't ");
74                         exit ( -1 );
75                 }
76         pam_set_item ( pamh, PAM_AUTHTOK, p );
77         pam_get_item ( pamh, PAM_USER, (void**) &s);
78         pam_set_data(pamh, "DATA", "Hi there! I'm data!", cleanup_func);
79         pam_get_data(pamh, "DATA", (void **) &s);
80         printf("%s\n", s);
81
82         fprintf( stdout, "*** Attempting to perform "
83                  "PAM authentication...\n");
84         fprintf( stdout, "%s\n",
85                  pam_strerror( pam_authenticate( pamh, 0 ) ) ) ;
86     
87         pam_end(pamh, PAM_SUCCESS);
88 }