| 1 | /* pam-test.c - simple PAM authentication test program |
| 2 | Copyright (C) 2007, 2009 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Poldi is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <security/pam_appl.h> |
| 23 | #include <security/pam_misc.h> |
| 24 | #include <ctype.h> |
| 25 | #include <unistd.h> |
| 26 | #include <getopt.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | |
| 31 | |
| 32 | #define PROGRAM_NAME "pam-test" |
| 33 | #define PROGRAM_VERSION "0.2" |
| 34 | |
| 35 | /* Use the standard conversation function from libpam-misc. */ |
| 36 | static struct pam_conv conv = |
| 37 | { |
| 38 | misc_conv, |
| 39 | NULL |
| 40 | }; |
| 41 | |
| 42 | static void |
| 43 | print_help (void) |
| 44 | { |
| 45 | printf ("\ |
| 46 | Usage: %s [options] <PAM service name>\n\ |
| 47 | Test PAM authentication.\n\ |
| 48 | \n\ |
| 49 | Options:\n\ |
| 50 | -h, --help print help information\n\ |
| 51 | -v, --version print version information\n\ |
| 52 | -u, --username specify username for authentication\n\ |
| 53 | \n\ |
| 54 | Report bugs to <[email protected]>.\n", PROGRAM_NAME); |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | print_version (void) |
| 59 | { |
| 60 | printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | test_auth (const char *servicename, const char *username) |
| 65 | { |
| 66 | const void *user_opaque; |
| 67 | const char *user; |
| 68 | pam_handle_t *handle; |
| 69 | int rc; |
| 70 | |
| 71 | /* Connect to PAM. */ |
| 72 | rc = pam_start (servicename, username, &conv, &handle); |
| 73 | if (rc != PAM_SUCCESS) |
| 74 | { |
| 75 | fprintf (stderr, "error: %s\n", pam_strerror (handle, rc)); |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | /* Try authentication. */ |
| 80 | rc = pam_authenticate (handle, 0); |
| 81 | if (rc != PAM_SUCCESS) |
| 82 | { |
| 83 | printf ("Authentication failed\n"); |
| 84 | fprintf (stderr, "error: %s\n", pam_strerror (handle, rc)); |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | printf ("Authentication succeeded\n"); |
| 89 | |
| 90 | /* Retrieve name of authenticated identity. */ |
| 91 | rc = pam_get_item (handle, PAM_USER, &user_opaque); |
| 92 | if (rc != PAM_SUCCESS) |
| 93 | { |
| 94 | fprintf (stderr, "error: %s\n", pam_strerror (handle, rc)); |
| 95 | goto out; |
| 96 | } |
| 97 | user = user_opaque; |
| 98 | printf ("Authenticated as user `%s'\n", user); |
| 99 | |
| 100 | /* Disconnect from PAM. */ |
| 101 | rc = pam_end (handle, rc); |
| 102 | if (rc != PAM_SUCCESS) |
| 103 | fprintf (stderr, "error: failed to release PAM handle\n"); |
| 104 | |
| 105 | out: |
| 106 | |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* This is a simple test program for PAM authentication. */ |
| 111 | int |
| 112 | main (int argc, char **argv) |
| 113 | { |
| 114 | const char *servicename; |
| 115 | const char *username; |
| 116 | int c; |
| 117 | |
| 118 | servicename = username = NULL; |
| 119 | |
| 120 | while (1) |
| 121 | { |
| 122 | static struct option long_options[] = |
| 123 | { |
| 124 | { "version", no_argument, 0, 'v' }, |
| 125 | { "help", no_argument, 0, 'h' }, |
| 126 | { "user", required_argument, 0, 'u' }, |
| 127 | { 0, 0, 0, 0 } |
| 128 | }; |
| 129 | int option_index = 0; |
| 130 | |
| 131 | c = getopt_long (argc, argv, "vhu:", |
| 132 | long_options, &option_index); |
| 133 | |
| 134 | /* Detect the end of the options. */ |
| 135 | if (c == -1) |
| 136 | break; |
| 137 | |
| 138 | switch (c) |
| 139 | { |
| 140 | case 'u': |
| 141 | username = strdup (optarg); |
| 142 | if (!username) |
| 143 | { |
| 144 | fprintf (stderr, "failed to duplicate username: %s", strerror (errno)); |
| 145 | exit (1); |
| 146 | } |
| 147 | break; |
| 148 | |
| 149 | case 'h': |
| 150 | print_help (); |
| 151 | exit (0); |
| 152 | break; |
| 153 | |
| 154 | case 'v': |
| 155 | print_version (); |
| 156 | exit (0); |
| 157 | break; |
| 158 | |
| 159 | case '?': |
| 160 | /* `getopt_long' already printed an error message. */ |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | abort (); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (argc - optind != 1) |
| 169 | { |
| 170 | print_help (); |
| 171 | exit (1); |
| 172 | } |
| 173 | |
| 174 | servicename = argv[optind]; |
| 175 | test_auth (servicename, username); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* end */ |